]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/geom/raid/tr_concat.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / geom / raid / tr_concat.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_CONCAT, "tr_concat_data", "GEOM_RAID CONCAT data");
44
45 struct g_raid_tr_concat_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_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;
59
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),
69         { 0, 0 }
70 };
71
72 static struct g_raid_tr_class g_raid_tr_concat_class = {
73         "CONCAT",
74         g_raid_tr_concat_methods,
75         sizeof(struct g_raid_tr_concat_object),
76         .trc_priority = 50
77 };
78
79 static int
80 g_raid_tr_taste_concat(struct g_raid_tr_object *tr, struct g_raid_volume *volume)
81 {
82         struct g_raid_tr_concat_object *trs;
83
84         trs = (struct g_raid_tr_concat_object *)tr;
85         if (tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_SINGLE &&
86             tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
87             !(tr->tro_volume->v_disks_count == 1 &&
88               tr->tro_volume->v_raid_level != G_RAID_VOLUME_RL_UNKNOWN))
89                 return (G_RAID_TR_TASTE_FAIL);
90         trs->trso_starting = 1;
91         return (G_RAID_TR_TASTE_SUCCEED);
92 }
93
94 static int
95 g_raid_tr_update_state_concat(struct g_raid_volume *vol)
96 {
97         struct g_raid_tr_concat_object *trs;
98         struct g_raid_softc *sc;
99         off_t size;
100         u_int s;
101         int i, n, f;
102
103         sc = vol->v_softc;
104         trs = (struct g_raid_tr_concat_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
122                 /*
123                  * Some metadata modules may not know CONCAT volume
124                  * mediasize until all disks connected. Recalculate.
125                  */
126                 if (G_RAID_VOLUME_S_ALIVE(s) &&
127                     !G_RAID_VOLUME_S_ALIVE(vol->v_state)) {
128                         size = 0;
129                         for (i = 0; i < vol->v_disks_count; i++) {
130                                 if (vol->v_subdisks[i].sd_state !=
131                                     G_RAID_SUBDISK_S_NONE)
132                                         size += vol->v_subdisks[i].sd_size;
133                         }
134                         vol->v_mediasize = size;
135                 }
136
137                 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
138                     G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
139                     G_RAID_EVENT_VOLUME);
140                 g_raid_change_volume_state(vol, s);
141                 if (!trs->trso_starting && !trs->trso_stopped)
142                         g_raid_write_metadata(sc, vol, NULL, NULL);
143         }
144         return (0);
145 }
146
147 static int
148 g_raid_tr_event_concat(struct g_raid_tr_object *tr,
149     struct g_raid_subdisk *sd, u_int event)
150 {
151         struct g_raid_tr_concat_object *trs;
152         struct g_raid_softc *sc;
153         struct g_raid_volume *vol;
154         int state;
155
156         trs = (struct g_raid_tr_concat_object *)tr;
157         vol = tr->tro_volume;
158         sc = vol->v_softc;
159
160         state = sd->sd_state;
161         if (state != G_RAID_SUBDISK_S_NONE &&
162             state != G_RAID_SUBDISK_S_FAILED &&
163             state != G_RAID_SUBDISK_S_ACTIVE) {
164                 G_RAID_DEBUG1(1, sc,
165                     "Promote subdisk %s:%d from %s to ACTIVE.",
166                     vol->v_name, sd->sd_pos,
167                     g_raid_subdisk_state2str(sd->sd_state));
168                 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_ACTIVE);
169         }
170         if (state != sd->sd_state &&
171             !trs->trso_starting && !trs->trso_stopped)
172                 g_raid_write_metadata(sc, vol, sd, NULL);
173         g_raid_tr_update_state_concat(vol);
174         return (0);
175 }
176
177 static int
178 g_raid_tr_start_concat(struct g_raid_tr_object *tr)
179 {
180         struct g_raid_tr_concat_object *trs;
181         struct g_raid_volume *vol;
182
183         trs = (struct g_raid_tr_concat_object *)tr;
184         vol = tr->tro_volume;
185         trs->trso_starting = 0;
186         g_raid_tr_update_state_concat(vol);
187         return (0);
188 }
189
190 static int
191 g_raid_tr_stop_concat(struct g_raid_tr_object *tr)
192 {
193         struct g_raid_tr_concat_object *trs;
194         struct g_raid_volume *vol;
195
196         trs = (struct g_raid_tr_concat_object *)tr;
197         vol = tr->tro_volume;
198         trs->trso_starting = 0;
199         trs->trso_stopped = 1;
200         g_raid_tr_update_state_concat(vol);
201         return (0);
202 }
203
204 static void
205 g_raid_tr_iostart_concat(struct g_raid_tr_object *tr, struct bio *bp)
206 {
207         struct g_raid_volume *vol;
208         struct g_raid_subdisk *sd;
209         struct bio_queue_head queue;
210         struct bio *cbp;
211         char *addr;
212         off_t offset, length, remain;
213         u_int no;
214
215         vol = tr->tro_volume;
216         if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL &&
217             vol->v_state != G_RAID_VOLUME_S_SUBOPTIMAL) {
218                 g_raid_iodone(bp, EIO);
219                 return;
220         }
221         if (bp->bio_cmd == BIO_FLUSH) {
222                 g_raid_tr_flush_common(tr, bp);
223                 return;
224         }
225
226         offset = bp->bio_offset;
227         remain = bp->bio_length;
228         addr = bp->bio_data;
229         no = 0;
230         while (no < vol->v_disks_count &&
231             offset >= vol->v_subdisks[no].sd_size) {
232                 offset -= vol->v_subdisks[no].sd_size;
233                 no++;
234         }
235         KASSERT(no < vol->v_disks_count,
236             ("Request starts after volume end (%ju)", bp->bio_offset));
237         bioq_init(&queue);
238         do {
239                 sd = &vol->v_subdisks[no];
240                 length = MIN(sd->sd_size - offset, remain);
241                 cbp = g_clone_bio(bp);
242                 if (cbp == NULL)
243                         goto failure;
244                 cbp->bio_offset = offset;
245                 cbp->bio_data = addr;
246                 cbp->bio_length = length;
247                 cbp->bio_caller1 = sd;
248                 bioq_insert_tail(&queue, cbp);
249                 remain -= length;
250                 addr += length;
251                 offset = 0;
252                 no++;
253                 KASSERT(no < vol->v_disks_count || remain == 0,
254                     ("Request ends after volume end (%ju, %ju)",
255                         bp->bio_offset, bp->bio_length));
256         } while (remain > 0);
257         for (cbp = bioq_first(&queue); cbp != NULL;
258             cbp = bioq_first(&queue)) {
259                 bioq_remove(&queue, cbp);
260                 sd = cbp->bio_caller1;
261                 cbp->bio_caller1 = NULL;
262                 g_raid_subdisk_iostart(sd, cbp);
263         }
264         return;
265 failure:
266         for (cbp = bioq_first(&queue); cbp != NULL;
267             cbp = bioq_first(&queue)) {
268                 bioq_remove(&queue, cbp);
269                 g_destroy_bio(cbp);
270         }
271         if (bp->bio_error == 0)
272                 bp->bio_error = ENOMEM;
273         g_raid_iodone(bp, bp->bio_error);
274 }
275
276 static int
277 g_raid_tr_kerneldump_concat(struct g_raid_tr_object *tr,
278     void *virtual, vm_offset_t physical, off_t boffset, size_t blength)
279 {
280         struct g_raid_volume *vol;
281         struct g_raid_subdisk *sd;
282         char *addr;
283         off_t offset, length, remain;
284         int error, no;
285
286         vol = tr->tro_volume;
287         if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
288                 return (ENXIO);
289
290         offset = boffset;
291         remain = blength;
292         addr = virtual;
293         no = 0;
294         while (no < vol->v_disks_count &&
295             offset >= vol->v_subdisks[no].sd_size) {
296                 offset -= vol->v_subdisks[no].sd_size;
297                 no++;
298         }
299         KASSERT(no < vol->v_disks_count,
300             ("Request starts after volume end (%ju)", boffset));
301         do {
302                 sd = &vol->v_subdisks[no];
303                 length = MIN(sd->sd_size - offset, remain);
304                 error = g_raid_subdisk_kerneldump(&vol->v_subdisks[no],
305                     addr, 0, offset, length);
306                 if (error != 0)
307                         return (error);
308                 remain -= length;
309                 addr += length;
310                 offset = 0;
311                 no++;
312                 KASSERT(no < vol->v_disks_count || remain == 0,
313                     ("Request ends after volume end (%ju, %zu)",
314                         boffset, blength));
315         } while (remain > 0);
316         return (0);
317 }
318
319 static void
320 g_raid_tr_iodone_concat(struct g_raid_tr_object *tr,
321     struct g_raid_subdisk *sd,struct bio *bp)
322 {
323         struct bio *pbp;
324
325         pbp = bp->bio_parent;
326         if (pbp->bio_error == 0)
327                 pbp->bio_error = bp->bio_error;
328         g_destroy_bio(bp);
329         pbp->bio_inbed++;
330         if (pbp->bio_children == pbp->bio_inbed) {
331                 pbp->bio_completed = pbp->bio_length;
332                 g_raid_iodone(pbp, bp->bio_error);
333         }
334 }
335
336 static int
337 g_raid_tr_free_concat(struct g_raid_tr_object *tr)
338 {
339
340         return (0);
341 }
342
343 G_RAID_TR_DECLARE(g_raid_tr_concat);