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