xref: /freebsd/sys/geom/mirror/g_mirror.c (revision ad6fc754f3d0dc2047171b41cb4d6e13da3deb98)
1 /*-
2  * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/limits.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/bio.h>
38 #include <sys/sbuf.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/eventhandler.h>
42 #include <vm/uma.h>
43 #include <geom/geom.h>
44 #include <sys/proc.h>
45 #include <sys/kthread.h>
46 #include <sys/sched.h>
47 #include <geom/mirror/g_mirror.h>
48 
49 FEATURE(geom_mirror, "GEOM mirroring support");
50 
51 static MALLOC_DEFINE(M_MIRROR, "mirror_data", "GEOM_MIRROR Data");
52 
53 SYSCTL_DECL(_kern_geom);
54 static SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0,
55     "GEOM_MIRROR stuff");
56 u_int g_mirror_debug = 0;
57 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RWTUN, &g_mirror_debug, 0,
58     "Debug level");
59 static u_int g_mirror_timeout = 4;
60 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RWTUN, &g_mirror_timeout,
61     0, "Time to wait on all mirror components");
62 static u_int g_mirror_idletime = 5;
63 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RWTUN,
64     &g_mirror_idletime, 0, "Mark components as clean when idling");
65 static u_int g_mirror_disconnect_on_failure = 1;
66 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN,
67     &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
68 static u_int g_mirror_syncreqs = 2;
69 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
70     &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests.");
71 
72 #define	MSLEEP(ident, mtx, priority, wmesg, timeout)	do {		\
73 	G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));	\
74 	msleep((ident), (mtx), (priority), (wmesg), (timeout));		\
75 	G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident));	\
76 } while (0)
77 
78 static eventhandler_tag g_mirror_post_sync = NULL;
79 static int g_mirror_shutdown = 0;
80 
81 static int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp,
82     struct g_geom *gp);
83 static g_taste_t g_mirror_taste;
84 static g_resize_t g_mirror_resize;
85 static void g_mirror_init(struct g_class *mp);
86 static void g_mirror_fini(struct g_class *mp);
87 
88 struct g_class g_mirror_class = {
89 	.name = G_MIRROR_CLASS_NAME,
90 	.version = G_VERSION,
91 	.ctlreq = g_mirror_config,
92 	.taste = g_mirror_taste,
93 	.destroy_geom = g_mirror_destroy_geom,
94 	.init = g_mirror_init,
95 	.fini = g_mirror_fini,
96 	.resize = g_mirror_resize
97 };
98 
99 
100 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
101 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
102 static void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
103 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
104     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
105 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
106 static void g_mirror_register_request(struct bio *bp);
107 static void g_mirror_sync_release(struct g_mirror_softc *sc);
108 
109 
110 static const char *
111 g_mirror_disk_state2str(int state)
112 {
113 
114 	switch (state) {
115 	case G_MIRROR_DISK_STATE_NONE:
116 		return ("NONE");
117 	case G_MIRROR_DISK_STATE_NEW:
118 		return ("NEW");
119 	case G_MIRROR_DISK_STATE_ACTIVE:
120 		return ("ACTIVE");
121 	case G_MIRROR_DISK_STATE_STALE:
122 		return ("STALE");
123 	case G_MIRROR_DISK_STATE_SYNCHRONIZING:
124 		return ("SYNCHRONIZING");
125 	case G_MIRROR_DISK_STATE_DISCONNECTED:
126 		return ("DISCONNECTED");
127 	case G_MIRROR_DISK_STATE_DESTROY:
128 		return ("DESTROY");
129 	default:
130 		return ("INVALID");
131 	}
132 }
133 
134 static const char *
135 g_mirror_device_state2str(int state)
136 {
137 
138 	switch (state) {
139 	case G_MIRROR_DEVICE_STATE_STARTING:
140 		return ("STARTING");
141 	case G_MIRROR_DEVICE_STATE_RUNNING:
142 		return ("RUNNING");
143 	default:
144 		return ("INVALID");
145 	}
146 }
147 
148 static const char *
149 g_mirror_get_diskname(struct g_mirror_disk *disk)
150 {
151 
152 	if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
153 		return ("[unknown]");
154 	return (disk->d_name);
155 }
156 
157 /*
158  * --- Events handling functions ---
159  * Events in geom_mirror are used to maintain disks and device status
160  * from one thread to simplify locking.
161  */
162 static void
163 g_mirror_event_free(struct g_mirror_event *ep)
164 {
165 
166 	free(ep, M_MIRROR);
167 }
168 
169 int
170 g_mirror_event_send(void *arg, int state, int flags)
171 {
172 	struct g_mirror_softc *sc;
173 	struct g_mirror_disk *disk;
174 	struct g_mirror_event *ep;
175 	int error;
176 
177 	ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
178 	G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
179 	if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
180 		disk = NULL;
181 		sc = arg;
182 	} else {
183 		disk = arg;
184 		sc = disk->d_softc;
185 	}
186 	ep->e_disk = disk;
187 	ep->e_state = state;
188 	ep->e_flags = flags;
189 	ep->e_error = 0;
190 	mtx_lock(&sc->sc_events_mtx);
191 	TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
192 	mtx_unlock(&sc->sc_events_mtx);
193 	G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
194 	mtx_lock(&sc->sc_queue_mtx);
195 	wakeup(sc);
196 	mtx_unlock(&sc->sc_queue_mtx);
197 	if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
198 		return (0);
199 	sx_assert(&sc->sc_lock, SX_XLOCKED);
200 	G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
201 	sx_xunlock(&sc->sc_lock);
202 	while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
203 		mtx_lock(&sc->sc_events_mtx);
204 		MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
205 		    hz * 5);
206 	}
207 	error = ep->e_error;
208 	g_mirror_event_free(ep);
209 	sx_xlock(&sc->sc_lock);
210 	return (error);
211 }
212 
213 static struct g_mirror_event *
214 g_mirror_event_get(struct g_mirror_softc *sc)
215 {
216 	struct g_mirror_event *ep;
217 
218 	mtx_lock(&sc->sc_events_mtx);
219 	ep = TAILQ_FIRST(&sc->sc_events);
220 	mtx_unlock(&sc->sc_events_mtx);
221 	return (ep);
222 }
223 
224 static void
225 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
226 {
227 
228 	mtx_lock(&sc->sc_events_mtx);
229 	TAILQ_REMOVE(&sc->sc_events, ep, e_next);
230 	mtx_unlock(&sc->sc_events_mtx);
231 }
232 
233 static void
234 g_mirror_event_cancel(struct g_mirror_disk *disk)
235 {
236 	struct g_mirror_softc *sc;
237 	struct g_mirror_event *ep, *tmpep;
238 
239 	sc = disk->d_softc;
240 	sx_assert(&sc->sc_lock, SX_XLOCKED);
241 
242 	mtx_lock(&sc->sc_events_mtx);
243 	TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
244 		if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
245 			continue;
246 		if (ep->e_disk != disk)
247 			continue;
248 		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
249 		if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
250 			g_mirror_event_free(ep);
251 		else {
252 			ep->e_error = ECANCELED;
253 			wakeup(ep);
254 		}
255 	}
256 	mtx_unlock(&sc->sc_events_mtx);
257 }
258 
259 /*
260  * Return the number of disks in given state.
261  * If state is equal to -1, count all connected disks.
262  */
263 u_int
264 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
265 {
266 	struct g_mirror_disk *disk;
267 	u_int n = 0;
268 
269 	sx_assert(&sc->sc_lock, SX_LOCKED);
270 
271 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
272 		if (state == -1 || disk->d_state == state)
273 			n++;
274 	}
275 	return (n);
276 }
277 
278 /*
279  * Find a disk in mirror by its disk ID.
280  */
281 static struct g_mirror_disk *
282 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
283 {
284 	struct g_mirror_disk *disk;
285 
286 	sx_assert(&sc->sc_lock, SX_XLOCKED);
287 
288 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
289 		if (disk->d_id == id)
290 			return (disk);
291 	}
292 	return (NULL);
293 }
294 
295 static u_int
296 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
297 {
298 	struct bio *bp;
299 	u_int nreqs = 0;
300 
301 	mtx_lock(&sc->sc_queue_mtx);
302 	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
303 		if (bp->bio_from == cp)
304 			nreqs++;
305 	}
306 	mtx_unlock(&sc->sc_queue_mtx);
307 	return (nreqs);
308 }
309 
310 static int
311 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
312 {
313 
314 	if (cp->index > 0) {
315 		G_MIRROR_DEBUG(2,
316 		    "I/O requests for %s exist, can't destroy it now.",
317 		    cp->provider->name);
318 		return (1);
319 	}
320 	if (g_mirror_nrequests(sc, cp) > 0) {
321 		G_MIRROR_DEBUG(2,
322 		    "I/O requests for %s in queue, can't destroy it now.",
323 		    cp->provider->name);
324 		return (1);
325 	}
326 	return (0);
327 }
328 
329 static void
330 g_mirror_destroy_consumer(void *arg, int flags __unused)
331 {
332 	struct g_consumer *cp;
333 
334 	g_topology_assert();
335 
336 	cp = arg;
337 	G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
338 	g_detach(cp);
339 	g_destroy_consumer(cp);
340 }
341 
342 static void
343 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
344 {
345 	struct g_provider *pp;
346 	int retaste_wait;
347 
348 	g_topology_assert();
349 
350 	cp->private = NULL;
351 	if (g_mirror_is_busy(sc, cp))
352 		return;
353 	pp = cp->provider;
354 	retaste_wait = 0;
355 	if (cp->acw == 1) {
356 		if ((pp->geom->flags & G_GEOM_WITHER) == 0)
357 			retaste_wait = 1;
358 	}
359 	G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
360 	    -cp->acw, -cp->ace, 0);
361 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
362 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
363 	if (retaste_wait) {
364 		/*
365 		 * After retaste event was send (inside g_access()), we can send
366 		 * event to detach and destroy consumer.
367 		 * A class, which has consumer to the given provider connected
368 		 * will not receive retaste event for the provider.
369 		 * This is the way how I ignore retaste events when I close
370 		 * consumers opened for write: I detach and destroy consumer
371 		 * after retaste event is sent.
372 		 */
373 		g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
374 		return;
375 	}
376 	G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
377 	g_detach(cp);
378 	g_destroy_consumer(cp);
379 }
380 
381 static int
382 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
383 {
384 	struct g_consumer *cp;
385 	int error;
386 
387 	g_topology_assert_not();
388 	KASSERT(disk->d_consumer == NULL,
389 	    ("Disk already connected (device %s).", disk->d_softc->sc_name));
390 
391 	g_topology_lock();
392 	cp = g_new_consumer(disk->d_softc->sc_geom);
393 	cp->flags |= G_CF_DIRECT_RECEIVE;
394 	error = g_attach(cp, pp);
395 	if (error != 0) {
396 		g_destroy_consumer(cp);
397 		g_topology_unlock();
398 		return (error);
399 	}
400 	error = g_access(cp, 1, 1, 1);
401 	if (error != 0) {
402 		g_detach(cp);
403 		g_destroy_consumer(cp);
404 		g_topology_unlock();
405 		G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
406 		    pp->name, error);
407 		return (error);
408 	}
409 	g_topology_unlock();
410 	disk->d_consumer = cp;
411 	disk->d_consumer->private = disk;
412 	disk->d_consumer->index = 0;
413 
414 	G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
415 	return (0);
416 }
417 
418 static void
419 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
420 {
421 
422 	g_topology_assert();
423 
424 	if (cp == NULL)
425 		return;
426 	if (cp->provider != NULL)
427 		g_mirror_kill_consumer(sc, cp);
428 	else
429 		g_destroy_consumer(cp);
430 }
431 
432 /*
433  * Initialize disk. This means allocate memory, create consumer, attach it
434  * to the provider and open access (r1w1e1) to it.
435  */
436 static struct g_mirror_disk *
437 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
438     struct g_mirror_metadata *md, int *errorp)
439 {
440 	struct g_mirror_disk *disk;
441 	int i, error;
442 
443 	disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
444 	if (disk == NULL) {
445 		error = ENOMEM;
446 		goto fail;
447 	}
448 	disk->d_softc = sc;
449 	error = g_mirror_connect_disk(disk, pp);
450 	if (error != 0)
451 		goto fail;
452 	disk->d_id = md->md_did;
453 	disk->d_state = G_MIRROR_DISK_STATE_NONE;
454 	disk->d_priority = md->md_priority;
455 	disk->d_flags = md->md_dflags;
456 	error = g_getattr("GEOM::candelete", disk->d_consumer, &i);
457 	if (error == 0 && i != 0)
458 		disk->d_flags |= G_MIRROR_DISK_FLAG_CANDELETE;
459 	if (md->md_provider[0] != '\0')
460 		disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
461 	disk->d_sync.ds_consumer = NULL;
462 	disk->d_sync.ds_offset = md->md_sync_offset;
463 	disk->d_sync.ds_offset_done = md->md_sync_offset;
464 	disk->d_genid = md->md_genid;
465 	disk->d_sync.ds_syncid = md->md_syncid;
466 	if (errorp != NULL)
467 		*errorp = 0;
468 	return (disk);
469 fail:
470 	if (errorp != NULL)
471 		*errorp = error;
472 	if (disk != NULL)
473 		free(disk, M_MIRROR);
474 	return (NULL);
475 }
476 
477 static void
478 g_mirror_destroy_disk(struct g_mirror_disk *disk)
479 {
480 	struct g_mirror_softc *sc;
481 
482 	g_topology_assert_not();
483 	sc = disk->d_softc;
484 	sx_assert(&sc->sc_lock, SX_XLOCKED);
485 
486 	LIST_REMOVE(disk, d_next);
487 	g_mirror_event_cancel(disk);
488 	if (sc->sc_hint == disk)
489 		sc->sc_hint = NULL;
490 	switch (disk->d_state) {
491 	case G_MIRROR_DISK_STATE_SYNCHRONIZING:
492 		g_mirror_sync_stop(disk, 1);
493 		/* FALLTHROUGH */
494 	case G_MIRROR_DISK_STATE_NEW:
495 	case G_MIRROR_DISK_STATE_STALE:
496 	case G_MIRROR_DISK_STATE_ACTIVE:
497 		g_topology_lock();
498 		g_mirror_disconnect_consumer(sc, disk->d_consumer);
499 		g_topology_unlock();
500 		free(disk, M_MIRROR);
501 		break;
502 	default:
503 		KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
504 		    g_mirror_get_diskname(disk),
505 		    g_mirror_disk_state2str(disk->d_state)));
506 	}
507 }
508 
509 static void
510 g_mirror_destroy_device(struct g_mirror_softc *sc)
511 {
512 	struct g_mirror_disk *disk;
513 	struct g_mirror_event *ep;
514 	struct g_geom *gp;
515 	struct g_consumer *cp, *tmpcp;
516 
517 	g_topology_assert_not();
518 	sx_assert(&sc->sc_lock, SX_XLOCKED);
519 
520 	gp = sc->sc_geom;
521 	if (sc->sc_provider != NULL)
522 		g_mirror_destroy_provider(sc);
523 	for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
524 	    disk = LIST_FIRST(&sc->sc_disks)) {
525 		disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
526 		g_mirror_update_metadata(disk);
527 		g_mirror_destroy_disk(disk);
528 	}
529 	while ((ep = g_mirror_event_get(sc)) != NULL) {
530 		g_mirror_event_remove(sc, ep);
531 		if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
532 			g_mirror_event_free(ep);
533 		else {
534 			ep->e_error = ECANCELED;
535 			ep->e_flags |= G_MIRROR_EVENT_DONE;
536 			G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
537 			mtx_lock(&sc->sc_events_mtx);
538 			wakeup(ep);
539 			mtx_unlock(&sc->sc_events_mtx);
540 		}
541 	}
542 	callout_drain(&sc->sc_callout);
543 
544 	g_topology_lock();
545 	LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
546 		g_mirror_disconnect_consumer(sc, cp);
547 	}
548 	g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
549 	G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
550 	g_wither_geom(gp, ENXIO);
551 	g_topology_unlock();
552 	mtx_destroy(&sc->sc_queue_mtx);
553 	mtx_destroy(&sc->sc_events_mtx);
554 	mtx_destroy(&sc->sc_done_mtx);
555 	sx_xunlock(&sc->sc_lock);
556 	sx_destroy(&sc->sc_lock);
557 }
558 
559 static void
560 g_mirror_orphan(struct g_consumer *cp)
561 {
562 	struct g_mirror_disk *disk;
563 
564 	g_topology_assert();
565 
566 	disk = cp->private;
567 	if (disk == NULL)
568 		return;
569 	disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
570 	g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
571 	    G_MIRROR_EVENT_DONTWAIT);
572 }
573 
574 /*
575  * Function should return the next active disk on the list.
576  * It is possible that it will be the same disk as given.
577  * If there are no active disks on list, NULL is returned.
578  */
579 static __inline struct g_mirror_disk *
580 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
581 {
582 	struct g_mirror_disk *dp;
583 
584 	for (dp = LIST_NEXT(disk, d_next); dp != disk;
585 	    dp = LIST_NEXT(dp, d_next)) {
586 		if (dp == NULL)
587 			dp = LIST_FIRST(&sc->sc_disks);
588 		if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
589 			break;
590 	}
591 	if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
592 		return (NULL);
593 	return (dp);
594 }
595 
596 static struct g_mirror_disk *
597 g_mirror_get_disk(struct g_mirror_softc *sc)
598 {
599 	struct g_mirror_disk *disk;
600 
601 	if (sc->sc_hint == NULL) {
602 		sc->sc_hint = LIST_FIRST(&sc->sc_disks);
603 		if (sc->sc_hint == NULL)
604 			return (NULL);
605 	}
606 	disk = sc->sc_hint;
607 	if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
608 		disk = g_mirror_find_next(sc, disk);
609 		if (disk == NULL)
610 			return (NULL);
611 	}
612 	sc->sc_hint = g_mirror_find_next(sc, disk);
613 	return (disk);
614 }
615 
616 static int
617 g_mirror_write_metadata(struct g_mirror_disk *disk,
618     struct g_mirror_metadata *md)
619 {
620 	struct g_mirror_softc *sc;
621 	struct g_consumer *cp;
622 	off_t offset, length;
623 	u_char *sector;
624 	int error = 0;
625 
626 	g_topology_assert_not();
627 	sc = disk->d_softc;
628 	sx_assert(&sc->sc_lock, SX_LOCKED);
629 
630 	cp = disk->d_consumer;
631 	KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
632 	KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
633 	KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
634 	    ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
635 	    cp->acw, cp->ace));
636 	length = cp->provider->sectorsize;
637 	offset = cp->provider->mediasize - length;
638 	sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
639 	if (md != NULL &&
640 	    (sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0) {
641 		/*
642 		 * Handle the case, when the size of parent provider reduced.
643 		 */
644 		if (offset < md->md_mediasize)
645 			error = ENOSPC;
646 		else
647 			mirror_metadata_encode(md, sector);
648 	}
649 	if (error == 0)
650 		error = g_write_data(cp, offset, sector, length);
651 	free(sector, M_MIRROR);
652 	if (error != 0) {
653 		if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
654 			disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
655 			G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
656 			    "(device=%s, error=%d).",
657 			    g_mirror_get_diskname(disk), sc->sc_name, error);
658 		} else {
659 			G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
660 			    "(device=%s, error=%d).",
661 			    g_mirror_get_diskname(disk), sc->sc_name, error);
662 		}
663 		if (g_mirror_disconnect_on_failure &&
664 		    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
665 			sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
666 			g_mirror_event_send(disk,
667 			    G_MIRROR_DISK_STATE_DISCONNECTED,
668 			    G_MIRROR_EVENT_DONTWAIT);
669 		}
670 	}
671 	return (error);
672 }
673 
674 static int
675 g_mirror_clear_metadata(struct g_mirror_disk *disk)
676 {
677 	int error;
678 
679 	g_topology_assert_not();
680 	sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
681 
682 	error = g_mirror_write_metadata(disk, NULL);
683 	if (error == 0) {
684 		G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
685 		    g_mirror_get_diskname(disk));
686 	} else {
687 		G_MIRROR_DEBUG(0,
688 		    "Cannot clear metadata on disk %s (error=%d).",
689 		    g_mirror_get_diskname(disk), error);
690 	}
691 	return (error);
692 }
693 
694 void
695 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
696     struct g_mirror_metadata *md)
697 {
698 
699 	strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
700 	md->md_version = G_MIRROR_VERSION;
701 	strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
702 	md->md_mid = sc->sc_id;
703 	md->md_all = sc->sc_ndisks;
704 	md->md_slice = sc->sc_slice;
705 	md->md_balance = sc->sc_balance;
706 	md->md_genid = sc->sc_genid;
707 	md->md_mediasize = sc->sc_mediasize;
708 	md->md_sectorsize = sc->sc_sectorsize;
709 	md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
710 	bzero(md->md_provider, sizeof(md->md_provider));
711 	if (disk == NULL) {
712 		md->md_did = arc4random();
713 		md->md_priority = 0;
714 		md->md_syncid = 0;
715 		md->md_dflags = 0;
716 		md->md_sync_offset = 0;
717 		md->md_provsize = 0;
718 	} else {
719 		md->md_did = disk->d_id;
720 		md->md_priority = disk->d_priority;
721 		md->md_syncid = disk->d_sync.ds_syncid;
722 		md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
723 		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
724 			md->md_sync_offset = disk->d_sync.ds_offset_done;
725 		else
726 			md->md_sync_offset = 0;
727 		if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
728 			strlcpy(md->md_provider,
729 			    disk->d_consumer->provider->name,
730 			    sizeof(md->md_provider));
731 		}
732 		md->md_provsize = disk->d_consumer->provider->mediasize;
733 	}
734 }
735 
736 void
737 g_mirror_update_metadata(struct g_mirror_disk *disk)
738 {
739 	struct g_mirror_softc *sc;
740 	struct g_mirror_metadata md;
741 	int error;
742 
743 	g_topology_assert_not();
744 	sc = disk->d_softc;
745 	sx_assert(&sc->sc_lock, SX_LOCKED);
746 
747 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0)
748 		g_mirror_fill_metadata(sc, disk, &md);
749 	error = g_mirror_write_metadata(disk, &md);
750 	if (error == 0) {
751 		G_MIRROR_DEBUG(2, "Metadata on %s updated.",
752 		    g_mirror_get_diskname(disk));
753 	} else {
754 		G_MIRROR_DEBUG(0,
755 		    "Cannot update metadata on disk %s (error=%d).",
756 		    g_mirror_get_diskname(disk), error);
757 	}
758 }
759 
760 static void
761 g_mirror_bump_syncid(struct g_mirror_softc *sc)
762 {
763 	struct g_mirror_disk *disk;
764 
765 	g_topology_assert_not();
766 	sx_assert(&sc->sc_lock, SX_XLOCKED);
767 	KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
768 	    ("%s called with no active disks (device=%s).", __func__,
769 	    sc->sc_name));
770 
771 	sc->sc_syncid++;
772 	G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
773 	    sc->sc_syncid);
774 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
775 		if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
776 		    disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
777 			disk->d_sync.ds_syncid = sc->sc_syncid;
778 			g_mirror_update_metadata(disk);
779 		}
780 	}
781 }
782 
783 static void
784 g_mirror_bump_genid(struct g_mirror_softc *sc)
785 {
786 	struct g_mirror_disk *disk;
787 
788 	g_topology_assert_not();
789 	sx_assert(&sc->sc_lock, SX_XLOCKED);
790 	KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
791 	    ("%s called with no active disks (device=%s).", __func__,
792 	    sc->sc_name));
793 
794 	sc->sc_genid++;
795 	G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
796 	    sc->sc_genid);
797 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
798 		if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
799 		    disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
800 			disk->d_genid = sc->sc_genid;
801 			g_mirror_update_metadata(disk);
802 		}
803 	}
804 }
805 
806 static int
807 g_mirror_idle(struct g_mirror_softc *sc, int acw)
808 {
809 	struct g_mirror_disk *disk;
810 	int timeout;
811 
812 	g_topology_assert_not();
813 	sx_assert(&sc->sc_lock, SX_XLOCKED);
814 
815 	if (sc->sc_provider == NULL)
816 		return (0);
817 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
818 		return (0);
819 	if (sc->sc_idle)
820 		return (0);
821 	if (sc->sc_writes > 0)
822 		return (0);
823 	if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
824 		timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
825 		if (!g_mirror_shutdown && timeout > 0)
826 			return (timeout);
827 	}
828 	sc->sc_idle = 1;
829 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
830 		if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
831 			continue;
832 		G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
833 		    g_mirror_get_diskname(disk), sc->sc_name);
834 		disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
835 		g_mirror_update_metadata(disk);
836 	}
837 	return (0);
838 }
839 
840 static void
841 g_mirror_unidle(struct g_mirror_softc *sc)
842 {
843 	struct g_mirror_disk *disk;
844 
845 	g_topology_assert_not();
846 	sx_assert(&sc->sc_lock, SX_XLOCKED);
847 
848 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
849 		return;
850 	sc->sc_idle = 0;
851 	sc->sc_last_write = time_uptime;
852 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
853 		if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
854 			continue;
855 		G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
856 		    g_mirror_get_diskname(disk), sc->sc_name);
857 		disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
858 		g_mirror_update_metadata(disk);
859 	}
860 }
861 
862 static void
863 g_mirror_flush_done(struct bio *bp)
864 {
865 	struct g_mirror_softc *sc;
866 	struct bio *pbp;
867 
868 	pbp = bp->bio_parent;
869 	sc = pbp->bio_to->geom->softc;
870 	mtx_lock(&sc->sc_done_mtx);
871 	if (pbp->bio_error == 0)
872 		pbp->bio_error = bp->bio_error;
873 	pbp->bio_completed += bp->bio_completed;
874 	pbp->bio_inbed++;
875 	if (pbp->bio_children == pbp->bio_inbed) {
876 		mtx_unlock(&sc->sc_done_mtx);
877 		g_io_deliver(pbp, pbp->bio_error);
878 	} else
879 		mtx_unlock(&sc->sc_done_mtx);
880 	g_destroy_bio(bp);
881 }
882 
883 static void
884 g_mirror_done(struct bio *bp)
885 {
886 	struct g_mirror_softc *sc;
887 
888 	sc = bp->bio_from->geom->softc;
889 	bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
890 	mtx_lock(&sc->sc_queue_mtx);
891 	bioq_disksort(&sc->sc_queue, bp);
892 	mtx_unlock(&sc->sc_queue_mtx);
893 	wakeup(sc);
894 }
895 
896 static void
897 g_mirror_regular_request(struct bio *bp)
898 {
899 	struct g_mirror_softc *sc;
900 	struct g_mirror_disk *disk;
901 	struct bio *pbp;
902 
903 	g_topology_assert_not();
904 
905 	pbp = bp->bio_parent;
906 	sc = pbp->bio_to->geom->softc;
907 	bp->bio_from->index--;
908 	if (bp->bio_cmd == BIO_WRITE)
909 		sc->sc_writes--;
910 	disk = bp->bio_from->private;
911 	if (disk == NULL) {
912 		g_topology_lock();
913 		g_mirror_kill_consumer(sc, bp->bio_from);
914 		g_topology_unlock();
915 	}
916 
917 	pbp->bio_inbed++;
918 	KASSERT(pbp->bio_inbed <= pbp->bio_children,
919 	    ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
920 	    pbp->bio_children));
921 	if (bp->bio_error == 0 && pbp->bio_error == 0) {
922 		G_MIRROR_LOGREQ(3, bp, "Request delivered.");
923 		g_destroy_bio(bp);
924 		if (pbp->bio_children == pbp->bio_inbed) {
925 			G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
926 			pbp->bio_completed = pbp->bio_length;
927 			if (pbp->bio_cmd == BIO_WRITE ||
928 			    pbp->bio_cmd == BIO_DELETE) {
929 				bioq_remove(&sc->sc_inflight, pbp);
930 				/* Release delayed sync requests if possible. */
931 				g_mirror_sync_release(sc);
932 			}
933 			g_io_deliver(pbp, pbp->bio_error);
934 		}
935 		return;
936 	} else if (bp->bio_error != 0) {
937 		if (pbp->bio_error == 0)
938 			pbp->bio_error = bp->bio_error;
939 		if (disk != NULL) {
940 			if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
941 				disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
942 				G_MIRROR_LOGREQ(0, bp,
943 				    "Request failed (error=%d).",
944 				    bp->bio_error);
945 			} else {
946 				G_MIRROR_LOGREQ(1, bp,
947 				    "Request failed (error=%d).",
948 				    bp->bio_error);
949 			}
950 			if (g_mirror_disconnect_on_failure &&
951 			    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
952 			{
953 				sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
954 				g_mirror_event_send(disk,
955 				    G_MIRROR_DISK_STATE_DISCONNECTED,
956 				    G_MIRROR_EVENT_DONTWAIT);
957 			}
958 		}
959 		switch (pbp->bio_cmd) {
960 		case BIO_DELETE:
961 		case BIO_WRITE:
962 			pbp->bio_inbed--;
963 			pbp->bio_children--;
964 			break;
965 		}
966 	}
967 	g_destroy_bio(bp);
968 
969 	switch (pbp->bio_cmd) {
970 	case BIO_READ:
971 		if (pbp->bio_inbed < pbp->bio_children)
972 			break;
973 		if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
974 			g_io_deliver(pbp, pbp->bio_error);
975 		else {
976 			pbp->bio_error = 0;
977 			mtx_lock(&sc->sc_queue_mtx);
978 			bioq_disksort(&sc->sc_queue, pbp);
979 			mtx_unlock(&sc->sc_queue_mtx);
980 			G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
981 			wakeup(sc);
982 		}
983 		break;
984 	case BIO_DELETE:
985 	case BIO_WRITE:
986 		if (pbp->bio_children == 0) {
987 			/*
988 			 * All requests failed.
989 			 */
990 		} else if (pbp->bio_inbed < pbp->bio_children) {
991 			/* Do nothing. */
992 			break;
993 		} else if (pbp->bio_children == pbp->bio_inbed) {
994 			/* Some requests succeeded. */
995 			pbp->bio_error = 0;
996 			pbp->bio_completed = pbp->bio_length;
997 		}
998 		bioq_remove(&sc->sc_inflight, pbp);
999 		/* Release delayed sync requests if possible. */
1000 		g_mirror_sync_release(sc);
1001 		g_io_deliver(pbp, pbp->bio_error);
1002 		break;
1003 	default:
1004 		KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
1005 		break;
1006 	}
1007 }
1008 
1009 static void
1010 g_mirror_sync_done(struct bio *bp)
1011 {
1012 	struct g_mirror_softc *sc;
1013 
1014 	G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
1015 	sc = bp->bio_from->geom->softc;
1016 	bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
1017 	mtx_lock(&sc->sc_queue_mtx);
1018 	bioq_disksort(&sc->sc_queue, bp);
1019 	mtx_unlock(&sc->sc_queue_mtx);
1020 	wakeup(sc);
1021 }
1022 
1023 static void
1024 g_mirror_candelete(struct bio *bp)
1025 {
1026 	struct g_mirror_softc *sc;
1027 	struct g_mirror_disk *disk;
1028 	int *val;
1029 
1030 	sc = bp->bio_to->geom->softc;
1031 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1032 		if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE)
1033 			break;
1034 	}
1035 	val = (int *)bp->bio_data;
1036 	*val = (disk != NULL);
1037 	g_io_deliver(bp, 0);
1038 }
1039 
1040 static void
1041 g_mirror_kernel_dump(struct bio *bp)
1042 {
1043 	struct g_mirror_softc *sc;
1044 	struct g_mirror_disk *disk;
1045 	struct bio *cbp;
1046 	struct g_kerneldump *gkd;
1047 
1048 	/*
1049 	 * We configure dumping to the first component, because this component
1050 	 * will be used for reading with 'prefer' balance algorithm.
1051 	 * If the component with the higest priority is currently disconnected
1052 	 * we will not be able to read the dump after the reboot if it will be
1053 	 * connected and synchronized later. Can we do something better?
1054 	 */
1055 	sc = bp->bio_to->geom->softc;
1056 	disk = LIST_FIRST(&sc->sc_disks);
1057 
1058 	gkd = (struct g_kerneldump *)bp->bio_data;
1059 	if (gkd->length > bp->bio_to->mediasize)
1060 		gkd->length = bp->bio_to->mediasize;
1061 	cbp = g_clone_bio(bp);
1062 	if (cbp == NULL) {
1063 		g_io_deliver(bp, ENOMEM);
1064 		return;
1065 	}
1066 	cbp->bio_done = g_std_done;
1067 	g_io_request(cbp, disk->d_consumer);
1068 	G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1069 	    g_mirror_get_diskname(disk));
1070 }
1071 
1072 static void
1073 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1074 {
1075 	struct bio_queue_head queue;
1076 	struct g_mirror_disk *disk;
1077 	struct g_consumer *cp;
1078 	struct bio *cbp;
1079 
1080 	bioq_init(&queue);
1081 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1082 		if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1083 			continue;
1084 		cbp = g_clone_bio(bp);
1085 		if (cbp == NULL) {
1086 			while ((cbp = bioq_takefirst(&queue)) != NULL)
1087 				g_destroy_bio(cbp);
1088 			if (bp->bio_error == 0)
1089 				bp->bio_error = ENOMEM;
1090 			g_io_deliver(bp, bp->bio_error);
1091 			return;
1092 		}
1093 		bioq_insert_tail(&queue, cbp);
1094 		cbp->bio_done = g_mirror_flush_done;
1095 		cbp->bio_caller1 = disk;
1096 		cbp->bio_to = disk->d_consumer->provider;
1097 	}
1098 	while ((cbp = bioq_takefirst(&queue)) != NULL) {
1099 		G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1100 		disk = cbp->bio_caller1;
1101 		cbp->bio_caller1 = NULL;
1102 		cp = disk->d_consumer;
1103 		KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1104 		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1105 		    cp->acr, cp->acw, cp->ace));
1106 		g_io_request(cbp, disk->d_consumer);
1107 	}
1108 }
1109 
1110 static void
1111 g_mirror_start(struct bio *bp)
1112 {
1113 	struct g_mirror_softc *sc;
1114 
1115 	sc = bp->bio_to->geom->softc;
1116 	/*
1117 	 * If sc == NULL or there are no valid disks, provider's error
1118 	 * should be set and g_mirror_start() should not be called at all.
1119 	 */
1120 	KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1121 	    ("Provider's error should be set (error=%d)(mirror=%s).",
1122 	    bp->bio_to->error, bp->bio_to->name));
1123 	G_MIRROR_LOGREQ(3, bp, "Request received.");
1124 
1125 	switch (bp->bio_cmd) {
1126 	case BIO_READ:
1127 	case BIO_WRITE:
1128 	case BIO_DELETE:
1129 		break;
1130 	case BIO_FLUSH:
1131 		g_mirror_flush(sc, bp);
1132 		return;
1133 	case BIO_GETATTR:
1134 		if (!strcmp(bp->bio_attribute, "GEOM::candelete")) {
1135 			g_mirror_candelete(bp);
1136 			return;
1137 		} else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1138 			g_mirror_kernel_dump(bp);
1139 			return;
1140 		}
1141 		/* FALLTHROUGH */
1142 	default:
1143 		g_io_deliver(bp, EOPNOTSUPP);
1144 		return;
1145 	}
1146 	mtx_lock(&sc->sc_queue_mtx);
1147 	bioq_disksort(&sc->sc_queue, bp);
1148 	mtx_unlock(&sc->sc_queue_mtx);
1149 	G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1150 	wakeup(sc);
1151 }
1152 
1153 /*
1154  * Return TRUE if the given request is colliding with a in-progress
1155  * synchronization request.
1156  */
1157 static int
1158 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1159 {
1160 	struct g_mirror_disk *disk;
1161 	struct bio *sbp;
1162 	off_t rstart, rend, sstart, send;
1163 	int i;
1164 
1165 	if (sc->sc_sync.ds_ndisks == 0)
1166 		return (0);
1167 	rstart = bp->bio_offset;
1168 	rend = bp->bio_offset + bp->bio_length;
1169 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1170 		if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1171 			continue;
1172 		for (i = 0; i < g_mirror_syncreqs; i++) {
1173 			sbp = disk->d_sync.ds_bios[i];
1174 			if (sbp == NULL)
1175 				continue;
1176 			sstart = sbp->bio_offset;
1177 			send = sbp->bio_offset + sbp->bio_length;
1178 			if (rend > sstart && rstart < send)
1179 				return (1);
1180 		}
1181 	}
1182 	return (0);
1183 }
1184 
1185 /*
1186  * Return TRUE if the given sync request is colliding with a in-progress regular
1187  * request.
1188  */
1189 static int
1190 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1191 {
1192 	off_t rstart, rend, sstart, send;
1193 	struct bio *bp;
1194 
1195 	if (sc->sc_sync.ds_ndisks == 0)
1196 		return (0);
1197 	sstart = sbp->bio_offset;
1198 	send = sbp->bio_offset + sbp->bio_length;
1199 	TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1200 		rstart = bp->bio_offset;
1201 		rend = bp->bio_offset + bp->bio_length;
1202 		if (rend > sstart && rstart < send)
1203 			return (1);
1204 	}
1205 	return (0);
1206 }
1207 
1208 /*
1209  * Puts request onto delayed queue.
1210  */
1211 static void
1212 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1213 {
1214 
1215 	G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1216 	bioq_insert_head(&sc->sc_regular_delayed, bp);
1217 }
1218 
1219 /*
1220  * Puts synchronization request onto delayed queue.
1221  */
1222 static void
1223 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1224 {
1225 
1226 	G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1227 	bioq_insert_tail(&sc->sc_sync_delayed, bp);
1228 }
1229 
1230 /*
1231  * Releases delayed regular requests which don't collide anymore with sync
1232  * requests.
1233  */
1234 static void
1235 g_mirror_regular_release(struct g_mirror_softc *sc)
1236 {
1237 	struct bio *bp, *bp2;
1238 
1239 	TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1240 		if (g_mirror_sync_collision(sc, bp))
1241 			continue;
1242 		bioq_remove(&sc->sc_regular_delayed, bp);
1243 		G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1244 		mtx_lock(&sc->sc_queue_mtx);
1245 		bioq_insert_head(&sc->sc_queue, bp);
1246 #if 0
1247 		/*
1248 		 * wakeup() is not needed, because this function is called from
1249 		 * the worker thread.
1250 		 */
1251 		wakeup(&sc->sc_queue);
1252 #endif
1253 		mtx_unlock(&sc->sc_queue_mtx);
1254 	}
1255 }
1256 
1257 /*
1258  * Releases delayed sync requests which don't collide anymore with regular
1259  * requests.
1260  */
1261 static void
1262 g_mirror_sync_release(struct g_mirror_softc *sc)
1263 {
1264 	struct bio *bp, *bp2;
1265 
1266 	TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1267 		if (g_mirror_regular_collision(sc, bp))
1268 			continue;
1269 		bioq_remove(&sc->sc_sync_delayed, bp);
1270 		G_MIRROR_LOGREQ(2, bp,
1271 		    "Releasing delayed synchronization request.");
1272 		g_io_request(bp, bp->bio_from);
1273 	}
1274 }
1275 
1276 /*
1277  * Handle synchronization requests.
1278  * Every synchronization request is two-steps process: first, READ request is
1279  * send to active provider and then WRITE request (with read data) to the provider
1280  * beeing synchronized. When WRITE is finished, new synchronization request is
1281  * send.
1282  */
1283 static void
1284 g_mirror_sync_request(struct bio *bp)
1285 {
1286 	struct g_mirror_softc *sc;
1287 	struct g_mirror_disk *disk;
1288 
1289 	bp->bio_from->index--;
1290 	sc = bp->bio_from->geom->softc;
1291 	disk = bp->bio_from->private;
1292 	if (disk == NULL) {
1293 		sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1294 		g_topology_lock();
1295 		g_mirror_kill_consumer(sc, bp->bio_from);
1296 		g_topology_unlock();
1297 		free(bp->bio_data, M_MIRROR);
1298 		g_destroy_bio(bp);
1299 		sx_xlock(&sc->sc_lock);
1300 		return;
1301 	}
1302 
1303 	/*
1304 	 * Synchronization request.
1305 	 */
1306 	switch (bp->bio_cmd) {
1307 	case BIO_READ:
1308 	    {
1309 		struct g_consumer *cp;
1310 
1311 		if (bp->bio_error != 0) {
1312 			G_MIRROR_LOGREQ(0, bp,
1313 			    "Synchronization request failed (error=%d).",
1314 			    bp->bio_error);
1315 			g_destroy_bio(bp);
1316 			return;
1317 		}
1318 		G_MIRROR_LOGREQ(3, bp,
1319 		    "Synchronization request half-finished.");
1320 		bp->bio_cmd = BIO_WRITE;
1321 		bp->bio_cflags = 0;
1322 		cp = disk->d_consumer;
1323 		KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1324 		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1325 		    cp->acr, cp->acw, cp->ace));
1326 		cp->index++;
1327 		g_io_request(bp, cp);
1328 		return;
1329 	    }
1330 	case BIO_WRITE:
1331 	    {
1332 		struct g_mirror_disk_sync *sync;
1333 		off_t offset;
1334 		void *data;
1335 		int i;
1336 
1337 		if (bp->bio_error != 0) {
1338 			G_MIRROR_LOGREQ(0, bp,
1339 			    "Synchronization request failed (error=%d).",
1340 			    bp->bio_error);
1341 			g_destroy_bio(bp);
1342 			sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1343 			g_mirror_event_send(disk,
1344 			    G_MIRROR_DISK_STATE_DISCONNECTED,
1345 			    G_MIRROR_EVENT_DONTWAIT);
1346 			return;
1347 		}
1348 		G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1349 		sync = &disk->d_sync;
1350 		if (sync->ds_offset >= sc->sc_mediasize ||
1351 		    sync->ds_consumer == NULL ||
1352 		    (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1353 			/* Don't send more synchronization requests. */
1354 			sync->ds_inflight--;
1355 			if (sync->ds_bios != NULL) {
1356 				i = (int)(uintptr_t)bp->bio_caller1;
1357 				sync->ds_bios[i] = NULL;
1358 			}
1359 			free(bp->bio_data, M_MIRROR);
1360 			g_destroy_bio(bp);
1361 			if (sync->ds_inflight > 0)
1362 				return;
1363 			if (sync->ds_consumer == NULL ||
1364 			    (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1365 				return;
1366 			}
1367 			/* Disk up-to-date, activate it. */
1368 			g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1369 			    G_MIRROR_EVENT_DONTWAIT);
1370 			return;
1371 		}
1372 
1373 		/* Send next synchronization request. */
1374 		data = bp->bio_data;
1375 		bzero(bp, sizeof(*bp));
1376 		bp->bio_cmd = BIO_READ;
1377 		bp->bio_offset = sync->ds_offset;
1378 		bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1379 		sync->ds_offset += bp->bio_length;
1380 		bp->bio_done = g_mirror_sync_done;
1381 		bp->bio_data = data;
1382 		bp->bio_from = sync->ds_consumer;
1383 		bp->bio_to = sc->sc_provider;
1384 		G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1385 		sync->ds_consumer->index++;
1386 		/*
1387 		 * Delay the request if it is colliding with a regular request.
1388 		 */
1389 		if (g_mirror_regular_collision(sc, bp))
1390 			g_mirror_sync_delay(sc, bp);
1391 		else
1392 			g_io_request(bp, sync->ds_consumer);
1393 
1394 		/* Release delayed requests if possible. */
1395 		g_mirror_regular_release(sc);
1396 
1397 		/* Find the smallest offset */
1398 		offset = sc->sc_mediasize;
1399 		for (i = 0; i < g_mirror_syncreqs; i++) {
1400 			bp = sync->ds_bios[i];
1401 			if (bp->bio_offset < offset)
1402 				offset = bp->bio_offset;
1403 		}
1404 		if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1405 			/* Update offset_done on every 100 blocks. */
1406 			sync->ds_offset_done = offset;
1407 			g_mirror_update_metadata(disk);
1408 		}
1409 		return;
1410 	    }
1411 	default:
1412 		KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1413 		    bp->bio_cmd, sc->sc_name));
1414 		break;
1415 	}
1416 }
1417 
1418 static void
1419 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1420 {
1421 	struct g_mirror_disk *disk;
1422 	struct g_consumer *cp;
1423 	struct bio *cbp;
1424 
1425 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1426 		if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1427 			break;
1428 	}
1429 	if (disk == NULL) {
1430 		if (bp->bio_error == 0)
1431 			bp->bio_error = ENXIO;
1432 		g_io_deliver(bp, bp->bio_error);
1433 		return;
1434 	}
1435 	cbp = g_clone_bio(bp);
1436 	if (cbp == NULL) {
1437 		if (bp->bio_error == 0)
1438 			bp->bio_error = ENOMEM;
1439 		g_io_deliver(bp, bp->bio_error);
1440 		return;
1441 	}
1442 	/*
1443 	 * Fill in the component buf structure.
1444 	 */
1445 	cp = disk->d_consumer;
1446 	cbp->bio_done = g_mirror_done;
1447 	cbp->bio_to = cp->provider;
1448 	G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1449 	KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1450 	    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1451 	    cp->acw, cp->ace));
1452 	cp->index++;
1453 	g_io_request(cbp, cp);
1454 }
1455 
1456 static void
1457 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1458 {
1459 	struct g_mirror_disk *disk;
1460 	struct g_consumer *cp;
1461 	struct bio *cbp;
1462 
1463 	disk = g_mirror_get_disk(sc);
1464 	if (disk == NULL) {
1465 		if (bp->bio_error == 0)
1466 			bp->bio_error = ENXIO;
1467 		g_io_deliver(bp, bp->bio_error);
1468 		return;
1469 	}
1470 	cbp = g_clone_bio(bp);
1471 	if (cbp == NULL) {
1472 		if (bp->bio_error == 0)
1473 			bp->bio_error = ENOMEM;
1474 		g_io_deliver(bp, bp->bio_error);
1475 		return;
1476 	}
1477 	/*
1478 	 * Fill in the component buf structure.
1479 	 */
1480 	cp = disk->d_consumer;
1481 	cbp->bio_done = g_mirror_done;
1482 	cbp->bio_to = cp->provider;
1483 	G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1484 	KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1485 	    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1486 	    cp->acw, cp->ace));
1487 	cp->index++;
1488 	g_io_request(cbp, cp);
1489 }
1490 
1491 #define TRACK_SIZE  (1 * 1024 * 1024)
1492 #define LOAD_SCALE	256
1493 #define ABS(x)		(((x) >= 0) ? (x) : (-(x)))
1494 
1495 static void
1496 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1497 {
1498 	struct g_mirror_disk *disk, *dp;
1499 	struct g_consumer *cp;
1500 	struct bio *cbp;
1501 	int prio, best;
1502 
1503 	/* Find a disk with the smallest load. */
1504 	disk = NULL;
1505 	best = INT_MAX;
1506 	LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1507 		if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1508 			continue;
1509 		prio = dp->load;
1510 		/* If disk head is precisely in position - highly prefer it. */
1511 		if (dp->d_last_offset == bp->bio_offset)
1512 			prio -= 2 * LOAD_SCALE;
1513 		else
1514 		/* If disk head is close to position - prefer it. */
1515 		if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1516 			prio -= 1 * LOAD_SCALE;
1517 		if (prio <= best) {
1518 			disk = dp;
1519 			best = prio;
1520 		}
1521 	}
1522 	KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1523 	cbp = g_clone_bio(bp);
1524 	if (cbp == NULL) {
1525 		if (bp->bio_error == 0)
1526 			bp->bio_error = ENOMEM;
1527 		g_io_deliver(bp, bp->bio_error);
1528 		return;
1529 	}
1530 	/*
1531 	 * Fill in the component buf structure.
1532 	 */
1533 	cp = disk->d_consumer;
1534 	cbp->bio_done = g_mirror_done;
1535 	cbp->bio_to = cp->provider;
1536 	G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1537 	KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1538 	    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1539 	    cp->acw, cp->ace));
1540 	cp->index++;
1541 	/* Remember last head position */
1542 	disk->d_last_offset = bp->bio_offset + bp->bio_length;
1543 	/* Update loads. */
1544 	LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1545 		dp->load = (dp->d_consumer->index * LOAD_SCALE +
1546 		    dp->load * 7) / 8;
1547 	}
1548 	g_io_request(cbp, cp);
1549 }
1550 
1551 static void
1552 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1553 {
1554 	struct bio_queue_head queue;
1555 	struct g_mirror_disk *disk;
1556 	struct g_consumer *cp;
1557 	struct bio *cbp;
1558 	off_t left, mod, offset, slice;
1559 	u_char *data;
1560 	u_int ndisks;
1561 
1562 	if (bp->bio_length <= sc->sc_slice) {
1563 		g_mirror_request_round_robin(sc, bp);
1564 		return;
1565 	}
1566 	ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1567 	slice = bp->bio_length / ndisks;
1568 	mod = slice % sc->sc_provider->sectorsize;
1569 	if (mod != 0)
1570 		slice += sc->sc_provider->sectorsize - mod;
1571 	/*
1572 	 * Allocate all bios before sending any request, so we can
1573 	 * return ENOMEM in nice and clean way.
1574 	 */
1575 	left = bp->bio_length;
1576 	offset = bp->bio_offset;
1577 	data = bp->bio_data;
1578 	bioq_init(&queue);
1579 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1580 		if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1581 			continue;
1582 		cbp = g_clone_bio(bp);
1583 		if (cbp == NULL) {
1584 			while ((cbp = bioq_takefirst(&queue)) != NULL)
1585 				bioq_remove(&queue, cbp);
1586 			if (bp->bio_error == 0)
1587 				bp->bio_error = ENOMEM;
1588 			g_io_deliver(bp, bp->bio_error);
1589 			return;
1590 		}
1591 		bioq_insert_tail(&queue, cbp);
1592 		cbp->bio_done = g_mirror_done;
1593 		cbp->bio_caller1 = disk;
1594 		cbp->bio_to = disk->d_consumer->provider;
1595 		cbp->bio_offset = offset;
1596 		cbp->bio_data = data;
1597 		cbp->bio_length = MIN(left, slice);
1598 		left -= cbp->bio_length;
1599 		if (left == 0)
1600 			break;
1601 		offset += cbp->bio_length;
1602 		data += cbp->bio_length;
1603 	}
1604 	while ((cbp = bioq_takefirst(&queue)) != NULL) {
1605 		G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1606 		disk = cbp->bio_caller1;
1607 		cbp->bio_caller1 = NULL;
1608 		cp = disk->d_consumer;
1609 		KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1610 		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1611 		    cp->acr, cp->acw, cp->ace));
1612 		disk->d_consumer->index++;
1613 		g_io_request(cbp, disk->d_consumer);
1614 	}
1615 }
1616 
1617 static void
1618 g_mirror_register_request(struct bio *bp)
1619 {
1620 	struct g_mirror_softc *sc;
1621 
1622 	sc = bp->bio_to->geom->softc;
1623 	switch (bp->bio_cmd) {
1624 	case BIO_READ:
1625 		switch (sc->sc_balance) {
1626 		case G_MIRROR_BALANCE_LOAD:
1627 			g_mirror_request_load(sc, bp);
1628 			break;
1629 		case G_MIRROR_BALANCE_PREFER:
1630 			g_mirror_request_prefer(sc, bp);
1631 			break;
1632 		case G_MIRROR_BALANCE_ROUND_ROBIN:
1633 			g_mirror_request_round_robin(sc, bp);
1634 			break;
1635 		case G_MIRROR_BALANCE_SPLIT:
1636 			g_mirror_request_split(sc, bp);
1637 			break;
1638 		}
1639 		return;
1640 	case BIO_WRITE:
1641 	case BIO_DELETE:
1642 	    {
1643 		struct g_mirror_disk *disk;
1644 		struct g_mirror_disk_sync *sync;
1645 		struct bio_queue_head queue;
1646 		struct g_consumer *cp;
1647 		struct bio *cbp;
1648 
1649 		/*
1650 		 * Delay the request if it is colliding with a synchronization
1651 		 * request.
1652 		 */
1653 		if (g_mirror_sync_collision(sc, bp)) {
1654 			g_mirror_regular_delay(sc, bp);
1655 			return;
1656 		}
1657 
1658 		if (sc->sc_idle)
1659 			g_mirror_unidle(sc);
1660 		else
1661 			sc->sc_last_write = time_uptime;
1662 
1663 		/*
1664 		 * Allocate all bios before sending any request, so we can
1665 		 * return ENOMEM in nice and clean way.
1666 		 */
1667 		bioq_init(&queue);
1668 		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1669 			sync = &disk->d_sync;
1670 			switch (disk->d_state) {
1671 			case G_MIRROR_DISK_STATE_ACTIVE:
1672 				break;
1673 			case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1674 				if (bp->bio_offset >= sync->ds_offset)
1675 					continue;
1676 				break;
1677 			default:
1678 				continue;
1679 			}
1680 			if (bp->bio_cmd == BIO_DELETE &&
1681 			    (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1682 				continue;
1683 			cbp = g_clone_bio(bp);
1684 			if (cbp == NULL) {
1685 				while ((cbp = bioq_takefirst(&queue)) != NULL)
1686 					g_destroy_bio(cbp);
1687 				if (bp->bio_error == 0)
1688 					bp->bio_error = ENOMEM;
1689 				g_io_deliver(bp, bp->bio_error);
1690 				return;
1691 			}
1692 			bioq_insert_tail(&queue, cbp);
1693 			cbp->bio_done = g_mirror_done;
1694 			cp = disk->d_consumer;
1695 			cbp->bio_caller1 = cp;
1696 			cbp->bio_to = cp->provider;
1697 			KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1698 			    ("Consumer %s not opened (r%dw%de%d).",
1699 			    cp->provider->name, cp->acr, cp->acw, cp->ace));
1700 		}
1701 		if (bioq_first(&queue) == NULL) {
1702 			g_io_deliver(bp, EOPNOTSUPP);
1703 			return;
1704 		}
1705 		while ((cbp = bioq_takefirst(&queue)) != NULL) {
1706 			G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1707 			cp = cbp->bio_caller1;
1708 			cbp->bio_caller1 = NULL;
1709 			cp->index++;
1710 			sc->sc_writes++;
1711 			g_io_request(cbp, cp);
1712 		}
1713 		/*
1714 		 * Put request onto inflight queue, so we can check if new
1715 		 * synchronization requests don't collide with it.
1716 		 */
1717 		bioq_insert_tail(&sc->sc_inflight, bp);
1718 		/*
1719 		 * Bump syncid on first write.
1720 		 */
1721 		if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1722 			sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1723 			g_mirror_bump_syncid(sc);
1724 		}
1725 		return;
1726 	    }
1727 	default:
1728 		KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1729 		    bp->bio_cmd, sc->sc_name));
1730 		break;
1731 	}
1732 }
1733 
1734 static int
1735 g_mirror_can_destroy(struct g_mirror_softc *sc)
1736 {
1737 	struct g_geom *gp;
1738 	struct g_consumer *cp;
1739 
1740 	g_topology_assert();
1741 	gp = sc->sc_geom;
1742 	if (gp->softc == NULL)
1743 		return (1);
1744 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1745 		return (0);
1746 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1747 		if (g_mirror_is_busy(sc, cp))
1748 			return (0);
1749 	}
1750 	gp = sc->sc_sync.ds_geom;
1751 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1752 		if (g_mirror_is_busy(sc, cp))
1753 			return (0);
1754 	}
1755 	G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1756 	    sc->sc_name);
1757 	return (1);
1758 }
1759 
1760 static int
1761 g_mirror_try_destroy(struct g_mirror_softc *sc)
1762 {
1763 
1764 	if (sc->sc_rootmount != NULL) {
1765 		G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1766 		    sc->sc_rootmount);
1767 		root_mount_rel(sc->sc_rootmount);
1768 		sc->sc_rootmount = NULL;
1769 	}
1770 	g_topology_lock();
1771 	if (!g_mirror_can_destroy(sc)) {
1772 		g_topology_unlock();
1773 		return (0);
1774 	}
1775 	sc->sc_geom->softc = NULL;
1776 	sc->sc_sync.ds_geom->softc = NULL;
1777 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1778 		g_topology_unlock();
1779 		G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1780 		    &sc->sc_worker);
1781 		/* Unlock sc_lock here, as it can be destroyed after wakeup. */
1782 		sx_xunlock(&sc->sc_lock);
1783 		wakeup(&sc->sc_worker);
1784 		sc->sc_worker = NULL;
1785 	} else {
1786 		g_topology_unlock();
1787 		g_mirror_destroy_device(sc);
1788 		free(sc, M_MIRROR);
1789 	}
1790 	return (1);
1791 }
1792 
1793 /*
1794  * Worker thread.
1795  */
1796 static void
1797 g_mirror_worker(void *arg)
1798 {
1799 	struct g_mirror_softc *sc;
1800 	struct g_mirror_event *ep;
1801 	struct bio *bp;
1802 	int timeout;
1803 
1804 	sc = arg;
1805 	thread_lock(curthread);
1806 	sched_prio(curthread, PRIBIO);
1807 	thread_unlock(curthread);
1808 
1809 	sx_xlock(&sc->sc_lock);
1810 	for (;;) {
1811 		G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1812 		/*
1813 		 * First take a look at events.
1814 		 * This is important to handle events before any I/O requests.
1815 		 */
1816 		ep = g_mirror_event_get(sc);
1817 		if (ep != NULL) {
1818 			g_mirror_event_remove(sc, ep);
1819 			if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1820 				/* Update only device status. */
1821 				G_MIRROR_DEBUG(3,
1822 				    "Running event for device %s.",
1823 				    sc->sc_name);
1824 				ep->e_error = 0;
1825 				g_mirror_update_device(sc, 1);
1826 			} else {
1827 				/* Update disk status. */
1828 				G_MIRROR_DEBUG(3, "Running event for disk %s.",
1829 				     g_mirror_get_diskname(ep->e_disk));
1830 				ep->e_error = g_mirror_update_disk(ep->e_disk,
1831 				    ep->e_state);
1832 				if (ep->e_error == 0)
1833 					g_mirror_update_device(sc, 0);
1834 			}
1835 			if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1836 				KASSERT(ep->e_error == 0,
1837 				    ("Error cannot be handled."));
1838 				g_mirror_event_free(ep);
1839 			} else {
1840 				ep->e_flags |= G_MIRROR_EVENT_DONE;
1841 				G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1842 				    ep);
1843 				mtx_lock(&sc->sc_events_mtx);
1844 				wakeup(ep);
1845 				mtx_unlock(&sc->sc_events_mtx);
1846 			}
1847 			if ((sc->sc_flags &
1848 			    G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1849 				if (g_mirror_try_destroy(sc)) {
1850 					curthread->td_pflags &= ~TDP_GEOM;
1851 					G_MIRROR_DEBUG(1, "Thread exiting.");
1852 					kproc_exit(0);
1853 				}
1854 			}
1855 			G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1856 			continue;
1857 		}
1858 		/*
1859 		 * Check if we can mark array as CLEAN and if we can't take
1860 		 * how much seconds should we wait.
1861 		 */
1862 		timeout = g_mirror_idle(sc, -1);
1863 		/*
1864 		 * Now I/O requests.
1865 		 */
1866 		/* Get first request from the queue. */
1867 		mtx_lock(&sc->sc_queue_mtx);
1868 		bp = bioq_first(&sc->sc_queue);
1869 		if (bp == NULL) {
1870 			if ((sc->sc_flags &
1871 			    G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1872 				mtx_unlock(&sc->sc_queue_mtx);
1873 				if (g_mirror_try_destroy(sc)) {
1874 					curthread->td_pflags &= ~TDP_GEOM;
1875 					G_MIRROR_DEBUG(1, "Thread exiting.");
1876 					kproc_exit(0);
1877 				}
1878 				mtx_lock(&sc->sc_queue_mtx);
1879 			}
1880 			sx_xunlock(&sc->sc_lock);
1881 			/*
1882 			 * XXX: We can miss an event here, because an event
1883 			 *      can be added without sx-device-lock and without
1884 			 *      mtx-queue-lock. Maybe I should just stop using
1885 			 *      dedicated mutex for events synchronization and
1886 			 *      stick with the queue lock?
1887 			 *      The event will hang here until next I/O request
1888 			 *      or next event is received.
1889 			 */
1890 			MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1891 			    timeout * hz);
1892 			sx_xlock(&sc->sc_lock);
1893 			G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1894 			continue;
1895 		}
1896 		bioq_remove(&sc->sc_queue, bp);
1897 		mtx_unlock(&sc->sc_queue_mtx);
1898 
1899 		if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1900 		    (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1901 			g_mirror_sync_request(bp);	/* READ */
1902 		} else if (bp->bio_to != sc->sc_provider) {
1903 			if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1904 				g_mirror_regular_request(bp);
1905 			else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1906 				g_mirror_sync_request(bp);	/* WRITE */
1907 			else {
1908 				KASSERT(0,
1909 				    ("Invalid request cflags=0x%hhx to=%s.",
1910 				    bp->bio_cflags, bp->bio_to->name));
1911 			}
1912 		} else {
1913 			g_mirror_register_request(bp);
1914 		}
1915 		G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1916 	}
1917 }
1918 
1919 static void
1920 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1921 {
1922 
1923 	sx_assert(&sc->sc_lock, SX_LOCKED);
1924 
1925 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1926 		return;
1927 	if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1928 		G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1929 		    g_mirror_get_diskname(disk), sc->sc_name);
1930 		disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1931 	} else if (sc->sc_idle &&
1932 	    (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1933 		G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1934 		    g_mirror_get_diskname(disk), sc->sc_name);
1935 		disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1936 	}
1937 }
1938 
1939 static void
1940 g_mirror_sync_start(struct g_mirror_disk *disk)
1941 {
1942 	struct g_mirror_softc *sc;
1943 	struct g_consumer *cp;
1944 	struct bio *bp;
1945 	int error, i;
1946 
1947 	g_topology_assert_not();
1948 	sc = disk->d_softc;
1949 	sx_assert(&sc->sc_lock, SX_LOCKED);
1950 
1951 	KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1952 	    ("Disk %s is not marked for synchronization.",
1953 	    g_mirror_get_diskname(disk)));
1954 	KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1955 	    ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1956 	    sc->sc_state));
1957 
1958 	sx_xunlock(&sc->sc_lock);
1959 	g_topology_lock();
1960 	cp = g_new_consumer(sc->sc_sync.ds_geom);
1961 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1962 	error = g_attach(cp, sc->sc_provider);
1963 	KASSERT(error == 0,
1964 	    ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1965 	error = g_access(cp, 1, 0, 0);
1966 	KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1967 	g_topology_unlock();
1968 	sx_xlock(&sc->sc_lock);
1969 
1970 	G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1971 	    g_mirror_get_diskname(disk));
1972 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
1973 		disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1974 	KASSERT(disk->d_sync.ds_consumer == NULL,
1975 	    ("Sync consumer already exists (device=%s, disk=%s).",
1976 	    sc->sc_name, g_mirror_get_diskname(disk)));
1977 
1978 	disk->d_sync.ds_consumer = cp;
1979 	disk->d_sync.ds_consumer->private = disk;
1980 	disk->d_sync.ds_consumer->index = 0;
1981 
1982 	/*
1983 	 * Allocate memory for synchronization bios and initialize them.
1984 	 */
1985 	disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
1986 	    M_MIRROR, M_WAITOK);
1987 	for (i = 0; i < g_mirror_syncreqs; i++) {
1988 		bp = g_alloc_bio();
1989 		disk->d_sync.ds_bios[i] = bp;
1990 		bp->bio_parent = NULL;
1991 		bp->bio_cmd = BIO_READ;
1992 		bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
1993 		bp->bio_cflags = 0;
1994 		bp->bio_offset = disk->d_sync.ds_offset;
1995 		bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1996 		disk->d_sync.ds_offset += bp->bio_length;
1997 		bp->bio_done = g_mirror_sync_done;
1998 		bp->bio_from = disk->d_sync.ds_consumer;
1999 		bp->bio_to = sc->sc_provider;
2000 		bp->bio_caller1 = (void *)(uintptr_t)i;
2001 	}
2002 
2003 	/* Increase the number of disks in SYNCHRONIZING state. */
2004 	sc->sc_sync.ds_ndisks++;
2005 	/* Set the number of in-flight synchronization requests. */
2006 	disk->d_sync.ds_inflight = g_mirror_syncreqs;
2007 
2008 	/*
2009 	 * Fire off first synchronization requests.
2010 	 */
2011 	for (i = 0; i < g_mirror_syncreqs; i++) {
2012 		bp = disk->d_sync.ds_bios[i];
2013 		G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
2014 		disk->d_sync.ds_consumer->index++;
2015 		/*
2016 		 * Delay the request if it is colliding with a regular request.
2017 		 */
2018 		if (g_mirror_regular_collision(sc, bp))
2019 			g_mirror_sync_delay(sc, bp);
2020 		else
2021 			g_io_request(bp, disk->d_sync.ds_consumer);
2022 	}
2023 }
2024 
2025 /*
2026  * Stop synchronization process.
2027  * type: 0 - synchronization finished
2028  *       1 - synchronization stopped
2029  */
2030 static void
2031 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
2032 {
2033 	struct g_mirror_softc *sc;
2034 	struct g_consumer *cp;
2035 
2036 	g_topology_assert_not();
2037 	sc = disk->d_softc;
2038 	sx_assert(&sc->sc_lock, SX_LOCKED);
2039 
2040 	KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2041 	    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2042 	    g_mirror_disk_state2str(disk->d_state)));
2043 	if (disk->d_sync.ds_consumer == NULL)
2044 		return;
2045 
2046 	if (type == 0) {
2047 		G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2048 		    sc->sc_name, g_mirror_get_diskname(disk));
2049 	} else /* if (type == 1) */ {
2050 		G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2051 		    sc->sc_name, g_mirror_get_diskname(disk));
2052 	}
2053 	free(disk->d_sync.ds_bios, M_MIRROR);
2054 	disk->d_sync.ds_bios = NULL;
2055 	cp = disk->d_sync.ds_consumer;
2056 	disk->d_sync.ds_consumer = NULL;
2057 	disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2058 	sc->sc_sync.ds_ndisks--;
2059 	sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2060 	g_topology_lock();
2061 	g_mirror_kill_consumer(sc, cp);
2062 	g_topology_unlock();
2063 	sx_xlock(&sc->sc_lock);
2064 }
2065 
2066 static void
2067 g_mirror_launch_provider(struct g_mirror_softc *sc)
2068 {
2069 	struct g_mirror_disk *disk;
2070 	struct g_provider *pp, *dp;
2071 
2072 	sx_assert(&sc->sc_lock, SX_LOCKED);
2073 
2074 	g_topology_lock();
2075 	pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2076 	pp->flags |= G_PF_DIRECT_RECEIVE;
2077 	pp->mediasize = sc->sc_mediasize;
2078 	pp->sectorsize = sc->sc_sectorsize;
2079 	pp->stripesize = 0;
2080 	pp->stripeoffset = 0;
2081 
2082 	/* Splitting of unmapped BIO's could work but isn't implemented now */
2083 	if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2084 		pp->flags |= G_PF_ACCEPT_UNMAPPED;
2085 
2086 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2087 		if (disk->d_consumer && disk->d_consumer->provider) {
2088 			dp = disk->d_consumer->provider;
2089 			if (dp->stripesize > pp->stripesize) {
2090 				pp->stripesize = dp->stripesize;
2091 				pp->stripeoffset = dp->stripeoffset;
2092 			}
2093 			/* A provider underneath us doesn't support unmapped */
2094 			if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2095 				G_MIRROR_DEBUG(0, "Cancelling unmapped "
2096 				    "because of %s.", dp->name);
2097 				pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2098 			}
2099 		}
2100 	}
2101 	sc->sc_provider = pp;
2102 	g_error_provider(pp, 0);
2103 	g_topology_unlock();
2104 	G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2105 	    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2106 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2107 		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2108 			g_mirror_sync_start(disk);
2109 	}
2110 }
2111 
2112 static void
2113 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2114 {
2115 	struct g_mirror_disk *disk;
2116 	struct bio *bp;
2117 
2118 	g_topology_assert_not();
2119 	KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2120 	    sc->sc_name));
2121 
2122 	g_topology_lock();
2123 	g_error_provider(sc->sc_provider, ENXIO);
2124 	mtx_lock(&sc->sc_queue_mtx);
2125 	while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
2126 		g_io_deliver(bp, ENXIO);
2127 	mtx_unlock(&sc->sc_queue_mtx);
2128 	G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2129 	    sc->sc_provider->name);
2130 	sc->sc_provider->flags |= G_PF_WITHER;
2131 	g_orphan_provider(sc->sc_provider, ENXIO);
2132 	g_topology_unlock();
2133 	sc->sc_provider = NULL;
2134 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2135 		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2136 			g_mirror_sync_stop(disk, 1);
2137 	}
2138 }
2139 
2140 static void
2141 g_mirror_go(void *arg)
2142 {
2143 	struct g_mirror_softc *sc;
2144 
2145 	sc = arg;
2146 	G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2147 	g_mirror_event_send(sc, 0,
2148 	    G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2149 }
2150 
2151 static u_int
2152 g_mirror_determine_state(struct g_mirror_disk *disk)
2153 {
2154 	struct g_mirror_softc *sc;
2155 	u_int state;
2156 
2157 	sc = disk->d_softc;
2158 	if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2159 		if ((disk->d_flags &
2160 		    G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2161 			/* Disk does not need synchronization. */
2162 			state = G_MIRROR_DISK_STATE_ACTIVE;
2163 		} else {
2164 			if ((sc->sc_flags &
2165 			     G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2166 			    (disk->d_flags &
2167 			     G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2168 				/*
2169 				 * We can start synchronization from
2170 				 * the stored offset.
2171 				 */
2172 				state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2173 			} else {
2174 				state = G_MIRROR_DISK_STATE_STALE;
2175 			}
2176 		}
2177 	} else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2178 		/*
2179 		 * Reset all synchronization data for this disk,
2180 		 * because if it even was synchronized, it was
2181 		 * synchronized to disks with different syncid.
2182 		 */
2183 		disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2184 		disk->d_sync.ds_offset = 0;
2185 		disk->d_sync.ds_offset_done = 0;
2186 		disk->d_sync.ds_syncid = sc->sc_syncid;
2187 		if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2188 		    (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2189 			state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2190 		} else {
2191 			state = G_MIRROR_DISK_STATE_STALE;
2192 		}
2193 	} else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2194 		/*
2195 		 * Not good, NOT GOOD!
2196 		 * It means that mirror was started on stale disks
2197 		 * and more fresh disk just arrive.
2198 		 * If there were writes, mirror is broken, sorry.
2199 		 * I think the best choice here is don't touch
2200 		 * this disk and inform the user loudly.
2201 		 */
2202 		G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2203 		    "disk (%s) arrives!! It will not be connected to the "
2204 		    "running device.", sc->sc_name,
2205 		    g_mirror_get_diskname(disk));
2206 		g_mirror_destroy_disk(disk);
2207 		state = G_MIRROR_DISK_STATE_NONE;
2208 		/* Return immediately, because disk was destroyed. */
2209 		return (state);
2210 	}
2211 	G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2212 	    g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2213 	return (state);
2214 }
2215 
2216 /*
2217  * Update device state.
2218  */
2219 static void
2220 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2221 {
2222 	struct g_mirror_disk *disk;
2223 	u_int state;
2224 
2225 	sx_assert(&sc->sc_lock, SX_XLOCKED);
2226 
2227 	switch (sc->sc_state) {
2228 	case G_MIRROR_DEVICE_STATE_STARTING:
2229 	    {
2230 		struct g_mirror_disk *pdisk, *tdisk;
2231 		u_int dirty, ndisks, genid, syncid;
2232 
2233 		KASSERT(sc->sc_provider == NULL,
2234 		    ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2235 		/*
2236 		 * Are we ready? We are, if all disks are connected or
2237 		 * if we have any disks and 'force' is true.
2238 		 */
2239 		ndisks = g_mirror_ndisks(sc, -1);
2240 		if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2241 			;
2242 		} else if (ndisks == 0) {
2243 			/*
2244 			 * Disks went down in starting phase, so destroy
2245 			 * device.
2246 			 */
2247 			callout_drain(&sc->sc_callout);
2248 			sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2249 			G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2250 			    sc->sc_rootmount);
2251 			root_mount_rel(sc->sc_rootmount);
2252 			sc->sc_rootmount = NULL;
2253 			return;
2254 		} else {
2255 			return;
2256 		}
2257 
2258 		/*
2259 		 * Activate all disks with the biggest syncid.
2260 		 */
2261 		if (force) {
2262 			/*
2263 			 * If 'force' is true, we have been called due to
2264 			 * timeout, so don't bother canceling timeout.
2265 			 */
2266 			ndisks = 0;
2267 			LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2268 				if ((disk->d_flags &
2269 				    G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2270 					ndisks++;
2271 				}
2272 			}
2273 			if (ndisks == 0) {
2274 				/* No valid disks found, destroy device. */
2275 				sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2276 				G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2277 				    __LINE__, sc->sc_rootmount);
2278 				root_mount_rel(sc->sc_rootmount);
2279 				sc->sc_rootmount = NULL;
2280 				return;
2281 			}
2282 		} else {
2283 			/* Cancel timeout. */
2284 			callout_drain(&sc->sc_callout);
2285 		}
2286 
2287 		/*
2288 		 * Find the biggest genid.
2289 		 */
2290 		genid = 0;
2291 		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2292 			if (disk->d_genid > genid)
2293 				genid = disk->d_genid;
2294 		}
2295 		sc->sc_genid = genid;
2296 		/*
2297 		 * Remove all disks without the biggest genid.
2298 		 */
2299 		LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2300 			if (disk->d_genid < genid) {
2301 				G_MIRROR_DEBUG(0,
2302 				    "Component %s (device %s) broken, skipping.",
2303 				    g_mirror_get_diskname(disk), sc->sc_name);
2304 				g_mirror_destroy_disk(disk);
2305 			}
2306 		}
2307 
2308 		/*
2309 		 * Find the biggest syncid.
2310 		 */
2311 		syncid = 0;
2312 		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2313 			if (disk->d_sync.ds_syncid > syncid)
2314 				syncid = disk->d_sync.ds_syncid;
2315 		}
2316 
2317 		/*
2318 		 * Here we need to look for dirty disks and if all disks
2319 		 * with the biggest syncid are dirty, we have to choose
2320 		 * one with the biggest priority and rebuild the rest.
2321 		 */
2322 		/*
2323 		 * Find the number of dirty disks with the biggest syncid.
2324 		 * Find the number of disks with the biggest syncid.
2325 		 * While here, find a disk with the biggest priority.
2326 		 */
2327 		dirty = ndisks = 0;
2328 		pdisk = NULL;
2329 		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2330 			if (disk->d_sync.ds_syncid != syncid)
2331 				continue;
2332 			if ((disk->d_flags &
2333 			    G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2334 				continue;
2335 			}
2336 			ndisks++;
2337 			if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2338 				dirty++;
2339 				if (pdisk == NULL ||
2340 				    pdisk->d_priority < disk->d_priority) {
2341 					pdisk = disk;
2342 				}
2343 			}
2344 		}
2345 		if (dirty == 0) {
2346 			/* No dirty disks at all, great. */
2347 		} else if (dirty == ndisks) {
2348 			/*
2349 			 * Force synchronization for all dirty disks except one
2350 			 * with the biggest priority.
2351 			 */
2352 			KASSERT(pdisk != NULL, ("pdisk == NULL"));
2353 			G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2354 			    "master disk for synchronization.",
2355 			    g_mirror_get_diskname(pdisk), sc->sc_name);
2356 			LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2357 				if (disk->d_sync.ds_syncid != syncid)
2358 					continue;
2359 				if ((disk->d_flags &
2360 				    G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2361 					continue;
2362 				}
2363 				KASSERT((disk->d_flags &
2364 				    G_MIRROR_DISK_FLAG_DIRTY) != 0,
2365 				    ("Disk %s isn't marked as dirty.",
2366 				    g_mirror_get_diskname(disk)));
2367 				/* Skip the disk with the biggest priority. */
2368 				if (disk == pdisk)
2369 					continue;
2370 				disk->d_sync.ds_syncid = 0;
2371 			}
2372 		} else if (dirty < ndisks) {
2373 			/*
2374 			 * Force synchronization for all dirty disks.
2375 			 * We have some non-dirty disks.
2376 			 */
2377 			LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2378 				if (disk->d_sync.ds_syncid != syncid)
2379 					continue;
2380 				if ((disk->d_flags &
2381 				    G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2382 					continue;
2383 				}
2384 				if ((disk->d_flags &
2385 				    G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2386 					continue;
2387 				}
2388 				disk->d_sync.ds_syncid = 0;
2389 			}
2390 		}
2391 
2392 		/* Reset hint. */
2393 		sc->sc_hint = NULL;
2394 		sc->sc_syncid = syncid;
2395 		if (force) {
2396 			/* Remember to bump syncid on first write. */
2397 			sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2398 		}
2399 		state = G_MIRROR_DEVICE_STATE_RUNNING;
2400 		G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2401 		    sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2402 		    g_mirror_device_state2str(state));
2403 		sc->sc_state = state;
2404 		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2405 			state = g_mirror_determine_state(disk);
2406 			g_mirror_event_send(disk, state,
2407 			    G_MIRROR_EVENT_DONTWAIT);
2408 			if (state == G_MIRROR_DISK_STATE_STALE)
2409 				sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2410 		}
2411 		break;
2412 	    }
2413 	case G_MIRROR_DEVICE_STATE_RUNNING:
2414 		if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2415 		    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2416 			/*
2417 			 * No active disks or no disks at all,
2418 			 * so destroy device.
2419 			 */
2420 			if (sc->sc_provider != NULL)
2421 				g_mirror_destroy_provider(sc);
2422 			sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2423 			break;
2424 		} else if (g_mirror_ndisks(sc,
2425 		    G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2426 		    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2427 			/*
2428 			 * We have active disks, launch provider if it doesn't
2429 			 * exist.
2430 			 */
2431 			if (sc->sc_provider == NULL)
2432 				g_mirror_launch_provider(sc);
2433 			if (sc->sc_rootmount != NULL) {
2434 				G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2435 				    __LINE__, sc->sc_rootmount);
2436 				root_mount_rel(sc->sc_rootmount);
2437 				sc->sc_rootmount = NULL;
2438 			}
2439 		}
2440 		/*
2441 		 * Genid should be bumped immediately, so do it here.
2442 		 */
2443 		if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2444 			sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2445 			g_mirror_bump_genid(sc);
2446 		}
2447 		break;
2448 	default:
2449 		KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2450 		    sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2451 		break;
2452 	}
2453 }
2454 
2455 /*
2456  * Update disk state and device state if needed.
2457  */
2458 #define	DISK_STATE_CHANGED()	G_MIRROR_DEBUG(1,			\
2459 	"Disk %s state changed from %s to %s (device %s).",		\
2460 	g_mirror_get_diskname(disk),					\
2461 	g_mirror_disk_state2str(disk->d_state),				\
2462 	g_mirror_disk_state2str(state), sc->sc_name)
2463 static int
2464 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2465 {
2466 	struct g_mirror_softc *sc;
2467 
2468 	sc = disk->d_softc;
2469 	sx_assert(&sc->sc_lock, SX_XLOCKED);
2470 
2471 again:
2472 	G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2473 	    g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2474 	    g_mirror_disk_state2str(state));
2475 	switch (state) {
2476 	case G_MIRROR_DISK_STATE_NEW:
2477 		/*
2478 		 * Possible scenarios:
2479 		 * 1. New disk arrive.
2480 		 */
2481 		/* Previous state should be NONE. */
2482 		KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2483 		    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2484 		    g_mirror_disk_state2str(disk->d_state)));
2485 		DISK_STATE_CHANGED();
2486 
2487 		disk->d_state = state;
2488 		if (LIST_EMPTY(&sc->sc_disks))
2489 			LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2490 		else {
2491 			struct g_mirror_disk *dp;
2492 
2493 			LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2494 				if (disk->d_priority >= dp->d_priority) {
2495 					LIST_INSERT_BEFORE(dp, disk, d_next);
2496 					dp = NULL;
2497 					break;
2498 				}
2499 				if (LIST_NEXT(dp, d_next) == NULL)
2500 					break;
2501 			}
2502 			if (dp != NULL)
2503 				LIST_INSERT_AFTER(dp, disk, d_next);
2504 		}
2505 		G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2506 		    sc->sc_name, g_mirror_get_diskname(disk));
2507 		if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2508 			break;
2509 		KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2510 		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2511 		    g_mirror_device_state2str(sc->sc_state),
2512 		    g_mirror_get_diskname(disk),
2513 		    g_mirror_disk_state2str(disk->d_state)));
2514 		state = g_mirror_determine_state(disk);
2515 		if (state != G_MIRROR_DISK_STATE_NONE)
2516 			goto again;
2517 		break;
2518 	case G_MIRROR_DISK_STATE_ACTIVE:
2519 		/*
2520 		 * Possible scenarios:
2521 		 * 1. New disk does not need synchronization.
2522 		 * 2. Synchronization process finished successfully.
2523 		 */
2524 		KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2525 		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2526 		    g_mirror_device_state2str(sc->sc_state),
2527 		    g_mirror_get_diskname(disk),
2528 		    g_mirror_disk_state2str(disk->d_state)));
2529 		/* Previous state should be NEW or SYNCHRONIZING. */
2530 		KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2531 		    disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2532 		    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2533 		    g_mirror_disk_state2str(disk->d_state)));
2534 		DISK_STATE_CHANGED();
2535 
2536 		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2537 			disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2538 			disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2539 			g_mirror_sync_stop(disk, 0);
2540 		}
2541 		disk->d_state = state;
2542 		disk->d_sync.ds_offset = 0;
2543 		disk->d_sync.ds_offset_done = 0;
2544 		g_mirror_update_idle(sc, disk);
2545 		g_mirror_update_metadata(disk);
2546 		G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2547 		    sc->sc_name, g_mirror_get_diskname(disk));
2548 		break;
2549 	case G_MIRROR_DISK_STATE_STALE:
2550 		/*
2551 		 * Possible scenarios:
2552 		 * 1. Stale disk was connected.
2553 		 */
2554 		/* Previous state should be NEW. */
2555 		KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2556 		    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2557 		    g_mirror_disk_state2str(disk->d_state)));
2558 		KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2559 		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2560 		    g_mirror_device_state2str(sc->sc_state),
2561 		    g_mirror_get_diskname(disk),
2562 		    g_mirror_disk_state2str(disk->d_state)));
2563 		/*
2564 		 * STALE state is only possible if device is marked
2565 		 * NOAUTOSYNC.
2566 		 */
2567 		KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2568 		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2569 		    g_mirror_device_state2str(sc->sc_state),
2570 		    g_mirror_get_diskname(disk),
2571 		    g_mirror_disk_state2str(disk->d_state)));
2572 		DISK_STATE_CHANGED();
2573 
2574 		disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2575 		disk->d_state = state;
2576 		g_mirror_update_metadata(disk);
2577 		G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2578 		    sc->sc_name, g_mirror_get_diskname(disk));
2579 		break;
2580 	case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2581 		/*
2582 		 * Possible scenarios:
2583 		 * 1. Disk which needs synchronization was connected.
2584 		 */
2585 		/* Previous state should be NEW. */
2586 		KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2587 		    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2588 		    g_mirror_disk_state2str(disk->d_state)));
2589 		KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2590 		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2591 		    g_mirror_device_state2str(sc->sc_state),
2592 		    g_mirror_get_diskname(disk),
2593 		    g_mirror_disk_state2str(disk->d_state)));
2594 		DISK_STATE_CHANGED();
2595 
2596 		if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2597 			disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2598 		disk->d_state = state;
2599 		if (sc->sc_provider != NULL) {
2600 			g_mirror_sync_start(disk);
2601 			g_mirror_update_metadata(disk);
2602 		}
2603 		break;
2604 	case G_MIRROR_DISK_STATE_DISCONNECTED:
2605 		/*
2606 		 * Possible scenarios:
2607 		 * 1. Device wasn't running yet, but disk disappear.
2608 		 * 2. Disk was active and disapppear.
2609 		 * 3. Disk disappear during synchronization process.
2610 		 */
2611 		if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2612 			/*
2613 			 * Previous state should be ACTIVE, STALE or
2614 			 * SYNCHRONIZING.
2615 			 */
2616 			KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2617 			    disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2618 			    disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2619 			    ("Wrong disk state (%s, %s).",
2620 			    g_mirror_get_diskname(disk),
2621 			    g_mirror_disk_state2str(disk->d_state)));
2622 		} else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2623 			/* Previous state should be NEW. */
2624 			KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2625 			    ("Wrong disk state (%s, %s).",
2626 			    g_mirror_get_diskname(disk),
2627 			    g_mirror_disk_state2str(disk->d_state)));
2628 			/*
2629 			 * Reset bumping syncid if disk disappeared in STARTING
2630 			 * state.
2631 			 */
2632 			if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2633 				sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2634 #ifdef	INVARIANTS
2635 		} else {
2636 			KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2637 			    sc->sc_name,
2638 			    g_mirror_device_state2str(sc->sc_state),
2639 			    g_mirror_get_diskname(disk),
2640 			    g_mirror_disk_state2str(disk->d_state)));
2641 #endif
2642 		}
2643 		DISK_STATE_CHANGED();
2644 		G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2645 		    sc->sc_name, g_mirror_get_diskname(disk));
2646 
2647 		g_mirror_destroy_disk(disk);
2648 		break;
2649 	case G_MIRROR_DISK_STATE_DESTROY:
2650 	    {
2651 		int error;
2652 
2653 		error = g_mirror_clear_metadata(disk);
2654 		if (error != 0)
2655 			return (error);
2656 		DISK_STATE_CHANGED();
2657 		G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2658 		    sc->sc_name, g_mirror_get_diskname(disk));
2659 
2660 		g_mirror_destroy_disk(disk);
2661 		sc->sc_ndisks--;
2662 		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2663 			g_mirror_update_metadata(disk);
2664 		}
2665 		break;
2666 	    }
2667 	default:
2668 		KASSERT(1 == 0, ("Unknown state (%u).", state));
2669 		break;
2670 	}
2671 	return (0);
2672 }
2673 #undef	DISK_STATE_CHANGED
2674 
2675 int
2676 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2677 {
2678 	struct g_provider *pp;
2679 	u_char *buf;
2680 	int error;
2681 
2682 	g_topology_assert();
2683 
2684 	error = g_access(cp, 1, 0, 0);
2685 	if (error != 0)
2686 		return (error);
2687 	pp = cp->provider;
2688 	g_topology_unlock();
2689 	/* Metadata are stored on last sector. */
2690 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2691 	    &error);
2692 	g_topology_lock();
2693 	g_access(cp, -1, 0, 0);
2694 	if (buf == NULL) {
2695 		G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2696 		    cp->provider->name, error);
2697 		return (error);
2698 	}
2699 
2700 	/* Decode metadata. */
2701 	error = mirror_metadata_decode(buf, md);
2702 	g_free(buf);
2703 	if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2704 		return (EINVAL);
2705 	if (md->md_version > G_MIRROR_VERSION) {
2706 		G_MIRROR_DEBUG(0,
2707 		    "Kernel module is too old to handle metadata from %s.",
2708 		    cp->provider->name);
2709 		return (EINVAL);
2710 	}
2711 	if (error != 0) {
2712 		G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2713 		    cp->provider->name);
2714 		return (error);
2715 	}
2716 
2717 	return (0);
2718 }
2719 
2720 static int
2721 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2722     struct g_mirror_metadata *md)
2723 {
2724 
2725 	if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2726 		G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2727 		    pp->name, md->md_did);
2728 		return (EEXIST);
2729 	}
2730 	if (md->md_all != sc->sc_ndisks) {
2731 		G_MIRROR_DEBUG(1,
2732 		    "Invalid '%s' field on disk %s (device %s), skipping.",
2733 		    "md_all", pp->name, sc->sc_name);
2734 		return (EINVAL);
2735 	}
2736 	if (md->md_slice != sc->sc_slice) {
2737 		G_MIRROR_DEBUG(1,
2738 		    "Invalid '%s' field on disk %s (device %s), skipping.",
2739 		    "md_slice", pp->name, sc->sc_name);
2740 		return (EINVAL);
2741 	}
2742 	if (md->md_balance != sc->sc_balance) {
2743 		G_MIRROR_DEBUG(1,
2744 		    "Invalid '%s' field on disk %s (device %s), skipping.",
2745 		    "md_balance", pp->name, sc->sc_name);
2746 		return (EINVAL);
2747 	}
2748 #if 0
2749 	if (md->md_mediasize != sc->sc_mediasize) {
2750 		G_MIRROR_DEBUG(1,
2751 		    "Invalid '%s' field on disk %s (device %s), skipping.",
2752 		    "md_mediasize", pp->name, sc->sc_name);
2753 		return (EINVAL);
2754 	}
2755 #endif
2756 	if (sc->sc_mediasize > pp->mediasize) {
2757 		G_MIRROR_DEBUG(1,
2758 		    "Invalid size of disk %s (device %s), skipping.", pp->name,
2759 		    sc->sc_name);
2760 		return (EINVAL);
2761 	}
2762 	if (md->md_sectorsize != sc->sc_sectorsize) {
2763 		G_MIRROR_DEBUG(1,
2764 		    "Invalid '%s' field on disk %s (device %s), skipping.",
2765 		    "md_sectorsize", pp->name, sc->sc_name);
2766 		return (EINVAL);
2767 	}
2768 	if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2769 		G_MIRROR_DEBUG(1,
2770 		    "Invalid sector size of disk %s (device %s), skipping.",
2771 		    pp->name, sc->sc_name);
2772 		return (EINVAL);
2773 	}
2774 	if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2775 		G_MIRROR_DEBUG(1,
2776 		    "Invalid device flags on disk %s (device %s), skipping.",
2777 		    pp->name, sc->sc_name);
2778 		return (EINVAL);
2779 	}
2780 	if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2781 		G_MIRROR_DEBUG(1,
2782 		    "Invalid disk flags on disk %s (device %s), skipping.",
2783 		    pp->name, sc->sc_name);
2784 		return (EINVAL);
2785 	}
2786 	return (0);
2787 }
2788 
2789 int
2790 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2791     struct g_mirror_metadata *md)
2792 {
2793 	struct g_mirror_disk *disk;
2794 	int error;
2795 
2796 	g_topology_assert_not();
2797 	G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2798 
2799 	error = g_mirror_check_metadata(sc, pp, md);
2800 	if (error != 0)
2801 		return (error);
2802 	if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2803 	    md->md_genid < sc->sc_genid) {
2804 		G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2805 		    pp->name, sc->sc_name);
2806 		return (EINVAL);
2807 	}
2808 	disk = g_mirror_init_disk(sc, pp, md, &error);
2809 	if (disk == NULL)
2810 		return (error);
2811 	error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2812 	    G_MIRROR_EVENT_WAIT);
2813 	if (error != 0)
2814 		return (error);
2815 	if (md->md_version < G_MIRROR_VERSION) {
2816 		G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2817 		    pp->name, md->md_version, G_MIRROR_VERSION);
2818 		g_mirror_update_metadata(disk);
2819 	}
2820 	return (0);
2821 }
2822 
2823 static void
2824 g_mirror_destroy_delayed(void *arg, int flag)
2825 {
2826 	struct g_mirror_softc *sc;
2827 	int error;
2828 
2829 	if (flag == EV_CANCEL) {
2830 		G_MIRROR_DEBUG(1, "Destroying canceled.");
2831 		return;
2832 	}
2833 	sc = arg;
2834 	g_topology_unlock();
2835 	sx_xlock(&sc->sc_lock);
2836 	KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2837 	    ("DESTROY flag set on %s.", sc->sc_name));
2838 	KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2839 	    ("DESTROYING flag not set on %s.", sc->sc_name));
2840 	G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2841 	error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2842 	if (error != 0) {
2843 		G_MIRROR_DEBUG(0, "Cannot destroy %s (error=%d).",
2844 		    sc->sc_name, error);
2845 		sx_xunlock(&sc->sc_lock);
2846 	}
2847 	g_topology_lock();
2848 }
2849 
2850 static int
2851 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2852 {
2853 	struct g_mirror_softc *sc;
2854 	int dcr, dcw, dce, error = 0;
2855 
2856 	g_topology_assert();
2857 	G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2858 	    acw, ace);
2859 
2860 	sc = pp->geom->softc;
2861 	if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
2862 		return (0);
2863 	KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2864 
2865 	dcr = pp->acr + acr;
2866 	dcw = pp->acw + acw;
2867 	dce = pp->ace + ace;
2868 
2869 	g_topology_unlock();
2870 	sx_xlock(&sc->sc_lock);
2871 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2872 	    LIST_EMPTY(&sc->sc_disks)) {
2873 		if (acr > 0 || acw > 0 || ace > 0)
2874 			error = ENXIO;
2875 		goto end;
2876 	}
2877 	if (dcw == 0)
2878 		g_mirror_idle(sc, dcw);
2879 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0) {
2880 		if (acr > 0 || acw > 0 || ace > 0) {
2881 			error = ENXIO;
2882 			goto end;
2883 		}
2884 		if (dcr == 0 && dcw == 0 && dce == 0) {
2885 			g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK,
2886 			    sc, NULL);
2887 		}
2888 	}
2889 end:
2890 	sx_xunlock(&sc->sc_lock);
2891 	g_topology_lock();
2892 	return (error);
2893 }
2894 
2895 static struct g_geom *
2896 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2897 {
2898 	struct g_mirror_softc *sc;
2899 	struct g_geom *gp;
2900 	int error, timeout;
2901 
2902 	g_topology_assert();
2903 	G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2904 	    md->md_mid);
2905 
2906 	/* One disk is minimum. */
2907 	if (md->md_all < 1)
2908 		return (NULL);
2909 	/*
2910 	 * Action geom.
2911 	 */
2912 	gp = g_new_geomf(mp, "%s", md->md_name);
2913 	sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2914 	gp->start = g_mirror_start;
2915 	gp->orphan = g_mirror_orphan;
2916 	gp->access = g_mirror_access;
2917 	gp->dumpconf = g_mirror_dumpconf;
2918 
2919 	sc->sc_id = md->md_mid;
2920 	sc->sc_slice = md->md_slice;
2921 	sc->sc_balance = md->md_balance;
2922 	sc->sc_mediasize = md->md_mediasize;
2923 	sc->sc_sectorsize = md->md_sectorsize;
2924 	sc->sc_ndisks = md->md_all;
2925 	sc->sc_flags = md->md_mflags;
2926 	sc->sc_bump_id = 0;
2927 	sc->sc_idle = 1;
2928 	sc->sc_last_write = time_uptime;
2929 	sc->sc_writes = 0;
2930 	sx_init(&sc->sc_lock, "gmirror:lock");
2931 	bioq_init(&sc->sc_queue);
2932 	mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2933 	bioq_init(&sc->sc_regular_delayed);
2934 	bioq_init(&sc->sc_inflight);
2935 	bioq_init(&sc->sc_sync_delayed);
2936 	LIST_INIT(&sc->sc_disks);
2937 	TAILQ_INIT(&sc->sc_events);
2938 	mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2939 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
2940 	mtx_init(&sc->sc_done_mtx, "gmirror:done", NULL, MTX_DEF);
2941 	sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2942 	gp->softc = sc;
2943 	sc->sc_geom = gp;
2944 	sc->sc_provider = NULL;
2945 	/*
2946 	 * Synchronization geom.
2947 	 */
2948 	gp = g_new_geomf(mp, "%s.sync", md->md_name);
2949 	gp->softc = sc;
2950 	gp->orphan = g_mirror_orphan;
2951 	sc->sc_sync.ds_geom = gp;
2952 	sc->sc_sync.ds_ndisks = 0;
2953 	error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2954 	    "g_mirror %s", md->md_name);
2955 	if (error != 0) {
2956 		G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2957 		    sc->sc_name);
2958 		g_destroy_geom(sc->sc_sync.ds_geom);
2959 		mtx_destroy(&sc->sc_done_mtx);
2960 		mtx_destroy(&sc->sc_events_mtx);
2961 		mtx_destroy(&sc->sc_queue_mtx);
2962 		sx_destroy(&sc->sc_lock);
2963 		g_destroy_geom(sc->sc_geom);
2964 		free(sc, M_MIRROR);
2965 		return (NULL);
2966 	}
2967 
2968 	G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
2969 	    sc->sc_name, sc->sc_ndisks, sc->sc_id);
2970 
2971 	sc->sc_rootmount = root_mount_hold("GMIRROR");
2972 	G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
2973 	/*
2974 	 * Run timeout.
2975 	 */
2976 	timeout = g_mirror_timeout * hz;
2977 	callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
2978 	return (sc->sc_geom);
2979 }
2980 
2981 int
2982 g_mirror_destroy(struct g_mirror_softc *sc, int how)
2983 {
2984 	struct g_mirror_disk *disk;
2985 	struct g_provider *pp;
2986 
2987 	g_topology_assert_not();
2988 	if (sc == NULL)
2989 		return (ENXIO);
2990 	sx_assert(&sc->sc_lock, SX_XLOCKED);
2991 
2992 	pp = sc->sc_provider;
2993 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
2994 		switch (how) {
2995 		case G_MIRROR_DESTROY_SOFT:
2996 			G_MIRROR_DEBUG(1,
2997 			    "Device %s is still open (r%dw%de%d).", pp->name,
2998 			    pp->acr, pp->acw, pp->ace);
2999 			return (EBUSY);
3000 		case G_MIRROR_DESTROY_DELAYED:
3001 			G_MIRROR_DEBUG(1,
3002 			    "Device %s will be destroyed on last close.",
3003 			    pp->name);
3004 			LIST_FOREACH(disk, &sc->sc_disks, d_next) {
3005 				if (disk->d_state ==
3006 				    G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3007 					g_mirror_sync_stop(disk, 1);
3008 				}
3009 			}
3010 			sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
3011 			return (EBUSY);
3012 		case G_MIRROR_DESTROY_HARD:
3013 			G_MIRROR_DEBUG(1, "Device %s is still open, so it "
3014 			    "can't be definitely removed.", pp->name);
3015 		}
3016 	}
3017 
3018 	g_topology_lock();
3019 	if (sc->sc_geom->softc == NULL) {
3020 		g_topology_unlock();
3021 		return (0);
3022 	}
3023 	sc->sc_geom->softc = NULL;
3024 	sc->sc_sync.ds_geom->softc = NULL;
3025 	g_topology_unlock();
3026 
3027 	sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
3028 	sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
3029 	G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3030 	sx_xunlock(&sc->sc_lock);
3031 	mtx_lock(&sc->sc_queue_mtx);
3032 	wakeup(sc);
3033 	mtx_unlock(&sc->sc_queue_mtx);
3034 	G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3035 	while (sc->sc_worker != NULL)
3036 		tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
3037 	G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3038 	sx_xlock(&sc->sc_lock);
3039 	g_mirror_destroy_device(sc);
3040 	free(sc, M_MIRROR);
3041 	return (0);
3042 }
3043 
3044 static void
3045 g_mirror_taste_orphan(struct g_consumer *cp)
3046 {
3047 
3048 	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3049 	    cp->provider->name));
3050 }
3051 
3052 static struct g_geom *
3053 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3054 {
3055 	struct g_mirror_metadata md;
3056 	struct g_mirror_softc *sc;
3057 	struct g_consumer *cp;
3058 	struct g_geom *gp;
3059 	int error;
3060 
3061 	g_topology_assert();
3062 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3063 	G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3064 
3065 	gp = g_new_geomf(mp, "mirror:taste");
3066 	/*
3067 	 * This orphan function should be never called.
3068 	 */
3069 	gp->orphan = g_mirror_taste_orphan;
3070 	cp = g_new_consumer(gp);
3071 	g_attach(cp, pp);
3072 	error = g_mirror_read_metadata(cp, &md);
3073 	g_detach(cp);
3074 	g_destroy_consumer(cp);
3075 	g_destroy_geom(gp);
3076 	if (error != 0)
3077 		return (NULL);
3078 	gp = NULL;
3079 
3080 	if (md.md_provider[0] != '\0' &&
3081 	    !g_compare_names(md.md_provider, pp->name))
3082 		return (NULL);
3083 	if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3084 		return (NULL);
3085 	if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3086 		G_MIRROR_DEBUG(0,
3087 		    "Device %s: provider %s marked as inactive, skipping.",
3088 		    md.md_name, pp->name);
3089 		return (NULL);
3090 	}
3091 	if (g_mirror_debug >= 2)
3092 		mirror_metadata_dump(&md);
3093 
3094 	/*
3095 	 * Let's check if device already exists.
3096 	 */
3097 	sc = NULL;
3098 	LIST_FOREACH(gp, &mp->geom, geom) {
3099 		sc = gp->softc;
3100 		if (sc == NULL)
3101 			continue;
3102 		if (sc->sc_sync.ds_geom == gp)
3103 			continue;
3104 		if (strcmp(md.md_name, sc->sc_name) != 0)
3105 			continue;
3106 		if (md.md_mid != sc->sc_id) {
3107 			G_MIRROR_DEBUG(0, "Device %s already configured.",
3108 			    sc->sc_name);
3109 			return (NULL);
3110 		}
3111 		break;
3112 	}
3113 	if (gp == NULL) {
3114 		gp = g_mirror_create(mp, &md);
3115 		if (gp == NULL) {
3116 			G_MIRROR_DEBUG(0, "Cannot create device %s.",
3117 			    md.md_name);
3118 			return (NULL);
3119 		}
3120 		sc = gp->softc;
3121 	}
3122 	G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3123 	g_topology_unlock();
3124 	sx_xlock(&sc->sc_lock);
3125 	sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3126 	error = g_mirror_add_disk(sc, pp, &md);
3127 	if (error != 0) {
3128 		G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3129 		    pp->name, gp->name, error);
3130 		if (LIST_EMPTY(&sc->sc_disks)) {
3131 			g_cancel_event(sc);
3132 			g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3133 			g_topology_lock();
3134 			return (NULL);
3135 		}
3136 		gp = NULL;
3137 	}
3138 	sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3139 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3140 		g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3141 		g_topology_lock();
3142 		return (NULL);
3143 	}
3144 	sx_xunlock(&sc->sc_lock);
3145 	g_topology_lock();
3146 	return (gp);
3147 }
3148 
3149 static void
3150 g_mirror_resize(struct g_consumer *cp)
3151 {
3152 	struct g_mirror_disk *disk;
3153 
3154 	g_topology_assert();
3155 	g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name);
3156 
3157 	disk = cp->private;
3158 	if (disk == NULL)
3159 		return;
3160 	g_topology_unlock();
3161 	g_mirror_update_metadata(disk);
3162 	g_topology_lock();
3163 }
3164 
3165 static int
3166 g_mirror_destroy_geom(struct gctl_req *req __unused,
3167     struct g_class *mp __unused, struct g_geom *gp)
3168 {
3169 	struct g_mirror_softc *sc;
3170 	int error;
3171 
3172 	g_topology_unlock();
3173 	sc = gp->softc;
3174 	sx_xlock(&sc->sc_lock);
3175 	g_cancel_event(sc);
3176 	error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3177 	if (error != 0)
3178 		sx_xunlock(&sc->sc_lock);
3179 	g_topology_lock();
3180 	return (error);
3181 }
3182 
3183 static void
3184 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3185     struct g_consumer *cp, struct g_provider *pp)
3186 {
3187 	struct g_mirror_softc *sc;
3188 
3189 	g_topology_assert();
3190 
3191 	sc = gp->softc;
3192 	if (sc == NULL)
3193 		return;
3194 	/* Skip synchronization geom. */
3195 	if (gp == sc->sc_sync.ds_geom)
3196 		return;
3197 	if (pp != NULL) {
3198 		/* Nothing here. */
3199 	} else if (cp != NULL) {
3200 		struct g_mirror_disk *disk;
3201 
3202 		disk = cp->private;
3203 		if (disk == NULL)
3204 			return;
3205 		g_topology_unlock();
3206 		sx_xlock(&sc->sc_lock);
3207 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3208 		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3209 			sbuf_printf(sb, "%s<Synchronized>", indent);
3210 			if (disk->d_sync.ds_offset == 0)
3211 				sbuf_printf(sb, "0%%");
3212 			else {
3213 				sbuf_printf(sb, "%u%%",
3214 				    (u_int)((disk->d_sync.ds_offset * 100) /
3215 				    sc->sc_provider->mediasize));
3216 			}
3217 			sbuf_printf(sb, "</Synchronized>\n");
3218 			if (disk->d_sync.ds_offset > 0) {
3219 				sbuf_printf(sb, "%s<BytesSynced>%jd"
3220 				    "</BytesSynced>\n", indent,
3221 				    (intmax_t)disk->d_sync.ds_offset);
3222 			}
3223 		}
3224 		sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3225 		    disk->d_sync.ds_syncid);
3226 		sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3227 		    disk->d_genid);
3228 		sbuf_printf(sb, "%s<Flags>", indent);
3229 		if (disk->d_flags == 0)
3230 			sbuf_printf(sb, "NONE");
3231 		else {
3232 			int first = 1;
3233 
3234 #define	ADD_FLAG(flag, name)	do {					\
3235 	if ((disk->d_flags & (flag)) != 0) {				\
3236 		if (!first)						\
3237 			sbuf_printf(sb, ", ");				\
3238 		else							\
3239 			first = 0;					\
3240 		sbuf_printf(sb, name);					\
3241 	}								\
3242 } while (0)
3243 			ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3244 			ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3245 			ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3246 			ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3247 			    "SYNCHRONIZING");
3248 			ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3249 			ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3250 #undef	ADD_FLAG
3251 		}
3252 		sbuf_printf(sb, "</Flags>\n");
3253 		sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3254 		    disk->d_priority);
3255 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3256 		    g_mirror_disk_state2str(disk->d_state));
3257 		sx_xunlock(&sc->sc_lock);
3258 		g_topology_lock();
3259 	} else {
3260 		g_topology_unlock();
3261 		sx_xlock(&sc->sc_lock);
3262 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3263 		sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3264 		sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3265 		sbuf_printf(sb, "%s<Flags>", indent);
3266 		if (sc->sc_flags == 0)
3267 			sbuf_printf(sb, "NONE");
3268 		else {
3269 			int first = 1;
3270 
3271 #define	ADD_FLAG(flag, name)	do {					\
3272 	if ((sc->sc_flags & (flag)) != 0) {				\
3273 		if (!first)						\
3274 			sbuf_printf(sb, ", ");				\
3275 		else							\
3276 			first = 0;					\
3277 		sbuf_printf(sb, name);					\
3278 	}								\
3279 } while (0)
3280 			ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3281 			ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3282 #undef	ADD_FLAG
3283 		}
3284 		sbuf_printf(sb, "</Flags>\n");
3285 		sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3286 		    (u_int)sc->sc_slice);
3287 		sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3288 		    balance_name(sc->sc_balance));
3289 		sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3290 		    sc->sc_ndisks);
3291 		sbuf_printf(sb, "%s<State>", indent);
3292 		if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3293 			sbuf_printf(sb, "%s", "STARTING");
3294 		else if (sc->sc_ndisks ==
3295 		    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3296 			sbuf_printf(sb, "%s", "COMPLETE");
3297 		else
3298 			sbuf_printf(sb, "%s", "DEGRADED");
3299 		sbuf_printf(sb, "</State>\n");
3300 		sx_xunlock(&sc->sc_lock);
3301 		g_topology_lock();
3302 	}
3303 }
3304 
3305 static void
3306 g_mirror_shutdown_post_sync(void *arg, int howto)
3307 {
3308 	struct g_class *mp;
3309 	struct g_geom *gp, *gp2;
3310 	struct g_mirror_softc *sc;
3311 	int error;
3312 
3313 	mp = arg;
3314 	DROP_GIANT();
3315 	g_topology_lock();
3316 	g_mirror_shutdown = 1;
3317 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3318 		if ((sc = gp->softc) == NULL)
3319 			continue;
3320 		/* Skip synchronization geom. */
3321 		if (gp == sc->sc_sync.ds_geom)
3322 			continue;
3323 		g_topology_unlock();
3324 		sx_xlock(&sc->sc_lock);
3325 		g_mirror_idle(sc, -1);
3326 		g_cancel_event(sc);
3327 		error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3328 		if (error != 0)
3329 			sx_xunlock(&sc->sc_lock);
3330 		g_topology_lock();
3331 	}
3332 	g_topology_unlock();
3333 	PICKUP_GIANT();
3334 }
3335 
3336 static void
3337 g_mirror_init(struct g_class *mp)
3338 {
3339 
3340 	g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3341 	    g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3342 	if (g_mirror_post_sync == NULL)
3343 		G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3344 }
3345 
3346 static void
3347 g_mirror_fini(struct g_class *mp)
3348 {
3349 
3350 	if (g_mirror_post_sync != NULL)
3351 		EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3352 }
3353 
3354 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);
3355