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