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