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