]>
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; | |
060f0226 | 2440 | uint16_t tryconfig_mmp_seq = 0; |
379ca9cf OF |
2441 | nvlist_t *nvinfo; |
2442 | ||
2443 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
2444 | nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO); | |
2445 | (void) nvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG, | |
2446 | &tryconfig_txg); | |
2447 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
2448 | &tryconfig_timestamp); | |
060f0226 OF |
2449 | (void) nvlist_lookup_uint16(nvinfo, ZPOOL_CONFIG_MMP_SEQ, |
2450 | &tryconfig_mmp_seq); | |
379ca9cf OF |
2451 | } |
2452 | ||
2453 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state); | |
379ca9cf OF |
2454 | |
2455 | /* | |
2456 | * Disable the MMP activity check - This is used by zdb which | |
2457 | * is intended to be used on potentially active pools. | |
2458 | */ | |
2459 | if (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) | |
2460 | return (B_FALSE); | |
2461 | ||
2462 | /* | |
2463 | * Skip the activity check when the MMP feature is disabled. | |
2464 | */ | |
2465 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay == 0) | |
2466 | return (B_FALSE); | |
2467 | /* | |
060f0226 OF |
2468 | * If the tryconfig_ values are nonzero, they are the results of an |
2469 | * earlier tryimport. If they all match the uberblock we just found, | |
2470 | * then the pool has not changed and we return false so we do not test | |
2471 | * a second time. | |
379ca9cf OF |
2472 | */ |
2473 | if (tryconfig_txg && tryconfig_txg == ub->ub_txg && | |
060f0226 OF |
2474 | tryconfig_timestamp && tryconfig_timestamp == ub->ub_timestamp && |
2475 | tryconfig_mmp_seq && tryconfig_mmp_seq == | |
2476 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) | |
379ca9cf OF |
2477 | return (B_FALSE); |
2478 | ||
2479 | /* | |
2480 | * Allow the activity check to be skipped when importing the pool | |
bbffb59e BB |
2481 | * on the same host which last imported it. Since the hostid from |
2482 | * configuration may be stale use the one read from the label. | |
379ca9cf | 2483 | */ |
bbffb59e BB |
2484 | if (nvlist_exists(label, ZPOOL_CONFIG_HOSTID)) |
2485 | hostid = fnvlist_lookup_uint64(label, ZPOOL_CONFIG_HOSTID); | |
2486 | ||
379ca9cf OF |
2487 | if (hostid == spa_get_hostid()) |
2488 | return (B_FALSE); | |
2489 | ||
2490 | /* | |
2491 | * Skip the activity test when the pool was cleanly exported. | |
2492 | */ | |
2493 | if (state != POOL_STATE_ACTIVE) | |
2494 | return (B_FALSE); | |
2495 | ||
2496 | return (B_TRUE); | |
2497 | } | |
2498 | ||
060f0226 OF |
2499 | /* |
2500 | * Nanoseconds the activity check must watch for changes on-disk. | |
2501 | */ | |
2502 | static uint64_t | |
2503 | spa_activity_check_duration(spa_t *spa, uberblock_t *ub) | |
2504 | { | |
2505 | uint64_t import_intervals = MAX(zfs_multihost_import_intervals, 1); | |
2506 | uint64_t multihost_interval = MSEC2NSEC( | |
2507 | MMP_INTERVAL_OK(zfs_multihost_interval)); | |
2508 | uint64_t import_delay = MAX(NANOSEC, import_intervals * | |
2509 | multihost_interval); | |
2510 | ||
2511 | /* | |
2512 | * Local tunables determine a minimum duration except for the case | |
2513 | * where we know when the remote host will suspend the pool if MMP | |
2514 | * writes do not land. | |
2515 | * | |
2516 | * See Big Theory comment at the top of mmp.c for the reasoning behind | |
2517 | * these cases and times. | |
2518 | */ | |
2519 | ||
2520 | ASSERT(MMP_IMPORT_SAFETY_FACTOR >= 100); | |
2521 | ||
2522 | if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
2523 | MMP_FAIL_INT(ub) > 0) { | |
2524 | ||
2525 | /* MMP on remote host will suspend pool after failed writes */ | |
2526 | import_delay = MMP_FAIL_INT(ub) * MSEC2NSEC(MMP_INTERVAL(ub)) * | |
2527 | MMP_IMPORT_SAFETY_FACTOR / 100; | |
2528 | ||
2529 | zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp " | |
2530 | "mmp_fails=%llu ub_mmp mmp_interval=%llu " | |
2531 | "import_intervals=%u", import_delay, MMP_FAIL_INT(ub), | |
2532 | MMP_INTERVAL(ub), import_intervals); | |
2533 | ||
2534 | } else if (MMP_INTERVAL_VALID(ub) && MMP_FAIL_INT_VALID(ub) && | |
2535 | MMP_FAIL_INT(ub) == 0) { | |
2536 | ||
2537 | /* MMP on remote host will never suspend pool */ | |
2538 | import_delay = MAX(import_delay, (MSEC2NSEC(MMP_INTERVAL(ub)) + | |
2539 | ub->ub_mmp_delay) * import_intervals); | |
2540 | ||
2541 | zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp " | |
2542 | "mmp_interval=%llu ub_mmp_delay=%llu " | |
2543 | "import_intervals=%u", import_delay, MMP_INTERVAL(ub), | |
2544 | ub->ub_mmp_delay, import_intervals); | |
2545 | ||
2546 | } else if (MMP_VALID(ub)) { | |
2547 | /* | |
2548 | * zfs-0.7 compatability case | |
2549 | */ | |
2550 | ||
2551 | import_delay = MAX(import_delay, (multihost_interval + | |
2552 | ub->ub_mmp_delay) * import_intervals); | |
2553 | ||
2554 | zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu " | |
2555 | "import_intervals=%u leaves=%u", import_delay, | |
2556 | ub->ub_mmp_delay, import_intervals, | |
2557 | vdev_count_leaves(spa)); | |
2558 | } else { | |
2559 | /* Using local tunings is the only reasonable option */ | |
2560 | zfs_dbgmsg("pool last imported on non-MMP aware " | |
2561 | "host using import_delay=%llu multihost_interval=%llu " | |
2562 | "import_intervals=%u", import_delay, multihost_interval, | |
2563 | import_intervals); | |
2564 | } | |
2565 | ||
2566 | return (import_delay); | |
2567 | } | |
2568 | ||
379ca9cf OF |
2569 | /* |
2570 | * Perform the import activity check. If the user canceled the import or | |
2571 | * we detected activity then fail. | |
2572 | */ | |
2573 | static int | |
2574 | spa_activity_check(spa_t *spa, uberblock_t *ub, nvlist_t *config) | |
2575 | { | |
379ca9cf OF |
2576 | uint64_t txg = ub->ub_txg; |
2577 | uint64_t timestamp = ub->ub_timestamp; | |
060f0226 OF |
2578 | uint64_t mmp_config = ub->ub_mmp_config; |
2579 | uint16_t mmp_seq = MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0; | |
2580 | uint64_t import_delay; | |
379ca9cf OF |
2581 | hrtime_t import_expire; |
2582 | nvlist_t *mmp_label = NULL; | |
2583 | vdev_t *rvd = spa->spa_root_vdev; | |
2584 | kcondvar_t cv; | |
2585 | kmutex_t mtx; | |
2586 | int error = 0; | |
2587 | ||
2588 | cv_init(&cv, NULL, CV_DEFAULT, NULL); | |
2589 | mutex_init(&mtx, NULL, MUTEX_DEFAULT, NULL); | |
2590 | mutex_enter(&mtx); | |
2591 | ||
2592 | /* | |
2593 | * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed | |
2594 | * during the earlier tryimport. If the txg recorded there is 0 then | |
2595 | * the pool is known to be active on another host. | |
2596 | * | |
060f0226 | 2597 | * Otherwise, the pool might be in use on another host. Check for |
379ca9cf OF |
2598 | * changes in the uberblocks on disk if necessary. |
2599 | */ | |
2600 | if (nvlist_exists(config, ZPOOL_CONFIG_LOAD_INFO)) { | |
2601 | nvlist_t *nvinfo = fnvlist_lookup_nvlist(config, | |
2602 | ZPOOL_CONFIG_LOAD_INFO); | |
2603 | ||
2604 | if (nvlist_exists(nvinfo, ZPOOL_CONFIG_MMP_TXG) && | |
2605 | fnvlist_lookup_uint64(nvinfo, ZPOOL_CONFIG_MMP_TXG) == 0) { | |
2606 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
2607 | error = SET_ERROR(EREMOTEIO); | |
2608 | goto out; | |
2609 | } | |
2610 | } | |
2611 | ||
060f0226 | 2612 | import_delay = spa_activity_check_duration(spa, ub); |
533ea041 | 2613 | |
379ca9cf OF |
2614 | /* Add a small random factor in case of simultaneous imports (0-25%) */ |
2615 | import_expire = gethrtime() + import_delay + | |
2616 | (import_delay * spa_get_random(250) / 1000); | |
2617 | ||
2618 | while (gethrtime() < import_expire) { | |
2619 | vdev_uberblock_load(rvd, ub, &mmp_label); | |
2620 | ||
060f0226 OF |
2621 | if (txg != ub->ub_txg || timestamp != ub->ub_timestamp || |
2622 | mmp_seq != (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)) { | |
2623 | zfs_dbgmsg("multihost activity detected " | |
2624 | "txg %llu ub_txg %llu " | |
2625 | "timestamp %llu ub_timestamp %llu " | |
2626 | "mmp_config %#llx ub_mmp_config %#llx", | |
2627 | txg, ub->ub_txg, timestamp, ub->ub_timestamp, | |
2628 | mmp_config, ub->ub_mmp_config); | |
2629 | ||
379ca9cf OF |
2630 | error = SET_ERROR(EREMOTEIO); |
2631 | break; | |
2632 | } | |
2633 | ||
2634 | if (mmp_label) { | |
2635 | nvlist_free(mmp_label); | |
2636 | mmp_label = NULL; | |
2637 | } | |
2638 | ||
2639 | error = cv_timedwait_sig(&cv, &mtx, ddi_get_lbolt() + hz); | |
2640 | if (error != -1) { | |
2641 | error = SET_ERROR(EINTR); | |
2642 | break; | |
2643 | } | |
2644 | error = 0; | |
2645 | } | |
2646 | ||
2647 | out: | |
2648 | mutex_exit(&mtx); | |
2649 | mutex_destroy(&mtx); | |
2650 | cv_destroy(&cv); | |
2651 | ||
2652 | /* | |
2653 | * If the pool is determined to be active store the status in the | |
2654 | * spa->spa_load_info nvlist. If the remote hostname or hostid are | |
2655 | * available from configuration read from disk store them as well. | |
2656 | * This allows 'zpool import' to generate a more useful message. | |
2657 | * | |
2658 | * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory) | |
2659 | * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool | |
2660 | * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool | |
2661 | */ | |
2662 | if (error == EREMOTEIO) { | |
2663 | char *hostname = "<unknown>"; | |
2664 | uint64_t hostid = 0; | |
2665 | ||
2666 | if (mmp_label) { | |
2667 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTNAME)) { | |
2668 | hostname = fnvlist_lookup_string(mmp_label, | |
2669 | ZPOOL_CONFIG_HOSTNAME); | |
2670 | fnvlist_add_string(spa->spa_load_info, | |
2671 | ZPOOL_CONFIG_MMP_HOSTNAME, hostname); | |
2672 | } | |
2673 | ||
2674 | if (nvlist_exists(mmp_label, ZPOOL_CONFIG_HOSTID)) { | |
2675 | hostid = fnvlist_lookup_uint64(mmp_label, | |
2676 | ZPOOL_CONFIG_HOSTID); | |
2677 | fnvlist_add_uint64(spa->spa_load_info, | |
2678 | ZPOOL_CONFIG_MMP_HOSTID, hostid); | |
2679 | } | |
2680 | } | |
2681 | ||
2682 | fnvlist_add_uint64(spa->spa_load_info, | |
2683 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_ACTIVE); | |
2684 | fnvlist_add_uint64(spa->spa_load_info, | |
2685 | ZPOOL_CONFIG_MMP_TXG, 0); | |
2686 | ||
2687 | error = spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO); | |
2688 | } | |
2689 | ||
2690 | if (mmp_label) | |
2691 | nvlist_free(mmp_label); | |
2692 | ||
2693 | return (error); | |
2694 | } | |
2695 | ||
9eb7b46e | 2696 | static int |
6cb8e530 PZ |
2697 | spa_verify_host(spa_t *spa, nvlist_t *mos_config) |
2698 | { | |
2699 | uint64_t hostid; | |
2700 | char *hostname; | |
2701 | uint64_t myhostid = 0; | |
2702 | ||
2703 | if (!spa_is_root(spa) && nvlist_lookup_uint64(mos_config, | |
2704 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
2705 | hostname = fnvlist_lookup_string(mos_config, | |
2706 | ZPOOL_CONFIG_HOSTNAME); | |
2707 | ||
2708 | myhostid = zone_get_hostid(NULL); | |
2709 | ||
2710 | if (hostid != 0 && myhostid != 0 && hostid != myhostid) { | |
2711 | cmn_err(CE_WARN, "pool '%s' could not be " | |
2712 | "loaded as it was last accessed by " | |
2713 | "another system (host: %s hostid: 0x%llx). " | |
2714 | "See: http://illumos.org/msg/ZFS-8000-EY", | |
2715 | spa_name(spa), hostname, (u_longlong_t)hostid); | |
2716 | spa_load_failed(spa, "hostid verification failed: pool " | |
2717 | "last accessed by host: %s (hostid: 0x%llx)", | |
2718 | hostname, (u_longlong_t)hostid); | |
2719 | return (SET_ERROR(EBADF)); | |
2720 | } | |
2721 | } | |
2722 | ||
2723 | return (0); | |
2724 | } | |
2725 | ||
2726 | static int | |
2727 | spa_ld_parse_config(spa_t *spa, spa_import_type_t type) | |
428870ff BB |
2728 | { |
2729 | int error = 0; | |
6cb8e530 | 2730 | nvlist_t *nvtree, *nvl, *config = spa->spa_config; |
1c27024e | 2731 | int parse; |
9eb7b46e | 2732 | vdev_t *rvd; |
6cb8e530 PZ |
2733 | uint64_t pool_guid; |
2734 | char *comment; | |
2735 | ||
2736 | /* | |
2737 | * Versioning wasn't explicitly added to the label until later, so if | |
2738 | * it's not present treat it as the initial version. | |
2739 | */ | |
2740 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
2741 | &spa->spa_ubsync.ub_version) != 0) | |
2742 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
2743 | ||
2744 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) { | |
2745 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
2746 | ZPOOL_CONFIG_POOL_GUID); | |
2747 | return (SET_ERROR(EINVAL)); | |
2748 | } | |
2749 | ||
d2734cce SD |
2750 | /* |
2751 | * If we are doing an import, ensure that the pool is not already | |
2752 | * imported by checking if its pool guid already exists in the | |
2753 | * spa namespace. | |
2754 | * | |
2755 | * The only case that we allow an already imported pool to be | |
2756 | * imported again, is when the pool is checkpointed and we want to | |
2757 | * look at its checkpointed state from userland tools like zdb. | |
2758 | */ | |
2759 | #ifdef _KERNEL | |
2760 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
2761 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
2762 | spa_guid_exists(pool_guid, 0)) { | |
2763 | #else | |
2764 | if ((spa->spa_load_state == SPA_LOAD_IMPORT || | |
2765 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) && | |
2766 | spa_guid_exists(pool_guid, 0) && | |
2767 | !spa_importing_readonly_checkpoint(spa)) { | |
2768 | #endif | |
6cb8e530 PZ |
2769 | spa_load_failed(spa, "a pool with guid %llu is already open", |
2770 | (u_longlong_t)pool_guid); | |
2771 | return (SET_ERROR(EEXIST)); | |
2772 | } | |
2773 | ||
2774 | spa->spa_config_guid = pool_guid; | |
2775 | ||
2776 | nvlist_free(spa->spa_load_info); | |
2777 | spa->spa_load_info = fnvlist_alloc(); | |
2778 | ||
2779 | ASSERT(spa->spa_comment == NULL); | |
2780 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
2781 | spa->spa_comment = spa_strdup(comment); | |
2782 | ||
2783 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
2784 | &spa->spa_config_txg); | |
2785 | ||
2786 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) == 0) | |
2787 | spa->spa_config_splitting = fnvlist_dup(nvl); | |
428870ff | 2788 | |
4a0ee12a PZ |
2789 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtree)) { |
2790 | spa_load_failed(spa, "invalid config provided: '%s' missing", | |
2791 | ZPOOL_CONFIG_VDEV_TREE); | |
2e528b49 | 2792 | return (SET_ERROR(EINVAL)); |
4a0ee12a | 2793 | } |
428870ff | 2794 | |
428870ff BB |
2795 | /* |
2796 | * Create "The Godfather" zio to hold all async IOs | |
2797 | */ | |
e022864d MA |
2798 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
2799 | KM_SLEEP); | |
1c27024e | 2800 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
2801 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
2802 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
2803 | ZIO_FLAG_GODFATHER); | |
2804 | } | |
428870ff BB |
2805 | |
2806 | /* | |
2807 | * Parse the configuration into a vdev tree. We explicitly set the | |
2808 | * value that will be returned by spa_version() since parsing the | |
2809 | * configuration requires knowing the version number. | |
2810 | */ | |
2811 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 PZ |
2812 | parse = (type == SPA_IMPORT_EXISTING ? |
2813 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
9eb7b46e | 2814 | error = spa_config_parse(spa, &rvd, nvtree, NULL, 0, parse); |
428870ff BB |
2815 | spa_config_exit(spa, SCL_ALL, FTAG); |
2816 | ||
4a0ee12a PZ |
2817 | if (error != 0) { |
2818 | spa_load_failed(spa, "unable to parse config [error=%d]", | |
2819 | error); | |
428870ff | 2820 | return (error); |
4a0ee12a | 2821 | } |
428870ff BB |
2822 | |
2823 | ASSERT(spa->spa_root_vdev == rvd); | |
c3520e7f MA |
2824 | ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT); |
2825 | ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT); | |
428870ff BB |
2826 | |
2827 | if (type != SPA_IMPORT_ASSEMBLE) { | |
2828 | ASSERT(spa_guid(spa) == pool_guid); | |
2829 | } | |
2830 | ||
9eb7b46e PZ |
2831 | return (0); |
2832 | } | |
2833 | ||
6cb8e530 PZ |
2834 | /* |
2835 | * Recursively open all vdevs in the vdev tree. This function is called twice: | |
2836 | * first with the untrusted config, then with the trusted config. | |
2837 | */ | |
9eb7b46e PZ |
2838 | static int |
2839 | spa_ld_open_vdevs(spa_t *spa) | |
2840 | { | |
2841 | int error = 0; | |
2842 | ||
6cb8e530 PZ |
2843 | /* |
2844 | * spa_missing_tvds_allowed defines how many top-level vdevs can be | |
2845 | * missing/unopenable for the root vdev to be still considered openable. | |
2846 | */ | |
2847 | if (spa->spa_trust_config) { | |
2848 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds; | |
2849 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_CACHEFILE) { | |
2850 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_cachefile; | |
2851 | } else if (spa->spa_config_source == SPA_CONFIG_SRC_SCAN) { | |
2852 | spa->spa_missing_tvds_allowed = zfs_max_missing_tvds_scan; | |
2853 | } else { | |
2854 | spa->spa_missing_tvds_allowed = 0; | |
2855 | } | |
2856 | ||
2857 | spa->spa_missing_tvds_allowed = | |
2858 | MAX(zfs_max_missing_tvds, spa->spa_missing_tvds_allowed); | |
2859 | ||
428870ff | 2860 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
9eb7b46e | 2861 | error = vdev_open(spa->spa_root_vdev); |
428870ff | 2862 | spa_config_exit(spa, SCL_ALL, FTAG); |
6cb8e530 PZ |
2863 | |
2864 | if (spa->spa_missing_tvds != 0) { | |
2865 | spa_load_note(spa, "vdev tree has %lld missing top-level " | |
2866 | "vdevs.", (u_longlong_t)spa->spa_missing_tvds); | |
2867 | if (spa->spa_trust_config && (spa->spa_mode & FWRITE)) { | |
2868 | /* | |
2869 | * Although theoretically we could allow users to open | |
2870 | * incomplete pools in RW mode, we'd need to add a lot | |
2871 | * of extra logic (e.g. adjust pool space to account | |
2872 | * for missing vdevs). | |
2873 | * This limitation also prevents users from accidentally | |
2874 | * opening the pool in RW mode during data recovery and | |
2875 | * damaging it further. | |
2876 | */ | |
2877 | spa_load_note(spa, "pools with missing top-level " | |
2878 | "vdevs can only be opened in read-only mode."); | |
2879 | error = SET_ERROR(ENXIO); | |
2880 | } else { | |
2881 | spa_load_note(spa, "current settings allow for maximum " | |
2882 | "%lld missing top-level vdevs at this stage.", | |
2883 | (u_longlong_t)spa->spa_missing_tvds_allowed); | |
2884 | } | |
2885 | } | |
4a0ee12a PZ |
2886 | if (error != 0) { |
2887 | spa_load_failed(spa, "unable to open vdev tree [error=%d]", | |
2888 | error); | |
2889 | } | |
6cb8e530 PZ |
2890 | if (spa->spa_missing_tvds != 0 || error != 0) |
2891 | vdev_dbgmsg_print_tree(spa->spa_root_vdev, 2); | |
9eb7b46e PZ |
2892 | |
2893 | return (error); | |
2894 | } | |
2895 | ||
6cb8e530 PZ |
2896 | /* |
2897 | * We need to validate the vdev labels against the configuration that | |
2898 | * we have in hand. This function is called twice: first with an untrusted | |
2899 | * config, then with a trusted config. The validation is more strict when the | |
2900 | * config is trusted. | |
2901 | */ | |
9eb7b46e | 2902 | static int |
6cb8e530 | 2903 | spa_ld_validate_vdevs(spa_t *spa) |
9eb7b46e PZ |
2904 | { |
2905 | int error = 0; | |
2906 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 2907 | |
6cb8e530 PZ |
2908 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
2909 | error = vdev_validate(rvd); | |
2910 | spa_config_exit(spa, SCL_ALL, FTAG); | |
428870ff | 2911 | |
6cb8e530 PZ |
2912 | if (error != 0) { |
2913 | spa_load_failed(spa, "vdev_validate failed [error=%d]", error); | |
2914 | return (error); | |
2915 | } | |
428870ff | 2916 | |
6cb8e530 PZ |
2917 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) { |
2918 | spa_load_failed(spa, "cannot open vdev tree after invalidating " | |
2919 | "some vdevs"); | |
2920 | vdev_dbgmsg_print_tree(rvd, 2); | |
2921 | return (SET_ERROR(ENXIO)); | |
428870ff BB |
2922 | } |
2923 | ||
9eb7b46e PZ |
2924 | return (0); |
2925 | } | |
2926 | ||
d2734cce SD |
2927 | static void |
2928 | spa_ld_select_uberblock_done(spa_t *spa, uberblock_t *ub) | |
2929 | { | |
2930 | spa->spa_state = POOL_STATE_ACTIVE; | |
2931 | spa->spa_ubsync = spa->spa_uberblock; | |
2932 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
2933 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
2934 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
2935 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
2936 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
2937 | spa->spa_prev_software_version = ub->ub_software_version; | |
2938 | } | |
2939 | ||
9eb7b46e | 2940 | static int |
6cb8e530 | 2941 | spa_ld_select_uberblock(spa_t *spa, spa_import_type_t type) |
9eb7b46e PZ |
2942 | { |
2943 | vdev_t *rvd = spa->spa_root_vdev; | |
2944 | nvlist_t *label; | |
2945 | uberblock_t *ub = &spa->spa_uberblock; | |
9eb7b46e PZ |
2946 | boolean_t activity_check = B_FALSE; |
2947 | ||
d2734cce SD |
2948 | /* |
2949 | * If we are opening the checkpointed state of the pool by | |
2950 | * rewinding to it, at this point we will have written the | |
2951 | * checkpointed uberblock to the vdev labels, so searching | |
2952 | * the labels will find the right uberblock. However, if | |
2953 | * we are opening the checkpointed state read-only, we have | |
2954 | * not modified the labels. Therefore, we must ignore the | |
2955 | * labels and continue using the spa_uberblock that was set | |
2956 | * by spa_ld_checkpoint_rewind. | |
2957 | * | |
2958 | * Note that it would be fine to ignore the labels when | |
2959 | * rewinding (opening writeable) as well. However, if we | |
2960 | * crash just after writing the labels, we will end up | |
2961 | * searching the labels. Doing so in the common case means | |
2962 | * that this code path gets exercised normally, rather than | |
2963 | * just in the edge case. | |
2964 | */ | |
2965 | if (ub->ub_checkpoint_txg != 0 && | |
2966 | spa_importing_readonly_checkpoint(spa)) { | |
2967 | spa_ld_select_uberblock_done(spa, ub); | |
2968 | return (0); | |
2969 | } | |
2970 | ||
428870ff BB |
2971 | /* |
2972 | * Find the best uberblock. | |
2973 | */ | |
9ae529ec | 2974 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
2975 | |
2976 | /* | |
2977 | * If we weren't able to find a single valid uberblock, return failure. | |
2978 | */ | |
9ae529ec CS |
2979 | if (ub->ub_txg == 0) { |
2980 | nvlist_free(label); | |
4a0ee12a | 2981 | spa_load_failed(spa, "no valid uberblock found"); |
428870ff | 2982 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 2983 | } |
428870ff | 2984 | |
4a0ee12a PZ |
2985 | spa_load_note(spa, "using uberblock with txg=%llu", |
2986 | (u_longlong_t)ub->ub_txg); | |
2987 | ||
2988 | ||
379ca9cf OF |
2989 | /* |
2990 | * For pools which have the multihost property on determine if the | |
2991 | * pool is truly inactive and can be safely imported. Prevent | |
2992 | * hosts which don't have a hostid set from importing the pool. | |
2993 | */ | |
6cb8e530 PZ |
2994 | activity_check = spa_activity_check_required(spa, ub, label, |
2995 | spa->spa_config); | |
379ca9cf | 2996 | if (activity_check) { |
379ca9cf OF |
2997 | if (ub->ub_mmp_magic == MMP_MAGIC && ub->ub_mmp_delay && |
2998 | spa_get_hostid() == 0) { | |
2999 | nvlist_free(label); | |
3000 | fnvlist_add_uint64(spa->spa_load_info, | |
3001 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3002 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3003 | } | |
3004 | ||
6cb8e530 | 3005 | int error = spa_activity_check(spa, ub, spa->spa_config); |
e889f0f5 OF |
3006 | if (error) { |
3007 | nvlist_free(label); | |
3008 | return (error); | |
3009 | } | |
3010 | ||
379ca9cf OF |
3011 | fnvlist_add_uint64(spa->spa_load_info, |
3012 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_INACTIVE); | |
3013 | fnvlist_add_uint64(spa->spa_load_info, | |
3014 | ZPOOL_CONFIG_MMP_TXG, ub->ub_txg); | |
060f0226 OF |
3015 | fnvlist_add_uint16(spa->spa_load_info, |
3016 | ZPOOL_CONFIG_MMP_SEQ, | |
3017 | (MMP_SEQ_VALID(ub) ? MMP_SEQ(ub) : 0)); | |
379ca9cf OF |
3018 | } |
3019 | ||
428870ff | 3020 | /* |
9ae529ec | 3021 | * If the pool has an unsupported version we can't open it. |
428870ff | 3022 | */ |
9ae529ec CS |
3023 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
3024 | nvlist_free(label); | |
4a0ee12a PZ |
3025 | spa_load_failed(spa, "version %llu is not supported", |
3026 | (u_longlong_t)ub->ub_version); | |
428870ff | 3027 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
3028 | } |
3029 | ||
3030 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3031 | nvlist_t *features; | |
3032 | ||
3033 | /* | |
3034 | * If we weren't able to find what's necessary for reading the | |
3035 | * MOS in the label, return failure. | |
3036 | */ | |
4a0ee12a PZ |
3037 | if (label == NULL) { |
3038 | spa_load_failed(spa, "label config unavailable"); | |
3039 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
3040 | ENXIO)); | |
3041 | } | |
3042 | ||
3043 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_FEATURES_FOR_READ, | |
3044 | &features) != 0) { | |
9ae529ec | 3045 | nvlist_free(label); |
4a0ee12a PZ |
3046 | spa_load_failed(spa, "invalid label: '%s' missing", |
3047 | ZPOOL_CONFIG_FEATURES_FOR_READ); | |
9ae529ec CS |
3048 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3049 | ENXIO)); | |
3050 | } | |
3051 | ||
3052 | /* | |
3053 | * Update our in-core representation with the definitive values | |
3054 | * from the label. | |
3055 | */ | |
3056 | nvlist_free(spa->spa_label_features); | |
3057 | VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); | |
3058 | } | |
3059 | ||
3060 | nvlist_free(label); | |
3061 | ||
3062 | /* | |
3063 | * Look through entries in the label nvlist's features_for_read. If | |
3064 | * there is a feature listed there which we don't understand then we | |
3065 | * cannot open a pool. | |
3066 | */ | |
3067 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
3068 | nvlist_t *unsup_feat; | |
9ae529ec CS |
3069 | |
3070 | VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == | |
3071 | 0); | |
3072 | ||
1c27024e DB |
3073 | for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features, |
3074 | NULL); nvp != NULL; | |
9ae529ec CS |
3075 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { |
3076 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
3077 | VERIFY(nvlist_add_string(unsup_feat, | |
3078 | nvpair_name(nvp), "") == 0); | |
3079 | } | |
3080 | } | |
3081 | ||
3082 | if (!nvlist_empty(unsup_feat)) { | |
3083 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
3084 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); | |
3085 | nvlist_free(unsup_feat); | |
4a0ee12a | 3086 | spa_load_failed(spa, "some features are unsupported"); |
9ae529ec CS |
3087 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3088 | ENOTSUP)); | |
3089 | } | |
3090 | ||
3091 | nvlist_free(unsup_feat); | |
3092 | } | |
428870ff | 3093 | |
428870ff BB |
3094 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { |
3095 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
6cb8e530 | 3096 | spa_try_repair(spa, spa->spa_config); |
428870ff BB |
3097 | spa_config_exit(spa, SCL_ALL, FTAG); |
3098 | nvlist_free(spa->spa_config_splitting); | |
3099 | spa->spa_config_splitting = NULL; | |
3100 | } | |
3101 | ||
3102 | /* | |
3103 | * Initialize internal SPA structures. | |
3104 | */ | |
d2734cce | 3105 | spa_ld_select_uberblock_done(spa, ub); |
428870ff | 3106 | |
9eb7b46e PZ |
3107 | return (0); |
3108 | } | |
3109 | ||
3110 | static int | |
3111 | spa_ld_open_rootbp(spa_t *spa) | |
3112 | { | |
3113 | int error = 0; | |
3114 | vdev_t *rvd = spa->spa_root_vdev; | |
a1d477c2 | 3115 | |
9ae529ec | 3116 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
4a0ee12a PZ |
3117 | if (error != 0) { |
3118 | spa_load_failed(spa, "unable to open rootbp in dsl_pool_init " | |
3119 | "[error=%d]", error); | |
428870ff | 3120 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3121 | } |
428870ff BB |
3122 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; |
3123 | ||
9eb7b46e PZ |
3124 | return (0); |
3125 | } | |
3126 | ||
3127 | static int | |
d2734cce | 3128 | spa_ld_trusted_config(spa_t *spa, spa_import_type_t type, |
6cb8e530 | 3129 | boolean_t reloading) |
9eb7b46e | 3130 | { |
6cb8e530 PZ |
3131 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
3132 | nvlist_t *nv, *mos_config, *policy; | |
3133 | int error = 0, copy_error; | |
3134 | uint64_t healthy_tvds, healthy_tvds_mos; | |
3135 | uint64_t mos_config_txg; | |
9eb7b46e | 3136 | |
4a0ee12a PZ |
3137 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object, B_TRUE) |
3138 | != 0) | |
428870ff BB |
3139 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3140 | ||
a1d477c2 | 3141 | /* |
6cb8e530 PZ |
3142 | * If we're assembling a pool from a split, the config provided is |
3143 | * already trusted so there is nothing to do. | |
a1d477c2 | 3144 | */ |
6cb8e530 PZ |
3145 | if (type == SPA_IMPORT_ASSEMBLE) |
3146 | return (0); | |
3147 | ||
3148 | healthy_tvds = spa_healthy_core_tvds(spa); | |
a1d477c2 | 3149 | |
6cb8e530 PZ |
3150 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) |
3151 | != 0) { | |
3152 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
3153 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3154 | } | |
3155 | ||
3156 | /* | |
3157 | * If we are doing an open, pool owner wasn't verified yet, thus do | |
3158 | * the verification here. | |
3159 | */ | |
3160 | if (spa->spa_load_state == SPA_LOAD_OPEN) { | |
3161 | error = spa_verify_host(spa, mos_config); | |
3162 | if (error != 0) { | |
a1d477c2 | 3163 | nvlist_free(mos_config); |
6cb8e530 | 3164 | return (error); |
a1d477c2 | 3165 | } |
6cb8e530 PZ |
3166 | } |
3167 | ||
3168 | nv = fnvlist_lookup_nvlist(mos_config, ZPOOL_CONFIG_VDEV_TREE); | |
a1d477c2 | 3169 | |
6cb8e530 PZ |
3170 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
3171 | ||
3172 | /* | |
3173 | * Build a new vdev tree from the trusted config | |
3174 | */ | |
3175 | VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); | |
3176 | ||
3177 | /* | |
3178 | * Vdev paths in the MOS may be obsolete. If the untrusted config was | |
3179 | * obtained by scanning /dev/dsk, then it will have the right vdev | |
3180 | * paths. We update the trusted MOS config with this information. | |
3181 | * We first try to copy the paths with vdev_copy_path_strict, which | |
3182 | * succeeds only when both configs have exactly the same vdev tree. | |
3183 | * If that fails, we fall back to a more flexible method that has a | |
3184 | * best effort policy. | |
3185 | */ | |
3186 | copy_error = vdev_copy_path_strict(rvd, mrvd); | |
3187 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3188 | spa_load_note(spa, "provided vdev tree:"); | |
3189 | vdev_dbgmsg_print_tree(rvd, 2); | |
3190 | spa_load_note(spa, "MOS vdev tree:"); | |
3191 | vdev_dbgmsg_print_tree(mrvd, 2); | |
3192 | } | |
3193 | if (copy_error != 0) { | |
3194 | spa_load_note(spa, "vdev_copy_path_strict failed, falling " | |
3195 | "back to vdev_copy_path_relaxed"); | |
3196 | vdev_copy_path_relaxed(rvd, mrvd); | |
3197 | } | |
3198 | ||
3199 | vdev_close(rvd); | |
3200 | vdev_free(rvd); | |
3201 | spa->spa_root_vdev = mrvd; | |
3202 | rvd = mrvd; | |
3203 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3204 | ||
3205 | /* | |
3206 | * We will use spa_config if we decide to reload the spa or if spa_load | |
3207 | * fails and we rewind. We must thus regenerate the config using the | |
8a393be3 PZ |
3208 | * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to |
3209 | * pass settings on how to load the pool and is not stored in the MOS. | |
3210 | * We copy it over to our new, trusted config. | |
6cb8e530 PZ |
3211 | */ |
3212 | mos_config_txg = fnvlist_lookup_uint64(mos_config, | |
3213 | ZPOOL_CONFIG_POOL_TXG); | |
3214 | nvlist_free(mos_config); | |
3215 | mos_config = spa_config_generate(spa, NULL, mos_config_txg, B_FALSE); | |
8a393be3 | 3216 | if (nvlist_lookup_nvlist(spa->spa_config, ZPOOL_LOAD_POLICY, |
6cb8e530 | 3217 | &policy) == 0) |
8a393be3 | 3218 | fnvlist_add_nvlist(mos_config, ZPOOL_LOAD_POLICY, policy); |
6cb8e530 PZ |
3219 | spa_config_set(spa, mos_config); |
3220 | spa->spa_config_source = SPA_CONFIG_SRC_MOS; | |
3221 | ||
3222 | /* | |
3223 | * Now that we got the config from the MOS, we should be more strict | |
3224 | * in checking blkptrs and can make assumptions about the consistency | |
3225 | * of the vdev tree. spa_trust_config must be set to true before opening | |
3226 | * vdevs in order for them to be writeable. | |
3227 | */ | |
3228 | spa->spa_trust_config = B_TRUE; | |
3229 | ||
3230 | /* | |
3231 | * Open and validate the new vdev tree | |
3232 | */ | |
3233 | error = spa_ld_open_vdevs(spa); | |
3234 | if (error != 0) | |
3235 | return (error); | |
3236 | ||
3237 | error = spa_ld_validate_vdevs(spa); | |
3238 | if (error != 0) | |
3239 | return (error); | |
3240 | ||
3241 | if (copy_error != 0 || spa_load_print_vdev_tree) { | |
3242 | spa_load_note(spa, "final vdev tree:"); | |
3243 | vdev_dbgmsg_print_tree(rvd, 2); | |
3244 | } | |
3245 | ||
3246 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT && | |
3247 | !spa->spa_extreme_rewind && zfs_max_missing_tvds == 0) { | |
a1d477c2 | 3248 | /* |
6cb8e530 PZ |
3249 | * Sanity check to make sure that we are indeed loading the |
3250 | * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds | |
3251 | * in the config provided and they happened to be the only ones | |
3252 | * to have the latest uberblock, we could involuntarily perform | |
3253 | * an extreme rewind. | |
a1d477c2 | 3254 | */ |
6cb8e530 PZ |
3255 | healthy_tvds_mos = spa_healthy_core_tvds(spa); |
3256 | if (healthy_tvds_mos - healthy_tvds >= | |
3257 | SPA_SYNC_MIN_VDEVS) { | |
3258 | spa_load_note(spa, "config provided misses too many " | |
3259 | "top-level vdevs compared to MOS (%lld vs %lld). ", | |
3260 | (u_longlong_t)healthy_tvds, | |
3261 | (u_longlong_t)healthy_tvds_mos); | |
3262 | spa_load_note(spa, "vdev tree:"); | |
3263 | vdev_dbgmsg_print_tree(rvd, 2); | |
3264 | if (reloading) { | |
3265 | spa_load_failed(spa, "config was already " | |
3266 | "provided from MOS. Aborting."); | |
3267 | return (spa_vdev_err(rvd, | |
3268 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3269 | } | |
3270 | spa_load_note(spa, "spa must be reloaded using MOS " | |
3271 | "config"); | |
3272 | return (SET_ERROR(EAGAIN)); | |
4a0ee12a | 3273 | } |
a1d477c2 MA |
3274 | } |
3275 | ||
6cb8e530 PZ |
3276 | error = spa_check_for_missing_logs(spa); |
3277 | if (error != 0) | |
3278 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
3279 | ||
3280 | if (rvd->vdev_guid_sum != spa->spa_uberblock.ub_guid_sum) { | |
3281 | spa_load_failed(spa, "uberblock guid sum doesn't match MOS " | |
3282 | "guid sum (%llu != %llu)", | |
3283 | (u_longlong_t)spa->spa_uberblock.ub_guid_sum, | |
3284 | (u_longlong_t)rvd->vdev_guid_sum); | |
3285 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
3286 | ENXIO)); | |
3287 | } | |
3288 | ||
9eb7b46e PZ |
3289 | return (0); |
3290 | } | |
3291 | ||
3292 | static int | |
3293 | spa_ld_open_indirect_vdev_metadata(spa_t *spa) | |
3294 | { | |
3295 | int error = 0; | |
3296 | vdev_t *rvd = spa->spa_root_vdev; | |
3297 | ||
a1d477c2 MA |
3298 | /* |
3299 | * Everything that we read before spa_remove_init() must be stored | |
3300 | * on concreted vdevs. Therefore we do this as early as possible. | |
3301 | */ | |
4a0ee12a PZ |
3302 | error = spa_remove_init(spa); |
3303 | if (error != 0) { | |
3304 | spa_load_failed(spa, "spa_remove_init failed [error=%d]", | |
3305 | error); | |
a1d477c2 | 3306 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3307 | } |
a1d477c2 | 3308 | |
9eb7b46e PZ |
3309 | /* |
3310 | * Retrieve information needed to condense indirect vdev mappings. | |
3311 | */ | |
3312 | error = spa_condense_init(spa); | |
3313 | if (error != 0) { | |
4a0ee12a PZ |
3314 | spa_load_failed(spa, "spa_condense_init failed [error=%d]", |
3315 | error); | |
9eb7b46e PZ |
3316 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3317 | } | |
3318 | ||
3319 | return (0); | |
3320 | } | |
3321 | ||
3322 | static int | |
4a0ee12a | 3323 | spa_ld_check_features(spa_t *spa, boolean_t *missing_feat_writep) |
9eb7b46e PZ |
3324 | { |
3325 | int error = 0; | |
3326 | vdev_t *rvd = spa->spa_root_vdev; | |
3327 | ||
9ae529ec CS |
3328 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
3329 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 3330 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
3331 | |
3332 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
4a0ee12a | 3333 | &spa->spa_feat_for_read_obj, B_TRUE) != 0) { |
9ae529ec CS |
3334 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3335 | } | |
3336 | ||
3337 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
4a0ee12a | 3338 | &spa->spa_feat_for_write_obj, B_TRUE) != 0) { |
9ae529ec CS |
3339 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3340 | } | |
3341 | ||
3342 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
4a0ee12a | 3343 | &spa->spa_feat_desc_obj, B_TRUE) != 0) { |
9ae529ec CS |
3344 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3345 | } | |
3346 | ||
b9b24bb4 CS |
3347 | enabled_feat = fnvlist_alloc(); |
3348 | unsup_feat = fnvlist_alloc(); | |
9ae529ec | 3349 | |
fa86b5db | 3350 | if (!spa_features_check(spa, B_FALSE, |
b9b24bb4 | 3351 | unsup_feat, enabled_feat)) |
9ae529ec CS |
3352 | missing_feat_read = B_TRUE; |
3353 | ||
4a0ee12a PZ |
3354 | if (spa_writeable(spa) || |
3355 | spa->spa_load_state == SPA_LOAD_TRYIMPORT) { | |
fa86b5db | 3356 | if (!spa_features_check(spa, B_TRUE, |
b9b24bb4 | 3357 | unsup_feat, enabled_feat)) { |
9eb7b46e | 3358 | *missing_feat_writep = B_TRUE; |
b9b24bb4 | 3359 | } |
9ae529ec CS |
3360 | } |
3361 | ||
b9b24bb4 CS |
3362 | fnvlist_add_nvlist(spa->spa_load_info, |
3363 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
3364 | ||
9ae529ec | 3365 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
3366 | fnvlist_add_nvlist(spa->spa_load_info, |
3367 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
3368 | } |
3369 | ||
b9b24bb4 CS |
3370 | fnvlist_free(enabled_feat); |
3371 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
3372 | |
3373 | if (!missing_feat_read) { | |
3374 | fnvlist_add_boolean(spa->spa_load_info, | |
3375 | ZPOOL_CONFIG_CAN_RDONLY); | |
3376 | } | |
3377 | ||
3378 | /* | |
3379 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
3380 | * twofold: to determine whether the pool is available for | |
3381 | * import in read-write mode and (if it is not) whether the | |
3382 | * pool is available for import in read-only mode. If the pool | |
3383 | * is available for import in read-write mode, it is displayed | |
3384 | * as available in userland; if it is not available for import | |
3385 | * in read-only mode, it is displayed as unavailable in | |
3386 | * userland. If the pool is available for import in read-only | |
3387 | * mode but not read-write mode, it is displayed as unavailable | |
3388 | * in userland with a special note that the pool is actually | |
3389 | * available for open in read-only mode. | |
3390 | * | |
3391 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
3392 | * missing a feature for write, we must first determine whether | |
3393 | * the pool can be opened read-only before returning to | |
3394 | * userland in order to know whether to display the | |
3395 | * abovementioned note. | |
3396 | */ | |
9eb7b46e | 3397 | if (missing_feat_read || (*missing_feat_writep && |
9ae529ec | 3398 | spa_writeable(spa))) { |
4a0ee12a | 3399 | spa_load_failed(spa, "pool uses unsupported features"); |
9ae529ec CS |
3400 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, |
3401 | ENOTSUP)); | |
3402 | } | |
b0bc7a84 MG |
3403 | |
3404 | /* | |
3405 | * Load refcounts for ZFS features from disk into an in-memory | |
3406 | * cache during SPA initialization. | |
3407 | */ | |
1c27024e | 3408 | for (spa_feature_t i = 0; i < SPA_FEATURES; i++) { |
b0bc7a84 MG |
3409 | uint64_t refcount; |
3410 | ||
3411 | error = feature_get_refcount_from_disk(spa, | |
3412 | &spa_feature_table[i], &refcount); | |
3413 | if (error == 0) { | |
3414 | spa->spa_feat_refcount_cache[i] = refcount; | |
3415 | } else if (error == ENOTSUP) { | |
3416 | spa->spa_feat_refcount_cache[i] = | |
3417 | SPA_FEATURE_DISABLED; | |
3418 | } else { | |
4a0ee12a PZ |
3419 | spa_load_failed(spa, "error getting refcount " |
3420 | "for feature %s [error=%d]", | |
3421 | spa_feature_table[i].fi_guid, error); | |
b0bc7a84 MG |
3422 | return (spa_vdev_err(rvd, |
3423 | VDEV_AUX_CORRUPT_DATA, EIO)); | |
3424 | } | |
3425 | } | |
3426 | } | |
3427 | ||
3428 | if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) { | |
3429 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG, | |
4a0ee12a | 3430 | &spa->spa_feat_enabled_txg_obj, B_TRUE) != 0) |
b0bc7a84 | 3431 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
9ae529ec CS |
3432 | } |
3433 | ||
f00ab3f2 TC |
3434 | /* |
3435 | * Encryption was added before bookmark_v2, even though bookmark_v2 | |
3436 | * is now a dependency. If this pool has encryption enabled without | |
3437 | * bookmark_v2, trigger an errata message. | |
3438 | */ | |
3439 | if (spa_feature_is_enabled(spa, SPA_FEATURE_ENCRYPTION) && | |
3440 | !spa_feature_is_enabled(spa, SPA_FEATURE_BOOKMARK_V2)) { | |
3441 | spa->spa_errata = ZPOOL_ERRATA_ZOL_8308_ENCRYPTION; | |
3442 | } | |
3443 | ||
9eb7b46e PZ |
3444 | return (0); |
3445 | } | |
3446 | ||
3447 | static int | |
3448 | spa_ld_load_special_directories(spa_t *spa) | |
3449 | { | |
3450 | int error = 0; | |
3451 | vdev_t *rvd = spa->spa_root_vdev; | |
3452 | ||
9ae529ec CS |
3453 | spa->spa_is_initializing = B_TRUE; |
3454 | error = dsl_pool_open(spa->spa_dsl_pool); | |
3455 | spa->spa_is_initializing = B_FALSE; | |
4a0ee12a PZ |
3456 | if (error != 0) { |
3457 | spa_load_failed(spa, "dsl_pool_open failed [error=%d]", error); | |
9ae529ec | 3458 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3459 | } |
9ae529ec | 3460 | |
9eb7b46e PZ |
3461 | return (0); |
3462 | } | |
428870ff | 3463 | |
9eb7b46e PZ |
3464 | static int |
3465 | spa_ld_get_props(spa_t *spa) | |
3466 | { | |
3467 | int error = 0; | |
3468 | uint64_t obj; | |
3469 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 3470 | |
3c67d83a TH |
3471 | /* Grab the checksum salt from the MOS. */ |
3472 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3473 | DMU_POOL_CHECKSUM_SALT, 1, | |
3474 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
3475 | spa->spa_cksum_salt.zcs_bytes); | |
3476 | if (error == ENOENT) { | |
3477 | /* Generate a new salt for subsequent use */ | |
3478 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
3479 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
3480 | } else if (error != 0) { | |
4a0ee12a PZ |
3481 | spa_load_failed(spa, "unable to retrieve checksum salt from " |
3482 | "MOS [error=%d]", error); | |
3c67d83a TH |
3483 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3484 | } | |
3485 | ||
4a0ee12a | 3486 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj, B_TRUE) != 0) |
428870ff BB |
3487 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
3488 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
4a0ee12a PZ |
3489 | if (error != 0) { |
3490 | spa_load_failed(spa, "error opening deferred-frees bpobj " | |
3491 | "[error=%d]", error); | |
428870ff | 3492 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3493 | } |
34dc7c2f BB |
3494 | |
3495 | /* | |
3496 | * Load the bit that tells us to use the new accounting function | |
3497 | * (raid-z deflation). If we have an older pool, this will not | |
3498 | * be present. | |
3499 | */ | |
4a0ee12a | 3500 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate, B_FALSE); |
428870ff BB |
3501 | if (error != 0 && error != ENOENT) |
3502 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3503 | ||
3504 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
4a0ee12a | 3505 | &spa->spa_creation_version, B_FALSE); |
428870ff BB |
3506 | if (error != 0 && error != ENOENT) |
3507 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
3508 | |
3509 | /* | |
3510 | * Load the persistent error log. If we have an older pool, this will | |
3511 | * not be present. | |
3512 | */ | |
4a0ee12a PZ |
3513 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last, |
3514 | B_FALSE); | |
428870ff BB |
3515 | if (error != 0 && error != ENOENT) |
3516 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 3517 | |
428870ff | 3518 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
4a0ee12a | 3519 | &spa->spa_errlog_scrub, B_FALSE); |
428870ff BB |
3520 | if (error != 0 && error != ENOENT) |
3521 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
3522 | |
3523 | /* | |
3524 | * Load the history object. If we have an older pool, this | |
3525 | * will not be present. | |
3526 | */ | |
4a0ee12a | 3527 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history, B_FALSE); |
428870ff BB |
3528 | if (error != 0 && error != ENOENT) |
3529 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3530 | ||
e0ab3ab5 JS |
3531 | /* |
3532 | * Load the per-vdev ZAP map. If we have an older pool, this will not | |
3533 | * be present; in this case, defer its creation to a later time to | |
3534 | * avoid dirtying the MOS this early / out of sync context. See | |
3535 | * spa_sync_config_object. | |
3536 | */ | |
3537 | ||
3538 | /* The sentinel is only available in the MOS config. */ | |
1c27024e | 3539 | nvlist_t *mos_config; |
4a0ee12a PZ |
3540 | if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0) { |
3541 | spa_load_failed(spa, "unable to retrieve MOS config"); | |
e0ab3ab5 | 3542 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3543 | } |
e0ab3ab5 JS |
3544 | |
3545 | error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP, | |
4a0ee12a | 3546 | &spa->spa_all_vdev_zaps, B_FALSE); |
e0ab3ab5 | 3547 | |
38640550 DB |
3548 | if (error == ENOENT) { |
3549 | VERIFY(!nvlist_exists(mos_config, | |
3550 | ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); | |
3551 | spa->spa_avz_action = AVZ_ACTION_INITIALIZE; | |
3552 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
3553 | } else if (error != 0) { | |
e0ab3ab5 | 3554 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
38640550 | 3555 | } else if (!nvlist_exists(mos_config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) { |
e0ab3ab5 JS |
3556 | /* |
3557 | * An older version of ZFS overwrote the sentinel value, so | |
3558 | * we have orphaned per-vdev ZAPs in the MOS. Defer their | |
3559 | * destruction to later; see spa_sync_config_object. | |
3560 | */ | |
3561 | spa->spa_avz_action = AVZ_ACTION_DESTROY; | |
3562 | /* | |
3563 | * We're assuming that no vdevs have had their ZAPs created | |
3564 | * before this. Better be sure of it. | |
3565 | */ | |
3566 | ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev)); | |
3567 | } | |
3568 | nvlist_free(mos_config); | |
3569 | ||
9eb7b46e PZ |
3570 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
3571 | ||
4a0ee12a PZ |
3572 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object, |
3573 | B_FALSE); | |
9eb7b46e PZ |
3574 | if (error && error != ENOENT) |
3575 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3576 | ||
3577 | if (error == 0) { | |
3578 | uint64_t autoreplace; | |
3579 | ||
3580 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
3581 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
3582 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
3583 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
3584 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
c02c1bec | 3585 | spa_prop_find(spa, ZPOOL_PROP_MULTIHOST, &spa->spa_multihost); |
9eb7b46e PZ |
3586 | spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, |
3587 | &spa->spa_dedup_ditto); | |
3588 | ||
3589 | spa->spa_autoreplace = (autoreplace != 0); | |
3590 | } | |
3591 | ||
6cb8e530 PZ |
3592 | /* |
3593 | * If we are importing a pool with missing top-level vdevs, | |
3594 | * we enforce that the pool doesn't panic or get suspended on | |
3595 | * error since the likelihood of missing data is extremely high. | |
3596 | */ | |
3597 | if (spa->spa_missing_tvds > 0 && | |
3598 | spa->spa_failmode != ZIO_FAILURE_MODE_CONTINUE && | |
3599 | spa->spa_load_state != SPA_LOAD_TRYIMPORT) { | |
3600 | spa_load_note(spa, "forcing failmode to 'continue' " | |
3601 | "as some top level vdevs are missing"); | |
3602 | spa->spa_failmode = ZIO_FAILURE_MODE_CONTINUE; | |
3603 | } | |
3604 | ||
9eb7b46e PZ |
3605 | return (0); |
3606 | } | |
3607 | ||
3608 | static int | |
3609 | spa_ld_open_aux_vdevs(spa_t *spa, spa_import_type_t type) | |
3610 | { | |
3611 | int error = 0; | |
3612 | vdev_t *rvd = spa->spa_root_vdev; | |
3613 | ||
428870ff BB |
3614 | /* |
3615 | * If we're assembling the pool from the split-off vdevs of | |
3616 | * an existing pool, we don't want to attach the spares & cache | |
3617 | * devices. | |
3618 | */ | |
34dc7c2f BB |
3619 | |
3620 | /* | |
3621 | * Load any hot spares for this pool. | |
3622 | */ | |
4a0ee12a PZ |
3623 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object, |
3624 | B_FALSE); | |
428870ff BB |
3625 | if (error != 0 && error != ENOENT) |
3626 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3627 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
3628 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
3629 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
4a0ee12a PZ |
3630 | &spa->spa_spares.sav_config) != 0) { |
3631 | spa_load_failed(spa, "error loading spares nvlist"); | |
428870ff | 3632 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3633 | } |
34dc7c2f | 3634 | |
b128c09f | 3635 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3636 | spa_load_spares(spa); |
b128c09f | 3637 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
3638 | } else if (error == 0) { |
3639 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
3640 | } |
3641 | ||
3642 | /* | |
3643 | * Load any level 2 ARC devices for this pool. | |
3644 | */ | |
428870ff | 3645 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
4a0ee12a | 3646 | &spa->spa_l2cache.sav_object, B_FALSE); |
428870ff BB |
3647 | if (error != 0 && error != ENOENT) |
3648 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
3649 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
3650 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
3651 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
4a0ee12a PZ |
3652 | &spa->spa_l2cache.sav_config) != 0) { |
3653 | spa_load_failed(spa, "error loading l2cache nvlist"); | |
428870ff | 3654 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3655 | } |
34dc7c2f | 3656 | |
b128c09f | 3657 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3658 | spa_load_l2cache(spa); |
b128c09f | 3659 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
3660 | } else if (error == 0) { |
3661 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
3662 | } |
3663 | ||
9eb7b46e PZ |
3664 | return (0); |
3665 | } | |
428870ff | 3666 | |
9eb7b46e | 3667 | static int |
4a0ee12a | 3668 | spa_ld_load_vdev_metadata(spa_t *spa) |
9eb7b46e PZ |
3669 | { |
3670 | int error = 0; | |
3671 | vdev_t *rvd = spa->spa_root_vdev; | |
34dc7c2f | 3672 | |
379ca9cf OF |
3673 | /* |
3674 | * If the 'multihost' property is set, then never allow a pool to | |
3675 | * be imported when the system hostid is zero. The exception to | |
3676 | * this rule is zdb which is always allowed to access pools. | |
3677 | */ | |
3678 | if (spa_multihost(spa) && spa_get_hostid() == 0 && | |
3679 | (spa->spa_import_flags & ZFS_IMPORT_SKIP_MMP) == 0) { | |
3680 | fnvlist_add_uint64(spa->spa_load_info, | |
3681 | ZPOOL_CONFIG_MMP_STATE, MMP_STATE_NO_HOSTID); | |
3682 | return (spa_vdev_err(rvd, VDEV_AUX_ACTIVE, EREMOTEIO)); | |
3683 | } | |
3684 | ||
34dc7c2f BB |
3685 | /* |
3686 | * If the 'autoreplace' property is set, then post a resource notifying | |
3687 | * the ZFS DE that it should not issue any faults for unopenable | |
3688 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
3689 | * unopenable vdevs so that the normal autoreplace handler can take | |
3690 | * over. | |
3691 | */ | |
4a0ee12a | 3692 | if (spa->spa_autoreplace && spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 3693 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
3694 | /* |
3695 | * For the import case, this is done in spa_import(), because | |
3696 | * at this point we're using the spare definitions from | |
3697 | * the MOS config, not necessarily from the userland config. | |
3698 | */ | |
4a0ee12a | 3699 | if (spa->spa_load_state != SPA_LOAD_IMPORT) { |
428870ff BB |
3700 | spa_aux_check_removed(&spa->spa_spares); |
3701 | spa_aux_check_removed(&spa->spa_l2cache); | |
3702 | } | |
3703 | } | |
34dc7c2f BB |
3704 | |
3705 | /* | |
9eb7b46e | 3706 | * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc. |
34dc7c2f | 3707 | */ |
a1d477c2 MA |
3708 | error = vdev_load(rvd); |
3709 | if (error != 0) { | |
4a0ee12a | 3710 | spa_load_failed(spa, "vdev_load failed [error=%d]", error); |
a1d477c2 MA |
3711 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, error)); |
3712 | } | |
3713 | ||
34dc7c2f | 3714 | /* |
9eb7b46e | 3715 | * Propagate the leaf DTLs we just loaded all the way up the vdev tree. |
34dc7c2f | 3716 | */ |
b128c09f | 3717 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3718 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE); |
b128c09f | 3719 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 3720 | |
9eb7b46e PZ |
3721 | return (0); |
3722 | } | |
3723 | ||
3724 | static int | |
3725 | spa_ld_load_dedup_tables(spa_t *spa) | |
3726 | { | |
3727 | int error = 0; | |
3728 | vdev_t *rvd = spa->spa_root_vdev; | |
3729 | ||
428870ff | 3730 | error = ddt_load(spa); |
4a0ee12a PZ |
3731 | if (error != 0) { |
3732 | spa_load_failed(spa, "ddt_load failed [error=%d]", error); | |
428870ff | 3733 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); |
4a0ee12a | 3734 | } |
428870ff | 3735 | |
9eb7b46e PZ |
3736 | return (0); |
3737 | } | |
3738 | ||
3739 | static int | |
3740 | spa_ld_verify_logs(spa_t *spa, spa_import_type_t type, char **ereport) | |
3741 | { | |
3742 | vdev_t *rvd = spa->spa_root_vdev; | |
428870ff | 3743 | |
4a0ee12a PZ |
3744 | if (type != SPA_IMPORT_ASSEMBLE && spa_writeable(spa)) { |
3745 | boolean_t missing = spa_check_logs(spa); | |
3746 | if (missing) { | |
6cb8e530 PZ |
3747 | if (spa->spa_missing_tvds != 0) { |
3748 | spa_load_note(spa, "spa_check_logs failed " | |
3749 | "so dropping the logs"); | |
3750 | } else { | |
3751 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
3752 | spa_load_failed(spa, "spa_check_logs failed"); | |
3753 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, | |
3754 | ENXIO)); | |
3755 | } | |
4a0ee12a | 3756 | } |
428870ff BB |
3757 | } |
3758 | ||
9eb7b46e PZ |
3759 | return (0); |
3760 | } | |
3761 | ||
3762 | static int | |
4a0ee12a | 3763 | spa_ld_verify_pool_data(spa_t *spa) |
9eb7b46e PZ |
3764 | { |
3765 | int error = 0; | |
3766 | vdev_t *rvd = spa->spa_root_vdev; | |
3767 | ||
3768 | /* | |
3769 | * We've successfully opened the pool, verify that we're ready | |
3770 | * to start pushing transactions. | |
3771 | */ | |
4a0ee12a | 3772 | if (spa->spa_load_state != SPA_LOAD_TRYIMPORT) { |
9eb7b46e PZ |
3773 | error = spa_load_verify(spa); |
3774 | if (error != 0) { | |
4a0ee12a PZ |
3775 | spa_load_failed(spa, "spa_load_verify failed " |
3776 | "[error=%d]", error); | |
9eb7b46e PZ |
3777 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
3778 | error)); | |
3779 | } | |
3780 | } | |
3781 | ||
3782 | return (0); | |
3783 | } | |
3784 | ||
3785 | static void | |
3786 | spa_ld_claim_log_blocks(spa_t *spa) | |
3787 | { | |
3788 | dmu_tx_t *tx; | |
3789 | dsl_pool_t *dp = spa_get_dsl(spa); | |
3790 | ||
3791 | /* | |
3792 | * Claim log blocks that haven't been committed yet. | |
3793 | * This must all happen in a single txg. | |
3794 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), | |
3795 | * invoked from zil_claim_log_block()'s i/o done callback. | |
3796 | * Price of rollback is that we abandon the log. | |
3797 | */ | |
3798 | spa->spa_claiming = B_TRUE; | |
3799 | ||
3800 | tx = dmu_tx_create_assigned(dp, spa_first_txg(spa)); | |
3801 | (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj, | |
3802 | zil_claim, tx, DS_FIND_CHILDREN); | |
3803 | dmu_tx_commit(tx); | |
3804 | ||
3805 | spa->spa_claiming = B_FALSE; | |
3806 | ||
3807 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
3808 | } | |
3809 | ||
3810 | static void | |
6cb8e530 | 3811 | spa_ld_check_for_config_update(spa_t *spa, uint64_t config_cache_txg, |
d2734cce | 3812 | boolean_t update_config_cache) |
9eb7b46e PZ |
3813 | { |
3814 | vdev_t *rvd = spa->spa_root_vdev; | |
3815 | int need_update = B_FALSE; | |
3816 | ||
3817 | /* | |
3818 | * If the config cache is stale, or we have uninitialized | |
3819 | * metaslabs (see spa_vdev_add()), then update the config. | |
3820 | * | |
3821 | * If this is a verbatim import, trust the current | |
3822 | * in-core spa_config and update the disk labels. | |
3823 | */ | |
d2734cce | 3824 | if (update_config_cache || config_cache_txg != spa->spa_config_txg || |
4a0ee12a PZ |
3825 | spa->spa_load_state == SPA_LOAD_IMPORT || |
3826 | spa->spa_load_state == SPA_LOAD_RECOVER || | |
9eb7b46e PZ |
3827 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) |
3828 | need_update = B_TRUE; | |
3829 | ||
3830 | for (int c = 0; c < rvd->vdev_children; c++) | |
3831 | if (rvd->vdev_child[c]->vdev_ms_array == 0) | |
3832 | need_update = B_TRUE; | |
3833 | ||
3834 | /* | |
3835 | * Update the config cache asychronously in case we're the | |
3836 | * root pool, in which case the config cache isn't writable yet. | |
3837 | */ | |
3838 | if (need_update) | |
3839 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
3840 | } | |
3841 | ||
6cb8e530 PZ |
3842 | static void |
3843 | spa_ld_prepare_for_reload(spa_t *spa) | |
3844 | { | |
3845 | int mode = spa->spa_mode; | |
3846 | int async_suspended = spa->spa_async_suspended; | |
3847 | ||
3848 | spa_unload(spa); | |
3849 | spa_deactivate(spa); | |
3850 | spa_activate(spa, mode); | |
3851 | ||
3852 | /* | |
3853 | * We save the value of spa_async_suspended as it gets reset to 0 by | |
3854 | * spa_unload(). We want to restore it back to the original value before | |
3855 | * returning as we might be calling spa_async_resume() later. | |
3856 | */ | |
3857 | spa->spa_async_suspended = async_suspended; | |
3858 | } | |
3859 | ||
9eb7b46e | 3860 | static int |
d2734cce SD |
3861 | spa_ld_read_checkpoint_txg(spa_t *spa) |
3862 | { | |
3863 | uberblock_t checkpoint; | |
3864 | int error = 0; | |
3865 | ||
3866 | ASSERT0(spa->spa_checkpoint_txg); | |
3867 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
3868 | ||
3869 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3870 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
3871 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
3872 | ||
3873 | if (error == ENOENT) | |
3874 | return (0); | |
3875 | ||
3876 | if (error != 0) | |
3877 | return (error); | |
3878 | ||
3879 | ASSERT3U(checkpoint.ub_txg, !=, 0); | |
3880 | ASSERT3U(checkpoint.ub_checkpoint_txg, !=, 0); | |
3881 | ASSERT3U(checkpoint.ub_timestamp, !=, 0); | |
3882 | spa->spa_checkpoint_txg = checkpoint.ub_txg; | |
3883 | spa->spa_checkpoint_info.sci_timestamp = checkpoint.ub_timestamp; | |
3884 | ||
3885 | return (0); | |
3886 | } | |
3887 | ||
3888 | static int | |
3889 | spa_ld_mos_init(spa_t *spa, spa_import_type_t type) | |
9eb7b46e PZ |
3890 | { |
3891 | int error = 0; | |
9eb7b46e | 3892 | |
4a0ee12a | 3893 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6cb8e530 | 3894 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); |
4a0ee12a | 3895 | |
9eb7b46e | 3896 | /* |
6cb8e530 PZ |
3897 | * Never trust the config that is provided unless we are assembling |
3898 | * a pool following a split. | |
3899 | * This means don't trust blkptrs and the vdev tree in general. This | |
3900 | * also effectively puts the spa in read-only mode since | |
3901 | * spa_writeable() checks for spa_trust_config to be true. | |
3902 | * We will later load a trusted config from the MOS. | |
9eb7b46e | 3903 | */ |
6cb8e530 PZ |
3904 | if (type != SPA_IMPORT_ASSEMBLE) |
3905 | spa->spa_trust_config = B_FALSE; | |
3906 | ||
9eb7b46e PZ |
3907 | /* |
3908 | * Parse the config provided to create a vdev tree. | |
3909 | */ | |
6cb8e530 | 3910 | error = spa_ld_parse_config(spa, type); |
9eb7b46e PZ |
3911 | if (error != 0) |
3912 | return (error); | |
3913 | ||
3914 | /* | |
3915 | * Now that we have the vdev tree, try to open each vdev. This involves | |
3916 | * opening the underlying physical device, retrieving its geometry and | |
3917 | * probing the vdev with a dummy I/O. The state of each vdev will be set | |
3918 | * based on the success of those operations. After this we'll be ready | |
3919 | * to read from the vdevs. | |
3920 | */ | |
3921 | error = spa_ld_open_vdevs(spa); | |
3922 | if (error != 0) | |
3923 | return (error); | |
3924 | ||
3925 | /* | |
3926 | * Read the label of each vdev and make sure that the GUIDs stored | |
3927 | * there match the GUIDs in the config provided. | |
6cb8e530 PZ |
3928 | * If we're assembling a new pool that's been split off from an |
3929 | * existing pool, the labels haven't yet been updated so we skip | |
3930 | * validation for now. | |
9eb7b46e | 3931 | */ |
6cb8e530 PZ |
3932 | if (type != SPA_IMPORT_ASSEMBLE) { |
3933 | error = spa_ld_validate_vdevs(spa); | |
3934 | if (error != 0) | |
3935 | return (error); | |
3936 | } | |
9eb7b46e PZ |
3937 | |
3938 | /* | |
d2734cce SD |
3939 | * Read all vdev labels to find the best uberblock (i.e. latest, |
3940 | * unless spa_load_max_txg is set) and store it in spa_uberblock. We | |
3941 | * get the list of features required to read blkptrs in the MOS from | |
3942 | * the vdev label with the best uberblock and verify that our version | |
3943 | * of zfs supports them all. | |
9eb7b46e | 3944 | */ |
6cb8e530 | 3945 | error = spa_ld_select_uberblock(spa, type); |
9eb7b46e PZ |
3946 | if (error != 0) |
3947 | return (error); | |
3948 | ||
3949 | /* | |
3950 | * Pass that uberblock to the dsl_pool layer which will open the root | |
3951 | * blkptr. This blkptr points to the latest version of the MOS and will | |
3952 | * allow us to read its contents. | |
3953 | */ | |
3954 | error = spa_ld_open_rootbp(spa); | |
3955 | if (error != 0) | |
3956 | return (error); | |
3957 | ||
d2734cce SD |
3958 | return (0); |
3959 | } | |
3960 | ||
3961 | static int | |
3962 | spa_ld_checkpoint_rewind(spa_t *spa) | |
3963 | { | |
3964 | uberblock_t checkpoint; | |
3965 | int error = 0; | |
3966 | ||
3967 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
3968 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
3969 | ||
3970 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
3971 | DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), | |
3972 | sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); | |
3973 | ||
3974 | if (error != 0) { | |
3975 | spa_load_failed(spa, "unable to retrieve checkpointed " | |
3976 | "uberblock from the MOS config [error=%d]", error); | |
3977 | ||
3978 | if (error == ENOENT) | |
3979 | error = ZFS_ERR_NO_CHECKPOINT; | |
3980 | ||
3981 | return (error); | |
3982 | } | |
3983 | ||
3984 | ASSERT3U(checkpoint.ub_txg, <, spa->spa_uberblock.ub_txg); | |
3985 | ASSERT3U(checkpoint.ub_txg, ==, checkpoint.ub_checkpoint_txg); | |
3986 | ||
3987 | /* | |
3988 | * We need to update the txg and timestamp of the checkpointed | |
3989 | * uberblock to be higher than the latest one. This ensures that | |
3990 | * the checkpointed uberblock is selected if we were to close and | |
3991 | * reopen the pool right after we've written it in the vdev labels. | |
3992 | * (also see block comment in vdev_uberblock_compare) | |
3993 | */ | |
3994 | checkpoint.ub_txg = spa->spa_uberblock.ub_txg + 1; | |
3995 | checkpoint.ub_timestamp = gethrestime_sec(); | |
3996 | ||
3997 | /* | |
3998 | * Set current uberblock to be the checkpointed uberblock. | |
3999 | */ | |
4000 | spa->spa_uberblock = checkpoint; | |
4001 | ||
4002 | /* | |
4003 | * If we are doing a normal rewind, then the pool is open for | |
4004 | * writing and we sync the "updated" checkpointed uberblock to | |
4005 | * disk. Once this is done, we've basically rewound the whole | |
4006 | * pool and there is no way back. | |
4007 | * | |
4008 | * There are cases when we don't want to attempt and sync the | |
4009 | * checkpointed uberblock to disk because we are opening a | |
4010 | * pool as read-only. Specifically, verifying the checkpointed | |
4011 | * state with zdb, and importing the checkpointed state to get | |
4012 | * a "preview" of its content. | |
4013 | */ | |
4014 | if (spa_writeable(spa)) { | |
4015 | vdev_t *rvd = spa->spa_root_vdev; | |
4016 | ||
4017 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
4018 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; | |
4019 | int svdcount = 0; | |
4020 | int children = rvd->vdev_children; | |
4021 | int c0 = spa_get_random(children); | |
4022 | ||
4023 | for (int c = 0; c < children; c++) { | |
4024 | vdev_t *vd = rvd->vdev_child[(c0 + c) % children]; | |
4025 | ||
4026 | /* Stop when revisiting the first vdev */ | |
4027 | if (c > 0 && svd[0] == vd) | |
4028 | break; | |
4029 | ||
4030 | if (vd->vdev_ms_array == 0 || vd->vdev_islog || | |
4031 | !vdev_is_concrete(vd)) | |
4032 | continue; | |
4033 | ||
4034 | svd[svdcount++] = vd; | |
4035 | if (svdcount == SPA_SYNC_MIN_VDEVS) | |
4036 | break; | |
4037 | } | |
4038 | error = vdev_config_sync(svd, svdcount, spa->spa_first_txg); | |
4039 | if (error == 0) | |
4040 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
4041 | spa_config_exit(spa, SCL_ALL, FTAG); | |
4042 | ||
4043 | if (error != 0) { | |
4044 | spa_load_failed(spa, "failed to write checkpointed " | |
4045 | "uberblock to the vdev labels [error=%d]", error); | |
4046 | return (error); | |
4047 | } | |
4048 | } | |
4049 | ||
4050 | return (0); | |
4051 | } | |
4052 | ||
4053 | static int | |
4054 | spa_ld_mos_with_trusted_config(spa_t *spa, spa_import_type_t type, | |
4055 | boolean_t *update_config_cache) | |
4056 | { | |
4057 | int error; | |
4058 | ||
4059 | /* | |
4060 | * Parse the config for pool, open and validate vdevs, | |
4061 | * select an uberblock, and use that uberblock to open | |
4062 | * the MOS. | |
4063 | */ | |
4064 | error = spa_ld_mos_init(spa, type); | |
4065 | if (error != 0) | |
4066 | return (error); | |
4067 | ||
9eb7b46e | 4068 | /* |
6cb8e530 PZ |
4069 | * Retrieve the trusted config stored in the MOS and use it to create |
4070 | * a new, exact version of the vdev tree, then reopen all vdevs. | |
9eb7b46e | 4071 | */ |
d2734cce | 4072 | error = spa_ld_trusted_config(spa, type, B_FALSE); |
6cb8e530 | 4073 | if (error == EAGAIN) { |
d2734cce SD |
4074 | if (update_config_cache != NULL) |
4075 | *update_config_cache = B_TRUE; | |
4076 | ||
6cb8e530 PZ |
4077 | /* |
4078 | * Redo the loading process with the trusted config if it is | |
4079 | * too different from the untrusted config. | |
4080 | */ | |
4081 | spa_ld_prepare_for_reload(spa); | |
d2734cce SD |
4082 | spa_load_note(spa, "RELOADING"); |
4083 | error = spa_ld_mos_init(spa, type); | |
4084 | if (error != 0) | |
4085 | return (error); | |
4086 | ||
4087 | error = spa_ld_trusted_config(spa, type, B_TRUE); | |
4088 | if (error != 0) | |
4089 | return (error); | |
4090 | ||
6cb8e530 | 4091 | } else if (error != 0) { |
9eb7b46e | 4092 | return (error); |
6cb8e530 | 4093 | } |
9eb7b46e | 4094 | |
d2734cce SD |
4095 | return (0); |
4096 | } | |
4097 | ||
4098 | /* | |
4099 | * Load an existing storage pool, using the config provided. This config | |
4100 | * describes which vdevs are part of the pool and is later validated against | |
4101 | * partial configs present in each vdev's label and an entire copy of the | |
4102 | * config stored in the MOS. | |
4103 | */ | |
4104 | static int | |
4105 | spa_load_impl(spa_t *spa, spa_import_type_t type, char **ereport) | |
4106 | { | |
4107 | int error = 0; | |
4108 | boolean_t missing_feat_write = B_FALSE; | |
4109 | boolean_t checkpoint_rewind = | |
4110 | (spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4111 | boolean_t update_config_cache = B_FALSE; | |
4112 | ||
4113 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
4114 | ASSERT(spa->spa_config_source != SPA_CONFIG_SRC_NONE); | |
4115 | ||
4116 | spa_load_note(spa, "LOADING"); | |
4117 | ||
4118 | error = spa_ld_mos_with_trusted_config(spa, type, &update_config_cache); | |
4119 | if (error != 0) | |
4120 | return (error); | |
4121 | ||
4122 | /* | |
4123 | * If we are rewinding to the checkpoint then we need to repeat | |
4124 | * everything we've done so far in this function but this time | |
4125 | * selecting the checkpointed uberblock and using that to open | |
4126 | * the MOS. | |
4127 | */ | |
4128 | if (checkpoint_rewind) { | |
4129 | /* | |
4130 | * If we are rewinding to the checkpoint update config cache | |
4131 | * anyway. | |
4132 | */ | |
4133 | update_config_cache = B_TRUE; | |
4134 | ||
4135 | /* | |
4136 | * Extract the checkpointed uberblock from the current MOS | |
4137 | * and use this as the pool's uberblock from now on. If the | |
4138 | * pool is imported as writeable we also write the checkpoint | |
4139 | * uberblock to the labels, making the rewind permanent. | |
4140 | */ | |
4141 | error = spa_ld_checkpoint_rewind(spa); | |
4142 | if (error != 0) | |
4143 | return (error); | |
4144 | ||
4145 | /* | |
4146 | * Redo the loading process process again with the | |
4147 | * checkpointed uberblock. | |
4148 | */ | |
4149 | spa_ld_prepare_for_reload(spa); | |
4150 | spa_load_note(spa, "LOADING checkpointed uberblock"); | |
4151 | error = spa_ld_mos_with_trusted_config(spa, type, NULL); | |
4152 | if (error != 0) | |
4153 | return (error); | |
4154 | } | |
4155 | ||
4156 | /* | |
4157 | * Retrieve the checkpoint txg if the pool has a checkpoint. | |
4158 | */ | |
4159 | error = spa_ld_read_checkpoint_txg(spa); | |
4160 | if (error != 0) | |
4161 | return (error); | |
4162 | ||
9eb7b46e PZ |
4163 | /* |
4164 | * Retrieve the mapping of indirect vdevs. Those vdevs were removed | |
4165 | * from the pool and their contents were re-mapped to other vdevs. Note | |
4166 | * that everything that we read before this step must have been | |
4167 | * rewritten on concrete vdevs after the last device removal was | |
4168 | * initiated. Otherwise we could be reading from indirect vdevs before | |
4169 | * we have loaded their mappings. | |
4170 | */ | |
4171 | error = spa_ld_open_indirect_vdev_metadata(spa); | |
4172 | if (error != 0) | |
4173 | return (error); | |
4174 | ||
4175 | /* | |
4176 | * Retrieve the full list of active features from the MOS and check if | |
4177 | * they are all supported. | |
4178 | */ | |
4a0ee12a | 4179 | error = spa_ld_check_features(spa, &missing_feat_write); |
9eb7b46e PZ |
4180 | if (error != 0) |
4181 | return (error); | |
4182 | ||
4183 | /* | |
4184 | * Load several special directories from the MOS needed by the dsl_pool | |
4185 | * layer. | |
4186 | */ | |
4187 | error = spa_ld_load_special_directories(spa); | |
4188 | if (error != 0) | |
4189 | return (error); | |
4190 | ||
9eb7b46e PZ |
4191 | /* |
4192 | * Retrieve pool properties from the MOS. | |
4193 | */ | |
4194 | error = spa_ld_get_props(spa); | |
4195 | if (error != 0) | |
4196 | return (error); | |
4197 | ||
4198 | /* | |
4199 | * Retrieve the list of auxiliary devices - cache devices and spares - | |
4200 | * and open them. | |
4201 | */ | |
4202 | error = spa_ld_open_aux_vdevs(spa, type); | |
4203 | if (error != 0) | |
4204 | return (error); | |
4205 | ||
4206 | /* | |
4207 | * Load the metadata for all vdevs. Also check if unopenable devices | |
4208 | * should be autoreplaced. | |
4209 | */ | |
4a0ee12a | 4210 | error = spa_ld_load_vdev_metadata(spa); |
9eb7b46e PZ |
4211 | if (error != 0) |
4212 | return (error); | |
4213 | ||
4214 | error = spa_ld_load_dedup_tables(spa); | |
4215 | if (error != 0) | |
4216 | return (error); | |
4217 | ||
4218 | /* | |
4219 | * Verify the logs now to make sure we don't have any unexpected errors | |
4220 | * when we claim log blocks later. | |
4221 | */ | |
4222 | error = spa_ld_verify_logs(spa, type, ereport); | |
4223 | if (error != 0) | |
4224 | return (error); | |
4225 | ||
9ae529ec | 4226 | if (missing_feat_write) { |
6cb8e530 | 4227 | ASSERT(spa->spa_load_state == SPA_LOAD_TRYIMPORT); |
9ae529ec CS |
4228 | |
4229 | /* | |
4230 | * At this point, we know that we can open the pool in | |
4231 | * read-only mode but not read-write mode. We now have enough | |
4232 | * information and can return to userland. | |
4233 | */ | |
9eb7b46e PZ |
4234 | return (spa_vdev_err(spa->spa_root_vdev, VDEV_AUX_UNSUP_FEAT, |
4235 | ENOTSUP)); | |
9ae529ec CS |
4236 | } |
4237 | ||
572e2857 | 4238 | /* |
9eb7b46e PZ |
4239 | * Traverse the last txgs to make sure the pool was left off in a safe |
4240 | * state. When performing an extreme rewind, we verify the whole pool, | |
4241 | * which can take a very long time. | |
572e2857 | 4242 | */ |
4a0ee12a | 4243 | error = spa_ld_verify_pool_data(spa); |
9eb7b46e PZ |
4244 | if (error != 0) |
4245 | return (error); | |
572e2857 | 4246 | |
9eb7b46e PZ |
4247 | /* |
4248 | * Calculate the deflated space for the pool. This must be done before | |
4249 | * we write anything to the pool because we'd need to update the space | |
4250 | * accounting using the deflated sizes. | |
4251 | */ | |
4252 | spa_update_dspace(spa); | |
4253 | ||
4254 | /* | |
4255 | * We have now retrieved all the information we needed to open the | |
4256 | * pool. If we are importing the pool in read-write mode, a few | |
4257 | * additional steps must be performed to finish the import. | |
4258 | */ | |
6cb8e530 | 4259 | if (spa_writeable(spa) && (spa->spa_load_state == SPA_LOAD_RECOVER || |
428870ff | 4260 | spa->spa_load_max_txg == UINT64_MAX)) { |
6cb8e530 PZ |
4261 | uint64_t config_cache_txg = spa->spa_config_txg; |
4262 | ||
4263 | ASSERT(spa->spa_load_state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f | 4264 | |
d2734cce SD |
4265 | /* |
4266 | * In case of a checkpoint rewind, log the original txg | |
4267 | * of the checkpointed uberblock. | |
4268 | */ | |
4269 | if (checkpoint_rewind) { | |
4270 | spa_history_log_internal(spa, "checkpoint rewind", | |
4271 | NULL, "rewound state to txg=%llu", | |
4272 | (u_longlong_t)spa->spa_uberblock.ub_checkpoint_txg); | |
4273 | } | |
4274 | ||
34dc7c2f | 4275 | /* |
9eb7b46e | 4276 | * Traverse the ZIL and claim all blocks. |
34dc7c2f | 4277 | */ |
9eb7b46e | 4278 | spa_ld_claim_log_blocks(spa); |
428870ff | 4279 | |
9eb7b46e PZ |
4280 | /* |
4281 | * Kick-off the syncing thread. | |
4282 | */ | |
34dc7c2f BB |
4283 | spa->spa_sync_on = B_TRUE; |
4284 | txg_sync_start(spa->spa_dsl_pool); | |
379ca9cf | 4285 | mmp_thread_start(spa); |
34dc7c2f BB |
4286 | |
4287 | /* | |
428870ff BB |
4288 | * Wait for all claims to sync. We sync up to the highest |
4289 | * claimed log block birth time so that claimed log blocks | |
4290 | * don't appear to be from the future. spa_claim_max_txg | |
9eb7b46e PZ |
4291 | * will have been set for us by ZIL traversal operations |
4292 | * performed above. | |
34dc7c2f | 4293 | */ |
428870ff | 4294 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
4295 | |
4296 | /* | |
9eb7b46e PZ |
4297 | * Check if we need to request an update of the config. On the |
4298 | * next sync, we would update the config stored in vdev labels | |
4299 | * and the cachefile (by default /etc/zfs/zpool.cache). | |
34dc7c2f | 4300 | */ |
6cb8e530 | 4301 | spa_ld_check_for_config_update(spa, config_cache_txg, |
d2734cce | 4302 | update_config_cache); |
fb5f0bc8 BB |
4303 | |
4304 | /* | |
4305 | * Check all DTLs to see if anything needs resilvering. | |
4306 | */ | |
428870ff | 4307 | if (!dsl_scan_resilvering(spa->spa_dsl_pool) && |
9eb7b46e | 4308 | vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) |
fb5f0bc8 | 4309 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
428870ff | 4310 | |
6f1ffb06 MA |
4311 | /* |
4312 | * Log the fact that we booted up (so that we can detect if | |
4313 | * we rebooted in the middle of an operation). | |
4314 | */ | |
d5e024cb | 4315 | spa_history_log_version(spa, "open", NULL); |
6f1ffb06 | 4316 | |
9b2266e3 SD |
4317 | spa_restart_removal(spa); |
4318 | spa_spawn_aux_threads(spa); | |
4319 | ||
428870ff BB |
4320 | /* |
4321 | * Delete any inconsistent datasets. | |
9b2266e3 SD |
4322 | * |
4323 | * Note: | |
4324 | * Since we may be issuing deletes for clones here, | |
4325 | * we make sure to do so after we've spawned all the | |
4326 | * auxiliary threads above (from which the livelist | |
4327 | * deletion zthr is part of). | |
428870ff BB |
4328 | */ |
4329 | (void) dmu_objset_find(spa_name(spa), | |
4330 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
4331 | ||
4332 | /* | |
4333 | * Clean up any stale temporary dataset userrefs. | |
4334 | */ | |
4335 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
a1d477c2 | 4336 | |
619f0976 GW |
4337 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
4338 | vdev_initialize_restart(spa->spa_root_vdev); | |
4339 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f BB |
4340 | } |
4341 | ||
4a0ee12a PZ |
4342 | spa_load_note(spa, "LOADED"); |
4343 | ||
428870ff BB |
4344 | return (0); |
4345 | } | |
34dc7c2f | 4346 | |
428870ff | 4347 | static int |
6cb8e530 | 4348 | spa_load_retry(spa_t *spa, spa_load_state_t state) |
428870ff | 4349 | { |
572e2857 BB |
4350 | int mode = spa->spa_mode; |
4351 | ||
428870ff BB |
4352 | spa_unload(spa); |
4353 | spa_deactivate(spa); | |
4354 | ||
dea377c0 | 4355 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1; |
428870ff | 4356 | |
572e2857 | 4357 | spa_activate(spa, mode); |
428870ff BB |
4358 | spa_async_suspend(spa); |
4359 | ||
4a0ee12a PZ |
4360 | spa_load_note(spa, "spa_load_retry: rewind, max txg: %llu", |
4361 | (u_longlong_t)spa->spa_load_max_txg); | |
4362 | ||
6cb8e530 | 4363 | return (spa_load(spa, state, SPA_IMPORT_EXISTING)); |
428870ff BB |
4364 | } |
4365 | ||
9ae529ec CS |
4366 | /* |
4367 | * If spa_load() fails this function will try loading prior txg's. If | |
4368 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
4369 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
4370 | * function will not rewind the pool and will return the same error as | |
4371 | * spa_load(). | |
4372 | */ | |
428870ff | 4373 | static int |
6cb8e530 PZ |
4374 | spa_load_best(spa_t *spa, spa_load_state_t state, uint64_t max_request, |
4375 | int rewind_flags) | |
428870ff | 4376 | { |
9ae529ec | 4377 | nvlist_t *loadinfo = NULL; |
428870ff BB |
4378 | nvlist_t *config = NULL; |
4379 | int load_error, rewind_error; | |
4380 | uint64_t safe_rewind_txg; | |
4381 | uint64_t min_txg; | |
4382 | ||
4383 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
4384 | spa->spa_load_max_txg = spa->spa_load_txg; | |
4385 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
4386 | } else { | |
4387 | spa->spa_load_max_txg = max_request; | |
dea377c0 MA |
4388 | if (max_request != UINT64_MAX) |
4389 | spa->spa_extreme_rewind = B_TRUE; | |
428870ff BB |
4390 | } |
4391 | ||
6cb8e530 | 4392 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING); |
428870ff BB |
4393 | if (load_error == 0) |
4394 | return (0); | |
d2734cce SD |
4395 | if (load_error == ZFS_ERR_NO_CHECKPOINT) { |
4396 | /* | |
4397 | * When attempting checkpoint-rewind on a pool with no | |
4398 | * checkpoint, we should not attempt to load uberblocks | |
4399 | * from previous txgs when spa_load fails. | |
4400 | */ | |
4401 | ASSERT(spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT); | |
4402 | return (load_error); | |
4403 | } | |
428870ff BB |
4404 | |
4405 | if (spa->spa_root_vdev != NULL) | |
4406 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
4407 | ||
4408 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
4409 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
4410 | ||
4411 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
4412 | nvlist_free(config); | |
4413 | return (load_error); | |
4414 | } | |
4415 | ||
9ae529ec CS |
4416 | if (state == SPA_LOAD_RECOVER) { |
4417 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 4418 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
4419 | } else { |
4420 | /* | |
4421 | * If we aren't rolling back save the load info from our first | |
4422 | * import attempt so that we can restore it after attempting | |
4423 | * to rewind. | |
4424 | */ | |
4425 | loadinfo = spa->spa_load_info; | |
4426 | spa->spa_load_info = fnvlist_alloc(); | |
4427 | } | |
428870ff BB |
4428 | |
4429 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
4430 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
4431 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
4432 | TXG_INITIAL : safe_rewind_txg; | |
4433 | ||
4434 | /* | |
4435 | * Continue as long as we're finding errors, we're still within | |
4436 | * the acceptable rewind range, and we're still finding uberblocks | |
4437 | */ | |
4438 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
4439 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
4440 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
4441 | spa->spa_extreme_rewind = B_TRUE; | |
6cb8e530 | 4442 | rewind_error = spa_load_retry(spa, state); |
428870ff BB |
4443 | } |
4444 | ||
428870ff BB |
4445 | spa->spa_extreme_rewind = B_FALSE; |
4446 | spa->spa_load_max_txg = UINT64_MAX; | |
4447 | ||
4448 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
4449 | spa_config_set(spa, config); | |
ee6370a7 | 4450 | else |
4451 | nvlist_free(config); | |
428870ff | 4452 | |
9ae529ec CS |
4453 | if (state == SPA_LOAD_RECOVER) { |
4454 | ASSERT3P(loadinfo, ==, NULL); | |
4455 | return (rewind_error); | |
4456 | } else { | |
4457 | /* Store the rewind info as part of the initial load info */ | |
4458 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
4459 | spa->spa_load_info); | |
4460 | ||
4461 | /* Restore the initial load info */ | |
4462 | fnvlist_free(spa->spa_load_info); | |
4463 | spa->spa_load_info = loadinfo; | |
4464 | ||
4465 | return (load_error); | |
4466 | } | |
34dc7c2f BB |
4467 | } |
4468 | ||
4469 | /* | |
4470 | * Pool Open/Import | |
4471 | * | |
4472 | * The import case is identical to an open except that the configuration is sent | |
4473 | * down from userland, instead of grabbed from the configuration cache. For the | |
4474 | * case of an open, the pool configuration will exist in the | |
4475 | * POOL_STATE_UNINITIALIZED state. | |
4476 | * | |
4477 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
4478 | * the same time open the pool, without having to keep around the spa_t in some | |
4479 | * ambiguous state. | |
4480 | */ | |
4481 | static int | |
428870ff BB |
4482 | spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, |
4483 | nvlist_t **config) | |
34dc7c2f BB |
4484 | { |
4485 | spa_t *spa; | |
572e2857 | 4486 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 4487 | int error; |
34dc7c2f | 4488 | int locked = B_FALSE; |
526af785 | 4489 | int firstopen = B_FALSE; |
34dc7c2f BB |
4490 | |
4491 | *spapp = NULL; | |
4492 | ||
4493 | /* | |
4494 | * As disgusting as this is, we need to support recursive calls to this | |
4495 | * function because dsl_dir_open() is called during spa_load(), and ends | |
4496 | * up calling spa_open() again. The real fix is to figure out how to | |
4497 | * avoid dsl_dir_open() calling this in the first place. | |
4498 | */ | |
c25b8f99 | 4499 | if (MUTEX_NOT_HELD(&spa_namespace_lock)) { |
34dc7c2f BB |
4500 | mutex_enter(&spa_namespace_lock); |
4501 | locked = B_TRUE; | |
4502 | } | |
4503 | ||
4504 | if ((spa = spa_lookup(pool)) == NULL) { | |
4505 | if (locked) | |
4506 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 4507 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 4508 | } |
428870ff | 4509 | |
34dc7c2f | 4510 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
8a393be3 | 4511 | zpool_load_policy_t policy; |
428870ff | 4512 | |
526af785 PJD |
4513 | firstopen = B_TRUE; |
4514 | ||
8a393be3 | 4515 | zpool_get_load_policy(nvpolicy ? nvpolicy : spa->spa_config, |
428870ff | 4516 | &policy); |
8a393be3 | 4517 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) |
428870ff | 4518 | state = SPA_LOAD_RECOVER; |
34dc7c2f | 4519 | |
fb5f0bc8 | 4520 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 4521 | |
428870ff BB |
4522 | if (state != SPA_LOAD_RECOVER) |
4523 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
6cb8e530 | 4524 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; |
428870ff | 4525 | |
4a0ee12a | 4526 | zfs_dbgmsg("spa_open_common: opening %s", pool); |
8a393be3 PZ |
4527 | error = spa_load_best(spa, state, policy.zlp_txg, |
4528 | policy.zlp_rewind); | |
34dc7c2f BB |
4529 | |
4530 | if (error == EBADF) { | |
4531 | /* | |
4532 | * If vdev_validate() returns failure (indicated by | |
4533 | * EBADF), it indicates that one of the vdevs indicates | |
4534 | * that the pool has been exported or destroyed. If | |
4535 | * this is the case, the config cache is out of sync and | |
4536 | * we should remove the pool from the namespace. | |
4537 | */ | |
34dc7c2f BB |
4538 | spa_unload(spa); |
4539 | spa_deactivate(spa); | |
a1d477c2 | 4540 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 4541 | spa_remove(spa); |
34dc7c2f BB |
4542 | if (locked) |
4543 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 4544 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
4545 | } |
4546 | ||
4547 | if (error) { | |
4548 | /* | |
4549 | * We can't open the pool, but we still have useful | |
4550 | * information: the state of each vdev after the | |
4551 | * attempted vdev_open(). Return this to the user. | |
4552 | */ | |
572e2857 | 4553 | if (config != NULL && spa->spa_config) { |
428870ff | 4554 | VERIFY(nvlist_dup(spa->spa_config, config, |
79c76d5b | 4555 | KM_SLEEP) == 0); |
572e2857 BB |
4556 | VERIFY(nvlist_add_nvlist(*config, |
4557 | ZPOOL_CONFIG_LOAD_INFO, | |
4558 | spa->spa_load_info) == 0); | |
4559 | } | |
34dc7c2f BB |
4560 | spa_unload(spa); |
4561 | spa_deactivate(spa); | |
428870ff | 4562 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
4563 | if (locked) |
4564 | mutex_exit(&spa_namespace_lock); | |
4565 | *spapp = NULL; | |
4566 | return (error); | |
34dc7c2f | 4567 | } |
34dc7c2f BB |
4568 | } |
4569 | ||
4570 | spa_open_ref(spa, tag); | |
4571 | ||
b128c09f | 4572 | if (config != NULL) |
34dc7c2f | 4573 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 4574 | |
572e2857 BB |
4575 | /* |
4576 | * If we've recovered the pool, pass back any information we | |
4577 | * gathered while doing the load. | |
4578 | */ | |
4579 | if (state == SPA_LOAD_RECOVER) { | |
4580 | VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, | |
4581 | spa->spa_load_info) == 0); | |
4582 | } | |
4583 | ||
428870ff BB |
4584 | if (locked) { |
4585 | spa->spa_last_open_failed = 0; | |
4586 | spa->spa_last_ubsync_txg = 0; | |
4587 | spa->spa_load_txg = 0; | |
4588 | mutex_exit(&spa_namespace_lock); | |
4589 | } | |
4590 | ||
526af785 | 4591 | if (firstopen) |
a0bd735a | 4592 | zvol_create_minors(spa, spa_name(spa), B_TRUE); |
526af785 | 4593 | |
428870ff BB |
4594 | *spapp = spa; |
4595 | ||
34dc7c2f BB |
4596 | return (0); |
4597 | } | |
4598 | ||
428870ff BB |
4599 | int |
4600 | spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, | |
4601 | nvlist_t **config) | |
4602 | { | |
4603 | return (spa_open_common(name, spapp, tag, policy, config)); | |
4604 | } | |
4605 | ||
34dc7c2f BB |
4606 | int |
4607 | spa_open(const char *name, spa_t **spapp, void *tag) | |
4608 | { | |
428870ff | 4609 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
4610 | } |
4611 | ||
4612 | /* | |
4613 | * Lookup the given spa_t, incrementing the inject count in the process, | |
4614 | * preventing it from being exported or destroyed. | |
4615 | */ | |
4616 | spa_t * | |
4617 | spa_inject_addref(char *name) | |
4618 | { | |
4619 | spa_t *spa; | |
4620 | ||
4621 | mutex_enter(&spa_namespace_lock); | |
4622 | if ((spa = spa_lookup(name)) == NULL) { | |
4623 | mutex_exit(&spa_namespace_lock); | |
4624 | return (NULL); | |
4625 | } | |
4626 | spa->spa_inject_ref++; | |
4627 | mutex_exit(&spa_namespace_lock); | |
4628 | ||
4629 | return (spa); | |
4630 | } | |
4631 | ||
4632 | void | |
4633 | spa_inject_delref(spa_t *spa) | |
4634 | { | |
4635 | mutex_enter(&spa_namespace_lock); | |
4636 | spa->spa_inject_ref--; | |
4637 | mutex_exit(&spa_namespace_lock); | |
4638 | } | |
4639 | ||
4640 | /* | |
4641 | * Add spares device information to the nvlist. | |
4642 | */ | |
4643 | static void | |
4644 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
4645 | { | |
4646 | nvlist_t **spares; | |
4647 | uint_t i, nspares; | |
4648 | nvlist_t *nvroot; | |
4649 | uint64_t guid; | |
4650 | vdev_stat_t *vs; | |
4651 | uint_t vsc; | |
4652 | uint64_t pool; | |
4653 | ||
9babb374 BB |
4654 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
4655 | ||
34dc7c2f BB |
4656 | if (spa->spa_spares.sav_count == 0) |
4657 | return; | |
4658 | ||
4659 | VERIFY(nvlist_lookup_nvlist(config, | |
4660 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
4661 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
4662 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
4663 | if (nspares != 0) { | |
4664 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
4665 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
4666 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
4667 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
4668 | ||
4669 | /* | |
4670 | * Go through and find any spares which have since been | |
4671 | * repurposed as an active spare. If this is the case, update | |
4672 | * their status appropriately. | |
4673 | */ | |
4674 | for (i = 0; i < nspares; i++) { | |
4675 | VERIFY(nvlist_lookup_uint64(spares[i], | |
4676 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
b128c09f BB |
4677 | if (spa_spare_exists(guid, &pool, NULL) && |
4678 | pool != 0ULL) { | |
34dc7c2f | 4679 | VERIFY(nvlist_lookup_uint64_array( |
428870ff | 4680 | spares[i], ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
4681 | (uint64_t **)&vs, &vsc) == 0); |
4682 | vs->vs_state = VDEV_STATE_CANT_OPEN; | |
4683 | vs->vs_aux = VDEV_AUX_SPARED; | |
4684 | } | |
4685 | } | |
4686 | } | |
4687 | } | |
4688 | ||
4689 | /* | |
4690 | * Add l2cache device information to the nvlist, including vdev stats. | |
4691 | */ | |
4692 | static void | |
4693 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
4694 | { | |
4695 | nvlist_t **l2cache; | |
4696 | uint_t i, j, nl2cache; | |
4697 | nvlist_t *nvroot; | |
4698 | uint64_t guid; | |
4699 | vdev_t *vd; | |
4700 | vdev_stat_t *vs; | |
4701 | uint_t vsc; | |
4702 | ||
9babb374 BB |
4703 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
4704 | ||
34dc7c2f BB |
4705 | if (spa->spa_l2cache.sav_count == 0) |
4706 | return; | |
4707 | ||
34dc7c2f BB |
4708 | VERIFY(nvlist_lookup_nvlist(config, |
4709 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
4710 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
4711 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
4712 | if (nl2cache != 0) { | |
4713 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
4714 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
4715 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
4716 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
4717 | ||
4718 | /* | |
4719 | * Update level 2 cache device stats. | |
4720 | */ | |
4721 | ||
4722 | for (i = 0; i < nl2cache; i++) { | |
4723 | VERIFY(nvlist_lookup_uint64(l2cache[i], | |
4724 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
4725 | ||
4726 | vd = NULL; | |
4727 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
4728 | if (guid == | |
4729 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
4730 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
4731 | break; | |
4732 | } | |
4733 | } | |
4734 | ASSERT(vd != NULL); | |
4735 | ||
4736 | VERIFY(nvlist_lookup_uint64_array(l2cache[i], | |
428870ff BB |
4737 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) |
4738 | == 0); | |
34dc7c2f | 4739 | vdev_get_stats(vd, vs); |
193a37cb TH |
4740 | vdev_config_generate_stats(vd, l2cache[i]); |
4741 | ||
34dc7c2f BB |
4742 | } |
4743 | } | |
34dc7c2f BB |
4744 | } |
4745 | ||
9ae529ec | 4746 | static void |
417104bd | 4747 | spa_feature_stats_from_disk(spa_t *spa, nvlist_t *features) |
9ae529ec | 4748 | { |
9ae529ec CS |
4749 | zap_cursor_t zc; |
4750 | zap_attribute_t za; | |
4751 | ||
9ae529ec CS |
4752 | if (spa->spa_feat_for_read_obj != 0) { |
4753 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
4754 | spa->spa_feat_for_read_obj); | |
4755 | zap_cursor_retrieve(&zc, &za) == 0; | |
4756 | zap_cursor_advance(&zc)) { | |
4757 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
4758 | za.za_num_integers == 1); | |
417104bd | 4759 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
4760 | za.za_first_integer)); |
4761 | } | |
4762 | zap_cursor_fini(&zc); | |
4763 | } | |
4764 | ||
4765 | if (spa->spa_feat_for_write_obj != 0) { | |
4766 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
4767 | spa->spa_feat_for_write_obj); | |
4768 | zap_cursor_retrieve(&zc, &za) == 0; | |
4769 | zap_cursor_advance(&zc)) { | |
4770 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
4771 | za.za_num_integers == 1); | |
417104bd | 4772 | VERIFY0(nvlist_add_uint64(features, za.za_name, |
9ae529ec CS |
4773 | za.za_first_integer)); |
4774 | } | |
4775 | zap_cursor_fini(&zc); | |
4776 | } | |
417104bd NB |
4777 | } |
4778 | ||
4779 | static void | |
4780 | spa_feature_stats_from_cache(spa_t *spa, nvlist_t *features) | |
4781 | { | |
4782 | int i; | |
4783 | ||
4784 | for (i = 0; i < SPA_FEATURES; i++) { | |
4785 | zfeature_info_t feature = spa_feature_table[i]; | |
4786 | uint64_t refcount; | |
4787 | ||
4788 | if (feature_get_refcount(spa, &feature, &refcount) != 0) | |
4789 | continue; | |
4790 | ||
4791 | VERIFY0(nvlist_add_uint64(features, feature.fi_guid, refcount)); | |
4792 | } | |
4793 | } | |
4794 | ||
4795 | /* | |
4796 | * Store a list of pool features and their reference counts in the | |
4797 | * config. | |
4798 | * | |
4799 | * The first time this is called on a spa, allocate a new nvlist, fetch | |
4800 | * the pool features and reference counts from disk, then save the list | |
4801 | * in the spa. In subsequent calls on the same spa use the saved nvlist | |
4802 | * and refresh its values from the cached reference counts. This | |
4803 | * ensures we don't block here on I/O on a suspended pool so 'zpool | |
4804 | * clear' can resume the pool. | |
4805 | */ | |
4806 | static void | |
4807 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
4808 | { | |
4eb30c68 | 4809 | nvlist_t *features; |
417104bd NB |
4810 | |
4811 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
4812 | ||
4eb30c68 NB |
4813 | mutex_enter(&spa->spa_feat_stats_lock); |
4814 | features = spa->spa_feat_stats; | |
4815 | ||
417104bd NB |
4816 | if (features != NULL) { |
4817 | spa_feature_stats_from_cache(spa, features); | |
4818 | } else { | |
4819 | VERIFY0(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP)); | |
4820 | spa->spa_feat_stats = features; | |
4821 | spa_feature_stats_from_disk(spa, features); | |
4822 | } | |
9ae529ec | 4823 | |
417104bd NB |
4824 | VERIFY0(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, |
4825 | features)); | |
4eb30c68 NB |
4826 | |
4827 | mutex_exit(&spa->spa_feat_stats_lock); | |
9ae529ec CS |
4828 | } |
4829 | ||
34dc7c2f | 4830 | int |
9ae529ec CS |
4831 | spa_get_stats(const char *name, nvlist_t **config, |
4832 | char *altroot, size_t buflen) | |
34dc7c2f BB |
4833 | { |
4834 | int error; | |
4835 | spa_t *spa; | |
4836 | ||
4837 | *config = NULL; | |
428870ff | 4838 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 4839 | |
9babb374 BB |
4840 | if (spa != NULL) { |
4841 | /* | |
4842 | * This still leaves a window of inconsistency where the spares | |
4843 | * or l2cache devices could change and the config would be | |
4844 | * self-inconsistent. | |
4845 | */ | |
4846 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 4847 | |
9babb374 | 4848 | if (*config != NULL) { |
572e2857 BB |
4849 | uint64_t loadtimes[2]; |
4850 | ||
4851 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
4852 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
4853 | VERIFY(nvlist_add_uint64_array(*config, | |
4854 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); | |
4855 | ||
b128c09f | 4856 | VERIFY(nvlist_add_uint64(*config, |
9babb374 BB |
4857 | ZPOOL_CONFIG_ERRCOUNT, |
4858 | spa_get_errlog_size(spa)) == 0); | |
4859 | ||
cec3a0a1 | 4860 | if (spa_suspended(spa)) { |
9babb374 BB |
4861 | VERIFY(nvlist_add_uint64(*config, |
4862 | ZPOOL_CONFIG_SUSPENDED, | |
4863 | spa->spa_failmode) == 0); | |
cec3a0a1 OF |
4864 | VERIFY(nvlist_add_uint64(*config, |
4865 | ZPOOL_CONFIG_SUSPENDED_REASON, | |
4866 | spa->spa_suspended) == 0); | |
4867 | } | |
b128c09f | 4868 | |
9babb374 BB |
4869 | spa_add_spares(spa, *config); |
4870 | spa_add_l2cache(spa, *config); | |
9ae529ec | 4871 | spa_add_feature_stats(spa, *config); |
9babb374 | 4872 | } |
34dc7c2f BB |
4873 | } |
4874 | ||
4875 | /* | |
4876 | * We want to get the alternate root even for faulted pools, so we cheat | |
4877 | * and call spa_lookup() directly. | |
4878 | */ | |
4879 | if (altroot) { | |
4880 | if (spa == NULL) { | |
4881 | mutex_enter(&spa_namespace_lock); | |
4882 | spa = spa_lookup(name); | |
4883 | if (spa) | |
4884 | spa_altroot(spa, altroot, buflen); | |
4885 | else | |
4886 | altroot[0] = '\0'; | |
4887 | spa = NULL; | |
4888 | mutex_exit(&spa_namespace_lock); | |
4889 | } else { | |
4890 | spa_altroot(spa, altroot, buflen); | |
4891 | } | |
4892 | } | |
4893 | ||
9babb374 BB |
4894 | if (spa != NULL) { |
4895 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 4896 | spa_close(spa, FTAG); |
9babb374 | 4897 | } |
34dc7c2f BB |
4898 | |
4899 | return (error); | |
4900 | } | |
4901 | ||
4902 | /* | |
4903 | * Validate that the auxiliary device array is well formed. We must have an | |
4904 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
4905 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
4906 | * specified, as long as they are well-formed. | |
4907 | */ | |
4908 | static int | |
4909 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
4910 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
4911 | vdev_labeltype_t label) | |
4912 | { | |
4913 | nvlist_t **dev; | |
4914 | uint_t i, ndev; | |
4915 | vdev_t *vd; | |
4916 | int error; | |
4917 | ||
b128c09f BB |
4918 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
4919 | ||
34dc7c2f BB |
4920 | /* |
4921 | * It's acceptable to have no devs specified. | |
4922 | */ | |
4923 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
4924 | return (0); | |
4925 | ||
4926 | if (ndev == 0) | |
2e528b49 | 4927 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
4928 | |
4929 | /* | |
4930 | * Make sure the pool is formatted with a version that supports this | |
4931 | * device type. | |
4932 | */ | |
4933 | if (spa_version(spa) < version) | |
2e528b49 | 4934 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
4935 | |
4936 | /* | |
4937 | * Set the pending device list so we correctly handle device in-use | |
4938 | * checking. | |
4939 | */ | |
4940 | sav->sav_pending = dev; | |
4941 | sav->sav_npending = ndev; | |
4942 | ||
4943 | for (i = 0; i < ndev; i++) { | |
4944 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
4945 | mode)) != 0) | |
4946 | goto out; | |
4947 | ||
4948 | if (!vd->vdev_ops->vdev_op_leaf) { | |
4949 | vdev_free(vd); | |
2e528b49 | 4950 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
4951 | goto out; |
4952 | } | |
4953 | ||
34dc7c2f BB |
4954 | vd->vdev_top = vd; |
4955 | ||
4956 | if ((error = vdev_open(vd)) == 0 && | |
4957 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
4958 | VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, | |
4959 | vd->vdev_guid) == 0); | |
4960 | } | |
4961 | ||
4962 | vdev_free(vd); | |
4963 | ||
4964 | if (error && | |
4965 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
4966 | goto out; | |
4967 | else | |
4968 | error = 0; | |
4969 | } | |
4970 | ||
4971 | out: | |
4972 | sav->sav_pending = NULL; | |
4973 | sav->sav_npending = 0; | |
4974 | return (error); | |
4975 | } | |
4976 | ||
4977 | static int | |
4978 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
4979 | { | |
4980 | int error; | |
4981 | ||
b128c09f BB |
4982 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
4983 | ||
34dc7c2f BB |
4984 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
4985 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
4986 | VDEV_LABEL_SPARE)) != 0) { | |
4987 | return (error); | |
4988 | } | |
4989 | ||
4990 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
4991 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
4992 | VDEV_LABEL_L2CACHE)); | |
4993 | } | |
4994 | ||
4995 | static void | |
4996 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
4997 | const char *config) | |
4998 | { | |
4999 | int i; | |
5000 | ||
5001 | if (sav->sav_config != NULL) { | |
5002 | nvlist_t **olddevs; | |
5003 | uint_t oldndevs; | |
5004 | nvlist_t **newdevs; | |
5005 | ||
5006 | /* | |
4e33ba4c | 5007 | * Generate new dev list by concatenating with the |
34dc7c2f BB |
5008 | * current dev list. |
5009 | */ | |
5010 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, | |
5011 | &olddevs, &oldndevs) == 0); | |
5012 | ||
5013 | newdevs = kmem_alloc(sizeof (void *) * | |
79c76d5b | 5014 | (ndevs + oldndevs), KM_SLEEP); |
34dc7c2f BB |
5015 | for (i = 0; i < oldndevs; i++) |
5016 | VERIFY(nvlist_dup(olddevs[i], &newdevs[i], | |
79c76d5b | 5017 | KM_SLEEP) == 0); |
34dc7c2f BB |
5018 | for (i = 0; i < ndevs; i++) |
5019 | VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], | |
79c76d5b | 5020 | KM_SLEEP) == 0); |
34dc7c2f BB |
5021 | |
5022 | VERIFY(nvlist_remove(sav->sav_config, config, | |
5023 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
5024 | ||
5025 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
5026 | config, newdevs, ndevs + oldndevs) == 0); | |
5027 | for (i = 0; i < oldndevs + ndevs; i++) | |
5028 | nvlist_free(newdevs[i]); | |
5029 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
5030 | } else { | |
5031 | /* | |
5032 | * Generate a new dev list. | |
5033 | */ | |
5034 | VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5035 | KM_SLEEP) == 0); |
34dc7c2f BB |
5036 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, |
5037 | devs, ndevs) == 0); | |
5038 | } | |
5039 | } | |
5040 | ||
5041 | /* | |
5042 | * Stop and drop level 2 ARC devices | |
5043 | */ | |
5044 | void | |
5045 | spa_l2cache_drop(spa_t *spa) | |
5046 | { | |
5047 | vdev_t *vd; | |
5048 | int i; | |
5049 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
5050 | ||
5051 | for (i = 0; i < sav->sav_count; i++) { | |
5052 | uint64_t pool; | |
5053 | ||
5054 | vd = sav->sav_vdevs[i]; | |
5055 | ASSERT(vd != NULL); | |
5056 | ||
fb5f0bc8 BB |
5057 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
5058 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 5059 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
5060 | } |
5061 | } | |
5062 | ||
b5256303 TC |
5063 | /* |
5064 | * Verify encryption parameters for spa creation. If we are encrypting, we must | |
5065 | * have the encryption feature flag enabled. | |
5066 | */ | |
5067 | static int | |
5068 | spa_create_check_encryption_params(dsl_crypto_params_t *dcp, | |
5069 | boolean_t has_encryption) | |
5070 | { | |
5071 | if (dcp->cp_crypt != ZIO_CRYPT_OFF && | |
5072 | dcp->cp_crypt != ZIO_CRYPT_INHERIT && | |
5073 | !has_encryption) | |
5074 | return (SET_ERROR(ENOTSUP)); | |
5075 | ||
1fff937a | 5076 | return (dmu_objset_create_crypt_check(NULL, dcp, NULL)); |
b5256303 TC |
5077 | } |
5078 | ||
34dc7c2f BB |
5079 | /* |
5080 | * Pool Creation | |
5081 | */ | |
5082 | int | |
5083 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b5256303 | 5084 | nvlist_t *zplprops, dsl_crypto_params_t *dcp) |
34dc7c2f BB |
5085 | { |
5086 | spa_t *spa; | |
5087 | char *altroot = NULL; | |
5088 | vdev_t *rvd; | |
5089 | dsl_pool_t *dp; | |
5090 | dmu_tx_t *tx; | |
9babb374 | 5091 | int error = 0; |
34dc7c2f BB |
5092 | uint64_t txg = TXG_INITIAL; |
5093 | nvlist_t **spares, **l2cache; | |
5094 | uint_t nspares, nl2cache; | |
52ce99dd | 5095 | uint64_t version, obj; |
9ae529ec | 5096 | boolean_t has_features; |
b5256303 TC |
5097 | boolean_t has_encryption; |
5098 | spa_feature_t feat; | |
5099 | char *feat_name; | |
83e9986f RY |
5100 | char *poolname; |
5101 | nvlist_t *nvl; | |
5102 | ||
cc99f275 DB |
5103 | if (props == NULL || |
5104 | nvlist_lookup_string(props, "tname", &poolname) != 0) | |
83e9986f | 5105 | poolname = (char *)pool; |
34dc7c2f BB |
5106 | |
5107 | /* | |
5108 | * If this pool already exists, return failure. | |
5109 | */ | |
5110 | mutex_enter(&spa_namespace_lock); | |
83e9986f | 5111 | if (spa_lookup(poolname) != NULL) { |
34dc7c2f | 5112 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5113 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5114 | } |
5115 | ||
5116 | /* | |
5117 | * Allocate a new spa_t structure. | |
5118 | */ | |
83e9986f RY |
5119 | nvl = fnvlist_alloc(); |
5120 | fnvlist_add_string(nvl, ZPOOL_CONFIG_POOL_NAME, pool); | |
34dc7c2f BB |
5121 | (void) nvlist_lookup_string(props, |
5122 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
83e9986f RY |
5123 | spa = spa_add(poolname, nvl, altroot); |
5124 | fnvlist_free(nvl); | |
fb5f0bc8 | 5125 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 5126 | |
34dc7c2f | 5127 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
5128 | spa_deactivate(spa); |
5129 | spa_remove(spa); | |
b128c09f | 5130 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
5131 | return (error); |
5132 | } | |
5133 | ||
83e9986f RY |
5134 | /* |
5135 | * Temporary pool names should never be written to disk. | |
5136 | */ | |
5137 | if (poolname != pool) | |
5138 | spa->spa_import_flags |= ZFS_IMPORT_TEMP_NAME; | |
5139 | ||
9ae529ec | 5140 | has_features = B_FALSE; |
b5256303 | 5141 | has_encryption = B_FALSE; |
1c27024e | 5142 | for (nvpair_t *elem = nvlist_next_nvpair(props, NULL); |
9ae529ec | 5143 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { |
b5256303 | 5144 | if (zpool_prop_feature(nvpair_name(elem))) { |
9ae529ec | 5145 | has_features = B_TRUE; |
b5256303 TC |
5146 | |
5147 | feat_name = strchr(nvpair_name(elem), '@') + 1; | |
5148 | VERIFY0(zfeature_lookup_name(feat_name, &feat)); | |
5149 | if (feat == SPA_FEATURE_ENCRYPTION) | |
5150 | has_encryption = B_TRUE; | |
5151 | } | |
5152 | } | |
5153 | ||
5154 | /* verify encryption params, if they were provided */ | |
5155 | if (dcp != NULL) { | |
5156 | error = spa_create_check_encryption_params(dcp, has_encryption); | |
5157 | if (error != 0) { | |
5158 | spa_deactivate(spa); | |
5159 | spa_remove(spa); | |
5160 | mutex_exit(&spa_namespace_lock); | |
5161 | return (error); | |
5162 | } | |
9ae529ec CS |
5163 | } |
5164 | ||
5165 | if (has_features || nvlist_lookup_uint64(props, | |
5166 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 5167 | version = SPA_VERSION; |
9ae529ec CS |
5168 | } |
5169 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
5170 | |
5171 | spa->spa_first_txg = txg; | |
5172 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
5173 | spa->spa_uberblock.ub_version = version; |
5174 | spa->spa_ubsync = spa->spa_uberblock; | |
3dfb57a3 | 5175 | spa->spa_load_state = SPA_LOAD_CREATE; |
a1d477c2 MA |
5176 | spa->spa_removing_phys.sr_state = DSS_NONE; |
5177 | spa->spa_removing_phys.sr_removing_vdev = -1; | |
5178 | spa->spa_removing_phys.sr_prev_indirect_vdev = -1; | |
34dc7c2f | 5179 | |
9babb374 BB |
5180 | /* |
5181 | * Create "The Godfather" zio to hold all async IOs | |
5182 | */ | |
e022864d MA |
5183 | spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *), |
5184 | KM_SLEEP); | |
1c27024e | 5185 | for (int i = 0; i < max_ncpus; i++) { |
e022864d MA |
5186 | spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, |
5187 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | | |
5188 | ZIO_FLAG_GODFATHER); | |
5189 | } | |
9babb374 | 5190 | |
34dc7c2f BB |
5191 | /* |
5192 | * Create the root vdev. | |
5193 | */ | |
b128c09f | 5194 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5195 | |
5196 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
5197 | ||
5198 | ASSERT(error != 0 || rvd != NULL); | |
5199 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
5200 | ||
5201 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
2e528b49 | 5202 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
5203 | |
5204 | if (error == 0 && | |
5205 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
5206 | (error = spa_validate_aux(spa, nvroot, txg, | |
5207 | VDEV_ALLOC_ADD)) == 0) { | |
cc99f275 DB |
5208 | /* |
5209 | * instantiate the metaslab groups (this will dirty the vdevs) | |
5210 | * we can no longer error exit past this point | |
5211 | */ | |
5212 | for (int c = 0; error == 0 && c < rvd->vdev_children; c++) { | |
5213 | vdev_t *vd = rvd->vdev_child[c]; | |
5214 | ||
5215 | vdev_metaslab_set_size(vd); | |
5216 | vdev_expand(vd, txg); | |
9babb374 | 5217 | } |
34dc7c2f BB |
5218 | } |
5219 | ||
b128c09f | 5220 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5221 | |
5222 | if (error != 0) { | |
5223 | spa_unload(spa); | |
5224 | spa_deactivate(spa); | |
5225 | spa_remove(spa); | |
5226 | mutex_exit(&spa_namespace_lock); | |
5227 | return (error); | |
5228 | } | |
5229 | ||
5230 | /* | |
5231 | * Get the list of spares, if specified. | |
5232 | */ | |
5233 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5234 | &spares, &nspares) == 0) { | |
5235 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, | |
79c76d5b | 5236 | KM_SLEEP) == 0); |
34dc7c2f BB |
5237 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
5238 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 5239 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5240 | spa_load_spares(spa); |
b128c09f | 5241 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5242 | spa->spa_spares.sav_sync = B_TRUE; |
5243 | } | |
5244 | ||
5245 | /* | |
5246 | * Get the list of level 2 cache devices, if specified. | |
5247 | */ | |
5248 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5249 | &l2cache, &nl2cache) == 0) { | |
5250 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 5251 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5252 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
5253 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 5254 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5255 | spa_load_l2cache(spa); |
b128c09f | 5256 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5257 | spa->spa_l2cache.sav_sync = B_TRUE; |
5258 | } | |
5259 | ||
9ae529ec | 5260 | spa->spa_is_initializing = B_TRUE; |
b5256303 | 5261 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, dcp, txg); |
9ae529ec | 5262 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 5263 | |
428870ff BB |
5264 | /* |
5265 | * Create DDTs (dedup tables). | |
5266 | */ | |
5267 | ddt_create(spa); | |
5268 | ||
5269 | spa_update_dspace(spa); | |
5270 | ||
34dc7c2f BB |
5271 | tx = dmu_tx_create_assigned(dp, txg); |
5272 | ||
d5e024cb BB |
5273 | /* |
5274 | * Create the pool's history object. | |
5275 | */ | |
5276 | if (version >= SPA_VERSION_ZPOOL_HISTORY && !spa->spa_history) | |
5277 | spa_history_create_obj(spa, tx); | |
5278 | ||
5279 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_CREATE); | |
5280 | spa_history_log_version(spa, "create", tx); | |
5281 | ||
34dc7c2f BB |
5282 | /* |
5283 | * Create the pool config object. | |
5284 | */ | |
5285 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 5286 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
5287 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
5288 | ||
5289 | if (zap_add(spa->spa_meta_objset, | |
5290 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
5291 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
5292 | cmn_err(CE_PANIC, "failed to add pool config"); | |
5293 | } | |
5294 | ||
428870ff BB |
5295 | if (zap_add(spa->spa_meta_objset, |
5296 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
5297 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
5298 | cmn_err(CE_PANIC, "failed to add pool version"); | |
5299 | } | |
5300 | ||
34dc7c2f BB |
5301 | /* Newly created pools with the right version are always deflated. */ |
5302 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
5303 | spa->spa_deflate = TRUE; | |
5304 | if (zap_add(spa->spa_meta_objset, | |
5305 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
5306 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
5307 | cmn_err(CE_PANIC, "failed to add deflate"); | |
5308 | } | |
5309 | } | |
5310 | ||
5311 | /* | |
428870ff | 5312 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
5313 | * because sync-to-convergence takes longer if the blocksize |
5314 | * keeps changing. | |
5315 | */ | |
428870ff BB |
5316 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
5317 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 5318 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 5319 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
5320 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
5321 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
5322 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 5323 | } |
428870ff BB |
5324 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
5325 | spa->spa_meta_objset, obj)); | |
34dc7c2f | 5326 | |
3c67d83a TH |
5327 | /* |
5328 | * Generate some random noise for salted checksums to operate on. | |
5329 | */ | |
5330 | (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes, | |
5331 | sizeof (spa->spa_cksum_salt.zcs_bytes)); | |
5332 | ||
34dc7c2f BB |
5333 | /* |
5334 | * Set pool properties. | |
5335 | */ | |
5336 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
5337 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
5338 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 5339 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
379ca9cf | 5340 | spa->spa_multihost = zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST); |
428870ff | 5341 | |
d164b209 BB |
5342 | if (props != NULL) { |
5343 | spa_configfile_set(spa, props, B_FALSE); | |
13fe0198 | 5344 | spa_sync_props(props, tx); |
d164b209 | 5345 | } |
34dc7c2f BB |
5346 | |
5347 | dmu_tx_commit(tx); | |
5348 | ||
5349 | spa->spa_sync_on = B_TRUE; | |
b5256303 | 5350 | txg_sync_start(dp); |
379ca9cf | 5351 | mmp_thread_start(spa); |
b5256303 | 5352 | txg_wait_synced(dp, txg); |
34dc7c2f | 5353 | |
9d5b5245 SD |
5354 | spa_spawn_aux_threads(spa); |
5355 | ||
a1d477c2 | 5356 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
34dc7c2f | 5357 | |
0c66c32d JG |
5358 | /* |
5359 | * Don't count references from objsets that are already closed | |
5360 | * and are making their way through the eviction process. | |
5361 | */ | |
5362 | spa_evicting_os_wait(spa); | |
424fd7c3 | 5363 | spa->spa_minref = zfs_refcount_count(&spa->spa_refcount); |
3dfb57a3 | 5364 | spa->spa_load_state = SPA_LOAD_NONE; |
b128c09f | 5365 | |
d164b209 BB |
5366 | mutex_exit(&spa_namespace_lock); |
5367 | ||
34dc7c2f BB |
5368 | return (0); |
5369 | } | |
5370 | ||
9babb374 BB |
5371 | /* |
5372 | * Import a non-root pool into the system. | |
5373 | */ | |
5374 | int | |
13fe0198 | 5375 | spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
5376 | { |
5377 | spa_t *spa; | |
5378 | char *altroot = NULL; | |
428870ff | 5379 | spa_load_state_t state = SPA_LOAD_IMPORT; |
8a393be3 | 5380 | zpool_load_policy_t policy; |
572e2857 BB |
5381 | uint64_t mode = spa_mode_global; |
5382 | uint64_t readonly = B_FALSE; | |
9babb374 | 5383 | int error; |
34dc7c2f BB |
5384 | nvlist_t *nvroot; |
5385 | nvlist_t **spares, **l2cache; | |
5386 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
5387 | |
5388 | /* | |
5389 | * If a pool with this name exists, return failure. | |
5390 | */ | |
5391 | mutex_enter(&spa_namespace_lock); | |
428870ff | 5392 | if (spa_lookup(pool) != NULL) { |
9babb374 | 5393 | mutex_exit(&spa_namespace_lock); |
2e528b49 | 5394 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
5395 | } |
5396 | ||
5397 | /* | |
5398 | * Create and initialize the spa structure. | |
5399 | */ | |
5400 | (void) nvlist_lookup_string(props, | |
5401 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
5402 | (void) nvlist_lookup_uint64(props, |
5403 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
5404 | if (readonly) | |
5405 | mode = FREAD; | |
428870ff | 5406 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
5407 | spa->spa_import_flags = flags; |
5408 | ||
5409 | /* | |
5410 | * Verbatim import - Take a pool and insert it into the namespace | |
5411 | * as if it had been loaded at boot. | |
5412 | */ | |
5413 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
5414 | if (props != NULL) | |
5415 | spa_configfile_set(spa, props, B_FALSE); | |
5416 | ||
a1d477c2 | 5417 | spa_write_cachefile(spa, B_FALSE, B_TRUE); |
12fa0466 | 5418 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
4a0ee12a | 5419 | zfs_dbgmsg("spa_import: verbatim import of %s", pool); |
572e2857 | 5420 | mutex_exit(&spa_namespace_lock); |
572e2857 BB |
5421 | return (0); |
5422 | } | |
5423 | ||
5424 | spa_activate(spa, mode); | |
34dc7c2f | 5425 | |
9babb374 BB |
5426 | /* |
5427 | * Don't start async tasks until we know everything is healthy. | |
5428 | */ | |
5429 | spa_async_suspend(spa); | |
b128c09f | 5430 | |
8a393be3 PZ |
5431 | zpool_get_load_policy(config, &policy); |
5432 | if (policy.zlp_rewind & ZPOOL_DO_REWIND) | |
572e2857 BB |
5433 | state = SPA_LOAD_RECOVER; |
5434 | ||
6cb8e530 | 5435 | spa->spa_config_source = SPA_CONFIG_SRC_TRYIMPORT; |
572e2857 | 5436 | |
6cb8e530 PZ |
5437 | if (state != SPA_LOAD_RECOVER) { |
5438 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
5439 | zfs_dbgmsg("spa_import: importing %s", pool); | |
5440 | } else { | |
5441 | zfs_dbgmsg("spa_import: importing %s, max_txg=%lld " | |
8a393be3 | 5442 | "(RECOVERY MODE)", pool, (longlong_t)policy.zlp_txg); |
6cb8e530 | 5443 | } |
8a393be3 | 5444 | error = spa_load_best(spa, state, policy.zlp_txg, policy.zlp_rewind); |
428870ff BB |
5445 | |
5446 | /* | |
572e2857 BB |
5447 | * Propagate anything learned while loading the pool and pass it |
5448 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 5449 | */ |
572e2857 BB |
5450 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
5451 | spa->spa_load_info) == 0); | |
34dc7c2f | 5452 | |
b128c09f | 5453 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5454 | /* |
9babb374 BB |
5455 | * Toss any existing sparelist, as it doesn't have any validity |
5456 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 5457 | */ |
9babb374 | 5458 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
5459 | nvlist_free(spa->spa_spares.sav_config); |
5460 | spa->spa_spares.sav_config = NULL; | |
5461 | spa_load_spares(spa); | |
5462 | } | |
9babb374 | 5463 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
5464 | nvlist_free(spa->spa_l2cache.sav_config); |
5465 | spa->spa_l2cache.sav_config = NULL; | |
5466 | spa_load_l2cache(spa); | |
5467 | } | |
5468 | ||
5469 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
5470 | &nvroot) == 0); | |
b128c09f | 5471 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 5472 | |
d164b209 BB |
5473 | if (props != NULL) |
5474 | spa_configfile_set(spa, props, B_FALSE); | |
5475 | ||
fb5f0bc8 BB |
5476 | if (error != 0 || (props && spa_writeable(spa) && |
5477 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
5478 | spa_unload(spa); |
5479 | spa_deactivate(spa); | |
5480 | spa_remove(spa); | |
34dc7c2f BB |
5481 | mutex_exit(&spa_namespace_lock); |
5482 | return (error); | |
5483 | } | |
5484 | ||
572e2857 BB |
5485 | spa_async_resume(spa); |
5486 | ||
34dc7c2f BB |
5487 | /* |
5488 | * Override any spares and level 2 cache devices as specified by | |
5489 | * the user, as these may have correct device names/devids, etc. | |
5490 | */ | |
5491 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
5492 | &spares, &nspares) == 0) { | |
5493 | if (spa->spa_spares.sav_config) | |
5494 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, | |
5495 | ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); | |
5496 | else | |
5497 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, | |
79c76d5b | 5498 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5499 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
5500 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 5501 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5502 | spa_load_spares(spa); |
b128c09f | 5503 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5504 | spa->spa_spares.sav_sync = B_TRUE; |
5505 | } | |
5506 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
5507 | &l2cache, &nl2cache) == 0) { | |
5508 | if (spa->spa_l2cache.sav_config) | |
5509 | VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, | |
5510 | ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); | |
5511 | else | |
5512 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
79c76d5b | 5513 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
5514 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
5515 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 5516 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5517 | spa_load_l2cache(spa); |
b128c09f | 5518 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5519 | spa->spa_l2cache.sav_sync = B_TRUE; |
5520 | } | |
5521 | ||
428870ff BB |
5522 | /* |
5523 | * Check for any removed devices. | |
5524 | */ | |
5525 | if (spa->spa_autoreplace) { | |
5526 | spa_aux_check_removed(&spa->spa_spares); | |
5527 | spa_aux_check_removed(&spa->spa_l2cache); | |
5528 | } | |
5529 | ||
fb5f0bc8 | 5530 | if (spa_writeable(spa)) { |
b128c09f BB |
5531 | /* |
5532 | * Update the config cache to include the newly-imported pool. | |
5533 | */ | |
45d1cae3 | 5534 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 5535 | } |
34dc7c2f | 5536 | |
34dc7c2f | 5537 | /* |
9babb374 BB |
5538 | * It's possible that the pool was expanded while it was exported. |
5539 | * We kick off an async task to handle this for us. | |
34dc7c2f | 5540 | */ |
9babb374 | 5541 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 5542 | |
d5e024cb | 5543 | spa_history_log_version(spa, "import", NULL); |
fb390aaf | 5544 | |
12fa0466 | 5545 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_IMPORT); |
fb390aaf | 5546 | |
a0bd735a | 5547 | zvol_create_minors(spa, pool, B_TRUE); |
526af785 | 5548 | |
fb390aaf HR |
5549 | mutex_exit(&spa_namespace_lock); |
5550 | ||
b128c09f BB |
5551 | return (0); |
5552 | } | |
5553 | ||
34dc7c2f BB |
5554 | nvlist_t * |
5555 | spa_tryimport(nvlist_t *tryconfig) | |
5556 | { | |
5557 | nvlist_t *config = NULL; | |
6cb8e530 | 5558 | char *poolname, *cachefile; |
34dc7c2f BB |
5559 | spa_t *spa; |
5560 | uint64_t state; | |
d164b209 | 5561 | int error; |
8a393be3 | 5562 | zpool_load_policy_t policy; |
34dc7c2f BB |
5563 | |
5564 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
5565 | return (NULL); | |
5566 | ||
5567 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
5568 | return (NULL); | |
5569 | ||
5570 | /* | |
5571 | * Create and initialize the spa structure. | |
5572 | */ | |
5573 | mutex_enter(&spa_namespace_lock); | |
428870ff | 5574 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
fb5f0bc8 | 5575 | spa_activate(spa, FREAD); |
34dc7c2f BB |
5576 | |
5577 | /* | |
8a393be3 | 5578 | * Rewind pool if a max txg was provided. |
34dc7c2f | 5579 | */ |
8a393be3 PZ |
5580 | zpool_get_load_policy(spa->spa_config, &policy); |
5581 | if (policy.zlp_txg != UINT64_MAX) { | |
5582 | spa->spa_load_max_txg = policy.zlp_txg; | |
6cb8e530 PZ |
5583 | spa->spa_extreme_rewind = B_TRUE; |
5584 | zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld", | |
8a393be3 | 5585 | poolname, (longlong_t)policy.zlp_txg); |
6cb8e530 PZ |
5586 | } else { |
5587 | zfs_dbgmsg("spa_tryimport: importing %s", poolname); | |
5588 | } | |
5589 | ||
5590 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_CACHEFILE, &cachefile) | |
5591 | == 0) { | |
5592 | zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile); | |
5593 | spa->spa_config_source = SPA_CONFIG_SRC_CACHEFILE; | |
5594 | } else { | |
5595 | spa->spa_config_source = SPA_CONFIG_SRC_SCAN; | |
5596 | } | |
5597 | ||
5598 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING); | |
34dc7c2f BB |
5599 | |
5600 | /* | |
5601 | * If 'tryconfig' was at least parsable, return the current config. | |
5602 | */ | |
5603 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 5604 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f BB |
5605 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, |
5606 | poolname) == 0); | |
5607 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
5608 | state) == 0); | |
5609 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
5610 | spa->spa_uberblock.ub_timestamp) == 0); | |
9ae529ec CS |
5611 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
5612 | spa->spa_load_info) == 0); | |
ffe9d382 BB |
5613 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, |
5614 | spa->spa_errata) == 0); | |
34dc7c2f BB |
5615 | |
5616 | /* | |
5617 | * If the bootfs property exists on this pool then we | |
5618 | * copy it out so that external consumers can tell which | |
5619 | * pools are bootable. | |
5620 | */ | |
d164b209 | 5621 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
79c76d5b | 5622 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
5623 | |
5624 | /* | |
5625 | * We have to play games with the name since the | |
5626 | * pool was opened as TRYIMPORT_NAME. | |
5627 | */ | |
b128c09f | 5628 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
5629 | spa->spa_bootfs, tmpname) == 0) { |
5630 | char *cp; | |
d1d7e268 MK |
5631 | char *dsname; |
5632 | ||
79c76d5b | 5633 | dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
34dc7c2f BB |
5634 | |
5635 | cp = strchr(tmpname, '/'); | |
5636 | if (cp == NULL) { | |
5637 | (void) strlcpy(dsname, tmpname, | |
5638 | MAXPATHLEN); | |
5639 | } else { | |
5640 | (void) snprintf(dsname, MAXPATHLEN, | |
5641 | "%s/%s", poolname, ++cp); | |
5642 | } | |
5643 | VERIFY(nvlist_add_string(config, | |
5644 | ZPOOL_CONFIG_BOOTFS, dsname) == 0); | |
5645 | kmem_free(dsname, MAXPATHLEN); | |
5646 | } | |
5647 | kmem_free(tmpname, MAXPATHLEN); | |
5648 | } | |
5649 | ||
5650 | /* | |
5651 | * Add the list of hot spares and level 2 cache devices. | |
5652 | */ | |
9babb374 | 5653 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
5654 | spa_add_spares(spa, config); |
5655 | spa_add_l2cache(spa, config); | |
9babb374 | 5656 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
5657 | } |
5658 | ||
5659 | spa_unload(spa); | |
5660 | spa_deactivate(spa); | |
5661 | spa_remove(spa); | |
5662 | mutex_exit(&spa_namespace_lock); | |
5663 | ||
5664 | return (config); | |
5665 | } | |
5666 | ||
5667 | /* | |
5668 | * Pool export/destroy | |
5669 | * | |
5670 | * The act of destroying or exporting a pool is very simple. We make sure there | |
5671 | * is no more pending I/O and any references to the pool are gone. Then, we | |
5672 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
5673 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
5674 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
5675 | */ |
5676 | static int | |
b128c09f | 5677 | spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 5678 | boolean_t force, boolean_t hardforce) |
34dc7c2f BB |
5679 | { |
5680 | spa_t *spa; | |
5681 | ||
5682 | if (oldconfig) | |
5683 | *oldconfig = NULL; | |
5684 | ||
fb5f0bc8 | 5685 | if (!(spa_mode_global & FWRITE)) |
2e528b49 | 5686 | return (SET_ERROR(EROFS)); |
34dc7c2f BB |
5687 | |
5688 | mutex_enter(&spa_namespace_lock); | |
5689 | if ((spa = spa_lookup(pool)) == NULL) { | |
5690 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5691 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
5692 | } |
5693 | ||
5694 | /* | |
5695 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
5696 | * reacquire the namespace lock, and see if we can export. | |
5697 | */ | |
5698 | spa_open_ref(spa, FTAG); | |
5699 | mutex_exit(&spa_namespace_lock); | |
5700 | spa_async_suspend(spa); | |
a0bd735a BP |
5701 | if (spa->spa_zvol_taskq) { |
5702 | zvol_remove_minors(spa, spa_name(spa), B_TRUE); | |
5703 | taskq_wait(spa->spa_zvol_taskq); | |
5704 | } | |
34dc7c2f BB |
5705 | mutex_enter(&spa_namespace_lock); |
5706 | spa_close(spa, FTAG); | |
5707 | ||
d14cfd83 IH |
5708 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) |
5709 | goto export_spa; | |
34dc7c2f | 5710 | /* |
d14cfd83 IH |
5711 | * The pool will be in core if it's openable, in which case we can |
5712 | * modify its state. Objsets may be open only because they're dirty, | |
5713 | * so we have to force it to sync before checking spa_refcnt. | |
34dc7c2f | 5714 | */ |
0c66c32d | 5715 | if (spa->spa_sync_on) { |
34dc7c2f | 5716 | txg_wait_synced(spa->spa_dsl_pool, 0); |
0c66c32d JG |
5717 | spa_evicting_os_wait(spa); |
5718 | } | |
34dc7c2f | 5719 | |
d14cfd83 IH |
5720 | /* |
5721 | * A pool cannot be exported or destroyed if there are active | |
5722 | * references. If we are resetting a pool, allow references by | |
5723 | * fault injection handlers. | |
5724 | */ | |
5725 | if (!spa_refcount_zero(spa) || | |
5726 | (spa->spa_inject_ref != 0 && | |
5727 | new_state != POOL_STATE_UNINITIALIZED)) { | |
5728 | spa_async_resume(spa); | |
5729 | mutex_exit(&spa_namespace_lock); | |
5730 | return (SET_ERROR(EBUSY)); | |
5731 | } | |
34dc7c2f | 5732 | |
d14cfd83 | 5733 | if (spa->spa_sync_on) { |
b128c09f BB |
5734 | /* |
5735 | * A pool cannot be exported if it has an active shared spare. | |
5736 | * This is to prevent other pools stealing the active spare | |
5737 | * from an exported pool. At user's own will, such pool can | |
5738 | * be forcedly exported. | |
5739 | */ | |
5740 | if (!force && new_state == POOL_STATE_EXPORTED && | |
5741 | spa_has_active_shared_spare(spa)) { | |
5742 | spa_async_resume(spa); | |
5743 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 5744 | return (SET_ERROR(EXDEV)); |
b128c09f | 5745 | } |
34dc7c2f | 5746 | |
619f0976 GW |
5747 | /* |
5748 | * We're about to export or destroy this pool. Make sure | |
5749 | * we stop all initializtion activity here before we | |
5750 | * set the spa_final_txg. This will ensure that all | |
5751 | * dirty data resulting from the initialization is | |
5752 | * committed to disk before we unload the pool. | |
5753 | */ | |
5754 | if (spa->spa_root_vdev != NULL) { | |
5755 | vdev_initialize_stop_all(spa->spa_root_vdev, | |
5756 | VDEV_INITIALIZE_ACTIVE); | |
5757 | } | |
5758 | ||
34dc7c2f BB |
5759 | /* |
5760 | * We want this to be reflected on every label, | |
5761 | * so mark them all dirty. spa_unload() will do the | |
5762 | * final sync that pushes these changes out. | |
5763 | */ | |
fb5f0bc8 | 5764 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 5765 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 5766 | spa->spa_state = new_state; |
428870ff BB |
5767 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
5768 | TXG_DEFER_SIZE + 1; | |
34dc7c2f | 5769 | vdev_config_dirty(spa->spa_root_vdev); |
b128c09f | 5770 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5771 | } |
5772 | } | |
5773 | ||
d14cfd83 | 5774 | export_spa: |
d5e024cb BB |
5775 | if (new_state == POOL_STATE_DESTROYED) |
5776 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_DESTROY); | |
5777 | else if (new_state == POOL_STATE_EXPORTED) | |
5778 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_POOL_EXPORT); | |
34dc7c2f BB |
5779 | |
5780 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
5781 | spa_unload(spa); | |
5782 | spa_deactivate(spa); | |
5783 | } | |
5784 | ||
5785 | if (oldconfig && spa->spa_config) | |
5786 | VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); | |
5787 | ||
5788 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 | 5789 | if (!hardforce) |
a1d477c2 | 5790 | spa_write_cachefile(spa, B_TRUE, B_TRUE); |
34dc7c2f | 5791 | spa_remove(spa); |
34dc7c2f BB |
5792 | } |
5793 | mutex_exit(&spa_namespace_lock); | |
5794 | ||
5795 | return (0); | |
5796 | } | |
5797 | ||
5798 | /* | |
5799 | * Destroy a storage pool. | |
5800 | */ | |
5801 | int | |
5802 | spa_destroy(char *pool) | |
5803 | { | |
fb5f0bc8 BB |
5804 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
5805 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
5806 | } |
5807 | ||
5808 | /* | |
5809 | * Export a storage pool. | |
5810 | */ | |
5811 | int | |
fb5f0bc8 BB |
5812 | spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, |
5813 | boolean_t hardforce) | |
34dc7c2f | 5814 | { |
fb5f0bc8 BB |
5815 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
5816 | force, hardforce)); | |
34dc7c2f BB |
5817 | } |
5818 | ||
5819 | /* | |
5820 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
5821 | * from the namespace in any way. | |
5822 | */ | |
5823 | int | |
5824 | spa_reset(char *pool) | |
5825 | { | |
b128c09f | 5826 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 5827 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
5828 | } |
5829 | ||
34dc7c2f BB |
5830 | /* |
5831 | * ========================================================================== | |
5832 | * Device manipulation | |
5833 | * ========================================================================== | |
5834 | */ | |
5835 | ||
5836 | /* | |
5837 | * Add a device to a storage pool. | |
5838 | */ | |
5839 | int | |
5840 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
5841 | { | |
428870ff | 5842 | uint64_t txg, id; |
fb5f0bc8 | 5843 | int error; |
34dc7c2f BB |
5844 | vdev_t *rvd = spa->spa_root_vdev; |
5845 | vdev_t *vd, *tvd; | |
5846 | nvlist_t **spares, **l2cache; | |
5847 | uint_t nspares, nl2cache; | |
5848 | ||
572e2857 BB |
5849 | ASSERT(spa_writeable(spa)); |
5850 | ||
34dc7c2f BB |
5851 | txg = spa_vdev_enter(spa); |
5852 | ||
5853 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
5854 | VDEV_ALLOC_ADD)) != 0) | |
5855 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5856 | ||
b128c09f | 5857 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
5858 | |
5859 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
5860 | &nspares) != 0) | |
5861 | nspares = 0; | |
5862 | ||
5863 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
5864 | &nl2cache) != 0) | |
5865 | nl2cache = 0; | |
5866 | ||
b128c09f | 5867 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 5868 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 5869 | |
b128c09f BB |
5870 | if (vd->vdev_children != 0 && |
5871 | (error = vdev_create(vd, txg, B_FALSE)) != 0) | |
5872 | return (spa_vdev_exit(spa, vd, txg, error)); | |
34dc7c2f BB |
5873 | |
5874 | /* | |
5875 | * We must validate the spares and l2cache devices after checking the | |
5876 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
5877 | */ | |
b128c09f | 5878 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 5879 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
5880 | |
5881 | /* | |
a1d477c2 MA |
5882 | * If we are in the middle of a device removal, we can only add |
5883 | * devices which match the existing devices in the pool. | |
5884 | * If we are in the middle of a removal, or have some indirect | |
5885 | * vdevs, we can not add raidz toplevels. | |
34dc7c2f | 5886 | */ |
a1d477c2 MA |
5887 | if (spa->spa_vdev_removal != NULL || |
5888 | spa->spa_removing_phys.sr_prev_indirect_vdev != -1) { | |
5889 | for (int c = 0; c < vd->vdev_children; c++) { | |
5890 | tvd = vd->vdev_child[c]; | |
5891 | if (spa->spa_vdev_removal != NULL && | |
9e052db4 | 5892 | tvd->vdev_ashift != spa->spa_max_ashift) { |
a1d477c2 MA |
5893 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
5894 | } | |
5895 | /* Fail if top level vdev is raidz */ | |
5896 | if (tvd->vdev_ops == &vdev_raidz_ops) { | |
5897 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); | |
5898 | } | |
5899 | /* | |
5900 | * Need the top level mirror to be | |
5901 | * a mirror of leaf vdevs only | |
5902 | */ | |
5903 | if (tvd->vdev_ops == &vdev_mirror_ops) { | |
5904 | for (uint64_t cid = 0; | |
5905 | cid < tvd->vdev_children; cid++) { | |
5906 | vdev_t *cvd = tvd->vdev_child[cid]; | |
5907 | if (!cvd->vdev_ops->vdev_op_leaf) { | |
5908 | return (spa_vdev_exit(spa, vd, | |
5909 | txg, EINVAL)); | |
5910 | } | |
5911 | } | |
5912 | } | |
5913 | } | |
5914 | } | |
5915 | ||
1c27024e | 5916 | for (int c = 0; c < vd->vdev_children; c++) { |
428870ff BB |
5917 | |
5918 | /* | |
5919 | * Set the vdev id to the first hole, if one exists. | |
5920 | */ | |
5921 | for (id = 0; id < rvd->vdev_children; id++) { | |
5922 | if (rvd->vdev_child[id]->vdev_ishole) { | |
5923 | vdev_free(rvd->vdev_child[id]); | |
5924 | break; | |
5925 | } | |
5926 | } | |
34dc7c2f BB |
5927 | tvd = vd->vdev_child[c]; |
5928 | vdev_remove_child(vd, tvd); | |
428870ff | 5929 | tvd->vdev_id = id; |
34dc7c2f BB |
5930 | vdev_add_child(rvd, tvd); |
5931 | vdev_config_dirty(tvd); | |
5932 | } | |
5933 | ||
5934 | if (nspares != 0) { | |
5935 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
5936 | ZPOOL_CONFIG_SPARES); | |
5937 | spa_load_spares(spa); | |
5938 | spa->spa_spares.sav_sync = B_TRUE; | |
5939 | } | |
5940 | ||
5941 | if (nl2cache != 0) { | |
5942 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
5943 | ZPOOL_CONFIG_L2CACHE); | |
5944 | spa_load_l2cache(spa); | |
5945 | spa->spa_l2cache.sav_sync = B_TRUE; | |
5946 | } | |
5947 | ||
5948 | /* | |
5949 | * We have to be careful when adding new vdevs to an existing pool. | |
5950 | * If other threads start allocating from these vdevs before we | |
5951 | * sync the config cache, and we lose power, then upon reboot we may | |
5952 | * fail to open the pool because there are DVAs that the config cache | |
5953 | * can't translate. Therefore, we first add the vdevs without | |
5954 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
5955 | * and then let spa_config_update() initialize the new metaslabs. | |
5956 | * | |
5957 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
5958 | * if we lose power at any point in this sequence, the remaining | |
5959 | * steps will be completed the next time we load the pool. | |
5960 | */ | |
5961 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
5962 | ||
5963 | mutex_enter(&spa_namespace_lock); | |
5964 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
12fa0466 | 5965 | spa_event_notify(spa, NULL, NULL, ESC_ZFS_VDEV_ADD); |
34dc7c2f BB |
5966 | mutex_exit(&spa_namespace_lock); |
5967 | ||
5968 | return (0); | |
5969 | } | |
5970 | ||
5971 | /* | |
5972 | * Attach a device to a mirror. The arguments are the path to any device | |
5973 | * in the mirror, and the nvroot for the new device. If the path specifies | |
5974 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
5975 | * | |
5976 | * If 'replacing' is specified, the new device is intended to replace the | |
5977 | * existing device; in this case the two devices are made into their own | |
5978 | * mirror using the 'replacing' vdev, which is functionally identical to | |
5979 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
5980 | * extra rules: you can't attach to it after it's been created, and upon | |
5981 | * completion of resilvering, the first disk (the one being replaced) | |
5982 | * is automatically detached. | |
5983 | */ | |
5984 | int | |
5985 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) | |
5986 | { | |
428870ff | 5987 | uint64_t txg, dtl_max_txg; |
1c27024e | 5988 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev); |
34dc7c2f BB |
5989 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
5990 | vdev_ops_t *pvops; | |
b128c09f BB |
5991 | char *oldvdpath, *newvdpath; |
5992 | int newvd_isspare; | |
5993 | int error; | |
34dc7c2f | 5994 | |
572e2857 BB |
5995 | ASSERT(spa_writeable(spa)); |
5996 | ||
34dc7c2f BB |
5997 | txg = spa_vdev_enter(spa); |
5998 | ||
b128c09f | 5999 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6000 | |
d2734cce SD |
6001 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6002 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6003 | error = (spa_has_checkpoint(spa)) ? | |
6004 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6005 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6006 | } | |
6007 | ||
9e052db4 | 6008 | if (spa->spa_vdev_removal != NULL) |
a1d477c2 | 6009 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
a1d477c2 | 6010 | |
34dc7c2f BB |
6011 | if (oldvd == NULL) |
6012 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6013 | ||
6014 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
6015 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6016 | ||
6017 | pvd = oldvd->vdev_parent; | |
6018 | ||
6019 | if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, | |
5ffb9d1d | 6020 | VDEV_ALLOC_ATTACH)) != 0) |
34dc7c2f BB |
6021 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
6022 | ||
6023 | if (newrootvd->vdev_children != 1) | |
6024 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6025 | ||
6026 | newvd = newrootvd->vdev_child[0]; | |
6027 | ||
6028 | if (!newvd->vdev_ops->vdev_op_leaf) | |
6029 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
6030 | ||
6031 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
6032 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
6033 | ||
6034 | /* | |
6035 | * Spares can't replace logs | |
6036 | */ | |
b128c09f | 6037 | if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) |
34dc7c2f BB |
6038 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
6039 | ||
6040 | if (!replacing) { | |
6041 | /* | |
6042 | * For attach, the only allowable parent is a mirror or the root | |
6043 | * vdev. | |
6044 | */ | |
6045 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
6046 | pvd->vdev_ops != &vdev_root_ops) | |
6047 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6048 | ||
6049 | pvops = &vdev_mirror_ops; | |
6050 | } else { | |
6051 | /* | |
6052 | * Active hot spares can only be replaced by inactive hot | |
6053 | * spares. | |
6054 | */ | |
6055 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 6056 | oldvd->vdev_isspare && |
34dc7c2f BB |
6057 | !spa_has_spare(spa, newvd->vdev_guid)) |
6058 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
6059 | ||
6060 | /* | |
6061 | * If the source is a hot spare, and the parent isn't already a | |
6062 | * spare, then we want to create a new hot spare. Otherwise, we | |
6063 | * want to create a replacing vdev. The user is not allowed to | |
6064 | * attach to a spared vdev child unless the 'isspare' state is | |
6065 | * the same (spare replaces spare, non-spare replaces | |
6066 | * non-spare). | |
6067 | */ | |
572e2857 BB |
6068 | if (pvd->vdev_ops == &vdev_replacing_ops && |
6069 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 6070 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6071 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
6072 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 6073 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
6074 | } |
6075 | ||
6076 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
6077 | pvops = &vdev_spare_ops; |
6078 | else | |
6079 | pvops = &vdev_replacing_ops; | |
6080 | } | |
6081 | ||
6082 | /* | |
9babb374 | 6083 | * Make sure the new device is big enough. |
34dc7c2f | 6084 | */ |
9babb374 | 6085 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
6086 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
6087 | ||
6088 | /* | |
6089 | * The new device cannot have a higher alignment requirement | |
6090 | * than the top-level vdev. | |
6091 | */ | |
6092 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
6093 | return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); | |
6094 | ||
6095 | /* | |
6096 | * If this is an in-place replacement, update oldvd's path and devid | |
6097 | * to make it distinguishable from newvd, and unopenable from now on. | |
6098 | */ | |
6099 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
6100 | spa_strfree(oldvd->vdev_path); | |
6101 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
79c76d5b | 6102 | KM_SLEEP); |
34dc7c2f BB |
6103 | (void) sprintf(oldvd->vdev_path, "%s/%s", |
6104 | newvd->vdev_path, "old"); | |
6105 | if (oldvd->vdev_devid != NULL) { | |
6106 | spa_strfree(oldvd->vdev_devid); | |
6107 | oldvd->vdev_devid = NULL; | |
6108 | } | |
6109 | } | |
6110 | ||
572e2857 | 6111 | /* mark the device being resilvered */ |
5d1f7fb6 | 6112 | newvd->vdev_resilver_txg = txg; |
572e2857 | 6113 | |
34dc7c2f BB |
6114 | /* |
6115 | * If the parent is not a mirror, or if we're replacing, insert the new | |
6116 | * mirror/replacing/spare vdev above oldvd. | |
6117 | */ | |
6118 | if (pvd->vdev_ops != pvops) | |
6119 | pvd = vdev_add_parent(oldvd, pvops); | |
6120 | ||
6121 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
6122 | ASSERT(pvd->vdev_ops == pvops); | |
6123 | ASSERT(oldvd->vdev_parent == pvd); | |
6124 | ||
6125 | /* | |
6126 | * Extract the new device from its root and add it to pvd. | |
6127 | */ | |
6128 | vdev_remove_child(newrootvd, newvd); | |
6129 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 6130 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
6131 | vdev_add_child(pvd, newvd); |
6132 | ||
6d82f98c IH |
6133 | /* |
6134 | * Reevaluate the parent vdev state. | |
6135 | */ | |
6136 | vdev_propagate_state(pvd); | |
6137 | ||
34dc7c2f BB |
6138 | tvd = newvd->vdev_top; |
6139 | ASSERT(pvd->vdev_top == tvd); | |
6140 | ASSERT(tvd->vdev_parent == rvd); | |
6141 | ||
6142 | vdev_config_dirty(tvd); | |
6143 | ||
6144 | /* | |
428870ff BB |
6145 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
6146 | * for any dmu_sync-ed blocks. It will propagate upward when | |
6147 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 6148 | */ |
428870ff | 6149 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 6150 | |
428870ff BB |
6151 | vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, |
6152 | dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 6153 | |
9babb374 | 6154 | if (newvd->vdev_isspare) { |
34dc7c2f | 6155 | spa_spare_activate(newvd); |
12fa0466 | 6156 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_SPARE); |
9babb374 BB |
6157 | } |
6158 | ||
b128c09f BB |
6159 | oldvdpath = spa_strdup(oldvd->vdev_path); |
6160 | newvdpath = spa_strdup(newvd->vdev_path); | |
6161 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
6162 | |
6163 | /* | |
6164 | * Mark newvd's DTL dirty in this txg. | |
6165 | */ | |
6166 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
6167 | ||
428870ff | 6168 | /* |
93cf2076 GW |
6169 | * Schedule the resilver to restart in the future. We do this to |
6170 | * ensure that dmu_sync-ed blocks have been stitched into the | |
80a91e74 TC |
6171 | * respective datasets. We do not do this if resilvers have been |
6172 | * deferred. | |
428870ff | 6173 | */ |
80a91e74 TC |
6174 | if (dsl_scan_resilvering(spa_get_dsl(spa)) && |
6175 | spa_feature_is_enabled(spa, SPA_FEATURE_RESILVER_DEFER)) | |
6176 | vdev_set_deferred_resilver(spa, newvd); | |
6177 | else | |
6178 | dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); | |
428870ff | 6179 | |
fb390aaf | 6180 | if (spa->spa_bootfs) |
12fa0466 | 6181 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_BOOTFS_VDEV_ATTACH); |
fb390aaf | 6182 | |
12fa0466 | 6183 | spa_event_notify(spa, newvd, NULL, ESC_ZFS_VDEV_ATTACH); |
fb390aaf | 6184 | |
428870ff BB |
6185 | /* |
6186 | * Commit the config | |
6187 | */ | |
6188 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 6189 | |
6f1ffb06 | 6190 | spa_history_log_internal(spa, "vdev attach", NULL, |
428870ff | 6191 | "%s vdev=%s %s vdev=%s", |
45d1cae3 BB |
6192 | replacing && newvd_isspare ? "spare in" : |
6193 | replacing ? "replace" : "attach", newvdpath, | |
6194 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
6195 | |
6196 | spa_strfree(oldvdpath); | |
6197 | spa_strfree(newvdpath); | |
6198 | ||
34dc7c2f BB |
6199 | return (0); |
6200 | } | |
6201 | ||
6202 | /* | |
6203 | * Detach a device from a mirror or replacing vdev. | |
d3cc8b15 | 6204 | * |
34dc7c2f BB |
6205 | * If 'replace_done' is specified, only detach if the parent |
6206 | * is a replacing vdev. | |
6207 | */ | |
6208 | int | |
fb5f0bc8 | 6209 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
6210 | { |
6211 | uint64_t txg; | |
fb5f0bc8 | 6212 | int error; |
1c27024e | 6213 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev); |
34dc7c2f BB |
6214 | vdev_t *vd, *pvd, *cvd, *tvd; |
6215 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 6216 | uint64_t unspare_guid = 0; |
428870ff | 6217 | char *vdpath; |
1c27024e | 6218 | |
572e2857 BB |
6219 | ASSERT(spa_writeable(spa)); |
6220 | ||
34dc7c2f BB |
6221 | txg = spa_vdev_enter(spa); |
6222 | ||
b128c09f | 6223 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f | 6224 | |
d2734cce SD |
6225 | /* |
6226 | * Besides being called directly from the userland through the | |
6227 | * ioctl interface, spa_vdev_detach() can be potentially called | |
6228 | * at the end of spa_vdev_resilver_done(). | |
6229 | * | |
6230 | * In the regular case, when we have a checkpoint this shouldn't | |
6231 | * happen as we never empty the DTLs of a vdev during the scrub | |
6232 | * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done() | |
6233 | * should never get here when we have a checkpoint. | |
6234 | * | |
6235 | * That said, even in a case when we checkpoint the pool exactly | |
6236 | * as spa_vdev_resilver_done() calls this function everything | |
6237 | * should be fine as the resilver will return right away. | |
6238 | */ | |
6239 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
6240 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6241 | error = (spa_has_checkpoint(spa)) ? | |
6242 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6243 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6244 | } | |
6245 | ||
34dc7c2f BB |
6246 | if (vd == NULL) |
6247 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
6248 | ||
6249 | if (!vd->vdev_ops->vdev_op_leaf) | |
6250 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6251 | ||
6252 | pvd = vd->vdev_parent; | |
6253 | ||
fb5f0bc8 BB |
6254 | /* |
6255 | * If the parent/child relationship is not as expected, don't do it. | |
6256 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
6257 | * vdev that's replacing B with C. The user's intent in replacing | |
6258 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
6259 | * the replace by detaching C, the expected behavior is to end up | |
6260 | * M(A,B). But suppose that right after deciding to detach C, | |
6261 | * the replacement of B completes. We would have M(A,C), and then | |
6262 | * ask to detach C, which would leave us with just A -- not what | |
6263 | * the user wanted. To prevent this, we make sure that the | |
6264 | * parent/child relationship hasn't changed -- in this example, | |
6265 | * that C's parent is still the replacing vdev R. | |
6266 | */ | |
6267 | if (pvd->vdev_guid != pguid && pguid != 0) | |
6268 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
6269 | ||
34dc7c2f | 6270 | /* |
572e2857 | 6271 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 6272 | */ |
572e2857 BB |
6273 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
6274 | pvd->vdev_ops != &vdev_spare_ops) | |
6275 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
6276 | |
6277 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
6278 | spa_version(spa) >= SPA_VERSION_SPARES); | |
6279 | ||
6280 | /* | |
6281 | * Only mirror, replacing, and spare vdevs support detach. | |
6282 | */ | |
6283 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
6284 | pvd->vdev_ops != &vdev_mirror_ops && | |
6285 | pvd->vdev_ops != &vdev_spare_ops) | |
6286 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
6287 | ||
6288 | /* | |
fb5f0bc8 BB |
6289 | * If this device has the only valid copy of some data, |
6290 | * we cannot safely detach it. | |
34dc7c2f | 6291 | */ |
fb5f0bc8 | 6292 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
6293 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
6294 | ||
fb5f0bc8 | 6295 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 6296 | |
b128c09f BB |
6297 | /* |
6298 | * If we are detaching the second disk from a replacing vdev, then | |
6299 | * check to see if we changed the original vdev's path to have "/old" | |
6300 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
6301 | */ | |
572e2857 BB |
6302 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
6303 | vd->vdev_path != NULL) { | |
6304 | size_t len = strlen(vd->vdev_path); | |
6305 | ||
1c27024e | 6306 | for (int c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
6307 | cvd = pvd->vdev_child[c]; |
6308 | ||
6309 | if (cvd == vd || cvd->vdev_path == NULL) | |
6310 | continue; | |
6311 | ||
6312 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
6313 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
6314 | spa_strfree(cvd->vdev_path); | |
6315 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
6316 | break; | |
6317 | } | |
b128c09f BB |
6318 | } |
6319 | } | |
6320 | ||
34dc7c2f BB |
6321 | /* |
6322 | * If we are detaching the original disk from a spare, then it implies | |
6323 | * that the spare should become a real disk, and be removed from the | |
6324 | * active spare list for the pool. | |
6325 | */ | |
6326 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 BB |
6327 | vd->vdev_id == 0 && |
6328 | pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) | |
34dc7c2f BB |
6329 | unspare = B_TRUE; |
6330 | ||
6331 | /* | |
6332 | * Erase the disk labels so the disk can be used for other things. | |
6333 | * This must be done after all other error cases are handled, | |
6334 | * but before we disembowel vd (so we can still do I/O to it). | |
6335 | * But if we can't do it, don't treat the error as fatal -- | |
6336 | * it may be that the unwritability of the disk is the reason | |
6337 | * it's being detached! | |
6338 | */ | |
6339 | error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
6340 | ||
6341 | /* | |
6342 | * Remove vd from its parent and compact the parent's children. | |
6343 | */ | |
6344 | vdev_remove_child(pvd, vd); | |
6345 | vdev_compact_children(pvd); | |
6346 | ||
6347 | /* | |
6348 | * Remember one of the remaining children so we can get tvd below. | |
6349 | */ | |
572e2857 | 6350 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
6351 | |
6352 | /* | |
6353 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
6354 | * do it now, marking the vdev as no longer a spare in the process. |
6355 | * We must do this before vdev_remove_parent(), because that can | |
6356 | * change the GUID if it creates a new toplevel GUID. For a similar | |
6357 | * reason, we must remove the spare now, in the same txg as the detach; | |
6358 | * otherwise someone could attach a new sibling, change the GUID, and | |
6359 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
6360 | */ |
6361 | if (unspare) { | |
6362 | ASSERT(cvd->vdev_isspare); | |
6363 | spa_spare_remove(cvd); | |
6364 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 6365 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 6366 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
6367 | } |
6368 | ||
428870ff BB |
6369 | /* |
6370 | * If the parent mirror/replacing vdev only has one child, | |
6371 | * the parent is no longer needed. Remove it from the tree. | |
6372 | */ | |
572e2857 BB |
6373 | if (pvd->vdev_children == 1) { |
6374 | if (pvd->vdev_ops == &vdev_spare_ops) | |
6375 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 6376 | vdev_remove_parent(cvd); |
572e2857 BB |
6377 | } |
6378 | ||
428870ff BB |
6379 | |
6380 | /* | |
6381 | * We don't set tvd until now because the parent we just removed | |
6382 | * may have been the previous top-level vdev. | |
6383 | */ | |
6384 | tvd = cvd->vdev_top; | |
6385 | ASSERT(tvd->vdev_parent == rvd); | |
6386 | ||
6387 | /* | |
6388 | * Reevaluate the parent vdev state. | |
6389 | */ | |
6390 | vdev_propagate_state(cvd); | |
6391 | ||
6392 | /* | |
6393 | * If the 'autoexpand' property is set on the pool then automatically | |
6394 | * try to expand the size of the pool. For example if the device we | |
6395 | * just detached was smaller than the others, it may be possible to | |
6396 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
6397 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
6398 | */ | |
6399 | if (spa->spa_autoexpand) { | |
6400 | vdev_reopen(tvd); | |
6401 | vdev_expand(tvd, txg); | |
6402 | } | |
6403 | ||
6404 | vdev_config_dirty(tvd); | |
6405 | ||
6406 | /* | |
6407 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
6408 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
6409 | * But first make sure we're not on any *other* txg's DTL list, to | |
6410 | * prevent vd from being accessed after it's freed. | |
6411 | */ | |
b6ca6193 | 6412 | vdpath = spa_strdup(vd->vdev_path ? vd->vdev_path : "none"); |
1c27024e | 6413 | for (int t = 0; t < TXG_SIZE; t++) |
428870ff BB |
6414 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
6415 | vd->vdev_detached = B_TRUE; | |
6416 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
6417 | ||
12fa0466 | 6418 | spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_REMOVE); |
428870ff | 6419 | |
572e2857 BB |
6420 | /* hang on to the spa before we release the lock */ |
6421 | spa_open_ref(spa, FTAG); | |
6422 | ||
428870ff BB |
6423 | error = spa_vdev_exit(spa, vd, txg, 0); |
6424 | ||
6f1ffb06 | 6425 | spa_history_log_internal(spa, "detach", NULL, |
428870ff BB |
6426 | "vdev=%s", vdpath); |
6427 | spa_strfree(vdpath); | |
6428 | ||
6429 | /* | |
6430 | * If this was the removal of the original device in a hot spare vdev, | |
6431 | * then we want to go through and remove the device from the hot spare | |
6432 | * list of every other pool. | |
6433 | */ | |
6434 | if (unspare) { | |
572e2857 BB |
6435 | spa_t *altspa = NULL; |
6436 | ||
428870ff | 6437 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
6438 | while ((altspa = spa_next(altspa)) != NULL) { |
6439 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
6440 | altspa == spa) | |
428870ff | 6441 | continue; |
572e2857 BB |
6442 | |
6443 | spa_open_ref(altspa, FTAG); | |
428870ff | 6444 | mutex_exit(&spa_namespace_lock); |
572e2857 | 6445 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 6446 | mutex_enter(&spa_namespace_lock); |
572e2857 | 6447 | spa_close(altspa, FTAG); |
428870ff BB |
6448 | } |
6449 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
6450 | |
6451 | /* search the rest of the vdevs for spares to remove */ | |
6452 | spa_vdev_resilver_done(spa); | |
428870ff BB |
6453 | } |
6454 | ||
572e2857 BB |
6455 | /* all done with the spa; OK to release */ |
6456 | mutex_enter(&spa_namespace_lock); | |
6457 | spa_close(spa, FTAG); | |
6458 | mutex_exit(&spa_namespace_lock); | |
6459 | ||
428870ff BB |
6460 | return (error); |
6461 | } | |
6462 | ||
c10d37dd GW |
6463 | static int |
6464 | spa_vdev_initialize_impl(spa_t *spa, uint64_t guid, uint64_t cmd_type, | |
6465 | list_t *vd_list) | |
619f0976 | 6466 | { |
c10d37dd GW |
6467 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6468 | ||
619f0976 GW |
6469 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); |
6470 | ||
6471 | /* Look up vdev and ensure it's a leaf. */ | |
6472 | vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); | |
6473 | if (vd == NULL || vd->vdev_detached) { | |
6474 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
6475 | return (SET_ERROR(ENODEV)); |
6476 | } else if (!vd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(vd)) { | |
6477 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
6478 | return (SET_ERROR(EINVAL)); |
6479 | } else if (!vdev_writeable(vd)) { | |
6480 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
619f0976 GW |
6481 | return (SET_ERROR(EROFS)); |
6482 | } | |
6483 | mutex_enter(&vd->vdev_initialize_lock); | |
6484 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
6485 | ||
6486 | /* | |
6487 | * When we activate an initialize action we check to see | |
6488 | * if the vdev_initialize_thread is NULL. We do this instead | |
6489 | * of using the vdev_initialize_state since there might be | |
6490 | * a previous initialization process which has completed but | |
6491 | * the thread is not exited. | |
6492 | */ | |
6493 | if (cmd_type == POOL_INITIALIZE_DO && | |
6494 | (vd->vdev_initialize_thread != NULL || | |
6495 | vd->vdev_top->vdev_removing)) { | |
6496 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
6497 | return (SET_ERROR(EBUSY)); |
6498 | } else if (cmd_type == POOL_INITIALIZE_CANCEL && | |
6499 | (vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE && | |
6500 | vd->vdev_initialize_state != VDEV_INITIALIZE_SUSPENDED)) { | |
6501 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
6502 | return (SET_ERROR(ESRCH)); |
6503 | } else if (cmd_type == POOL_INITIALIZE_SUSPEND && | |
6504 | vd->vdev_initialize_state != VDEV_INITIALIZE_ACTIVE) { | |
6505 | mutex_exit(&vd->vdev_initialize_lock); | |
619f0976 GW |
6506 | return (SET_ERROR(ESRCH)); |
6507 | } | |
6508 | ||
6509 | switch (cmd_type) { | |
6510 | case POOL_INITIALIZE_DO: | |
6511 | vdev_initialize(vd); | |
6512 | break; | |
6513 | case POOL_INITIALIZE_CANCEL: | |
c10d37dd | 6514 | vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, vd_list); |
619f0976 GW |
6515 | break; |
6516 | case POOL_INITIALIZE_SUSPEND: | |
c10d37dd | 6517 | vdev_initialize_stop(vd, VDEV_INITIALIZE_SUSPENDED, vd_list); |
619f0976 GW |
6518 | break; |
6519 | default: | |
6520 | panic("invalid cmd_type %llu", (unsigned long long)cmd_type); | |
6521 | } | |
6522 | mutex_exit(&vd->vdev_initialize_lock); | |
6523 | ||
c10d37dd GW |
6524 | return (0); |
6525 | } | |
6526 | ||
6527 | int | |
6528 | spa_vdev_initialize(spa_t *spa, nvlist_t *nv, uint64_t cmd_type, | |
6529 | nvlist_t *vdev_errlist) | |
6530 | { | |
6531 | int total_errors = 0; | |
6532 | list_t vd_list; | |
6533 | ||
6534 | list_create(&vd_list, sizeof (vdev_t), | |
6535 | offsetof(vdev_t, vdev_initialize_node)); | |
6536 | ||
6537 | /* | |
6538 | * We hold the namespace lock through the whole function | |
6539 | * to prevent any changes to the pool while we're starting or | |
6540 | * stopping initialization. The config and state locks are held so that | |
6541 | * we can properly assess the vdev state before we commit to | |
6542 | * the initializing operation. | |
6543 | */ | |
6544 | mutex_enter(&spa_namespace_lock); | |
6545 | ||
6546 | for (nvpair_t *pair = nvlist_next_nvpair(nv, NULL); | |
6547 | pair != NULL; pair = nvlist_next_nvpair(nv, pair)) { | |
6548 | uint64_t vdev_guid = fnvpair_value_uint64(pair); | |
6549 | ||
6550 | int error = spa_vdev_initialize_impl(spa, vdev_guid, cmd_type, | |
6551 | &vd_list); | |
6552 | if (error != 0) { | |
6553 | char guid_as_str[MAXNAMELEN]; | |
6554 | ||
6555 | (void) snprintf(guid_as_str, sizeof (guid_as_str), | |
6556 | "%llu", (unsigned long long)vdev_guid); | |
6557 | fnvlist_add_int64(vdev_errlist, guid_as_str, error); | |
6558 | total_errors++; | |
6559 | } | |
6560 | } | |
6561 | ||
6562 | /* Wait for all initialize threads to stop. */ | |
6563 | vdev_initialize_stop_wait(spa, &vd_list); | |
6564 | ||
619f0976 GW |
6565 | /* Sync out the initializing state */ |
6566 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
6567 | mutex_exit(&spa_namespace_lock); | |
6568 | ||
c10d37dd | 6569 | list_destroy(&vd_list); |
619f0976 | 6570 | |
c10d37dd GW |
6571 | return (total_errors); |
6572 | } | |
619f0976 | 6573 | |
428870ff BB |
6574 | /* |
6575 | * Split a set of devices from their mirrors, and create a new pool from them. | |
6576 | */ | |
6577 | int | |
6578 | spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, | |
6579 | nvlist_t *props, boolean_t exp) | |
6580 | { | |
6581 | int error = 0; | |
6582 | uint64_t txg, *glist; | |
6583 | spa_t *newspa; | |
6584 | uint_t c, children, lastlog; | |
6585 | nvlist_t **child, *nvl, *tmp; | |
6586 | dmu_tx_t *tx; | |
6587 | char *altroot = NULL; | |
6588 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
6589 | boolean_t activate_slog; | |
6590 | ||
572e2857 | 6591 | ASSERT(spa_writeable(spa)); |
428870ff BB |
6592 | |
6593 | txg = spa_vdev_enter(spa); | |
6594 | ||
d2734cce SD |
6595 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
6596 | if (spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) { | |
6597 | error = (spa_has_checkpoint(spa)) ? | |
6598 | ZFS_ERR_CHECKPOINT_EXISTS : ZFS_ERR_DISCARDING_CHECKPOINT; | |
6599 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6600 | } | |
6601 | ||
428870ff BB |
6602 | /* clear the log and flush everything up to now */ |
6603 | activate_slog = spa_passivate_log(spa); | |
6604 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
a1d477c2 | 6605 | error = spa_reset_logs(spa); |
428870ff BB |
6606 | txg = spa_vdev_config_enter(spa); |
6607 | ||
6608 | if (activate_slog) | |
6609 | spa_activate_log(spa); | |
6610 | ||
6611 | if (error != 0) | |
6612 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6613 | ||
6614 | /* check new spa name before going any further */ | |
6615 | if (spa_lookup(newname) != NULL) | |
6616 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
6617 | ||
6618 | /* | |
6619 | * scan through all the children to ensure they're all mirrors | |
6620 | */ | |
6621 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
6622 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
6623 | &children) != 0) | |
6624 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
6625 | ||
6626 | /* first, check to ensure we've got the right child count */ | |
6627 | rvd = spa->spa_root_vdev; | |
6628 | lastlog = 0; | |
6629 | for (c = 0; c < rvd->vdev_children; c++) { | |
6630 | vdev_t *vd = rvd->vdev_child[c]; | |
6631 | ||
6632 | /* don't count the holes & logs as children */ | |
a1d477c2 | 6633 | if (vd->vdev_islog || !vdev_is_concrete(vd)) { |
428870ff BB |
6634 | if (lastlog == 0) |
6635 | lastlog = c; | |
6636 | continue; | |
6637 | } | |
6638 | ||
6639 | lastlog = 0; | |
6640 | } | |
6641 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
6642 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
6643 | ||
6644 | /* next, ensure no spare or cache devices are part of the split */ | |
6645 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
6646 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
6647 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
6648 | ||
79c76d5b BB |
6649 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP); |
6650 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP); | |
428870ff BB |
6651 | |
6652 | /* then, loop over each vdev and validate it */ | |
6653 | for (c = 0; c < children; c++) { | |
6654 | uint64_t is_hole = 0; | |
6655 | ||
6656 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
6657 | &is_hole); | |
6658 | ||
6659 | if (is_hole != 0) { | |
6660 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
6661 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
6662 | continue; | |
6663 | } else { | |
2e528b49 | 6664 | error = SET_ERROR(EINVAL); |
428870ff BB |
6665 | break; |
6666 | } | |
6667 | } | |
6668 | ||
6669 | /* which disk is going to be split? */ | |
6670 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
6671 | &glist[c]) != 0) { | |
2e528b49 | 6672 | error = SET_ERROR(EINVAL); |
428870ff BB |
6673 | break; |
6674 | } | |
6675 | ||
6676 | /* look it up in the spa */ | |
6677 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
6678 | if (vml[c] == NULL) { | |
2e528b49 | 6679 | error = SET_ERROR(ENODEV); |
428870ff BB |
6680 | break; |
6681 | } | |
6682 | ||
6683 | /* make sure there's nothing stopping the split */ | |
6684 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
6685 | vml[c]->vdev_islog || | |
a1d477c2 | 6686 | !vdev_is_concrete(vml[c]) || |
428870ff BB |
6687 | vml[c]->vdev_isspare || |
6688 | vml[c]->vdev_isl2cache || | |
6689 | !vdev_writeable(vml[c]) || | |
6690 | vml[c]->vdev_children != 0 || | |
6691 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
6692 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
2e528b49 | 6693 | error = SET_ERROR(EINVAL); |
428870ff BB |
6694 | break; |
6695 | } | |
6696 | ||
733b5722 RS |
6697 | if (vdev_dtl_required(vml[c]) || |
6698 | vdev_resilver_needed(vml[c], NULL, NULL)) { | |
2e528b49 | 6699 | error = SET_ERROR(EBUSY); |
428870ff BB |
6700 | break; |
6701 | } | |
6702 | ||
6703 | /* we need certain info from the top level */ | |
6704 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, | |
6705 | vml[c]->vdev_top->vdev_ms_array) == 0); | |
6706 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
6707 | vml[c]->vdev_top->vdev_ms_shift) == 0); | |
6708 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
6709 | vml[c]->vdev_top->vdev_asize) == 0); | |
6710 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
6711 | vml[c]->vdev_top->vdev_ashift) == 0); | |
e0ab3ab5 JS |
6712 | |
6713 | /* transfer per-vdev ZAPs */ | |
6714 | ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0); | |
6715 | VERIFY0(nvlist_add_uint64(child[c], | |
6716 | ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap)); | |
6717 | ||
6718 | ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0); | |
6719 | VERIFY0(nvlist_add_uint64(child[c], | |
6720 | ZPOOL_CONFIG_VDEV_TOP_ZAP, | |
6721 | vml[c]->vdev_parent->vdev_top_zap)); | |
428870ff BB |
6722 | } |
6723 | ||
6724 | if (error != 0) { | |
6725 | kmem_free(vml, children * sizeof (vdev_t *)); | |
6726 | kmem_free(glist, children * sizeof (uint64_t)); | |
6727 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
6728 | } | |
6729 | ||
6730 | /* stop writers from using the disks */ | |
6731 | for (c = 0; c < children; c++) { | |
6732 | if (vml[c] != NULL) | |
6733 | vml[c]->vdev_offline = B_TRUE; | |
6734 | } | |
6735 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
6736 | |
6737 | /* | |
428870ff BB |
6738 | * Temporarily record the splitting vdevs in the spa config. This |
6739 | * will disappear once the config is regenerated. | |
34dc7c2f | 6740 | */ |
79c76d5b | 6741 | VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
6742 | VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, |
6743 | glist, children) == 0); | |
6744 | kmem_free(glist, children * sizeof (uint64_t)); | |
34dc7c2f | 6745 | |
428870ff BB |
6746 | mutex_enter(&spa->spa_props_lock); |
6747 | VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, | |
6748 | nvl) == 0); | |
6749 | mutex_exit(&spa->spa_props_lock); | |
6750 | spa->spa_config_splitting = nvl; | |
6751 | vdev_config_dirty(spa->spa_root_vdev); | |
6752 | ||
6753 | /* configure and create the new pool */ | |
6754 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); | |
6755 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
6756 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); | |
6757 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
6758 | spa_version(spa)) == 0); | |
6759 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
6760 | spa->spa_config_txg) == 0); | |
6761 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
6762 | spa_generate_guid(NULL)) == 0); | |
e0ab3ab5 | 6763 | VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)); |
428870ff BB |
6764 | (void) nvlist_lookup_string(props, |
6765 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 6766 | |
428870ff BB |
6767 | /* add the new pool to the namespace */ |
6768 | newspa = spa_add(newname, config, altroot); | |
e0ab3ab5 | 6769 | newspa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
6770 | newspa->spa_config_txg = spa->spa_config_txg; |
6771 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
6772 | ||
6773 | /* release the spa config lock, retaining the namespace lock */ | |
6774 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
6775 | ||
6776 | if (zio_injection_enabled) | |
6777 | zio_handle_panic_injection(spa, FTAG, 1); | |
6778 | ||
6779 | spa_activate(newspa, spa_mode_global); | |
6780 | spa_async_suspend(newspa); | |
6781 | ||
c10d37dd GW |
6782 | /* |
6783 | * Temporarily stop the initializing activity. We set the state to | |
6784 | * ACTIVE so that we know to resume the initializing once the split | |
6785 | * has completed. | |
6786 | */ | |
6787 | list_t vd_list; | |
6788 | list_create(&vd_list, sizeof (vdev_t), | |
6789 | offsetof(vdev_t, vdev_initialize_node)); | |
6790 | ||
619f0976 GW |
6791 | for (c = 0; c < children; c++) { |
6792 | if (vml[c] != NULL) { | |
619f0976 | 6793 | mutex_enter(&vml[c]->vdev_initialize_lock); |
c10d37dd GW |
6794 | vdev_initialize_stop(vml[c], VDEV_INITIALIZE_ACTIVE, |
6795 | &vd_list); | |
619f0976 GW |
6796 | mutex_exit(&vml[c]->vdev_initialize_lock); |
6797 | } | |
6798 | } | |
c10d37dd GW |
6799 | vdev_initialize_stop_wait(spa, &vd_list); |
6800 | list_destroy(&vd_list); | |
619f0976 | 6801 | |
6cb8e530 PZ |
6802 | newspa->spa_config_source = SPA_CONFIG_SRC_SPLIT; |
6803 | ||
428870ff | 6804 | /* create the new pool from the disks of the original pool */ |
6cb8e530 | 6805 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE); |
428870ff BB |
6806 | if (error) |
6807 | goto out; | |
6808 | ||
6809 | /* if that worked, generate a real config for the new pool */ | |
6810 | if (newspa->spa_root_vdev != NULL) { | |
6811 | VERIFY(nvlist_alloc(&newspa->spa_config_splitting, | |
79c76d5b | 6812 | NV_UNIQUE_NAME, KM_SLEEP) == 0); |
428870ff BB |
6813 | VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, |
6814 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); | |
6815 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, | |
6816 | B_TRUE)); | |
9babb374 | 6817 | } |
34dc7c2f | 6818 | |
428870ff BB |
6819 | /* set the props */ |
6820 | if (props != NULL) { | |
6821 | spa_configfile_set(newspa, props, B_FALSE); | |
6822 | error = spa_prop_set(newspa, props); | |
6823 | if (error) | |
6824 | goto out; | |
6825 | } | |
34dc7c2f | 6826 | |
428870ff BB |
6827 | /* flush everything */ |
6828 | txg = spa_vdev_config_enter(newspa); | |
6829 | vdev_config_dirty(newspa->spa_root_vdev); | |
6830 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 6831 | |
428870ff BB |
6832 | if (zio_injection_enabled) |
6833 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 6834 | |
428870ff | 6835 | spa_async_resume(newspa); |
34dc7c2f | 6836 | |
428870ff BB |
6837 | /* finally, update the original pool's config */ |
6838 | txg = spa_vdev_config_enter(spa); | |
6839 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
6840 | error = dmu_tx_assign(tx, TXG_WAIT); | |
6841 | if (error != 0) | |
6842 | dmu_tx_abort(tx); | |
6843 | for (c = 0; c < children; c++) { | |
6844 | if (vml[c] != NULL) { | |
6845 | vdev_split(vml[c]); | |
6846 | if (error == 0) | |
6f1ffb06 MA |
6847 | spa_history_log_internal(spa, "detach", tx, |
6848 | "vdev=%s", vml[c]->vdev_path); | |
e0ab3ab5 | 6849 | |
428870ff | 6850 | vdev_free(vml[c]); |
34dc7c2f | 6851 | } |
34dc7c2f | 6852 | } |
e0ab3ab5 | 6853 | spa->spa_avz_action = AVZ_ACTION_REBUILD; |
428870ff BB |
6854 | vdev_config_dirty(spa->spa_root_vdev); |
6855 | spa->spa_config_splitting = NULL; | |
6856 | nvlist_free(nvl); | |
6857 | if (error == 0) | |
6858 | dmu_tx_commit(tx); | |
6859 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
6860 | ||
6861 | if (zio_injection_enabled) | |
6862 | zio_handle_panic_injection(spa, FTAG, 3); | |
6863 | ||
6864 | /* split is complete; log a history record */ | |
6f1ffb06 MA |
6865 | spa_history_log_internal(newspa, "split", NULL, |
6866 | "from pool %s", spa_name(spa)); | |
428870ff BB |
6867 | |
6868 | kmem_free(vml, children * sizeof (vdev_t *)); | |
6869 | ||
6870 | /* if we're not going to mount the filesystems in userland, export */ | |
6871 | if (exp) | |
6872 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
6873 | B_FALSE, B_FALSE); | |
6874 | ||
6875 | return (error); | |
6876 | ||
6877 | out: | |
6878 | spa_unload(newspa); | |
6879 | spa_deactivate(newspa); | |
6880 | spa_remove(newspa); | |
6881 | ||
6882 | txg = spa_vdev_config_enter(spa); | |
6883 | ||
6884 | /* re-online all offlined disks */ | |
6885 | for (c = 0; c < children; c++) { | |
6886 | if (vml[c] != NULL) | |
6887 | vml[c]->vdev_offline = B_FALSE; | |
6888 | } | |
619f0976 GW |
6889 | |
6890 | /* restart initializing disks as necessary */ | |
6891 | spa_async_request(spa, SPA_ASYNC_INITIALIZE_RESTART); | |
6892 | ||
428870ff BB |
6893 | vdev_reopen(spa->spa_root_vdev); |
6894 | ||
6895 | nvlist_free(spa->spa_config_splitting); | |
6896 | spa->spa_config_splitting = NULL; | |
6897 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 6898 | |
428870ff | 6899 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
6900 | return (error); |
6901 | } | |
6902 | ||
34dc7c2f BB |
6903 | /* |
6904 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
d3cc8b15 | 6905 | * currently spared, so we can detach it. |
34dc7c2f BB |
6906 | */ |
6907 | static vdev_t * | |
6908 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
6909 | { | |
6910 | vdev_t *newvd, *oldvd; | |
34dc7c2f | 6911 | |
1c27024e | 6912 | for (int c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
6913 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
6914 | if (oldvd != NULL) | |
6915 | return (oldvd); | |
6916 | } | |
6917 | ||
6918 | /* | |
572e2857 BB |
6919 | * Check for a completed replacement. We always consider the first |
6920 | * vdev in the list to be the oldest vdev, and the last one to be | |
6921 | * the newest (see spa_vdev_attach() for how that works). In | |
6922 | * the case where the newest vdev is faulted, we will not automatically | |
6923 | * remove it after a resilver completes. This is OK as it will require | |
6924 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 6925 | */ |
572e2857 BB |
6926 | if (vd->vdev_ops == &vdev_replacing_ops) { |
6927 | ASSERT(vd->vdev_children > 1); | |
6928 | ||
6929 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 6930 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 6931 | |
fb5f0bc8 | 6932 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 6933 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 6934 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 6935 | return (oldvd); |
34dc7c2f BB |
6936 | } |
6937 | ||
6938 | /* | |
6939 | * Check for a completed resilver with the 'unspare' flag set. | |
f65fbee1 | 6940 | * Also potentially update faulted state. |
34dc7c2f | 6941 | */ |
572e2857 BB |
6942 | if (vd->vdev_ops == &vdev_spare_ops) { |
6943 | vdev_t *first = vd->vdev_child[0]; | |
6944 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
6945 | ||
6946 | if (last->vdev_unspare) { | |
6947 | oldvd = first; | |
6948 | newvd = last; | |
6949 | } else if (first->vdev_unspare) { | |
6950 | oldvd = last; | |
6951 | newvd = first; | |
6952 | } else { | |
6953 | oldvd = NULL; | |
6954 | } | |
34dc7c2f | 6955 | |
572e2857 | 6956 | if (oldvd != NULL && |
fb5f0bc8 | 6957 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 6958 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 6959 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 6960 | return (oldvd); |
572e2857 | 6961 | |
f65fbee1 JJ |
6962 | vdev_propagate_state(vd); |
6963 | ||
572e2857 BB |
6964 | /* |
6965 | * If there are more than two spares attached to a disk, | |
6966 | * and those spares are not required, then we want to | |
6967 | * attempt to free them up now so that they can be used | |
6968 | * by other pools. Once we're back down to a single | |
6969 | * disk+spare, we stop removing them. | |
6970 | */ | |
6971 | if (vd->vdev_children > 2) { | |
6972 | newvd = vd->vdev_child[1]; | |
6973 | ||
6974 | if (newvd->vdev_isspare && last->vdev_isspare && | |
6975 | vdev_dtl_empty(last, DTL_MISSING) && | |
6976 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
6977 | !vdev_dtl_required(newvd)) | |
6978 | return (newvd); | |
34dc7c2f | 6979 | } |
34dc7c2f BB |
6980 | } |
6981 | ||
6982 | return (NULL); | |
6983 | } | |
6984 | ||
6985 | static void | |
6986 | spa_vdev_resilver_done(spa_t *spa) | |
6987 | { | |
fb5f0bc8 BB |
6988 | vdev_t *vd, *pvd, *ppvd; |
6989 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 6990 | |
fb5f0bc8 | 6991 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
6992 | |
6993 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
6994 | pvd = vd->vdev_parent; |
6995 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 6996 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
6997 | pguid = pvd->vdev_guid; |
6998 | ppguid = ppvd->vdev_guid; | |
6999 | sguid = 0; | |
34dc7c2f BB |
7000 | /* |
7001 | * If we have just finished replacing a hot spared device, then | |
7002 | * we need to detach the parent's first child (the original hot | |
7003 | * spare) as well. | |
7004 | */ | |
572e2857 BB |
7005 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
7006 | ppvd->vdev_children == 2) { | |
34dc7c2f | 7007 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 7008 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 7009 | } |
5d1f7fb6 GW |
7010 | ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd)); |
7011 | ||
fb5f0bc8 BB |
7012 | spa_config_exit(spa, SCL_ALL, FTAG); |
7013 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 7014 | return; |
fb5f0bc8 | 7015 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 7016 | return; |
fb5f0bc8 | 7017 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
7018 | } |
7019 | ||
fb5f0bc8 | 7020 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
7021 | } |
7022 | ||
7023 | /* | |
428870ff | 7024 | * Update the stored path or FRU for this vdev. |
34dc7c2f BB |
7025 | */ |
7026 | int | |
9babb374 BB |
7027 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
7028 | boolean_t ispath) | |
34dc7c2f | 7029 | { |
b128c09f | 7030 | vdev_t *vd; |
428870ff | 7031 | boolean_t sync = B_FALSE; |
34dc7c2f | 7032 | |
572e2857 BB |
7033 | ASSERT(spa_writeable(spa)); |
7034 | ||
428870ff | 7035 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 7036 | |
9babb374 | 7037 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 7038 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
7039 | |
7040 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 7041 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 7042 | |
9babb374 | 7043 | if (ispath) { |
428870ff BB |
7044 | if (strcmp(value, vd->vdev_path) != 0) { |
7045 | spa_strfree(vd->vdev_path); | |
7046 | vd->vdev_path = spa_strdup(value); | |
7047 | sync = B_TRUE; | |
7048 | } | |
9babb374 | 7049 | } else { |
428870ff BB |
7050 | if (vd->vdev_fru == NULL) { |
7051 | vd->vdev_fru = spa_strdup(value); | |
7052 | sync = B_TRUE; | |
7053 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 7054 | spa_strfree(vd->vdev_fru); |
428870ff BB |
7055 | vd->vdev_fru = spa_strdup(value); |
7056 | sync = B_TRUE; | |
7057 | } | |
9babb374 | 7058 | } |
34dc7c2f | 7059 | |
428870ff | 7060 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
7061 | } |
7062 | ||
9babb374 BB |
7063 | int |
7064 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
7065 | { | |
7066 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
7067 | } | |
7068 | ||
7069 | int | |
7070 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
7071 | { | |
7072 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
7073 | } | |
7074 | ||
34dc7c2f BB |
7075 | /* |
7076 | * ========================================================================== | |
428870ff | 7077 | * SPA Scanning |
34dc7c2f BB |
7078 | * ========================================================================== |
7079 | */ | |
0ea05c64 AP |
7080 | int |
7081 | spa_scrub_pause_resume(spa_t *spa, pool_scrub_cmd_t cmd) | |
7082 | { | |
7083 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7084 | ||
7085 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
7086 | return (SET_ERROR(EBUSY)); | |
7087 | ||
7088 | return (dsl_scrub_set_pause_resume(spa->spa_dsl_pool, cmd)); | |
7089 | } | |
34dc7c2f | 7090 | |
34dc7c2f | 7091 | int |
428870ff BB |
7092 | spa_scan_stop(spa_t *spa) |
7093 | { | |
7094 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
7095 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
2e528b49 | 7096 | return (SET_ERROR(EBUSY)); |
428870ff BB |
7097 | return (dsl_scan_cancel(spa->spa_dsl_pool)); |
7098 | } | |
7099 | ||
7100 | int | |
7101 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 7102 | { |
b128c09f | 7103 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 7104 | |
428870ff | 7105 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
2e528b49 | 7106 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f | 7107 | |
34dc7c2f | 7108 | /* |
b128c09f BB |
7109 | * If a resilver was requested, but there is no DTL on a |
7110 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 7111 | */ |
428870ff | 7112 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
7113 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
7114 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
7115 | return (0); |
7116 | } | |
7117 | ||
428870ff | 7118 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
7119 | } |
7120 | ||
7121 | /* | |
7122 | * ========================================================================== | |
7123 | * SPA async task processing | |
7124 | * ========================================================================== | |
7125 | */ | |
7126 | ||
7127 | static void | |
7128 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
7129 | { | |
b128c09f | 7130 | if (vd->vdev_remove_wanted) { |
428870ff BB |
7131 | vd->vdev_remove_wanted = B_FALSE; |
7132 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 7133 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
7134 | |
7135 | /* | |
7136 | * We want to clear the stats, but we don't want to do a full | |
7137 | * vdev_clear() as that will cause us to throw away | |
7138 | * degraded/faulted state as well as attempt to reopen the | |
7139 | * device, all of which is a waste. | |
7140 | */ | |
7141 | vd->vdev_stat.vs_read_errors = 0; | |
7142 | vd->vdev_stat.vs_write_errors = 0; | |
7143 | vd->vdev_stat.vs_checksum_errors = 0; | |
7144 | ||
b128c09f BB |
7145 | vdev_state_dirty(vd->vdev_top); |
7146 | } | |
34dc7c2f | 7147 | |
1c27024e | 7148 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
7149 | spa_async_remove(spa, vd->vdev_child[c]); |
7150 | } | |
7151 | ||
7152 | static void | |
7153 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
7154 | { | |
7155 | if (vd->vdev_probe_wanted) { | |
428870ff | 7156 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 7157 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 7158 | } |
b128c09f | 7159 | |
1c27024e | 7160 | for (int c = 0; c < vd->vdev_children; c++) |
b128c09f | 7161 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
7162 | } |
7163 | ||
9babb374 BB |
7164 | static void |
7165 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
7166 | { | |
9babb374 BB |
7167 | if (!spa->spa_autoexpand) |
7168 | return; | |
7169 | ||
1c27024e | 7170 | for (int c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
7171 | vdev_t *cvd = vd->vdev_child[c]; |
7172 | spa_async_autoexpand(spa, cvd); | |
7173 | } | |
7174 | ||
7175 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
7176 | return; | |
7177 | ||
12fa0466 | 7178 | spa_event_notify(vd->vdev_spa, vd, NULL, ESC_ZFS_VDEV_AUTOEXPAND); |
9babb374 BB |
7179 | } |
7180 | ||
34dc7c2f | 7181 | static void |
c25b8f99 | 7182 | spa_async_thread(void *arg) |
34dc7c2f | 7183 | { |
c25b8f99 | 7184 | spa_t *spa = (spa_t *)arg; |
80a91e74 | 7185 | dsl_pool_t *dp = spa->spa_dsl_pool; |
867959b5 | 7186 | int tasks; |
34dc7c2f BB |
7187 | |
7188 | ASSERT(spa->spa_sync_on); | |
7189 | ||
7190 | mutex_enter(&spa->spa_async_lock); | |
7191 | tasks = spa->spa_async_tasks; | |
7192 | spa->spa_async_tasks = 0; | |
7193 | mutex_exit(&spa->spa_async_lock); | |
7194 | ||
7195 | /* | |
7196 | * See if the config needs to be updated. | |
7197 | */ | |
7198 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 7199 | uint64_t old_space, new_space; |
9babb374 | 7200 | |
34dc7c2f | 7201 | mutex_enter(&spa_namespace_lock); |
428870ff | 7202 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7203 | old_space += metaslab_class_get_space(spa_special_class(spa)); |
7204 | old_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
7205 | ||
34dc7c2f | 7206 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
cc99f275 | 7207 | |
428870ff | 7208 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
cc99f275 DB |
7209 | new_space += metaslab_class_get_space(spa_special_class(spa)); |
7210 | new_space += metaslab_class_get_space(spa_dedup_class(spa)); | |
34dc7c2f | 7211 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
7212 | |
7213 | /* | |
7214 | * If the pool grew as a result of the config update, | |
7215 | * then log an internal history event. | |
7216 | */ | |
428870ff | 7217 | if (new_space != old_space) { |
6f1ffb06 | 7218 | spa_history_log_internal(spa, "vdev online", NULL, |
45d1cae3 | 7219 | "pool '%s' size: %llu(+%llu)", |
428870ff | 7220 | spa_name(spa), new_space, new_space - old_space); |
9babb374 | 7221 | } |
34dc7c2f BB |
7222 | } |
7223 | ||
7224 | /* | |
7225 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 7226 | */ |
b128c09f | 7227 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 7228 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 7229 | spa_async_remove(spa, spa->spa_root_vdev); |
867959b5 | 7230 | for (int i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 7231 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
867959b5 | 7232 | for (int i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
7233 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
7234 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
7235 | } |
7236 | ||
9babb374 BB |
7237 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
7238 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7239 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
7240 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7241 | } | |
7242 | ||
34dc7c2f | 7243 | /* |
b128c09f | 7244 | * See if any devices need to be probed. |
34dc7c2f | 7245 | */ |
b128c09f | 7246 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 7247 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
7248 | spa_async_probe(spa, spa->spa_root_vdev); |
7249 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
7250 | } | |
34dc7c2f BB |
7251 | |
7252 | /* | |
b128c09f | 7253 | * If any devices are done replacing, detach them. |
34dc7c2f | 7254 | */ |
b128c09f BB |
7255 | if (tasks & SPA_ASYNC_RESILVER_DONE) |
7256 | spa_vdev_resilver_done(spa); | |
34dc7c2f BB |
7257 | |
7258 | /* | |
7259 | * Kick off a resilver. | |
7260 | */ | |
80a91e74 TC |
7261 | if (tasks & SPA_ASYNC_RESILVER && |
7262 | (!dsl_scan_resilvering(dp) || | |
7263 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_RESILVER_DEFER))) | |
7264 | dsl_resilver_restart(dp, 0); | |
34dc7c2f | 7265 | |
619f0976 GW |
7266 | if (tasks & SPA_ASYNC_INITIALIZE_RESTART) { |
7267 | mutex_enter(&spa_namespace_lock); | |
7268 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
7269 | vdev_initialize_restart(spa->spa_root_vdev); | |
7270 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
7271 | mutex_exit(&spa_namespace_lock); | |
7272 | } | |
7273 | ||
34dc7c2f BB |
7274 | /* |
7275 | * Let the world know that we're done. | |
7276 | */ | |
7277 | mutex_enter(&spa->spa_async_lock); | |
7278 | spa->spa_async_thread = NULL; | |
7279 | cv_broadcast(&spa->spa_async_cv); | |
7280 | mutex_exit(&spa->spa_async_lock); | |
7281 | thread_exit(); | |
7282 | } | |
7283 | ||
7284 | void | |
7285 | spa_async_suspend(spa_t *spa) | |
7286 | { | |
7287 | mutex_enter(&spa->spa_async_lock); | |
7288 | spa->spa_async_suspended++; | |
9d5b5245 | 7289 | while (spa->spa_async_thread != NULL) |
34dc7c2f BB |
7290 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); |
7291 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 MA |
7292 | |
7293 | spa_vdev_remove_suspend(spa); | |
9d5b5245 SD |
7294 | |
7295 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a SD |
7296 | if (condense_thread != NULL) |
7297 | zthr_cancel(condense_thread); | |
d2734cce SD |
7298 | |
7299 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a SD |
7300 | if (discard_thread != NULL) |
7301 | zthr_cancel(discard_thread); | |
34dc7c2f BB |
7302 | } |
7303 | ||
7304 | void | |
7305 | spa_async_resume(spa_t *spa) | |
7306 | { | |
7307 | mutex_enter(&spa->spa_async_lock); | |
7308 | ASSERT(spa->spa_async_suspended != 0); | |
7309 | spa->spa_async_suspended--; | |
7310 | mutex_exit(&spa->spa_async_lock); | |
a1d477c2 | 7311 | spa_restart_removal(spa); |
9d5b5245 SD |
7312 | |
7313 | zthr_t *condense_thread = spa->spa_condense_zthr; | |
61c3391a | 7314 | if (condense_thread != NULL) |
9d5b5245 | 7315 | zthr_resume(condense_thread); |
d2734cce SD |
7316 | |
7317 | zthr_t *discard_thread = spa->spa_checkpoint_discard_zthr; | |
61c3391a | 7318 | if (discard_thread != NULL) |
d2734cce | 7319 | zthr_resume(discard_thread); |
34dc7c2f BB |
7320 | } |
7321 | ||
e6cfd633 WA |
7322 | static boolean_t |
7323 | spa_async_tasks_pending(spa_t *spa) | |
7324 | { | |
7325 | uint_t non_config_tasks; | |
7326 | uint_t config_task; | |
7327 | boolean_t config_task_suspended; | |
7328 | ||
7329 | non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE; | |
7330 | config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE; | |
7331 | if (spa->spa_ccw_fail_time == 0) { | |
7332 | config_task_suspended = B_FALSE; | |
7333 | } else { | |
7334 | config_task_suspended = | |
7335 | (gethrtime() - spa->spa_ccw_fail_time) < | |
05852b34 | 7336 | ((hrtime_t)zfs_ccw_retry_interval * NANOSEC); |
e6cfd633 WA |
7337 | } |
7338 | ||
7339 | return (non_config_tasks || (config_task && !config_task_suspended)); | |
7340 | } | |
7341 | ||
34dc7c2f BB |
7342 | static void |
7343 | spa_async_dispatch(spa_t *spa) | |
7344 | { | |
7345 | mutex_enter(&spa->spa_async_lock); | |
e6cfd633 WA |
7346 | if (spa_async_tasks_pending(spa) && |
7347 | !spa->spa_async_suspended && | |
34dc7c2f | 7348 | spa->spa_async_thread == NULL && |
e6cfd633 | 7349 | rootdir != NULL) |
34dc7c2f BB |
7350 | spa->spa_async_thread = thread_create(NULL, 0, |
7351 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
7352 | mutex_exit(&spa->spa_async_lock); | |
7353 | } | |
7354 | ||
7355 | void | |
7356 | spa_async_request(spa_t *spa, int task) | |
7357 | { | |
428870ff | 7358 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
7359 | mutex_enter(&spa->spa_async_lock); |
7360 | spa->spa_async_tasks |= task; | |
7361 | mutex_exit(&spa->spa_async_lock); | |
7362 | } | |
7363 | ||
7364 | /* | |
7365 | * ========================================================================== | |
7366 | * SPA syncing routines | |
7367 | * ========================================================================== | |
7368 | */ | |
7369 | ||
428870ff BB |
7370 | static int |
7371 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
34dc7c2f | 7372 | { |
428870ff BB |
7373 | bpobj_t *bpo = arg; |
7374 | bpobj_enqueue(bpo, bp, tx); | |
7375 | return (0); | |
7376 | } | |
34dc7c2f | 7377 | |
428870ff BB |
7378 | static int |
7379 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
7380 | { | |
7381 | zio_t *zio = arg; | |
34dc7c2f | 7382 | |
428870ff BB |
7383 | zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, |
7384 | zio->io_flags)); | |
7385 | return (0); | |
34dc7c2f BB |
7386 | } |
7387 | ||
e8b96c60 MA |
7388 | /* |
7389 | * Note: this simple function is not inlined to make it easier to dtrace the | |
7390 | * amount of time spent syncing frees. | |
7391 | */ | |
7392 | static void | |
7393 | spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx) | |
7394 | { | |
7395 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
7396 | bplist_iterate(bpl, spa_free_sync_cb, zio, tx); | |
7397 | VERIFY(zio_wait(zio) == 0); | |
7398 | } | |
7399 | ||
7400 | /* | |
7401 | * Note: this simple function is not inlined to make it easier to dtrace the | |
7402 | * amount of time spent syncing deferred frees. | |
7403 | */ | |
7404 | static void | |
7405 | spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx) | |
7406 | { | |
8dc2197b SD |
7407 | if (spa_sync_pass(spa) != 1) |
7408 | return; | |
7409 | ||
e8b96c60 MA |
7410 | zio_t *zio = zio_root(spa, NULL, NULL, 0); |
7411 | VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj, | |
7412 | spa_free_sync_cb, zio, tx), ==, 0); | |
7413 | VERIFY0(zio_wait(zio)); | |
7414 | } | |
7415 | ||
34dc7c2f BB |
7416 | static void |
7417 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
7418 | { | |
7419 | char *packed = NULL; | |
b128c09f | 7420 | size_t bufsize; |
34dc7c2f BB |
7421 | size_t nvsize = 0; |
7422 | dmu_buf_t *db; | |
7423 | ||
7424 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
7425 | ||
b128c09f BB |
7426 | /* |
7427 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
b0bc7a84 | 7428 | * information. This avoids the dmu_buf_will_dirty() path and |
b128c09f BB |
7429 | * saves us a pre-read to get data we don't actually care about. |
7430 | */ | |
9ae529ec | 7431 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
79c76d5b | 7432 | packed = vmem_alloc(bufsize, KM_SLEEP); |
34dc7c2f BB |
7433 | |
7434 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
79c76d5b | 7435 | KM_SLEEP) == 0); |
b128c09f | 7436 | bzero(packed + nvsize, bufsize - nvsize); |
34dc7c2f | 7437 | |
b128c09f | 7438 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 7439 | |
00b46022 | 7440 | vmem_free(packed, bufsize); |
34dc7c2f BB |
7441 | |
7442 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
7443 | dmu_buf_will_dirty(db, tx); | |
7444 | *(uint64_t *)db->db_data = nvsize; | |
7445 | dmu_buf_rele(db, FTAG); | |
7446 | } | |
7447 | ||
7448 | static void | |
7449 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
7450 | const char *config, const char *entry) | |
7451 | { | |
7452 | nvlist_t *nvroot; | |
7453 | nvlist_t **list; | |
7454 | int i; | |
7455 | ||
7456 | if (!sav->sav_sync) | |
7457 | return; | |
7458 | ||
7459 | /* | |
7460 | * Update the MOS nvlist describing the list of available devices. | |
7461 | * spa_validate_aux() will have already made sure this nvlist is | |
7462 | * valid and the vdevs are labeled appropriately. | |
7463 | */ | |
7464 | if (sav->sav_object == 0) { | |
7465 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
7466 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
7467 | sizeof (uint64_t), tx); | |
7468 | VERIFY(zap_update(spa->spa_meta_objset, | |
7469 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
7470 | &sav->sav_object, tx) == 0); | |
7471 | } | |
7472 | ||
79c76d5b | 7473 | VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
34dc7c2f BB |
7474 | if (sav->sav_count == 0) { |
7475 | VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); | |
7476 | } else { | |
79c76d5b | 7477 | list = kmem_alloc(sav->sav_count*sizeof (void *), KM_SLEEP); |
34dc7c2f BB |
7478 | for (i = 0; i < sav->sav_count; i++) |
7479 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 7480 | B_FALSE, VDEV_CONFIG_L2CACHE); |
34dc7c2f BB |
7481 | VERIFY(nvlist_add_nvlist_array(nvroot, config, list, |
7482 | sav->sav_count) == 0); | |
7483 | for (i = 0; i < sav->sav_count; i++) | |
7484 | nvlist_free(list[i]); | |
7485 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
7486 | } | |
7487 | ||
7488 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
7489 | nvlist_free(nvroot); | |
7490 | ||
7491 | sav->sav_sync = B_FALSE; | |
7492 | } | |
7493 | ||
e0ab3ab5 JS |
7494 | /* |
7495 | * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t. | |
7496 | * The all-vdev ZAP must be empty. | |
7497 | */ | |
7498 | static void | |
7499 | spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx) | |
7500 | { | |
7501 | spa_t *spa = vd->vdev_spa; | |
e0ab3ab5 JS |
7502 | |
7503 | if (vd->vdev_top_zap != 0) { | |
7504 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
7505 | vd->vdev_top_zap, tx)); | |
7506 | } | |
7507 | if (vd->vdev_leaf_zap != 0) { | |
7508 | VERIFY0(zap_add_int(spa->spa_meta_objset, avz, | |
7509 | vd->vdev_leaf_zap, tx)); | |
7510 | } | |
1c27024e | 7511 | for (uint64_t i = 0; i < vd->vdev_children; i++) { |
e0ab3ab5 JS |
7512 | spa_avz_build(vd->vdev_child[i], avz, tx); |
7513 | } | |
7514 | } | |
7515 | ||
34dc7c2f BB |
7516 | static void |
7517 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
7518 | { | |
7519 | nvlist_t *config; | |
7520 | ||
e0ab3ab5 JS |
7521 | /* |
7522 | * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS, | |
7523 | * its config may not be dirty but we still need to build per-vdev ZAPs. | |
7524 | * Similarly, if the pool is being assembled (e.g. after a split), we | |
7525 | * need to rebuild the AVZ although the config may not be dirty. | |
7526 | */ | |
7527 | if (list_is_empty(&spa->spa_config_dirty_list) && | |
7528 | spa->spa_avz_action == AVZ_ACTION_NONE) | |
34dc7c2f BB |
7529 | return; |
7530 | ||
b128c09f BB |
7531 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
7532 | ||
e0ab3ab5 | 7533 | ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE || |
38640550 | 7534 | spa->spa_avz_action == AVZ_ACTION_INITIALIZE || |
e0ab3ab5 JS |
7535 | spa->spa_all_vdev_zaps != 0); |
7536 | ||
7537 | if (spa->spa_avz_action == AVZ_ACTION_REBUILD) { | |
e0ab3ab5 JS |
7538 | /* Make and build the new AVZ */ |
7539 | uint64_t new_avz = zap_create(spa->spa_meta_objset, | |
7540 | DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx); | |
7541 | spa_avz_build(spa->spa_root_vdev, new_avz, tx); | |
7542 | ||
7543 | /* Diff old AVZ with new one */ | |
1c27024e DB |
7544 | zap_cursor_t zc; |
7545 | zap_attribute_t za; | |
7546 | ||
e0ab3ab5 JS |
7547 | for (zap_cursor_init(&zc, spa->spa_meta_objset, |
7548 | spa->spa_all_vdev_zaps); | |
7549 | zap_cursor_retrieve(&zc, &za) == 0; | |
7550 | zap_cursor_advance(&zc)) { | |
7551 | uint64_t vdzap = za.za_first_integer; | |
7552 | if (zap_lookup_int(spa->spa_meta_objset, new_avz, | |
7553 | vdzap) == ENOENT) { | |
7554 | /* | |
7555 | * ZAP is listed in old AVZ but not in new one; | |
7556 | * destroy it | |
7557 | */ | |
7558 | VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap, | |
7559 | tx)); | |
7560 | } | |
7561 | } | |
7562 | ||
7563 | zap_cursor_fini(&zc); | |
7564 | ||
7565 | /* Destroy the old AVZ */ | |
7566 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
7567 | spa->spa_all_vdev_zaps, tx)); | |
7568 | ||
7569 | /* Replace the old AVZ in the dir obj with the new one */ | |
7570 | VERIFY0(zap_update(spa->spa_meta_objset, | |
7571 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, | |
7572 | sizeof (new_avz), 1, &new_avz, tx)); | |
7573 | ||
7574 | spa->spa_all_vdev_zaps = new_avz; | |
7575 | } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) { | |
7576 | zap_cursor_t zc; | |
7577 | zap_attribute_t za; | |
7578 | ||
7579 | /* Walk through the AVZ and destroy all listed ZAPs */ | |
7580 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
7581 | spa->spa_all_vdev_zaps); | |
7582 | zap_cursor_retrieve(&zc, &za) == 0; | |
7583 | zap_cursor_advance(&zc)) { | |
7584 | uint64_t zap = za.za_first_integer; | |
7585 | VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx)); | |
7586 | } | |
7587 | ||
7588 | zap_cursor_fini(&zc); | |
7589 | ||
7590 | /* Destroy and unlink the AVZ itself */ | |
7591 | VERIFY0(zap_destroy(spa->spa_meta_objset, | |
7592 | spa->spa_all_vdev_zaps, tx)); | |
7593 | VERIFY0(zap_remove(spa->spa_meta_objset, | |
7594 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx)); | |
7595 | spa->spa_all_vdev_zaps = 0; | |
7596 | } | |
7597 | ||
7598 | if (spa->spa_all_vdev_zaps == 0) { | |
7599 | spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset, | |
7600 | DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT, | |
7601 | DMU_POOL_VDEV_ZAP_MAP, tx); | |
7602 | } | |
7603 | spa->spa_avz_action = AVZ_ACTION_NONE; | |
7604 | ||
7605 | /* Create ZAPs for vdevs that don't have them. */ | |
7606 | vdev_construct_zaps(spa->spa_root_vdev, tx); | |
7607 | ||
b128c09f BB |
7608 | config = spa_config_generate(spa, spa->spa_root_vdev, |
7609 | dmu_tx_get_txg(tx), B_FALSE); | |
7610 | ||
ea0b2538 GW |
7611 | /* |
7612 | * If we're upgrading the spa version then make sure that | |
7613 | * the config object gets updated with the correct version. | |
7614 | */ | |
7615 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
7616 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
7617 | spa->spa_uberblock.ub_version); | |
7618 | ||
b128c09f | 7619 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f | 7620 | |
8a5fc748 | 7621 | nvlist_free(spa->spa_config_syncing); |
34dc7c2f BB |
7622 | spa->spa_config_syncing = config; |
7623 | ||
7624 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
7625 | } | |
7626 | ||
9ae529ec | 7627 | static void |
13fe0198 | 7628 | spa_sync_version(void *arg, dmu_tx_t *tx) |
9ae529ec | 7629 | { |
13fe0198 MA |
7630 | uint64_t *versionp = arg; |
7631 | uint64_t version = *versionp; | |
7632 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
9ae529ec CS |
7633 | |
7634 | /* | |
7635 | * Setting the version is special cased when first creating the pool. | |
7636 | */ | |
7637 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
7638 | ||
8dca0a9a | 7639 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
7640 | ASSERT(version >= spa_version(spa)); |
7641 | ||
7642 | spa->spa_uberblock.ub_version = version; | |
7643 | vdev_config_dirty(spa->spa_root_vdev); | |
6f1ffb06 | 7644 | spa_history_log_internal(spa, "set", tx, "version=%lld", version); |
9ae529ec CS |
7645 | } |
7646 | ||
34dc7c2f BB |
7647 | /* |
7648 | * Set zpool properties. | |
7649 | */ | |
7650 | static void | |
13fe0198 | 7651 | spa_sync_props(void *arg, dmu_tx_t *tx) |
34dc7c2f | 7652 | { |
13fe0198 MA |
7653 | nvlist_t *nvp = arg; |
7654 | spa_t *spa = dmu_tx_pool(tx)->dp_spa; | |
34dc7c2f | 7655 | objset_t *mos = spa->spa_meta_objset; |
9ae529ec | 7656 | nvpair_t *elem = NULL; |
b128c09f BB |
7657 | |
7658 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 7659 | |
34dc7c2f | 7660 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec CS |
7661 | uint64_t intval; |
7662 | char *strval, *fname; | |
7663 | zpool_prop_t prop; | |
7664 | const char *propname; | |
7665 | zprop_type_t proptype; | |
fa86b5db | 7666 | spa_feature_t fid; |
9ae529ec | 7667 | |
31864e3d BB |
7668 | switch (prop = zpool_name_to_prop(nvpair_name(elem))) { |
7669 | case ZPOOL_PROP_INVAL: | |
9ae529ec CS |
7670 | /* |
7671 | * We checked this earlier in spa_prop_validate(). | |
7672 | */ | |
7673 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
7674 | ||
7675 | fname = strchr(nvpair_name(elem), '@') + 1; | |
fa86b5db | 7676 | VERIFY0(zfeature_lookup_name(fname, &fid)); |
9ae529ec | 7677 | |
fa86b5db | 7678 | spa_feature_enable(spa, fid, tx); |
6f1ffb06 MA |
7679 | spa_history_log_internal(spa, "set", tx, |
7680 | "%s=enabled", nvpair_name(elem)); | |
9ae529ec CS |
7681 | break; |
7682 | ||
34dc7c2f | 7683 | case ZPOOL_PROP_VERSION: |
93cf2076 | 7684 | intval = fnvpair_value_uint64(elem); |
34dc7c2f | 7685 | /* |
4e33ba4c | 7686 | * The version is synced separately before other |
9ae529ec | 7687 | * properties and should be correct by now. |
34dc7c2f | 7688 | */ |
9ae529ec | 7689 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
7690 | break; |
7691 | ||
7692 | case ZPOOL_PROP_ALTROOT: | |
7693 | /* | |
7694 | * 'altroot' is a non-persistent property. It should | |
7695 | * have been set temporarily at creation or import time. | |
7696 | */ | |
7697 | ASSERT(spa->spa_root != NULL); | |
7698 | break; | |
7699 | ||
572e2857 | 7700 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
7701 | case ZPOOL_PROP_CACHEFILE: |
7702 | /* | |
572e2857 BB |
7703 | * 'readonly' and 'cachefile' are also non-persisitent |
7704 | * properties. | |
34dc7c2f | 7705 | */ |
34dc7c2f | 7706 | break; |
d96eb2b1 | 7707 | case ZPOOL_PROP_COMMENT: |
93cf2076 | 7708 | strval = fnvpair_value_string(elem); |
d96eb2b1 DM |
7709 | if (spa->spa_comment != NULL) |
7710 | spa_strfree(spa->spa_comment); | |
7711 | spa->spa_comment = spa_strdup(strval); | |
7712 | /* | |
7713 | * We need to dirty the configuration on all the vdevs | |
7714 | * so that their labels get updated. It's unnecessary | |
7715 | * to do this for pool creation since the vdev's | |
4e33ba4c | 7716 | * configuration has already been dirtied. |
d96eb2b1 DM |
7717 | */ |
7718 | if (tx->tx_txg != TXG_INITIAL) | |
7719 | vdev_config_dirty(spa->spa_root_vdev); | |
6f1ffb06 MA |
7720 | spa_history_log_internal(spa, "set", tx, |
7721 | "%s=%s", nvpair_name(elem), strval); | |
d96eb2b1 | 7722 | break; |
34dc7c2f BB |
7723 | default: |
7724 | /* | |
7725 | * Set pool property values in the poolprops mos object. | |
7726 | */ | |
34dc7c2f | 7727 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
7728 | spa->spa_pool_props_object = |
7729 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 7730 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 7731 | tx); |
34dc7c2f | 7732 | } |
34dc7c2f BB |
7733 | |
7734 | /* normalize the property name */ | |
7735 | propname = zpool_prop_to_name(prop); | |
7736 | proptype = zpool_prop_get_type(prop); | |
7737 | ||
7738 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
7739 | ASSERT(proptype == PROP_TYPE_STRING); | |
93cf2076 GW |
7740 | strval = fnvpair_value_string(elem); |
7741 | VERIFY0(zap_update(mos, | |
34dc7c2f | 7742 | spa->spa_pool_props_object, propname, |
93cf2076 | 7743 | 1, strlen(strval) + 1, strval, tx)); |
6f1ffb06 MA |
7744 | spa_history_log_internal(spa, "set", tx, |
7745 | "%s=%s", nvpair_name(elem), strval); | |
34dc7c2f | 7746 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { |
93cf2076 | 7747 | intval = fnvpair_value_uint64(elem); |
34dc7c2f BB |
7748 | |
7749 | if (proptype == PROP_TYPE_INDEX) { | |
7750 | const char *unused; | |
93cf2076 GW |
7751 | VERIFY0(zpool_prop_index_to_string( |
7752 | prop, intval, &unused)); | |
34dc7c2f | 7753 | } |
93cf2076 | 7754 | VERIFY0(zap_update(mos, |
34dc7c2f | 7755 | spa->spa_pool_props_object, propname, |
93cf2076 | 7756 | 8, 1, &intval, tx)); |
6f1ffb06 MA |
7757 | spa_history_log_internal(spa, "set", tx, |
7758 | "%s=%lld", nvpair_name(elem), intval); | |
34dc7c2f BB |
7759 | } else { |
7760 | ASSERT(0); /* not allowed */ | |
7761 | } | |
7762 | ||
7763 | switch (prop) { | |
7764 | case ZPOOL_PROP_DELEGATION: | |
7765 | spa->spa_delegation = intval; | |
7766 | break; | |
7767 | case ZPOOL_PROP_BOOTFS: | |
7768 | spa->spa_bootfs = intval; | |
7769 | break; | |
7770 | case ZPOOL_PROP_FAILUREMODE: | |
7771 | spa->spa_failmode = intval; | |
7772 | break; | |
9babb374 BB |
7773 | case ZPOOL_PROP_AUTOEXPAND: |
7774 | spa->spa_autoexpand = intval; | |
428870ff BB |
7775 | if (tx->tx_txg != TXG_INITIAL) |
7776 | spa_async_request(spa, | |
7777 | SPA_ASYNC_AUTOEXPAND); | |
7778 | break; | |
379ca9cf OF |
7779 | case ZPOOL_PROP_MULTIHOST: |
7780 | spa->spa_multihost = intval; | |
7781 | break; | |
428870ff BB |
7782 | case ZPOOL_PROP_DEDUPDITTO: |
7783 | spa->spa_dedup_ditto = intval; | |
9babb374 | 7784 | break; |
34dc7c2f BB |
7785 | default: |
7786 | break; | |
7787 | } | |
7788 | } | |
7789 | ||
34dc7c2f | 7790 | } |
b128c09f BB |
7791 | |
7792 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
7793 | } |
7794 | ||
428870ff BB |
7795 | /* |
7796 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
7797 | * reflect the new version this txg, so there must be no changes this | |
7798 | * txg to anything that the upgrade code depends on after it executes. | |
7799 | * Therefore this must be called after dsl_pool_sync() does the sync | |
7800 | * tasks. | |
7801 | */ | |
7802 | static void | |
7803 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
7804 | { | |
8dc2197b SD |
7805 | if (spa_sync_pass(spa) != 1) |
7806 | return; | |
428870ff | 7807 | |
8dc2197b | 7808 | dsl_pool_t *dp = spa->spa_dsl_pool; |
13fe0198 MA |
7809 | rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG); |
7810 | ||
428870ff BB |
7811 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && |
7812 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
7813 | dsl_pool_create_origin(dp, tx); | |
7814 | ||
7815 | /* Keeping the origin open increases spa_minref */ | |
7816 | spa->spa_minref += 3; | |
7817 | } | |
7818 | ||
7819 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
7820 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
7821 | dsl_pool_upgrade_clones(dp, tx); | |
7822 | } | |
7823 | ||
7824 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
7825 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
7826 | dsl_pool_upgrade_dir_clones(dp, tx); | |
7827 | ||
7828 | /* Keeping the freedir open increases spa_minref */ | |
7829 | spa->spa_minref += 3; | |
7830 | } | |
9ae529ec CS |
7831 | |
7832 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
7833 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
7834 | spa_feature_create_zap_objects(spa, tx); | |
7835 | } | |
62bdd5eb DL |
7836 | |
7837 | /* | |
7838 | * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable | |
7839 | * when possibility to use lz4 compression for metadata was added | |
7840 | * Old pools that have this feature enabled must be upgraded to have | |
7841 | * this feature active | |
7842 | */ | |
7843 | if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
7844 | boolean_t lz4_en = spa_feature_is_enabled(spa, | |
7845 | SPA_FEATURE_LZ4_COMPRESS); | |
7846 | boolean_t lz4_ac = spa_feature_is_active(spa, | |
7847 | SPA_FEATURE_LZ4_COMPRESS); | |
7848 | ||
7849 | if (lz4_en && !lz4_ac) | |
7850 | spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx); | |
7851 | } | |
3c67d83a TH |
7852 | |
7853 | /* | |
7854 | * If we haven't written the salt, do so now. Note that the | |
7855 | * feature may not be activated yet, but that's fine since | |
7856 | * the presence of this ZAP entry is backwards compatible. | |
7857 | */ | |
7858 | if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
7859 | DMU_POOL_CHECKSUM_SALT) == ENOENT) { | |
7860 | VERIFY0(zap_add(spa->spa_meta_objset, | |
7861 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1, | |
7862 | sizeof (spa->spa_cksum_salt.zcs_bytes), | |
7863 | spa->spa_cksum_salt.zcs_bytes, tx)); | |
7864 | } | |
7865 | ||
13fe0198 | 7866 | rrw_exit(&dp->dp_config_rwlock, FTAG); |
428870ff BB |
7867 | } |
7868 | ||
a1d477c2 MA |
7869 | static void |
7870 | vdev_indirect_state_sync_verify(vdev_t *vd) | |
7871 | { | |
7872 | ASSERTV(vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping); | |
7873 | ASSERTV(vdev_indirect_births_t *vib = vd->vdev_indirect_births); | |
7874 | ||
7875 | if (vd->vdev_ops == &vdev_indirect_ops) { | |
7876 | ASSERT(vim != NULL); | |
7877 | ASSERT(vib != NULL); | |
7878 | } | |
7879 | ||
27f80e85 BB |
7880 | uint64_t obsolete_sm_object = 0; |
7881 | ASSERT0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); | |
7882 | if (obsolete_sm_object != 0) { | |
a1d477c2 MA |
7883 | ASSERT(vd->vdev_obsolete_sm != NULL); |
7884 | ASSERT(vd->vdev_removing || | |
7885 | vd->vdev_ops == &vdev_indirect_ops); | |
7886 | ASSERT(vdev_indirect_mapping_num_entries(vim) > 0); | |
7887 | ASSERT(vdev_indirect_mapping_bytes_mapped(vim) > 0); | |
27f80e85 | 7888 | ASSERT3U(obsolete_sm_object, ==, |
a1d477c2 MA |
7889 | space_map_object(vd->vdev_obsolete_sm)); |
7890 | ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim), >=, | |
7891 | space_map_allocated(vd->vdev_obsolete_sm)); | |
7892 | } | |
7893 | ASSERT(vd->vdev_obsolete_segments != NULL); | |
7894 | ||
7895 | /* | |
7896 | * Since frees / remaps to an indirect vdev can only | |
7897 | * happen in syncing context, the obsolete segments | |
7898 | * tree must be empty when we start syncing. | |
7899 | */ | |
7900 | ASSERT0(range_tree_space(vd->vdev_obsolete_segments)); | |
7901 | } | |
7902 | ||
34dc7c2f | 7903 | /* |
8dc2197b SD |
7904 | * Set the top-level vdev's max queue depth. Evaluate each top-level's |
7905 | * async write queue depth in case it changed. The max queue depth will | |
7906 | * not change in the middle of syncing out this txg. | |
34dc7c2f | 7907 | */ |
8dc2197b SD |
7908 | static void |
7909 | spa_sync_adjust_vdev_max_queue_depth(spa_t *spa) | |
34dc7c2f | 7910 | { |
8dc2197b SD |
7911 | ASSERT(spa_writeable(spa)); |
7912 | ||
34dc7c2f | 7913 | vdev_t *rvd = spa->spa_root_vdev; |
3dfb57a3 DB |
7914 | uint32_t max_queue_depth = zfs_vdev_async_write_max_active * |
7915 | zfs_vdev_queue_depth_pct / 100; | |
8dc2197b SD |
7916 | metaslab_class_t *normal = spa_normal_class(spa); |
7917 | metaslab_class_t *special = spa_special_class(spa); | |
7918 | metaslab_class_t *dedup = spa_dedup_class(spa); | |
34dc7c2f | 7919 | |
492f64e9 | 7920 | uint64_t slots_per_allocator = 0; |
1c27024e | 7921 | for (int c = 0; c < rvd->vdev_children; c++) { |
3dfb57a3 | 7922 | vdev_t *tvd = rvd->vdev_child[c]; |
cc99f275 | 7923 | |
8dc2197b | 7924 | metaslab_group_t *mg = tvd->vdev_mg; |
cc99f275 DB |
7925 | if (mg == NULL || !metaslab_group_initialized(mg)) |
7926 | continue; | |
3dfb57a3 | 7927 | |
8dc2197b | 7928 | metaslab_class_t *mc = mg->mg_class; |
cc99f275 | 7929 | if (mc != normal && mc != special && mc != dedup) |
3dfb57a3 DB |
7930 | continue; |
7931 | ||
7932 | /* | |
7933 | * It is safe to do a lock-free check here because only async | |
7934 | * allocations look at mg_max_alloc_queue_depth, and async | |
7935 | * allocations all happen from spa_sync(). | |
7936 | */ | |
492f64e9 | 7937 | for (int i = 0; i < spa->spa_alloc_count; i++) |
424fd7c3 TS |
7938 | ASSERT0(zfs_refcount_count( |
7939 | &(mg->mg_alloc_queue_depth[i]))); | |
3dfb57a3 | 7940 | mg->mg_max_alloc_queue_depth = max_queue_depth; |
492f64e9 PD |
7941 | |
7942 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
7943 | mg->mg_cur_max_alloc_queue_depth[i] = | |
7944 | zfs_vdev_def_queue_depth; | |
7945 | } | |
7946 | slots_per_allocator += zfs_vdev_def_queue_depth; | |
3dfb57a3 | 7947 | } |
cc99f275 | 7948 | |
492f64e9 | 7949 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
424fd7c3 TS |
7950 | ASSERT0(zfs_refcount_count(&normal->mc_alloc_slots[i])); |
7951 | ASSERT0(zfs_refcount_count(&special->mc_alloc_slots[i])); | |
7952 | ASSERT0(zfs_refcount_count(&dedup->mc_alloc_slots[i])); | |
cc99f275 DB |
7953 | normal->mc_alloc_max_slots[i] = slots_per_allocator; |
7954 | special->mc_alloc_max_slots[i] = slots_per_allocator; | |
7955 | dedup->mc_alloc_max_slots[i] = slots_per_allocator; | |
7956 | } | |
7957 | normal->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
7958 | special->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
7959 | dedup->mc_alloc_throttle_enabled = zio_dva_throttle_enabled; | |
8dc2197b SD |
7960 | } |
7961 | ||
7962 | static void | |
7963 | spa_sync_condense_indirect(spa_t *spa, dmu_tx_t *tx) | |
7964 | { | |
7965 | ASSERT(spa_writeable(spa)); | |
3dfb57a3 | 7966 | |
8dc2197b | 7967 | vdev_t *rvd = spa->spa_root_vdev; |
a1d477c2 MA |
7968 | for (int c = 0; c < rvd->vdev_children; c++) { |
7969 | vdev_t *vd = rvd->vdev_child[c]; | |
7970 | vdev_indirect_state_sync_verify(vd); | |
7971 | ||
7972 | if (vdev_indirect_should_condense(vd)) { | |
7973 | spa_condense_indirect_start_sync(vd, tx); | |
7974 | break; | |
7975 | } | |
7976 | } | |
8dc2197b SD |
7977 | } |
7978 | ||
7979 | static void | |
7980 | spa_sync_iterate_to_convergence(spa_t *spa, dmu_tx_t *tx) | |
7981 | { | |
7982 | objset_t *mos = spa->spa_meta_objset; | |
7983 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
7984 | uint64_t txg = tx->tx_txg; | |
7985 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
a1d477c2 | 7986 | |
34dc7c2f | 7987 | do { |
428870ff | 7988 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
7989 | |
7990 | spa_sync_config_object(spa, tx); | |
7991 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
7992 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
7993 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
7994 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
7995 | spa_errlog_sync(spa, txg); | |
7996 | dsl_pool_sync(dp, txg); | |
7997 | ||
55d85d5a | 7998 | if (pass < zfs_sync_pass_deferred_free) { |
e8b96c60 | 7999 | spa_sync_frees(spa, free_bpl, tx); |
428870ff | 8000 | } else { |
905edb40 MA |
8001 | /* |
8002 | * We can not defer frees in pass 1, because | |
8003 | * we sync the deferred frees later in pass 1. | |
8004 | */ | |
8005 | ASSERT3U(pass, >, 1); | |
428870ff | 8006 | bplist_iterate(free_bpl, bpobj_enqueue_cb, |
e8b96c60 | 8007 | &spa->spa_deferred_bpobj, tx); |
34dc7c2f BB |
8008 | } |
8009 | ||
428870ff BB |
8010 | ddt_sync(spa, txg); |
8011 | dsl_scan_sync(dp, tx); | |
8dc2197b SD |
8012 | svr_sync(spa, tx); |
8013 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 8014 | |
8dc2197b | 8015 | vdev_t *vd = NULL; |
a1d477c2 MA |
8016 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) |
8017 | != NULL) | |
428870ff BB |
8018 | vdev_sync(vd, txg); |
8019 | ||
8dc2197b SD |
8020 | /* |
8021 | * Note: We need to check if the MOS is dirty because we could | |
8022 | * have marked the MOS dirty without updating the uberblock | |
8023 | * (e.g. if we have sync tasks but no dirty user data). We need | |
8024 | * to check the uberblock's rootbp because it is updated if we | |
8025 | * have synced out dirty data (though in this case the MOS will | |
8026 | * most likely also be dirty due to second order effects, we | |
8027 | * don't want to rely on that here). | |
8028 | */ | |
8029 | if (pass == 1 && | |
8030 | spa->spa_uberblock.ub_rootbp.blk_birth < txg && | |
8031 | !dmu_objset_is_dirty(mos, txg)) { | |
905edb40 | 8032 | /* |
8dc2197b SD |
8033 | * Nothing changed on the first pass, therefore this |
8034 | * TXG is a no-op. Avoid syncing deferred frees, so | |
8035 | * that we can keep this TXG as a no-op. | |
905edb40 | 8036 | */ |
8dc2197b SD |
8037 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); |
8038 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
8039 | ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg)); | |
8040 | ASSERT(txg_list_empty(&dp->dp_early_sync_tasks, txg)); | |
8041 | break; | |
905edb40 | 8042 | } |
34dc7c2f | 8043 | |
8dc2197b | 8044 | spa_sync_deferred_frees(spa, tx); |
428870ff | 8045 | } while (dmu_objset_is_dirty(mos, txg)); |
8dc2197b | 8046 | } |
34dc7c2f | 8047 | |
8dc2197b SD |
8048 | /* |
8049 | * Rewrite the vdev configuration (which includes the uberblock) to | |
8050 | * commit the transaction group. | |
8051 | * | |
8052 | * If there are no dirty vdevs, we sync the uberblock to a few random | |
8053 | * top-level vdevs that are known to be visible in the config cache | |
8054 | * (see spa_vdev_add() for a complete description). If there *are* dirty | |
8055 | * vdevs, sync the uberblock to all vdevs. | |
8056 | */ | |
8057 | static void | |
8058 | spa_sync_rewrite_vdev_config(spa_t *spa, dmu_tx_t *tx) | |
8059 | { | |
8060 | vdev_t *rvd = spa->spa_root_vdev; | |
8061 | uint64_t txg = tx->tx_txg; | |
a1d477c2 | 8062 | |
b128c09f | 8063 | for (;;) { |
8dc2197b SD |
8064 | int error = 0; |
8065 | ||
b128c09f BB |
8066 | /* |
8067 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
8068 | * while we're attempting to write the vdev labels. | |
8069 | */ | |
8070 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
8071 | ||
8072 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
d2734cce | 8073 | vdev_t *svd[SPA_SYNC_MIN_VDEVS] = { NULL }; |
b128c09f BB |
8074 | int svdcount = 0; |
8075 | int children = rvd->vdev_children; | |
8076 | int c0 = spa_get_random(children); | |
b128c09f | 8077 | |
1c27024e | 8078 | for (int c = 0; c < children; c++) { |
8dc2197b SD |
8079 | vdev_t *vd = |
8080 | rvd->vdev_child[(c0 + c) % children]; | |
d2734cce SD |
8081 | |
8082 | /* Stop when revisiting the first vdev */ | |
8083 | if (c > 0 && svd[0] == vd) | |
8084 | break; | |
8085 | ||
8dc2197b SD |
8086 | if (vd->vdev_ms_array == 0 || |
8087 | vd->vdev_islog || | |
a1d477c2 | 8088 | !vdev_is_concrete(vd)) |
b128c09f | 8089 | continue; |
d2734cce | 8090 | |
b128c09f | 8091 | svd[svdcount++] = vd; |
6cb8e530 | 8092 | if (svdcount == SPA_SYNC_MIN_VDEVS) |
b128c09f BB |
8093 | break; |
8094 | } | |
b6fcb792 | 8095 | error = vdev_config_sync(svd, svdcount, txg); |
b128c09f BB |
8096 | } else { |
8097 | error = vdev_config_sync(rvd->vdev_child, | |
b6fcb792 | 8098 | rvd->vdev_children, txg); |
34dc7c2f | 8099 | } |
34dc7c2f | 8100 | |
3bc7e0fb GW |
8101 | if (error == 0) |
8102 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
8103 | ||
b128c09f BB |
8104 | spa_config_exit(spa, SCL_STATE, FTAG); |
8105 | ||
8106 | if (error == 0) | |
8107 | break; | |
cec3a0a1 | 8108 | zio_suspend(spa, NULL, ZIO_SUSPEND_IOERR); |
b128c09f BB |
8109 | zio_resume_wait(spa); |
8110 | } | |
8dc2197b SD |
8111 | } |
8112 | ||
8113 | /* | |
8114 | * Sync the specified transaction group. New blocks may be dirtied as | |
8115 | * part of the process, so we iterate until it converges. | |
8116 | */ | |
8117 | void | |
8118 | spa_sync(spa_t *spa, uint64_t txg) | |
8119 | { | |
8120 | vdev_t *vd = NULL; | |
8121 | ||
8122 | VERIFY(spa_writeable(spa)); | |
8123 | ||
8124 | /* | |
8125 | * Wait for i/os issued in open context that need to complete | |
8126 | * before this txg syncs. | |
8127 | */ | |
8128 | (void) zio_wait(spa->spa_txg_zio[txg & TXG_MASK]); | |
8129 | spa->spa_txg_zio[txg & TXG_MASK] = zio_root(spa, NULL, NULL, | |
8130 | ZIO_FLAG_CANFAIL); | |
8131 | ||
8132 | /* | |
8133 | * Lock out configuration changes. | |
8134 | */ | |
8135 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
8136 | ||
8137 | spa->spa_syncing_txg = txg; | |
8138 | spa->spa_sync_pass = 0; | |
8139 | ||
8140 | for (int i = 0; i < spa->spa_alloc_count; i++) { | |
8141 | mutex_enter(&spa->spa_alloc_locks[i]); | |
8142 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
8143 | mutex_exit(&spa->spa_alloc_locks[i]); | |
8144 | } | |
8145 | ||
8146 | /* | |
8147 | * If there are any pending vdev state changes, convert them | |
8148 | * into config changes that go out with this transaction group. | |
8149 | */ | |
8150 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
8151 | while (list_head(&spa->spa_state_dirty_list) != NULL) { | |
8152 | /* | |
8153 | * We need the write lock here because, for aux vdevs, | |
8154 | * calling vdev_config_dirty() modifies sav_config. | |
8155 | * This is ugly and will become unnecessary when we | |
8156 | * eliminate the aux vdev wart by integrating all vdevs | |
8157 | * into the root vdev tree. | |
8158 | */ | |
8159 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
8160 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
8161 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
8162 | vdev_state_clean(vd); | |
8163 | vdev_config_dirty(vd); | |
8164 | } | |
8165 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
8166 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
8167 | } | |
8168 | spa_config_exit(spa, SCL_STATE, FTAG); | |
8169 | ||
8170 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
8171 | dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg); | |
8172 | ||
8173 | spa->spa_sync_starttime = gethrtime(); | |
8174 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); | |
8175 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, | |
8176 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
8177 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
8178 | ||
8179 | /* | |
8180 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
8181 | * set spa_deflate if we have no raid-z vdevs. | |
8182 | */ | |
8183 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
8184 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
8185 | vdev_t *rvd = spa->spa_root_vdev; | |
8186 | ||
8187 | int i; | |
8188 | for (i = 0; i < rvd->vdev_children; i++) { | |
8189 | vd = rvd->vdev_child[i]; | |
8190 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
8191 | break; | |
8192 | } | |
8193 | if (i == rvd->vdev_children) { | |
8194 | spa->spa_deflate = TRUE; | |
8195 | VERIFY0(zap_add(spa->spa_meta_objset, | |
8196 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
8197 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
8198 | } | |
8199 | } | |
8200 | ||
8201 | spa_sync_adjust_vdev_max_queue_depth(spa); | |
8202 | ||
8203 | spa_sync_condense_indirect(spa, tx); | |
8204 | ||
8205 | spa_sync_iterate_to_convergence(spa, tx); | |
8206 | ||
8207 | #ifdef ZFS_DEBUG | |
8208 | if (!list_is_empty(&spa->spa_config_dirty_list)) { | |
8209 | /* | |
8210 | * Make sure that the number of ZAPs for all the vdevs matches | |
8211 | * the number of ZAPs in the per-vdev ZAP list. This only gets | |
8212 | * called if the config is dirty; otherwise there may be | |
8213 | * outstanding AVZ operations that weren't completed in | |
8214 | * spa_sync_config_object. | |
8215 | */ | |
8216 | uint64_t all_vdev_zap_entry_count; | |
8217 | ASSERT0(zap_count(spa->spa_meta_objset, | |
8218 | spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count)); | |
8219 | ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==, | |
8220 | all_vdev_zap_entry_count); | |
8221 | } | |
8222 | #endif | |
8223 | ||
8224 | if (spa->spa_vdev_removal != NULL) { | |
8225 | ASSERT0(spa->spa_vdev_removal->svr_bytes_done[txg & TXG_MASK]); | |
8226 | } | |
8227 | ||
8228 | spa_sync_rewrite_vdev_config(spa, tx); | |
34dc7c2f BB |
8229 | dmu_tx_commit(tx); |
8230 | ||
57ddcda1 | 8231 | taskq_cancel_id(system_delay_taskq, spa->spa_deadman_tqid); |
cc92e9d0 GW |
8232 | spa->spa_deadman_tqid = 0; |
8233 | ||
34dc7c2f BB |
8234 | /* |
8235 | * Clear the dirty config list. | |
8236 | */ | |
b128c09f | 8237 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
8238 | vdev_config_clean(vd); |
8239 | ||
8240 | /* | |
8241 | * Now that the new config has synced transactionally, | |
8242 | * let it become visible to the config cache. | |
8243 | */ | |
8244 | if (spa->spa_config_syncing != NULL) { | |
8245 | spa_config_set(spa, spa->spa_config_syncing); | |
8246 | spa->spa_config_txg = txg; | |
8247 | spa->spa_config_syncing = NULL; | |
8248 | } | |
8249 | ||
428870ff | 8250 | dsl_pool_sync_done(dp, txg); |
34dc7c2f | 8251 | |
492f64e9 PD |
8252 | for (int i = 0; i < spa->spa_alloc_count; i++) { |
8253 | mutex_enter(&spa->spa_alloc_locks[i]); | |
8254 | VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i])); | |
8255 | mutex_exit(&spa->spa_alloc_locks[i]); | |
8256 | } | |
3dfb57a3 | 8257 | |
34dc7c2f BB |
8258 | /* |
8259 | * Update usable space statistics. | |
8260 | */ | |
619f0976 GW |
8261 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))) |
8262 | != NULL) | |
34dc7c2f BB |
8263 | vdev_sync_done(vd, txg); |
8264 | ||
428870ff BB |
8265 | spa_update_dspace(spa); |
8266 | ||
34dc7c2f BB |
8267 | /* |
8268 | * It had better be the case that we didn't dirty anything | |
8269 | * since vdev_config_sync(). | |
8270 | */ | |
8271 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
8272 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
8273 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff | 8274 | |
d2734cce SD |
8275 | while (zfs_pause_spa_sync) |
8276 | delay(1); | |
8277 | ||
428870ff | 8278 | spa->spa_sync_pass = 0; |
34dc7c2f | 8279 | |
55922e73 GW |
8280 | /* |
8281 | * Update the last synced uberblock here. We want to do this at | |
8282 | * the end of spa_sync() so that consumers of spa_last_synced_txg() | |
8283 | * will be guaranteed that all the processing associated with | |
8284 | * that txg has been completed. | |
8285 | */ | |
8286 | spa->spa_ubsync = spa->spa_uberblock; | |
b128c09f | 8287 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 8288 | |
428870ff BB |
8289 | spa_handle_ignored_writes(spa); |
8290 | ||
34dc7c2f BB |
8291 | /* |
8292 | * If any async tasks have been requested, kick them off. | |
8293 | */ | |
8294 | spa_async_dispatch(spa); | |
8295 | } | |
8296 | ||
8297 | /* | |
8298 | * Sync all pools. We don't want to hold the namespace lock across these | |
8299 | * operations, so we take a reference on the spa_t and drop the lock during the | |
8300 | * sync. | |
8301 | */ | |
8302 | void | |
8303 | spa_sync_allpools(void) | |
8304 | { | |
8305 | spa_t *spa = NULL; | |
8306 | mutex_enter(&spa_namespace_lock); | |
8307 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
8308 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
8309 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
8310 | continue; |
8311 | spa_open_ref(spa, FTAG); | |
8312 | mutex_exit(&spa_namespace_lock); | |
8313 | txg_wait_synced(spa_get_dsl(spa), 0); | |
8314 | mutex_enter(&spa_namespace_lock); | |
8315 | spa_close(spa, FTAG); | |
8316 | } | |
8317 | mutex_exit(&spa_namespace_lock); | |
8318 | } | |
8319 | ||
8320 | /* | |
8321 | * ========================================================================== | |
8322 | * Miscellaneous routines | |
8323 | * ========================================================================== | |
8324 | */ | |
8325 | ||
8326 | /* | |
8327 | * Remove all pools in the system. | |
8328 | */ | |
8329 | void | |
8330 | spa_evict_all(void) | |
8331 | { | |
8332 | spa_t *spa; | |
8333 | ||
8334 | /* | |
8335 | * Remove all cached state. All pools should be closed now, | |
8336 | * so every spa in the AVL tree should be unreferenced. | |
8337 | */ | |
8338 | mutex_enter(&spa_namespace_lock); | |
8339 | while ((spa = spa_next(NULL)) != NULL) { | |
8340 | /* | |
8341 | * Stop async tasks. The async thread may need to detach | |
8342 | * a device that's been replaced, which requires grabbing | |
8343 | * spa_namespace_lock, so we must drop it here. | |
8344 | */ | |
8345 | spa_open_ref(spa, FTAG); | |
8346 | mutex_exit(&spa_namespace_lock); | |
8347 | spa_async_suspend(spa); | |
8348 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
8349 | spa_close(spa, FTAG); |
8350 | ||
8351 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
8352 | spa_unload(spa); | |
8353 | spa_deactivate(spa); | |
8354 | } | |
8355 | spa_remove(spa); | |
8356 | } | |
8357 | mutex_exit(&spa_namespace_lock); | |
8358 | } | |
8359 | ||
8360 | vdev_t * | |
9babb374 | 8361 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 8362 | { |
b128c09f BB |
8363 | vdev_t *vd; |
8364 | int i; | |
8365 | ||
8366 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
8367 | return (vd); | |
8368 | ||
9babb374 | 8369 | if (aux) { |
b128c09f BB |
8370 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
8371 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
8372 | if (vd->vdev_guid == guid) |
8373 | return (vd); | |
8374 | } | |
8375 | ||
8376 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
8377 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
8378 | if (vd->vdev_guid == guid) |
8379 | return (vd); | |
8380 | } | |
8381 | } | |
8382 | ||
8383 | return (NULL); | |
34dc7c2f BB |
8384 | } |
8385 | ||
8386 | void | |
8387 | spa_upgrade(spa_t *spa, uint64_t version) | |
8388 | { | |
572e2857 BB |
8389 | ASSERT(spa_writeable(spa)); |
8390 | ||
b128c09f | 8391 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
8392 | |
8393 | /* | |
8394 | * This should only be called for a non-faulted pool, and since a | |
8395 | * future version would result in an unopenable pool, this shouldn't be | |
8396 | * possible. | |
8397 | */ | |
8dca0a9a | 8398 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
9b67f605 | 8399 | ASSERT3U(version, >=, spa->spa_uberblock.ub_version); |
34dc7c2f BB |
8400 | |
8401 | spa->spa_uberblock.ub_version = version; | |
8402 | vdev_config_dirty(spa->spa_root_vdev); | |
8403 | ||
b128c09f | 8404 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
8405 | |
8406 | txg_wait_synced(spa_get_dsl(spa), 0); | |
8407 | } | |
8408 | ||
8409 | boolean_t | |
8410 | spa_has_spare(spa_t *spa, uint64_t guid) | |
8411 | { | |
8412 | int i; | |
8413 | uint64_t spareguid; | |
8414 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
8415 | ||
8416 | for (i = 0; i < sav->sav_count; i++) | |
8417 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
8418 | return (B_TRUE); | |
8419 | ||
8420 | for (i = 0; i < sav->sav_npending; i++) { | |
8421 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
8422 | &spareguid) == 0 && spareguid == guid) | |
8423 | return (B_TRUE); | |
8424 | } | |
8425 | ||
8426 | return (B_FALSE); | |
8427 | } | |
8428 | ||
b128c09f BB |
8429 | /* |
8430 | * Check if a pool has an active shared spare device. | |
8431 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
8432 | */ | |
8433 | static boolean_t | |
8434 | spa_has_active_shared_spare(spa_t *spa) | |
8435 | { | |
8436 | int i, refcnt; | |
8437 | uint64_t pool; | |
8438 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
8439 | ||
8440 | for (i = 0; i < sav->sav_count; i++) { | |
8441 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
8442 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
8443 | refcnt > 2) | |
8444 | return (B_TRUE); | |
8445 | } | |
8446 | ||
8447 | return (B_FALSE); | |
8448 | } | |
8449 | ||
a1d477c2 | 8450 | sysevent_t * |
12fa0466 DE |
8451 | spa_event_create(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
8452 | { | |
8453 | sysevent_t *ev = NULL; | |
8454 | #ifdef _KERNEL | |
8455 | nvlist_t *resource; | |
8456 | ||
8457 | resource = zfs_event_create(spa, vd, FM_SYSEVENT_CLASS, name, hist_nvl); | |
8458 | if (resource) { | |
8459 | ev = kmem_alloc(sizeof (sysevent_t), KM_SLEEP); | |
8460 | ev->resource = resource; | |
8461 | } | |
8462 | #endif | |
8463 | return (ev); | |
8464 | } | |
8465 | ||
a1d477c2 | 8466 | void |
12fa0466 DE |
8467 | spa_event_post(sysevent_t *ev) |
8468 | { | |
8469 | #ifdef _KERNEL | |
8470 | if (ev) { | |
8471 | zfs_zevent_post(ev->resource, NULL, zfs_zevent_post_cb); | |
8472 | kmem_free(ev, sizeof (*ev)); | |
8473 | } | |
8474 | #endif | |
8475 | } | |
8476 | ||
34dc7c2f | 8477 | /* |
fb390aaf HR |
8478 | * Post a zevent corresponding to the given sysevent. The 'name' must be one |
8479 | * of the event definitions in sys/sysevent/eventdefs.h. The payload will be | |
34dc7c2f BB |
8480 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
8481 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
8482 | * or zdb as real changes. | |
8483 | */ | |
8484 | void | |
12fa0466 | 8485 | spa_event_notify(spa_t *spa, vdev_t *vd, nvlist_t *hist_nvl, const char *name) |
34dc7c2f | 8486 | { |
12fa0466 | 8487 | spa_event_post(spa_event_create(spa, vd, hist_nvl, name)); |
34dc7c2f | 8488 | } |
c28b2279 | 8489 | |
93ce2b4c | 8490 | #if defined(_KERNEL) |
c28b2279 BB |
8491 | /* state manipulation functions */ |
8492 | EXPORT_SYMBOL(spa_open); | |
8493 | EXPORT_SYMBOL(spa_open_rewind); | |
8494 | EXPORT_SYMBOL(spa_get_stats); | |
8495 | EXPORT_SYMBOL(spa_create); | |
c28b2279 BB |
8496 | EXPORT_SYMBOL(spa_import); |
8497 | EXPORT_SYMBOL(spa_tryimport); | |
8498 | EXPORT_SYMBOL(spa_destroy); | |
8499 | EXPORT_SYMBOL(spa_export); | |
8500 | EXPORT_SYMBOL(spa_reset); | |
8501 | EXPORT_SYMBOL(spa_async_request); | |
8502 | EXPORT_SYMBOL(spa_async_suspend); | |
8503 | EXPORT_SYMBOL(spa_async_resume); | |
8504 | EXPORT_SYMBOL(spa_inject_addref); | |
8505 | EXPORT_SYMBOL(spa_inject_delref); | |
8506 | EXPORT_SYMBOL(spa_scan_stat_init); | |
8507 | EXPORT_SYMBOL(spa_scan_get_stats); | |
8508 | ||
8509 | /* device maniion */ | |
8510 | EXPORT_SYMBOL(spa_vdev_add); | |
8511 | EXPORT_SYMBOL(spa_vdev_attach); | |
8512 | EXPORT_SYMBOL(spa_vdev_detach); | |
c28b2279 BB |
8513 | EXPORT_SYMBOL(spa_vdev_setpath); |
8514 | EXPORT_SYMBOL(spa_vdev_setfru); | |
8515 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
8516 | ||
8517 | /* spare statech is global across all pools) */ | |
8518 | EXPORT_SYMBOL(spa_spare_add); | |
8519 | EXPORT_SYMBOL(spa_spare_remove); | |
8520 | EXPORT_SYMBOL(spa_spare_exists); | |
8521 | EXPORT_SYMBOL(spa_spare_activate); | |
8522 | ||
8523 | /* L2ARC statech is global across all pools) */ | |
8524 | EXPORT_SYMBOL(spa_l2cache_add); | |
8525 | EXPORT_SYMBOL(spa_l2cache_remove); | |
8526 | EXPORT_SYMBOL(spa_l2cache_exists); | |
8527 | EXPORT_SYMBOL(spa_l2cache_activate); | |
8528 | EXPORT_SYMBOL(spa_l2cache_drop); | |
8529 | ||
8530 | /* scanning */ | |
8531 | EXPORT_SYMBOL(spa_scan); | |
8532 | EXPORT_SYMBOL(spa_scan_stop); | |
8533 | ||
8534 | /* spa syncing */ | |
8535 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
8536 | EXPORT_SYMBOL(spa_sync_allpools); | |
8537 | ||
8538 | /* properties */ | |
8539 | EXPORT_SYMBOL(spa_prop_set); | |
8540 | EXPORT_SYMBOL(spa_prop_get); | |
8541 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
8542 | ||
8543 | /* asynchronous event notification */ | |
8544 | EXPORT_SYMBOL(spa_event_notify); | |
8545 | #endif | |
dea377c0 | 8546 | |
93ce2b4c | 8547 | #if defined(_KERNEL) |
dea377c0 MA |
8548 | module_param(spa_load_verify_maxinflight, int, 0644); |
8549 | MODULE_PARM_DESC(spa_load_verify_maxinflight, | |
8550 | "Max concurrent traversal I/Os while verifying pool during import -X"); | |
8551 | ||
8552 | module_param(spa_load_verify_metadata, int, 0644); | |
8553 | MODULE_PARM_DESC(spa_load_verify_metadata, | |
8554 | "Set to traverse metadata on pool import"); | |
8555 | ||
8556 | module_param(spa_load_verify_data, int, 0644); | |
8557 | MODULE_PARM_DESC(spa_load_verify_data, | |
8558 | "Set to traverse data on pool import"); | |
dcb6bed1 | 8559 | |
6cb8e530 PZ |
8560 | module_param(spa_load_print_vdev_tree, int, 0644); |
8561 | MODULE_PARM_DESC(spa_load_print_vdev_tree, | |
8562 | "Print vdev tree to zfs_dbgmsg during pool import"); | |
8563 | ||
02730c33 | 8564 | /* CSTYLED */ |
dcb6bed1 D |
8565 | module_param(zio_taskq_batch_pct, uint, 0444); |
8566 | MODULE_PARM_DESC(zio_taskq_batch_pct, | |
8567 | "Percentage of CPUs to run an IO worker thread"); | |
8568 | ||
6cb8e530 PZ |
8569 | /* BEGIN CSTYLED */ |
8570 | module_param(zfs_max_missing_tvds, ulong, 0644); | |
8571 | MODULE_PARM_DESC(zfs_max_missing_tvds, | |
8572 | "Allow importing pool with up to this number of missing top-level vdevs" | |
8573 | " (in read-only mode)"); | |
8574 | /* END CSTYLED */ | |
8575 | ||
dea377c0 | 8576 | #endif |