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