]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_dev.c
Add a sysctl kern.geom.part.check_integrity for those who has corrupt
[FreeBSD/FreeBSD.git] / sys / geom / geom_dev.c
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$");
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
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;
60
61 static 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
73 static g_taste_t g_dev_taste;
74 static g_orphan_t g_dev_orphan;
75
76 static 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
83 void
84 g_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
96 struct g_provider *
97 g_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
111 static struct g_geom *
112 g_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, len;
117         struct cdev *dev, *adev;
118         char buf[64], *val;
119
120         g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
121         g_topology_assert();
122         LIST_FOREACH(cp, &pp->consumers, consumers)
123                 if (cp->geom->class == mp)
124                         return (NULL);
125         gp = g_new_geomf(mp, pp->name);
126         cp = g_new_consumer(gp);
127         error = g_attach(cp, pp);
128         KASSERT(error == 0,
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);
132         if (error != 0) {
133                 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
134                     __func__, gp->name, error);
135                 g_detach(cp);
136                 g_destroy_consumer(cp);
137                 g_destroy_geom(gp);
138                 return (NULL);
139         }
140
141         /* Search for device alias name and create it if found. */
142         adev = NULL;
143         for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) {
144                 snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name);
145                 buf[14 + len] = 0;
146                 val = getenv(buf);
147                 if (val != NULL) {
148                         snprintf(buf, sizeof(buf), "%s%s",
149                             val, gp->name + len);
150                         freeenv(val);
151                         make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
152                             &adev, dev, "%s", buf);
153                         break;
154                 }
155         }
156
157         if (pp->flags & G_PF_CANDELETE)
158                 dev->si_flags |= SI_CANDELETE;
159         dev->si_iosize_max = MAXPHYS;
160         gp->softc = dev;
161         dev->si_drv1 = gp;
162         dev->si_drv2 = cp;
163         if (adev != NULL) {
164                 if (pp->flags & G_PF_CANDELETE)
165                         adev->si_flags |= SI_CANDELETE;
166                 adev->si_iosize_max = MAXPHYS;
167                 adev->si_drv1 = gp;
168                 adev->si_drv2 = cp;
169         }
170         return (gp);
171 }
172
173 static int
174 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
175 {
176         struct g_geom *gp;
177         struct g_consumer *cp;
178         int error, r, w, e;
179
180         gp = dev->si_drv1;
181         cp = dev->si_drv2;
182         if (gp == NULL || cp == NULL || gp->softc != dev)
183                 return(ENXIO);          /* g_dev_taste() not done yet */
184
185         g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
186             gp->name, flags, fmt, td);
187
188         r = flags & FREAD ? 1 : 0;
189         w = flags & FWRITE ? 1 : 0;
190 #ifdef notyet
191         e = flags & O_EXCL ? 1 : 0;
192 #else
193         e = 0;
194 #endif
195         if (w) {
196                 /*
197                  * When running in very secure mode, do not allow
198                  * opens for writing of any disks.
199                  */
200                 error = securelevel_ge(td->td_ucred, 2);
201                 if (error)
202                         return (error);
203         }
204         g_topology_lock();
205         if (dev->si_devsw == NULL)
206                 error = ENXIO;          /* We were orphaned */
207         else
208                 error = g_access(cp, r, w, e);
209         g_topology_unlock();
210         return(error);
211 }
212
213 static int
214 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
215 {
216         struct g_geom *gp;
217         struct g_consumer *cp;
218         int error, r, w, e, i;
219
220         gp = dev->si_drv1;
221         cp = dev->si_drv2;
222         if (gp == NULL || cp == NULL)
223                 return(ENXIO);
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;
228 #ifdef notyet
229         e = flags & O_EXCL ? -1 : 0;
230 #else
231         e = 0;
232 #endif
233         g_topology_lock();
234         if (dev->si_devsw == NULL)
235                 error = ENXIO;          /* We were orphaned */
236         else
237                 error = g_access(cp, r, w, e);
238         for (i = 0; i < 10 * hz;) {
239                 if (cp->acr != 0 || cp->acw != 0)
240                         break;
241                 if (cp->nstart == cp->nend)
242                         break;
243                 pause("gdevwclose", hz / 10);
244                 i += hz / 10;
245         }
246         if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
247                 printf("WARNING: Final close of geom_dev(%s) %s %s\n",
248                     gp->name,
249                     "still has outstanding I/O after 10 seconds.",
250                     "Completing close anyway, panic may happen later.");
251         }
252         g_topology_unlock();
253         return (error);
254 }
255
256 /*
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.
261  */
262 static int
263 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
264 {
265         struct g_geom *gp;
266         struct g_consumer *cp;
267         struct g_provider *pp;
268         struct g_kerneldump kd;
269         off_t offset, length, chunk;
270         int i, error;
271         u_int u;
272
273         gp = dev->si_drv1;
274         cp = dev->si_drv2;
275         pp = cp->provider;
276
277         error = 0;
278         KASSERT(cp->acr || cp->acw,
279             ("Consumer with zero access count in g_dev_ioctl"));
280
281         i = IOCPARM_LEN(cmd);
282         switch (cmd) {
283         case DIOCGSECTORSIZE:
284                 *(u_int *)data = cp->provider->sectorsize;
285                 if (*(u_int *)data == 0)
286                         error = ENOENT;
287                 break;
288         case DIOCGMEDIASIZE:
289                 *(off_t *)data = cp->provider->mediasize;
290                 if (*(off_t *)data == 0)
291                         error = ENOENT;
292                 break;
293         case DIOCGFWSECTORS:
294                 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
295                 if (error == 0 && *(u_int *)data == 0)
296                         error = ENOENT;
297                 break;
298         case DIOCGFWHEADS:
299                 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
300                 if (error == 0 && *(u_int *)data == 0)
301                         error = ENOENT;
302                 break;
303         case DIOCGFRONTSTUFF:
304                 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
305                 break;
306         case DIOCSKERNELDUMP:
307                 u = *((u_int *)data);
308                 if (!u) {
309                         set_dumper(NULL);
310                         error = 0;
311                         break;
312                 }
313                 kd.offset = 0;
314                 kd.length = OFF_MAX;
315                 i = sizeof kd;
316                 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
317                 if (!error) {
318                         error = set_dumper(&kd.di);
319                         if (!error)
320                                 dev->si_flags |= SI_DUMPDEV;
321                 }
322                 break;
323         case DIOCGFLUSH:
324                 error = g_io_flush(cp);
325                 break;
326         case DIOCGDELETE:
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,
332                             length);
333                         error = EINVAL;
334                         break;
335                 }
336                 while (length > 0) { 
337                         chunk = length;
338                         if (chunk > 65536 * cp->provider->sectorsize)
339                                 chunk = 65536 * cp->provider->sectorsize;
340                         error = g_delete_data(cp, offset, chunk);
341                         length -= chunk;
342                         offset += chunk;
343                         if (error)
344                                 break;
345                         /*
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.
349                          */
350                         if (SIGPENDING(td))
351                                 break;
352                 }
353                 break;
354         case DIOCGIDENT:
355                 error = g_io_getattr("GEOM::ident", cp, &i, data);
356                 break;
357         case DIOCGPROVIDERNAME:
358                 if (pp == NULL)
359                         return (ENOENT);
360                 strlcpy(data, pp->name, i);
361                 break;
362         case DIOCGSTRIPESIZE:
363                 *(off_t *)data = cp->provider->stripesize;
364                 break;
365         case DIOCGSTRIPEOFFSET:
366                 *(off_t *)data = cp->provider->stripeoffset;
367                 break;
368         default:
369                 if (cp->provider->geom->ioctl != NULL) {
370                         error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
371                 } else {
372                         error = ENOIOCTL;
373                 }
374         }
375
376         return (error);
377 }
378
379 static void
380 g_dev_done(struct bio *bp2)
381 {
382         struct bio *bp;
383
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",
388                     bp2, bp->bio_error);
389                 bp->bio_flags |= BIO_ERROR;
390         } else {
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);
393         }
394         bp->bio_resid = bp->bio_length - bp2->bio_completed;
395         bp->bio_completed = bp2->bio_completed;
396         g_destroy_bio(bp2);
397         biodone(bp);
398 }
399
400 static void
401 g_dev_strategy(struct bio *bp)
402 {
403         struct g_consumer *cp;
404         struct bio *bp2;
405         struct cdev *dev;
406
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));
411         dev = bp->bio_dev;
412         cp = dev->si_drv2;
413         KASSERT(cp->acr || cp->acw,
414             ("Consumer with zero access count in g_dev_strategy"));
415 #ifdef INVARIANTS
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);
420                 return;
421         }
422 #endif
423         for (;;) {
424                 /*
425                  * XXX: This is not an ideal solution, but I belive it to
426                  * XXX: deadlock safe, all things considered.
427                  */
428                 bp2 = g_clone_bio(bp);
429                 if (bp2 != NULL)
430                         break;
431                 pause("gdstrat", hz / 10);
432         }
433         KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
434         bp2->bio_done = g_dev_done;
435         g_trace(G_T_BIO,
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"));
442
443 }
444
445 /*
446  * g_dev_orphan()
447  *
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.
456  */
457
458 static void
459 g_dev_orphan(struct g_consumer *cp)
460 {
461         struct g_geom *gp;
462         struct cdev *dev;
463
464         g_topology_assert();
465         gp = cp->geom;
466         dev = gp->softc;
467         g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
468
469         /* Reset any dump-area set on this device */
470         if (dev->si_flags & SI_DUMPDEV)
471                 set_dumper(NULL);
472
473         /* Destroy the struct cdev *so we get no more requests */
474         destroy_dev(dev);
475
476         /* Wait for the cows to come home */
477         while (cp->nstart != cp->nend)
478                 pause("gdevorphan", hz / 10);
479
480         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
481                 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
482
483         g_detach(cp);
484         g_destroy_consumer(cp);
485         g_destroy_geom(gp);
486 }
487
488 DECLARE_GEOM_CLASS(g_dev_class, g_dev);