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