tr_raid0.c revision 240465
1/*-
2 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/raid/tr_raid0.c 240465 2012-09-13 13:27:09Z mav $");
29
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/raid/g_raid.h"
41#include "g_raid_tr_if.h"
42
43static MALLOC_DEFINE(M_TR_RAID0, "tr_raid0_data", "GEOM_RAID RAID0 data");
44
45struct g_raid_tr_raid0_object {
46	struct g_raid_tr_object	 trso_base;
47	int			 trso_starting;
48	int			 trso_stopped;
49};
50
51static g_raid_tr_taste_t g_raid_tr_taste_raid0;
52static g_raid_tr_event_t g_raid_tr_event_raid0;
53static g_raid_tr_start_t g_raid_tr_start_raid0;
54static g_raid_tr_stop_t g_raid_tr_stop_raid0;
55static g_raid_tr_iostart_t g_raid_tr_iostart_raid0;
56static g_raid_tr_iodone_t g_raid_tr_iodone_raid0;
57static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid0;
58static g_raid_tr_free_t g_raid_tr_free_raid0;
59
60static 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),
69	{ 0, 0 }
70};
71
72static struct g_raid_tr_class g_raid_tr_raid0_class = {
73	"RAID0",
74	g_raid_tr_raid0_methods,
75	sizeof(struct g_raid_tr_raid0_object),
76	.trc_enable = 1,
77	.trc_priority = 100
78};
79
80static int
81g_raid_tr_taste_raid0(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
82{
83	struct g_raid_tr_raid0_object *trs;
84
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);
91}
92
93static int
94g_raid_tr_update_state_raid0(struct g_raid_volume *vol)
95{
96	struct g_raid_tr_raid0_object *trs;
97	struct g_raid_softc *sc;
98	u_int s;
99	int n, f;
100
101	sc = vol->v_softc;
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;
107	else {
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) {
111			if (f == 0)
112				s = G_RAID_VOLUME_S_OPTIMAL;
113			else
114				s = G_RAID_VOLUME_S_SUBOPTIMAL;
115		} else
116			s = G_RAID_VOLUME_S_BROKEN;
117	}
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);
125	}
126	return (0);
127}
128
129static int
130g_raid_tr_event_raid0(struct g_raid_tr_object *tr,
131    struct g_raid_subdisk *sd, u_int event)
132{
133	struct g_raid_tr_raid0_object *trs;
134	struct g_raid_softc *sc;
135	struct g_raid_volume *vol;
136	int state;
137
138	trs = (struct g_raid_tr_raid0_object *)tr;
139	vol = tr->tro_volume;
140	sc = vol->v_softc;
141
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) {
146		G_RAID_DEBUG1(1, sc,
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);
151	}
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);
156	return (0);
157}
158
159static int
160g_raid_tr_start_raid0(struct g_raid_tr_object *tr)
161{
162	struct g_raid_tr_raid0_object *trs;
163	struct g_raid_volume *vol;
164
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);
169	return (0);
170}
171
172static int
173g_raid_tr_stop_raid0(struct g_raid_tr_object *tr)
174{
175	struct g_raid_tr_raid0_object *trs;
176	struct g_raid_volume *vol;
177
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);
183	return (0);
184}
185
186static void
187g_raid_tr_iostart_raid0(struct g_raid_tr_object *tr, struct bio *bp)
188{
189	struct g_raid_volume *vol;
190	struct g_raid_subdisk *sd;
191	struct bio_queue_head queue;
192	struct bio *cbp;
193	char *addr;
194	off_t offset, start, length, nstripe, remain;
195	u_int no, strip_size;
196
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);
201		return;
202	}
203	if (bp->bio_cmd == BIO_FLUSH) {
204		g_raid_tr_flush_common(tr, bp);
205		return;
206	}
207	addr = bp->bio_data;
208	strip_size = vol->v_strip_size;
209
210	/* Stripe number. */
211	nstripe = bp->bio_offset / strip_size;
212	/* Start position in stripe. */
213	start = bp->bio_offset % strip_size;
214	/* Disk number. */
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;
220
221	bioq_init(&queue);
222	do {
223		length = MIN(strip_size - start, remain);
224		cbp = g_clone_bio(bp);
225		if (cbp == NULL)
226			goto failure;
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) {
233			no = 0;
234			offset += strip_size;
235		}
236		remain -= length;
237		addr += length;
238		start = 0;
239	} while (remain > 0);
240	for (cbp = bioq_first(&queue); cbp != NULL;
241	    cbp = bioq_first(&queue)) {
242		bioq_remove(&queue, cbp);
243		sd = cbp->bio_caller1;
244		cbp->bio_caller1 = NULL;
245		g_raid_subdisk_iostart(sd, cbp);
246	}
247	return;
248failure:
249	for (cbp = bioq_first(&queue); cbp != NULL;
250	    cbp = bioq_first(&queue)) {
251		bioq_remove(&queue, cbp);
252		g_destroy_bio(cbp);
253	}
254	if (bp->bio_error == 0)
255		bp->bio_error = ENOMEM;
256	g_raid_iodone(bp, bp->bio_error);
257}
258
259static int
260g_raid_tr_kerneldump_raid0(struct g_raid_tr_object *tr,
261    void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
262{
263	struct g_raid_volume *vol;
264	char *addr;
265	off_t offset, start, length, nstripe, remain;
266	u_int no, strip_size;
267	int error;
268
269	vol = tr->tro_volume;
270	if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
271		return (ENXIO);
272	addr = virtual;
273	strip_size = vol->v_strip_size;
274
275	/* Stripe number. */
276	nstripe = boffset / strip_size;
277	/* Start position in stripe. */
278	start = boffset % strip_size;
279	/* Disk number. */
280	no = nstripe % vol->v_disks_count;
281	/* Stripe tart position in disk. */
282	offset = (nstripe / vol->v_disks_count) * strip_size;
283	/* Length of data to operate. */
284	remain = blength;
285
286	do {
287		length = MIN(strip_size - start, remain);
288		error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
289		    addr, 0, offset + start, length);
290		if (error != 0)
291			return (error);
292		if (++no >= vol->v_disks_count) {
293			no = 0;
294			offset += strip_size;
295		}
296		remain -= length;
297		addr += length;
298		start = 0;
299	} while (remain > 0);
300	return (0);
301}
302
303static void
304g_raid_tr_iodone_raid0(struct g_raid_tr_object *tr,
305    struct g_raid_subdisk *sd,struct bio *bp)
306{
307	struct bio *pbp;
308
309	pbp = bp->bio_parent;
310	if (pbp->bio_error == 0)
311		pbp->bio_error = bp->bio_error;
312	g_destroy_bio(bp);
313	pbp->bio_inbed++;
314	if (pbp->bio_children == pbp->bio_inbed) {
315		pbp->bio_completed = pbp->bio_length;
316		g_raid_iodone(pbp, bp->bio_error);
317	}
318}
319
320static int
321g_raid_tr_free_raid0(struct g_raid_tr_object *tr)
322{
323
324	return (0);
325}
326
327G_RAID_TR_DECLARE(raid0, "RAID0");
328