geom_dev.c revision 133318
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_dev.c 133318 2004-08-08 07:57:53Z phk $");
38116196Sobrien
3992108Sphk#include <sys/param.h>
4092108Sphk#include <sys/systm.h>
4192108Sphk#include <sys/malloc.h>
4292108Sphk#include <sys/kernel.h>
4392108Sphk#include <sys/conf.h>
4492108Sphk#include <sys/bio.h>
4592108Sphk#include <sys/lock.h>
4692108Sphk#include <sys/mutex.h>
47130712Sphk#include <sys/proc.h>
4892108Sphk#include <sys/errno.h>
4992108Sphk#include <sys/time.h>
5092108Sphk#include <sys/disk.h>
5192108Sphk#include <sys/fcntl.h>
52114216Skan#include <sys/limits.h>
5392108Sphk#include <geom/geom.h>
5495323Sphk#include <geom/geom_int.h>
5592108Sphk
5692108Sphkstatic d_open_t		g_dev_open;
5792108Sphkstatic d_close_t	g_dev_close;
5892108Sphkstatic d_strategy_t	g_dev_strategy;
5992108Sphkstatic d_ioctl_t	g_dev_ioctl;
6092108Sphk
6192108Sphkstatic struct cdevsw g_dev_cdevsw = {
62126080Sphk	.d_version =	D_VERSION,
63111815Sphk	.d_open =	g_dev_open,
64111815Sphk	.d_close =	g_dev_close,
65111815Sphk	.d_read =	physread,
66111815Sphk	.d_write =	physwrite,
67111815Sphk	.d_ioctl =	g_dev_ioctl,
68111815Sphk	.d_strategy =	g_dev_strategy,
69111815Sphk	.d_name =	"g_dev",
70111815Sphk	.d_maj =	GEOM_MAJOR,
71126080Sphk	.d_flags =	D_DISK | D_TRACKCLOSE,
7292108Sphk};
7392108Sphk
7492108Sphkstatic g_taste_t g_dev_taste;
7592108Sphkstatic g_orphan_t g_dev_orphan;
7692108Sphk
7793248Sphkstatic struct g_class g_dev_class	= {
78112552Sphk	.name = "DEV",
79133318Sphk	.version = G_VERSION,
80112552Sphk	.taste = g_dev_taste,
81133314Sphk	.orphan = g_dev_orphan,
8292108Sphk};
8392108Sphk
84115960Sphkvoid
85105947Sphkg_dev_print(void)
86105947Sphk{
87105947Sphk	struct g_geom *gp;
88115960Sphk	char const *p = "";
89105947Sphk
90115960Sphk	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
91115960Sphk		printf("%s%s", p, gp->name);
92115960Sphk		p = " ";
93115960Sphk	}
94105947Sphk	printf("\n");
95105947Sphk}
96105947Sphk
97119593Sphkstruct g_provider *
98130585Sphkg_dev_getprovider(struct cdev *dev)
99119593Sphk{
100119593Sphk	struct g_consumer *cp;
101119593Sphk
102119593Sphk	if (dev == NULL)
103119593Sphk		return (NULL);
104119593Sphk	if (devsw(dev) != &g_dev_cdevsw)
105119593Sphk		return (NULL);
106119593Sphk	cp = dev->si_drv2;
107119593Sphk	return (cp->provider);
108119593Sphk}
109119593Sphk
110119593Sphk
11192108Sphkstatic struct g_geom *
11293250Sphkg_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
11392108Sphk{
11492108Sphk	struct g_geom *gp;
11592108Sphk	struct g_consumer *cp;
116110540Sphk	static int unit = GEOM_MINOR_PROVIDERS;
11796987Sphk	int error;
118130585Sphk	struct cdev *dev;
11992108Sphk
12092108Sphk	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
12192108Sphk	g_topology_assert();
12292108Sphk	LIST_FOREACH(cp, &pp->consumers, consumers)
12393248Sphk		if (cp->geom->class == mp)
12492108Sphk			return (NULL);
12592108Sphk	gp = g_new_geomf(mp, pp->name);
12692108Sphk	cp = g_new_consumer(gp);
12796987Sphk	error = g_attach(cp, pp);
12896987Sphk	KASSERT(error == 0,
12996987Sphk	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
13096987Sphk	/*
13196987Sphk	 * XXX: I'm not 100% sure we can call make_dev(9) without Giant
13296987Sphk	 * yet.  Once we can, we don't need to drop topology here either.
13396987Sphk	 */
13492108Sphk	g_topology_unlock();
13592108Sphk	mtx_lock(&Giant);
13696987Sphk	dev = make_dev(&g_dev_cdevsw, unit2minor(unit++),
137104316Sphk	    UID_ROOT, GID_OPERATOR, 0640, gp->name);
138110700Sphk	if (pp->flags & G_PF_CANDELETE)
139110700Sphk		dev->si_flags |= SI_CANDELETE;
14096987Sphk	mtx_unlock(&Giant);
14196987Sphk	g_topology_lock();
142110728Sphk	dev->si_iosize_max = MAXPHYS;
143110710Sphk	dev->si_stripesize = pp->stripesize;
144110710Sphk	dev->si_stripeoffset = pp->stripeoffset;
14592108Sphk	gp->softc = dev;
14692108Sphk	dev->si_drv1 = gp;
14792108Sphk	dev->si_drv2 = cp;
14892108Sphk	return (gp);
14992108Sphk}
15092108Sphk
15192108Sphkstatic int
152130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
15392108Sphk{
15492108Sphk	struct g_geom *gp;
15592108Sphk	struct g_consumer *cp;
15692108Sphk	int error, r, w, e;
15792108Sphk
15892108Sphk	gp = dev->si_drv1;
15992108Sphk	cp = dev->si_drv2;
160112978Sphk	if (gp == NULL || cp == NULL || gp->softc != dev)
161112978Sphk		return(ENXIO);		/* g_dev_taste() not done yet */
162112978Sphk
16392108Sphk	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
16492108Sphk	    gp->name, flags, fmt, td);
165130712Sphk
16692108Sphk	r = flags & FREAD ? 1 : 0;
16792108Sphk	w = flags & FWRITE ? 1 : 0;
168103004Sphk#ifdef notyet
16992108Sphk	e = flags & O_EXCL ? 1 : 0;
170103004Sphk#else
171103004Sphk	e = 0;
172103004Sphk#endif
173130712Sphk	if (w) {
174130712Sphk		/*
175130712Sphk		 * When running in very secure mode, do not allow
176130712Sphk		 * opens for writing of any disks.
177130712Sphk		 */
178130712Sphk		error = securelevel_ge(td->td_ucred, 2);
179130712Sphk		if (error)
180130712Sphk			return (error);
181130712Sphk	}
182112978Sphk	g_topology_lock();
183112978Sphk	if (dev->si_devsw == NULL)
184112978Sphk		error = ENXIO;		/* We were orphaned */
185112978Sphk	else
186125755Sphk		error = g_access(cp, r, w, e);
18792108Sphk	g_topology_unlock();
18898066Sphk	g_waitidle();
189112978Sphk	if (!error)
190112978Sphk		dev->si_bsize_phys = cp->provider->sectorsize;
19192108Sphk	return(error);
19292108Sphk}
19392108Sphk
19492108Sphkstatic int
195130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
19692108Sphk{
19792108Sphk	struct g_geom *gp;
19892108Sphk	struct g_consumer *cp;
199114864Sphk	int error, r, w, e, i;
20092108Sphk
20192108Sphk	gp = dev->si_drv1;
20292108Sphk	cp = dev->si_drv2;
20392108Sphk	if (gp == NULL || cp == NULL)
20492108Sphk		return(ENXIO);
20592108Sphk	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
20692108Sphk	    gp->name, flags, fmt, td);
20792108Sphk	r = flags & FREAD ? -1 : 0;
20892108Sphk	w = flags & FWRITE ? -1 : 0;
209103004Sphk#ifdef notyet
21092108Sphk	e = flags & O_EXCL ? -1 : 0;
211103004Sphk#else
212103004Sphk	e = 0;
213103004Sphk#endif
214112978Sphk	g_topology_lock();
215112978Sphk	if (dev->si_devsw == NULL)
216112978Sphk		error = ENXIO;		/* We were orphaned */
217112978Sphk	else
218125755Sphk		error = g_access(cp, r, w, e);
219114864Sphk	for (i = 0; i < 10 * hz;) {
220114864Sphk		if (cp->acr != 0 || cp->acw != 0)
221114864Sphk			break;
222114864Sphk 		if (cp->nstart == cp->nend)
223114864Sphk			break;
224114864Sphk		tsleep(&i, PRIBIO, "gdevwclose", hz / 10);
225114864Sphk		i += hz / 10;
226114864Sphk	}
227114864Sphk	if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
228124880Sphk		printf("WARNING: Final close of geom_dev(%s) %s %s\n",
229114864Sphk		    gp->name,
230114864Sphk		    "still has outstanding I/O after 10 seconds.",
231114864Sphk		    "Completing close anyway, panic may happen later.");
232114864Sphk	}
23392108Sphk	g_topology_unlock();
23498066Sphk	g_waitidle();
23592108Sphk	return (error);
23692108Sphk}
23792108Sphk
238112978Sphk/*
239112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against
240112978Sphk * XXX: a concurrent orphanization.  We cannot close it by holding topology
241112978Sphk * XXX: since that would prevent us from doing our job, and stalling events
242112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks.
243112978Sphk */
24492108Sphkstatic int
245130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
24692108Sphk{
247115515Sphk	struct g_geom *gp;
24892108Sphk	struct g_consumer *cp;
24995038Sphk	struct g_kerneldump kd;
25092108Sphk	int i, error;
25195038Sphk	u_int u;
25292108Sphk
25392108Sphk	gp = dev->si_drv1;
25492108Sphk	cp = dev->si_drv2;
25592108Sphk
25692108Sphk	error = 0;
257112978Sphk	KASSERT(cp->acr || cp->acw,
258112978Sphk	    ("Consumer with zero access count in g_dev_ioctl"));
25992403Sphk
26092698Sphk	i = IOCPARM_LEN(cmd);
26192698Sphk	switch (cmd) {
26292698Sphk	case DIOCGSECTORSIZE:
263105551Sphk		*(u_int *)data = cp->provider->sectorsize;
264105551Sphk		if (*(u_int *)data == 0)
265105180Snjl			error = ENOENT;
26692698Sphk		break;
26792698Sphk	case DIOCGMEDIASIZE:
268105551Sphk		*(off_t *)data = cp->provider->mediasize;
269105551Sphk		if (*(off_t *)data == 0)
270105180Snjl			error = ENOENT;
27192698Sphk		break;
27292698Sphk	case DIOCGFWSECTORS:
27393250Sphk		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
274105180Snjl		if (error == 0 && *(u_int *)data == 0)
275105180Snjl			error = ENOENT;
27692698Sphk		break;
27792698Sphk	case DIOCGFWHEADS:
27893250Sphk		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
279105180Snjl		if (error == 0 && *(u_int *)data == 0)
280105180Snjl			error = ENOENT;
28192698Sphk		break;
28294287Sphk	case DIOCGFRONTSTUFF:
28394287Sphk		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
28494287Sphk		break;
28595038Sphk	case DIOCSKERNELDUMP:
28695038Sphk		u = *((u_int *)data);
28795038Sphk		if (!u) {
28895038Sphk			set_dumper(NULL);
28995038Sphk			error = 0;
29095038Sphk			break;
29195038Sphk		}
29295038Sphk		kd.offset = 0;
29395038Sphk		kd.length = OFF_MAX;
29495038Sphk		i = sizeof kd;
29595038Sphk		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
29695038Sphk		if (!error)
29795038Sphk			dev->si_flags |= SI_DUMPDEV;
29895038Sphk		break;
299104602Sphk
30092698Sphk	default:
301119660Sphk		if (cp->provider->geom->ioctl != NULL) {
302119660Sphk			error = cp->provider->geom->ioctl(cp->provider, cmd, data, td);
303119749Sphk		} else {
304119749Sphk			error = ENOIOCTL;
305119660Sphk		}
30692698Sphk	}
30792403Sphk
30898066Sphk	g_waitidle();
30992108Sphk	return (error);
31092108Sphk}
31192108Sphk
31292108Sphkstatic void
31392108Sphkg_dev_done(struct bio *bp2)
31492108Sphk{
31592108Sphk	struct bio *bp;
31692108Sphk
317110517Sphk	bp = bp2->bio_parent;
31892108Sphk	bp->bio_error = bp2->bio_error;
31992108Sphk	if (bp->bio_error != 0) {
32092108Sphk		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
32192108Sphk		    bp2, bp->bio_error);
32292108Sphk		bp->bio_flags |= BIO_ERROR;
32392108Sphk	} else {
324105540Sphk		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
325105540Sphk		    bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
32692108Sphk	}
32792108Sphk	bp->bio_resid = bp->bio_bcount - bp2->bio_completed;
32892108Sphk	g_destroy_bio(bp2);
32992108Sphk	biodone(bp);
33092108Sphk}
33192108Sphk
33292108Sphkstatic void
33392108Sphkg_dev_strategy(struct bio *bp)
33492108Sphk{
33592108Sphk	struct g_consumer *cp;
33692108Sphk	struct bio *bp2;
337130585Sphk	struct cdev *dev;
33892108Sphk
339106300Sphk	KASSERT(bp->bio_cmd == BIO_READ ||
340106300Sphk	        bp->bio_cmd == BIO_WRITE ||
341106300Sphk	        bp->bio_cmd == BIO_DELETE,
342106300Sphk		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
34392108Sphk	dev = bp->bio_dev;
34492108Sphk	cp = dev->si_drv2;
345112978Sphk	KASSERT(cp->acr || cp->acw,
346112978Sphk	    ("Consumer with zero access count in g_dev_strategy"));
347112978Sphk
348118869Sphk	for (;;) {
349118869Sphk		/*
350118869Sphk		 * XXX: This is not an ideal solution, but I belive it to
351118869Sphk		 * XXX: deadlock safe, all things considered.
352118869Sphk		 */
353118869Sphk		bp2 = g_clone_bio(bp);
354118869Sphk		if (bp2 != NULL)
355118869Sphk			break;
356118869Sphk		tsleep(&bp, PRIBIO, "gdstrat", hz / 10);
357118869Sphk	}
358107834Sphk	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
35992108Sphk	bp2->bio_length = (off_t)bp->bio_bcount;
36092108Sphk	bp2->bio_done = g_dev_done;
36192108Sphk	g_trace(G_T_BIO,
362105540Sphk	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
363105540Sphk	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
364105540Sphk	    bp2->bio_data, bp2->bio_cmd);
36592108Sphk	g_io_request(bp2, cp);
366112978Sphk	KASSERT(cp->acr || cp->acw,
367112978Sphk	    ("g_dev_strategy raced with g_dev_close and lost"));
368112978Sphk
36992108Sphk}
37092108Sphk
37196987Sphk/*
37296987Sphk * g_dev_orphan()
37396987Sphk *
374112024Sphk * Called from below when the provider orphaned us.
375112024Sphk * - Clear any dump settings.
376130585Sphk * - Destroy the struct cdev *to prevent any more request from coming in.  The
377112024Sphk *   provider is already marked with an error, so anything which comes in
378112024Sphk *   in the interrim will be returned immediately.
379112024Sphk * - Wait for any outstanding I/O to finish.
380112024Sphk * - Set our access counts to zero, whatever they were.
381112024Sphk * - Detach and self-destruct.
38296987Sphk */
38392108Sphk
38492108Sphkstatic void
38593250Sphkg_dev_orphan(struct g_consumer *cp)
38692108Sphk{
38792108Sphk	struct g_geom *gp;
388130585Sphk	struct cdev *dev;
38992108Sphk
390112024Sphk	g_topology_assert();
39192108Sphk	gp = cp->geom;
392112024Sphk	dev = gp->softc;
39392108Sphk	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
394112024Sphk
395112024Sphk	/* Reset any dump-area set on this device */
39695038Sphk	if (dev->si_flags & SI_DUMPDEV)
39795038Sphk		set_dumper(NULL);
398112024Sphk
399130585Sphk	/* Destroy the struct cdev *so we get no more requests */
40092108Sphk	destroy_dev(dev);
401112024Sphk
402112024Sphk	/* Wait for the cows to come home */
403112024Sphk	while (cp->nstart != cp->nend)
404112978Sphk		msleep(&dev, NULL, PRIBIO, "gdevorphan", hz / 10);
405112024Sphk
40692108Sphk	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
407125755Sphk		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
408112024Sphk
40998066Sphk	g_detach(cp);
41092108Sphk	g_destroy_consumer(cp);
41192108Sphk	g_destroy_geom(gp);
41292108Sphk}
41392108Sphk
41496987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev);
415