2 * Copyright (c) 2006-2007 Ivan Voras <ivoras@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 /* Implementation notes:
28 * - "Components" are wrappers around providers that make up the
29 * virtual storage (i.e. a virstor has "physical" components)
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
47 #include <sys/kthread.h>
48 #include <sys/mutex.h>
50 #include <geom/geom.h>
52 #include <geom/virstor/g_virstor.h>
53 #include <geom/virstor/g_virstor_md.h>
55 /* Declare malloc(9) label */
56 static MALLOC_DEFINE(M_GVIRSTOR, "gvirstor", "GEOM_VIRSTOR Data");
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;
65 /* Declare & initialize class structure ("geom class") */
66 struct g_class g_virstor_class = {
67 .name = G_VIRSTOR_CLASS_NAME,
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. */
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");
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)");
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");
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");
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,
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);
119 static void dump_me(struct virstor_map_entry *me, unsigned int nr);
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 *,
127 static void update_metadata(struct g_virstor_softc *);
128 static void fill_metadata(struct g_virstor_softc *, struct g_virstor_metadata *,
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 *);
138 static void invalid_call(void);
140 * Initialise GEOM class (per-class callback)
143 g_virstor_init(struct g_class *mp __unused)
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);
150 /* Init UMA zones, TAILQ's, other global vars */
154 * Finalise GEOM class (per-class callback)
157 g_virstor_fini(struct g_class *mp __unused)
160 /* Deinit UMA zones & global vars */
164 * Config (per-class callback)
167 g_virstor_config(struct gctl_req *req, struct g_class *cp, char const *verb)
173 version = gctl_get_paraml(req, "version", sizeof(*version));
174 if (version == NULL) {
175 gctl_error(req, "Failed to get 'version' argument");
178 if (*version != G_VIRSTOR_VERSION) {
179 gctl_error(req, "Userland and kernel versions out of sync");
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);
191 gctl_error(req, "unknown verb: '%s'", verb);
196 * "stop" verb from userland
199 virstor_ctl_stop(struct gctl_req *req, struct g_class *cp)
204 nargs = gctl_get_paraml(req, "nargs", sizeof *nargs);
206 gctl_error(req, "Error fetching argument '%s'", "nargs");
210 gctl_error(req, "Invalid number of arguments");
213 force = gctl_get_paraml(req, "force", sizeof *force);
215 gctl_error(req, "Error fetching argument '%s'", "force");
220 for (i = 0; i < *nargs; i++) {
223 struct g_virstor_softc *sc;
226 sprintf(param, "arg%d", i);
227 name = gctl_get_asciiparam(req, param);
228 sc = virstor_find_geom(cp, name);
229 LOG_MSG(LVL_INFO, "Stopping %s by the userland command",
232 if ((error = virstor_geom_destroy(sc, TRUE, TRUE)) != 0) {
233 LOG_MSG(LVL_ERROR, "Cannot destroy %s: %d",
234 sc->geom->name, error);
241 * "add" verb from userland - add new component(s) to the structure.
242 * This will be done all at once in here, without going through the
243 * .taste function for new components.
246 virstor_ctl_add(struct gctl_req *req, struct g_class *cp)
248 /* Note: while this is going on, I/O is being done on
249 * the g_up and g_down threads. The idea is to make changes
250 * to softc members in a way that can atomically activate
251 * them all at once. */
252 struct g_virstor_softc *sc;
253 int *hardcode, *nargs;
254 const char *geom_name; /* geom to add a component to */
255 struct g_consumer *fcp;
256 struct g_virstor_bio_q *bq;
261 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
263 gctl_error(req, "Error fetching argument '%s'", "nargs");
267 gctl_error(req, "Invalid number of arguments");
270 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
271 if (hardcode == NULL) {
272 gctl_error(req, "Error fetching argument '%s'", "hardcode");
276 /* Find "our" geom */
277 geom_name = gctl_get_asciiparam(req, "arg0");
278 if (geom_name == NULL) {
279 gctl_error(req, "Error fetching argument '%s'", "geom_name (arg0)");
282 sc = virstor_find_geom(cp, geom_name);
284 gctl_error(req, "Don't know anything about '%s'", geom_name);
288 if (virstor_valid_components(sc) != sc->n_components) {
289 LOG_MSG(LVL_ERROR, "Cannot add components to incomplete "
290 "virstor %s", sc->geom->name);
291 gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
295 fcp = sc->components[0].gcons;
298 for (i = 1; i < *nargs; i++) {
299 struct g_virstor_metadata md;
301 const char *prov_name;
302 struct g_provider *pp;
303 struct g_consumer *cp;
307 snprintf(aname, sizeof aname, "arg%d", i);
308 prov_name = gctl_get_asciiparam(req, aname);
309 if (strncmp(prov_name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
310 prov_name += strlen(_PATH_DEV);
312 pp = g_provider_by_name(prov_name);
314 /* This is the most common error so be verbose about it */
316 gctl_error(req, "Invalid provider: '%s' (added"
317 " %u components)", prov_name, added);
320 gctl_error(req, "Invalid provider: '%s'",
326 cp = g_new_consumer(sc->geom);
328 gctl_error(req, "Cannot create consumer");
332 error = g_attach(cp, pp);
334 gctl_error(req, "Cannot attach a consumer to %s",
336 g_destroy_consumer(cp);
340 if (fcp->acr != 0 || fcp->acw != 0 || fcp->ace != 0) {
341 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
343 gctl_error(req, "Access request failed for %s",
345 g_destroy_consumer(cp);
350 if (fcp->provider->sectorsize != pp->sectorsize) {
351 gctl_error(req, "Sector size doesn't fit for %s",
353 g_destroy_consumer(cp);
357 for (j = 0; j < sc->n_components; j++) {
358 if (strcmp(sc->components[j].gcons->provider->name,
360 gctl_error(req, "Component %s already in %s",
361 pp->name, sc->geom->name);
362 g_destroy_consumer(cp);
367 sc->components = realloc(sc->components,
368 sizeof(*sc->components) * (sc->n_components + 1),
369 M_GVIRSTOR, M_WAITOK);
371 nc = sc->n_components;
372 sc->components[nc].gcons = cp;
373 sc->components[nc].sc = sc;
374 sc->components[nc].index = nc;
375 sc->components[nc].chunk_count = cp->provider->mediasize /
377 sc->components[nc].chunk_next = 0;
378 sc->components[nc].chunk_reserved = 0;
380 if (sc->components[nc].chunk_count < 4) {
381 gctl_error(req, "Provider too small: %s",
383 g_destroy_consumer(cp);
387 fill_metadata(sc, &md, nc, *hardcode);
388 write_metadata(cp, &md);
389 /* The new component becomes visible when n_components is
395 /* This call to update_metadata() is critical. In case there's a
396 * power failure in the middle of it and some components are updated
397 * while others are not, there will be trouble on next .taste() iff
398 * a non-updated component is detected first */
401 LOG_MSG(LVL_INFO, "Added %d component(s) to %s", added,
403 /* Fire off BIOs previously queued because there wasn't any
404 * physical space left. If the BIOs still can't be satisfied
405 * they will again be added to the end of the queue (during
406 * which the mutex will be recursed) */
407 bq = malloc(sizeof(*bq), M_GVIRSTOR, M_WAITOK);
409 mtx_lock(&sc->delayed_bio_q_mtx);
410 /* First, insert a sentinel to the queue end, so we don't
411 * end up in an infinite loop if there's still no free
412 * space available. */
413 STAILQ_INSERT_TAIL(&sc->delayed_bio_q, bq, linkage);
414 while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
415 bq = STAILQ_FIRST(&sc->delayed_bio_q);
416 if (bq->bio != NULL) {
417 g_virstor_start(bq->bio);
418 STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
419 free(bq, M_GVIRSTOR);
421 STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
422 free(bq, M_GVIRSTOR);
426 mtx_unlock(&sc->delayed_bio_q_mtx);
431 * Find a geom handled by the class
433 static struct g_virstor_softc *
434 virstor_find_geom(const struct g_class *cp, const char *name)
438 LIST_FOREACH(gp, &cp->geom, geom) {
439 if (strcmp(name, gp->name) == 0)
446 * Update metadata on all components to reflect the current state
451 * Expects things to be set up so write_metadata() can work, i.e.
452 * the topology lock must be held.
455 update_metadata(struct g_virstor_softc *sc)
457 struct g_virstor_metadata md;
460 if (virstor_valid_components(sc) != sc->n_components)
461 return; /* Incomplete device */
462 LOG_MSG(LVL_DEBUG, "Updating metadata on components for %s",
464 /* Update metadata on components */
465 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__,
466 sc->geom->class->name, sc->geom->name);
468 for (n = 0; n < sc->n_components; n++) {
469 read_metadata(sc->components[n].gcons, &md);
470 md.chunk_next = sc->components[n].chunk_next;
471 md.flags = sc->components[n].flags;
472 md.md_count = sc->n_components;
473 write_metadata(sc->components[n].gcons, &md);
478 * Fills metadata (struct md) from information stored in softc and the nc'th
479 * component of virstor
482 fill_metadata(struct g_virstor_softc *sc, struct g_virstor_metadata *md,
483 u_int nc, u_int hardcode)
485 struct g_virstor_component *c;
487 bzero(md, sizeof *md);
488 c = &sc->components[nc];
490 strncpy(md->md_magic, G_VIRSTOR_MAGIC, sizeof md->md_magic);
491 md->md_version = G_VIRSTOR_VERSION;
492 strncpy(md->md_name, sc->geom->name, sizeof md->md_name);
494 md->md_virsize = sc->virsize;
495 md->md_chunk_size = sc->chunk_size;
496 md->md_count = sc->n_components;
499 strncpy(md->provider, c->gcons->provider->name,
500 sizeof md->provider);
503 md->provsize = c->gcons->provider->mediasize;
504 md->chunk_count = c->chunk_count;
505 md->chunk_next = c->chunk_next;
506 md->chunk_reserved = c->chunk_reserved;
507 md->flags = c->flags;
511 * Remove a component from virstor device.
512 * Can only be done if the component is unallocated.
515 virstor_ctl_remove(struct gctl_req *req, struct g_class *cp)
517 /* As this is executed in parallel to I/O, operations on virstor
518 * structures must be as atomic as possible. */
519 struct g_virstor_softc *sc;
521 const char *geom_name;
525 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
527 gctl_error(req, "Error fetching argument '%s'", "nargs");
531 gctl_error(req, "Invalid number of arguments");
534 /* Find "our" geom */
535 geom_name = gctl_get_asciiparam(req, "arg0");
536 if (geom_name == NULL) {
537 gctl_error(req, "Error fetching argument '%s'",
541 sc = virstor_find_geom(cp, geom_name);
543 gctl_error(req, "Don't know anything about '%s'", geom_name);
547 if (virstor_valid_components(sc) != sc->n_components) {
548 LOG_MSG(LVL_ERROR, "Cannot remove components from incomplete "
549 "virstor %s", sc->geom->name);
550 gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
555 for (i = 1; i < *nargs; i++) {
557 const char *prov_name;
559 struct g_virstor_component *newcomp, *compbak;
561 sprintf(param, "arg%d", i);
562 prov_name = gctl_get_asciiparam(req, param);
563 if (strncmp(prov_name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
564 prov_name += strlen(_PATH_DEV);
567 for (j = 0; j < sc->n_components; j++) {
568 if (strcmp(sc->components[j].gcons->provider->name,
575 LOG_MSG(LVL_ERROR, "No %s component in %s",
576 prov_name, sc->geom->name);
580 compbak = sc->components;
581 newcomp = malloc(sc->n_components * sizeof(*sc->components),
582 M_GVIRSTOR, M_WAITOK | M_ZERO);
583 bcopy(sc->components, newcomp, found * sizeof(*sc->components));
584 bcopy(&sc->components[found + 1], newcomp + found,
585 found * sizeof(*sc->components));
586 if ((sc->components[j].flags & VIRSTOR_PROVIDER_ALLOCATED) != 0) {
587 LOG_MSG(LVL_ERROR, "Allocated provider %s cannot be "
589 prov_name, sc->geom->name);
590 free(newcomp, M_GVIRSTOR);
591 /* We'll consider this non-fatal error */
594 /* Renumerate unallocated components */
595 for (j = 0; j < sc->n_components-1; j++) {
596 if ((sc->components[j].flags &
597 VIRSTOR_PROVIDER_ALLOCATED) == 0) {
598 sc->components[j].index = j;
601 /* This is the critical section. If a component allocation
602 * event happens while both variables are not yet set,
603 * there will be trouble. Something will panic on encountering
604 * NULL sc->components[x].gcomp member.
605 * Luckily, component allocation happens very rarely and
606 * removing components is an abnormal action in any case. */
607 sc->components = newcomp;
609 /* End critical section */
612 if (clear_metadata(&compbak[found]) != 0) {
613 LOG_MSG(LVL_WARNING, "Trouble ahead: cannot clear "
614 "metadata on %s", prov_name);
616 g_detach(compbak[found].gcons);
617 g_destroy_consumer(compbak[found].gcons);
620 free(compbak, M_GVIRSTOR);
625 /* This call to update_metadata() is critical. In case there's a
626 * power failure in the middle of it and some components are updated
627 * while others are not, there will be trouble on next .taste() iff
628 * a non-updated component is detected first */
632 LOG_MSG(LVL_INFO, "Removed %d component(s) from %s", removed,
637 * Clear metadata sector on component
640 clear_metadata(struct g_virstor_component *comp)
645 LOG_MSG(LVL_INFO, "Clearing metadata on %s",
646 comp->gcons->provider->name);
648 error = g_access(comp->gcons, 0, 1, 0);
651 buf = malloc(comp->gcons->provider->sectorsize, M_GVIRSTOR,
653 error = g_write_data(comp->gcons,
654 comp->gcons->provider->mediasize -
655 comp->gcons->provider->sectorsize,
657 comp->gcons->provider->sectorsize);
658 free(buf, M_GVIRSTOR);
659 g_access(comp->gcons, 0, -1, 0);
664 * Destroy geom forcibly.
667 g_virstor_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
670 struct g_virstor_softc *sc;
674 KASSERT(sc != NULL, ("%s: NULL sc", __func__));
677 LOG_MSG(LVL_DEBUG, "%s called for %s, sc=%p", __func__, gp->name,
688 LOG_MSG(LVL_INFO, "INVARIANTS detected");
689 LOG_MSG(LVL_INFO, "Verifying allocation "
690 "table for %s", sc->geom->name);
692 for (n = 0; n < sc->chunk_count; n++) {
693 if (sc->map[n].flags || VIRSTOR_MAP_ALLOCATED != 0)
696 LOG_MSG(LVL_INFO, "Device %s has %d allocated chunks",
697 sc->geom->name, count);
700 if (virstor_valid_components(sc) != sc->n_components) {
701 /* This is a incomplete virstor device (not all
702 * components have been found) */
703 LOG_MSG(LVL_ERROR, "Device %s is incomplete",
707 error = g_access(sc->components[0].gcons, 1, 0, 0);
708 KASSERT(error == 0, ("%s: g_access failed (%d)", __func__,
710 /* Compare the whole on-disk allocation table with what's
711 * currently in memory */
712 while (n < sc->chunk_count) {
713 buf = g_read_data(sc->components[0].gcons, off,
714 sc->sectorsize, &error);
715 KASSERT(buf != NULL, ("g_read_data returned NULL (%d) "
716 "for read at %jd", error, off));
717 if (bcmp(buf, &sc->map[n], sc->sectorsize) != 0) {
718 LOG_MSG(LVL_ERROR, "ERROR in allocation table, "
719 "entry %d, offset %jd", n, off);
723 n += sc->me_per_sector;
724 off += sc->sectorsize;
727 error = g_access(sc->components[0].gcons, -1, 0, 0);
728 KASSERT(error == 0, ("%s: g_access failed (%d) on exit",
731 LOG_MSG(LVL_ERROR, "ALLOCATION TABLE CORRUPTED FOR %s "
732 "(%d sectors don't match, max %zu allocations)",
733 sc->geom->name, count,
734 count * sc->me_per_sector);
736 LOG_MSG(LVL_INFO, "Allocation table ok for %s",
742 virstor_geom_destroy(sc, FALSE, FALSE);
750 * Taste event (per-class callback)
751 * Examines a provider and creates geom instances if needed
753 static struct g_geom *
754 g_virstor_taste(struct g_class *mp, struct g_provider *pp, int flags)
756 struct g_virstor_metadata md;
758 struct g_consumer *cp;
759 struct g_virstor_softc *sc;
762 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
764 LOG_MSG(LVL_DEBUG, "Tasting %s", pp->name);
766 /* We need a dummy geom to attach a consumer to the given provider */
767 gp = g_new_geomf(mp, "virstor:taste.helper");
768 gp->start = (void *)invalid_call; /* XXX: hacked up so the */
769 gp->access = (void *)invalid_call; /* compiler doesn't complain. */
770 gp->orphan = (void *)invalid_call; /* I really want these to fail. */
772 cp = g_new_consumer(gp);
774 error = read_metadata(cp, &md);
776 g_destroy_consumer(cp);
782 if (strcmp(md.md_magic, G_VIRSTOR_MAGIC) != 0)
784 if (md.md_version != G_VIRSTOR_VERSION) {
785 LOG_MSG(LVL_ERROR, "Kernel module version invalid "
786 "to handle %s (%s) : %d should be %d",
787 md.md_name, pp->name, md.md_version, G_VIRSTOR_VERSION);
790 if (md.provsize != pp->mediasize)
793 /* If the provider name is hardcoded, use the offered provider only
794 * if it's been offered with its proper name (the one used in
795 * the label command). */
796 if (md.provider[0] != '\0') {
797 if (strcmp(md.provider, pp->name) != 0)
801 /* Iterate all geoms this class already knows about to see if a new
802 * geom instance of this class needs to be created (in case the provider
803 * is first from a (possibly) multi-consumer geom) or it just needs
804 * to be added to an existing instance. */
807 LIST_FOREACH(gp, &mp->geom, geom) {
811 if (strcmp(md.md_name, sc->geom->name) != 0)
813 if (md.md_id != sc->id)
817 if (gp != NULL) { /* We found an existing geom instance; add to it */
818 LOG_MSG(LVL_INFO, "Adding %s to %s", pp->name, md.md_name);
819 error = add_provider_to_geom(sc, pp, &md);
821 LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
822 pp->name, md.md_name, error);
825 } else { /* New geom instance needs to be created */
826 gp = create_virstor_geom(mp, &md);
828 LOG_MSG(LVL_ERROR, "Error creating new instance of "
829 "class %s: %s", mp->name, md.md_name);
830 LOG_MSG(LVL_DEBUG, "Error creating %s at %s",
831 md.md_name, pp->name);
835 LOG_MSG(LVL_INFO, "Adding %s to %s (first found)", pp->name,
837 error = add_provider_to_geom(sc, pp, &md);
839 LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
840 pp->name, md.md_name, error);
841 virstor_geom_destroy(sc, TRUE, FALSE);
850 * Destroyes consumer passed to it in arguments. Used as a callback
854 delay_destroy_consumer(void *arg, int flags __unused)
856 struct g_consumer *c = arg;
857 KASSERT(c != NULL, ("%s: invalid consumer", __func__));
858 LOG_MSG(LVL_DEBUG, "Consumer %s destroyed with delay",
861 g_destroy_consumer(c);
865 * Remove a component (consumer) from geom instance; If it's the first
866 * component being removed, orphan the provider to announce geom's being
870 remove_component(struct g_virstor_softc *sc, struct g_virstor_component *comp,
873 struct g_consumer *c;
875 KASSERT(comp->gcons != NULL, ("Component with no consumer in %s",
880 KASSERT(c->provider != NULL, ("%s: no provider", __func__));
881 LOG_MSG(LVL_DEBUG, "Component %s removed from %s", c->provider->name,
883 if (sc->provider != NULL) {
885 sc->provider->flags |= G_PF_WITHER;
886 g_orphan_provider(sc->provider, ENXIO);
888 LOG_MSG(LVL_INFO, "Removing provider %s", sc->geom->name);
891 if (c->acr > 0 || c->acw > 0 || c->ace > 0)
892 g_access(c, -c->acr, -c->acw, -c->ace);
894 /* Destroy consumer after it's tasted */
895 g_post_event(delay_destroy_consumer, c, M_WAITOK, NULL);
898 g_destroy_consumer(c);
903 * Destroy geom - called internally
904 * See g_virstor_destroy_geom for the other one
907 virstor_geom_destroy(struct g_virstor_softc *sc, boolean_t force,
910 struct g_provider *pp;
920 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
921 LOG_MSG(force ? LVL_WARNING : LVL_ERROR,
922 "Device %s is still open.", pp->name);
927 for (n = 0; n < sc->n_components; n++) {
928 if (sc->components[n].gcons != NULL)
929 remove_component(sc, &sc->components[n], delay);
935 KASSERT(sc->provider == NULL, ("Provider still exists for %s",
938 /* XXX: This might or might not work, since we're called with
939 * the topology lock held. Also, it might panic the kernel if
940 * the error'd BIO is in softupdates code. */
941 mtx_lock(&sc->delayed_bio_q_mtx);
942 while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
943 struct g_virstor_bio_q *bq;
944 bq = STAILQ_FIRST(&sc->delayed_bio_q);
945 bq->bio->bio_error = ENOSPC;
946 g_io_deliver(bq->bio, EIO);
947 STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
948 free(bq, M_GVIRSTOR);
950 mtx_unlock(&sc->delayed_bio_q_mtx);
951 mtx_destroy(&sc->delayed_bio_q_mtx);
953 free(sc->map, M_GVIRSTOR);
954 free(sc->components, M_GVIRSTOR);
955 bzero(sc, sizeof *sc);
956 free(sc, M_GVIRSTOR);
958 pp = LIST_FIRST(&gp->provider); /* We only offer one provider */
959 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
960 LOG_MSG(LVL_DEBUG, "Device %s destroyed", gp->name);
962 g_wither_geom(gp, ENXIO);
968 * Utility function: read metadata & decode. Wants topology lock to be
972 read_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
974 struct g_provider *pp;
979 error = g_access(cp, 1, 0, 0);
984 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
987 g_access(cp, -1, 0, 0);
991 virstor_metadata_decode(buf, md);
998 * Utility function: encode & write metadata. Assumes topology lock is
1002 write_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
1004 struct g_provider *pp;
1008 KASSERT(cp != NULL && md != NULL && cp->provider != NULL,
1009 ("Something's fishy in %s", __func__));
1010 LOG_MSG(LVL_DEBUG, "Writing metadata on %s", cp->provider->name);
1011 g_topology_assert();
1012 error = g_access(cp, 0, 1, 0);
1017 buf = malloc(pp->sectorsize, M_GVIRSTOR, M_WAITOK);
1018 virstor_metadata_encode(md, buf);
1019 g_topology_unlock();
1020 error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf,
1023 g_access(cp, 0, -1, 0);
1025 free(buf, M_GVIRSTOR);
1030 * Creates a new instance of this GEOM class, initialise softc
1032 static struct g_geom *
1033 create_virstor_geom(struct g_class *mp, struct g_virstor_metadata *md)
1036 struct g_virstor_softc *sc;
1038 LOG_MSG(LVL_DEBUG, "Creating geom instance for %s (id=%u)",
1039 md->md_name, md->md_id);
1041 if (md->md_count < 1 || md->md_chunk_size < 1 ||
1042 md->md_virsize < md->md_chunk_size) {
1043 /* This is bogus configuration, and probably means data is
1044 * somehow corrupted. Panic, maybe? */
1045 LOG_MSG(LVL_ERROR, "Nonsensical metadata information for %s",
1050 /* Check if it's already created */
1051 LIST_FOREACH(gp, &mp->geom, geom) {
1053 if (sc != NULL && strcmp(sc->geom->name, md->md_name) == 0) {
1054 LOG_MSG(LVL_WARNING, "Geom %s already exists",
1056 if (sc->id != md->md_id) {
1058 "Some stale or invalid components "
1059 "exist for virstor device named %s. "
1060 "You will need to <CLEAR> all stale "
1061 "components and maybe reconfigure "
1062 "the virstor device. Tune "
1063 "kern.geom.virstor.debug sysctl up "
1064 "for more information.",
1070 gp = g_new_geomf(mp, "%s", md->md_name);
1071 gp->softc = NULL; /* to circumevent races that test softc */
1073 gp->start = g_virstor_start;
1074 gp->spoiled = g_virstor_orphan;
1075 gp->orphan = g_virstor_orphan;
1076 gp->access = g_virstor_access;
1077 gp->dumpconf = g_virstor_dumpconf;
1079 sc = malloc(sizeof(*sc), M_GVIRSTOR, M_WAITOK | M_ZERO);
1081 sc->n_components = md->md_count;
1082 sc->components = malloc(sizeof(struct g_virstor_component) * md->md_count,
1083 M_GVIRSTOR, M_WAITOK | M_ZERO);
1084 sc->chunk_size = md->md_chunk_size;
1085 sc->virsize = md->md_virsize;
1086 STAILQ_INIT(&sc->delayed_bio_q);
1087 mtx_init(&sc->delayed_bio_q_mtx, "gvirstor_delayed_bio_q_mtx",
1088 "gvirstor", MTX_DEF | MTX_RECURSE);
1091 sc->provider = NULL; /* virstor_check_and_run will create it */
1094 LOG_MSG(LVL_ANNOUNCE, "Device %s created", sc->geom->name);
1100 * Add provider to a GEOM class instance
1103 add_provider_to_geom(struct g_virstor_softc *sc, struct g_provider *pp,
1104 struct g_virstor_metadata *md)
1106 struct g_virstor_component *component;
1107 struct g_consumer *cp, *fcp;
1111 if (md->no >= sc->n_components)
1114 /* "Current" compontent */
1115 component = &(sc->components[md->no]);
1116 if (component->gcons != NULL)
1120 fcp = LIST_FIRST(&gp->consumer);
1122 cp = g_new_consumer(gp);
1123 error = g_attach(cp, pp);
1126 g_destroy_consumer(cp);
1131 if (fcp->provider->sectorsize != pp->sectorsize) {
1132 /* TODO: this can be made to work */
1133 LOG_MSG(LVL_ERROR, "Provider %s of %s has invalid "
1134 "sector size (%d)", pp->name, sc->geom->name,
1138 if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) {
1139 /* Replicate access permissions from first "live" consumer
1141 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
1144 g_destroy_consumer(cp);
1150 /* Bring up a new component */
1151 cp->private = component;
1152 component->gcons = cp;
1154 component->index = md->no;
1155 component->chunk_count = md->chunk_count;
1156 component->chunk_next = md->chunk_next;
1157 component->chunk_reserved = md->chunk_reserved;
1158 component->flags = md->flags;
1160 LOG_MSG(LVL_DEBUG, "%s attached to %s", pp->name, sc->geom->name);
1162 virstor_check_and_run(sc);
1167 * Check if everything's ready to create the geom provider & device entry,
1168 * create and start provider.
1169 * Called ultimately by .taste, from g_event thread
1172 virstor_check_and_run(struct g_virstor_softc *sc)
1179 if (virstor_valid_components(sc) != sc->n_components)
1182 if (virstor_valid_components(sc) == 0) {
1183 /* This is actually a candidate for panic() */
1184 LOG_MSG(LVL_ERROR, "No valid components for %s?",
1185 sc->provider->name);
1189 sc->sectorsize = sc->components[0].gcons->provider->sectorsize;
1191 /* Initialise allocation map from the first consumer */
1192 sc->chunk_count = sc->virsize / sc->chunk_size;
1193 if (sc->chunk_count * (off_t)sc->chunk_size != sc->virsize) {
1194 LOG_MSG(LVL_WARNING, "Device %s truncated to %ju bytes",
1196 sc->chunk_count * (off_t)sc->chunk_size);
1198 sc->map_size = sc->chunk_count * sizeof *(sc->map);
1199 /* The following allocation is in order of 4MB - 8MB */
1200 sc->map = malloc(sc->map_size, M_GVIRSTOR, M_WAITOK);
1201 KASSERT(sc->map != NULL, ("%s: Memory allocation error (%zu bytes) for %s",
1202 __func__, sc->map_size, sc->provider->name));
1203 sc->map_sectors = sc->map_size / sc->sectorsize;
1206 for (n = 0; n < sc->n_components; n++)
1207 count += sc->components[n].chunk_count;
1208 LOG_MSG(LVL_INFO, "Device %s has %zu physical chunks and %zu virtual "
1210 sc->geom->name, count, sc->chunk_count, sc->chunk_size / 1024);
1212 error = g_access(sc->components[0].gcons, 1, 0, 0);
1214 LOG_MSG(LVL_ERROR, "Cannot acquire read access for %s to "
1215 "read allocation map for %s",
1216 sc->components[0].gcons->provider->name,
1220 /* Read in the allocation map */
1221 LOG_MSG(LVL_DEBUG, "Reading map for %s from %s", sc->geom->name,
1222 sc->components[0].gcons->provider->name);
1223 off = count = n = 0;
1224 while (count < sc->map_size) {
1225 struct g_virstor_map_entry *mapbuf;
1228 bs = MIN(MAXPHYS, sc->map_size - count);
1229 if (bs % sc->sectorsize != 0) {
1230 /* Check for alignment errors */
1231 bs = (bs / sc->sectorsize) * sc->sectorsize;
1234 LOG_MSG(LVL_ERROR, "Trouble: map is not sector-aligned "
1235 "for %s on %s", sc->geom->name,
1236 sc->components[0].gcons->provider->name);
1238 mapbuf = g_read_data(sc->components[0].gcons, off, bs, &error);
1239 if (mapbuf == NULL) {
1240 free(sc->map, M_GVIRSTOR);
1241 LOG_MSG(LVL_ERROR, "Error reading allocation map "
1242 "for %s from %s (offset %ju) (error %d)",
1244 sc->components[0].gcons->provider->name,
1249 bcopy(mapbuf, &sc->map[n], bs);
1252 n += bs / sizeof *(sc->map);
1255 g_access(sc->components[0].gcons, -1, 0, 0);
1256 LOG_MSG(LVL_DEBUG, "Read map for %s", sc->geom->name);
1258 /* find first component with allocatable chunks */
1260 for (n = 0; n < sc->n_components; n++) {
1261 if (sc->components[n].chunk_next <
1262 sc->components[n].chunk_count) {
1268 /* not found? set it to the last component and handle it
1270 index = sc->n_components - 1;
1272 if (index >= sc->n_components - g_virstor_component_watermark - 1) {
1273 LOG_MSG(LVL_WARNING, "Device %s running out of components "
1274 "(%d/%u: %s)", sc->geom->name,
1277 sc->components[index].gcons->provider->name);
1279 sc->curr_component = index;
1281 if (sc->components[index].chunk_next >=
1282 sc->components[index].chunk_count - g_virstor_chunk_watermark) {
1283 LOG_MSG(LVL_WARNING,
1284 "Component %s of %s is running out of free space "
1286 sc->components[index].gcons->provider->name,
1287 sc->geom->name, sc->components[index].chunk_count -
1288 sc->components[index].chunk_next);
1291 sc->me_per_sector = sc->sectorsize / sizeof *(sc->map);
1292 if (sc->sectorsize % sizeof *(sc->map) != 0) {
1294 "%s: Map entries don't fit exactly in a sector (%s)",
1295 __func__, sc->geom->name);
1299 /* Recalculate allocated chunks in components & at the same time
1300 * verify map data is sane. We could trust metadata on this, but
1301 * we want to make sure. */
1302 for (n = 0; n < sc->n_components; n++)
1303 sc->components[n].chunk_next = sc->components[n].chunk_reserved;
1305 for (n = 0; n < sc->chunk_count; n++) {
1306 if (sc->map[n].provider_no >= sc->n_components ||
1307 sc->map[n].provider_chunk >=
1308 sc->components[sc->map[n].provider_no].chunk_count) {
1309 LOG_MSG(LVL_ERROR, "%s: Invalid entry %u in map for %s",
1310 __func__, (u_int)n, sc->geom->name);
1311 LOG_MSG(LVL_ERROR, "%s: provider_no: %u, n_components: %u"
1312 " provider_chunk: %u, chunk_count: %u", __func__,
1313 sc->map[n].provider_no, sc->n_components,
1314 sc->map[n].provider_chunk,
1315 sc->components[sc->map[n].provider_no].chunk_count);
1318 if (sc->map[n].flags & VIRSTOR_MAP_ALLOCATED)
1319 sc->components[sc->map[n].provider_no].chunk_next++;
1322 sc->provider = g_new_providerf(sc->geom, "virstor/%s",
1325 sc->provider->sectorsize = sc->sectorsize;
1326 sc->provider->mediasize = sc->virsize;
1327 g_error_provider(sc->provider, 0);
1329 LOG_MSG(LVL_INFO, "%s activated", sc->provider->name);
1330 LOG_MSG(LVL_DEBUG, "%s starting with current component %u, starting "
1331 "chunk %u", sc->provider->name, sc->curr_component,
1332 sc->components[sc->curr_component].chunk_next);
1336 * Returns count of active providers in this geom instance
1339 virstor_valid_components(struct g_virstor_softc *sc)
1344 KASSERT(sc != NULL, ("%s: softc is NULL", __func__));
1345 KASSERT(sc->components != NULL, ("%s: sc->components is NULL", __func__));
1346 for (i = 0; i < sc->n_components; i++)
1347 if (sc->components[i].gcons != NULL)
1353 * Called when the consumer gets orphaned (?)
1356 g_virstor_orphan(struct g_consumer *cp)
1358 struct g_virstor_softc *sc;
1359 struct g_virstor_component *comp;
1362 g_topology_assert();
1369 KASSERT(comp != NULL, ("%s: No component in private part of consumer",
1371 remove_component(sc, comp, FALSE);
1372 if (virstor_valid_components(sc) == 0)
1373 virstor_geom_destroy(sc, TRUE, FALSE);
1377 * Called to notify geom when it's been opened, and for what intent
1380 g_virstor_access(struct g_provider *pp, int dr, int dw, int de)
1382 struct g_consumer *c;
1383 struct g_virstor_softc *sc;
1387 KASSERT(pp != NULL, ("%s: NULL provider", __func__));
1389 KASSERT(gp != NULL, ("%s: NULL geom", __func__));
1393 /* It seems that .access can be called with negative dr,dw,dx
1394 * in this case but I want to check for myself */
1395 LOG_MSG(LVL_WARNING, "access(%d, %d, %d) for %s",
1396 dr, dw, de, pp->name);
1397 /* This should only happen when geom is withered so
1398 * allow only negative requests */
1399 KASSERT(dr <= 0 && dw <= 0 && de <= 0,
1400 ("%s: Positive access for %s", __func__, pp->name));
1401 if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
1402 LOG_MSG(LVL_DEBUG, "Device %s definitely destroyed",
1407 /* Grab an exclusive bit to propagate on our consumers on first open */
1408 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1410 /* ... drop it on close */
1411 if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0) {
1413 update_metadata(sc); /* Writes statistical information */
1417 LIST_FOREACH(c, &gp->consumer, consumer) {
1418 KASSERT(c != NULL, ("%s: consumer is NULL", __func__));
1419 error = g_access(c, dr, dw, de);
1421 struct g_consumer *c2;
1423 /* Backout earlier changes */
1424 LIST_FOREACH(c2, &gp->consumer, consumer) {
1425 if (c2 == c) /* all eariler components fixed */
1427 g_access(c2, -dr, -dw, -de);
1436 * Generate XML dump of current state
1439 g_virstor_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1440 struct g_consumer *cp, struct g_provider *pp)
1442 struct g_virstor_softc *sc;
1444 g_topology_assert();
1447 if (sc == NULL || pp != NULL)
1451 /* For each component */
1452 struct g_virstor_component *comp;
1457 sbuf_printf(sb, "%s<ComponentIndex>%u</ComponentIndex>\n",
1458 indent, comp->index);
1459 sbuf_printf(sb, "%s<ChunkCount>%u</ChunkCount>\n",
1460 indent, comp->chunk_count);
1461 sbuf_printf(sb, "%s<ChunksUsed>%u</ChunksUsed>\n",
1462 indent, comp->chunk_next);
1463 sbuf_printf(sb, "%s<ChunksReserved>%u</ChunksReserved>\n",
1464 indent, comp->chunk_reserved);
1465 sbuf_printf(sb, "%s<StorageFree>%u%%</StorageFree>\n",
1467 comp->chunk_next > 0 ? 100 -
1468 ((comp->chunk_next + comp->chunk_reserved) * 100) /
1469 comp->chunk_count : 100);
1471 /* For the whole thing */
1472 u_int count, used, i;
1475 count = used = size = 0;
1476 for (i = 0; i < sc->n_components; i++) {
1477 if (sc->components[i].gcons != NULL) {
1478 count += sc->components[i].chunk_count;
1479 used += sc->components[i].chunk_next +
1480 sc->components[i].chunk_reserved;
1481 size += sc->components[i].gcons->
1482 provider->mediasize;
1486 sbuf_printf(sb, "%s<Status>"
1487 "Components=%u, Online=%u</Status>\n", indent,
1488 sc->n_components, virstor_valid_components(sc));
1489 sbuf_printf(sb, "%s<State>%u%% physical free</State>\n",
1490 indent, 100-(used * 100) / count);
1491 sbuf_printf(sb, "%s<ChunkSize>%zu</ChunkSize>\n", indent,
1493 sbuf_printf(sb, "%s<PhysicalFree>%u%%</PhysicalFree>\n",
1494 indent, used > 0 ? 100 - (used * 100) / count : 100);
1495 sbuf_printf(sb, "%s<ChunkPhysicalCount>%u</ChunkPhysicalCount>\n",
1497 sbuf_printf(sb, "%s<ChunkVirtualCount>%zu</ChunkVirtualCount>\n",
1498 indent, sc->chunk_count);
1499 sbuf_printf(sb, "%s<PhysicalBacking>%zu%%</PhysicalBacking>\n",
1501 (count * 100) / sc->chunk_count);
1502 sbuf_printf(sb, "%s<PhysicalBackingSize>%jd</PhysicalBackingSize>\n",
1504 sbuf_printf(sb, "%s<VirtualSize>%jd</VirtualSize>\n", indent,
1510 * GEOM .done handler
1511 * Can't use standard handler because one requested IO may
1512 * fork into additional data IOs
1515 g_virstor_done(struct bio *b)
1517 struct g_virstor_softc *sc;
1518 struct bio *parent_b;
1520 parent_b = b->bio_parent;
1521 sc = parent_b->bio_to->geom->softc;
1523 if (b->bio_error != 0) {
1524 LOG_MSG(LVL_ERROR, "Error %d for offset=%ju, length=%ju, %s",
1525 b->bio_error, b->bio_offset, b->bio_length,
1527 if (parent_b->bio_error == 0)
1528 parent_b->bio_error = b->bio_error;
1531 parent_b->bio_inbed++;
1532 parent_b->bio_completed += b->bio_completed;
1534 if (parent_b->bio_children == parent_b->bio_inbed) {
1535 parent_b->bio_completed = parent_b->bio_length;
1536 g_io_deliver(parent_b, parent_b->bio_error);
1543 * Called in g_down thread
1546 g_virstor_start(struct bio *b)
1548 struct g_virstor_softc *sc;
1549 struct g_virstor_component *comp;
1551 struct g_provider *pp;
1553 off_t offset, length;
1554 struct bio_queue_head bq;
1555 size_t chunk_size; /* cached for convenience */
1559 sc = pp->geom->softc;
1560 KASSERT(sc != NULL, ("%s: no softc (error=%d, device=%s)", __func__,
1561 b->bio_to->error, b->bio_to->name));
1563 LOG_REQ(LVL_MOREDEBUG, b, "%s", __func__);
1565 switch (b->bio_cmd) {
1571 g_io_deliver(b, EOPNOTSUPP);
1575 LOG_MSG(LVL_DEBUG2, "BIO arrived, size=%ju", b->bio_length);
1578 chunk_size = sc->chunk_size;
1580 offset = b->bio_offset; /* virtual offset and length */
1581 length = b->bio_length;
1583 while (length > 0) {
1584 size_t chunk_index, in_chunk_offset, in_chunk_length;
1585 struct virstor_map_entry *me;
1587 chunk_index = offset / chunk_size; /* round downwards */
1588 in_chunk_offset = offset % chunk_size;
1589 in_chunk_length = min(length, chunk_size - in_chunk_offset);
1590 LOG_MSG(LVL_DEBUG, "Mapped %s(%ju, %ju) to (%zu,%zu,%zu)",
1591 b->bio_cmd == BIO_READ ? "R" : "W",
1593 chunk_index, in_chunk_offset, in_chunk_length);
1594 me = &sc->map[chunk_index];
1596 if (b->bio_cmd == BIO_READ || b->bio_cmd == BIO_DELETE) {
1597 if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
1598 /* Reads from unallocated chunks return zeroed
1600 if (b->bio_cmd == BIO_READ)
1601 bzero(addr, in_chunk_length);
1603 comp = &sc->components[me->provider_no];
1605 cb = g_clone_bio(b);
1607 bioq_dismantle(&bq);
1608 if (b->bio_error == 0)
1609 b->bio_error = ENOMEM;
1610 g_io_deliver(b, b->bio_error);
1613 cb->bio_to = comp->gcons->provider;
1614 cb->bio_done = g_virstor_done;
1616 (off_t)me->provider_chunk * (off_t)chunk_size
1618 cb->bio_length = in_chunk_length;
1619 cb->bio_data = addr;
1620 cb->bio_caller1 = comp;
1621 bioq_disksort(&bq, cb);
1623 } else { /* handle BIO_WRITE */
1624 KASSERT(b->bio_cmd == BIO_WRITE,
1625 ("%s: Unknown command %d", __func__,
1628 if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
1629 /* We have a virtual chunk, represented by
1630 * the "me" entry, but it's not yet allocated
1631 * (tied to) a physical chunk. So do it now. */
1632 struct virstor_map_entry *data_me;
1633 u_int phys_chunk, comp_no;
1637 error = allocate_chunk(sc, &comp, &comp_no,
1640 /* We cannot allocate a physical chunk
1641 * to satisfy this request, so we'll
1642 * delay it to when we can...
1643 * XXX: this will prevent the fs from
1644 * being umounted! */
1645 struct g_virstor_bio_q *biq;
1646 biq = malloc(sizeof *biq, M_GVIRSTOR,
1649 bioq_dismantle(&bq);
1650 if (b->bio_error == 0)
1651 b->bio_error = ENOMEM;
1652 g_io_deliver(b, b->bio_error);
1656 mtx_lock(&sc->delayed_bio_q_mtx);
1657 STAILQ_INSERT_TAIL(&sc->delayed_bio_q,
1659 mtx_unlock(&sc->delayed_bio_q_mtx);
1660 LOG_MSG(LVL_WARNING, "Delaying BIO "
1661 "(size=%ju) until free physical "
1662 "space can be found on %s",
1664 sc->provider->name);
1667 LOG_MSG(LVL_DEBUG, "Allocated chunk %u on %s "
1670 comp->gcons->provider->name,
1671 sc->provider->name);
1673 me->provider_no = comp_no;
1674 me->provider_chunk = phys_chunk;
1675 me->flags |= VIRSTOR_MAP_ALLOCATED;
1677 cb = g_clone_bio(b);
1679 me->flags &= ~VIRSTOR_MAP_ALLOCATED;
1680 me->provider_no = 0;
1681 me->provider_chunk = 0;
1682 bioq_dismantle(&bq);
1683 if (b->bio_error == 0)
1684 b->bio_error = ENOMEM;
1685 g_io_deliver(b, b->bio_error);
1689 /* The allocation table is stored continuously
1690 * at the start of the drive. We need to
1691 * calculate the offset of the sector that holds
1692 * this map entry both on the drive and in the
1694 * sc_offset will end up pointing to the drive
1696 s_offset = chunk_index * sizeof *me;
1697 s_offset = (s_offset / sc->sectorsize) *
1700 /* data_me points to map entry sector
1701 * in memory (analoguos to offset) */
1702 data_me = &sc->map[(chunk_index /
1703 sc->me_per_sector) * sc->me_per_sector];
1705 /* Commit sector with map entry to storage */
1706 cb->bio_to = sc->components[0].gcons->provider;
1707 cb->bio_done = g_virstor_done;
1708 cb->bio_offset = s_offset;
1709 cb->bio_data = (char *)data_me;
1710 cb->bio_length = sc->sectorsize;
1711 cb->bio_caller1 = &sc->components[0];
1712 bioq_disksort(&bq, cb);
1715 comp = &sc->components[me->provider_no];
1716 cb = g_clone_bio(b);
1718 bioq_dismantle(&bq);
1719 if (b->bio_error == 0)
1720 b->bio_error = ENOMEM;
1721 g_io_deliver(b, b->bio_error);
1724 /* Finally, handle the data */
1725 cb->bio_to = comp->gcons->provider;
1726 cb->bio_done = g_virstor_done;
1727 cb->bio_offset = (off_t)me->provider_chunk*(off_t)chunk_size +
1729 cb->bio_length = in_chunk_length;
1730 cb->bio_data = addr;
1731 cb->bio_caller1 = comp;
1732 bioq_disksort(&bq, cb);
1734 addr += in_chunk_length;
1735 length -= in_chunk_length;
1736 offset += in_chunk_length;
1739 /* Fire off bio's here */
1741 for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) {
1742 bioq_remove(&bq, cb);
1743 LOG_REQ(LVL_MOREDEBUG, cb, "Firing request");
1744 comp = cb->bio_caller1;
1745 cb->bio_caller1 = NULL;
1746 LOG_MSG(LVL_DEBUG, " firing bio, offset=%ju, length=%ju",
1747 cb->bio_offset, cb->bio_length);
1748 g_io_request(cb, comp->gcons);
1751 if (count == 0) { /* We handled everything locally */
1752 b->bio_completed = b->bio_length;
1759 * Allocate a chunk from a physical provider. Returns physical component,
1760 * chunk index relative to the component and the component's index.
1763 allocate_chunk(struct g_virstor_softc *sc, struct g_virstor_component **comp,
1764 u_int *comp_no_p, u_int *chunk)
1768 KASSERT(sc->curr_component < sc->n_components,
1769 ("%s: Invalid curr_component: %u", __func__, sc->curr_component));
1771 comp_no = sc->curr_component;
1772 *comp = &sc->components[comp_no];
1773 dump_component(*comp);
1774 if ((*comp)->chunk_next >= (*comp)->chunk_count) {
1775 /* This component is full. Allocate next component */
1776 if (comp_no >= sc->n_components-1) {
1777 LOG_MSG(LVL_ERROR, "All physical space allocated for %s",
1781 (*comp)->flags &= ~VIRSTOR_PROVIDER_CURRENT;
1782 sc->curr_component = ++comp_no;
1784 *comp = &sc->components[comp_no];
1785 if (comp_no >= sc->n_components - g_virstor_component_watermark-1)
1786 LOG_MSG(LVL_WARNING, "Device %s running out of components "
1787 "(switching to %u/%u: %s)", sc->geom->name,
1788 comp_no+1, sc->n_components,
1789 (*comp)->gcons->provider->name);
1790 /* Take care not to overwrite reserved chunks */
1791 if ( (*comp)->chunk_reserved > 0 &&
1792 (*comp)->chunk_next < (*comp)->chunk_reserved)
1793 (*comp)->chunk_next = (*comp)->chunk_reserved;
1796 VIRSTOR_PROVIDER_ALLOCATED | VIRSTOR_PROVIDER_CURRENT;
1797 dump_component(*comp);
1798 *comp_no_p = comp_no;
1799 *chunk = (*comp)->chunk_next++;
1801 *comp_no_p = comp_no;
1802 *chunk = (*comp)->chunk_next++;
1807 /* Dump a component */
1809 dump_component(struct g_virstor_component *comp)
1812 if (g_virstor_debug < LVL_DEBUG2)
1814 printf("Component %d: %s\n", comp->index, comp->gcons->provider->name);
1815 printf(" chunk_count: %u\n", comp->chunk_count);
1816 printf(" chunk_next: %u\n", comp->chunk_next);
1817 printf(" flags: %u\n", comp->flags);
1821 /* Dump a map entry */
1823 dump_me(struct virstor_map_entry *me, unsigned int nr)
1825 if (g_virstor_debug < LVL_DEBUG)
1827 printf("VIRT. CHUNK #%d: ", nr);
1828 if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0)
1829 printf("(unallocated)\n");
1831 printf("allocated at provider %u, provider_chunk %u\n",
1832 me->provider_no, me->provider_chunk);
1837 * Dismantle bio_queue and destroy its components
1840 bioq_dismantle(struct bio_queue_head *bq)
1844 for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
1851 * The function that shouldn't be called.
1852 * When this is called, the stack is already garbled because of
1853 * argument mismatch. There's nothing to do now but panic, which is
1854 * accidentally the whole purpose of this function.
1855 * Motivation: to guard from accidentally calling geom methods when
1856 * they shouldn't be called. (see g_..._taste)
1861 panic("invalid_call() has just been called. Something's fishy here.");
1864 DECLARE_GEOM_CLASS(g_virstor_class, g_virstor); /* Let there be light */