]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/raid/tr_raid5.c
amd64: use register macros for gdb_cpu_getreg()
[FreeBSD/FreeBSD.git] / sys / geom / raid / tr_raid5.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
43 #include <geom/geom.h>
44 #include "geom/raid/g_raid.h"
45 #include "g_raid_tr_if.h"
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_enable = 1,
99         .trc_priority = 100
100 };
101
102 static int
103 g_raid_tr_taste_raid5(struct g_raid_tr_object *tr, struct g_raid_volume *vol)
104 {
105         struct g_raid_tr_raid5_object *trs;
106         u_int qual;
107
108         trs = (struct g_raid_tr_raid5_object *)tr;
109         qual = tr->tro_volume->v_raid_level_qualifier;
110         if (tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID4 &&
111             (qual == G_RAID_VOLUME_RLQ_R4P0 ||
112              qual == G_RAID_VOLUME_RLQ_R4PN)) {
113                 /* RAID4 */
114         } else if ((tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5 ||
115              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5E ||
116              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5EE ||
117              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID5R ||
118              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAID6 ||
119              tr->tro_volume->v_raid_level == G_RAID_VOLUME_RL_RAIDMDF) &&
120             (qual == G_RAID_VOLUME_RLQ_R5RA ||
121              qual == G_RAID_VOLUME_RLQ_R5RS ||
122              qual == G_RAID_VOLUME_RLQ_R5LA ||
123              qual == G_RAID_VOLUME_RLQ_R5LS)) {
124                 /* RAID5/5E/5EE/5R/6/MDF */
125         } else
126                 return (G_RAID_TR_TASTE_FAIL);
127         trs->trso_starting = 1;
128         return (G_RAID_TR_TASTE_SUCCEED);
129 }
130
131 static int
132 g_raid_tr_update_state_raid5(struct g_raid_volume *vol,
133     struct g_raid_subdisk *sd)
134 {
135         struct g_raid_tr_raid5_object *trs;
136         struct g_raid_softc *sc;
137         u_int s;
138         int na, ns, nu;
139
140         sc = vol->v_softc;
141         trs = (struct g_raid_tr_raid5_object *)vol->v_tr;
142         if (trs->trso_stopping &&
143             (trs->trso_flags & TR_RAID5_F_DOING_SOME) == 0)
144                 s = G_RAID_VOLUME_S_STOPPED;
145         else if (trs->trso_starting)
146                 s = G_RAID_VOLUME_S_STARTING;
147         else {
148                 na = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_ACTIVE);
149                 ns = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) +
150                      g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC);
151                 nu = g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED);
152                 if (na == vol->v_disks_count)
153                         s = G_RAID_VOLUME_S_OPTIMAL;
154                 else if (na + ns == vol->v_disks_count ||
155                     na + ns + nu == vol->v_disks_count /* XXX: Temporary. */)
156                         s = G_RAID_VOLUME_S_SUBOPTIMAL;
157                 else if (na == vol->v_disks_count - 1 ||
158                     na + ns + nu == vol->v_disks_count)
159                         s = G_RAID_VOLUME_S_DEGRADED;
160                 else
161                         s = G_RAID_VOLUME_S_BROKEN;
162         }
163         if (s != vol->v_state) {
164                 g_raid_event_send(vol, G_RAID_VOLUME_S_ALIVE(s) ?
165                     G_RAID_VOLUME_E_UP : G_RAID_VOLUME_E_DOWN,
166                     G_RAID_EVENT_VOLUME);
167                 g_raid_change_volume_state(vol, s);
168                 if (!trs->trso_starting && !trs->trso_stopping)
169                         g_raid_write_metadata(sc, vol, NULL, NULL);
170         }
171         return (0);
172 }
173
174 static int
175 g_raid_tr_event_raid5(struct g_raid_tr_object *tr,
176     struct g_raid_subdisk *sd, u_int event)
177 {
178
179         g_raid_tr_update_state_raid5(tr->tro_volume, sd);
180         return (0);
181 }
182
183 static int
184 g_raid_tr_start_raid5(struct g_raid_tr_object *tr)
185 {
186         struct g_raid_tr_raid5_object *trs;
187         struct g_raid_volume *vol;
188
189         trs = (struct g_raid_tr_raid5_object *)tr;
190         trs->trso_starting = 0;
191         vol = tr->tro_volume;
192         vol->v_read_only = 1;
193         g_raid_tr_update_state_raid5(vol, NULL);
194         return (0);
195 }
196
197 static int
198 g_raid_tr_stop_raid5(struct g_raid_tr_object *tr)
199 {
200         struct g_raid_tr_raid5_object *trs;
201         struct g_raid_volume *vol;
202
203         trs = (struct g_raid_tr_raid5_object *)tr;
204         vol = tr->tro_volume;
205         trs->trso_starting = 0;
206         trs->trso_stopping = 1;
207         g_raid_tr_update_state_raid5(vol, NULL);
208         return (0);
209 }
210
211 static void
212 g_raid_tr_iostart_raid5_read(struct g_raid_tr_object *tr, struct bio *bp)
213 {
214         struct g_raid_volume *vol;
215         struct g_raid_subdisk *sd;
216         struct bio_queue_head queue;
217         struct bio *cbp;
218         char *addr;
219         off_t offset, start, length, nstripe, remain;
220         int no, pno, ddisks, pdisks, protate, pleft;
221         u_int strip_size, lvl, qual;
222
223         vol = tr->tro_volume;
224         addr = bp->bio_data;
225         strip_size = vol->v_strip_size;
226         lvl = tr->tro_volume->v_raid_level;
227         qual = tr->tro_volume->v_raid_level_qualifier;
228         protate = tr->tro_volume->v_rotate_parity;
229
230         /* Stripe number. */
231         nstripe = bp->bio_offset / strip_size;
232         /* Start position in stripe. */
233         start = bp->bio_offset % strip_size;
234         /* Number of data and parity disks. */
235         if (lvl == G_RAID_VOLUME_RL_RAIDMDF)
236                 pdisks = tr->tro_volume->v_mdf_pdisks;
237         else if (lvl == G_RAID_VOLUME_RL_RAID5EE ||
238             lvl == G_RAID_VOLUME_RL_RAID6)
239                 pdisks = 2;
240         else
241                 pdisks = 1;
242         ddisks = vol->v_disks_count - pdisks;
243         /* Parity disk number. */
244         if (lvl == G_RAID_VOLUME_RL_RAID4) {
245                 if (qual == 0)          /* P0 */
246                         pno = 0;
247                 else                    /* PN */
248                         pno = ddisks;
249                 pleft = -1;
250         } else {
251                 pno = (nstripe / (ddisks * protate)) % vol->v_disks_count;
252                 pleft = protate - (nstripe / ddisks) % protate;
253                 if (qual >= 2) {        /* PN/Left */
254                         pno = ddisks - pno;
255                         if (pno < 0)
256                                 pno += vol->v_disks_count;
257                 }
258         }
259         /* Data disk number. */
260         no = nstripe % ddisks;
261         if (lvl == G_RAID_VOLUME_RL_RAID4) {
262                 if (qual == 0)
263                         no += pdisks;
264         } else if (qual & 1) {  /* Continuation/Symmetric */
265                 no = (pno + pdisks + no) % vol->v_disks_count;
266         } else if (no >= pno)   /* Restart/Asymmetric */
267                 no += pdisks;
268         else
269                 no += imax(0, pno + pdisks - vol->v_disks_count);
270         /* Stripe start position in disk. */
271         offset = (nstripe / ddisks) * strip_size;
272         /* Length of data to operate. */
273         remain = bp->bio_length;
274
275         bioq_init(&queue);
276         do {
277                 length = MIN(strip_size - start, remain);
278                 cbp = g_clone_bio(bp);
279                 if (cbp == NULL)
280                         goto failure;
281                 cbp->bio_offset = offset + start;
282                 cbp->bio_data = addr;
283                 cbp->bio_length = length;
284                 cbp->bio_caller1 = &vol->v_subdisks[no];
285                 bioq_insert_tail(&queue, cbp);
286                 no++;
287                 if (lvl == G_RAID_VOLUME_RL_RAID4) {
288                         no %= vol->v_disks_count;
289                         if (no == pno)
290                                 no = (no + pdisks) % vol->v_disks_count;
291                 } else if (qual & 1) {  /* Continuation/Symmetric */
292                         no %= vol->v_disks_count;
293                         if (no == pno) {
294                                 if ((--pleft) <= 0) {
295                                         pleft += protate;
296                                         if (qual < 2)   /* P0/Right */
297                                                 pno++;
298                                         else            /* PN/Left */
299                                                 pno += vol->v_disks_count - 1;
300                                         pno %= vol->v_disks_count;
301                                 }
302                                 no = (pno + pdisks) % vol->v_disks_count;
303                                 offset += strip_size;
304                         }
305                 } else {                /* Restart/Asymmetric */
306                         if (no == pno)
307                                 no += pdisks;
308                         if (no >= vol->v_disks_count) {
309                                 no -= vol->v_disks_count;
310                                 if ((--pleft) <= 0) {
311                                         pleft += protate;
312                                         if (qual < 2)   /* P0/Right */
313                                                 pno++;
314                                         else            /* PN/Left */
315                                                 pno += vol->v_disks_count - 1;
316                                         pno %= vol->v_disks_count;
317                                 }
318                                 if (no == pno)
319                                         no += pdisks;
320                                 else
321                                         no += imax(0, pno + pdisks - vol->v_disks_count);
322                                 offset += strip_size;
323                         }
324                 }
325                 remain -= length;
326                 addr += length;
327                 start = 0;
328         } while (remain > 0);
329         while ((cbp = bioq_takefirst(&queue)) != NULL) {
330                 sd = cbp->bio_caller1;
331                 cbp->bio_caller1 = NULL;
332                 g_raid_subdisk_iostart(sd, cbp);
333         }
334         return;
335 failure:
336         while ((cbp = bioq_takefirst(&queue)) != NULL)
337                 g_destroy_bio(cbp);
338         if (bp->bio_error == 0)
339                 bp->bio_error = ENOMEM;
340         g_raid_iodone(bp, bp->bio_error);
341 }
342
343 static void
344 g_raid_tr_iostart_raid5(struct g_raid_tr_object *tr, struct bio *bp)
345 {
346         struct g_raid_volume *vol;
347
348         vol = tr->tro_volume;
349         if (vol->v_state < G_RAID_VOLUME_S_SUBOPTIMAL) {
350                 g_raid_iodone(bp, EIO);
351                 return;
352         }
353         switch (bp->bio_cmd) {
354         case BIO_READ:
355                 g_raid_tr_iostart_raid5_read(tr, bp);
356                 break;
357         case BIO_WRITE:
358         case BIO_DELETE:
359         case BIO_FLUSH:
360         case BIO_SPEEDUP:
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
376         pbp = bp->bio_parent;
377         if (pbp->bio_error == 0)
378                 pbp->bio_error = bp->bio_error;
379         pbp->bio_inbed++;
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, pbp->bio_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(raid5, "RAID5");