1172302Spjd/*-
2172302Spjd * Copyright (c) 2006-2007 Ivan Voras <ivoras@freebsd.org>
3172302Spjd * All rights reserved.
4172302Spjd *
5172302Spjd * Redistribution and use in source and binary forms, with or without
6172302Spjd * modification, are permitted provided that the following conditions
7172302Spjd * are met:
8172302Spjd * 1. Redistributions of source code must retain the above copyright
9172302Spjd *    notice, this list of conditions and the following disclaimer.
10172302Spjd * 2. Redistributions in binary form must reproduce the above copyright
11172302Spjd *    notice, this list of conditions and the following disclaimer in the
12172302Spjd *    documentation and/or other materials provided with the distribution.
13172302Spjd *
14172302Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15172302Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16172302Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17172302Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18172302Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19172302Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20172302Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21172302Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22172302Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23172302Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24172302Spjd * SUCH DAMAGE.
25172302Spjd */
26172302Spjd
27172302Spjd/* Implementation notes:
28172302Spjd * - "Components" are wrappers around providers that make up the
29172302Spjd *   virtual storage (i.e. a virstor has "physical" components)
30172302Spjd */
31172302Spjd
32172302Spjd#include <sys/cdefs.h>
33172302Spjd__FBSDID("$FreeBSD$");
34172302Spjd
35172302Spjd#include <sys/param.h>
36172302Spjd#include <sys/systm.h>
37172302Spjd#include <sys/kernel.h>
38172302Spjd#include <sys/module.h>
39172302Spjd#include <sys/lock.h>
40172302Spjd#include <sys/mutex.h>
41172302Spjd#include <sys/sx.h>
42172302Spjd#include <sys/bio.h>
43223921Sae#include <sys/sbuf.h>
44172302Spjd#include <sys/sysctl.h>
45172302Spjd#include <sys/malloc.h>
46172302Spjd#include <sys/time.h>
47172302Spjd#include <sys/proc.h>
48172302Spjd#include <sys/kthread.h>
49172302Spjd#include <sys/mutex.h>
50172302Spjd#include <vm/uma.h>
51172302Spjd#include <geom/geom.h>
52172302Spjd
53172302Spjd#include <geom/virstor/g_virstor.h>
54172302Spjd#include <geom/virstor/g_virstor_md.h>
55172302Spjd
56219029SnetchildFEATURE(g_virstor, "GEOM virtual storage support");
57219029Snetchild
58172302Spjd/* Declare malloc(9) label */
59172302Spjdstatic MALLOC_DEFINE(M_GVIRSTOR, "gvirstor", "GEOM_VIRSTOR Data");
60172302Spjd
61172302Spjd/* GEOM class methods */
62172302Spjdstatic g_init_t g_virstor_init;
63172302Spjdstatic g_fini_t g_virstor_fini;
64172302Spjdstatic g_taste_t g_virstor_taste;
65172302Spjdstatic g_ctl_req_t g_virstor_config;
66172302Spjdstatic g_ctl_destroy_geom_t g_virstor_destroy_geom;
67172302Spjd
68172302Spjd/* Declare & initialize class structure ("geom class") */
69172302Spjdstruct g_class g_virstor_class = {
70172302Spjd	.name =		G_VIRSTOR_CLASS_NAME,
71172302Spjd	.version =	G_VERSION,
72172302Spjd	.init =		g_virstor_init,
73172302Spjd	.fini =		g_virstor_fini,
74172302Spjd	.taste =	g_virstor_taste,
75172302Spjd	.ctlreq =	g_virstor_config,
76172302Spjd	.destroy_geom = g_virstor_destroy_geom
77172302Spjd	/* The .dumpconf and the rest are only usable for a geom instance, so
78172302Spjd	 * they will be set when such instance is created. */
79172302Spjd};
80172302Spjd
81172302Spjd/* Declare sysctl's and loader tunables */
82172302SpjdSYSCTL_DECL(_kern_geom);
83227309Sedstatic SYSCTL_NODE(_kern_geom, OID_AUTO, virstor, CTLFLAG_RW, 0,
84227309Sed    "GEOM_GVIRSTOR information");
85172302Spjd
86172302Spjdstatic u_int g_virstor_debug = 2; /* XXX: lower to 2 when released to public */
87172302SpjdTUNABLE_INT("kern.geom.virstor.debug", &g_virstor_debug);
88172302SpjdSYSCTL_UINT(_kern_geom_virstor, OID_AUTO, debug, CTLFLAG_RW, &g_virstor_debug,
89172302Spjd    0, "Debug level (2=production, 5=normal, 15=excessive)");
90172302Spjd
91172302Spjdstatic u_int g_virstor_chunk_watermark = 100;
92172302SpjdTUNABLE_INT("kern.geom.virstor.chunk_watermark", &g_virstor_chunk_watermark);
93172302SpjdSYSCTL_UINT(_kern_geom_virstor, OID_AUTO, chunk_watermark, CTLFLAG_RW,
94172302Spjd    &g_virstor_chunk_watermark, 0,
95172302Spjd    "Minimum number of free chunks before issuing administrative warning");
96172302Spjd
97172302Spjdstatic u_int g_virstor_component_watermark = 1;
98172302SpjdTUNABLE_INT("kern.geom.virstor.component_watermark",
99172302Spjd    &g_virstor_component_watermark);
100172302SpjdSYSCTL_UINT(_kern_geom_virstor, OID_AUTO, component_watermark, CTLFLAG_RW,
101172302Spjd    &g_virstor_component_watermark, 0,
102172302Spjd    "Minimum number of free components before issuing administrative warning");
103172302Spjd
104172302Spjdstatic int read_metadata(struct g_consumer *, struct g_virstor_metadata *);
105202987Sivorasstatic void write_metadata(struct g_consumer *, struct g_virstor_metadata *);
106172302Spjdstatic int clear_metadata(struct g_virstor_component *);
107172302Spjdstatic int add_provider_to_geom(struct g_virstor_softc *, struct g_provider *,
108172302Spjd    struct g_virstor_metadata *);
109172302Spjdstatic struct g_geom *create_virstor_geom(struct g_class *,
110172302Spjd    struct g_virstor_metadata *);
111172302Spjdstatic void virstor_check_and_run(struct g_virstor_softc *);
112172302Spjdstatic u_int virstor_valid_components(struct g_virstor_softc *);
113172302Spjdstatic int virstor_geom_destroy(struct g_virstor_softc *, boolean_t,
114172302Spjd    boolean_t);
115172302Spjdstatic void remove_component(struct g_virstor_softc *,
116172302Spjd    struct g_virstor_component *, boolean_t);
117172302Spjdstatic void bioq_dismantle(struct bio_queue_head *);
118172302Spjdstatic int allocate_chunk(struct g_virstor_softc *,
119172302Spjd    struct g_virstor_component **, u_int *, u_int *);
120172302Spjdstatic void delay_destroy_consumer(void *, int);
121172302Spjdstatic void dump_component(struct g_virstor_component *comp);
122172302Spjd#if 0
123172302Spjdstatic void dump_me(struct virstor_map_entry *me, unsigned int nr);
124172302Spjd#endif
125172302Spjd
126172302Spjdstatic void virstor_ctl_stop(struct gctl_req *, struct g_class *);
127172302Spjdstatic void virstor_ctl_add(struct gctl_req *, struct g_class *);
128172302Spjdstatic void virstor_ctl_remove(struct gctl_req *, struct g_class *);
129172302Spjdstatic struct g_virstor_softc * virstor_find_geom(const struct g_class *,
130172302Spjd    const char *);
131172302Spjdstatic void update_metadata(struct g_virstor_softc *);
132172302Spjdstatic void fill_metadata(struct g_virstor_softc *, struct g_virstor_metadata *,
133172302Spjd    u_int, u_int);
134172302Spjd
135172302Spjdstatic void g_virstor_orphan(struct g_consumer *);
136172302Spjdstatic int g_virstor_access(struct g_provider *, int, int, int);
137172302Spjdstatic void g_virstor_start(struct bio *);
138172302Spjdstatic void g_virstor_dumpconf(struct sbuf *, const char *, struct g_geom *,
139172302Spjd    struct g_consumer *, struct g_provider *);
140172302Spjdstatic void g_virstor_done(struct bio *);
141172302Spjd
142172302Spjdstatic void invalid_call(void);
143172302Spjd/*
144172302Spjd * Initialise GEOM class (per-class callback)
145172302Spjd */
146172302Spjdstatic void
147172302Spjdg_virstor_init(struct g_class *mp __unused)
148172302Spjd{
149172302Spjd
150172302Spjd	/* Catch map struct size mismatch at compile time; Map entries must
151172302Spjd	 * fit into MAXPHYS exactly, with no wasted space. */
152172302Spjd	CTASSERT(VIRSTOR_MAP_BLOCK_ENTRIES*VIRSTOR_MAP_ENTRY_SIZE == MAXPHYS);
153172302Spjd
154172302Spjd	/* Init UMA zones, TAILQ's, other global vars */
155172302Spjd}
156172302Spjd
157172302Spjd/*
158172302Spjd * Finalise GEOM class (per-class callback)
159172302Spjd */
160172302Spjdstatic void
161172302Spjdg_virstor_fini(struct g_class *mp __unused)
162172302Spjd{
163172302Spjd
164172302Spjd	/* Deinit UMA zones & global vars */
165172302Spjd}
166172302Spjd
167172302Spjd/*
168172302Spjd * Config (per-class callback)
169172302Spjd */
170172302Spjdstatic void
171172302Spjdg_virstor_config(struct gctl_req *req, struct g_class *cp, char const *verb)
172172302Spjd{
173172302Spjd	uint32_t *version;
174172302Spjd
175172302Spjd	g_topology_assert();
176172302Spjd
177172302Spjd	version = gctl_get_paraml(req, "version", sizeof(*version));
178172302Spjd	if (version == NULL) {
179172302Spjd		gctl_error(req, "Failed to get 'version' argument");
180172302Spjd		return;
181172302Spjd	}
182172302Spjd	if (*version != G_VIRSTOR_VERSION) {
183172302Spjd		gctl_error(req, "Userland and kernel versions out of sync");
184172302Spjd		return;
185172302Spjd	}
186172302Spjd
187172302Spjd	g_topology_unlock();
188172302Spjd	if (strcmp(verb, "add") == 0)
189172302Spjd		virstor_ctl_add(req, cp);
190172302Spjd	else if (strcmp(verb, "stop") == 0 || strcmp(verb, "destroy") == 0)
191172302Spjd		virstor_ctl_stop(req, cp);
192172302Spjd	else if (strcmp(verb, "remove") == 0)
193172302Spjd		virstor_ctl_remove(req, cp);
194172302Spjd	else
195172302Spjd		gctl_error(req, "unknown verb: '%s'", verb);
196172302Spjd	g_topology_lock();
197172302Spjd}
198172302Spjd
199172302Spjd/*
200172302Spjd * "stop" verb from userland
201172302Spjd */
202172302Spjdstatic void
203172302Spjdvirstor_ctl_stop(struct gctl_req *req, struct g_class *cp)
204172302Spjd{
205172302Spjd	int *force, *nargs;
206172302Spjd	int i;
207172302Spjd
208172302Spjd	nargs = gctl_get_paraml(req, "nargs", sizeof *nargs);
209172302Spjd	if (nargs == NULL) {
210172302Spjd		gctl_error(req, "Error fetching argument '%s'", "nargs");
211172302Spjd		return;
212172302Spjd	}
213172302Spjd	if (*nargs < 1) {
214172302Spjd		gctl_error(req, "Invalid number of arguments");
215172302Spjd		return;
216172302Spjd	}
217172302Spjd	force = gctl_get_paraml(req, "force", sizeof *force);
218172302Spjd	if (force == NULL) {
219172302Spjd		gctl_error(req, "Error fetching argument '%s'", "force");
220172302Spjd		return;
221172302Spjd	}
222172302Spjd
223172302Spjd	g_topology_lock();
224172302Spjd	for (i = 0; i < *nargs; i++) {
225172302Spjd		char param[8];
226172302Spjd		const char *name;
227172302Spjd		struct g_virstor_softc *sc;
228172302Spjd		int error;
229172302Spjd
230172302Spjd		sprintf(param, "arg%d", i);
231172302Spjd		name = gctl_get_asciiparam(req, param);
232180120Sdelphij		if (name == NULL) {
233180120Sdelphij			gctl_error(req, "No 'arg%d' argument", i);
234180120Sdelphij			g_topology_unlock();
235180120Sdelphij			return;
236180120Sdelphij		}
237172302Spjd		sc = virstor_find_geom(cp, name);
238239021Sjimharris		if (sc == NULL) {
239239021Sjimharris			gctl_error(req, "Don't know anything about '%s'", name);
240239021Sjimharris			g_topology_unlock();
241239021Sjimharris			return;
242239021Sjimharris		}
243239021Sjimharris
244172302Spjd		LOG_MSG(LVL_INFO, "Stopping %s by the userland command",
245172302Spjd		    sc->geom->name);
246172302Spjd		update_metadata(sc);
247172302Spjd		if ((error = virstor_geom_destroy(sc, TRUE, TRUE)) != 0) {
248172302Spjd			LOG_MSG(LVL_ERROR, "Cannot destroy %s: %d",
249172302Spjd			    sc->geom->name, error);
250172302Spjd		}
251172302Spjd	}
252172302Spjd	g_topology_unlock();
253172302Spjd}
254172302Spjd
255172302Spjd/*
256172302Spjd * "add" verb from userland - add new component(s) to the structure.
257172302Spjd * This will be done all at once in here, without going through the
258172302Spjd * .taste function for new components.
259172302Spjd */
260172302Spjdstatic void
261172302Spjdvirstor_ctl_add(struct gctl_req *req, struct g_class *cp)
262172302Spjd{
263172302Spjd	/* Note: while this is going on, I/O is being done on
264172302Spjd	 * the g_up and g_down threads. The idea is to make changes
265172302Spjd	 * to softc members in a way that can atomically activate
266172302Spjd	 * them all at once. */
267172302Spjd	struct g_virstor_softc *sc;
268172302Spjd	int *hardcode, *nargs;
269172302Spjd	const char *geom_name;	/* geom to add a component to */
270172302Spjd	struct g_consumer *fcp;
271172302Spjd	struct g_virstor_bio_q *bq;
272172302Spjd	u_int added;
273172302Spjd	int error;
274172302Spjd	int i;
275172302Spjd
276172302Spjd	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
277172302Spjd	if (nargs == NULL) {
278172302Spjd		gctl_error(req, "Error fetching argument '%s'", "nargs");
279172302Spjd		return;
280172302Spjd	}
281172302Spjd	if (*nargs < 2) {
282172302Spjd		gctl_error(req, "Invalid number of arguments");
283172302Spjd		return;
284172302Spjd	}
285172302Spjd	hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
286172302Spjd	if (hardcode == NULL) {
287172302Spjd		gctl_error(req, "Error fetching argument '%s'", "hardcode");
288172302Spjd		return;
289172302Spjd	}
290172302Spjd
291172302Spjd	/* Find "our" geom */
292172302Spjd	geom_name = gctl_get_asciiparam(req, "arg0");
293172302Spjd	if (geom_name == NULL) {
294172302Spjd		gctl_error(req, "Error fetching argument '%s'", "geom_name (arg0)");
295172302Spjd		return;
296172302Spjd	}
297172302Spjd	sc = virstor_find_geom(cp, geom_name);
298172302Spjd	if (sc == NULL) {
299172302Spjd		gctl_error(req, "Don't know anything about '%s'", geom_name);
300172302Spjd		return;
301172302Spjd	}
302172302Spjd
303172302Spjd	if (virstor_valid_components(sc) != sc->n_components) {
304172302Spjd		LOG_MSG(LVL_ERROR, "Cannot add components to incomplete "
305172302Spjd		    "virstor %s", sc->geom->name);
306172302Spjd		gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
307172302Spjd		return;
308172302Spjd	}
309172302Spjd
310172302Spjd	fcp = sc->components[0].gcons;
311172302Spjd	added = 0;
312172302Spjd	g_topology_lock();
313172302Spjd	for (i = 1; i < *nargs; i++) {
314172302Spjd		struct g_virstor_metadata md;
315172302Spjd		char aname[8];
316172302Spjd		const char *prov_name;
317172302Spjd		struct g_provider *pp;
318172302Spjd		struct g_consumer *cp;
319172302Spjd		u_int nc;
320172302Spjd		u_int j;
321172302Spjd
322172302Spjd		snprintf(aname, sizeof aname, "arg%d", i);
323172302Spjd		prov_name = gctl_get_asciiparam(req, aname);
324203408Sdelphij		if (prov_name == NULL) {
325203408Sdelphij			gctl_error(req, "Error fetching argument '%s'", aname);
326203408Sdelphij			g_topology_unlock();
327203408Sdelphij			return;
328203408Sdelphij		}
329213662Sae		if (strncmp(prov_name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
330213662Sae			prov_name += sizeof(_PATH_DEV) - 1;
331172302Spjd
332172302Spjd		pp = g_provider_by_name(prov_name);
333172302Spjd		if (pp == NULL) {
334172302Spjd			/* This is the most common error so be verbose about it */
335172302Spjd			if (added != 0) {
336172302Spjd				gctl_error(req, "Invalid provider: '%s' (added"
337172302Spjd				    " %u components)", prov_name, added);
338172302Spjd				update_metadata(sc);
339172302Spjd			} else {
340172302Spjd				gctl_error(req, "Invalid provider: '%s'",
341172302Spjd				    prov_name);
342172302Spjd			}
343172302Spjd			g_topology_unlock();
344172302Spjd			return;
345172302Spjd		}
346172302Spjd		cp = g_new_consumer(sc->geom);
347172302Spjd		if (cp == NULL) {
348172302Spjd			gctl_error(req, "Cannot create consumer");
349172302Spjd			g_topology_unlock();
350172302Spjd			return;
351172302Spjd		}
352172302Spjd		error = g_attach(cp, pp);
353172302Spjd		if (error != 0) {
354172302Spjd			gctl_error(req, "Cannot attach a consumer to %s",
355172302Spjd			    pp->name);
356172302Spjd			g_destroy_consumer(cp);
357172302Spjd			g_topology_unlock();
358172302Spjd			return;
359172302Spjd		}
360172302Spjd		if (fcp->acr != 0 || fcp->acw != 0 || fcp->ace != 0) {
361172302Spjd			error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
362172302Spjd			if (error != 0) {
363172302Spjd				gctl_error(req, "Access request failed for %s",
364172302Spjd				    pp->name);
365172302Spjd				g_destroy_consumer(cp);
366172302Spjd				g_topology_unlock();
367172302Spjd				return;
368172302Spjd			}
369172302Spjd		}
370172302Spjd		if (fcp->provider->sectorsize != pp->sectorsize) {
371172302Spjd			gctl_error(req, "Sector size doesn't fit for %s",
372172302Spjd			    pp->name);
373172302Spjd			g_destroy_consumer(cp);
374172302Spjd			g_topology_unlock();
375172302Spjd			return;
376172302Spjd		}
377172302Spjd		for (j = 0; j < sc->n_components; j++) {
378172302Spjd			if (strcmp(sc->components[j].gcons->provider->name,
379172302Spjd			    pp->name) == 0) {
380172302Spjd				gctl_error(req, "Component %s already in %s",
381172302Spjd				    pp->name, sc->geom->name);
382172302Spjd				g_destroy_consumer(cp);
383172302Spjd				g_topology_unlock();
384172302Spjd				return;
385172302Spjd			}
386172302Spjd		}
387172302Spjd		sc->components = realloc(sc->components,
388172302Spjd		    sizeof(*sc->components) * (sc->n_components + 1),
389172302Spjd		    M_GVIRSTOR, M_WAITOK);
390172302Spjd
391172302Spjd		nc = sc->n_components;
392172302Spjd		sc->components[nc].gcons = cp;
393172302Spjd		sc->components[nc].sc = sc;
394172302Spjd		sc->components[nc].index = nc;
395172302Spjd		sc->components[nc].chunk_count = cp->provider->mediasize /
396172302Spjd		    sc->chunk_size;
397172302Spjd		sc->components[nc].chunk_next = 0;
398172302Spjd		sc->components[nc].chunk_reserved = 0;
399172302Spjd
400172302Spjd		if (sc->components[nc].chunk_count < 4) {
401172302Spjd			gctl_error(req, "Provider too small: %s",
402172302Spjd			    cp->provider->name);
403172302Spjd			g_destroy_consumer(cp);
404172302Spjd			g_topology_unlock();
405172302Spjd			return;
406172302Spjd		}
407172302Spjd		fill_metadata(sc, &md, nc, *hardcode);
408172302Spjd		write_metadata(cp, &md);
409172302Spjd		/* The new component becomes visible when n_components is
410172302Spjd		 * incremented */
411172302Spjd		sc->n_components++;
412172302Spjd		added++;
413172302Spjd
414172302Spjd	}
415172302Spjd	/* This call to update_metadata() is critical. In case there's a
416172302Spjd	 * power failure in the middle of it and some components are updated
417172302Spjd	 * while others are not, there will be trouble on next .taste() iff
418172302Spjd	 * a non-updated component is detected first */
419172302Spjd	update_metadata(sc);
420172302Spjd	g_topology_unlock();
421172302Spjd	LOG_MSG(LVL_INFO, "Added %d component(s) to %s", added,
422172302Spjd	    sc->geom->name);
423172302Spjd	/* Fire off BIOs previously queued because there wasn't any
424172302Spjd	 * physical space left. If the BIOs still can't be satisfied
425172302Spjd	 * they will again be added to the end of the queue (during
426172302Spjd	 * which the mutex will be recursed) */
427172302Spjd	bq = malloc(sizeof(*bq), M_GVIRSTOR, M_WAITOK);
428172302Spjd	bq->bio = NULL;
429172302Spjd	mtx_lock(&sc->delayed_bio_q_mtx);
430172302Spjd	/* First, insert a sentinel to the queue end, so we don't
431172302Spjd	 * end up in an infinite loop if there's still no free
432172302Spjd	 * space available. */
433172302Spjd	STAILQ_INSERT_TAIL(&sc->delayed_bio_q, bq, linkage);
434172302Spjd	while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
435172302Spjd		bq = STAILQ_FIRST(&sc->delayed_bio_q);
436172302Spjd		if (bq->bio != NULL) {
437172302Spjd			g_virstor_start(bq->bio);
438172302Spjd			STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
439172302Spjd			free(bq, M_GVIRSTOR);
440172302Spjd		} else {
441172302Spjd			STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
442172302Spjd			free(bq, M_GVIRSTOR);
443172302Spjd			break;
444172302Spjd		}
445172302Spjd	}
446172302Spjd	mtx_unlock(&sc->delayed_bio_q_mtx);
447172302Spjd
448172302Spjd}
449172302Spjd
450172302Spjd/*
451172302Spjd * Find a geom handled by the class
452172302Spjd */
453172302Spjdstatic struct g_virstor_softc *
454172302Spjdvirstor_find_geom(const struct g_class *cp, const char *name)
455172302Spjd{
456172302Spjd	struct g_geom *gp;
457172302Spjd
458172302Spjd	LIST_FOREACH(gp, &cp->geom, geom) {
459172302Spjd		if (strcmp(name, gp->name) == 0)
460172302Spjd			return (gp->softc);
461172302Spjd	}
462172302Spjd	return (NULL);
463172302Spjd}
464172302Spjd
465172302Spjd/*
466172302Spjd * Update metadata on all components to reflect the current state
467172302Spjd * of these fields:
468172302Spjd *    - chunk_next
469172302Spjd *    - flags
470172302Spjd *    - md_count
471172302Spjd * Expects things to be set up so write_metadata() can work, i.e.
472172302Spjd * the topology lock must be held.
473172302Spjd */
474172302Spjdstatic void
475172302Spjdupdate_metadata(struct g_virstor_softc *sc)
476172302Spjd{
477172302Spjd	struct g_virstor_metadata md;
478172302Spjd	int n;
479172302Spjd
480172302Spjd	if (virstor_valid_components(sc) != sc->n_components)
481172302Spjd		return; /* Incomplete device */
482172302Spjd	LOG_MSG(LVL_DEBUG, "Updating metadata on components for %s",
483172302Spjd	    sc->geom->name);
484172302Spjd	/* Update metadata on components */
485172302Spjd	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__,
486172302Spjd	    sc->geom->class->name, sc->geom->name);
487172302Spjd	g_topology_assert();
488172302Spjd	for (n = 0; n < sc->n_components; n++) {
489172302Spjd		read_metadata(sc->components[n].gcons, &md);
490172302Spjd		md.chunk_next = sc->components[n].chunk_next;
491172302Spjd		md.flags = sc->components[n].flags;
492172302Spjd		md.md_count = sc->n_components;
493172302Spjd		write_metadata(sc->components[n].gcons, &md);
494172302Spjd	}
495172302Spjd}
496172302Spjd
497172302Spjd/*
498172302Spjd * Fills metadata (struct md) from information stored in softc and the nc'th
499172302Spjd * component of virstor
500172302Spjd */
501172302Spjdstatic void
502172302Spjdfill_metadata(struct g_virstor_softc *sc, struct g_virstor_metadata *md,
503172302Spjd    u_int nc, u_int hardcode)
504172302Spjd{
505172302Spjd	struct g_virstor_component *c;
506172302Spjd
507172302Spjd	bzero(md, sizeof *md);
508172302Spjd	c = &sc->components[nc];
509172302Spjd
510172302Spjd	strncpy(md->md_magic, G_VIRSTOR_MAGIC, sizeof md->md_magic);
511172302Spjd	md->md_version = G_VIRSTOR_VERSION;
512172302Spjd	strncpy(md->md_name, sc->geom->name, sizeof md->md_name);
513172302Spjd	md->md_id = sc->id;
514172302Spjd	md->md_virsize = sc->virsize;
515172302Spjd	md->md_chunk_size = sc->chunk_size;
516172302Spjd	md->md_count = sc->n_components;
517172302Spjd
518172302Spjd	if (hardcode) {
519172302Spjd		strncpy(md->provider, c->gcons->provider->name,
520172302Spjd		    sizeof md->provider);
521172302Spjd	}
522172302Spjd	md->no = nc;
523172302Spjd	md->provsize = c->gcons->provider->mediasize;
524172302Spjd	md->chunk_count = c->chunk_count;
525172302Spjd	md->chunk_next = c->chunk_next;
526172302Spjd	md->chunk_reserved = c->chunk_reserved;
527172302Spjd	md->flags = c->flags;
528172302Spjd}
529172302Spjd
530172302Spjd/*
531172302Spjd * Remove a component from virstor device.
532172302Spjd * Can only be done if the component is unallocated.
533172302Spjd */
534172302Spjdstatic void
535172302Spjdvirstor_ctl_remove(struct gctl_req *req, struct g_class *cp)
536172302Spjd{
537172302Spjd	/* As this is executed in parallel to I/O, operations on virstor
538172302Spjd	 * structures must be as atomic as possible. */
539172302Spjd	struct g_virstor_softc *sc;
540172302Spjd	int *nargs;
541172302Spjd	const char *geom_name;
542172302Spjd	u_int removed;
543172302Spjd	int i;
544172302Spjd
545172302Spjd	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
546172302Spjd	if (nargs == NULL) {
547172302Spjd		gctl_error(req, "Error fetching argument '%s'", "nargs");
548172302Spjd		return;
549172302Spjd	}
550172302Spjd	if (*nargs < 2) {
551172302Spjd		gctl_error(req, "Invalid number of arguments");
552172302Spjd		return;
553172302Spjd	}
554172302Spjd	/* Find "our" geom */
555172302Spjd	geom_name = gctl_get_asciiparam(req, "arg0");
556172302Spjd	if (geom_name == NULL) {
557172302Spjd		gctl_error(req, "Error fetching argument '%s'",
558172302Spjd		    "geom_name (arg0)");
559172302Spjd		return;
560172302Spjd	}
561172302Spjd	sc = virstor_find_geom(cp, geom_name);
562172302Spjd	if (sc == NULL) {
563172302Spjd		gctl_error(req, "Don't know anything about '%s'", geom_name);
564172302Spjd		return;
565172302Spjd	}
566172302Spjd
567172302Spjd	if (virstor_valid_components(sc) != sc->n_components) {
568172302Spjd		LOG_MSG(LVL_ERROR, "Cannot remove components from incomplete "
569172302Spjd		    "virstor %s", sc->geom->name);
570172302Spjd		gctl_error(req, "Virstor %s is incomplete", sc->geom->name);
571172302Spjd		return;
572172302Spjd	}
573172302Spjd
574172302Spjd	removed = 0;
575172302Spjd	for (i = 1; i < *nargs; i++) {
576172302Spjd		char param[8];
577172302Spjd		const char *prov_name;
578172302Spjd		int j, found;
579172302Spjd		struct g_virstor_component *newcomp, *compbak;
580172302Spjd
581172302Spjd		sprintf(param, "arg%d", i);
582172302Spjd		prov_name = gctl_get_asciiparam(req, param);
583203408Sdelphij		if (prov_name == NULL) {
584203408Sdelphij			gctl_error(req, "Error fetching argument '%s'", param);
585203408Sdelphij			return;
586203408Sdelphij		}
587213662Sae		if (strncmp(prov_name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
588213662Sae			prov_name += sizeof(_PATH_DEV) - 1;
589172302Spjd
590172302Spjd		found = -1;
591172302Spjd		for (j = 0; j < sc->n_components; j++) {
592172302Spjd			if (strcmp(sc->components[j].gcons->provider->name,
593172302Spjd			    prov_name) == 0) {
594172302Spjd				found = j;
595172302Spjd				break;
596172302Spjd			}
597172302Spjd		}
598172302Spjd		if (found == -1) {
599172302Spjd			LOG_MSG(LVL_ERROR, "No %s component in %s",
600172302Spjd			    prov_name, sc->geom->name);
601172302Spjd			continue;
602172302Spjd		}
603172302Spjd
604172302Spjd		compbak = sc->components;
605172302Spjd		newcomp = malloc(sc->n_components * sizeof(*sc->components),
606172302Spjd		    M_GVIRSTOR, M_WAITOK | M_ZERO);
607172302Spjd		bcopy(sc->components, newcomp, found * sizeof(*sc->components));
608172302Spjd		bcopy(&sc->components[found + 1], newcomp + found,
609172302Spjd		    found * sizeof(*sc->components));
610172302Spjd		if ((sc->components[j].flags & VIRSTOR_PROVIDER_ALLOCATED) != 0) {
611172302Spjd			LOG_MSG(LVL_ERROR, "Allocated provider %s cannot be "
612172302Spjd			    "removed from %s",
613172302Spjd			    prov_name, sc->geom->name);
614172302Spjd			free(newcomp, M_GVIRSTOR);
615172302Spjd			/* We'll consider this non-fatal error */
616172302Spjd			continue;
617172302Spjd		}
618172302Spjd		/* Renumerate unallocated components */
619172302Spjd		for (j = 0; j < sc->n_components-1; j++) {
620172302Spjd			if ((sc->components[j].flags &
621172302Spjd			    VIRSTOR_PROVIDER_ALLOCATED) == 0) {
622172302Spjd				sc->components[j].index = j;
623172302Spjd			}
624172302Spjd		}
625172302Spjd		/* This is the critical section. If a component allocation
626172302Spjd		 * event happens while both variables are not yet set,
627172302Spjd		 * there will be trouble. Something will panic on encountering
628172302Spjd		 * NULL sc->components[x].gcomp member.
629172302Spjd		 * Luckily, component allocation happens very rarely and
630172302Spjd		 * removing components is an abnormal action in any case. */
631172302Spjd		sc->components = newcomp;
632172302Spjd		sc->n_components--;
633172302Spjd		/* End critical section */
634172302Spjd
635172302Spjd		g_topology_lock();
636172302Spjd		if (clear_metadata(&compbak[found]) != 0) {
637172302Spjd			LOG_MSG(LVL_WARNING, "Trouble ahead: cannot clear "
638172302Spjd			    "metadata on %s", prov_name);
639172302Spjd		}
640172302Spjd		g_detach(compbak[found].gcons);
641172302Spjd		g_destroy_consumer(compbak[found].gcons);
642172302Spjd		g_topology_unlock();
643172302Spjd
644172302Spjd		free(compbak, M_GVIRSTOR);
645172302Spjd
646172302Spjd		removed++;
647172302Spjd	}
648172302Spjd
649172302Spjd	/* This call to update_metadata() is critical. In case there's a
650172302Spjd	 * power failure in the middle of it and some components are updated
651172302Spjd	 * while others are not, there will be trouble on next .taste() iff
652172302Spjd	 * a non-updated component is detected first */
653172302Spjd	g_topology_lock();
654172302Spjd	update_metadata(sc);
655172302Spjd	g_topology_unlock();
656172302Spjd	LOG_MSG(LVL_INFO, "Removed %d component(s) from %s", removed,
657172302Spjd	    sc->geom->name);
658172302Spjd}
659172302Spjd
660172302Spjd/*
661172302Spjd * Clear metadata sector on component
662172302Spjd */
663172302Spjdstatic int
664172302Spjdclear_metadata(struct g_virstor_component *comp)
665172302Spjd{
666172302Spjd	char *buf;
667172302Spjd	int error;
668172302Spjd
669172302Spjd	LOG_MSG(LVL_INFO, "Clearing metadata on %s",
670172302Spjd	    comp->gcons->provider->name);
671172302Spjd	g_topology_assert();
672172302Spjd	error = g_access(comp->gcons, 0, 1, 0);
673172302Spjd	if (error != 0)
674172302Spjd		return (error);
675172302Spjd	buf = malloc(comp->gcons->provider->sectorsize, M_GVIRSTOR,
676172302Spjd	    M_WAITOK | M_ZERO);
677172302Spjd	error = g_write_data(comp->gcons,
678172302Spjd	    comp->gcons->provider->mediasize -
679172302Spjd	    comp->gcons->provider->sectorsize,
680172302Spjd	    buf,
681172302Spjd	    comp->gcons->provider->sectorsize);
682172302Spjd	free(buf, M_GVIRSTOR);
683172302Spjd	g_access(comp->gcons, 0, -1, 0);
684172302Spjd	return (error);
685172302Spjd}
686172302Spjd
687172302Spjd/*
688172302Spjd * Destroy geom forcibly.
689172302Spjd */
690172302Spjdstatic int
691172302Spjdg_virstor_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
692172302Spjd    struct g_geom *gp)
693172302Spjd{
694172302Spjd	struct g_virstor_softc *sc;
695172302Spjd	int exitval;
696172302Spjd
697172302Spjd	sc = gp->softc;
698172302Spjd	KASSERT(sc != NULL, ("%s: NULL sc", __func__));
699172302Spjd
700172302Spjd	exitval = 0;
701172302Spjd	LOG_MSG(LVL_DEBUG, "%s called for %s, sc=%p", __func__, gp->name,
702172302Spjd	    gp->softc);
703172302Spjd
704172302Spjd	if (sc != NULL) {
705172302Spjd#ifdef INVARIANTS
706172302Spjd		char *buf;
707172302Spjd		int error;
708172302Spjd		off_t off;
709172302Spjd		int isclean, count;
710172302Spjd		int n;
711172302Spjd
712172302Spjd		LOG_MSG(LVL_INFO, "INVARIANTS detected");
713172302Spjd		LOG_MSG(LVL_INFO, "Verifying allocation "
714172302Spjd		    "table for %s", sc->geom->name);
715172302Spjd		count = 0;
716172302Spjd		for (n = 0; n < sc->chunk_count; n++) {
717172302Spjd			if (sc->map[n].flags || VIRSTOR_MAP_ALLOCATED != 0)
718172302Spjd				count++;
719172302Spjd		}
720172302Spjd		LOG_MSG(LVL_INFO, "Device %s has %d allocated chunks",
721172302Spjd		    sc->geom->name, count);
722172302Spjd		n = off = count = 0;
723172302Spjd		isclean = 1;
724172302Spjd		if (virstor_valid_components(sc) != sc->n_components) {
725172302Spjd			/* This is a incomplete virstor device (not all
726172302Spjd			 * components have been found) */
727172302Spjd			LOG_MSG(LVL_ERROR, "Device %s is incomplete",
728172302Spjd			    sc->geom->name);
729172302Spjd			goto bailout;
730172302Spjd		}
731172302Spjd		error = g_access(sc->components[0].gcons, 1, 0, 0);
732172302Spjd		KASSERT(error == 0, ("%s: g_access failed (%d)", __func__,
733172302Spjd		    error));
734172302Spjd		/* Compare the whole on-disk allocation table with what's
735172302Spjd		 * currently in memory */
736172302Spjd		while (n < sc->chunk_count) {
737172302Spjd			buf = g_read_data(sc->components[0].gcons, off,
738172302Spjd			    sc->sectorsize, &error);
739172302Spjd			KASSERT(buf != NULL, ("g_read_data returned NULL (%d) "
740172302Spjd			    "for read at %jd", error, off));
741172302Spjd			if (bcmp(buf, &sc->map[n], sc->sectorsize) != 0) {
742172302Spjd				LOG_MSG(LVL_ERROR, "ERROR in allocation table, "
743172302Spjd				    "entry %d, offset %jd", n, off);
744172302Spjd				isclean = 0;
745172302Spjd				count++;
746172302Spjd			}
747172302Spjd			n += sc->me_per_sector;
748172302Spjd			off += sc->sectorsize;
749172302Spjd			g_free(buf);
750172302Spjd		}
751172302Spjd		error = g_access(sc->components[0].gcons, -1, 0, 0);
752172302Spjd		KASSERT(error == 0, ("%s: g_access failed (%d) on exit",
753172302Spjd		    __func__, error));
754172302Spjd		if (isclean != 1) {
755172302Spjd			LOG_MSG(LVL_ERROR, "ALLOCATION TABLE CORRUPTED FOR %s "
756172304Spjd			    "(%d sectors don't match, max %zu allocations)",
757172302Spjd			    sc->geom->name, count,
758172302Spjd			    count * sc->me_per_sector);
759172302Spjd		} else {
760172302Spjd			LOG_MSG(LVL_INFO, "Allocation table ok for %s",
761172302Spjd			    sc->geom->name);
762172302Spjd		}
763172302Spjdbailout:
764172302Spjd#endif
765172302Spjd		update_metadata(sc);
766172302Spjd		virstor_geom_destroy(sc, FALSE, FALSE);
767172302Spjd		exitval = EAGAIN;
768172302Spjd	} else
769172302Spjd		exitval = 0;
770172302Spjd	return (exitval);
771172302Spjd}
772172302Spjd
773172302Spjd/*
774172302Spjd * Taste event (per-class callback)
775172302Spjd * Examines a provider and creates geom instances if needed
776172302Spjd */
777172302Spjdstatic struct g_geom *
778172302Spjdg_virstor_taste(struct g_class *mp, struct g_provider *pp, int flags)
779172302Spjd{
780172302Spjd	struct g_virstor_metadata md;
781172302Spjd	struct g_geom *gp;
782172302Spjd	struct g_consumer *cp;
783172302Spjd	struct g_virstor_softc *sc;
784172302Spjd	int error;
785172302Spjd
786172302Spjd	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
787172302Spjd	g_topology_assert();
788172302Spjd	LOG_MSG(LVL_DEBUG, "Tasting %s", pp->name);
789172302Spjd
790172302Spjd	/* We need a dummy geom to attach a consumer to the given provider */
791172302Spjd	gp = g_new_geomf(mp, "virstor:taste.helper");
792172302Spjd	gp->start = (void *)invalid_call;	/* XXX: hacked up so the        */
793172302Spjd	gp->access = (void *)invalid_call;	/* compiler doesn't complain.   */
794172302Spjd	gp->orphan = (void *)invalid_call;	/* I really want these to fail. */
795172302Spjd
796172302Spjd	cp = g_new_consumer(gp);
797172302Spjd	g_attach(cp, pp);
798172302Spjd	error = read_metadata(cp, &md);
799172302Spjd	g_detach(cp);
800172302Spjd	g_destroy_consumer(cp);
801172302Spjd	g_destroy_geom(gp);
802172302Spjd
803172302Spjd	if (error != 0)
804172302Spjd		return (NULL);
805172302Spjd
806172302Spjd	if (strcmp(md.md_magic, G_VIRSTOR_MAGIC) != 0)
807172302Spjd		return (NULL);
808172302Spjd	if (md.md_version != G_VIRSTOR_VERSION) {
809172302Spjd		LOG_MSG(LVL_ERROR, "Kernel module version invalid "
810172302Spjd		    "to handle %s (%s) : %d should be %d",
811172302Spjd		    md.md_name, pp->name, md.md_version, G_VIRSTOR_VERSION);
812172302Spjd		return (NULL);
813172302Spjd	}
814172302Spjd	if (md.provsize != pp->mediasize)
815172302Spjd		return (NULL);
816172302Spjd
817172302Spjd	/* If the provider name is hardcoded, use the offered provider only
818172302Spjd	 * if it's been offered with its proper name (the one used in
819172302Spjd	 * the label command). */
820221101Smav	if (md.provider[0] != '\0' &&
821221101Smav	    !g_compare_names(md.provider, pp->name))
822221101Smav		return (NULL);
823172302Spjd
824172302Spjd	/* Iterate all geoms this class already knows about to see if a new
825172302Spjd	 * geom instance of this class needs to be created (in case the provider
826172302Spjd	 * is first from a (possibly) multi-consumer geom) or it just needs
827172302Spjd	 * to be added to an existing instance. */
828172302Spjd	sc = NULL;
829172302Spjd	gp = NULL;
830172302Spjd	LIST_FOREACH(gp, &mp->geom, geom) {
831172302Spjd		sc = gp->softc;
832172302Spjd		if (sc == NULL)
833172302Spjd			continue;
834172302Spjd		if (strcmp(md.md_name, sc->geom->name) != 0)
835172302Spjd			continue;
836172302Spjd		if (md.md_id != sc->id)
837172302Spjd			continue;
838172302Spjd		break;
839172302Spjd	}
840172302Spjd	if (gp != NULL) { /* We found an existing geom instance; add to it */
841172302Spjd		LOG_MSG(LVL_INFO, "Adding %s to %s", pp->name, md.md_name);
842172302Spjd		error = add_provider_to_geom(sc, pp, &md);
843172302Spjd		if (error != 0) {
844172302Spjd			LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
845172302Spjd			    pp->name, md.md_name, error);
846172302Spjd			return (NULL);
847172302Spjd		}
848172302Spjd	} else { /* New geom instance needs to be created */
849172302Spjd		gp = create_virstor_geom(mp, &md);
850172302Spjd		if (gp == NULL) {
851172302Spjd			LOG_MSG(LVL_ERROR, "Error creating new instance of "
852172302Spjd			    "class %s: %s", mp->name, md.md_name);
853172302Spjd			LOG_MSG(LVL_DEBUG, "Error creating %s at %s",
854172302Spjd			    md.md_name, pp->name);
855172302Spjd			return (NULL);
856172302Spjd		}
857172302Spjd		sc = gp->softc;
858172302Spjd		LOG_MSG(LVL_INFO, "Adding %s to %s (first found)", pp->name,
859172302Spjd		    md.md_name);
860172302Spjd		error = add_provider_to_geom(sc, pp, &md);
861172302Spjd		if (error != 0) {
862172302Spjd			LOG_MSG(LVL_ERROR, "Error adding %s to %s (error %d)",
863172302Spjd			    pp->name, md.md_name, error);
864172302Spjd			virstor_geom_destroy(sc, TRUE, FALSE);
865172302Spjd			return (NULL);
866172302Spjd		}
867172302Spjd	}
868172302Spjd
869172302Spjd	return (gp);
870172302Spjd}
871172302Spjd
872172302Spjd/*
873172302Spjd * Destroyes consumer passed to it in arguments. Used as a callback
874172302Spjd * on g_event queue.
875172302Spjd */
876172302Spjdstatic void
877172302Spjddelay_destroy_consumer(void *arg, int flags __unused)
878172302Spjd{
879172302Spjd	struct g_consumer *c = arg;
880172302Spjd	KASSERT(c != NULL, ("%s: invalid consumer", __func__));
881172302Spjd	LOG_MSG(LVL_DEBUG, "Consumer %s destroyed with delay",
882172302Spjd	    c->provider->name);
883172302Spjd	g_detach(c);
884172302Spjd	g_destroy_consumer(c);
885172302Spjd}
886172302Spjd
887172302Spjd/*
888172302Spjd * Remove a component (consumer) from geom instance; If it's the first
889172302Spjd * component being removed, orphan the provider to announce geom's being
890172302Spjd * dismantled
891172302Spjd */
892172302Spjdstatic void
893172302Spjdremove_component(struct g_virstor_softc *sc, struct g_virstor_component *comp,
894172302Spjd    boolean_t delay)
895172302Spjd{
896172302Spjd	struct g_consumer *c;
897172302Spjd
898172302Spjd	KASSERT(comp->gcons != NULL, ("Component with no consumer in %s",
899172302Spjd	    sc->geom->name));
900172302Spjd	c = comp->gcons;
901172302Spjd
902172302Spjd	comp->gcons = NULL;
903172302Spjd	KASSERT(c->provider != NULL, ("%s: no provider", __func__));
904172302Spjd	LOG_MSG(LVL_DEBUG, "Component %s removed from %s", c->provider->name,
905172302Spjd	    sc->geom->name);
906172302Spjd	if (sc->provider != NULL) {
907172302Spjd		/* Whither, GEOM? */
908172302Spjd		sc->provider->flags |= G_PF_WITHER;
909172302Spjd		g_orphan_provider(sc->provider, ENXIO);
910172302Spjd		sc->provider = NULL;
911172302Spjd		LOG_MSG(LVL_INFO, "Removing provider %s", sc->geom->name);
912172302Spjd	}
913172302Spjd
914172302Spjd	if (c->acr > 0 || c->acw > 0 || c->ace > 0)
915172302Spjd		g_access(c, -c->acr, -c->acw, -c->ace);
916172302Spjd	if (delay) {
917172302Spjd		/* Destroy consumer after it's tasted */
918172302Spjd		g_post_event(delay_destroy_consumer, c, M_WAITOK, NULL);
919172302Spjd	} else {
920172302Spjd		g_detach(c);
921172302Spjd		g_destroy_consumer(c);
922172302Spjd	}
923172302Spjd}
924172302Spjd
925172302Spjd/*
926172302Spjd * Destroy geom - called internally
927172302Spjd * See g_virstor_destroy_geom for the other one
928172302Spjd */
929172302Spjdstatic int
930172302Spjdvirstor_geom_destroy(struct g_virstor_softc *sc, boolean_t force,
931172302Spjd    boolean_t delay)
932172302Spjd{
933172302Spjd	struct g_provider *pp;
934172302Spjd	struct g_geom *gp;
935172302Spjd	int n;
936172302Spjd
937172302Spjd	g_topology_assert();
938172302Spjd
939172302Spjd	if (sc == NULL)
940172302Spjd		return (ENXIO);
941172302Spjd
942172302Spjd	pp = sc->provider;
943172302Spjd	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
944172302Spjd		LOG_MSG(force ? LVL_WARNING : LVL_ERROR,
945172302Spjd		    "Device %s is still open.", pp->name);
946172302Spjd		if (!force)
947172302Spjd			return (EBUSY);
948172302Spjd	}
949172302Spjd
950172302Spjd	for (n = 0; n < sc->n_components; n++) {
951172302Spjd		if (sc->components[n].gcons != NULL)
952172302Spjd			remove_component(sc, &sc->components[n], delay);
953172302Spjd	}
954172302Spjd
955172302Spjd	gp = sc->geom;
956172302Spjd	gp->softc = NULL;
957172302Spjd
958172302Spjd	KASSERT(sc->provider == NULL, ("Provider still exists for %s",
959172302Spjd	    gp->name));
960172302Spjd
961172302Spjd	/* XXX: This might or might not work, since we're called with
962172302Spjd	 * the topology lock held. Also, it might panic the kernel if
963172302Spjd	 * the error'd BIO is in softupdates code. */
964172302Spjd	mtx_lock(&sc->delayed_bio_q_mtx);
965172302Spjd	while (!STAILQ_EMPTY(&sc->delayed_bio_q)) {
966172302Spjd		struct g_virstor_bio_q *bq;
967172302Spjd		bq = STAILQ_FIRST(&sc->delayed_bio_q);
968172302Spjd		bq->bio->bio_error = ENOSPC;
969172302Spjd		g_io_deliver(bq->bio, EIO);
970172302Spjd		STAILQ_REMOVE_HEAD(&sc->delayed_bio_q, linkage);
971172302Spjd		free(bq, M_GVIRSTOR);
972172302Spjd	}
973172302Spjd	mtx_unlock(&sc->delayed_bio_q_mtx);
974172302Spjd	mtx_destroy(&sc->delayed_bio_q_mtx);
975172302Spjd
976172302Spjd	free(sc->map, M_GVIRSTOR);
977172302Spjd	free(sc->components, M_GVIRSTOR);
978172302Spjd	bzero(sc, sizeof *sc);
979172302Spjd	free(sc, M_GVIRSTOR);
980172302Spjd
981172302Spjd	pp = LIST_FIRST(&gp->provider); /* We only offer one provider */
982172302Spjd	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
983172302Spjd		LOG_MSG(LVL_DEBUG, "Device %s destroyed", gp->name);
984172302Spjd
985172302Spjd	g_wither_geom(gp, ENXIO);
986172302Spjd
987172302Spjd	return (0);
988172302Spjd}
989172302Spjd
990172302Spjd/*
991172302Spjd * Utility function: read metadata & decode. Wants topology lock to be
992172302Spjd * held.
993172302Spjd */
994172302Spjdstatic int
995172302Spjdread_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
996172302Spjd{
997172302Spjd	struct g_provider *pp;
998172302Spjd	char *buf;
999172302Spjd	int error;
1000172302Spjd
1001172302Spjd	g_topology_assert();
1002172302Spjd	error = g_access(cp, 1, 0, 0);
1003172302Spjd	if (error != 0)
1004172302Spjd		return (error);
1005172302Spjd	pp = cp->provider;
1006172302Spjd	g_topology_unlock();
1007172302Spjd	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
1008172302Spjd	    &error);
1009172302Spjd	g_topology_lock();
1010172302Spjd	g_access(cp, -1, 0, 0);
1011172302Spjd	if (buf == NULL)
1012172302Spjd		return (error);
1013172302Spjd
1014172302Spjd	virstor_metadata_decode(buf, md);
1015172302Spjd	g_free(buf);
1016172302Spjd
1017172302Spjd	return (0);
1018172302Spjd}
1019172302Spjd
1020172302Spjd/**
1021172302Spjd * Utility function: encode & write metadata. Assumes topology lock is
1022172302Spjd * held.
1023202987Sivoras *
1024202987Sivoras * There is no useful way of recovering from errors in this function,
1025202987Sivoras * not involving panicking the kernel. If the metadata cannot be written
1026202987Sivoras * the most we can do is notify the operator and hope he spots it and
1027202987Sivoras * replaces the broken drive.
1028172302Spjd */
1029202987Sivorasstatic void
1030172302Spjdwrite_metadata(struct g_consumer *cp, struct g_virstor_metadata *md)
1031172302Spjd{
1032172302Spjd	struct g_provider *pp;
1033172302Spjd	char *buf;
1034172302Spjd	int error;
1035172302Spjd
1036172302Spjd	KASSERT(cp != NULL && md != NULL && cp->provider != NULL,
1037172302Spjd	    ("Something's fishy in %s", __func__));
1038172302Spjd	LOG_MSG(LVL_DEBUG, "Writing metadata on %s", cp->provider->name);
1039172302Spjd	g_topology_assert();
1040172302Spjd	error = g_access(cp, 0, 1, 0);
1041202987Sivoras	if (error != 0) {
1042202987Sivoras		LOG_MSG(LVL_ERROR, "g_access(0,1,0) failed for %s: %d",
1043202987Sivoras		    cp->provider->name, error);
1044202987Sivoras		return;
1045202987Sivoras	}
1046172302Spjd	pp = cp->provider;
1047172302Spjd
1048172302Spjd	buf = malloc(pp->sectorsize, M_GVIRSTOR, M_WAITOK);
1049172302Spjd	virstor_metadata_encode(md, buf);
1050172302Spjd	g_topology_unlock();
1051172302Spjd	error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf,
1052172302Spjd	    pp->sectorsize);
1053172302Spjd	g_topology_lock();
1054172302Spjd	g_access(cp, 0, -1, 0);
1055202987Sivoras	free(buf, M_GVIRSTOR);
1056172302Spjd
1057202987Sivoras	if (error != 0)
1058202987Sivoras		LOG_MSG(LVL_ERROR, "Error %d writing metadata to %s",
1059202987Sivoras		    error, cp->provider->name);
1060172302Spjd}
1061172302Spjd
1062172302Spjd/*
1063172302Spjd * Creates a new instance of this GEOM class, initialise softc
1064172302Spjd */
1065172302Spjdstatic struct g_geom *
1066172302Spjdcreate_virstor_geom(struct g_class *mp, struct g_virstor_metadata *md)
1067172302Spjd{
1068172302Spjd	struct g_geom *gp;
1069172302Spjd	struct g_virstor_softc *sc;
1070172302Spjd
1071172302Spjd	LOG_MSG(LVL_DEBUG, "Creating geom instance for %s (id=%u)",
1072172302Spjd	    md->md_name, md->md_id);
1073172302Spjd
1074172302Spjd	if (md->md_count < 1 || md->md_chunk_size < 1 ||
1075172302Spjd	    md->md_virsize < md->md_chunk_size) {
1076172302Spjd		/* This is bogus configuration, and probably means data is
1077172302Spjd		 * somehow corrupted. Panic, maybe? */
1078172302Spjd		LOG_MSG(LVL_ERROR, "Nonsensical metadata information for %s",
1079172302Spjd		    md->md_name);
1080172302Spjd		return (NULL);
1081172302Spjd	}
1082172302Spjd
1083172302Spjd	/* Check if it's already created */
1084172302Spjd	LIST_FOREACH(gp, &mp->geom, geom) {
1085172302Spjd		sc = gp->softc;
1086172302Spjd		if (sc != NULL && strcmp(sc->geom->name, md->md_name) == 0) {
1087172302Spjd			LOG_MSG(LVL_WARNING, "Geom %s already exists",
1088172302Spjd			    md->md_name);
1089172302Spjd			if (sc->id != md->md_id) {
1090172302Spjd				LOG_MSG(LVL_ERROR,
1091172302Spjd				    "Some stale or invalid components "
1092172302Spjd				    "exist for virstor device named %s. "
1093172302Spjd				    "You will need to <CLEAR> all stale "
1094172302Spjd				    "components and maybe reconfigure "
1095172302Spjd				    "the virstor device. Tune "
1096172302Spjd				    "kern.geom.virstor.debug sysctl up "
1097172302Spjd				    "for more information.",
1098172302Spjd				    sc->geom->name);
1099172302Spjd			}
1100172302Spjd			return (NULL);
1101172302Spjd		}
1102172302Spjd	}
1103172302Spjd	gp = g_new_geomf(mp, "%s", md->md_name);
1104172302Spjd	gp->softc = NULL; /* to circumevent races that test softc */
1105172302Spjd
1106172302Spjd	gp->start = g_virstor_start;
1107172302Spjd	gp->spoiled = g_virstor_orphan;
1108172302Spjd	gp->orphan = g_virstor_orphan;
1109172302Spjd	gp->access = g_virstor_access;
1110172302Spjd	gp->dumpconf = g_virstor_dumpconf;
1111172302Spjd
1112172302Spjd	sc = malloc(sizeof(*sc), M_GVIRSTOR, M_WAITOK | M_ZERO);
1113172302Spjd	sc->id = md->md_id;
1114172302Spjd	sc->n_components = md->md_count;
1115172302Spjd	sc->components = malloc(sizeof(struct g_virstor_component) * md->md_count,
1116172302Spjd	    M_GVIRSTOR, M_WAITOK | M_ZERO);
1117172302Spjd	sc->chunk_size = md->md_chunk_size;
1118172302Spjd	sc->virsize = md->md_virsize;
1119172302Spjd	STAILQ_INIT(&sc->delayed_bio_q);
1120172302Spjd	mtx_init(&sc->delayed_bio_q_mtx, "gvirstor_delayed_bio_q_mtx",
1121172302Spjd	    "gvirstor", MTX_DEF | MTX_RECURSE);
1122172302Spjd
1123172302Spjd	sc->geom = gp;
1124172302Spjd	sc->provider = NULL; /* virstor_check_and_run will create it */
1125172302Spjd	gp->softc = sc;
1126172302Spjd
1127172302Spjd	LOG_MSG(LVL_ANNOUNCE, "Device %s created", sc->geom->name);
1128172302Spjd
1129172302Spjd	return (gp);
1130172302Spjd}
1131172302Spjd
1132172302Spjd/*
1133172302Spjd * Add provider to a GEOM class instance
1134172302Spjd */
1135172302Spjdstatic int
1136172302Spjdadd_provider_to_geom(struct g_virstor_softc *sc, struct g_provider *pp,
1137172302Spjd    struct g_virstor_metadata *md)
1138172302Spjd{
1139172302Spjd	struct g_virstor_component *component;
1140172302Spjd	struct g_consumer *cp, *fcp;
1141172302Spjd	struct g_geom *gp;
1142172302Spjd	int error;
1143172302Spjd
1144172302Spjd	if (md->no >= sc->n_components)
1145172302Spjd		return (EINVAL);
1146172302Spjd
1147172302Spjd	/* "Current" compontent */
1148172302Spjd	component = &(sc->components[md->no]);
1149172302Spjd	if (component->gcons != NULL)
1150172302Spjd		return (EEXIST);
1151172302Spjd
1152172302Spjd	gp = sc->geom;
1153172302Spjd	fcp = LIST_FIRST(&gp->consumer);
1154172302Spjd
1155172302Spjd	cp = g_new_consumer(gp);
1156172302Spjd	error = g_attach(cp, pp);
1157172302Spjd
1158172302Spjd	if (error != 0) {
1159172302Spjd		g_destroy_consumer(cp);
1160172302Spjd		return (error);
1161172302Spjd	}
1162172302Spjd
1163172302Spjd	if (fcp != NULL) {
1164172302Spjd		if (fcp->provider->sectorsize != pp->sectorsize) {
1165172302Spjd			/* TODO: this can be made to work */
1166172302Spjd			LOG_MSG(LVL_ERROR, "Provider %s of %s has invalid "
1167172302Spjd			    "sector size (%d)", pp->name, sc->geom->name,
1168172302Spjd			    pp->sectorsize);
1169172302Spjd			return (EINVAL);
1170172302Spjd		}
1171172302Spjd		if (fcp->acr > 0 || fcp->acw || fcp->ace > 0) {
1172172302Spjd			/* Replicate access permissions from first "live" consumer
1173172302Spjd			 * to the new one */
1174172302Spjd			error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
1175172302Spjd			if (error != 0) {
1176172302Spjd				g_detach(cp);
1177172302Spjd				g_destroy_consumer(cp);
1178172302Spjd				return (error);
1179172302Spjd			}
1180172302Spjd		}
1181172302Spjd	}
1182172302Spjd
1183172302Spjd	/* Bring up a new component */
1184172302Spjd	cp->private = component;
1185172302Spjd	component->gcons = cp;
1186172302Spjd	component->sc = sc;
1187172302Spjd	component->index = md->no;
1188172302Spjd	component->chunk_count = md->chunk_count;
1189172302Spjd	component->chunk_next = md->chunk_next;
1190172302Spjd	component->chunk_reserved = md->chunk_reserved;
1191172302Spjd	component->flags = md->flags;
1192172302Spjd
1193172302Spjd	LOG_MSG(LVL_DEBUG, "%s attached to %s", pp->name, sc->geom->name);
1194172302Spjd
1195172302Spjd	virstor_check_and_run(sc);
1196172302Spjd	return (0);
1197172302Spjd}
1198172302Spjd
1199172302Spjd/*
1200172302Spjd * Check if everything's ready to create the geom provider & device entry,
1201172302Spjd * create and start provider.
1202172302Spjd * Called ultimately by .taste, from g_event thread
1203172302Spjd */
1204172302Spjdstatic void
1205172302Spjdvirstor_check_and_run(struct g_virstor_softc *sc)
1206172302Spjd{
1207172302Spjd	off_t off;
1208172302Spjd	size_t n, count;
1209172302Spjd	int index;
1210172302Spjd	int error;
1211172302Spjd
1212172302Spjd	if (virstor_valid_components(sc) != sc->n_components)
1213172302Spjd		return;
1214172302Spjd
1215172302Spjd	if (virstor_valid_components(sc) == 0) {
1216172302Spjd		/* This is actually a candidate for panic() */
1217172302Spjd		LOG_MSG(LVL_ERROR, "No valid components for %s?",
1218172302Spjd		    sc->provider->name);
1219172302Spjd		return;
1220172302Spjd	}
1221172302Spjd
1222172302Spjd	sc->sectorsize = sc->components[0].gcons->provider->sectorsize;
1223172302Spjd
1224172302Spjd	/* Initialise allocation map from the first consumer */
1225172302Spjd	sc->chunk_count = sc->virsize / sc->chunk_size;
1226172302Spjd	if (sc->chunk_count * (off_t)sc->chunk_size != sc->virsize) {
1227172302Spjd		LOG_MSG(LVL_WARNING, "Device %s truncated to %ju bytes",
1228172302Spjd		    sc->provider->name,
1229172302Spjd		    sc->chunk_count * (off_t)sc->chunk_size);
1230172302Spjd	}
1231172302Spjd	sc->map_size = sc->chunk_count * sizeof *(sc->map);
1232172302Spjd	/* The following allocation is in order of 4MB - 8MB */
1233172302Spjd	sc->map = malloc(sc->map_size, M_GVIRSTOR, M_WAITOK);
1234172302Spjd	KASSERT(sc->map != NULL, ("%s: Memory allocation error (%zu bytes) for %s",
1235172302Spjd	    __func__, sc->map_size, sc->provider->name));
1236172302Spjd	sc->map_sectors = sc->map_size / sc->sectorsize;
1237172302Spjd
1238172302Spjd	count = 0;
1239172302Spjd	for (n = 0; n < sc->n_components; n++)
1240172302Spjd		count += sc->components[n].chunk_count;
1241172302Spjd	LOG_MSG(LVL_INFO, "Device %s has %zu physical chunks and %zu virtual "
1242172302Spjd	    "(%zu KB chunks)",
1243172302Spjd	    sc->geom->name, count, sc->chunk_count, sc->chunk_size / 1024);
1244172302Spjd
1245172302Spjd	error = g_access(sc->components[0].gcons, 1, 0, 0);
1246172302Spjd	if (error != 0) {
1247172302Spjd		LOG_MSG(LVL_ERROR, "Cannot acquire read access for %s to "
1248172302Spjd		    "read allocation map for %s",
1249172302Spjd		    sc->components[0].gcons->provider->name,
1250172302Spjd		    sc->geom->name);
1251172302Spjd		return;
1252172302Spjd	}
1253172302Spjd	/* Read in the allocation map */
1254172302Spjd	LOG_MSG(LVL_DEBUG, "Reading map for %s from %s", sc->geom->name,
1255172302Spjd	    sc->components[0].gcons->provider->name);
1256172302Spjd	off = count = n = 0;
1257172302Spjd	while (count < sc->map_size) {
1258172302Spjd		struct g_virstor_map_entry *mapbuf;
1259172302Spjd		size_t bs;
1260172302Spjd
1261172302Spjd		bs = MIN(MAXPHYS, sc->map_size - count);
1262172302Spjd		if (bs % sc->sectorsize != 0) {
1263172302Spjd			/* Check for alignment errors */
1264172302Spjd			bs = (bs / sc->sectorsize) * sc->sectorsize;
1265172302Spjd			if (bs == 0)
1266172302Spjd				break;
1267172302Spjd			LOG_MSG(LVL_ERROR, "Trouble: map is not sector-aligned "
1268172302Spjd			    "for %s on %s", sc->geom->name,
1269172302Spjd			    sc->components[0].gcons->provider->name);
1270172302Spjd		}
1271172302Spjd		mapbuf = g_read_data(sc->components[0].gcons, off, bs, &error);
1272172302Spjd		if (mapbuf == NULL) {
1273172302Spjd			free(sc->map, M_GVIRSTOR);
1274172302Spjd			LOG_MSG(LVL_ERROR, "Error reading allocation map "
1275172302Spjd			    "for %s from %s (offset %ju) (error %d)",
1276172302Spjd			    sc->geom->name,
1277172302Spjd			    sc->components[0].gcons->provider->name,
1278172302Spjd			    off, error);
1279172302Spjd			return;
1280172302Spjd		}
1281172302Spjd
1282172302Spjd		bcopy(mapbuf, &sc->map[n], bs);
1283172302Spjd		off += bs;
1284172302Spjd		count += bs;
1285172302Spjd		n += bs / sizeof *(sc->map);
1286172302Spjd		g_free(mapbuf);
1287172302Spjd	}
1288172302Spjd	g_access(sc->components[0].gcons, -1, 0, 0);
1289172302Spjd	LOG_MSG(LVL_DEBUG, "Read map for %s", sc->geom->name);
1290172302Spjd
1291172302Spjd	/* find first component with allocatable chunks */
1292172302Spjd	index = -1;
1293172302Spjd	for (n = 0; n < sc->n_components; n++) {
1294172302Spjd		if (sc->components[n].chunk_next <
1295172302Spjd		    sc->components[n].chunk_count) {
1296172302Spjd			index = n;
1297172302Spjd			break;
1298172302Spjd		}
1299172302Spjd	}
1300172302Spjd	if (index == -1)
1301172302Spjd		/* not found? set it to the last component and handle it
1302172302Spjd		 * later */
1303172302Spjd		index = sc->n_components - 1;
1304172302Spjd
1305172302Spjd	if (index >= sc->n_components - g_virstor_component_watermark - 1) {
1306172302Spjd		LOG_MSG(LVL_WARNING, "Device %s running out of components "
1307172302Spjd		    "(%d/%u: %s)", sc->geom->name,
1308172302Spjd		    index+1,
1309172302Spjd		    sc->n_components,
1310172302Spjd		    sc->components[index].gcons->provider->name);
1311172302Spjd	}
1312172302Spjd	sc->curr_component = index;
1313172302Spjd
1314172302Spjd	if (sc->components[index].chunk_next >=
1315172302Spjd	    sc->components[index].chunk_count - g_virstor_chunk_watermark) {
1316172302Spjd		LOG_MSG(LVL_WARNING,
1317172302Spjd		    "Component %s of %s is running out of free space "
1318172302Spjd		    "(%u chunks left)",
1319172302Spjd		    sc->components[index].gcons->provider->name,
1320172302Spjd		    sc->geom->name, sc->components[index].chunk_count -
1321172302Spjd		    sc->components[index].chunk_next);
1322172302Spjd	}
1323172302Spjd
1324172302Spjd	sc->me_per_sector = sc->sectorsize / sizeof *(sc->map);
1325172302Spjd	if (sc->sectorsize % sizeof *(sc->map) != 0) {
1326172302Spjd		LOG_MSG(LVL_ERROR,
1327172302Spjd		    "%s: Map entries don't fit exactly in a sector (%s)",
1328172302Spjd		    __func__, sc->geom->name);
1329172302Spjd		return;
1330172302Spjd	}
1331172302Spjd
1332172302Spjd	/* Recalculate allocated chunks in components & at the same time
1333172302Spjd	 * verify map data is sane. We could trust metadata on this, but
1334172302Spjd	 * we want to make sure. */
1335172302Spjd	for (n = 0; n < sc->n_components; n++)
1336172302Spjd		sc->components[n].chunk_next = sc->components[n].chunk_reserved;
1337172302Spjd
1338172302Spjd	for (n = 0; n < sc->chunk_count; n++) {
1339172302Spjd		if (sc->map[n].provider_no >= sc->n_components ||
1340172302Spjd			sc->map[n].provider_chunk >=
1341172302Spjd			sc->components[sc->map[n].provider_no].chunk_count) {
1342172302Spjd			LOG_MSG(LVL_ERROR, "%s: Invalid entry %u in map for %s",
1343172302Spjd			    __func__, (u_int)n, sc->geom->name);
1344172302Spjd			LOG_MSG(LVL_ERROR, "%s: provider_no: %u, n_components: %u"
1345172302Spjd			    " provider_chunk: %u, chunk_count: %u", __func__,
1346172302Spjd			    sc->map[n].provider_no, sc->n_components,
1347172302Spjd			    sc->map[n].provider_chunk,
1348172302Spjd			    sc->components[sc->map[n].provider_no].chunk_count);
1349172302Spjd			return;
1350172302Spjd		}
1351172302Spjd		if (sc->map[n].flags & VIRSTOR_MAP_ALLOCATED)
1352172302Spjd			sc->components[sc->map[n].provider_no].chunk_next++;
1353172302Spjd	}
1354172302Spjd
1355172302Spjd	sc->provider = g_new_providerf(sc->geom, "virstor/%s",
1356172302Spjd	    sc->geom->name);
1357172302Spjd
1358172302Spjd	sc->provider->sectorsize = sc->sectorsize;
1359172302Spjd	sc->provider->mediasize = sc->virsize;
1360172302Spjd	g_error_provider(sc->provider, 0);
1361172302Spjd
1362172302Spjd	LOG_MSG(LVL_INFO, "%s activated", sc->provider->name);
1363172302Spjd	LOG_MSG(LVL_DEBUG, "%s starting with current component %u, starting "
1364172302Spjd	    "chunk %u", sc->provider->name, sc->curr_component,
1365172302Spjd	    sc->components[sc->curr_component].chunk_next);
1366172302Spjd}
1367172302Spjd
1368172302Spjd/*
1369172302Spjd * Returns count of active providers in this geom instance
1370172302Spjd */
1371172302Spjdstatic u_int
1372172302Spjdvirstor_valid_components(struct g_virstor_softc *sc)
1373172302Spjd{
1374172302Spjd	unsigned int nc, i;
1375172302Spjd
1376172302Spjd	nc = 0;
1377172302Spjd	KASSERT(sc != NULL, ("%s: softc is NULL", __func__));
1378172302Spjd	KASSERT(sc->components != NULL, ("%s: sc->components is NULL", __func__));
1379172302Spjd	for (i = 0; i < sc->n_components; i++)
1380172302Spjd		if (sc->components[i].gcons != NULL)
1381172302Spjd			nc++;
1382172302Spjd	return (nc);
1383172302Spjd}
1384172302Spjd
1385172302Spjd/*
1386172302Spjd * Called when the consumer gets orphaned (?)
1387172302Spjd */
1388172302Spjdstatic void
1389172302Spjdg_virstor_orphan(struct g_consumer *cp)
1390172302Spjd{
1391172302Spjd	struct g_virstor_softc *sc;
1392172302Spjd	struct g_virstor_component *comp;
1393172302Spjd	struct g_geom *gp;
1394172302Spjd
1395172302Spjd	g_topology_assert();
1396172302Spjd	gp = cp->geom;
1397172302Spjd	sc = gp->softc;
1398172302Spjd	if (sc == NULL)
1399172302Spjd		return;
1400172302Spjd
1401172302Spjd	comp = cp->private;
1402172302Spjd	KASSERT(comp != NULL, ("%s: No component in private part of consumer",
1403172302Spjd	    __func__));
1404172302Spjd	remove_component(sc, comp, FALSE);
1405172302Spjd	if (virstor_valid_components(sc) == 0)
1406172302Spjd		virstor_geom_destroy(sc, TRUE, FALSE);
1407172302Spjd}
1408172302Spjd
1409172302Spjd/*
1410172302Spjd * Called to notify geom when it's been opened, and for what intent
1411172302Spjd */
1412172302Spjdstatic int
1413172302Spjdg_virstor_access(struct g_provider *pp, int dr, int dw, int de)
1414172302Spjd{
1415172302Spjd	struct g_consumer *c;
1416172302Spjd	struct g_virstor_softc *sc;
1417172302Spjd	struct g_geom *gp;
1418172302Spjd	int error;
1419172302Spjd
1420172302Spjd	KASSERT(pp != NULL, ("%s: NULL provider", __func__));
1421172302Spjd	gp = pp->geom;
1422172302Spjd	KASSERT(gp != NULL, ("%s: NULL geom", __func__));
1423172302Spjd	sc = gp->softc;
1424172302Spjd
1425172302Spjd	if (sc == NULL) {
1426172302Spjd		/* It seems that .access can be called with negative dr,dw,dx
1427172302Spjd		 * in this case but I want to check for myself */
1428172302Spjd		LOG_MSG(LVL_WARNING, "access(%d, %d, %d) for %s",
1429172302Spjd		    dr, dw, de, pp->name);
1430172302Spjd		/* This should only happen when geom is withered so
1431172302Spjd		 * allow only negative requests */
1432172302Spjd		KASSERT(dr <= 0 && dw <= 0 && de <= 0,
1433172302Spjd		    ("%s: Positive access for %s", __func__, pp->name));
1434172302Spjd		if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0)
1435172302Spjd			LOG_MSG(LVL_DEBUG, "Device %s definitely destroyed",
1436172302Spjd			    pp->name);
1437172302Spjd		return (0);
1438172302Spjd	}
1439172302Spjd
1440172302Spjd	/* Grab an exclusive bit to propagate on our consumers on first open */
1441172302Spjd	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1442172302Spjd		de++;
1443172302Spjd	/* ... drop it on close */
1444172302Spjd	if (pp->acr + dr == 0 && pp->acw + dw == 0 && pp->ace + de == 0) {
1445172302Spjd		de--;
1446172302Spjd		update_metadata(sc);	/* Writes statistical information */
1447172302Spjd	}
1448172302Spjd
1449172302Spjd	error = ENXIO;
1450172302Spjd	LIST_FOREACH(c, &gp->consumer, consumer) {
1451172302Spjd		KASSERT(c != NULL, ("%s: consumer is NULL", __func__));
1452172302Spjd		error = g_access(c, dr, dw, de);
1453172302Spjd		if (error != 0) {
1454172302Spjd			struct g_consumer *c2;
1455172302Spjd
1456172302Spjd			/* Backout earlier changes */
1457172302Spjd			LIST_FOREACH(c2, &gp->consumer, consumer) {
1458172302Spjd				if (c2 == c) /* all eariler components fixed */
1459172302Spjd					return (error);
1460172302Spjd				g_access(c2, -dr, -dw, -de);
1461172302Spjd			}
1462172302Spjd		}
1463172302Spjd	}
1464172302Spjd
1465172302Spjd	return (error);
1466172302Spjd}
1467172302Spjd
1468172302Spjd/*
1469172302Spjd * Generate XML dump of current state
1470172302Spjd */
1471172302Spjdstatic void
1472172302Spjdg_virstor_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1473172302Spjd    struct g_consumer *cp, struct g_provider *pp)
1474172302Spjd{
1475172302Spjd	struct g_virstor_softc *sc;
1476172302Spjd
1477172302Spjd	g_topology_assert();
1478172302Spjd	sc = gp->softc;
1479172302Spjd
1480172302Spjd	if (sc == NULL || pp != NULL)
1481172302Spjd		return;
1482172302Spjd
1483172302Spjd	if (cp != NULL) {
1484172302Spjd		/* For each component */
1485172302Spjd		struct g_virstor_component *comp;
1486172302Spjd
1487172302Spjd		comp = cp->private;
1488172302Spjd		if (comp == NULL)
1489172302Spjd			return;
1490172302Spjd		sbuf_printf(sb, "%s<ComponentIndex>%u</ComponentIndex>\n",
1491172302Spjd		    indent, comp->index);
1492172302Spjd		sbuf_printf(sb, "%s<ChunkCount>%u</ChunkCount>\n",
1493172302Spjd		    indent, comp->chunk_count);
1494172302Spjd		sbuf_printf(sb, "%s<ChunksUsed>%u</ChunksUsed>\n",
1495172302Spjd		    indent, comp->chunk_next);
1496172302Spjd		sbuf_printf(sb, "%s<ChunksReserved>%u</ChunksReserved>\n",
1497172302Spjd		    indent, comp->chunk_reserved);
1498172302Spjd		sbuf_printf(sb, "%s<StorageFree>%u%%</StorageFree>\n",
1499172302Spjd		    indent,
1500172302Spjd		    comp->chunk_next > 0 ? 100 -
1501172302Spjd		    ((comp->chunk_next + comp->chunk_reserved) * 100) /
1502172302Spjd		    comp->chunk_count : 100);
1503172302Spjd	} else {
1504172302Spjd		/* For the whole thing */
1505172302Spjd		u_int count, used, i;
1506172302Spjd		off_t size;
1507172302Spjd
1508172302Spjd		count = used = size = 0;
1509172302Spjd		for (i = 0; i < sc->n_components; i++) {
1510172302Spjd			if (sc->components[i].gcons != NULL) {
1511172302Spjd				count += sc->components[i].chunk_count;
1512172302Spjd				used += sc->components[i].chunk_next +
1513172302Spjd				    sc->components[i].chunk_reserved;
1514172302Spjd				size += sc->components[i].gcons->
1515172302Spjd				    provider->mediasize;
1516172302Spjd			}
1517172302Spjd		}
1518172302Spjd
1519172302Spjd		sbuf_printf(sb, "%s<Status>"
1520172302Spjd		    "Components=%u, Online=%u</Status>\n", indent,
1521172302Spjd		    sc->n_components, virstor_valid_components(sc));
1522172302Spjd		sbuf_printf(sb, "%s<State>%u%% physical free</State>\n",
1523172302Spjd		    indent, 100-(used * 100) / count);
1524172302Spjd		sbuf_printf(sb, "%s<ChunkSize>%zu</ChunkSize>\n", indent,
1525172302Spjd		    sc->chunk_size);
1526172302Spjd		sbuf_printf(sb, "%s<PhysicalFree>%u%%</PhysicalFree>\n",
1527172302Spjd		    indent, used > 0 ? 100 - (used * 100) / count : 100);
1528172302Spjd		sbuf_printf(sb, "%s<ChunkPhysicalCount>%u</ChunkPhysicalCount>\n",
1529172302Spjd		    indent, count);
1530172302Spjd		sbuf_printf(sb, "%s<ChunkVirtualCount>%zu</ChunkVirtualCount>\n",
1531172302Spjd		    indent, sc->chunk_count);
1532172302Spjd		sbuf_printf(sb, "%s<PhysicalBacking>%zu%%</PhysicalBacking>\n",
1533172302Spjd		    indent,
1534172302Spjd		    (count * 100) / sc->chunk_count);
1535172302Spjd		sbuf_printf(sb, "%s<PhysicalBackingSize>%jd</PhysicalBackingSize>\n",
1536172302Spjd		    indent, size);
1537172302Spjd		sbuf_printf(sb, "%s<VirtualSize>%jd</VirtualSize>\n", indent,
1538172302Spjd		    sc->virsize);
1539172302Spjd	}
1540172302Spjd}
1541172302Spjd
1542172302Spjd/*
1543172302Spjd * GEOM .done handler
1544172302Spjd * Can't use standard handler because one requested IO may
1545172302Spjd * fork into additional data IOs
1546172302Spjd */
1547172302Spjdstatic void
1548172302Spjdg_virstor_done(struct bio *b)
1549172302Spjd{
1550172302Spjd	struct g_virstor_softc *sc;
1551172302Spjd	struct bio *parent_b;
1552172302Spjd
1553172302Spjd	parent_b = b->bio_parent;
1554172302Spjd	sc = parent_b->bio_to->geom->softc;
1555172302Spjd
1556172302Spjd	if (b->bio_error != 0) {
1557172302Spjd		LOG_MSG(LVL_ERROR, "Error %d for offset=%ju, length=%ju, %s",
1558172302Spjd		    b->bio_error, b->bio_offset, b->bio_length,
1559172302Spjd		    b->bio_to->name);
1560172302Spjd		if (parent_b->bio_error == 0)
1561172302Spjd			parent_b->bio_error = b->bio_error;
1562172302Spjd	}
1563172302Spjd
1564172302Spjd	parent_b->bio_inbed++;
1565172302Spjd	parent_b->bio_completed += b->bio_completed;
1566172302Spjd
1567172302Spjd	if (parent_b->bio_children == parent_b->bio_inbed) {
1568172302Spjd		parent_b->bio_completed = parent_b->bio_length;
1569172302Spjd		g_io_deliver(parent_b, parent_b->bio_error);
1570172302Spjd	}
1571172302Spjd	g_destroy_bio(b);
1572172302Spjd}
1573172302Spjd
1574172302Spjd/*
1575172302Spjd * I/O starts here
1576172302Spjd * Called in g_down thread
1577172302Spjd */
1578172302Spjdstatic void
1579172302Spjdg_virstor_start(struct bio *b)
1580172302Spjd{
1581172302Spjd	struct g_virstor_softc *sc;
1582172302Spjd	struct g_virstor_component *comp;
1583172302Spjd	struct bio *cb;
1584172302Spjd	struct g_provider *pp;
1585172302Spjd	char *addr;
1586172302Spjd	off_t offset, length;
1587172302Spjd	struct bio_queue_head bq;
1588172302Spjd	size_t chunk_size;	/* cached for convenience */
1589172302Spjd	u_int count;
1590172302Spjd
1591172302Spjd	pp = b->bio_to;
1592172302Spjd	sc = pp->geom->softc;
1593172302Spjd	KASSERT(sc != NULL, ("%s: no softc (error=%d, device=%s)", __func__,
1594172302Spjd	    b->bio_to->error, b->bio_to->name));
1595172302Spjd
1596172302Spjd	LOG_REQ(LVL_MOREDEBUG, b, "%s", __func__);
1597172302Spjd
1598172302Spjd	switch (b->bio_cmd) {
1599172302Spjd	case BIO_READ:
1600172302Spjd	case BIO_WRITE:
1601172302Spjd	case BIO_DELETE:
1602172302Spjd		break;
1603172302Spjd	default:
1604172302Spjd		g_io_deliver(b, EOPNOTSUPP);
1605172302Spjd		return;
1606172302Spjd	}
1607172302Spjd
1608172302Spjd	LOG_MSG(LVL_DEBUG2, "BIO arrived, size=%ju", b->bio_length);
1609172302Spjd	bioq_init(&bq);
1610172302Spjd
1611172302Spjd	chunk_size = sc->chunk_size;
1612172302Spjd	addr = b->bio_data;
1613172302Spjd	offset = b->bio_offset;	/* virtual offset and length */
1614172302Spjd	length = b->bio_length;
1615172302Spjd
1616172302Spjd	while (length > 0) {
1617172302Spjd		size_t chunk_index, in_chunk_offset, in_chunk_length;
1618172302Spjd		struct virstor_map_entry *me;
1619172302Spjd
1620172302Spjd		chunk_index = offset / chunk_size; /* round downwards */
1621172302Spjd		in_chunk_offset = offset % chunk_size;
1622172302Spjd		in_chunk_length = min(length, chunk_size - in_chunk_offset);
1623172302Spjd		LOG_MSG(LVL_DEBUG, "Mapped %s(%ju, %ju) to (%zu,%zu,%zu)",
1624172302Spjd		    b->bio_cmd == BIO_READ ? "R" : "W",
1625172302Spjd		    offset, length,
1626172302Spjd		    chunk_index, in_chunk_offset, in_chunk_length);
1627172302Spjd		me = &sc->map[chunk_index];
1628172302Spjd
1629172302Spjd		if (b->bio_cmd == BIO_READ || b->bio_cmd == BIO_DELETE) {
1630172302Spjd			if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
1631172302Spjd				/* Reads from unallocated chunks return zeroed
1632172302Spjd				 * buffers */
1633172302Spjd				if (b->bio_cmd == BIO_READ)
1634172302Spjd					bzero(addr, in_chunk_length);
1635172302Spjd			} else {
1636172302Spjd				comp = &sc->components[me->provider_no];
1637172302Spjd
1638172302Spjd				cb = g_clone_bio(b);
1639172302Spjd				if (cb == NULL) {
1640172302Spjd					bioq_dismantle(&bq);
1641172302Spjd					if (b->bio_error == 0)
1642172302Spjd						b->bio_error = ENOMEM;
1643172302Spjd					g_io_deliver(b, b->bio_error);
1644172302Spjd					return;
1645172302Spjd				}
1646172302Spjd				cb->bio_to = comp->gcons->provider;
1647172302Spjd				cb->bio_done = g_virstor_done;
1648172302Spjd				cb->bio_offset =
1649172302Spjd				    (off_t)me->provider_chunk * (off_t)chunk_size
1650172302Spjd				    + in_chunk_offset;
1651172302Spjd				cb->bio_length = in_chunk_length;
1652172302Spjd				cb->bio_data = addr;
1653172302Spjd				cb->bio_caller1 = comp;
1654172302Spjd				bioq_disksort(&bq, cb);
1655172302Spjd			}
1656172302Spjd		} else { /* handle BIO_WRITE */
1657172302Spjd			KASSERT(b->bio_cmd == BIO_WRITE,
1658172302Spjd			    ("%s: Unknown command %d", __func__,
1659172302Spjd			    b->bio_cmd));
1660172302Spjd
1661172302Spjd			if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0) {
1662172302Spjd				/* We have a virtual chunk, represented by
1663172302Spjd				 * the "me" entry, but it's not yet allocated
1664172302Spjd				 * (tied to) a physical chunk. So do it now. */
1665172302Spjd				struct virstor_map_entry *data_me;
1666172302Spjd				u_int phys_chunk, comp_no;
1667172302Spjd				off_t s_offset;
1668172302Spjd				int error;
1669172302Spjd
1670172302Spjd				error = allocate_chunk(sc, &comp, &comp_no,
1671172302Spjd				    &phys_chunk);
1672172302Spjd				if (error != 0) {
1673172302Spjd					/* We cannot allocate a physical chunk
1674172302Spjd					 * to satisfy this request, so we'll
1675172302Spjd					 * delay it to when we can...
1676172302Spjd					 * XXX: this will prevent the fs from
1677172302Spjd					 * being umounted! */
1678172302Spjd					struct g_virstor_bio_q *biq;
1679172302Spjd					biq = malloc(sizeof *biq, M_GVIRSTOR,
1680172302Spjd					    M_NOWAIT);
1681172302Spjd					if (biq == NULL) {
1682172302Spjd						bioq_dismantle(&bq);
1683172302Spjd						if (b->bio_error == 0)
1684172302Spjd							b->bio_error = ENOMEM;
1685172302Spjd						g_io_deliver(b, b->bio_error);
1686172302Spjd						return;
1687172302Spjd					}
1688172302Spjd					biq->bio = b;
1689172302Spjd					mtx_lock(&sc->delayed_bio_q_mtx);
1690172302Spjd					STAILQ_INSERT_TAIL(&sc->delayed_bio_q,
1691172302Spjd					    biq, linkage);
1692172302Spjd					mtx_unlock(&sc->delayed_bio_q_mtx);
1693172302Spjd					LOG_MSG(LVL_WARNING, "Delaying BIO "
1694172302Spjd					    "(size=%ju) until free physical "
1695172302Spjd					    "space can be found on %s",
1696172302Spjd					    b->bio_length,
1697172302Spjd					    sc->provider->name);
1698172302Spjd					return;
1699172302Spjd				}
1700172302Spjd				LOG_MSG(LVL_DEBUG, "Allocated chunk %u on %s "
1701172302Spjd				    "for %s",
1702172302Spjd				    phys_chunk,
1703172302Spjd				    comp->gcons->provider->name,
1704172302Spjd				    sc->provider->name);
1705172302Spjd
1706172302Spjd				me->provider_no = comp_no;
1707172302Spjd				me->provider_chunk = phys_chunk;
1708172302Spjd				me->flags |= VIRSTOR_MAP_ALLOCATED;
1709172302Spjd
1710172302Spjd				cb = g_clone_bio(b);
1711172302Spjd				if (cb == NULL) {
1712172302Spjd					me->flags &= ~VIRSTOR_MAP_ALLOCATED;
1713172302Spjd					me->provider_no = 0;
1714172302Spjd					me->provider_chunk = 0;
1715172302Spjd					bioq_dismantle(&bq);
1716172302Spjd					if (b->bio_error == 0)
1717172302Spjd						b->bio_error = ENOMEM;
1718172302Spjd					g_io_deliver(b, b->bio_error);
1719172302Spjd					return;
1720172302Spjd				}
1721172302Spjd
1722172302Spjd				/* The allocation table is stored continuously
1723172302Spjd				 * at the start of the drive. We need to
1724172302Spjd				 * calculate the offset of the sector that holds
1725172302Spjd				 * this map entry both on the drive and in the
1726172302Spjd				 * map array.
1727172302Spjd				 * sc_offset will end up pointing to the drive
1728172302Spjd				 * sector. */
1729172302Spjd				s_offset = chunk_index * sizeof *me;
1730172302Spjd				s_offset = (s_offset / sc->sectorsize) *
1731172302Spjd				    sc->sectorsize;
1732172302Spjd
1733172302Spjd				/* data_me points to map entry sector
1734172302Spjd				 * in memory (analoguos to offset) */
1735172302Spjd				data_me = &sc->map[(chunk_index /
1736172302Spjd				    sc->me_per_sector) * sc->me_per_sector];
1737172302Spjd
1738172302Spjd				/* Commit sector with map entry to storage */
1739172302Spjd				cb->bio_to = sc->components[0].gcons->provider;
1740172302Spjd				cb->bio_done = g_virstor_done;
1741172302Spjd				cb->bio_offset = s_offset;
1742172302Spjd				cb->bio_data = (char *)data_me;
1743172302Spjd				cb->bio_length = sc->sectorsize;
1744172302Spjd				cb->bio_caller1 = &sc->components[0];
1745172302Spjd				bioq_disksort(&bq, cb);
1746172302Spjd			}
1747172302Spjd
1748172302Spjd			comp = &sc->components[me->provider_no];
1749172302Spjd			cb = g_clone_bio(b);
1750172302Spjd			if (cb == NULL) {
1751172302Spjd				bioq_dismantle(&bq);
1752172302Spjd				if (b->bio_error == 0)
1753172302Spjd					b->bio_error = ENOMEM;
1754172302Spjd				g_io_deliver(b, b->bio_error);
1755172302Spjd				return;
1756172302Spjd			}
1757172302Spjd			/* Finally, handle the data */
1758172302Spjd			cb->bio_to = comp->gcons->provider;
1759172302Spjd			cb->bio_done = g_virstor_done;
1760172302Spjd			cb->bio_offset = (off_t)me->provider_chunk*(off_t)chunk_size +
1761172302Spjd			    in_chunk_offset;
1762172302Spjd			cb->bio_length = in_chunk_length;
1763172302Spjd			cb->bio_data = addr;
1764172302Spjd			cb->bio_caller1 = comp;
1765172302Spjd			bioq_disksort(&bq, cb);
1766172302Spjd		}
1767172302Spjd		addr += in_chunk_length;
1768172302Spjd		length -= in_chunk_length;
1769172302Spjd		offset += in_chunk_length;
1770172302Spjd	}
1771172302Spjd
1772172302Spjd	/* Fire off bio's here */
1773172302Spjd	count = 0;
1774172302Spjd	for (cb = bioq_first(&bq); cb != NULL; cb = bioq_first(&bq)) {
1775172302Spjd		bioq_remove(&bq, cb);
1776172302Spjd		LOG_REQ(LVL_MOREDEBUG, cb, "Firing request");
1777172302Spjd		comp = cb->bio_caller1;
1778172302Spjd		cb->bio_caller1 = NULL;
1779172302Spjd		LOG_MSG(LVL_DEBUG, " firing bio, offset=%ju, length=%ju",
1780172302Spjd		    cb->bio_offset, cb->bio_length);
1781172302Spjd		g_io_request(cb, comp->gcons);
1782172302Spjd		count++;
1783172302Spjd	}
1784172302Spjd	if (count == 0) { /* We handled everything locally */
1785172302Spjd		b->bio_completed = b->bio_length;
1786172302Spjd		g_io_deliver(b, 0);
1787172302Spjd	}
1788172302Spjd
1789172302Spjd}
1790172302Spjd
1791172302Spjd/*
1792172302Spjd * Allocate a chunk from a physical provider. Returns physical component,
1793172302Spjd * chunk index relative to the component and the component's index.
1794172302Spjd */
1795172302Spjdstatic int
1796172302Spjdallocate_chunk(struct g_virstor_softc *sc, struct g_virstor_component **comp,
1797172302Spjd    u_int *comp_no_p, u_int *chunk)
1798172302Spjd{
1799172302Spjd	u_int comp_no;
1800172302Spjd
1801172302Spjd	KASSERT(sc->curr_component < sc->n_components,
1802172302Spjd	    ("%s: Invalid curr_component: %u",  __func__, sc->curr_component));
1803172302Spjd
1804172302Spjd	comp_no = sc->curr_component;
1805172302Spjd	*comp = &sc->components[comp_no];
1806172302Spjd	dump_component(*comp);
1807172302Spjd	if ((*comp)->chunk_next >= (*comp)->chunk_count) {
1808172302Spjd		/* This component is full. Allocate next component */
1809172302Spjd		if (comp_no >= sc->n_components-1) {
1810172302Spjd			LOG_MSG(LVL_ERROR, "All physical space allocated for %s",
1811172302Spjd			    sc->geom->name);
1812172302Spjd			return (-1);
1813172302Spjd		}
1814172302Spjd		(*comp)->flags &= ~VIRSTOR_PROVIDER_CURRENT;
1815172302Spjd		sc->curr_component = ++comp_no;
1816172302Spjd
1817172302Spjd		*comp = &sc->components[comp_no];
1818172302Spjd		if (comp_no >= sc->n_components - g_virstor_component_watermark-1)
1819172302Spjd			LOG_MSG(LVL_WARNING, "Device %s running out of components "
1820172302Spjd			    "(switching to %u/%u: %s)", sc->geom->name,
1821172302Spjd			    comp_no+1, sc->n_components,
1822172302Spjd			    (*comp)->gcons->provider->name);
1823172302Spjd		/* Take care not to overwrite reserved chunks */
1824172302Spjd		if ( (*comp)->chunk_reserved > 0 &&
1825172302Spjd		    (*comp)->chunk_next < (*comp)->chunk_reserved)
1826172302Spjd			(*comp)->chunk_next = (*comp)->chunk_reserved;
1827172302Spjd
1828172302Spjd		(*comp)->flags |=
1829172302Spjd		    VIRSTOR_PROVIDER_ALLOCATED | VIRSTOR_PROVIDER_CURRENT;
1830172302Spjd		dump_component(*comp);
1831172302Spjd		*comp_no_p = comp_no;
1832172302Spjd		*chunk = (*comp)->chunk_next++;
1833172302Spjd	} else {
1834172302Spjd		*comp_no_p = comp_no;
1835172302Spjd		*chunk = (*comp)->chunk_next++;
1836172302Spjd	}
1837172302Spjd	return (0);
1838172302Spjd}
1839172302Spjd
1840172302Spjd/* Dump a component */
1841172302Spjdstatic void
1842172302Spjddump_component(struct g_virstor_component *comp)
1843172302Spjd{
1844172302Spjd
1845172302Spjd	if (g_virstor_debug < LVL_DEBUG2)
1846172302Spjd		return;
1847172302Spjd	printf("Component %d: %s\n", comp->index, comp->gcons->provider->name);
1848172302Spjd	printf("  chunk_count: %u\n", comp->chunk_count);
1849172302Spjd	printf("   chunk_next: %u\n", comp->chunk_next);
1850172302Spjd	printf("        flags: %u\n", comp->flags);
1851172302Spjd}
1852172302Spjd
1853172302Spjd#if 0
1854172302Spjd/* Dump a map entry */
1855172302Spjdstatic void
1856172302Spjddump_me(struct virstor_map_entry *me, unsigned int nr)
1857172302Spjd{
1858172302Spjd	if (g_virstor_debug < LVL_DEBUG)
1859172302Spjd		return;
1860172302Spjd	printf("VIRT. CHUNK #%d: ", nr);
1861172302Spjd	if ((me->flags & VIRSTOR_MAP_ALLOCATED) == 0)
1862172302Spjd		printf("(unallocated)\n");
1863172302Spjd	else
1864172302Spjd		printf("allocated at provider %u, provider_chunk %u\n",
1865172302Spjd		    me->provider_no, me->provider_chunk);
1866172302Spjd}
1867172302Spjd#endif
1868172302Spjd
1869172302Spjd/*
1870172302Spjd * Dismantle bio_queue and destroy its components
1871172302Spjd */
1872172302Spjdstatic void
1873172302Spjdbioq_dismantle(struct bio_queue_head *bq)
1874172302Spjd{
1875172302Spjd	struct bio *b;
1876172302Spjd
1877172302Spjd	for (b = bioq_first(bq); b != NULL; b = bioq_first(bq)) {
1878172302Spjd		bioq_remove(bq, b);
1879172302Spjd		g_destroy_bio(b);
1880172302Spjd	}
1881172302Spjd}
1882172302Spjd
1883172302Spjd/*
1884172302Spjd * The function that shouldn't be called.
1885172302Spjd * When this is called, the stack is already garbled because of
1886172302Spjd * argument mismatch. There's nothing to do now but panic, which is
1887172302Spjd * accidentally the whole purpose of this function.
1888172302Spjd * Motivation: to guard from accidentally calling geom methods when
1889172302Spjd * they shouldn't be called. (see g_..._taste)
1890172302Spjd */
1891172302Spjdstatic void
1892172302Spjdinvalid_call(void)
1893172302Spjd{
1894172302Spjd	panic("invalid_call() has just been called. Something's fishy here.");
1895172302Spjd}
1896172302Spjd
1897172302SpjdDECLARE_GEOM_CLASS(g_virstor_class, g_virstor); /* Let there be light */
1898