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