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