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),
81 g_raid_tr_taste_raid0(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
83 struct g_raid_tr_raid0_object *trs;
85 trs = (struct g_raid_tr_raid0_object *)tr;
86 if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_RAID0 ||
87 tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_NONE)
88 return (G_RAID_TR_TASTE_FAIL);
89 trs->trso_starting = 1;
90 return (G_RAID_TR_TASTE_SUCCEED);
94 g_raid_tr_update_state_raid0(struct g_raid_volume *vol)
96 struct g_raid_tr_raid0_object *trs;
97 struct g_raid_softc *sc;
102 trs = (struct g_raid_tr_raid0_object *)vol->v_tr;
103 if (trs->trso_stopped)
104 s = G_RAID_VOLUME_S_STOPPED;
105 else if (trs->trso_starting)
106 s = G_RAID_VOLUME_S_STARTING;
108 n = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
109 f = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_FAILED);
110 if (n + f == vol->v_disks_count) {
112 s = G_RAID_VOLUME_S_OPTIMAL;
114 s = G_RAID_VOLUME_S_SUBOPTIMAL;
116 s = G_RAID_VOLUME_S_BROKEN;
118 if (s != vol->v_state) {
119 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
120 G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
121 G_RAID_EVENT_VOLUME);
122 g_raid_change_volume_state(vol, s);
123 if (!trs->trso_starting && !trs->trso_stopped)
124 g_raid_write_metadata(sc, vol, NULL, NULL);
130 g_raid_tr_event_raid0(struct g_raid_tr_object *tr,
131 struct g_raid_subdisk *sd, u_int event)
133 struct g_raid_tr_raid0_object *trs;
134 struct g_raid_softc *sc;
135 struct g_raid_volume *vol;
138 trs = (struct g_raid_tr_raid0_object *)tr;
139 vol = tr->tro_volume;
142 state = sd->sd_state;
143 if (state != G_RAID_SUBDISK_S_NONE &&
144 state != G_RAID_SUBDISK_S_FAILED &&
145 state != G_RAID_SUBDISK_S_ACTIVE) {
147 "Promote subdisk %s:%d from %s to ACTIVE.",
148 vol->v_name, sd->sd_pos,
149 g_raid_subdisk_state2str(sd->sd_state));
150 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
152 if (state != sd->sd_state &&
153 !trs->trso_starting && !trs->trso_stopped)
154 g_raid_write_metadata(sc, vol, sd, NULL);
155 g_raid_tr_update_state_raid0(vol);
160 g_raid_tr_start_raid0(struct g_raid_tr_object *tr)
162 struct g_raid_tr_raid0_object *trs;
163 struct g_raid_volume *vol;
165 trs = (struct g_raid_tr_raid0_object *)tr;
166 vol = tr->tro_volume;
167 trs->trso_starting = 0;
168 g_raid_tr_update_state_raid0(vol);
173 g_raid_tr_stop_raid0(struct g_raid_tr_object *tr)
175 struct g_raid_tr_raid0_object *trs;
176 struct g_raid_volume *vol;
178 trs = (struct g_raid_tr_raid0_object *)tr;
179 vol = tr->tro_volume;
180 trs->trso_starting = 0;
181 trs->trso_stopped = 1;
182 g_raid_tr_update_state_raid0(vol);
187 g_raid_tr_iostart_raid0(struct g_raid_tr_object *tr, struct bio *bp)
189 struct g_raid_volume *vol;
190 struct g_raid_subdisk *sd;
191 struct bio_queue_head queue;
194 off_t offset, start, length, nstripe, remain;
195 u_int no, strip_size;
197 vol = tr->tro_volume;
198 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
199 vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) {
200 g_raid_iodone(bp, EIO);
203 if (bp->bio_cmd == BIO_FLUSH) {
204 g_raid_tr_flush_common(tr, bp);
208 strip_size = vol->v_strip_size;
211 nstripe = bp->bio_offset / strip_size;
212 /* Start position in stripe. */
213 start = bp->bio_offset % strip_size;
215 no = nstripe % vol->v_disks_count;
216 /* Stripe start position in disk. */
217 offset = (nstripe / vol->v_disks_count) * strip_size;
218 /* Length of data to operate. */
219 remain = bp->bio_length;
223 length = MIN(strip_size - start, remain);
224 cbp = g_clone_bio(bp);
227 cbp->bio_offset = offset + start;
228 cbp->bio_data = addr;
229 cbp->bio_length = length;
230 cbp->bio_caller1 = &vol->v_subdisks[no];
231 bioq_insert_tail(&queue, cbp);
232 if (++no >= vol->v_disks_count) {
234 offset += strip_size;
237 if (bp->bio_cmd != BIO_DELETE)
240 } while (remain > 0);
241 for (cbp = bioq_first(&queue); cbp != NULL;
242 cbp = bioq_first(&queue)) {
243 bioq_remove(&queue, cbp);
244 sd = cbp->bio_caller1;
245 cbp->bio_caller1 = NULL;
246 g_raid_subdisk_iostart(sd, cbp);
250 for (cbp = bioq_first(&queue); cbp != NULL;
251 cbp = bioq_first(&queue)) {
252 bioq_remove(&queue, cbp);
255 if (bp->bio_error == 0)
256 bp->bio_error = ENOMEM;
257 g_raid_iodone(bp, bp->bio_error);
261 g_raid_tr_kerneldump_raid0(struct g_raid_tr_object *tr,
262 void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
264 struct g_raid_volume *vol;
266 off_t offset, start, length, nstripe, remain;
267 u_int no, strip_size;
270 vol = tr->tro_volume;
271 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
274 strip_size = vol->v_strip_size;
277 nstripe = boffset / strip_size;
278 /* Start position in stripe. */
279 start = boffset % strip_size;
281 no = nstripe % vol->v_disks_count;
282 /* Stripe tart position in disk. */
283 offset = (nstripe / vol->v_disks_count) * strip_size;
284 /* Length of data to operate. */
288 length = MIN(strip_size - start, remain);
289 error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
290 addr, 0, offset + start, length);
293 if (++no >= vol->v_disks_count) {
295 offset += strip_size;
300 } while (remain > 0);
305 g_raid_tr_iodone_raid0(struct g_raid_tr_object *tr,
306 struct g_raid_subdisk *sd,struct bio *bp)
310 pbp = bp->bio_parent;
311 if (pbp->bio_error == 0)
312 pbp->bio_error = bp->bio_error;
315 if (pbp->bio_children == pbp->bio_inbed) {
316 pbp->bio_completed = pbp->bio_length;
317 g_raid_iodone(pbp, bp->bio_error);
322 g_raid_tr_free_raid0(struct g_raid_tr_object *tr)
328 G_RAID_TR_DECLARE(raid0, "RAID0");