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