Deleted Added
sdiff udiff text old ( 254112 ) new ( 254591 )
full compact
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 */
28
29#include <sys/zfs_context.h>
30#include <sys/fm/fs/zfs.h>
31#include <sys/spa.h>
32#include <sys/spa_impl.h>
33#include <sys/dmu.h>
34#include <sys/dmu_tx.h>
35#include <sys/vdev_impl.h>
36#include <sys/uberblock_impl.h>
37#include <sys/metaslab.h>
38#include <sys/metaslab_impl.h>
39#include <sys/space_map.h>
40#include <sys/zio.h>
41#include <sys/zap.h>
42#include <sys/fs/zfs.h>
43#include <sys/arc.h>
44#include <sys/zil.h>
45#include <sys/dsl_scan.h>
46#include <sys/trim_map.h>
47
48SYSCTL_DECL(_vfs_zfs);
49SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
50
51/*
52 * Virtual device management.
53 */
54
55/**
56 * The limit for ZFS to automatically increase a top-level vdev's ashift
57 * from logical ashift to physical ashift.
58 *
59 * Example: one or more 512B emulation child vdevs
60 * child->vdev_ashift = 9 (512 bytes)
61 * child->vdev_physical_ashift = 12 (4096 bytes)
62 * zfs_max_auto_ashift = 11 (2048 bytes)
63 *
64 * On pool creation or the addition of a new top-leve vdev, ZFS will
65 * bump the ashift of the top-level vdev to 2048.
66 *
67 * Example: one or more 512B emulation child vdevs
68 * child->vdev_ashift = 9 (512 bytes)
69 * child->vdev_physical_ashift = 12 (4096 bytes)
70 * zfs_max_auto_ashift = 13 (8192 bytes)
71 *
72 * On pool creation or the addition of a new top-leve vdev, ZFS will
73 * bump the ashift of the top-level vdev to 4096.
74 */
75static uint64_t zfs_max_auto_ashift = SPA_MAXASHIFT;
76
77static int
78sysctl_vfs_zfs_max_auto_ashift(SYSCTL_HANDLER_ARGS)
79{
80 uint64_t val;
81 int err;
82
83 val = zfs_max_auto_ashift;
84 err = sysctl_handle_64(oidp, &val, 0, req);
85 if (err != 0 || req->newptr == NULL)
86 return (err);
87
88 if (val > SPA_MAXASHIFT)
89 val = SPA_MAXASHIFT;
90
91 zfs_max_auto_ashift = val;
92
93 return (0);
94}
95SYSCTL_PROC(_vfs_zfs, OID_AUTO, max_auto_ashift,
96 CTLTYPE_U64 | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(uint64_t),
97 sysctl_vfs_zfs_max_auto_ashift, "QU",
98 "Cap on logical -> physical ashift adjustment on new top-level vdevs.");
99
100static vdev_ops_t *vdev_ops_table[] = {
101 &vdev_root_ops,
102 &vdev_raidz_ops,
103 &vdev_mirror_ops,
104 &vdev_replacing_ops,
105 &vdev_spare_ops,
106#ifdef _KERNEL
107 &vdev_geom_ops,
108#else
109 &vdev_disk_ops,
110#endif
111 &vdev_file_ops,
112 &vdev_missing_ops,
113 &vdev_hole_ops,
114 NULL
115};
116
117
118/*
119 * Given a vdev type, return the appropriate ops vector.
120 */
121static vdev_ops_t *
122vdev_getops(const char *type)
123{
124 vdev_ops_t *ops, **opspp;
125
126 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
127 if (strcmp(ops->vdev_op_type, type) == 0)
128 break;
129
130 return (ops);
131}
132
133/*
134 * Default asize function: return the MAX of psize with the asize of
135 * all children. This is what's used by anything other than RAID-Z.
136 */
137uint64_t
138vdev_default_asize(vdev_t *vd, uint64_t psize)
139{
140 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
141 uint64_t csize;
142
143 for (int c = 0; c < vd->vdev_children; c++) {
144 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
145 asize = MAX(asize, csize);
146 }
147
148 return (asize);
149}
150
151/*
152 * Get the minimum allocatable size. We define the allocatable size as
153 * the vdev's asize rounded to the nearest metaslab. This allows us to
154 * replace or attach devices which don't have the same physical size but
155 * can still satisfy the same number of allocations.
156 */
157uint64_t
158vdev_get_min_asize(vdev_t *vd)
159{
160 vdev_t *pvd = vd->vdev_parent;
161
162 /*
163 * If our parent is NULL (inactive spare or cache) or is the root,
164 * just return our own asize.
165 */
166 if (pvd == NULL)
167 return (vd->vdev_asize);
168
169 /*
170 * The top-level vdev just returns the allocatable size rounded
171 * to the nearest metaslab.
172 */
173 if (vd == vd->vdev_top)
174 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
175
176 /*
177 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
178 * so each child must provide at least 1/Nth of its asize.
179 */
180 if (pvd->vdev_ops == &vdev_raidz_ops)
181 return (pvd->vdev_min_asize / pvd->vdev_children);
182
183 return (pvd->vdev_min_asize);
184}
185
186void
187vdev_set_min_asize(vdev_t *vd)
188{
189 vd->vdev_min_asize = vdev_get_min_asize(vd);
190
191 for (int c = 0; c < vd->vdev_children; c++)
192 vdev_set_min_asize(vd->vdev_child[c]);
193}
194
195vdev_t *
196vdev_lookup_top(spa_t *spa, uint64_t vdev)
197{
198 vdev_t *rvd = spa->spa_root_vdev;
199
200 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
201
202 if (vdev < rvd->vdev_children) {
203 ASSERT(rvd->vdev_child[vdev] != NULL);
204 return (rvd->vdev_child[vdev]);
205 }
206
207 return (NULL);
208}
209
210vdev_t *
211vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
212{
213 vdev_t *mvd;
214
215 if (vd->vdev_guid == guid)
216 return (vd);
217
218 for (int c = 0; c < vd->vdev_children; c++)
219 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
220 NULL)
221 return (mvd);
222
223 return (NULL);
224}
225
226void
227vdev_add_child(vdev_t *pvd, vdev_t *cvd)
228{
229 size_t oldsize, newsize;
230 uint64_t id = cvd->vdev_id;
231 vdev_t **newchild;
232
233 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
234 ASSERT(cvd->vdev_parent == NULL);
235
236 cvd->vdev_parent = pvd;
237
238 if (pvd == NULL)
239 return;
240
241 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
242
243 oldsize = pvd->vdev_children * sizeof (vdev_t *);
244 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
245 newsize = pvd->vdev_children * sizeof (vdev_t *);
246
247 newchild = kmem_zalloc(newsize, KM_SLEEP);
248 if (pvd->vdev_child != NULL) {
249 bcopy(pvd->vdev_child, newchild, oldsize);
250 kmem_free(pvd->vdev_child, oldsize);
251 }
252
253 pvd->vdev_child = newchild;
254 pvd->vdev_child[id] = cvd;
255
256 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
257 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
258
259 /*
260 * Walk up all ancestors to update guid sum.
261 */
262 for (; pvd != NULL; pvd = pvd->vdev_parent)
263 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
264}
265
266void
267vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
268{
269 int c;
270 uint_t id = cvd->vdev_id;
271
272 ASSERT(cvd->vdev_parent == pvd);
273
274 if (pvd == NULL)
275 return;
276
277 ASSERT(id < pvd->vdev_children);
278 ASSERT(pvd->vdev_child[id] == cvd);
279
280 pvd->vdev_child[id] = NULL;
281 cvd->vdev_parent = NULL;
282
283 for (c = 0; c < pvd->vdev_children; c++)
284 if (pvd->vdev_child[c])
285 break;
286
287 if (c == pvd->vdev_children) {
288 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
289 pvd->vdev_child = NULL;
290 pvd->vdev_children = 0;
291 }
292
293 /*
294 * Walk up all ancestors to update guid sum.
295 */
296 for (; pvd != NULL; pvd = pvd->vdev_parent)
297 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
298}
299
300/*
301 * Remove any holes in the child array.
302 */
303void
304vdev_compact_children(vdev_t *pvd)
305{
306 vdev_t **newchild, *cvd;
307 int oldc = pvd->vdev_children;
308 int newc;
309
310 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
311
312 for (int c = newc = 0; c < oldc; c++)
313 if (pvd->vdev_child[c])
314 newc++;
315
316 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
317
318 for (int c = newc = 0; c < oldc; c++) {
319 if ((cvd = pvd->vdev_child[c]) != NULL) {
320 newchild[newc] = cvd;
321 cvd->vdev_id = newc++;
322 }
323 }
324
325 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
326 pvd->vdev_child = newchild;
327 pvd->vdev_children = newc;
328}
329
330/*
331 * Allocate and minimally initialize a vdev_t.
332 */
333vdev_t *
334vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
335{
336 vdev_t *vd;
337
338 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
339
340 if (spa->spa_root_vdev == NULL) {
341 ASSERT(ops == &vdev_root_ops);
342 spa->spa_root_vdev = vd;
343 spa->spa_load_guid = spa_generate_guid(NULL);
344 }
345
346 if (guid == 0 && ops != &vdev_hole_ops) {
347 if (spa->spa_root_vdev == vd) {
348 /*
349 * The root vdev's guid will also be the pool guid,
350 * which must be unique among all pools.
351 */
352 guid = spa_generate_guid(NULL);
353 } else {
354 /*
355 * Any other vdev's guid must be unique within the pool.
356 */
357 guid = spa_generate_guid(spa);
358 }
359 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
360 }
361
362 vd->vdev_spa = spa;
363 vd->vdev_id = id;
364 vd->vdev_guid = guid;
365 vd->vdev_guid_sum = guid;
366 vd->vdev_ops = ops;
367 vd->vdev_state = VDEV_STATE_CLOSED;
368 vd->vdev_ishole = (ops == &vdev_hole_ops);
369
370 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
371 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
372 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
373 for (int t = 0; t < DTL_TYPES; t++) {
374 space_map_create(&vd->vdev_dtl[t], 0, -1ULL, 0,
375 &vd->vdev_dtl_lock);
376 }
377 txg_list_create(&vd->vdev_ms_list,
378 offsetof(struct metaslab, ms_txg_node));
379 txg_list_create(&vd->vdev_dtl_list,
380 offsetof(struct vdev, vdev_dtl_node));
381 vd->vdev_stat.vs_timestamp = gethrtime();
382 vdev_queue_init(vd);
383 vdev_cache_init(vd);
384
385 return (vd);
386}
387
388/*
389 * Allocate a new vdev. The 'alloctype' is used to control whether we are
390 * creating a new vdev or loading an existing one - the behavior is slightly
391 * different for each case.
392 */
393int
394vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
395 int alloctype)
396{
397 vdev_ops_t *ops;
398 char *type;
399 uint64_t guid = 0, islog, nparity;
400 vdev_t *vd;
401
402 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
403
404 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
405 return (SET_ERROR(EINVAL));
406
407 if ((ops = vdev_getops(type)) == NULL)
408 return (SET_ERROR(EINVAL));
409
410 /*
411 * If this is a load, get the vdev guid from the nvlist.
412 * Otherwise, vdev_alloc_common() will generate one for us.
413 */
414 if (alloctype == VDEV_ALLOC_LOAD) {
415 uint64_t label_id;
416
417 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
418 label_id != id)
419 return (SET_ERROR(EINVAL));
420
421 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
422 return (SET_ERROR(EINVAL));
423 } else if (alloctype == VDEV_ALLOC_SPARE) {
424 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
425 return (SET_ERROR(EINVAL));
426 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
427 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
428 return (SET_ERROR(EINVAL));
429 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
430 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
431 return (SET_ERROR(EINVAL));
432 }
433
434 /*
435 * The first allocated vdev must be of type 'root'.
436 */
437 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
438 return (SET_ERROR(EINVAL));
439
440 /*
441 * Determine whether we're a log vdev.
442 */
443 islog = 0;
444 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
445 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
446 return (SET_ERROR(ENOTSUP));
447
448 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
449 return (SET_ERROR(ENOTSUP));
450
451 /*
452 * Set the nparity property for RAID-Z vdevs.
453 */
454 nparity = -1ULL;
455 if (ops == &vdev_raidz_ops) {
456 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
457 &nparity) == 0) {
458 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
459 return (SET_ERROR(EINVAL));
460 /*
461 * Previous versions could only support 1 or 2 parity
462 * device.
463 */
464 if (nparity > 1 &&
465 spa_version(spa) < SPA_VERSION_RAIDZ2)
466 return (SET_ERROR(ENOTSUP));
467 if (nparity > 2 &&
468 spa_version(spa) < SPA_VERSION_RAIDZ3)
469 return (SET_ERROR(ENOTSUP));
470 } else {
471 /*
472 * We require the parity to be specified for SPAs that
473 * support multiple parity levels.
474 */
475 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
476 return (SET_ERROR(EINVAL));
477 /*
478 * Otherwise, we default to 1 parity device for RAID-Z.
479 */
480 nparity = 1;
481 }
482 } else {
483 nparity = 0;
484 }
485 ASSERT(nparity != -1ULL);
486
487 vd = vdev_alloc_common(spa, id, guid, ops);
488
489 vd->vdev_islog = islog;
490 vd->vdev_nparity = nparity;
491
492 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
493 vd->vdev_path = spa_strdup(vd->vdev_path);
494 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
495 vd->vdev_devid = spa_strdup(vd->vdev_devid);
496 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
497 &vd->vdev_physpath) == 0)
498 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
499 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
500 vd->vdev_fru = spa_strdup(vd->vdev_fru);
501
502 /*
503 * Set the whole_disk property. If it's not specified, leave the value
504 * as -1.
505 */
506 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
507 &vd->vdev_wholedisk) != 0)
508 vd->vdev_wholedisk = -1ULL;
509
510 /*
511 * Look for the 'not present' flag. This will only be set if the device
512 * was not present at the time of import.
513 */
514 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
515 &vd->vdev_not_present);
516
517 /*
518 * Get the alignment requirement.
519 */
520 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
521
522 /*
523 * Retrieve the vdev creation time.
524 */
525 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
526 &vd->vdev_crtxg);
527
528 /*
529 * If we're a top-level vdev, try to load the allocation parameters.
530 */
531 if (parent && !parent->vdev_parent &&
532 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
533 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
534 &vd->vdev_ms_array);
535 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
536 &vd->vdev_ms_shift);
537 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
538 &vd->vdev_asize);
539 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
540 &vd->vdev_removing);
541 }
542
543 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
544 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
545 alloctype == VDEV_ALLOC_ADD ||
546 alloctype == VDEV_ALLOC_SPLIT ||
547 alloctype == VDEV_ALLOC_ROOTPOOL);
548 vd->vdev_mg = metaslab_group_create(islog ?
549 spa_log_class(spa) : spa_normal_class(spa), vd);
550 }
551
552 /*
553 * If we're a leaf vdev, try to load the DTL object and other state.
554 */
555 if (vd->vdev_ops->vdev_op_leaf &&
556 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
557 alloctype == VDEV_ALLOC_ROOTPOOL)) {
558 if (alloctype == VDEV_ALLOC_LOAD) {
559 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
560 &vd->vdev_dtl_smo.smo_object);
561 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
562 &vd->vdev_unspare);
563 }
564
565 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
566 uint64_t spare = 0;
567
568 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
569 &spare) == 0 && spare)
570 spa_spare_add(vd);
571 }
572
573 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
574 &vd->vdev_offline);
575
576 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
577 &vd->vdev_resilver_txg);
578
579 /*
580 * When importing a pool, we want to ignore the persistent fault
581 * state, as the diagnosis made on another system may not be
582 * valid in the current context. Local vdevs will
583 * remain in the faulted state.
584 */
585 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
586 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
587 &vd->vdev_faulted);
588 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
589 &vd->vdev_degraded);
590 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
591 &vd->vdev_removed);
592
593 if (vd->vdev_faulted || vd->vdev_degraded) {
594 char *aux;
595
596 vd->vdev_label_aux =
597 VDEV_AUX_ERR_EXCEEDED;
598 if (nvlist_lookup_string(nv,
599 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
600 strcmp(aux, "external") == 0)
601 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
602 }
603 }
604 }
605
606 /*
607 * Add ourselves to the parent's list of children.
608 */
609 vdev_add_child(parent, vd);
610
611 *vdp = vd;
612
613 return (0);
614}
615
616void
617vdev_free(vdev_t *vd)
618{
619 spa_t *spa = vd->vdev_spa;
620
621 /*
622 * vdev_free() implies closing the vdev first. This is simpler than
623 * trying to ensure complicated semantics for all callers.
624 */
625 vdev_close(vd);
626
627 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
628 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
629
630 /*
631 * Free all children.
632 */
633 for (int c = 0; c < vd->vdev_children; c++)
634 vdev_free(vd->vdev_child[c]);
635
636 ASSERT(vd->vdev_child == NULL);
637 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
638
639 /*
640 * Discard allocation state.
641 */
642 if (vd->vdev_mg != NULL) {
643 vdev_metaslab_fini(vd);
644 metaslab_group_destroy(vd->vdev_mg);
645 }
646
647 ASSERT0(vd->vdev_stat.vs_space);
648 ASSERT0(vd->vdev_stat.vs_dspace);
649 ASSERT0(vd->vdev_stat.vs_alloc);
650
651 /*
652 * Remove this vdev from its parent's child list.
653 */
654 vdev_remove_child(vd->vdev_parent, vd);
655
656 ASSERT(vd->vdev_parent == NULL);
657
658 /*
659 * Clean up vdev structure.
660 */
661 vdev_queue_fini(vd);
662 vdev_cache_fini(vd);
663
664 if (vd->vdev_path)
665 spa_strfree(vd->vdev_path);
666 if (vd->vdev_devid)
667 spa_strfree(vd->vdev_devid);
668 if (vd->vdev_physpath)
669 spa_strfree(vd->vdev_physpath);
670 if (vd->vdev_fru)
671 spa_strfree(vd->vdev_fru);
672
673 if (vd->vdev_isspare)
674 spa_spare_remove(vd);
675 if (vd->vdev_isl2cache)
676 spa_l2cache_remove(vd);
677
678 txg_list_destroy(&vd->vdev_ms_list);
679 txg_list_destroy(&vd->vdev_dtl_list);
680
681 mutex_enter(&vd->vdev_dtl_lock);
682 for (int t = 0; t < DTL_TYPES; t++) {
683 space_map_unload(&vd->vdev_dtl[t]);
684 space_map_destroy(&vd->vdev_dtl[t]);
685 }
686 mutex_exit(&vd->vdev_dtl_lock);
687
688 mutex_destroy(&vd->vdev_dtl_lock);
689 mutex_destroy(&vd->vdev_stat_lock);
690 mutex_destroy(&vd->vdev_probe_lock);
691
692 if (vd == spa->spa_root_vdev)
693 spa->spa_root_vdev = NULL;
694
695 kmem_free(vd, sizeof (vdev_t));
696}
697
698/*
699 * Transfer top-level vdev state from svd to tvd.
700 */
701static void
702vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
703{
704 spa_t *spa = svd->vdev_spa;
705 metaslab_t *msp;
706 vdev_t *vd;
707 int t;
708
709 ASSERT(tvd == tvd->vdev_top);
710
711 tvd->vdev_ms_array = svd->vdev_ms_array;
712 tvd->vdev_ms_shift = svd->vdev_ms_shift;
713 tvd->vdev_ms_count = svd->vdev_ms_count;
714
715 svd->vdev_ms_array = 0;
716 svd->vdev_ms_shift = 0;
717 svd->vdev_ms_count = 0;
718
719 if (tvd->vdev_mg)
720 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
721 tvd->vdev_mg = svd->vdev_mg;
722 tvd->vdev_ms = svd->vdev_ms;
723
724 svd->vdev_mg = NULL;
725 svd->vdev_ms = NULL;
726
727 if (tvd->vdev_mg != NULL)
728 tvd->vdev_mg->mg_vd = tvd;
729
730 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
731 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
732 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
733
734 svd->vdev_stat.vs_alloc = 0;
735 svd->vdev_stat.vs_space = 0;
736 svd->vdev_stat.vs_dspace = 0;
737
738 for (t = 0; t < TXG_SIZE; t++) {
739 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
740 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
741 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
742 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
743 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
744 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
745 }
746
747 if (list_link_active(&svd->vdev_config_dirty_node)) {
748 vdev_config_clean(svd);
749 vdev_config_dirty(tvd);
750 }
751
752 if (list_link_active(&svd->vdev_state_dirty_node)) {
753 vdev_state_clean(svd);
754 vdev_state_dirty(tvd);
755 }
756
757 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
758 svd->vdev_deflate_ratio = 0;
759
760 tvd->vdev_islog = svd->vdev_islog;
761 svd->vdev_islog = 0;
762}
763
764static void
765vdev_top_update(vdev_t *tvd, vdev_t *vd)
766{
767 if (vd == NULL)
768 return;
769
770 vd->vdev_top = tvd;
771
772 for (int c = 0; c < vd->vdev_children; c++)
773 vdev_top_update(tvd, vd->vdev_child[c]);
774}
775
776/*
777 * Add a mirror/replacing vdev above an existing vdev.
778 */
779vdev_t *
780vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
781{
782 spa_t *spa = cvd->vdev_spa;
783 vdev_t *pvd = cvd->vdev_parent;
784 vdev_t *mvd;
785
786 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
787
788 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
789
790 mvd->vdev_asize = cvd->vdev_asize;
791 mvd->vdev_min_asize = cvd->vdev_min_asize;
792 mvd->vdev_max_asize = cvd->vdev_max_asize;
793 mvd->vdev_ashift = cvd->vdev_ashift;
794 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
795 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
796 mvd->vdev_state = cvd->vdev_state;
797 mvd->vdev_crtxg = cvd->vdev_crtxg;
798
799 vdev_remove_child(pvd, cvd);
800 vdev_add_child(pvd, mvd);
801 cvd->vdev_id = mvd->vdev_children;
802 vdev_add_child(mvd, cvd);
803 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
804
805 if (mvd == mvd->vdev_top)
806 vdev_top_transfer(cvd, mvd);
807
808 return (mvd);
809}
810
811/*
812 * Remove a 1-way mirror/replacing vdev from the tree.
813 */
814void
815vdev_remove_parent(vdev_t *cvd)
816{
817 vdev_t *mvd = cvd->vdev_parent;
818 vdev_t *pvd = mvd->vdev_parent;
819
820 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
821
822 ASSERT(mvd->vdev_children == 1);
823 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
824 mvd->vdev_ops == &vdev_replacing_ops ||
825 mvd->vdev_ops == &vdev_spare_ops);
826 cvd->vdev_ashift = mvd->vdev_ashift;
827 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
828 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
829
830 vdev_remove_child(mvd, cvd);
831 vdev_remove_child(pvd, mvd);
832
833 /*
834 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
835 * Otherwise, we could have detached an offline device, and when we
836 * go to import the pool we'll think we have two top-level vdevs,
837 * instead of a different version of the same top-level vdev.
838 */
839 if (mvd->vdev_top == mvd) {
840 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
841 cvd->vdev_orig_guid = cvd->vdev_guid;
842 cvd->vdev_guid += guid_delta;
843 cvd->vdev_guid_sum += guid_delta;
844 }
845 cvd->vdev_id = mvd->vdev_id;
846 vdev_add_child(pvd, cvd);
847 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
848
849 if (cvd == cvd->vdev_top)
850 vdev_top_transfer(mvd, cvd);
851
852 ASSERT(mvd->vdev_children == 0);
853 vdev_free(mvd);
854}
855
856int
857vdev_metaslab_init(vdev_t *vd, uint64_t txg)
858{
859 spa_t *spa = vd->vdev_spa;
860 objset_t *mos = spa->spa_meta_objset;
861 uint64_t m;
862 uint64_t oldc = vd->vdev_ms_count;
863 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
864 metaslab_t **mspp;
865 int error;
866
867 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
868
869 /*
870 * This vdev is not being allocated from yet or is a hole.
871 */
872 if (vd->vdev_ms_shift == 0)
873 return (0);
874
875 ASSERT(!vd->vdev_ishole);
876
877 /*
878 * Compute the raidz-deflation ratio. Note, we hard-code
879 * in 128k (1 << 17) because it is the current "typical" blocksize.
880 * Even if SPA_MAXBLOCKSIZE changes, this algorithm must never change,
881 * or we will inconsistently account for existing bp's.
882 */
883 vd->vdev_deflate_ratio = (1 << 17) /
884 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
885
886 ASSERT(oldc <= newc);
887
888 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
889
890 if (oldc != 0) {
891 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
892 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
893 }
894
895 vd->vdev_ms = mspp;
896 vd->vdev_ms_count = newc;
897
898 for (m = oldc; m < newc; m++) {
899 space_map_obj_t smo = { 0, 0, 0 };
900 if (txg == 0) {
901 uint64_t object = 0;
902 error = dmu_read(mos, vd->vdev_ms_array,
903 m * sizeof (uint64_t), sizeof (uint64_t), &object,
904 DMU_READ_PREFETCH);
905 if (error)
906 return (error);
907 if (object != 0) {
908 dmu_buf_t *db;
909 error = dmu_bonus_hold(mos, object, FTAG, &db);
910 if (error)
911 return (error);
912 ASSERT3U(db->db_size, >=, sizeof (smo));
913 bcopy(db->db_data, &smo, sizeof (smo));
914 ASSERT3U(smo.smo_object, ==, object);
915 dmu_buf_rele(db, FTAG);
916 }
917 }
918 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
919 m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
920 }
921
922 if (txg == 0)
923 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
924
925 /*
926 * If the vdev is being removed we don't activate
927 * the metaslabs since we want to ensure that no new
928 * allocations are performed on this device.
929 */
930 if (oldc == 0 && !vd->vdev_removing)
931 metaslab_group_activate(vd->vdev_mg);
932
933 if (txg == 0)
934 spa_config_exit(spa, SCL_ALLOC, FTAG);
935
936 return (0);
937}
938
939void
940vdev_metaslab_fini(vdev_t *vd)
941{
942 uint64_t m;
943 uint64_t count = vd->vdev_ms_count;
944
945 if (vd->vdev_ms != NULL) {
946 metaslab_group_passivate(vd->vdev_mg);
947 for (m = 0; m < count; m++)
948 if (vd->vdev_ms[m] != NULL)
949 metaslab_fini(vd->vdev_ms[m]);
950 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
951 vd->vdev_ms = NULL;
952 }
953}
954
955typedef struct vdev_probe_stats {
956 boolean_t vps_readable;
957 boolean_t vps_writeable;
958 int vps_flags;
959} vdev_probe_stats_t;
960
961static void
962vdev_probe_done(zio_t *zio)
963{
964 spa_t *spa = zio->io_spa;
965 vdev_t *vd = zio->io_vd;
966 vdev_probe_stats_t *vps = zio->io_private;
967
968 ASSERT(vd->vdev_probe_zio != NULL);
969
970 if (zio->io_type == ZIO_TYPE_READ) {
971 if (zio->io_error == 0)
972 vps->vps_readable = 1;
973 if (zio->io_error == 0 && spa_writeable(spa)) {
974 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
975 zio->io_offset, zio->io_size, zio->io_data,
976 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
977 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
978 } else {
979 zio_buf_free(zio->io_data, zio->io_size);
980 }
981 } else if (zio->io_type == ZIO_TYPE_WRITE) {
982 if (zio->io_error == 0)
983 vps->vps_writeable = 1;
984 zio_buf_free(zio->io_data, zio->io_size);
985 } else if (zio->io_type == ZIO_TYPE_NULL) {
986 zio_t *pio;
987
988 vd->vdev_cant_read |= !vps->vps_readable;
989 vd->vdev_cant_write |= !vps->vps_writeable;
990
991 if (vdev_readable(vd) &&
992 (vdev_writeable(vd) || !spa_writeable(spa))) {
993 zio->io_error = 0;
994 } else {
995 ASSERT(zio->io_error != 0);
996 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
997 spa, vd, NULL, 0, 0);
998 zio->io_error = SET_ERROR(ENXIO);
999 }
1000
1001 mutex_enter(&vd->vdev_probe_lock);
1002 ASSERT(vd->vdev_probe_zio == zio);
1003 vd->vdev_probe_zio = NULL;
1004 mutex_exit(&vd->vdev_probe_lock);
1005
1006 while ((pio = zio_walk_parents(zio)) != NULL)
1007 if (!vdev_accessible(vd, pio))
1008 pio->io_error = SET_ERROR(ENXIO);
1009
1010 kmem_free(vps, sizeof (*vps));
1011 }
1012}
1013
1014/*
1015 * Determine whether this device is accessible.
1016 *
1017 * Read and write to several known locations: the pad regions of each
1018 * vdev label but the first, which we leave alone in case it contains
1019 * a VTOC.
1020 */
1021zio_t *
1022vdev_probe(vdev_t *vd, zio_t *zio)
1023{
1024 spa_t *spa = vd->vdev_spa;
1025 vdev_probe_stats_t *vps = NULL;
1026 zio_t *pio;
1027
1028 ASSERT(vd->vdev_ops->vdev_op_leaf);
1029
1030 /*
1031 * Don't probe the probe.
1032 */
1033 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1034 return (NULL);
1035
1036 /*
1037 * To prevent 'probe storms' when a device fails, we create
1038 * just one probe i/o at a time. All zios that want to probe
1039 * this vdev will become parents of the probe io.
1040 */
1041 mutex_enter(&vd->vdev_probe_lock);
1042
1043 if ((pio = vd->vdev_probe_zio) == NULL) {
1044 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1045
1046 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1047 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1048 ZIO_FLAG_TRYHARD;
1049
1050 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1051 /*
1052 * vdev_cant_read and vdev_cant_write can only
1053 * transition from TRUE to FALSE when we have the
1054 * SCL_ZIO lock as writer; otherwise they can only
1055 * transition from FALSE to TRUE. This ensures that
1056 * any zio looking at these values can assume that
1057 * failures persist for the life of the I/O. That's
1058 * important because when a device has intermittent
1059 * connectivity problems, we want to ensure that
1060 * they're ascribed to the device (ENXIO) and not
1061 * the zio (EIO).
1062 *
1063 * Since we hold SCL_ZIO as writer here, clear both
1064 * values so the probe can reevaluate from first
1065 * principles.
1066 */
1067 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1068 vd->vdev_cant_read = B_FALSE;
1069 vd->vdev_cant_write = B_FALSE;
1070 }
1071
1072 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1073 vdev_probe_done, vps,
1074 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1075
1076 /*
1077 * We can't change the vdev state in this context, so we
1078 * kick off an async task to do it on our behalf.
1079 */
1080 if (zio != NULL) {
1081 vd->vdev_probe_wanted = B_TRUE;
1082 spa_async_request(spa, SPA_ASYNC_PROBE);
1083 }
1084 }
1085
1086 if (zio != NULL)
1087 zio_add_child(zio, pio);
1088
1089 mutex_exit(&vd->vdev_probe_lock);
1090
1091 if (vps == NULL) {
1092 ASSERT(zio != NULL);
1093 return (NULL);
1094 }
1095
1096 for (int l = 1; l < VDEV_LABELS; l++) {
1097 zio_nowait(zio_read_phys(pio, vd,
1098 vdev_label_offset(vd->vdev_psize, l,
1099 offsetof(vdev_label_t, vl_pad2)),
1100 VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1101 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1102 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1103 }
1104
1105 if (zio == NULL)
1106 return (pio);
1107
1108 zio_nowait(pio);
1109 return (NULL);
1110}
1111
1112static void
1113vdev_open_child(void *arg)
1114{
1115 vdev_t *vd = arg;
1116
1117 vd->vdev_open_thread = curthread;
1118 vd->vdev_open_error = vdev_open(vd);
1119 vd->vdev_open_thread = NULL;
1120}
1121
1122boolean_t
1123vdev_uses_zvols(vdev_t *vd)
1124{
1125 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1126 strlen(ZVOL_DIR)) == 0)
1127 return (B_TRUE);
1128 for (int c = 0; c < vd->vdev_children; c++)
1129 if (vdev_uses_zvols(vd->vdev_child[c]))
1130 return (B_TRUE);
1131 return (B_FALSE);
1132}
1133
1134void
1135vdev_open_children(vdev_t *vd)
1136{
1137 taskq_t *tq;
1138 int children = vd->vdev_children;
1139
1140 /*
1141 * in order to handle pools on top of zvols, do the opens
1142 * in a single thread so that the same thread holds the
1143 * spa_namespace_lock
1144 */
1145 if (B_TRUE || vdev_uses_zvols(vd)) {
1146 for (int c = 0; c < children; c++)
1147 vd->vdev_child[c]->vdev_open_error =
1148 vdev_open(vd->vdev_child[c]);
1149 return;
1150 }
1151 tq = taskq_create("vdev_open", children, minclsyspri,
1152 children, children, TASKQ_PREPOPULATE);
1153
1154 for (int c = 0; c < children; c++)
1155 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1156 TQ_SLEEP) != 0);
1157
1158 taskq_destroy(tq);
1159}
1160
1161/*
1162 * Prepare a virtual device for access.
1163 */
1164int
1165vdev_open(vdev_t *vd)
1166{
1167 spa_t *spa = vd->vdev_spa;
1168 int error;
1169 uint64_t osize = 0;
1170 uint64_t max_osize = 0;
1171 uint64_t asize, max_asize, psize;
1172 uint64_t logical_ashift = 0;
1173 uint64_t physical_ashift = 0;
1174
1175 ASSERT(vd->vdev_open_thread == curthread ||
1176 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1177 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1178 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1179 vd->vdev_state == VDEV_STATE_OFFLINE);
1180
1181 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1182 vd->vdev_cant_read = B_FALSE;
1183 vd->vdev_cant_write = B_FALSE;
1184 vd->vdev_min_asize = vdev_get_min_asize(vd);
1185
1186 /*
1187 * If this vdev is not removed, check its fault status. If it's
1188 * faulted, bail out of the open.
1189 */
1190 if (!vd->vdev_removed && vd->vdev_faulted) {
1191 ASSERT(vd->vdev_children == 0);
1192 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1193 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1194 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1195 vd->vdev_label_aux);
1196 return (SET_ERROR(ENXIO));
1197 } else if (vd->vdev_offline) {
1198 ASSERT(vd->vdev_children == 0);
1199 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1200 return (SET_ERROR(ENXIO));
1201 }
1202
1203 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
1204 &logical_ashift, &physical_ashift);
1205
1206 /*
1207 * Reset the vdev_reopening flag so that we actually close
1208 * the vdev on error.
1209 */
1210 vd->vdev_reopening = B_FALSE;
1211 if (zio_injection_enabled && error == 0)
1212 error = zio_handle_device_injection(vd, NULL, ENXIO);
1213
1214 if (error) {
1215 if (vd->vdev_removed &&
1216 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1217 vd->vdev_removed = B_FALSE;
1218
1219 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1220 vd->vdev_stat.vs_aux);
1221 return (error);
1222 }
1223
1224 vd->vdev_removed = B_FALSE;
1225
1226 /*
1227 * Recheck the faulted flag now that we have confirmed that
1228 * the vdev is accessible. If we're faulted, bail.
1229 */
1230 if (vd->vdev_faulted) {
1231 ASSERT(vd->vdev_children == 0);
1232 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1233 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1234 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1235 vd->vdev_label_aux);
1236 return (SET_ERROR(ENXIO));
1237 }
1238
1239 if (vd->vdev_degraded) {
1240 ASSERT(vd->vdev_children == 0);
1241 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1242 VDEV_AUX_ERR_EXCEEDED);
1243 } else {
1244 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1245 }
1246
1247 /*
1248 * For hole or missing vdevs we just return success.
1249 */
1250 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1251 return (0);
1252
1253 if (vd->vdev_ops->vdev_op_leaf) {
1254 vd->vdev_notrim = B_FALSE;
1255 trim_map_create(vd);
1256 }
1257
1258 for (int c = 0; c < vd->vdev_children; c++) {
1259 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1260 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1261 VDEV_AUX_NONE);
1262 break;
1263 }
1264 }
1265
1266 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1267 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1268
1269 if (vd->vdev_children == 0) {
1270 if (osize < SPA_MINDEVSIZE) {
1271 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1272 VDEV_AUX_TOO_SMALL);
1273 return (SET_ERROR(EOVERFLOW));
1274 }
1275 psize = osize;
1276 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1277 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1278 VDEV_LABEL_END_SIZE);
1279 } else {
1280 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1281 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1282 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1283 VDEV_AUX_TOO_SMALL);
1284 return (SET_ERROR(EOVERFLOW));
1285 }
1286 psize = 0;
1287 asize = osize;
1288 max_asize = max_osize;
1289 }
1290
1291 vd->vdev_psize = psize;
1292
1293 /*
1294 * Make sure the allocatable size hasn't shrunk.
1295 */
1296 if (asize < vd->vdev_min_asize) {
1297 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1298 VDEV_AUX_BAD_LABEL);
1299 return (SET_ERROR(EINVAL));
1300 }
1301
1302 vd->vdev_physical_ashift =
1303 MAX(physical_ashift, vd->vdev_physical_ashift);
1304 vd->vdev_logical_ashift = MAX(logical_ashift, vd->vdev_logical_ashift);
1305 vd->vdev_ashift = MAX(vd->vdev_logical_ashift, vd->vdev_ashift);
1306
1307 if (vd->vdev_logical_ashift > SPA_MAXASHIFT) {
1308 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1309 VDEV_AUX_ASHIFT_TOO_BIG);
1310 return (EINVAL);
1311 }
1312
1313 if (vd->vdev_asize == 0) {
1314 /*
1315 * This is the first-ever open, so use the computed values.
1316 * For testing purposes, a higher ashift can be requested.
1317 */
1318 vd->vdev_asize = asize;
1319 vd->vdev_max_asize = max_asize;
1320 } else {
1321 /*
1322 * Make sure the alignment requirement hasn't increased.
1323 */
1324 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
1325 vd->vdev_ops->vdev_op_leaf) {
1326 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1327 VDEV_AUX_BAD_LABEL);
1328 return (EINVAL);
1329 }
1330 vd->vdev_max_asize = max_asize;
1331 }
1332
1333 /*
1334 * If all children are healthy and the asize has increased,
1335 * then we've experienced dynamic LUN growth. If automatic
1336 * expansion is enabled then use the additional space.
1337 */
1338 if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1339 (vd->vdev_expanding || spa->spa_autoexpand))
1340 vd->vdev_asize = asize;
1341
1342 vdev_set_min_asize(vd);
1343
1344 /*
1345 * Ensure we can issue some IO before declaring the
1346 * vdev open for business.
1347 */
1348 if (vd->vdev_ops->vdev_op_leaf &&
1349 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1350 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1351 VDEV_AUX_ERR_EXCEEDED);
1352 return (error);
1353 }
1354
1355 /*
1356 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1357 * resilver. But don't do this if we are doing a reopen for a scrub,
1358 * since this would just restart the scrub we are already doing.
1359 */
1360 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1361 vdev_resilver_needed(vd, NULL, NULL))
1362 spa_async_request(spa, SPA_ASYNC_RESILVER);
1363
1364 return (0);
1365}
1366
1367/*
1368 * Called once the vdevs are all opened, this routine validates the label
1369 * contents. This needs to be done before vdev_load() so that we don't
1370 * inadvertently do repair I/Os to the wrong device.
1371 *
1372 * If 'strict' is false ignore the spa guid check. This is necessary because
1373 * if the machine crashed during a re-guid the new guid might have been written
1374 * to all of the vdev labels, but not the cached config. The strict check
1375 * will be performed when the pool is opened again using the mos config.
1376 *
1377 * This function will only return failure if one of the vdevs indicates that it
1378 * has since been destroyed or exported. This is only possible if
1379 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1380 * will be updated but the function will return 0.
1381 */
1382int
1383vdev_validate(vdev_t *vd, boolean_t strict)
1384{
1385 spa_t *spa = vd->vdev_spa;
1386 nvlist_t *label;
1387 uint64_t guid = 0, top_guid;
1388 uint64_t state;
1389
1390 for (int c = 0; c < vd->vdev_children; c++)
1391 if (vdev_validate(vd->vdev_child[c], strict) != 0)
1392 return (SET_ERROR(EBADF));
1393
1394 /*
1395 * If the device has already failed, or was marked offline, don't do
1396 * any further validation. Otherwise, label I/O will fail and we will
1397 * overwrite the previous state.
1398 */
1399 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1400 uint64_t aux_guid = 0;
1401 nvlist_t *nvl;
1402 uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1403 spa_last_synced_txg(spa) : -1ULL;
1404
1405 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1406 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1407 VDEV_AUX_BAD_LABEL);
1408 return (0);
1409 }
1410
1411 /*
1412 * Determine if this vdev has been split off into another
1413 * pool. If so, then refuse to open it.
1414 */
1415 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1416 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1417 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1418 VDEV_AUX_SPLIT_POOL);
1419 nvlist_free(label);
1420 return (0);
1421 }
1422
1423 if (strict && (nvlist_lookup_uint64(label,
1424 ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1425 guid != spa_guid(spa))) {
1426 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1427 VDEV_AUX_CORRUPT_DATA);
1428 nvlist_free(label);
1429 return (0);
1430 }
1431
1432 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1433 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1434 &aux_guid) != 0)
1435 aux_guid = 0;
1436
1437 /*
1438 * If this vdev just became a top-level vdev because its
1439 * sibling was detached, it will have adopted the parent's
1440 * vdev guid -- but the label may or may not be on disk yet.
1441 * Fortunately, either version of the label will have the
1442 * same top guid, so if we're a top-level vdev, we can
1443 * safely compare to that instead.
1444 *
1445 * If we split this vdev off instead, then we also check the
1446 * original pool's guid. We don't want to consider the vdev
1447 * corrupt if it is partway through a split operation.
1448 */
1449 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1450 &guid) != 0 ||
1451 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1452 &top_guid) != 0 ||
1453 ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1454 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1455 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1456 VDEV_AUX_CORRUPT_DATA);
1457 nvlist_free(label);
1458 return (0);
1459 }
1460
1461 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1462 &state) != 0) {
1463 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1464 VDEV_AUX_CORRUPT_DATA);
1465 nvlist_free(label);
1466 return (0);
1467 }
1468
1469 nvlist_free(label);
1470
1471 /*
1472 * If this is a verbatim import, no need to check the
1473 * state of the pool.
1474 */
1475 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1476 spa_load_state(spa) == SPA_LOAD_OPEN &&
1477 state != POOL_STATE_ACTIVE)
1478 return (SET_ERROR(EBADF));
1479
1480 /*
1481 * If we were able to open and validate a vdev that was
1482 * previously marked permanently unavailable, clear that state
1483 * now.
1484 */
1485 if (vd->vdev_not_present)
1486 vd->vdev_not_present = 0;
1487 }
1488
1489 return (0);
1490}
1491
1492/*
1493 * Close a virtual device.
1494 */
1495void
1496vdev_close(vdev_t *vd)
1497{
1498 spa_t *spa = vd->vdev_spa;
1499 vdev_t *pvd = vd->vdev_parent;
1500
1501 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1502
1503 /*
1504 * If our parent is reopening, then we are as well, unless we are
1505 * going offline.
1506 */
1507 if (pvd != NULL && pvd->vdev_reopening)
1508 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1509
1510 vd->vdev_ops->vdev_op_close(vd);
1511
1512 vdev_cache_purge(vd);
1513
1514 if (vd->vdev_ops->vdev_op_leaf)
1515 trim_map_destroy(vd);
1516
1517 /*
1518 * We record the previous state before we close it, so that if we are
1519 * doing a reopen(), we don't generate FMA ereports if we notice that
1520 * it's still faulted.
1521 */
1522 vd->vdev_prevstate = vd->vdev_state;
1523
1524 if (vd->vdev_offline)
1525 vd->vdev_state = VDEV_STATE_OFFLINE;
1526 else
1527 vd->vdev_state = VDEV_STATE_CLOSED;
1528 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1529}
1530
1531void
1532vdev_hold(vdev_t *vd)
1533{
1534 spa_t *spa = vd->vdev_spa;
1535
1536 ASSERT(spa_is_root(spa));
1537 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1538 return;
1539
1540 for (int c = 0; c < vd->vdev_children; c++)
1541 vdev_hold(vd->vdev_child[c]);
1542
1543 if (vd->vdev_ops->vdev_op_leaf)
1544 vd->vdev_ops->vdev_op_hold(vd);
1545}
1546
1547void
1548vdev_rele(vdev_t *vd)
1549{
1550 spa_t *spa = vd->vdev_spa;
1551
1552 ASSERT(spa_is_root(spa));
1553 for (int c = 0; c < vd->vdev_children; c++)
1554 vdev_rele(vd->vdev_child[c]);
1555
1556 if (vd->vdev_ops->vdev_op_leaf)
1557 vd->vdev_ops->vdev_op_rele(vd);
1558}
1559
1560/*
1561 * Reopen all interior vdevs and any unopened leaves. We don't actually
1562 * reopen leaf vdevs which had previously been opened as they might deadlock
1563 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1564 * If the leaf has never been opened then open it, as usual.
1565 */
1566void
1567vdev_reopen(vdev_t *vd)
1568{
1569 spa_t *spa = vd->vdev_spa;
1570
1571 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1572
1573 /* set the reopening flag unless we're taking the vdev offline */
1574 vd->vdev_reopening = !vd->vdev_offline;
1575 vdev_close(vd);
1576 (void) vdev_open(vd);
1577
1578 /*
1579 * Call vdev_validate() here to make sure we have the same device.
1580 * Otherwise, a device with an invalid label could be successfully
1581 * opened in response to vdev_reopen().
1582 */
1583 if (vd->vdev_aux) {
1584 (void) vdev_validate_aux(vd);
1585 if (vdev_readable(vd) && vdev_writeable(vd) &&
1586 vd->vdev_aux == &spa->spa_l2cache &&
1587 !l2arc_vdev_present(vd))
1588 l2arc_add_vdev(spa, vd);
1589 } else {
1590 (void) vdev_validate(vd, B_TRUE);
1591 }
1592
1593 /*
1594 * Reassess parent vdev's health.
1595 */
1596 vdev_propagate_state(vd);
1597}
1598
1599int
1600vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1601{
1602 int error;
1603
1604 /*
1605 * Normally, partial opens (e.g. of a mirror) are allowed.
1606 * For a create, however, we want to fail the request if
1607 * there are any components we can't open.
1608 */
1609 error = vdev_open(vd);
1610
1611 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1612 vdev_close(vd);
1613 return (error ? error : ENXIO);
1614 }
1615
1616 /*
1617 * Recursively initialize all labels.
1618 */
1619 if ((error = vdev_label_init(vd, txg, isreplacing ?
1620 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1621 vdev_close(vd);
1622 return (error);
1623 }
1624
1625 return (0);
1626}
1627
1628void
1629vdev_metaslab_set_size(vdev_t *vd)
1630{
1631 /*
1632 * Aim for roughly 200 metaslabs per vdev.
1633 */
1634 vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1635 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1636}
1637
1638/*
1639 * Maximize performance by inflating the configured ashift for
1640 * top level vdevs to be as close to the physical ashift as
1641 * possible without exceeding the administrator specified
1642 * limit.
1643 */
1644void
1645vdev_ashift_optimize(vdev_t *vd)
1646{
1647 if (vd == vd->vdev_top &&
1648 (vd->vdev_ashift < vd->vdev_physical_ashift) &&
1649 (vd->vdev_ashift < zfs_max_auto_ashift)) {
1650 vd->vdev_ashift = MIN(zfs_max_auto_ashift,
1651 vd->vdev_physical_ashift);
1652 }
1653}
1654
1655void
1656vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1657{
1658 ASSERT(vd == vd->vdev_top);
1659 ASSERT(!vd->vdev_ishole);
1660 ASSERT(ISP2(flags));
1661 ASSERT(spa_writeable(vd->vdev_spa));
1662
1663 if (flags & VDD_METASLAB)
1664 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1665
1666 if (flags & VDD_DTL)
1667 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1668
1669 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1670}
1671
1672/*
1673 * DTLs.
1674 *
1675 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1676 * the vdev has less than perfect replication. There are four kinds of DTL:
1677 *
1678 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1679 *
1680 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1681 *
1682 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1683 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1684 * txgs that was scrubbed.
1685 *
1686 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1687 * persistent errors or just some device being offline.
1688 * Unlike the other three, the DTL_OUTAGE map is not generally
1689 * maintained; it's only computed when needed, typically to
1690 * determine whether a device can be detached.
1691 *
1692 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1693 * either has the data or it doesn't.
1694 *
1695 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1696 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1697 * if any child is less than fully replicated, then so is its parent.
1698 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1699 * comprising only those txgs which appear in 'maxfaults' or more children;
1700 * those are the txgs we don't have enough replication to read. For example,
1701 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1702 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1703 * two child DTL_MISSING maps.
1704 *
1705 * It should be clear from the above that to compute the DTLs and outage maps
1706 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1707 * Therefore, that is all we keep on disk. When loading the pool, or after
1708 * a configuration change, we generate all other DTLs from first principles.
1709 */
1710void
1711vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1712{
1713 space_map_t *sm = &vd->vdev_dtl[t];
1714
1715 ASSERT(t < DTL_TYPES);
1716 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1717 ASSERT(spa_writeable(vd->vdev_spa));
1718
1719 mutex_enter(sm->sm_lock);
1720 if (!space_map_contains(sm, txg, size))
1721 space_map_add(sm, txg, size);
1722 mutex_exit(sm->sm_lock);
1723}
1724
1725boolean_t
1726vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1727{
1728 space_map_t *sm = &vd->vdev_dtl[t];
1729 boolean_t dirty = B_FALSE;
1730
1731 ASSERT(t < DTL_TYPES);
1732 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1733
1734 mutex_enter(sm->sm_lock);
1735 if (sm->sm_space != 0)
1736 dirty = space_map_contains(sm, txg, size);
1737 mutex_exit(sm->sm_lock);
1738
1739 return (dirty);
1740}
1741
1742boolean_t
1743vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
1744{
1745 space_map_t *sm = &vd->vdev_dtl[t];
1746 boolean_t empty;
1747
1748 mutex_enter(sm->sm_lock);
1749 empty = (sm->sm_space == 0);
1750 mutex_exit(sm->sm_lock);
1751
1752 return (empty);
1753}
1754
1755/*
1756 * Returns the lowest txg in the DTL range.
1757 */
1758static uint64_t
1759vdev_dtl_min(vdev_t *vd)
1760{
1761 space_seg_t *ss;
1762
1763 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1764 ASSERT3U(vd->vdev_dtl[DTL_MISSING].sm_space, !=, 0);
1765 ASSERT0(vd->vdev_children);
1766
1767 ss = avl_first(&vd->vdev_dtl[DTL_MISSING].sm_root);
1768 return (ss->ss_start - 1);
1769}
1770
1771/*
1772 * Returns the highest txg in the DTL.
1773 */
1774static uint64_t
1775vdev_dtl_max(vdev_t *vd)
1776{
1777 space_seg_t *ss;
1778
1779 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1780 ASSERT3U(vd->vdev_dtl[DTL_MISSING].sm_space, !=, 0);
1781 ASSERT0(vd->vdev_children);
1782
1783 ss = avl_last(&vd->vdev_dtl[DTL_MISSING].sm_root);
1784 return (ss->ss_end);
1785}
1786
1787/*
1788 * Determine if a resilvering vdev should remove any DTL entries from
1789 * its range. If the vdev was resilvering for the entire duration of the
1790 * scan then it should excise that range from its DTLs. Otherwise, this
1791 * vdev is considered partially resilvered and should leave its DTL
1792 * entries intact. The comment in vdev_dtl_reassess() describes how we
1793 * excise the DTLs.
1794 */
1795static boolean_t
1796vdev_dtl_should_excise(vdev_t *vd)
1797{
1798 spa_t *spa = vd->vdev_spa;
1799 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1800
1801 ASSERT0(scn->scn_phys.scn_errors);
1802 ASSERT0(vd->vdev_children);
1803
1804 if (vd->vdev_resilver_txg == 0 ||
1805 vd->vdev_dtl[DTL_MISSING].sm_space == 0)
1806 return (B_TRUE);
1807
1808 /*
1809 * When a resilver is initiated the scan will assign the scn_max_txg
1810 * value to the highest txg value that exists in all DTLs. If this
1811 * device's max DTL is not part of this scan (i.e. it is not in
1812 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1813 * for excision.
1814 */
1815 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1816 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1817 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1818 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1819 return (B_TRUE);
1820 }
1821 return (B_FALSE);
1822}
1823
1824/*
1825 * Reassess DTLs after a config change or scrub completion.
1826 */
1827void
1828vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1829{
1830 spa_t *spa = vd->vdev_spa;
1831 avl_tree_t reftree;
1832 int minref;
1833
1834 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1835
1836 for (int c = 0; c < vd->vdev_children; c++)
1837 vdev_dtl_reassess(vd->vdev_child[c], txg,
1838 scrub_txg, scrub_done);
1839
1840 if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
1841 return;
1842
1843 if (vd->vdev_ops->vdev_op_leaf) {
1844 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1845
1846 mutex_enter(&vd->vdev_dtl_lock);
1847
1848 /*
1849 * If we've completed a scan cleanly then determine
1850 * if this vdev should remove any DTLs. We only want to
1851 * excise regions on vdevs that were available during
1852 * the entire duration of this scan.
1853 */
1854 if (scrub_txg != 0 &&
1855 (spa->spa_scrub_started ||
1856 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1857 vdev_dtl_should_excise(vd)) {
1858 /*
1859 * We completed a scrub up to scrub_txg. If we
1860 * did it without rebooting, then the scrub dtl
1861 * will be valid, so excise the old region and
1862 * fold in the scrub dtl. Otherwise, leave the
1863 * dtl as-is if there was an error.
1864 *
1865 * There's little trick here: to excise the beginning
1866 * of the DTL_MISSING map, we put it into a reference
1867 * tree and then add a segment with refcnt -1 that
1868 * covers the range [0, scrub_txg). This means
1869 * that each txg in that range has refcnt -1 or 0.
1870 * We then add DTL_SCRUB with a refcnt of 2, so that
1871 * entries in the range [0, scrub_txg) will have a
1872 * positive refcnt -- either 1 or 2. We then convert
1873 * the reference tree into the new DTL_MISSING map.
1874 */
1875 space_map_ref_create(&reftree);
1876 space_map_ref_add_map(&reftree,
1877 &vd->vdev_dtl[DTL_MISSING], 1);
1878 space_map_ref_add_seg(&reftree, 0, scrub_txg, -1);
1879 space_map_ref_add_map(&reftree,
1880 &vd->vdev_dtl[DTL_SCRUB], 2);
1881 space_map_ref_generate_map(&reftree,
1882 &vd->vdev_dtl[DTL_MISSING], 1);
1883 space_map_ref_destroy(&reftree);
1884 }
1885 space_map_vacate(&vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
1886 space_map_walk(&vd->vdev_dtl[DTL_MISSING],
1887 space_map_add, &vd->vdev_dtl[DTL_PARTIAL]);
1888 if (scrub_done)
1889 space_map_vacate(&vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
1890 space_map_vacate(&vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
1891 if (!vdev_readable(vd))
1892 space_map_add(&vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
1893 else
1894 space_map_walk(&vd->vdev_dtl[DTL_MISSING],
1895 space_map_add, &vd->vdev_dtl[DTL_OUTAGE]);
1896
1897 /*
1898 * If the vdev was resilvering and no longer has any
1899 * DTLs then reset its resilvering flag.
1900 */
1901 if (vd->vdev_resilver_txg != 0 &&
1902 vd->vdev_dtl[DTL_MISSING].sm_space == 0 &&
1903 vd->vdev_dtl[DTL_OUTAGE].sm_space == 0)
1904 vd->vdev_resilver_txg = 0;
1905
1906 mutex_exit(&vd->vdev_dtl_lock);
1907
1908 if (txg != 0)
1909 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1910 return;
1911 }
1912
1913 mutex_enter(&vd->vdev_dtl_lock);
1914 for (int t = 0; t < DTL_TYPES; t++) {
1915 /* account for child's outage in parent's missing map */
1916 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
1917 if (t == DTL_SCRUB)
1918 continue; /* leaf vdevs only */
1919 if (t == DTL_PARTIAL)
1920 minref = 1; /* i.e. non-zero */
1921 else if (vd->vdev_nparity != 0)
1922 minref = vd->vdev_nparity + 1; /* RAID-Z */
1923 else
1924 minref = vd->vdev_children; /* any kind of mirror */
1925 space_map_ref_create(&reftree);
1926 for (int c = 0; c < vd->vdev_children; c++) {
1927 vdev_t *cvd = vd->vdev_child[c];
1928 mutex_enter(&cvd->vdev_dtl_lock);
1929 space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1);
1930 mutex_exit(&cvd->vdev_dtl_lock);
1931 }
1932 space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
1933 space_map_ref_destroy(&reftree);
1934 }
1935 mutex_exit(&vd->vdev_dtl_lock);
1936}
1937
1938static int
1939vdev_dtl_load(vdev_t *vd)
1940{
1941 spa_t *spa = vd->vdev_spa;
1942 space_map_obj_t *smo = &vd->vdev_dtl_smo;
1943 objset_t *mos = spa->spa_meta_objset;
1944 dmu_buf_t *db;
1945 int error;
1946
1947 ASSERT(vd->vdev_children == 0);
1948
1949 if (smo->smo_object == 0)
1950 return (0);
1951
1952 ASSERT(!vd->vdev_ishole);
1953
1954 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1955 return (error);
1956
1957 ASSERT3U(db->db_size, >=, sizeof (*smo));
1958 bcopy(db->db_data, smo, sizeof (*smo));
1959 dmu_buf_rele(db, FTAG);
1960
1961 mutex_enter(&vd->vdev_dtl_lock);
1962 error = space_map_load(&vd->vdev_dtl[DTL_MISSING],
1963 NULL, SM_ALLOC, smo, mos);
1964 mutex_exit(&vd->vdev_dtl_lock);
1965
1966 return (error);
1967}
1968
1969void
1970vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1971{
1972 spa_t *spa = vd->vdev_spa;
1973 space_map_obj_t *smo = &vd->vdev_dtl_smo;
1974 space_map_t *sm = &vd->vdev_dtl[DTL_MISSING];
1975 objset_t *mos = spa->spa_meta_objset;
1976 space_map_t smsync;
1977 kmutex_t smlock;
1978 dmu_buf_t *db;
1979 dmu_tx_t *tx;
1980
1981 ASSERT(!vd->vdev_ishole);
1982
1983 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1984
1985 if (vd->vdev_detached) {
1986 if (smo->smo_object != 0) {
1987 int err = dmu_object_free(mos, smo->smo_object, tx);
1988 ASSERT0(err);
1989 smo->smo_object = 0;
1990 }
1991 dmu_tx_commit(tx);
1992 return;
1993 }
1994
1995 if (smo->smo_object == 0) {
1996 ASSERT(smo->smo_objsize == 0);
1997 ASSERT(smo->smo_alloc == 0);
1998 smo->smo_object = dmu_object_alloc(mos,
1999 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
2000 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
2001 ASSERT(smo->smo_object != 0);
2002 vdev_config_dirty(vd->vdev_top);
2003 }
2004
2005 bzero(&smlock, sizeof (smlock));
2006 mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
2007
2008 space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
2009 &smlock);
2010
2011 mutex_enter(&smlock);
2012
2013 mutex_enter(&vd->vdev_dtl_lock);
2014 space_map_walk(sm, space_map_add, &smsync);
2015 mutex_exit(&vd->vdev_dtl_lock);
2016
2017 space_map_truncate(smo, mos, tx);
2018 space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
2019 space_map_vacate(&smsync, NULL, NULL);
2020
2021 space_map_destroy(&smsync);
2022
2023 mutex_exit(&smlock);
2024 mutex_destroy(&smlock);
2025
2026 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
2027 dmu_buf_will_dirty(db, tx);
2028 ASSERT3U(db->db_size, >=, sizeof (*smo));
2029 bcopy(smo, db->db_data, sizeof (*smo));
2030 dmu_buf_rele(db, FTAG);
2031
2032 dmu_tx_commit(tx);
2033}
2034
2035/*
2036 * Determine whether the specified vdev can be offlined/detached/removed
2037 * without losing data.
2038 */
2039boolean_t
2040vdev_dtl_required(vdev_t *vd)
2041{
2042 spa_t *spa = vd->vdev_spa;
2043 vdev_t *tvd = vd->vdev_top;
2044 uint8_t cant_read = vd->vdev_cant_read;
2045 boolean_t required;
2046
2047 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2048
2049 if (vd == spa->spa_root_vdev || vd == tvd)
2050 return (B_TRUE);
2051
2052 /*
2053 * Temporarily mark the device as unreadable, and then determine
2054 * whether this results in any DTL outages in the top-level vdev.
2055 * If not, we can safely offline/detach/remove the device.
2056 */
2057 vd->vdev_cant_read = B_TRUE;
2058 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2059 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2060 vd->vdev_cant_read = cant_read;
2061 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2062
2063 if (!required && zio_injection_enabled)
2064 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2065
2066 return (required);
2067}
2068
2069/*
2070 * Determine if resilver is needed, and if so the txg range.
2071 */
2072boolean_t
2073vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2074{
2075 boolean_t needed = B_FALSE;
2076 uint64_t thismin = UINT64_MAX;
2077 uint64_t thismax = 0;
2078
2079 if (vd->vdev_children == 0) {
2080 mutex_enter(&vd->vdev_dtl_lock);
2081 if (vd->vdev_dtl[DTL_MISSING].sm_space != 0 &&
2082 vdev_writeable(vd)) {
2083
2084 thismin = vdev_dtl_min(vd);
2085 thismax = vdev_dtl_max(vd);
2086 needed = B_TRUE;
2087 }
2088 mutex_exit(&vd->vdev_dtl_lock);
2089 } else {
2090 for (int c = 0; c < vd->vdev_children; c++) {
2091 vdev_t *cvd = vd->vdev_child[c];
2092 uint64_t cmin, cmax;
2093
2094 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2095 thismin = MIN(thismin, cmin);
2096 thismax = MAX(thismax, cmax);
2097 needed = B_TRUE;
2098 }
2099 }
2100 }
2101
2102 if (needed && minp) {
2103 *minp = thismin;
2104 *maxp = thismax;
2105 }
2106 return (needed);
2107}
2108
2109void
2110vdev_load(vdev_t *vd)
2111{
2112 /*
2113 * Recursively load all children.
2114 */
2115 for (int c = 0; c < vd->vdev_children; c++)
2116 vdev_load(vd->vdev_child[c]);
2117
2118 /*
2119 * If this is a top-level vdev, initialize its metaslabs.
2120 */
2121 if (vd == vd->vdev_top && !vd->vdev_ishole &&
2122 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2123 vdev_metaslab_init(vd, 0) != 0))
2124 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2125 VDEV_AUX_CORRUPT_DATA);
2126
2127 /*
2128 * If this is a leaf vdev, load its DTL.
2129 */
2130 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2131 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2132 VDEV_AUX_CORRUPT_DATA);
2133}
2134
2135/*
2136 * The special vdev case is used for hot spares and l2cache devices. Its
2137 * sole purpose it to set the vdev state for the associated vdev. To do this,
2138 * we make sure that we can open the underlying device, then try to read the
2139 * label, and make sure that the label is sane and that it hasn't been
2140 * repurposed to another pool.
2141 */
2142int
2143vdev_validate_aux(vdev_t *vd)
2144{
2145 nvlist_t *label;
2146 uint64_t guid, version;
2147 uint64_t state;
2148
2149 if (!vdev_readable(vd))
2150 return (0);
2151
2152 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2153 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2154 VDEV_AUX_CORRUPT_DATA);
2155 return (-1);
2156 }
2157
2158 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2159 !SPA_VERSION_IS_SUPPORTED(version) ||
2160 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2161 guid != vd->vdev_guid ||
2162 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2163 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2164 VDEV_AUX_CORRUPT_DATA);
2165 nvlist_free(label);
2166 return (-1);
2167 }
2168
2169 /*
2170 * We don't actually check the pool state here. If it's in fact in
2171 * use by another pool, we update this fact on the fly when requested.
2172 */
2173 nvlist_free(label);
2174 return (0);
2175}
2176
2177void
2178vdev_remove(vdev_t *vd, uint64_t txg)
2179{
2180 spa_t *spa = vd->vdev_spa;
2181 objset_t *mos = spa->spa_meta_objset;
2182 dmu_tx_t *tx;
2183
2184 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2185
2186 if (vd->vdev_dtl_smo.smo_object) {
2187 ASSERT0(vd->vdev_dtl_smo.smo_alloc);
2188 (void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
2189 vd->vdev_dtl_smo.smo_object = 0;
2190 }
2191
2192 if (vd->vdev_ms != NULL) {
2193 for (int m = 0; m < vd->vdev_ms_count; m++) {
2194 metaslab_t *msp = vd->vdev_ms[m];
2195
2196 if (msp == NULL || msp->ms_smo.smo_object == 0)
2197 continue;
2198
2199 ASSERT0(msp->ms_smo.smo_alloc);
2200 (void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
2201 msp->ms_smo.smo_object = 0;
2202 }
2203 }
2204
2205 if (vd->vdev_ms_array) {
2206 (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
2207 vd->vdev_ms_array = 0;
2208 vd->vdev_ms_shift = 0;
2209 }
2210 dmu_tx_commit(tx);
2211}
2212
2213void
2214vdev_sync_done(vdev_t *vd, uint64_t txg)
2215{
2216 metaslab_t *msp;
2217 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2218
2219 ASSERT(!vd->vdev_ishole);
2220
2221 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2222 metaslab_sync_done(msp, txg);
2223
2224 if (reassess)
2225 metaslab_sync_reassess(vd->vdev_mg);
2226}
2227
2228void
2229vdev_sync(vdev_t *vd, uint64_t txg)
2230{
2231 spa_t *spa = vd->vdev_spa;
2232 vdev_t *lvd;
2233 metaslab_t *msp;
2234 dmu_tx_t *tx;
2235
2236 ASSERT(!vd->vdev_ishole);
2237
2238 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2239 ASSERT(vd == vd->vdev_top);
2240 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2241 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2242 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2243 ASSERT(vd->vdev_ms_array != 0);
2244 vdev_config_dirty(vd);
2245 dmu_tx_commit(tx);
2246 }
2247
2248 /*
2249 * Remove the metadata associated with this vdev once it's empty.
2250 */
2251 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
2252 vdev_remove(vd, txg);
2253
2254 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2255 metaslab_sync(msp, txg);
2256 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2257 }
2258
2259 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2260 vdev_dtl_sync(lvd, txg);
2261
2262 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2263}
2264
2265uint64_t
2266vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2267{
2268 return (vd->vdev_ops->vdev_op_asize(vd, psize));
2269}
2270
2271/*
2272 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2273 * not be opened, and no I/O is attempted.
2274 */
2275int
2276vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2277{
2278 vdev_t *vd, *tvd;
2279
2280 spa_vdev_state_enter(spa, SCL_NONE);
2281
2282 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2283 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2284
2285 if (!vd->vdev_ops->vdev_op_leaf)
2286 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2287
2288 tvd = vd->vdev_top;
2289
2290 /*
2291 * We don't directly use the aux state here, but if we do a
2292 * vdev_reopen(), we need this value to be present to remember why we
2293 * were faulted.
2294 */
2295 vd->vdev_label_aux = aux;
2296
2297 /*
2298 * Faulted state takes precedence over degraded.
2299 */
2300 vd->vdev_delayed_close = B_FALSE;
2301 vd->vdev_faulted = 1ULL;
2302 vd->vdev_degraded = 0ULL;
2303 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
2304
2305 /*
2306 * If this device has the only valid copy of the data, then
2307 * back off and simply mark the vdev as degraded instead.
2308 */
2309 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
2310 vd->vdev_degraded = 1ULL;
2311 vd->vdev_faulted = 0ULL;
2312
2313 /*
2314 * If we reopen the device and it's not dead, only then do we
2315 * mark it degraded.
2316 */
2317 vdev_reopen(tvd);
2318
2319 if (vdev_readable(vd))
2320 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
2321 }
2322
2323 return (spa_vdev_state_exit(spa, vd, 0));
2324}
2325
2326/*
2327 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2328 * user that something is wrong. The vdev continues to operate as normal as far
2329 * as I/O is concerned.
2330 */
2331int
2332vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2333{
2334 vdev_t *vd;
2335
2336 spa_vdev_state_enter(spa, SCL_NONE);
2337
2338 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2339 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2340
2341 if (!vd->vdev_ops->vdev_op_leaf)
2342 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2343
2344 /*
2345 * If the vdev is already faulted, then don't do anything.
2346 */
2347 if (vd->vdev_faulted || vd->vdev_degraded)
2348 return (spa_vdev_state_exit(spa, NULL, 0));
2349
2350 vd->vdev_degraded = 1ULL;
2351 if (!vdev_is_dead(vd))
2352 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2353 aux);
2354
2355 return (spa_vdev_state_exit(spa, vd, 0));
2356}
2357
2358/*
2359 * Online the given vdev.
2360 *
2361 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2362 * spare device should be detached when the device finishes resilvering.
2363 * Second, the online should be treated like a 'test' online case, so no FMA
2364 * events are generated if the device fails to open.
2365 */
2366int
2367vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2368{
2369 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2370
2371 spa_vdev_state_enter(spa, SCL_NONE);
2372
2373 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2374 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2375
2376 if (!vd->vdev_ops->vdev_op_leaf)
2377 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2378
2379 tvd = vd->vdev_top;
2380 vd->vdev_offline = B_FALSE;
2381 vd->vdev_tmpoffline = B_FALSE;
2382 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2383 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2384
2385 /* XXX - L2ARC 1.0 does not support expansion */
2386 if (!vd->vdev_aux) {
2387 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2388 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2389 }
2390
2391 vdev_reopen(tvd);
2392 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
2393
2394 if (!vd->vdev_aux) {
2395 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2396 pvd->vdev_expanding = B_FALSE;
2397 }
2398
2399 if (newstate)
2400 *newstate = vd->vdev_state;
2401 if ((flags & ZFS_ONLINE_UNSPARE) &&
2402 !vdev_is_dead(vd) && vd->vdev_parent &&
2403 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2404 vd->vdev_parent->vdev_child[0] == vd)
2405 vd->vdev_unspare = B_TRUE;
2406
2407 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2408
2409 /* XXX - L2ARC 1.0 does not support expansion */
2410 if (vd->vdev_aux)
2411 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2412 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2413 }
2414 return (spa_vdev_state_exit(spa, vd, 0));
2415}
2416
2417static int
2418vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2419{
2420 vdev_t *vd, *tvd;
2421 int error = 0;
2422 uint64_t generation;
2423 metaslab_group_t *mg;
2424
2425top:
2426 spa_vdev_state_enter(spa, SCL_ALLOC);
2427
2428 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2429 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2430
2431 if (!vd->vdev_ops->vdev_op_leaf)
2432 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2433
2434 tvd = vd->vdev_top;
2435 mg = tvd->vdev_mg;
2436 generation = spa->spa_config_generation + 1;
2437
2438 /*
2439 * If the device isn't already offline, try to offline it.
2440 */
2441 if (!vd->vdev_offline) {
2442 /*
2443 * If this device has the only valid copy of some data,
2444 * don't allow it to be offlined. Log devices are always
2445 * expendable.
2446 */
2447 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2448 vdev_dtl_required(vd))
2449 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2450
2451 /*
2452 * If the top-level is a slog and it has had allocations
2453 * then proceed. We check that the vdev's metaslab group
2454 * is not NULL since it's possible that we may have just
2455 * added this vdev but not yet initialized its metaslabs.
2456 */
2457 if (tvd->vdev_islog && mg != NULL) {
2458 /*
2459 * Prevent any future allocations.
2460 */
2461 metaslab_group_passivate(mg);
2462 (void) spa_vdev_state_exit(spa, vd, 0);
2463
2464 error = spa_offline_log(spa);
2465
2466 spa_vdev_state_enter(spa, SCL_ALLOC);
2467
2468 /*
2469 * Check to see if the config has changed.
2470 */
2471 if (error || generation != spa->spa_config_generation) {
2472 metaslab_group_activate(mg);
2473 if (error)
2474 return (spa_vdev_state_exit(spa,
2475 vd, error));
2476 (void) spa_vdev_state_exit(spa, vd, 0);
2477 goto top;
2478 }
2479 ASSERT0(tvd->vdev_stat.vs_alloc);
2480 }
2481
2482 /*
2483 * Offline this device and reopen its top-level vdev.
2484 * If the top-level vdev is a log device then just offline
2485 * it. Otherwise, if this action results in the top-level
2486 * vdev becoming unusable, undo it and fail the request.
2487 */
2488 vd->vdev_offline = B_TRUE;
2489 vdev_reopen(tvd);
2490
2491 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2492 vdev_is_dead(tvd)) {
2493 vd->vdev_offline = B_FALSE;
2494 vdev_reopen(tvd);
2495 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2496 }
2497
2498 /*
2499 * Add the device back into the metaslab rotor so that
2500 * once we online the device it's open for business.
2501 */
2502 if (tvd->vdev_islog && mg != NULL)
2503 metaslab_group_activate(mg);
2504 }
2505
2506 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2507
2508 return (spa_vdev_state_exit(spa, vd, 0));
2509}
2510
2511int
2512vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2513{
2514 int error;
2515
2516 mutex_enter(&spa->spa_vdev_top_lock);
2517 error = vdev_offline_locked(spa, guid, flags);
2518 mutex_exit(&spa->spa_vdev_top_lock);
2519
2520 return (error);
2521}
2522
2523/*
2524 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2525 * vdev_offline(), we assume the spa config is locked. We also clear all
2526 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2527 */
2528void
2529vdev_clear(spa_t *spa, vdev_t *vd)
2530{
2531 vdev_t *rvd = spa->spa_root_vdev;
2532
2533 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2534
2535 if (vd == NULL)
2536 vd = rvd;
2537
2538 vd->vdev_stat.vs_read_errors = 0;
2539 vd->vdev_stat.vs_write_errors = 0;
2540 vd->vdev_stat.vs_checksum_errors = 0;
2541
2542 for (int c = 0; c < vd->vdev_children; c++)
2543 vdev_clear(spa, vd->vdev_child[c]);
2544
2545 if (vd == rvd) {
2546 for (int c = 0; c < spa->spa_l2cache.sav_count; c++)
2547 vdev_clear(spa, spa->spa_l2cache.sav_vdevs[c]);
2548
2549 for (int c = 0; c < spa->spa_spares.sav_count; c++)
2550 vdev_clear(spa, spa->spa_spares.sav_vdevs[c]);
2551 }
2552
2553 /*
2554 * If we're in the FAULTED state or have experienced failed I/O, then
2555 * clear the persistent state and attempt to reopen the device. We
2556 * also mark the vdev config dirty, so that the new faulted state is
2557 * written out to disk.
2558 */
2559 if (vd->vdev_faulted || vd->vdev_degraded ||
2560 !vdev_readable(vd) || !vdev_writeable(vd)) {
2561
2562 /*
2563 * When reopening in reponse to a clear event, it may be due to
2564 * a fmadm repair request. In this case, if the device is
2565 * still broken, we want to still post the ereport again.
2566 */
2567 vd->vdev_forcefault = B_TRUE;
2568
2569 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2570 vd->vdev_cant_read = B_FALSE;
2571 vd->vdev_cant_write = B_FALSE;
2572
2573 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
2574
2575 vd->vdev_forcefault = B_FALSE;
2576
2577 if (vd != rvd && vdev_writeable(vd->vdev_top))
2578 vdev_state_dirty(vd->vdev_top);
2579
2580 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2581 spa_async_request(spa, SPA_ASYNC_RESILVER);
2582
2583 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
2584 }
2585
2586 /*
2587 * When clearing a FMA-diagnosed fault, we always want to
2588 * unspare the device, as we assume that the original spare was
2589 * done in response to the FMA fault.
2590 */
2591 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2592 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2593 vd->vdev_parent->vdev_child[0] == vd)
2594 vd->vdev_unspare = B_TRUE;
2595}
2596
2597boolean_t
2598vdev_is_dead(vdev_t *vd)
2599{
2600 /*
2601 * Holes and missing devices are always considered "dead".
2602 * This simplifies the code since we don't have to check for
2603 * these types of devices in the various code paths.
2604 * Instead we rely on the fact that we skip over dead devices
2605 * before issuing I/O to them.
2606 */
2607 return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
2608 vd->vdev_ops == &vdev_missing_ops);
2609}
2610
2611boolean_t
2612vdev_readable(vdev_t *vd)
2613{
2614 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2615}
2616
2617boolean_t
2618vdev_writeable(vdev_t *vd)
2619{
2620 return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2621}
2622
2623boolean_t
2624vdev_allocatable(vdev_t *vd)
2625{
2626 uint64_t state = vd->vdev_state;
2627
2628 /*
2629 * We currently allow allocations from vdevs which may be in the
2630 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2631 * fails to reopen then we'll catch it later when we're holding
2632 * the proper locks. Note that we have to get the vdev state
2633 * in a local variable because although it changes atomically,
2634 * we're asking two separate questions about it.
2635 */
2636 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
2637 !vd->vdev_cant_write && !vd->vdev_ishole);
2638}
2639
2640boolean_t
2641vdev_accessible(vdev_t *vd, zio_t *zio)
2642{
2643 ASSERT(zio->io_vd == vd);
2644
2645 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2646 return (B_FALSE);
2647
2648 if (zio->io_type == ZIO_TYPE_READ)
2649 return (!vd->vdev_cant_read);
2650
2651 if (zio->io_type == ZIO_TYPE_WRITE)
2652 return (!vd->vdev_cant_write);
2653
2654 return (B_TRUE);
2655}
2656
2657/*
2658 * Get statistics for the given vdev.
2659 */
2660void
2661vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2662{
2663 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2664
2665 mutex_enter(&vd->vdev_stat_lock);
2666 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2667 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2668 vs->vs_state = vd->vdev_state;
2669 vs->vs_rsize = vdev_get_min_asize(vd);
2670 if (vd->vdev_ops->vdev_op_leaf)
2671 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2672 vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize;
2673 vs->vs_configured_ashift = vd->vdev_top != NULL
2674 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
2675 vs->vs_logical_ashift = vd->vdev_logical_ashift;
2676 vs->vs_physical_ashift = vd->vdev_physical_ashift;
2677 mutex_exit(&vd->vdev_stat_lock);
2678
2679 /*
2680 * If we're getting stats on the root vdev, aggregate the I/O counts
2681 * over all top-level vdevs (i.e. the direct children of the root).
2682 */
2683 if (vd == rvd) {
2684 for (int c = 0; c < rvd->vdev_children; c++) {
2685 vdev_t *cvd = rvd->vdev_child[c];
2686 vdev_stat_t *cvs = &cvd->vdev_stat;
2687
2688 mutex_enter(&vd->vdev_stat_lock);
2689 for (int t = 0; t < ZIO_TYPES; t++) {
2690 vs->vs_ops[t] += cvs->vs_ops[t];
2691 vs->vs_bytes[t] += cvs->vs_bytes[t];
2692 }
2693 cvs->vs_scan_removing = cvd->vdev_removing;
2694 mutex_exit(&vd->vdev_stat_lock);
2695 }
2696 }
2697}
2698
2699void
2700vdev_clear_stats(vdev_t *vd)
2701{
2702 mutex_enter(&vd->vdev_stat_lock);
2703 vd->vdev_stat.vs_space = 0;
2704 vd->vdev_stat.vs_dspace = 0;
2705 vd->vdev_stat.vs_alloc = 0;
2706 mutex_exit(&vd->vdev_stat_lock);
2707}
2708
2709void
2710vdev_scan_stat_init(vdev_t *vd)
2711{
2712 vdev_stat_t *vs = &vd->vdev_stat;
2713
2714 for (int c = 0; c < vd->vdev_children; c++)
2715 vdev_scan_stat_init(vd->vdev_child[c]);
2716
2717 mutex_enter(&vd->vdev_stat_lock);
2718 vs->vs_scan_processed = 0;
2719 mutex_exit(&vd->vdev_stat_lock);
2720}
2721
2722void
2723vdev_stat_update(zio_t *zio, uint64_t psize)
2724{
2725 spa_t *spa = zio->io_spa;
2726 vdev_t *rvd = spa->spa_root_vdev;
2727 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2728 vdev_t *pvd;
2729 uint64_t txg = zio->io_txg;
2730 vdev_stat_t *vs = &vd->vdev_stat;
2731 zio_type_t type = zio->io_type;
2732 int flags = zio->io_flags;
2733
2734 /*
2735 * If this i/o is a gang leader, it didn't do any actual work.
2736 */
2737 if (zio->io_gang_tree)
2738 return;
2739
2740 if (zio->io_error == 0) {
2741 /*
2742 * If this is a root i/o, don't count it -- we've already
2743 * counted the top-level vdevs, and vdev_get_stats() will
2744 * aggregate them when asked. This reduces contention on
2745 * the root vdev_stat_lock and implicitly handles blocks
2746 * that compress away to holes, for which there is no i/o.
2747 * (Holes never create vdev children, so all the counters
2748 * remain zero, which is what we want.)
2749 *
2750 * Note: this only applies to successful i/o (io_error == 0)
2751 * because unlike i/o counts, errors are not additive.
2752 * When reading a ditto block, for example, failure of
2753 * one top-level vdev does not imply a root-level error.
2754 */
2755 if (vd == rvd)
2756 return;
2757
2758 ASSERT(vd == zio->io_vd);
2759
2760 if (flags & ZIO_FLAG_IO_BYPASS)
2761 return;
2762
2763 mutex_enter(&vd->vdev_stat_lock);
2764
2765 if (flags & ZIO_FLAG_IO_REPAIR) {
2766 if (flags & ZIO_FLAG_SCAN_THREAD) {
2767 dsl_scan_phys_t *scn_phys =
2768 &spa->spa_dsl_pool->dp_scan->scn_phys;
2769 uint64_t *processed = &scn_phys->scn_processed;
2770
2771 /* XXX cleanup? */
2772 if (vd->vdev_ops->vdev_op_leaf)
2773 atomic_add_64(processed, psize);
2774 vs->vs_scan_processed += psize;
2775 }
2776
2777 if (flags & ZIO_FLAG_SELF_HEAL)
2778 vs->vs_self_healed += psize;
2779 }
2780
2781 vs->vs_ops[type]++;
2782 vs->vs_bytes[type] += psize;
2783
2784 mutex_exit(&vd->vdev_stat_lock);
2785 return;
2786 }
2787
2788 if (flags & ZIO_FLAG_SPECULATIVE)
2789 return;
2790
2791 /*
2792 * If this is an I/O error that is going to be retried, then ignore the
2793 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
2794 * hard errors, when in reality they can happen for any number of
2795 * innocuous reasons (bus resets, MPxIO link failure, etc).
2796 */
2797 if (zio->io_error == EIO &&
2798 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
2799 return;
2800
2801 /*
2802 * Intent logs writes won't propagate their error to the root
2803 * I/O so don't mark these types of failures as pool-level
2804 * errors.
2805 */
2806 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
2807 return;
2808
2809 mutex_enter(&vd->vdev_stat_lock);
2810 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2811 if (zio->io_error == ECKSUM)
2812 vs->vs_checksum_errors++;
2813 else
2814 vs->vs_read_errors++;
2815 }
2816 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2817 vs->vs_write_errors++;
2818 mutex_exit(&vd->vdev_stat_lock);
2819
2820 if (type == ZIO_TYPE_WRITE && txg != 0 &&
2821 (!(flags & ZIO_FLAG_IO_REPAIR) ||
2822 (flags & ZIO_FLAG_SCAN_THREAD) ||
2823 spa->spa_claiming)) {
2824 /*
2825 * This is either a normal write (not a repair), or it's
2826 * a repair induced by the scrub thread, or it's a repair
2827 * made by zil_claim() during spa_load() in the first txg.
2828 * In the normal case, we commit the DTL change in the same
2829 * txg as the block was born. In the scrub-induced repair
2830 * case, we know that scrubs run in first-pass syncing context,
2831 * so we commit the DTL change in spa_syncing_txg(spa).
2832 * In the zil_claim() case, we commit in spa_first_txg(spa).
2833 *
2834 * We currently do not make DTL entries for failed spontaneous
2835 * self-healing writes triggered by normal (non-scrubbing)
2836 * reads, because we have no transactional context in which to
2837 * do so -- and it's not clear that it'd be desirable anyway.
2838 */
2839 if (vd->vdev_ops->vdev_op_leaf) {
2840 uint64_t commit_txg = txg;
2841 if (flags & ZIO_FLAG_SCAN_THREAD) {
2842 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2843 ASSERT(spa_sync_pass(spa) == 1);
2844 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2845 commit_txg = spa_syncing_txg(spa);
2846 } else if (spa->spa_claiming) {
2847 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2848 commit_txg = spa_first_txg(spa);
2849 }
2850 ASSERT(commit_txg >= spa_syncing_txg(spa));
2851 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2852 return;
2853 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2854 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
2855 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2856 }
2857 if (vd != rvd)
2858 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2859 }
2860}
2861
2862/*
2863 * Update the in-core space usage stats for this vdev, its metaslab class,
2864 * and the root vdev.
2865 */
2866void
2867vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2868 int64_t space_delta)
2869{
2870 int64_t dspace_delta = space_delta;
2871 spa_t *spa = vd->vdev_spa;
2872 vdev_t *rvd = spa->spa_root_vdev;
2873 metaslab_group_t *mg = vd->vdev_mg;
2874 metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2875
2876 ASSERT(vd == vd->vdev_top);
2877
2878 /*
2879 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2880 * factor. We must calculate this here and not at the root vdev
2881 * because the root vdev's psize-to-asize is simply the max of its
2882 * childrens', thus not accurate enough for us.
2883 */
2884 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2885 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
2886 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2887 vd->vdev_deflate_ratio;
2888
2889 mutex_enter(&vd->vdev_stat_lock);
2890 vd->vdev_stat.vs_alloc += alloc_delta;
2891 vd->vdev_stat.vs_space += space_delta;
2892 vd->vdev_stat.vs_dspace += dspace_delta;
2893 mutex_exit(&vd->vdev_stat_lock);
2894
2895 if (mc == spa_normal_class(spa)) {
2896 mutex_enter(&rvd->vdev_stat_lock);
2897 rvd->vdev_stat.vs_alloc += alloc_delta;
2898 rvd->vdev_stat.vs_space += space_delta;
2899 rvd->vdev_stat.vs_dspace += dspace_delta;
2900 mutex_exit(&rvd->vdev_stat_lock);
2901 }
2902
2903 if (mc != NULL) {
2904 ASSERT(rvd == vd->vdev_parent);
2905 ASSERT(vd->vdev_ms_count != 0);
2906
2907 metaslab_class_space_update(mc,
2908 alloc_delta, defer_delta, space_delta, dspace_delta);
2909 }
2910}
2911
2912/*
2913 * Mark a top-level vdev's config as dirty, placing it on the dirty list
2914 * so that it will be written out next time the vdev configuration is synced.
2915 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2916 */
2917void
2918vdev_config_dirty(vdev_t *vd)
2919{
2920 spa_t *spa = vd->vdev_spa;
2921 vdev_t *rvd = spa->spa_root_vdev;
2922 int c;
2923
2924 ASSERT(spa_writeable(spa));
2925
2926 /*
2927 * If this is an aux vdev (as with l2cache and spare devices), then we
2928 * update the vdev config manually and set the sync flag.
2929 */
2930 if (vd->vdev_aux != NULL) {
2931 spa_aux_vdev_t *sav = vd->vdev_aux;
2932 nvlist_t **aux;
2933 uint_t naux;
2934
2935 for (c = 0; c < sav->sav_count; c++) {
2936 if (sav->sav_vdevs[c] == vd)
2937 break;
2938 }
2939
2940 if (c == sav->sav_count) {
2941 /*
2942 * We're being removed. There's nothing more to do.
2943 */
2944 ASSERT(sav->sav_sync == B_TRUE);
2945 return;
2946 }
2947
2948 sav->sav_sync = B_TRUE;
2949
2950 if (nvlist_lookup_nvlist_array(sav->sav_config,
2951 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
2952 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
2953 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
2954 }
2955
2956 ASSERT(c < naux);
2957
2958 /*
2959 * Setting the nvlist in the middle if the array is a little
2960 * sketchy, but it will work.
2961 */
2962 nvlist_free(aux[c]);
2963 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
2964
2965 return;
2966 }
2967
2968 /*
2969 * The dirty list is protected by the SCL_CONFIG lock. The caller
2970 * must either hold SCL_CONFIG as writer, or must be the sync thread
2971 * (which holds SCL_CONFIG as reader). There's only one sync thread,
2972 * so this is sufficient to ensure mutual exclusion.
2973 */
2974 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2975 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2976 spa_config_held(spa, SCL_CONFIG, RW_READER)));
2977
2978 if (vd == rvd) {
2979 for (c = 0; c < rvd->vdev_children; c++)
2980 vdev_config_dirty(rvd->vdev_child[c]);
2981 } else {
2982 ASSERT(vd == vd->vdev_top);
2983
2984 if (!list_link_active(&vd->vdev_config_dirty_node) &&
2985 !vd->vdev_ishole)
2986 list_insert_head(&spa->spa_config_dirty_list, vd);
2987 }
2988}
2989
2990void
2991vdev_config_clean(vdev_t *vd)
2992{
2993 spa_t *spa = vd->vdev_spa;
2994
2995 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2996 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2997 spa_config_held(spa, SCL_CONFIG, RW_READER)));
2998
2999 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3000 list_remove(&spa->spa_config_dirty_list, vd);
3001}
3002
3003/*
3004 * Mark a top-level vdev's state as dirty, so that the next pass of
3005 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3006 * the state changes from larger config changes because they require
3007 * much less locking, and are often needed for administrative actions.
3008 */
3009void
3010vdev_state_dirty(vdev_t *vd)
3011{
3012 spa_t *spa = vd->vdev_spa;
3013
3014 ASSERT(spa_writeable(spa));
3015 ASSERT(vd == vd->vdev_top);
3016
3017 /*
3018 * The state list is protected by the SCL_STATE lock. The caller
3019 * must either hold SCL_STATE as writer, or must be the sync thread
3020 * (which holds SCL_STATE as reader). There's only one sync thread,
3021 * so this is sufficient to ensure mutual exclusion.
3022 */
3023 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3024 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3025 spa_config_held(spa, SCL_STATE, RW_READER)));
3026
3027 if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
3028 list_insert_head(&spa->spa_state_dirty_list, vd);
3029}
3030
3031void
3032vdev_state_clean(vdev_t *vd)
3033{
3034 spa_t *spa = vd->vdev_spa;
3035
3036 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3037 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3038 spa_config_held(spa, SCL_STATE, RW_READER)));
3039
3040 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3041 list_remove(&spa->spa_state_dirty_list, vd);
3042}
3043
3044/*
3045 * Propagate vdev state up from children to parent.
3046 */
3047void
3048vdev_propagate_state(vdev_t *vd)
3049{
3050 spa_t *spa = vd->vdev_spa;
3051 vdev_t *rvd = spa->spa_root_vdev;
3052 int degraded = 0, faulted = 0;
3053 int corrupted = 0;
3054 vdev_t *child;
3055
3056 if (vd->vdev_children > 0) {
3057 for (int c = 0; c < vd->vdev_children; c++) {
3058 child = vd->vdev_child[c];
3059
3060 /*
3061 * Don't factor holes into the decision.
3062 */
3063 if (child->vdev_ishole)
3064 continue;
3065
3066 if (!vdev_readable(child) ||
3067 (!vdev_writeable(child) && spa_writeable(spa))) {
3068 /*
3069 * Root special: if there is a top-level log
3070 * device, treat the root vdev as if it were
3071 * degraded.
3072 */
3073 if (child->vdev_islog && vd == rvd)
3074 degraded++;
3075 else
3076 faulted++;
3077 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3078 degraded++;
3079 }
3080
3081 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3082 corrupted++;
3083 }
3084
3085 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3086
3087 /*
3088 * Root special: if there is a top-level vdev that cannot be
3089 * opened due to corrupted metadata, then propagate the root
3090 * vdev's aux state as 'corrupt' rather than 'insufficient
3091 * replicas'.
3092 */
3093 if (corrupted && vd == rvd &&
3094 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3095 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3096 VDEV_AUX_CORRUPT_DATA);
3097 }
3098
3099 if (vd->vdev_parent)
3100 vdev_propagate_state(vd->vdev_parent);
3101}
3102
3103/*
3104 * Set a vdev's state. If this is during an open, we don't update the parent
3105 * state, because we're in the process of opening children depth-first.
3106 * Otherwise, we propagate the change to the parent.
3107 *
3108 * If this routine places a device in a faulted state, an appropriate ereport is
3109 * generated.
3110 */
3111void
3112vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3113{
3114 uint64_t save_state;
3115 spa_t *spa = vd->vdev_spa;
3116
3117 if (state == vd->vdev_state) {
3118 vd->vdev_stat.vs_aux = aux;
3119 return;
3120 }
3121
3122 save_state = vd->vdev_state;
3123
3124 vd->vdev_state = state;
3125 vd->vdev_stat.vs_aux = aux;
3126
3127 /*
3128 * If we are setting the vdev state to anything but an open state, then
3129 * always close the underlying device unless the device has requested
3130 * a delayed close (i.e. we're about to remove or fault the device).
3131 * Otherwise, we keep accessible but invalid devices open forever.
3132 * We don't call vdev_close() itself, because that implies some extra
3133 * checks (offline, etc) that we don't want here. This is limited to
3134 * leaf devices, because otherwise closing the device will affect other
3135 * children.
3136 */
3137 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3138 vd->vdev_ops->vdev_op_leaf)
3139 vd->vdev_ops->vdev_op_close(vd);
3140
3141 /*
3142 * If we have brought this vdev back into service, we need
3143 * to notify fmd so that it can gracefully repair any outstanding
3144 * cases due to a missing device. We do this in all cases, even those
3145 * that probably don't correlate to a repaired fault. This is sure to
3146 * catch all cases, and we let the zfs-retire agent sort it out. If
3147 * this is a transient state it's OK, as the retire agent will
3148 * double-check the state of the vdev before repairing it.
3149 */
3150 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3151 vd->vdev_prevstate != state)
3152 zfs_post_state_change(spa, vd);
3153
3154 if (vd->vdev_removed &&
3155 state == VDEV_STATE_CANT_OPEN &&
3156 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3157 /*
3158 * If the previous state is set to VDEV_STATE_REMOVED, then this
3159 * device was previously marked removed and someone attempted to
3160 * reopen it. If this failed due to a nonexistent device, then
3161 * keep the device in the REMOVED state. We also let this be if
3162 * it is one of our special test online cases, which is only
3163 * attempting to online the device and shouldn't generate an FMA
3164 * fault.
3165 */
3166 vd->vdev_state = VDEV_STATE_REMOVED;
3167 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
3168 } else if (state == VDEV_STATE_REMOVED) {
3169 vd->vdev_removed = B_TRUE;
3170 } else if (state == VDEV_STATE_CANT_OPEN) {
3171 /*
3172 * If we fail to open a vdev during an import or recovery, we
3173 * mark it as "not available", which signifies that it was
3174 * never there to begin with. Failure to open such a device
3175 * is not considered an error.
3176 */
3177 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3178 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3179 vd->vdev_ops->vdev_op_leaf)
3180 vd->vdev_not_present = 1;
3181
3182 /*
3183 * Post the appropriate ereport. If the 'prevstate' field is
3184 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3185 * that this is part of a vdev_reopen(). In this case, we don't
3186 * want to post the ereport if the device was already in the
3187 * CANT_OPEN state beforehand.
3188 *
3189 * If the 'checkremove' flag is set, then this is an attempt to
3190 * online the device in response to an insertion event. If we
3191 * hit this case, then we have detected an insertion event for a
3192 * faulted or offline device that wasn't in the removed state.
3193 * In this scenario, we don't post an ereport because we are
3194 * about to replace the device, or attempt an online with
3195 * vdev_forcefault, which will generate the fault for us.
3196 */
3197 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
3198 !vd->vdev_not_present && !vd->vdev_checkremove &&
3199 vd != spa->spa_root_vdev) {
3200 const char *class;
3201
3202 switch (aux) {
3203 case VDEV_AUX_OPEN_FAILED:
3204 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3205 break;
3206 case VDEV_AUX_CORRUPT_DATA:
3207 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3208 break;
3209 case VDEV_AUX_NO_REPLICAS:
3210 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3211 break;
3212 case VDEV_AUX_BAD_GUID_SUM:
3213 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3214 break;
3215 case VDEV_AUX_TOO_SMALL:
3216 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3217 break;
3218 case VDEV_AUX_BAD_LABEL:
3219 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3220 break;
3221 default:
3222 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3223 }
3224
3225 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3226 }
3227
3228 /* Erase any notion of persistent removed state */
3229 vd->vdev_removed = B_FALSE;
3230 } else {
3231 vd->vdev_removed = B_FALSE;
3232 }
3233
3234 if (!isopen && vd->vdev_parent)
3235 vdev_propagate_state(vd->vdev_parent);
3236}
3237
3238/*
3239 * Check the vdev configuration to ensure that it's capable of supporting
3240 * a root pool.
3241 *
3242 * On Solaris, we do not support RAID-Z or partial configuration. In
3243 * addition, only a single top-level vdev is allowed and none of the
3244 * leaves can be wholedisks.
3245 *
3246 * For FreeBSD, we can boot from any configuration. There is a
3247 * limitation that the boot filesystem must be either uncompressed or
3248 * compresses with lzjb compression but I'm not sure how to enforce
3249 * that here.
3250 */
3251boolean_t
3252vdev_is_bootable(vdev_t *vd)
3253{
3254#ifdef sun
3255 if (!vd->vdev_ops->vdev_op_leaf) {
3256 char *vdev_type = vd->vdev_ops->vdev_op_type;
3257
3258 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
3259 vd->vdev_children > 1) {
3260 return (B_FALSE);
3261 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
3262 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
3263 return (B_FALSE);
3264 }
3265 } else if (vd->vdev_wholedisk == 1) {
3266 return (B_FALSE);
3267 }
3268
3269 for (int c = 0; c < vd->vdev_children; c++) {
3270 if (!vdev_is_bootable(vd->vdev_child[c]))
3271 return (B_FALSE);
3272 }
3273#endif /* sun */
3274 return (B_TRUE);
3275}
3276
3277/*
3278 * Load the state from the original vdev tree (ovd) which
3279 * we've retrieved from the MOS config object. If the original
3280 * vdev was offline or faulted then we transfer that state to the
3281 * device in the current vdev tree (nvd).
3282 */
3283void
3284vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3285{
3286 spa_t *spa = nvd->vdev_spa;
3287
3288 ASSERT(nvd->vdev_top->vdev_islog);
3289 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3290 ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3291
3292 for (int c = 0; c < nvd->vdev_children; c++)
3293 vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3294
3295 if (nvd->vdev_ops->vdev_op_leaf) {
3296 /*
3297 * Restore the persistent vdev state
3298 */
3299 nvd->vdev_offline = ovd->vdev_offline;
3300 nvd->vdev_faulted = ovd->vdev_faulted;
3301 nvd->vdev_degraded = ovd->vdev_degraded;
3302 nvd->vdev_removed = ovd->vdev_removed;
3303 }
3304}
3305
3306/*
3307 * Determine if a log device has valid content. If the vdev was
3308 * removed or faulted in the MOS config then we know that
3309 * the content on the log device has already been written to the pool.
3310 */
3311boolean_t
3312vdev_log_state_valid(vdev_t *vd)
3313{
3314 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
3315 !vd->vdev_removed)
3316 return (B_TRUE);
3317
3318 for (int c = 0; c < vd->vdev_children; c++)
3319 if (vdev_log_state_valid(vd->vdev_child[c]))
3320 return (B_TRUE);
3321
3322 return (B_FALSE);
3323}
3324
3325/*
3326 * Expand a vdev if possible.
3327 */
3328void
3329vdev_expand(vdev_t *vd, uint64_t txg)
3330{
3331 ASSERT(vd->vdev_top == vd);
3332 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3333
3334 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3335 VERIFY(vdev_metaslab_init(vd, txg) == 0);
3336 vdev_config_dirty(vd);
3337 }
3338}
3339
3340/*
3341 * Split a vdev.
3342 */
3343void
3344vdev_split(vdev_t *vd)
3345{
3346 vdev_t *cvd, *pvd = vd->vdev_parent;
3347
3348 vdev_remove_child(pvd, vd);
3349 vdev_compact_children(pvd);
3350
3351 cvd = pvd->vdev_child[0];
3352 if (pvd->vdev_children == 1) {
3353 vdev_remove_parent(cvd);
3354 cvd->vdev_splitting = B_TRUE;
3355 }
3356 vdev_propagate_state(cvd);
3357}
3358
3359void
3360vdev_deadman(vdev_t *vd)
3361{
3362 for (int c = 0; c < vd->vdev_children; c++) {
3363 vdev_t *cvd = vd->vdev_child[c];
3364
3365 vdev_deadman(cvd);
3366 }
3367
3368 if (vd->vdev_ops->vdev_op_leaf) {
3369 vdev_queue_t *vq = &vd->vdev_queue;
3370
3371 mutex_enter(&vq->vq_lock);
3372 if (avl_numnodes(&vq->vq_pending_tree) > 0) {
3373 spa_t *spa = vd->vdev_spa;
3374 zio_t *fio;
3375 uint64_t delta;
3376
3377 /*
3378 * Look at the head of all the pending queues,
3379 * if any I/O has been outstanding for longer than
3380 * the spa_deadman_synctime we panic the system.
3381 */
3382 fio = avl_first(&vq->vq_pending_tree);
3383 delta = gethrtime() - fio->io_timestamp;
3384 if (delta > spa_deadman_synctime(spa)) {
3385 zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3386 "delta %lluns, last io %lluns",
3387 fio->io_timestamp, delta,
3388 vq->vq_io_complete_ts);
3389 fm_panic("I/O to pool '%s' appears to be "
3390 "hung on vdev guid %llu at '%s'.",
3391 spa_name(spa),
3392 (long long unsigned int) vd->vdev_guid,
3393 vd->vdev_path);
3394 }
3395 }
3396 mutex_exit(&vq->vq_lock);
3397 }
3398}