Deleted Added
full compact
geom_dev.c (107834) geom_dev.c (108294)
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 *
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: head/sys/geom/geom_dev.c 107834 2002-12-13 22:04:45Z phk $
35 * $FreeBSD: head/sys/geom/geom_dev.c 108294 2002-12-26 20:45:37Z phk $
36 */
37
38#include <sys/param.h>
39#include <sys/stdint.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/errno.h>
48#include <sys/time.h>
49#include <sys/disk.h>
50#include <sys/fcntl.h>
51#include <geom/geom.h>
52#include <geom/geom_int.h>
53#include <machine/limits.h>
54
55#define CDEV_MAJOR 4
56
57static d_open_t g_dev_open;
58static d_close_t g_dev_close;
59static d_strategy_t g_dev_strategy;
60static d_ioctl_t g_dev_ioctl;
61static d_psize_t g_dev_psize;
62
63static struct cdevsw g_dev_cdevsw = {
64 /* open */ g_dev_open,
65 /* close */ g_dev_close,
66 /* read */ physread,
67 /* write */ physwrite,
68 /* ioctl */ g_dev_ioctl,
69 /* poll */ nopoll,
70 /* mmap */ nommap,
71 /* strategy */ g_dev_strategy,
72 /* name */ "g_dev",
73 /* maj */ CDEV_MAJOR,
74 /* dump */ nodump,
75 /* psize */ g_dev_psize,
76 /* flags */ D_DISK | D_CANFREE | D_TRACKCLOSE,
77 /* kqfilter */ nokqfilter
78};
79
80static g_taste_t g_dev_taste;
81static g_orphan_t g_dev_orphan;
82
83static struct g_class g_dev_class = {
84 "DEV",
85 g_dev_taste,
86 NULL,
87 G_CLASS_INITIALIZER
88};
89
90int
91g_dev_print(void)
92{
93 struct g_geom *gp;
94
95 if (LIST_EMPTY(&g_dev_class.geom))
96 return (0);
97 printf("List of GEOM disk devices:\n ");
98 LIST_FOREACH(gp, &g_dev_class.geom, geom)
99 printf(" %s", gp->name);
100 printf("\n");
101 return (1);
102}
103
36 */
37
38#include <sys/param.h>
39#include <sys/stdint.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/errno.h>
48#include <sys/time.h>
49#include <sys/disk.h>
50#include <sys/fcntl.h>
51#include <geom/geom.h>
52#include <geom/geom_int.h>
53#include <machine/limits.h>
54
55#define CDEV_MAJOR 4
56
57static d_open_t g_dev_open;
58static d_close_t g_dev_close;
59static d_strategy_t g_dev_strategy;
60static d_ioctl_t g_dev_ioctl;
61static d_psize_t g_dev_psize;
62
63static struct cdevsw g_dev_cdevsw = {
64 /* open */ g_dev_open,
65 /* close */ g_dev_close,
66 /* read */ physread,
67 /* write */ physwrite,
68 /* ioctl */ g_dev_ioctl,
69 /* poll */ nopoll,
70 /* mmap */ nommap,
71 /* strategy */ g_dev_strategy,
72 /* name */ "g_dev",
73 /* maj */ CDEV_MAJOR,
74 /* dump */ nodump,
75 /* psize */ g_dev_psize,
76 /* flags */ D_DISK | D_CANFREE | D_TRACKCLOSE,
77 /* kqfilter */ nokqfilter
78};
79
80static g_taste_t g_dev_taste;
81static g_orphan_t g_dev_orphan;
82
83static struct g_class g_dev_class = {
84 "DEV",
85 g_dev_taste,
86 NULL,
87 G_CLASS_INITIALIZER
88};
89
90int
91g_dev_print(void)
92{
93 struct g_geom *gp;
94
95 if (LIST_EMPTY(&g_dev_class.geom))
96 return (0);
97 printf("List of GEOM disk devices:\n ");
98 LIST_FOREACH(gp, &g_dev_class.geom, geom)
99 printf(" %s", gp->name);
100 printf("\n");
101 return (1);
102}
103
104/*
105 * XXX: This is disgusting and wrong in every way imaginable: The only reason
106 * XXX: we have a clone function is because of the root-mount hack we currently
107 * XXX: employ. An improvment would be to unregister this cloner once we know
108 * XXX: we no longer need it. Ideally, root-fs would be mounted through DEVFS
109 * XXX: eliminating the need for this hack.
110 */
104static void
105g_dev_clone(void *arg __unused, char *name, int namelen __unused, dev_t *dev)
106{
107 struct g_geom *gp;
108
109 if (*dev != NODEV)
110 return;
111
112 g_waitidle();
113
111static void
112g_dev_clone(void *arg __unused, char *name, int namelen __unused, dev_t *dev)
113{
114 struct g_geom *gp;
115
116 if (*dev != NODEV)
117 return;
118
119 g_waitidle();
120
114 /* XXX: can I drop Giant here ??? */
115 /* g_topology_lock(); */
116 LIST_FOREACH(gp, &g_dev_class.geom, geom) {
117 if (strcmp(gp->name, name))
118 continue;
119 *dev = gp->softc;
120 g_trace(G_T_TOPOLOGY, "g_dev_clone(%s) = %p", name, *dev);
121 return;
122 }
123 /* g_topology_unlock(); */
124 return;
125}
126
127static void
128g_dev_register_cloner(void *foo __unused)
129{
130 static int once;
131
132 /* XXX: why would this happen more than once ?? */
133 if (!once) {
134 EVENTHANDLER_REGISTER(dev_clone, g_dev_clone, 0, 1000);
135 once++;
136 }
137}
138
139SYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL);
140
141static struct g_geom *
142g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
143{
144 struct g_geom *gp;
145 struct g_consumer *cp;
146 static int unit;
147 int error;
148 dev_t dev;
149
150 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
151 g_topology_assert();
152 LIST_FOREACH(cp, &pp->consumers, consumers)
153 if (cp->geom->class == mp)
154 return (NULL);
155 gp = g_new_geomf(mp, pp->name);
156 gp->orphan = g_dev_orphan;
157 cp = g_new_consumer(gp);
158 error = g_attach(cp, pp);
159 KASSERT(error == 0,
160 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
161 /*
162 * XXX: I'm not 100% sure we can call make_dev(9) without Giant
163 * yet. Once we can, we don't need to drop topology here either.
164 */
165 g_topology_unlock();
166 mtx_lock(&Giant);
167 dev = make_dev(&g_dev_cdevsw, unit2minor(unit++),
168 UID_ROOT, GID_OPERATOR, 0640, gp->name);
169 mtx_unlock(&Giant);
170 g_topology_lock();
171
172 gp->softc = dev;
173 dev->si_drv1 = gp;
174 dev->si_drv2 = cp;
175 return (gp);
176}
177
178static int
179g_dev_open(dev_t dev, int flags, int fmt, struct thread *td)
180{
181 struct g_geom *gp;
182 struct g_consumer *cp;
183 int error, r, w, e;
184
185 gp = dev->si_drv1;
186 cp = dev->si_drv2;
187 if (gp == NULL || cp == NULL)
188 return(ENXIO);
189 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
190 gp->name, flags, fmt, td);
191 DROP_GIANT();
192 g_topology_lock();
193 g_silence();
194 r = flags & FREAD ? 1 : 0;
195 w = flags & FWRITE ? 1 : 0;
196#ifdef notyet
197 e = flags & O_EXCL ? 1 : 0;
198#else
199 e = 0;
200#endif
201 error = g_access_rel(cp, r, w, e);
202 g_topology_unlock();
203 PICKUP_GIANT();
204 g_waitidle();
205 return(error);
206}
207
208static int
209g_dev_close(dev_t dev, int flags, int fmt, struct thread *td)
210{
211 struct g_geom *gp;
212 struct g_consumer *cp;
213 int error, r, w, e;
214
215 gp = dev->si_drv1;
216 cp = dev->si_drv2;
217 if (gp == NULL || cp == NULL)
218 return(ENXIO);
219 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
220 gp->name, flags, fmt, td);
221 DROP_GIANT();
222 g_topology_lock();
223 g_silence();
224 r = flags & FREAD ? -1 : 0;
225 w = flags & FWRITE ? -1 : 0;
226#ifdef notyet
227 e = flags & O_EXCL ? -1 : 0;
228#else
229 e = 0;
230#endif
231 error = g_access_rel(cp, r, w, e);
232 g_topology_unlock();
233 PICKUP_GIANT();
234 g_waitidle();
235 return (error);
236}
237
238static int
239g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
240{
241 struct g_geom *gp, *gp2;
242 struct g_consumer *cp;
243 struct g_provider *pp2;
244 struct g_kerneldump kd;
245 int i, error;
246 u_int u;
247 struct g_ioctl *gio;
248
249 gp = dev->si_drv1;
250 cp = dev->si_drv2;
251 pp2 = cp->provider;
252 gp2 = pp2->geom;
253 gio = NULL;
254
255 error = 0;
256 DROP_GIANT();
257
258 i = IOCPARM_LEN(cmd);
259 switch (cmd) {
260 case DIOCGSECTORSIZE:
261 *(u_int *)data = cp->provider->sectorsize;
262 if (*(u_int *)data == 0)
263 error = ENOENT;
264 break;
265 case DIOCGMEDIASIZE:
266 *(off_t *)data = cp->provider->mediasize;
267 if (*(off_t *)data == 0)
268 error = ENOENT;
269 break;
270 case DIOCGFWSECTORS:
271 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
272 if (error == 0 && *(u_int *)data == 0)
273 error = ENOENT;
274 break;
275 case DIOCGFWHEADS:
276 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
277 if (error == 0 && *(u_int *)data == 0)
278 error = ENOENT;
279 break;
280 case DIOCGFRONTSTUFF:
281 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
282 break;
283 case DIOCSKERNELDUMP:
284 u = *((u_int *)data);
285 if (!u) {
286 set_dumper(NULL);
287 error = 0;
288 break;
289 }
290 kd.offset = 0;
291 kd.length = OFF_MAX;
292 i = sizeof kd;
293 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
294 if (!error)
295 dev->si_flags |= SI_DUMPDEV;
296 break;
297
298 default:
299 gio = g_malloc(sizeof *gio, M_WAITOK | M_ZERO);
300 gio->cmd = cmd;
301 gio->data = data;
302 gio->fflag = fflag;
303 gio->td = td;
304 i = sizeof *gio;
305 if (cmd & IOC_IN)
306 error = g_io_setattr("GEOM::ioctl", cp, i, gio);
307 else
308 error = g_io_getattr("GEOM::ioctl", cp, &i, gio);
309 break;
310 }
311
312 PICKUP_GIANT();
313 if (error == EDIRIOCTL) {
314 KASSERT(gio != NULL, ("NULL gio but EDIRIOCTL"));
315 KASSERT(gio->func != NULL, ("NULL function but EDIRIOCTL"));
316 error = (gio->func)(gio->dev, cmd, data, fflag, td);
317 }
318 if (gio != NULL)
319 g_free(gio);
320 g_waitidle();
321 if (error == ENOIOCTL) {
322 if (g_debugflags & G_T_TOPOLOGY) {
323 i = IOCGROUP(cmd);
324 printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name);
325 if (i > ' ' && i <= '~')
326 printf(" '%c'", (int)IOCGROUP(cmd));
327 else
328 printf(" 0x%lx", IOCGROUP(cmd));
329 printf("/%ld ", cmd & 0xff);
330 if (cmd & IOC_IN)
331 printf("I");
332 if (cmd & IOC_OUT)
333 printf("O");
334 printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd));
335 }
336 error = ENOTTY;
337 }
338 return (error);
339}
340
341static int
342g_dev_psize(dev_t dev)
343{
344 struct g_consumer *cp;
345 off_t mediasize;
346
347 cp = dev->si_drv2;
348
349 mediasize = cp->provider->mediasize;
350 return (mediasize >> DEV_BSHIFT);
351}
352
353static void
354g_dev_done(struct bio *bp2)
355{
356 struct bio *bp;
357
358 bp = bp2->bio_linkage;
359 bp->bio_error = bp2->bio_error;
360 if (bp->bio_error != 0) {
361 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
362 bp2, bp->bio_error);
363 bp->bio_flags |= BIO_ERROR;
364 } else {
365 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
366 bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
367 }
368 bp->bio_resid = bp->bio_bcount - bp2->bio_completed;
369 g_destroy_bio(bp2);
370 mtx_lock(&Giant);
371 biodone(bp);
372 mtx_unlock(&Giant);
373}
374
375static void
376g_dev_strategy(struct bio *bp)
377{
378 struct g_geom *gp;
379 struct g_consumer *cp;
380 struct bio *bp2;
381 dev_t dev;
382
383 KASSERT(bp->bio_cmd == BIO_READ ||
384 bp->bio_cmd == BIO_WRITE ||
385 bp->bio_cmd == BIO_DELETE,
386 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
387 dev = bp->bio_dev;
388 gp = dev->si_drv1;
389 cp = dev->si_drv2;
390 bp2 = g_clone_bio(bp);
391 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
392 bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT;
393 KASSERT(bp2->bio_offset >= 0,
394 ("Negative bio_offset (%jd) on bio %p",
395 (intmax_t)bp2->bio_offset, bp));
396 bp2->bio_length = (off_t)bp->bio_bcount;
397 bp2->bio_done = g_dev_done;
398 g_trace(G_T_BIO,
399 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
400 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
401 bp2->bio_data, bp2->bio_cmd);
402 g_io_request(bp2, cp);
403}
404
405/*
406 * g_dev_orphan()
407 *
408 * Called from below when the provider orphaned us. It is our responsibility
409 * to get the access counts back to zero, until we do so the stack below will
410 * not unravel. We must clear the kernel-dump settings, if this is the
411 * current dumpdev. We call destroy_dev(9) to send our dev_t the way of
412 * punched cards and if we have non-zero access counts, we call down with
413 * them negated before we detattch and selfdestruct.
414 */
415
416static void
417g_dev_orphan(struct g_consumer *cp)
418{
419 struct g_geom *gp;
420 dev_t dev;
421
422 gp = cp->geom;
423 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
424 g_topology_assert();
425 if (cp->biocount > 0)
426 return;
427 dev = gp->softc;
428 if (dev->si_flags & SI_DUMPDEV)
429 set_dumper(NULL);
430 /* XXX: we may need Giant for now */
431 destroy_dev(dev);
432 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
433 g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace);
434 g_detach(cp);
435 g_destroy_consumer(cp);
436 g_destroy_geom(gp);
437}
438
439DECLARE_GEOM_CLASS(g_dev_class, g_dev);
121 /* g_topology_lock(); */
122 LIST_FOREACH(gp, &g_dev_class.geom, geom) {
123 if (strcmp(gp->name, name))
124 continue;
125 *dev = gp->softc;
126 g_trace(G_T_TOPOLOGY, "g_dev_clone(%s) = %p", name, *dev);
127 return;
128 }
129 /* g_topology_unlock(); */
130 return;
131}
132
133static void
134g_dev_register_cloner(void *foo __unused)
135{
136 static int once;
137
138 /* XXX: why would this happen more than once ?? */
139 if (!once) {
140 EVENTHANDLER_REGISTER(dev_clone, g_dev_clone, 0, 1000);
141 once++;
142 }
143}
144
145SYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL);
146
147static struct g_geom *
148g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
149{
150 struct g_geom *gp;
151 struct g_consumer *cp;
152 static int unit;
153 int error;
154 dev_t dev;
155
156 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
157 g_topology_assert();
158 LIST_FOREACH(cp, &pp->consumers, consumers)
159 if (cp->geom->class == mp)
160 return (NULL);
161 gp = g_new_geomf(mp, pp->name);
162 gp->orphan = g_dev_orphan;
163 cp = g_new_consumer(gp);
164 error = g_attach(cp, pp);
165 KASSERT(error == 0,
166 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
167 /*
168 * XXX: I'm not 100% sure we can call make_dev(9) without Giant
169 * yet. Once we can, we don't need to drop topology here either.
170 */
171 g_topology_unlock();
172 mtx_lock(&Giant);
173 dev = make_dev(&g_dev_cdevsw, unit2minor(unit++),
174 UID_ROOT, GID_OPERATOR, 0640, gp->name);
175 mtx_unlock(&Giant);
176 g_topology_lock();
177
178 gp->softc = dev;
179 dev->si_drv1 = gp;
180 dev->si_drv2 = cp;
181 return (gp);
182}
183
184static int
185g_dev_open(dev_t dev, int flags, int fmt, struct thread *td)
186{
187 struct g_geom *gp;
188 struct g_consumer *cp;
189 int error, r, w, e;
190
191 gp = dev->si_drv1;
192 cp = dev->si_drv2;
193 if (gp == NULL || cp == NULL)
194 return(ENXIO);
195 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
196 gp->name, flags, fmt, td);
197 DROP_GIANT();
198 g_topology_lock();
199 g_silence();
200 r = flags & FREAD ? 1 : 0;
201 w = flags & FWRITE ? 1 : 0;
202#ifdef notyet
203 e = flags & O_EXCL ? 1 : 0;
204#else
205 e = 0;
206#endif
207 error = g_access_rel(cp, r, w, e);
208 g_topology_unlock();
209 PICKUP_GIANT();
210 g_waitidle();
211 return(error);
212}
213
214static int
215g_dev_close(dev_t dev, int flags, int fmt, struct thread *td)
216{
217 struct g_geom *gp;
218 struct g_consumer *cp;
219 int error, r, w, e;
220
221 gp = dev->si_drv1;
222 cp = dev->si_drv2;
223 if (gp == NULL || cp == NULL)
224 return(ENXIO);
225 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
226 gp->name, flags, fmt, td);
227 DROP_GIANT();
228 g_topology_lock();
229 g_silence();
230 r = flags & FREAD ? -1 : 0;
231 w = flags & FWRITE ? -1 : 0;
232#ifdef notyet
233 e = flags & O_EXCL ? -1 : 0;
234#else
235 e = 0;
236#endif
237 error = g_access_rel(cp, r, w, e);
238 g_topology_unlock();
239 PICKUP_GIANT();
240 g_waitidle();
241 return (error);
242}
243
244static int
245g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
246{
247 struct g_geom *gp, *gp2;
248 struct g_consumer *cp;
249 struct g_provider *pp2;
250 struct g_kerneldump kd;
251 int i, error;
252 u_int u;
253 struct g_ioctl *gio;
254
255 gp = dev->si_drv1;
256 cp = dev->si_drv2;
257 pp2 = cp->provider;
258 gp2 = pp2->geom;
259 gio = NULL;
260
261 error = 0;
262 DROP_GIANT();
263
264 i = IOCPARM_LEN(cmd);
265 switch (cmd) {
266 case DIOCGSECTORSIZE:
267 *(u_int *)data = cp->provider->sectorsize;
268 if (*(u_int *)data == 0)
269 error = ENOENT;
270 break;
271 case DIOCGMEDIASIZE:
272 *(off_t *)data = cp->provider->mediasize;
273 if (*(off_t *)data == 0)
274 error = ENOENT;
275 break;
276 case DIOCGFWSECTORS:
277 error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
278 if (error == 0 && *(u_int *)data == 0)
279 error = ENOENT;
280 break;
281 case DIOCGFWHEADS:
282 error = g_io_getattr("GEOM::fwheads", cp, &i, data);
283 if (error == 0 && *(u_int *)data == 0)
284 error = ENOENT;
285 break;
286 case DIOCGFRONTSTUFF:
287 error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
288 break;
289 case DIOCSKERNELDUMP:
290 u = *((u_int *)data);
291 if (!u) {
292 set_dumper(NULL);
293 error = 0;
294 break;
295 }
296 kd.offset = 0;
297 kd.length = OFF_MAX;
298 i = sizeof kd;
299 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
300 if (!error)
301 dev->si_flags |= SI_DUMPDEV;
302 break;
303
304 default:
305 gio = g_malloc(sizeof *gio, M_WAITOK | M_ZERO);
306 gio->cmd = cmd;
307 gio->data = data;
308 gio->fflag = fflag;
309 gio->td = td;
310 i = sizeof *gio;
311 if (cmd & IOC_IN)
312 error = g_io_setattr("GEOM::ioctl", cp, i, gio);
313 else
314 error = g_io_getattr("GEOM::ioctl", cp, &i, gio);
315 break;
316 }
317
318 PICKUP_GIANT();
319 if (error == EDIRIOCTL) {
320 KASSERT(gio != NULL, ("NULL gio but EDIRIOCTL"));
321 KASSERT(gio->func != NULL, ("NULL function but EDIRIOCTL"));
322 error = (gio->func)(gio->dev, cmd, data, fflag, td);
323 }
324 if (gio != NULL)
325 g_free(gio);
326 g_waitidle();
327 if (error == ENOIOCTL) {
328 if (g_debugflags & G_T_TOPOLOGY) {
329 i = IOCGROUP(cmd);
330 printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name);
331 if (i > ' ' && i <= '~')
332 printf(" '%c'", (int)IOCGROUP(cmd));
333 else
334 printf(" 0x%lx", IOCGROUP(cmd));
335 printf("/%ld ", cmd & 0xff);
336 if (cmd & IOC_IN)
337 printf("I");
338 if (cmd & IOC_OUT)
339 printf("O");
340 printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd));
341 }
342 error = ENOTTY;
343 }
344 return (error);
345}
346
347static int
348g_dev_psize(dev_t dev)
349{
350 struct g_consumer *cp;
351 off_t mediasize;
352
353 cp = dev->si_drv2;
354
355 mediasize = cp->provider->mediasize;
356 return (mediasize >> DEV_BSHIFT);
357}
358
359static void
360g_dev_done(struct bio *bp2)
361{
362 struct bio *bp;
363
364 bp = bp2->bio_linkage;
365 bp->bio_error = bp2->bio_error;
366 if (bp->bio_error != 0) {
367 g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
368 bp2, bp->bio_error);
369 bp->bio_flags |= BIO_ERROR;
370 } else {
371 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
372 bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
373 }
374 bp->bio_resid = bp->bio_bcount - bp2->bio_completed;
375 g_destroy_bio(bp2);
376 mtx_lock(&Giant);
377 biodone(bp);
378 mtx_unlock(&Giant);
379}
380
381static void
382g_dev_strategy(struct bio *bp)
383{
384 struct g_geom *gp;
385 struct g_consumer *cp;
386 struct bio *bp2;
387 dev_t dev;
388
389 KASSERT(bp->bio_cmd == BIO_READ ||
390 bp->bio_cmd == BIO_WRITE ||
391 bp->bio_cmd == BIO_DELETE,
392 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
393 dev = bp->bio_dev;
394 gp = dev->si_drv1;
395 cp = dev->si_drv2;
396 bp2 = g_clone_bio(bp);
397 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
398 bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT;
399 KASSERT(bp2->bio_offset >= 0,
400 ("Negative bio_offset (%jd) on bio %p",
401 (intmax_t)bp2->bio_offset, bp));
402 bp2->bio_length = (off_t)bp->bio_bcount;
403 bp2->bio_done = g_dev_done;
404 g_trace(G_T_BIO,
405 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
406 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
407 bp2->bio_data, bp2->bio_cmd);
408 g_io_request(bp2, cp);
409}
410
411/*
412 * g_dev_orphan()
413 *
414 * Called from below when the provider orphaned us. It is our responsibility
415 * to get the access counts back to zero, until we do so the stack below will
416 * not unravel. We must clear the kernel-dump settings, if this is the
417 * current dumpdev. We call destroy_dev(9) to send our dev_t the way of
418 * punched cards and if we have non-zero access counts, we call down with
419 * them negated before we detattch and selfdestruct.
420 */
421
422static void
423g_dev_orphan(struct g_consumer *cp)
424{
425 struct g_geom *gp;
426 dev_t dev;
427
428 gp = cp->geom;
429 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
430 g_topology_assert();
431 if (cp->biocount > 0)
432 return;
433 dev = gp->softc;
434 if (dev->si_flags & SI_DUMPDEV)
435 set_dumper(NULL);
436 /* XXX: we may need Giant for now */
437 destroy_dev(dev);
438 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
439 g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace);
440 g_detach(cp);
441 g_destroy_consumer(cp);
442 g_destroy_geom(gp);
443}
444
445DECLARE_GEOM_CLASS(g_dev_class, g_dev);