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