]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_dev.c
This commit was generated by cvs2svn to compensate for changes in r93526,
[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  * $FreeBSD$
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
42 #include <sys/conf.h>
43 #include <sys/bio.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/disk.h>
49 #include <sys/fcntl.h>
50 #include <geom/geom.h>
51
52 #define CDEV_MAJOR      4
53
54 static d_open_t         g_dev_open;
55 static d_close_t        g_dev_close;
56 static d_strategy_t     g_dev_strategy;
57 static d_ioctl_t        g_dev_ioctl;
58 static d_psize_t        g_dev_psize;
59
60 static struct cdevsw g_dev_cdevsw = {
61         /* open */      g_dev_open,
62         /* close */     g_dev_close,
63         /* read */      physread,
64         /* write */     physwrite,
65         /* ioctl */     g_dev_ioctl,
66         /* poll */      nopoll,
67         /* mmap */      nommap,
68         /* strategy */  g_dev_strategy,
69         /* name */      "g_dev",
70         /* maj */       CDEV_MAJOR,
71         /* dump */      nodump,
72         /* psize */     g_dev_psize,
73         /* flags */     D_DISK | D_CANFREE | D_TRACKCLOSE,
74 };
75
76 static g_taste_t g_dev_taste;
77 static g_orphan_t g_dev_orphan;
78
79 static struct g_class g_dev_class       = {
80         "DEV-class",
81         g_dev_taste,
82         NULL,
83         g_dev_orphan,
84         NULL,
85         G_CLASS_INITSTUFF
86 };
87
88 static void
89 g_dev_clone(void *arg, char *name, int namelen, dev_t *dev)
90 {
91         struct g_geom *gp;
92
93         if (*dev != NODEV)
94                 return;
95
96         g_trace(G_T_TOPOLOGY, "g_dev_clone(%s)", name);
97         g_rattle();
98
99         /* XXX: can I drop Giant here ??? */
100         /* g_topology_lock(); */
101         LIST_FOREACH(gp, &g_dev_class.geom, geom) {
102                 if (strcmp(gp->name, name))
103                         continue;
104                 *dev = gp->softc;
105                 g_trace(G_T_TOPOLOGY, "g_dev_clone(%s) = %p", name, *dev);
106                 return;
107         }
108         /* g_topology_unlock(); */
109         return;
110 }
111
112 static void
113 g_dev_register_cloner(void *foo __unused)
114 {
115         static int once;
116
117         if (!once) {
118                 if (!once)
119                         EVENTHANDLER_REGISTER(dev_clone, g_dev_clone, 0, 1000);
120                 once++;
121         }
122 }
123
124 SYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL);
125
126 static struct g_geom *
127 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
128 {
129         struct g_geom *gp;
130         struct g_consumer *cp;
131         static int unit;
132         u_int secsize;
133         off_t mediasize;
134         int error, j;
135         dev_t dev;
136
137         g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
138         g_topology_assert();
139         LIST_FOREACH(cp, &pp->consumers, consumers)
140                 if (cp->geom->class == mp)
141                         return (NULL);
142         gp = g_new_geomf(mp, pp->name);
143         cp = g_new_consumer(gp);
144         g_attach(cp, pp);
145         error = g_access_rel(cp, 1, 0, 0);
146         g_topology_unlock();
147         if (!error) {
148                 j = sizeof secsize;
149                 error = g_io_getattr("GEOM::sectorsize", cp, &j, &secsize);
150                 if (error) {
151                         secsize = 512;
152                         printf("g_bsd_taste: error %d Sectors are %d bytes\n",
153                             error, secsize);
154                 }
155                 j = sizeof mediasize;
156                 error = g_io_getattr("GEOM::mediasize", cp, &j, &mediasize);
157                 if (error) {
158                         mediasize = 0;
159                         printf("g_error %d Mediasize is %lld bytes\n",
160                             error, (long long)mediasize);
161                 }
162                 g_topology_lock();
163                 g_access_rel(cp, -1, 0, 0);
164                 g_topology_unlock();
165         } else {
166                 secsize = 512;
167                 mediasize = 0;
168         }
169         mtx_lock(&Giant);
170         if (mediasize != 0)
171                 printf("GEOM: \"%s\" %lld bytes in %lld sectors of %u bytes\n",
172                     pp->name, (long long)mediasize, 
173                     (long long)mediasize / secsize, secsize);
174         else
175                 printf("GEOM: \"%s\" (size unavailable)\n", pp->name);
176         dev = make_dev(&g_dev_cdevsw, unit++,
177             UID_ROOT, GID_WHEEL, 0600, gp->name);
178         gp->softc = dev;
179         dev->si_drv1 = gp;
180         dev->si_drv2 = cp;
181         mtx_unlock(&Giant);
182         g_topology_lock();
183         return (gp);
184 }
185
186 static int
187 g_dev_open(dev_t dev, int flags, int fmt, struct thread *td)
188 {
189         struct g_geom *gp;
190         struct g_consumer *cp;
191         int error, r, w, e;
192
193         gp = dev->si_drv1;
194         cp = dev->si_drv2;
195         if (gp == NULL || cp == NULL)
196                 return(ENXIO);
197         g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
198             gp->name, flags, fmt, td);
199         DROP_GIANT();
200         g_topology_lock();
201         g_silence();
202         r = flags & FREAD ? 1 : 0;
203         w = flags & FWRITE ? 1 : 0;
204         e = flags & O_EXCL ? 1 : 0;
205         error = g_access_rel(cp, r, w, e);
206         g_topology_unlock();
207         PICKUP_GIANT();
208         g_rattle();
209         return(error);
210 }
211
212 static int
213 g_dev_close(dev_t dev, int flags, int fmt, struct thread *td)
214 {
215         struct g_geom *gp;
216         struct g_consumer *cp;
217         int error, r, w, e;
218
219         gp = dev->si_drv1;
220         cp = dev->si_drv2;
221         if (gp == NULL || cp == NULL)
222                 return(ENXIO);
223         g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
224             gp->name, flags, fmt, td);
225         DROP_GIANT();
226         g_topology_lock();
227         g_silence();
228         r = flags & FREAD ? -1 : 0;
229         w = flags & FWRITE ? -1 : 0;
230         e = flags & O_EXCL ? -1 : 0;
231         error = g_access_rel(cp, r, w, e);
232         g_topology_unlock();
233         PICKUP_GIANT();
234         g_rattle();
235         return (error);
236 }
237
238 static int
239 g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
240 {
241         struct g_geom *gp;
242         struct g_consumer *cp;
243         int i, error;
244         struct g_ioctl *gio;
245
246         gp = dev->si_drv1;
247         cp = dev->si_drv2;
248
249         error = 0;
250         DROP_GIANT();
251
252         i = IOCPARM_LEN(cmd);
253         switch (cmd) {
254         case DIOCGSECTORSIZE:
255                 error = g_io_getattr("GEOM::sectorsize", cp, &i, data);
256                 break;
257         case DIOCGMEDIASIZE:
258                 error = g_io_getattr("GEOM::mediasize", cp, &i, data);
259                 break;
260         case DIOCGFWSECTORS:
261                 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
262                 break;
263         case DIOCGFWHEADS:
264                 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
265                 break;
266         case DIOCGFWCYLINDERS:
267                 error = g_io_getattr("GEOM::fwcylinders", cp, &i, data);
268                 break;
269         default:
270                 gio = g_malloc(sizeof *gio, M_WAITOK);
271                 gio->cmd = cmd;
272                 gio->data = data;
273                 gio->fflag = fflag;
274                 gio->td = td;
275                 i = sizeof *gio;
276                 if (cmd & IOC_IN)
277                         error = g_io_setattr("GEOM::ioctl", cp, i, gio);
278                 else
279                         error = g_io_getattr("GEOM::ioctl", cp, &i, gio);
280                 g_free(gio);
281                 break;
282         }
283
284         if (error != 0 && cmd == DIOCGDVIRGIN) {
285                 g_topology_lock();
286                 gp = g_create_geomf("BSD-class", cp->provider, NULL);
287                 g_topology_unlock();
288         }
289         PICKUP_GIANT();
290         g_rattle();
291         if (error == ENOIOCTL) {
292                 i = IOCGROUP(cmd);
293                 printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name);
294                 if (i > ' ' && i <= '~')
295                         printf(" '%c'", (int)IOCGROUP(cmd));
296                 else
297                         printf(" 0x%lx", IOCGROUP(cmd));
298                 printf("/%ld ", cmd & 0xff);
299                 if (cmd & IOC_IN)
300                         printf("I");
301                 if (cmd & IOC_OUT)
302                         printf("O");
303                 printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd));
304                 error = ENOTTY;
305         }
306         return (error);
307 }
308
309 static int
310 g_dev_psize(dev_t dev)
311 {
312         struct g_consumer *cp;
313         int i, error;
314         off_t mediasize;
315
316         cp = dev->si_drv2;
317
318         i = sizeof mediasize;
319         error = g_io_getattr("GEOM::mediasize", cp, &i, &mediasize);
320         if (error)
321                 return (-1);
322         return (mediasize >> DEV_BSHIFT);
323 }
324
325 static void
326 g_dev_done(struct bio *bp2)
327 {
328         struct bio *bp;
329
330         bp = bp2->bio_linkage;
331         bp->bio_error = bp2->bio_error;
332         if (bp->bio_error != 0) {
333                 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
334                     bp2, bp->bio_error);
335                 bp->bio_flags |= BIO_ERROR;
336         } else {
337                 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %lld",
338                     bp2, bp, bp->bio_resid, bp2->bio_completed);
339         }
340         bp->bio_resid = bp->bio_bcount - bp2->bio_completed;
341         g_destroy_bio(bp2);
342         mtx_lock(&Giant);
343         biodone(bp);
344         mtx_unlock(&Giant);
345 }
346
347 static void
348 g_dev_strategy(struct bio *bp)
349 {
350         struct g_geom *gp;
351         struct g_consumer *cp;
352         struct bio *bp2;
353         dev_t dev;
354
355         dev = bp->bio_dev;
356         gp = dev->si_drv1;
357         cp = dev->si_drv2;
358         bp2 = g_clone_bio(bp);
359         bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT;
360         bp2->bio_length = (off_t)bp->bio_bcount;
361         bp2->bio_done = g_dev_done;
362         g_trace(G_T_BIO,
363             "g_dev_strategy(%p/%p) offset %lld length %lld data %p cmd %d",
364             bp, bp2, bp->bio_offset, bp2->bio_length, bp2->bio_data,
365             bp2->bio_cmd);
366         g_io_request(bp2, cp);
367 }
368
369
370 static void
371 g_dev_orphan(struct g_consumer *cp)
372 {
373         struct g_geom *gp;
374         dev_t dev;
375
376         gp = cp->geom;
377         g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
378         g_topology_assert();
379         if (cp->biocount > 0)
380                 return;
381         dev = gp->softc;
382         destroy_dev(dev);
383         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
384                 g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace);
385         g_dettach(cp);
386         g_destroy_consumer(cp);
387         g_destroy_geom(gp);
388 }
389
390 DECLARE_GEOM_CLASS(g_dev_class, g_dev)
391