1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2007 Lukas Ertl
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 AUTHOR 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 AUTHOR 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/param.h>
30 #include <sys/bio.h>
31 #include <sys/lock.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
34
35 #include <geom/geom.h>
36 #include <geom/vinum/geom_vinum_var.h>
37 #include <geom/vinum/geom_vinum.h>
38
39 void
gv_volume_flush(struct gv_volume * v)40 gv_volume_flush(struct gv_volume *v)
41 {
42 struct gv_softc *sc;
43 struct bio *bp;
44
45 KASSERT(v != NULL, ("NULL v"));
46 sc = v->vinumconf;
47 KASSERT(sc != NULL, ("NULL sc"));
48
49 bp = bioq_takefirst(v->wqueue);
50 while (bp != NULL) {
51 gv_volume_start(sc, bp);
52 bp = bioq_takefirst(v->wqueue);
53 }
54 }
55
56 void
gv_volume_start(struct gv_softc * sc,struct bio * bp)57 gv_volume_start(struct gv_softc *sc, struct bio *bp)
58 {
59 struct gv_volume *v;
60 struct gv_plex *p, *lp;
61 int numwrites;
62
63 v = bp->bio_to->private;
64 if (v == NULL || v->state != GV_VOL_UP) {
65 g_io_deliver(bp, ENXIO);
66 return;
67 }
68
69 switch (bp->bio_cmd) {
70 case BIO_READ:
71 /*
72 * Try to find a good plex where we can send the request to,
73 * round-robin-style. The plex either has to be up, or it's a
74 * degraded RAID5 plex. Check if we have delayed requests. Put
75 * this request on the delayed queue if so. This makes sure that
76 * we don't read old values.
77 */
78 if (bioq_first(v->wqueue) != NULL) {
79 bioq_insert_tail(v->wqueue, bp);
80 break;
81 }
82 lp = v->last_read_plex;
83 if (lp == NULL)
84 lp = LIST_FIRST(&v->plexes);
85 p = LIST_NEXT(lp, in_volume);
86 if (p == NULL)
87 p = LIST_FIRST(&v->plexes);
88 do {
89 if (p == NULL) {
90 p = lp;
91 break;
92 }
93 if ((p->state > GV_PLEX_DEGRADED) ||
94 (p->state >= GV_PLEX_DEGRADED &&
95 p->org == GV_PLEX_RAID5))
96 break;
97 p = LIST_NEXT(p, in_volume);
98 if (p == NULL)
99 p = LIST_FIRST(&v->plexes);
100 } while (p != lp);
101
102 if ((p == NULL) ||
103 (p->org == GV_PLEX_RAID5 && p->state < GV_PLEX_DEGRADED) ||
104 (p->org != GV_PLEX_RAID5 && p->state <= GV_PLEX_DEGRADED)) {
105 g_io_deliver(bp, ENXIO);
106 return;
107 }
108 v->last_read_plex = p;
109
110 /* Hand it down to the plex logic. */
111 gv_plex_start(p, bp);
112 break;
113
114 case BIO_WRITE:
115 case BIO_DELETE:
116 /* Delay write-requests if any plex is synchronizing. */
117 LIST_FOREACH(p, &v->plexes, in_volume) {
118 if (p->flags & GV_PLEX_SYNCING) {
119 bioq_insert_tail(v->wqueue, bp);
120 return;
121 }
122 }
123
124 numwrites = 0;
125 /* Give the BIO to each plex of this volume. */
126 LIST_FOREACH(p, &v->plexes, in_volume) {
127 if (p->state < GV_PLEX_DEGRADED)
128 continue;
129 gv_plex_start(p, bp);
130 numwrites++;
131 }
132 if (numwrites == 0)
133 g_io_deliver(bp, ENXIO);
134 break;
135 }
136 }
137
138 void
gv_bio_done(struct gv_softc * sc,struct bio * bp)139 gv_bio_done(struct gv_softc *sc, struct bio *bp)
140 {
141 struct gv_volume *v __diagused;
142 struct gv_plex *p;
143 struct gv_sd *s;
144
145 s = bp->bio_caller1;
146 KASSERT(s != NULL, ("gv_bio_done: NULL s"));
147 p = s->plex_sc;
148 KASSERT(p != NULL, ("gv_bio_done: NULL p"));
149 v = p->vol_sc;
150 KASSERT(v != NULL, ("gv_bio_done: NULL v"));
151
152 switch (p->org) {
153 case GV_PLEX_CONCAT:
154 case GV_PLEX_STRIPED:
155 gv_plex_normal_done(p, bp);
156 break;
157 case GV_PLEX_RAID5:
158 gv_plex_raid5_done(p, bp);
159 break;
160 }
161
162 gv_drive_done(s->drive_sc);
163 }
164