geom_dev.c revision 219950
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/geom/geom_dev.c 219950 2011-03-24 08:37:48Z mav $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/malloc.h>
42#include <sys/kernel.h>
43#include <sys/conf.h>
44#include <sys/bio.h>
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/proc.h>
48#include <sys/errno.h>
49#include <sys/time.h>
50#include <sys/disk.h>
51#include <sys/fcntl.h>
52#include <sys/limits.h>
53#include <geom/geom.h>
54#include <geom/geom_int.h>
55
56static d_open_t		g_dev_open;
57static d_close_t	g_dev_close;
58static d_strategy_t	g_dev_strategy;
59static d_ioctl_t	g_dev_ioctl;
60
61static struct cdevsw g_dev_cdevsw = {
62	.d_version =	D_VERSION,
63	.d_open =	g_dev_open,
64	.d_close =	g_dev_close,
65	.d_read =	physread,
66	.d_write =	physwrite,
67	.d_ioctl =	g_dev_ioctl,
68	.d_strategy =	g_dev_strategy,
69	.d_name =	"g_dev",
70	.d_flags =	D_DISK | D_TRACKCLOSE,
71};
72
73static g_taste_t g_dev_taste;
74static g_orphan_t g_dev_orphan;
75
76static struct g_class g_dev_class	= {
77	.name = "DEV",
78	.version = G_VERSION,
79	.taste = g_dev_taste,
80	.orphan = g_dev_orphan,
81};
82
83void
84g_dev_print(void)
85{
86	struct g_geom *gp;
87	char const *p = "";
88
89	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
90		printf("%s%s", p, gp->name);
91		p = " ";
92	}
93	printf("\n");
94}
95
96struct g_provider *
97g_dev_getprovider(struct cdev *dev)
98{
99	struct g_consumer *cp;
100
101	g_topology_assert();
102	if (dev == NULL)
103		return (NULL);
104	if (dev->si_devsw != &g_dev_cdevsw)
105		return (NULL);
106	cp = dev->si_drv2;
107	return (cp->provider);
108}
109
110
111static struct g_geom *
112g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
113{
114	struct g_geom *gp;
115	struct g_consumer *cp;
116	int error;
117	struct cdev *dev;
118
119	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
120	g_topology_assert();
121	LIST_FOREACH(cp, &pp->consumers, consumers)
122		if (cp->geom->class == mp)
123			return (NULL);
124	gp = g_new_geomf(mp, pp->name);
125	cp = g_new_consumer(gp);
126	error = g_attach(cp, pp);
127	KASSERT(error == 0,
128	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
129	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
130	    &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
131	if (error != 0) {
132		printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
133		    __func__, gp->name, error);
134		g_detach(cp);
135		g_destroy_consumer(cp);
136		g_destroy_geom(gp);
137		return (NULL);
138	}
139	if (pp->flags & G_PF_CANDELETE)
140		dev->si_flags |= SI_CANDELETE;
141	dev->si_iosize_max = MAXPHYS;
142	gp->softc = dev;
143	dev->si_drv1 = gp;
144	dev->si_drv2 = cp;
145	return (gp);
146}
147
148static int
149g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
150{
151	struct g_geom *gp;
152	struct g_consumer *cp;
153	int error, r, w, e;
154
155	gp = dev->si_drv1;
156	cp = dev->si_drv2;
157	if (gp == NULL || cp == NULL || gp->softc != dev)
158		return(ENXIO);		/* g_dev_taste() not done yet */
159
160	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
161	    gp->name, flags, fmt, td);
162
163	r = flags & FREAD ? 1 : 0;
164	w = flags & FWRITE ? 1 : 0;
165#ifdef notyet
166	e = flags & O_EXCL ? 1 : 0;
167#else
168	e = 0;
169#endif
170	if (w) {
171		/*
172		 * When running in very secure mode, do not allow
173		 * opens for writing of any disks.
174		 */
175		error = securelevel_ge(td->td_ucred, 2);
176		if (error)
177			return (error);
178	}
179	g_topology_lock();
180	if (dev->si_devsw == NULL)
181		error = ENXIO;		/* We were orphaned */
182	else
183		error = g_access(cp, r, w, e);
184	g_topology_unlock();
185	return(error);
186}
187
188static int
189g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
190{
191	struct g_geom *gp;
192	struct g_consumer *cp;
193	int error, r, w, e, i;
194
195	gp = dev->si_drv1;
196	cp = dev->si_drv2;
197	if (gp == NULL || cp == NULL)
198		return(ENXIO);
199	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
200	    gp->name, flags, fmt, td);
201	r = flags & FREAD ? -1 : 0;
202	w = flags & FWRITE ? -1 : 0;
203#ifdef notyet
204	e = flags & O_EXCL ? -1 : 0;
205#else
206	e = 0;
207#endif
208	g_topology_lock();
209	if (dev->si_devsw == NULL)
210		error = ENXIO;		/* We were orphaned */
211	else
212		error = g_access(cp, r, w, e);
213	for (i = 0; i < 10 * hz;) {
214		if (cp->acr != 0 || cp->acw != 0)
215			break;
216 		if (cp->nstart == cp->nend)
217			break;
218		pause("gdevwclose", hz / 10);
219		i += hz / 10;
220	}
221	if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
222		printf("WARNING: Final close of geom_dev(%s) %s %s\n",
223		    gp->name,
224		    "still has outstanding I/O after 10 seconds.",
225		    "Completing close anyway, panic may happen later.");
226	}
227	g_topology_unlock();
228	return (error);
229}
230
231/*
232 * XXX: Until we have unmessed the ioctl situation, there is a race against
233 * XXX: a concurrent orphanization.  We cannot close it by holding topology
234 * XXX: since that would prevent us from doing our job, and stalling events
235 * XXX: will break (actually: stall) the BSD disklabel hacks.
236 */
237static int
238g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
239{
240	struct g_geom *gp;
241	struct g_consumer *cp;
242	struct g_provider *pp;
243	struct g_kerneldump kd;
244	off_t offset, length, chunk;
245	int i, error;
246	u_int u;
247
248	gp = dev->si_drv1;
249	cp = dev->si_drv2;
250	pp = cp->provider;
251
252	error = 0;
253	KASSERT(cp->acr || cp->acw,
254	    ("Consumer with zero access count in g_dev_ioctl"));
255
256	i = IOCPARM_LEN(cmd);
257	switch (cmd) {
258	case DIOCGSECTORSIZE:
259		*(u_int *)data = cp->provider->sectorsize;
260		if (*(u_int *)data == 0)
261			error = ENOENT;
262		break;
263	case DIOCGMEDIASIZE:
264		*(off_t *)data = cp->provider->mediasize;
265		if (*(off_t *)data == 0)
266			error = ENOENT;
267		break;
268	case DIOCGFWSECTORS:
269		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
270		if (error == 0 && *(u_int *)data == 0)
271			error = ENOENT;
272		break;
273	case DIOCGFWHEADS:
274		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
275		if (error == 0 && *(u_int *)data == 0)
276			error = ENOENT;
277		break;
278	case DIOCGFRONTSTUFF:
279		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
280		break;
281	case DIOCSKERNELDUMP:
282		u = *((u_int *)data);
283		if (!u) {
284			set_dumper(NULL);
285			error = 0;
286			break;
287		}
288		kd.offset = 0;
289		kd.length = OFF_MAX;
290		i = sizeof kd;
291		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
292		if (!error) {
293			error = set_dumper(&kd.di);
294			if (!error)
295				dev->si_flags |= SI_DUMPDEV;
296		}
297		break;
298	case DIOCGFLUSH:
299		error = g_io_flush(cp);
300		break;
301	case DIOCGDELETE:
302		offset = ((off_t *)data)[0];
303		length = ((off_t *)data)[1];
304		if ((offset % cp->provider->sectorsize) != 0 ||
305		    (length % cp->provider->sectorsize) != 0 || length <= 0) {
306			printf("%s: offset=%jd length=%jd\n", __func__, offset,
307			    length);
308			error = EINVAL;
309			break;
310		}
311		while (length > 0) {
312			chunk = length;
313			if (chunk > 65536 * cp->provider->sectorsize)
314				chunk = 65536 * cp->provider->sectorsize;
315			error = g_delete_data(cp, offset, chunk);
316			length -= chunk;
317			offset += chunk;
318			if (error)
319				break;
320			/*
321			 * Since the request size is unbounded, the service
322			 * time is likewise.  We make this ioctl interruptible
323			 * by checking for signals for each bio.
324			 */
325			if (SIGPENDING(td))
326				break;
327		}
328		break;
329	case DIOCGIDENT:
330		error = g_io_getattr("GEOM::ident", cp, &i, data);
331		break;
332	case DIOCGPROVIDERNAME:
333		if (pp == NULL)
334			return (ENOENT);
335		strlcpy(data, pp->name, i);
336		break;
337	case DIOCGSTRIPESIZE:
338		*(off_t *)data = cp->provider->stripesize;
339		break;
340	case DIOCGSTRIPEOFFSET:
341		*(off_t *)data = cp->provider->stripeoffset;
342		break;
343	default:
344		if (cp->provider->geom->ioctl != NULL) {
345			error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
346		} else {
347			error = ENOIOCTL;
348		}
349	}
350
351	return (error);
352}
353
354static void
355g_dev_done(struct bio *bp2)
356{
357	struct bio *bp;
358
359	bp = bp2->bio_parent;
360	bp->bio_error = bp2->bio_error;
361	if (bp->bio_error != 0) {
362		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
363		    bp2, bp->bio_error);
364		bp->bio_flags |= BIO_ERROR;
365	} else {
366		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
367		    bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
368	}
369	bp->bio_resid = bp->bio_length - bp2->bio_completed;
370	bp->bio_completed = bp2->bio_completed;
371	g_destroy_bio(bp2);
372	biodone(bp);
373}
374
375static void
376g_dev_strategy(struct bio *bp)
377{
378	struct g_consumer *cp;
379	struct bio *bp2;
380	struct cdev *dev;
381
382	KASSERT(bp->bio_cmd == BIO_READ ||
383	        bp->bio_cmd == BIO_WRITE ||
384	        bp->bio_cmd == BIO_DELETE,
385		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
386	dev = bp->bio_dev;
387	cp = dev->si_drv2;
388	KASSERT(cp->acr || cp->acw,
389	    ("Consumer with zero access count in g_dev_strategy"));
390#ifdef INVARIANTS
391	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
392	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
393		bp->bio_resid = bp->bio_bcount;
394		biofinish(bp, NULL, EINVAL);
395		return;
396	}
397#endif
398	for (;;) {
399		/*
400		 * XXX: This is not an ideal solution, but I belive it to
401		 * XXX: deadlock safe, all things considered.
402		 */
403		bp2 = g_clone_bio(bp);
404		if (bp2 != NULL)
405			break;
406		pause("gdstrat", hz / 10);
407	}
408	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
409	bp2->bio_done = g_dev_done;
410	g_trace(G_T_BIO,
411	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
412	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
413	    bp2->bio_data, bp2->bio_cmd);
414	g_io_request(bp2, cp);
415	KASSERT(cp->acr || cp->acw,
416	    ("g_dev_strategy raced with g_dev_close and lost"));
417
418}
419
420/*
421 * g_dev_orphan()
422 *
423 * Called from below when the provider orphaned us.
424 * - Clear any dump settings.
425 * - Destroy the struct cdev *to prevent any more request from coming in.  The
426 *   provider is already marked with an error, so anything which comes in
427 *   in the interrim will be returned immediately.
428 * - Wait for any outstanding I/O to finish.
429 * - Set our access counts to zero, whatever they were.
430 * - Detach and self-destruct.
431 */
432
433static void
434g_dev_orphan(struct g_consumer *cp)
435{
436	struct g_geom *gp;
437	struct cdev *dev;
438
439	g_topology_assert();
440	gp = cp->geom;
441	dev = gp->softc;
442	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
443
444	/* Reset any dump-area set on this device */
445	if (dev->si_flags & SI_DUMPDEV)
446		set_dumper(NULL);
447
448	/* Destroy the struct cdev *so we get no more requests */
449	destroy_dev(dev);
450
451	/* Wait for the cows to come home */
452	while (cp->nstart != cp->nend)
453		pause("gdevorphan", hz / 10);
454
455	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
456		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
457
458	g_detach(cp);
459	g_destroy_consumer(cp);
460	g_destroy_geom(gp);
461}
462
463DECLARE_GEOM_CLASS(g_dev_class, g_dev);
464