]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/geom/virstor/g_virstor.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / geom / virstor / g_virstor.c
1 /*-
2  * Copyright (c) 2006-2007 Ivan Voras <ivoras@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 /* Implementation notes:
28  * - "Components" are wrappers around providers that make up the
29  *   virtual storage (i.e. a virstor has "physical" components)
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/sx.h>
42 #include <sys/bio.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
45 #include <sys/time.h>
46 #include <sys/proc.h>
47 #include <sys/kthread.h>
48 #include <sys/mutex.h>
49 #include <vm/uma.h>
50 #include <geom/geom.h>
51
52 #include <geom/virstor/g_virstor.h>
53 #include <geom/virstor/g_virstor_md.h>
54
55 /* Declare malloc(9) label */
56 static MALLOC_DEFINE(M_GVIRSTOR, "gvirstor", "GEOM_VIRSTOR Data");
57
58 /* GEOM class methods */
59 static g_init_t g_virstor_init;
60 static g_fini_t g_virstor_fini;
61 static g_taste_t g_virstor_taste;
62 static g_ctl_req_t g_virstor_config;
63 static g_ctl_destroy_geom_t g_virstor_destroy_geom;
64
65 /* Declare & initialize class structure ("geom class") */
66 struct g_class g_virstor_class = {
67         .name =         G_VIRSTOR_CLASS_NAME,
68         .version =      G_VERSION,
69         .init =         g_virstor_init,
70         .fini =         g_virstor_fini,
71         .taste =        g_virstor_taste,
72         .ctlreq =       g_virstor_config,
73         .destroy_geom = g_virstor_destroy_geom
74         /* The .dumpconf and the rest are only usable for a geom instance, so
75          * they will be set when such instance is created. */
76 };
77
78 /* Declare sysctl's and loader tunables */
79 SYSCTL_DECL(_kern_geom);
80 SYSCTL_NODE(_kern_geom, OID_AUTO, virstor, CTLFLAG_RW, 0, "GEOM_GVIRSTOR information");
81
82 static u_int g_virstor_debug = 2; /* XXX: lower to 2 when released to public */
83 TUNABLE_INT("kern.geom.virstor.debug", &g_virstor_debug);
84 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, debug, CTLFLAG_RW, &g_virstor_debug,
85     0, "Debug level (2=production, 5=normal, 15=excessive)");
86
87 static u_int g_virstor_chunk_watermark = 100;
88 TUNABLE_INT("kern.geom.virstor.chunk_watermark", &g_virstor_chunk_watermark);
89 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, chunk_watermark, CTLFLAG_RW,
90     &g_virstor_chunk_watermark, 0,
91     "Minimum number of free chunks before issuing administrative warning");
92
93 static u_int g_virstor_component_watermark = 1;
94 TUNABLE_INT("kern.geom.virstor.component_watermark",
95     &g_virstor_component_watermark);
96 SYSCTL_UINT(_kern_geom_virstor, OID_AUTO, component_watermark, CTLFLAG_RW,
97     &g_virstor_component_watermark, 0,
98     "Minimum number of free components before issuing administrative warning");
99
100 static int read_metadata(struct g_consumer *, struct g_virstor_metadata *);
101 static int write_metadata(struct g_consumer *, struct g_virstor_metadata *);
102 static int clear_metadata(struct g_virstor_component *);
103 static int add_provider_to_geom(struct g_virstor_softc *, struct g_provider *,
104     struct g_virstor_metadata *);
105 static struct g_geom *create_virstor_geom(struct g_class *,
106     struct g_virstor_metadata *);
107 static void virstor_check_and_run(struct g_virstor_softc *);
108 static u_int virstor_valid_components(struct g_virstor_softc *);
109 static int virstor_geom_destroy(struct g_virstor_softc *, boolean_t,
110     boolean_t);
111 static void remove_component(struct g_virstor_softc *,
112     struct g_virstor_component *, boolean_t);
113 static void bioq_dismantle(struct bio_queue_head *);
114 static int allocate_chunk(struct g_virstor_softc *,
115     struct g_virstor_component **, u_int *, u_int *);
116 static void delay_destroy_consumer(void *, int);
117 static void dump_component(struct g_virstor_component *comp);
118 #if 0
119 static void dump_me(struct virstor_map_entry *me, unsigned int nr);
120 #endif
121
122 static void virstor_ctl_stop(struct gctl_req *, struct g_class *);
123 static void virstor_ctl_add(struct gctl_req *, struct g_class *);
124 static void virstor_ctl_remove(struct gctl_req *, struct g_class *);
125 static struct g_virstor_softc * virstor_find_geom(const struct g_class *,
126     const char *);
127 static void update_metadata(struct g_virstor_softc *);
128 static void fill_metadata(struct g_virstor_softc *, struct g_virstor_metadata *,
129     u_int, u_int);
130
131 static void g_virstor_orphan(struct g_consumer *);
132 static int g_virstor_access(struct g_provider *, int, int, int);
133 static void g_virstor_start(struct bio *);
134 static void g_virstor_dumpconf(struct sbuf *, const char *, struct g_geom *,
135     struct g_consumer *, struct g_provider *);
136 static void g_virstor_done(struct bio *);
137
138 static void invalid_call(void);
139 /*
140  * Initialise GEOM class (per-class callback)
141  */
142 static void
143 g_virstor_init(struct g_class *mp __unused)
144 {
145
146         /* Catch map struct size mismatch at compile time; Map entries must
147          * fit into MAXPHYS exactly, with no wasted space. */
148         CTASSERT(VIRSTOR_MAP_BLOCK_ENTRIES*VIRSTOR_MAP_ENTRY_SIZE == MAXPHYS);
149
150         /* Init UMA zones, TAILQ's, other global vars */
151 }
152
153 /*
154  * Finalise GEOM class (per-class callback)
155  */
156 static void
157 g_virstor_fini(struct g_class *mp __unused)
158 {
159
160         /* Deinit UMA zones & global vars */
161 }
162
163 /*
164  * Config (per-class callback)
165  */
166 static void
167 g_virstor_config(struct gctl_req *req, struct g_class *cp, char const *verb)
168 {
169         uint32_t *version;
170
171         g_topology_assert();
172
173         version = gctl_get_paraml(req, "version", sizeof(*version));
174         if (version == NULL) {
175                 gctl_error(req, "Failed to get 'version' argument");
176                 return;
177         }
178         if (*version != G_VIRSTOR_VERSION) {
179                 gctl_error(req, "Userland and kernel versions out of sync");
180                 return;
181         }
182
183         g_topology_unlock();
184         if (strcmp(verb, "add") == 0)
185                 virstor_ctl_add(req, cp);
186         else if (strcmp(verb, "stop") == 0 || strcmp(verb, "destroy") == 0)
187                 virstor_ctl_stop(req, cp);
188         else if (strcmp(verb, "remove") == 0)
189                 virstor_ctl_remove(req, cp);
190         else
191                 gctl_error(req, "unknown verb: '%s'", verb);
192         g_topology_lock();
193 }
194
195 /*
196  * "stop" verb from userland
197  */
198 static void
199 virstor_ctl_stop(struct gctl_req *req, struct g_class *cp)
200 {
201         int *force, *nargs;
202         int i;
203
204         nargs = gctl_get_paraml(req, "nargs", sizeof *nargs);
205         if (nargs == NULL) {
206                 gctl_error(req, "Error fetching argument '%s'", "nargs");
207                 return;
208         }
209         if (*nargs < 1) {
210                 gctl_error(req, "Invalid number of arguments");
211                 return;
212         }
213         force = gctl_get_paraml(req, "force", sizeof *force);
214         if (force == NULL) {
215                 gctl_error(req, "Error fetching argument '%s'", "force");
216                 return;
217         }
218
219         g_topology_lock();
220         for (i = 0; i < *nargs; i++) {
221                 char param[8];
222                 const char *name;
223                 struct g_virstor_softc *sc;
224                 int error;
225
226                 sprintf(param, "arg%d", i);
227                 name = gctl_get_asciiparam(req, param);
228                 if (name == NULL) {
229                         gctl_error(req, "No 'arg%d' argument", i);
230                         g_topology_unlock();
231                         return;
232                 }
233                 sc = virstor_find_geom(cp, name);
234                 LOG_MSG(LVL_INFO, "Stopping %s by the userland command",
235                     sc->geom->name);
236                 update_metadata(sc);
237                 if ((error = virstor_geom_destroy(sc, TRUE, TRUE)) != 0) {
238                         LOG_MSG(LVL_ERROR, "Cannot destroy %s: %d",
239                             sc->geom->name, error);
240                 }
241         }
242         g_topology_unlock();
243 }
244
245 /*
246  * "add" verb from userland - add new component(s) to the structure.
247  * This will be done all at once in here, without going through the
248  * .taste function for new components.
249  */
250 static void
251 virstor_ctl_add(struct gctl_req *req, struct g_class *cp)
252 {
253         /* Note: while this is going on, I/O is being done on
254          * the g_up and g_down threads. The idea is to make changes
255          * to softc members in a way that can atomically activate
256          * them all at once. */
257         struct g_virstor_softc *sc;
258         int *hardcode, *nargs;
259         const char *geom_name;  /* geom to add a component to */
260         struct g_consumer *fcp;
261         struct g_virstor_bio_q *bq;
262         u_int added;
263         int error;
264         int i;
265
266         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
267         if (nargs == NULL) {
268                 gctl_error(req, "Error fetching argument '%s'", "nargs");
269                 return;
270         }
271         if (*nargs < 2) {
272                 gctl_error(req, "Invalid number of arguments");
273                 return;
274         }
275         hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
276         if (hardcode == NULL) {
277                 gctl_error(req, "Error fetching argument '%s'", "hardcode");
278                 return;
279         }
280
281         /* Find "our" geom */
282         geom_name = gctl_get_asciiparam(req, "arg0");
283         if (geom_name == NULL) {
284                 gctl_error(req, "Error fetching argument '%s'", "geom_name (arg0)");
285                 return;
286         }
287         sc = virstor_find_geom(cp, geom_name);
288         if (sc == NULL) {
289                 gctl_error(req, "Don't know anything about '%s'", geom_name);
290                 return;
291         }
292
293         if (virstor_valid_components(sc) != sc->n_components) {
294                 LOG_MSG(LVL_ERROR, "Cannot add components to incomplete "
295                     "virstor %s", sc->geom->name);
296                 gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
297                 return;
298         }
299
300         fcp = sc->components[0].gcons;
301         added = 0;
302         g_topology_lock();
303         for (i = 1; i < *nargs; i++) {
304                 struct g_virstor_metadata md;
305                 char aname[8];
306                 const char *prov_name;
307                 struct g_provider *pp;
308                 struct g_consumer *cp;
309                 u_int nc;
310                 u_int j;
311
312                 snprintf(aname, sizeof aname, "arg%d", i);
313                 prov_name = gctl_get_asciiparam(req, aname);
314                 if (prov_name == NULL) {
315                         gctl_error(req, "Error fetching argument '%s'", aname);
316                         g_topology_unlock();
317                         return;
318                 }
319                 if (strncmp(prov_name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
320                         prov_name += strlen(_PATH_DEV);
321
322                 pp = g_provider_by_name(prov_name);
323                 if (pp == NULL) {
324                         /* This is the most common error so be verbose about it */
325                         if (added != 0) {
326                                 gctl_error(req, "Invalid provider: '%s' (added"
327                                     " %u components)", prov_name, added);
328                                 update_metadata(sc);
329                         } else {
330                                 gctl_error(req, "Invalid provider: '%s'",
331                                     prov_name);
332                         }
333                         g_topology_unlock();
334                         return;
335                 }
336                 cp = g_new_consumer(sc->geom);
337                 if (cp == NULL) {
338                         gctl_error(req, "Cannot create consumer");
339                         g_topology_unlock();
340                         return;
341                 }
342                 error = g_attach(cp, pp);
343                 if (error != 0) {
344                         gctl_error(req, "Cannot attach a consumer to %s",
345                             pp->name);
346                         g_destroy_consumer(cp);
347                         g_topology_unlock();
348                         return;
349                 }
350                 if (fcp->acr != 0 || fcp->acw != 0 || fcp->ace != 0) {
351                         error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
352                         if (error != 0) {
353                                 gctl_error(req, "Access request failed for %s",
354                                     pp->name);
355                                 g_destroy_consumer(cp);
356                                 g_topology_unlock();
357                                 return;
358                         }
359                 }
360                 if (fcp->provider->sectorsize != pp->sectorsize) {
361                         gctl_error(req, "Sector size doesn't fit for %s",
362                             pp->name);
363                         g_destroy_consumer(cp);
364                         g_topology_unlock();
365                         return;
366                 }
367                 for (j = 0; j < sc->n_components; j++) {
368                         if (strcmp(sc->components[j].gcons->provider->name,
369                             pp->name) == 0) {
370                                 gctl_error(req, "Component %s already in %s",
371                                     pp->name, sc->geom->name);
372                                 g_destroy_consumer(cp);
373                                 g_topology_unlock();
374                                 return;
375                         }
376                 }
377                 sc->components = realloc(sc->components,
378                     sizeof(*sc->components) * (sc->n_components + 1),
379                     M_GVIRSTOR, M_WAITOK);
380
381                 nc = sc->n_components;
382                 sc->components[nc].gcons = cp;
383                 sc->components[nc].sc = sc;
384                 sc->components[nc].index = nc;
385                 sc->components[nc].chunk_count = cp->provider->mediasize /
386                     sc->chunk_size;
387                 sc->components[nc].chunk_next = 0;
388                 sc->components[nc].chunk_reserved = 0;
389
390                 if (sc->components[nc].chunk_count < 4) {
391                         gctl_error(req, "Provider too small: %s",
392                             cp->provider->name);
393                         g_destroy_consumer(cp);
394                         g_topology_unlock();
395                         return;
396                 }
397                 fill_metadata(sc, &md, nc, *hardcode);
398                 write_metadata(cp, &md);
399                 /* The new component becomes visible when n_components is
400                  * incremented */
401                 sc->n_components++;
402                 added++;
403
404         }
405         /* This call to update_metadata() is critical. In case there's a
406          * power failure in the middle of it and some components are updated
407          * while others are not, there will be trouble on next .taste() iff
408          * a non-updated component is detected first */
409         update_metadata(sc);
410         g_topology_unlock();
411         LOG_MSG(LVL_INFO, "Added %d component(s) to %s", added,
412             sc->geom->name);
413         /* Fire off BIOs previously queued because there wasn't any
414          * physical space left. If the BIOs still can't be satisfied
415          * they will again be added to the end of the queue (during
416          * which the mutex will be recursed) */
417         bq = malloc(sizeof(*bq), M_GVIRSTOR, M_WAITOK);
418         bq->bio = NULL;
419         mtx_lock(&sc->delayed_bio_q_mtx);
420         /* First, insert a sentinel to the queue end, so we don't
421          * end up in an infinite loop if there's still no free
422          * space available. */
423         STAILQ_INSERT_TAIL(&sc->delayed_bio_q, bq, linkage);
424         while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
425                 bq = STAILQ_FIRST(&sc->delayed_bio_q);
426                 if (bq->bio != NULL) {
427                         g_virstor_start(bq->bio);
428                         STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
429                         free(bq, M_GVIRSTOR);
430                 } else {
431                         STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
432                         free(bq, M_GVIRSTOR);
433                         break;
434                 }
435         }
436         mtx_unlock(&sc->delayed_bio_q_mtx);
437
438 }
439
440 /*
441  * Find a geom handled by the class
442  */
443 static struct g_virstor_softc *
444 virstor_find_geom(const struct g_class *cp, const char *name)
445 {
446         struct g_geom *gp;
447
448         LIST_FOREACH(gp, &cp->geom, geom) {
449                 if (strcmp(name, gp->name) == 0)
450                         return (gp->softc);
451         }
452         return (NULL);
453 }
454
455 /*
456  * Update metadata on all components to reflect the current state
457  * of these fields:
458  *    - chunk_next
459  *    - flags
460  *    - md_count
461  * Expects things to be set up so write_metadata() can work, i.e.
462  * the topology lock must be held.
463  */
464 static void
465 update_metadata(struct g_virstor_softc *sc)
466 {
467         struct g_virstor_metadata md;
468         int n;
469
470         if (virstor_valid_components(sc) != sc->n_components)
471                 return; /* Incomplete device */
472         LOG_MSG(LVL_DEBUG, "Updating metadata on components for %s",
473             sc->geom->name);
474         /* Update metadata on components */
475         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__,
476             sc->geom->class->name, sc->geom->name);
477         g_topology_assert();
478         for (n = 0; n < sc->n_components; n++) {
479                 read_metadata(sc->components[n].gcons, &md);
480                 md.chunk_next = sc->components[n].chunk_next;
481                 md.flags = sc->components[n].flags;
482                 md.md_count = sc->n_components;
483                 write_metadata(sc->components[n].gcons, &md);
484         }
485 }
486
487 /*
488  * Fills metadata (struct md) from information stored in softc and the nc'th
489  * component of virstor
490  */
491 static void
492 fill_metadata(struct g_virstor_softc *sc, struct g_virstor_metadata *md,
493     u_int nc, u_int hardcode)
494 {
495         struct g_virstor_component *c;
496
497         bzero(md, sizeof *md);
498         c = &sc->components[nc];
499
500         strncpy(md->md_magic, G_VIRSTOR_MAGIC, sizeof md->md_magic);
501         md->md_version = G_VIRSTOR_VERSION;
502         strncpy(md->md_name, sc->geom->name, sizeof md->md_name);
503         md->md_id = sc->id;
504         md->md_virsize = sc->virsize;
505         md->md_chunk_size = sc->chunk_size;
506         md->md_count = sc->n_components;
507
508         if (hardcode) {
509                 strncpy(md->provider, c->gcons->provider->name,
510                     sizeof md->provider);
511         }
512         md->no = nc;
513         md->provsize = c->gcons->provider->mediasize;
514         md->chunk_count = c->chunk_count;
515         md->chunk_next = c->chunk_next;
516         md->chunk_reserved = c->chunk_reserved;
517         md->flags = c->flags;
518 }
519
520 /*
521  * Remove a component from virstor device.
522  * Can only be done if the component is unallocated.
523  */
524 static void
525 virstor_ctl_remove(struct gctl_req *req, struct g_class *cp)
526 {
527         /* As this is executed in parallel to I/O, operations on virstor
528          * structures must be as atomic as possible. */
529         struct g_virstor_softc *sc;
530         int *nargs;
531         const char *geom_name;
532         u_int removed;
533         int i;
534
535         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
536         if (nargs == NULL) {
537                 gctl_error(req, "Error fetching argument '%s'", "nargs");
538                 return;
539         }
540         if (*nargs < 2) {
541                 gctl_error(req, "Invalid number of arguments");
542                 return;
543         }
544         /* Find "our" geom */
545         geom_name = gctl_get_asciiparam(req, "arg0");
546         if (geom_name == NULL) {
547                 gctl_error(req, "Error fetching argument '%s'",
548                     "geom_name (arg0)");
549                 return;
550         }
551         sc = virstor_find_geom(cp, geom_name);
552         if (sc == NULL) {
553                 gctl_error(req, "Don't know anything about '%s'", geom_name);
554                 return;
555         }
556
557         if (virstor_valid_components(sc) != sc->n_components) {
558                 LOG_MSG(LVL_ERROR, "Cannot remove components from incomplete "
559                     "virstor %s", sc->geom->name);
560                 gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
561                 return;
562         }
563
564         removed = 0;
565         for (i = 1; i < *nargs; i++) {
566                 char param[8];
567                 const char *prov_name;
568                 int j, found;
569                 struct g_virstor_component *newcomp, *compbak;
570
571                 sprintf(param, "arg%d", i);
572                 prov_name = gctl_get_asciiparam(req, param);
573                 if (prov_name == NULL) {
574                         gctl_error(req, "Error fetching argument '%s'", param);
575                         return;
576                 }
577                 if (strncmp(prov_name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
578                         prov_name += strlen(_PATH_DEV);
579
580                 found = -1;
581                 for (j = 0; j < sc->n_components; j++) {
582                         if (strcmp(sc->components[j].gcons->provider->name,
583                             prov_name) == 0) {
584                                 found = j;
585                                 break;
586                         }
587                 }
588                 if (found == -1) {
589                         LOG_MSG(LVL_ERROR, "No %s component in %s",
590                             prov_name, sc->geom->name);
591                         continue;
592                 }
593
594                 compbak = sc->components;
595                 newcomp = malloc(sc->n_components * sizeof(*sc->components),
596                     M_GVIRSTOR, M_WAITOK | M_ZERO);
597                 bcopy(sc->components, newcomp, found * sizeof(*sc->components));
598                 bcopy(&sc->components[found + 1], newcomp + found,
599                     found * sizeof(*sc->components));
600                 if ((sc->components[j].flags & VIRSTOR_PROVIDER_ALLOCATED) != 0) {
601                         LOG_MSG(LVL_ERROR, "Allocated provider %s cannot be "
602                             "removed from %s",
603                             prov_name, sc->geom->name);
604                         free(newcomp, M_GVIRSTOR);
605                         /* We'll consider this non-fatal error */
606                         continue;
607                 }
608                 /* Renumerate unallocated components */
609                 for (j = 0; j < sc->n_components-1; j++) {
610                         if ((sc->components[j].flags &
611                             VIRSTOR_PROVIDER_ALLOCATED) == 0) {
612                                 sc->components[j].index = j;
613                         }
614                 }
615                 /* This is the critical section. If a component allocation
616                  * event happens while both variables are not yet set,
617                  * there will be trouble. Something will panic on encountering
618                  * NULL sc->components[x].gcomp member.
619                  * Luckily, component allocation happens very rarely and
620                  * removing components is an abnormal action in any case. */
621                 sc->components = newcomp;
622                 sc->n_components--;
623                 /* End critical section */
624
625                 g_topology_lock();
626                 if (clear_metadata(&compbak[found]) != 0) {
627                         LOG_MSG(LVL_WARNING, "Trouble ahead: cannot clear "
628                             "metadata on %s", prov_name);
629                 }
630                 g_detach(compbak[found].gcons);
631                 g_destroy_consumer(compbak[found].gcons);
632                 g_topology_unlock();
633
634                 free(compbak, M_GVIRSTOR);
635
636                 removed++;
637         }
638
639         /* This call to update_metadata() is critical. In case there's a
640          * power failure in the middle of it and some components are updated
641          * while others are not, there will be trouble on next .taste() iff
642          * a non-updated component is detected first */
643         g_topology_lock();
644         update_metadata(sc);
645         g_topology_unlock();
646         LOG_MSG(LVL_INFO, "Removed %d component(s) from %s", removed,
647             sc->geom->name);
648 }
649
650 /*
651  * Clear metadata sector on component
652  */
653 static int
654 clear_metadata(struct g_virstor_component *comp)
655 {
656         char *buf;
657         int error;
658
659         LOG_MSG(LVL_INFO, "Clearing metadata on %s",
660             comp->gcons->provider->name);
661         g_topology_assert();
662         error = g_access(comp->gcons, 0, 1, 0);
663         if (error != 0)
664                 return (error);
665         buf = malloc(comp->gcons->provider->sectorsize, M_GVIRSTOR,
666             M_WAITOK | M_ZERO);
667         error = g_write_data(comp->gcons,
668             comp->gcons->provider->mediasize -
669             comp->gcons->provider->sectorsize,
670             buf,
671             comp->gcons->provider->sectorsize);
672         free(buf, M_GVIRSTOR);
673         g_access(comp->gcons, 0, -1, 0);
674         return (error);
675 }
676
677 /*
678  * Destroy geom forcibly.
679  */
680 static int
681 g_virstor_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
682     struct g_geom *gp)
683 {
684         struct g_virstor_softc *sc;
685         int exitval;
686
687         sc = gp->softc;
688         KASSERT(sc != NULL, ("%s: NULL sc", __func__));
689         
690         exitval = 0;
691         LOG_MSG(LVL_DEBUG, "%s called for %s, sc=%p", __func__, gp->name,
692             gp->softc);
693
694         if (sc != NULL) {
695 #ifdef INVARIANTS
696                 char *buf;
697                 int error;
698                 off_t off;
699                 int isclean, count;
700                 int n;
701
702                 LOG_MSG(LVL_INFO, "INVARIANTS detected");
703                 LOG_MSG(LVL_INFO, "Verifying allocation "
704                     "table for %s", sc->geom->name);
705                 count = 0;
706                 for (n = 0; n < sc->chunk_count; n++) {
707                         if (sc->map[n].flags || VIRSTOR_MAP_ALLOCATED != 0)
708                                 count++;
709                 }
710                 LOG_MSG(LVL_INFO, "Device %s has %d allocated chunks",
711                     sc->geom->name, count);
712                 n = off = count = 0;
713                 isclean = 1;
714                 if (virstor_valid_components(sc) != sc->n_components) {
715                         /* This is a incomplete virstor device (not all
716                          * components have been found) */
717                         LOG_MSG(LVL_ERROR, "Device %s is incomplete",
718                             sc->geom->name);
719                         goto bailout;
720                 }
721                 error = g_access(sc->components[0].gcons, 1, 0, 0);
722                 KASSERT(error == 0, ("%s: g_access failed (%d)", __func__,
723                     error));
724                 /* Compare the whole on-disk allocation table with what's
725                  * currently in memory */
726                 while (n < sc->chunk_count) {
727                         buf = g_read_data(sc->components[0].gcons, off,
728                             sc->sectorsize, &error);
729                         KASSERT(buf != NULL, ("g_read_data returned NULL (%d) "
730                             "for read at %jd", error, off));
731                         if (bcmp(buf, &sc->map[n], sc->sectorsize) != 0) {
732                                 LOG_MSG(LVL_ERROR, "ERROR in allocation table, "
733                                     "entry %d, offset %jd", n, off);
734                                 isclean = 0;
735                                 count++;
736                         }
737                         n += sc->me_per_sector;
738                         off += sc->sectorsize;
739                         g_free(buf);
740                 }
741                 error = g_access(sc->components[0].gcons, -1, 0, 0);
742                 KASSERT(error == 0, ("%s: g_access failed (%d) on exit",
743                     __func__, error));
744                 if (isclean != 1) {
745                         LOG_MSG(LVL_ERROR, "ALLOCATION TABLE CORRUPTED FOR %s "
746                             "(%d sectors don't match, max %zu allocations)",
747                             sc->geom->name, count,
748                             count * sc->me_per_sector);
749                 } else {
750                         LOG_MSG(LVL_INFO, "Allocation table ok for %s",
751                             sc->geom->name);
752                 }
753 bailout:
754 #endif
755                 update_metadata(sc);
756                 virstor_geom_destroy(sc, FALSE, FALSE);
757                 exitval = EAGAIN;
758         } else
759                 exitval = 0;
760         return (exitval);
761 }
762
763 /*
764  * Taste event (per-class callback)
765  * Examines a provider and creates geom instances if needed
766  */
767 static struct g_geom *
768 g_virstor_taste(struct g_class *mp, struct g_provider *pp, int flags)
769 {
770         struct g_virstor_metadata md;
771         struct g_geom *gp;
772         struct g_consumer *cp;
773         struct g_virstor_softc *sc;
774         int error;
775
776         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
777         g_topology_assert();
778         LOG_MSG(LVL_DEBUG, "Tasting %s", pp->name);
779
780         /* We need a dummy geom to attach a consumer to the given provider */
781         gp = g_new_geomf(mp, "virstor:taste.helper");
782         gp->start = (void *)invalid_call;       /* XXX: hacked up so the        */
783         gp->access = (void *)invalid_call;      /* compiler doesn't complain.   */
784         gp->orphan = (void *)invalid_call;      /* I really want these to fail. */
785
786         cp = g_new_consumer(gp);
787         g_attach(cp, pp);
788         error = read_metadata(cp, &md);
789         g_detach(cp);
790         g_destroy_consumer(cp);
791         g_destroy_geom(gp);
792
793         if (error != 0)
794                 return (NULL);
795
796         if (strcmp(md.md_magic, G_VIRSTOR_MAGIC) != 0)
797                 return (NULL);
798         if (md.md_version != G_VIRSTOR_VERSION) {
799                 LOG_MSG(LVL_ERROR, "Kernel module version invalid "
800                     "to handle %s (%s) : %d should be %d",
801                     md.md_name, pp->name, md.md_version, G_VIRSTOR_VERSION);
802                 return (NULL);
803         }
804         if (md.provsize != pp->mediasize)
805                 return (NULL);
806
807         /* If the provider name is hardcoded, use the offered provider only
808          * if it's been offered with its proper name (the one used in
809          * the label command). */
810         if (md.provider[0] != '\0') {
811                 if (strcmp(md.provider, pp->name) != 0)
812                         return (NULL);
813         }
814
815         /* Iterate all geoms this class already knows about to see if a new
816          * geom instance of this class needs to be created (in case the provider
817          * is first from a (possibly) multi-consumer geom) or it just needs
818          * to be added to an existing instance. */
819         sc = NULL;
820         gp = NULL;
821         LIST_FOREACH(gp, &mp->geom, geom) {
822                 sc = gp->softc;
823                 if (sc == NULL)
824                         continue;
825                 if (strcmp(md.md_name, sc->geom->name) != 0)
826                         continue;
827                 if (md.md_id != sc->id)
828                         continue;
829                 break;
830         }
831         if (gp != NULL) { /* We found an existing geom instance; add to it */
832                 LOG_MSG(LVL_INFO, "Adding %s to %s", pp->name, md.md_name);
833                 error = add_provider_to_geom(sc, pp, &md);
834                 if (error != 0) {
835                         LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
836                             pp->name, md.md_name, error);
837                         return (NULL);
838                 }
839         } else { /* New geom instance needs to be created */
840                 gp = create_virstor_geom(mp, &md);
841                 if (gp == NULL) {
842                         LOG_MSG(LVL_ERROR, "Error creating new instance of "
843                             "class %s: %s", mp->name, md.md_name);
844                         LOG_MSG(LVL_DEBUG, "Error creating %s at %s",
845                             md.md_name, pp->name);
846                         return (NULL);
847                 }
848                 sc = gp->softc;
849                 LOG_MSG(LVL_INFO, "Adding %s to %s (first found)", pp->name,
850                     md.md_name);
851                 error = add_provider_to_geom(sc, pp, &md);
852                 if (error != 0) {
853                         LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
854                             pp->name, md.md_name, error);
855                         virstor_geom_destroy(sc, TRUE, FALSE);
856                         return (NULL);
857                 }
858         }
859
860         return (gp);
861 }
862
863 /*
864  * Destroyes consumer passed to it in arguments. Used as a callback
865  * on g_event queue.
866  */
867 static void
868 delay_destroy_consumer(void *arg, int flags __unused)
869 {
870         struct g_consumer *c = arg;
871         KASSERT(c != NULL, ("%s: invalid consumer", __func__));
872         LOG_MSG(LVL_DEBUG, "Consumer %s destroyed with delay",
873             c->provider->name);
874         g_detach(c);
875         g_destroy_consumer(c);
876 }
877
878 /*
879  * Remove a component (consumer) from geom instance; If it's the first
880  * component being removed, orphan the provider to announce geom's being
881  * dismantled
882  */
883 static void
884 remove_component(struct g_virstor_softc *sc, struct g_virstor_component *comp,
885     boolean_t delay)
886 {
887         struct g_consumer *c;
888
889         KASSERT(comp->gcons != NULL, ("Component with no consumer in %s",
890             sc->geom->name));
891         c = comp->gcons;
892
893         comp->gcons = NULL;
894         KASSERT(c->provider != NULL, ("%s: no provider", __func__));
895         LOG_MSG(LVL_DEBUG, "Component %s removed from %s", c->provider->name,
896             sc->geom->name);
897         if (sc->provider != NULL) {
898                 /* Whither, GEOM? */
899                 sc->provider->flags |= G_PF_WITHER;
900                 g_orphan_provider(sc->provider, ENXIO);
901                 sc->provider = NULL;
902                 LOG_MSG(LVL_INFO, "Removing provider %s", sc->geom->name);
903         }
904
905         if (c->acr > 0 || c->acw > 0 || c->ace > 0)
906                 g_access(c, -c->acr, -c->acw, -c->ace);
907         if (delay) {
908                 /* Destroy consumer after it's tasted */
909                 g_post_event(delay_destroy_consumer, c, M_WAITOK, NULL);
910         } else {
911                 g_detach(c);
912                 g_destroy_consumer(c);
913         }
914 }
915
916 /*
917  * Destroy geom - called internally
918  * See g_virstor_destroy_geom for the other one
919  */
920 static int
921 virstor_geom_destroy(struct g_virstor_softc *sc, boolean_t force,
922     boolean_t delay)
923 {
924         struct g_provider *pp;
925         struct g_geom *gp;
926         int n;
927
928         g_topology_assert();
929
930         if (sc == NULL)
931                 return (ENXIO);
932
933         pp = sc->provider;
934         if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
935                 LOG_MSG(force ? LVL_WARNING : LVL_ERROR,
936                     "Device %s is still open.", pp->name);
937                 if (!force)
938                         return (EBUSY);
939         }
940
941         for (n = 0; n < sc->n_components; n++) {
942                 if (sc->components[n].gcons != NULL)
943                         remove_component(sc, &sc->components[n], delay);
944         }
945
946         gp = sc->geom;
947         gp->softc = NULL;
948
949         KASSERT(sc->provider == NULL, ("Provider still exists for %s",
950             gp->name));
951
952         /* XXX: This might or might not work, since we're called with
953          * the topology lock held. Also, it might panic the kernel if
954          * the error'd BIO is in softupdates code. */
955         mtx_lock(&sc->delayed_bio_q_mtx);
956         while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
957                 struct g_virstor_bio_q *bq;
958                 bq = STAILQ_FIRST(&sc->delayed_bio_q);
959                 bq->bio->bio_error = ENOSPC;
960                 g_io_deliver(bq->bio, EIO);
961                 STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
962                 free(bq, M_GVIRSTOR);
963         }
964         mtx_unlock(&sc->delayed_bio_q_mtx);
965         mtx_destroy(&sc->delayed_bio_q_mtx);
966
967         free(sc->map, M_GVIRSTOR);
968         free(sc->components, M_GVIRSTOR);
969         bzero(sc, sizeof *sc);
970         free(sc, M_GVIRSTOR);
971
972         pp = LIST_FIRST(&gp->provider); /* We only offer one provider */
973         if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
974                 LOG_MSG(LVL_DEBUG, "Device %s destroyed", gp->name);
975
976         g_wither_geom(gp, ENXIO);
977
978         return (0);
979 }
980
981 /*
982  * Utility function: read metadata & decode. Wants topology lock to be
983  * held.
984  */
985 static int
986 read_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
987 {
988         struct g_provider *pp;
989         char *buf;
990         int error;
991
992         g_topology_assert();
993         error = g_access(cp, 1, 0, 0);
994         if (error != 0)
995                 return (error);
996         pp = cp->provider;
997         g_topology_unlock();
998         buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
999             &error);
1000         g_topology_lock();
1001         g_access(cp, -1, 0, 0);
1002         if (buf == NULL)
1003                 return (error);
1004
1005         virstor_metadata_decode(buf, md);
1006         g_free(buf);
1007
1008         return (0);
1009 }
1010
1011 /**
1012  * Utility function: encode & write metadata. Assumes topology lock is
1013  * held.
1014  */
1015 static int
1016 write_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
1017 {
1018         struct g_provider *pp;
1019         char *buf;
1020         int error;
1021
1022         KASSERT(cp != NULL && md != NULL && cp->provider != NULL,
1023             ("Something's fishy in %s", __func__));
1024         LOG_MSG(LVL_DEBUG, "Writing metadata on %s", cp->provider->name);
1025         g_topology_assert();
1026         error = g_access(cp, 0, 1, 0);
1027         if (error != 0)
1028                 return (error);
1029         pp = cp->provider;
1030
1031         buf = malloc(pp->sectorsize, M_GVIRSTOR, M_WAITOK);
1032         virstor_metadata_encode(md, buf);
1033         g_topology_unlock();
1034         error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf,
1035             pp->sectorsize);
1036         g_topology_lock();
1037         g_access(cp, 0, -1, 0);
1038
1039         free(buf, M_GVIRSTOR);
1040         return (0);
1041 }
1042
1043 /*
1044  * Creates a new instance of this GEOM class, initialise softc
1045  */
1046 static struct g_geom *
1047 create_virstor_geom(struct g_class *mp, struct g_virstor_metadata *md)
1048 {
1049         struct g_geom *gp;
1050         struct g_virstor_softc *sc;
1051
1052         LOG_MSG(LVL_DEBUG, "Creating geom instance for %s (id=%u)",
1053             md->md_name, md->md_id);
1054
1055         if (md->md_count < 1 || md->md_chunk_size < 1 ||
1056             md->md_virsize < md->md_chunk_size) {
1057                 /* This is bogus configuration, and probably means data is
1058                  * somehow corrupted. Panic, maybe? */
1059                 LOG_MSG(LVL_ERROR, "Nonsensical metadata information for %s",
1060                     md->md_name);
1061                 return (NULL);
1062         }
1063
1064         /* Check if it's already created */
1065         LIST_FOREACH(gp, &mp->geom, geom) {
1066                 sc = gp->softc;
1067                 if (sc != NULL && strcmp(sc->geom->name, md->md_name) == 0) {
1068                         LOG_MSG(LVL_WARNING, "Geom %s already exists",
1069                             md->md_name);
1070                         if (sc->id != md->md_id) {
1071                                 LOG_MSG(LVL_ERROR,
1072                                     "Some stale or invalid components "
1073                                     "exist for virstor device named %s. "
1074                                     "You will need to <CLEAR> all stale "
1075                                     "components and maybe reconfigure "
1076                                     "the virstor device. Tune "
1077                                     "kern.geom.virstor.debug sysctl up "
1078                                     "for more information.",
1079                                     sc->geom->name);
1080                         }
1081                         return (NULL);
1082                 }
1083         }
1084         gp = g_new_geomf(mp, "%s", md->md_name);
1085         gp->softc = NULL; /* to circumevent races that test softc */
1086
1087         gp->start = g_virstor_start;
1088         gp->spoiled = g_virstor_orphan;
1089         gp->orphan = g_virstor_orphan;
1090         gp->access = g_virstor_access;
1091         gp->dumpconf = g_virstor_dumpconf;
1092
1093         sc = malloc(sizeof(*sc), M_GVIRSTOR, M_WAITOK | M_ZERO);
1094         sc->id = md->md_id;
1095         sc->n_components = md->md_count;
1096         sc->components = malloc(sizeof(struct g_virstor_component) * md->md_count,
1097             M_GVIRSTOR, M_WAITOK | M_ZERO);
1098         sc->chunk_size = md->md_chunk_size;
1099         sc->virsize = md->md_virsize;
1100         STAILQ_INIT(&sc->delayed_bio_q);
1101         mtx_init(&sc->delayed_bio_q_mtx, "gvirstor_delayed_bio_q_mtx",
1102             "gvirstor", MTX_DEF | MTX_RECURSE);
1103
1104         sc->geom = gp;
1105         sc->provider = NULL; /* virstor_check_and_run will create it */
1106         gp->softc = sc;
1107
1108         LOG_MSG(LVL_ANNOUNCE, "Device %s created", sc->geom->name);
1109
1110         return (gp);
1111 }
1112
1113 /*
1114  * Add provider to a GEOM class instance
1115  */
1116 static int
1117 add_provider_to_geom(struct g_virstor_softc *sc, struct g_provider *pp,
1118     struct g_virstor_metadata *md)
1119 {
1120         struct g_virstor_component *component;
1121         struct g_consumer *cp, *fcp;
1122         struct g_geom *gp;
1123         int error;
1124
1125         if (md->no >= sc->n_components)
1126                 return (EINVAL);
1127
1128         /* "Current" compontent */
1129         component = &(sc->components[md->no]);
1130         if (component->gcons != NULL)
1131                 return (EEXIST);
1132
1133         gp = sc->geom;
1134         fcp = LIST_FIRST(&gp->consumer);
1135
1136         cp = g_new_consumer(gp);
1137         error = g_attach(cp, pp);
1138
1139         if (error != 0) {
1140                 g_destroy_consumer(cp);
1141                 return (error);
1142         }
1143
1144         if (fcp != NULL) {
1145                 if (fcp->provider->sectorsize != pp->sectorsize) {
1146                         /* TODO: this can be made to work */
1147                         LOG_MSG(LVL_ERROR, "Provider %s of %s has invalid "
1148                             "sector size (%d)", pp->name, sc->geom->name,
1149                             pp->sectorsize);
1150                         return (EINVAL);
1151                 }
1152                 if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) {
1153                         /* Replicate access permissions from first "live" consumer
1154                          * to the new one */
1155                         error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
1156                         if (error != 0) {
1157                                 g_detach(cp);
1158                                 g_destroy_consumer(cp);
1159                                 return (error);
1160                         }
1161                 }
1162         }
1163
1164         /* Bring up a new component */
1165         cp->private = component;
1166         component->gcons = cp;
1167         component->sc = sc;
1168         component->index = md->no;
1169         component->chunk_count = md->chunk_count;
1170         component->chunk_next = md->chunk_next;
1171         component->chunk_reserved = md->chunk_reserved;
1172         component->flags = md->flags;
1173
1174         LOG_MSG(LVL_DEBUG, "%s attached to %s", pp->name, sc->geom->name);
1175
1176         virstor_check_and_run(sc);
1177         return (0);
1178 }
1179
1180 /*
1181  * Check if everything's ready to create the geom provider & device entry,
1182  * create and start provider.
1183  * Called ultimately by .taste, from g_event thread
1184  */
1185 static void
1186 virstor_check_and_run(struct g_virstor_softc *sc)
1187 {
1188         off_t off;
1189         size_t n, count;
1190         int index;
1191         int error;
1192
1193         if (virstor_valid_components(sc) != sc->n_components)
1194                 return;
1195
1196         if (virstor_valid_components(sc) == 0) {
1197                 /* This is actually a candidate for panic() */
1198                 LOG_MSG(LVL_ERROR, "No valid components for %s?",
1199                     sc->provider->name);
1200                 return;
1201         }
1202
1203         sc->sectorsize = sc->components[0].gcons->provider->sectorsize;
1204
1205         /* Initialise allocation map from the first consumer */
1206         sc->chunk_count = sc->virsize / sc->chunk_size;
1207         if (sc->chunk_count * (off_t)sc->chunk_size != sc->virsize) {
1208                 LOG_MSG(LVL_WARNING, "Device %s truncated to %ju bytes",
1209                     sc->provider->name,
1210                     sc->chunk_count * (off_t)sc->chunk_size);
1211         }
1212         sc->map_size = sc->chunk_count * sizeof *(sc->map);
1213         /* The following allocation is in order of 4MB - 8MB */
1214         sc->map = malloc(sc->map_size, M_GVIRSTOR, M_WAITOK);
1215         KASSERT(sc->map != NULL, ("%s: Memory allocation error (%zu bytes) for %s",
1216             __func__, sc->map_size, sc->provider->name));
1217         sc->map_sectors = sc->map_size / sc->sectorsize;
1218
1219         count = 0;
1220         for (n = 0; n < sc->n_components; n++)
1221                 count += sc->components[n].chunk_count;
1222         LOG_MSG(LVL_INFO, "Device %s has %zu physical chunks and %zu virtual "
1223             "(%zu KB chunks)",
1224             sc->geom->name, count, sc->chunk_count, sc->chunk_size / 1024);
1225
1226         error = g_access(sc->components[0].gcons, 1, 0, 0);
1227         if (error != 0) {
1228                 LOG_MSG(LVL_ERROR, "Cannot acquire read access for %s to "
1229                     "read allocation map for %s",
1230                     sc->components[0].gcons->provider->name,
1231                     sc->geom->name);
1232                 return;
1233         }
1234         /* Read in the allocation map */
1235         LOG_MSG(LVL_DEBUG, "Reading map for %s from %s", sc->geom->name,
1236             sc->components[0].gcons->provider->name);
1237         off = count = n = 0;
1238         while (count < sc->map_size) {
1239                 struct g_virstor_map_entry *mapbuf;
1240                 size_t bs;
1241
1242                 bs = MIN(MAXPHYS, sc->map_size - count);
1243                 if (bs % sc->sectorsize != 0) {
1244                         /* Check for alignment errors */
1245                         bs = (bs / sc->sectorsize) * sc->sectorsize;
1246                         if (bs == 0)
1247                                 break;
1248                         LOG_MSG(LVL_ERROR, "Trouble: map is not sector-aligned "
1249                             "for %s on %s", sc->geom->name,
1250                             sc->components[0].gcons->provider->name);
1251                 }
1252                 mapbuf = g_read_data(sc->components[0].gcons, off, bs, &error);
1253                 if (mapbuf == NULL) {
1254                         free(sc->map, M_GVIRSTOR);
1255                         LOG_MSG(LVL_ERROR, "Error reading allocation map "
1256                             "for %s from %s (offset %ju) (error %d)",
1257                             sc->geom->name,
1258                             sc->components[0].gcons->provider->name,
1259                             off, error);
1260                         return;
1261                 }
1262
1263                 bcopy(mapbuf, &sc->map[n], bs);
1264                 off += bs;
1265                 count += bs;
1266                 n += bs / sizeof *(sc->map);
1267                 g_free(mapbuf);
1268         }
1269         g_access(sc->components[0].gcons, -1, 0, 0);
1270         LOG_MSG(LVL_DEBUG, "Read map for %s", sc->geom->name);
1271
1272         /* find first component with allocatable chunks */
1273         index = -1;
1274         for (n = 0; n < sc->n_components; n++) {
1275                 if (sc->components[n].chunk_next <
1276                     sc->components[n].chunk_count) {
1277                         index = n;
1278                         break;
1279                 }
1280         }
1281         if (index == -1)
1282                 /* not found? set it to the last component and handle it
1283                  * later */
1284                 index = sc->n_components - 1;
1285
1286         if (index >= sc->n_components - g_virstor_component_watermark - 1) {
1287                 LOG_MSG(LVL_WARNING, "Device %s running out of components "
1288                     "(%d/%u: %s)", sc->geom->name,
1289                     index+1,
1290                     sc->n_components,
1291                     sc->components[index].gcons->provider->name);
1292         }
1293         sc->curr_component = index;
1294
1295         if (sc->components[index].chunk_next >=
1296             sc->components[index].chunk_count - g_virstor_chunk_watermark) {
1297                 LOG_MSG(LVL_WARNING,
1298                     "Component %s of %s is running out of free space "
1299                     "(%u chunks left)",
1300                     sc->components[index].gcons->provider->name,
1301                     sc->geom->name, sc->components[index].chunk_count -
1302                     sc->components[index].chunk_next);
1303         }
1304
1305         sc->me_per_sector = sc->sectorsize / sizeof *(sc->map);
1306         if (sc->sectorsize % sizeof *(sc->map) != 0) {
1307                 LOG_MSG(LVL_ERROR,
1308                     "%s: Map entries don't fit exactly in a sector (%s)",
1309                     __func__, sc->geom->name);
1310                 return;
1311         }
1312
1313         /* Recalculate allocated chunks in components & at the same time
1314          * verify map data is sane. We could trust metadata on this, but
1315          * we want to make sure. */
1316         for (n = 0; n < sc->n_components; n++)
1317                 sc->components[n].chunk_next = sc->components[n].chunk_reserved;
1318
1319         for (n = 0; n < sc->chunk_count; n++) {
1320                 if (sc->map[n].provider_no >= sc->n_components ||
1321                         sc->map[n].provider_chunk >=
1322                         sc->components[sc->map[n].provider_no].chunk_count) {
1323                         LOG_MSG(LVL_ERROR, "%s: Invalid entry %u in map for %s",
1324                             __func__, (u_int)n, sc->geom->name);
1325                         LOG_MSG(LVL_ERROR, "%s: provider_no: %u, n_components: %u"
1326                             " provider_chunk: %u, chunk_count: %u", __func__,
1327                             sc->map[n].provider_no, sc->n_components,
1328                             sc->map[n].provider_chunk,
1329                             sc->components[sc->map[n].provider_no].chunk_count);
1330                         return;
1331                 }
1332                 if (sc->map[n].flags & VIRSTOR_MAP_ALLOCATED)
1333                         sc->components[sc->map[n].provider_no].chunk_next++;
1334         }
1335
1336         sc->provider = g_new_providerf(sc->geom, "virstor/%s",
1337             sc->geom->name);
1338
1339         sc->provider->sectorsize = sc->sectorsize;
1340         sc->provider->mediasize = sc->virsize;
1341         g_error_provider(sc->provider, 0);
1342
1343         LOG_MSG(LVL_INFO, "%s activated", sc->provider->name);
1344         LOG_MSG(LVL_DEBUG, "%s starting with current component %u, starting "
1345             "chunk %u", sc->provider->name, sc->curr_component,
1346             sc->components[sc->curr_component].chunk_next);
1347 }
1348
1349 /*
1350  * Returns count of active providers in this geom instance
1351  */
1352 static u_int
1353 virstor_valid_components(struct g_virstor_softc *sc)
1354 {
1355         unsigned int nc, i;
1356
1357         nc = 0;
1358         KASSERT(sc != NULL, ("%s: softc is NULL", __func__));
1359         KASSERT(sc->components != NULL, ("%s: sc->components is NULL", __func__));
1360         for (i = 0; i < sc->n_components; i++)
1361                 if (sc->components[i].gcons != NULL)
1362                         nc++;
1363         return (nc);
1364 }
1365
1366 /*
1367  * Called when the consumer gets orphaned (?)
1368  */
1369 static void
1370 g_virstor_orphan(struct g_consumer *cp)
1371 {
1372         struct g_virstor_softc *sc;
1373         struct g_virstor_component *comp;
1374         struct g_geom *gp;
1375
1376         g_topology_assert();
1377         gp = cp->geom;
1378         sc = gp->softc;
1379         if (sc == NULL)
1380                 return;
1381
1382         comp = cp->private;
1383         KASSERT(comp != NULL, ("%s: No component in private part of consumer",
1384             __func__));
1385         remove_component(sc, comp, FALSE);
1386         if (virstor_valid_components(sc) == 0)
1387                 virstor_geom_destroy(sc, TRUE, FALSE);
1388 }
1389
1390 /*
1391  * Called to notify geom when it's been opened, and for what intent
1392  */
1393 static int
1394 g_virstor_access(struct g_provider *pp, int dr, int dw, int de)
1395 {
1396         struct g_consumer *c;
1397         struct g_virstor_softc *sc;
1398         struct g_geom *gp;
1399         int error;
1400
1401         KASSERT(pp != NULL, ("%s: NULL provider", __func__));
1402         gp = pp->geom;
1403         KASSERT(gp != NULL, ("%s: NULL geom", __func__));
1404         sc = gp->softc;
1405
1406         if (sc == NULL) {
1407                 /* It seems that .access can be called with negative dr,dw,dx
1408                  * in this case but I want to check for myself */
1409                 LOG_MSG(LVL_WARNING, "access(%d, %d, %d) for %s",
1410                     dr, dw, de, pp->name);
1411                 /* This should only happen when geom is withered so
1412                  * allow only negative requests */
1413                 KASSERT(dr <= 0 && dw <= 0 && de <= 0,
1414                     ("%s: Positive access for %s", __func__, pp->name));
1415                 if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
1416                         LOG_MSG(LVL_DEBUG, "Device %s definitely destroyed",
1417                             pp->name);
1418                 return (0);
1419         }
1420
1421         /* Grab an exclusive bit to propagate on our consumers on first open */
1422         if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1423                 de++;
1424         /* ... drop it on close */
1425         if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0) {
1426                 de--;
1427                 update_metadata(sc);    /* Writes statistical information */
1428         }
1429
1430         error = ENXIO;
1431         LIST_FOREACH(c, &gp->consumer, consumer) {
1432                 KASSERT(c != NULL, ("%s: consumer is NULL", __func__));
1433                 error = g_access(c, dr, dw, de);
1434                 if (error != 0) {
1435                         struct g_consumer *c2;
1436
1437                         /* Backout earlier changes */
1438                         LIST_FOREACH(c2, &gp->consumer, consumer) {
1439                                 if (c2 == c) /* all eariler components fixed */
1440                                         return (error);
1441                                 g_access(c2, -dr, -dw, -de);
1442                         }
1443                 }
1444         }
1445
1446         return (error);
1447 }
1448
1449 /*
1450  * Generate XML dump of current state
1451  */
1452 static void
1453 g_virstor_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1454     struct g_consumer *cp, struct g_provider *pp)
1455 {
1456         struct g_virstor_softc *sc;
1457
1458         g_topology_assert();
1459         sc = gp->softc;
1460
1461         if (sc == NULL || pp != NULL)
1462                 return;
1463
1464         if (cp != NULL) {
1465                 /* For each component */
1466                 struct g_virstor_component *comp;
1467
1468                 comp = cp->private;
1469                 if (comp == NULL)
1470                         return;
1471                 sbuf_printf(sb, "%s<ComponentIndex>%u</ComponentIndex>\n",
1472                     indent, comp->index);
1473                 sbuf_printf(sb, "%s<ChunkCount>%u</ChunkCount>\n",
1474                     indent, comp->chunk_count);
1475                 sbuf_printf(sb, "%s<ChunksUsed>%u</ChunksUsed>\n",
1476                     indent, comp->chunk_next);
1477                 sbuf_printf(sb, "%s<ChunksReserved>%u</ChunksReserved>\n",
1478                     indent, comp->chunk_reserved);
1479                 sbuf_printf(sb, "%s<StorageFree>%u%%</StorageFree>\n",
1480                     indent,
1481                     comp->chunk_next > 0 ? 100 -
1482                     ((comp->chunk_next + comp->chunk_reserved) * 100) /
1483                     comp->chunk_count : 100);
1484         } else {
1485                 /* For the whole thing */
1486                 u_int count, used, i;
1487                 off_t size;
1488
1489                 count = used = size = 0;
1490                 for (i = 0; i < sc->n_components; i++) {
1491                         if (sc->components[i].gcons != NULL) {
1492                                 count += sc->components[i].chunk_count;
1493                                 used += sc->components[i].chunk_next +
1494                                     sc->components[i].chunk_reserved;
1495                                 size += sc->components[i].gcons->
1496                                     provider->mediasize;
1497                         }
1498                 }
1499
1500                 sbuf_printf(sb, "%s<Status>"
1501                     "Components=%u, Online=%u</Status>\n", indent,
1502                     sc->n_components, virstor_valid_components(sc));
1503                 sbuf_printf(sb, "%s<State>%u%% physical free</State>\n",
1504                     indent, 100-(used * 100) / count);
1505                 sbuf_printf(sb, "%s<ChunkSize>%zu</ChunkSize>\n", indent,
1506                     sc->chunk_size);
1507                 sbuf_printf(sb, "%s<PhysicalFree>%u%%</PhysicalFree>\n",
1508                     indent, used > 0 ? 100 - (used * 100) / count : 100);
1509                 sbuf_printf(sb, "%s<ChunkPhysicalCount>%u</ChunkPhysicalCount>\n",
1510                     indent, count);
1511                 sbuf_printf(sb, "%s<ChunkVirtualCount>%zu</ChunkVirtualCount>\n",
1512                     indent, sc->chunk_count);
1513                 sbuf_printf(sb, "%s<PhysicalBacking>%zu%%</PhysicalBacking>\n",
1514                     indent,
1515                     (count * 100) / sc->chunk_count);
1516                 sbuf_printf(sb, "%s<PhysicalBackingSize>%jd</PhysicalBackingSize>\n",
1517                     indent, size);
1518                 sbuf_printf(sb, "%s<VirtualSize>%jd</VirtualSize>\n", indent,
1519                     sc->virsize);
1520         }
1521 }
1522
1523 /*
1524  * GEOM .done handler
1525  * Can't use standard handler because one requested IO may
1526  * fork into additional data IOs
1527  */
1528 static void
1529 g_virstor_done(struct bio *b)
1530 {
1531         struct g_virstor_softc *sc;
1532         struct bio *parent_b;
1533
1534         parent_b = b->bio_parent;
1535         sc = parent_b->bio_to->geom->softc;
1536
1537         if (b->bio_error != 0) {
1538                 LOG_MSG(LVL_ERROR, "Error %d for offset=%ju, length=%ju, %s",
1539                     b->bio_error, b->bio_offset, b->bio_length,
1540                     b->bio_to->name);
1541                 if (parent_b->bio_error == 0)
1542                         parent_b->bio_error = b->bio_error;
1543         }
1544
1545         parent_b->bio_inbed++;
1546         parent_b->bio_completed += b->bio_completed;
1547
1548         if (parent_b->bio_children == parent_b->bio_inbed) {
1549                 parent_b->bio_completed = parent_b->bio_length;
1550                 g_io_deliver(parent_b, parent_b->bio_error);
1551         }
1552         g_destroy_bio(b);
1553 }
1554
1555 /*
1556  * I/O starts here
1557  * Called in g_down thread
1558  */
1559 static void
1560 g_virstor_start(struct bio *b)
1561 {
1562         struct g_virstor_softc *sc;
1563         struct g_virstor_component *comp;
1564         struct bio *cb;
1565         struct g_provider *pp;
1566         char *addr;
1567         off_t offset, length;
1568         struct bio_queue_head bq;
1569         size_t chunk_size;      /* cached for convenience */
1570         u_int count;
1571
1572         pp = b->bio_to;
1573         sc = pp->geom->softc;
1574         KASSERT(sc != NULL, ("%s: no softc (error=%d, device=%s)", __func__,
1575             b->bio_to->error, b->bio_to->name));
1576
1577         LOG_REQ(LVL_MOREDEBUG, b, "%s", __func__);
1578
1579         switch (b->bio_cmd) {
1580         case BIO_READ:
1581         case BIO_WRITE:
1582         case BIO_DELETE:
1583                 break;
1584         default:
1585                 g_io_deliver(b, EOPNOTSUPP);
1586                 return;
1587         }
1588
1589         LOG_MSG(LVL_DEBUG2, "BIO arrived, size=%ju", b->bio_length);
1590         bioq_init(&bq);
1591
1592         chunk_size = sc->chunk_size;
1593         addr = b->bio_data;
1594         offset = b->bio_offset; /* virtual offset and length */
1595         length = b->bio_length;
1596
1597         while (length > 0) {
1598                 size_t chunk_index, in_chunk_offset, in_chunk_length;
1599                 struct virstor_map_entry *me;
1600
1601                 chunk_index = offset / chunk_size; /* round downwards */
1602                 in_chunk_offset = offset % chunk_size;
1603                 in_chunk_length = min(length, chunk_size - in_chunk_offset);
1604                 LOG_MSG(LVL_DEBUG, "Mapped %s(%ju, %ju) to (%zu,%zu,%zu)",
1605                     b->bio_cmd == BIO_READ ? "R" : "W",
1606                     offset, length,
1607                     chunk_index, in_chunk_offset, in_chunk_length);
1608                 me = &sc->map[chunk_index];
1609
1610                 if (b->bio_cmd == BIO_READ || b->bio_cmd == BIO_DELETE) {
1611                         if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
1612                                 /* Reads from unallocated chunks return zeroed
1613                                  * buffers */
1614                                 if (b->bio_cmd == BIO_READ)
1615                                         bzero(addr, in_chunk_length);
1616                         } else {
1617                                 comp = &sc->components[me->provider_no];
1618
1619                                 cb = g_clone_bio(b);
1620                                 if (cb == NULL) {
1621                                         bioq_dismantle(&bq);
1622                                         if (b->bio_error == 0)
1623                                                 b->bio_error = ENOMEM;
1624                                         g_io_deliver(b, b->bio_error);
1625                                         return;
1626                                 }
1627                                 cb->bio_to = comp->gcons->provider;
1628                                 cb->bio_done = g_virstor_done;
1629                                 cb->bio_offset =
1630                                     (off_t)me->provider_chunk * (off_t)chunk_size
1631                                     + in_chunk_offset;
1632                                 cb->bio_length = in_chunk_length;
1633                                 cb->bio_data = addr;
1634                                 cb->bio_caller1 = comp;
1635                                 bioq_disksort(&bq, cb);
1636                         }
1637                 } else { /* handle BIO_WRITE */
1638                         KASSERT(b->bio_cmd == BIO_WRITE,
1639                             ("%s: Unknown command %d", __func__,
1640                             b->bio_cmd));
1641
1642                         if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
1643                                 /* We have a virtual chunk, represented by
1644                                  * the "me" entry, but it's not yet allocated
1645                                  * (tied to) a physical chunk. So do it now. */
1646                                 struct virstor_map_entry *data_me;
1647                                 u_int phys_chunk, comp_no;
1648                                 off_t s_offset;
1649                                 int error;
1650
1651                                 error = allocate_chunk(sc, &comp, &comp_no,
1652                                     &phys_chunk);
1653                                 if (error != 0) {
1654                                         /* We cannot allocate a physical chunk
1655                                          * to satisfy this request, so we'll
1656                                          * delay it to when we can...
1657                                          * XXX: this will prevent the fs from
1658                                          * being umounted! */
1659                                         struct g_virstor_bio_q *biq;
1660                                         biq = malloc(sizeof *biq, M_GVIRSTOR,
1661                                             M_NOWAIT);
1662                                         if (biq == NULL) {
1663                                                 bioq_dismantle(&bq);
1664                                                 if (b->bio_error == 0)
1665                                                         b->bio_error = ENOMEM;
1666                                                 g_io_deliver(b, b->bio_error);
1667                                                 return;
1668                                         }
1669                                         biq->bio = b;
1670                                         mtx_lock(&sc->delayed_bio_q_mtx);
1671                                         STAILQ_INSERT_TAIL(&sc->delayed_bio_q,
1672                                             biq, linkage);
1673                                         mtx_unlock(&sc->delayed_bio_q_mtx);
1674                                         LOG_MSG(LVL_WARNING, "Delaying BIO "
1675                                             "(size=%ju) until free physical "
1676                                             "space can be found on %s",
1677                                             b->bio_length,
1678                                             sc->provider->name);
1679                                         return;
1680                                 }
1681                                 LOG_MSG(LVL_DEBUG, "Allocated chunk %u on %s "
1682                                     "for %s",
1683                                     phys_chunk,
1684                                     comp->gcons->provider->name,
1685                                     sc->provider->name);
1686
1687                                 me->provider_no = comp_no;
1688                                 me->provider_chunk = phys_chunk;
1689                                 me->flags |= VIRSTOR_MAP_ALLOCATED;
1690
1691                                 cb = g_clone_bio(b);
1692                                 if (cb == NULL) {
1693                                         me->flags &= ~VIRSTOR_MAP_ALLOCATED;
1694                                         me->provider_no = 0;
1695                                         me->provider_chunk = 0;
1696                                         bioq_dismantle(&bq);
1697                                         if (b->bio_error == 0)
1698                                                 b->bio_error = ENOMEM;
1699                                         g_io_deliver(b, b->bio_error);
1700                                         return;
1701                                 }
1702
1703                                 /* The allocation table is stored continuously
1704                                  * at the start of the drive. We need to
1705                                  * calculate the offset of the sector that holds
1706                                  * this map entry both on the drive and in the
1707                                  * map array.
1708                                  * sc_offset will end up pointing to the drive
1709                                  * sector. */
1710                                 s_offset = chunk_index * sizeof *me;
1711                                 s_offset = (s_offset / sc->sectorsize) *
1712                                     sc->sectorsize;
1713
1714                                 /* data_me points to map entry sector
1715                                  * in memory (analoguos to offset) */
1716                                 data_me = &sc->map[(chunk_index /
1717                                     sc->me_per_sector) * sc->me_per_sector];
1718
1719                                 /* Commit sector with map entry to storage */
1720                                 cb->bio_to = sc->components[0].gcons->provider;
1721                                 cb->bio_done = g_virstor_done;
1722                                 cb->bio_offset = s_offset;
1723                                 cb->bio_data = (char *)data_me;
1724                                 cb->bio_length = sc->sectorsize;
1725                                 cb->bio_caller1 = &sc->components[0];
1726                                 bioq_disksort(&bq, cb);
1727                         }
1728
1729                         comp = &sc->components[me->provider_no];
1730                         cb = g_clone_bio(b);
1731                         if (cb == NULL) {
1732                                 bioq_dismantle(&bq);
1733                                 if (b->bio_error == 0)
1734                                         b->bio_error = ENOMEM;
1735                                 g_io_deliver(b, b->bio_error);
1736                                 return;
1737                         }
1738                         /* Finally, handle the data */
1739                         cb->bio_to = comp->gcons->provider;
1740                         cb->bio_done = g_virstor_done;
1741                         cb->bio_offset = (off_t)me->provider_chunk*(off_t)chunk_size +
1742                             in_chunk_offset;
1743                         cb->bio_length = in_chunk_length;
1744                         cb->bio_data = addr;
1745                         cb->bio_caller1 = comp;
1746                         bioq_disksort(&bq, cb);
1747                 }
1748                 addr += in_chunk_length;
1749                 length -= in_chunk_length;
1750                 offset += in_chunk_length;
1751         }
1752
1753         /* Fire off bio's here */
1754         count = 0;
1755         for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) {
1756                 bioq_remove(&bq, cb);
1757                 LOG_REQ(LVL_MOREDEBUG, cb, "Firing request");
1758                 comp = cb->bio_caller1;
1759                 cb->bio_caller1 = NULL;
1760                 LOG_MSG(LVL_DEBUG, " firing bio, offset=%ju, length=%ju",
1761                     cb->bio_offset, cb->bio_length);
1762                 g_io_request(cb, comp->gcons);
1763                 count++;
1764         }
1765         if (count == 0) { /* We handled everything locally */
1766                 b->bio_completed = b->bio_length;
1767                 g_io_deliver(b, 0);
1768         }
1769
1770 }
1771
1772 /*
1773  * Allocate a chunk from a physical provider. Returns physical component,
1774  * chunk index relative to the component and the component's index.
1775  */
1776 static int
1777 allocate_chunk(struct g_virstor_softc *sc, struct g_virstor_component **comp,
1778     u_int *comp_no_p, u_int *chunk)
1779 {
1780         u_int comp_no;
1781
1782         KASSERT(sc->curr_component < sc->n_components,
1783             ("%s: Invalid curr_component: %u",  __func__, sc->curr_component));
1784
1785         comp_no = sc->curr_component;
1786         *comp = &sc->components[comp_no];
1787         dump_component(*comp);
1788         if ((*comp)->chunk_next >= (*comp)->chunk_count) {
1789                 /* This component is full. Allocate next component */
1790                 if (comp_no >= sc->n_components-1) {
1791                         LOG_MSG(LVL_ERROR, "All physical space allocated for %s",
1792                             sc->geom->name);
1793                         return (-1);
1794                 }
1795                 (*comp)->flags &= ~VIRSTOR_PROVIDER_CURRENT;
1796                 sc->curr_component = ++comp_no;
1797
1798                 *comp = &sc->components[comp_no];
1799                 if (comp_no >= sc->n_components - g_virstor_component_watermark-1)
1800                         LOG_MSG(LVL_WARNING, "Device %s running out of components "
1801                             "(switching to %u/%u: %s)", sc->geom->name,
1802                             comp_no+1, sc->n_components,
1803                             (*comp)->gcons->provider->name);
1804                 /* Take care not to overwrite reserved chunks */
1805                 if ( (*comp)->chunk_reserved > 0 &&
1806                     (*comp)->chunk_next < (*comp)->chunk_reserved)
1807                         (*comp)->chunk_next = (*comp)->chunk_reserved;
1808
1809                 (*comp)->flags |=
1810                     VIRSTOR_PROVIDER_ALLOCATED | VIRSTOR_PROVIDER_CURRENT;
1811                 dump_component(*comp);
1812                 *comp_no_p = comp_no;
1813                 *chunk = (*comp)->chunk_next++;
1814         } else {
1815                 *comp_no_p = comp_no;
1816                 *chunk = (*comp)->chunk_next++;
1817         }
1818         return (0);
1819 }
1820
1821 /* Dump a component */
1822 static void
1823 dump_component(struct g_virstor_component *comp)
1824 {
1825
1826         if (g_virstor_debug < LVL_DEBUG2)
1827                 return;
1828         printf("Component %d: %s\n", comp->index, comp->gcons->provider->name);
1829         printf("  chunk_count: %u\n", comp->chunk_count);
1830         printf("   chunk_next: %u\n", comp->chunk_next);
1831         printf("        flags: %u\n", comp->flags);
1832 }
1833
1834 #if 0
1835 /* Dump a map entry */
1836 static void
1837 dump_me(struct virstor_map_entry *me, unsigned int nr)
1838 {
1839         if (g_virstor_debug < LVL_DEBUG)
1840                 return;
1841         printf("VIRT. CHUNK #%d: ", nr);
1842         if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0)
1843                 printf("(unallocated)\n");
1844         else
1845                 printf("allocated at provider %u, provider_chunk %u\n",
1846                     me->provider_no, me->provider_chunk);
1847 }
1848 #endif
1849
1850 /*
1851  * Dismantle bio_queue and destroy its components
1852  */
1853 static void
1854 bioq_dismantle(struct bio_queue_head *bq)
1855 {
1856         struct bio *b;
1857
1858         for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
1859                 bioq_remove(bq, b);
1860                 g_destroy_bio(b);
1861         }
1862 }
1863
1864 /*
1865  * The function that shouldn't be called.
1866  * When this is called, the stack is already garbled because of
1867  * argument mismatch. There's nothing to do now but panic, which is
1868  * accidentally the whole purpose of this function.
1869  * Motivation: to guard from accidentally calling geom methods when
1870  * they shouldn't be called. (see g_..._taste)
1871  */
1872 static void
1873 invalid_call(void)
1874 {
1875         panic("invalid_call() has just been called. Something's fishy here.");
1876 }
1877
1878 DECLARE_GEOM_CLASS(g_virstor_class, g_virstor); /* Let there be light */