xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev.c (revision 560e6e964ab70b838deafc93f6d9da97f690ec25)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/fm/fs/zfs.h>
30 #include <sys/spa.h>
31 #include <sys/spa_impl.h>
32 #include <sys/dmu.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/vdev_impl.h>
35 #include <sys/uberblock_impl.h>
36 #include <sys/metaslab.h>
37 #include <sys/metaslab_impl.h>
38 #include <sys/space_map.h>
39 #include <sys/zio.h>
40 #include <sys/zap.h>
41 #include <sys/fs/zfs.h>
42 
43 /*
44  * Virtual device management.
45  */
46 
47 static vdev_ops_t *vdev_ops_table[] = {
48 	&vdev_root_ops,
49 	&vdev_raidz_ops,
50 	&vdev_mirror_ops,
51 	&vdev_replacing_ops,
52 	&vdev_disk_ops,
53 	&vdev_file_ops,
54 	&vdev_missing_ops,
55 	NULL
56 };
57 
58 /*
59  * Given a vdev type, return the appropriate ops vector.
60  */
61 static vdev_ops_t *
62 vdev_getops(const char *type)
63 {
64 	vdev_ops_t *ops, **opspp;
65 
66 	for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
67 		if (strcmp(ops->vdev_op_type, type) == 0)
68 			break;
69 
70 	return (ops);
71 }
72 
73 /*
74  * Default asize function: return the MAX of psize with the asize of
75  * all children.  This is what's used by anything other than RAID-Z.
76  */
77 uint64_t
78 vdev_default_asize(vdev_t *vd, uint64_t psize)
79 {
80 	uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
81 	uint64_t csize;
82 	uint64_t c;
83 
84 	for (c = 0; c < vd->vdev_children; c++) {
85 		csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
86 		asize = MAX(asize, csize);
87 	}
88 
89 	return (asize);
90 }
91 
92 /*
93  * Get the replaceable or attachable device size.
94  * If the parent is a mirror or raidz, the replaceable size is the minimum
95  * psize of all its children. For the rest, just return our own psize.
96  *
97  * e.g.
98  *			psize	rsize
99  * root			-	-
100  *	mirror/raidz	-	-
101  *	    disk1	20g	20g
102  *	    disk2 	40g	20g
103  *	disk3 		80g	80g
104  */
105 uint64_t
106 vdev_get_rsize(vdev_t *vd)
107 {
108 	vdev_t *pvd, *cvd;
109 	uint64_t c, rsize;
110 
111 	pvd = vd->vdev_parent;
112 
113 	/*
114 	 * If our parent is NULL or the root, just return our own psize.
115 	 */
116 	if (pvd == NULL || pvd->vdev_parent == NULL)
117 		return (vd->vdev_psize);
118 
119 	rsize = 0;
120 
121 	for (c = 0; c < pvd->vdev_children; c++) {
122 		cvd = pvd->vdev_child[c];
123 		rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
124 	}
125 
126 	return (rsize);
127 }
128 
129 vdev_t *
130 vdev_lookup_top(spa_t *spa, uint64_t vdev)
131 {
132 	vdev_t *rvd = spa->spa_root_vdev;
133 
134 	if (vdev < rvd->vdev_children)
135 		return (rvd->vdev_child[vdev]);
136 
137 	return (NULL);
138 }
139 
140 vdev_t *
141 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
142 {
143 	int c;
144 	vdev_t *mvd;
145 
146 	if (vd->vdev_guid == guid)
147 		return (vd);
148 
149 	for (c = 0; c < vd->vdev_children; c++)
150 		if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
151 		    NULL)
152 			return (mvd);
153 
154 	return (NULL);
155 }
156 
157 void
158 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
159 {
160 	size_t oldsize, newsize;
161 	uint64_t id = cvd->vdev_id;
162 	vdev_t **newchild;
163 
164 	ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
165 	ASSERT(cvd->vdev_parent == NULL);
166 
167 	cvd->vdev_parent = pvd;
168 
169 	if (pvd == NULL)
170 		return;
171 
172 	ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
173 
174 	oldsize = pvd->vdev_children * sizeof (vdev_t *);
175 	pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
176 	newsize = pvd->vdev_children * sizeof (vdev_t *);
177 
178 	newchild = kmem_zalloc(newsize, KM_SLEEP);
179 	if (pvd->vdev_child != NULL) {
180 		bcopy(pvd->vdev_child, newchild, oldsize);
181 		kmem_free(pvd->vdev_child, oldsize);
182 	}
183 
184 	pvd->vdev_child = newchild;
185 	pvd->vdev_child[id] = cvd;
186 
187 	cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
188 	ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
189 
190 	/*
191 	 * Walk up all ancestors to update guid sum.
192 	 */
193 	for (; pvd != NULL; pvd = pvd->vdev_parent)
194 		pvd->vdev_guid_sum += cvd->vdev_guid_sum;
195 }
196 
197 void
198 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
199 {
200 	int c;
201 	uint_t id = cvd->vdev_id;
202 
203 	ASSERT(cvd->vdev_parent == pvd);
204 
205 	if (pvd == NULL)
206 		return;
207 
208 	ASSERT(id < pvd->vdev_children);
209 	ASSERT(pvd->vdev_child[id] == cvd);
210 
211 	pvd->vdev_child[id] = NULL;
212 	cvd->vdev_parent = NULL;
213 
214 	for (c = 0; c < pvd->vdev_children; c++)
215 		if (pvd->vdev_child[c])
216 			break;
217 
218 	if (c == pvd->vdev_children) {
219 		kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
220 		pvd->vdev_child = NULL;
221 		pvd->vdev_children = 0;
222 	}
223 
224 	/*
225 	 * Walk up all ancestors to update guid sum.
226 	 */
227 	for (; pvd != NULL; pvd = pvd->vdev_parent)
228 		pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
229 }
230 
231 /*
232  * Remove any holes in the child array.
233  */
234 void
235 vdev_compact_children(vdev_t *pvd)
236 {
237 	vdev_t **newchild, *cvd;
238 	int oldc = pvd->vdev_children;
239 	int newc, c;
240 
241 	ASSERT(spa_config_held(pvd->vdev_spa, RW_WRITER));
242 
243 	for (c = newc = 0; c < oldc; c++)
244 		if (pvd->vdev_child[c])
245 			newc++;
246 
247 	newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
248 
249 	for (c = newc = 0; c < oldc; c++) {
250 		if ((cvd = pvd->vdev_child[c]) != NULL) {
251 			newchild[newc] = cvd;
252 			cvd->vdev_id = newc++;
253 		}
254 	}
255 
256 	kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
257 	pvd->vdev_child = newchild;
258 	pvd->vdev_children = newc;
259 }
260 
261 /*
262  * Allocate and minimally initialize a vdev_t.
263  */
264 static vdev_t *
265 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
266 {
267 	vdev_t *vd;
268 
269 	vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
270 
271 	if (spa->spa_root_vdev == NULL) {
272 		ASSERT(ops == &vdev_root_ops);
273 		spa->spa_root_vdev = vd;
274 	}
275 
276 	if (guid == 0) {
277 		if (spa->spa_root_vdev == vd) {
278 			/*
279 			 * The root vdev's guid will also be the pool guid,
280 			 * which must be unique among all pools.
281 			 */
282 			while (guid == 0 || spa_guid_exists(guid, 0))
283 				guid = spa_get_random(-1ULL);
284 		} else {
285 			/*
286 			 * Any other vdev's guid must be unique within the pool.
287 			 */
288 			while (guid == 0 ||
289 			    spa_guid_exists(spa_guid(spa), guid))
290 				guid = spa_get_random(-1ULL);
291 		}
292 		ASSERT(!spa_guid_exists(spa_guid(spa), guid));
293 	}
294 
295 	vd->vdev_spa = spa;
296 	vd->vdev_id = id;
297 	vd->vdev_guid = guid;
298 	vd->vdev_guid_sum = guid;
299 	vd->vdev_ops = ops;
300 	vd->vdev_state = VDEV_STATE_CLOSED;
301 
302 	mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
303 	space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
304 	space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
305 	txg_list_create(&vd->vdev_ms_list,
306 	    offsetof(struct metaslab, ms_txg_node));
307 	txg_list_create(&vd->vdev_dtl_list,
308 	    offsetof(struct vdev, vdev_dtl_node));
309 	vd->vdev_stat.vs_timestamp = gethrtime();
310 
311 	return (vd);
312 }
313 
314 /*
315  * Free a vdev_t that has been removed from service.
316  */
317 static void
318 vdev_free_common(vdev_t *vd)
319 {
320 	spa_t *spa = vd->vdev_spa;
321 
322 	if (vd->vdev_path)
323 		spa_strfree(vd->vdev_path);
324 	if (vd->vdev_devid)
325 		spa_strfree(vd->vdev_devid);
326 
327 	txg_list_destroy(&vd->vdev_ms_list);
328 	txg_list_destroy(&vd->vdev_dtl_list);
329 	mutex_enter(&vd->vdev_dtl_lock);
330 	space_map_unload(&vd->vdev_dtl_map);
331 	space_map_destroy(&vd->vdev_dtl_map);
332 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
333 	space_map_destroy(&vd->vdev_dtl_scrub);
334 	mutex_exit(&vd->vdev_dtl_lock);
335 	mutex_destroy(&vd->vdev_dtl_lock);
336 
337 	if (vd == spa->spa_root_vdev)
338 		spa->spa_root_vdev = NULL;
339 
340 	kmem_free(vd, sizeof (vdev_t));
341 }
342 
343 /*
344  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
345  * creating a new vdev or loading an existing one - the behavior is slightly
346  * different for each case.
347  */
348 vdev_t *
349 vdev_alloc(spa_t *spa, nvlist_t *nv, vdev_t *parent, uint_t id, int alloctype)
350 {
351 	vdev_ops_t *ops;
352 	char *type;
353 	uint64_t guid = 0;
354 	vdev_t *vd;
355 
356 	ASSERT(spa_config_held(spa, RW_WRITER));
357 
358 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
359 		return (NULL);
360 
361 	if ((ops = vdev_getops(type)) == NULL)
362 		return (NULL);
363 
364 	/*
365 	 * If this is a load, get the vdev guid from the nvlist.
366 	 * Otherwise, vdev_alloc_common() will generate one for us.
367 	 */
368 	if (alloctype == VDEV_ALLOC_LOAD) {
369 		uint64_t label_id;
370 
371 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
372 		    label_id != id)
373 			return (NULL);
374 
375 		if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
376 			return (NULL);
377 	}
378 
379 	vd = vdev_alloc_common(spa, id, guid, ops);
380 
381 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
382 		vd->vdev_path = spa_strdup(vd->vdev_path);
383 	if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
384 		vd->vdev_devid = spa_strdup(vd->vdev_devid);
385 
386 	/*
387 	 * Set the whole_disk property.  If it's not specified, leave the value
388 	 * as -1.
389 	 */
390 	if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
391 	    &vd->vdev_wholedisk) != 0)
392 		vd->vdev_wholedisk = -1ULL;
393 
394 	/*
395 	 * Look for the 'not present' flag.  This will only be set if the device
396 	 * was not present at the time of import.
397 	 */
398 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
399 	    &vd->vdev_not_present);
400 
401 	/*
402 	 * Get the alignment requirement.
403 	 */
404 	(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
405 
406 	/*
407 	 * If we're a top-level vdev, try to load the allocation parameters.
408 	 */
409 	if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
410 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
411 		    &vd->vdev_ms_array);
412 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
413 		    &vd->vdev_ms_shift);
414 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
415 		    &vd->vdev_asize);
416 	}
417 
418 	/*
419 	 * If we're a leaf vdev, try to load the DTL object and offline state.
420 	 */
421 	if (vd->vdev_ops->vdev_op_leaf && alloctype == VDEV_ALLOC_LOAD) {
422 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
423 		    &vd->vdev_dtl.smo_object);
424 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
425 		    &vd->vdev_offline);
426 	}
427 
428 	/*
429 	 * Add ourselves to the parent's list of children.
430 	 */
431 	vdev_add_child(parent, vd);
432 
433 	return (vd);
434 }
435 
436 void
437 vdev_free(vdev_t *vd)
438 {
439 	int c;
440 
441 	/*
442 	 * vdev_free() implies closing the vdev first.  This is simpler than
443 	 * trying to ensure complicated semantics for all callers.
444 	 */
445 	vdev_close(vd);
446 
447 	ASSERT(!list_link_active(&vd->vdev_dirty_node));
448 
449 	/*
450 	 * Free all children.
451 	 */
452 	for (c = 0; c < vd->vdev_children; c++)
453 		vdev_free(vd->vdev_child[c]);
454 
455 	ASSERT(vd->vdev_child == NULL);
456 	ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
457 
458 	/*
459 	 * Discard allocation state.
460 	 */
461 	if (vd == vd->vdev_top)
462 		vdev_metaslab_fini(vd);
463 
464 	ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
465 	ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
466 
467 	/*
468 	 * Remove this vdev from its parent's child list.
469 	 */
470 	vdev_remove_child(vd->vdev_parent, vd);
471 
472 	ASSERT(vd->vdev_parent == NULL);
473 
474 	vdev_free_common(vd);
475 }
476 
477 /*
478  * Transfer top-level vdev state from svd to tvd.
479  */
480 static void
481 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
482 {
483 	spa_t *spa = svd->vdev_spa;
484 	metaslab_t *msp;
485 	vdev_t *vd;
486 	int t;
487 
488 	ASSERT(tvd == tvd->vdev_top);
489 
490 	tvd->vdev_ms_array = svd->vdev_ms_array;
491 	tvd->vdev_ms_shift = svd->vdev_ms_shift;
492 	tvd->vdev_ms_count = svd->vdev_ms_count;
493 
494 	svd->vdev_ms_array = 0;
495 	svd->vdev_ms_shift = 0;
496 	svd->vdev_ms_count = 0;
497 
498 	tvd->vdev_mg = svd->vdev_mg;
499 	tvd->vdev_ms = svd->vdev_ms;
500 
501 	svd->vdev_mg = NULL;
502 	svd->vdev_ms = NULL;
503 
504 	if (tvd->vdev_mg != NULL)
505 		tvd->vdev_mg->mg_vd = tvd;
506 
507 	tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
508 	tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
509 
510 	svd->vdev_stat.vs_alloc = 0;
511 	svd->vdev_stat.vs_space = 0;
512 
513 	for (t = 0; t < TXG_SIZE; t++) {
514 		while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
515 			(void) txg_list_add(&tvd->vdev_ms_list, msp, t);
516 		while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
517 			(void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
518 		if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
519 			(void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
520 	}
521 
522 	if (list_link_active(&svd->vdev_dirty_node)) {
523 		vdev_config_clean(svd);
524 		vdev_config_dirty(tvd);
525 	}
526 
527 	tvd->vdev_reopen_wanted = svd->vdev_reopen_wanted;
528 	svd->vdev_reopen_wanted = 0;
529 }
530 
531 static void
532 vdev_top_update(vdev_t *tvd, vdev_t *vd)
533 {
534 	int c;
535 
536 	if (vd == NULL)
537 		return;
538 
539 	vd->vdev_top = tvd;
540 
541 	for (c = 0; c < vd->vdev_children; c++)
542 		vdev_top_update(tvd, vd->vdev_child[c]);
543 }
544 
545 /*
546  * Add a mirror/replacing vdev above an existing vdev.
547  */
548 vdev_t *
549 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
550 {
551 	spa_t *spa = cvd->vdev_spa;
552 	vdev_t *pvd = cvd->vdev_parent;
553 	vdev_t *mvd;
554 
555 	ASSERT(spa_config_held(spa, RW_WRITER));
556 
557 	mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
558 
559 	mvd->vdev_asize = cvd->vdev_asize;
560 	mvd->vdev_ashift = cvd->vdev_ashift;
561 	mvd->vdev_state = cvd->vdev_state;
562 
563 	vdev_remove_child(pvd, cvd);
564 	vdev_add_child(pvd, mvd);
565 	cvd->vdev_id = mvd->vdev_children;
566 	vdev_add_child(mvd, cvd);
567 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
568 
569 	if (mvd == mvd->vdev_top)
570 		vdev_top_transfer(cvd, mvd);
571 
572 	return (mvd);
573 }
574 
575 /*
576  * Remove a 1-way mirror/replacing vdev from the tree.
577  */
578 void
579 vdev_remove_parent(vdev_t *cvd)
580 {
581 	vdev_t *mvd = cvd->vdev_parent;
582 	vdev_t *pvd = mvd->vdev_parent;
583 
584 	ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
585 
586 	ASSERT(mvd->vdev_children == 1);
587 	ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
588 	    mvd->vdev_ops == &vdev_replacing_ops);
589 	cvd->vdev_ashift = mvd->vdev_ashift;
590 
591 	vdev_remove_child(mvd, cvd);
592 	vdev_remove_child(pvd, mvd);
593 	cvd->vdev_id = mvd->vdev_id;
594 	vdev_add_child(pvd, cvd);
595 	vdev_top_update(cvd->vdev_top, cvd->vdev_top);
596 
597 	if (cvd == cvd->vdev_top)
598 		vdev_top_transfer(mvd, cvd);
599 
600 	ASSERT(mvd->vdev_children == 0);
601 	vdev_free(mvd);
602 }
603 
604 int
605 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
606 {
607 	spa_t *spa = vd->vdev_spa;
608 	objset_t *mos = spa->spa_meta_objset;
609 	metaslab_class_t *mc = spa_metaslab_class_select(spa);
610 	uint64_t m;
611 	uint64_t oldc = vd->vdev_ms_count;
612 	uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
613 	metaslab_t **mspp;
614 	int error;
615 
616 	if (vd->vdev_ms_shift == 0)	/* not being allocated from yet */
617 		return (0);
618 
619 	dprintf("%s oldc %llu newc %llu\n", vdev_description(vd), oldc, newc);
620 
621 	ASSERT(oldc <= newc);
622 
623 	if (vd->vdev_mg == NULL)
624 		vd->vdev_mg = metaslab_group_create(mc, vd);
625 
626 	mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
627 
628 	if (oldc != 0) {
629 		bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
630 		kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
631 	}
632 
633 	vd->vdev_ms = mspp;
634 	vd->vdev_ms_count = newc;
635 
636 	for (m = oldc; m < newc; m++) {
637 		space_map_obj_t smo = { 0, 0, 0 };
638 		if (txg == 0) {
639 			uint64_t object = 0;
640 			error = dmu_read(mos, vd->vdev_ms_array,
641 			    m * sizeof (uint64_t), sizeof (uint64_t), &object);
642 			if (error)
643 				return (error);
644 			if (object != 0) {
645 				dmu_buf_t *db;
646 				error = dmu_bonus_hold(mos, object, FTAG, &db);
647 				if (error)
648 					return (error);
649 				ASSERT3U(db->db_size, ==, sizeof (smo));
650 				bcopy(db->db_data, &smo, db->db_size);
651 				ASSERT3U(smo.smo_object, ==, object);
652 				dmu_buf_rele(db, FTAG);
653 			}
654 		}
655 		vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
656 		    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
657 	}
658 
659 	return (0);
660 }
661 
662 void
663 vdev_metaslab_fini(vdev_t *vd)
664 {
665 	uint64_t m;
666 	uint64_t count = vd->vdev_ms_count;
667 
668 	if (vd->vdev_ms != NULL) {
669 		for (m = 0; m < count; m++)
670 			if (vd->vdev_ms[m] != NULL)
671 				metaslab_fini(vd->vdev_ms[m]);
672 		kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
673 		vd->vdev_ms = NULL;
674 	}
675 }
676 
677 /*
678  * Prepare a virtual device for access.
679  */
680 int
681 vdev_open(vdev_t *vd)
682 {
683 	int error;
684 	vdev_knob_t *vk;
685 	int c;
686 	uint64_t osize = 0;
687 	uint64_t asize, psize;
688 	uint64_t ashift = 0;
689 
690 	ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
691 	    vd->vdev_state == VDEV_STATE_CANT_OPEN ||
692 	    vd->vdev_state == VDEV_STATE_OFFLINE);
693 
694 	if (vd->vdev_fault_mode == VDEV_FAULT_COUNT)
695 		vd->vdev_fault_arg >>= 1;
696 	else
697 		vd->vdev_fault_mode = VDEV_FAULT_NONE;
698 
699 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
700 
701 	for (vk = vdev_knob_next(NULL); vk != NULL; vk = vdev_knob_next(vk)) {
702 		uint64_t *valp = (uint64_t *)((char *)vd + vk->vk_offset);
703 
704 		*valp = vk->vk_default;
705 		*valp = MAX(*valp, vk->vk_min);
706 		*valp = MIN(*valp, vk->vk_max);
707 	}
708 
709 	if (vd->vdev_ops->vdev_op_leaf) {
710 		vdev_cache_init(vd);
711 		vdev_queue_init(vd);
712 		vd->vdev_cache_active = B_TRUE;
713 	}
714 
715 	if (vd->vdev_offline) {
716 		ASSERT(vd->vdev_children == 0);
717 		vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
718 		return (ENXIO);
719 	}
720 
721 	error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
722 
723 	if (zio_injection_enabled && error == 0)
724 		error = zio_handle_device_injection(vd, ENXIO);
725 
726 	dprintf("%s = %d, osize %llu, state = %d\n",
727 	    vdev_description(vd), error, osize, vd->vdev_state);
728 
729 	if (error) {
730 		vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
731 		    vd->vdev_stat.vs_aux);
732 		return (error);
733 	}
734 
735 	vd->vdev_state = VDEV_STATE_HEALTHY;
736 
737 	for (c = 0; c < vd->vdev_children; c++)
738 		if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
739 			vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
740 			    VDEV_AUX_NONE);
741 			break;
742 		}
743 
744 	osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
745 
746 	if (vd->vdev_children == 0) {
747 		if (osize < SPA_MINDEVSIZE) {
748 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
749 			    VDEV_AUX_TOO_SMALL);
750 			return (EOVERFLOW);
751 		}
752 		psize = osize;
753 		asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
754 	} else {
755 		if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
756 		    (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
757 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
758 			    VDEV_AUX_TOO_SMALL);
759 			return (EOVERFLOW);
760 		}
761 		psize = 0;
762 		asize = osize;
763 	}
764 
765 	vd->vdev_psize = psize;
766 
767 	if (vd->vdev_asize == 0) {
768 		/*
769 		 * This is the first-ever open, so use the computed values.
770 		 * For testing purposes, a higher ashift can be requested.
771 		 */
772 		vd->vdev_asize = asize;
773 		vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
774 	} else {
775 		/*
776 		 * Make sure the alignment requirement hasn't increased.
777 		 */
778 		if (ashift > vd->vdev_top->vdev_ashift) {
779 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
780 			    VDEV_AUX_BAD_LABEL);
781 			return (EINVAL);
782 		}
783 
784 		/*
785 		 * Make sure the device hasn't shrunk.
786 		 */
787 		if (asize < vd->vdev_asize) {
788 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
789 			    VDEV_AUX_BAD_LABEL);
790 			return (EINVAL);
791 		}
792 
793 		/*
794 		 * If all children are healthy and the asize has increased,
795 		 * then we've experienced dynamic LUN growth.
796 		 */
797 		if (vd->vdev_state == VDEV_STATE_HEALTHY &&
798 		    asize > vd->vdev_asize) {
799 			vd->vdev_asize = asize;
800 		}
801 	}
802 
803 	/*
804 	 * This allows the ZFS DE to close cases appropriately.  If a device
805 	 * goes away and later returns, we want to close the associated case.
806 	 * But it's not enough to simply post this only when a device goes from
807 	 * CANT_OPEN -> HEALTHY.  If we reboot the system and the device is
808 	 * back, we also need to close the case (otherwise we will try to replay
809 	 * it).  So we have to post this notifier every time.  Since this only
810 	 * occurs during pool open or error recovery, this should not be an
811 	 * issue.
812 	 */
813 	zfs_post_ok(vd->vdev_spa, vd);
814 
815 	return (0);
816 }
817 
818 /*
819  * Called once the vdevs are all opened, this routine validates the label
820  * contents.  This needs to be done before vdev_load() so that we don't
821  * inadvertently do repair I/Os to the wrong device, and so that vdev_reopen()
822  * won't succeed if the device has been changed underneath.
823  *
824  * This function will only return failure if one of the vdevs indicates that it
825  * has since been destroyed or exported.  This is only possible if
826  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
827  * will be updated but the function will return 0.
828  */
829 int
830 vdev_validate(vdev_t *vd)
831 {
832 	spa_t *spa = vd->vdev_spa;
833 	int c;
834 	nvlist_t *label;
835 	uint64_t guid;
836 	uint64_t state;
837 
838 	for (c = 0; c < vd->vdev_children; c++)
839 		if (vdev_validate(vd->vdev_child[c]) != 0)
840 			return (-1);
841 
842 	if (vd->vdev_ops->vdev_op_leaf) {
843 
844 		if ((label = vdev_label_read_config(vd)) == NULL) {
845 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
846 			    VDEV_AUX_BAD_LABEL);
847 			return (0);
848 		}
849 
850 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
851 		    &guid) != 0 || guid != spa_guid(spa)) {
852 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
853 			    VDEV_AUX_CORRUPT_DATA);
854 			nvlist_free(label);
855 			return (0);
856 		}
857 
858 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
859 		    &guid) != 0 || guid != vd->vdev_guid) {
860 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
861 			    VDEV_AUX_CORRUPT_DATA);
862 			nvlist_free(label);
863 			return (0);
864 		}
865 
866 		if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
867 		    &state) != 0) {
868 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
869 			    VDEV_AUX_CORRUPT_DATA);
870 			nvlist_free(label);
871 			return (0);
872 		}
873 
874 		nvlist_free(label);
875 
876 		if (spa->spa_load_state == SPA_LOAD_OPEN &&
877 		    state != POOL_STATE_ACTIVE)
878 			return (-1);
879 	}
880 
881 	/*
882 	 * If we were able to open and validate a vdev that was previously
883 	 * marked permanently unavailable, clear that state now.
884 	 */
885 	if (vd->vdev_not_present)
886 		vd->vdev_not_present = 0;
887 
888 	return (0);
889 }
890 
891 /*
892  * Close a virtual device.
893  */
894 void
895 vdev_close(vdev_t *vd)
896 {
897 	vd->vdev_ops->vdev_op_close(vd);
898 
899 	if (vd->vdev_cache_active) {
900 		vdev_cache_fini(vd);
901 		vdev_queue_fini(vd);
902 		vd->vdev_cache_active = B_FALSE;
903 	}
904 
905 	/*
906 	 * We record the previous state before we close it, so  that if we are
907 	 * doing a reopen(), we don't generate FMA ereports if we notice that
908 	 * it's still faulted.
909 	 */
910 	vd->vdev_prevstate = vd->vdev_state;
911 
912 	if (vd->vdev_offline)
913 		vd->vdev_state = VDEV_STATE_OFFLINE;
914 	else
915 		vd->vdev_state = VDEV_STATE_CLOSED;
916 	vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
917 }
918 
919 void
920 vdev_reopen(vdev_t *vd)
921 {
922 	spa_t *spa = vd->vdev_spa;
923 
924 	ASSERT(spa_config_held(spa, RW_WRITER));
925 
926 	vdev_close(vd);
927 	(void) vdev_open(vd);
928 
929 	/*
930 	 * Reassess root vdev's health.
931 	 */
932 	vdev_propagate_state(spa->spa_root_vdev);
933 }
934 
935 int
936 vdev_create(vdev_t *vd, uint64_t txg)
937 {
938 	int error;
939 
940 	/*
941 	 * Normally, partial opens (e.g. of a mirror) are allowed.
942 	 * For a create, however, we want to fail the request if
943 	 * there are any components we can't open.
944 	 */
945 	error = vdev_open(vd);
946 
947 	if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
948 		vdev_close(vd);
949 		return (error ? error : ENXIO);
950 	}
951 
952 	/*
953 	 * Recursively initialize all labels.
954 	 */
955 	if ((error = vdev_label_init(vd, txg)) != 0) {
956 		vdev_close(vd);
957 		return (error);
958 	}
959 
960 	return (0);
961 }
962 
963 /*
964  * The is the latter half of vdev_create().  It is distinct because it
965  * involves initiating transactions in order to do metaslab creation.
966  * For creation, we want to try to create all vdevs at once and then undo it
967  * if anything fails; this is much harder if we have pending transactions.
968  */
969 void
970 vdev_init(vdev_t *vd, uint64_t txg)
971 {
972 	/*
973 	 * Aim for roughly 200 metaslabs per vdev.
974 	 */
975 	vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
976 	vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
977 
978 	/*
979 	 * Initialize the vdev's metaslabs.  This can't fail because
980 	 * there's nothing to read when creating all new metaslabs.
981 	 */
982 	VERIFY(vdev_metaslab_init(vd, txg) == 0);
983 }
984 
985 void
986 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
987 {
988 	ASSERT(vd == vd->vdev_top);
989 	ASSERT(ISP2(flags));
990 
991 	if (flags & VDD_METASLAB)
992 		(void) txg_list_add(&vd->vdev_ms_list, arg, txg);
993 
994 	if (flags & VDD_DTL)
995 		(void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
996 
997 	(void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
998 }
999 
1000 void
1001 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1002 {
1003 	mutex_enter(sm->sm_lock);
1004 	if (!space_map_contains(sm, txg, size))
1005 		space_map_add(sm, txg, size);
1006 	mutex_exit(sm->sm_lock);
1007 }
1008 
1009 int
1010 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1011 {
1012 	int dirty;
1013 
1014 	/*
1015 	 * Quick test without the lock -- covers the common case that
1016 	 * there are no dirty time segments.
1017 	 */
1018 	if (sm->sm_space == 0)
1019 		return (0);
1020 
1021 	mutex_enter(sm->sm_lock);
1022 	dirty = space_map_contains(sm, txg, size);
1023 	mutex_exit(sm->sm_lock);
1024 
1025 	return (dirty);
1026 }
1027 
1028 /*
1029  * Reassess DTLs after a config change or scrub completion.
1030  */
1031 void
1032 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1033 {
1034 	spa_t *spa = vd->vdev_spa;
1035 	int c;
1036 
1037 	ASSERT(spa_config_held(spa, RW_WRITER));
1038 
1039 	if (vd->vdev_children == 0) {
1040 		mutex_enter(&vd->vdev_dtl_lock);
1041 		/*
1042 		 * We're successfully scrubbed everything up to scrub_txg.
1043 		 * Therefore, excise all old DTLs up to that point, then
1044 		 * fold in the DTLs for everything we couldn't scrub.
1045 		 */
1046 		if (scrub_txg != 0) {
1047 			space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1048 			space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1049 		}
1050 		if (scrub_done)
1051 			space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1052 		mutex_exit(&vd->vdev_dtl_lock);
1053 		if (txg != 0)
1054 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1055 		return;
1056 	}
1057 
1058 	/*
1059 	 * Make sure the DTLs are always correct under the scrub lock.
1060 	 */
1061 	if (vd == spa->spa_root_vdev)
1062 		mutex_enter(&spa->spa_scrub_lock);
1063 
1064 	mutex_enter(&vd->vdev_dtl_lock);
1065 	space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1066 	space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1067 	mutex_exit(&vd->vdev_dtl_lock);
1068 
1069 	for (c = 0; c < vd->vdev_children; c++) {
1070 		vdev_t *cvd = vd->vdev_child[c];
1071 		vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1072 		mutex_enter(&vd->vdev_dtl_lock);
1073 		space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1074 		space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1075 		mutex_exit(&vd->vdev_dtl_lock);
1076 	}
1077 
1078 	if (vd == spa->spa_root_vdev)
1079 		mutex_exit(&spa->spa_scrub_lock);
1080 }
1081 
1082 static int
1083 vdev_dtl_load(vdev_t *vd)
1084 {
1085 	spa_t *spa = vd->vdev_spa;
1086 	space_map_obj_t *smo = &vd->vdev_dtl;
1087 	objset_t *mos = spa->spa_meta_objset;
1088 	dmu_buf_t *db;
1089 	int error;
1090 
1091 	ASSERT(vd->vdev_children == 0);
1092 
1093 	if (smo->smo_object == 0)
1094 		return (0);
1095 
1096 	if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1097 		return (error);
1098 
1099 	ASSERT3U(db->db_size, ==, sizeof (*smo));
1100 	bcopy(db->db_data, smo, db->db_size);
1101 	dmu_buf_rele(db, FTAG);
1102 
1103 	mutex_enter(&vd->vdev_dtl_lock);
1104 	error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1105 	mutex_exit(&vd->vdev_dtl_lock);
1106 
1107 	return (error);
1108 }
1109 
1110 void
1111 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1112 {
1113 	spa_t *spa = vd->vdev_spa;
1114 	space_map_obj_t *smo = &vd->vdev_dtl;
1115 	space_map_t *sm = &vd->vdev_dtl_map;
1116 	objset_t *mos = spa->spa_meta_objset;
1117 	space_map_t smsync;
1118 	kmutex_t smlock;
1119 	dmu_buf_t *db;
1120 	dmu_tx_t *tx;
1121 
1122 	dprintf("%s in txg %llu pass %d\n",
1123 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1124 
1125 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1126 
1127 	if (vd->vdev_detached) {
1128 		if (smo->smo_object != 0) {
1129 			int err = dmu_object_free(mos, smo->smo_object, tx);
1130 			ASSERT3U(err, ==, 0);
1131 			smo->smo_object = 0;
1132 		}
1133 		dmu_tx_commit(tx);
1134 		dprintf("detach %s committed in txg %llu\n",
1135 		    vdev_description(vd), txg);
1136 		return;
1137 	}
1138 
1139 	if (smo->smo_object == 0) {
1140 		ASSERT(smo->smo_objsize == 0);
1141 		ASSERT(smo->smo_alloc == 0);
1142 		smo->smo_object = dmu_object_alloc(mos,
1143 		    DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1144 		    DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1145 		ASSERT(smo->smo_object != 0);
1146 		vdev_config_dirty(vd->vdev_top);
1147 	}
1148 
1149 	mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1150 
1151 	space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1152 	    &smlock);
1153 
1154 	mutex_enter(&smlock);
1155 
1156 	mutex_enter(&vd->vdev_dtl_lock);
1157 	space_map_walk(sm, space_map_add, &smsync);
1158 	mutex_exit(&vd->vdev_dtl_lock);
1159 
1160 	space_map_truncate(smo, mos, tx);
1161 	space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1162 
1163 	space_map_destroy(&smsync);
1164 
1165 	mutex_exit(&smlock);
1166 	mutex_destroy(&smlock);
1167 
1168 	VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1169 	dmu_buf_will_dirty(db, tx);
1170 	ASSERT3U(db->db_size, ==, sizeof (*smo));
1171 	bcopy(smo, db->db_data, db->db_size);
1172 	dmu_buf_rele(db, FTAG);
1173 
1174 	dmu_tx_commit(tx);
1175 }
1176 
1177 void
1178 vdev_load(vdev_t *vd)
1179 {
1180 	int c;
1181 
1182 	/*
1183 	 * Recursively load all children.
1184 	 */
1185 	for (c = 0; c < vd->vdev_children; c++)
1186 		vdev_load(vd->vdev_child[c]);
1187 
1188 	/*
1189 	 * If this is a top-level vdev, initialize its metaslabs.
1190 	 */
1191 	if (vd == vd->vdev_top &&
1192 	    (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1193 	    vdev_metaslab_init(vd, 0) != 0))
1194 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1195 		    VDEV_AUX_CORRUPT_DATA);
1196 
1197 	/*
1198 	 * If this is a leaf vdev, load its DTL.
1199 	 */
1200 	if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1201 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1202 		    VDEV_AUX_CORRUPT_DATA);
1203 }
1204 
1205 void
1206 vdev_sync_done(vdev_t *vd, uint64_t txg)
1207 {
1208 	metaslab_t *msp;
1209 
1210 	dprintf("%s txg %llu\n", vdev_description(vd), txg);
1211 
1212 	while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1213 		metaslab_sync_done(msp, txg);
1214 }
1215 
1216 void
1217 vdev_sync(vdev_t *vd, uint64_t txg)
1218 {
1219 	spa_t *spa = vd->vdev_spa;
1220 	vdev_t *lvd;
1221 	metaslab_t *msp;
1222 	dmu_tx_t *tx;
1223 
1224 	dprintf("%s txg %llu pass %d\n",
1225 	    vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1226 
1227 	if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1228 		ASSERT(vd == vd->vdev_top);
1229 		tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1230 		vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1231 		    DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1232 		ASSERT(vd->vdev_ms_array != 0);
1233 		vdev_config_dirty(vd);
1234 		dmu_tx_commit(tx);
1235 	}
1236 
1237 	while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1238 		metaslab_sync(msp, txg);
1239 		(void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1240 	}
1241 
1242 	while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1243 		vdev_dtl_sync(lvd, txg);
1244 
1245 	(void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1246 }
1247 
1248 uint64_t
1249 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1250 {
1251 	return (vd->vdev_ops->vdev_op_asize(vd, psize));
1252 }
1253 
1254 void
1255 vdev_io_start(zio_t *zio)
1256 {
1257 	zio->io_vd->vdev_ops->vdev_op_io_start(zio);
1258 }
1259 
1260 void
1261 vdev_io_done(zio_t *zio)
1262 {
1263 	zio->io_vd->vdev_ops->vdev_op_io_done(zio);
1264 }
1265 
1266 const char *
1267 vdev_description(vdev_t *vd)
1268 {
1269 	if (vd == NULL || vd->vdev_ops == NULL)
1270 		return ("<unknown>");
1271 
1272 	if (vd->vdev_path != NULL)
1273 		return (vd->vdev_path);
1274 
1275 	if (vd->vdev_parent == NULL)
1276 		return (spa_name(vd->vdev_spa));
1277 
1278 	return (vd->vdev_ops->vdev_op_type);
1279 }
1280 
1281 int
1282 vdev_online(spa_t *spa, uint64_t guid)
1283 {
1284 	vdev_t *rvd, *vd;
1285 	uint64_t txg;
1286 
1287 	txg = spa_vdev_enter(spa);
1288 
1289 	rvd = spa->spa_root_vdev;
1290 
1291 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1292 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1293 
1294 	if (!vd->vdev_ops->vdev_op_leaf)
1295 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1296 
1297 	dprintf("ONLINE: %s\n", vdev_description(vd));
1298 
1299 	vd->vdev_offline = B_FALSE;
1300 	vd->vdev_tmpoffline = B_FALSE;
1301 	vdev_reopen(vd->vdev_top);
1302 
1303 	vdev_config_dirty(vd->vdev_top);
1304 
1305 	(void) spa_vdev_exit(spa, NULL, txg, 0);
1306 
1307 	VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1308 
1309 	return (0);
1310 }
1311 
1312 int
1313 vdev_offline(spa_t *spa, uint64_t guid, int istmp)
1314 {
1315 	vdev_t *rvd, *vd;
1316 	uint64_t txg;
1317 
1318 	txg = spa_vdev_enter(spa);
1319 
1320 	rvd = spa->spa_root_vdev;
1321 
1322 	if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1323 		return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1324 
1325 	if (!vd->vdev_ops->vdev_op_leaf)
1326 		return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1327 
1328 	dprintf("OFFLINE: %s\n", vdev_description(vd));
1329 
1330 	/*
1331 	 * If the device isn't already offline, try to offline it.
1332 	 */
1333 	if (!vd->vdev_offline) {
1334 		/*
1335 		 * If this device's top-level vdev has a non-empty DTL,
1336 		 * don't allow the device to be offlined.
1337 		 *
1338 		 * XXX -- make this more precise by allowing the offline
1339 		 * as long as the remaining devices don't have any DTL holes.
1340 		 */
1341 		if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1342 			return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1343 
1344 		/*
1345 		 * Offline this device and reopen its top-level vdev.
1346 		 * If this action results in the top-level vdev becoming
1347 		 * unusable, undo it and fail the request.
1348 		 */
1349 		vd->vdev_offline = B_TRUE;
1350 		vdev_reopen(vd->vdev_top);
1351 		if (vdev_is_dead(vd->vdev_top)) {
1352 			vd->vdev_offline = B_FALSE;
1353 			vdev_reopen(vd->vdev_top);
1354 			return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1355 		}
1356 	}
1357 
1358 	vd->vdev_tmpoffline = istmp;
1359 
1360 	vdev_config_dirty(vd->vdev_top);
1361 
1362 	return (spa_vdev_exit(spa, NULL, txg, 0));
1363 }
1364 
1365 /*
1366  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
1367  * vdev_offline(), we assume the spa config is locked.  We also clear all
1368  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
1369  */
1370 void
1371 vdev_clear(spa_t *spa, vdev_t *vd)
1372 {
1373 	int c;
1374 
1375 	if (vd == NULL)
1376 		vd = spa->spa_root_vdev;
1377 
1378 	vd->vdev_stat.vs_read_errors = 0;
1379 	vd->vdev_stat.vs_write_errors = 0;
1380 	vd->vdev_stat.vs_checksum_errors = 0;
1381 
1382 	for (c = 0; c < vd->vdev_children; c++)
1383 		vdev_clear(spa, vd->vdev_child[c]);
1384 }
1385 
1386 int
1387 vdev_is_dead(vdev_t *vd)
1388 {
1389 	return (vd->vdev_state <= VDEV_STATE_CANT_OPEN);
1390 }
1391 
1392 int
1393 vdev_error_inject(vdev_t *vd, zio_t *zio)
1394 {
1395 	int error = 0;
1396 
1397 	if (vd->vdev_fault_mode == VDEV_FAULT_NONE)
1398 		return (0);
1399 
1400 	if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0)
1401 		return (0);
1402 
1403 	switch (vd->vdev_fault_mode) {
1404 	case VDEV_FAULT_RANDOM:
1405 		if (spa_get_random(vd->vdev_fault_arg) == 0)
1406 			error = EIO;
1407 		break;
1408 
1409 	case VDEV_FAULT_COUNT:
1410 		if ((int64_t)--vd->vdev_fault_arg <= 0)
1411 			vd->vdev_fault_mode = VDEV_FAULT_NONE;
1412 		error = EIO;
1413 		break;
1414 	}
1415 
1416 	if (error != 0) {
1417 		dprintf("returning %d for type %d on %s state %d offset %llx\n",
1418 		    error, zio->io_type, vdev_description(vd),
1419 		    vd->vdev_state, zio->io_offset);
1420 	}
1421 
1422 	return (error);
1423 }
1424 
1425 /*
1426  * Get statistics for the given vdev.
1427  */
1428 void
1429 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1430 {
1431 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1432 	int c, t;
1433 
1434 	mutex_enter(&vd->vdev_stat_lock);
1435 	bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1436 	vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1437 	vs->vs_state = vd->vdev_state;
1438 	vs->vs_rsize = vdev_get_rsize(vd);
1439 	mutex_exit(&vd->vdev_stat_lock);
1440 
1441 	/*
1442 	 * If we're getting stats on the root vdev, aggregate the I/O counts
1443 	 * over all top-level vdevs (i.e. the direct children of the root).
1444 	 */
1445 	if (vd == rvd) {
1446 		for (c = 0; c < rvd->vdev_children; c++) {
1447 			vdev_t *cvd = rvd->vdev_child[c];
1448 			vdev_stat_t *cvs = &cvd->vdev_stat;
1449 
1450 			mutex_enter(&vd->vdev_stat_lock);
1451 			for (t = 0; t < ZIO_TYPES; t++) {
1452 				vs->vs_ops[t] += cvs->vs_ops[t];
1453 				vs->vs_bytes[t] += cvs->vs_bytes[t];
1454 			}
1455 			vs->vs_read_errors += cvs->vs_read_errors;
1456 			vs->vs_write_errors += cvs->vs_write_errors;
1457 			vs->vs_checksum_errors += cvs->vs_checksum_errors;
1458 			vs->vs_scrub_examined += cvs->vs_scrub_examined;
1459 			vs->vs_scrub_errors += cvs->vs_scrub_errors;
1460 			mutex_exit(&vd->vdev_stat_lock);
1461 		}
1462 	}
1463 }
1464 
1465 void
1466 vdev_stat_update(zio_t *zio)
1467 {
1468 	vdev_t *vd = zio->io_vd;
1469 	vdev_t *pvd;
1470 	uint64_t txg = zio->io_txg;
1471 	vdev_stat_t *vs = &vd->vdev_stat;
1472 	zio_type_t type = zio->io_type;
1473 	int flags = zio->io_flags;
1474 
1475 	if (zio->io_error == 0) {
1476 		if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1477 			mutex_enter(&vd->vdev_stat_lock);
1478 			vs->vs_ops[type]++;
1479 			vs->vs_bytes[type] += zio->io_size;
1480 			mutex_exit(&vd->vdev_stat_lock);
1481 		}
1482 		if ((flags & ZIO_FLAG_IO_REPAIR) &&
1483 		    zio->io_delegate_list == NULL) {
1484 			mutex_enter(&vd->vdev_stat_lock);
1485 			if (flags & ZIO_FLAG_SCRUB_THREAD)
1486 				vs->vs_scrub_repaired += zio->io_size;
1487 			else
1488 				vs->vs_self_healed += zio->io_size;
1489 			mutex_exit(&vd->vdev_stat_lock);
1490 		}
1491 		return;
1492 	}
1493 
1494 	if (flags & ZIO_FLAG_SPECULATIVE)
1495 		return;
1496 
1497 	if (!vdev_is_dead(vd)) {
1498 		mutex_enter(&vd->vdev_stat_lock);
1499 		if (type == ZIO_TYPE_READ) {
1500 			if (zio->io_error == ECKSUM)
1501 				vs->vs_checksum_errors++;
1502 			else
1503 				vs->vs_read_errors++;
1504 		}
1505 		if (type == ZIO_TYPE_WRITE)
1506 			vs->vs_write_errors++;
1507 		mutex_exit(&vd->vdev_stat_lock);
1508 	}
1509 
1510 	if (type == ZIO_TYPE_WRITE) {
1511 		if (txg == 0 || vd->vdev_children != 0)
1512 			return;
1513 		if (flags & ZIO_FLAG_SCRUB_THREAD) {
1514 			ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1515 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1516 				vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1517 		}
1518 		if (!(flags & ZIO_FLAG_IO_REPAIR)) {
1519 			if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
1520 				return;
1521 			vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1522 			for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1523 				vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
1524 		}
1525 	}
1526 }
1527 
1528 void
1529 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
1530 {
1531 	int c;
1532 	vdev_stat_t *vs = &vd->vdev_stat;
1533 
1534 	for (c = 0; c < vd->vdev_children; c++)
1535 		vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
1536 
1537 	mutex_enter(&vd->vdev_stat_lock);
1538 
1539 	if (type == POOL_SCRUB_NONE) {
1540 		/*
1541 		 * Update completion and end time.  Leave everything else alone
1542 		 * so we can report what happened during the previous scrub.
1543 		 */
1544 		vs->vs_scrub_complete = complete;
1545 		vs->vs_scrub_end = gethrestime_sec();
1546 	} else {
1547 		vs->vs_scrub_type = type;
1548 		vs->vs_scrub_complete = 0;
1549 		vs->vs_scrub_examined = 0;
1550 		vs->vs_scrub_repaired = 0;
1551 		vs->vs_scrub_errors = 0;
1552 		vs->vs_scrub_start = gethrestime_sec();
1553 		vs->vs_scrub_end = 0;
1554 	}
1555 
1556 	mutex_exit(&vd->vdev_stat_lock);
1557 }
1558 
1559 /*
1560  * Update the in-core space usage stats for this vdev and the root vdev.
1561  */
1562 void
1563 vdev_space_update(vdev_t *vd, uint64_t space_delta, uint64_t alloc_delta)
1564 {
1565 	ASSERT(vd == vd->vdev_top);
1566 
1567 	do {
1568 		mutex_enter(&vd->vdev_stat_lock);
1569 		vd->vdev_stat.vs_space += space_delta;
1570 		vd->vdev_stat.vs_alloc += alloc_delta;
1571 		mutex_exit(&vd->vdev_stat_lock);
1572 	} while ((vd = vd->vdev_parent) != NULL);
1573 }
1574 
1575 /*
1576  * Various knobs to tune a vdev.
1577  */
1578 static vdev_knob_t vdev_knob[] = {
1579 	{
1580 		"cache_size",
1581 		"size of the read-ahead cache",
1582 		0,
1583 		1ULL << 30,
1584 		10ULL << 20,
1585 		offsetof(struct vdev, vdev_cache.vc_size)
1586 	},
1587 	{
1588 		"cache_bshift",
1589 		"log2 of cache blocksize",
1590 		SPA_MINBLOCKSHIFT,
1591 		SPA_MAXBLOCKSHIFT,
1592 		16,
1593 		offsetof(struct vdev, vdev_cache.vc_bshift)
1594 	},
1595 	{
1596 		"cache_max",
1597 		"largest block size to cache",
1598 		0,
1599 		SPA_MAXBLOCKSIZE,
1600 		1ULL << 14,
1601 		offsetof(struct vdev, vdev_cache.vc_max)
1602 	},
1603 	{
1604 		"min_pending",
1605 		"minimum pending I/Os to the disk",
1606 		1,
1607 		10000,
1608 		2,
1609 		offsetof(struct vdev, vdev_queue.vq_min_pending)
1610 	},
1611 	{
1612 		"max_pending",
1613 		"maximum pending I/Os to the disk",
1614 		1,
1615 		10000,
1616 		35,
1617 		offsetof(struct vdev, vdev_queue.vq_max_pending)
1618 	},
1619 	{
1620 		"scrub_limit",
1621 		"maximum scrub/resilver I/O queue",
1622 		0,
1623 		10000,
1624 		70,
1625 		offsetof(struct vdev, vdev_queue.vq_scrub_limit)
1626 	},
1627 	{
1628 		"agg_limit",
1629 		"maximum size of aggregated I/Os",
1630 		0,
1631 		SPA_MAXBLOCKSIZE,
1632 		SPA_MAXBLOCKSIZE,
1633 		offsetof(struct vdev, vdev_queue.vq_agg_limit)
1634 	},
1635 	{
1636 		"time_shift",
1637 		"deadline = pri + (lbolt >> time_shift)",
1638 		0,
1639 		63,
1640 		4,
1641 		offsetof(struct vdev, vdev_queue.vq_time_shift)
1642 	},
1643 	{
1644 		"ramp_rate",
1645 		"exponential I/O issue ramp-up rate",
1646 		1,
1647 		10000,
1648 		2,
1649 		offsetof(struct vdev, vdev_queue.vq_ramp_rate)
1650 	},
1651 };
1652 
1653 vdev_knob_t *
1654 vdev_knob_next(vdev_knob_t *vk)
1655 {
1656 	if (vk == NULL)
1657 		return (vdev_knob);
1658 
1659 	if (++vk == vdev_knob + sizeof (vdev_knob) / sizeof (vdev_knob_t))
1660 		return (NULL);
1661 
1662 	return (vk);
1663 }
1664 
1665 /*
1666  * Mark a top-level vdev's config as dirty, placing it on the dirty list
1667  * so that it will be written out next time the vdev configuration is synced.
1668  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
1669  */
1670 void
1671 vdev_config_dirty(vdev_t *vd)
1672 {
1673 	spa_t *spa = vd->vdev_spa;
1674 	vdev_t *rvd = spa->spa_root_vdev;
1675 	int c;
1676 
1677 	/*
1678 	 * The dirty list is protected by the config lock.  The caller must
1679 	 * either hold the config lock as writer, or must be the sync thread
1680 	 * (which holds the lock as reader).  There's only one sync thread,
1681 	 * so this is sufficient to ensure mutual exclusion.
1682 	 */
1683 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1684 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1685 
1686 	if (vd == rvd) {
1687 		for (c = 0; c < rvd->vdev_children; c++)
1688 			vdev_config_dirty(rvd->vdev_child[c]);
1689 	} else {
1690 		ASSERT(vd == vd->vdev_top);
1691 
1692 		if (!list_link_active(&vd->vdev_dirty_node))
1693 			list_insert_head(&spa->spa_dirty_list, vd);
1694 	}
1695 }
1696 
1697 void
1698 vdev_config_clean(vdev_t *vd)
1699 {
1700 	spa_t *spa = vd->vdev_spa;
1701 
1702 	ASSERT(spa_config_held(spa, RW_WRITER) ||
1703 	    dsl_pool_sync_context(spa_get_dsl(spa)));
1704 
1705 	ASSERT(list_link_active(&vd->vdev_dirty_node));
1706 	list_remove(&spa->spa_dirty_list, vd);
1707 }
1708 
1709 void
1710 vdev_propagate_state(vdev_t *vd)
1711 {
1712 	vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1713 	int degraded = 0, faulted = 0;
1714 	int corrupted = 0;
1715 	int c;
1716 	vdev_t *child;
1717 
1718 	for (c = 0; c < vd->vdev_children; c++) {
1719 		child = vd->vdev_child[c];
1720 		if (child->vdev_state <= VDEV_STATE_CANT_OPEN)
1721 			faulted++;
1722 		else if (child->vdev_state == VDEV_STATE_DEGRADED)
1723 			degraded++;
1724 
1725 		if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
1726 			corrupted++;
1727 	}
1728 
1729 	vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
1730 
1731 	/*
1732 	 * Root special: if there is a toplevel vdev that cannot be
1733 	 * opened due to corrupted metadata, then propagate the root
1734 	 * vdev's aux state as 'corrupt' rather than 'insufficient
1735 	 * replicas'.
1736 	 */
1737 	if (corrupted && vd == rvd && rvd->vdev_state == VDEV_STATE_CANT_OPEN)
1738 		vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
1739 		    VDEV_AUX_CORRUPT_DATA);
1740 }
1741 
1742 /*
1743  * Set a vdev's state.  If this is during an open, we don't update the parent
1744  * state, because we're in the process of opening children depth-first.
1745  * Otherwise, we propagate the change to the parent.
1746  *
1747  * If this routine places a device in a faulted state, an appropriate ereport is
1748  * generated.
1749  */
1750 void
1751 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
1752 {
1753 	uint64_t save_state;
1754 
1755 	if (state == vd->vdev_state) {
1756 		vd->vdev_stat.vs_aux = aux;
1757 		return;
1758 	}
1759 
1760 	save_state = vd->vdev_state;
1761 
1762 	vd->vdev_state = state;
1763 	vd->vdev_stat.vs_aux = aux;
1764 
1765 	if (state == VDEV_STATE_CANT_OPEN) {
1766 		/*
1767 		 * If we fail to open a vdev during an import, we mark it as
1768 		 * "not available", which signifies that it was never there to
1769 		 * begin with.  Failure to open such a device is not considered
1770 		 * an error.
1771 		 */
1772 		if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT &&
1773 		    vd->vdev_ops->vdev_op_leaf)
1774 			vd->vdev_not_present = 1;
1775 
1776 		/*
1777 		 * Post the appropriate ereport.  If the 'prevstate' field is
1778 		 * set to something other than VDEV_STATE_UNKNOWN, it indicates
1779 		 * that this is part of a vdev_reopen().  In this case, we don't
1780 		 * want to post the ereport if the device was already in the
1781 		 * CANT_OPEN state beforehand.
1782 		 */
1783 		if (vd->vdev_prevstate != state && !vd->vdev_not_present &&
1784 		    vd != vd->vdev_spa->spa_root_vdev) {
1785 			const char *class;
1786 
1787 			switch (aux) {
1788 			case VDEV_AUX_OPEN_FAILED:
1789 				class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
1790 				break;
1791 			case VDEV_AUX_CORRUPT_DATA:
1792 				class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
1793 				break;
1794 			case VDEV_AUX_NO_REPLICAS:
1795 				class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
1796 				break;
1797 			case VDEV_AUX_BAD_GUID_SUM:
1798 				class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
1799 				break;
1800 			case VDEV_AUX_TOO_SMALL:
1801 				class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
1802 				break;
1803 			case VDEV_AUX_BAD_LABEL:
1804 				class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
1805 				break;
1806 			default:
1807 				class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
1808 			}
1809 
1810 			zfs_ereport_post(class, vd->vdev_spa,
1811 			    vd, NULL, save_state, 0);
1812 		}
1813 	}
1814 
1815 	if (isopen)
1816 		return;
1817 
1818 	if (vd->vdev_parent != NULL)
1819 		vdev_propagate_state(vd->vdev_parent);
1820 }
1821