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/g_raid.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) 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: releng/10.0/sys/geom/raid/g_raid.c 254275 2013-08-13 07:56:40Z mav $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/systm.h>
   32 #include <sys/kernel.h>
   33 #include <sys/module.h>
   34 #include <sys/limits.h>
   35 #include <sys/lock.h>
   36 #include <sys/mutex.h>
   37 #include <sys/bio.h>
   38 #include <sys/sbuf.h>
   39 #include <sys/sysctl.h>
   40 #include <sys/malloc.h>
   41 #include <sys/eventhandler.h>
   42 #include <vm/uma.h>
   43 #include <geom/geom.h>
   44 #include <sys/proc.h>
   45 #include <sys/kthread.h>
   46 #include <sys/sched.h>
   47 #include <geom/raid/g_raid.h>
   48 #include "g_raid_md_if.h"
   49 #include "g_raid_tr_if.h"
   50 
   51 static MALLOC_DEFINE(M_RAID, "raid_data", "GEOM_RAID Data");
   52 
   53 SYSCTL_DECL(_kern_geom);
   54 SYSCTL_NODE(_kern_geom, OID_AUTO, raid, CTLFLAG_RW, 0, "GEOM_RAID stuff");
   55 int g_raid_enable = 1;
   56 TUNABLE_INT("kern.geom.raid.enable", &g_raid_enable);
   57 SYSCTL_INT(_kern_geom_raid, OID_AUTO, enable, CTLFLAG_RW,
   58     &g_raid_enable, 0, "Enable on-disk metadata taste");
   59 u_int g_raid_aggressive_spare = 0;
   60 TUNABLE_INT("kern.geom.raid.aggressive_spare", &g_raid_aggressive_spare);
   61 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RW,
   62     &g_raid_aggressive_spare, 0, "Use disks without metadata as spare");
   63 u_int g_raid_debug = 0;
   64 TUNABLE_INT("kern.geom.raid.debug", &g_raid_debug);
   65 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RW, &g_raid_debug, 0,
   66     "Debug level");
   67 int g_raid_read_err_thresh = 10;
   68 TUNABLE_INT("kern.geom.raid.read_err_thresh", &g_raid_read_err_thresh);
   69 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RW,
   70     &g_raid_read_err_thresh, 0,
   71     "Number of read errors equated to disk failure");
   72 u_int g_raid_start_timeout = 30;
   73 TUNABLE_INT("kern.geom.raid.start_timeout", &g_raid_start_timeout);
   74 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RW,
   75     &g_raid_start_timeout, 0,
   76     "Time to wait for all array components");
   77 static u_int g_raid_clean_time = 5;
   78 TUNABLE_INT("kern.geom.raid.clean_time", &g_raid_clean_time);
   79 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RW,
   80     &g_raid_clean_time, 0, "Mark volume as clean when idling");
   81 static u_int g_raid_disconnect_on_failure = 1;
   82 TUNABLE_INT("kern.geom.raid.disconnect_on_failure",
   83     &g_raid_disconnect_on_failure);
   84 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
   85     &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
   86 static u_int g_raid_name_format = 0;
   87 TUNABLE_INT("kern.geom.raid.name_format", &g_raid_name_format);
   88 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RW,
   89     &g_raid_name_format, 0, "Providers name format.");
   90 static u_int g_raid_idle_threshold = 1000000;
   91 TUNABLE_INT("kern.geom.raid.idle_threshold", &g_raid_idle_threshold);
   92 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RW,
   93     &g_raid_idle_threshold, 1000000,
   94     "Time in microseconds to consider a volume idle.");
   95 static u_int ar_legacy_aliases = 1;
   96 SYSCTL_INT(_kern_geom_raid, OID_AUTO, legacy_aliases, CTLFLAG_RW,
   97            &ar_legacy_aliases, 0, "Create aliases named as the legacy ataraid style.");
   98 TUNABLE_INT("kern.geom_raid.legacy_aliases", &ar_legacy_aliases);
   99 
  100 
  101 #define MSLEEP(rv, ident, mtx, priority, wmesg, timeout)        do {    \
  102         G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));         \
  103         rv = msleep((ident), (mtx), (priority), (wmesg), (timeout));    \
  104         G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident));         \
  105 } while (0)
  106 
  107 LIST_HEAD(, g_raid_md_class) g_raid_md_classes =
  108     LIST_HEAD_INITIALIZER(g_raid_md_classes);
  109 
  110 LIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
  111     LIST_HEAD_INITIALIZER(g_raid_tr_classes);
  112 
  113 LIST_HEAD(, g_raid_volume) g_raid_volumes =
  114     LIST_HEAD_INITIALIZER(g_raid_volumes);
  115 
  116 static eventhandler_tag g_raid_post_sync = NULL;
  117 static int g_raid_started = 0;
  118 static int g_raid_shutdown = 0;
  119 
  120 static int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
  121     struct g_geom *gp);
  122 static g_taste_t g_raid_taste;
  123 static void g_raid_init(struct g_class *mp);
  124 static void g_raid_fini(struct g_class *mp);
  125 
  126 struct g_class g_raid_class = {
  127         .name = G_RAID_CLASS_NAME,
  128         .version = G_VERSION,
  129         .ctlreq = g_raid_ctl,
  130         .taste = g_raid_taste,
  131         .destroy_geom = g_raid_destroy_geom,
  132         .init = g_raid_init,
  133         .fini = g_raid_fini
  134 };
  135 
  136 static void g_raid_destroy_provider(struct g_raid_volume *vol);
  137 static int g_raid_update_disk(struct g_raid_disk *disk, u_int event);
  138 static int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event);
  139 static int g_raid_update_volume(struct g_raid_volume *vol, u_int event);
  140 static int g_raid_update_node(struct g_raid_softc *sc, u_int event);
  141 static void g_raid_dumpconf(struct sbuf *sb, const char *indent,
  142     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
  143 static void g_raid_start(struct bio *bp);
  144 static void g_raid_start_request(struct bio *bp);
  145 static void g_raid_disk_done(struct bio *bp);
  146 static void g_raid_poll(struct g_raid_softc *sc);
  147 
  148 static const char *
  149 g_raid_node_event2str(int event)
  150 {
  151 
  152         switch (event) {
  153         case G_RAID_NODE_E_WAKE:
  154                 return ("WAKE");
  155         case G_RAID_NODE_E_START:
  156                 return ("START");
  157         default:
  158                 return ("INVALID");
  159         }
  160 }
  161 
  162 const char *
  163 g_raid_disk_state2str(int state)
  164 {
  165 
  166         switch (state) {
  167         case G_RAID_DISK_S_NONE:
  168                 return ("NONE");
  169         case G_RAID_DISK_S_OFFLINE:
  170                 return ("OFFLINE");
  171         case G_RAID_DISK_S_DISABLED:
  172                 return ("DISABLED");
  173         case G_RAID_DISK_S_FAILED:
  174                 return ("FAILED");
  175         case G_RAID_DISK_S_STALE_FAILED:
  176                 return ("STALE_FAILED");
  177         case G_RAID_DISK_S_SPARE:
  178                 return ("SPARE");
  179         case G_RAID_DISK_S_STALE:
  180                 return ("STALE");
  181         case G_RAID_DISK_S_ACTIVE:
  182                 return ("ACTIVE");
  183         default:
  184                 return ("INVALID");
  185         }
  186 }
  187 
  188 static const char *
  189 g_raid_disk_event2str(int event)
  190 {
  191 
  192         switch (event) {
  193         case G_RAID_DISK_E_DISCONNECTED:
  194                 return ("DISCONNECTED");
  195         default:
  196                 return ("INVALID");
  197         }
  198 }
  199 
  200 const char *
  201 g_raid_subdisk_state2str(int state)
  202 {
  203 
  204         switch (state) {
  205         case G_RAID_SUBDISK_S_NONE:
  206                 return ("NONE");
  207         case G_RAID_SUBDISK_S_FAILED:
  208                 return ("FAILED");
  209         case G_RAID_SUBDISK_S_NEW:
  210                 return ("NEW");
  211         case G_RAID_SUBDISK_S_REBUILD:
  212                 return ("REBUILD");
  213         case G_RAID_SUBDISK_S_UNINITIALIZED:
  214                 return ("UNINITIALIZED");
  215         case G_RAID_SUBDISK_S_STALE:
  216                 return ("STALE");
  217         case G_RAID_SUBDISK_S_RESYNC:
  218                 return ("RESYNC");
  219         case G_RAID_SUBDISK_S_ACTIVE:
  220                 return ("ACTIVE");
  221         default:
  222                 return ("INVALID");
  223         }
  224 }
  225 
  226 static const char *
  227 g_raid_subdisk_event2str(int event)
  228 {
  229 
  230         switch (event) {
  231         case G_RAID_SUBDISK_E_NEW:
  232                 return ("NEW");
  233         case G_RAID_SUBDISK_E_FAILED:
  234                 return ("FAILED");
  235         case G_RAID_SUBDISK_E_DISCONNECTED:
  236                 return ("DISCONNECTED");
  237         default:
  238                 return ("INVALID");
  239         }
  240 }
  241 
  242 const char *
  243 g_raid_volume_state2str(int state)
  244 {
  245 
  246         switch (state) {
  247         case G_RAID_VOLUME_S_STARTING:
  248                 return ("STARTING");
  249         case G_RAID_VOLUME_S_BROKEN:
  250                 return ("BROKEN");
  251         case G_RAID_VOLUME_S_DEGRADED:
  252                 return ("DEGRADED");
  253         case G_RAID_VOLUME_S_SUBOPTIMAL:
  254                 return ("SUBOPTIMAL");
  255         case G_RAID_VOLUME_S_OPTIMAL:
  256                 return ("OPTIMAL");
  257         case G_RAID_VOLUME_S_UNSUPPORTED:
  258                 return ("UNSUPPORTED");
  259         case G_RAID_VOLUME_S_STOPPED:
  260                 return ("STOPPED");
  261         default:
  262                 return ("INVALID");
  263         }
  264 }
  265 
  266 static const char *
  267 g_raid_volume_event2str(int event)
  268 {
  269 
  270         switch (event) {
  271         case G_RAID_VOLUME_E_UP:
  272                 return ("UP");
  273         case G_RAID_VOLUME_E_DOWN:
  274                 return ("DOWN");
  275         case G_RAID_VOLUME_E_START:
  276                 return ("START");
  277         case G_RAID_VOLUME_E_STARTMD:
  278                 return ("STARTMD");
  279         default:
  280                 return ("INVALID");
  281         }
  282 }
  283 
  284 const char *
  285 g_raid_volume_level2str(int level, int qual)
  286 {
  287 
  288         switch (level) {
  289         case G_RAID_VOLUME_RL_RAID0:
  290                 return ("RAID0");
  291         case G_RAID_VOLUME_RL_RAID1:
  292                 return ("RAID1");
  293         case G_RAID_VOLUME_RL_RAID3:
  294                 if (qual == G_RAID_VOLUME_RLQ_R3P0)
  295                         return ("RAID3-P0");
  296                 if (qual == G_RAID_VOLUME_RLQ_R3PN)
  297                         return ("RAID3-PN");
  298                 return ("RAID3");
  299         case G_RAID_VOLUME_RL_RAID4:
  300                 if (qual == G_RAID_VOLUME_RLQ_R4P0)
  301                         return ("RAID4-P0");
  302                 if (qual == G_RAID_VOLUME_RLQ_R4PN)
  303                         return ("RAID4-PN");
  304                 return ("RAID4");
  305         case G_RAID_VOLUME_RL_RAID5:
  306                 if (qual == G_RAID_VOLUME_RLQ_R5RA)
  307                         return ("RAID5-RA");
  308                 if (qual == G_RAID_VOLUME_RLQ_R5RS)
  309                         return ("RAID5-RS");
  310                 if (qual == G_RAID_VOLUME_RLQ_R5LA)
  311                         return ("RAID5-LA");
  312                 if (qual == G_RAID_VOLUME_RLQ_R5LS)
  313                         return ("RAID5-LS");
  314                 return ("RAID5");
  315         case G_RAID_VOLUME_RL_RAID6:
  316                 if (qual == G_RAID_VOLUME_RLQ_R6RA)
  317                         return ("RAID6-RA");
  318                 if (qual == G_RAID_VOLUME_RLQ_R6RS)
  319                         return ("RAID6-RS");
  320                 if (qual == G_RAID_VOLUME_RLQ_R6LA)
  321                         return ("RAID6-LA");
  322                 if (qual == G_RAID_VOLUME_RLQ_R6LS)
  323                         return ("RAID6-LS");
  324                 return ("RAID6");
  325         case G_RAID_VOLUME_RL_RAIDMDF:
  326                 if (qual == G_RAID_VOLUME_RLQ_RMDFRA)
  327                         return ("RAIDMDF-RA");
  328                 if (qual == G_RAID_VOLUME_RLQ_RMDFRS)
  329                         return ("RAIDMDF-RS");
  330                 if (qual == G_RAID_VOLUME_RLQ_RMDFLA)
  331                         return ("RAIDMDF-LA");
  332                 if (qual == G_RAID_VOLUME_RLQ_RMDFLS)
  333                         return ("RAIDMDF-LS");
  334                 return ("RAIDMDF");
  335         case G_RAID_VOLUME_RL_RAID1E:
  336                 if (qual == G_RAID_VOLUME_RLQ_R1EA)
  337                         return ("RAID1E-A");
  338                 if (qual == G_RAID_VOLUME_RLQ_R1EO)
  339                         return ("RAID1E-O");
  340                 return ("RAID1E");
  341         case G_RAID_VOLUME_RL_SINGLE:
  342                 return ("SINGLE");
  343         case G_RAID_VOLUME_RL_CONCAT:
  344                 return ("CONCAT");
  345         case G_RAID_VOLUME_RL_RAID5E:
  346                 if (qual == G_RAID_VOLUME_RLQ_R5ERA)
  347                         return ("RAID5E-RA");
  348                 if (qual == G_RAID_VOLUME_RLQ_R5ERS)
  349                         return ("RAID5E-RS");
  350                 if (qual == G_RAID_VOLUME_RLQ_R5ELA)
  351                         return ("RAID5E-LA");
  352                 if (qual == G_RAID_VOLUME_RLQ_R5ELS)
  353                         return ("RAID5E-LS");
  354                 return ("RAID5E");
  355         case G_RAID_VOLUME_RL_RAID5EE:
  356                 if (qual == G_RAID_VOLUME_RLQ_R5EERA)
  357                         return ("RAID5EE-RA");
  358                 if (qual == G_RAID_VOLUME_RLQ_R5EERS)
  359                         return ("RAID5EE-RS");
  360                 if (qual == G_RAID_VOLUME_RLQ_R5EELA)
  361                         return ("RAID5EE-LA");
  362                 if (qual == G_RAID_VOLUME_RLQ_R5EELS)
  363                         return ("RAID5EE-LS");
  364                 return ("RAID5EE");
  365         case G_RAID_VOLUME_RL_RAID5R:
  366                 if (qual == G_RAID_VOLUME_RLQ_R5RRA)
  367                         return ("RAID5R-RA");
  368                 if (qual == G_RAID_VOLUME_RLQ_R5RRS)
  369                         return ("RAID5R-RS");
  370                 if (qual == G_RAID_VOLUME_RLQ_R5RLA)
  371                         return ("RAID5R-LA");
  372                 if (qual == G_RAID_VOLUME_RLQ_R5RLS)
  373                         return ("RAID5R-LS");
  374                 return ("RAID5E");
  375         default:
  376                 return ("UNKNOWN");
  377         }
  378 }
  379 
  380 int
  381 g_raid_volume_str2level(const char *str, int *level, int *qual)
  382 {
  383 
  384         *level = G_RAID_VOLUME_RL_UNKNOWN;
  385         *qual = G_RAID_VOLUME_RLQ_NONE;
  386         if (strcasecmp(str, "RAID0") == 0)
  387                 *level = G_RAID_VOLUME_RL_RAID0;
  388         else if (strcasecmp(str, "RAID1") == 0)
  389                 *level = G_RAID_VOLUME_RL_RAID1;
  390         else if (strcasecmp(str, "RAID3-P0") == 0) {
  391                 *level = G_RAID_VOLUME_RL_RAID3;
  392                 *qual = G_RAID_VOLUME_RLQ_R3P0;
  393         } else if (strcasecmp(str, "RAID3-PN") == 0 ||
  394                    strcasecmp(str, "RAID3") == 0) {
  395                 *level = G_RAID_VOLUME_RL_RAID3;
  396                 *qual = G_RAID_VOLUME_RLQ_R3PN;
  397         } else if (strcasecmp(str, "RAID4-P0") == 0) {
  398                 *level = G_RAID_VOLUME_RL_RAID4;
  399                 *qual = G_RAID_VOLUME_RLQ_R4P0;
  400         } else if (strcasecmp(str, "RAID4-PN") == 0 ||
  401                    strcasecmp(str, "RAID4") == 0) {
  402                 *level = G_RAID_VOLUME_RL_RAID4;
  403                 *qual = G_RAID_VOLUME_RLQ_R4PN;
  404         } else if (strcasecmp(str, "RAID5-RA") == 0) {
  405                 *level = G_RAID_VOLUME_RL_RAID5;
  406                 *qual = G_RAID_VOLUME_RLQ_R5RA;
  407         } else if (strcasecmp(str, "RAID5-RS") == 0) {
  408                 *level = G_RAID_VOLUME_RL_RAID5;
  409                 *qual = G_RAID_VOLUME_RLQ_R5RS;
  410         } else if (strcasecmp(str, "RAID5") == 0 ||
  411                    strcasecmp(str, "RAID5-LA") == 0) {
  412                 *level = G_RAID_VOLUME_RL_RAID5;
  413                 *qual = G_RAID_VOLUME_RLQ_R5LA;
  414         } else if (strcasecmp(str, "RAID5-LS") == 0) {
  415                 *level = G_RAID_VOLUME_RL_RAID5;
  416                 *qual = G_RAID_VOLUME_RLQ_R5LS;
  417         } else if (strcasecmp(str, "RAID6-RA") == 0) {
  418                 *level = G_RAID_VOLUME_RL_RAID6;
  419                 *qual = G_RAID_VOLUME_RLQ_R6RA;
  420         } else if (strcasecmp(str, "RAID6-RS") == 0) {
  421                 *level = G_RAID_VOLUME_RL_RAID6;
  422                 *qual = G_RAID_VOLUME_RLQ_R6RS;
  423         } else if (strcasecmp(str, "RAID6") == 0 ||
  424                    strcasecmp(str, "RAID6-LA") == 0) {
  425                 *level = G_RAID_VOLUME_RL_RAID6;
  426                 *qual = G_RAID_VOLUME_RLQ_R6LA;
  427         } else if (strcasecmp(str, "RAID6-LS") == 0) {
  428                 *level = G_RAID_VOLUME_RL_RAID6;
  429                 *qual = G_RAID_VOLUME_RLQ_R6LS;
  430         } else if (strcasecmp(str, "RAIDMDF-RA") == 0) {
  431                 *level = G_RAID_VOLUME_RL_RAIDMDF;
  432                 *qual = G_RAID_VOLUME_RLQ_RMDFRA;
  433         } else if (strcasecmp(str, "RAIDMDF-RS") == 0) {
  434                 *level = G_RAID_VOLUME_RL_RAIDMDF;
  435                 *qual = G_RAID_VOLUME_RLQ_RMDFRS;
  436         } else if (strcasecmp(str, "RAIDMDF") == 0 ||
  437                    strcasecmp(str, "RAIDMDF-LA") == 0) {
  438                 *level = G_RAID_VOLUME_RL_RAIDMDF;
  439                 *qual = G_RAID_VOLUME_RLQ_RMDFLA;
  440         } else if (strcasecmp(str, "RAIDMDF-LS") == 0) {
  441                 *level = G_RAID_VOLUME_RL_RAIDMDF;
  442                 *qual = G_RAID_VOLUME_RLQ_RMDFLS;
  443         } else if (strcasecmp(str, "RAID10") == 0 ||
  444                    strcasecmp(str, "RAID1E") == 0 ||
  445                    strcasecmp(str, "RAID1E-A") == 0) {
  446                 *level = G_RAID_VOLUME_RL_RAID1E;
  447                 *qual = G_RAID_VOLUME_RLQ_R1EA;
  448         } else if (strcasecmp(str, "RAID1E-O") == 0) {
  449                 *level = G_RAID_VOLUME_RL_RAID1E;
  450                 *qual = G_RAID_VOLUME_RLQ_R1EO;
  451         } else if (strcasecmp(str, "SINGLE") == 0)
  452                 *level = G_RAID_VOLUME_RL_SINGLE;
  453         else if (strcasecmp(str, "CONCAT") == 0)
  454                 *level = G_RAID_VOLUME_RL_CONCAT;
  455         else if (strcasecmp(str, "RAID5E-RA") == 0) {
  456                 *level = G_RAID_VOLUME_RL_RAID5E;
  457                 *qual = G_RAID_VOLUME_RLQ_R5ERA;
  458         } else if (strcasecmp(str, "RAID5E-RS") == 0) {
  459                 *level = G_RAID_VOLUME_RL_RAID5E;
  460                 *qual = G_RAID_VOLUME_RLQ_R5ERS;
  461         } else if (strcasecmp(str, "RAID5E") == 0 ||
  462                    strcasecmp(str, "RAID5E-LA") == 0) {
  463                 *level = G_RAID_VOLUME_RL_RAID5E;
  464                 *qual = G_RAID_VOLUME_RLQ_R5ELA;
  465         } else if (strcasecmp(str, "RAID5E-LS") == 0) {
  466                 *level = G_RAID_VOLUME_RL_RAID5E;
  467                 *qual = G_RAID_VOLUME_RLQ_R5ELS;
  468         } else if (strcasecmp(str, "RAID5EE-RA") == 0) {
  469                 *level = G_RAID_VOLUME_RL_RAID5EE;
  470                 *qual = G_RAID_VOLUME_RLQ_R5EERA;
  471         } else if (strcasecmp(str, "RAID5EE-RS") == 0) {
  472                 *level = G_RAID_VOLUME_RL_RAID5EE;
  473                 *qual = G_RAID_VOLUME_RLQ_R5EERS;
  474         } else if (strcasecmp(str, "RAID5EE") == 0 ||
  475                    strcasecmp(str, "RAID5EE-LA") == 0) {
  476                 *level = G_RAID_VOLUME_RL_RAID5EE;
  477                 *qual = G_RAID_VOLUME_RLQ_R5EELA;
  478         } else if (strcasecmp(str, "RAID5EE-LS") == 0) {
  479                 *level = G_RAID_VOLUME_RL_RAID5EE;
  480                 *qual = G_RAID_VOLUME_RLQ_R5EELS;
  481         } else if (strcasecmp(str, "RAID5R-RA") == 0) {
  482                 *level = G_RAID_VOLUME_RL_RAID5R;
  483                 *qual = G_RAID_VOLUME_RLQ_R5RRA;
  484         } else if (strcasecmp(str, "RAID5R-RS") == 0) {
  485                 *level = G_RAID_VOLUME_RL_RAID5R;
  486                 *qual = G_RAID_VOLUME_RLQ_R5RRS;
  487         } else if (strcasecmp(str, "RAID5R") == 0 ||
  488                    strcasecmp(str, "RAID5R-LA") == 0) {
  489                 *level = G_RAID_VOLUME_RL_RAID5R;
  490                 *qual = G_RAID_VOLUME_RLQ_R5RLA;
  491         } else if (strcasecmp(str, "RAID5R-LS") == 0) {
  492                 *level = G_RAID_VOLUME_RL_RAID5R;
  493                 *qual = G_RAID_VOLUME_RLQ_R5RLS;
  494         } else
  495                 return (-1);
  496         return (0);
  497 }
  498 
  499 const char *
  500 g_raid_get_diskname(struct g_raid_disk *disk)
  501 {
  502 
  503         if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
  504                 return ("[unknown]");
  505         return (disk->d_consumer->provider->name);
  506 }
  507 
  508 void
  509 g_raid_get_disk_info(struct g_raid_disk *disk)
  510 {
  511         struct g_consumer *cp = disk->d_consumer;
  512         int error, len;
  513 
  514         /* Read kernel dumping information. */
  515         disk->d_kd.offset = 0;
  516         disk->d_kd.length = OFF_MAX;
  517         len = sizeof(disk->d_kd);
  518         error = g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
  519         if (error)
  520                 disk->d_kd.di.dumper = NULL;
  521         if (disk->d_kd.di.dumper == NULL)
  522                 G_RAID_DEBUG1(2, disk->d_softc,
  523                     "Dumping not supported by %s: %d.", 
  524                     cp->provider->name, error);
  525 
  526         /* Read BIO_DELETE support. */
  527         error = g_getattr("GEOM::candelete", cp, &disk->d_candelete);
  528         if (error)
  529                 disk->d_candelete = 0;
  530         if (!disk->d_candelete)
  531                 G_RAID_DEBUG1(2, disk->d_softc,
  532                     "BIO_DELETE not supported by %s: %d.", 
  533                     cp->provider->name, error);
  534 }
  535 
  536 void
  537 g_raid_report_disk_state(struct g_raid_disk *disk)
  538 {
  539         struct g_raid_subdisk *sd;
  540         int len, state;
  541         uint32_t s;
  542 
  543         if (disk->d_consumer == NULL)
  544                 return;
  545         if (disk->d_state == G_RAID_DISK_S_DISABLED) {
  546                 s = G_STATE_ACTIVE; /* XXX */
  547         } else if (disk->d_state == G_RAID_DISK_S_FAILED ||
  548             disk->d_state == G_RAID_DISK_S_STALE_FAILED) {
  549                 s = G_STATE_FAILED;
  550         } else {
  551                 state = G_RAID_SUBDISK_S_ACTIVE;
  552                 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
  553                         if (sd->sd_state < state)
  554                                 state = sd->sd_state;
  555                 }
  556                 if (state == G_RAID_SUBDISK_S_FAILED)
  557                         s = G_STATE_FAILED;
  558                 else if (state == G_RAID_SUBDISK_S_NEW ||
  559                     state == G_RAID_SUBDISK_S_REBUILD)
  560                         s = G_STATE_REBUILD;
  561                 else if (state == G_RAID_SUBDISK_S_STALE ||
  562                     state == G_RAID_SUBDISK_S_RESYNC)
  563                         s = G_STATE_RESYNC;
  564                 else
  565                         s = G_STATE_ACTIVE;
  566         }
  567         len = sizeof(s);
  568         g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s);
  569         G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.",
  570             g_raid_get_diskname(disk), s);
  571 }
  572 
  573 void
  574 g_raid_change_disk_state(struct g_raid_disk *disk, int state)
  575 {
  576 
  577         G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.",
  578             g_raid_get_diskname(disk),
  579             g_raid_disk_state2str(disk->d_state),
  580             g_raid_disk_state2str(state));
  581         disk->d_state = state;
  582         g_raid_report_disk_state(disk);
  583 }
  584 
  585 void
  586 g_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
  587 {
  588 
  589         G_RAID_DEBUG1(0, sd->sd_softc,
  590             "Subdisk %s:%d-%s state changed from %s to %s.",
  591             sd->sd_volume->v_name, sd->sd_pos,
  592             sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
  593             g_raid_subdisk_state2str(sd->sd_state),
  594             g_raid_subdisk_state2str(state));
  595         sd->sd_state = state;
  596         if (sd->sd_disk)
  597                 g_raid_report_disk_state(sd->sd_disk);
  598 }
  599 
  600 void
  601 g_raid_change_volume_state(struct g_raid_volume *vol, int state)
  602 {
  603 
  604         G_RAID_DEBUG1(0, vol->v_softc,
  605             "Volume %s state changed from %s to %s.",
  606             vol->v_name,
  607             g_raid_volume_state2str(vol->v_state),
  608             g_raid_volume_state2str(state));
  609         vol->v_state = state;
  610 }
  611 
  612 /*
  613  * --- Events handling functions ---
  614  * Events in geom_raid are used to maintain subdisks and volumes status
  615  * from one thread to simplify locking.
  616  */
  617 static void
  618 g_raid_event_free(struct g_raid_event *ep)
  619 {
  620 
  621         free(ep, M_RAID);
  622 }
  623 
  624 int
  625 g_raid_event_send(void *arg, int event, int flags)
  626 {
  627         struct g_raid_softc *sc;
  628         struct g_raid_event *ep;
  629         int error;
  630 
  631         if ((flags & G_RAID_EVENT_VOLUME) != 0) {
  632                 sc = ((struct g_raid_volume *)arg)->v_softc;
  633         } else if ((flags & G_RAID_EVENT_DISK) != 0) {
  634                 sc = ((struct g_raid_disk *)arg)->d_softc;
  635         } else if ((flags & G_RAID_EVENT_SUBDISK) != 0) {
  636                 sc = ((struct g_raid_subdisk *)arg)->sd_softc;
  637         } else {
  638                 sc = arg;
  639         }
  640         ep = malloc(sizeof(*ep), M_RAID,
  641             sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
  642         if (ep == NULL)
  643                 return (ENOMEM);
  644         ep->e_tgt = arg;
  645         ep->e_event = event;
  646         ep->e_flags = flags;
  647         ep->e_error = 0;
  648         G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc);
  649         mtx_lock(&sc->sc_queue_mtx);
  650         TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
  651         mtx_unlock(&sc->sc_queue_mtx);
  652         wakeup(sc);
  653 
  654         if ((flags & G_RAID_EVENT_WAIT) == 0)
  655                 return (0);
  656 
  657         sx_assert(&sc->sc_lock, SX_XLOCKED);
  658         G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep);
  659         sx_xunlock(&sc->sc_lock);
  660         while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) {
  661                 mtx_lock(&sc->sc_queue_mtx);
  662                 MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event",
  663                     hz * 5);
  664         }
  665         error = ep->e_error;
  666         g_raid_event_free(ep);
  667         sx_xlock(&sc->sc_lock);
  668         return (error);
  669 }
  670 
  671 static void
  672 g_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
  673 {
  674         struct g_raid_event *ep, *tmpep;
  675 
  676         sx_assert(&sc->sc_lock, SX_XLOCKED);
  677 
  678         mtx_lock(&sc->sc_queue_mtx);
  679         TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
  680                 if (ep->e_tgt != tgt)
  681                         continue;
  682                 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
  683                 if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0)
  684                         g_raid_event_free(ep);
  685                 else {
  686                         ep->e_error = ECANCELED;
  687                         wakeup(ep);
  688                 }
  689         }
  690         mtx_unlock(&sc->sc_queue_mtx);
  691 }
  692 
  693 static int
  694 g_raid_event_check(struct g_raid_softc *sc, void *tgt)
  695 {
  696         struct g_raid_event *ep;
  697         int     res = 0;
  698 
  699         sx_assert(&sc->sc_lock, SX_XLOCKED);
  700 
  701         mtx_lock(&sc->sc_queue_mtx);
  702         TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
  703                 if (ep->e_tgt != tgt)
  704                         continue;
  705                 res = 1;
  706                 break;
  707         }
  708         mtx_unlock(&sc->sc_queue_mtx);
  709         return (res);
  710 }
  711 
  712 /*
  713  * Return the number of disks in given state.
  714  * If state is equal to -1, count all connected disks.
  715  */
  716 u_int
  717 g_raid_ndisks(struct g_raid_softc *sc, int state)
  718 {
  719         struct g_raid_disk *disk;
  720         u_int n;
  721 
  722         sx_assert(&sc->sc_lock, SX_LOCKED);
  723 
  724         n = 0;
  725         TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
  726                 if (disk->d_state == state || state == -1)
  727                         n++;
  728         }
  729         return (n);
  730 }
  731 
  732 /*
  733  * Return the number of subdisks in given state.
  734  * If state is equal to -1, count all connected disks.
  735  */
  736 u_int
  737 g_raid_nsubdisks(struct g_raid_volume *vol, int state)
  738 {
  739         struct g_raid_subdisk *subdisk;
  740         struct g_raid_softc *sc;
  741         u_int i, n ;
  742 
  743         sc = vol->v_softc;
  744         sx_assert(&sc->sc_lock, SX_LOCKED);
  745 
  746         n = 0;
  747         for (i = 0; i < vol->v_disks_count; i++) {
  748                 subdisk = &vol->v_subdisks[i];
  749                 if ((state == -1 &&
  750                      subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
  751                     subdisk->sd_state == state)
  752                         n++;
  753         }
  754         return (n);
  755 }
  756 
  757 /*
  758  * Return the first subdisk in given state.
  759  * If state is equal to -1, then the first connected disks.
  760  */
  761 struct g_raid_subdisk *
  762 g_raid_get_subdisk(struct g_raid_volume *vol, int state)
  763 {
  764         struct g_raid_subdisk *sd;
  765         struct g_raid_softc *sc;
  766         u_int i;
  767 
  768         sc = vol->v_softc;
  769         sx_assert(&sc->sc_lock, SX_LOCKED);
  770 
  771         for (i = 0; i < vol->v_disks_count; i++) {
  772                 sd = &vol->v_subdisks[i];
  773                 if ((state == -1 &&
  774                      sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
  775                     sd->sd_state == state)
  776                         return (sd);
  777         }
  778         return (NULL);
  779 }
  780 
  781 struct g_consumer *
  782 g_raid_open_consumer(struct g_raid_softc *sc, const char *name)
  783 {
  784         struct g_consumer *cp;
  785         struct g_provider *pp;
  786 
  787         g_topology_assert();
  788 
  789         if (strncmp(name, "/dev/", 5) == 0)
  790                 name += 5;
  791         pp = g_provider_by_name(name);
  792         if (pp == NULL)
  793                 return (NULL);
  794         cp = g_new_consumer(sc->sc_geom);
  795         if (g_attach(cp, pp) != 0) {
  796                 g_destroy_consumer(cp);
  797                 return (NULL);
  798         }
  799         if (g_access(cp, 1, 1, 1) != 0) {
  800                 g_detach(cp);
  801                 g_destroy_consumer(cp);
  802                 return (NULL);
  803         }
  804         return (cp);
  805 }
  806 
  807 static u_int
  808 g_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
  809 {
  810         struct bio *bp;
  811         u_int nreqs = 0;
  812 
  813         mtx_lock(&sc->sc_queue_mtx);
  814         TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
  815                 if (bp->bio_from == cp)
  816                         nreqs++;
  817         }
  818         mtx_unlock(&sc->sc_queue_mtx);
  819         return (nreqs);
  820 }
  821 
  822 u_int
  823 g_raid_nopens(struct g_raid_softc *sc)
  824 {
  825         struct g_raid_volume *vol;
  826         u_int opens;
  827 
  828         opens = 0;
  829         TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
  830                 if (vol->v_provider_open != 0)
  831                         opens++;
  832         }
  833         return (opens);
  834 }
  835 
  836 static int
  837 g_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
  838 {
  839 
  840         if (cp->index > 0) {
  841                 G_RAID_DEBUG1(2, sc,
  842                     "I/O requests for %s exist, can't destroy it now.",
  843                     cp->provider->name);
  844                 return (1);
  845         }
  846         if (g_raid_nrequests(sc, cp) > 0) {
  847                 G_RAID_DEBUG1(2, sc,
  848                     "I/O requests for %s in queue, can't destroy it now.",
  849                     cp->provider->name);
  850                 return (1);
  851         }
  852         return (0);
  853 }
  854 
  855 static void
  856 g_raid_destroy_consumer(void *arg, int flags __unused)
  857 {
  858         struct g_consumer *cp;
  859 
  860         g_topology_assert();
  861 
  862         cp = arg;
  863         G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
  864         g_detach(cp);
  865         g_destroy_consumer(cp);
  866 }
  867 
  868 void
  869 g_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
  870 {
  871         struct g_provider *pp;
  872         int retaste_wait;
  873 
  874         g_topology_assert_not();
  875 
  876         g_topology_lock();
  877         cp->private = NULL;
  878         if (g_raid_consumer_is_busy(sc, cp))
  879                 goto out;
  880         pp = cp->provider;
  881         retaste_wait = 0;
  882         if (cp->acw == 1) {
  883                 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
  884                         retaste_wait = 1;
  885         }
  886         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
  887                 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
  888         if (retaste_wait) {
  889                 /*
  890                  * After retaste event was send (inside g_access()), we can send
  891                  * event to detach and destroy consumer.
  892                  * A class, which has consumer to the given provider connected
  893                  * will not receive retaste event for the provider.
  894                  * This is the way how I ignore retaste events when I close
  895                  * consumers opened for write: I detach and destroy consumer
  896                  * after retaste event is sent.
  897                  */
  898                 g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
  899                 goto out;
  900         }
  901         G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
  902         g_detach(cp);
  903         g_destroy_consumer(cp);
  904 out:
  905         g_topology_unlock();
  906 }
  907 
  908 static void
  909 g_raid_orphan(struct g_consumer *cp)
  910 {
  911         struct g_raid_disk *disk;
  912 
  913         g_topology_assert();
  914 
  915         disk = cp->private;
  916         if (disk == NULL)
  917                 return;
  918         g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
  919             G_RAID_EVENT_DISK);
  920 }
  921 
  922 static void
  923 g_raid_clean(struct g_raid_volume *vol, int acw)
  924 {
  925         struct g_raid_softc *sc;
  926         int timeout;
  927 
  928         sc = vol->v_softc;
  929         g_topology_assert_not();
  930         sx_assert(&sc->sc_lock, SX_XLOCKED);
  931 
  932 //      if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
  933 //              return;
  934         if (!vol->v_dirty)
  935                 return;
  936         if (vol->v_writes > 0)
  937                 return;
  938         if (acw > 0 || (acw == -1 &&
  939             vol->v_provider != NULL && vol->v_provider->acw > 0)) {
  940                 timeout = g_raid_clean_time - (time_uptime - vol->v_last_write);
  941                 if (!g_raid_shutdown && timeout > 0)
  942                         return;
  943         }
  944         vol->v_dirty = 0;
  945         G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
  946             vol->v_name);
  947         g_raid_write_metadata(sc, vol, NULL, NULL);
  948 }
  949 
  950 static void
  951 g_raid_dirty(struct g_raid_volume *vol)
  952 {
  953         struct g_raid_softc *sc;
  954 
  955         sc = vol->v_softc;
  956         g_topology_assert_not();
  957         sx_assert(&sc->sc_lock, SX_XLOCKED);
  958 
  959 //      if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
  960 //              return;
  961         vol->v_dirty = 1;
  962         G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
  963             vol->v_name);
  964         g_raid_write_metadata(sc, vol, NULL, NULL);
  965 }
  966 
  967 void
  968 g_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
  969 {
  970         struct g_raid_softc *sc;
  971         struct g_raid_volume *vol;
  972         struct g_raid_subdisk *sd;
  973         struct bio_queue_head queue;
  974         struct bio *cbp;
  975         int i;
  976 
  977         vol = tr->tro_volume;
  978         sc = vol->v_softc;
  979 
  980         /*
  981          * Allocate all bios before sending any request, so we can return
  982          * ENOMEM in nice and clean way.
  983          */
  984         bioq_init(&queue);
  985         for (i = 0; i < vol->v_disks_count; i++) {
  986                 sd = &vol->v_subdisks[i];
  987                 if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
  988                     sd->sd_state == G_RAID_SUBDISK_S_FAILED)
  989                         continue;
  990                 cbp = g_clone_bio(bp);
  991                 if (cbp == NULL)
  992                         goto failure;
  993                 cbp->bio_caller1 = sd;
  994                 bioq_insert_tail(&queue, cbp);
  995         }
  996         for (cbp = bioq_first(&queue); cbp != NULL;
  997             cbp = bioq_first(&queue)) {
  998                 bioq_remove(&queue, cbp);
  999                 sd = cbp->bio_caller1;
 1000                 cbp->bio_caller1 = NULL;
 1001                 g_raid_subdisk_iostart(sd, cbp);
 1002         }
 1003         return;
 1004 failure:
 1005         for (cbp = bioq_first(&queue); cbp != NULL;
 1006             cbp = bioq_first(&queue)) {
 1007                 bioq_remove(&queue, cbp);
 1008                 g_destroy_bio(cbp);
 1009         }
 1010         if (bp->bio_error == 0)
 1011                 bp->bio_error = ENOMEM;
 1012         g_raid_iodone(bp, bp->bio_error);
 1013 }
 1014 
 1015 static void
 1016 g_raid_tr_kerneldump_common_done(struct bio *bp)
 1017 {
 1018 
 1019         bp->bio_flags |= BIO_DONE;
 1020 }
 1021 
 1022 int
 1023 g_raid_tr_kerneldump_common(struct g_raid_tr_object *tr,
 1024     void *virtual, vm_offset_t physical, off_t offset, size_t length)
 1025 {
 1026         struct g_raid_softc *sc;
 1027         struct g_raid_volume *vol;
 1028         struct bio bp;
 1029 
 1030         vol = tr->tro_volume;
 1031         sc = vol->v_softc;
 1032 
 1033         bzero(&bp, sizeof(bp));
 1034         bp.bio_cmd = BIO_WRITE;
 1035         bp.bio_done = g_raid_tr_kerneldump_common_done;
 1036         bp.bio_attribute = NULL;
 1037         bp.bio_offset = offset;
 1038         bp.bio_length = length;
 1039         bp.bio_data = virtual;
 1040         bp.bio_to = vol->v_provider;
 1041 
 1042         g_raid_start(&bp);
 1043         while (!(bp.bio_flags & BIO_DONE)) {
 1044                 G_RAID_DEBUG1(4, sc, "Poll...");
 1045                 g_raid_poll(sc);
 1046                 DELAY(10);
 1047         }
 1048 
 1049         return (bp.bio_error != 0 ? EIO : 0);
 1050 }
 1051 
 1052 static int
 1053 g_raid_dump(void *arg,
 1054     void *virtual, vm_offset_t physical, off_t offset, size_t length)
 1055 {
 1056         struct g_raid_volume *vol;
 1057         int error;
 1058 
 1059         vol = (struct g_raid_volume *)arg;
 1060         G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.",
 1061             (long long unsigned)offset, (long long unsigned)length);
 1062 
 1063         error = G_RAID_TR_KERNELDUMP(vol->v_tr,
 1064             virtual, physical, offset, length);
 1065         return (error);
 1066 }
 1067 
 1068 static void
 1069 g_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp)
 1070 {
 1071         struct g_kerneldump *gkd;
 1072         struct g_provider *pp;
 1073         struct g_raid_volume *vol;
 1074 
 1075         gkd = (struct g_kerneldump*)bp->bio_data;
 1076         pp = bp->bio_to;
 1077         vol = pp->private;
 1078         g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)",
 1079                 pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
 1080         gkd->di.dumper = g_raid_dump;
 1081         gkd->di.priv = vol;
 1082         gkd->di.blocksize = vol->v_sectorsize;
 1083         gkd->di.maxiosize = DFLTPHYS;
 1084         gkd->di.mediaoffset = gkd->offset;
 1085         if ((gkd->offset + gkd->length) > vol->v_mediasize)
 1086                 gkd->length = vol->v_mediasize - gkd->offset;
 1087         gkd->di.mediasize = gkd->length;
 1088         g_io_deliver(bp, 0);
 1089 }
 1090 
 1091 static void
 1092 g_raid_candelete(struct g_raid_softc *sc, struct bio *bp)
 1093 {
 1094         struct g_provider *pp;
 1095         struct g_raid_volume *vol;
 1096         struct g_raid_subdisk *sd;
 1097         int *val;
 1098         int i;
 1099 
 1100         val = (int *)bp->bio_data;
 1101         pp = bp->bio_to;
 1102         vol = pp->private;
 1103         *val = 0;
 1104         for (i = 0; i < vol->v_disks_count; i++) {
 1105                 sd = &vol->v_subdisks[i];
 1106                 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
 1107                         continue;
 1108                 if (sd->sd_disk->d_candelete) {
 1109                         *val = 1;
 1110                         break;
 1111                 }
 1112         }
 1113         g_io_deliver(bp, 0);
 1114 }
 1115 
 1116 static void
 1117 g_raid_start(struct bio *bp)
 1118 {
 1119         struct g_raid_softc *sc;
 1120 
 1121         sc = bp->bio_to->geom->softc;
 1122         /*
 1123          * If sc == NULL or there are no valid disks, provider's error
 1124          * should be set and g_raid_start() should not be called at all.
 1125          */
 1126 //      KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING,
 1127 //          ("Provider's error should be set (error=%d)(mirror=%s).",
 1128 //          bp->bio_to->error, bp->bio_to->name));
 1129         G_RAID_LOGREQ(3, bp, "Request received.");
 1130 
 1131         switch (bp->bio_cmd) {
 1132         case BIO_READ:
 1133         case BIO_WRITE:
 1134         case BIO_DELETE:
 1135         case BIO_FLUSH:
 1136                 break;
 1137         case BIO_GETATTR:
 1138                 if (!strcmp(bp->bio_attribute, "GEOM::candelete"))
 1139                         g_raid_candelete(sc, bp);
 1140                 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
 1141                         g_raid_kerneldump(sc, bp);
 1142                 else
 1143                         g_io_deliver(bp, EOPNOTSUPP);
 1144                 return;
 1145         default:
 1146                 g_io_deliver(bp, EOPNOTSUPP);
 1147                 return;
 1148         }
 1149         mtx_lock(&sc->sc_queue_mtx);
 1150         bioq_disksort(&sc->sc_queue, bp);
 1151         mtx_unlock(&sc->sc_queue_mtx);
 1152         if (!dumping) {
 1153                 G_RAID_DEBUG1(4, sc, "Waking up %p.", sc);
 1154                 wakeup(sc);
 1155         }
 1156 }
 1157 
 1158 static int
 1159 g_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len)
 1160 {
 1161         /*
 1162          * 5 cases:
 1163          * (1) bp entirely below NO
 1164          * (2) bp entirely above NO
 1165          * (3) bp start below, but end in range YES
 1166          * (4) bp entirely within YES
 1167          * (5) bp starts within, ends above YES
 1168          *
 1169          * lock range 10-19 (offset 10 length 10)
 1170          * (1) 1-5: first if kicks it out
 1171          * (2) 30-35: second if kicks it out
 1172          * (3) 5-15: passes both ifs
 1173          * (4) 12-14: passes both ifs
 1174          * (5) 19-20: passes both
 1175          */
 1176         off_t lend = lstart + len - 1;
 1177         off_t bstart = bp->bio_offset;
 1178         off_t bend = bp->bio_offset + bp->bio_length - 1;
 1179 
 1180         if (bend < lstart)
 1181                 return (0);
 1182         if (lend < bstart)
 1183                 return (0);
 1184         return (1);
 1185 }
 1186 
 1187 static int
 1188 g_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp)
 1189 {
 1190         struct g_raid_lock *lp;
 1191 
 1192         sx_assert(&vol->v_softc->sc_lock, SX_LOCKED);
 1193 
 1194         LIST_FOREACH(lp, &vol->v_locks, l_next) {
 1195                 if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length))
 1196                         return (1);
 1197         }
 1198         return (0);
 1199 }
 1200 
 1201 static void
 1202 g_raid_start_request(struct bio *bp)
 1203 {
 1204         struct g_raid_softc *sc;
 1205         struct g_raid_volume *vol;
 1206 
 1207         sc = bp->bio_to->geom->softc;
 1208         sx_assert(&sc->sc_lock, SX_LOCKED);
 1209         vol = bp->bio_to->private;
 1210 
 1211         /*
 1212          * Check to see if this item is in a locked range.  If so,
 1213          * queue it to our locked queue and return.  We'll requeue
 1214          * it when the range is unlocked.  Internal I/O for the
 1215          * rebuild/rescan/recovery process is excluded from this
 1216          * check so we can actually do the recovery.
 1217          */
 1218         if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) &&
 1219             g_raid_is_in_locked_range(vol, bp)) {
 1220                 G_RAID_LOGREQ(3, bp, "Defer request.");
 1221                 bioq_insert_tail(&vol->v_locked, bp);
 1222                 return;
 1223         }
 1224 
 1225         /*
 1226          * If we're actually going to do the write/delete, then
 1227          * update the idle stats for the volume.
 1228          */
 1229         if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
 1230                 if (!vol->v_dirty)
 1231                         g_raid_dirty(vol);
 1232                 vol->v_writes++;
 1233         }
 1234 
 1235         /*
 1236          * Put request onto inflight queue, so we can check if new
 1237          * synchronization requests don't collide with it.  Then tell
 1238          * the transformation layer to start the I/O.
 1239          */
 1240         bioq_insert_tail(&vol->v_inflight, bp);
 1241         G_RAID_LOGREQ(4, bp, "Request started");
 1242         G_RAID_TR_IOSTART(vol->v_tr, bp);
 1243 }
 1244 
 1245 static void
 1246 g_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp)
 1247 {
 1248         off_t off, len;
 1249         struct bio *nbp;
 1250         struct g_raid_lock *lp;
 1251 
 1252         vol->v_pending_lock = 0;
 1253         LIST_FOREACH(lp, &vol->v_locks, l_next) {
 1254                 if (lp->l_pending) {
 1255                         off = lp->l_offset;
 1256                         len = lp->l_length;
 1257                         lp->l_pending = 0;
 1258                         TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) {
 1259                                 if (g_raid_bio_overlaps(nbp, off, len))
 1260                                         lp->l_pending++;
 1261                         }
 1262                         if (lp->l_pending) {
 1263                                 vol->v_pending_lock = 1;
 1264                                 G_RAID_DEBUG1(4, vol->v_softc,
 1265                                     "Deferred lock(%jd, %jd) has %d pending",
 1266                                     (intmax_t)off, (intmax_t)(off + len),
 1267                                     lp->l_pending);
 1268                                 continue;
 1269                         }
 1270                         G_RAID_DEBUG1(4, vol->v_softc,
 1271                             "Deferred lock of %jd to %jd completed",
 1272                             (intmax_t)off, (intmax_t)(off + len));
 1273                         G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
 1274                 }
 1275         }
 1276 }
 1277 
 1278 void
 1279 g_raid_iodone(struct bio *bp, int error)
 1280 {
 1281         struct g_raid_softc *sc;
 1282         struct g_raid_volume *vol;
 1283 
 1284         sc = bp->bio_to->geom->softc;
 1285         sx_assert(&sc->sc_lock, SX_LOCKED);
 1286         vol = bp->bio_to->private;
 1287         G_RAID_LOGREQ(3, bp, "Request done: %d.", error);
 1288 
 1289         /* Update stats if we done write/delete. */
 1290         if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
 1291                 vol->v_writes--;
 1292                 vol->v_last_write = time_uptime;
 1293         }
 1294 
 1295         bioq_remove(&vol->v_inflight, bp);
 1296         if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp))
 1297                 g_raid_finish_with_locked_ranges(vol, bp);
 1298         getmicrouptime(&vol->v_last_done);
 1299         g_io_deliver(bp, error);
 1300 }
 1301 
 1302 int
 1303 g_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len,
 1304     struct bio *ignore, void *argp)
 1305 {
 1306         struct g_raid_softc *sc;
 1307         struct g_raid_lock *lp;
 1308         struct bio *bp;
 1309 
 1310         sc = vol->v_softc;
 1311         lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO);
 1312         LIST_INSERT_HEAD(&vol->v_locks, lp, l_next);
 1313         lp->l_offset = off;
 1314         lp->l_length = len;
 1315         lp->l_callback_arg = argp;
 1316 
 1317         lp->l_pending = 0;
 1318         TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) {
 1319                 if (bp != ignore && g_raid_bio_overlaps(bp, off, len))
 1320                         lp->l_pending++;
 1321         }       
 1322 
 1323         /*
 1324          * If there are any writes that are pending, we return EBUSY.  All
 1325          * callers will have to wait until all pending writes clear.
 1326          */
 1327         if (lp->l_pending > 0) {
 1328                 vol->v_pending_lock = 1;
 1329                 G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend",
 1330                     (intmax_t)off, (intmax_t)(off+len), lp->l_pending);
 1331                 return (EBUSY);
 1332         }
 1333         G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd",
 1334             (intmax_t)off, (intmax_t)(off+len));
 1335         G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
 1336         return (0);
 1337 }
 1338 
 1339 int
 1340 g_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len)
 1341 {
 1342         struct g_raid_lock *lp;
 1343         struct g_raid_softc *sc;
 1344         struct bio *bp;
 1345 
 1346         sc = vol->v_softc;
 1347         LIST_FOREACH(lp, &vol->v_locks, l_next) {
 1348                 if (lp->l_offset == off && lp->l_length == len) {
 1349                         LIST_REMOVE(lp, l_next);
 1350                         /* XXX
 1351                          * Right now we just put them all back on the queue
 1352                          * and hope for the best.  We hope this because any
 1353                          * locked ranges will go right back on this list
 1354                          * when the worker thread runs.
 1355                          * XXX
 1356                          */
 1357                         G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd",
 1358                             (intmax_t)lp->l_offset,
 1359                             (intmax_t)(lp->l_offset+lp->l_length));
 1360                         mtx_lock(&sc->sc_queue_mtx);
 1361                         while ((bp = bioq_takefirst(&vol->v_locked)) != NULL)
 1362                                 bioq_disksort(&sc->sc_queue, bp);
 1363                         mtx_unlock(&sc->sc_queue_mtx);
 1364                         free(lp, M_RAID);
 1365                         return (0);
 1366                 }
 1367         }
 1368         return (EINVAL);
 1369 }
 1370 
 1371 void
 1372 g_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp)
 1373 {
 1374         struct g_consumer *cp;
 1375         struct g_raid_disk *disk, *tdisk;
 1376 
 1377         bp->bio_caller1 = sd;
 1378 
 1379         /*
 1380          * Make sure that the disk is present. Generally it is a task of
 1381          * transformation layers to not send requests to absent disks, but
 1382          * it is better to be safe and report situation then sorry.
 1383          */
 1384         if (sd->sd_disk == NULL) {
 1385                 G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!");
 1386 nodisk:
 1387                 bp->bio_from = NULL;
 1388                 bp->bio_to = NULL;
 1389                 bp->bio_error = ENXIO;
 1390                 g_raid_disk_done(bp);
 1391                 return;
 1392         }
 1393         disk = sd->sd_disk;
 1394         if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
 1395             disk->d_state != G_RAID_DISK_S_FAILED) {
 1396                 G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a "
 1397                     "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
 1398                 goto nodisk;
 1399         }
 1400 
 1401         cp = disk->d_consumer;
 1402         bp->bio_from = cp;
 1403         bp->bio_to = cp->provider;
 1404         cp->index++;
 1405 
 1406         /* Update average disks load. */
 1407         TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) {
 1408                 if (tdisk->d_consumer == NULL)
 1409                         tdisk->d_load = 0;
 1410                 else
 1411                         tdisk->d_load = (tdisk->d_consumer->index *
 1412                             G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8;
 1413         }
 1414 
 1415         disk->d_last_offset = bp->bio_offset + bp->bio_length;
 1416         if (dumping) {
 1417                 G_RAID_LOGREQ(3, bp, "Sending dumping request.");
 1418                 if (bp->bio_cmd == BIO_WRITE) {
 1419                         bp->bio_error = g_raid_subdisk_kerneldump(sd,
 1420                             bp->bio_data, 0, bp->bio_offset, bp->bio_length);
 1421                 } else
 1422                         bp->bio_error = EOPNOTSUPP;
 1423                 g_raid_disk_done(bp);
 1424         } else {
 1425                 bp->bio_done = g_raid_disk_done;
 1426                 bp->bio_offset += sd->sd_offset;
 1427                 G_RAID_LOGREQ(3, bp, "Sending request.");
 1428                 g_io_request(bp, cp);
 1429         }
 1430 }
 1431 
 1432 int
 1433 g_raid_subdisk_kerneldump(struct g_raid_subdisk *sd,
 1434     void *virtual, vm_offset_t physical, off_t offset, size_t length)
 1435 {
 1436 
 1437         if (sd->sd_disk == NULL)
 1438                 return (ENXIO);
 1439         if (sd->sd_disk->d_kd.di.dumper == NULL)
 1440                 return (EOPNOTSUPP);
 1441         return (dump_write(&sd->sd_disk->d_kd.di,
 1442             virtual, physical,
 1443             sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset,
 1444             length));
 1445 }
 1446 
 1447 static void
 1448 g_raid_disk_done(struct bio *bp)
 1449 {
 1450         struct g_raid_softc *sc;
 1451         struct g_raid_subdisk *sd;
 1452 
 1453         sd = bp->bio_caller1;
 1454         sc = sd->sd_softc;
 1455         mtx_lock(&sc->sc_queue_mtx);
 1456         bioq_disksort(&sc->sc_queue, bp);
 1457         mtx_unlock(&sc->sc_queue_mtx);
 1458         if (!dumping)
 1459                 wakeup(sc);
 1460 }
 1461 
 1462 static void
 1463 g_raid_disk_done_request(struct bio *bp)
 1464 {
 1465         struct g_raid_softc *sc;
 1466         struct g_raid_disk *disk;
 1467         struct g_raid_subdisk *sd;
 1468         struct g_raid_volume *vol;
 1469 
 1470         g_topology_assert_not();
 1471 
 1472         G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
 1473         sd = bp->bio_caller1;
 1474         sc = sd->sd_softc;
 1475         vol = sd->sd_volume;
 1476         if (bp->bio_from != NULL) {
 1477                 bp->bio_from->index--;
 1478                 disk = bp->bio_from->private;
 1479                 if (disk == NULL)
 1480                         g_raid_kill_consumer(sc, bp->bio_from);
 1481         }
 1482         bp->bio_offset -= sd->sd_offset;
 1483 
 1484         G_RAID_TR_IODONE(vol->v_tr, sd, bp);
 1485 }
 1486 
 1487 static void
 1488 g_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
 1489 {
 1490 
 1491         if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
 1492                 ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
 1493         else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
 1494                 ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
 1495         else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
 1496                 ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
 1497         else
 1498                 ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
 1499         if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
 1500                 KASSERT(ep->e_error == 0,
 1501                     ("Error cannot be handled."));
 1502                 g_raid_event_free(ep);
 1503         } else {
 1504                 ep->e_flags |= G_RAID_EVENT_DONE;
 1505                 G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
 1506                 mtx_lock(&sc->sc_queue_mtx);
 1507                 wakeup(ep);
 1508                 mtx_unlock(&sc->sc_queue_mtx);
 1509         }
 1510 }
 1511 
 1512 /*
 1513  * Worker thread.
 1514  */
 1515 static void
 1516 g_raid_worker(void *arg)
 1517 {
 1518         struct g_raid_softc *sc;
 1519         struct g_raid_event *ep;
 1520         struct g_raid_volume *vol;
 1521         struct bio *bp;
 1522         struct timeval now, t;
 1523         int timeout, rv;
 1524 
 1525         sc = arg;
 1526         thread_lock(curthread);
 1527         sched_prio(curthread, PRIBIO);
 1528         thread_unlock(curthread);
 1529 
 1530         sx_xlock(&sc->sc_lock);
 1531         for (;;) {
 1532                 mtx_lock(&sc->sc_queue_mtx);
 1533                 /*
 1534                  * First take a look at events.
 1535                  * This is important to handle events before any I/O requests.
 1536                  */
 1537                 bp = NULL;
 1538                 vol = NULL;
 1539                 rv = 0;
 1540                 ep = TAILQ_FIRST(&sc->sc_events);
 1541                 if (ep != NULL)
 1542                         TAILQ_REMOVE(&sc->sc_events, ep, e_next);
 1543                 else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
 1544                         ;
 1545                 else {
 1546                         getmicrouptime(&now);
 1547                         t = now;
 1548                         TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
 1549                                 if (bioq_first(&vol->v_inflight) == NULL &&
 1550                                     vol->v_tr &&
 1551                                     timevalcmp(&vol->v_last_done, &t, < ))
 1552                                         t = vol->v_last_done;
 1553                         }
 1554                         timevalsub(&t, &now);
 1555                         timeout = g_raid_idle_threshold +
 1556                             t.tv_sec * 1000000 + t.tv_usec;
 1557                         if (timeout > 0) {
 1558                                 /*
 1559                                  * Two steps to avoid overflows at HZ=1000
 1560                                  * and idle timeouts > 2.1s.  Some rounding
 1561                                  * errors can occur, but they are < 1tick,
 1562                                  * which is deemed to be close enough for
 1563                                  * this purpose.
 1564                                  */
 1565                                 int micpertic = 1000000 / hz;
 1566                                 timeout = (timeout + micpertic - 1) / micpertic;
 1567                                 sx_xunlock(&sc->sc_lock);
 1568                                 MSLEEP(rv, sc, &sc->sc_queue_mtx,
 1569                                     PRIBIO | PDROP, "-", timeout);
 1570                                 sx_xlock(&sc->sc_lock);
 1571                                 goto process;
 1572                         } else
 1573                                 rv = EWOULDBLOCK;
 1574                 }
 1575                 mtx_unlock(&sc->sc_queue_mtx);
 1576 process:
 1577                 if (ep != NULL) {
 1578                         g_raid_handle_event(sc, ep);
 1579                 } else if (bp != NULL) {
 1580                         if (bp->bio_to != NULL &&
 1581                             bp->bio_to->geom == sc->sc_geom)
 1582                                 g_raid_start_request(bp);
 1583                         else
 1584                                 g_raid_disk_done_request(bp);
 1585                 } else if (rv == EWOULDBLOCK) {
 1586                         TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
 1587                                 g_raid_clean(vol, -1);
 1588                                 if (bioq_first(&vol->v_inflight) == NULL &&
 1589                                     vol->v_tr) {
 1590                                         t.tv_sec = g_raid_idle_threshold / 1000000;
 1591                                         t.tv_usec = g_raid_idle_threshold % 1000000;
 1592                                         timevaladd(&t, &vol->v_last_done);
 1593                                         getmicrouptime(&now);
 1594                                         if (timevalcmp(&t, &now, <= )) {
 1595                                                 G_RAID_TR_IDLE(vol->v_tr);
 1596                                                 vol->v_last_done = now;
 1597                                         }
 1598                                 }
 1599                         }
 1600                 }
 1601                 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
 1602                         g_raid_destroy_node(sc, 1);     /* May not return. */
 1603         }
 1604 }
 1605 
 1606 static void
 1607 g_raid_poll(struct g_raid_softc *sc)
 1608 {
 1609         struct g_raid_event *ep;
 1610         struct bio *bp;
 1611 
 1612         sx_xlock(&sc->sc_lock);
 1613         mtx_lock(&sc->sc_queue_mtx);
 1614         /*
 1615          * First take a look at events.
 1616          * This is important to handle events before any I/O requests.
 1617          */
 1618         ep = TAILQ_FIRST(&sc->sc_events);
 1619         if (ep != NULL) {
 1620                 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
 1621                 mtx_unlock(&sc->sc_queue_mtx);
 1622                 g_raid_handle_event(sc, ep);
 1623                 goto out;
 1624         }
 1625         bp = bioq_takefirst(&sc->sc_queue);
 1626         if (bp != NULL) {
 1627                 mtx_unlock(&sc->sc_queue_mtx);
 1628                 if (bp->bio_from == NULL ||
 1629                     bp->bio_from->geom != sc->sc_geom)
 1630                         g_raid_start_request(bp);
 1631                 else
 1632                         g_raid_disk_done_request(bp);
 1633         }
 1634 out:
 1635         sx_xunlock(&sc->sc_lock);
 1636 }
 1637 
 1638 static void
 1639 g_raid_launch_provider(struct g_raid_volume *vol)
 1640 {
 1641         struct g_raid_disk *disk;
 1642         struct g_raid_softc *sc;
 1643         struct g_provider *pp;
 1644         char name[G_RAID_MAX_VOLUMENAME];
 1645         char   announce_buf[80], buf1[32];
 1646         off_t off;
 1647 
 1648         sc = vol->v_softc;
 1649         sx_assert(&sc->sc_lock, SX_LOCKED);
 1650 
 1651         g_topology_lock();
 1652         /* Try to name provider with volume name. */
 1653         snprintf(name, sizeof(name), "raid/%s", vol->v_name);
 1654         if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
 1655             g_provider_by_name(name) != NULL) {
 1656                 /* Otherwise use sequential volume number. */
 1657                 snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
 1658         }
 1659 
 1660         /*
 1661          * Create a /dev/ar%d that the old ataraid(4) stack once
 1662          * created as an alias for /dev/raid/r%d if requested.
 1663          * This helps going from stable/7 ataraid devices to newer
 1664          * FreeBSD releases. sbruno 07 MAY 2013
 1665          */
 1666 
 1667         if (ar_legacy_aliases) {
 1668                 snprintf(announce_buf, sizeof(announce_buf),
 1669                         "kern.devalias.%s", name);
 1670                 snprintf(buf1, sizeof(buf1),
 1671                         "ar%d", vol->v_global_id);
 1672                 setenv(announce_buf, buf1);
 1673         }
 1674 
 1675         pp = g_new_providerf(sc->sc_geom, "%s", name);
 1676         pp->private = vol;
 1677         pp->mediasize = vol->v_mediasize;
 1678         pp->sectorsize = vol->v_sectorsize;
 1679         pp->stripesize = 0;
 1680         pp->stripeoffset = 0;
 1681         if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
 1682             vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
 1683             vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
 1684             vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
 1685                 if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
 1686                     disk->d_consumer != NULL &&
 1687                     disk->d_consumer->provider != NULL) {
 1688                         pp->stripesize = disk->d_consumer->provider->stripesize;
 1689                         off = disk->d_consumer->provider->stripeoffset;
 1690                         pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
 1691                         if (off > 0)
 1692                                 pp->stripeoffset %= off;
 1693                 }
 1694                 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
 1695                         pp->stripesize *= (vol->v_disks_count - 1);
 1696                         pp->stripeoffset *= (vol->v_disks_count - 1);
 1697                 }
 1698         } else
 1699                 pp->stripesize = vol->v_strip_size;
 1700         vol->v_provider = pp;
 1701         g_error_provider(pp, 0);
 1702         g_topology_unlock();
 1703         G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
 1704             pp->name, vol->v_name);
 1705 }
 1706 
 1707 static void
 1708 g_raid_destroy_provider(struct g_raid_volume *vol)
 1709 {
 1710         struct g_raid_softc *sc;
 1711         struct g_provider *pp;
 1712         struct bio *bp, *tmp;
 1713 
 1714         g_topology_assert_not();
 1715         sc = vol->v_softc;
 1716         pp = vol->v_provider;
 1717         KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
 1718 
 1719         g_topology_lock();
 1720         g_error_provider(pp, ENXIO);
 1721         mtx_lock(&sc->sc_queue_mtx);
 1722         TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
 1723                 if (bp->bio_to != pp)
 1724                         continue;
 1725                 bioq_remove(&sc->sc_queue, bp);
 1726                 g_io_deliver(bp, ENXIO);
 1727         }
 1728         mtx_unlock(&sc->sc_queue_mtx);
 1729         G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
 1730             pp->name, vol->v_name);
 1731         g_wither_provider(pp, ENXIO);
 1732         g_topology_unlock();
 1733         vol->v_provider = NULL;
 1734 }
 1735 
 1736 /*
 1737  * Update device state.
 1738  */
 1739 static int
 1740 g_raid_update_volume(struct g_raid_volume *vol, u_int event)
 1741 {
 1742         struct g_raid_softc *sc;
 1743 
 1744         sc = vol->v_softc;
 1745         sx_assert(&sc->sc_lock, SX_XLOCKED);
 1746 
 1747         G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
 1748             g_raid_volume_event2str(event),
 1749             vol->v_name);
 1750         switch (event) {
 1751         case G_RAID_VOLUME_E_DOWN:
 1752                 if (vol->v_provider != NULL)
 1753                         g_raid_destroy_provider(vol);
 1754                 break;
 1755         case G_RAID_VOLUME_E_UP:
 1756                 if (vol->v_provider == NULL)
 1757                         g_raid_launch_provider(vol);
 1758                 break;
 1759         case G_RAID_VOLUME_E_START:
 1760                 if (vol->v_tr)
 1761                         G_RAID_TR_START(vol->v_tr);
 1762                 return (0);
 1763         default:
 1764                 if (sc->sc_md)
 1765                         G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
 1766                 return (0);
 1767         }
 1768 
 1769         /* Manage root mount release. */
 1770         if (vol->v_starting) {
 1771                 vol->v_starting = 0;
 1772                 G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
 1773                 root_mount_rel(vol->v_rootmount);
 1774                 vol->v_rootmount = NULL;
 1775         }
 1776         if (vol->v_stopping && vol->v_provider_open == 0)
 1777                 g_raid_destroy_volume(vol);
 1778         return (0);
 1779 }
 1780 
 1781 /*
 1782  * Update subdisk state.
 1783  */
 1784 static int
 1785 g_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
 1786 {
 1787         struct g_raid_softc *sc;
 1788         struct g_raid_volume *vol;
 1789 
 1790         sc = sd->sd_softc;
 1791         vol = sd->sd_volume;
 1792         sx_assert(&sc->sc_lock, SX_XLOCKED);
 1793 
 1794         G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
 1795             g_raid_subdisk_event2str(event),
 1796             vol->v_name, sd->sd_pos,
 1797             sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
 1798         if (vol->v_tr)
 1799                 G_RAID_TR_EVENT(vol->v_tr, sd, event);
 1800 
 1801         return (0);
 1802 }
 1803 
 1804 /*
 1805  * Update disk state.
 1806  */
 1807 static int
 1808 g_raid_update_disk(struct g_raid_disk *disk, u_int event)
 1809 {
 1810         struct g_raid_softc *sc;
 1811 
 1812         sc = disk->d_softc;
 1813         sx_assert(&sc->sc_lock, SX_XLOCKED);
 1814 
 1815         G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
 1816             g_raid_disk_event2str(event),
 1817             g_raid_get_diskname(disk));
 1818 
 1819         if (sc->sc_md)
 1820                 G_RAID_MD_EVENT(sc->sc_md, disk, event);
 1821         return (0);
 1822 }
 1823 
 1824 /*
 1825  * Node event.
 1826  */
 1827 static int
 1828 g_raid_update_node(struct g_raid_softc *sc, u_int event)
 1829 {
 1830         sx_assert(&sc->sc_lock, SX_XLOCKED);
 1831 
 1832         G_RAID_DEBUG1(2, sc, "Event %s for the array.",
 1833             g_raid_node_event2str(event));
 1834 
 1835         if (event == G_RAID_NODE_E_WAKE)
 1836                 return (0);
 1837         if (sc->sc_md)
 1838                 G_RAID_MD_EVENT(sc->sc_md, NULL, event);
 1839         return (0);
 1840 }
 1841 
 1842 static int
 1843 g_raid_access(struct g_provider *pp, int acr, int acw, int ace)
 1844 {
 1845         struct g_raid_volume *vol;
 1846         struct g_raid_softc *sc;
 1847         int dcw, opens, error = 0;
 1848 
 1849         g_topology_assert();
 1850         sc = pp->geom->softc;
 1851         vol = pp->private;
 1852         KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
 1853         KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
 1854 
 1855         G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
 1856             acr, acw, ace);
 1857         dcw = pp->acw + acw;
 1858 
 1859         g_topology_unlock();
 1860         sx_xlock(&sc->sc_lock);
 1861         /* Deny new opens while dying. */
 1862         if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
 1863                 error = ENXIO;
 1864                 goto out;
 1865         }
 1866         /* Deny write opens for read-only volumes. */
 1867         if (vol->v_read_only && acw > 0) {
 1868                 error = EROFS;
 1869                 goto out;
 1870         }
 1871         if (dcw == 0)
 1872                 g_raid_clean(vol, dcw);
 1873         vol->v_provider_open += acr + acw + ace;
 1874         /* Handle delayed node destruction. */
 1875         if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
 1876             vol->v_provider_open == 0) {
 1877                 /* Count open volumes. */
 1878                 opens = g_raid_nopens(sc);
 1879                 if (opens == 0) {
 1880                         sc->sc_stopping = G_RAID_DESTROY_HARD;
 1881                         /* Wake up worker to make it selfdestruct. */
 1882                         g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
 1883                 }
 1884         }
 1885         /* Handle open volume destruction. */
 1886         if (vol->v_stopping && vol->v_provider_open == 0)
 1887                 g_raid_destroy_volume(vol);
 1888 out:
 1889         sx_xunlock(&sc->sc_lock);
 1890         g_topology_lock();
 1891         return (error);
 1892 }
 1893 
 1894 struct g_raid_softc *
 1895 g_raid_create_node(struct g_class *mp,
 1896     const char *name, struct g_raid_md_object *md)
 1897 {
 1898         struct g_raid_softc *sc;
 1899         struct g_geom *gp;
 1900         int error;
 1901 
 1902         g_topology_assert();
 1903         G_RAID_DEBUG(1, "Creating array %s.", name);
 1904 
 1905         gp = g_new_geomf(mp, "%s", name);
 1906         sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
 1907         gp->start = g_raid_start;
 1908         gp->orphan = g_raid_orphan;
 1909         gp->access = g_raid_access;
 1910         gp->dumpconf = g_raid_dumpconf;
 1911 
 1912         sc->sc_md = md;
 1913         sc->sc_geom = gp;
 1914         sc->sc_flags = 0;
 1915         TAILQ_INIT(&sc->sc_volumes);
 1916         TAILQ_INIT(&sc->sc_disks);
 1917         sx_init(&sc->sc_lock, "graid:lock");
 1918         mtx_init(&sc->sc_queue_mtx, "graid:queue", NULL, MTX_DEF);
 1919         TAILQ_INIT(&sc->sc_events);
 1920         bioq_init(&sc->sc_queue);
 1921         gp->softc = sc;
 1922         error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
 1923             "g_raid %s", name);
 1924         if (error != 0) {
 1925                 G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
 1926                 mtx_destroy(&sc->sc_queue_mtx);
 1927                 sx_destroy(&sc->sc_lock);
 1928                 g_destroy_geom(sc->sc_geom);
 1929                 free(sc, M_RAID);
 1930                 return (NULL);
 1931         }
 1932 
 1933         G_RAID_DEBUG1(0, sc, "Array %s created.", name);
 1934         return (sc);
 1935 }
 1936 
 1937 struct g_raid_volume *
 1938 g_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
 1939 {
 1940         struct g_raid_volume    *vol, *vol1;
 1941         int i;
 1942 
 1943         G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
 1944         vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
 1945         vol->v_softc = sc;
 1946         strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
 1947         vol->v_state = G_RAID_VOLUME_S_STARTING;
 1948         vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
 1949         vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
 1950         vol->v_rotate_parity = 1;
 1951         bioq_init(&vol->v_inflight);
 1952         bioq_init(&vol->v_locked);
 1953         LIST_INIT(&vol->v_locks);
 1954         for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
 1955                 vol->v_subdisks[i].sd_softc = sc;
 1956                 vol->v_subdisks[i].sd_volume = vol;
 1957                 vol->v_subdisks[i].sd_pos = i;
 1958                 vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
 1959         }
 1960 
 1961         /* Find free ID for this volume. */
 1962         g_topology_lock();
 1963         vol1 = vol;
 1964         if (id >= 0) {
 1965                 LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
 1966                         if (vol1->v_global_id == id)
 1967                                 break;
 1968                 }
 1969         }
 1970         if (vol1 != NULL) {
 1971                 for (id = 0; ; id++) {
 1972                         LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
 1973                                 if (vol1->v_global_id == id)
 1974                                         break;
 1975                         }
 1976                         if (vol1 == NULL)
 1977                                 break;
 1978                 }
 1979         }
 1980         vol->v_global_id = id;
 1981         LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
 1982         g_topology_unlock();
 1983 
 1984         /* Delay root mounting. */
 1985         vol->v_rootmount = root_mount_hold("GRAID");
 1986         G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
 1987         vol->v_starting = 1;
 1988         TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
 1989         return (vol);
 1990 }
 1991 
 1992 struct g_raid_disk *
 1993 g_raid_create_disk(struct g_raid_softc *sc)
 1994 {
 1995         struct g_raid_disk      *disk;
 1996 
 1997         G_RAID_DEBUG1(1, sc, "Creating disk.");
 1998         disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
 1999         disk->d_softc = sc;
 2000         disk->d_state = G_RAID_DISK_S_NONE;
 2001         TAILQ_INIT(&disk->d_subdisks);
 2002         TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
 2003         return (disk);
 2004 }
 2005 
 2006 int g_raid_start_volume(struct g_raid_volume *vol)
 2007 {
 2008         struct g_raid_tr_class *class;
 2009         struct g_raid_tr_object *obj;
 2010         int status;
 2011 
 2012         G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
 2013         LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
 2014                 if (!class->trc_enable)
 2015                         continue;
 2016                 G_RAID_DEBUG1(2, vol->v_softc,
 2017                     "Tasting volume %s for %s transformation.",
 2018                     vol->v_name, class->name);
 2019                 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
 2020                     M_WAITOK);
 2021                 obj->tro_class = class;
 2022                 obj->tro_volume = vol;
 2023                 status = G_RAID_TR_TASTE(obj, vol);
 2024                 if (status != G_RAID_TR_TASTE_FAIL)
 2025                         break;
 2026                 kobj_delete((kobj_t)obj, M_RAID);
 2027         }
 2028         if (class == NULL) {
 2029                 G_RAID_DEBUG1(0, vol->v_softc,
 2030                     "No transformation module found for %s.",
 2031                     vol->v_name);
 2032                 vol->v_tr = NULL;
 2033                 g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
 2034                 g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
 2035                     G_RAID_EVENT_VOLUME);
 2036                 return (-1);
 2037         }
 2038         G_RAID_DEBUG1(2, vol->v_softc,
 2039             "Transformation module %s chosen for %s.",
 2040             class->name, vol->v_name);
 2041         vol->v_tr = obj;
 2042         return (0);
 2043 }
 2044 
 2045 int
 2046 g_raid_destroy_node(struct g_raid_softc *sc, int worker)
 2047 {
 2048         struct g_raid_volume *vol, *tmpv;
 2049         struct g_raid_disk *disk, *tmpd;
 2050         int error = 0;
 2051 
 2052         sc->sc_stopping = G_RAID_DESTROY_HARD;
 2053         TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
 2054                 if (g_raid_destroy_volume(vol))
 2055                         error = EBUSY;
 2056         }
 2057         if (error)
 2058                 return (error);
 2059         TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
 2060                 if (g_raid_destroy_disk(disk))
 2061                         error = EBUSY;
 2062         }
 2063         if (error)
 2064                 return (error);
 2065         if (sc->sc_md) {
 2066                 G_RAID_MD_FREE(sc->sc_md);
 2067                 kobj_delete((kobj_t)sc->sc_md, M_RAID);
 2068                 sc->sc_md = NULL;
 2069         }
 2070         if (sc->sc_geom != NULL) {
 2071                 G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
 2072                 g_topology_lock();
 2073                 sc->sc_geom->softc = NULL;
 2074                 g_wither_geom(sc->sc_geom, ENXIO);
 2075                 g_topology_unlock();
 2076                 sc->sc_geom = NULL;
 2077         } else
 2078                 G_RAID_DEBUG(1, "Array destroyed.");
 2079         if (worker) {
 2080                 g_raid_event_cancel(sc, sc);
 2081                 mtx_destroy(&sc->sc_queue_mtx);
 2082                 sx_xunlock(&sc->sc_lock);
 2083                 sx_destroy(&sc->sc_lock);
 2084                 wakeup(&sc->sc_stopping);
 2085                 free(sc, M_RAID);
 2086                 curthread->td_pflags &= ~TDP_GEOM;
 2087                 G_RAID_DEBUG(1, "Thread exiting.");
 2088                 kproc_exit(0);
 2089         } else {
 2090                 /* Wake up worker to make it selfdestruct. */
 2091                 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
 2092         }
 2093         return (0);
 2094 }
 2095 
 2096 int
 2097 g_raid_destroy_volume(struct g_raid_volume *vol)
 2098 {
 2099         struct g_raid_softc *sc;
 2100         struct g_raid_disk *disk;
 2101         int i;
 2102 
 2103         sc = vol->v_softc;
 2104         G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
 2105         vol->v_stopping = 1;
 2106         if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
 2107                 if (vol->v_tr) {
 2108                         G_RAID_TR_STOP(vol->v_tr);
 2109                         return (EBUSY);
 2110                 } else
 2111                         vol->v_state = G_RAID_VOLUME_S_STOPPED;
 2112         }
 2113         if (g_raid_event_check(sc, vol) != 0)
 2114                 return (EBUSY);
 2115         if (vol->v_provider != NULL)
 2116                 return (EBUSY);
 2117         if (vol->v_provider_open != 0)
 2118                 return (EBUSY);
 2119         if (vol->v_tr) {
 2120                 G_RAID_TR_FREE(vol->v_tr);
 2121                 kobj_delete((kobj_t)vol->v_tr, M_RAID);
 2122                 vol->v_tr = NULL;
 2123         }
 2124         if (vol->v_rootmount)
 2125                 root_mount_rel(vol->v_rootmount);
 2126         g_topology_lock();
 2127         LIST_REMOVE(vol, v_global_next);
 2128         g_topology_unlock();
 2129         TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
 2130         for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
 2131                 g_raid_event_cancel(sc, &vol->v_subdisks[i]);
 2132                 disk = vol->v_subdisks[i].sd_disk;
 2133                 if (disk == NULL)
 2134                         continue;
 2135                 TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
 2136         }
 2137         G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
 2138         if (sc->sc_md)
 2139                 G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
 2140         g_raid_event_cancel(sc, vol);
 2141         free(vol, M_RAID);
 2142         if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
 2143                 /* Wake up worker to let it selfdestruct. */
 2144                 g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
 2145         }
 2146         return (0);
 2147 }
 2148 
 2149 int
 2150 g_raid_destroy_disk(struct g_raid_disk *disk)
 2151 {
 2152         struct g_raid_softc *sc;
 2153         struct g_raid_subdisk *sd, *tmp;
 2154 
 2155         sc = disk->d_softc;
 2156         G_RAID_DEBUG1(2, sc, "Destroying disk.");
 2157         if (disk->d_consumer) {
 2158                 g_raid_kill_consumer(sc, disk->d_consumer);
 2159                 disk->d_consumer = NULL;
 2160         }
 2161         TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
 2162                 g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
 2163                 g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
 2164                     G_RAID_EVENT_SUBDISK);
 2165                 TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
 2166                 sd->sd_disk = NULL;
 2167         }
 2168         TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
 2169         if (sc->sc_md)
 2170                 G_RAID_MD_FREE_DISK(sc->sc_md, disk);
 2171         g_raid_event_cancel(sc, disk);
 2172         free(disk, M_RAID);
 2173         return (0);
 2174 }
 2175 
 2176 int
 2177 g_raid_destroy(struct g_raid_softc *sc, int how)
 2178 {
 2179         int error, opens;
 2180 
 2181         g_topology_assert_not();
 2182         if (sc == NULL)
 2183                 return (ENXIO);
 2184         sx_assert(&sc->sc_lock, SX_XLOCKED);
 2185 
 2186         /* Count open volumes. */
 2187         opens = g_raid_nopens(sc);
 2188 
 2189         /* React on some opened volumes. */
 2190         if (opens > 0) {
 2191                 switch (how) {
 2192                 case G_RAID_DESTROY_SOFT:
 2193                         G_RAID_DEBUG1(1, sc,
 2194                             "%d volumes are still open.",
 2195                             opens);
 2196                         sx_xunlock(&sc->sc_lock);
 2197                         return (EBUSY);
 2198                 case G_RAID_DESTROY_DELAYED:
 2199                         G_RAID_DEBUG1(1, sc,
 2200                             "Array will be destroyed on last close.");
 2201                         sc->sc_stopping = G_RAID_DESTROY_DELAYED;
 2202                         sx_xunlock(&sc->sc_lock);
 2203                         return (EBUSY);
 2204                 case G_RAID_DESTROY_HARD:
 2205                         G_RAID_DEBUG1(1, sc,
 2206                             "%d volumes are still open.",
 2207                             opens);
 2208                 }
 2209         }
 2210 
 2211         /* Mark node for destruction. */
 2212         sc->sc_stopping = G_RAID_DESTROY_HARD;
 2213         /* Wake up worker to let it selfdestruct. */
 2214         g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
 2215         /* Sleep until node destroyed. */
 2216         error = sx_sleep(&sc->sc_stopping, &sc->sc_lock,
 2217             PRIBIO | PDROP, "r:destroy", hz * 3);
 2218         return (error == EWOULDBLOCK ? EBUSY : 0);
 2219 }
 2220 
 2221 static void
 2222 g_raid_taste_orphan(struct g_consumer *cp)
 2223 {
 2224 
 2225         KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
 2226             cp->provider->name));
 2227 }
 2228 
 2229 static struct g_geom *
 2230 g_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
 2231 {
 2232         struct g_consumer *cp;
 2233         struct g_geom *gp, *geom;
 2234         struct g_raid_md_class *class;
 2235         struct g_raid_md_object *obj;
 2236         int status;
 2237 
 2238         g_topology_assert();
 2239         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
 2240         if (!g_raid_enable)
 2241                 return (NULL);
 2242         G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
 2243 
 2244         gp = g_new_geomf(mp, "raid:taste");
 2245         /*
 2246          * This orphan function should be never called.
 2247          */
 2248         gp->orphan = g_raid_taste_orphan;
 2249         cp = g_new_consumer(gp);
 2250         g_attach(cp, pp);
 2251 
 2252         geom = NULL;
 2253         LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
 2254                 if (!class->mdc_enable)
 2255                         continue;
 2256                 G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.",
 2257                     pp->name, class->name);
 2258                 obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
 2259                     M_WAITOK);
 2260                 obj->mdo_class = class;
 2261                 status = G_RAID_MD_TASTE(obj, mp, cp, &geom);
 2262                 if (status != G_RAID_MD_TASTE_NEW)
 2263                         kobj_delete((kobj_t)obj, M_RAID);
 2264                 if (status != G_RAID_MD_TASTE_FAIL)
 2265                         break;
 2266         }
 2267 
 2268         g_detach(cp);
 2269         g_destroy_consumer(cp);
 2270         g_destroy_geom(gp);
 2271         G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name);
 2272         return (geom);
 2273 }
 2274 
 2275 int
 2276 g_raid_create_node_format(const char *format, struct gctl_req *req,
 2277     struct g_geom **gp)
 2278 {
 2279         struct g_raid_md_class *class;
 2280         struct g_raid_md_object *obj;
 2281         int status;
 2282 
 2283         G_RAID_DEBUG(2, "Creating array for %s metadata.", format);
 2284         LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
 2285                 if (strcasecmp(class->name, format) == 0)
 2286                         break;
 2287         }
 2288         if (class == NULL) {
 2289                 G_RAID_DEBUG(1, "No support for %s metadata.", format);
 2290                 return (G_RAID_MD_TASTE_FAIL);
 2291         }
 2292         obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
 2293             M_WAITOK);
 2294         obj->mdo_class = class;
 2295         status = G_RAID_MD_CREATE_REQ(obj, &g_raid_class, req, gp);
 2296         if (status != G_RAID_MD_TASTE_NEW)
 2297                 kobj_delete((kobj_t)obj, M_RAID);
 2298         return (status);
 2299 }
 2300 
 2301 static int
 2302 g_raid_destroy_geom(struct gctl_req *req __unused,
 2303     struct g_class *mp __unused, struct g_geom *gp)
 2304 {
 2305         struct g_raid_softc *sc;
 2306         int error;
 2307 
 2308         g_topology_unlock();
 2309         sc = gp->softc;
 2310         sx_xlock(&sc->sc_lock);
 2311         g_cancel_event(sc);
 2312         error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT);
 2313         g_topology_lock();
 2314         return (error);
 2315 }
 2316 
 2317 void g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol,
 2318     struct g_raid_subdisk *sd, struct g_raid_disk *disk)
 2319 {
 2320 
 2321         if (sc->sc_stopping == G_RAID_DESTROY_HARD)
 2322                 return;
 2323         if (sc->sc_md)
 2324                 G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk);
 2325 }
 2326 
 2327 void g_raid_fail_disk(struct g_raid_softc *sc,
 2328     struct g_raid_subdisk *sd, struct g_raid_disk *disk)
 2329 {
 2330 
 2331         if (disk == NULL)
 2332                 disk = sd->sd_disk;
 2333         if (disk == NULL) {
 2334                 G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!");
 2335                 return;
 2336         }
 2337         if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
 2338                 G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a "
 2339                     "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
 2340                 return;
 2341         }
 2342         if (sc->sc_md)
 2343                 G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk);
 2344 }
 2345 
 2346 static void
 2347 g_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
 2348     struct g_consumer *cp, struct g_provider *pp)
 2349 {
 2350         struct g_raid_softc *sc;
 2351         struct g_raid_volume *vol;
 2352         struct g_raid_subdisk *sd;
 2353         struct g_raid_disk *disk;
 2354         int i, s;
 2355 
 2356         g_topology_assert();
 2357 
 2358         sc = gp->softc;
 2359         if (sc == NULL)
 2360                 return;
 2361         if (pp != NULL) {
 2362                 vol = pp->private;
 2363                 g_topology_unlock();
 2364                 sx_xlock(&sc->sc_lock);
 2365                 sbuf_printf(sb, "%s<descr>%s %s volume</descr>\n", indent,
 2366                     sc->sc_md->mdo_class->name,
 2367                     g_raid_volume_level2str(vol->v_raid_level,
 2368                     vol->v_raid_level_qualifier));
 2369                 sbuf_printf(sb, "%s<Label>%s</Label>\n", indent,
 2370                     vol->v_name);
 2371                 sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent,
 2372                     g_raid_volume_level2str(vol->v_raid_level,
 2373                     vol->v_raid_level_qualifier));
 2374                 sbuf_printf(sb,
 2375                     "%s<Transformation>%s</Transformation>\n", indent,
 2376                     vol->v_tr ? vol->v_tr->tro_class->name : "NONE");
 2377                 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
 2378                     vol->v_disks_count);
 2379                 sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent,
 2380                     vol->v_strip_size);
 2381                 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
 2382                     g_raid_volume_state2str(vol->v_state));
 2383                 sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent,
 2384                     vol->v_dirty ? "Yes" : "No");
 2385                 sbuf_printf(sb, "%s<Subdisks>", indent);
 2386                 for (i = 0; i < vol->v_disks_count; i++) {
 2387                         sd = &vol->v_subdisks[i];
 2388                         if (sd->sd_disk != NULL &&
 2389                             sd->sd_disk->d_consumer != NULL) {
 2390                                 sbuf_printf(sb, "%s ",
 2391                                     g_raid_get_diskname(sd->sd_disk));
 2392                         } else {
 2393                                 sbuf_printf(sb, "NONE ");
 2394                         }
 2395                         sbuf_printf(sb, "(%s",
 2396                             g_raid_subdisk_state2str(sd->sd_state));
 2397                         if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
 2398                             sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
 2399                                 sbuf_printf(sb, " %d%%",
 2400                                     (int)(sd->sd_rebuild_pos * 100 /
 2401                                      sd->sd_size));
 2402                         }
 2403                         sbuf_printf(sb, ")");
 2404                         if (i + 1 < vol->v_disks_count)
 2405                                 sbuf_printf(sb, ", ");
 2406                 }
 2407                 sbuf_printf(sb, "</Subdisks>\n");
 2408                 sx_xunlock(&sc->sc_lock);
 2409                 g_topology_lock();
 2410         } else if (cp != NULL) {
 2411                 disk = cp->private;
 2412                 if (disk == NULL)
 2413                         return;
 2414                 g_topology_unlock();
 2415                 sx_xlock(&sc->sc_lock);
 2416                 sbuf_printf(sb, "%s<State>%s", indent,
 2417                     g_raid_disk_state2str(disk->d_state));
 2418                 if (!TAILQ_EMPTY(&disk->d_subdisks)) {
 2419                         sbuf_printf(sb, " (");
 2420                         TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
 2421                                 sbuf_printf(sb, "%s",
 2422                                     g_raid_subdisk_state2str(sd->sd_state));
 2423                                 if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
 2424                                     sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
 2425                                         sbuf_printf(sb, " %d%%",
 2426                                             (int)(sd->sd_rebuild_pos * 100 /
 2427                                              sd->sd_size));
 2428                                 }
 2429                                 if (TAILQ_NEXT(sd, sd_next))
 2430                                         sbuf_printf(sb, ", ");
 2431                         }
 2432                         sbuf_printf(sb, ")");
 2433                 }
 2434                 sbuf_printf(sb, "</State>\n");
 2435                 sbuf_printf(sb, "%s<Subdisks>", indent);
 2436                 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
 2437                         sbuf_printf(sb, "r%d(%s):%d@%ju",
 2438                             sd->sd_volume->v_global_id,
 2439                             sd->sd_volume->v_name,
 2440                             sd->sd_pos, sd->sd_offset);
 2441                         if (TAILQ_NEXT(sd, sd_next))
 2442                                 sbuf_printf(sb, ", ");
 2443                 }
 2444                 sbuf_printf(sb, "</Subdisks>\n");
 2445                 sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent,
 2446                     disk->d_read_errs);
 2447                 sx_xunlock(&sc->sc_lock);
 2448                 g_topology_lock();
 2449         } else {
 2450                 g_topology_unlock();
 2451                 sx_xlock(&sc->sc_lock);
 2452                 if (sc->sc_md) {
 2453                         sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent,
 2454                             sc->sc_md->mdo_class->name);
 2455                 }
 2456                 if (!TAILQ_EMPTY(&sc->sc_volumes)) {
 2457                         s = 0xff;
 2458                         TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
 2459                                 if (vol->v_state < s)
 2460                                         s = vol->v_state;
 2461                         }
 2462                         sbuf_printf(sb, "%s<State>%s</State>\n", indent,
 2463                             g_raid_volume_state2str(s));
 2464                 }
 2465                 sx_xunlock(&sc->sc_lock);
 2466                 g_topology_lock();
 2467         }
 2468 }
 2469 
 2470 static void
 2471 g_raid_shutdown_post_sync(void *arg, int howto)
 2472 {
 2473         struct g_class *mp;
 2474         struct g_geom *gp, *gp2;
 2475         struct g_raid_softc *sc;
 2476         struct g_raid_volume *vol;
 2477 
 2478         mp = arg;
 2479         DROP_GIANT();
 2480         g_topology_lock();
 2481         g_raid_shutdown = 1;
 2482         LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
 2483                 if ((sc = gp->softc) == NULL)
 2484                         continue;
 2485                 g_topology_unlock();
 2486                 sx_xlock(&sc->sc_lock);
 2487                 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next)
 2488                         g_raid_clean(vol, -1);
 2489                 g_cancel_event(sc);
 2490                 g_raid_destroy(sc, G_RAID_DESTROY_DELAYED);
 2491                 g_topology_lock();
 2492         }
 2493         g_topology_unlock();
 2494         PICKUP_GIANT();
 2495 }
 2496 
 2497 static void
 2498 g_raid_init(struct g_class *mp)
 2499 {
 2500 
 2501         g_raid_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
 2502             g_raid_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
 2503         if (g_raid_post_sync == NULL)
 2504                 G_RAID_DEBUG(0, "Warning! Cannot register shutdown event.");
 2505         g_raid_started = 1;
 2506 }
 2507 
 2508 static void
 2509 g_raid_fini(struct g_class *mp)
 2510 {
 2511 
 2512         if (g_raid_post_sync != NULL)
 2513                 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_raid_post_sync);
 2514         g_raid_started = 0;
 2515 }
 2516 
 2517 int
 2518 g_raid_md_modevent(module_t mod, int type, void *arg)
 2519 {
 2520         struct g_raid_md_class *class, *c, *nc;
 2521         int error;
 2522 
 2523         error = 0;
 2524         class = arg;
 2525         switch (type) {
 2526         case MOD_LOAD:
 2527                 c = LIST_FIRST(&g_raid_md_classes);
 2528                 if (c == NULL || c->mdc_priority > class->mdc_priority)
 2529                         LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list);
 2530                 else {
 2531                         while ((nc = LIST_NEXT(c, mdc_list)) != NULL &&
 2532                             nc->mdc_priority < class->mdc_priority)
 2533                                 c = nc;
 2534                         LIST_INSERT_AFTER(c, class, mdc_list);
 2535                 }
 2536                 if (g_raid_started)
 2537                         g_retaste(&g_raid_class);
 2538                 break;
 2539         case MOD_UNLOAD:
 2540                 LIST_REMOVE(class, mdc_list);
 2541                 break;
 2542         default:
 2543                 error = EOPNOTSUPP;
 2544                 break;
 2545         }
 2546 
 2547         return (error);
 2548 }
 2549 
 2550 int
 2551 g_raid_tr_modevent(module_t mod, int type, void *arg)
 2552 {
 2553         struct g_raid_tr_class *class, *c, *nc;
 2554         int error;
 2555 
 2556         error = 0;
 2557         class = arg;
 2558         switch (type) {
 2559         case MOD_LOAD:
 2560                 c = LIST_FIRST(&g_raid_tr_classes);
 2561                 if (c == NULL || c->trc_priority > class->trc_priority)
 2562                         LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list);
 2563                 else {
 2564                         while ((nc = LIST_NEXT(c, trc_list)) != NULL &&
 2565                             nc->trc_priority < class->trc_priority)
 2566                                 c = nc;
 2567                         LIST_INSERT_AFTER(c, class, trc_list);
 2568                 }
 2569                 break;
 2570         case MOD_UNLOAD:
 2571                 LIST_REMOVE(class, trc_list);
 2572                 break;
 2573         default:
 2574                 error = EOPNOTSUPP;
 2575                 break;
 2576         }
 2577 
 2578         return (error);
 2579 }
 2580 
 2581 /*
 2582  * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid)
 2583  * to reduce module priority, allowing submodules to register them first.
 2584  */
 2585 static moduledata_t g_raid_mod = {
 2586         "g_raid",
 2587         g_modevent,
 2588         &g_raid_class
 2589 };
 2590 DECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
 2591 MODULE_VERSION(geom_raid, 0);

Cache object: a2679a3f7324bd5fc4a79ca54287e74a


[ 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.