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 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev,
131 &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name);
133 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
134 __func__, gp->name, error);
136 g_destroy_consumer(cp);
141 /* Search for device alias name and create it if found. */
143 for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) {
144 snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name);
148 snprintf(buf, sizeof(buf), "%s%s",
149 val, gp->name + len);
151 make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
152 &adev, dev, "%s", buf);
157 if (pp->flags & G_PF_CANDELETE)
158 dev->si_flags |= SI_CANDELETE;
159 dev->si_iosize_max = MAXPHYS;
164 if (pp->flags & G_PF_CANDELETE)
165 adev->si_flags |= SI_CANDELETE;
166 adev->si_iosize_max = MAXPHYS;
174 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
177 struct g_consumer *cp;
182 if (gp == NULL || cp == NULL || gp->softc != dev)
183 return(ENXIO); /* g_dev_taste() not done yet */
185 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
186 gp->name, flags, fmt, td);
188 r = flags & FREAD ? 1 : 0;
189 w = flags & FWRITE ? 1 : 0;
191 e = flags & O_EXCL ? 1 : 0;
197 * When running in very secure mode, do not allow
198 * opens for writing of any disks.
200 error = securelevel_ge(td->td_ucred, 2);
205 if (dev->si_devsw == NULL)
206 error = ENXIO; /* We were orphaned */
208 error = g_access(cp, r, w, e);
214 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
217 struct g_consumer *cp;
218 int error, r, w, e, i;
222 if (gp == NULL || cp == NULL)
224 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
225 gp->name, flags, fmt, td);
226 r = flags & FREAD ? -1 : 0;
227 w = flags & FWRITE ? -1 : 0;
229 e = flags & O_EXCL ? -1 : 0;
234 if (dev->si_devsw == NULL)
235 error = ENXIO; /* We were orphaned */
237 error = g_access(cp, r, w, e);
238 for (i = 0; i < 10 * hz;) {
239 if (cp->acr != 0 || cp->acw != 0)
241 if (cp->nstart == cp->nend)
243 pause("gdevwclose", hz / 10);
246 if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
247 printf("WARNING: Final close of geom_dev(%s) %s %s\n",
249 "still has outstanding I/O after 10 seconds.",
250 "Completing close anyway, panic may happen later.");
257 * XXX: Until we have unmessed the ioctl situation, there is a race against
258 * XXX: a concurrent orphanization. We cannot close it by holding topology
259 * XXX: since that would prevent us from doing our job, and stalling events
260 * XXX: will break (actually: stall) the BSD disklabel hacks.
263 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
266 struct g_consumer *cp;
267 struct g_provider *pp;
268 struct g_kerneldump kd;
269 off_t offset, length, chunk;
278 KASSERT(cp->acr || cp->acw,
279 ("Consumer with zero access count in g_dev_ioctl"));
281 i = IOCPARM_LEN(cmd);
283 case DIOCGSECTORSIZE:
284 *(u_int *)data = cp->provider->sectorsize;
285 if (*(u_int *)data == 0)
289 *(off_t *)data = cp->provider->mediasize;
290 if (*(off_t *)data == 0)
294 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
295 if (error == 0 && *(u_int *)data == 0)
299 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
300 if (error == 0 && *(u_int *)data == 0)
303 case DIOCGFRONTSTUFF:
304 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
306 case DIOCSKERNELDUMP:
307 u = *((u_int *)data);
316 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
318 error = set_dumper(&kd.di);
320 dev->si_flags |= SI_DUMPDEV;
324 error = g_io_flush(cp);
327 offset = ((off_t *)data)[0];
328 length = ((off_t *)data)[1];
329 if ((offset % cp->provider->sectorsize) != 0 ||
330 (length % cp->provider->sectorsize) != 0 || length <= 0) {
331 printf("%s: offset=%jd length=%jd\n", __func__, offset,
338 if (chunk > 65536 * cp->provider->sectorsize)
339 chunk = 65536 * cp->provider->sectorsize;
340 error = g_delete_data(cp, offset, chunk);
346 * Since the request size is unbounded, the service
347 * time is likewise. We make this ioctl interruptible
348 * by checking for signals for each bio.
355 error = g_io_getattr("GEOM::ident", cp, &i, data);
357 case DIOCGPROVIDERNAME:
360 strlcpy(data, pp->name, i);
362 case DIOCGSTRIPESIZE:
363 *(off_t *)data = cp->provider->stripesize;
365 case DIOCGSTRIPEOFFSET:
366 *(off_t *)data = cp->provider->stripeoffset;
369 if (cp->provider->geom->ioctl != NULL) {
370 error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
380 g_dev_done(struct bio *bp2)
384 bp = bp2->bio_parent;
385 bp->bio_error = bp2->bio_error;
386 if (bp->bio_error != 0) {
387 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
389 bp->bio_flags |= BIO_ERROR;
391 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
392 bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
394 bp->bio_resid = bp->bio_length - bp2->bio_completed;
395 bp->bio_completed = bp2->bio_completed;
401 g_dev_strategy(struct bio *bp)
403 struct g_consumer *cp;
407 KASSERT(bp->bio_cmd == BIO_READ ||
408 bp->bio_cmd == BIO_WRITE ||
409 bp->bio_cmd == BIO_DELETE,
410 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
413 KASSERT(cp->acr || cp->acw,
414 ("Consumer with zero access count in g_dev_strategy"));
416 if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
417 (bp->bio_bcount % cp->provider->sectorsize) != 0) {
418 bp->bio_resid = bp->bio_bcount;
419 biofinish(bp, NULL, EINVAL);
425 * XXX: This is not an ideal solution, but I belive it to
426 * XXX: deadlock safe, all things considered.
428 bp2 = g_clone_bio(bp);
431 pause("gdstrat", hz / 10);
433 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
434 bp2->bio_done = g_dev_done;
436 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
437 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
438 bp2->bio_data, bp2->bio_cmd);
439 g_io_request(bp2, cp);
440 KASSERT(cp->acr || cp->acw,
441 ("g_dev_strategy raced with g_dev_close and lost"));
448 * Called from below when the provider orphaned us.
449 * - Clear any dump settings.
450 * - Destroy the struct cdev *to prevent any more request from coming in. The
451 * provider is already marked with an error, so anything which comes in
452 * in the interrim will be returned immediately.
453 * - Wait for any outstanding I/O to finish.
454 * - Set our access counts to zero, whatever they were.
455 * - Detach and self-destruct.
459 g_dev_orphan(struct g_consumer *cp)
467 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
469 /* Reset any dump-area set on this device */
470 if (dev->si_flags & SI_DUMPDEV)
473 /* Destroy the struct cdev *so we get no more requests */
476 /* Wait for the cows to come home */
477 while (cp->nstart != cp->nend)
478 pause("gdevorphan", hz / 10);
480 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
481 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
484 g_destroy_consumer(cp);
488 DECLARE_GEOM_CLASS(g_dev_class, g_dev);