2 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
39 #include <geom/geom.h>
40 #include "geom/raid/g_raid.h"
41 #include "g_raid_tr_if.h"
43 static MALLOC_DEFINE(M_TR_RAID0, "tr_raid0_data", "GEOM_RAID RAID0 data");
45 struct g_raid_tr_raid0_object {
46 struct g_raid_tr_object trso_base;
51 static g_raid_tr_taste_t g_raid_tr_taste_raid0;
52 static g_raid_tr_event_t g_raid_tr_event_raid0;
53 static g_raid_tr_start_t g_raid_tr_start_raid0;
54 static g_raid_tr_stop_t g_raid_tr_stop_raid0;
55 static g_raid_tr_iostart_t g_raid_tr_iostart_raid0;
56 static g_raid_tr_iodone_t g_raid_tr_iodone_raid0;
57 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid0;
58 static g_raid_tr_free_t g_raid_tr_free_raid0;
60 static kobj_method_t g_raid_tr_raid0_methods[] = {
61 KOBJMETHOD(g_raid_tr_taste, g_raid_tr_taste_raid0),
62 KOBJMETHOD(g_raid_tr_event, g_raid_tr_event_raid0),
63 KOBJMETHOD(g_raid_tr_start, g_raid_tr_start_raid0),
64 KOBJMETHOD(g_raid_tr_stop, g_raid_tr_stop_raid0),
65 KOBJMETHOD(g_raid_tr_iostart, g_raid_tr_iostart_raid0),
66 KOBJMETHOD(g_raid_tr_iodone, g_raid_tr_iodone_raid0),
67 KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid0),
68 KOBJMETHOD(g_raid_tr_free, g_raid_tr_free_raid0),
72 static struct g_raid_tr_class g_raid_tr_raid0_class = {
74 g_raid_tr_raid0_methods,
75 sizeof(struct g_raid_tr_raid0_object),
80 g_raid_tr_taste_raid0(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
82 struct g_raid_tr_raid0_object *trs;
84 trs = (struct g_raid_tr_raid0_object *)tr;
85 if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_RAID0 ||
86 tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_NONE)
87 return (G_RAID_TR_TASTE_FAIL);
88 trs->trso_starting = 1;
89 return (G_RAID_TR_TASTE_SUCCEED);
93 g_raid_tr_update_state_raid0(struct g_raid_volume *vol)
95 struct g_raid_tr_raid0_object *trs;
96 struct g_raid_softc *sc;
101 trs = (struct g_raid_tr_raid0_object *)vol->v_tr;
102 if (trs->trso_stopped)
103 s = G_RAID_VOLUME_S_STOPPED;
104 else if (trs->trso_starting)
105 s = G_RAID_VOLUME_S_STARTING;
107 n = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
108 f = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_FAILED);
109 if (n + f == vol->v_disks_count) {
111 s = G_RAID_VOLUME_S_OPTIMAL;
113 s = G_RAID_VOLUME_S_SUBOPTIMAL;
115 s = G_RAID_VOLUME_S_BROKEN;
117 if (s != vol->v_state) {
118 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
119 G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
120 G_RAID_EVENT_VOLUME);
121 g_raid_change_volume_state(vol, s);
122 if (!trs->trso_starting && !trs->trso_stopped)
123 g_raid_write_metadata(sc, vol, NULL, NULL);
129 g_raid_tr_event_raid0(struct g_raid_tr_object *tr,
130 struct g_raid_subdisk *sd, u_int event)
132 struct g_raid_tr_raid0_object *trs;
133 struct g_raid_softc *sc;
134 struct g_raid_volume *vol;
137 trs = (struct g_raid_tr_raid0_object *)tr;
138 vol = tr->tro_volume;
141 state = sd->sd_state;
142 if (state != G_RAID_SUBDISK_S_NONE &&
143 state != G_RAID_SUBDISK_S_FAILED &&
144 state != G_RAID_SUBDISK_S_ACTIVE) {
146 "Promote subdisk %s:%d from %s to ACTIVE.",
147 vol->v_name, sd->sd_pos,
148 g_raid_subdisk_state2str(sd->sd_state));
149 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
151 if (state != sd->sd_state &&
152 !trs->trso_starting && !trs->trso_stopped)
153 g_raid_write_metadata(sc, vol, sd, NULL);
154 g_raid_tr_update_state_raid0(vol);
159 g_raid_tr_start_raid0(struct g_raid_tr_object *tr)
161 struct g_raid_tr_raid0_object *trs;
162 struct g_raid_volume *vol;
164 trs = (struct g_raid_tr_raid0_object *)tr;
165 vol = tr->tro_volume;
166 trs->trso_starting = 0;
167 g_raid_tr_update_state_raid0(vol);
172 g_raid_tr_stop_raid0(struct g_raid_tr_object *tr)
174 struct g_raid_tr_raid0_object *trs;
175 struct g_raid_volume *vol;
177 trs = (struct g_raid_tr_raid0_object *)tr;
178 vol = tr->tro_volume;
179 trs->trso_starting = 0;
180 trs->trso_stopped = 1;
181 g_raid_tr_update_state_raid0(vol);
186 g_raid_tr_iostart_raid0(struct g_raid_tr_object *tr, struct bio *bp)
188 struct g_raid_volume *vol;
189 struct g_raid_subdisk *sd;
190 struct bio_queue_head queue;
193 off_t offset, start, length, nstripe, remain;
194 u_int no, strip_size;
196 vol = tr->tro_volume;
197 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
198 vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) {
199 g_raid_iodone(bp, EIO);
202 if (bp->bio_cmd == BIO_FLUSH) {
203 g_raid_tr_flush_common(tr, bp);
207 strip_size = vol->v_strip_size;
210 nstripe = bp->bio_offset / strip_size;
211 /* Start position in stripe. */
212 start = bp->bio_offset % strip_size;
214 no = nstripe % vol->v_disks_count;
215 /* Stripe start position in disk. */
216 offset = (nstripe / vol->v_disks_count) * strip_size;
217 /* Length of data to operate. */
218 remain = bp->bio_length;
222 length = MIN(strip_size - start, remain);
223 cbp = g_clone_bio(bp);
226 cbp->bio_offset = offset + start;
227 cbp->bio_data = addr;
228 cbp->bio_length = length;
229 cbp->bio_caller1 = &vol->v_subdisks[no];
230 bioq_insert_tail(&queue, cbp);
231 if (++no >= vol->v_disks_count) {
233 offset += strip_size;
238 } while (remain > 0);
239 for (cbp = bioq_first(&queue); cbp != NULL;
240 cbp = bioq_first(&queue)) {
241 bioq_remove(&queue, cbp);
242 sd = cbp->bio_caller1;
243 cbp->bio_caller1 = NULL;
244 g_raid_subdisk_iostart(sd, cbp);
248 for (cbp = bioq_first(&queue); cbp != NULL;
249 cbp = bioq_first(&queue)) {
250 bioq_remove(&queue, cbp);
253 if (bp->bio_error == 0)
254 bp->bio_error = ENOMEM;
255 g_raid_iodone(bp, bp->bio_error);
259 g_raid_tr_kerneldump_raid0(struct g_raid_tr_object *tr,
260 void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
262 struct g_raid_volume *vol;
264 off_t offset, start, length, nstripe, remain;
265 u_int no, strip_size;
268 vol = tr->tro_volume;
269 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
272 strip_size = vol->v_strip_size;
275 nstripe = boffset / strip_size;
276 /* Start position in stripe. */
277 start = boffset % strip_size;
279 no = nstripe % vol->v_disks_count;
280 /* Stripe tart position in disk. */
281 offset = (nstripe / vol->v_disks_count) * strip_size;
282 /* Length of data to operate. */
286 length = MIN(strip_size - start, remain);
287 error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
288 addr, 0, offset + start, length);
291 if (++no >= vol->v_disks_count) {
293 offset += strip_size;
298 } while (remain > 0);
303 g_raid_tr_iodone_raid0(struct g_raid_tr_object *tr,
304 struct g_raid_subdisk *sd,struct bio *bp)
308 pbp = bp->bio_parent;
309 if (pbp->bio_error == 0)
310 pbp->bio_error = bp->bio_error;
313 if (pbp->bio_children == pbp->bio_inbed) {
314 pbp->bio_completed = pbp->bio_length;
315 g_raid_iodone(pbp, bp->bio_error);
320 g_raid_tr_free_raid0(struct g_raid_tr_object *tr)
326 G_RAID_TR_DECLARE(g_raid_tr_raid0);