The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/geom/raid/tr_raid5.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: dfbf3d8512b8f898b1ecf872d64ef43f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.