2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
41 #include <geom/geom.h>
42 #include "geom/raid/g_raid.h"
43 #include "g_raid_tr_if.h"
45 static MALLOC_DEFINE(M_TR_RAID0, "tr_raid0_data", "GEOM_RAID RAID0 data");
47 struct g_raid_tr_raid0_object {
48 struct g_raid_tr_object trso_base;
53 static g_raid_tr_taste_t g_raid_tr_taste_raid0;
54 static g_raid_tr_event_t g_raid_tr_event_raid0;
55 static g_raid_tr_start_t g_raid_tr_start_raid0;
56 static g_raid_tr_stop_t g_raid_tr_stop_raid0;
57 static g_raid_tr_iostart_t g_raid_tr_iostart_raid0;
58 static g_raid_tr_iodone_t g_raid_tr_iodone_raid0;
59 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid0;
60 static g_raid_tr_free_t g_raid_tr_free_raid0;
62 static kobj_method_t g_raid_tr_raid0_methods[] = {
63 KOBJMETHOD(g_raid_tr_taste, g_raid_tr_taste_raid0),
64 KOBJMETHOD(g_raid_tr_event, g_raid_tr_event_raid0),
65 KOBJMETHOD(g_raid_tr_start, g_raid_tr_start_raid0),
66 KOBJMETHOD(g_raid_tr_stop, g_raid_tr_stop_raid0),
67 KOBJMETHOD(g_raid_tr_iostart, g_raid_tr_iostart_raid0),
68 KOBJMETHOD(g_raid_tr_iodone, g_raid_tr_iodone_raid0),
69 KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid0),
70 KOBJMETHOD(g_raid_tr_free, g_raid_tr_free_raid0),
74 static struct g_raid_tr_class g_raid_tr_raid0_class = {
76 g_raid_tr_raid0_methods,
77 sizeof(struct g_raid_tr_raid0_object),
80 .trc_accept_unmapped = 1
84 g_raid_tr_taste_raid0(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
86 struct g_raid_tr_raid0_object *trs;
88 trs = (struct g_raid_tr_raid0_object *)tr;
89 if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_RAID0 ||
90 tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_NONE)
91 return (G_RAID_TR_TASTE_FAIL);
92 trs->trso_starting = 1;
93 return (G_RAID_TR_TASTE_SUCCEED);
97 g_raid_tr_update_state_raid0(struct g_raid_volume *vol)
99 struct g_raid_tr_raid0_object *trs;
100 struct g_raid_softc *sc;
105 trs = (struct g_raid_tr_raid0_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) {
122 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
123 G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
124 G_RAID_EVENT_VOLUME);
125 g_raid_change_volume_state(vol, s);
126 if (!trs->trso_starting && !trs->trso_stopped)
127 g_raid_write_metadata(sc, vol, NULL, NULL);
133 g_raid_tr_event_raid0(struct g_raid_tr_object *tr,
134 struct g_raid_subdisk *sd, u_int event)
136 struct g_raid_tr_raid0_object *trs;
137 struct g_raid_softc *sc;
138 struct g_raid_volume *vol;
141 trs = (struct g_raid_tr_raid0_object *)tr;
142 vol = tr->tro_volume;
145 state = sd->sd_state;
146 if (state != G_RAID_SUBDISK_S_NONE &&
147 state != G_RAID_SUBDISK_S_FAILED &&
148 state != G_RAID_SUBDISK_S_ACTIVE) {
150 "Promote subdisk %s:%d from %s to ACTIVE.",
151 vol->v_name, sd->sd_pos,
152 g_raid_subdisk_state2str(sd->sd_state));
153 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
155 if (state != sd->sd_state &&
156 !trs->trso_starting && !trs->trso_stopped)
157 g_raid_write_metadata(sc, vol, sd, NULL);
158 g_raid_tr_update_state_raid0(vol);
163 g_raid_tr_start_raid0(struct g_raid_tr_object *tr)
165 struct g_raid_tr_raid0_object *trs;
166 struct g_raid_volume *vol;
168 trs = (struct g_raid_tr_raid0_object *)tr;
169 vol = tr->tro_volume;
170 trs->trso_starting = 0;
171 g_raid_tr_update_state_raid0(vol);
176 g_raid_tr_stop_raid0(struct g_raid_tr_object *tr)
178 struct g_raid_tr_raid0_object *trs;
179 struct g_raid_volume *vol;
181 trs = (struct g_raid_tr_raid0_object *)tr;
182 vol = tr->tro_volume;
183 trs->trso_starting = 0;
184 trs->trso_stopped = 1;
185 g_raid_tr_update_state_raid0(vol);
190 g_raid_tr_iostart_raid0(struct g_raid_tr_object *tr, struct bio *bp)
192 struct g_raid_volume *vol;
193 struct g_raid_subdisk *sd;
194 struct bio_queue_head queue;
197 off_t offset, start, length, nstripe, remain;
198 u_int no, strip_size;
200 vol = tr->tro_volume;
201 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
202 vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) {
203 g_raid_iodone(bp, EIO);
206 if (bp->bio_cmd == BIO_FLUSH) {
207 g_raid_tr_flush_common(tr, bp);
210 if ((bp->bio_flags & BIO_UNMAPPED) != 0)
214 strip_size = vol->v_strip_size;
217 nstripe = bp->bio_offset / strip_size;
218 /* Start position in stripe. */
219 start = bp->bio_offset % strip_size;
221 no = nstripe % vol->v_disks_count;
222 /* Stripe start position in disk. */
223 offset = (nstripe / vol->v_disks_count) * strip_size;
224 /* Length of data to operate. */
225 remain = bp->bio_length;
229 length = MIN(strip_size - start, remain);
230 cbp = g_clone_bio(bp);
233 cbp->bio_offset = offset + start;
234 cbp->bio_length = length;
235 if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
236 bp->bio_cmd != BIO_DELETE) {
237 cbp->bio_ma_offset += (uintptr_t)addr;
238 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
239 cbp->bio_ma_offset %= PAGE_SIZE;
240 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
241 cbp->bio_length) / PAGE_SIZE;
243 cbp->bio_data = addr;
244 cbp->bio_caller1 = &vol->v_subdisks[no];
245 bioq_insert_tail(&queue, cbp);
246 if (++no >= vol->v_disks_count) {
248 offset += strip_size;
251 if (bp->bio_cmd != BIO_DELETE)
254 } while (remain > 0);
255 while ((cbp = bioq_takefirst(&queue)) != NULL) {
256 sd = cbp->bio_caller1;
257 cbp->bio_caller1 = NULL;
258 g_raid_subdisk_iostart(sd, cbp);
262 while ((cbp = bioq_takefirst(&queue)) != NULL)
264 if (bp->bio_error == 0)
265 bp->bio_error = ENOMEM;
266 g_raid_iodone(bp, bp->bio_error);
270 g_raid_tr_kerneldump_raid0(struct g_raid_tr_object *tr,
271 void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
273 struct g_raid_volume *vol;
275 off_t offset, start, length, nstripe, remain;
276 u_int no, strip_size;
279 vol = tr->tro_volume;
280 if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
283 strip_size = vol->v_strip_size;
286 nstripe = boffset / strip_size;
287 /* Start position in stripe. */
288 start = boffset % strip_size;
290 no = nstripe % vol->v_disks_count;
291 /* Stripe tart position in disk. */
292 offset = (nstripe / vol->v_disks_count) * strip_size;
293 /* Length of data to operate. */
297 length = MIN(strip_size - start, remain);
298 error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
299 addr, 0, offset + start, length);
302 if (++no >= vol->v_disks_count) {
304 offset += strip_size;
309 } while (remain > 0);
314 g_raid_tr_iodone_raid0(struct g_raid_tr_object *tr,
315 struct g_raid_subdisk *sd,struct bio *bp)
319 pbp = bp->bio_parent;
320 if (pbp->bio_error == 0)
321 pbp->bio_error = bp->bio_error;
324 if (pbp->bio_children == pbp->bio_inbed) {
325 pbp->bio_completed = pbp->bio_length;
326 g_raid_iodone(pbp, pbp->bio_error);
331 g_raid_tr_free_raid0(struct g_raid_tr_object *tr)
337 G_RAID_TR_DECLARE(raid0, "RAID0");