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