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