]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/geom/raid/tr_raid0.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / geom / raid / tr_raid0.c
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$");
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
43 static MALLOC_DEFINE(M_TR_RAID0, "tr_raid0_data", "GEOM_RAID RAID0 data");
44
45 struct g_raid_tr_raid0_object {
46         struct g_raid_tr_object  trso_base;
47         int                      trso_starting;
48         int                      trso_stopped;
49 };
50
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;
59
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),
69         { 0, 0 }
70 };
71
72 static 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
80 static int
81 g_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
93 static int
94 g_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
129 static int
130 g_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
159 static int
160 g_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
172 static int
173 g_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
186 static void
187 g_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                 if (bp->bio_cmd != BIO_DELETE)
238                         addr += length;
239                 start = 0;
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);
247         }
248         return;
249 failure:
250         for (cbp = bioq_first(&queue); cbp != NULL;
251             cbp = bioq_first(&queue)) {
252                 bioq_remove(&queue, cbp);
253                 g_destroy_bio(cbp);
254         }
255         if (bp->bio_error == 0)
256                 bp->bio_error = ENOMEM;
257         g_raid_iodone(bp, bp->bio_error);
258 }
259
260 static int
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)
263 {
264         struct g_raid_volume *vol;
265         char *addr;
266         off_t offset, start, length, nstripe, remain;
267         u_int no, strip_size;
268         int error;
269
270         vol = tr->tro_volume;
271         if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
272                 return (ENXIO);
273         addr = virtual;
274         strip_size = vol->v_strip_size;
275
276         /* Stripe number. */
277         nstripe = boffset / strip_size;
278         /* Start position in stripe. */
279         start = boffset % strip_size;
280         /* Disk number. */
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. */
285         remain = blength;
286
287         do {
288                 length = MIN(strip_size - start, remain);
289                 error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
290                     addr, 0, offset + start, length);
291                 if (error != 0)
292                         return (error);
293                 if (++no >= vol->v_disks_count) {
294                         no = 0;
295                         offset += strip_size;
296                 }
297                 remain -= length;
298                 addr += length;
299                 start = 0;
300         } while (remain > 0);
301         return (0);
302 }
303
304 static void
305 g_raid_tr_iodone_raid0(struct g_raid_tr_object *tr,
306     struct g_raid_subdisk *sd,struct bio *bp)
307 {
308         struct bio *pbp;
309
310         pbp = bp->bio_parent;
311         if (pbp->bio_error == 0)
312                 pbp->bio_error = bp->bio_error;
313         g_destroy_bio(bp);
314         pbp->bio_inbed++;
315         if (pbp->bio_children == pbp->bio_inbed) {
316                 pbp->bio_completed = pbp->bio_length;
317                 g_raid_iodone(pbp, bp->bio_error);
318         }
319 }
320
321 static int
322 g_raid_tr_free_raid0(struct g_raid_tr_object *tr)
323 {
324
325         return (0);
326 }
327
328 G_RAID_TR_DECLARE(raid0, "RAID0");