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