2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
46 #include <sys/mutex.h>
48 #include <sys/errno.h>
51 #include <sys/fcntl.h>
52 #include <sys/limits.h>
53 #include <geom/geom.h>
54 #include <geom/geom_int.h>
56 static d_open_t g_dev_open;
57 static d_close_t g_dev_close;
58 static d_strategy_t g_dev_strategy;
59 static d_ioctl_t g_dev_ioctl;
61 static struct cdevsw g_dev_cdevsw = {
62 .d_version = D_VERSION,
64 .d_close = g_dev_close,
67 .d_ioctl = g_dev_ioctl,
68 .d_strategy = g_dev_strategy,
70 .d_flags = D_DISK | D_TRACKCLOSE,
73 static g_taste_t g_dev_taste;
74 static g_orphan_t g_dev_orphan;
76 static struct g_class g_dev_class = {
80 .orphan = g_dev_orphan,
89 LIST_FOREACH(gp, &g_dev_class.geom, geom) {
90 printf("%s%s", p, gp->name);
97 g_dev_getprovider(struct cdev *dev)
99 struct g_consumer *cp;
104 if (dev->si_devsw != &g_dev_cdevsw)
107 return (cp->provider);
111 static struct g_geom *
112 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
115 struct g_consumer *cp;
117 struct cdev *dev, *adev;
120 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
122 LIST_FOREACH(cp, &pp->consumers, consumers)
123 if (cp->geom->class == mp)
125 gp = g_new_geomf(mp, pp->name);
126 cp = g_new_consumer(gp);
127 error = g_attach(cp, pp);
129 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
130 dev = make_dev(&g_dev_cdevsw, 0,
131 UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
133 /* Search for device alias name and create it if found. */
135 for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) {
136 snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name);
140 snprintf(buf, sizeof(buf), "%s%s",
141 val, gp->name + len);
143 adev = make_dev_alias(dev, buf);
148 if (pp->flags & G_PF_CANDELETE)
149 dev->si_flags |= SI_CANDELETE;
150 dev->si_iosize_max = MAXPHYS;
155 if (pp->flags & G_PF_CANDELETE)
156 adev->si_flags |= SI_CANDELETE;
157 adev->si_iosize_max = MAXPHYS;
165 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
168 struct g_consumer *cp;
173 if (gp == NULL || cp == NULL || gp->softc != dev)
174 return(ENXIO); /* g_dev_taste() not done yet */
176 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
177 gp->name, flags, fmt, td);
179 r = flags & FREAD ? 1 : 0;
180 w = flags & FWRITE ? 1 : 0;
182 e = flags & O_EXCL ? 1 : 0;
188 * When running in very secure mode, do not allow
189 * opens for writing of any disks.
191 error = securelevel_ge(td->td_ucred, 2);
196 if (dev->si_devsw == NULL)
197 error = ENXIO; /* We were orphaned */
199 error = g_access(cp, r, w, e);
205 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
208 struct g_consumer *cp;
209 int error, r, w, e, i;
213 if (gp == NULL || cp == NULL)
215 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
216 gp->name, flags, fmt, td);
217 r = flags & FREAD ? -1 : 0;
218 w = flags & FWRITE ? -1 : 0;
220 e = flags & O_EXCL ? -1 : 0;
225 if (dev->si_devsw == NULL)
226 error = ENXIO; /* We were orphaned */
228 error = g_access(cp, r, w, e);
229 for (i = 0; i < 10 * hz;) {
230 if (cp->acr != 0 || cp->acw != 0)
232 if (cp->nstart == cp->nend)
234 pause("gdevwclose", hz / 10);
237 if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
238 printf("WARNING: Final close of geom_dev(%s) %s %s\n",
240 "still has outstanding I/O after 10 seconds.",
241 "Completing close anyway, panic may happen later.");
248 * XXX: Until we have unmessed the ioctl situation, there is a race against
249 * XXX: a concurrent orphanization. We cannot close it by holding topology
250 * XXX: since that would prevent us from doing our job, and stalling events
251 * XXX: will break (actually: stall) the BSD disklabel hacks.
254 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
257 struct g_consumer *cp;
258 struct g_provider *pp;
259 struct g_kerneldump kd;
260 off_t offset, length, chunk;
269 KASSERT(cp->acr || cp->acw,
270 ("Consumer with zero access count in g_dev_ioctl"));
272 i = IOCPARM_LEN(cmd);
274 case DIOCGSECTORSIZE:
275 *(u_int *)data = cp->provider->sectorsize;
276 if (*(u_int *)data == 0)
280 *(off_t *)data = cp->provider->mediasize;
281 if (*(off_t *)data == 0)
285 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
286 if (error == 0 && *(u_int *)data == 0)
290 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
291 if (error == 0 && *(u_int *)data == 0)
294 case DIOCGFRONTSTUFF:
295 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
297 case DIOCSKERNELDUMP:
298 u = *((u_int *)data);
307 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
309 error = set_dumper(&kd.di);
311 dev->si_flags |= SI_DUMPDEV;
315 error = g_io_flush(cp);
318 offset = ((off_t *)data)[0];
319 length = ((off_t *)data)[1];
320 if ((offset % cp->provider->sectorsize) != 0 ||
321 (length % cp->provider->sectorsize) != 0 || length <= 0) {
322 printf("%s: offset=%jd length=%jd\n", __func__, offset,
329 if (chunk > 65536 * cp->provider->sectorsize)
330 chunk = 65536 * cp->provider->sectorsize;
331 error = g_delete_data(cp, offset, chunk);
337 * Since the request size is unbounded, the service
338 * time is likewise. We make this ioctl interruptible
339 * by checking for signals for each bio.
346 error = g_io_getattr("GEOM::ident", cp, &i, data);
348 case DIOCGPROVIDERNAME:
351 strlcpy(data, pp->name, i);
353 case DIOCGSTRIPESIZE:
354 *(off_t *)data = cp->provider->stripesize;
356 case DIOCGSTRIPEOFFSET:
357 *(off_t *)data = cp->provider->stripeoffset;
360 if (cp->provider->geom->ioctl != NULL) {
361 error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
371 g_dev_done(struct bio *bp2)
375 bp = bp2->bio_parent;
376 bp->bio_error = bp2->bio_error;
377 if (bp->bio_error != 0) {
378 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
380 bp->bio_flags |= BIO_ERROR;
382 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
383 bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
385 bp->bio_resid = bp->bio_length - bp2->bio_completed;
386 bp->bio_completed = bp2->bio_completed;
392 g_dev_strategy(struct bio *bp)
394 struct g_consumer *cp;
398 KASSERT(bp->bio_cmd == BIO_READ ||
399 bp->bio_cmd == BIO_WRITE ||
400 bp->bio_cmd == BIO_DELETE,
401 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
404 KASSERT(cp->acr || cp->acw,
405 ("Consumer with zero access count in g_dev_strategy"));
407 if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
408 (bp->bio_bcount % cp->provider->sectorsize) != 0) {
409 bp->bio_resid = bp->bio_bcount;
410 biofinish(bp, NULL, EINVAL);
416 * XXX: This is not an ideal solution, but I belive it to
417 * XXX: deadlock safe, all things considered.
419 bp2 = g_clone_bio(bp);
422 pause("gdstrat", hz / 10);
424 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
425 bp2->bio_done = g_dev_done;
427 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
428 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
429 bp2->bio_data, bp2->bio_cmd);
430 g_io_request(bp2, cp);
431 KASSERT(cp->acr || cp->acw,
432 ("g_dev_strategy raced with g_dev_close and lost"));
439 * Called from below when the provider orphaned us.
440 * - Clear any dump settings.
441 * - Destroy the struct cdev *to prevent any more request from coming in. The
442 * provider is already marked with an error, so anything which comes in
443 * in the interrim will be returned immediately.
444 * - Wait for any outstanding I/O to finish.
445 * - Set our access counts to zero, whatever they were.
446 * - Detach and self-destruct.
450 g_dev_orphan(struct g_consumer *cp)
458 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
460 /* Reset any dump-area set on this device */
461 if (dev->si_flags & SI_DUMPDEV)
464 /* Destroy the struct cdev *so we get no more requests */
467 /* Wait for the cows to come home */
468 while (cp->nstart != cp->nend)
469 pause("gdevorphan", hz / 10);
471 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
472 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
475 g_destroy_consumer(cp);
479 DECLARE_GEOM_CLASS(g_dev_class, g_dev);