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_CONCAT, "tr_concat_data", "GEOM_RAID CONCAT data");
45 struct g_raid_tr_concat_object {
46 struct g_raid_tr_object trso_base;
51 static g_raid_tr_taste_t g_raid_tr_taste_concat;
52 static g_raid_tr_event_t g_raid_tr_event_concat;
53 static g_raid_tr_start_t g_raid_tr_start_concat;
54 static g_raid_tr_stop_t g_raid_tr_stop_concat;
55 static g_raid_tr_iostart_t g_raid_tr_iostart_concat;
56 static g_raid_tr_iodone_t g_raid_tr_iodone_concat;
57 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_concat;
58 static g_raid_tr_free_t g_raid_tr_free_concat;
60 static kobj_method_t g_raid_tr_concat_methods[] = {
61 KOBJMETHOD(g_raid_tr_taste, g_raid_tr_taste_concat),
62 KOBJMETHOD(g_raid_tr_event, g_raid_tr_event_concat),
63 KOBJMETHOD(g_raid_tr_start, g_raid_tr_start_concat),
64 KOBJMETHOD(g_raid_tr_stop, g_raid_tr_stop_concat),
65 KOBJMETHOD(g_raid_tr_iostart, g_raid_tr_iostart_concat),
66 KOBJMETHOD(g_raid_tr_iodone, g_raid_tr_iodone_concat),
67 KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_concat),
68 KOBJMETHOD(g_raid_tr_free, g_raid_tr_free_concat),
72 static struct g_raid_tr_class g_raid_tr_concat_class = {
74 g_raid_tr_concat_methods,
75 sizeof(struct g_raid_tr_concat_object),
81 g_raid_tr_taste_concat(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
83 struct g_raid_tr_concat_object *trs;
85 trs = (struct g_raid_tr_concat_object *)tr;
86 if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_SINGLE &&
87 tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
88 !(tr->tro_volume->v_disks_count == 1 &&
89 tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_UNKNOWN))
90 return (G_RAID_TR_TASTE_FAIL);
91 trs->trso_starting = 1;
92 return (G_RAID_TR_TASTE_SUCCEED);
96 g_raid_tr_update_state_concat(struct g_raid_volume *vol)
98 struct g_raid_tr_concat_object *trs;
99 struct g_raid_softc *sc;
105 trs = (struct g_raid_tr_concat_object *)vol->v_tr;
106 if (trs->trso_stopped)
107 s = G_RAID_VOLUME_S_STOPPED;
108 else if (trs->trso_starting)
109 s = G_RAID_VOLUME_S_STARTING;
111 n = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
112 f = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_FAILED);
113 if (n + f == vol->v_disks_count) {
115 s = G_RAID_VOLUME_S_OPTIMAL;
117 s = G_RAID_VOLUME_S_SUBOPTIMAL;
119 s = G_RAID_VOLUME_S_BROKEN;
121 if (s != vol->v_state) {
124 * Some metadata modules may not know CONCAT volume
125 * mediasize until all disks connected. Recalculate.
127 if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT &&
128 G_RAID_VOLUME_S_ALIVE(s) &&
129 !G_RAID_VOLUME_S_ALIVE(vol->v_state)) {
131 for (i = 0; i < vol->v_disks_count; i++) {
132 if (vol->v_subdisks[i].sd_state !=
133 G_RAID_SUBDISK_S_NONE)
134 size += vol->v_subdisks[i].sd_size;
136 vol->v_mediasize = size;
139 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
140 G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
141 G_RAID_EVENT_VOLUME);
142 g_raid_change_volume_state(vol, s);
143 if (!trs->trso_starting && !trs->trso_stopped)
144 g_raid_write_metadata(sc, vol, NULL, NULL);
150 g_raid_tr_event_concat(struct g_raid_tr_object *tr,
151 struct g_raid_subdisk *sd, u_int event)
153 struct g_raid_tr_concat_object *trs;
154 struct g_raid_softc *sc;
155 struct g_raid_volume *vol;
158 trs = (struct g_raid_tr_concat_object *)tr;
159 vol = tr->tro_volume;
162 state = sd->sd_state;
163 if (state != G_RAID_SUBDISK_S_NONE &&
164 state != G_RAID_SUBDISK_S_FAILED &&
165 state != G_RAID_SUBDISK_S_ACTIVE) {
167 "Promote subdisk %s:%d from %s to ACTIVE.",
168 vol->v_name, sd->sd_pos,
169 g_raid_subdisk_state2str(sd->sd_state));
170 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
172 if (state != sd->sd_state &&
173 !trs->trso_starting && !trs->trso_stopped)
174 g_raid_write_metadata(sc, vol, sd, NULL);
175 g_raid_tr_update_state_concat(vol);
180 g_raid_tr_start_concat(struct g_raid_tr_object *tr)
182 struct g_raid_tr_concat_object *trs;
183 struct g_raid_volume *vol;
185 trs = (struct g_raid_tr_concat_object *)tr;
186 vol = tr->tro_volume;
187 trs->trso_starting = 0;
188 g_raid_tr_update_state_concat(vol);
193 g_raid_tr_stop_concat(struct g_raid_tr_object *tr)
195 struct g_raid_tr_concat_object *trs;
196 struct g_raid_volume *vol;
198 trs = (struct g_raid_tr_concat_object *)tr;
199 vol = tr->tro_volume;
200 trs->trso_starting = 0;
201 trs->trso_stopped = 1;
202 g_raid_tr_update_state_concat(vol);
207 g_raid_tr_iostart_concat(struct g_raid_tr_object *tr, struct bio *bp)
209 struct g_raid_volume *vol;
210 struct g_raid_subdisk *sd;
211 struct bio_queue_head queue;
214 off_t offset, length, remain;
217 vol = tr->tro_volume;
218 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
219 vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) {
220 g_raid_iodone(bp, EIO);
223 if (bp->bio_cmd == BIO_FLUSH) {
224 g_raid_tr_flush_common(tr, bp);
228 offset = bp->bio_offset;
229 remain = bp->bio_length;
232 while (no < vol->v_disks_count &&
233 offset >= vol->v_subdisks[no].sd_size) {
234 offset -= vol->v_subdisks[no].sd_size;
237 KASSERT(no < vol->v_disks_count,
238 ("Request starts after volume end (%ju)", bp->bio_offset));
241 sd = &vol->v_subdisks[no];
242 length = MIN(sd->sd_size - offset, remain);
243 cbp = g_clone_bio(bp);
246 cbp->bio_offset = offset;
247 cbp->bio_data = addr;
248 cbp->bio_length = length;
249 cbp->bio_caller1 = sd;
250 bioq_insert_tail(&queue, cbp);
252 if (bp->bio_cmd != BIO_DELETE)
256 KASSERT(no < vol->v_disks_count || remain == 0,
257 ("Request ends after volume end (%ju, %ju)",
258 bp->bio_offset, bp->bio_length));
259 } while (remain > 0);
260 for (cbp = bioq_first(&queue); cbp != NULL;
261 cbp = bioq_first(&queue)) {
262 bioq_remove(&queue, cbp);
263 sd = cbp->bio_caller1;
264 cbp->bio_caller1 = NULL;
265 g_raid_subdisk_iostart(sd, cbp);
269 for (cbp = bioq_first(&queue); cbp != NULL;
270 cbp = bioq_first(&queue)) {
271 bioq_remove(&queue, cbp);
274 if (bp->bio_error == 0)
275 bp->bio_error = ENOMEM;
276 g_raid_iodone(bp, bp->bio_error);
280 g_raid_tr_kerneldump_concat(struct g_raid_tr_object *tr,
281 void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
283 struct g_raid_volume *vol;
284 struct g_raid_subdisk *sd;
286 off_t offset, length, remain;
289 vol = tr->tro_volume;
290 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
297 while (no < vol->v_disks_count &&
298 offset >= vol->v_subdisks[no].sd_size) {
299 offset -= vol->v_subdisks[no].sd_size;
302 KASSERT(no < vol->v_disks_count,
303 ("Request starts after volume end (%ju)", boffset));
305 sd = &vol->v_subdisks[no];
306 length = MIN(sd->sd_size - offset, remain);
307 error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
308 addr, 0, offset, length);
315 KASSERT(no < vol->v_disks_count || remain == 0,
316 ("Request ends after volume end (%ju, %zu)",
318 } while (remain > 0);
323 g_raid_tr_iodone_concat(struct g_raid_tr_object *tr,
324 struct g_raid_subdisk *sd,struct bio *bp)
328 pbp = bp->bio_parent;
329 if (pbp->bio_error == 0)
330 pbp->bio_error = bp->bio_error;
333 if (pbp->bio_children == pbp->bio_inbed) {
334 pbp->bio_completed = pbp->bio_length;
335 g_raid_iodone(pbp, bp->bio_error);
340 g_raid_tr_free_concat(struct g_raid_tr_object *tr)
346 G_RAID_TR_DECLARE(concat, "CONCAT");