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