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