geom_dev.c revision 219950
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 219950 2011-03-24 08:37:48Z mav $");
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",
70126080Sphk	.d_flags =	D_DISK | D_TRACKCLOSE,
7192108Sphk};
7292108Sphk
7392108Sphkstatic g_taste_t g_dev_taste;
7492108Sphkstatic g_orphan_t g_dev_orphan;
7592108Sphk
7693248Sphkstatic struct g_class g_dev_class	= {
77112552Sphk	.name = "DEV",
78133318Sphk	.version = G_VERSION,
79112552Sphk	.taste = g_dev_taste,
80133314Sphk	.orphan = g_dev_orphan,
8192108Sphk};
8292108Sphk
83115960Sphkvoid
84105947Sphkg_dev_print(void)
85105947Sphk{
86105947Sphk	struct g_geom *gp;
87115960Sphk	char const *p = "";
88105947Sphk
89115960Sphk	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
90115960Sphk		printf("%s%s", p, gp->name);
91115960Sphk		p = " ";
92115960Sphk	}
93105947Sphk	printf("\n");
94105947Sphk}
95105947Sphk
96119593Sphkstruct g_provider *
97130585Sphkg_dev_getprovider(struct cdev *dev)
98119593Sphk{
99119593Sphk	struct g_consumer *cp;
100119593Sphk
101135716Sphk	g_topology_assert();
102119593Sphk	if (dev == NULL)
103119593Sphk		return (NULL);
104135716Sphk	if (dev->si_devsw != &g_dev_cdevsw)
105143790Sphk		return (NULL);
106143790Sphk	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;
11696987Sphk	int error;
117130585Sphk	struct cdev *dev;
11892108Sphk
11992108Sphk	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
12092108Sphk	g_topology_assert();
12192108Sphk	LIST_FOREACH(cp, &pp->consumers, consumers)
12293248Sphk		if (cp->geom->class == mp)
12392108Sphk			return (NULL);
12492108Sphk	gp = g_new_geomf(mp, pp->name);
12592108Sphk	cp = g_new_consumer(gp);
12696987Sphk	error = g_attach(cp, pp);
12796987Sphk	KASSERT(error == 0,
12896987Sphk	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
129214063Sjh	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
130214063Sjh	    &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
131214063Sjh	if (error != 0) {
132214063Sjh		printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
133214063Sjh		    __func__, gp->name, error);
134214063Sjh		g_detach(cp);
135214063Sjh		g_destroy_consumer(cp);
136214063Sjh		g_destroy_geom(gp);
137214063Sjh		return (NULL);
138214063Sjh	}
139110700Sphk	if (pp->flags & G_PF_CANDELETE)
140110700Sphk		dev->si_flags |= SI_CANDELETE;
141110728Sphk	dev->si_iosize_max = MAXPHYS;
14292108Sphk	gp->softc = dev;
14392108Sphk	dev->si_drv1 = gp;
14492108Sphk	dev->si_drv2 = cp;
14592108Sphk	return (gp);
14692108Sphk}
14792108Sphk
14892108Sphkstatic int
149130585Sphkg_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
15092108Sphk{
15192108Sphk	struct g_geom *gp;
15292108Sphk	struct g_consumer *cp;
15392108Sphk	int error, r, w, e;
15492108Sphk
15592108Sphk	gp = dev->si_drv1;
15692108Sphk	cp = dev->si_drv2;
157112978Sphk	if (gp == NULL || cp == NULL || gp->softc != dev)
158112978Sphk		return(ENXIO);		/* g_dev_taste() not done yet */
159112978Sphk
16092108Sphk	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
16192108Sphk	    gp->name, flags, fmt, td);
162130712Sphk
16392108Sphk	r = flags & FREAD ? 1 : 0;
16492108Sphk	w = flags & FWRITE ? 1 : 0;
165103004Sphk#ifdef notyet
16692108Sphk	e = flags & O_EXCL ? 1 : 0;
167103004Sphk#else
168103004Sphk	e = 0;
169103004Sphk#endif
170130712Sphk	if (w) {
171130712Sphk		/*
172130712Sphk		 * When running in very secure mode, do not allow
173130712Sphk		 * opens for writing of any disks.
174130712Sphk		 */
175130712Sphk		error = securelevel_ge(td->td_ucred, 2);
176130712Sphk		if (error)
177130712Sphk			return (error);
178130712Sphk	}
179112978Sphk	g_topology_lock();
180112978Sphk	if (dev->si_devsw == NULL)
181112978Sphk		error = ENXIO;		/* We were orphaned */
182112978Sphk	else
183125755Sphk		error = g_access(cp, r, w, e);
18492108Sphk	g_topology_unlock();
18592108Sphk	return(error);
18692108Sphk}
18792108Sphk
18892108Sphkstatic int
189130585Sphkg_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
19092108Sphk{
19192108Sphk	struct g_geom *gp;
19292108Sphk	struct g_consumer *cp;
193114864Sphk	int error, r, w, e, i;
19492108Sphk
19592108Sphk	gp = dev->si_drv1;
19692108Sphk	cp = dev->si_drv2;
19792108Sphk	if (gp == NULL || cp == NULL)
19892108Sphk		return(ENXIO);
19992108Sphk	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
20092108Sphk	    gp->name, flags, fmt, td);
20192108Sphk	r = flags & FREAD ? -1 : 0;
20292108Sphk	w = flags & FWRITE ? -1 : 0;
203103004Sphk#ifdef notyet
20492108Sphk	e = flags & O_EXCL ? -1 : 0;
205103004Sphk#else
206103004Sphk	e = 0;
207103004Sphk#endif
208112978Sphk	g_topology_lock();
209112978Sphk	if (dev->si_devsw == NULL)
210112978Sphk		error = ENXIO;		/* We were orphaned */
211112978Sphk	else
212125755Sphk		error = g_access(cp, r, w, e);
213114864Sphk	for (i = 0; i < 10 * hz;) {
214114864Sphk		if (cp->acr != 0 || cp->acw != 0)
215114864Sphk			break;
216114864Sphk 		if (cp->nstart == cp->nend)
217114864Sphk			break;
218167086Sjhb		pause("gdevwclose", hz / 10);
219114864Sphk		i += hz / 10;
220114864Sphk	}
221114864Sphk	if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
222124880Sphk		printf("WARNING: Final close of geom_dev(%s) %s %s\n",
223114864Sphk		    gp->name,
224114864Sphk		    "still has outstanding I/O after 10 seconds.",
225114864Sphk		    "Completing close anyway, panic may happen later.");
226114864Sphk	}
22792108Sphk	g_topology_unlock();
22892108Sphk	return (error);
22992108Sphk}
23092108Sphk
231112978Sphk/*
232112978Sphk * XXX: Until we have unmessed the ioctl situation, there is a race against
233112978Sphk * XXX: a concurrent orphanization.  We cannot close it by holding topology
234112978Sphk * XXX: since that would prevent us from doing our job, and stalling events
235112978Sphk * XXX: will break (actually: stall) the BSD disklabel hacks.
236112978Sphk */
23792108Sphkstatic int
238130585Sphkg_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
23992108Sphk{
240115515Sphk	struct g_geom *gp;
24192108Sphk	struct g_consumer *cp;
242182843Slulf	struct g_provider *pp;
24395038Sphk	struct g_kerneldump kd;
244174674Sphk	off_t offset, length, chunk;
24592108Sphk	int i, error;
24695038Sphk	u_int u;
24792108Sphk
24892108Sphk	gp = dev->si_drv1;
24992108Sphk	cp = dev->si_drv2;
250182843Slulf	pp = cp->provider;
25192108Sphk
25292108Sphk	error = 0;
253112978Sphk	KASSERT(cp->acr || cp->acw,
254112978Sphk	    ("Consumer with zero access count in g_dev_ioctl"));
25592403Sphk
25692698Sphk	i = IOCPARM_LEN(cmd);
25792698Sphk	switch (cmd) {
25892698Sphk	case DIOCGSECTORSIZE:
259105551Sphk		*(u_int *)data = cp->provider->sectorsize;
260105551Sphk		if (*(u_int *)data == 0)
261105180Snjl			error = ENOENT;
26292698Sphk		break;
26392698Sphk	case DIOCGMEDIASIZE:
264105551Sphk		*(off_t *)data = cp->provider->mediasize;
265105551Sphk		if (*(off_t *)data == 0)
266105180Snjl			error = ENOENT;
26792698Sphk		break;
26892698Sphk	case DIOCGFWSECTORS:
26993250Sphk		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
270105180Snjl		if (error == 0 && *(u_int *)data == 0)
271105180Snjl			error = ENOENT;
27292698Sphk		break;
27392698Sphk	case DIOCGFWHEADS:
27493250Sphk		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
275105180Snjl		if (error == 0 && *(u_int *)data == 0)
276105180Snjl			error = ENOENT;
27792698Sphk		break;
27894287Sphk	case DIOCGFRONTSTUFF:
27994287Sphk		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
28094287Sphk		break;
28195038Sphk	case DIOCSKERNELDUMP:
28295038Sphk		u = *((u_int *)data);
28395038Sphk		if (!u) {
28495038Sphk			set_dumper(NULL);
28595038Sphk			error = 0;
28695038Sphk			break;
28795038Sphk		}
28895038Sphk		kd.offset = 0;
28995038Sphk		kd.length = OFF_MAX;
29095038Sphk		i = sizeof kd;
29195038Sphk		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
292219950Smav		if (!error) {
293219950Smav			error = set_dumper(&kd.di);
294219950Smav			if (!error)
295219950Smav				dev->si_flags |= SI_DUMPDEV;
296219950Smav		}
29795038Sphk		break;
298169284Spjd	case DIOCGFLUSH:
299169284Spjd		error = g_io_flush(cp);
300169284Spjd		break;
301169284Spjd	case DIOCGDELETE:
302169284Spjd		offset = ((off_t *)data)[0];
303169284Spjd		length = ((off_t *)data)[1];
304169284Spjd		if ((offset % cp->provider->sectorsize) != 0 ||
305174669Sphk		    (length % cp->provider->sectorsize) != 0 || length <= 0) {
306169284Spjd			printf("%s: offset=%jd length=%jd\n", __func__, offset,
307169284Spjd			    length);
308169284Spjd			error = EINVAL;
309169284Spjd			break;
310169284Spjd		}
311174674Sphk		while (length > 0) {
312174674Sphk			chunk = length;
313201139Smav			if (chunk > 65536 * cp->provider->sectorsize)
314201139Smav				chunk = 65536 * cp->provider->sectorsize;
315174674Sphk			error = g_delete_data(cp, offset, chunk);
316174674Sphk			length -= chunk;
317174674Sphk			offset += chunk;
318174674Sphk			if (error)
319174674Sphk				break;
320174674Sphk			/*
321174674Sphk			 * Since the request size is unbounded, the service
322174674Sphk			 * time is likewise.  We make this ioctl interruptible
323174674Sphk			 * by checking for signals for each bio.
324174674Sphk			 */
325174674Sphk			if (SIGPENDING(td))
326174674Sphk				break;
327174674Sphk		}
328169284Spjd		break;
329169284Spjd	case DIOCGIDENT:
330169284Spjd		error = g_io_getattr("GEOM::ident", cp, &i, data);
331169284Spjd		break;
332182843Slulf	case DIOCGPROVIDERNAME:
333182843Slulf		if (pp == NULL)
334182843Slulf			return (ENOENT);
335182843Slulf		strlcpy(data, pp->name, i);
336182843Slulf		break;
337200934Smav	case DIOCGSTRIPESIZE:
338200934Smav		*(off_t *)data = cp->provider->stripesize;
339200934Smav		break;
340200934Smav	case DIOCGSTRIPEOFFSET:
341200934Smav		*(off_t *)data = cp->provider->stripeoffset;
342200934Smav		break;
34392698Sphk	default:
344119660Sphk		if (cp->provider->geom->ioctl != NULL) {
345138732Sphk			error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
346119749Sphk		} else {
347119749Sphk			error = ENOIOCTL;
348119660Sphk		}
34992698Sphk	}
35092403Sphk
35192108Sphk	return (error);
35292108Sphk}
35392108Sphk
35492108Sphkstatic void
35592108Sphkg_dev_done(struct bio *bp2)
35692108Sphk{
35792108Sphk	struct bio *bp;
35892108Sphk
359110517Sphk	bp = bp2->bio_parent;
36092108Sphk	bp->bio_error = bp2->bio_error;
36192108Sphk	if (bp->bio_error != 0) {
36292108Sphk		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
36392108Sphk		    bp2, bp->bio_error);
36492108Sphk		bp->bio_flags |= BIO_ERROR;
36592108Sphk	} else {
366105540Sphk		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
367105540Sphk		    bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
36892108Sphk	}
369137029Sphk	bp->bio_resid = bp->bio_length - bp2->bio_completed;
370137029Sphk	bp->bio_completed = bp2->bio_completed;
37192108Sphk	g_destroy_bio(bp2);
37292108Sphk	biodone(bp);
37392108Sphk}
37492108Sphk
37592108Sphkstatic void
37692108Sphkg_dev_strategy(struct bio *bp)
37792108Sphk{
37892108Sphk	struct g_consumer *cp;
37992108Sphk	struct bio *bp2;
380130585Sphk	struct cdev *dev;
38192108Sphk
382106300Sphk	KASSERT(bp->bio_cmd == BIO_READ ||
383106300Sphk	        bp->bio_cmd == BIO_WRITE ||
384106300Sphk	        bp->bio_cmd == BIO_DELETE,
385106300Sphk		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
38692108Sphk	dev = bp->bio_dev;
38792108Sphk	cp = dev->si_drv2;
388112978Sphk	KASSERT(cp->acr || cp->acw,
389112978Sphk	    ("Consumer with zero access count in g_dev_strategy"));
390196964Smav#ifdef INVARIANTS
391135865Spjd	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
392135865Spjd	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
393159756Ssimon		bp->bio_resid = bp->bio_bcount;
394135865Spjd		biofinish(bp, NULL, EINVAL);
395135865Spjd		return;
396135865Spjd	}
397196964Smav#endif
398118869Sphk	for (;;) {
399118869Sphk		/*
400118869Sphk		 * XXX: This is not an ideal solution, but I belive it to
401118869Sphk		 * XXX: deadlock safe, all things considered.
402118869Sphk		 */
403118869Sphk		bp2 = g_clone_bio(bp);
404118869Sphk		if (bp2 != NULL)
405118869Sphk			break;
406167086Sjhb		pause("gdstrat", hz / 10);
407118869Sphk	}
408107834Sphk	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
40992108Sphk	bp2->bio_done = g_dev_done;
41092108Sphk	g_trace(G_T_BIO,
411105540Sphk	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
412105540Sphk	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
413105540Sphk	    bp2->bio_data, bp2->bio_cmd);
41492108Sphk	g_io_request(bp2, cp);
415112978Sphk	KASSERT(cp->acr || cp->acw,
416112978Sphk	    ("g_dev_strategy raced with g_dev_close and lost"));
417112978Sphk
41892108Sphk}
41992108Sphk
42096987Sphk/*
42196987Sphk * g_dev_orphan()
42296987Sphk *
423112024Sphk * Called from below when the provider orphaned us.
424112024Sphk * - Clear any dump settings.
425130585Sphk * - Destroy the struct cdev *to prevent any more request from coming in.  The
426112024Sphk *   provider is already marked with an error, so anything which comes in
427112024Sphk *   in the interrim will be returned immediately.
428112024Sphk * - Wait for any outstanding I/O to finish.
429112024Sphk * - Set our access counts to zero, whatever they were.
430112024Sphk * - Detach and self-destruct.
43196987Sphk */
43292108Sphk
43392108Sphkstatic void
43493250Sphkg_dev_orphan(struct g_consumer *cp)
43592108Sphk{
43692108Sphk	struct g_geom *gp;
437130585Sphk	struct cdev *dev;
43892108Sphk
439112024Sphk	g_topology_assert();
44092108Sphk	gp = cp->geom;
441112024Sphk	dev = gp->softc;
44292108Sphk	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
443112024Sphk
444112024Sphk	/* Reset any dump-area set on this device */
44595038Sphk	if (dev->si_flags & SI_DUMPDEV)
44695038Sphk		set_dumper(NULL);
447112024Sphk
448130585Sphk	/* Destroy the struct cdev *so we get no more requests */
44992108Sphk	destroy_dev(dev);
450112024Sphk
451112024Sphk	/* Wait for the cows to come home */
452112024Sphk	while (cp->nstart != cp->nend)
453167086Sjhb		pause("gdevorphan", hz / 10);
454112024Sphk
45592108Sphk	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
456125755Sphk		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
457112024Sphk
45898066Sphk	g_detach(cp);
45992108Sphk	g_destroy_consumer(cp);
46092108Sphk	g_destroy_geom(gp);
46192108Sphk}
46292108Sphk
46396987SphkDECLARE_GEOM_CLASS(g_dev_class, g_dev);
464