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