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