xref: /freebsd/sys/geom/raid/tr_raid0.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
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 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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/kobj.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
39 #include <geom/geom.h>
40 #include <geom/geom_dbg.h>
41 #include "geom/raid/g_raid.h"
42 #include "g_raid_tr_if.h"
43 
44 static MALLOC_DEFINE(M_TR_RAID0, "tr_raid0_data", "GEOM_RAID RAID0 data");
45 
46 struct g_raid_tr_raid0_object {
47 	struct g_raid_tr_object	 trso_base;
48 	int			 trso_starting;
49 	int			 trso_stopped;
50 };
51 
52 static g_raid_tr_taste_t g_raid_tr_taste_raid0;
53 static g_raid_tr_event_t g_raid_tr_event_raid0;
54 static g_raid_tr_start_t g_raid_tr_start_raid0;
55 static g_raid_tr_stop_t g_raid_tr_stop_raid0;
56 static g_raid_tr_iostart_t g_raid_tr_iostart_raid0;
57 static g_raid_tr_iodone_t g_raid_tr_iodone_raid0;
58 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid0;
59 static g_raid_tr_free_t g_raid_tr_free_raid0;
60 
61 static kobj_method_t g_raid_tr_raid0_methods[] = {
62 	KOBJMETHOD(g_raid_tr_taste,	g_raid_tr_taste_raid0),
63 	KOBJMETHOD(g_raid_tr_event,	g_raid_tr_event_raid0),
64 	KOBJMETHOD(g_raid_tr_start,	g_raid_tr_start_raid0),
65 	KOBJMETHOD(g_raid_tr_stop,	g_raid_tr_stop_raid0),
66 	KOBJMETHOD(g_raid_tr_iostart,	g_raid_tr_iostart_raid0),
67 	KOBJMETHOD(g_raid_tr_iodone,	g_raid_tr_iodone_raid0),
68 	KOBJMETHOD(g_raid_tr_kerneldump,	g_raid_tr_kerneldump_raid0),
69 	KOBJMETHOD(g_raid_tr_free,	g_raid_tr_free_raid0),
70 	{ 0, 0 }
71 };
72 
73 static struct g_raid_tr_class g_raid_tr_raid0_class = {
74 	"RAID0",
75 	g_raid_tr_raid0_methods,
76 	sizeof(struct g_raid_tr_raid0_object),
77 	.trc_enable = 1,
78 	.trc_priority = 100,
79 	.trc_accept_unmapped = 1
80 };
81 
82 static int
83 g_raid_tr_taste_raid0(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
84 {
85 	struct g_raid_tr_raid0_object *trs;
86 
87 	trs = (struct g_raid_tr_raid0_object *)tr;
88 	if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_RAID0 ||
89 	    tr->tro_volume->v_raid_level_qualifier != G_RAID_VOLUME_RLQ_NONE)
90 		return (G_RAID_TR_TASTE_FAIL);
91 	trs->trso_starting = 1;
92 	return (G_RAID_TR_TASTE_SUCCEED);
93 }
94 
95 static int
96 g_raid_tr_update_state_raid0(struct g_raid_volume *vol)
97 {
98 	struct g_raid_tr_raid0_object *trs;
99 	struct g_raid_softc *sc;
100 	u_int s;
101 	int n, f;
102 
103 	sc = vol->v_softc;
104 	trs = (struct g_raid_tr_raid0_object *)vol->v_tr;
105 	if (trs->trso_stopped)
106 		s = G_RAID_VOLUME_S_STOPPED;
107 	else if (trs->trso_starting)
108 		s = G_RAID_VOLUME_S_STARTING;
109 	else {
110 		n = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
111 		f = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_FAILED);
112 		if (n + f == vol->v_disks_count) {
113 			if (f == 0)
114 				s = G_RAID_VOLUME_S_OPTIMAL;
115 			else
116 				s = G_RAID_VOLUME_S_SUBOPTIMAL;
117 		} else
118 			s = G_RAID_VOLUME_S_BROKEN;
119 	}
120 	if (s != vol->v_state) {
121 		g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
122 		    G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
123 		    G_RAID_EVENT_VOLUME);
124 		g_raid_change_volume_state(vol, s);
125 		if (!trs->trso_starting && !trs->trso_stopped)
126 			g_raid_write_metadata(sc, vol, NULL, NULL);
127 	}
128 	return (0);
129 }
130 
131 static int
132 g_raid_tr_event_raid0(struct g_raid_tr_object *tr,
133     struct g_raid_subdisk *sd, u_int event)
134 {
135 	struct g_raid_tr_raid0_object *trs;
136 	struct g_raid_softc *sc;
137 	struct g_raid_volume *vol;
138 	int state;
139 
140 	trs = (struct g_raid_tr_raid0_object *)tr;
141 	vol = tr->tro_volume;
142 	sc = vol->v_softc;
143 
144 	state = sd->sd_state;
145 	if (state != G_RAID_SUBDISK_S_NONE &&
146 	    state != G_RAID_SUBDISK_S_FAILED &&
147 	    state != G_RAID_SUBDISK_S_ACTIVE) {
148 		G_RAID_DEBUG1(1, sc,
149 		    "Promote subdisk %s:%d from %s to ACTIVE.",
150 		    vol->v_name, sd->sd_pos,
151 		    g_raid_subdisk_state2str(sd->sd_state));
152 		g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
153 	}
154 	if (state != sd->sd_state &&
155 	    !trs->trso_starting && !trs->trso_stopped)
156 		g_raid_write_metadata(sc, vol, sd, NULL);
157 	g_raid_tr_update_state_raid0(vol);
158 	return (0);
159 }
160 
161 static int
162 g_raid_tr_start_raid0(struct g_raid_tr_object *tr)
163 {
164 	struct g_raid_tr_raid0_object *trs;
165 	struct g_raid_volume *vol;
166 
167 	trs = (struct g_raid_tr_raid0_object *)tr;
168 	vol = tr->tro_volume;
169 	trs->trso_starting = 0;
170 	g_raid_tr_update_state_raid0(vol);
171 	return (0);
172 }
173 
174 static int
175 g_raid_tr_stop_raid0(struct g_raid_tr_object *tr)
176 {
177 	struct g_raid_tr_raid0_object *trs;
178 	struct g_raid_volume *vol;
179 
180 	trs = (struct g_raid_tr_raid0_object *)tr;
181 	vol = tr->tro_volume;
182 	trs->trso_starting = 0;
183 	trs->trso_stopped = 1;
184 	g_raid_tr_update_state_raid0(vol);
185 	return (0);
186 }
187 
188 static void
189 g_raid_tr_iostart_raid0(struct g_raid_tr_object *tr, struct bio *bp)
190 {
191 	struct g_raid_volume *vol;
192 	struct g_raid_subdisk *sd;
193 	struct bio_queue_head queue;
194 	struct bio *cbp;
195 	char *addr;
196 	off_t offset, start, length, nstripe, remain;
197 	u_int no, strip_size;
198 
199 	vol = tr->tro_volume;
200 	if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
201 	    vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) {
202 		g_raid_iodone(bp, EIO);
203 		return;
204 	}
205 	if (bp->bio_cmd == BIO_FLUSH || bp->bio_cmd == BIO_SPEEDUP) {
206 		g_raid_tr_flush_common(tr, bp);
207 		return;
208 	}
209 	if ((bp->bio_flags & BIO_UNMAPPED) != 0)
210 		addr = NULL;
211 	else
212 		addr = bp->bio_data;
213 	strip_size = vol->v_strip_size;
214 
215 	/* Stripe number. */
216 	nstripe = bp->bio_offset / strip_size;
217 	/* Start position in stripe. */
218 	start = bp->bio_offset % strip_size;
219 	/* Disk number. */
220 	no = nstripe % vol->v_disks_count;
221 	/* Stripe start position in disk. */
222 	offset = (nstripe / vol->v_disks_count) * strip_size;
223 	/* Length of data to operate. */
224 	remain = bp->bio_length;
225 
226 	bioq_init(&queue);
227 	do {
228 		length = MIN(strip_size - start, remain);
229 		cbp = g_clone_bio(bp);
230 		if (cbp == NULL)
231 			goto failure;
232 		cbp->bio_offset = offset + start;
233 		cbp->bio_length = length;
234 		if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
235 		    bp->bio_cmd != BIO_DELETE) {
236 			cbp->bio_ma_offset += (uintptr_t)addr;
237 			cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
238 			cbp->bio_ma_offset %= PAGE_SIZE;
239 			cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
240 			    cbp->bio_length) / PAGE_SIZE;
241 		} else
242 			cbp->bio_data = addr;
243 		cbp->bio_caller1 = &vol->v_subdisks[no];
244 		bioq_insert_tail(&queue, cbp);
245 		if (++no >= vol->v_disks_count) {
246 			no = 0;
247 			offset += strip_size;
248 		}
249 		remain -= length;
250 		if (bp->bio_cmd != BIO_DELETE)
251 			addr += length;
252 		start = 0;
253 	} while (remain > 0);
254 	while ((cbp = bioq_takefirst(&queue)) != NULL) {
255 		sd = cbp->bio_caller1;
256 		cbp->bio_caller1 = NULL;
257 		g_raid_subdisk_iostart(sd, cbp);
258 	}
259 	return;
260 failure:
261 	while ((cbp = bioq_takefirst(&queue)) != NULL)
262 		g_destroy_bio(cbp);
263 	if (bp->bio_error == 0)
264 		bp->bio_error = ENOMEM;
265 	g_raid_iodone(bp, bp->bio_error);
266 }
267 
268 static int
269 g_raid_tr_kerneldump_raid0(struct g_raid_tr_object *tr,
270     void *virtual, off_t boffset, size_t blength)
271 {
272 	struct g_raid_volume *vol;
273 	char *addr;
274 	off_t offset, start, length, nstripe, remain;
275 	u_int no, strip_size;
276 	int error;
277 
278 	vol = tr->tro_volume;
279 	if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
280 		return (ENXIO);
281 	addr = virtual;
282 	strip_size = vol->v_strip_size;
283 
284 	/* Stripe number. */
285 	nstripe = boffset / strip_size;
286 	/* Start position in stripe. */
287 	start = boffset % strip_size;
288 	/* Disk number. */
289 	no = nstripe % vol->v_disks_count;
290 	/* Stripe tart position in disk. */
291 	offset = (nstripe / vol->v_disks_count) * strip_size;
292 	/* Length of data to operate. */
293 	remain = blength;
294 
295 	do {
296 		length = MIN(strip_size - start, remain);
297 		error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no], addr,
298 		    offset + start, length);
299 		if (error != 0)
300 			return (error);
301 		if (++no >= vol->v_disks_count) {
302 			no = 0;
303 			offset += strip_size;
304 		}
305 		remain -= length;
306 		addr += length;
307 		start = 0;
308 	} while (remain > 0);
309 	return (0);
310 }
311 
312 static void
313 g_raid_tr_iodone_raid0(struct g_raid_tr_object *tr,
314     struct g_raid_subdisk *sd,struct bio *bp)
315 {
316 	struct bio *pbp;
317 
318 	pbp = bp->bio_parent;
319 	if (pbp->bio_error == 0)
320 		pbp->bio_error = bp->bio_error;
321 	g_destroy_bio(bp);
322 	pbp->bio_inbed++;
323 	if (pbp->bio_children == pbp->bio_inbed) {
324 		pbp->bio_completed = pbp->bio_length;
325 		g_raid_iodone(pbp, pbp->bio_error);
326 	}
327 }
328 
329 static int
330 g_raid_tr_free_raid0(struct g_raid_tr_object *tr)
331 {
332 
333 	return (0);
334 }
335 
336 G_RAID_TR_DECLARE(raid0, "RAID0");
337