]>
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. |
4747a7d3 | 24 | * Copyright (c) 2011, 2017 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. |
0ea05c64 | 32 | * Copyright (c) 2017 Datto Inc. |
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> |
c28b2279 | 60 | #include <sys/vdev_disk.h> |
34dc7c2f | 61 | #include <sys/metaslab.h> |
428870ff | 62 | #include <sys/metaslab_impl.h> |
379ca9cf | 63 | #include <sys/mmp.h> |
34dc7c2f BB |
64 | #include <sys/uberblock_impl.h> |
65 | #include <sys/txg.h> | |
66 | #include <sys/avl.h> | |
a1d477c2 | 67 | #include <sys/bpobj.h> |
34dc7c2f BB |
68 | #include <sys/dmu_traverse.h> |
69 | #include <sys/dmu_objset.h> | |
70 | #include <sys/unique.h> | |
71 | #include <sys/dsl_pool.h> | |
72 | #include <sys/dsl_dataset.h> | |
73 | #include <sys/dsl_dir.h> | |
74 | #include <sys/dsl_prop.h> | |
75 | #include <sys/dsl_synctask.h> | |
76 | #include <sys/fs/zfs.h> | |
77 | #include <sys/arc.h> | |
78 | #include <sys/callb.h> | |
79 | #include <sys/systeminfo.h> | |
34dc7c2f | 80 | #include <sys/spa_boot.h> |
9babb374 | 81 | #include <sys/zfs_ioctl.h> |
428870ff | 82 | #include <sys/dsl_scan.h> |
9ae529ec | 83 | #include <sys/zfeature.h> |
13fe0198 | 84 | #include <sys/dsl_destroy.h> |
526af785 | 85 | #include <sys/zvol.h> |
34dc7c2f | 86 | |
d164b209 | 87 | #ifdef _KERNEL |
12fa0466 DE |
88 | #include <sys/fm/protocol.h> |
89 | #include <sys/fm/util.h> | |
428870ff | 90 | #include <sys/callb.h> |
d164b209 BB |
91 | #include <sys/zone.h> |
92 | #endif /* _KERNEL */ | |
93 | ||
34dc7c2f BB |
94 | #include "zfs_prop.h" |
95 | #include "zfs_comutil.h" | |
96 | ||
e6cfd633 WA |
97 | /* |
98 | * The interval, in seconds, at which failed configuration cache file writes | |
99 | * should be retried. | |
100 | */ | |
a1d477c2 | 101 | int zfs_ccw_retry_interval = 300; |
e6cfd633 | 102 | |
428870ff | 103 | typedef enum zti_modes { |
7ef5e54e | 104 | ZTI_MODE_FIXED, /* value is # of threads (min 1) */ |
7ef5e54e AL |
105 | ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */ |
106 | ZTI_MODE_NULL, /* don't create a taskq */ | |
107 | ZTI_NMODES | |
428870ff | 108 | } zti_modes_t; |
34dc7c2f | 109 | |
7ef5e54e AL |
110 | #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) } |
111 | #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 } | |
112 | #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 } | |
113 | #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 } | |
9babb374 | 114 | |
7ef5e54e AL |
115 | #define ZTI_N(n) ZTI_P(n, 1) |
116 | #define ZTI_ONE ZTI_N(1) | |
9babb374 BB |
117 | |
118 | typedef struct zio_taskq_info { | |
7ef5e54e | 119 | zti_modes_t zti_mode; |
428870ff | 120 | uint_t zti_value; |
7ef5e54e | 121 | uint_t zti_count; |
9babb374 BB |
122 | } zio_taskq_info_t; |
123 | ||
124 | static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { | |
451041db | 125 | "iss", "iss_h", "int", "int_h" |
9babb374 BB |
126 | }; |
127 | ||
428870ff | 128 | /* |
7ef5e54e AL |
129 | * This table defines the taskq settings for each ZFS I/O type. When |
130 | * initializing a pool, we use this table to create an appropriately sized | |
131 | * taskq. Some operations are low volume and therefore have a small, static | |
132 | * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE | |
133 | * macros. Other operations process a large amount of data; the ZTI_BATCH | |
134 | * macro causes us to create a taskq oriented for throughput. Some operations | |
135 | * are so high frequency and short-lived that the taskq itself can become a a | |
136 | * point of lock contention. The ZTI_P(#, #) macro indicates that we need an | |
137 | * additional degree of parallelism specified by the number of threads per- | |
138 | * taskq and the number of taskqs; when dispatching an event in this case, the | |
139 | * particular taskq is chosen at random. | |
140 | * | |
141 | * The different taskq priorities are to handle the different contexts (issue | |
142 | * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that | |
143 | * need to be handled with minimum delay. | |
428870ff BB |
144 | */ |
145 | const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { | |
146 | /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ | |
7ef5e54e | 147 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */ |
aa9af22c BB |
148 | { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */ |
149 | { ZTI_BATCH, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */ | |
150 | { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */ | |
7ef5e54e AL |
151 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */ |
152 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */ | |
9babb374 BB |
153 | }; |
154 | ||
13fe0198 MA |
155 | static void spa_sync_version(void *arg, dmu_tx_t *tx); |
156 | static void spa_sync_props(void *arg, dmu_tx_t *tx); | |
b128c09f | 157 | static boolean_t spa_has_active_shared_spare(spa_t *spa); |
d2734cce | 158 | static int spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport); |
572e2857 | 159 | static void spa_vdev_resilver_done(spa_t *spa); |
428870ff | 160 | |
e8b96c60 | 161 | uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */ |
428870ff BB |
162 | boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ |
163 | uint_t zio_taskq_basedc = 80; /* base duty cycle */ | |
164 | ||
165 | boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ | |
166 | ||
afd2f7b7 PZ |
167 | /* |
168 | * Report any spa_load_verify errors found, but do not fail spa_load. | |
169 | * This is used by zdb to analyze non-idle pools. | |
170 | */ | |
171 | boolean_t spa_load_verify_dryrun = B_FALSE; | |
172 | ||
428870ff BB |
173 | /* |
174 | * This (illegal) pool name is used when temporarily importing a spa_t in order | |
175 | * to get the vdev stats associated with the imported devices. | |
176 | */ | |
177 | #define TRYIMPORT_NAME "$import" | |
34dc7c2f | 178 | |
6cb8e530 PZ |
179 | /* |
180 | * For debugging purposes: print out vdev tree during pool import. | |
181 | */ | |
182 | int spa_load_print_vdev_tree = B_FALSE; | |
183 | ||
184 | /* | |
185 | * A non-zero value for zfs_max_missing_tvds means that we allow importing | |
186 | * pools with missing top-level vdevs. This is strictly intended for advanced | |
187 | * pool recovery cases since missing data is almost inevitable. Pools with | |
188 | * missing devices can only be imported read-only for safety reasons, and their | |
189 | * fail-mode will be automatically set to "continue". | |
190 | * | |
191 | * With 1 missing vdev we should be able to import the pool and mount all | |
192 | * datasets. User data that was not modified after the missing device has been | |
193 | * added should be recoverable. This means that snapshots created prior to the | |
194 | * addition of that device should be completely intact. | |
195 | * | |
196 | * With 2 missing vdevs, some datasets may fail to mount since there are | |
197 | * dataset statistics that are stored as regular metadata. Some data might be | |
198 | * recoverable if those vdevs were added recently. | |
199 | * | |
200 | * With 3 or more missing vdevs, the pool is severely damaged and MOS entries | |
201 | * may be missing entirely. Chances of data recovery are very low. Note that | |
202 | * there are also risks of performing an inadvertent rewind as we might be | |
203 | * missing all the vdevs with the latest uberblocks. | |
204 | */ | |
205 | unsigned long zfs_max_missing_tvds = 0; | |
206 | ||
207 | /* | |
208 | * The parameters below are similar to zfs_max_missing_tvds but are only | |
209 | * intended for a preliminary open of the pool with an untrusted config which | |
210 | * might be incomplete or out-dated. | |
211 | * | |
212 | * We are more tolerant for pools opened from a cachefile since we could have | |
213 | * an out-dated cachefile where a device removal was not registered. | |
214 | * We could have set the limit arbitrarily high but in the case where devices | |
215 | * are really missing we would want to return the proper error codes; we chose | |
216 | * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available | |
217 | * and we get a chance to retrieve the trusted config. | |
218 | */ | |
219 | uint64_t zfs_max_missing_tvds_cachefile = SPA_DVAS_PER_BP - 1; | |
d2734cce | 220 | |
6cb8e530 PZ |
221 | /* |
222 | * In the case where config was assembled by scanning device paths (/dev/dsks | |
223 | * by default) we are less tolerant since all the existing devices should have | |
224 | * been detected and we want spa_load to return the right error codes. | |
225 | */ | |
226 | uint64_t zfs_max_missing_tvds_scan = 0; | |
227 | ||
d2734cce SD |
228 | /* |
229 | * Debugging aid that pauses spa_sync() towards the end. | |
230 | */ | |
231 | boolean_t zfs_pause_spa_sync = B_FALSE; | |
232 | ||
34dc7c2f BB |
233 | /* |
234 | * ========================================================================== | |
235 | * SPA properties routines | |
236 | * ========================================================================== | |
237 | */ | |
238 | ||
239 | /* | |
240 | * Add a (source=src, propname=propval) list to an nvlist. | |
241 | */ | |
242 | static void | |
243 | spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, | |
244 | uint64_t intval, zprop_source_t src) | |
245 | { | |
246 | const char *propname = zpool_prop_to_name(prop); | |
247 | nvlist_t *propval; | |
248 | ||
79c76d5b | 249 | VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
250 | VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); |
251 | ||
252 | if (strval != NULL) | |
253 | VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); | |
254 | else | |
255 | VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); | |
256 | ||
257 | VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); | |
258 | nvlist_free(propval); | |
259 | } | |
260 | ||
261 | /* | |
262 | * Get property values from the spa configuration. | |
263 | */ | |
264 | static void | |
265 | spa_prop_get_config(spa_t *spa, nvlist_t **nvp) | |
266 | { | |
1bd201e7 | 267 | vdev_t *rvd = spa->spa_root_vdev; |
9ae529ec | 268 | dsl_pool_t *pool = spa->spa_dsl_pool; |
f3a7f661 | 269 | uint64_t size, alloc, cap, version; |
82ab6848 | 270 | const zprop_source_t src = ZPROP_SRC_NONE; |
b128c09f | 271 | spa_config_dirent_t *dp; |
f3a7f661 | 272 | metaslab_class_t *mc = spa_normal_class(spa); |
b128c09f BB |
273 | |
274 | ASSERT(MUTEX_HELD(&spa->spa_props_lock)); | |
34dc7c2f | 275 | |
1bd201e7 | 276 | if (rvd != NULL) { |
cc99f275 DB |
277 | alloc = metaslab_class_get_alloc(mc); |
278 | alloc += metaslab_class_get_alloc(spa_special_class(spa)); | |
279 | alloc += metaslab_class_get_alloc(spa_dedup_class(spa)); | |
280 | ||
281 | size = metaslab_class_get_space(mc); | |
282 | size += metaslab_class_get_space(spa_special_class(spa)); | |
283 | size += metaslab_class_get_space(spa_dedup_class(spa)); | |
284 | ||
d164b209 BB |
285 | spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); |
286 | spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); | |
428870ff BB |
287 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); |
288 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, | |
289 | size - alloc, src); | |
d2734cce SD |
290 | spa_prop_add_list(*nvp, ZPOOL_PROP_CHECKPOINT, NULL, |
291 | spa->spa_checkpoint_info.sci_dspace, src); | |
1bd201e7 | 292 | |
f3a7f661 GW |
293 | spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL, |
294 | metaslab_class_fragmentation(mc), src); | |
295 | spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, | |
296 | metaslab_class_expandable_space(mc), src); | |
572e2857 BB |
297 | spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, |
298 | (spa_mode(spa) == FREAD), src); | |
d164b209 | 299 | |
428870ff | 300 | cap = (size == 0) ? 0 : (alloc * 100 / size); |
d164b209 BB |
301 | spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); |
302 | ||
428870ff BB |
303 | spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, |
304 | ddt_get_pool_dedup_ratio(spa), src); | |
305 | ||
d164b209 | 306 | spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, |
1bd201e7 | 307 | rvd->vdev_state, src); |
d164b209 BB |
308 | |
309 | version = spa_version(spa); | |
82ab6848 HM |
310 | if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) { |
311 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
312 | version, ZPROP_SRC_DEFAULT); | |
313 | } else { | |
314 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, | |
315 | version, ZPROP_SRC_LOCAL); | |
316 | } | |
a448a255 SD |
317 | spa_prop_add_list(*nvp, ZPOOL_PROP_LOAD_GUID, |
318 | NULL, spa_load_guid(spa), src); | |
d164b209 | 319 | } |
34dc7c2f | 320 | |
9ae529ec | 321 | if (pool != NULL) { |
9ae529ec CS |
322 | /* |
323 | * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, | |
324 | * when opening pools before this version freedir will be NULL. | |
325 | */ | |
fbeddd60 | 326 | if (pool->dp_free_dir != NULL) { |
9ae529ec | 327 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, |
d683ddbb JG |
328 | dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes, |
329 | src); | |
9ae529ec CS |
330 | } else { |
331 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, | |
332 | NULL, 0, src); | |
333 | } | |
fbeddd60 MA |
334 | |
335 | if (pool->dp_leak_dir != NULL) { | |
336 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL, | |
d683ddbb JG |
337 | dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes, |
338 | src); | |
fbeddd60 MA |
339 | } else { |
340 | spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, | |
341 | NULL, 0, src); | |
342 | } | |
9ae529ec CS |
343 | } |
344 | ||
34dc7c2f | 345 | spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); |
34dc7c2f | 346 | |
d96eb2b1 DM |
347 | if (spa->spa_comment != NULL) { |
348 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, | |
349 | 0, ZPROP_SRC_LOCAL); | |
350 | } | |
351 | ||
34dc7c2f BB |
352 | if (spa->spa_root != NULL) |
353 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, | |
354 | 0, ZPROP_SRC_LOCAL); | |
355 | ||
f1512ee6 MA |
356 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { |
357 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
358 | MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE); | |
359 | } else { | |
360 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL, | |
361 | SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE); | |
362 | } | |
363 | ||
50c957f7 NB |
364 | if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) { |
365 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
366 | DNODE_MAX_SIZE, ZPROP_SRC_NONE); | |
367 | } else { | |
368 | spa_prop_add_list(*nvp, ZPOOL_PROP_MAXDNODESIZE, NULL, | |
369 | DNODE_MIN_SIZE, ZPROP_SRC_NONE); | |
370 | } | |
371 | ||
b128c09f BB |
372 | if ((dp = list_head(&spa->spa_config_list)) != NULL) { |
373 | if (dp->scd_path == NULL) { | |
34dc7c2f | 374 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f BB |
375 | "none", 0, ZPROP_SRC_LOCAL); |
376 | } else if (strcmp(dp->scd_path, spa_config_path) != 0) { | |
34dc7c2f | 377 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f | 378 | dp->scd_path, 0, ZPROP_SRC_LOCAL); |
34dc7c2f BB |
379 | } |
380 | } | |
381 | } | |
382 | ||
383 | /* | |
384 | * Get zpool property values. | |
385 | */ | |
386 | int | |
387 | spa_prop_get(spa_t *spa, nvlist_t **nvp) | |
388 | { | |
428870ff | 389 | objset_t *mos = spa->spa_meta_objset; |
34dc7c2f BB |
390 | zap_cursor_t zc; |
391 | zap_attribute_t za; | |
34dc7c2f BB |
392 | int err; |
393 | ||
79c76d5b | 394 | err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP); |
c28b2279 | 395 | if (err) |
d1d7e268 | 396 | return (err); |
34dc7c2f | 397 | |
b128c09f BB |
398 | mutex_enter(&spa->spa_props_lock); |
399 | ||
34dc7c2f BB |
400 | /* |
401 | * Get properties from the spa config. | |
402 | */ | |
403 | spa_prop_get_config(spa, nvp); | |
404 | ||
34dc7c2f | 405 | /* If no pool property object, no more prop to get. */ |
428870ff | 406 | if (mos == NULL || spa->spa_pool_props_object == 0) { |
34dc7c2f | 407 | mutex_exit(&spa->spa_props_lock); |
c28b2279 | 408 | goto out; |
34dc7c2f BB |
409 | } |
410 | ||
411 | /* | |
412 | * Get properties from the MOS pool property object. | |
413 | */ | |
414 | for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); | |
415 | (err = zap_cursor_retrieve(&zc, &za)) == 0; | |
416 | zap_cursor_advance(&zc)) { | |
417 | uint64_t intval = 0; | |
418 | char *strval = NULL; | |
419 | zprop_source_t src = ZPROP_SRC_DEFAULT; | |
420 | zpool_prop_t prop; | |
421 | ||
31864e3d | 422 | if ((prop = zpool_name_to_prop(za.za_name)) == ZPOOL_PROP_INVAL) |
34dc7c2f BB |
423 | continue; |
424 | ||
425 | switch (za.za_integer_length) { | |
426 | case 8: | |
427 | /* integer property */ | |
428 | if (za.za_first_integer != | |
429 | zpool_prop_default_numeric(prop)) | |
430 | src = ZPROP_SRC_LOCAL; | |
431 | ||
432 | if (prop == ZPOOL_PROP_BOOTFS) { | |
433 | dsl_pool_t *dp; | |
434 | dsl_dataset_t *ds = NULL; | |
435 | ||
436 | dp = spa_get_dsl(spa); | |
13fe0198 | 437 | dsl_pool_config_enter(dp, FTAG); |
619f0976 GW |
438 | err = dsl_dataset_hold_obj(dp, |
439 | za.za_first_integer, FTAG, &ds); | |
440 | if (err != 0) { | |
13fe0198 | 441 | dsl_pool_config_exit(dp, FTAG); |
34dc7c2f BB |
442 | break; |
443 | } | |
444 | ||
eca7b760 | 445 | strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, |
79c76d5b | 446 | KM_SLEEP); |
34dc7c2f | 447 | dsl_dataset_name(ds, strval); |
b128c09f | 448 | dsl_dataset_rele(ds, FTAG); |
13fe0198 | 449 | dsl_pool_config_exit(dp, FTAG); |
34dc7c2f BB |
450 | } else { |
451 | strval = NULL; | |
452 | intval = za.za_first_integer; | |
453 | } | |
454 | ||
455 | spa_prop_add_list(*nvp, prop, strval, intval, src); | |
456 | ||
457 | if (strval != NULL) | |
eca7b760 | 458 | kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN); |
34dc7c2f BB |
459 | |
460 | break; | |
461 | ||
462 | case 1: | |
463 | /* string property */ | |
79c76d5b | 464 | strval = kmem_alloc(za.za_num_integers, KM_SLEEP); |
34dc7c2f BB |
465 | err = zap_lookup(mos, spa->spa_pool_props_object, |
466 | za.za_name, 1, za.za_num_integers, strval); | |
467 | if (err) { | |
468 | kmem_free(strval, za.za_num_integers); | |
469 | break; | |
470 | } | |
471 | spa_prop_add_list(*nvp, prop, strval, 0, src); | |
472 | kmem_free(strval, za.za_num_integers); | |
473 | break; | |
474 | ||
475 | default: | |
476 | break; | |
477 | } | |
478 | } | |
479 | zap_cursor_fini(&zc); | |
480 | mutex_exit(&spa->spa_props_lock); | |
481 | out: | |
482 | if (err && err != ENOENT) { | |
483 | nvlist_free(*nvp); | |
484 | *nvp = NULL; | |
485 | return (err); | |
486 | } | |
487 | ||
488 | return (0); | |
489 | } | |
490 | ||
491 | /* | |
492 | * Validate the given pool properties nvlist and modify the list | |
493 | * for the property values to be set. | |
494 | */ | |
495 | static int | |
496 | spa_prop_validate(spa_t *spa, nvlist_t *props) | |
497 | { | |
498 | nvpair_t *elem; | |
499 | int error = 0, reset_bootfs = 0; | |
d4ed6673 | 500 | uint64_t objnum = 0; |
9ae529ec | 501 | boolean_t has_feature = B_FALSE; |
34dc7c2f BB |
502 | |
503 | elem = NULL; | |
504 | while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { | |
34dc7c2f | 505 | uint64_t intval; |
9ae529ec CS |
506 | char *strval, *slash, *check, *fname; |
507 | const char *propname = nvpair_name(elem); | |
508 | zpool_prop_t prop = zpool_name_to_prop(propname); | |
509 | ||
31864e3d BB |
510 | switch (prop) { |
511 | case ZPOOL_PROP_INVAL: | |
9ae529ec | 512 | if (!zpool_prop_feature(propname)) { |
2e528b49 | 513 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
514 | break; |
515 | } | |
516 | ||
517 | /* | |
518 | * Sanitize the input. | |
519 | */ | |
520 | if (nvpair_type(elem) != DATA_TYPE_UINT64) { | |
2e528b49 | 521 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
522 | break; |
523 | } | |
524 | ||
525 | if (nvpair_value_uint64(elem, &intval) != 0) { | |
2e528b49 | 526 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
527 | break; |
528 | } | |
34dc7c2f | 529 | |
9ae529ec | 530 | if (intval != 0) { |
2e528b49 | 531 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
532 | break; |
533 | } | |
34dc7c2f | 534 | |
9ae529ec CS |
535 | fname = strchr(propname, '@') + 1; |
536 | if (zfeature_lookup_name(fname, NULL) != 0) { | |
2e528b49 | 537 | error = SET_ERROR(EINVAL); |
9ae529ec CS |
538 | break; |
539 | } | |
540 | ||
541 | has_feature = B_TRUE; | |
542 | break; | |
34dc7c2f | 543 | |
34dc7c2f BB |
544 | case ZPOOL_PROP_VERSION: |
545 | error = nvpair_value_uint64(elem, &intval); | |
546 | if (!error && | |
9ae529ec CS |
547 | (intval < spa_version(spa) || |
548 | intval > SPA_VERSION_BEFORE_FEATURES || | |
549 | has_feature)) | |
2e528b49 | 550 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
551 | break; |
552 | ||
553 | case ZPOOL_PROP_DELEGATION: | |
554 | case ZPOOL_PROP_AUTOREPLACE: | |
b128c09f | 555 | case ZPOOL_PROP_LISTSNAPS: |
9babb374 | 556 | case ZPOOL_PROP_AUTOEXPAND: |
34dc7c2f BB |
557 | error = nvpair_value_uint64(elem, &intval); |
558 | if (!error && intval > 1) | |
2e528b49 | 559 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
560 | break; |
561 | ||
379ca9cf OF |
562 | case ZPOOL_PROP_MULTIHOST: |
563 | error = nvpair_value_uint64(elem, &intval); | |
564 | if (!error && intval > 1) | |
565 | error = SET_ERROR(EINVAL); | |
566 | ||
567 | if (!error && !spa_get_hostid()) | |
568 | error = SET_ERROR(ENOTSUP); | |
569 | ||
570 | break; | |
571 | ||
34dc7c2f | 572 | case ZPOOL_PROP_BOOTFS: |
9babb374 BB |
573 | /* |
574 | * If the pool version is less than SPA_VERSION_BOOTFS, | |
575 | * or the pool is still being created (version == 0), | |
576 | * the bootfs property cannot be set. | |
577 | */ | |
34dc7c2f | 578 | if (spa_version(spa) < SPA_VERSION_BOOTFS) { |
2e528b49 | 579 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
580 | break; |
581 | } | |
582 | ||
583 | /* | |
b128c09f | 584 | * Make sure the vdev config is bootable |
34dc7c2f | 585 | */ |
b128c09f | 586 | if (!vdev_is_bootable(spa->spa_root_vdev)) { |
2e528b49 | 587 | error = SET_ERROR(ENOTSUP); |
34dc7c2f BB |
588 | break; |
589 | } | |
590 | ||
591 | reset_bootfs = 1; | |
592 | ||
593 | error = nvpair_value_string(elem, &strval); | |
594 | ||
595 | if (!error) { | |
9ae529ec | 596 | objset_t *os; |
f1512ee6 | 597 | uint64_t propval; |
b128c09f | 598 | |
34dc7c2f BB |
599 | if (strval == NULL || strval[0] == '\0') { |
600 | objnum = zpool_prop_default_numeric( | |
601 | ZPOOL_PROP_BOOTFS); | |
602 | break; | |
603 | } | |
604 | ||
d1d7e268 | 605 | error = dmu_objset_hold(strval, FTAG, &os); |
619f0976 | 606 | if (error != 0) |
34dc7c2f | 607 | break; |
b128c09f | 608 | |
f1512ee6 MA |
609 | /* |
610 | * Must be ZPL, and its property settings | |
611 | * must be supported by GRUB (compression | |
50c957f7 NB |
612 | * is not gzip, and large blocks or large |
613 | * dnodes are not used). | |
f1512ee6 | 614 | */ |
428870ff BB |
615 | |
616 | if (dmu_objset_type(os) != DMU_OST_ZFS) { | |
2e528b49 | 617 | error = SET_ERROR(ENOTSUP); |
13fe0198 MA |
618 | } else if ((error = |
619 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
b128c09f | 620 | zfs_prop_to_name(ZFS_PROP_COMPRESSION), |
f1512ee6 MA |
621 | &propval)) == 0 && |
622 | !BOOTFS_COMPRESS_VALID(propval)) { | |
623 | error = SET_ERROR(ENOTSUP); | |
50c957f7 NB |
624 | } else if ((error = |
625 | dsl_prop_get_int_ds(dmu_objset_ds(os), | |
626 | zfs_prop_to_name(ZFS_PROP_DNODESIZE), | |
627 | &propval)) == 0 && | |
628 | propval != ZFS_DNSIZE_LEGACY) { | |
629 | error = SET_ERROR(ENOTSUP); | |
b128c09f BB |
630 | } else { |
631 | objnum = dmu_objset_id(os); | |
632 | } | |
428870ff | 633 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
634 | } |
635 | break; | |
b128c09f | 636 | |
34dc7c2f BB |
637 | case ZPOOL_PROP_FAILUREMODE: |
638 | error = nvpair_value_uint64(elem, &intval); | |
3bfd95d5 | 639 | if (!error && intval > ZIO_FAILURE_MODE_PANIC) |
2e528b49 | 640 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
641 | |
642 | /* | |
643 | * This is a special case which only occurs when | |
644 | * the pool has completely failed. This allows | |
645 | * the user to change the in-core failmode property | |
646 | * without syncing it out to disk (I/Os might | |
647 | * currently be blocked). We do this by returning | |
648 | * EIO to the caller (spa_prop_set) to trick it | |
649 | * into thinking we encountered a property validation | |
650 | * error. | |
651 | */ | |
b128c09f | 652 | if (!error && spa_suspended(spa)) { |
34dc7c2f | 653 | spa->spa_failmode = intval; |
2e528b49 | 654 | error = SET_ERROR(EIO); |
34dc7c2f BB |
655 | } |
656 | break; | |
657 | ||
658 | case ZPOOL_PROP_CACHEFILE: | |
659 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
660 | break; | |
661 | ||
662 | if (strval[0] == '\0') | |
663 | break; | |
664 | ||
665 | if (strcmp(strval, "none") == 0) | |
666 | break; | |
667 | ||
668 | if (strval[0] != '/') { | |
2e528b49 | 669 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
670 | break; |
671 | } | |
672 | ||
673 | slash = strrchr(strval, '/'); | |
674 | ASSERT(slash != NULL); | |
675 | ||
676 | if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || | |
677 | strcmp(slash, "/..") == 0) | |
2e528b49 | 678 | error = SET_ERROR(EINVAL); |
34dc7c2f | 679 | break; |
428870ff | 680 | |
d96eb2b1 DM |
681 | case ZPOOL_PROP_COMMENT: |
682 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
683 | break; | |
684 | for (check = strval; *check != '\0'; check++) { | |
685 | if (!isprint(*check)) { | |
2e528b49 | 686 | error = SET_ERROR(EINVAL); |
d96eb2b1 DM |
687 | break; |
688 | } | |
d96eb2b1 DM |
689 | } |
690 | if (strlen(strval) > ZPROP_MAX_COMMENT) | |
2e528b49 | 691 | error = SET_ERROR(E2BIG); |
d96eb2b1 DM |
692 | break; |
693 | ||
428870ff BB |
694 | case ZPOOL_PROP_DEDUPDITTO: |
695 | if (spa_version(spa) < SPA_VERSION_DEDUP) | |
2e528b49 | 696 | error = SET_ERROR(ENOTSUP); |
428870ff BB |
697 | else |
698 | error = nvpair_value_uint64(elem, &intval); | |
699 | if (error == 0 && | |
700 | intval != 0 && intval < ZIO_DEDUPDITTO_MIN) | |
2e528b49 | 701 | error = SET_ERROR(EINVAL); |
428870ff | 702 | break; |
e75c13c3 BB |
703 | |
704 | default: | |
705 | break; | |
34dc7c2f BB |
706 | } |
707 | ||
708 | if (error) | |
709 | break; | |
710 | } | |
711 | ||
712 | if (!error && reset_bootfs) { | |
713 | error = nvlist_remove(props, | |
714 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); | |
715 | ||
716 | if (!error) { | |
717 | error = nvlist_add_uint64(props, | |
718 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); | |
719 | } | |
720 | } | |
721 | ||
722 | return (error); | |
723 | } | |
724 | ||
d164b209 BB |
725 | void |
726 | spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) | |
727 | { | |
728 | char *cachefile; | |
729 | spa_config_dirent_t *dp; | |
730 | ||
731 | if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
732 | &cachefile) != 0) | |
733 | return; | |
734 | ||
735 | dp = kmem_alloc(sizeof (spa_config_dirent_t), | |
79c76d5b | 736 | KM_SLEEP); |
d164b209 BB |
737 | |
738 | if (cachefile[0] == '\0') | |
739 | dp->scd_path = spa_strdup(spa_config_path); | |
740 | else if (strcmp(cachefile, "none") == 0) | |
741 | dp->scd_path = NULL; | |
742 | else | |
743 | dp->scd_path = spa_strdup(cachefile); | |
744 | ||
745 | list_insert_head(&spa->spa_config_list, dp); | |
746 | if (need_sync) | |
747 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
748 | } | |
749 | ||
34dc7c2f BB |
750 | int |
751 | spa_prop_set(spa_t *spa, nvlist_t *nvp) | |
752 | { | |
753 | int error; | |
9ae529ec | 754 | nvpair_t *elem = NULL; |
d164b209 | 755 | boolean_t need_sync = B_FALSE; |
34dc7c2f BB |
756 | |
757 | if ((error = spa_prop_validate(spa, nvp)) != 0) | |
758 | return (error); | |
759 | ||
d164b209 | 760 | while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { |
9ae529ec | 761 | zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); |
d164b209 | 762 | |
572e2857 BB |
763 | if (prop == ZPOOL_PROP_CACHEFILE || |
764 | prop == ZPOOL_PROP_ALTROOT || | |
765 | prop == ZPOOL_PROP_READONLY) | |
d164b209 BB |
766 | continue; |
767 | ||
31864e3d | 768 | if (prop == ZPOOL_PROP_VERSION || prop == ZPOOL_PROP_INVAL) { |
9ae529ec CS |
769 | uint64_t ver; |
770 | ||
771 | if (prop == ZPOOL_PROP_VERSION) { | |
772 | VERIFY(nvpair_value_uint64(elem, &ver) == 0); | |
773 | } else { | |
774 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
775 | ver = SPA_VERSION_FEATURES; | |
776 | need_sync = B_TRUE; | |
777 | } | |
778 | ||
779 | /* Save time if the version is already set. */ | |
780 | if (ver == spa_version(spa)) | |
781 | continue; | |
782 | ||
783 | /* | |
784 | * In addition to the pool directory object, we might | |
785 | * create the pool properties object, the features for | |
786 | * read object, the features for write object, or the | |
787 | * feature descriptions object. | |
788 | */ | |
13fe0198 | 789 | error = dsl_sync_task(spa->spa_name, NULL, |
3d45fdd6 MA |
790 | spa_sync_version, &ver, |
791 | 6, ZFS_SPACE_CHECK_RESERVED); | |
9ae529ec CS |
792 | if (error) |
793 | return (error); | |
794 | continue; | |
795 | } | |
796 | ||
d164b209 BB |
797 | need_sync = B_TRUE; |
798 | break; | |
799 | } | |
800 | ||
9ae529ec | 801 | if (need_sync) { |
13fe0198 | 802 | return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props, |
3d45fdd6 | 803 | nvp, 6, ZFS_SPACE_CHECK_RESERVED)); |
9ae529ec CS |
804 | } |
805 | ||
806 | return (0); | |
34dc7c2f BB |
807 | } |
808 | ||
809 | /* | |
810 | * If the bootfs property value is dsobj, clear it. | |
811 | */ | |
812 | void | |
813 | spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) | |
814 | { | |
815 | if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { | |
816 | VERIFY(zap_remove(spa->spa_meta_objset, | |
817 | spa->spa_pool_props_object, | |
818 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); | |
819 | spa->spa_bootfs = 0; | |
820 | } | |
821 | } | |
822 | ||
3bc7e0fb GW |
823 | /*ARGSUSED*/ |
824 | static int | |
13fe0198 | 825 | spa_change_guid_check(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 826 | { |
1c27024e | 827 | ASSERTV(uint64_t *newguid = arg); |
13fe0198 | 828 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; |
3bc7e0fb GW |
829 | vdev_t *rvd = spa->spa_root_vdev; |
830 | uint64_t vdev_state; | |
3bc7e0fb | 831 | |
d2734cce SD |
832 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { |
833 | int error = (spa_has_checkpoint(spa)) ? | |
834 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
835 | return (SET_ERROR(error)); | |
836 | } | |
837 | ||
3bc7e0fb GW |
838 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
839 | vdev_state = rvd->vdev_state; | |
840 | spa_config_exit(spa, SCL_STATE, FTAG); | |
841 | ||
842 | if (vdev_state != VDEV_STATE_HEALTHY) | |
2e528b49 | 843 | return (SET_ERROR(ENXIO)); |
3bc7e0fb GW |
844 | |
845 | ASSERT3U(spa_guid(spa), !=, *newguid); | |
846 | ||
847 | return (0); | |
848 | } | |
849 | ||
850 | static void | |
13fe0198 | 851 | spa_change_guid_sync(void *arg, dmu_tx_t *tx) |
3bc7e0fb | 852 | { |
13fe0198 MA |
853 | uint64_t *newguid = arg; |
854 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
3bc7e0fb GW |
855 | uint64_t oldguid; |
856 | vdev_t *rvd = spa->spa_root_vdev; | |
857 | ||
858 | oldguid = spa_guid(spa); | |
859 | ||
860 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
861 | rvd->vdev_guid = *newguid; | |
862 | rvd->vdev_guid_sum += (*newguid - oldguid); | |
863 | vdev_config_dirty(rvd); | |
864 | spa_config_exit(spa, SCL_STATE, FTAG); | |
865 | ||
6f1ffb06 MA |
866 | spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu", |
867 | oldguid, *newguid); | |
3bc7e0fb GW |
868 | } |
869 | ||
3541dc6d GA |
870 | /* |
871 | * Change the GUID for the pool. This is done so that we can later | |
872 | * re-import a pool built from a clone of our own vdevs. We will modify | |
873 | * the root vdev's guid, our own pool guid, and then mark all of our | |
874 | * vdevs dirty. Note that we must make sure that all our vdevs are | |
875 | * online when we do this, or else any vdevs that weren't present | |
876 | * would be orphaned from our pool. We are also going to issue a | |
877 | * sysevent to update any watchers. | |
878 | */ | |
879 | int | |
880 | spa_change_guid(spa_t *spa) | |
881 | { | |
3bc7e0fb GW |
882 | int error; |
883 | uint64_t guid; | |
3541dc6d | 884 | |
621dd7bb | 885 | mutex_enter(&spa->spa_vdev_top_lock); |
3bc7e0fb GW |
886 | mutex_enter(&spa_namespace_lock); |
887 | guid = spa_generate_guid(NULL); | |
3541dc6d | 888 | |
13fe0198 | 889 | error = dsl_sync_task(spa->spa_name, spa_change_guid_check, |
3d45fdd6 | 890 | spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED); |
3541dc6d | 891 | |
3bc7e0fb | 892 | if (error == 0) { |
a1d477c2 | 893 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 894 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_REGUID); |
3bc7e0fb | 895 | } |
3541dc6d | 896 | |
3bc7e0fb | 897 | mutex_exit(&spa_namespace_lock); |
621dd7bb | 898 | mutex_exit(&spa->spa_vdev_top_lock); |
3541dc6d | 899 | |
3bc7e0fb | 900 | return (error); |
3541dc6d GA |
901 | } |
902 | ||
34dc7c2f BB |
903 | /* |
904 | * ========================================================================== | |
905 | * SPA state manipulation (open/create/destroy/import/export) | |
906 | * ========================================================================== | |
907 | */ | |
908 | ||
909 | static int | |
910 | spa_error_entry_compare(const void *a, const void *b) | |
911 | { | |
ee36c709 GN |
912 | const spa_error_entry_t *sa = (const spa_error_entry_t *)a; |
913 | const spa_error_entry_t *sb = (const spa_error_entry_t *)b; | |
34dc7c2f BB |
914 | int ret; |
915 | ||
ee36c709 | 916 | ret = memcmp(&sa->se_bookmark, &sb->se_bookmark, |
5dbd68a3 | 917 | sizeof (zbookmark_phys_t)); |
34dc7c2f | 918 | |
ee36c709 | 919 | return (AVL_ISIGN(ret)); |
34dc7c2f BB |
920 | } |
921 | ||
922 | /* | |
923 | * Utility function which retrieves copies of the current logs and | |
924 | * re-initializes them in the process. | |
925 | */ | |
926 | void | |
927 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
928 | { | |
929 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
930 | ||
931 | bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); | |
932 | bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); | |
933 | ||
934 | avl_create(&spa->spa_errlist_scrub, | |
935 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
936 | offsetof(spa_error_entry_t, se_avl)); | |
937 | avl_create(&spa->spa_errlist_last, | |
938 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
939 | offsetof(spa_error_entry_t, se_avl)); | |
940 | } | |
941 | ||
7ef5e54e AL |
942 | static void |
943 | spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
34dc7c2f | 944 | { |
7ef5e54e AL |
945 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; |
946 | enum zti_modes mode = ztip->zti_mode; | |
947 | uint_t value = ztip->zti_value; | |
948 | uint_t count = ztip->zti_count; | |
949 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1c27024e | 950 | uint_t flags = 0; |
428870ff | 951 | boolean_t batch = B_FALSE; |
34dc7c2f | 952 | |
7ef5e54e AL |
953 | if (mode == ZTI_MODE_NULL) { |
954 | tqs->stqs_count = 0; | |
955 | tqs->stqs_taskq = NULL; | |
956 | return; | |
957 | } | |
428870ff | 958 | |
7ef5e54e | 959 | ASSERT3U(count, >, 0); |
428870ff | 960 | |
7ef5e54e AL |
961 | tqs->stqs_count = count; |
962 | tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP); | |
428870ff | 963 | |
e8b96c60 MA |
964 | switch (mode) { |
965 | case ZTI_MODE_FIXED: | |
966 | ASSERT3U(value, >=, 1); | |
967 | value = MAX(value, 1); | |
d33931a8 | 968 | flags |= TASKQ_DYNAMIC; |
e8b96c60 | 969 | break; |
7ef5e54e | 970 | |
e8b96c60 MA |
971 | case ZTI_MODE_BATCH: |
972 | batch = B_TRUE; | |
973 | flags |= TASKQ_THREADS_CPU_PCT; | |
dcb6bed1 | 974 | value = MIN(zio_taskq_batch_pct, 100); |
e8b96c60 | 975 | break; |
7ef5e54e | 976 | |
e8b96c60 MA |
977 | default: |
978 | panic("unrecognized mode for %s_%s taskq (%u:%u) in " | |
979 | "spa_activate()", | |
980 | zio_type_name[t], zio_taskq_types[q], mode, value); | |
981 | break; | |
982 | } | |
7ef5e54e | 983 | |
1c27024e | 984 | for (uint_t i = 0; i < count; i++) { |
e8b96c60 | 985 | taskq_t *tq; |
af430294 | 986 | char name[32]; |
7ef5e54e | 987 | |
af430294 MA |
988 | (void) snprintf(name, sizeof (name), "%s_%s", |
989 | zio_type_name[t], zio_taskq_types[q]); | |
7ef5e54e AL |
990 | |
991 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { | |
992 | if (batch) | |
993 | flags |= TASKQ_DC_BATCH; | |
994 | ||
995 | tq = taskq_create_sysdc(name, value, 50, INT_MAX, | |
996 | spa->spa_proc, zio_taskq_basedc, flags); | |
997 | } else { | |
e8b96c60 MA |
998 | pri_t pri = maxclsyspri; |
999 | /* | |
1000 | * The write issue taskq can be extremely CPU | |
1229323d BB |
1001 | * intensive. Run it at slightly less important |
1002 | * priority than the other taskqs. Under Linux this | |
1003 | * means incrementing the priority value on platforms | |
1004 | * like illumos it should be decremented. | |
e8b96c60 MA |
1005 | */ |
1006 | if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE) | |
1229323d | 1007 | pri++; |
e8b96c60 MA |
1008 | |
1009 | tq = taskq_create_proc(name, value, pri, 50, | |
7ef5e54e AL |
1010 | INT_MAX, spa->spa_proc, flags); |
1011 | } | |
1012 | ||
1013 | tqs->stqs_taskq[i] = tq; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | static void | |
1018 | spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q) | |
1019 | { | |
1020 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
7ef5e54e AL |
1021 | |
1022 | if (tqs->stqs_taskq == NULL) { | |
1023 | ASSERT3U(tqs->stqs_count, ==, 0); | |
1024 | return; | |
1025 | } | |
1026 | ||
1c27024e | 1027 | for (uint_t i = 0; i < tqs->stqs_count; i++) { |
7ef5e54e AL |
1028 | ASSERT3P(tqs->stqs_taskq[i], !=, NULL); |
1029 | taskq_destroy(tqs->stqs_taskq[i]); | |
428870ff | 1030 | } |
34dc7c2f | 1031 | |
7ef5e54e AL |
1032 | kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *)); |
1033 | tqs->stqs_taskq = NULL; | |
1034 | } | |
34dc7c2f | 1035 | |
7ef5e54e AL |
1036 | /* |
1037 | * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority. | |
1038 | * Note that a type may have multiple discrete taskqs to avoid lock contention | |
1039 | * on the taskq itself. In that case we choose which taskq at random by using | |
1040 | * the low bits of gethrtime(). | |
1041 | */ | |
1042 | void | |
1043 | spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1044 | task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent) | |
1045 | { | |
1046 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1047 | taskq_t *tq; | |
1048 | ||
1049 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1050 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1051 | ||
1052 | if (tqs->stqs_count == 1) { | |
1053 | tq = tqs->stqs_taskq[0]; | |
1054 | } else { | |
c12936b1 | 1055 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
428870ff | 1056 | } |
7ef5e54e AL |
1057 | |
1058 | taskq_dispatch_ent(tq, func, arg, flags, ent); | |
428870ff BB |
1059 | } |
1060 | ||
044baf00 BB |
1061 | /* |
1062 | * Same as spa_taskq_dispatch_ent() but block on the task until completion. | |
1063 | */ | |
1064 | void | |
1065 | spa_taskq_dispatch_sync(spa_t *spa, zio_type_t t, zio_taskq_type_t q, | |
1066 | task_func_t *func, void *arg, uint_t flags) | |
1067 | { | |
1068 | spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q]; | |
1069 | taskq_t *tq; | |
1070 | taskqid_t id; | |
1071 | ||
1072 | ASSERT3P(tqs->stqs_taskq, !=, NULL); | |
1073 | ASSERT3U(tqs->stqs_count, !=, 0); | |
1074 | ||
1075 | if (tqs->stqs_count == 1) { | |
1076 | tq = tqs->stqs_taskq[0]; | |
1077 | } else { | |
c12936b1 | 1078 | tq = tqs->stqs_taskq[((uint64_t)gethrtime()) % tqs->stqs_count]; |
044baf00 BB |
1079 | } |
1080 | ||
1081 | id = taskq_dispatch(tq, func, arg, flags); | |
1082 | if (id) | |
1083 | taskq_wait_id(tq, id); | |
1084 | } | |
1085 | ||
428870ff BB |
1086 | static void |
1087 | spa_create_zio_taskqs(spa_t *spa) | |
1088 | { | |
1c27024e DB |
1089 | for (int t = 0; t < ZIO_TYPES; t++) { |
1090 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1091 | spa_taskqs_init(spa, t, q); |
428870ff BB |
1092 | } |
1093 | } | |
1094 | } | |
9babb374 | 1095 | |
c25b8f99 BB |
1096 | /* |
1097 | * Disabled until spa_thread() can be adapted for Linux. | |
1098 | */ | |
1099 | #undef HAVE_SPA_THREAD | |
1100 | ||
7b89a549 | 1101 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) |
428870ff BB |
1102 | static void |
1103 | spa_thread(void *arg) | |
1104 | { | |
93ce2b4c | 1105 | psetid_t zio_taskq_psrset_bind = PS_NONE; |
428870ff | 1106 | callb_cpr_t cprinfo; |
9babb374 | 1107 | |
428870ff BB |
1108 | spa_t *spa = arg; |
1109 | user_t *pu = PTOU(curproc); | |
9babb374 | 1110 | |
428870ff BB |
1111 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, |
1112 | spa->spa_name); | |
9babb374 | 1113 | |
428870ff BB |
1114 | ASSERT(curproc != &p0); |
1115 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
1116 | "zpool-%s", spa->spa_name); | |
1117 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
1118 | ||
1119 | /* bind this thread to the requested psrset */ | |
1120 | if (zio_taskq_psrset_bind != PS_NONE) { | |
1121 | pool_lock(); | |
1122 | mutex_enter(&cpu_lock); | |
1123 | mutex_enter(&pidlock); | |
1124 | mutex_enter(&curproc->p_lock); | |
1125 | ||
1126 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
1127 | 0, NULL, NULL) == 0) { | |
1128 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
1129 | } else { | |
1130 | cmn_err(CE_WARN, | |
1131 | "Couldn't bind process for zfs pool \"%s\" to " | |
1132 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
1133 | } | |
1134 | ||
1135 | mutex_exit(&curproc->p_lock); | |
1136 | mutex_exit(&pidlock); | |
1137 | mutex_exit(&cpu_lock); | |
1138 | pool_unlock(); | |
1139 | } | |
1140 | ||
1141 | if (zio_taskq_sysdc) { | |
1142 | sysdc_thread_enter(curthread, 100, 0); | |
1143 | } | |
1144 | ||
1145 | spa->spa_proc = curproc; | |
1146 | spa->spa_did = curthread->t_did; | |
1147 | ||
1148 | spa_create_zio_taskqs(spa); | |
1149 | ||
1150 | mutex_enter(&spa->spa_proc_lock); | |
1151 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
1152 | ||
1153 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
1154 | cv_broadcast(&spa->spa_proc_cv); | |
1155 | ||
1156 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
1157 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
1158 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1159 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
1160 | ||
1161 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
1162 | spa->spa_proc_state = SPA_PROC_GONE; | |
1163 | spa->spa_proc = &p0; | |
1164 | cv_broadcast(&spa->spa_proc_cv); | |
1165 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
1166 | ||
1167 | mutex_enter(&curproc->p_lock); | |
1168 | lwp_exit(); | |
1169 | } | |
1170 | #endif | |
1171 | ||
1172 | /* | |
1173 | * Activate an uninitialized pool. | |
1174 | */ | |
1175 | static void | |
1176 | spa_activate(spa_t *spa, int mode) | |
1177 | { | |
1178 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); | |
1179 | ||
1180 | spa->spa_state = POOL_STATE_ACTIVE; | |
1181 | spa->spa_mode = mode; | |
1182 | ||
1183 | spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
1184 | spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
cc99f275 DB |
1185 | spa->spa_special_class = metaslab_class_create(spa, zfs_metaslab_ops); |
1186 | spa->spa_dedup_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
428870ff BB |
1187 | |
1188 | /* Try to create a covering process */ | |
1189 | mutex_enter(&spa->spa_proc_lock); | |
1190 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
1191 | ASSERT(spa->spa_proc == &p0); | |
1192 | spa->spa_did = 0; | |
1193 | ||
7b89a549 | 1194 | #ifdef HAVE_SPA_THREAD |
428870ff BB |
1195 | /* Only create a process if we're going to be around a while. */ |
1196 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
1197 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
1198 | NULL, 0) == 0) { | |
1199 | spa->spa_proc_state = SPA_PROC_CREATED; | |
1200 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
1201 | cv_wait(&spa->spa_proc_cv, | |
1202 | &spa->spa_proc_lock); | |
9babb374 | 1203 | } |
428870ff BB |
1204 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); |
1205 | ASSERT(spa->spa_proc != &p0); | |
1206 | ASSERT(spa->spa_did != 0); | |
1207 | } else { | |
1208 | #ifdef _KERNEL | |
1209 | cmn_err(CE_WARN, | |
1210 | "Couldn't create process for zfs pool \"%s\"\n", | |
1211 | spa->spa_name); | |
1212 | #endif | |
b128c09f | 1213 | } |
34dc7c2f | 1214 | } |
7b89a549 | 1215 | #endif /* HAVE_SPA_THREAD */ |
428870ff BB |
1216 | mutex_exit(&spa->spa_proc_lock); |
1217 | ||
1218 | /* If we didn't create a process, we need to create our taskqs. */ | |
1219 | if (spa->spa_proc == &p0) { | |
1220 | spa_create_zio_taskqs(spa); | |
1221 | } | |
34dc7c2f | 1222 | |
619f0976 GW |
1223 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1224 | spa->spa_txg_zio[i] = zio_root(spa, NULL, NULL, | |
1225 | ZIO_FLAG_CANFAIL); | |
1226 | } | |
a1d477c2 | 1227 | |
b128c09f BB |
1228 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), |
1229 | offsetof(vdev_t, vdev_config_dirty_node)); | |
0c66c32d JG |
1230 | list_create(&spa->spa_evicting_os_list, sizeof (objset_t), |
1231 | offsetof(objset_t, os_evicting_node)); | |
b128c09f BB |
1232 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), |
1233 | offsetof(vdev_t, vdev_state_dirty_node)); | |
34dc7c2f | 1234 | |
4747a7d3 | 1235 | txg_list_create(&spa->spa_vdev_txg_list, spa, |
34dc7c2f BB |
1236 | offsetof(struct vdev, vdev_txg_node)); |
1237 | ||
1238 | avl_create(&spa->spa_errlist_scrub, | |
1239 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1240 | offsetof(spa_error_entry_t, se_avl)); | |
1241 | avl_create(&spa->spa_errlist_last, | |
1242 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
1243 | offsetof(spa_error_entry_t, se_avl)); | |
a0bd735a | 1244 | |
b5256303 TC |
1245 | spa_keystore_init(&spa->spa_keystore); |
1246 | ||
a0bd735a BP |
1247 | /* |
1248 | * This taskq is used to perform zvol-minor-related tasks | |
1249 | * asynchronously. This has several advantages, including easy | |
1250 | * resolution of various deadlocks (zfsonlinux bug #3681). | |
1251 | * | |
1252 | * The taskq must be single threaded to ensure tasks are always | |
1253 | * processed in the order in which they were dispatched. | |
1254 | * | |
1255 | * A taskq per pool allows one to keep the pools independent. | |
1256 | * This way if one pool is suspended, it will not impact another. | |
1257 | * | |
1258 | * The preferred location to dispatch a zvol minor task is a sync | |
1259 | * task. In this context, there is easy access to the spa_t and minimal | |
1260 | * error handling is required because the sync task must succeed. | |
1261 | */ | |
1262 | spa->spa_zvol_taskq = taskq_create("z_zvol", 1, defclsyspri, | |
1263 | 1, INT_MAX, 0); | |
1de321e6 | 1264 | |
77d8a0f1 | 1265 | /* |
1266 | * Taskq dedicated to prefetcher threads: this is used to prevent the | |
1267 | * pool traverse code from monopolizing the global (and limited) | |
1268 | * system_taskq by inappropriately scheduling long running tasks on it. | |
1269 | */ | |
1270 | spa->spa_prefetch_taskq = taskq_create("z_prefetch", boot_ncpus, | |
1271 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); | |
1272 | ||
1de321e6 JX |
1273 | /* |
1274 | * The taskq to upgrade datasets in this pool. Currently used by | |
9c5167d1 | 1275 | * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA. |
1de321e6 JX |
1276 | */ |
1277 | spa->spa_upgrade_taskq = taskq_create("z_upgrade", boot_ncpus, | |
1278 | defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC); | |
34dc7c2f BB |
1279 | } |
1280 | ||
1281 | /* | |
1282 | * Opposite of spa_activate(). | |
1283 | */ | |
1284 | static void | |
1285 | spa_deactivate(spa_t *spa) | |
1286 | { | |
34dc7c2f BB |
1287 | ASSERT(spa->spa_sync_on == B_FALSE); |
1288 | ASSERT(spa->spa_dsl_pool == NULL); | |
1289 | ASSERT(spa->spa_root_vdev == NULL); | |
9babb374 | 1290 | ASSERT(spa->spa_async_zio_root == NULL); |
34dc7c2f BB |
1291 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); |
1292 | ||
0c66c32d JG |
1293 | spa_evicting_os_wait(spa); |
1294 | ||
a0bd735a BP |
1295 | if (spa->spa_zvol_taskq) { |
1296 | taskq_destroy(spa->spa_zvol_taskq); | |
1297 | spa->spa_zvol_taskq = NULL; | |
1298 | } | |
1299 | ||
77d8a0f1 | 1300 | if (spa->spa_prefetch_taskq) { |
1301 | taskq_destroy(spa->spa_prefetch_taskq); | |
1302 | spa->spa_prefetch_taskq = NULL; | |
1303 | } | |
1304 | ||
1de321e6 JX |
1305 | if (spa->spa_upgrade_taskq) { |
1306 | taskq_destroy(spa->spa_upgrade_taskq); | |
1307 | spa->spa_upgrade_taskq = NULL; | |
1308 | } | |
1309 | ||
34dc7c2f BB |
1310 | txg_list_destroy(&spa->spa_vdev_txg_list); |
1311 | ||
b128c09f | 1312 | list_destroy(&spa->spa_config_dirty_list); |
0c66c32d | 1313 | list_destroy(&spa->spa_evicting_os_list); |
b128c09f | 1314 | list_destroy(&spa->spa_state_dirty_list); |
34dc7c2f | 1315 | |
57ddcda1 | 1316 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 | 1317 | |
1c27024e DB |
1318 | for (int t = 0; t < ZIO_TYPES; t++) { |
1319 | for (int q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
7ef5e54e | 1320 | spa_taskqs_fini(spa, t, q); |
b128c09f | 1321 | } |
34dc7c2f BB |
1322 | } |
1323 | ||
a1d477c2 MA |
1324 | for (size_t i = 0; i < TXG_SIZE; i++) { |
1325 | ASSERT3P(spa->spa_txg_zio[i], !=, NULL); | |
1326 | VERIFY0(zio_wait(spa->spa_txg_zio[i])); | |
1327 | spa->spa_txg_zio[i] = NULL; | |
1328 | } | |
1329 | ||
34dc7c2f BB |
1330 | metaslab_class_destroy(spa->spa_normal_class); |
1331 | spa->spa_normal_class = NULL; | |
1332 | ||
1333 | metaslab_class_destroy(spa->spa_log_class); | |
1334 | spa->spa_log_class = NULL; | |
1335 | ||
cc99f275 DB |
1336 | metaslab_class_destroy(spa->spa_special_class); |
1337 | spa->spa_special_class = NULL; | |
1338 | ||
1339 | metaslab_class_destroy(spa->spa_dedup_class); | |
1340 | spa->spa_dedup_class = NULL; | |
1341 | ||
34dc7c2f BB |
1342 | /* |
1343 | * If this was part of an import or the open otherwise failed, we may | |
1344 | * still have errors left in the queues. Empty them just in case. | |
1345 | */ | |
1346 | spa_errlog_drain(spa); | |
34dc7c2f BB |
1347 | avl_destroy(&spa->spa_errlist_scrub); |
1348 | avl_destroy(&spa->spa_errlist_last); | |
1349 | ||
b5256303 TC |
1350 | spa_keystore_fini(&spa->spa_keystore); |
1351 | ||
34dc7c2f | 1352 | spa->spa_state = POOL_STATE_UNINITIALIZED; |
428870ff BB |
1353 | |
1354 | mutex_enter(&spa->spa_proc_lock); | |
1355 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1356 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1357 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1358 | cv_broadcast(&spa->spa_proc_cv); | |
1359 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1360 | ASSERT(spa->spa_proc != &p0); | |
1361 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1362 | } | |
1363 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1364 | spa->spa_proc_state = SPA_PROC_NONE; | |
1365 | } | |
1366 | ASSERT(spa->spa_proc == &p0); | |
1367 | mutex_exit(&spa->spa_proc_lock); | |
1368 | ||
1369 | /* | |
1370 | * We want to make sure spa_thread() has actually exited the ZFS | |
1371 | * module, so that the module can't be unloaded out from underneath | |
1372 | * it. | |
1373 | */ | |
1374 | if (spa->spa_did != 0) { | |
1375 | thread_join(spa->spa_did); | |
1376 | spa->spa_did = 0; | |
1377 | } | |
34dc7c2f BB |
1378 | } |
1379 | ||
1380 | /* | |
1381 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1382 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1383 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1384 | * All vdev validation is done by the vdev_alloc() routine. | |
1385 | */ | |
1386 | static int | |
1387 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, | |
1388 | uint_t id, int atype) | |
1389 | { | |
1390 | nvlist_t **child; | |
9babb374 | 1391 | uint_t children; |
34dc7c2f BB |
1392 | int error; |
1393 | ||
1394 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1395 | return (error); | |
1396 | ||
1397 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1398 | return (0); | |
1399 | ||
b128c09f BB |
1400 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1401 | &child, &children); | |
1402 | ||
1403 | if (error == ENOENT) | |
1404 | return (0); | |
1405 | ||
1406 | if (error) { | |
34dc7c2f BB |
1407 | vdev_free(*vdp); |
1408 | *vdp = NULL; | |
2e528b49 | 1409 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1410 | } |
1411 | ||
1c27024e | 1412 | for (int c = 0; c < children; c++) { |
34dc7c2f BB |
1413 | vdev_t *vd; |
1414 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1415 | atype)) != 0) { | |
1416 | vdev_free(*vdp); | |
1417 | *vdp = NULL; | |
1418 | return (error); | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | ASSERT(*vdp != NULL); | |
1423 | ||
1424 | return (0); | |
1425 | } | |
1426 | ||
1427 | /* | |
1428 | * Opposite of spa_load(). | |
1429 | */ | |
1430 | static void | |
1431 | spa_unload(spa_t *spa) | |
1432 | { | |
1c27024e | 1433 | int i; |
34dc7c2f | 1434 | |
b128c09f BB |
1435 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
1436 | ||
4a0ee12a PZ |
1437 | spa_load_note(spa, "UNLOADING"); |
1438 | ||
34dc7c2f BB |
1439 | /* |
1440 | * Stop async tasks. | |
1441 | */ | |
1442 | spa_async_suspend(spa); | |
1443 | ||
619f0976 GW |
1444 | if (spa->spa_root_vdev) { |
1445 | vdev_initialize_stop_all(spa->spa_root_vdev, | |
1446 | VDEV_INITIALIZE_ACTIVE); | |
1447 | } | |
1448 | ||
34dc7c2f BB |
1449 | /* |
1450 | * Stop syncing. | |
1451 | */ | |
1452 | if (spa->spa_sync_on) { | |
1453 | txg_sync_stop(spa->spa_dsl_pool); | |
1454 | spa->spa_sync_on = B_FALSE; | |
1455 | } | |
1456 | ||
4e21fd06 DB |
1457 | /* |
1458 | * Even though vdev_free() also calls vdev_metaslab_fini, we need | |
1459 | * to call it earlier, before we wait for async i/o to complete. | |
1460 | * This ensures that there is no async metaslab prefetching, by | |
1461 | * calling taskq_wait(mg_taskq). | |
1462 | */ | |
1463 | if (spa->spa_root_vdev != NULL) { | |
619f0976 | 1464 | spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); |
1c27024e | 1465 | for (int c = 0; c < spa->spa_root_vdev->vdev_children; c++) |
4e21fd06 | 1466 | vdev_metaslab_fini(spa->spa_root_vdev->vdev_child[c]); |
619f0976 | 1467 | spa_config_exit(spa, SCL_ALL, spa); |
4e21fd06 DB |
1468 | } |
1469 | ||
379ca9cf OF |
1470 | if (spa->spa_mmp.mmp_thread) |
1471 | mmp_thread_stop(spa); | |
1472 | ||
34dc7c2f | 1473 | /* |
b128c09f | 1474 | * Wait for any outstanding async I/O to complete. |
34dc7c2f | 1475 | */ |
9babb374 | 1476 | if (spa->spa_async_zio_root != NULL) { |
1c27024e | 1477 | for (int i = 0; i < max_ncpus; i++) |
e022864d MA |
1478 | (void) zio_wait(spa->spa_async_zio_root[i]); |
1479 | kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *)); | |
9babb374 BB |
1480 | spa->spa_async_zio_root = NULL; |
1481 | } | |
34dc7c2f | 1482 | |
a1d477c2 MA |
1483 | if (spa->spa_vdev_removal != NULL) { |
1484 | spa_vdev_removal_destroy(spa->spa_vdev_removal); | |
1485 | spa->spa_vdev_removal = NULL; | |
1486 | } | |
1487 | ||
9d5b5245 | 1488 | if (spa->spa_condense_zthr != NULL) { |
9d5b5245 SD |
1489 | zthr_destroy(spa->spa_condense_zthr); |
1490 | spa->spa_condense_zthr = NULL; | |
1491 | } | |
1492 | ||
d2734cce | 1493 | if (spa->spa_checkpoint_discard_zthr != NULL) { |
d2734cce SD |
1494 | zthr_destroy(spa->spa_checkpoint_discard_zthr); |
1495 | spa->spa_checkpoint_discard_zthr = NULL; | |
1496 | } | |
1497 | ||
a1d477c2 MA |
1498 | spa_condense_fini(spa); |
1499 | ||
428870ff BB |
1500 | bpobj_close(&spa->spa_deferred_bpobj); |
1501 | ||
619f0976 | 1502 | spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); |
93cf2076 GW |
1503 | |
1504 | /* | |
1505 | * Close all vdevs. | |
1506 | */ | |
1507 | if (spa->spa_root_vdev) | |
1508 | vdev_free(spa->spa_root_vdev); | |
1509 | ASSERT(spa->spa_root_vdev == NULL); | |
1510 | ||
34dc7c2f BB |
1511 | /* |
1512 | * Close the dsl pool. | |
1513 | */ | |
1514 | if (spa->spa_dsl_pool) { | |
1515 | dsl_pool_close(spa->spa_dsl_pool); | |
1516 | spa->spa_dsl_pool = NULL; | |
428870ff | 1517 | spa->spa_meta_objset = NULL; |
34dc7c2f BB |
1518 | } |
1519 | ||
428870ff BB |
1520 | ddt_unload(spa); |
1521 | ||
fb5f0bc8 BB |
1522 | /* |
1523 | * Drop and purge level 2 cache | |
1524 | */ | |
1525 | spa_l2cache_drop(spa); | |
1526 | ||
34dc7c2f BB |
1527 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1528 | vdev_free(spa->spa_spares.sav_vdevs[i]); | |
1529 | if (spa->spa_spares.sav_vdevs) { | |
1530 | kmem_free(spa->spa_spares.sav_vdevs, | |
1531 | spa->spa_spares.sav_count * sizeof (void *)); | |
1532 | spa->spa_spares.sav_vdevs = NULL; | |
1533 | } | |
1534 | if (spa->spa_spares.sav_config) { | |
1535 | nvlist_free(spa->spa_spares.sav_config); | |
1536 | spa->spa_spares.sav_config = NULL; | |
1537 | } | |
b128c09f | 1538 | spa->spa_spares.sav_count = 0; |
34dc7c2f | 1539 | |
5ffb9d1d GW |
1540 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
1541 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); | |
34dc7c2f | 1542 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); |
5ffb9d1d | 1543 | } |
34dc7c2f BB |
1544 | if (spa->spa_l2cache.sav_vdevs) { |
1545 | kmem_free(spa->spa_l2cache.sav_vdevs, | |
1546 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1547 | spa->spa_l2cache.sav_vdevs = NULL; | |
1548 | } | |
1549 | if (spa->spa_l2cache.sav_config) { | |
1550 | nvlist_free(spa->spa_l2cache.sav_config); | |
1551 | spa->spa_l2cache.sav_config = NULL; | |
1552 | } | |
b128c09f | 1553 | spa->spa_l2cache.sav_count = 0; |
34dc7c2f BB |
1554 | |
1555 | spa->spa_async_suspended = 0; | |
fb5f0bc8 | 1556 | |
a1d477c2 MA |
1557 | spa->spa_indirect_vdevs_loaded = B_FALSE; |
1558 | ||
d96eb2b1 DM |
1559 | if (spa->spa_comment != NULL) { |
1560 | spa_strfree(spa->spa_comment); | |
1561 | spa->spa_comment = NULL; | |
1562 | } | |
1563 | ||
619f0976 | 1564 | spa_config_exit(spa, SCL_ALL, spa); |
34dc7c2f BB |
1565 | } |
1566 | ||
1567 | /* | |
1568 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1569 | * this pool. When this is called, we have some form of basic information in | |
1570 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1571 | * then re-generate a more complete list including status information. | |
1572 | */ | |
a1d477c2 | 1573 | void |
34dc7c2f BB |
1574 | spa_load_spares(spa_t *spa) |
1575 | { | |
1576 | nvlist_t **spares; | |
1577 | uint_t nspares; | |
1578 | int i; | |
1579 | vdev_t *vd, *tvd; | |
1580 | ||
d2734cce SD |
1581 | #ifndef _KERNEL |
1582 | /* | |
1583 | * zdb opens both the current state of the pool and the | |
1584 | * checkpointed state (if present), with a different spa_t. | |
1585 | * | |
1586 | * As spare vdevs are shared among open pools, we skip loading | |
1587 | * them when we load the checkpointed state of the pool. | |
1588 | */ | |
1589 | if (!spa_writeable(spa)) | |
1590 | return; | |
1591 | #endif | |
1592 | ||
b128c09f BB |
1593 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1594 | ||
34dc7c2f BB |
1595 | /* |
1596 | * First, close and free any existing spare vdevs. | |
1597 | */ | |
1598 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1599 | vd = spa->spa_spares.sav_vdevs[i]; | |
1600 | ||
1601 | /* Undo the call to spa_activate() below */ | |
b128c09f BB |
1602 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1603 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
34dc7c2f BB |
1604 | spa_spare_remove(tvd); |
1605 | vdev_close(vd); | |
1606 | vdev_free(vd); | |
1607 | } | |
1608 | ||
1609 | if (spa->spa_spares.sav_vdevs) | |
1610 | kmem_free(spa->spa_spares.sav_vdevs, | |
1611 | spa->spa_spares.sav_count * sizeof (void *)); | |
1612 | ||
1613 | if (spa->spa_spares.sav_config == NULL) | |
1614 | nspares = 0; | |
1615 | else | |
1616 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
1617 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
1618 | ||
1619 | spa->spa_spares.sav_count = (int)nspares; | |
1620 | spa->spa_spares.sav_vdevs = NULL; | |
1621 | ||
1622 | if (nspares == 0) | |
1623 | return; | |
1624 | ||
1625 | /* | |
1626 | * Construct the array of vdevs, opening them to get status in the | |
1627 | * process. For each spare, there is potentially two different vdev_t | |
1628 | * structures associated with it: one in the list of spares (used only | |
1629 | * for basic validation purposes) and one in the active vdev | |
1630 | * configuration (if it's spared in). During this phase we open and | |
1631 | * validate each vdev on the spare list. If the vdev also exists in the | |
1632 | * active configuration, then we also mark this vdev as an active spare. | |
1633 | */ | |
904ea276 | 1634 | spa->spa_spares.sav_vdevs = kmem_zalloc(nspares * sizeof (void *), |
79c76d5b | 1635 | KM_SLEEP); |
34dc7c2f BB |
1636 | for (i = 0; i < spa->spa_spares.sav_count; i++) { |
1637 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1638 | VDEV_ALLOC_SPARE) == 0); | |
1639 | ASSERT(vd != NULL); | |
1640 | ||
1641 | spa->spa_spares.sav_vdevs[i] = vd; | |
1642 | ||
b128c09f BB |
1643 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1644 | B_FALSE)) != NULL) { | |
34dc7c2f BB |
1645 | if (!tvd->vdev_isspare) |
1646 | spa_spare_add(tvd); | |
1647 | ||
1648 | /* | |
1649 | * We only mark the spare active if we were successfully | |
1650 | * able to load the vdev. Otherwise, importing a pool | |
1651 | * with a bad active spare would result in strange | |
1652 | * behavior, because multiple pool would think the spare | |
1653 | * is actively in use. | |
1654 | * | |
1655 | * There is a vulnerability here to an equally bizarre | |
1656 | * circumstance, where a dead active spare is later | |
1657 | * brought back to life (onlined or otherwise). Given | |
1658 | * the rarity of this scenario, and the extra complexity | |
1659 | * it adds, we ignore the possibility. | |
1660 | */ | |
1661 | if (!vdev_is_dead(tvd)) | |
1662 | spa_spare_activate(tvd); | |
1663 | } | |
1664 | ||
b128c09f | 1665 | vd->vdev_top = vd; |
9babb374 | 1666 | vd->vdev_aux = &spa->spa_spares; |
b128c09f | 1667 | |
34dc7c2f BB |
1668 | if (vdev_open(vd) != 0) |
1669 | continue; | |
1670 | ||
34dc7c2f BB |
1671 | if (vdev_validate_aux(vd) == 0) |
1672 | spa_spare_add(vd); | |
1673 | } | |
1674 | ||
1675 | /* | |
1676 | * Recompute the stashed list of spares, with status information | |
1677 | * this time. | |
1678 | */ | |
1679 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, | |
1680 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1681 | ||
1682 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
79c76d5b | 1683 | KM_SLEEP); |
34dc7c2f BB |
1684 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1685 | spares[i] = vdev_config_generate(spa, | |
428870ff | 1686 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); |
34dc7c2f BB |
1687 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
1688 | ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); | |
1689 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1690 | nvlist_free(spares[i]); | |
1691 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1692 | } | |
1693 | ||
1694 | /* | |
1695 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1696 | * this pool. When this is called, we have some form of basic information in | |
1697 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1698 | * then re-generate a more complete list including status information. | |
1699 | * Devices which are already active have their details maintained, and are | |
1700 | * not re-opened. | |
1701 | */ | |
a1d477c2 | 1702 | void |
34dc7c2f BB |
1703 | spa_load_l2cache(spa_t *spa) |
1704 | { | |
460f239e | 1705 | nvlist_t **l2cache = NULL; |
34dc7c2f BB |
1706 | uint_t nl2cache; |
1707 | int i, j, oldnvdevs; | |
9babb374 | 1708 | uint64_t guid; |
a117a6d6 | 1709 | vdev_t *vd, **oldvdevs, **newvdevs; |
34dc7c2f BB |
1710 | spa_aux_vdev_t *sav = &spa->spa_l2cache; |
1711 | ||
d2734cce SD |
1712 | #ifndef _KERNEL |
1713 | /* | |
1714 | * zdb opens both the current state of the pool and the | |
1715 | * checkpointed state (if present), with a different spa_t. | |
1716 | * | |
1717 | * As L2 caches are part of the ARC which is shared among open | |
1718 | * pools, we skip loading them when we load the checkpointed | |
1719 | * state of the pool. | |
1720 | */ | |
1721 | if (!spa_writeable(spa)) | |
1722 | return; | |
1723 | #endif | |
1724 | ||
b128c09f BB |
1725 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1726 | ||
34dc7c2f BB |
1727 | oldvdevs = sav->sav_vdevs; |
1728 | oldnvdevs = sav->sav_count; | |
1729 | sav->sav_vdevs = NULL; | |
1730 | sav->sav_count = 0; | |
1731 | ||
67d60824 NB |
1732 | if (sav->sav_config == NULL) { |
1733 | nl2cache = 0; | |
1734 | newvdevs = NULL; | |
1735 | goto out; | |
1736 | } | |
1737 | ||
1738 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, | |
1739 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
1740 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP); | |
1741 | ||
34dc7c2f BB |
1742 | /* |
1743 | * Process new nvlist of vdevs. | |
1744 | */ | |
1745 | for (i = 0; i < nl2cache; i++) { | |
1746 | VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, | |
1747 | &guid) == 0); | |
1748 | ||
1749 | newvdevs[i] = NULL; | |
1750 | for (j = 0; j < oldnvdevs; j++) { | |
1751 | vd = oldvdevs[j]; | |
1752 | if (vd != NULL && guid == vd->vdev_guid) { | |
1753 | /* | |
1754 | * Retain previous vdev for add/remove ops. | |
1755 | */ | |
1756 | newvdevs[i] = vd; | |
1757 | oldvdevs[j] = NULL; | |
1758 | break; | |
1759 | } | |
1760 | } | |
1761 | ||
1762 | if (newvdevs[i] == NULL) { | |
1763 | /* | |
1764 | * Create new vdev | |
1765 | */ | |
1766 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
1767 | VDEV_ALLOC_L2CACHE) == 0); | |
1768 | ASSERT(vd != NULL); | |
1769 | newvdevs[i] = vd; | |
1770 | ||
1771 | /* | |
1772 | * Commit this vdev as an l2cache device, | |
1773 | * even if it fails to open. | |
1774 | */ | |
1775 | spa_l2cache_add(vd); | |
1776 | ||
b128c09f BB |
1777 | vd->vdev_top = vd; |
1778 | vd->vdev_aux = sav; | |
1779 | ||
1780 | spa_l2cache_activate(vd); | |
1781 | ||
34dc7c2f BB |
1782 | if (vdev_open(vd) != 0) |
1783 | continue; | |
1784 | ||
34dc7c2f BB |
1785 | (void) vdev_validate_aux(vd); |
1786 | ||
9babb374 BB |
1787 | if (!vdev_is_dead(vd)) |
1788 | l2arc_add_vdev(spa, vd); | |
34dc7c2f BB |
1789 | } |
1790 | } | |
1791 | ||
67d60824 NB |
1792 | sav->sav_vdevs = newvdevs; |
1793 | sav->sav_count = (int)nl2cache; | |
1794 | ||
1795 | /* | |
1796 | * Recompute the stashed list of l2cache devices, with status | |
1797 | * information this time. | |
1798 | */ | |
1799 | VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, | |
1800 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1801 | ||
460f239e D |
1802 | if (sav->sav_count > 0) |
1803 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), | |
1804 | KM_SLEEP); | |
67d60824 NB |
1805 | for (i = 0; i < sav->sav_count; i++) |
1806 | l2cache[i] = vdev_config_generate(spa, | |
1807 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); | |
1808 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
1809 | ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); | |
1810 | ||
1811 | out: | |
34dc7c2f BB |
1812 | /* |
1813 | * Purge vdevs that were dropped | |
1814 | */ | |
1815 | for (i = 0; i < oldnvdevs; i++) { | |
1816 | uint64_t pool; | |
1817 | ||
1818 | vd = oldvdevs[i]; | |
1819 | if (vd != NULL) { | |
5ffb9d1d GW |
1820 | ASSERT(vd->vdev_isl2cache); |
1821 | ||
fb5f0bc8 BB |
1822 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
1823 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 1824 | l2arc_remove_vdev(vd); |
5ffb9d1d GW |
1825 | vdev_clear_stats(vd); |
1826 | vdev_free(vd); | |
34dc7c2f BB |
1827 | } |
1828 | } | |
1829 | ||
1830 | if (oldvdevs) | |
1831 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); | |
1832 | ||
34dc7c2f BB |
1833 | for (i = 0; i < sav->sav_count; i++) |
1834 | nvlist_free(l2cache[i]); | |
1835 | if (sav->sav_count) | |
1836 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
1837 | } | |
1838 | ||
1839 | static int | |
1840 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
1841 | { | |
1842 | dmu_buf_t *db; | |
1843 | char *packed = NULL; | |
1844 | size_t nvsize = 0; | |
1845 | int error; | |
1846 | *value = NULL; | |
1847 | ||
c3275b56 BB |
1848 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); |
1849 | if (error) | |
1850 | return (error); | |
1851 | ||
34dc7c2f BB |
1852 | nvsize = *(uint64_t *)db->db_data; |
1853 | dmu_buf_rele(db, FTAG); | |
1854 | ||
77aef6f6 | 1855 | packed = vmem_alloc(nvsize, KM_SLEEP); |
9babb374 BB |
1856 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, |
1857 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
1858 | if (error == 0) |
1859 | error = nvlist_unpack(packed, nvsize, value, 0); | |
77aef6f6 | 1860 | vmem_free(packed, nvsize); |
34dc7c2f BB |
1861 | |
1862 | return (error); | |
1863 | } | |
1864 | ||
6cb8e530 PZ |
1865 | /* |
1866 | * Concrete top-level vdevs that are not missing and are not logs. At every | |
1867 | * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds. | |
1868 | */ | |
1869 | static uint64_t | |
1870 | spa_healthy_core_tvds(spa_t *spa) | |
1871 | { | |
1872 | vdev_t *rvd = spa->spa_root_vdev; | |
1873 | uint64_t tvds = 0; | |
1874 | ||
1875 | for (uint64_t i = 0; i < rvd->vdev_children; i++) { | |
1876 | vdev_t *vd = rvd->vdev_child[i]; | |
1877 | if (vd->vdev_islog) | |
1878 | continue; | |
1879 | if (vdev_is_concrete(vd) && !vdev_is_dead(vd)) | |
1880 | tvds++; | |
1881 | } | |
1882 | ||
1883 | return (tvds); | |
1884 | } | |
1885 | ||
34dc7c2f BB |
1886 | /* |
1887 | * Checks to see if the given vdev could not be opened, in which case we post a | |
1888 | * sysevent to notify the autoreplace code that the device has been removed. | |
1889 | */ | |
1890 | static void | |
1891 | spa_check_removed(vdev_t *vd) | |
1892 | { | |
6cb8e530 | 1893 | for (uint64_t c = 0; c < vd->vdev_children; c++) |
34dc7c2f BB |
1894 | spa_check_removed(vd->vdev_child[c]); |
1895 | ||
7011fb60 | 1896 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) && |
a1d477c2 | 1897 | vdev_is_concrete(vd)) { |
fb390aaf | 1898 | zfs_post_autoreplace(vd->vdev_spa, vd); |
12fa0466 | 1899 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_CHECK); |
34dc7c2f BB |
1900 | } |
1901 | } | |
1902 | ||
6cb8e530 PZ |
1903 | static int |
1904 | spa_check_for_missing_logs(spa_t *spa) | |
9babb374 | 1905 | { |
6cb8e530 | 1906 | vdev_t *rvd = spa->spa_root_vdev; |
9babb374 | 1907 | |
428870ff | 1908 | /* |
572e2857 | 1909 | * If we're doing a normal import, then build up any additional |
6cb8e530 | 1910 | * diagnostic information about missing log devices. |
572e2857 | 1911 | * We'll pass this up to the user for further processing. |
428870ff | 1912 | */ |
572e2857 BB |
1913 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { |
1914 | nvlist_t **child, *nv; | |
1915 | uint64_t idx = 0; | |
1916 | ||
160987b5 | 1917 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t *), |
79c76d5b BB |
1918 | KM_SLEEP); |
1919 | VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
572e2857 | 1920 | |
6cb8e530 | 1921 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { |
572e2857 | 1922 | vdev_t *tvd = rvd->vdev_child[c]; |
572e2857 | 1923 | |
6cb8e530 PZ |
1924 | /* |
1925 | * We consider a device as missing only if it failed | |
1926 | * to open (i.e. offline or faulted is not considered | |
1927 | * as missing). | |
1928 | */ | |
1929 | if (tvd->vdev_islog && | |
1930 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
1931 | child[idx++] = vdev_config_generate(spa, tvd, | |
1932 | B_FALSE, VDEV_CONFIG_MISSING); | |
1933 | } | |
572e2857 | 1934 | } |
9babb374 | 1935 | |
6cb8e530 PZ |
1936 | if (idx > 0) { |
1937 | fnvlist_add_nvlist_array(nv, | |
1938 | ZPOOL_CONFIG_CHILDREN, child, idx); | |
1939 | fnvlist_add_nvlist(spa->spa_load_info, | |
1940 | ZPOOL_CONFIG_MISSING_DEVICES, nv); | |
572e2857 | 1941 | |
6cb8e530 | 1942 | for (uint64_t i = 0; i < idx; i++) |
572e2857 BB |
1943 | nvlist_free(child[i]); |
1944 | } | |
1945 | nvlist_free(nv); | |
1946 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
572e2857 | 1947 | |
6cb8e530 PZ |
1948 | if (idx > 0) { |
1949 | spa_load_failed(spa, "some log devices are missing"); | |
db7d07e1 | 1950 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 PZ |
1951 | return (SET_ERROR(ENXIO)); |
1952 | } | |
1953 | } else { | |
1954 | for (uint64_t c = 0; c < rvd->vdev_children; c++) { | |
1955 | vdev_t *tvd = rvd->vdev_child[c]; | |
a1d477c2 | 1956 | |
6cb8e530 PZ |
1957 | if (tvd->vdev_islog && |
1958 | tvd->vdev_state == VDEV_STATE_CANT_OPEN) { | |
572e2857 | 1959 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
6cb8e530 PZ |
1960 | spa_load_note(spa, "some log devices are " |
1961 | "missing, ZIL is dropped."); | |
db7d07e1 | 1962 | vdev_dbgmsg_print_tree(rvd, 2); |
6cb8e530 | 1963 | break; |
e0ab3ab5 | 1964 | } |
572e2857 | 1965 | } |
9babb374 | 1966 | } |
e0ab3ab5 | 1967 | |
6cb8e530 | 1968 | return (0); |
9babb374 BB |
1969 | } |
1970 | ||
b128c09f BB |
1971 | /* |
1972 | * Check for missing log devices | |
1973 | */ | |
13fe0198 | 1974 | static boolean_t |
b128c09f BB |
1975 | spa_check_logs(spa_t *spa) |
1976 | { | |
13fe0198 | 1977 | boolean_t rv = B_FALSE; |
9c43027b | 1978 | dsl_pool_t *dp = spa_get_dsl(spa); |
13fe0198 | 1979 | |
b128c09f | 1980 | switch (spa->spa_log_state) { |
e75c13c3 BB |
1981 | default: |
1982 | break; | |
b128c09f BB |
1983 | case SPA_LOG_MISSING: |
1984 | /* need to recheck in case slog has been restored */ | |
1985 | case SPA_LOG_UNKNOWN: | |
9c43027b AJ |
1986 | rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj, |
1987 | zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0); | |
13fe0198 | 1988 | if (rv) |
428870ff | 1989 | spa_set_log_state(spa, SPA_LOG_MISSING); |
b128c09f | 1990 | break; |
b128c09f | 1991 | } |
13fe0198 | 1992 | return (rv); |
b128c09f BB |
1993 | } |
1994 | ||
428870ff BB |
1995 | static boolean_t |
1996 | spa_passivate_log(spa_t *spa) | |
34dc7c2f | 1997 | { |
428870ff BB |
1998 | vdev_t *rvd = spa->spa_root_vdev; |
1999 | boolean_t slog_found = B_FALSE; | |
b128c09f | 2000 | |
428870ff | 2001 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
fb5f0bc8 | 2002 | |
428870ff BB |
2003 | if (!spa_has_slogs(spa)) |
2004 | return (B_FALSE); | |
34dc7c2f | 2005 | |
1c27024e | 2006 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
2007 | vdev_t *tvd = rvd->vdev_child[c]; |
2008 | metaslab_group_t *mg = tvd->vdev_mg; | |
34dc7c2f | 2009 | |
428870ff BB |
2010 | if (tvd->vdev_islog) { |
2011 | metaslab_group_passivate(mg); | |
2012 | slog_found = B_TRUE; | |
2013 | } | |
34dc7c2f BB |
2014 | } |
2015 | ||
428870ff BB |
2016 | return (slog_found); |
2017 | } | |
34dc7c2f | 2018 | |
428870ff BB |
2019 | static void |
2020 | spa_activate_log(spa_t *spa) | |
2021 | { | |
2022 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 2023 | |
428870ff BB |
2024 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
2025 | ||
1c27024e | 2026 | for (int c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
2027 | vdev_t *tvd = rvd->vdev_child[c]; |
2028 | metaslab_group_t *mg = tvd->vdev_mg; | |
2029 | ||
2030 | if (tvd->vdev_islog) | |
2031 | metaslab_group_activate(mg); | |
34dc7c2f | 2032 | } |
428870ff | 2033 | } |
34dc7c2f | 2034 | |
428870ff | 2035 | int |
a1d477c2 | 2036 | spa_reset_logs(spa_t *spa) |
428870ff | 2037 | { |
13fe0198 | 2038 | int error; |
9babb374 | 2039 | |
a1d477c2 | 2040 | error = dmu_objset_find(spa_name(spa), zil_reset, |
13fe0198 MA |
2041 | NULL, DS_FIND_CHILDREN); |
2042 | if (error == 0) { | |
428870ff BB |
2043 | /* |
2044 | * We successfully offlined the log device, sync out the | |
2045 | * current txg so that the "stubby" block can be removed | |
2046 | * by zil_sync(). | |
2047 | */ | |
2048 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
2049 | } | |
2050 | return (error); | |
2051 | } | |
34dc7c2f | 2052 | |
428870ff BB |
2053 | static void |
2054 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
2055 | { | |
1c27024e | 2056 | for (int i = 0; i < sav->sav_count; i++) |
428870ff BB |
2057 | spa_check_removed(sav->sav_vdevs[i]); |
2058 | } | |
34dc7c2f | 2059 | |
428870ff BB |
2060 | void |
2061 | spa_claim_notify(zio_t *zio) | |
2062 | { | |
2063 | spa_t *spa = zio->io_spa; | |
34dc7c2f | 2064 | |
428870ff BB |
2065 | if (zio->io_error) |
2066 | return; | |
34dc7c2f | 2067 | |
428870ff BB |
2068 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ |
2069 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
2070 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
2071 | mutex_exit(&spa->spa_props_lock); | |
2072 | } | |
34dc7c2f | 2073 | |
428870ff BB |
2074 | typedef struct spa_load_error { |
2075 | uint64_t sle_meta_count; | |
2076 | uint64_t sle_data_count; | |
2077 | } spa_load_error_t; | |
34dc7c2f | 2078 | |
428870ff BB |
2079 | static void |
2080 | spa_load_verify_done(zio_t *zio) | |
2081 | { | |
2082 | blkptr_t *bp = zio->io_bp; | |
2083 | spa_load_error_t *sle = zio->io_private; | |
2084 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
2085 | int error = zio->io_error; | |
dea377c0 | 2086 | spa_t *spa = zio->io_spa; |
34dc7c2f | 2087 | |
a6255b7f | 2088 | abd_free(zio->io_abd); |
428870ff | 2089 | if (error) { |
9ae529ec | 2090 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && |
428870ff | 2091 | type != DMU_OT_INTENT_LOG) |
bc89ac84 | 2092 | atomic_inc_64(&sle->sle_meta_count); |
428870ff | 2093 | else |
bc89ac84 | 2094 | atomic_inc_64(&sle->sle_data_count); |
34dc7c2f | 2095 | } |
dea377c0 MA |
2096 | |
2097 | mutex_enter(&spa->spa_scrub_lock); | |
d4a72f23 | 2098 | spa->spa_load_verify_ios--; |
dea377c0 MA |
2099 | cv_broadcast(&spa->spa_scrub_io_cv); |
2100 | mutex_exit(&spa->spa_scrub_lock); | |
428870ff | 2101 | } |
34dc7c2f | 2102 | |
dea377c0 MA |
2103 | /* |
2104 | * Maximum number of concurrent scrub i/os to create while verifying | |
2105 | * a pool while importing it. | |
2106 | */ | |
2107 | int spa_load_verify_maxinflight = 10000; | |
2108 | int spa_load_verify_metadata = B_TRUE; | |
2109 | int spa_load_verify_data = B_TRUE; | |
2110 | ||
428870ff BB |
2111 | /*ARGSUSED*/ |
2112 | static int | |
2113 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
5dbd68a3 | 2114 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) |
428870ff | 2115 | { |
fcff0f35 | 2116 | if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) |
dea377c0 MA |
2117 | return (0); |
2118 | /* | |
2119 | * Note: normally this routine will not be called if | |
2120 | * spa_load_verify_metadata is not set. However, it may be useful | |
2121 | * to manually set the flag after the traversal has begun. | |
2122 | */ | |
2123 | if (!spa_load_verify_metadata) | |
2124 | return (0); | |
a6255b7f | 2125 | if (!BP_IS_METADATA(bp) && !spa_load_verify_data) |
dea377c0 MA |
2126 | return (0); |
2127 | ||
1c27024e DB |
2128 | zio_t *rio = arg; |
2129 | size_t size = BP_GET_PSIZE(bp); | |
dea377c0 MA |
2130 | |
2131 | mutex_enter(&spa->spa_scrub_lock); | |
d4a72f23 | 2132 | while (spa->spa_load_verify_ios >= spa_load_verify_maxinflight) |
dea377c0 | 2133 | cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); |
d4a72f23 | 2134 | spa->spa_load_verify_ios++; |
dea377c0 MA |
2135 | mutex_exit(&spa->spa_scrub_lock); |
2136 | ||
a6255b7f | 2137 | zio_nowait(zio_read(rio, spa, bp, abd_alloc_for_io(size, B_FALSE), size, |
dea377c0 MA |
2138 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, |
2139 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
2140 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
428870ff BB |
2141 | return (0); |
2142 | } | |
34dc7c2f | 2143 | |
d1d19c78 PD |
2144 | /* ARGSUSED */ |
2145 | int | |
2146 | verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) | |
2147 | { | |
2148 | if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN) | |
2149 | return (SET_ERROR(ENAMETOOLONG)); | |
2150 | ||
2151 | return (0); | |
2152 | } | |
2153 | ||
428870ff BB |
2154 | static int |
2155 | spa_load_verify(spa_t *spa) | |
2156 | { | |
2157 | zio_t *rio; | |
2158 | spa_load_error_t sle = { 0 }; | |
8a393be3 | 2159 | zpool_load_policy_t policy; |
428870ff | 2160 | boolean_t verify_ok = B_FALSE; |
dea377c0 | 2161 | int error = 0; |
34dc7c2f | 2162 | |
8a393be3 | 2163 | zpool_get_load_policy(spa->spa_config, &policy); |
34dc7c2f | 2164 | |
8a393be3 | 2165 | if (policy.zlp_rewind & ZPOOL_NEVER_REWIND) |
428870ff | 2166 | return (0); |
34dc7c2f | 2167 | |
d1d19c78 PD |
2168 | dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); |
2169 | error = dmu_objset_find_dp(spa->spa_dsl_pool, | |
2170 | spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL, | |
2171 | DS_FIND_CHILDREN); | |
2172 | dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); | |
2173 | if (error != 0) | |
2174 | return (error); | |
2175 | ||
428870ff BB |
2176 | rio = zio_root(spa, NULL, &sle, |
2177 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
34dc7c2f | 2178 | |
dea377c0 | 2179 | if (spa_load_verify_metadata) { |
4a0ee12a PZ |
2180 | if (spa->spa_extreme_rewind) { |
2181 | spa_load_note(spa, "performing a complete scan of the " | |
2182 | "pool since extreme rewind is on. This may take " | |
2183 | "a very long time.\n (spa_load_verify_data=%u, " | |
2184 | "spa_load_verify_metadata=%u)", | |
2185 | spa_load_verify_data, spa_load_verify_metadata); | |
2186 | } | |
dea377c0 | 2187 | error = traverse_pool(spa, spa->spa_verify_min_txg, |
b5256303 TC |
2188 | TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | |
2189 | TRAVERSE_NO_DECRYPT, spa_load_verify_cb, rio); | |
dea377c0 | 2190 | } |
428870ff BB |
2191 | |
2192 | (void) zio_wait(rio); | |
2193 | ||
2194 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
2195 | spa->spa_load_data_errors = sle.sle_data_count; | |
2196 | ||
afd2f7b7 PZ |
2197 | if (sle.sle_meta_count != 0 || sle.sle_data_count != 0) { |
2198 | spa_load_note(spa, "spa_load_verify found %llu metadata errors " | |
2199 | "and %llu data errors", (u_longlong_t)sle.sle_meta_count, | |
2200 | (u_longlong_t)sle.sle_data_count); | |
2201 | } | |
2202 | ||
2203 | if (spa_load_verify_dryrun || | |
8a393be3 PZ |
2204 | (!error && sle.sle_meta_count <= policy.zlp_maxmeta && |
2205 | sle.sle_data_count <= policy.zlp_maxdata)) { | |
572e2857 BB |
2206 | int64_t loss = 0; |
2207 | ||
428870ff BB |
2208 | verify_ok = B_TRUE; |
2209 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
2210 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
572e2857 BB |
2211 | |
2212 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
2213 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2214 | ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); | |
2215 | VERIFY(nvlist_add_int64(spa->spa_load_info, | |
2216 | ZPOOL_CONFIG_REWIND_TIME, loss) == 0); | |
2217 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
2218 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); | |
428870ff BB |
2219 | } else { |
2220 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
2221 | } | |
2222 | ||
afd2f7b7 PZ |
2223 | if (spa_load_verify_dryrun) |
2224 | return (0); | |
2225 | ||
428870ff BB |
2226 | if (error) { |
2227 | if (error != ENXIO && error != EIO) | |
2e528b49 | 2228 | error = SET_ERROR(EIO); |
428870ff BB |
2229 | return (error); |
2230 | } | |
2231 | ||
2232 | return (verify_ok ? 0 : EIO); | |
2233 | } | |
2234 | ||
2235 | /* | |
2236 | * Find a value in the pool props object. | |
2237 | */ | |
2238 | static void | |
2239 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
2240 | { | |
2241 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
2242 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
2243 | } | |
2244 | ||
2245 | /* | |
2246 | * Find a value in the pool directory object. | |
2247 | */ | |
2248 | static int | |
4a0ee12a | 2249 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val, boolean_t log_enoent) |
428870ff | 2250 | { |
4a0ee12a PZ |
2251 | int error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, |
2252 | name, sizeof (uint64_t), 1, val); | |
2253 | ||
2254 | if (error != 0 && (error != ENOENT || log_enoent)) { | |
2255 | spa_load_failed(spa, "couldn't get '%s' value in MOS directory " | |
2256 | "[error=%d]", name, error); | |
2257 | } | |
2258 | ||
2259 | return (error); | |
428870ff BB |
2260 | } |
2261 | ||
2262 | static int | |
2263 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
2264 | { | |
2265 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
a1d477c2 | 2266 | return (SET_ERROR(err)); |
428870ff BB |
2267 | } |
2268 | ||
9d5b5245 SD |
2269 | static void |
2270 | spa_spawn_aux_threads(spa_t *spa) | |
2271 | { | |
2272 | ASSERT(spa_writeable(spa)); | |
2273 | ||
2274 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2275 | ||
2276 | spa_start_indirect_condensing_thread(spa); | |
d2734cce SD |
2277 | |
2278 | ASSERT3P(spa->spa_checkpoint_discard_zthr, ==, NULL); | |
2279 | spa->spa_checkpoint_discard_zthr = | |
2280 | zthr_create(spa_checkpoint_discard_thread_check, | |
2281 | spa_checkpoint_discard_thread, spa); | |
9d5b5245 SD |
2282 | } |
2283 | ||
428870ff BB |
2284 | /* |
2285 | * Fix up config after a partly-completed split. This is done with the | |
2286 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
2287 | * pool have that entry in their config, but only the splitting one contains | |
2288 | * a list of all the guids of the vdevs that are being split off. | |
2289 | * | |
2290 | * This function determines what to do with that list: either rejoin | |
2291 | * all the disks to the pool, or complete the splitting process. To attempt | |
2292 | * the rejoin, each disk that is offlined is marked online again, and | |
2293 | * we do a reopen() call. If the vdev label for every disk that was | |
2294 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
2295 | * then we call vdev_split() on each disk, and complete the split. | |
2296 | * | |
2297 | * Otherwise we leave the config alone, with all the vdevs in place in | |
2298 | * the original pool. | |
2299 | */ | |
2300 | static void | |
2301 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
2302 | { | |
2303 | uint_t extracted; | |
2304 | uint64_t *glist; | |
2305 | uint_t i, gcount; | |
2306 | nvlist_t *nvl; | |
2307 | vdev_t **vd; | |
2308 | boolean_t attempt_reopen; | |
2309 | ||
2310 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
2311 | return; | |
2312 | ||
2313 | /* check that the config is complete */ | |
2314 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
2315 | &glist, &gcount) != 0) | |
2316 | return; | |
2317 | ||
79c76d5b | 2318 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP); |
428870ff BB |
2319 | |
2320 | /* attempt to online all the vdevs & validate */ | |
2321 | attempt_reopen = B_TRUE; | |
2322 | for (i = 0; i < gcount; i++) { | |
2323 | if (glist[i] == 0) /* vdev is hole */ | |
2324 | continue; | |
2325 | ||
2326 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
2327 | if (vd[i] == NULL) { | |
2328 | /* | |
2329 | * Don't bother attempting to reopen the disks; | |
2330 | * just do the split. | |
2331 | */ | |
2332 | attempt_reopen = B_FALSE; | |
2333 | } else { | |
2334 | /* attempt to re-online it */ | |
2335 | vd[i]->vdev_offline = B_FALSE; | |
2336 | } | |
2337 | } | |
2338 | ||
2339 | if (attempt_reopen) { | |
2340 | vdev_reopen(spa->spa_root_vdev); | |
2341 | ||
2342 | /* check each device to see what state it's in */ | |
2343 | for (extracted = 0, i = 0; i < gcount; i++) { | |
2344 | if (vd[i] != NULL && | |
2345 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
2346 | break; | |
2347 | ++extracted; | |
2348 | } | |
2349 | } | |
2350 | ||
2351 | /* | |
2352 | * If every disk has been moved to the new pool, or if we never | |
2353 | * even attempted to look at them, then we split them off for | |
2354 | * good. | |
2355 | */ | |
2356 | if (!attempt_reopen || gcount == extracted) { | |
2357 | for (i = 0; i < gcount; i++) | |
2358 | if (vd[i] != NULL) | |
2359 | vdev_split(vd[i]); | |
2360 | vdev_reopen(spa->spa_root_vdev); | |
2361 | } | |
2362 | ||
2363 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
2364 | } | |
2365 | ||
2366 | static int | |
6cb8e530 | 2367 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type) |
428870ff | 2368 | { |
428870ff BB |
2369 | char *ereport = FM_EREPORT_ZFS_POOL; |
2370 | int error; | |
428870ff | 2371 | |
6cb8e530 | 2372 | spa->spa_load_state = state; |
9ae529ec | 2373 | |
6cb8e530 | 2374 | gethrestime(&spa->spa_loaded_ts); |
d2734cce | 2375 | error = spa_load_impl(spa, type, &ereport); |
428870ff | 2376 | |
0c66c32d JG |
2377 | /* |
2378 | * Don't count references from objsets that are already closed | |
2379 | * and are making their way through the eviction process. | |
2380 | */ | |
2381 | spa_evicting_os_wait(spa); | |
424fd7c3 | 2382 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
572e2857 BB |
2383 | if (error) { |
2384 | if (error != EEXIST) { | |
2385 | spa->spa_loaded_ts.tv_sec = 0; | |
2386 | spa->spa_loaded_ts.tv_nsec = 0; | |
2387 | } | |
2388 | if (error != EBADF) { | |
b5256303 | 2389 | zfs_ereport_post(ereport, spa, NULL, NULL, NULL, 0, 0); |
572e2857 BB |
2390 | } |
2391 | } | |
428870ff BB |
2392 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; |
2393 | spa->spa_ena = 0; | |
2394 | ||
2395 | return (error); | |
2396 | } | |
2397 | ||
33cf67cd | 2398 | #ifdef ZFS_DEBUG |
e0ab3ab5 JS |
2399 | /* |
2400 | * Count the number of per-vdev ZAPs associated with all of the vdevs in the | |
2401 | * vdev tree rooted in the given vd, and ensure that each ZAP is present in the | |
2402 | * spa's per-vdev ZAP list. | |
2403 | */ | |
2404 | static uint64_t | |
2405 | vdev_count_verify_zaps(vdev_t *vd) | |
2406 | { | |
2407 | spa_t *spa = vd->vdev_spa; | |
2408 | uint64_t total = 0; | |
e0ab3ab5 JS |
2409 | |
2410 | if (vd->vdev_top_zap != 0) { | |
2411 | total++; | |
2412 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2413 | spa->spa_all_vdev_zaps, vd->vdev_top_zap)); | |
2414 | } | |
2415 | if (vd->vdev_leaf_zap != 0) { | |
2416 | total++; | |
2417 | ASSERT0(zap_lookup_int(spa->spa_meta_objset, | |
2418 | spa->spa_all_vdev_zaps, vd->vdev_leaf_zap)); | |
2419 | } | |
2420 | ||
1c27024e | 2421 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
2422 | total += vdev_count_verify_zaps(vd->vdev_child[i]); |
2423 | } | |
2424 | ||
2425 | return (total); | |
2426 | } | |
33cf67cd | 2427 | #endif |
e0ab3ab5 | 2428 | |
379ca9cf OF |
2429 | /* |
2430 | * Determine whether the activity check is required. | |
2431 | */ | |
2432 | static boolean_t | |
bbffb59e BB |
2433 | spa_activity_check_required(spa_t *spa, uberblock_t *ub, nvlist_t *label, |
2434 | nvlist_t *config) | |
379ca9cf OF |
2435 | { |
2436 | uint64_t state = 0; | |
2437 | uint64_t hostid = 0; | |
2438 | uint64_t tryconfig_txg = 0; | |
2439 | uint64_t tryconfig_timestamp = 0; | |
2440 | nvlist_t *nvinfo; | |
2441 | ||
2442 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
2443 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
2444 | (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, | |
2445 | &tryconfig_txg); | |
2446 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
2447 | &tryconfig_timestamp); | |
2448 | } | |
2449 | ||
2450 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); | |
379ca9cf OF |
2451 | |
2452 | /* | |
2453 | * Disable the MMP activity check - This is used by zdb which | |
2454 | * is intended to be used on potentially active pools. | |
2455 | */ | |
2456 | if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) | |
2457 | return (B_FALSE); | |
2458 | ||
2459 | /* | |
2460 | * Skip the activity check when the MMP feature is disabled. | |
2461 | */ | |
2462 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) | |
2463 | return (B_FALSE); | |
2464 | /* | |
2465 | * If the tryconfig_* values are nonzero, they are the results of an | |
2466 | * earlier tryimport. If they match the uberblock we just found, then | |
2467 | * the pool has not changed and we return false so we do not test a | |
2468 | * second time. | |
2469 | */ | |
2470 | if (tryconfig_txg && tryconfig_txg == ub->ub_txg && | |
2471 | tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp) | |
2472 | return (B_FALSE); | |
2473 | ||
2474 | /* | |
2475 | * Allow the activity check to be skipped when importing the pool | |
bbffb59e BB |
2476 | * on the same host which last imported it. Since the hostid from |
2477 | * configuration may be stale use the one read from the label. | |
379ca9cf | 2478 | */ |
bbffb59e BB |
2479 | if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) |
2480 | hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); | |
2481 | ||
379ca9cf OF |
2482 | if (hostid == spa_get_hostid()) |
2483 | return (B_FALSE); | |
2484 | ||
2485 | /* | |
2486 | * Skip the activity test when the pool was cleanly exported. | |
2487 | */ | |
2488 | if (state != POOL_STATE_ACTIVE) | |
2489 | return (B_FALSE); | |
2490 | ||
2491 | return (B_TRUE); | |
2492 | } | |
2493 | ||
2494 | /* | |
2495 | * Perform the import activity check. If the user canceled the import or | |
2496 | * we detected activity then fail. | |
2497 | */ | |
2498 | static int | |
2499 | spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) | |
2500 | { | |
2501 | uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); | |
2502 | uint64_t txg = ub->ub_txg; | |
2503 | uint64_t timestamp = ub->ub_timestamp; | |
2504 | uint64_t import_delay = NANOSEC; | |
2505 | hrtime_t import_expire; | |
2506 | nvlist_t *mmp_label = NULL; | |
2507 | vdev_t *rvd = spa->spa_root_vdev; | |
2508 | kcondvar_t cv; | |
2509 | kmutex_t mtx; | |
2510 | int error = 0; | |
2511 | ||
2512 | cv_init(&cv, NULL, CV_DEFAULT, NULL); | |
2513 | mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); | |
2514 | mutex_enter(&mtx); | |
2515 | ||
2516 | /* | |
2517 | * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed | |
2518 | * during the earlier tryimport. If the txg recorded there is 0 then | |
2519 | * the pool is known to be active on another host. | |
2520 | * | |
2521 | * Otherwise, the pool might be in use on another node. Check for | |
2522 | * changes in the uberblocks on disk if necessary. | |
2523 | */ | |
2524 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
2525 | nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, | |
2526 | ZPOOL_CONFIG_LOAD_INFO); | |
2527 | ||
2528 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && | |
2529 | fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { | |
2530 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
2531 | error = SET_ERROR(EREMOTEIO); | |
2532 | goto out; | |
2533 | } | |
2534 | } | |
2535 | ||
2536 | /* | |
2537 | * Preferentially use the zfs_multihost_interval from the node which | |
2538 | * last imported the pool. This value is stored in an MMP uberblock as. | |
2539 | * | |
2540 | * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval | |
2541 | */ | |
2542 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay) | |
2543 | import_delay = MAX(import_delay, import_intervals * | |
802ae562 | 2544 | ub->ub_mmp_delay * MAX(vdev_count_leaves(spa), 1)); |
379ca9cf OF |
2545 | |
2546 | /* Apply a floor using the local default values. */ | |
2547 | import_delay = MAX(import_delay, import_intervals * | |
2548 | MSEC2NSEC(MAX(zfs_multihost_interval, MMP_MIN_INTERVAL))); | |
2549 | ||
533ea041 OF |
2550 | zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu import_intervals=%u " |
2551 | "leaves=%u", import_delay, ub->ub_mmp_delay, import_intervals, | |
2552 | vdev_count_leaves(spa)); | |
2553 | ||
379ca9cf OF |
2554 | /* Add a small random factor in case of simultaneous imports (0-25%) */ |
2555 | import_expire = gethrtime() + import_delay + | |
2556 | (import_delay * spa_get_random(250) / 1000); | |
2557 | ||
2558 | while (gethrtime() < import_expire) { | |
2559 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
2560 | ||
2561 | if (txg != ub->ub_txg || timestamp != ub->ub_timestamp) { | |
2562 | error = SET_ERROR(EREMOTEIO); | |
2563 | break; | |
2564 | } | |
2565 | ||
2566 | if (mmp_label) { | |
2567 | nvlist_free(mmp_label); | |
2568 | mmp_label = NULL; | |
2569 | } | |
2570 | ||
2571 | error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); | |
2572 | if (error != -1) { | |
2573 | error = SET_ERROR(EINTR); | |
2574 | break; | |
2575 | } | |
2576 | error = 0; | |
2577 | } | |
2578 | ||
2579 | out: | |
2580 | mutex_exit(&mtx); | |
2581 | mutex_destroy(&mtx); | |
2582 | cv_destroy(&cv); | |
2583 | ||
2584 | /* | |
2585 | * If the pool is determined to be active store the status in the | |
2586 | * spa->spa_load_info nvlist. If the remote hostname or hostid are | |
2587 | * available from configuration read from disk store them as well. | |
2588 | * This allows 'zpool import' to generate a more useful message. | |
2589 | * | |
2590 | * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) | |
2591 | * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool | |
2592 | * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool | |
2593 | */ | |
2594 | if (error == EREMOTEIO) { | |
2595 | char *hostname = "<unknown>"; | |
2596 | uint64_t hostid = 0; | |
2597 | ||
2598 | if (mmp_label) { | |
2599 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { | |
2600 | hostname = fnvlist_lookup_string(mmp_label, | |
2601 | ZPOOL_CONFIG_HOSTNAME); | |
2602 | fnvlist_add_string(spa->spa_load_info, | |
2603 | ZPOOL_CONFIG_MMP_HOSTNAME, hostname); | |
2604 | } | |
2605 | ||
2606 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { | |
2607 | hostid = fnvlist_lookup_uint64(mmp_label, | |
2608 | ZPOOL_CONFIG_HOSTID); | |
2609 | fnvlist_add_uint64(spa->spa_load_info, | |
2610 | ZPOOL_CONFIG_MMP_HOSTID, hostid); | |
2611 | } | |
2612 | } | |
2613 | ||
2614 | fnvlist_add_uint64(spa->spa_load_info, | |
2615 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); | |
2616 | fnvlist_add_uint64(spa->spa_load_info, | |
2617 | ZPOOL_CONFIG_MMP_TXG, 0); | |
2618 | ||
2619 | error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); | |
2620 | } | |
2621 | ||
2622 | if (mmp_label) | |
2623 | nvlist_free(mmp_label); | |
2624 | ||
2625 | return (error); | |
2626 | } | |
2627 | ||
9eb7b46e | 2628 | static int |
6cb8e530 PZ |
2629 | spa_verify_host(spa_t *spa, nvlist_t *mos_config) |
2630 | { | |
2631 | uint64_t hostid; | |
2632 | char *hostname; | |
2633 | uint64_t myhostid = 0; | |
2634 | ||
2635 | if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, | |
2636 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
2637 | hostname = fnvlist_lookup_string(mos_config, | |
2638 | ZPOOL_CONFIG_HOSTNAME); | |
2639 | ||
2640 | myhostid = zone_get_hostid(NULL); | |
2641 | ||
2642 | if (hostid != 0 && myhostid != 0 && hostid != myhostid) { | |
2643 | cmn_err(CE_WARN, "pool '%s' could not be " | |
2644 | "loaded as it was last accessed by " | |
2645 | "another system (host: %s hostid: 0x%llx). " | |
2646 | "See: http://illumos.org/msg/ZFS-8000-EY", | |
2647 | spa_name(spa), hostname, (u_longlong_t)hostid); | |
2648 | spa_load_failed(spa, "hostid verification failed: pool " | |
2649 | "last accessed by host: %s (hostid: 0x%llx)", | |
2650 | hostname, (u_longlong_t)hostid); | |
2651 | return (SET_ERROR(EBADF)); | |
2652 | } | |
2653 | } | |
2654 | ||
2655 | return (0); | |
2656 | } | |
2657 | ||
2658 | static int | |
2659 | spa_ld_parse_config(spa_t *spa, spa_import_type_t type) | |
428870ff BB |
2660 | { |
2661 | int error = 0; | |
6cb8e530 | 2662 | nvlist_t *nvtree, *nvl, *config = spa->spa_config; |
1c27024e | 2663 | int parse; |
9eb7b46e | 2664 | vdev_t *rvd; |
6cb8e530 PZ |
2665 | uint64_t pool_guid; |
2666 | char *comment; | |
2667 | ||
2668 | /* | |
2669 | * Versioning wasn't explicitly added to the label until later, so if | |
2670 | * it's not present treat it as the initial version. | |
2671 | */ | |
2672 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
2673 | &spa->spa_ubsync.ub_version) != 0) | |
2674 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
2675 | ||
2676 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { | |
2677 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
2678 | ZPOOL_CONFIG_POOL_GUID); | |
2679 | return (SET_ERROR(EINVAL)); | |
2680 | } | |
2681 | ||
d2734cce SD |
2682 | /* |
2683 | * If we are doing an import, ensure that the pool is not already | |
2684 | * imported by checking if its pool guid already exists in the | |
2685 | * spa namespace. | |
2686 | * | |
2687 | * The only case that we allow an already imported pool to be | |
2688 | * imported again, is when the pool is checkpointed and we want to | |
2689 | * look at its checkpointed state from userland tools like zdb. | |
2690 | */ | |
2691 | #ifdef _KERNEL | |
2692 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
2693 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
2694 | spa_guid_exists(pool_guid, 0)) { | |
2695 | #else | |
2696 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
2697 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
2698 | spa_guid_exists(pool_guid, 0) && | |
2699 | !spa_importing_readonly_checkpoint(spa)) { | |
2700 | #endif | |
6cb8e530 PZ |
2701 | spa_load_failed(spa, "a pool with guid %llu is already open", |
2702 | (u_longlong_t)pool_guid); | |
2703 | return (SET_ERROR(EEXIST)); | |
2704 | } | |
2705 | ||
2706 | spa->spa_config_guid = pool_guid; | |
2707 | ||
2708 | nvlist_free(spa->spa_load_info); | |
2709 | spa->spa_load_info = fnvlist_alloc(); | |
2710 | ||
2711 | ASSERT(spa->spa_comment == NULL); | |
2712 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
2713 | spa->spa_comment = spa_strdup(comment); | |
2714 | ||
2715 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
2716 | &spa->spa_config_txg); | |
2717 | ||
2718 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) | |
2719 | spa->spa_config_splitting = fnvlist_dup(nvl); | |
428870ff | 2720 | |
4a0ee12a PZ |
2721 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { |
2722 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
2723 | ZPOOL_CONFIG_VDEV_TREE); | |
2e528b49 | 2724 | return (SET_ERROR(EINVAL)); |
4a0ee12a | 2725 | } |
428870ff | 2726 | |
428870ff BB |
2727 | /* |
2728 | * Create "The Godfather" zio to hold all async IOs | |
2729 | */ | |
e022864d MA |
2730 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
2731 | KM_SLEEP); | |
1c27024e | 2732 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
2733 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
2734 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
2735 | ZIO_FLAG_GODFATHER); | |
2736 | } | |
428870ff BB |
2737 | |
2738 | /* | |
2739 | * Parse the configuration into a vdev tree. We explicitly set the | |
2740 | * value that will be returned by spa_version() since parsing the | |
2741 | * configuration requires knowing the version number. | |
2742 | */ | |
2743 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 PZ |
2744 | parse = (type == SPA_IMPORT_EXISTING ? |
2745 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
9eb7b46e | 2746 | error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); |
428870ff BB |
2747 | spa_config_exit(spa, SCL_ALL, FTAG); |
2748 | ||
4a0ee12a PZ |
2749 | if (error != 0) { |
2750 | spa_load_failed(spa, "unable to parse config [error=%d]", | |
2751 | error); | |
428870ff | 2752 | return (error); |
4a0ee12a | 2753 | } |
428870ff BB |
2754 | |
2755 | ASSERT(spa->spa_root_vdev == rvd); | |
c3520e7f MA |
2756 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); |
2757 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
428870ff BB |
2758 | |
2759 | if (type != SPA_IMPORT_ASSEMBLE) { | |
2760 | ASSERT(spa_guid(spa) == pool_guid); | |
2761 | } | |
2762 | ||
9eb7b46e PZ |
2763 | return (0); |
2764 | } | |
2765 | ||
6cb8e530 PZ |
2766 | /* |
2767 | * Recursively open all vdevs in the vdev tree. This function is called twice: | |
2768 | * first with the untrusted config, then with the trusted config. | |
2769 | */ | |
9eb7b46e PZ |
2770 | static int |
2771 | spa_ld_open_vdevs(spa_t *spa) | |
2772 | { | |
2773 | int error = 0; | |
2774 | ||
6cb8e530 PZ |
2775 | /* |
2776 | * spa_missing_tvds_allowed defines how many top-level vdevs can be | |
2777 | * missing/unopenable for the root vdev to be still considered openable. | |
2778 | */ | |
2779 | if (spa->spa_trust_config) { | |
2780 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; | |
2781 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { | |
2782 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; | |
2783 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { | |
2784 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; | |
2785 | } else { | |
2786 | spa->spa_missing_tvds_allowed = 0; | |
2787 | } | |
2788 | ||
2789 | spa->spa_missing_tvds_allowed = | |
2790 | MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); | |
2791 | ||
428870ff | 2792 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9eb7b46e | 2793 | error = vdev_open(spa->spa_root_vdev); |
428870ff | 2794 | spa_config_exit(spa, SCL_ALL, FTAG); |
6cb8e530 PZ |
2795 | |
2796 | if (spa->spa_missing_tvds != 0) { | |
2797 | spa_load_note(spa, "vdev tree has %lld missing top-level " | |
2798 | "vdevs.", (u_longlong_t)spa->spa_missing_tvds); | |
2799 | if (spa->spa_trust_config && (spa->spa_mode & FWRITE)) { | |
2800 | /* | |
2801 | * Although theoretically we could allow users to open | |
2802 | * incomplete pools in RW mode, we'd need to add a lot | |
2803 | * of extra logic (e.g. adjust pool space to account | |
2804 | * for missing vdevs). | |
2805 | * This limitation also prevents users from accidentally | |
2806 | * opening the pool in RW mode during data recovery and | |
2807 | * damaging it further. | |
2808 | */ | |
2809 | spa_load_note(spa, "pools with missing top-level " | |
2810 | "vdevs can only be opened in read-only mode."); | |
2811 | error = SET_ERROR(ENXIO); | |
2812 | } else { | |
2813 | spa_load_note(spa, "current settings allow for maximum " | |
2814 | "%lld missing top-level vdevs at this stage.", | |
2815 | (u_longlong_t)spa->spa_missing_tvds_allowed); | |
2816 | } | |
2817 | } | |
4a0ee12a PZ |
2818 | if (error != 0) { |
2819 | spa_load_failed(spa, "unable to open vdev tree [error=%d]", | |
2820 | error); | |
2821 | } | |
6cb8e530 PZ |
2822 | if (spa->spa_missing_tvds != 0 || error != 0) |
2823 | vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); | |
9eb7b46e PZ |
2824 | |
2825 | return (error); | |
2826 | } | |
2827 | ||
6cb8e530 PZ |
2828 | /* |
2829 | * We need to validate the vdev labels against the configuration that | |
2830 | * we have in hand. This function is called twice: first with an untrusted | |
2831 | * config, then with a trusted config. The validation is more strict when the | |
2832 | * config is trusted. | |
2833 | */ | |
9eb7b46e | 2834 | static int |
6cb8e530 | 2835 | spa_ld_validate_vdevs(spa_t *spa) |
9eb7b46e PZ |
2836 | { |
2837 | int error = 0; | |
2838 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 2839 | |
6cb8e530 PZ |
2840 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
2841 | error = vdev_validate(rvd); | |
2842 | spa_config_exit(spa, SCL_ALL, FTAG); | |
428870ff | 2843 | |
6cb8e530 PZ |
2844 | if (error != 0) { |
2845 | spa_load_failed(spa, "vdev_validate failed [error=%d]", error); | |
2846 | return (error); | |
2847 | } | |
428870ff | 2848 | |
6cb8e530 PZ |
2849 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { |
2850 | spa_load_failed(spa, "cannot open vdev tree after invalidating " | |
2851 | "some vdevs"); | |
2852 | vdev_dbgmsg_print_tree(rvd, 2); | |
2853 | return (SET_ERROR(ENXIO)); | |
428870ff BB |
2854 | } |
2855 | ||
9eb7b46e PZ |
2856 | return (0); |
2857 | } | |
2858 | ||
d2734cce SD |
2859 | static void |
2860 | spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) | |
2861 | { | |
2862 | spa->spa_state = POOL_STATE_ACTIVE; | |
2863 | spa->spa_ubsync = spa->spa_uberblock; | |
2864 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
2865 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
2866 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
2867 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
2868 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
2869 | spa->spa_prev_software_version = ub->ub_software_version; | |
2870 | } | |
2871 | ||
9eb7b46e | 2872 | static int |
6cb8e530 | 2873 | spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) |
9eb7b46e PZ |
2874 | { |
2875 | vdev_t *rvd = spa->spa_root_vdev; | |
2876 | nvlist_t *label; | |
2877 | uberblock_t *ub = &spa->spa_uberblock; | |
9eb7b46e PZ |
2878 | boolean_t activity_check = B_FALSE; |
2879 | ||
d2734cce SD |
2880 | /* |
2881 | * If we are opening the checkpointed state of the pool by | |
2882 | * rewinding to it, at this point we will have written the | |
2883 | * checkpointed uberblock to the vdev labels, so searching | |
2884 | * the labels will find the right uberblock. However, if | |
2885 | * we are opening the checkpointed state read-only, we have | |
2886 | * not modified the labels. Therefore, we must ignore the | |
2887 | * labels and continue using the spa_uberblock that was set | |
2888 | * by spa_ld_checkpoint_rewind. | |
2889 | * | |
2890 | * Note that it would be fine to ignore the labels when | |
2891 | * rewinding (opening writeable) as well. However, if we | |
2892 | * crash just after writing the labels, we will end up | |
2893 | * searching the labels. Doing so in the common case means | |
2894 | * that this code path gets exercised normally, rather than | |
2895 | * just in the edge case. | |
2896 | */ | |
2897 | if (ub->ub_checkpoint_txg != 0 && | |
2898 | spa_importing_readonly_checkpoint(spa)) { | |
2899 | spa_ld_select_uberblock_done(spa, ub); | |
2900 | return (0); | |
2901 | } | |
2902 | ||
428870ff BB |
2903 | /* |
2904 | * Find the best uberblock. | |
2905 | */ | |
9ae529ec | 2906 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
2907 | |
2908 | /* | |
2909 | * If we weren't able to find a single valid uberblock, return failure. | |
2910 | */ | |
9ae529ec CS |
2911 | if (ub->ub_txg == 0) { |
2912 | nvlist_free(label); | |
4a0ee12a | 2913 | spa_load_failed(spa, "no valid uberblock found"); |
428870ff | 2914 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 2915 | } |
428870ff | 2916 | |
4a0ee12a PZ |
2917 | spa_load_note(spa, "using uberblock with txg=%llu", |
2918 | (u_longlong_t)ub->ub_txg); | |
2919 | ||
2920 | ||
379ca9cf OF |
2921 | /* |
2922 | * For pools which have the multihost property on determine if the | |
2923 | * pool is truly inactive and can be safely imported. Prevent | |
2924 | * hosts which don't have a hostid set from importing the pool. | |
2925 | */ | |
6cb8e530 PZ |
2926 | activity_check = spa_activity_check_required(spa, ub, label, |
2927 | spa->spa_config); | |
379ca9cf | 2928 | if (activity_check) { |
379ca9cf OF |
2929 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && |
2930 | spa_get_hostid() == 0) { | |
2931 | nvlist_free(label); | |
2932 | fnvlist_add_uint64(spa->spa_load_info, | |
2933 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
2934 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
2935 | } | |
2936 | ||
6cb8e530 | 2937 | int error = spa_activity_check(spa, ub, spa->spa_config); |
e889f0f5 OF |
2938 | if (error) { |
2939 | nvlist_free(label); | |
2940 | return (error); | |
2941 | } | |
2942 | ||
379ca9cf OF |
2943 | fnvlist_add_uint64(spa->spa_load_info, |
2944 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); | |
2945 | fnvlist_add_uint64(spa->spa_load_info, | |
2946 | ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); | |
2947 | } | |
2948 | ||
428870ff | 2949 | /* |
9ae529ec | 2950 | * If the pool has an unsupported version we can't open it. |
428870ff | 2951 | */ |
9ae529ec CS |
2952 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
2953 | nvlist_free(label); | |
4a0ee12a PZ |
2954 | spa_load_failed(spa, "version %llu is not supported", |
2955 | (u_longlong_t)ub->ub_version); | |
428870ff | 2956 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
2957 | } |
2958 | ||
2959 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
2960 | nvlist_t *features; | |
2961 | ||
2962 | /* | |
2963 | * If we weren't able to find what's necessary for reading the | |
2964 | * MOS in the label, return failure. | |
2965 | */ | |
4a0ee12a PZ |
2966 | if (label == NULL) { |
2967 | spa_load_failed(spa, "label config unavailable"); | |
2968 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
2969 | ENXIO)); | |
2970 | } | |
2971 | ||
2972 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, | |
2973 | &features) != 0) { | |
9ae529ec | 2974 | nvlist_free(label); |
4a0ee12a PZ |
2975 | spa_load_failed(spa, "invalid label: '%s' missing", |
2976 | ZPOOL_CONFIG_FEATURES_FOR_READ); | |
9ae529ec CS |
2977 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
2978 | ENXIO)); | |
2979 | } | |
2980 | ||
2981 | /* | |
2982 | * Update our in-core representation with the definitive values | |
2983 | * from the label. | |
2984 | */ | |
2985 | nvlist_free(spa->spa_label_features); | |
2986 | VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); | |
2987 | } | |
2988 | ||
2989 | nvlist_free(label); | |
2990 | ||
2991 | /* | |
2992 | * Look through entries in the label nvlist's features_for_read. If | |
2993 | * there is a feature listed there which we don't understand then we | |
2994 | * cannot open a pool. | |
2995 | */ | |
2996 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
2997 | nvlist_t *unsup_feat; | |
9ae529ec CS |
2998 | |
2999 | VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == | |
3000 | 0); | |
3001 | ||
1c27024e DB |
3002 | for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, |
3003 | NULL); nvp != NULL; | |
9ae529ec CS |
3004 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { |
3005 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
3006 | VERIFY(nvlist_add_string(unsup_feat, | |
3007 | nvpair_name(nvp), "") == 0); | |
3008 | } | |
3009 | } | |
3010 | ||
3011 | if (!nvlist_empty(unsup_feat)) { | |
3012 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
3013 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); | |
3014 | nvlist_free(unsup_feat); | |
4a0ee12a | 3015 | spa_load_failed(spa, "some features are unsupported"); |
9ae529ec CS |
3016 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3017 | ENOTSUP)); | |
3018 | } | |
3019 | ||
3020 | nvlist_free(unsup_feat); | |
3021 | } | |
428870ff | 3022 | |
428870ff BB |
3023 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { |
3024 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 | 3025 | spa_try_repair(spa, spa->spa_config); |
428870ff BB |
3026 | spa_config_exit(spa, SCL_ALL, FTAG); |
3027 | nvlist_free(spa->spa_config_splitting); | |
3028 | spa->spa_config_splitting = NULL; | |
3029 | } | |
3030 | ||
3031 | /* | |
3032 | * Initialize internal SPA structures. | |
3033 | */ | |
d2734cce | 3034 | spa_ld_select_uberblock_done(spa, ub); |
428870ff | 3035 | |
9eb7b46e PZ |
3036 | return (0); |
3037 | } | |
3038 | ||
3039 | static int | |
3040 | spa_ld_open_rootbp(spa_t *spa) | |
3041 | { | |
3042 | int error = 0; | |
3043 | vdev_t *rvd = spa->spa_root_vdev; | |
a1d477c2 | 3044 | |
9ae529ec | 3045 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
4a0ee12a PZ |
3046 | if (error != 0) { |
3047 | spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " | |
3048 | "[error=%d]", error); | |
428870ff | 3049 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3050 | } |
428870ff BB |
3051 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; |
3052 | ||
9eb7b46e PZ |
3053 | return (0); |
3054 | } | |
3055 | ||
3056 | static int | |
d2734cce | 3057 | spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, |
6cb8e530 | 3058 | boolean_t reloading) |
9eb7b46e | 3059 | { |
6cb8e530 PZ |
3060 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
3061 | nvlist_t *nv, *mos_config, *policy; | |
3062 | int error = 0, copy_error; | |
3063 | uint64_t healthy_tvds, healthy_tvds_mos; | |
3064 | uint64_t mos_config_txg; | |
9eb7b46e | 3065 | |
4a0ee12a PZ |
3066 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) |
3067 | != 0) | |
428870ff BB |
3068 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3069 | ||
a1d477c2 | 3070 | /* |
6cb8e530 PZ |
3071 | * If we're assembling a pool from a split, the config provided is |
3072 | * already trusted so there is nothing to do. | |
a1d477c2 | 3073 | */ |
6cb8e530 PZ |
3074 | if (type == SPA_IMPORT_ASSEMBLE) |
3075 | return (0); | |
3076 | ||
3077 | healthy_tvds = spa_healthy_core_tvds(spa); | |
a1d477c2 | 3078 | |
6cb8e530 PZ |
3079 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) |
3080 | != 0) { | |
3081 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
3082 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3083 | } | |
3084 | ||
3085 | /* | |
3086 | * If we are doing an open, pool owner wasn't verified yet, thus do | |
3087 | * the verification here. | |
3088 | */ | |
3089 | if (spa->spa_load_state == SPA_LOAD_OPEN) { | |
3090 | error = spa_verify_host(spa, mos_config); | |
3091 | if (error != 0) { | |
a1d477c2 | 3092 | nvlist_free(mos_config); |
6cb8e530 | 3093 | return (error); |
a1d477c2 | 3094 | } |
6cb8e530 PZ |
3095 | } |
3096 | ||
3097 | nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); | |
a1d477c2 | 3098 | |
6cb8e530 PZ |
3099 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3100 | ||
3101 | /* | |
3102 | * Build a new vdev tree from the trusted config | |
3103 | */ | |
3104 | VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); | |
3105 | ||
3106 | /* | |
3107 | * Vdev paths in the MOS may be obsolete. If the untrusted config was | |
3108 | * obtained by scanning /dev/dsk, then it will have the right vdev | |
3109 | * paths. We update the trusted MOS config with this information. | |
3110 | * We first try to copy the paths with vdev_copy_path_strict, which | |
3111 | * succeeds only when both configs have exactly the same vdev tree. | |
3112 | * If that fails, we fall back to a more flexible method that has a | |
3113 | * best effort policy. | |
3114 | */ | |
3115 | copy_error = vdev_copy_path_strict(rvd, mrvd); | |
3116 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3117 | spa_load_note(spa, "provided vdev tree:"); | |
3118 | vdev_dbgmsg_print_tree(rvd, 2); | |
3119 | spa_load_note(spa, "MOS vdev tree:"); | |
3120 | vdev_dbgmsg_print_tree(mrvd, 2); | |
3121 | } | |
3122 | if (copy_error != 0) { | |
3123 | spa_load_note(spa, "vdev_copy_path_strict failed, falling " | |
3124 | "back to vdev_copy_path_relaxed"); | |
3125 | vdev_copy_path_relaxed(rvd, mrvd); | |
3126 | } | |
3127 | ||
3128 | vdev_close(rvd); | |
3129 | vdev_free(rvd); | |
3130 | spa->spa_root_vdev = mrvd; | |
3131 | rvd = mrvd; | |
3132 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3133 | ||
3134 | /* | |
3135 | * We will use spa_config if we decide to reload the spa or if spa_load | |
3136 | * fails and we rewind. We must thus regenerate the config using the | |
8a393be3 PZ |
3137 | * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to |
3138 | * pass settings on how to load the pool and is not stored in the MOS. | |
3139 | * We copy it over to our new, trusted config. | |
6cb8e530 PZ |
3140 | */ |
3141 | mos_config_txg = fnvlist_lookup_uint64(mos_config, | |
3142 | ZPOOL_CONFIG_POOL_TXG); | |
3143 | nvlist_free(mos_config); | |
3144 | mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); | |
8a393be3 | 3145 | if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, |
6cb8e530 | 3146 | &policy) == 0) |
8a393be3 | 3147 | fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); |
6cb8e530 PZ |
3148 | spa_config_set(spa, mos_config); |
3149 | spa->spa_config_source = SPA_CONFIG_SRC_MOS; | |
3150 | ||
3151 | /* | |
3152 | * Now that we got the config from the MOS, we should be more strict | |
3153 | * in checking blkptrs and can make assumptions about the consistency | |
3154 | * of the vdev tree. spa_trust_config must be set to true before opening | |
3155 | * vdevs in order for them to be writeable. | |
3156 | */ | |
3157 | spa->spa_trust_config = B_TRUE; | |
3158 | ||
3159 | /* | |
3160 | * Open and validate the new vdev tree | |
3161 | */ | |
3162 | error = spa_ld_open_vdevs(spa); | |
3163 | if (error != 0) | |
3164 | return (error); | |
3165 | ||
3166 | error = spa_ld_validate_vdevs(spa); | |
3167 | if (error != 0) | |
3168 | return (error); | |
3169 | ||
3170 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3171 | spa_load_note(spa, "final vdev tree:"); | |
3172 | vdev_dbgmsg_print_tree(rvd, 2); | |
3173 | } | |
3174 | ||
3175 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && | |
3176 | !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { | |
a1d477c2 | 3177 | /* |
6cb8e530 PZ |
3178 | * Sanity check to make sure that we are indeed loading the |
3179 | * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds | |
3180 | * in the config provided and they happened to be the only ones | |
3181 | * to have the latest uberblock, we could involuntarily perform | |
3182 | * an extreme rewind. | |
a1d477c2 | 3183 | */ |
6cb8e530 PZ |
3184 | healthy_tvds_mos = spa_healthy_core_tvds(spa); |
3185 | if (healthy_tvds_mos - healthy_tvds >= | |
3186 | SPA_SYNC_MIN_VDEVS) { | |
3187 | spa_load_note(spa, "config provided misses too many " | |
3188 | "top-level vdevs compared to MOS (%lld vs %lld). ", | |
3189 | (u_longlong_t)healthy_tvds, | |
3190 | (u_longlong_t)healthy_tvds_mos); | |
3191 | spa_load_note(spa, "vdev tree:"); | |
3192 | vdev_dbgmsg_print_tree(rvd, 2); | |
3193 | if (reloading) { | |
3194 | spa_load_failed(spa, "config was already " | |
3195 | "provided from MOS. Aborting."); | |
3196 | return (spa_vdev_err(rvd, | |
3197 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3198 | } | |
3199 | spa_load_note(spa, "spa must be reloaded using MOS " | |
3200 | "config"); | |
3201 | return (SET_ERROR(EAGAIN)); | |
4a0ee12a | 3202 | } |
a1d477c2 MA |
3203 | } |
3204 | ||
6cb8e530 PZ |
3205 | error = spa_check_for_missing_logs(spa); |
3206 | if (error != 0) | |
3207 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
3208 | ||
3209 | if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { | |
3210 | spa_load_failed(spa, "uberblock guid sum doesn't match MOS " | |
3211 | "guid sum (%llu != %llu)", | |
3212 | (u_longlong_t)spa->spa_uberblock.ub_guid_sum, | |
3213 | (u_longlong_t)rvd->vdev_guid_sum); | |
3214 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
3215 | ENXIO)); | |
3216 | } | |
3217 | ||
9eb7b46e PZ |
3218 | return (0); |
3219 | } | |
3220 | ||
3221 | static int | |
3222 | spa_ld_open_indirect_vdev_metadata(spa_t *spa) | |
3223 | { | |
3224 | int error = 0; | |
3225 | vdev_t *rvd = spa->spa_root_vdev; | |
3226 | ||
a1d477c2 MA |
3227 | /* |
3228 | * Everything that we read before spa_remove_init() must be stored | |
3229 | * on concreted vdevs. Therefore we do this as early as possible. | |
3230 | */ | |
4a0ee12a PZ |
3231 | error = spa_remove_init(spa); |
3232 | if (error != 0) { | |
3233 | spa_load_failed(spa, "spa_remove_init failed [error=%d]", | |
3234 | error); | |
a1d477c2 | 3235 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3236 | } |
a1d477c2 | 3237 | |
9eb7b46e PZ |
3238 | /* |
3239 | * Retrieve information needed to condense indirect vdev mappings. | |
3240 | */ | |
3241 | error = spa_condense_init(spa); | |
3242 | if (error != 0) { | |
4a0ee12a PZ |
3243 | spa_load_failed(spa, "spa_condense_init failed [error=%d]", |
3244 | error); | |
9eb7b46e PZ |
3245 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3246 | } | |
3247 | ||
3248 | return (0); | |
3249 | } | |
3250 | ||
3251 | static int | |
4a0ee12a | 3252 | spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) |
9eb7b46e PZ |
3253 | { |
3254 | int error = 0; | |
3255 | vdev_t *rvd = spa->spa_root_vdev; | |
3256 | ||
9ae529ec CS |
3257 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
3258 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 3259 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
3260 | |
3261 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
4a0ee12a | 3262 | &spa->spa_feat_for_read_obj, B_TRUE) != 0) { |
9ae529ec CS |
3263 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3264 | } | |
3265 | ||
3266 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
4a0ee12a | 3267 | &spa->spa_feat_for_write_obj, B_TRUE) != 0) { |
9ae529ec CS |
3268 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3269 | } | |
3270 | ||
3271 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
4a0ee12a | 3272 | &spa->spa_feat_desc_obj, B_TRUE) != 0) { |
9ae529ec CS |
3273 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3274 | } | |
3275 | ||
b9b24bb4 CS |
3276 | enabled_feat = fnvlist_alloc(); |
3277 | unsup_feat = fnvlist_alloc(); | |
9ae529ec | 3278 | |
fa86b5db | 3279 | if (!spa_features_check(spa, B_FALSE, |
b9b24bb4 | 3280 | unsup_feat, enabled_feat)) |
9ae529ec CS |
3281 | missing_feat_read = B_TRUE; |
3282 | ||
4a0ee12a PZ |
3283 | if (spa_writeable(spa) || |
3284 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) { | |
fa86b5db | 3285 | if (!spa_features_check(spa, B_TRUE, |
b9b24bb4 | 3286 | unsup_feat, enabled_feat)) { |
9eb7b46e | 3287 | *missing_feat_writep = B_TRUE; |
b9b24bb4 | 3288 | } |
9ae529ec CS |
3289 | } |
3290 | ||
b9b24bb4 CS |
3291 | fnvlist_add_nvlist(spa->spa_load_info, |
3292 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
3293 | ||
9ae529ec | 3294 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
3295 | fnvlist_add_nvlist(spa->spa_load_info, |
3296 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
3297 | } |
3298 | ||
b9b24bb4 CS |
3299 | fnvlist_free(enabled_feat); |
3300 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
3301 | |
3302 | if (!missing_feat_read) { | |
3303 | fnvlist_add_boolean(spa->spa_load_info, | |
3304 | ZPOOL_CONFIG_CAN_RDONLY); | |
3305 | } | |
3306 | ||
3307 | /* | |
3308 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
3309 | * twofold: to determine whether the pool is available for | |
3310 | * import in read-write mode and (if it is not) whether the | |
3311 | * pool is available for import in read-only mode. If the pool | |
3312 | * is available for import in read-write mode, it is displayed | |
3313 | * as available in userland; if it is not available for import | |
3314 | * in read-only mode, it is displayed as unavailable in | |
3315 | * userland. If the pool is available for import in read-only | |
3316 | * mode but not read-write mode, it is displayed as unavailable | |
3317 | * in userland with a special note that the pool is actually | |
3318 | * available for open in read-only mode. | |
3319 | * | |
3320 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
3321 | * missing a feature for write, we must first determine whether | |
3322 | * the pool can be opened read-only before returning to | |
3323 | * userland in order to know whether to display the | |
3324 | * abovementioned note. | |
3325 | */ | |
9eb7b46e | 3326 | if (missing_feat_read || (*missing_feat_writep && |
9ae529ec | 3327 | spa_writeable(spa))) { |
4a0ee12a | 3328 | spa_load_failed(spa, "pool uses unsupported features"); |
9ae529ec CS |
3329 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3330 | ENOTSUP)); | |
3331 | } | |
b0bc7a84 MG |
3332 | |
3333 | /* | |
3334 | * Load refcounts for ZFS features from disk into an in-memory | |
3335 | * cache during SPA initialization. | |
3336 | */ | |
1c27024e | 3337 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { |
b0bc7a84 MG |
3338 | uint64_t refcount; |
3339 | ||
3340 | error = feature_get_refcount_from_disk(spa, | |
3341 | &spa_feature_table[i], &refcount); | |
3342 | if (error == 0) { | |
3343 | spa->spa_feat_refcount_cache[i] = refcount; | |
3344 | } else if (error == ENOTSUP) { | |
3345 | spa->spa_feat_refcount_cache[i] = | |
3346 | SPA_FEATURE_DISABLED; | |
3347 | } else { | |
4a0ee12a PZ |
3348 | spa_load_failed(spa, "error getting refcount " |
3349 | "for feature %s [error=%d]", | |
3350 | spa_feature_table[i].fi_guid, error); | |
b0bc7a84 MG |
3351 | return (spa_vdev_err(rvd, |
3352 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3353 | } | |
3354 | } | |
3355 | } | |
3356 | ||
3357 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
3358 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
4a0ee12a | 3359 | &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) |
b0bc7a84 | 3360 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
9ae529ec CS |
3361 | } |
3362 | ||
9eb7b46e PZ |
3363 | return (0); |
3364 | } | |
3365 | ||
3366 | static int | |
3367 | spa_ld_load_special_directories(spa_t *spa) | |
3368 | { | |
3369 | int error = 0; | |
3370 | vdev_t *rvd = spa->spa_root_vdev; | |
3371 | ||
9ae529ec CS |
3372 | spa->spa_is_initializing = B_TRUE; |
3373 | error = dsl_pool_open(spa->spa_dsl_pool); | |
3374 | spa->spa_is_initializing = B_FALSE; | |
4a0ee12a PZ |
3375 | if (error != 0) { |
3376 | spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); | |
9ae529ec | 3377 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3378 | } |
9ae529ec | 3379 | |
9eb7b46e PZ |
3380 | return (0); |
3381 | } | |
428870ff | 3382 | |
9eb7b46e PZ |
3383 | static int |
3384 | spa_ld_get_props(spa_t *spa) | |
3385 | { | |
3386 | int error = 0; | |
3387 | uint64_t obj; | |
3388 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 3389 | |
3c67d83a TH |
3390 | /* Grab the checksum salt from the MOS. */ |
3391 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3392 | DMU_POOL_CHECKSUM_SALT, 1, | |
3393 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
3394 | spa->spa_cksum_salt.zcs_bytes); | |
3395 | if (error == ENOENT) { | |
3396 | /* Generate a new salt for subsequent use */ | |
3397 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
3398 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
3399 | } else if (error != 0) { | |
4a0ee12a PZ |
3400 | spa_load_failed(spa, "unable to retrieve checksum salt from " |
3401 | "MOS [error=%d]", error); | |
3c67d83a TH |
3402 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3403 | } | |
3404 | ||
4a0ee12a | 3405 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) |
428870ff BB |
3406 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3407 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
4a0ee12a PZ |
3408 | if (error != 0) { |
3409 | spa_load_failed(spa, "error opening deferred-frees bpobj " | |
3410 | "[error=%d]", error); | |
428870ff | 3411 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3412 | } |
34dc7c2f BB |
3413 | |
3414 | /* | |
3415 | * Load the bit that tells us to use the new accounting function | |
3416 | * (raid-z deflation). If we have an older pool, this will not | |
3417 | * be present. | |
3418 | */ | |
4a0ee12a | 3419 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); |
428870ff BB |
3420 | if (error != 0 && error != ENOENT) |
3421 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3422 | ||
3423 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
4a0ee12a | 3424 | &spa->spa_creation_version, B_FALSE); |
428870ff BB |
3425 | if (error != 0 && error != ENOENT) |
3426 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
3427 | |
3428 | /* | |
3429 | * Load the persistent error log. If we have an older pool, this will | |
3430 | * not be present. | |
3431 | */ | |
4a0ee12a PZ |
3432 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, |
3433 | B_FALSE); | |
428870ff BB |
3434 | if (error != 0 && error != ENOENT) |
3435 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 3436 | |
428870ff | 3437 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
4a0ee12a | 3438 | &spa->spa_errlog_scrub, B_FALSE); |
428870ff BB |
3439 | if (error != 0 && error != ENOENT) |
3440 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
3441 | |
3442 | /* | |
3443 | * Load the history object. If we have an older pool, this | |
3444 | * will not be present. | |
3445 | */ | |
4a0ee12a | 3446 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); |
428870ff BB |
3447 | if (error != 0 && error != ENOENT) |
3448 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3449 | ||
e0ab3ab5 JS |
3450 | /* |
3451 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
3452 | * be present; in this case, defer its creation to a later time to | |
3453 | * avoid dirtying the MOS this early / out of sync context. See | |
3454 | * spa_sync_config_object. | |
3455 | */ | |
3456 | ||
3457 | /* The sentinel is only available in the MOS config. */ | |
1c27024e | 3458 | nvlist_t *mos_config; |
4a0ee12a PZ |
3459 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { |
3460 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
e0ab3ab5 | 3461 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3462 | } |
e0ab3ab5 JS |
3463 | |
3464 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
4a0ee12a | 3465 | &spa->spa_all_vdev_zaps, B_FALSE); |
e0ab3ab5 | 3466 | |
38640550 DB |
3467 | if (error == ENOENT) { |
3468 | VERIFY(!nvlist_exists(mos_config, | |
3469 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
3470 | spa->spa_avz_action = AVZ_ACTION_INITIALIZE; | |
3471 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
3472 | } else if (error != 0) { | |
e0ab3ab5 | 3473 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
38640550 | 3474 | } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { |
e0ab3ab5 JS |
3475 | /* |
3476 | * An older version of ZFS overwrote the sentinel value, so | |
3477 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
3478 | * destruction to later; see spa_sync_config_object. | |
3479 | */ | |
3480 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
3481 | /* | |
3482 | * We're assuming that no vdevs have had their ZAPs created | |
3483 | * before this. Better be sure of it. | |
3484 | */ | |
3485 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
3486 | } | |
3487 | nvlist_free(mos_config); | |
3488 | ||
9eb7b46e PZ |
3489 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
3490 | ||
4a0ee12a PZ |
3491 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, |
3492 | B_FALSE); | |
9eb7b46e PZ |
3493 | if (error && error != ENOENT) |
3494 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3495 | ||
3496 | if (error == 0) { | |
3497 | uint64_t autoreplace; | |
3498 | ||
3499 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
3500 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
3501 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
3502 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
3503 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
c02c1bec | 3504 | spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); |
9eb7b46e PZ |
3505 | spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, |
3506 | &spa->spa_dedup_ditto); | |
3507 | ||
3508 | spa->spa_autoreplace = (autoreplace != 0); | |
3509 | } | |
3510 | ||
6cb8e530 PZ |
3511 | /* |
3512 | * If we are importing a pool with missing top-level vdevs, | |
3513 | * we enforce that the pool doesn't panic or get suspended on | |
3514 | * error since the likelihood of missing data is extremely high. | |
3515 | */ | |
3516 | if (spa->spa_missing_tvds > 0 && | |
3517 | spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && | |
3518 | spa->spa_load_state != SPA_LOAD_TRYIMPORT) { | |
3519 | spa_load_note(spa, "forcing failmode to 'continue' " | |
3520 | "as some top level vdevs are missing"); | |
3521 | spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; | |
3522 | } | |
3523 | ||
9eb7b46e PZ |
3524 | return (0); |
3525 | } | |
3526 | ||
3527 | static int | |
3528 | spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) | |
3529 | { | |
3530 | int error = 0; | |
3531 | vdev_t *rvd = spa->spa_root_vdev; | |
3532 | ||
428870ff BB |
3533 | /* |
3534 | * If we're assembling the pool from the split-off vdevs of | |
3535 | * an existing pool, we don't want to attach the spares & cache | |
3536 | * devices. | |
3537 | */ | |
34dc7c2f BB |
3538 | |
3539 | /* | |
3540 | * Load any hot spares for this pool. | |
3541 | */ | |
4a0ee12a PZ |
3542 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, |
3543 | B_FALSE); | |
428870ff BB |
3544 | if (error != 0 && error != ENOENT) |
3545 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3546 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
3547 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
3548 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
4a0ee12a PZ |
3549 | &spa->spa_spares.sav_config) != 0) { |
3550 | spa_load_failed(spa, "error loading spares nvlist"); | |
428870ff | 3551 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3552 | } |
34dc7c2f | 3553 | |
b128c09f | 3554 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3555 | spa_load_spares(spa); |
b128c09f | 3556 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
3557 | } else if (error == 0) { |
3558 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
3559 | } |
3560 | ||
3561 | /* | |
3562 | * Load any level 2 ARC devices for this pool. | |
3563 | */ | |
428870ff | 3564 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
4a0ee12a | 3565 | &spa->spa_l2cache.sav_object, B_FALSE); |
428870ff BB |
3566 | if (error != 0 && error != ENOENT) |
3567 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3568 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
3569 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
3570 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
4a0ee12a PZ |
3571 | &spa->spa_l2cache.sav_config) != 0) { |
3572 | spa_load_failed(spa, "error loading l2cache nvlist"); | |
428870ff | 3573 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3574 | } |
34dc7c2f | 3575 | |
b128c09f | 3576 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3577 | spa_load_l2cache(spa); |
b128c09f | 3578 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
3579 | } else if (error == 0) { |
3580 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
3581 | } |
3582 | ||
9eb7b46e PZ |
3583 | return (0); |
3584 | } | |
428870ff | 3585 | |
9eb7b46e | 3586 | static int |
4a0ee12a | 3587 | spa_ld_load_vdev_metadata(spa_t *spa) |
9eb7b46e PZ |
3588 | { |
3589 | int error = 0; | |
3590 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 3591 | |
379ca9cf OF |
3592 | /* |
3593 | * If the 'multihost' property is set, then never allow a pool to | |
3594 | * be imported when the system hostid is zero. The exception to | |
3595 | * this rule is zdb which is always allowed to access pools. | |
3596 | */ | |
3597 | if (spa_multihost(spa) && spa_get_hostid() == 0 && | |
3598 | (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { | |
3599 | fnvlist_add_uint64(spa->spa_load_info, | |
3600 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3601 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3602 | } | |
3603 | ||
34dc7c2f BB |
3604 | /* |
3605 | * If the 'autoreplace' property is set, then post a resource notifying | |
3606 | * the ZFS DE that it should not issue any faults for unopenable | |
3607 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
3608 | * unopenable vdevs so that the normal autoreplace handler can take | |
3609 | * over. | |
3610 | */ | |
4a0ee12a | 3611 | if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 3612 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
3613 | /* |
3614 | * For the import case, this is done in spa_import(), because | |
3615 | * at this point we're using the spare definitions from | |
3616 | * the MOS config, not necessarily from the userland config. | |
3617 | */ | |
4a0ee12a | 3618 | if (spa->spa_load_state != SPA_LOAD_IMPORT) { |
428870ff BB |
3619 | spa_aux_check_removed(&spa->spa_spares); |
3620 | spa_aux_check_removed(&spa->spa_l2cache); | |
3621 | } | |
3622 | } | |
34dc7c2f BB |
3623 | |
3624 | /* | |
9eb7b46e | 3625 | * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. |
34dc7c2f | 3626 | */ |
a1d477c2 MA |
3627 | error = vdev_load(rvd); |
3628 | if (error != 0) { | |
4a0ee12a | 3629 | spa_load_failed(spa, "vdev_load failed [error=%d]", error); |
a1d477c2 MA |
3630 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3631 | } | |
3632 | ||
34dc7c2f | 3633 | /* |
9eb7b46e | 3634 | * Propagate the leaf DTLs we just loaded all the way up the vdev tree. |
34dc7c2f | 3635 | */ |
b128c09f | 3636 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3637 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE); |
b128c09f | 3638 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 3639 | |
9eb7b46e PZ |
3640 | return (0); |
3641 | } | |
3642 | ||
3643 | static int | |
3644 | spa_ld_load_dedup_tables(spa_t *spa) | |
3645 | { | |
3646 | int error = 0; | |
3647 | vdev_t *rvd = spa->spa_root_vdev; | |
3648 | ||
428870ff | 3649 | error = ddt_load(spa); |
4a0ee12a PZ |
3650 | if (error != 0) { |
3651 | spa_load_failed(spa, "ddt_load failed [error=%d]", error); | |
428870ff | 3652 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3653 | } |
428870ff | 3654 | |
9eb7b46e PZ |
3655 | return (0); |
3656 | } | |
3657 | ||
3658 | static int | |
3659 | spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) | |
3660 | { | |
3661 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 3662 | |
4a0ee12a PZ |
3663 | if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { |
3664 | boolean_t missing = spa_check_logs(spa); | |
3665 | if (missing) { | |
6cb8e530 PZ |
3666 | if (spa->spa_missing_tvds != 0) { |
3667 | spa_load_note(spa, "spa_check_logs failed " | |
3668 | "so dropping the logs"); | |
3669 | } else { | |
3670 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
3671 | spa_load_failed(spa, "spa_check_logs failed"); | |
3672 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, | |
3673 | ENXIO)); | |
3674 | } | |
4a0ee12a | 3675 | } |
428870ff BB |
3676 | } |
3677 | ||
9eb7b46e PZ |
3678 | return (0); |
3679 | } | |
3680 | ||
3681 | static int | |
4a0ee12a | 3682 | spa_ld_verify_pool_data(spa_t *spa) |
9eb7b46e PZ |
3683 | { |
3684 | int error = 0; | |
3685 | vdev_t *rvd = spa->spa_root_vdev; | |
3686 | ||
3687 | /* | |
3688 | * We've successfully opened the pool, verify that we're ready | |
3689 | * to start pushing transactions. | |
3690 | */ | |
4a0ee12a | 3691 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
9eb7b46e PZ |
3692 | error = spa_load_verify(spa); |
3693 | if (error != 0) { | |
4a0ee12a PZ |
3694 | spa_load_failed(spa, "spa_load_verify failed " |
3695 | "[error=%d]", error); | |
9eb7b46e PZ |
3696 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3697 | error)); | |
3698 | } | |
3699 | } | |
3700 | ||
3701 | return (0); | |
3702 | } | |
3703 | ||
3704 | static void | |
3705 | spa_ld_claim_log_blocks(spa_t *spa) | |
3706 | { | |
3707 | dmu_tx_t *tx; | |
3708 | dsl_pool_t *dp = spa_get_dsl(spa); | |
3709 | ||
3710 | /* | |
3711 | * Claim log blocks that haven't been committed yet. | |
3712 | * This must all happen in a single txg. | |
3713 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
3714 | * invoked from zil_claim_log_block()'s i/o done callback. | |
3715 | * Price of rollback is that we abandon the log. | |
3716 | */ | |
3717 | spa->spa_claiming = B_TRUE; | |
3718 | ||
3719 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
3720 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
3721 | zil_claim, tx, DS_FIND_CHILDREN); | |
3722 | dmu_tx_commit(tx); | |
3723 | ||
3724 | spa->spa_claiming = B_FALSE; | |
3725 | ||
3726 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
3727 | } | |
3728 | ||
3729 | static void | |
6cb8e530 | 3730 | spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, |
d2734cce | 3731 | boolean_t update_config_cache) |
9eb7b46e PZ |
3732 | { |
3733 | vdev_t *rvd = spa->spa_root_vdev; | |
3734 | int need_update = B_FALSE; | |
3735 | ||
3736 | /* | |
3737 | * If the config cache is stale, or we have uninitialized | |
3738 | * metaslabs (see spa_vdev_add()), then update the config. | |
3739 | * | |
3740 | * If this is a verbatim import, trust the current | |
3741 | * in-core spa_config and update the disk labels. | |
3742 | */ | |
d2734cce | 3743 | if (update_config_cache || config_cache_txg != spa->spa_config_txg || |
4a0ee12a PZ |
3744 | spa->spa_load_state == SPA_LOAD_IMPORT || |
3745 | spa->spa_load_state == SPA_LOAD_RECOVER || | |
9eb7b46e PZ |
3746 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) |
3747 | need_update = B_TRUE; | |
3748 | ||
3749 | for (int c = 0; c < rvd->vdev_children; c++) | |
3750 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
3751 | need_update = B_TRUE; | |
3752 | ||
3753 | /* | |
3754 | * Update the config cache asychronously in case we're the | |
3755 | * root pool, in which case the config cache isn't writable yet. | |
3756 | */ | |
3757 | if (need_update) | |
3758 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
3759 | } | |
3760 | ||
6cb8e530 PZ |
3761 | static void |
3762 | spa_ld_prepare_for_reload(spa_t *spa) | |
3763 | { | |
3764 | int mode = spa->spa_mode; | |
3765 | int async_suspended = spa->spa_async_suspended; | |
3766 | ||
3767 | spa_unload(spa); | |
3768 | spa_deactivate(spa); | |
3769 | spa_activate(spa, mode); | |
3770 | ||
3771 | /* | |
3772 | * We save the value of spa_async_suspended as it gets reset to 0 by | |
3773 | * spa_unload(). We want to restore it back to the original value before | |
3774 | * returning as we might be calling spa_async_resume() later. | |
3775 | */ | |
3776 | spa->spa_async_suspended = async_suspended; | |
3777 | } | |
3778 | ||
9eb7b46e | 3779 | static int |
d2734cce SD |
3780 | spa_ld_read_checkpoint_txg(spa_t *spa) |
3781 | { | |
3782 | uberblock_t checkpoint; | |
3783 | int error = 0; | |
3784 | ||
3785 | ASSERT0(spa->spa_checkpoint_txg); | |
3786 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
3787 | ||
3788 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3789 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
3790 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
3791 | ||
3792 | if (error == ENOENT) | |
3793 | return (0); | |
3794 | ||
3795 | if (error != 0) | |
3796 | return (error); | |
3797 | ||
3798 | ASSERT3U(checkpoint.ub_txg, !=, 0); | |
3799 | ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); | |
3800 | ASSERT3U(checkpoint.ub_timestamp, !=, 0); | |
3801 | spa->spa_checkpoint_txg = checkpoint.ub_txg; | |
3802 | spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; | |
3803 | ||
3804 | return (0); | |
3805 | } | |
3806 | ||
3807 | static int | |
3808 | spa_ld_mos_init(spa_t *spa, spa_import_type_t type) | |
9eb7b46e PZ |
3809 | { |
3810 | int error = 0; | |
9eb7b46e | 3811 | |
4a0ee12a | 3812 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6cb8e530 | 3813 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); |
4a0ee12a | 3814 | |
9eb7b46e | 3815 | /* |
6cb8e530 PZ |
3816 | * Never trust the config that is provided unless we are assembling |
3817 | * a pool following a split. | |
3818 | * This means don't trust blkptrs and the vdev tree in general. This | |
3819 | * also effectively puts the spa in read-only mode since | |
3820 | * spa_writeable() checks for spa_trust_config to be true. | |
3821 | * We will later load a trusted config from the MOS. | |
9eb7b46e | 3822 | */ |
6cb8e530 PZ |
3823 | if (type != SPA_IMPORT_ASSEMBLE) |
3824 | spa->spa_trust_config = B_FALSE; | |
3825 | ||
9eb7b46e PZ |
3826 | /* |
3827 | * Parse the config provided to create a vdev tree. | |
3828 | */ | |
6cb8e530 | 3829 | error = spa_ld_parse_config(spa, type); |
9eb7b46e PZ |
3830 | if (error != 0) |
3831 | return (error); | |
3832 | ||
3833 | /* | |
3834 | * Now that we have the vdev tree, try to open each vdev. This involves | |
3835 | * opening the underlying physical device, retrieving its geometry and | |
3836 | * probing the vdev with a dummy I/O. The state of each vdev will be set | |
3837 | * based on the success of those operations. After this we'll be ready | |
3838 | * to read from the vdevs. | |
3839 | */ | |
3840 | error = spa_ld_open_vdevs(spa); | |
3841 | if (error != 0) | |
3842 | return (error); | |
3843 | ||
3844 | /* | |
3845 | * Read the label of each vdev and make sure that the GUIDs stored | |
3846 | * there match the GUIDs in the config provided. | |
6cb8e530 PZ |
3847 | * If we're assembling a new pool that's been split off from an |
3848 | * existing pool, the labels haven't yet been updated so we skip | |
3849 | * validation for now. | |
9eb7b46e | 3850 | */ |
6cb8e530 PZ |
3851 | if (type != SPA_IMPORT_ASSEMBLE) { |
3852 | error = spa_ld_validate_vdevs(spa); | |
3853 | if (error != 0) | |
3854 | return (error); | |
3855 | } | |
9eb7b46e PZ |
3856 | |
3857 | /* | |
d2734cce SD |
3858 | * Read all vdev labels to find the best uberblock (i.e. latest, |
3859 | * unless spa_load_max_txg is set) and store it in spa_uberblock. We | |
3860 | * get the list of features required to read blkptrs in the MOS from | |
3861 | * the vdev label with the best uberblock and verify that our version | |
3862 | * of zfs supports them all. | |
9eb7b46e | 3863 | */ |
6cb8e530 | 3864 | error = spa_ld_select_uberblock(spa, type); |
9eb7b46e PZ |
3865 | if (error != 0) |
3866 | return (error); | |
3867 | ||
3868 | /* | |
3869 | * Pass that uberblock to the dsl_pool layer which will open the root | |
3870 | * blkptr. This blkptr points to the latest version of the MOS and will | |
3871 | * allow us to read its contents. | |
3872 | */ | |
3873 | error = spa_ld_open_rootbp(spa); | |
3874 | if (error != 0) | |
3875 | return (error); | |
3876 | ||
d2734cce SD |
3877 | return (0); |
3878 | } | |
3879 | ||
3880 | static int | |
3881 | spa_ld_checkpoint_rewind(spa_t *spa) | |
3882 | { | |
3883 | uberblock_t checkpoint; | |
3884 | int error = 0; | |
3885 | ||
3886 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
3887 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
3888 | ||
3889 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3890 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
3891 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
3892 | ||
3893 | if (error != 0) { | |
3894 | spa_load_failed(spa, "unable to retrieve checkpointed " | |
3895 | "uberblock from the MOS config [error=%d]", error); | |
3896 | ||
3897 | if (error == ENOENT) | |
3898 | error = ZFS_ERR_NO_CHECKPOINT; | |
3899 | ||
3900 | return (error); | |
3901 | } | |
3902 | ||
3903 | ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); | |
3904 | ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); | |
3905 | ||
3906 | /* | |
3907 | * We need to update the txg and timestamp of the checkpointed | |
3908 | * uberblock to be higher than the latest one. This ensures that | |
3909 | * the checkpointed uberblock is selected if we were to close and | |
3910 | * reopen the pool right after we've written it in the vdev labels. | |
3911 | * (also see block comment in vdev_uberblock_compare) | |
3912 | */ | |
3913 | checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; | |
3914 | checkpoint.ub_timestamp = gethrestime_sec(); | |
3915 | ||
3916 | /* | |
3917 | * Set current uberblock to be the checkpointed uberblock. | |
3918 | */ | |
3919 | spa->spa_uberblock = checkpoint; | |
3920 | ||
3921 | /* | |
3922 | * If we are doing a normal rewind, then the pool is open for | |
3923 | * writing and we sync the "updated" checkpointed uberblock to | |
3924 | * disk. Once this is done, we've basically rewound the whole | |
3925 | * pool and there is no way back. | |
3926 | * | |
3927 | * There are cases when we don't want to attempt and sync the | |
3928 | * checkpointed uberblock to disk because we are opening a | |
3929 | * pool as read-only. Specifically, verifying the checkpointed | |
3930 | * state with zdb, and importing the checkpointed state to get | |
3931 | * a "preview" of its content. | |
3932 | */ | |
3933 | if (spa_writeable(spa)) { | |
3934 | vdev_t *rvd = spa->spa_root_vdev; | |
3935 | ||
3936 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
3937 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; | |
3938 | int svdcount = 0; | |
3939 | int children = rvd->vdev_children; | |
3940 | int c0 = spa_get_random(children); | |
3941 | ||
3942 | for (int c = 0; c < children; c++) { | |
3943 | vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; | |
3944 | ||
3945 | /* Stop when revisiting the first vdev */ | |
3946 | if (c > 0 && svd[0] == vd) | |
3947 | break; | |
3948 | ||
3949 | if (vd->vdev_ms_array == 0 || vd->vdev_islog || | |
3950 | !vdev_is_concrete(vd)) | |
3951 | continue; | |
3952 | ||
3953 | svd[svdcount++] = vd; | |
3954 | if (svdcount == SPA_SYNC_MIN_VDEVS) | |
3955 | break; | |
3956 | } | |
3957 | error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); | |
3958 | if (error == 0) | |
3959 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
3960 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3961 | ||
3962 | if (error != 0) { | |
3963 | spa_load_failed(spa, "failed to write checkpointed " | |
3964 | "uberblock to the vdev labels [error=%d]", error); | |
3965 | return (error); | |
3966 | } | |
3967 | } | |
3968 | ||
3969 | return (0); | |
3970 | } | |
3971 | ||
3972 | static int | |
3973 | spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, | |
3974 | boolean_t *update_config_cache) | |
3975 | { | |
3976 | int error; | |
3977 | ||
3978 | /* | |
3979 | * Parse the config for pool, open and validate vdevs, | |
3980 | * select an uberblock, and use that uberblock to open | |
3981 | * the MOS. | |
3982 | */ | |
3983 | error = spa_ld_mos_init(spa, type); | |
3984 | if (error != 0) | |
3985 | return (error); | |
3986 | ||
9eb7b46e | 3987 | /* |
6cb8e530 PZ |
3988 | * Retrieve the trusted config stored in the MOS and use it to create |
3989 | * a new, exact version of the vdev tree, then reopen all vdevs. | |
9eb7b46e | 3990 | */ |
d2734cce | 3991 | error = spa_ld_trusted_config(spa, type, B_FALSE); |
6cb8e530 | 3992 | if (error == EAGAIN) { |
d2734cce SD |
3993 | if (update_config_cache != NULL) |
3994 | *update_config_cache = B_TRUE; | |
3995 | ||
6cb8e530 PZ |
3996 | /* |
3997 | * Redo the loading process with the trusted config if it is | |
3998 | * too different from the untrusted config. | |
3999 | */ | |
4000 | spa_ld_prepare_for_reload(spa); | |
d2734cce SD |
4001 | spa_load_note(spa, "RELOADING"); |
4002 | error = spa_ld_mos_init(spa, type); | |
4003 | if (error != 0) | |
4004 | return (error); | |
4005 | ||
4006 | error = spa_ld_trusted_config(spa, type, B_TRUE); | |
4007 | if (error != 0) | |
4008 | return (error); | |
4009 | ||
6cb8e530 | 4010 | } else if (error != 0) { |
9eb7b46e | 4011 | return (error); |
6cb8e530 | 4012 | } |
9eb7b46e | 4013 | |
d2734cce SD |
4014 | return (0); |
4015 | } | |
4016 | ||
4017 | /* | |
4018 | * Load an existing storage pool, using the config provided. This config | |
4019 | * describes which vdevs are part of the pool and is later validated against | |
4020 | * partial configs present in each vdev's label and an entire copy of the | |
4021 | * config stored in the MOS. | |
4022 | */ | |
4023 | static int | |
4024 | spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport) | |
4025 | { | |
4026 | int error = 0; | |
4027 | boolean_t missing_feat_write = B_FALSE; | |
4028 | boolean_t checkpoint_rewind = | |
4029 | (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4030 | boolean_t update_config_cache = B_FALSE; | |
4031 | ||
4032 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4033 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); | |
4034 | ||
4035 | spa_load_note(spa, "LOADING"); | |
4036 | ||
4037 | error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); | |
4038 | if (error != 0) | |
4039 | return (error); | |
4040 | ||
4041 | /* | |
4042 | * If we are rewinding to the checkpoint then we need to repeat | |
4043 | * everything we've done so far in this function but this time | |
4044 | * selecting the checkpointed uberblock and using that to open | |
4045 | * the MOS. | |
4046 | */ | |
4047 | if (checkpoint_rewind) { | |
4048 | /* | |
4049 | * If we are rewinding to the checkpoint update config cache | |
4050 | * anyway. | |
4051 | */ | |
4052 | update_config_cache = B_TRUE; | |
4053 | ||
4054 | /* | |
4055 | * Extract the checkpointed uberblock from the current MOS | |
4056 | * and use this as the pool's uberblock from now on. If the | |
4057 | * pool is imported as writeable we also write the checkpoint | |
4058 | * uberblock to the labels, making the rewind permanent. | |
4059 | */ | |
4060 | error = spa_ld_checkpoint_rewind(spa); | |
4061 | if (error != 0) | |
4062 | return (error); | |
4063 | ||
4064 | /* | |
4065 | * Redo the loading process process again with the | |
4066 | * checkpointed uberblock. | |
4067 | */ | |
4068 | spa_ld_prepare_for_reload(spa); | |
4069 | spa_load_note(spa, "LOADING checkpointed uberblock"); | |
4070 | error = spa_ld_mos_with_trusted_config(spa, type, NULL); | |
4071 | if (error != 0) | |
4072 | return (error); | |
4073 | } | |
4074 | ||
4075 | /* | |
4076 | * Retrieve the checkpoint txg if the pool has a checkpoint. | |
4077 | */ | |
4078 | error = spa_ld_read_checkpoint_txg(spa); | |
4079 | if (error != 0) | |
4080 | return (error); | |
4081 | ||
9eb7b46e PZ |
4082 | /* |
4083 | * Retrieve the mapping of indirect vdevs. Those vdevs were removed | |
4084 | * from the pool and their contents were re-mapped to other vdevs. Note | |
4085 | * that everything that we read before this step must have been | |
4086 | * rewritten on concrete vdevs after the last device removal was | |
4087 | * initiated. Otherwise we could be reading from indirect vdevs before | |
4088 | * we have loaded their mappings. | |
4089 | */ | |
4090 | error = spa_ld_open_indirect_vdev_metadata(spa); | |
4091 | if (error != 0) | |
4092 | return (error); | |
4093 | ||
4094 | /* | |
4095 | * Retrieve the full list of active features from the MOS and check if | |
4096 | * they are all supported. | |
4097 | */ | |
4a0ee12a | 4098 | error = spa_ld_check_features(spa, &missing_feat_write); |
9eb7b46e PZ |
4099 | if (error != 0) |
4100 | return (error); | |
4101 | ||
4102 | /* | |
4103 | * Load several special directories from the MOS needed by the dsl_pool | |
4104 | * layer. | |
4105 | */ | |
4106 | error = spa_ld_load_special_directories(spa); | |
4107 | if (error != 0) | |
4108 | return (error); | |
4109 | ||
9eb7b46e PZ |
4110 | /* |
4111 | * Retrieve pool properties from the MOS. | |
4112 | */ | |
4113 | error = spa_ld_get_props(spa); | |
4114 | if (error != 0) | |
4115 | return (error); | |
4116 | ||
4117 | /* | |
4118 | * Retrieve the list of auxiliary devices - cache devices and spares - | |
4119 | * and open them. | |
4120 | */ | |
4121 | error = spa_ld_open_aux_vdevs(spa, type); | |
4122 | if (error != 0) | |
4123 | return (error); | |
4124 | ||
4125 | /* | |
4126 | * Load the metadata for all vdevs. Also check if unopenable devices | |
4127 | * should be autoreplaced. | |
4128 | */ | |
4a0ee12a | 4129 | error = spa_ld_load_vdev_metadata(spa); |
9eb7b46e PZ |
4130 | if (error != 0) |
4131 | return (error); | |
4132 | ||
4133 | error = spa_ld_load_dedup_tables(spa); | |
4134 | if (error != 0) | |
4135 | return (error); | |
4136 | ||
4137 | /* | |
4138 | * Verify the logs now to make sure we don't have any unexpected errors | |
4139 | * when we claim log blocks later. | |
4140 | */ | |
4141 | error = spa_ld_verify_logs(spa, type, ereport); | |
4142 | if (error != 0) | |
4143 | return (error); | |
4144 | ||
9ae529ec | 4145 | if (missing_feat_write) { |
6cb8e530 | 4146 | ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); |
9ae529ec CS |
4147 | |
4148 | /* | |
4149 | * At this point, we know that we can open the pool in | |
4150 | * read-only mode but not read-write mode. We now have enough | |
4151 | * information and can return to userland. | |
4152 | */ | |
9eb7b46e PZ |
4153 | return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, |
4154 | ENOTSUP)); | |
9ae529ec CS |
4155 | } |
4156 | ||
572e2857 | 4157 | /* |
9eb7b46e PZ |
4158 | * Traverse the last txgs to make sure the pool was left off in a safe |
4159 | * state. When performing an extreme rewind, we verify the whole pool, | |
4160 | * which can take a very long time. | |
572e2857 | 4161 | */ |
4a0ee12a | 4162 | error = spa_ld_verify_pool_data(spa); |
9eb7b46e PZ |
4163 | if (error != 0) |
4164 | return (error); | |
572e2857 | 4165 | |
9eb7b46e PZ |
4166 | /* |
4167 | * Calculate the deflated space for the pool. This must be done before | |
4168 | * we write anything to the pool because we'd need to update the space | |
4169 | * accounting using the deflated sizes. | |
4170 | */ | |
4171 | spa_update_dspace(spa); | |
4172 | ||
4173 | /* | |
4174 | * We have now retrieved all the information we needed to open the | |
4175 | * pool. If we are importing the pool in read-write mode, a few | |
4176 | * additional steps must be performed to finish the import. | |
4177 | */ | |
6cb8e530 | 4178 | if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || |
428870ff | 4179 | spa->spa_load_max_txg == UINT64_MAX)) { |
6cb8e530 PZ |
4180 | uint64_t config_cache_txg = spa->spa_config_txg; |
4181 | ||
4182 | ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f | 4183 | |
d2734cce SD |
4184 | /* |
4185 | * In case of a checkpoint rewind, log the original txg | |
4186 | * of the checkpointed uberblock. | |
4187 | */ | |
4188 | if (checkpoint_rewind) { | |
4189 | spa_history_log_internal(spa, "checkpoint rewind", | |
4190 | NULL, "rewound state to txg=%llu", | |
4191 | (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); | |
4192 | } | |
4193 | ||
34dc7c2f | 4194 | /* |
9eb7b46e | 4195 | * Traverse the ZIL and claim all blocks. |
34dc7c2f | 4196 | */ |
9eb7b46e | 4197 | spa_ld_claim_log_blocks(spa); |
428870ff | 4198 | |
9eb7b46e PZ |
4199 | /* |
4200 | * Kick-off the syncing thread. | |
4201 | */ | |
34dc7c2f BB |
4202 | spa->spa_sync_on = B_TRUE; |
4203 | txg_sync_start(spa->spa_dsl_pool); | |
379ca9cf | 4204 | mmp_thread_start(spa); |
34dc7c2f BB |
4205 | |
4206 | /* | |
428870ff BB |
4207 | * Wait for all claims to sync. We sync up to the highest |
4208 | * claimed log block birth time so that claimed log blocks | |
4209 | * don't appear to be from the future. spa_claim_max_txg | |
9eb7b46e PZ |
4210 | * will have been set for us by ZIL traversal operations |
4211 | * performed above. | |
34dc7c2f | 4212 | */ |
428870ff | 4213 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
4214 | |
4215 | /* | |
9eb7b46e PZ |
4216 | * Check if we need to request an update of the config. On the |
4217 | * next sync, we would update the config stored in vdev labels | |
4218 | * and the cachefile (by default /etc/zfs/zpool.cache). | |
34dc7c2f | 4219 | */ |
6cb8e530 | 4220 | spa_ld_check_for_config_update(spa, config_cache_txg, |
d2734cce | 4221 | update_config_cache); |
fb5f0bc8 BB |
4222 | |
4223 | /* | |
4224 | * Check all DTLs to see if anything needs resilvering. | |
4225 | */ | |
428870ff | 4226 | if (!dsl_scan_resilvering(spa->spa_dsl_pool) && |
9eb7b46e | 4227 | vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) |
fb5f0bc8 | 4228 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
428870ff | 4229 | |
6f1ffb06 MA |
4230 | /* |
4231 | * Log the fact that we booted up (so that we can detect if | |
4232 | * we rebooted in the middle of an operation). | |
4233 | */ | |
d5e024cb | 4234 | spa_history_log_version(spa, "open", NULL); |
6f1ffb06 | 4235 | |
9b2266e3 SD |
4236 | spa_restart_removal(spa); |
4237 | spa_spawn_aux_threads(spa); | |
4238 | ||
428870ff BB |
4239 | /* |
4240 | * Delete any inconsistent datasets. | |
9b2266e3 SD |
4241 | * |
4242 | * Note: | |
4243 | * Since we may be issuing deletes for clones here, | |
4244 | * we make sure to do so after we've spawned all the | |
4245 | * auxiliary threads above (from which the livelist | |
4246 | * deletion zthr is part of). | |
428870ff BB |
4247 | */ |
4248 | (void) dmu_objset_find(spa_name(spa), | |
4249 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
4250 | ||
4251 | /* | |
4252 | * Clean up any stale temporary dataset userrefs. | |
4253 | */ | |
4254 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
a1d477c2 | 4255 | |
619f0976 GW |
4256 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
4257 | vdev_initialize_restart(spa->spa_root_vdev); | |
4258 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f BB |
4259 | } |
4260 | ||
4a0ee12a PZ |
4261 | spa_load_note(spa, "LOADED"); |
4262 | ||
428870ff BB |
4263 | return (0); |
4264 | } | |
34dc7c2f | 4265 | |
428870ff | 4266 | static int |
6cb8e530 | 4267 | spa_load_retry(spa_t *spa, spa_load_state_t state) |
428870ff | 4268 | { |
572e2857 BB |
4269 | int mode = spa->spa_mode; |
4270 | ||
428870ff BB |
4271 | spa_unload(spa); |
4272 | spa_deactivate(spa); | |
4273 | ||
dea377c0 | 4274 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; |
428870ff | 4275 | |
572e2857 | 4276 | spa_activate(spa, mode); |
428870ff BB |
4277 | spa_async_suspend(spa); |
4278 | ||
4a0ee12a PZ |
4279 | spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", |
4280 | (u_longlong_t)spa->spa_load_max_txg); | |
4281 | ||
6cb8e530 | 4282 | return (spa_load(spa, state, SPA_IMPORT_EXISTING)); |
428870ff BB |
4283 | } |
4284 | ||
9ae529ec CS |
4285 | /* |
4286 | * If spa_load() fails this function will try loading prior txg's. If | |
4287 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
4288 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
4289 | * function will not rewind the pool and will return the same error as | |
4290 | * spa_load(). | |
4291 | */ | |
428870ff | 4292 | static int |
6cb8e530 PZ |
4293 | spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, |
4294 | int rewind_flags) | |
428870ff | 4295 | { |
9ae529ec | 4296 | nvlist_t *loadinfo = NULL; |
428870ff BB |
4297 | nvlist_t *config = NULL; |
4298 | int load_error, rewind_error; | |
4299 | uint64_t safe_rewind_txg; | |
4300 | uint64_t min_txg; | |
4301 | ||
4302 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
4303 | spa->spa_load_max_txg = spa->spa_load_txg; | |
4304 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
4305 | } else { | |
4306 | spa->spa_load_max_txg = max_request; | |
dea377c0 MA |
4307 | if (max_request != UINT64_MAX) |
4308 | spa->spa_extreme_rewind = B_TRUE; | |
428870ff BB |
4309 | } |
4310 | ||
6cb8e530 | 4311 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); |
428870ff BB |
4312 | if (load_error == 0) |
4313 | return (0); | |
d2734cce SD |
4314 | if (load_error == ZFS_ERR_NO_CHECKPOINT) { |
4315 | /* | |
4316 | * When attempting checkpoint-rewind on a pool with no | |
4317 | * checkpoint, we should not attempt to load uberblocks | |
4318 | * from previous txgs when spa_load fails. | |
4319 | */ | |
4320 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4321 | return (load_error); | |
4322 | } | |
428870ff BB |
4323 | |
4324 | if (spa->spa_root_vdev != NULL) | |
4325 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
4326 | ||
4327 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
4328 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
4329 | ||
4330 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
4331 | nvlist_free(config); | |
4332 | return (load_error); | |
4333 | } | |
4334 | ||
9ae529ec CS |
4335 | if (state == SPA_LOAD_RECOVER) { |
4336 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 4337 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
4338 | } else { |
4339 | /* | |
4340 | * If we aren't rolling back save the load info from our first | |
4341 | * import attempt so that we can restore it after attempting | |
4342 | * to rewind. | |
4343 | */ | |
4344 | loadinfo = spa->spa_load_info; | |
4345 | spa->spa_load_info = fnvlist_alloc(); | |
4346 | } | |
428870ff BB |
4347 | |
4348 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
4349 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
4350 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
4351 | TXG_INITIAL : safe_rewind_txg; | |
4352 | ||
4353 | /* | |
4354 | * Continue as long as we're finding errors, we're still within | |
4355 | * the acceptable rewind range, and we're still finding uberblocks | |
4356 | */ | |
4357 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
4358 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
4359 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
4360 | spa->spa_extreme_rewind = B_TRUE; | |
6cb8e530 | 4361 | rewind_error = spa_load_retry(spa, state); |
428870ff BB |
4362 | } |
4363 | ||
428870ff BB |
4364 | spa->spa_extreme_rewind = B_FALSE; |
4365 | spa->spa_load_max_txg = UINT64_MAX; | |
4366 | ||
4367 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
4368 | spa_config_set(spa, config); | |
ee6370a7 | 4369 | else |
4370 | nvlist_free(config); | |
428870ff | 4371 | |
9ae529ec CS |
4372 | if (state == SPA_LOAD_RECOVER) { |
4373 | ASSERT3P(loadinfo, ==, NULL); | |
4374 | return (rewind_error); | |
4375 | } else { | |
4376 | /* Store the rewind info as part of the initial load info */ | |
4377 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
4378 | spa->spa_load_info); | |
4379 | ||
4380 | /* Restore the initial load info */ | |
4381 | fnvlist_free(spa->spa_load_info); | |
4382 | spa->spa_load_info = loadinfo; | |
4383 | ||
4384 | return (load_error); | |
4385 | } | |
34dc7c2f BB |
4386 | } |
4387 | ||
4388 | /* | |
4389 | * Pool Open/Import | |
4390 | * | |
4391 | * The import case is identical to an open except that the configuration is sent | |
4392 | * down from userland, instead of grabbed from the configuration cache. For the | |
4393 | * case of an open, the pool configuration will exist in the | |
4394 | * POOL_STATE_UNINITIALIZED state. | |
4395 | * | |
4396 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
4397 | * the same time open the pool, without having to keep around the spa_t in some | |
4398 | * ambiguous state. | |
4399 | */ | |
4400 | static int | |
428870ff BB |
4401 | spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, |
4402 | nvlist_t **config) | |
34dc7c2f BB |
4403 | { |
4404 | spa_t *spa; | |
572e2857 | 4405 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 4406 | int error; |
34dc7c2f | 4407 | int locked = B_FALSE; |
526af785 | 4408 | int firstopen = B_FALSE; |
34dc7c2f BB |
4409 | |
4410 | *spapp = NULL; | |
4411 | ||
4412 | /* | |
4413 | * As disgusting as this is, we need to support recursive calls to this | |
4414 | * function because dsl_dir_open() is called during spa_load(), and ends | |
4415 | * up calling spa_open() again. The real fix is to figure out how to | |
4416 | * avoid dsl_dir_open() calling this in the first place. | |
4417 | */ | |
c25b8f99 | 4418 | if (MUTEX_NOT_HELD(&spa_namespace_lock)) { |
34dc7c2f BB |
4419 | mutex_enter(&spa_namespace_lock); |
4420 | locked = B_TRUE; | |
4421 | } | |
4422 | ||
4423 | if ((spa = spa_lookup(pool)) == NULL) { | |
4424 | if (locked) | |
4425 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 4426 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 4427 | } |
428870ff | 4428 | |
34dc7c2f | 4429 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
8a393be3 | 4430 | zpool_load_policy_t policy; |
428870ff | 4431 | |
526af785 PJD |
4432 | firstopen = B_TRUE; |
4433 | ||
8a393be3 | 4434 | zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, |
428870ff | 4435 | &policy); |
8a393be3 | 4436 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) |
428870ff | 4437 | state = SPA_LOAD_RECOVER; |
34dc7c2f | 4438 | |
fb5f0bc8 | 4439 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 4440 | |
428870ff BB |
4441 | if (state != SPA_LOAD_RECOVER) |
4442 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6cb8e530 | 4443 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; |
428870ff | 4444 | |
4a0ee12a | 4445 | zfs_dbgmsg("spa_open_common: opening %s", pool); |
8a393be3 PZ |
4446 | error = spa_load_best(spa, state, policy.zlp_txg, |
4447 | policy.zlp_rewind); | |
34dc7c2f BB |
4448 | |
4449 | if (error == EBADF) { | |
4450 | /* | |
4451 | * If vdev_validate() returns failure (indicated by | |
4452 | * EBADF), it indicates that one of the vdevs indicates | |
4453 | * that the pool has been exported or destroyed. If | |
4454 | * this is the case, the config cache is out of sync and | |
4455 | * we should remove the pool from the namespace. | |
4456 | */ | |
34dc7c2f BB |
4457 | spa_unload(spa); |
4458 | spa_deactivate(spa); | |
a1d477c2 | 4459 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 4460 | spa_remove(spa); |
34dc7c2f BB |
4461 | if (locked) |
4462 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 4463 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
4464 | } |
4465 | ||
4466 | if (error) { | |
4467 | /* | |
4468 | * We can't open the pool, but we still have useful | |
4469 | * information: the state of each vdev after the | |
4470 | * attempted vdev_open(). Return this to the user. | |
4471 | */ | |
572e2857 | 4472 | if (config != NULL && spa->spa_config) { |
428870ff | 4473 | VERIFY(nvlist_dup(spa->spa_config, config, |
79c76d5b | 4474 | KM_SLEEP) == 0); |
572e2857 BB |
4475 | VERIFY(nvlist_add_nvlist(*config, |
4476 | ZPOOL_CONFIG_LOAD_INFO, | |
4477 | spa->spa_load_info) == 0); | |
4478 | } | |
34dc7c2f BB |
4479 | spa_unload(spa); |
4480 | spa_deactivate(spa); | |
428870ff | 4481 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
4482 | if (locked) |
4483 | mutex_exit(&spa_namespace_lock); | |
4484 | *spapp = NULL; | |
4485 | return (error); | |
34dc7c2f | 4486 | } |
34dc7c2f BB |
4487 | } |
4488 | ||
4489 | spa_open_ref(spa, tag); | |
4490 | ||
b128c09f | 4491 | if (config != NULL) |
34dc7c2f | 4492 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 4493 | |
572e2857 BB |
4494 | /* |
4495 | * If we've recovered the pool, pass back any information we | |
4496 | * gathered while doing the load. | |
4497 | */ | |
4498 | if (state == SPA_LOAD_RECOVER) { | |
4499 | VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, | |
4500 | spa->spa_load_info) == 0); | |
4501 | } | |
4502 | ||
428870ff BB |
4503 | if (locked) { |
4504 | spa->spa_last_open_failed = 0; | |
4505 | spa->spa_last_ubsync_txg = 0; | |
4506 | spa->spa_load_txg = 0; | |
4507 | mutex_exit(&spa_namespace_lock); | |
4508 | } | |
4509 | ||
526af785 | 4510 | if (firstopen) |
a0bd735a | 4511 | zvol_create_minors(spa, spa_name(spa), B_TRUE); |
526af785 | 4512 | |
428870ff BB |
4513 | *spapp = spa; |
4514 | ||
34dc7c2f BB |
4515 | return (0); |
4516 | } | |
4517 | ||
428870ff BB |
4518 | int |
4519 | spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, | |
4520 | nvlist_t **config) | |
4521 | { | |
4522 | return (spa_open_common(name, spapp, tag, policy, config)); | |
4523 | } | |
4524 | ||
34dc7c2f BB |
4525 | int |
4526 | spa_open(const char *name, spa_t **spapp, void *tag) | |
4527 | { | |
428870ff | 4528 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
4529 | } |
4530 | ||
4531 | /* | |
4532 | * Lookup the given spa_t, incrementing the inject count in the process, | |
4533 | * preventing it from being exported or destroyed. | |
4534 | */ | |
4535 | spa_t * | |
4536 | spa_inject_addref(char *name) | |
4537 | { | |
4538 | spa_t *spa; | |
4539 | ||
4540 | mutex_enter(&spa_namespace_lock); | |
4541 | if ((spa = spa_lookup(name)) == NULL) { | |
4542 | mutex_exit(&spa_namespace_lock); | |
4543 | return (NULL); | |
4544 | } | |
4545 | spa->spa_inject_ref++; | |
4546 | mutex_exit(&spa_namespace_lock); | |
4547 | ||
4548 | return (spa); | |
4549 | } | |
4550 | ||
4551 | void | |
4552 | spa_inject_delref(spa_t *spa) | |
4553 | { | |
4554 | mutex_enter(&spa_namespace_lock); | |
4555 | spa->spa_inject_ref--; | |
4556 | mutex_exit(&spa_namespace_lock); | |
4557 | } | |
4558 | ||
4559 | /* | |
4560 | * Add spares device information to the nvlist. | |
4561 | */ | |
4562 | static void | |
4563 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
4564 | { | |
4565 | nvlist_t **spares; | |
4566 | uint_t i, nspares; | |
4567 | nvlist_t *nvroot; | |
4568 | uint64_t guid; | |
4569 | vdev_stat_t *vs; | |
4570 | uint_t vsc; | |
4571 | uint64_t pool; | |
4572 | ||
9babb374 BB |
4573 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
4574 | ||
34dc7c2f BB |
4575 | if (spa->spa_spares.sav_count == 0) |
4576 | return; | |
4577 | ||
4578 | VERIFY(nvlist_lookup_nvlist(config, | |
4579 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
4580 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
4581 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
4582 | if (nspares != 0) { | |
4583 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
4584 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
4585 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
4586 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
4587 | ||
4588 | /* | |
4589 | * Go through and find any spares which have since been | |
4590 | * repurposed as an active spare. If this is the case, update | |
4591 | * their status appropriately. | |
4592 | */ | |
4593 | for (i = 0; i < nspares; i++) { | |
4594 | VERIFY(nvlist_lookup_uint64(spares[i], | |
4595 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
b128c09f BB |
4596 | if (spa_spare_exists(guid, &pool, NULL) && |
4597 | pool != 0ULL) { | |
34dc7c2f | 4598 | VERIFY(nvlist_lookup_uint64_array( |
428870ff | 4599 | spares[i], ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
4600 | (uint64_t **)&vs, &vsc) == 0); |
4601 | vs->vs_state = VDEV_STATE_CANT_OPEN; | |
4602 | vs->vs_aux = VDEV_AUX_SPARED; | |
4603 | } | |
4604 | } | |
4605 | } | |
4606 | } | |
4607 | ||
4608 | /* | |
4609 | * Add l2cache device information to the nvlist, including vdev stats. | |
4610 | */ | |
4611 | static void | |
4612 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
4613 | { | |
4614 | nvlist_t **l2cache; | |
4615 | uint_t i, j, nl2cache; | |
4616 | nvlist_t *nvroot; | |
4617 | uint64_t guid; | |
4618 | vdev_t *vd; | |
4619 | vdev_stat_t *vs; | |
4620 | uint_t vsc; | |
4621 | ||
9babb374 BB |
4622 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
4623 | ||
34dc7c2f BB |
4624 | if (spa->spa_l2cache.sav_count == 0) |
4625 | return; | |
4626 | ||
34dc7c2f BB |
4627 | VERIFY(nvlist_lookup_nvlist(config, |
4628 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
4629 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
4630 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
4631 | if (nl2cache != 0) { | |
4632 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
4633 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
4634 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
4635 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
4636 | ||
4637 | /* | |
4638 | * Update level 2 cache device stats. | |
4639 | */ | |
4640 | ||
4641 | for (i = 0; i < nl2cache; i++) { | |
4642 | VERIFY(nvlist_lookup_uint64(l2cache[i], | |
4643 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
4644 | ||
4645 | vd = NULL; | |
4646 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
4647 | if (guid == | |
4648 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
4649 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
4650 | break; | |
4651 | } | |
4652 | } | |
4653 | ASSERT(vd != NULL); | |
4654 | ||
4655 | VERIFY(nvlist_lookup_uint64_array(l2cache[i], | |
428870ff BB |
4656 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) |
4657 | == 0); | |
34dc7c2f | 4658 | vdev_get_stats(vd, vs); |
193a37cb TH |
4659 | vdev_config_generate_stats(vd, l2cache[i]); |
4660 | ||
34dc7c2f BB |
4661 | } |
4662 | } | |
34dc7c2f BB |
4663 | } |
4664 | ||
9ae529ec | 4665 | static void |
417104bd | 4666 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) |
9ae529ec | 4667 | { |
9ae529ec CS |
4668 | zap_cursor_t zc; |
4669 | zap_attribute_t za; | |
4670 | ||
9ae529ec CS |
4671 | if (spa->spa_feat_for_read_obj != 0) { |
4672 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
4673 | spa->spa_feat_for_read_obj); | |
4674 | zap_cursor_retrieve(&zc, &za) == 0; | |
4675 | zap_cursor_advance(&zc)) { | |
4676 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
4677 | za.za_num_integers == 1); | |
417104bd | 4678 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
4679 | za.za_first_integer)); |
4680 | } | |
4681 | zap_cursor_fini(&zc); | |
4682 | } | |
4683 | ||
4684 | if (spa->spa_feat_for_write_obj != 0) { | |
4685 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
4686 | spa->spa_feat_for_write_obj); | |
4687 | zap_cursor_retrieve(&zc, &za) == 0; | |
4688 | zap_cursor_advance(&zc)) { | |
4689 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
4690 | za.za_num_integers == 1); | |
417104bd | 4691 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
4692 | za.za_first_integer)); |
4693 | } | |
4694 | zap_cursor_fini(&zc); | |
4695 | } | |
417104bd NB |
4696 | } |
4697 | ||
4698 | static void | |
4699 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
4700 | { | |
4701 | int i; | |
4702 | ||
4703 | for (i = 0; i < SPA_FEATURES; i++) { | |
4704 | zfeature_info_t feature = spa_feature_table[i]; | |
4705 | uint64_t refcount; | |
4706 | ||
4707 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
4708 | continue; | |
4709 | ||
4710 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
4711 | } | |
4712 | } | |
4713 | ||
4714 | /* | |
4715 | * Store a list of pool features and their reference counts in the | |
4716 | * config. | |
4717 | * | |
4718 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
4719 | * the pool features and reference counts from disk, then save the list | |
4720 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
4721 | * and refresh its values from the cached reference counts. This | |
4722 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
4723 | * clear' can resume the pool. | |
4724 | */ | |
4725 | static void | |
4726 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
4727 | { | |
4eb30c68 | 4728 | nvlist_t *features; |
417104bd NB |
4729 | |
4730 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
4731 | ||
4eb30c68 NB |
4732 | mutex_enter(&spa->spa_feat_stats_lock); |
4733 | features = spa->spa_feat_stats; | |
4734 | ||
417104bd NB |
4735 | if (features != NULL) { |
4736 | spa_feature_stats_from_cache(spa, features); | |
4737 | } else { | |
4738 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
4739 | spa->spa_feat_stats = features; | |
4740 | spa_feature_stats_from_disk(spa, features); | |
4741 | } | |
9ae529ec | 4742 | |
417104bd NB |
4743 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, |
4744 | features)); | |
4eb30c68 NB |
4745 | |
4746 | mutex_exit(&spa->spa_feat_stats_lock); | |
9ae529ec CS |
4747 | } |
4748 | ||
34dc7c2f | 4749 | int |
9ae529ec CS |
4750 | spa_get_stats(const char *name, nvlist_t **config, |
4751 | char *altroot, size_t buflen) | |
34dc7c2f BB |
4752 | { |
4753 | int error; | |
4754 | spa_t *spa; | |
4755 | ||
4756 | *config = NULL; | |
428870ff | 4757 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 4758 | |
9babb374 BB |
4759 | if (spa != NULL) { |
4760 | /* | |
4761 | * This still leaves a window of inconsistency where the spares | |
4762 | * or l2cache devices could change and the config would be | |
4763 | * self-inconsistent. | |
4764 | */ | |
4765 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 4766 | |
9babb374 | 4767 | if (*config != NULL) { |
572e2857 BB |
4768 | uint64_t loadtimes[2]; |
4769 | ||
4770 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
4771 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
4772 | VERIFY(nvlist_add_uint64_array(*config, | |
4773 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); | |
4774 | ||
b128c09f | 4775 | VERIFY(nvlist_add_uint64(*config, |
9babb374 BB |
4776 | ZPOOL_CONFIG_ERRCOUNT, |
4777 | spa_get_errlog_size(spa)) == 0); | |
4778 | ||
cec3a0a1 | 4779 | if (spa_suspended(spa)) { |
9babb374 BB |
4780 | VERIFY(nvlist_add_uint64(*config, |
4781 | ZPOOL_CONFIG_SUSPENDED, | |
4782 | spa->spa_failmode) == 0); | |
cec3a0a1 OF |
4783 | VERIFY(nvlist_add_uint64(*config, |
4784 | ZPOOL_CONFIG_SUSPENDED_REASON, | |
4785 | spa->spa_suspended) == 0); | |
4786 | } | |
b128c09f | 4787 | |
9babb374 BB |
4788 | spa_add_spares(spa, *config); |
4789 | spa_add_l2cache(spa, *config); | |
9ae529ec | 4790 | spa_add_feature_stats(spa, *config); |
9babb374 | 4791 | } |
34dc7c2f BB |
4792 | } |
4793 | ||
4794 | /* | |
4795 | * We want to get the alternate root even for faulted pools, so we cheat | |
4796 | * and call spa_lookup() directly. | |
4797 | */ | |
4798 | if (altroot) { | |
4799 | if (spa == NULL) { | |
4800 | mutex_enter(&spa_namespace_lock); | |
4801 | spa = spa_lookup(name); | |
4802 | if (spa) | |
4803 | spa_altroot(spa, altroot, buflen); | |
4804 | else | |
4805 | altroot[0] = '\0'; | |
4806 | spa = NULL; | |
4807 | mutex_exit(&spa_namespace_lock); | |
4808 | } else { | |
4809 | spa_altroot(spa, altroot, buflen); | |
4810 | } | |
4811 | } | |
4812 | ||
9babb374 BB |
4813 | if (spa != NULL) { |
4814 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 4815 | spa_close(spa, FTAG); |
9babb374 | 4816 | } |
34dc7c2f BB |
4817 | |
4818 | return (error); | |
4819 | } | |
4820 | ||
4821 | /* | |
4822 | * Validate that the auxiliary device array is well formed. We must have an | |
4823 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
4824 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
4825 | * specified, as long as they are well-formed. | |
4826 | */ | |
4827 | static int | |
4828 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
4829 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
4830 | vdev_labeltype_t label) | |
4831 | { | |
4832 | nvlist_t **dev; | |
4833 | uint_t i, ndev; | |
4834 | vdev_t *vd; | |
4835 | int error; | |
4836 | ||
b128c09f BB |
4837 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
4838 | ||
34dc7c2f BB |
4839 | /* |
4840 | * It's acceptable to have no devs specified. | |
4841 | */ | |
4842 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
4843 | return (0); | |
4844 | ||
4845 | if (ndev == 0) | |
2e528b49 | 4846 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
4847 | |
4848 | /* | |
4849 | * Make sure the pool is formatted with a version that supports this | |
4850 | * device type. | |
4851 | */ | |
4852 | if (spa_version(spa) < version) | |
2e528b49 | 4853 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
4854 | |
4855 | /* | |
4856 | * Set the pending device list so we correctly handle device in-use | |
4857 | * checking. | |
4858 | */ | |
4859 | sav->sav_pending = dev; | |
4860 | sav->sav_npending = ndev; | |
4861 | ||
4862 | for (i = 0; i < ndev; i++) { | |
4863 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
4864 | mode)) != 0) | |
4865 | goto out; | |
4866 | ||
4867 | if (!vd->vdev_ops->vdev_op_leaf) { | |
4868 | vdev_free(vd); | |
2e528b49 | 4869 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
4870 | goto out; |
4871 | } | |
4872 | ||
34dc7c2f BB |
4873 | vd->vdev_top = vd; |
4874 | ||
4875 | if ((error = vdev_open(vd)) == 0 && | |
4876 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
4877 | VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, | |
4878 | vd->vdev_guid) == 0); | |
4879 | } | |
4880 | ||
4881 | vdev_free(vd); | |
4882 | ||
4883 | if (error && | |
4884 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
4885 | goto out; | |
4886 | else | |
4887 | error = 0; | |
4888 | } | |
4889 | ||
4890 | out: | |
4891 | sav->sav_pending = NULL; | |
4892 | sav->sav_npending = 0; | |
4893 | return (error); | |
4894 | } | |
4895 | ||
4896 | static int | |
4897 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
4898 | { | |
4899 | int error; | |
4900 | ||
b128c09f BB |
4901 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
4902 | ||
34dc7c2f BB |
4903 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
4904 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
4905 | VDEV_LABEL_SPARE)) != 0) { | |
4906 | return (error); | |
4907 | } | |
4908 | ||
4909 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
4910 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
4911 | VDEV_LABEL_L2CACHE)); | |
4912 | } | |
4913 | ||
4914 | static void | |
4915 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
4916 | const char *config) | |
4917 | { | |
4918 | int i; | |
4919 | ||
4920 | if (sav->sav_config != NULL) { | |
4921 | nvlist_t **olddevs; | |
4922 | uint_t oldndevs; | |
4923 | nvlist_t **newdevs; | |
4924 | ||
4925 | /* | |
4e33ba4c | 4926 | * Generate new dev list by concatenating with the |
34dc7c2f BB |
4927 | * current dev list. |
4928 | */ | |
4929 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, | |
4930 | &olddevs, &oldndevs) == 0); | |
4931 | ||
4932 | newdevs = kmem_alloc(sizeof (void *) * | |
79c76d5b | 4933 | (ndevs + oldndevs), KM_SLEEP); |
34dc7c2f BB |
4934 | for (i = 0; i < oldndevs; i++) |
4935 | VERIFY(nvlist_dup(olddevs[i], &newdevs[i], | |
79c76d5b | 4936 | KM_SLEEP) == 0); |
34dc7c2f BB |
4937 | for (i = 0; i < ndevs; i++) |
4938 | VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], | |
79c76d5b | 4939 | KM_SLEEP) == 0); |
34dc7c2f BB |
4940 | |
4941 | VERIFY(nvlist_remove(sav->sav_config, config, | |
4942 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
4943 | ||
4944 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
4945 | config, newdevs, ndevs + oldndevs) == 0); | |
4946 | for (i = 0; i < oldndevs + ndevs; i++) | |
4947 | nvlist_free(newdevs[i]); | |
4948 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
4949 | } else { | |
4950 | /* | |
4951 | * Generate a new dev list. | |
4952 | */ | |
4953 | VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 4954 | KM_SLEEP) == 0); |
34dc7c2f BB |
4955 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, |
4956 | devs, ndevs) == 0); | |
4957 | } | |
4958 | } | |
4959 | ||
4960 | /* | |
4961 | * Stop and drop level 2 ARC devices | |
4962 | */ | |
4963 | void | |
4964 | spa_l2cache_drop(spa_t *spa) | |
4965 | { | |
4966 | vdev_t *vd; | |
4967 | int i; | |
4968 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
4969 | ||
4970 | for (i = 0; i < sav->sav_count; i++) { | |
4971 | uint64_t pool; | |
4972 | ||
4973 | vd = sav->sav_vdevs[i]; | |
4974 | ASSERT(vd != NULL); | |
4975 | ||
fb5f0bc8 BB |
4976 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
4977 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 4978 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
4979 | } |
4980 | } | |
4981 | ||
b5256303 TC |
4982 | /* |
4983 | * Verify encryption parameters for spa creation. If we are encrypting, we must | |
4984 | * have the encryption feature flag enabled. | |
4985 | */ | |
4986 | static int | |
4987 | spa_create_check_encryption_params(dsl_crypto_params_t *dcp, | |
4988 | boolean_t has_encryption) | |
4989 | { | |
4990 | if (dcp->cp_crypt != ZIO_CRYPT_OFF && | |
4991 | dcp->cp_crypt != ZIO_CRYPT_INHERIT && | |
4992 | !has_encryption) | |
4993 | return (SET_ERROR(ENOTSUP)); | |
4994 | ||
1fff937a | 4995 | return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); |
b5256303 TC |
4996 | } |
4997 | ||
34dc7c2f BB |
4998 | /* |
4999 | * Pool Creation | |
5000 | */ | |
5001 | int | |
5002 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b5256303 | 5003 | nvlist_t *zplprops, dsl_crypto_params_t *dcp) |
34dc7c2f BB |
5004 | { |
5005 | spa_t *spa; | |
5006 | char *altroot = NULL; | |
5007 | vdev_t *rvd; | |
5008 | dsl_pool_t *dp; | |
5009 | dmu_tx_t *tx; | |
9babb374 | 5010 | int error = 0; |
34dc7c2f BB |
5011 | uint64_t txg = TXG_INITIAL; |
5012 | nvlist_t **spares, **l2cache; | |
5013 | uint_t nspares, nl2cache; | |
52ce99dd | 5014 | uint64_t version, obj; |
9ae529ec | 5015 | boolean_t has_features; |
b5256303 TC |
5016 | boolean_t has_encryption; |
5017 | spa_feature_t feat; | |
5018 | char *feat_name; | |
83e9986f RY |
5019 | char *poolname; |
5020 | nvlist_t *nvl; | |
5021 | ||
cc99f275 DB |
5022 | if (props == NULL || |
5023 | nvlist_lookup_string(props, "tname", &poolname) != 0) | |
83e9986f | 5024 | poolname = (char *)pool; |
34dc7c2f BB |
5025 | |
5026 | /* | |
5027 | * If this pool already exists, return failure. | |
5028 | */ | |
5029 | mutex_enter(&spa_namespace_lock); | |
83e9986f | 5030 | if (spa_lookup(poolname) != NULL) { |
34dc7c2f | 5031 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5032 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5033 | } |
5034 | ||
5035 | /* | |
5036 | * Allocate a new spa_t structure. | |
5037 | */ | |
83e9986f RY |
5038 | nvl = fnvlist_alloc(); |
5039 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
34dc7c2f BB |
5040 | (void) nvlist_lookup_string(props, |
5041 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
83e9986f RY |
5042 | spa = spa_add(poolname, nvl, altroot); |
5043 | fnvlist_free(nvl); | |
fb5f0bc8 | 5044 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5045 | |
34dc7c2f | 5046 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
5047 | spa_deactivate(spa); |
5048 | spa_remove(spa); | |
b128c09f | 5049 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
5050 | return (error); |
5051 | } | |
5052 | ||
83e9986f RY |
5053 | /* |
5054 | * Temporary pool names should never be written to disk. | |
5055 | */ | |
5056 | if (poolname != pool) | |
5057 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
5058 | ||
9ae529ec | 5059 | has_features = B_FALSE; |
b5256303 | 5060 | has_encryption = B_FALSE; |
1c27024e | 5061 | for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); |
9ae529ec | 5062 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { |
b5256303 | 5063 | if (zpool_prop_feature(nvpair_name(elem))) { |
9ae529ec | 5064 | has_features = B_TRUE; |
b5256303 TC |
5065 | |
5066 | feat_name = strchr(nvpair_name(elem), '@') + 1; | |
5067 | VERIFY0(zfeature_lookup_name(feat_name, &feat)); | |
5068 | if (feat == SPA_FEATURE_ENCRYPTION) | |
5069 | has_encryption = B_TRUE; | |
5070 | } | |
5071 | } | |
5072 | ||
5073 | /* verify encryption params, if they were provided */ | |
5074 | if (dcp != NULL) { | |
5075 | error = spa_create_check_encryption_params(dcp, has_encryption); | |
5076 | if (error != 0) { | |
5077 | spa_deactivate(spa); | |
5078 | spa_remove(spa); | |
5079 | mutex_exit(&spa_namespace_lock); | |
5080 | return (error); | |
5081 | } | |
9ae529ec CS |
5082 | } |
5083 | ||
5084 | if (has_features || nvlist_lookup_uint64(props, | |
5085 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 5086 | version = SPA_VERSION; |
9ae529ec CS |
5087 | } |
5088 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
5089 | |
5090 | spa->spa_first_txg = txg; | |
5091 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
5092 | spa->spa_uberblock.ub_version = version; |
5093 | spa->spa_ubsync = spa->spa_uberblock; | |
3dfb57a3 | 5094 | spa->spa_load_state = SPA_LOAD_CREATE; |
a1d477c2 MA |
5095 | spa->spa_removing_phys.sr_state = DSS_NONE; |
5096 | spa->spa_removing_phys.sr_removing_vdev = -1; | |
5097 | spa->spa_removing_phys.sr_prev_indirect_vdev = -1; | |
34dc7c2f | 5098 | |
9babb374 BB |
5099 | /* |
5100 | * Create "The Godfather" zio to hold all async IOs | |
5101 | */ | |
e022864d MA |
5102 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
5103 | KM_SLEEP); | |
1c27024e | 5104 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
5105 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
5106 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
5107 | ZIO_FLAG_GODFATHER); | |
5108 | } | |
9babb374 | 5109 | |
34dc7c2f BB |
5110 | /* |
5111 | * Create the root vdev. | |
5112 | */ | |
b128c09f | 5113 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5114 | |
5115 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
5116 | ||
5117 | ASSERT(error != 0 || rvd != NULL); | |
5118 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
5119 | ||
5120 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
2e528b49 | 5121 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5122 | |
5123 | if (error == 0 && | |
5124 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
5125 | (error = spa_validate_aux(spa, nvroot, txg, | |
5126 | VDEV_ALLOC_ADD)) == 0) { | |
cc99f275 DB |
5127 | /* |
5128 | * instantiate the metaslab groups (this will dirty the vdevs) | |
5129 | * we can no longer error exit past this point | |
5130 | */ | |
5131 | for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { | |
5132 | vdev_t *vd = rvd->vdev_child[c]; | |
5133 | ||
5134 | vdev_metaslab_set_size(vd); | |
5135 | vdev_expand(vd, txg); | |
9babb374 | 5136 | } |
34dc7c2f BB |
5137 | } |
5138 | ||
b128c09f | 5139 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5140 | |
5141 | if (error != 0) { | |
5142 | spa_unload(spa); | |
5143 | spa_deactivate(spa); | |
5144 | spa_remove(spa); | |
5145 | mutex_exit(&spa_namespace_lock); | |
5146 | return (error); | |
5147 | } | |
5148 | ||
5149 | /* | |
5150 | * Get the list of spares, if specified. | |
5151 | */ | |
5152 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5153 | &spares, &nspares) == 0) { | |
5154 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5155 | KM_SLEEP) == 0); |
34dc7c2f BB |
5156 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
5157 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 5158 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5159 | spa_load_spares(spa); |
b128c09f | 5160 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5161 | spa->spa_spares.sav_sync = B_TRUE; |
5162 | } | |
5163 | ||
5164 | /* | |
5165 | * Get the list of level 2 cache devices, if specified. | |
5166 | */ | |
5167 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5168 | &l2cache, &nl2cache) == 0) { | |
5169 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 5170 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5171 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
5172 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 5173 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5174 | spa_load_l2cache(spa); |
b128c09f | 5175 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5176 | spa->spa_l2cache.sav_sync = B_TRUE; |
5177 | } | |
5178 | ||
9ae529ec | 5179 | spa->spa_is_initializing = B_TRUE; |
b5256303 | 5180 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); |
9ae529ec | 5181 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 5182 | |
428870ff BB |
5183 | /* |
5184 | * Create DDTs (dedup tables). | |
5185 | */ | |
5186 | ddt_create(spa); | |
5187 | ||
5188 | spa_update_dspace(spa); | |
5189 | ||
34dc7c2f BB |
5190 | tx = dmu_tx_create_assigned(dp, txg); |
5191 | ||
d5e024cb BB |
5192 | /* |
5193 | * Create the pool's history object. | |
5194 | */ | |
5195 | if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history) | |
5196 | spa_history_create_obj(spa, tx); | |
5197 | ||
5198 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); | |
5199 | spa_history_log_version(spa, "create", tx); | |
5200 | ||
34dc7c2f BB |
5201 | /* |
5202 | * Create the pool config object. | |
5203 | */ | |
5204 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 5205 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
5206 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
5207 | ||
5208 | if (zap_add(spa->spa_meta_objset, | |
5209 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
5210 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
5211 | cmn_err(CE_PANIC, "failed to add pool config"); | |
5212 | } | |
5213 | ||
428870ff BB |
5214 | if (zap_add(spa->spa_meta_objset, |
5215 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
5216 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
5217 | cmn_err(CE_PANIC, "failed to add pool version"); | |
5218 | } | |
5219 | ||
34dc7c2f BB |
5220 | /* Newly created pools with the right version are always deflated. */ |
5221 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
5222 | spa->spa_deflate = TRUE; | |
5223 | if (zap_add(spa->spa_meta_objset, | |
5224 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
5225 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
5226 | cmn_err(CE_PANIC, "failed to add deflate"); | |
5227 | } | |
5228 | } | |
5229 | ||
5230 | /* | |
428870ff | 5231 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
5232 | * because sync-to-convergence takes longer if the blocksize |
5233 | * keeps changing. | |
5234 | */ | |
428870ff BB |
5235 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
5236 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 5237 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 5238 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
5239 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
5240 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
5241 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 5242 | } |
428870ff BB |
5243 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
5244 | spa->spa_meta_objset, obj)); | |
34dc7c2f | 5245 | |
3c67d83a TH |
5246 | /* |
5247 | * Generate some random noise for salted checksums to operate on. | |
5248 | */ | |
5249 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
5250 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
5251 | ||
34dc7c2f BB |
5252 | /* |
5253 | * Set pool properties. | |
5254 | */ | |
5255 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
5256 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
5257 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 5258 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
379ca9cf | 5259 | spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); |
428870ff | 5260 | |
d164b209 BB |
5261 | if (props != NULL) { |
5262 | spa_configfile_set(spa, props, B_FALSE); | |
13fe0198 | 5263 | spa_sync_props(props, tx); |
d164b209 | 5264 | } |
34dc7c2f BB |
5265 | |
5266 | dmu_tx_commit(tx); | |
5267 | ||
5268 | spa->spa_sync_on = B_TRUE; | |
b5256303 | 5269 | txg_sync_start(dp); |
379ca9cf | 5270 | mmp_thread_start(spa); |
b5256303 | 5271 | txg_wait_synced(dp, txg); |
34dc7c2f | 5272 | |
9d5b5245 SD |
5273 | spa_spawn_aux_threads(spa); |
5274 | ||
a1d477c2 | 5275 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
34dc7c2f | 5276 | |
0c66c32d JG |
5277 | /* |
5278 | * Don't count references from objsets that are already closed | |
5279 | * and are making their way through the eviction process. | |
5280 | */ | |
5281 | spa_evicting_os_wait(spa); | |
424fd7c3 | 5282 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
3dfb57a3 | 5283 | spa->spa_load_state = SPA_LOAD_NONE; |
b128c09f | 5284 | |
d164b209 BB |
5285 | mutex_exit(&spa_namespace_lock); |
5286 | ||
34dc7c2f BB |
5287 | return (0); |
5288 | } | |
5289 | ||
9babb374 BB |
5290 | /* |
5291 | * Import a non-root pool into the system. | |
5292 | */ | |
5293 | int | |
13fe0198 | 5294 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
5295 | { |
5296 | spa_t *spa; | |
5297 | char *altroot = NULL; | |
428870ff | 5298 | spa_load_state_t state = SPA_LOAD_IMPORT; |
8a393be3 | 5299 | zpool_load_policy_t policy; |
572e2857 BB |
5300 | uint64_t mode = spa_mode_global; |
5301 | uint64_t readonly = B_FALSE; | |
9babb374 | 5302 | int error; |
34dc7c2f BB |
5303 | nvlist_t *nvroot; |
5304 | nvlist_t **spares, **l2cache; | |
5305 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
5306 | |
5307 | /* | |
5308 | * If a pool with this name exists, return failure. | |
5309 | */ | |
5310 | mutex_enter(&spa_namespace_lock); | |
428870ff | 5311 | if (spa_lookup(pool) != NULL) { |
9babb374 | 5312 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5313 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5314 | } |
5315 | ||
5316 | /* | |
5317 | * Create and initialize the spa structure. | |
5318 | */ | |
5319 | (void) nvlist_lookup_string(props, | |
5320 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
5321 | (void) nvlist_lookup_uint64(props, |
5322 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
5323 | if (readonly) | |
5324 | mode = FREAD; | |
428870ff | 5325 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
5326 | spa->spa_import_flags = flags; |
5327 | ||
5328 | /* | |
5329 | * Verbatim import - Take a pool and insert it into the namespace | |
5330 | * as if it had been loaded at boot. | |
5331 | */ | |
5332 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
5333 | if (props != NULL) | |
5334 | spa_configfile_set(spa, props, B_FALSE); | |
5335 | ||
a1d477c2 | 5336 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 5337 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
4a0ee12a | 5338 | zfs_dbgmsg("spa_import: verbatim import of %s", pool); |
572e2857 | 5339 | mutex_exit(&spa_namespace_lock); |
572e2857 BB |
5340 | return (0); |
5341 | } | |
5342 | ||
5343 | spa_activate(spa, mode); | |
34dc7c2f | 5344 | |
9babb374 BB |
5345 | /* |
5346 | * Don't start async tasks until we know everything is healthy. | |
5347 | */ | |
5348 | spa_async_suspend(spa); | |
b128c09f | 5349 | |
8a393be3 PZ |
5350 | zpool_get_load_policy(config, &policy); |
5351 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) | |
572e2857 BB |
5352 | state = SPA_LOAD_RECOVER; |
5353 | ||
6cb8e530 | 5354 | spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; |
572e2857 | 5355 | |
6cb8e530 PZ |
5356 | if (state != SPA_LOAD_RECOVER) { |
5357 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
5358 | zfs_dbgmsg("spa_import: importing %s", pool); | |
5359 | } else { | |
5360 | zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " | |
8a393be3 | 5361 | "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); |
6cb8e530 | 5362 | } |
8a393be3 | 5363 | error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); |
428870ff BB |
5364 | |
5365 | /* | |
572e2857 BB |
5366 | * Propagate anything learned while loading the pool and pass it |
5367 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 5368 | */ |
572e2857 BB |
5369 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
5370 | spa->spa_load_info) == 0); | |
34dc7c2f | 5371 | |
b128c09f | 5372 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5373 | /* |
9babb374 BB |
5374 | * Toss any existing sparelist, as it doesn't have any validity |
5375 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 5376 | */ |
9babb374 | 5377 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
5378 | nvlist_free(spa->spa_spares.sav_config); |
5379 | spa->spa_spares.sav_config = NULL; | |
5380 | spa_load_spares(spa); | |
5381 | } | |
9babb374 | 5382 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
5383 | nvlist_free(spa->spa_l2cache.sav_config); |
5384 | spa->spa_l2cache.sav_config = NULL; | |
5385 | spa_load_l2cache(spa); | |
5386 | } | |
5387 | ||
5388 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
5389 | &nvroot) == 0); | |
b128c09f | 5390 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 5391 | |
d164b209 BB |
5392 | if (props != NULL) |
5393 | spa_configfile_set(spa, props, B_FALSE); | |
5394 | ||
fb5f0bc8 BB |
5395 | if (error != 0 || (props && spa_writeable(spa) && |
5396 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
5397 | spa_unload(spa); |
5398 | spa_deactivate(spa); | |
5399 | spa_remove(spa); | |
34dc7c2f BB |
5400 | mutex_exit(&spa_namespace_lock); |
5401 | return (error); | |
5402 | } | |
5403 | ||
572e2857 BB |
5404 | spa_async_resume(spa); |
5405 | ||
34dc7c2f BB |
5406 | /* |
5407 | * Override any spares and level 2 cache devices as specified by | |
5408 | * the user, as these may have correct device names/devids, etc. | |
5409 | */ | |
5410 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5411 | &spares, &nspares) == 0) { | |
5412 | if (spa->spa_spares.sav_config) | |
5413 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, | |
5414 | ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); | |
5415 | else | |
5416 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, | |
79c76d5b | 5417 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5418 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
5419 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 5420 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5421 | spa_load_spares(spa); |
b128c09f | 5422 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5423 | spa->spa_spares.sav_sync = B_TRUE; |
5424 | } | |
5425 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5426 | &l2cache, &nl2cache) == 0) { | |
5427 | if (spa->spa_l2cache.sav_config) | |
5428 | VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, | |
5429 | ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); | |
5430 | else | |
5431 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 5432 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5433 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
5434 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 5435 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5436 | spa_load_l2cache(spa); |
b128c09f | 5437 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5438 | spa->spa_l2cache.sav_sync = B_TRUE; |
5439 | } | |
5440 | ||
428870ff BB |
5441 | /* |
5442 | * Check for any removed devices. | |
5443 | */ | |
5444 | if (spa->spa_autoreplace) { | |
5445 | spa_aux_check_removed(&spa->spa_spares); | |
5446 | spa_aux_check_removed(&spa->spa_l2cache); | |
5447 | } | |
5448 | ||
fb5f0bc8 | 5449 | if (spa_writeable(spa)) { |
b128c09f BB |
5450 | /* |
5451 | * Update the config cache to include the newly-imported pool. | |
5452 | */ | |
45d1cae3 | 5453 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 5454 | } |
34dc7c2f | 5455 | |
34dc7c2f | 5456 | /* |
9babb374 BB |
5457 | * It's possible that the pool was expanded while it was exported. |
5458 | * We kick off an async task to handle this for us. | |
34dc7c2f | 5459 | */ |
9babb374 | 5460 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 5461 | |
d5e024cb | 5462 | spa_history_log_version(spa, "import", NULL); |
fb390aaf | 5463 | |
12fa0466 | 5464 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
fb390aaf | 5465 | |
a0bd735a | 5466 | zvol_create_minors(spa, pool, B_TRUE); |
526af785 | 5467 | |
fb390aaf HR |
5468 | mutex_exit(&spa_namespace_lock); |
5469 | ||
b128c09f BB |
5470 | return (0); |
5471 | } | |
5472 | ||
34dc7c2f BB |
5473 | nvlist_t * |
5474 | spa_tryimport(nvlist_t *tryconfig) | |
5475 | { | |
5476 | nvlist_t *config = NULL; | |
6cb8e530 | 5477 | char *poolname, *cachefile; |
34dc7c2f BB |
5478 | spa_t *spa; |
5479 | uint64_t state; | |
d164b209 | 5480 | int error; |
8a393be3 | 5481 | zpool_load_policy_t policy; |
34dc7c2f BB |
5482 | |
5483 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
5484 | return (NULL); | |
5485 | ||
5486 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
5487 | return (NULL); | |
5488 | ||
5489 | /* | |
5490 | * Create and initialize the spa structure. | |
5491 | */ | |
5492 | mutex_enter(&spa_namespace_lock); | |
428870ff | 5493 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
fb5f0bc8 | 5494 | spa_activate(spa, FREAD); |
34dc7c2f BB |
5495 | |
5496 | /* | |
8a393be3 | 5497 | * Rewind pool if a max txg was provided. |
34dc7c2f | 5498 | */ |
8a393be3 PZ |
5499 | zpool_get_load_policy(spa->spa_config, &policy); |
5500 | if (policy.zlp_txg != UINT64_MAX) { | |
5501 | spa->spa_load_max_txg = policy.zlp_txg; | |
6cb8e530 PZ |
5502 | spa->spa_extreme_rewind = B_TRUE; |
5503 | zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", | |
8a393be3 | 5504 | poolname, (longlong_t)policy.zlp_txg); |
6cb8e530 PZ |
5505 | } else { |
5506 | zfs_dbgmsg("spa_tryimport: importing %s", poolname); | |
5507 | } | |
5508 | ||
5509 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) | |
5510 | == 0) { | |
5511 | zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); | |
5512 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; | |
5513 | } else { | |
5514 | spa->spa_config_source = SPA_CONFIG_SRC_SCAN; | |
5515 | } | |
5516 | ||
5517 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); | |
34dc7c2f BB |
5518 | |
5519 | /* | |
5520 | * If 'tryconfig' was at least parsable, return the current config. | |
5521 | */ | |
5522 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 5523 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f BB |
5524 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, |
5525 | poolname) == 0); | |
5526 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
5527 | state) == 0); | |
5528 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
5529 | spa->spa_uberblock.ub_timestamp) == 0); | |
9ae529ec CS |
5530 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
5531 | spa->spa_load_info) == 0); | |
ffe9d382 BB |
5532 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, |
5533 | spa->spa_errata) == 0); | |
34dc7c2f BB |
5534 | |
5535 | /* | |
5536 | * If the bootfs property exists on this pool then we | |
5537 | * copy it out so that external consumers can tell which | |
5538 | * pools are bootable. | |
5539 | */ | |
d164b209 | 5540 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
79c76d5b | 5541 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
5542 | |
5543 | /* | |
5544 | * We have to play games with the name since the | |
5545 | * pool was opened as TRYIMPORT_NAME. | |
5546 | */ | |
b128c09f | 5547 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
5548 | spa->spa_bootfs, tmpname) == 0) { |
5549 | char *cp; | |
d1d7e268 MK |
5550 | char *dsname; |
5551 | ||
79c76d5b | 5552 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
5553 | |
5554 | cp = strchr(tmpname, '/'); | |
5555 | if (cp == NULL) { | |
5556 | (void) strlcpy(dsname, tmpname, | |
5557 | MAXPATHLEN); | |
5558 | } else { | |
5559 | (void) snprintf(dsname, MAXPATHLEN, | |
5560 | "%s/%s", poolname, ++cp); | |
5561 | } | |
5562 | VERIFY(nvlist_add_string(config, | |
5563 | ZPOOL_CONFIG_BOOTFS, dsname) == 0); | |
5564 | kmem_free(dsname, MAXPATHLEN); | |
5565 | } | |
5566 | kmem_free(tmpname, MAXPATHLEN); | |
5567 | } | |
5568 | ||
5569 | /* | |
5570 | * Add the list of hot spares and level 2 cache devices. | |
5571 | */ | |
9babb374 | 5572 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
5573 | spa_add_spares(spa, config); |
5574 | spa_add_l2cache(spa, config); | |
9babb374 | 5575 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
5576 | } |
5577 | ||
5578 | spa_unload(spa); | |
5579 | spa_deactivate(spa); | |
5580 | spa_remove(spa); | |
5581 | mutex_exit(&spa_namespace_lock); | |
5582 | ||
5583 | return (config); | |
5584 | } | |
5585 | ||
5586 | /* | |
5587 | * Pool export/destroy | |
5588 | * | |
5589 | * The act of destroying or exporting a pool is very simple. We make sure there | |
5590 | * is no more pending I/O and any references to the pool are gone. Then, we | |
5591 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
5592 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
5593 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
5594 | */ |
5595 | static int | |
b128c09f | 5596 | spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 5597 | boolean_t force, boolean_t hardforce) |
34dc7c2f BB |
5598 | { |
5599 | spa_t *spa; | |
5600 | ||
5601 | if (oldconfig) | |
5602 | *oldconfig = NULL; | |
5603 | ||
fb5f0bc8 | 5604 | if (!(spa_mode_global & FWRITE)) |
2e528b49 | 5605 | return (SET_ERROR(EROFS)); |
34dc7c2f BB |
5606 | |
5607 | mutex_enter(&spa_namespace_lock); | |
5608 | if ((spa = spa_lookup(pool)) == NULL) { | |
5609 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5610 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
5611 | } |
5612 | ||
5613 | /* | |
5614 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
5615 | * reacquire the namespace lock, and see if we can export. | |
5616 | */ | |
5617 | spa_open_ref(spa, FTAG); | |
5618 | mutex_exit(&spa_namespace_lock); | |
5619 | spa_async_suspend(spa); | |
a0bd735a BP |
5620 | if (spa->spa_zvol_taskq) { |
5621 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
5622 | taskq_wait(spa->spa_zvol_taskq); | |
5623 | } | |
34dc7c2f BB |
5624 | mutex_enter(&spa_namespace_lock); |
5625 | spa_close(spa, FTAG); | |
5626 | ||
d14cfd83 IH |
5627 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) |
5628 | goto export_spa; | |
34dc7c2f | 5629 | /* |
d14cfd83 IH |
5630 | * The pool will be in core if it's openable, in which case we can |
5631 | * modify its state. Objsets may be open only because they're dirty, | |
5632 | * so we have to force it to sync before checking spa_refcnt. | |
34dc7c2f | 5633 | */ |
0c66c32d | 5634 | if (spa->spa_sync_on) { |
34dc7c2f | 5635 | txg_wait_synced(spa->spa_dsl_pool, 0); |
0c66c32d JG |
5636 | spa_evicting_os_wait(spa); |
5637 | } | |
34dc7c2f | 5638 | |
d14cfd83 IH |
5639 | /* |
5640 | * A pool cannot be exported or destroyed if there are active | |
5641 | * references. If we are resetting a pool, allow references by | |
5642 | * fault injection handlers. | |
5643 | */ | |
5644 | if (!spa_refcount_zero(spa) || | |
5645 | (spa->spa_inject_ref != 0 && | |
5646 | new_state != POOL_STATE_UNINITIALIZED)) { | |
5647 | spa_async_resume(spa); | |
5648 | mutex_exit(&spa_namespace_lock); | |
5649 | return (SET_ERROR(EBUSY)); | |
5650 | } | |
34dc7c2f | 5651 | |
d14cfd83 | 5652 | if (spa->spa_sync_on) { |
b128c09f BB |
5653 | /* |
5654 | * A pool cannot be exported if it has an active shared spare. | |
5655 | * This is to prevent other pools stealing the active spare | |
5656 | * from an exported pool. At user's own will, such pool can | |
5657 | * be forcedly exported. | |
5658 | */ | |
5659 | if (!force && new_state == POOL_STATE_EXPORTED && | |
5660 | spa_has_active_shared_spare(spa)) { | |
5661 | spa_async_resume(spa); | |
5662 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5663 | return (SET_ERROR(EXDEV)); |
b128c09f | 5664 | } |
34dc7c2f | 5665 | |
619f0976 GW |
5666 | /* |
5667 | * We're about to export or destroy this pool. Make sure | |
5668 | * we stop all initializtion activity here before we | |
5669 | * set the spa_final_txg. This will ensure that all | |
5670 | * dirty data resulting from the initialization is | |
5671 | * committed to disk before we unload the pool. | |
5672 | */ | |
5673 | if (spa->spa_root_vdev != NULL) { | |
5674 | vdev_initialize_stop_all(spa->spa_root_vdev, | |
5675 | VDEV_INITIALIZE_ACTIVE); | |
5676 | } | |
5677 | ||
34dc7c2f BB |
5678 | /* |
5679 | * We want this to be reflected on every label, | |
5680 | * so mark them all dirty. spa_unload() will do the | |
5681 | * final sync that pushes these changes out. | |
5682 | */ | |
fb5f0bc8 | 5683 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 5684 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5685 | spa->spa_state = new_state; |
428870ff BB |
5686 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
5687 | TXG_DEFER_SIZE + 1; | |
34dc7c2f | 5688 | vdev_config_dirty(spa->spa_root_vdev); |
b128c09f | 5689 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5690 | } |
5691 | } | |
5692 | ||
d14cfd83 | 5693 | export_spa: |
d5e024cb BB |
5694 | if (new_state == POOL_STATE_DESTROYED) |
5695 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); | |
5696 | else if (new_state == POOL_STATE_EXPORTED) | |
5697 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT); | |
34dc7c2f BB |
5698 | |
5699 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
5700 | spa_unload(spa); | |
5701 | spa_deactivate(spa); | |
5702 | } | |
5703 | ||
5704 | if (oldconfig && spa->spa_config) | |
5705 | VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); | |
5706 | ||
5707 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 | 5708 | if (!hardforce) |
a1d477c2 | 5709 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 5710 | spa_remove(spa); |
34dc7c2f BB |
5711 | } |
5712 | mutex_exit(&spa_namespace_lock); | |
5713 | ||
5714 | return (0); | |
5715 | } | |
5716 | ||
5717 | /* | |
5718 | * Destroy a storage pool. | |
5719 | */ | |
5720 | int | |
5721 | spa_destroy(char *pool) | |
5722 | { | |
fb5f0bc8 BB |
5723 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
5724 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
5725 | } |
5726 | ||
5727 | /* | |
5728 | * Export a storage pool. | |
5729 | */ | |
5730 | int | |
fb5f0bc8 BB |
5731 | spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, |
5732 | boolean_t hardforce) | |
34dc7c2f | 5733 | { |
fb5f0bc8 BB |
5734 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
5735 | force, hardforce)); | |
34dc7c2f BB |
5736 | } |
5737 | ||
5738 | /* | |
5739 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
5740 | * from the namespace in any way. | |
5741 | */ | |
5742 | int | |
5743 | spa_reset(char *pool) | |
5744 | { | |
b128c09f | 5745 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 5746 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
5747 | } |
5748 | ||
34dc7c2f BB |
5749 | /* |
5750 | * ========================================================================== | |
5751 | * Device manipulation | |
5752 | * ========================================================================== | |
5753 | */ | |
5754 | ||
5755 | /* | |
5756 | * Add a device to a storage pool. | |
5757 | */ | |
5758 | int | |
5759 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
5760 | { | |
428870ff | 5761 | uint64_t txg, id; |
fb5f0bc8 | 5762 | int error; |
34dc7c2f BB |
5763 | vdev_t *rvd = spa->spa_root_vdev; |
5764 | vdev_t *vd, *tvd; | |
5765 | nvlist_t **spares, **l2cache; | |
5766 | uint_t nspares, nl2cache; | |
5767 | ||
572e2857 BB |
5768 | ASSERT(spa_writeable(spa)); |
5769 | ||
34dc7c2f BB |
5770 | txg = spa_vdev_enter(spa); |
5771 | ||
5772 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
5773 | VDEV_ALLOC_ADD)) != 0) | |
5774 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5775 | ||
b128c09f | 5776 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
5777 | |
5778 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
5779 | &nspares) != 0) | |
5780 | nspares = 0; | |
5781 | ||
5782 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
5783 | &nl2cache) != 0) | |
5784 | nl2cache = 0; | |
5785 | ||
b128c09f | 5786 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 5787 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 5788 | |
b128c09f BB |
5789 | if (vd->vdev_children != 0 && |
5790 | (error = vdev_create(vd, txg, B_FALSE)) != 0) | |
5791 | return (spa_vdev_exit(spa, vd, txg, error)); | |
34dc7c2f BB |
5792 | |
5793 | /* | |
5794 | * We must validate the spares and l2cache devices after checking the | |
5795 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
5796 | */ | |
b128c09f | 5797 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 5798 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
5799 | |
5800 | /* | |
a1d477c2 MA |
5801 | * If we are in the middle of a device removal, we can only add |
5802 | * devices which match the existing devices in the pool. | |
5803 | * If we are in the middle of a removal, or have some indirect | |
5804 | * vdevs, we can not add raidz toplevels. | |
34dc7c2f | 5805 | */ |
a1d477c2 MA |
5806 | if (spa->spa_vdev_removal != NULL || |
5807 | spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { | |
5808 | for (int c = 0; c < vd->vdev_children; c++) { | |
5809 | tvd = vd->vdev_child[c]; | |
5810 | if (spa->spa_vdev_removal != NULL && | |
9e052db4 | 5811 | tvd->vdev_ashift != spa->spa_max_ashift) { |
a1d477c2 MA |
5812 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
5813 | } | |
5814 | /* Fail if top level vdev is raidz */ | |
5815 | if (tvd->vdev_ops == &vdev_raidz_ops) { | |
5816 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); | |
5817 | } | |
5818 | /* | |
5819 | * Need the top level mirror to be | |
5820 | * a mirror of leaf vdevs only | |
5821 | */ | |
5822 | if (tvd->vdev_ops == &vdev_mirror_ops) { | |
5823 | for (uint64_t cid = 0; | |
5824 | cid < tvd->vdev_children; cid++) { | |
5825 | vdev_t *cvd = tvd->vdev_child[cid]; | |
5826 | if (!cvd->vdev_ops->vdev_op_leaf) { | |
5827 | return (spa_vdev_exit(spa, vd, | |
5828 | txg, EINVAL)); | |
5829 | } | |
5830 | } | |
5831 | } | |
5832 | } | |
5833 | } | |
5834 | ||
1c27024e | 5835 | for (int c = 0; c < vd->vdev_children; c++) { |
428870ff BB |
5836 | |
5837 | /* | |
5838 | * Set the vdev id to the first hole, if one exists. | |
5839 | */ | |
5840 | for (id = 0; id < rvd->vdev_children; id++) { | |
5841 | if (rvd->vdev_child[id]->vdev_ishole) { | |
5842 | vdev_free(rvd->vdev_child[id]); | |
5843 | break; | |
5844 | } | |
5845 | } | |
34dc7c2f BB |
5846 | tvd = vd->vdev_child[c]; |
5847 | vdev_remove_child(vd, tvd); | |
428870ff | 5848 | tvd->vdev_id = id; |
34dc7c2f BB |
5849 | vdev_add_child(rvd, tvd); |
5850 | vdev_config_dirty(tvd); | |
5851 | } | |
5852 | ||
5853 | if (nspares != 0) { | |
5854 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
5855 | ZPOOL_CONFIG_SPARES); | |
5856 | spa_load_spares(spa); | |
5857 | spa->spa_spares.sav_sync = B_TRUE; | |
5858 | } | |
5859 | ||
5860 | if (nl2cache != 0) { | |
5861 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
5862 | ZPOOL_CONFIG_L2CACHE); | |
5863 | spa_load_l2cache(spa); | |
5864 | spa->spa_l2cache.sav_sync = B_TRUE; | |
5865 | } | |
5866 | ||
5867 | /* | |
5868 | * We have to be careful when adding new vdevs to an existing pool. | |
5869 | * If other threads start allocating from these vdevs before we | |
5870 | * sync the config cache, and we lose power, then upon reboot we may | |
5871 | * fail to open the pool because there are DVAs that the config cache | |
5872 | * can't translate. Therefore, we first add the vdevs without | |
5873 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
5874 | * and then let spa_config_update() initialize the new metaslabs. | |
5875 | * | |
5876 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
5877 | * if we lose power at any point in this sequence, the remaining | |
5878 | * steps will be completed the next time we load the pool. | |
5879 | */ | |
5880 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
5881 | ||
5882 | mutex_enter(&spa_namespace_lock); | |
5883 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
12fa0466 | 5884 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); |
34dc7c2f BB |
5885 | mutex_exit(&spa_namespace_lock); |
5886 | ||
5887 | return (0); | |
5888 | } | |
5889 | ||
5890 | /* | |
5891 | * Attach a device to a mirror. The arguments are the path to any device | |
5892 | * in the mirror, and the nvroot for the new device. If the path specifies | |
5893 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
5894 | * | |
5895 | * If 'replacing' is specified, the new device is intended to replace the | |
5896 | * existing device; in this case the two devices are made into their own | |
5897 | * mirror using the 'replacing' vdev, which is functionally identical to | |
5898 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
5899 | * extra rules: you can't attach to it after it's been created, and upon | |
5900 | * completion of resilvering, the first disk (the one being replaced) | |
5901 | * is automatically detached. | |
5902 | */ | |
5903 | int | |
5904 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) | |
5905 | { | |
428870ff | 5906 | uint64_t txg, dtl_max_txg; |
1c27024e | 5907 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev); |
34dc7c2f BB |
5908 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
5909 | vdev_ops_t *pvops; | |
b128c09f BB |
5910 | char *oldvdpath, *newvdpath; |
5911 | int newvd_isspare; | |
5912 | int error; | |
34dc7c2f | 5913 | |
572e2857 BB |
5914 | ASSERT(spa_writeable(spa)); |
5915 | ||
34dc7c2f BB |
5916 | txg = spa_vdev_enter(spa); |
5917 | ||
b128c09f | 5918 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 5919 | |
d2734cce SD |
5920 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
5921 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
5922 | error = (spa_has_checkpoint(spa)) ? | |
5923 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
5924 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5925 | } | |
5926 | ||
9e052db4 | 5927 | if (spa->spa_vdev_removal != NULL) |
a1d477c2 | 5928 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
a1d477c2 | 5929 | |
34dc7c2f BB |
5930 | if (oldvd == NULL) |
5931 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
5932 | ||
5933 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
5934 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
5935 | ||
5936 | pvd = oldvd->vdev_parent; | |
5937 | ||
5938 | if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, | |
5ffb9d1d | 5939 | VDEV_ALLOC_ATTACH)) != 0) |
34dc7c2f BB |
5940 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
5941 | ||
5942 | if (newrootvd->vdev_children != 1) | |
5943 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
5944 | ||
5945 | newvd = newrootvd->vdev_child[0]; | |
5946 | ||
5947 | if (!newvd->vdev_ops->vdev_op_leaf) | |
5948 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
5949 | ||
5950 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
5951 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
5952 | ||
5953 | /* | |
5954 | * Spares can't replace logs | |
5955 | */ | |
b128c09f | 5956 | if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) |
34dc7c2f BB |
5957 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
5958 | ||
5959 | if (!replacing) { | |
5960 | /* | |
5961 | * For attach, the only allowable parent is a mirror or the root | |
5962 | * vdev. | |
5963 | */ | |
5964 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
5965 | pvd->vdev_ops != &vdev_root_ops) | |
5966 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
5967 | ||
5968 | pvops = &vdev_mirror_ops; | |
5969 | } else { | |
5970 | /* | |
5971 | * Active hot spares can only be replaced by inactive hot | |
5972 | * spares. | |
5973 | */ | |
5974 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 5975 | oldvd->vdev_isspare && |
34dc7c2f BB |
5976 | !spa_has_spare(spa, newvd->vdev_guid)) |
5977 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
5978 | ||
5979 | /* | |
5980 | * If the source is a hot spare, and the parent isn't already a | |
5981 | * spare, then we want to create a new hot spare. Otherwise, we | |
5982 | * want to create a replacing vdev. The user is not allowed to | |
5983 | * attach to a spared vdev child unless the 'isspare' state is | |
5984 | * the same (spare replaces spare, non-spare replaces | |
5985 | * non-spare). | |
5986 | */ | |
572e2857 BB |
5987 | if (pvd->vdev_ops == &vdev_replacing_ops && |
5988 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 5989 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
5990 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
5991 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 5992 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
5993 | } |
5994 | ||
5995 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
5996 | pvops = &vdev_spare_ops; |
5997 | else | |
5998 | pvops = &vdev_replacing_ops; | |
5999 | } | |
6000 | ||
6001 | /* | |
9babb374 | 6002 | * Make sure the new device is big enough. |
34dc7c2f | 6003 | */ |
9babb374 | 6004 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
6005 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
6006 | ||
6007 | /* | |
6008 | * The new device cannot have a higher alignment requirement | |
6009 | * than the top-level vdev. | |
6010 | */ | |
6011 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
6012 | return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); | |
6013 | ||
6014 | /* | |
6015 | * If this is an in-place replacement, update oldvd's path and devid | |
6016 | * to make it distinguishable from newvd, and unopenable from now on. | |
6017 | */ | |
6018 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
6019 | spa_strfree(oldvd->vdev_path); | |
6020 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
79c76d5b | 6021 | KM_SLEEP); |
34dc7c2f BB |
6022 | (void) sprintf(oldvd->vdev_path, "%s/%s", |
6023 | newvd->vdev_path, "old"); | |
6024 | if (oldvd->vdev_devid != NULL) { | |
6025 | spa_strfree(oldvd->vdev_devid); | |
6026 | oldvd->vdev_devid = NULL; | |
6027 | } | |
6028 | } | |
6029 | ||
572e2857 | 6030 | /* mark the device being resilvered */ |
5d1f7fb6 | 6031 | newvd->vdev_resilver_txg = txg; |
572e2857 | 6032 | |
34dc7c2f BB |
6033 | /* |
6034 | * If the parent is not a mirror, or if we're replacing, insert the new | |
6035 | * mirror/replacing/spare vdev above oldvd. | |
6036 | */ | |
6037 | if (pvd->vdev_ops != pvops) | |
6038 | pvd = vdev_add_parent(oldvd, pvops); | |
6039 | ||
6040 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
6041 | ASSERT(pvd->vdev_ops == pvops); | |
6042 | ASSERT(oldvd->vdev_parent == pvd); | |
6043 | ||
6044 | /* | |
6045 | * Extract the new device from its root and add it to pvd. | |
6046 | */ | |
6047 | vdev_remove_child(newrootvd, newvd); | |
6048 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 6049 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
6050 | vdev_add_child(pvd, newvd); |
6051 | ||
6d82f98c IH |
6052 | /* |
6053 | * Reevaluate the parent vdev state. | |
6054 | */ | |
6055 | vdev_propagate_state(pvd); | |
6056 | ||
34dc7c2f BB |
6057 | tvd = newvd->vdev_top; |
6058 | ASSERT(pvd->vdev_top == tvd); | |
6059 | ASSERT(tvd->vdev_parent == rvd); | |
6060 | ||
6061 | vdev_config_dirty(tvd); | |
6062 | ||
6063 | /* | |
428870ff BB |
6064 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
6065 | * for any dmu_sync-ed blocks. It will propagate upward when | |
6066 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 6067 | */ |
428870ff | 6068 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 6069 | |
428870ff BB |
6070 | vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, |
6071 | dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 6072 | |
9babb374 | 6073 | if (newvd->vdev_isspare) { |
34dc7c2f | 6074 | spa_spare_activate(newvd); |
12fa0466 | 6075 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); |
9babb374 BB |
6076 | } |
6077 | ||
b128c09f BB |
6078 | oldvdpath = spa_strdup(oldvd->vdev_path); |
6079 | newvdpath = spa_strdup(newvd->vdev_path); | |
6080 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
6081 | |
6082 | /* | |
6083 | * Mark newvd's DTL dirty in this txg. | |
6084 | */ | |
6085 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
6086 | ||
428870ff | 6087 | /* |
93cf2076 GW |
6088 | * Schedule the resilver to restart in the future. We do this to |
6089 | * ensure that dmu_sync-ed blocks have been stitched into the | |
80a91e74 TC |
6090 | * respective datasets. We do not do this if resilvers have been |
6091 | * deferred. | |
428870ff | 6092 | */ |
80a91e74 TC |
6093 | if (dsl_scan_resilvering(spa_get_dsl(spa)) && |
6094 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
6095 | vdev_set_deferred_resilver(spa, newvd); | |
6096 | else | |
6097 | dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); | |
428870ff | 6098 | |
fb390aaf | 6099 | if (spa->spa_bootfs) |
12fa0466 | 6100 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); |
fb390aaf | 6101 | |
12fa0466 | 6102 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); |
fb390aaf | 6103 | |
428870ff BB |
6104 | /* |
6105 | * Commit the config | |
6106 | */ | |
6107 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 6108 | |
6f1ffb06 | 6109 | spa_history_log_internal(spa, "vdev attach", NULL, |
428870ff | 6110 | "%s vdev=%s %s vdev=%s", |
45d1cae3 BB |
6111 | replacing && newvd_isspare ? "spare in" : |
6112 | replacing ? "replace" : "attach", newvdpath, | |
6113 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
6114 | |
6115 | spa_strfree(oldvdpath); | |
6116 | spa_strfree(newvdpath); | |
6117 | ||
34dc7c2f BB |
6118 | return (0); |
6119 | } | |
6120 | ||
6121 | /* | |
6122 | * Detach a device from a mirror or replacing vdev. | |
d3cc8b15 | 6123 | * |
34dc7c2f BB |
6124 | * If 'replace_done' is specified, only detach if the parent |
6125 | * is a replacing vdev. | |
6126 | */ | |
6127 | int | |
fb5f0bc8 | 6128 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
6129 | { |
6130 | uint64_t txg; | |
fb5f0bc8 | 6131 | int error; |
1c27024e | 6132 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev); |
34dc7c2f BB |
6133 | vdev_t *vd, *pvd, *cvd, *tvd; |
6134 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 6135 | uint64_t unspare_guid = 0; |
428870ff | 6136 | char *vdpath; |
1c27024e | 6137 | |
572e2857 BB |
6138 | ASSERT(spa_writeable(spa)); |
6139 | ||
34dc7c2f BB |
6140 | txg = spa_vdev_enter(spa); |
6141 | ||
b128c09f | 6142 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6143 | |
d2734cce SD |
6144 | /* |
6145 | * Besides being called directly from the userland through the | |
6146 | * ioctl interface, spa_vdev_detach() can be potentially called | |
6147 | * at the end of spa_vdev_resilver_done(). | |
6148 | * | |
6149 | * In the regular case, when we have a checkpoint this shouldn't | |
6150 | * happen as we never empty the DTLs of a vdev during the scrub | |
6151 | * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() | |
6152 | * should never get here when we have a checkpoint. | |
6153 | * | |
6154 | * That said, even in a case when we checkpoint the pool exactly | |
6155 | * as spa_vdev_resilver_done() calls this function everything | |
6156 | * should be fine as the resilver will return right away. | |
6157 | */ | |
6158 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
6159 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6160 | error = (spa_has_checkpoint(spa)) ? | |
6161 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6162 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6163 | } | |
6164 | ||
34dc7c2f BB |
6165 | if (vd == NULL) |
6166 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6167 | ||
6168 | if (!vd->vdev_ops->vdev_op_leaf) | |
6169 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6170 | ||
6171 | pvd = vd->vdev_parent; | |
6172 | ||
fb5f0bc8 BB |
6173 | /* |
6174 | * If the parent/child relationship is not as expected, don't do it. | |
6175 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
6176 | * vdev that's replacing B with C. The user's intent in replacing | |
6177 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
6178 | * the replace by detaching C, the expected behavior is to end up | |
6179 | * M(A,B). But suppose that right after deciding to detach C, | |
6180 | * the replacement of B completes. We would have M(A,C), and then | |
6181 | * ask to detach C, which would leave us with just A -- not what | |
6182 | * the user wanted. To prevent this, we make sure that the | |
6183 | * parent/child relationship hasn't changed -- in this example, | |
6184 | * that C's parent is still the replacing vdev R. | |
6185 | */ | |
6186 | if (pvd->vdev_guid != pguid && pguid != 0) | |
6187 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
6188 | ||
34dc7c2f | 6189 | /* |
572e2857 | 6190 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 6191 | */ |
572e2857 BB |
6192 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
6193 | pvd->vdev_ops != &vdev_spare_ops) | |
6194 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
6195 | |
6196 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
6197 | spa_version(spa) >= SPA_VERSION_SPARES); | |
6198 | ||
6199 | /* | |
6200 | * Only mirror, replacing, and spare vdevs support detach. | |
6201 | */ | |
6202 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
6203 | pvd->vdev_ops != &vdev_mirror_ops && | |
6204 | pvd->vdev_ops != &vdev_spare_ops) | |
6205 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6206 | ||
6207 | /* | |
fb5f0bc8 BB |
6208 | * If this device has the only valid copy of some data, |
6209 | * we cannot safely detach it. | |
34dc7c2f | 6210 | */ |
fb5f0bc8 | 6211 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
6212 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
6213 | ||
fb5f0bc8 | 6214 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 6215 | |
b128c09f BB |
6216 | /* |
6217 | * If we are detaching the second disk from a replacing vdev, then | |
6218 | * check to see if we changed the original vdev's path to have "/old" | |
6219 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
6220 | */ | |
572e2857 BB |
6221 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
6222 | vd->vdev_path != NULL) { | |
6223 | size_t len = strlen(vd->vdev_path); | |
6224 | ||
1c27024e | 6225 | for (int c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
6226 | cvd = pvd->vdev_child[c]; |
6227 | ||
6228 | if (cvd == vd || cvd->vdev_path == NULL) | |
6229 | continue; | |
6230 | ||
6231 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
6232 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
6233 | spa_strfree(cvd->vdev_path); | |
6234 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
6235 | break; | |
6236 | } | |
b128c09f BB |
6237 | } |
6238 | } | |
6239 | ||
34dc7c2f BB |
6240 | /* |
6241 | * If we are detaching the original disk from a spare, then it implies | |
6242 | * that the spare should become a real disk, and be removed from the | |
6243 | * active spare list for the pool. | |
6244 | */ | |
6245 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 BB |
6246 | vd->vdev_id == 0 && |
6247 | pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) | |
34dc7c2f BB |
6248 | unspare = B_TRUE; |
6249 | ||
6250 | /* | |
6251 | * Erase the disk labels so the disk can be used for other things. | |
6252 | * This must be done after all other error cases are handled, | |
6253 | * but before we disembowel vd (so we can still do I/O to it). | |
6254 | * But if we can't do it, don't treat the error as fatal -- | |
6255 | * it may be that the unwritability of the disk is the reason | |
6256 | * it's being detached! | |
6257 | */ | |
6258 | error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
6259 | ||
6260 | /* | |
6261 | * Remove vd from its parent and compact the parent's children. | |
6262 | */ | |
6263 | vdev_remove_child(pvd, vd); | |
6264 | vdev_compact_children(pvd); | |
6265 | ||
6266 | /* | |
6267 | * Remember one of the remaining children so we can get tvd below. | |
6268 | */ | |
572e2857 | 6269 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
6270 | |
6271 | /* | |
6272 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
6273 | * do it now, marking the vdev as no longer a spare in the process. |
6274 | * We must do this before vdev_remove_parent(), because that can | |
6275 | * change the GUID if it creates a new toplevel GUID. For a similar | |
6276 | * reason, we must remove the spare now, in the same txg as the detach; | |
6277 | * otherwise someone could attach a new sibling, change the GUID, and | |
6278 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
6279 | */ |
6280 | if (unspare) { | |
6281 | ASSERT(cvd->vdev_isspare); | |
6282 | spa_spare_remove(cvd); | |
6283 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 6284 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 6285 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
6286 | } |
6287 | ||
428870ff BB |
6288 | /* |
6289 | * If the parent mirror/replacing vdev only has one child, | |
6290 | * the parent is no longer needed. Remove it from the tree. | |
6291 | */ | |
572e2857 BB |
6292 | if (pvd->vdev_children == 1) { |
6293 | if (pvd->vdev_ops == &vdev_spare_ops) | |
6294 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 6295 | vdev_remove_parent(cvd); |
572e2857 BB |
6296 | } |
6297 | ||
428870ff BB |
6298 | |
6299 | /* | |
6300 | * We don't set tvd until now because the parent we just removed | |
6301 | * may have been the previous top-level vdev. | |
6302 | */ | |
6303 | tvd = cvd->vdev_top; | |
6304 | ASSERT(tvd->vdev_parent == rvd); | |
6305 | ||
6306 | /* | |
6307 | * Reevaluate the parent vdev state. | |
6308 | */ | |
6309 | vdev_propagate_state(cvd); | |
6310 | ||
6311 | /* | |
6312 | * If the 'autoexpand' property is set on the pool then automatically | |
6313 | * try to expand the size of the pool. For example if the device we | |
6314 | * just detached was smaller than the others, it may be possible to | |
6315 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
6316 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
6317 | */ | |
6318 | if (spa->spa_autoexpand) { | |
6319 | vdev_reopen(tvd); | |
6320 | vdev_expand(tvd, txg); | |
6321 | } | |
6322 | ||
6323 | vdev_config_dirty(tvd); | |
6324 | ||
6325 | /* | |
6326 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
6327 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
6328 | * But first make sure we're not on any *other* txg's DTL list, to | |
6329 | * prevent vd from being accessed after it's freed. | |
6330 | */ | |
b6ca6193 | 6331 | vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none"); |
1c27024e | 6332 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff BB |
6333 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
6334 | vd->vdev_detached = B_TRUE; | |
6335 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
6336 | ||
12fa0466 | 6337 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); |
428870ff | 6338 | |
572e2857 BB |
6339 | /* hang on to the spa before we release the lock */ |
6340 | spa_open_ref(spa, FTAG); | |
6341 | ||
428870ff BB |
6342 | error = spa_vdev_exit(spa, vd, txg, 0); |
6343 | ||
6f1ffb06 | 6344 | spa_history_log_internal(spa, "detach", NULL, |
428870ff BB |
6345 | "vdev=%s", vdpath); |
6346 | spa_strfree(vdpath); | |
6347 | ||
6348 | /* | |
6349 | * If this was the removal of the original device in a hot spare vdev, | |
6350 | * then we want to go through and remove the device from the hot spare | |
6351 | * list of every other pool. | |
6352 | */ | |
6353 | if (unspare) { | |
572e2857 BB |
6354 | spa_t *altspa = NULL; |
6355 | ||
428870ff | 6356 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
6357 | while ((altspa = spa_next(altspa)) != NULL) { |
6358 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
6359 | altspa == spa) | |
428870ff | 6360 | continue; |
572e2857 BB |
6361 | |
6362 | spa_open_ref(altspa, FTAG); | |
428870ff | 6363 | mutex_exit(&spa_namespace_lock); |
572e2857 | 6364 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 6365 | mutex_enter(&spa_namespace_lock); |
572e2857 | 6366 | spa_close(altspa, FTAG); |
428870ff BB |
6367 | } |
6368 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
6369 | |
6370 | /* search the rest of the vdevs for spares to remove */ | |
6371 | spa_vdev_resilver_done(spa); | |
428870ff BB |
6372 | } |
6373 | ||
572e2857 BB |
6374 | /* all done with the spa; OK to release */ |
6375 | mutex_enter(&spa_namespace_lock); | |
6376 | spa_close(spa, FTAG); | |
6377 | mutex_exit(&spa_namespace_lock); | |
6378 | ||
428870ff BB |
6379 | return (error); |
6380 | } | |
6381 | ||
c10d37dd GW |
6382 | static int |
6383 | spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
6384 | list_t *vd_list) | |
619f0976 | 6385 | { |
c10d37dd GW |
6386 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6387 | ||
619f0976 GW |
6388 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); |
6389 | ||
6390 | /* Look up vdev and ensure it's a leaf. */ | |
6391 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
6392 | if (vd == NULL || vd->vdev_detached) { | |
6393 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
6394 | return (SET_ERROR(ENODEV)); |
6395 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
6396 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
6397 | return (SET_ERROR(EINVAL)); |
6398 | } else if (!vdev_writeable(vd)) { | |
6399 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
6400 | return (SET_ERROR(EROFS)); |
6401 | } | |
6402 | mutex_enter(&vd->vdev_initialize_lock); | |
6403 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
6404 | ||
6405 | /* | |
6406 | * When we activate an initialize action we check to see | |
6407 | * if the vdev_initialize_thread is NULL. We do this instead | |
6408 | * of using the vdev_initialize_state since there might be | |
6409 | * a previous initialization process which has completed but | |
6410 | * the thread is not exited. | |
6411 | */ | |
6412 | if (cmd_type == POOL_INITIALIZE_DO && | |
6413 | (vd->vdev_initialize_thread != NULL || | |
6414 | vd->vdev_top->vdev_removing)) { | |
6415 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
6416 | return (SET_ERROR(EBUSY)); |
6417 | } else if (cmd_type == POOL_INITIALIZE_CANCEL && | |
6418 | (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && | |
6419 | vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { | |
6420 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
6421 | return (SET_ERROR(ESRCH)); |
6422 | } else if (cmd_type == POOL_INITIALIZE_SUSPEND && | |
6423 | vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { | |
6424 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
6425 | return (SET_ERROR(ESRCH)); |
6426 | } | |
6427 | ||
6428 | switch (cmd_type) { | |
6429 | case POOL_INITIALIZE_DO: | |
6430 | vdev_initialize(vd); | |
6431 | break; | |
6432 | case POOL_INITIALIZE_CANCEL: | |
c10d37dd | 6433 | vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); |
619f0976 GW |
6434 | break; |
6435 | case POOL_INITIALIZE_SUSPEND: | |
c10d37dd | 6436 | vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); |
619f0976 GW |
6437 | break; |
6438 | default: | |
6439 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
6440 | } | |
6441 | mutex_exit(&vd->vdev_initialize_lock); | |
6442 | ||
c10d37dd GW |
6443 | return (0); |
6444 | } | |
6445 | ||
6446 | int | |
6447 | spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, | |
6448 | nvlist_t *vdev_errlist) | |
6449 | { | |
6450 | int total_errors = 0; | |
6451 | list_t vd_list; | |
6452 | ||
6453 | list_create(&vd_list, sizeof (vdev_t), | |
6454 | offsetof(vdev_t, vdev_initialize_node)); | |
6455 | ||
6456 | /* | |
6457 | * We hold the namespace lock through the whole function | |
6458 | * to prevent any changes to the pool while we're starting or | |
6459 | * stopping initialization. The config and state locks are held so that | |
6460 | * we can properly assess the vdev state before we commit to | |
6461 | * the initializing operation. | |
6462 | */ | |
6463 | mutex_enter(&spa_namespace_lock); | |
6464 | ||
6465 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
6466 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
6467 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
6468 | ||
6469 | int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, | |
6470 | &vd_list); | |
6471 | if (error != 0) { | |
6472 | char guid_as_str[MAXNAMELEN]; | |
6473 | ||
6474 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
6475 | "%llu", (unsigned long long)vdev_guid); | |
6476 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
6477 | total_errors++; | |
6478 | } | |
6479 | } | |
6480 | ||
6481 | /* Wait for all initialize threads to stop. */ | |
6482 | vdev_initialize_stop_wait(spa, &vd_list); | |
6483 | ||
619f0976 GW |
6484 | /* Sync out the initializing state */ |
6485 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
6486 | mutex_exit(&spa_namespace_lock); | |
6487 | ||
c10d37dd | 6488 | list_destroy(&vd_list); |
619f0976 | 6489 | |
c10d37dd GW |
6490 | return (total_errors); |
6491 | } | |
619f0976 | 6492 | |
428870ff BB |
6493 | /* |
6494 | * Split a set of devices from their mirrors, and create a new pool from them. | |
6495 | */ | |
6496 | int | |
6497 | spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, | |
6498 | nvlist_t *props, boolean_t exp) | |
6499 | { | |
6500 | int error = 0; | |
6501 | uint64_t txg, *glist; | |
6502 | spa_t *newspa; | |
6503 | uint_t c, children, lastlog; | |
6504 | nvlist_t **child, *nvl, *tmp; | |
6505 | dmu_tx_t *tx; | |
6506 | char *altroot = NULL; | |
6507 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
6508 | boolean_t activate_slog; | |
6509 | ||
572e2857 | 6510 | ASSERT(spa_writeable(spa)); |
428870ff BB |
6511 | |
6512 | txg = spa_vdev_enter(spa); | |
6513 | ||
d2734cce SD |
6514 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6515 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6516 | error = (spa_has_checkpoint(spa)) ? | |
6517 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6518 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6519 | } | |
6520 | ||
428870ff BB |
6521 | /* clear the log and flush everything up to now */ |
6522 | activate_slog = spa_passivate_log(spa); | |
6523 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
a1d477c2 | 6524 | error = spa_reset_logs(spa); |
428870ff BB |
6525 | txg = spa_vdev_config_enter(spa); |
6526 | ||
6527 | if (activate_slog) | |
6528 | spa_activate_log(spa); | |
6529 | ||
6530 | if (error != 0) | |
6531 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6532 | ||
6533 | /* check new spa name before going any further */ | |
6534 | if (spa_lookup(newname) != NULL) | |
6535 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
6536 | ||
6537 | /* | |
6538 | * scan through all the children to ensure they're all mirrors | |
6539 | */ | |
6540 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
6541 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
6542 | &children) != 0) | |
6543 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
6544 | ||
6545 | /* first, check to ensure we've got the right child count */ | |
6546 | rvd = spa->spa_root_vdev; | |
6547 | lastlog = 0; | |
6548 | for (c = 0; c < rvd->vdev_children; c++) { | |
6549 | vdev_t *vd = rvd->vdev_child[c]; | |
6550 | ||
6551 | /* don't count the holes & logs as children */ | |
a1d477c2 | 6552 | if (vd->vdev_islog || !vdev_is_concrete(vd)) { |
428870ff BB |
6553 | if (lastlog == 0) |
6554 | lastlog = c; | |
6555 | continue; | |
6556 | } | |
6557 | ||
6558 | lastlog = 0; | |
6559 | } | |
6560 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
6561 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
6562 | ||
6563 | /* next, ensure no spare or cache devices are part of the split */ | |
6564 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
6565 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
6566 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
6567 | ||
79c76d5b BB |
6568 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); |
6569 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
428870ff BB |
6570 | |
6571 | /* then, loop over each vdev and validate it */ | |
6572 | for (c = 0; c < children; c++) { | |
6573 | uint64_t is_hole = 0; | |
6574 | ||
6575 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
6576 | &is_hole); | |
6577 | ||
6578 | if (is_hole != 0) { | |
6579 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
6580 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
6581 | continue; | |
6582 | } else { | |
2e528b49 | 6583 | error = SET_ERROR(EINVAL); |
428870ff BB |
6584 | break; |
6585 | } | |
6586 | } | |
6587 | ||
6588 | /* which disk is going to be split? */ | |
6589 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
6590 | &glist[c]) != 0) { | |
2e528b49 | 6591 | error = SET_ERROR(EINVAL); |
428870ff BB |
6592 | break; |
6593 | } | |
6594 | ||
6595 | /* look it up in the spa */ | |
6596 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
6597 | if (vml[c] == NULL) { | |
2e528b49 | 6598 | error = SET_ERROR(ENODEV); |
428870ff BB |
6599 | break; |
6600 | } | |
6601 | ||
6602 | /* make sure there's nothing stopping the split */ | |
6603 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
6604 | vml[c]->vdev_islog || | |
a1d477c2 | 6605 | !vdev_is_concrete(vml[c]) || |
428870ff BB |
6606 | vml[c]->vdev_isspare || |
6607 | vml[c]->vdev_isl2cache || | |
6608 | !vdev_writeable(vml[c]) || | |
6609 | vml[c]->vdev_children != 0 || | |
6610 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
6611 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
2e528b49 | 6612 | error = SET_ERROR(EINVAL); |
428870ff BB |
6613 | break; |
6614 | } | |
6615 | ||
733b5722 RS |
6616 | if (vdev_dtl_required(vml[c]) || |
6617 | vdev_resilver_needed(vml[c], NULL, NULL)) { | |
2e528b49 | 6618 | error = SET_ERROR(EBUSY); |
428870ff BB |
6619 | break; |
6620 | } | |
6621 | ||
6622 | /* we need certain info from the top level */ | |
6623 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, | |
6624 | vml[c]->vdev_top->vdev_ms_array) == 0); | |
6625 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
6626 | vml[c]->vdev_top->vdev_ms_shift) == 0); | |
6627 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
6628 | vml[c]->vdev_top->vdev_asize) == 0); | |
6629 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
6630 | vml[c]->vdev_top->vdev_ashift) == 0); | |
e0ab3ab5 JS |
6631 | |
6632 | /* transfer per-vdev ZAPs */ | |
6633 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
6634 | VERIFY0(nvlist_add_uint64(child[c], | |
6635 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
6636 | ||
6637 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
6638 | VERIFY0(nvlist_add_uint64(child[c], | |
6639 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
6640 | vml[c]->vdev_parent->vdev_top_zap)); | |
428870ff BB |
6641 | } |
6642 | ||
6643 | if (error != 0) { | |
6644 | kmem_free(vml, children * sizeof (vdev_t *)); | |
6645 | kmem_free(glist, children * sizeof (uint64_t)); | |
6646 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6647 | } | |
6648 | ||
6649 | /* stop writers from using the disks */ | |
6650 | for (c = 0; c < children; c++) { | |
6651 | if (vml[c] != NULL) | |
6652 | vml[c]->vdev_offline = B_TRUE; | |
6653 | } | |
6654 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
6655 | |
6656 | /* | |
428870ff BB |
6657 | * Temporarily record the splitting vdevs in the spa config. This |
6658 | * will disappear once the config is regenerated. | |
34dc7c2f | 6659 | */ |
79c76d5b | 6660 | VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
6661 | VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, |
6662 | glist, children) == 0); | |
6663 | kmem_free(glist, children * sizeof (uint64_t)); | |
34dc7c2f | 6664 | |
428870ff BB |
6665 | mutex_enter(&spa->spa_props_lock); |
6666 | VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, | |
6667 | nvl) == 0); | |
6668 | mutex_exit(&spa->spa_props_lock); | |
6669 | spa->spa_config_splitting = nvl; | |
6670 | vdev_config_dirty(spa->spa_root_vdev); | |
6671 | ||
6672 | /* configure and create the new pool */ | |
6673 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); | |
6674 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
6675 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); | |
6676 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
6677 | spa_version(spa)) == 0); | |
6678 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
6679 | spa->spa_config_txg) == 0); | |
6680 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
6681 | spa_generate_guid(NULL)) == 0); | |
e0ab3ab5 | 6682 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); |
428870ff BB |
6683 | (void) nvlist_lookup_string(props, |
6684 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 6685 | |
428870ff BB |
6686 | /* add the new pool to the namespace */ |
6687 | newspa = spa_add(newname, config, altroot); | |
e0ab3ab5 | 6688 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
6689 | newspa->spa_config_txg = spa->spa_config_txg; |
6690 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
6691 | ||
6692 | /* release the spa config lock, retaining the namespace lock */ | |
6693 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
6694 | ||
6695 | if (zio_injection_enabled) | |
6696 | zio_handle_panic_injection(spa, FTAG, 1); | |
6697 | ||
6698 | spa_activate(newspa, spa_mode_global); | |
6699 | spa_async_suspend(newspa); | |
6700 | ||
c10d37dd GW |
6701 | /* |
6702 | * Temporarily stop the initializing activity. We set the state to | |
6703 | * ACTIVE so that we know to resume the initializing once the split | |
6704 | * has completed. | |
6705 | */ | |
6706 | list_t vd_list; | |
6707 | list_create(&vd_list, sizeof (vdev_t), | |
6708 | offsetof(vdev_t, vdev_initialize_node)); | |
6709 | ||
619f0976 GW |
6710 | for (c = 0; c < children; c++) { |
6711 | if (vml[c] != NULL) { | |
619f0976 | 6712 | mutex_enter(&vml[c]->vdev_initialize_lock); |
c10d37dd GW |
6713 | vdev_initialize_stop(vml[c], VDEV_INITIALIZE_ACTIVE, |
6714 | &vd_list); | |
619f0976 GW |
6715 | mutex_exit(&vml[c]->vdev_initialize_lock); |
6716 | } | |
6717 | } | |
c10d37dd GW |
6718 | vdev_initialize_stop_wait(spa, &vd_list); |
6719 | list_destroy(&vd_list); | |
619f0976 | 6720 | |
6cb8e530 PZ |
6721 | newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; |
6722 | ||
428870ff | 6723 | /* create the new pool from the disks of the original pool */ |
6cb8e530 | 6724 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); |
428870ff BB |
6725 | if (error) |
6726 | goto out; | |
6727 | ||
6728 | /* if that worked, generate a real config for the new pool */ | |
6729 | if (newspa->spa_root_vdev != NULL) { | |
6730 | VERIFY(nvlist_alloc(&newspa->spa_config_splitting, | |
79c76d5b | 6731 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
6732 | VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, |
6733 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); | |
6734 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, | |
6735 | B_TRUE)); | |
9babb374 | 6736 | } |
34dc7c2f | 6737 | |
428870ff BB |
6738 | /* set the props */ |
6739 | if (props != NULL) { | |
6740 | spa_configfile_set(newspa, props, B_FALSE); | |
6741 | error = spa_prop_set(newspa, props); | |
6742 | if (error) | |
6743 | goto out; | |
6744 | } | |
34dc7c2f | 6745 | |
428870ff BB |
6746 | /* flush everything */ |
6747 | txg = spa_vdev_config_enter(newspa); | |
6748 | vdev_config_dirty(newspa->spa_root_vdev); | |
6749 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 6750 | |
428870ff BB |
6751 | if (zio_injection_enabled) |
6752 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 6753 | |
428870ff | 6754 | spa_async_resume(newspa); |
34dc7c2f | 6755 | |
428870ff BB |
6756 | /* finally, update the original pool's config */ |
6757 | txg = spa_vdev_config_enter(spa); | |
6758 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
6759 | error = dmu_tx_assign(tx, TXG_WAIT); | |
6760 | if (error != 0) | |
6761 | dmu_tx_abort(tx); | |
6762 | for (c = 0; c < children; c++) { | |
6763 | if (vml[c] != NULL) { | |
6764 | vdev_split(vml[c]); | |
6765 | if (error == 0) | |
6f1ffb06 MA |
6766 | spa_history_log_internal(spa, "detach", tx, |
6767 | "vdev=%s", vml[c]->vdev_path); | |
e0ab3ab5 | 6768 | |
428870ff | 6769 | vdev_free(vml[c]); |
34dc7c2f | 6770 | } |
34dc7c2f | 6771 | } |
e0ab3ab5 | 6772 | spa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
6773 | vdev_config_dirty(spa->spa_root_vdev); |
6774 | spa->spa_config_splitting = NULL; | |
6775 | nvlist_free(nvl); | |
6776 | if (error == 0) | |
6777 | dmu_tx_commit(tx); | |
6778 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
6779 | ||
6780 | if (zio_injection_enabled) | |
6781 | zio_handle_panic_injection(spa, FTAG, 3); | |
6782 | ||
6783 | /* split is complete; log a history record */ | |
6f1ffb06 MA |
6784 | spa_history_log_internal(newspa, "split", NULL, |
6785 | "from pool %s", spa_name(spa)); | |
428870ff BB |
6786 | |
6787 | kmem_free(vml, children * sizeof (vdev_t *)); | |
6788 | ||
6789 | /* if we're not going to mount the filesystems in userland, export */ | |
6790 | if (exp) | |
6791 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
6792 | B_FALSE, B_FALSE); | |
6793 | ||
6794 | return (error); | |
6795 | ||
6796 | out: | |
6797 | spa_unload(newspa); | |
6798 | spa_deactivate(newspa); | |
6799 | spa_remove(newspa); | |
6800 | ||
6801 | txg = spa_vdev_config_enter(spa); | |
6802 | ||
6803 | /* re-online all offlined disks */ | |
6804 | for (c = 0; c < children; c++) { | |
6805 | if (vml[c] != NULL) | |
6806 | vml[c]->vdev_offline = B_FALSE; | |
6807 | } | |
619f0976 GW |
6808 | |
6809 | /* restart initializing disks as necessary */ | |
6810 | spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); | |
6811 | ||
428870ff BB |
6812 | vdev_reopen(spa->spa_root_vdev); |
6813 | ||
6814 | nvlist_free(spa->spa_config_splitting); | |
6815 | spa->spa_config_splitting = NULL; | |
6816 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 6817 | |
428870ff | 6818 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
6819 | return (error); |
6820 | } | |
6821 | ||
34dc7c2f BB |
6822 | /* |
6823 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
d3cc8b15 | 6824 | * currently spared, so we can detach it. |
34dc7c2f BB |
6825 | */ |
6826 | static vdev_t * | |
6827 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
6828 | { | |
6829 | vdev_t *newvd, *oldvd; | |
34dc7c2f | 6830 | |
1c27024e | 6831 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
6832 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
6833 | if (oldvd != NULL) | |
6834 | return (oldvd); | |
6835 | } | |
6836 | ||
6837 | /* | |
572e2857 BB |
6838 | * Check for a completed replacement. We always consider the first |
6839 | * vdev in the list to be the oldest vdev, and the last one to be | |
6840 | * the newest (see spa_vdev_attach() for how that works). In | |
6841 | * the case where the newest vdev is faulted, we will not automatically | |
6842 | * remove it after a resilver completes. This is OK as it will require | |
6843 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 6844 | */ |
572e2857 BB |
6845 | if (vd->vdev_ops == &vdev_replacing_ops) { |
6846 | ASSERT(vd->vdev_children > 1); | |
6847 | ||
6848 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 6849 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 6850 | |
fb5f0bc8 | 6851 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 6852 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 6853 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 6854 | return (oldvd); |
34dc7c2f BB |
6855 | } |
6856 | ||
6857 | /* | |
6858 | * Check for a completed resilver with the 'unspare' flag set. | |
f65fbee1 | 6859 | * Also potentially update faulted state. |
34dc7c2f | 6860 | */ |
572e2857 BB |
6861 | if (vd->vdev_ops == &vdev_spare_ops) { |
6862 | vdev_t *first = vd->vdev_child[0]; | |
6863 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
6864 | ||
6865 | if (last->vdev_unspare) { | |
6866 | oldvd = first; | |
6867 | newvd = last; | |
6868 | } else if (first->vdev_unspare) { | |
6869 | oldvd = last; | |
6870 | newvd = first; | |
6871 | } else { | |
6872 | oldvd = NULL; | |
6873 | } | |
34dc7c2f | 6874 | |
572e2857 | 6875 | if (oldvd != NULL && |
fb5f0bc8 | 6876 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 6877 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 6878 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 6879 | return (oldvd); |
572e2857 | 6880 | |
f65fbee1 JJ |
6881 | vdev_propagate_state(vd); |
6882 | ||
572e2857 BB |
6883 | /* |
6884 | * If there are more than two spares attached to a disk, | |
6885 | * and those spares are not required, then we want to | |
6886 | * attempt to free them up now so that they can be used | |
6887 | * by other pools. Once we're back down to a single | |
6888 | * disk+spare, we stop removing them. | |
6889 | */ | |
6890 | if (vd->vdev_children > 2) { | |
6891 | newvd = vd->vdev_child[1]; | |
6892 | ||
6893 | if (newvd->vdev_isspare && last->vdev_isspare && | |
6894 | vdev_dtl_empty(last, DTL_MISSING) && | |
6895 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
6896 | !vdev_dtl_required(newvd)) | |
6897 | return (newvd); | |
34dc7c2f | 6898 | } |
34dc7c2f BB |
6899 | } |
6900 | ||
6901 | return (NULL); | |
6902 | } | |
6903 | ||
6904 | static void | |
6905 | spa_vdev_resilver_done(spa_t *spa) | |
6906 | { | |
fb5f0bc8 BB |
6907 | vdev_t *vd, *pvd, *ppvd; |
6908 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 6909 | |
fb5f0bc8 | 6910 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
6911 | |
6912 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
6913 | pvd = vd->vdev_parent; |
6914 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 6915 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
6916 | pguid = pvd->vdev_guid; |
6917 | ppguid = ppvd->vdev_guid; | |
6918 | sguid = 0; | |
34dc7c2f BB |
6919 | /* |
6920 | * If we have just finished replacing a hot spared device, then | |
6921 | * we need to detach the parent's first child (the original hot | |
6922 | * spare) as well. | |
6923 | */ | |
572e2857 BB |
6924 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
6925 | ppvd->vdev_children == 2) { | |
34dc7c2f | 6926 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 6927 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 6928 | } |
5d1f7fb6 GW |
6929 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); |
6930 | ||
fb5f0bc8 BB |
6931 | spa_config_exit(spa, SCL_ALL, FTAG); |
6932 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 6933 | return; |
fb5f0bc8 | 6934 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 6935 | return; |
fb5f0bc8 | 6936 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
6937 | } |
6938 | ||
fb5f0bc8 | 6939 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6940 | } |
6941 | ||
6942 | /* | |
428870ff | 6943 | * Update the stored path or FRU for this vdev. |
34dc7c2f BB |
6944 | */ |
6945 | int | |
9babb374 BB |
6946 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
6947 | boolean_t ispath) | |
34dc7c2f | 6948 | { |
b128c09f | 6949 | vdev_t *vd; |
428870ff | 6950 | boolean_t sync = B_FALSE; |
34dc7c2f | 6951 | |
572e2857 BB |
6952 | ASSERT(spa_writeable(spa)); |
6953 | ||
428870ff | 6954 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 6955 | |
9babb374 | 6956 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 6957 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
6958 | |
6959 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 6960 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 6961 | |
9babb374 | 6962 | if (ispath) { |
428870ff BB |
6963 | if (strcmp(value, vd->vdev_path) != 0) { |
6964 | spa_strfree(vd->vdev_path); | |
6965 | vd->vdev_path = spa_strdup(value); | |
6966 | sync = B_TRUE; | |
6967 | } | |
9babb374 | 6968 | } else { |
428870ff BB |
6969 | if (vd->vdev_fru == NULL) { |
6970 | vd->vdev_fru = spa_strdup(value); | |
6971 | sync = B_TRUE; | |
6972 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 6973 | spa_strfree(vd->vdev_fru); |
428870ff BB |
6974 | vd->vdev_fru = spa_strdup(value); |
6975 | sync = B_TRUE; | |
6976 | } | |
9babb374 | 6977 | } |
34dc7c2f | 6978 | |
428870ff | 6979 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
6980 | } |
6981 | ||
9babb374 BB |
6982 | int |
6983 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
6984 | { | |
6985 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
6986 | } | |
6987 | ||
6988 | int | |
6989 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
6990 | { | |
6991 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
6992 | } | |
6993 | ||
34dc7c2f BB |
6994 | /* |
6995 | * ========================================================================== | |
428870ff | 6996 | * SPA Scanning |
34dc7c2f BB |
6997 | * ========================================================================== |
6998 | */ | |
0ea05c64 AP |
6999 | int |
7000 | spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) | |
7001 | { | |
7002 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7003 | ||
7004 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
7005 | return (SET_ERROR(EBUSY)); | |
7006 | ||
7007 | return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); | |
7008 | } | |
34dc7c2f | 7009 | |
34dc7c2f | 7010 | int |
428870ff BB |
7011 | spa_scan_stop(spa_t *spa) |
7012 | { | |
7013 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7014 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
2e528b49 | 7015 | return (SET_ERROR(EBUSY)); |
428870ff BB |
7016 | return (dsl_scan_cancel(spa->spa_dsl_pool)); |
7017 | } | |
7018 | ||
7019 | int | |
7020 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 7021 | { |
b128c09f | 7022 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 7023 | |
428870ff | 7024 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
2e528b49 | 7025 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 7026 | |
34dc7c2f | 7027 | /* |
b128c09f BB |
7028 | * If a resilver was requested, but there is no DTL on a |
7029 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 7030 | */ |
428870ff | 7031 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
7032 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
7033 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
7034 | return (0); |
7035 | } | |
7036 | ||
428870ff | 7037 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
7038 | } |
7039 | ||
7040 | /* | |
7041 | * ========================================================================== | |
7042 | * SPA async task processing | |
7043 | * ========================================================================== | |
7044 | */ | |
7045 | ||
7046 | static void | |
7047 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
7048 | { | |
b128c09f | 7049 | if (vd->vdev_remove_wanted) { |
428870ff BB |
7050 | vd->vdev_remove_wanted = B_FALSE; |
7051 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 7052 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
7053 | |
7054 | /* | |
7055 | * We want to clear the stats, but we don't want to do a full | |
7056 | * vdev_clear() as that will cause us to throw away | |
7057 | * degraded/faulted state as well as attempt to reopen the | |
7058 | * device, all of which is a waste. | |
7059 | */ | |
7060 | vd->vdev_stat.vs_read_errors = 0; | |
7061 | vd->vdev_stat.vs_write_errors = 0; | |
7062 | vd->vdev_stat.vs_checksum_errors = 0; | |
7063 | ||
b128c09f BB |
7064 | vdev_state_dirty(vd->vdev_top); |
7065 | } | |
34dc7c2f | 7066 | |
1c27024e | 7067 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
7068 | spa_async_remove(spa, vd->vdev_child[c]); |
7069 | } | |
7070 | ||
7071 | static void | |
7072 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
7073 | { | |
7074 | if (vd->vdev_probe_wanted) { | |
428870ff | 7075 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 7076 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 7077 | } |
b128c09f | 7078 | |
1c27024e | 7079 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 7080 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
7081 | } |
7082 | ||
9babb374 BB |
7083 | static void |
7084 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
7085 | { | |
9babb374 BB |
7086 | if (!spa->spa_autoexpand) |
7087 | return; | |
7088 | ||
1c27024e | 7089 | for (int c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
7090 | vdev_t *cvd = vd->vdev_child[c]; |
7091 | spa_async_autoexpand(spa, cvd); | |
7092 | } | |
7093 | ||
7094 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
7095 | return; | |
7096 | ||
12fa0466 | 7097 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND); |
9babb374 BB |
7098 | } |
7099 | ||
34dc7c2f | 7100 | static void |
c25b8f99 | 7101 | spa_async_thread(void *arg) |
34dc7c2f | 7102 | { |
c25b8f99 | 7103 | spa_t *spa = (spa_t *)arg; |
80a91e74 | 7104 | dsl_pool_t *dp = spa->spa_dsl_pool; |
867959b5 | 7105 | int tasks; |
34dc7c2f BB |
7106 | |
7107 | ASSERT(spa->spa_sync_on); | |
7108 | ||
7109 | mutex_enter(&spa->spa_async_lock); | |
7110 | tasks = spa->spa_async_tasks; | |
7111 | spa->spa_async_tasks = 0; | |
7112 | mutex_exit(&spa->spa_async_lock); | |
7113 | ||
7114 | /* | |
7115 | * See if the config needs to be updated. | |
7116 | */ | |
7117 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 7118 | uint64_t old_space, new_space; |
9babb374 | 7119 | |
34dc7c2f | 7120 | mutex_enter(&spa_namespace_lock); |
428870ff | 7121 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7122 | old_space += metaslab_class_get_space(spa_special_class(spa)); |
7123 | old_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
7124 | ||
34dc7c2f | 7125 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
cc99f275 | 7126 | |
428870ff | 7127 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7128 | new_space += metaslab_class_get_space(spa_special_class(spa)); |
7129 | new_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
34dc7c2f | 7130 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
7131 | |
7132 | /* | |
7133 | * If the pool grew as a result of the config update, | |
7134 | * then log an internal history event. | |
7135 | */ | |
428870ff | 7136 | if (new_space != old_space) { |
6f1ffb06 | 7137 | spa_history_log_internal(spa, "vdev online", NULL, |
45d1cae3 | 7138 | "pool '%s' size: %llu(+%llu)", |
428870ff | 7139 | spa_name(spa), new_space, new_space - old_space); |
9babb374 | 7140 | } |
34dc7c2f BB |
7141 | } |
7142 | ||
7143 | /* | |
7144 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 7145 | */ |
b128c09f | 7146 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 7147 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 7148 | spa_async_remove(spa, spa->spa_root_vdev); |
867959b5 | 7149 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 7150 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
867959b5 | 7151 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
7152 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
7153 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
7154 | } |
7155 | ||
9babb374 BB |
7156 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
7157 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7158 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
7159 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7160 | } | |
7161 | ||
34dc7c2f | 7162 | /* |
b128c09f | 7163 | * See if any devices need to be probed. |
34dc7c2f | 7164 | */ |
b128c09f | 7165 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 7166 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
7167 | spa_async_probe(spa, spa->spa_root_vdev); |
7168 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
7169 | } | |
34dc7c2f BB |
7170 | |
7171 | /* | |
b128c09f | 7172 | * If any devices are done replacing, detach them. |
34dc7c2f | 7173 | */ |
b128c09f BB |
7174 | if (tasks & SPA_ASYNC_RESILVER_DONE) |
7175 | spa_vdev_resilver_done(spa); | |
34dc7c2f BB |
7176 | |
7177 | /* | |
7178 | * Kick off a resilver. | |
7179 | */ | |
80a91e74 TC |
7180 | if (tasks & SPA_ASYNC_RESILVER && |
7181 | (!dsl_scan_resilvering(dp) || | |
7182 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) | |
7183 | dsl_resilver_restart(dp, 0); | |
34dc7c2f | 7184 | |
619f0976 GW |
7185 | if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { |
7186 | mutex_enter(&spa_namespace_lock); | |
7187 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7188 | vdev_initialize_restart(spa->spa_root_vdev); | |
7189 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7190 | mutex_exit(&spa_namespace_lock); | |
7191 | } | |
7192 | ||
34dc7c2f BB |
7193 | /* |
7194 | * Let the world know that we're done. | |
7195 | */ | |
7196 | mutex_enter(&spa->spa_async_lock); | |
7197 | spa->spa_async_thread = NULL; | |
7198 | cv_broadcast(&spa->spa_async_cv); | |
7199 | mutex_exit(&spa->spa_async_lock); | |
7200 | thread_exit(); | |
7201 | } | |
7202 | ||
7203 | void | |
7204 | spa_async_suspend(spa_t *spa) | |
7205 | { | |
7206 | mutex_enter(&spa->spa_async_lock); | |
7207 | spa->spa_async_suspended++; | |
9d5b5245 | 7208 | while (spa->spa_async_thread != NULL) |
34dc7c2f BB |
7209 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); |
7210 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 MA |
7211 | |
7212 | spa_vdev_remove_suspend(spa); | |
9d5b5245 SD |
7213 | |
7214 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a SD |
7215 | if (condense_thread != NULL) |
7216 | zthr_cancel(condense_thread); | |
d2734cce SD |
7217 | |
7218 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a SD |
7219 | if (discard_thread != NULL) |
7220 | zthr_cancel(discard_thread); | |
34dc7c2f BB |
7221 | } |
7222 | ||
7223 | void | |
7224 | spa_async_resume(spa_t *spa) | |
7225 | { | |
7226 | mutex_enter(&spa->spa_async_lock); | |
7227 | ASSERT(spa->spa_async_suspended != 0); | |
7228 | spa->spa_async_suspended--; | |
7229 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 | 7230 | spa_restart_removal(spa); |
9d5b5245 SD |
7231 | |
7232 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a | 7233 | if (condense_thread != NULL) |
9d5b5245 | 7234 | zthr_resume(condense_thread); |
d2734cce SD |
7235 | |
7236 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a | 7237 | if (discard_thread != NULL) |
d2734cce | 7238 | zthr_resume(discard_thread); |
34dc7c2f BB |
7239 | } |
7240 | ||
e6cfd633 WA |
7241 | static boolean_t |
7242 | spa_async_tasks_pending(spa_t *spa) | |
7243 | { | |
7244 | uint_t non_config_tasks; | |
7245 | uint_t config_task; | |
7246 | boolean_t config_task_suspended; | |
7247 | ||
7248 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
7249 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
7250 | if (spa->spa_ccw_fail_time == 0) { | |
7251 | config_task_suspended = B_FALSE; | |
7252 | } else { | |
7253 | config_task_suspended = | |
7254 | (gethrtime() - spa->spa_ccw_fail_time) < | |
05852b34 | 7255 | ((hrtime_t)zfs_ccw_retry_interval * NANOSEC); |
e6cfd633 WA |
7256 | } |
7257 | ||
7258 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
7259 | } | |
7260 | ||
34dc7c2f BB |
7261 | static void |
7262 | spa_async_dispatch(spa_t *spa) | |
7263 | { | |
7264 | mutex_enter(&spa->spa_async_lock); | |
e6cfd633 WA |
7265 | if (spa_async_tasks_pending(spa) && |
7266 | !spa->spa_async_suspended && | |
34dc7c2f | 7267 | spa->spa_async_thread == NULL && |
e6cfd633 | 7268 | rootdir != NULL) |
34dc7c2f BB |
7269 | spa->spa_async_thread = thread_create(NULL, 0, |
7270 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
7271 | mutex_exit(&spa->spa_async_lock); | |
7272 | } | |
7273 | ||
7274 | void | |
7275 | spa_async_request(spa_t *spa, int task) | |
7276 | { | |
428870ff | 7277 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
7278 | mutex_enter(&spa->spa_async_lock); |
7279 | spa->spa_async_tasks |= task; | |
7280 | mutex_exit(&spa->spa_async_lock); | |
7281 | } | |
7282 | ||
7283 | /* | |
7284 | * ========================================================================== | |
7285 | * SPA syncing routines | |
7286 | * ========================================================================== | |
7287 | */ | |
7288 | ||
428870ff BB |
7289 | static int |
7290 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
34dc7c2f | 7291 | { |
428870ff BB |
7292 | bpobj_t *bpo = arg; |
7293 | bpobj_enqueue(bpo, bp, tx); | |
7294 | return (0); | |
7295 | } | |
34dc7c2f | 7296 | |
428870ff BB |
7297 | static int |
7298 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
7299 | { | |
7300 | zio_t *zio = arg; | |
34dc7c2f | 7301 | |
428870ff BB |
7302 | zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, |
7303 | zio->io_flags)); | |
7304 | return (0); | |
34dc7c2f BB |
7305 | } |
7306 | ||
e8b96c60 MA |
7307 | /* |
7308 | * Note: this simple function is not inlined to make it easier to dtrace the | |
7309 | * amount of time spent syncing frees. | |
7310 | */ | |
7311 | static void | |
7312 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
7313 | { | |
7314 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
7315 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
7316 | VERIFY(zio_wait(zio) == 0); | |
7317 | } | |
7318 | ||
7319 | /* | |
7320 | * Note: this simple function is not inlined to make it easier to dtrace the | |
7321 | * amount of time spent syncing deferred frees. | |
7322 | */ | |
7323 | static void | |
7324 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
7325 | { | |
8dc2197b SD |
7326 | if (spa_sync_pass(spa) != 1) |
7327 | return; | |
7328 | ||
e8b96c60 MA |
7329 | zio_t *zio = zio_root(spa, NULL, NULL, 0); |
7330 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
7331 | spa_free_sync_cb, zio, tx), ==, 0); | |
7332 | VERIFY0(zio_wait(zio)); | |
7333 | } | |
7334 | ||
34dc7c2f BB |
7335 | static void |
7336 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
7337 | { | |
7338 | char *packed = NULL; | |
b128c09f | 7339 | size_t bufsize; |
34dc7c2f BB |
7340 | size_t nvsize = 0; |
7341 | dmu_buf_t *db; | |
7342 | ||
7343 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
7344 | ||
b128c09f BB |
7345 | /* |
7346 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
b0bc7a84 | 7347 | * information. This avoids the dmu_buf_will_dirty() path and |
b128c09f BB |
7348 | * saves us a pre-read to get data we don't actually care about. |
7349 | */ | |
9ae529ec | 7350 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
79c76d5b | 7351 | packed = vmem_alloc(bufsize, KM_SLEEP); |
34dc7c2f BB |
7352 | |
7353 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
79c76d5b | 7354 | KM_SLEEP) == 0); |
b128c09f | 7355 | bzero(packed + nvsize, bufsize - nvsize); |
34dc7c2f | 7356 | |
b128c09f | 7357 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 7358 | |
00b46022 | 7359 | vmem_free(packed, bufsize); |
34dc7c2f BB |
7360 | |
7361 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
7362 | dmu_buf_will_dirty(db, tx); | |
7363 | *(uint64_t *)db->db_data = nvsize; | |
7364 | dmu_buf_rele(db, FTAG); | |
7365 | } | |
7366 | ||
7367 | static void | |
7368 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
7369 | const char *config, const char *entry) | |
7370 | { | |
7371 | nvlist_t *nvroot; | |
7372 | nvlist_t **list; | |
7373 | int i; | |
7374 | ||
7375 | if (!sav->sav_sync) | |
7376 | return; | |
7377 | ||
7378 | /* | |
7379 | * Update the MOS nvlist describing the list of available devices. | |
7380 | * spa_validate_aux() will have already made sure this nvlist is | |
7381 | * valid and the vdevs are labeled appropriately. | |
7382 | */ | |
7383 | if (sav->sav_object == 0) { | |
7384 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
7385 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
7386 | sizeof (uint64_t), tx); | |
7387 | VERIFY(zap_update(spa->spa_meta_objset, | |
7388 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
7389 | &sav->sav_object, tx) == 0); | |
7390 | } | |
7391 | ||
79c76d5b | 7392 | VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
7393 | if (sav->sav_count == 0) { |
7394 | VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); | |
7395 | } else { | |
79c76d5b | 7396 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); |
34dc7c2f BB |
7397 | for (i = 0; i < sav->sav_count; i++) |
7398 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 7399 | B_FALSE, VDEV_CONFIG_L2CACHE); |
34dc7c2f BB |
7400 | VERIFY(nvlist_add_nvlist_array(nvroot, config, list, |
7401 | sav->sav_count) == 0); | |
7402 | for (i = 0; i < sav->sav_count; i++) | |
7403 | nvlist_free(list[i]); | |
7404 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
7405 | } | |
7406 | ||
7407 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
7408 | nvlist_free(nvroot); | |
7409 | ||
7410 | sav->sav_sync = B_FALSE; | |
7411 | } | |
7412 | ||
e0ab3ab5 JS |
7413 | /* |
7414 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
7415 | * The all-vdev ZAP must be empty. | |
7416 | */ | |
7417 | static void | |
7418 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
7419 | { | |
7420 | spa_t *spa = vd->vdev_spa; | |
e0ab3ab5 JS |
7421 | |
7422 | if (vd->vdev_top_zap != 0) { | |
7423 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
7424 | vd->vdev_top_zap, tx)); | |
7425 | } | |
7426 | if (vd->vdev_leaf_zap != 0) { | |
7427 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
7428 | vd->vdev_leaf_zap, tx)); | |
7429 | } | |
1c27024e | 7430 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
7431 | spa_avz_build(vd->vdev_child[i], avz, tx); |
7432 | } | |
7433 | } | |
7434 | ||
34dc7c2f BB |
7435 | static void |
7436 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
7437 | { | |
7438 | nvlist_t *config; | |
7439 | ||
e0ab3ab5 JS |
7440 | /* |
7441 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
7442 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
7443 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
7444 | * need to rebuild the AVZ although the config may not be dirty. | |
7445 | */ | |
7446 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
7447 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
34dc7c2f BB |
7448 | return; |
7449 | ||
b128c09f BB |
7450 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
7451 | ||
e0ab3ab5 | 7452 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || |
38640550 | 7453 | spa->spa_avz_action == AVZ_ACTION_INITIALIZE || |
e0ab3ab5 JS |
7454 | spa->spa_all_vdev_zaps != 0); |
7455 | ||
7456 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
e0ab3ab5 JS |
7457 | /* Make and build the new AVZ */ |
7458 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
7459 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
7460 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
7461 | ||
7462 | /* Diff old AVZ with new one */ | |
1c27024e DB |
7463 | zap_cursor_t zc; |
7464 | zap_attribute_t za; | |
7465 | ||
e0ab3ab5 JS |
7466 | for (zap_cursor_init(&zc, spa->spa_meta_objset, |
7467 | spa->spa_all_vdev_zaps); | |
7468 | zap_cursor_retrieve(&zc, &za) == 0; | |
7469 | zap_cursor_advance(&zc)) { | |
7470 | uint64_t vdzap = za.za_first_integer; | |
7471 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
7472 | vdzap) == ENOENT) { | |
7473 | /* | |
7474 | * ZAP is listed in old AVZ but not in new one; | |
7475 | * destroy it | |
7476 | */ | |
7477 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
7478 | tx)); | |
7479 | } | |
7480 | } | |
7481 | ||
7482 | zap_cursor_fini(&zc); | |
7483 | ||
7484 | /* Destroy the old AVZ */ | |
7485 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
7486 | spa->spa_all_vdev_zaps, tx)); | |
7487 | ||
7488 | /* Replace the old AVZ in the dir obj with the new one */ | |
7489 | VERIFY0(zap_update(spa->spa_meta_objset, | |
7490 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
7491 | sizeof (new_avz), 1, &new_avz, tx)); | |
7492 | ||
7493 | spa->spa_all_vdev_zaps = new_avz; | |
7494 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
7495 | zap_cursor_t zc; | |
7496 | zap_attribute_t za; | |
7497 | ||
7498 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
7499 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
7500 | spa->spa_all_vdev_zaps); | |
7501 | zap_cursor_retrieve(&zc, &za) == 0; | |
7502 | zap_cursor_advance(&zc)) { | |
7503 | uint64_t zap = za.za_first_integer; | |
7504 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
7505 | } | |
7506 | ||
7507 | zap_cursor_fini(&zc); | |
7508 | ||
7509 | /* Destroy and unlink the AVZ itself */ | |
7510 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
7511 | spa->spa_all_vdev_zaps, tx)); | |
7512 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
7513 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
7514 | spa->spa_all_vdev_zaps = 0; | |
7515 | } | |
7516 | ||
7517 | if (spa->spa_all_vdev_zaps == 0) { | |
7518 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
7519 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
7520 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
7521 | } | |
7522 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
7523 | ||
7524 | /* Create ZAPs for vdevs that don't have them. */ | |
7525 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
7526 | ||
b128c09f BB |
7527 | config = spa_config_generate(spa, spa->spa_root_vdev, |
7528 | dmu_tx_get_txg(tx), B_FALSE); | |
7529 | ||
ea0b2538 GW |
7530 | /* |
7531 | * If we're upgrading the spa version then make sure that | |
7532 | * the config object gets updated with the correct version. | |
7533 | */ | |
7534 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
7535 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
7536 | spa->spa_uberblock.ub_version); | |
7537 | ||
b128c09f | 7538 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f | 7539 | |
8a5fc748 | 7540 | nvlist_free(spa->spa_config_syncing); |
34dc7c2f BB |
7541 | spa->spa_config_syncing = config; |
7542 | ||
7543 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
7544 | } | |
7545 | ||
9ae529ec | 7546 | static void |
13fe0198 | 7547 | spa_sync_version(void *arg, dmu_tx_t *tx) |
9ae529ec | 7548 | { |
13fe0198 MA |
7549 | uint64_t *versionp = arg; |
7550 | uint64_t version = *versionp; | |
7551 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
9ae529ec CS |
7552 | |
7553 | /* | |
7554 | * Setting the version is special cased when first creating the pool. | |
7555 | */ | |
7556 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
7557 | ||
8dca0a9a | 7558 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
7559 | ASSERT(version >= spa_version(spa)); |
7560 | ||
7561 | spa->spa_uberblock.ub_version = version; | |
7562 | vdev_config_dirty(spa->spa_root_vdev); | |
6f1ffb06 | 7563 | spa_history_log_internal(spa, "set", tx, "version=%lld", version); |
9ae529ec CS |
7564 | } |
7565 | ||
34dc7c2f BB |
7566 | /* |
7567 | * Set zpool properties. | |
7568 | */ | |
7569 | static void | |
13fe0198 | 7570 | spa_sync_props(void *arg, dmu_tx_t *tx) |
34dc7c2f | 7571 | { |
13fe0198 MA |
7572 | nvlist_t *nvp = arg; |
7573 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f | 7574 | objset_t *mos = spa->spa_meta_objset; |
9ae529ec | 7575 | nvpair_t *elem = NULL; |
b128c09f BB |
7576 | |
7577 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 7578 | |
34dc7c2f | 7579 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec CS |
7580 | uint64_t intval; |
7581 | char *strval, *fname; | |
7582 | zpool_prop_t prop; | |
7583 | const char *propname; | |
7584 | zprop_type_t proptype; | |
fa86b5db | 7585 | spa_feature_t fid; |
9ae529ec | 7586 | |
31864e3d BB |
7587 | switch (prop = zpool_name_to_prop(nvpair_name(elem))) { |
7588 | case ZPOOL_PROP_INVAL: | |
9ae529ec CS |
7589 | /* |
7590 | * We checked this earlier in spa_prop_validate(). | |
7591 | */ | |
7592 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
7593 | ||
7594 | fname = strchr(nvpair_name(elem), '@') + 1; | |
fa86b5db | 7595 | VERIFY0(zfeature_lookup_name(fname, &fid)); |
9ae529ec | 7596 | |
fa86b5db | 7597 | spa_feature_enable(spa, fid, tx); |
6f1ffb06 MA |
7598 | spa_history_log_internal(spa, "set", tx, |
7599 | "%s=enabled", nvpair_name(elem)); | |
9ae529ec CS |
7600 | break; |
7601 | ||
34dc7c2f | 7602 | case ZPOOL_PROP_VERSION: |
93cf2076 | 7603 | intval = fnvpair_value_uint64(elem); |
34dc7c2f | 7604 | /* |
4e33ba4c | 7605 | * The version is synced separately before other |
9ae529ec | 7606 | * properties and should be correct by now. |
34dc7c2f | 7607 | */ |
9ae529ec | 7608 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
7609 | break; |
7610 | ||
7611 | case ZPOOL_PROP_ALTROOT: | |
7612 | /* | |
7613 | * 'altroot' is a non-persistent property. It should | |
7614 | * have been set temporarily at creation or import time. | |
7615 | */ | |
7616 | ASSERT(spa->spa_root != NULL); | |
7617 | break; | |
7618 | ||
572e2857 | 7619 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
7620 | case ZPOOL_PROP_CACHEFILE: |
7621 | /* | |
572e2857 BB |
7622 | * 'readonly' and 'cachefile' are also non-persisitent |
7623 | * properties. | |
34dc7c2f | 7624 | */ |
34dc7c2f | 7625 | break; |
d96eb2b1 | 7626 | case ZPOOL_PROP_COMMENT: |
93cf2076 | 7627 | strval = fnvpair_value_string(elem); |
d96eb2b1 DM |
7628 | if (spa->spa_comment != NULL) |
7629 | spa_strfree(spa->spa_comment); | |
7630 | spa->spa_comment = spa_strdup(strval); | |
7631 | /* | |
7632 | * We need to dirty the configuration on all the vdevs | |
7633 | * so that their labels get updated. It's unnecessary | |
7634 | * to do this for pool creation since the vdev's | |
4e33ba4c | 7635 | * configuration has already been dirtied. |
d96eb2b1 DM |
7636 | */ |
7637 | if (tx->tx_txg != TXG_INITIAL) | |
7638 | vdev_config_dirty(spa->spa_root_vdev); | |
6f1ffb06 MA |
7639 | spa_history_log_internal(spa, "set", tx, |
7640 | "%s=%s", nvpair_name(elem), strval); | |
d96eb2b1 | 7641 | break; |
34dc7c2f BB |
7642 | default: |
7643 | /* | |
7644 | * Set pool property values in the poolprops mos object. | |
7645 | */ | |
34dc7c2f | 7646 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
7647 | spa->spa_pool_props_object = |
7648 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 7649 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 7650 | tx); |
34dc7c2f | 7651 | } |
34dc7c2f BB |
7652 | |
7653 | /* normalize the property name */ | |
7654 | propname = zpool_prop_to_name(prop); | |
7655 | proptype = zpool_prop_get_type(prop); | |
7656 | ||
7657 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
7658 | ASSERT(proptype == PROP_TYPE_STRING); | |
93cf2076 GW |
7659 | strval = fnvpair_value_string(elem); |
7660 | VERIFY0(zap_update(mos, | |
34dc7c2f | 7661 | spa->spa_pool_props_object, propname, |
93cf2076 | 7662 | 1, strlen(strval) + 1, strval, tx)); |
6f1ffb06 MA |
7663 | spa_history_log_internal(spa, "set", tx, |
7664 | "%s=%s", nvpair_name(elem), strval); | |
34dc7c2f | 7665 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { |
93cf2076 | 7666 | intval = fnvpair_value_uint64(elem); |
34dc7c2f BB |
7667 | |
7668 | if (proptype == PROP_TYPE_INDEX) { | |
7669 | const char *unused; | |
93cf2076 GW |
7670 | VERIFY0(zpool_prop_index_to_string( |
7671 | prop, intval, &unused)); | |
34dc7c2f | 7672 | } |
93cf2076 | 7673 | VERIFY0(zap_update(mos, |
34dc7c2f | 7674 | spa->spa_pool_props_object, propname, |
93cf2076 | 7675 | 8, 1, &intval, tx)); |
6f1ffb06 MA |
7676 | spa_history_log_internal(spa, "set", tx, |
7677 | "%s=%lld", nvpair_name(elem), intval); | |
34dc7c2f BB |
7678 | } else { |
7679 | ASSERT(0); /* not allowed */ | |
7680 | } | |
7681 | ||
7682 | switch (prop) { | |
7683 | case ZPOOL_PROP_DELEGATION: | |
7684 | spa->spa_delegation = intval; | |
7685 | break; | |
7686 | case ZPOOL_PROP_BOOTFS: | |
7687 | spa->spa_bootfs = intval; | |
7688 | break; | |
7689 | case ZPOOL_PROP_FAILUREMODE: | |
7690 | spa->spa_failmode = intval; | |
7691 | break; | |
9babb374 BB |
7692 | case ZPOOL_PROP_AUTOEXPAND: |
7693 | spa->spa_autoexpand = intval; | |
428870ff BB |
7694 | if (tx->tx_txg != TXG_INITIAL) |
7695 | spa_async_request(spa, | |
7696 | SPA_ASYNC_AUTOEXPAND); | |
7697 | break; | |
379ca9cf OF |
7698 | case ZPOOL_PROP_MULTIHOST: |
7699 | spa->spa_multihost = intval; | |
7700 | break; | |
428870ff BB |
7701 | case ZPOOL_PROP_DEDUPDITTO: |
7702 | spa->spa_dedup_ditto = intval; | |
9babb374 | 7703 | break; |
34dc7c2f BB |
7704 | default: |
7705 | break; | |
7706 | } | |
7707 | } | |
7708 | ||
34dc7c2f | 7709 | } |
b128c09f BB |
7710 | |
7711 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
7712 | } |
7713 | ||
428870ff BB |
7714 | /* |
7715 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
7716 | * reflect the new version this txg, so there must be no changes this | |
7717 | * txg to anything that the upgrade code depends on after it executes. | |
7718 | * Therefore this must be called after dsl_pool_sync() does the sync | |
7719 | * tasks. | |
7720 | */ | |
7721 | static void | |
7722 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
7723 | { | |
8dc2197b SD |
7724 | if (spa_sync_pass(spa) != 1) |
7725 | return; | |
428870ff | 7726 | |
8dc2197b | 7727 | dsl_pool_t *dp = spa->spa_dsl_pool; |
13fe0198 MA |
7728 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
7729 | ||
428870ff BB |
7730 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && |
7731 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
7732 | dsl_pool_create_origin(dp, tx); | |
7733 | ||
7734 | /* Keeping the origin open increases spa_minref */ | |
7735 | spa->spa_minref += 3; | |
7736 | } | |
7737 | ||
7738 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
7739 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
7740 | dsl_pool_upgrade_clones(dp, tx); | |
7741 | } | |
7742 | ||
7743 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
7744 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
7745 | dsl_pool_upgrade_dir_clones(dp, tx); | |
7746 | ||
7747 | /* Keeping the freedir open increases spa_minref */ | |
7748 | spa->spa_minref += 3; | |
7749 | } | |
9ae529ec CS |
7750 | |
7751 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
7752 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
7753 | spa_feature_create_zap_objects(spa, tx); | |
7754 | } | |
62bdd5eb DL |
7755 | |
7756 | /* | |
7757 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
7758 | * when possibility to use lz4 compression for metadata was added | |
7759 | * Old pools that have this feature enabled must be upgraded to have | |
7760 | * this feature active | |
7761 | */ | |
7762 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
7763 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
7764 | SPA_FEATURE_LZ4_COMPRESS); | |
7765 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
7766 | SPA_FEATURE_LZ4_COMPRESS); | |
7767 | ||
7768 | if (lz4_en && !lz4_ac) | |
7769 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
7770 | } | |
3c67d83a TH |
7771 | |
7772 | /* | |
7773 | * If we haven't written the salt, do so now. Note that the | |
7774 | * feature may not be activated yet, but that's fine since | |
7775 | * the presence of this ZAP entry is backwards compatible. | |
7776 | */ | |
7777 | if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
7778 | DMU_POOL_CHECKSUM_SALT) == ENOENT) { | |
7779 | VERIFY0(zap_add(spa->spa_meta_objset, | |
7780 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, | |
7781 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
7782 | spa->spa_cksum_salt.zcs_bytes, tx)); | |
7783 | } | |
7784 | ||
13fe0198 | 7785 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
428870ff BB |
7786 | } |
7787 | ||
a1d477c2 MA |
7788 | static void |
7789 | vdev_indirect_state_sync_verify(vdev_t *vd) | |
7790 | { | |
7791 | ASSERTV(vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping); | |
7792 | ASSERTV(vdev_indirect_births_t *vib = vd->vdev_indirect_births); | |
7793 | ||
7794 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
7795 | ASSERT(vim != NULL); | |
7796 | ASSERT(vib != NULL); | |
7797 | } | |
7798 | ||
27f80e85 BB |
7799 | uint64_t obsolete_sm_object = 0; |
7800 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
7801 | if (obsolete_sm_object != 0) { | |
a1d477c2 MA |
7802 | ASSERT(vd->vdev_obsolete_sm != NULL); |
7803 | ASSERT(vd->vdev_removing || | |
7804 | vd->vdev_ops == &vdev_indirect_ops); | |
7805 | ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); | |
7806 | ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); | |
27f80e85 | 7807 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
7808 | space_map_object(vd->vdev_obsolete_sm)); |
7809 | ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, | |
7810 | space_map_allocated(vd->vdev_obsolete_sm)); | |
7811 | } | |
7812 | ASSERT(vd->vdev_obsolete_segments != NULL); | |
7813 | ||
7814 | /* | |
7815 | * Since frees / remaps to an indirect vdev can only | |
7816 | * happen in syncing context, the obsolete segments | |
7817 | * tree must be empty when we start syncing. | |
7818 | */ | |
7819 | ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); | |
7820 | } | |
7821 | ||
34dc7c2f | 7822 | /* |
8dc2197b SD |
7823 | * Set the top-level vdev's max queue depth. Evaluate each top-level's |
7824 | * async write queue depth in case it changed. The max queue depth will | |
7825 | * not change in the middle of syncing out this txg. | |
34dc7c2f | 7826 | */ |
8dc2197b SD |
7827 | static void |
7828 | spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) | |
34dc7c2f | 7829 | { |
8dc2197b SD |
7830 | ASSERT(spa_writeable(spa)); |
7831 | ||
34dc7c2f | 7832 | vdev_t *rvd = spa->spa_root_vdev; |
3dfb57a3 DB |
7833 | uint32_t max_queue_depth = zfs_vdev_async_write_max_active * |
7834 | zfs_vdev_queue_depth_pct / 100; | |
8dc2197b SD |
7835 | metaslab_class_t *normal = spa_normal_class(spa); |
7836 | metaslab_class_t *special = spa_special_class(spa); | |
7837 | metaslab_class_t *dedup = spa_dedup_class(spa); | |
34dc7c2f | 7838 | |
492f64e9 | 7839 | uint64_t slots_per_allocator = 0; |
1c27024e | 7840 | for (int c = 0; c < rvd->vdev_children; c++) { |
3dfb57a3 | 7841 | vdev_t *tvd = rvd->vdev_child[c]; |
cc99f275 | 7842 | |
8dc2197b | 7843 | metaslab_group_t *mg = tvd->vdev_mg; |
cc99f275 DB |
7844 | if (mg == NULL || !metaslab_group_initialized(mg)) |
7845 | continue; | |
3dfb57a3 | 7846 | |
8dc2197b | 7847 | metaslab_class_t *mc = mg->mg_class; |
cc99f275 | 7848 | if (mc != normal && mc != special && mc != dedup) |
3dfb57a3 DB |
7849 | continue; |
7850 | ||
7851 | /* | |
7852 | * It is safe to do a lock-free check here because only async | |
7853 | * allocations look at mg_max_alloc_queue_depth, and async | |
7854 | * allocations all happen from spa_sync(). | |
7855 | */ | |
492f64e9 | 7856 | for (int i = 0; i < spa->spa_alloc_count; i++) |
424fd7c3 TS |
7857 | ASSERT0(zfs_refcount_count( |
7858 | &(mg->mg_alloc_queue_depth[i]))); | |
3dfb57a3 | 7859 | mg->mg_max_alloc_queue_depth = max_queue_depth; |
492f64e9 PD |
7860 | |
7861 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
7862 | mg->mg_cur_max_alloc_queue_depth[i] = | |
7863 | zfs_vdev_def_queue_depth; | |
7864 | } | |
7865 | slots_per_allocator += zfs_vdev_def_queue_depth; | |
3dfb57a3 | 7866 | } |
cc99f275 | 7867 | |
492f64e9 | 7868 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
424fd7c3 TS |
7869 | ASSERT0(zfs_refcount_count(&normal->mc_alloc_slots[i])); |
7870 | ASSERT0(zfs_refcount_count(&special->mc_alloc_slots[i])); | |
7871 | ASSERT0(zfs_refcount_count(&dedup->mc_alloc_slots[i])); | |
cc99f275 DB |
7872 | normal->mc_alloc_max_slots[i] = slots_per_allocator; |
7873 | special->mc_alloc_max_slots[i] = slots_per_allocator; | |
7874 | dedup->mc_alloc_max_slots[i] = slots_per_allocator; | |
7875 | } | |
7876 | normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
7877 | special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
7878 | dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8dc2197b SD |
7879 | } |
7880 | ||
7881 | static void | |
7882 | spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) | |
7883 | { | |
7884 | ASSERT(spa_writeable(spa)); | |
3dfb57a3 | 7885 | |
8dc2197b | 7886 | vdev_t *rvd = spa->spa_root_vdev; |
a1d477c2 MA |
7887 | for (int c = 0; c < rvd->vdev_children; c++) { |
7888 | vdev_t *vd = rvd->vdev_child[c]; | |
7889 | vdev_indirect_state_sync_verify(vd); | |
7890 | ||
7891 | if (vdev_indirect_should_condense(vd)) { | |
7892 | spa_condense_indirect_start_sync(vd, tx); | |
7893 | break; | |
7894 | } | |
7895 | } | |
8dc2197b SD |
7896 | } |
7897 | ||
7898 | static void | |
7899 | spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) | |
7900 | { | |
7901 | objset_t *mos = spa->spa_meta_objset; | |
7902 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
7903 | uint64_t txg = tx->tx_txg; | |
7904 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
a1d477c2 | 7905 | |
34dc7c2f | 7906 | do { |
428870ff | 7907 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
7908 | |
7909 | spa_sync_config_object(spa, tx); | |
7910 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
7911 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
7912 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
7913 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
7914 | spa_errlog_sync(spa, txg); | |
7915 | dsl_pool_sync(dp, txg); | |
7916 | ||
55d85d5a | 7917 | if (pass < zfs_sync_pass_deferred_free) { |
e8b96c60 | 7918 | spa_sync_frees(spa, free_bpl, tx); |
428870ff | 7919 | } else { |
905edb40 MA |
7920 | /* |
7921 | * We can not defer frees in pass 1, because | |
7922 | * we sync the deferred frees later in pass 1. | |
7923 | */ | |
7924 | ASSERT3U(pass, >, 1); | |
428870ff | 7925 | bplist_iterate(free_bpl, bpobj_enqueue_cb, |
e8b96c60 | 7926 | &spa->spa_deferred_bpobj, tx); |
34dc7c2f BB |
7927 | } |
7928 | ||
428870ff BB |
7929 | ddt_sync(spa, txg); |
7930 | dsl_scan_sync(dp, tx); | |
8dc2197b SD |
7931 | svr_sync(spa, tx); |
7932 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 7933 | |
8dc2197b | 7934 | vdev_t *vd = NULL; |
a1d477c2 MA |
7935 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) |
7936 | != NULL) | |
428870ff BB |
7937 | vdev_sync(vd, txg); |
7938 | ||
8dc2197b SD |
7939 | /* |
7940 | * Note: We need to check if the MOS is dirty because we could | |
7941 | * have marked the MOS dirty without updating the uberblock | |
7942 | * (e.g. if we have sync tasks but no dirty user data). We need | |
7943 | * to check the uberblock's rootbp because it is updated if we | |
7944 | * have synced out dirty data (though in this case the MOS will | |
7945 | * most likely also be dirty due to second order effects, we | |
7946 | * don't want to rely on that here). | |
7947 | */ | |
7948 | if (pass == 1 && | |
7949 | spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
7950 | !dmu_objset_is_dirty(mos, txg)) { | |
905edb40 | 7951 | /* |
8dc2197b SD |
7952 | * Nothing changed on the first pass, therefore this |
7953 | * TXG is a no-op. Avoid syncing deferred frees, so | |
7954 | * that we can keep this TXG as a no-op. | |
905edb40 | 7955 | */ |
8dc2197b SD |
7956 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); |
7957 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
7958 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
7959 | ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); | |
7960 | break; | |
905edb40 | 7961 | } |
34dc7c2f | 7962 | |
8dc2197b | 7963 | spa_sync_deferred_frees(spa, tx); |
428870ff | 7964 | } while (dmu_objset_is_dirty(mos, txg)); |
8dc2197b | 7965 | } |
34dc7c2f | 7966 | |
8dc2197b SD |
7967 | /* |
7968 | * Rewrite the vdev configuration (which includes the uberblock) to | |
7969 | * commit the transaction group. | |
7970 | * | |
7971 | * If there are no dirty vdevs, we sync the uberblock to a few random | |
7972 | * top-level vdevs that are known to be visible in the config cache | |
7973 | * (see spa_vdev_add() for a complete description). If there *are* dirty | |
7974 | * vdevs, sync the uberblock to all vdevs. | |
7975 | */ | |
7976 | static void | |
7977 | spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) | |
7978 | { | |
7979 | vdev_t *rvd = spa->spa_root_vdev; | |
7980 | uint64_t txg = tx->tx_txg; | |
a1d477c2 | 7981 | |
b128c09f | 7982 | for (;;) { |
8dc2197b SD |
7983 | int error = 0; |
7984 | ||
b128c09f BB |
7985 | /* |
7986 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
7987 | * while we're attempting to write the vdev labels. | |
7988 | */ | |
7989 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
7990 | ||
7991 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
d2734cce | 7992 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; |
b128c09f BB |
7993 | int svdcount = 0; |
7994 | int children = rvd->vdev_children; | |
7995 | int c0 = spa_get_random(children); | |
b128c09f | 7996 | |
1c27024e | 7997 | for (int c = 0; c < children; c++) { |
8dc2197b SD |
7998 | vdev_t *vd = |
7999 | rvd->vdev_child[(c0 + c) % children]; | |
d2734cce SD |
8000 | |
8001 | /* Stop when revisiting the first vdev */ | |
8002 | if (c > 0 && svd[0] == vd) | |
8003 | break; | |
8004 | ||
8dc2197b SD |
8005 | if (vd->vdev_ms_array == 0 || |
8006 | vd->vdev_islog || | |
a1d477c2 | 8007 | !vdev_is_concrete(vd)) |
b128c09f | 8008 | continue; |
d2734cce | 8009 | |
b128c09f | 8010 | svd[svdcount++] = vd; |
6cb8e530 | 8011 | if (svdcount == SPA_SYNC_MIN_VDEVS) |
b128c09f BB |
8012 | break; |
8013 | } | |
b6fcb792 | 8014 | error = vdev_config_sync(svd, svdcount, txg); |
b128c09f BB |
8015 | } else { |
8016 | error = vdev_config_sync(rvd->vdev_child, | |
b6fcb792 | 8017 | rvd->vdev_children, txg); |
34dc7c2f | 8018 | } |
34dc7c2f | 8019 | |
3bc7e0fb GW |
8020 | if (error == 0) |
8021 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
8022 | ||
b128c09f BB |
8023 | spa_config_exit(spa, SCL_STATE, FTAG); |
8024 | ||
8025 | if (error == 0) | |
8026 | break; | |
cec3a0a1 | 8027 | zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); |
b128c09f BB |
8028 | zio_resume_wait(spa); |
8029 | } | |
8dc2197b SD |
8030 | } |
8031 | ||
8032 | /* | |
8033 | * Sync the specified transaction group. New blocks may be dirtied as | |
8034 | * part of the process, so we iterate until it converges. | |
8035 | */ | |
8036 | void | |
8037 | spa_sync(spa_t *spa, uint64_t txg) | |
8038 | { | |
8039 | vdev_t *vd = NULL; | |
8040 | ||
8041 | VERIFY(spa_writeable(spa)); | |
8042 | ||
8043 | /* | |
8044 | * Wait for i/os issued in open context that need to complete | |
8045 | * before this txg syncs. | |
8046 | */ | |
8047 | (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); | |
8048 | spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, | |
8049 | ZIO_FLAG_CANFAIL); | |
8050 | ||
8051 | /* | |
8052 | * Lock out configuration changes. | |
8053 | */ | |
8054 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8055 | ||
8056 | spa->spa_syncing_txg = txg; | |
8057 | spa->spa_sync_pass = 0; | |
8058 | ||
8059 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
8060 | mutex_enter(&spa->spa_alloc_locks[i]); | |
8061 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
8062 | mutex_exit(&spa->spa_alloc_locks[i]); | |
8063 | } | |
8064 | ||
8065 | /* | |
8066 | * If there are any pending vdev state changes, convert them | |
8067 | * into config changes that go out with this transaction group. | |
8068 | */ | |
8069 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
8070 | while (list_head(&spa->spa_state_dirty_list) != NULL) { | |
8071 | /* | |
8072 | * We need the write lock here because, for aux vdevs, | |
8073 | * calling vdev_config_dirty() modifies sav_config. | |
8074 | * This is ugly and will become unnecessary when we | |
8075 | * eliminate the aux vdev wart by integrating all vdevs | |
8076 | * into the root vdev tree. | |
8077 | */ | |
8078 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
8079 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
8080 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
8081 | vdev_state_clean(vd); | |
8082 | vdev_config_dirty(vd); | |
8083 | } | |
8084 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
8085 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
8086 | } | |
8087 | spa_config_exit(spa, SCL_STATE, FTAG); | |
8088 | ||
8089 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
8090 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
8091 | ||
8092 | spa->spa_sync_starttime = gethrtime(); | |
8093 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); | |
8094 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, | |
8095 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
8096 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
8097 | ||
8098 | /* | |
8099 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
8100 | * set spa_deflate if we have no raid-z vdevs. | |
8101 | */ | |
8102 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
8103 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
8104 | vdev_t *rvd = spa->spa_root_vdev; | |
8105 | ||
8106 | int i; | |
8107 | for (i = 0; i < rvd->vdev_children; i++) { | |
8108 | vd = rvd->vdev_child[i]; | |
8109 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
8110 | break; | |
8111 | } | |
8112 | if (i == rvd->vdev_children) { | |
8113 | spa->spa_deflate = TRUE; | |
8114 | VERIFY0(zap_add(spa->spa_meta_objset, | |
8115 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
8116 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
8117 | } | |
8118 | } | |
8119 | ||
8120 | spa_sync_adjust_vdev_max_queue_depth(spa); | |
8121 | ||
8122 | spa_sync_condense_indirect(spa, tx); | |
8123 | ||
8124 | spa_sync_iterate_to_convergence(spa, tx); | |
8125 | ||
8126 | #ifdef ZFS_DEBUG | |
8127 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
8128 | /* | |
8129 | * Make sure that the number of ZAPs for all the vdevs matches | |
8130 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
8131 | * called if the config is dirty; otherwise there may be | |
8132 | * outstanding AVZ operations that weren't completed in | |
8133 | * spa_sync_config_object. | |
8134 | */ | |
8135 | uint64_t all_vdev_zap_entry_count; | |
8136 | ASSERT0(zap_count(spa->spa_meta_objset, | |
8137 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
8138 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
8139 | all_vdev_zap_entry_count); | |
8140 | } | |
8141 | #endif | |
8142 | ||
8143 | if (spa->spa_vdev_removal != NULL) { | |
8144 | ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); | |
8145 | } | |
8146 | ||
8147 | spa_sync_rewrite_vdev_config(spa, tx); | |
34dc7c2f BB |
8148 | dmu_tx_commit(tx); |
8149 | ||
57ddcda1 | 8150 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 GW |
8151 | spa->spa_deadman_tqid = 0; |
8152 | ||
34dc7c2f BB |
8153 | /* |
8154 | * Clear the dirty config list. | |
8155 | */ | |
b128c09f | 8156 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
8157 | vdev_config_clean(vd); |
8158 | ||
8159 | /* | |
8160 | * Now that the new config has synced transactionally, | |
8161 | * let it become visible to the config cache. | |
8162 | */ | |
8163 | if (spa->spa_config_syncing != NULL) { | |
8164 | spa_config_set(spa, spa->spa_config_syncing); | |
8165 | spa->spa_config_txg = txg; | |
8166 | spa->spa_config_syncing = NULL; | |
8167 | } | |
8168 | ||
428870ff | 8169 | dsl_pool_sync_done(dp, txg); |
34dc7c2f | 8170 | |
492f64e9 PD |
8171 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
8172 | mutex_enter(&spa->spa_alloc_locks[i]); | |
8173 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
8174 | mutex_exit(&spa->spa_alloc_locks[i]); | |
8175 | } | |
3dfb57a3 | 8176 | |
34dc7c2f BB |
8177 | /* |
8178 | * Update usable space statistics. | |
8179 | */ | |
619f0976 GW |
8180 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) |
8181 | != NULL) | |
34dc7c2f BB |
8182 | vdev_sync_done(vd, txg); |
8183 | ||
428870ff BB |
8184 | spa_update_dspace(spa); |
8185 | ||
34dc7c2f BB |
8186 | /* |
8187 | * It had better be the case that we didn't dirty anything | |
8188 | * since vdev_config_sync(). | |
8189 | */ | |
8190 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
8191 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
8192 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff | 8193 | |
d2734cce SD |
8194 | while (zfs_pause_spa_sync) |
8195 | delay(1); | |
8196 | ||
428870ff | 8197 | spa->spa_sync_pass = 0; |
34dc7c2f | 8198 | |
55922e73 GW |
8199 | /* |
8200 | * Update the last synced uberblock here. We want to do this at | |
8201 | * the end of spa_sync() so that consumers of spa_last_synced_txg() | |
8202 | * will be guaranteed that all the processing associated with | |
8203 | * that txg has been completed. | |
8204 | */ | |
8205 | spa->spa_ubsync = spa->spa_uberblock; | |
b128c09f | 8206 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 8207 | |
428870ff BB |
8208 | spa_handle_ignored_writes(spa); |
8209 | ||
34dc7c2f BB |
8210 | /* |
8211 | * If any async tasks have been requested, kick them off. | |
8212 | */ | |
8213 | spa_async_dispatch(spa); | |
8214 | } | |
8215 | ||
8216 | /* | |
8217 | * Sync all pools. We don't want to hold the namespace lock across these | |
8218 | * operations, so we take a reference on the spa_t and drop the lock during the | |
8219 | * sync. | |
8220 | */ | |
8221 | void | |
8222 | spa_sync_allpools(void) | |
8223 | { | |
8224 | spa_t *spa = NULL; | |
8225 | mutex_enter(&spa_namespace_lock); | |
8226 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
8227 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
8228 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
8229 | continue; |
8230 | spa_open_ref(spa, FTAG); | |
8231 | mutex_exit(&spa_namespace_lock); | |
8232 | txg_wait_synced(spa_get_dsl(spa), 0); | |
8233 | mutex_enter(&spa_namespace_lock); | |
8234 | spa_close(spa, FTAG); | |
8235 | } | |
8236 | mutex_exit(&spa_namespace_lock); | |
8237 | } | |
8238 | ||
8239 | /* | |
8240 | * ========================================================================== | |
8241 | * Miscellaneous routines | |
8242 | * ========================================================================== | |
8243 | */ | |
8244 | ||
8245 | /* | |
8246 | * Remove all pools in the system. | |
8247 | */ | |
8248 | void | |
8249 | spa_evict_all(void) | |
8250 | { | |
8251 | spa_t *spa; | |
8252 | ||
8253 | /* | |
8254 | * Remove all cached state. All pools should be closed now, | |
8255 | * so every spa in the AVL tree should be unreferenced. | |
8256 | */ | |
8257 | mutex_enter(&spa_namespace_lock); | |
8258 | while ((spa = spa_next(NULL)) != NULL) { | |
8259 | /* | |
8260 | * Stop async tasks. The async thread may need to detach | |
8261 | * a device that's been replaced, which requires grabbing | |
8262 | * spa_namespace_lock, so we must drop it here. | |
8263 | */ | |
8264 | spa_open_ref(spa, FTAG); | |
8265 | mutex_exit(&spa_namespace_lock); | |
8266 | spa_async_suspend(spa); | |
8267 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
8268 | spa_close(spa, FTAG); |
8269 | ||
8270 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
8271 | spa_unload(spa); | |
8272 | spa_deactivate(spa); | |
8273 | } | |
8274 | spa_remove(spa); | |
8275 | } | |
8276 | mutex_exit(&spa_namespace_lock); | |
8277 | } | |
8278 | ||
8279 | vdev_t * | |
9babb374 | 8280 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 8281 | { |
b128c09f BB |
8282 | vdev_t *vd; |
8283 | int i; | |
8284 | ||
8285 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
8286 | return (vd); | |
8287 | ||
9babb374 | 8288 | if (aux) { |
b128c09f BB |
8289 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
8290 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
8291 | if (vd->vdev_guid == guid) |
8292 | return (vd); | |
8293 | } | |
8294 | ||
8295 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
8296 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
8297 | if (vd->vdev_guid == guid) |
8298 | return (vd); | |
8299 | } | |
8300 | } | |
8301 | ||
8302 | return (NULL); | |
34dc7c2f BB |
8303 | } |
8304 | ||
8305 | void | |
8306 | spa_upgrade(spa_t *spa, uint64_t version) | |
8307 | { | |
572e2857 BB |
8308 | ASSERT(spa_writeable(spa)); |
8309 | ||
b128c09f | 8310 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
8311 | |
8312 | /* | |
8313 | * This should only be called for a non-faulted pool, and since a | |
8314 | * future version would result in an unopenable pool, this shouldn't be | |
8315 | * possible. | |
8316 | */ | |
8dca0a9a | 8317 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
9b67f605 | 8318 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); |
34dc7c2f BB |
8319 | |
8320 | spa->spa_uberblock.ub_version = version; | |
8321 | vdev_config_dirty(spa->spa_root_vdev); | |
8322 | ||
b128c09f | 8323 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
8324 | |
8325 | txg_wait_synced(spa_get_dsl(spa), 0); | |
8326 | } | |
8327 | ||
8328 | boolean_t | |
8329 | spa_has_spare(spa_t *spa, uint64_t guid) | |
8330 | { | |
8331 | int i; | |
8332 | uint64_t spareguid; | |
8333 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
8334 | ||
8335 | for (i = 0; i < sav->sav_count; i++) | |
8336 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
8337 | return (B_TRUE); | |
8338 | ||
8339 | for (i = 0; i < sav->sav_npending; i++) { | |
8340 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
8341 | &spareguid) == 0 && spareguid == guid) | |
8342 | return (B_TRUE); | |
8343 | } | |
8344 | ||
8345 | return (B_FALSE); | |
8346 | } | |
8347 | ||
b128c09f BB |
8348 | /* |
8349 | * Check if a pool has an active shared spare device. | |
8350 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
8351 | */ | |
8352 | static boolean_t | |
8353 | spa_has_active_shared_spare(spa_t *spa) | |
8354 | { | |
8355 | int i, refcnt; | |
8356 | uint64_t pool; | |
8357 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
8358 | ||
8359 | for (i = 0; i < sav->sav_count; i++) { | |
8360 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
8361 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
8362 | refcnt > 2) | |
8363 | return (B_TRUE); | |
8364 | } | |
8365 | ||
8366 | return (B_FALSE); | |
8367 | } | |
8368 | ||
a1d477c2 | 8369 | sysevent_t * |
12fa0466 DE |
8370 | spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
8371 | { | |
8372 | sysevent_t *ev = NULL; | |
8373 | #ifdef _KERNEL | |
8374 | nvlist_t *resource; | |
8375 | ||
8376 | resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl); | |
8377 | if (resource) { | |
8378 | ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP); | |
8379 | ev->resource = resource; | |
8380 | } | |
8381 | #endif | |
8382 | return (ev); | |
8383 | } | |
8384 | ||
a1d477c2 | 8385 | void |
12fa0466 DE |
8386 | spa_event_post(sysevent_t *ev) |
8387 | { | |
8388 | #ifdef _KERNEL | |
8389 | if (ev) { | |
8390 | zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb); | |
8391 | kmem_free(ev, sizeof (*ev)); | |
8392 | } | |
8393 | #endif | |
8394 | } | |
8395 | ||
34dc7c2f | 8396 | /* |
fb390aaf HR |
8397 | * Post a zevent corresponding to the given sysevent. The 'name' must be one |
8398 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
34dc7c2f BB |
8399 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
8400 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
8401 | * or zdb as real changes. | |
8402 | */ | |
8403 | void | |
12fa0466 | 8404 | spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
34dc7c2f | 8405 | { |
12fa0466 | 8406 | spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); |
34dc7c2f | 8407 | } |
c28b2279 | 8408 | |
93ce2b4c | 8409 | #if defined(_KERNEL) |
c28b2279 BB |
8410 | /* state manipulation functions */ |
8411 | EXPORT_SYMBOL(spa_open); | |
8412 | EXPORT_SYMBOL(spa_open_rewind); | |
8413 | EXPORT_SYMBOL(spa_get_stats); | |
8414 | EXPORT_SYMBOL(spa_create); | |
c28b2279 BB |
8415 | EXPORT_SYMBOL(spa_import); |
8416 | EXPORT_SYMBOL(spa_tryimport); | |
8417 | EXPORT_SYMBOL(spa_destroy); | |
8418 | EXPORT_SYMBOL(spa_export); | |
8419 | EXPORT_SYMBOL(spa_reset); | |
8420 | EXPORT_SYMBOL(spa_async_request); | |
8421 | EXPORT_SYMBOL(spa_async_suspend); | |
8422 | EXPORT_SYMBOL(spa_async_resume); | |
8423 | EXPORT_SYMBOL(spa_inject_addref); | |
8424 | EXPORT_SYMBOL(spa_inject_delref); | |
8425 | EXPORT_SYMBOL(spa_scan_stat_init); | |
8426 | EXPORT_SYMBOL(spa_scan_get_stats); | |
8427 | ||
8428 | /* device maniion */ | |
8429 | EXPORT_SYMBOL(spa_vdev_add); | |
8430 | EXPORT_SYMBOL(spa_vdev_attach); | |
8431 | EXPORT_SYMBOL(spa_vdev_detach); | |
c28b2279 BB |
8432 | EXPORT_SYMBOL(spa_vdev_setpath); |
8433 | EXPORT_SYMBOL(spa_vdev_setfru); | |
8434 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
8435 | ||
8436 | /* spare statech is global across all pools) */ | |
8437 | EXPORT_SYMBOL(spa_spare_add); | |
8438 | EXPORT_SYMBOL(spa_spare_remove); | |
8439 | EXPORT_SYMBOL(spa_spare_exists); | |
8440 | EXPORT_SYMBOL(spa_spare_activate); | |
8441 | ||
8442 | /* L2ARC statech is global across all pools) */ | |
8443 | EXPORT_SYMBOL(spa_l2cache_add); | |
8444 | EXPORT_SYMBOL(spa_l2cache_remove); | |
8445 | EXPORT_SYMBOL(spa_l2cache_exists); | |
8446 | EXPORT_SYMBOL(spa_l2cache_activate); | |
8447 | EXPORT_SYMBOL(spa_l2cache_drop); | |
8448 | ||
8449 | /* scanning */ | |
8450 | EXPORT_SYMBOL(spa_scan); | |
8451 | EXPORT_SYMBOL(spa_scan_stop); | |
8452 | ||
8453 | /* spa syncing */ | |
8454 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
8455 | EXPORT_SYMBOL(spa_sync_allpools); | |
8456 | ||
8457 | /* properties */ | |
8458 | EXPORT_SYMBOL(spa_prop_set); | |
8459 | EXPORT_SYMBOL(spa_prop_get); | |
8460 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
8461 | ||
8462 | /* asynchronous event notification */ | |
8463 | EXPORT_SYMBOL(spa_event_notify); | |
8464 | #endif | |
dea377c0 | 8465 | |
93ce2b4c | 8466 | #if defined(_KERNEL) |
dea377c0 MA |
8467 | module_param(spa_load_verify_maxinflight, int, 0644); |
8468 | MODULE_PARM_DESC(spa_load_verify_maxinflight, | |
8469 | "Max concurrent traversal I/Os while verifying pool during import -X"); | |
8470 | ||
8471 | module_param(spa_load_verify_metadata, int, 0644); | |
8472 | MODULE_PARM_DESC(spa_load_verify_metadata, | |
8473 | "Set to traverse metadata on pool import"); | |
8474 | ||
8475 | module_param(spa_load_verify_data, int, 0644); | |
8476 | MODULE_PARM_DESC(spa_load_verify_data, | |
8477 | "Set to traverse data on pool import"); | |
dcb6bed1 | 8478 | |
6cb8e530 PZ |
8479 | module_param(spa_load_print_vdev_tree, int, 0644); |
8480 | MODULE_PARM_DESC(spa_load_print_vdev_tree, | |
8481 | "Print vdev tree to zfs_dbgmsg during pool import"); | |
8482 | ||
02730c33 | 8483 | /* CSTYLED */ |
dcb6bed1 D |
8484 | module_param(zio_taskq_batch_pct, uint, 0444); |
8485 | MODULE_PARM_DESC(zio_taskq_batch_pct, | |
8486 | "Percentage of CPUs to run an IO worker thread"); | |
8487 | ||
6cb8e530 PZ |
8488 | /* BEGIN CSTYLED */ |
8489 | module_param(zfs_max_missing_tvds, ulong, 0644); | |
8490 | MODULE_PARM_DESC(zfs_max_missing_tvds, | |
8491 | "Allow importing pool with up to this number of missing top-level vdevs" | |
8492 | " (in read-only mode)"); | |
8493 | /* END CSTYLED */ | |
8494 | ||
dea377c0 | 8495 | #endif |