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