geom_slice.c revision 131046
192108Sphk/*-
292108Sphk * Copyright (c) 2002 Poul-Henning Kamp
392108Sphk * Copyright (c) 2002 Networks Associates Technology, Inc.
492108Sphk * All rights reserved.
592108Sphk *
692108Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp
792108Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc.
892108Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
992108Sphk * DARPA CHATS research program.
1092108Sphk *
1192108Sphk * Redistribution and use in source and binary forms, with or without
1292108Sphk * modification, are permitted provided that the following conditions
1392108Sphk * are met:
1492108Sphk * 1. Redistributions of source code must retain the above copyright
1592108Sphk *    notice, this list of conditions and the following disclaimer.
1692108Sphk * 2. Redistributions in binary form must reproduce the above copyright
1792108Sphk *    notice, this list of conditions and the following disclaimer in the
1892108Sphk *    documentation and/or other materials provided with the distribution.
1992108Sphk * 3. The names of the authors may not be used to endorse or promote
2092108Sphk *    products derived from this software without specific prior written
2192108Sphk *    permission.
2292108Sphk *
2392108Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2492108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2592108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2692108Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2792108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2892108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2992108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3092108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3192108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3292108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3392108Sphk * SUCH DAMAGE.
3492108Sphk */
3592108Sphk
36116196Sobrien#include <sys/cdefs.h>
37116196Sobrien__FBSDID("$FreeBSD: head/sys/geom/geom_slice.c 131046 2004-06-24 10:50:20Z pjd $");
3892108Sphk
3992108Sphk#include <sys/param.h>
4092108Sphk#include <sys/systm.h>
4192108Sphk#include <sys/kernel.h>
4292108Sphk#include <sys/malloc.h>
4392108Sphk#include <sys/bio.h>
4492108Sphk#include <sys/sysctl.h>
4592108Sphk#include <sys/proc.h>
4692108Sphk#include <sys/kthread.h>
4792108Sphk#include <sys/lock.h>
4892108Sphk#include <sys/mutex.h>
4992108Sphk#include <sys/errno.h>
5092108Sphk#include <sys/sbuf.h>
5192108Sphk#include <geom/geom.h>
5292108Sphk#include <geom/geom_slice.h>
5392108Sphk#include <machine/stdarg.h>
5492108Sphk
5593776Sphkstatic g_orphan_t g_slice_orphan;
5693776Sphkstatic g_access_t g_slice_access;
5793776Sphkstatic g_start_t g_slice_start;
5893776Sphk
5995321Sphkstatic struct g_slicer *
60114493Sphkg_slice_alloc(unsigned nslice, unsigned scsize)
6192108Sphk{
6292108Sphk	struct g_slicer *gsp;
6392108Sphk
64111119Simp	gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO);
65131046Spjd	if (scsize > 0)
66131046Spjd		gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO);
67131046Spjd	else
68131046Spjd		gsp->softc = NULL;
69104057Sphk	gsp->slices = g_malloc(nslice * sizeof(struct g_slice),
70111119Simp	    M_WAITOK | M_ZERO);
7192108Sphk	gsp->nslice = nslice;
7292108Sphk	return (gsp);
7392108Sphk}
7492108Sphk
75114493Sphkstatic void
76114493Sphkg_slice_free(struct g_slicer *gsp)
77114493Sphk{
78114493Sphk
79114493Sphk	g_free(gsp->slices);
80114493Sphk	if (gsp->hotspot != NULL)
81114493Sphk		g_free(gsp->hotspot);
82114493Sphk	g_free(gsp->softc);
83114493Sphk	g_free(gsp);
84114493Sphk}
85114493Sphk
8693776Sphkstatic int
8792108Sphkg_slice_access(struct g_provider *pp, int dr, int dw, int de)
8892108Sphk{
89107953Sphk	int error;
90107953Sphk	u_int u;
9192108Sphk	struct g_geom *gp;
9292108Sphk	struct g_consumer *cp;
9392108Sphk	struct g_provider *pp2;
9492108Sphk	struct g_slicer *gsp;
9592108Sphk	struct g_slice *gsl, *gsl2;
9692108Sphk
9792108Sphk	gp = pp->geom;
9892108Sphk	cp = LIST_FIRST(&gp->consumer);
9992108Sphk	KASSERT (cp != NULL, ("g_slice_access but no consumer"));
10092108Sphk	gsp = gp->softc;
10192108Sphk	gsl = &gsp->slices[pp->index];
102107953Sphk	for (u = 0; u < gsp->nslice; u++) {
103107953Sphk		gsl2 = &gsp->slices[u];
10492108Sphk		if (gsl2->length == 0)
10592108Sphk			continue;
106107953Sphk		if (u == pp->index)
10792108Sphk			continue;
10892108Sphk		if (gsl->offset + gsl->length <= gsl2->offset)
10992108Sphk			continue;
11092108Sphk		if (gsl2->offset + gsl2->length <= gsl->offset)
11192108Sphk			continue;
11292108Sphk		/* overlap */
11392108Sphk		pp2 = gsl2->provider;
11492108Sphk		if ((pp->acw + dw) > 0 && pp2->ace > 0)
11592108Sphk			return (EPERM);
11692108Sphk		if ((pp->ace + de) > 0 && pp2->acw > 0)
11792108Sphk			return (EPERM);
11892108Sphk	}
11992108Sphk	/* On first open, grab an extra "exclusive" bit */
12092108Sphk	if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
12192108Sphk		de++;
12292108Sphk	/* ... and let go of it on last close */
12392108Sphk	if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1)
12492108Sphk		de--;
125125755Sphk	error = g_access(cp, dr, dw, de);
12692108Sphk	return (error);
12792108Sphk}
12892108Sphk
129113713Sphk/*
130113713Sphk * XXX: It should be possible to specify here if we should finish all of the
131113713Sphk * XXX: bio, or only the non-hot bits.  This would get messy if there were
132113713Sphk * XXX: two hot spots in the same bio, so for now we simply finish off the
133113713Sphk * XXX: entire bio.  Modifying hot data on the way to disk is frowned on
134113713Sphk * XXX: so making that considerably harder is not a bad idea anyway.
135113713Sphk */
136107522Sphkvoid
137107522Sphkg_slice_finish_hot(struct bio *bp)
138107522Sphk{
139107522Sphk	struct bio *bp2;
140107522Sphk	struct g_geom *gp;
141107522Sphk	struct g_consumer *cp;
142107522Sphk	struct g_slicer *gsp;
143107522Sphk	struct g_slice *gsl;
144107953Sphk	int idx;
145107522Sphk
146113713Sphk	KASSERT(bp->bio_to != NULL,
147113713Sphk	    ("NULL bio_to in g_slice_finish_hot(%p)", bp));
148113713Sphk	KASSERT(bp->bio_from != NULL,
149113713Sphk	    ("NULL bio_from in g_slice_finish_hot(%p)", bp));
150107522Sphk	gp = bp->bio_to->geom;
151107522Sphk	gsp = gp->softc;
152107522Sphk	cp = LIST_FIRST(&gp->consumer);
153107522Sphk	KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp));
154107953Sphk	idx = bp->bio_to->index;
155107953Sphk	gsl = &gsp->slices[idx];
156107522Sphk
157107522Sphk	bp2 = g_clone_bio(bp);
158107522Sphk	if (bp2 == NULL) {
159107522Sphk		g_io_deliver(bp, ENOMEM);
160107522Sphk		return;
161107522Sphk	}
162107522Sphk	if (bp2->bio_offset + bp2->bio_length > gsl->length)
163107522Sphk		bp2->bio_length = gsl->length - bp2->bio_offset;
164107522Sphk	bp2->bio_done = g_std_done;
165107522Sphk	bp2->bio_offset += gsl->offset;
166107522Sphk	g_io_request(bp2, cp);
167107522Sphk	return;
168107522Sphk}
169107522Sphk
17093776Sphkstatic void
17192108Sphkg_slice_start(struct bio *bp)
17292108Sphk{
17392108Sphk	struct bio *bp2;
17492108Sphk	struct g_provider *pp;
17592108Sphk	struct g_geom *gp;
17692108Sphk	struct g_consumer *cp;
17792108Sphk	struct g_slicer *gsp;
178113712Sphk	struct g_slice *gsl;
179113713Sphk	struct g_slice_hot *ghp;
180107953Sphk	int idx, error;
181107522Sphk	u_int m_index;
18294287Sphk	off_t t;
18392108Sphk
18492108Sphk	pp = bp->bio_to;
18592108Sphk	gp = pp->geom;
18692108Sphk	gsp = gp->softc;
18792108Sphk	cp = LIST_FIRST(&gp->consumer);
188107953Sphk	idx = pp->index;
189107953Sphk	gsl = &gsp->slices[idx];
19092108Sphk	switch(bp->bio_cmd) {
19192108Sphk	case BIO_READ:
19292108Sphk	case BIO_WRITE:
19392108Sphk	case BIO_DELETE:
19492108Sphk		if (bp->bio_offset > gsl->length) {
195104195Sphk			g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */
19692108Sphk			return;
19792108Sphk		}
198107522Sphk		/*
199107522Sphk		 * Check if we collide with any hot spaces, and call the
200107522Sphk		 * method once if so.
201107522Sphk		 */
202107832Sphk		t = bp->bio_offset + gsl->offset;
203113712Sphk		for (m_index = 0; m_index < gsp->nhotspot; m_index++) {
204113713Sphk			ghp = &gsp->hotspot[m_index];
205113713Sphk			if (t >= ghp->offset + ghp->length)
206107522Sphk				continue;
207113713Sphk			if (t + bp->bio_length <= ghp->offset)
208107522Sphk				continue;
209113713Sphk			switch(bp->bio_cmd) {
210113713Sphk			case BIO_READ:		idx = ghp->ract; break;
211113713Sphk			case BIO_WRITE:		idx = ghp->wact; break;
212113713Sphk			case BIO_DELETE:	idx = ghp->dact; break;
213113713Sphk			}
214113713Sphk			switch(idx) {
215113713Sphk			case G_SLICE_HOT_ALLOW:
216113713Sphk				/* Fall out and continue normal processing */
217113713Sphk				continue;
218113713Sphk			case G_SLICE_HOT_DENY:
219113713Sphk				g_io_deliver(bp, EROFS);
220107522Sphk				return;
221113713Sphk			case G_SLICE_HOT_START:
222113713Sphk				error = gsp->start(bp);
223113713Sphk				if (error && error != EJUSTRETURN)
224113713Sphk					g_io_deliver(bp, error);
225107522Sphk				return;
226113713Sphk			case G_SLICE_HOT_CALL:
227113937Sphk				error = g_post_event(gsp->hot, bp, M_NOWAIT,
228113937Sphk				    gp, NULL);
229113713Sphk				if (error)
230113713Sphk					g_io_deliver(bp, error);
231113713Sphk				return;
232107522Sphk			}
233107522Sphk			break;
234107522Sphk		}
23592108Sphk		bp2 = g_clone_bio(bp);
236104057Sphk		if (bp2 == NULL) {
237104195Sphk			g_io_deliver(bp, ENOMEM);
238104057Sphk			return;
239104057Sphk		}
24092108Sphk		if (bp2->bio_offset + bp2->bio_length > gsl->length)
24192108Sphk			bp2->bio_length = gsl->length - bp2->bio_offset;
24292108Sphk		bp2->bio_done = g_std_done;
24392108Sphk		bp2->bio_offset += gsl->offset;
24492108Sphk		g_io_request(bp2, cp);
24592108Sphk		return;
24692108Sphk	case BIO_GETATTR:
24794287Sphk		/* Give the real method a chance to override */
248113878Sphk		if (gsp->start != NULL && gsp->start(bp))
24994287Sphk			return;
25095038Sphk		if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) {
25195038Sphk			struct g_kerneldump *gkd;
25295038Sphk
25395038Sphk			gkd = (struct g_kerneldump *)bp->bio_data;
254107953Sphk			gkd->offset += gsp->slices[idx].offset;
255107953Sphk			if (gkd->length > gsp->slices[idx].length)
256107953Sphk				gkd->length = gsp->slices[idx].length;
25795038Sphk			/* now, pass it on downwards... */
25895038Sphk		}
25992108Sphk		bp2 = g_clone_bio(bp);
260104081Sphk		if (bp2 == NULL) {
261104195Sphk			g_io_deliver(bp, ENOMEM);
262104081Sphk			return;
263104081Sphk		}
26492108Sphk		bp2->bio_done = g_std_done;
26592108Sphk		g_io_request(bp2, cp);
26692108Sphk		break;
26792108Sphk	default:
268104195Sphk		g_io_deliver(bp, EOPNOTSUPP);
26992108Sphk		return;
27092108Sphk	}
27192108Sphk}
27292108Sphk
27392108Sphkvoid
274107953Sphkg_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
27592108Sphk{
27692108Sphk	struct g_slicer *gsp;
27792108Sphk
27892108Sphk	gsp = gp->softc;
279106101Sphk	if (indent == NULL) {
280106101Sphk		sbuf_printf(sb, " i %u", pp->index);
281106101Sphk		sbuf_printf(sb, " o %ju",
282106101Sphk		    (uintmax_t)gsp->slices[pp->index].offset);
283106101Sphk		return;
284106101Sphk	}
28592108Sphk	if (pp != NULL) {
28692108Sphk		sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index);
287105540Sphk		sbuf_printf(sb, "%s<length>%ju</length>\n",
288105540Sphk		    indent, (uintmax_t)gsp->slices[pp->index].length);
289105540Sphk		sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent,
290105540Sphk		    (uintmax_t)gsp->slices[pp->index].length / 512);
291105540Sphk		sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent,
292105540Sphk		    (uintmax_t)gsp->slices[pp->index].offset);
293105540Sphk		sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent,
294105540Sphk		    (uintmax_t)gsp->slices[pp->index].offset / 512);
29592108Sphk	}
29692108Sphk}
29792108Sphk
298104064Sphkint
299107953Sphkg_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...)
300104064Sphk{
301110710Sphk	struct g_provider *pp, *pp2;
302104064Sphk	struct g_slicer *gsp;
303104064Sphk	struct g_slice *gsl;
304104064Sphk	va_list ap;
305104064Sphk	struct sbuf *sb;
306115506Sphk	int acc;
307104064Sphk
308104195Sphk	g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)",
309107953Sphk	     gp->name, idx, how);
310104064Sphk	g_topology_assert();
311104064Sphk	gsp = gp->softc;
312107953Sphk	if (idx >= gsp->nslice)
313104064Sphk		return(EINVAL);
314107953Sphk	gsl = &gsp->slices[idx];
315104064Sphk	pp = gsl->provider;
316104064Sphk	if (pp != NULL)
317104064Sphk		acc = pp->acr + pp->acw + pp->ace;
318104064Sphk	else
319104064Sphk		acc = 0;
320104064Sphk	if (acc != 0 && how != G_SLICE_CONFIG_FORCE) {
321104064Sphk		if (length < gsl->length)
322104064Sphk			return(EBUSY);
323104064Sphk		if (offset != gsl->offset)
324104064Sphk			return(EBUSY);
325104064Sphk	}
326104064Sphk	/* XXX: check offset + length <= MEDIASIZE */
327104064Sphk	if (how == G_SLICE_CONFIG_CHECK)
328104064Sphk		return (0);
329104064Sphk	gsl->length = length;
330104064Sphk	gsl->offset = offset;
331105542Sphk	gsl->sectorsize = sectorsize;
332105957Sphk	if (length == 0) {
333105957Sphk		if (pp == NULL)
334105957Sphk			return (0);
335105957Sphk		if (bootverbose)
336105957Sphk			printf("GEOM: Deconfigure %s\n", pp->name);
337104064Sphk		g_orphan_provider(pp, ENXIO);
338104064Sphk		gsl->provider = NULL;
339104064Sphk		gsp->nprovider--;
340104064Sphk		return (0);
341104064Sphk	}
342105957Sphk	if (pp != NULL) {
343105957Sphk		if (bootverbose)
344105957Sphk			printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n",
345105957Sphk			    pp->name, (intmax_t)offset, (intmax_t)length,
346105957Sphk			    (intmax_t)(offset + length - 1));
347107116Sphk		pp->mediasize = gsl->length;
348105957Sphk		return (0);
349105957Sphk	}
350115949Sphk	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
351104064Sphk	va_start(ap, fmt);
352104064Sphk	sbuf_vprintf(sb, fmt, ap);
353115949Sphk	va_end(ap);
354104064Sphk	sbuf_finish(sb);
355104064Sphk	pp = g_new_providerf(gp, sbuf_data(sb));
356110710Sphk	pp2 = LIST_FIRST(&gp->consumer)->provider;
357110710Sphk	pp->flags = pp2->flags & G_PF_CANDELETE;
358110713Sphk	if (pp2->stripesize > 0) {
359110713Sphk		pp->stripesize = pp2->stripesize;
360110713Sphk		pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize;
361110713Sphk	}
362105957Sphk	if (bootverbose)
363105957Sphk		printf("GEOM: Configure %s, start %jd length %jd end %jd\n",
364105957Sphk		    pp->name, (intmax_t)offset, (intmax_t)length,
365105957Sphk		    (intmax_t)(offset + length - 1));
366107953Sphk	pp->index = idx;
367105542Sphk	pp->mediasize = gsl->length;
368105542Sphk	pp->sectorsize = gsl->sectorsize;
369104064Sphk	gsl->provider = pp;
370104064Sphk	gsp->nprovider++;
371104064Sphk	g_error_provider(pp, 0);
372104064Sphk	sbuf_delete(sb);
373104064Sphk	return(0);
374104064Sphk}
375104064Sphk
376113713Sphk/*
377113713Sphk * Configure "hotspots".  A hotspot is a piece of the parent device which
378113713Sphk * this particular slicer cares about for some reason.  Typically because
379113713Sphk * it contains meta-data used to configure the slicer.
380113713Sphk * A hotspot is identified by its index number. The offset and length are
381113713Sphk * relative to the parent device, and the three "?act" fields specify
382113713Sphk * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE.
383113713Sphk *
384113713Sphk * XXX: There may be a race relative to g_slice_start() here, if an existing
385113713Sphk * XXX: hotspot is changed wile I/O is happening.  Should this become a problem
386113713Sphk * XXX: we can protect the hotspot stuff with a mutex.
387113713Sphk */
388113713Sphk
389107522Sphkint
390113713Sphkg_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact)
391107522Sphk{
392107522Sphk	struct g_slicer *gsp;
393113712Sphk	struct g_slice_hot *gsl, *gsl2;
394107522Sphk
395113713Sphk	g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)",
396113713Sphk	    gp->name, idx, (intmax_t)offset, (intmax_t)length);
397107522Sphk	g_topology_assert();
398107522Sphk	gsp = gp->softc;
399113712Sphk	gsl = gsp->hotspot;
400113712Sphk	if(idx >= gsp->nhotspot) {
401111119Simp		gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO);
402113712Sphk		if (gsp->hotspot != NULL)
403113712Sphk			bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2);
404113712Sphk		gsp->hotspot = gsl2;
405113712Sphk		if (gsp->hotspot != NULL)
406107522Sphk			g_free(gsl);
407107522Sphk		gsl = gsl2;
408113712Sphk		gsp->nhotspot = idx + 1;
409107522Sphk	}
410107953Sphk	gsl[idx].offset = offset;
411107953Sphk	gsl[idx].length = length;
412113878Sphk	KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START)
413113878Sphk	    || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start"));
414113878Sphk	/* XXX: check that we _have_ a start function if HOT_START specified */
415113713Sphk	gsl[idx].ract = ract;
416113713Sphk	gsl[idx].dact = dact;
417113713Sphk	gsl[idx].wact = wact;
418107522Sphk	return (0);
419107522Sphk}
420107522Sphk
421114504Sphkvoid
422114504Sphkg_slice_spoiled(struct g_consumer *cp)
423114504Sphk{
424114504Sphk	struct g_geom *gp;
425114504Sphk	struct g_slicer *gsp;
426114504Sphk
427114504Sphk	g_topology_assert();
428114504Sphk	gp = cp->geom;
429114504Sphk	g_trace(G_T_TOPOLOGY, "g_slice_spoiled(%p/%s)", cp, gp->name);
430114504Sphk	gsp = gp->softc;
431114504Sphk	gp->softc = NULL;
432114504Sphk	g_slice_free(gsp);
433114504Sphk	g_wither_geom(gp, ENXIO);
434114504Sphk}
435114504Sphk
436115506Sphkint
437115506Sphkg_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
438115506Sphk{
439115506Sphk
440115506Sphk	g_slice_spoiled(LIST_FIRST(&gp->consumer));
441115506Sphk	return (0);
442115506Sphk}
443115506Sphk
44492108Sphkstruct g_geom *
445107522Sphkg_slice_new(struct g_class *mp, u_int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start)
44692108Sphk{
44792108Sphk	struct g_geom *gp;
44892108Sphk	struct g_slicer *gsp;
44992108Sphk	struct g_consumer *cp;
45092108Sphk	void **vp;
451113390Sphk	int error;
45292108Sphk
45392108Sphk	g_topology_assert();
45492108Sphk	vp = (void **)extrap;
45592108Sphk	gp = g_new_geomf(mp, "%s", pp->name);
456114493Sphk	gsp = g_slice_alloc(slices, extra);
45792108Sphk	gsp->start = start;
45893776Sphk	gp->access = g_slice_access;
45993776Sphk	gp->orphan = g_slice_orphan;
46092108Sphk	gp->softc = gsp;
46192108Sphk	gp->start = g_slice_start;
462114504Sphk	gp->spoiled = g_slice_spoiled;
46392108Sphk	gp->dumpconf = g_slice_dumpconf;
464115506Sphk	if (gp->class->destroy_geom == NULL)
465115506Sphk		gp->class->destroy_geom = g_slice_destroy_geom;
46692108Sphk	cp = g_new_consumer(gp);
467105551Sphk	error = g_attach(cp, pp);
468105551Sphk	if (error == 0)
469125755Sphk		error = g_access(cp, 1, 0, 0);
47092108Sphk	if (error) {
471114504Sphk		g_wither_geom(gp, ENXIO);
47292108Sphk		return (NULL);
47392108Sphk	}
474131046Spjd	if (extrap != NULL)
475131046Spjd		*vp = gsp->softc;
47692108Sphk	*cpp = cp;
47792108Sphk	return (gp);
47892108Sphk}
47992108Sphk
48093776Sphkstatic void
48193250Sphkg_slice_orphan(struct g_consumer *cp)
48292108Sphk{
48392108Sphk
48492108Sphk	g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name);
48592108Sphk	g_topology_assert();
48692108Sphk	KASSERT(cp->provider->error != 0,
48792108Sphk	    ("g_slice_orphan with error == 0"));
48892108Sphk
489107522Sphk	/* XXX: Not good enough we leak the softc and its suballocations */
490114504Sphk	g_slice_free(cp->geom->softc);
491114504Sphk	g_wither_geom(cp->geom, cp->provider->error);
49292108Sphk}
493