]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - sys/geom/raid/tr_raid5.c
Copy stable/9 to releng/9.1 as part of the 9.1-RELEASE release process.
[FreeBSD/releng/9.1.git] / sys / geom / raid / tr_raid5.c
1 /*-
2  * Copyright (c) 2012 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/limits.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <geom/geom.h>
42 #include "geom/raid/g_raid.h"
43 #include "g_raid_tr_if.h"
44
45 SYSCTL_DECL(_kern_geom_raid);
46
47 static MALLOC_DEFINE(M_TR_RAID5, "tr_raid5_data", "GEOM_RAID RAID5 data");
48
49 #define TR_RAID5_NONE 0
50 #define TR_RAID5_REBUILD 1
51 #define TR_RAID5_RESYNC 2
52
53 #define TR_RAID5_F_DOING_SOME   0x1
54 #define TR_RAID5_F_LOCKED       0x2
55 #define TR_RAID5_F_ABORT        0x4
56
57 struct g_raid_tr_raid5_object {
58         struct g_raid_tr_object  trso_base;
59         int                      trso_starting;
60         int                      trso_stopping;
61         int                      trso_type;
62         int                      trso_recover_slabs; /* slabs before rest */
63         int                      trso_fair_io;
64         int                      trso_meta_update;
65         int                      trso_flags;
66         struct g_raid_subdisk   *trso_failed_sd; /* like per volume */
67         void                    *trso_buffer;    /* Buffer space */
68         struct bio               trso_bio;
69 };
70
71 static g_raid_tr_taste_t g_raid_tr_taste_raid5;
72 static g_raid_tr_event_t g_raid_tr_event_raid5;
73 static g_raid_tr_start_t g_raid_tr_start_raid5;
74 static g_raid_tr_stop_t g_raid_tr_stop_raid5;
75 static g_raid_tr_iostart_t g_raid_tr_iostart_raid5;
76 static g_raid_tr_iodone_t g_raid_tr_iodone_raid5;
77 static g_raid_tr_kerneldump_t g_raid_tr_kerneldump_raid5;
78 static g_raid_tr_locked_t g_raid_tr_locked_raid5;
79 static g_raid_tr_free_t g_raid_tr_free_raid5;
80
81 static kobj_method_t g_raid_tr_raid5_methods[] = {
82         KOBJMETHOD(g_raid_tr_taste,     g_raid_tr_taste_raid5),
83         KOBJMETHOD(g_raid_tr_event,     g_raid_tr_event_raid5),
84         KOBJMETHOD(g_raid_tr_start,     g_raid_tr_start_raid5),
85         KOBJMETHOD(g_raid_tr_stop,      g_raid_tr_stop_raid5),
86         KOBJMETHOD(g_raid_tr_iostart,   g_raid_tr_iostart_raid5),
87         KOBJMETHOD(g_raid_tr_iodone,    g_raid_tr_iodone_raid5),
88         KOBJMETHOD(g_raid_tr_kerneldump, g_raid_tr_kerneldump_raid5),
89         KOBJMETHOD(g_raid_tr_locked,    g_raid_tr_locked_raid5),
90         KOBJMETHOD(g_raid_tr_free,      g_raid_tr_free_raid5),
91         { 0, 0 }
92 };
93
94 static struct g_raid_tr_class g_raid_tr_raid5_class = {
95         "RAID5",
96         g_raid_tr_raid5_methods,
97         sizeof(struct g_raid_tr_raid5_object),
98         .trc_priority = 100
99 };
100
101 static int
102 g_raid_tr_taste_raid5(struct g_raid_tr_object *tr, struct g_raid_volume *vol)
103 {
104         struct g_raid_tr_raid5_object *trs;
105         u_int qual;
106
107         trs = (struct g_raid_tr_raid5_object *)tr;
108         qual = tr->tro_volume->v_raid_level_qualifier;
109         if (tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID4 &&
110             qual >= 0 && qual <= 1) {
111                 /* RAID4 */
112         } else if ((tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5 ||
113              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5E ||
114              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5EE ||
115              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5R ||
116              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID6 ||
117              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAIDMDF) &&
118             qual >= 0 && qual <= 3) {
119                 /* RAID5/5E/5EE/5R/6/MDF */
120         } else
121                 return (G_RAID_TR_TASTE_FAIL);
122         trs->trso_starting = 1;
123         return (G_RAID_TR_TASTE_SUCCEED);
124 }
125
126 static int
127 g_raid_tr_update_state_raid5(struct g_raid_volume *vol,
128     struct g_raid_subdisk *sd)
129 {
130         struct g_raid_tr_raid5_object *trs;
131         struct g_raid_softc *sc;
132         u_int s;
133         int na, ns, nu;
134
135         sc = vol->v_softc;
136         trs = (struct g_raid_tr_raid5_object *)vol->v_tr;
137         if (trs->trso_stopping &&
138             (trs->trso_flags & TR_RAID5_F_DOING_SOME) == 0)
139                 s = G_RAID_VOLUME_S_STOPPED;
140         else if (trs->trso_starting)
141                 s = G_RAID_VOLUME_S_STARTING;
142         else {
143                 na = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
144                 ns = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
145                      g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC);
146                 nu = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED);
147                 if (na == vol->v_disks_count)
148                         s = G_RAID_VOLUME_S_OPTIMAL;
149                 else if (na + ns == vol->v_disks_count ||
150                     na + ns + nu == vol->v_disks_count /* XXX: Temporary. */)
151                         s = G_RAID_VOLUME_S_SUBOPTIMAL;
152                 else if (na == vol->v_disks_count - 1 ||
153                     na + ns + nu == vol->v_disks_count)
154                         s = G_RAID_VOLUME_S_DEGRADED;
155                 else
156                         s = G_RAID_VOLUME_S_BROKEN;
157         }
158         if (s != vol->v_state) {
159                 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
160                     G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
161                     G_RAID_EVENT_VOLUME);
162                 g_raid_change_volume_state(vol, s);
163                 if (!trs->trso_starting && !trs->trso_stopping)
164                         g_raid_write_metadata(sc, vol, NULL, NULL);
165         }
166         return (0);
167 }
168
169 static int
170 g_raid_tr_event_raid5(struct g_raid_tr_object *tr,
171     struct g_raid_subdisk *sd, u_int event)
172 {
173
174         g_raid_tr_update_state_raid5(tr->tro_volume, sd);
175         return (0);
176 }
177
178 static int
179 g_raid_tr_start_raid5(struct g_raid_tr_object *tr)
180 {
181         struct g_raid_tr_raid5_object *trs;
182         struct g_raid_volume *vol;
183
184         trs = (struct g_raid_tr_raid5_object *)tr;
185         vol = tr->tro_volume;
186         trs->trso_starting = 0;
187         g_raid_tr_update_state_raid5(vol, NULL);
188         return (0);
189 }
190
191 static int
192 g_raid_tr_stop_raid5(struct g_raid_tr_object *tr)
193 {
194         struct g_raid_tr_raid5_object *trs;
195         struct g_raid_volume *vol;
196
197         trs = (struct g_raid_tr_raid5_object *)tr;
198         vol = tr->tro_volume;
199         trs->trso_starting = 0;
200         trs->trso_stopping = 1;
201         g_raid_tr_update_state_raid5(vol, NULL);
202         return (0);
203 }
204
205 static void
206 g_raid_tr_iostart_raid5_read(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, start, length, nstripe, remain;
214         int no, pno, ddisks, pdisks, protate, pleft;
215         u_int strip_size, lvl, qual;
216
217         vol = tr->tro_volume;
218         addr = bp->bio_data;
219         strip_size = vol->v_strip_size;
220         lvl = tr->tro_volume->v_raid_level;
221         qual = tr->tro_volume->v_raid_level_qualifier;
222         protate = tr->tro_volume->v_rotate_parity;
223
224         /* Stripe number. */
225         nstripe = bp->bio_offset / strip_size;
226         /* Start position in stripe. */
227         start = bp->bio_offset % strip_size;
228         /* Number of data and parity disks. */
229         if (lvl == G_RAID_VOLUME_RL_RAIDMDF)
230                 pdisks = tr->tro_volume->v_mdf_pdisks;
231         else if (lvl == G_RAID_VOLUME_RL_RAID5EE ||
232             lvl == G_RAID_VOLUME_RL_RAID6)
233                 pdisks = 2;
234         else
235                 pdisks = 1;
236         ddisks = vol->v_disks_count - pdisks;
237         /* Parity disk number. */
238         if (lvl == G_RAID_VOLUME_RL_RAID4) {
239                 if (qual == 0)          /* P0 */
240                         pno = 0;
241                 else                    /* PN */
242                         pno = ddisks;
243                 pleft = -1;
244         } else {
245                 pno = (nstripe / (ddisks * protate)) % vol->v_disks_count;
246                 pleft = protate - (nstripe / ddisks) % protate;
247                 if (qual >= 2) {        /* PN/Left */
248                         pno = ddisks - pno;
249                         if (pno < 0)
250                                 pno += vol->v_disks_count;
251                 }
252         }
253         /* Data disk number. */
254         no = nstripe % ddisks;
255         if (lvl == G_RAID_VOLUME_RL_RAID4) {
256                 if (qual == 0)
257                         no += pdisks;
258         } else if (qual & 1) {  /* Continuation/Symmetric */
259                 no = (pno + pdisks + no) % vol->v_disks_count;
260         } else if (no >= pno)   /* Restart/Asymmetric */
261                 no += pdisks;
262         else
263                 no += imax(0, pno + pdisks - vol->v_disks_count);
264         /* Stripe start position in disk. */
265         offset = (nstripe / ddisks) * strip_size;
266         /* Length of data to operate. */
267         remain = bp->bio_length;
268
269         bioq_init(&queue);
270         do {
271                 length = MIN(strip_size - start, remain);
272                 cbp = g_clone_bio(bp);
273                 if (cbp == NULL)
274                         goto failure;
275                 cbp->bio_offset = offset + start;
276                 cbp->bio_data = addr;
277                 cbp->bio_length = length;
278                 cbp->bio_caller1 = &vol->v_subdisks[no];
279                 bioq_insert_tail(&queue, cbp);
280                 no++;
281                 if (lvl == G_RAID_VOLUME_RL_RAID4) {
282                         no %= vol->v_disks_count;
283                         if (no == pno)
284                                 no = (no + pdisks) % vol->v_disks_count;
285                 } else if (qual & 1) {  /* Continuation/Symmetric */
286                         no %= vol->v_disks_count;
287                         if (no == pno) {
288                                 if ((--pleft) <= 0) {
289                                         pleft += protate;
290                                         if (qual < 2)   /* P0/Right */
291                                                 pno++;
292                                         else            /* PN/Left */
293                                                 pno += vol->v_disks_count - 1;
294                                         pno %= vol->v_disks_count;
295                                 }
296                                 no = (pno + pdisks) % vol->v_disks_count;
297                                 offset += strip_size;
298                         }
299                 } else {                /* Restart/Asymmetric */
300                         if (no == pno)
301                                 no += pdisks;
302                         if (no >= vol->v_disks_count) {
303                                 no -= vol->v_disks_count;
304                                 if ((--pleft) <= 0) {
305                                         pleft += protate;
306                                         if (qual < 2)   /* P0/Right */
307                                                 pno++;
308                                         else            /* PN/Left */
309                                                 pno += vol->v_disks_count - 1;
310                                         pno %= vol->v_disks_count;
311                                 }
312                                 if (no == pno)
313                                         no += pdisks;
314                                 else
315                                         no += imax(0, pno + pdisks - vol->v_disks_count);
316                                 offset += strip_size;
317                         }
318                 }
319                 remain -= length;
320                 addr += length;
321                 start = 0;
322         } while (remain > 0);
323         for (cbp = bioq_first(&queue); cbp != NULL;
324             cbp = bioq_first(&queue)) {
325                 bioq_remove(&queue, cbp);
326                 sd = cbp->bio_caller1;
327                 cbp->bio_caller1 = NULL;
328                 g_raid_subdisk_iostart(sd, cbp);
329         }
330         return;
331 failure:
332         for (cbp = bioq_first(&queue); cbp != NULL;
333             cbp = bioq_first(&queue)) {
334                 bioq_remove(&queue, cbp);
335                 g_destroy_bio(cbp);
336         }
337         if (bp->bio_error == 0)
338                 bp->bio_error = ENOMEM;
339         g_raid_iodone(bp, bp->bio_error);
340 }
341
342 static void
343 g_raid_tr_iostart_raid5(struct g_raid_tr_object *tr, struct bio *bp)
344 {
345         struct g_raid_volume *vol;
346         struct g_raid_tr_raid5_object *trs;
347
348         vol = tr->tro_volume;
349         trs = (struct g_raid_tr_raid5_object *)tr;
350         if (vol->v_state < G_RAID_VOLUME_S_SUBOPTIMAL) {
351                 g_raid_iodone(bp, EIO);
352                 return;
353         }
354         switch (bp->bio_cmd) {
355         case BIO_READ:
356                 g_raid_tr_iostart_raid5_read(tr, bp);
357                 break;
358         case BIO_WRITE:
359         case BIO_DELETE:
360         case BIO_FLUSH:
361                 g_raid_iodone(bp, ENODEV);
362                 break;
363         default:
364                 KASSERT(1 == 0, ("Invalid command here: %u (volume=%s)",
365                     bp->bio_cmd, vol->v_name));
366                 break;
367         }
368 }
369
370 static void
371 g_raid_tr_iodone_raid5(struct g_raid_tr_object *tr,
372     struct g_raid_subdisk *sd, struct bio *bp)
373 {
374         struct bio *pbp;
375         int error;
376
377         pbp = bp->bio_parent;
378         pbp->bio_inbed++;
379         error = bp->bio_error;
380         g_destroy_bio(bp);
381         if (pbp->bio_children == pbp->bio_inbed) {
382                 pbp->bio_completed = pbp->bio_length;
383                 g_raid_iodone(pbp, error);
384         }
385 }
386
387 static int
388 g_raid_tr_kerneldump_raid5(struct g_raid_tr_object *tr,
389     void *virtual, vm_offset_t physical, off_t offset, size_t length)
390 {
391
392         return (ENODEV);
393 }
394
395 static int
396 g_raid_tr_locked_raid5(struct g_raid_tr_object *tr, void *argp)
397 {
398         struct bio *bp;
399         struct g_raid_subdisk *sd;
400
401         bp = (struct bio *)argp;
402         sd = (struct g_raid_subdisk *)bp->bio_caller1;
403         g_raid_subdisk_iostart(sd, bp);
404
405         return (0);
406 }
407
408 static int
409 g_raid_tr_free_raid5(struct g_raid_tr_object *tr)
410 {
411         struct g_raid_tr_raid5_object *trs;
412
413         trs = (struct g_raid_tr_raid5_object *)tr;
414
415         if (trs->trso_buffer != NULL) {
416                 free(trs->trso_buffer, M_TR_RAID5);
417                 trs->trso_buffer = NULL;
418         }
419         return (0);
420 }
421
422 G_RAID_TR_DECLARE(g_raid_tr_raid5);