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