xref: /freebsd/sys/geom/geom_dev.c (revision 224af215a6fe8d5e5e2c91cc97c48bdd67c991c7)
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 #include <geom/geom_int.h>
52 #include <machine/limits.h>
53 
54 #define CDEV_MAJOR	4
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 static d_psize_t	g_dev_psize;
61 
62 static struct cdevsw g_dev_cdevsw = {
63         /* open */      g_dev_open,
64         /* close */     g_dev_close,
65         /* read */      physread,
66         /* write */     physwrite,
67         /* ioctl */     g_dev_ioctl,
68         /* poll */      nopoll,
69         /* mmap */      nommap,
70         /* strategy */  g_dev_strategy,
71         /* name */      "g_dev",
72         /* maj */       CDEV_MAJOR,
73         /* dump */      nodump,
74         /* psize */     g_dev_psize,
75         /* flags */     D_DISK | D_CANFREE | D_TRACKCLOSE,
76 };
77 
78 static g_taste_t g_dev_taste;
79 static g_orphan_t g_dev_orphan;
80 
81 static struct g_class g_dev_class	= {
82 	"DEV",
83 	g_dev_taste,
84 	NULL,
85 	G_CLASS_INITIALIZER
86 };
87 
88 static void
89 g_dev_clone(void *arg, char *name, int namelen __unused, 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_waitidle();
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 	/* XXX: why would this happen more than 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 	int error;
133 	dev_t dev;
134 
135 	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
136 	g_topology_assert();
137 	LIST_FOREACH(cp, &pp->consumers, consumers)
138 		if (cp->geom->class == mp)
139 			return (NULL);
140 	gp = g_new_geomf(mp, pp->name);
141 	gp->orphan = g_dev_orphan;
142 	cp = g_new_consumer(gp);
143 	error = g_attach(cp, pp);
144 	KASSERT(error == 0,
145 	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
146 	/*
147 	 * XXX: I'm not 100% sure we can call make_dev(9) without Giant
148 	 * yet.  Once we can, we don't need to drop topology here either.
149 	 */
150 	g_topology_unlock();
151 	mtx_lock(&Giant);
152 	dev = make_dev(&g_dev_cdevsw, unit2minor(unit++),
153 	    UID_ROOT, GID_WHEEL, 0600, gp->name);
154 	mtx_unlock(&Giant);
155 	g_topology_lock();
156 
157 	gp->softc = dev;
158 	dev->si_drv1 = gp;
159 	dev->si_drv2 = cp;
160 	return (gp);
161 }
162 
163 static int
164 g_dev_open(dev_t dev, int flags, int fmt, struct thread *td)
165 {
166 	struct g_geom *gp;
167 	struct g_consumer *cp;
168 	int error, r, w, e;
169 
170 	gp = dev->si_drv1;
171 	cp = dev->si_drv2;
172 	if (gp == NULL || cp == NULL)
173 		return(ENXIO);
174 	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
175 	    gp->name, flags, fmt, td);
176 	DROP_GIANT();
177 	g_topology_lock();
178 	g_silence();
179 	r = flags & FREAD ? 1 : 0;
180 	w = flags & FWRITE ? 1 : 0;
181 #ifdef notyet
182 	e = flags & O_EXCL ? 1 : 0;
183 #else
184 	e = 0;
185 #endif
186 	error = g_access_rel(cp, r, w, e);
187 	g_topology_unlock();
188 	PICKUP_GIANT();
189 	g_waitidle();
190 	return(error);
191 }
192 
193 static int
194 g_dev_close(dev_t dev, int flags, int fmt, struct thread *td)
195 {
196 	struct g_geom *gp;
197 	struct g_consumer *cp;
198 	int error, r, w, e;
199 
200 	gp = dev->si_drv1;
201 	cp = dev->si_drv2;
202 	if (gp == NULL || cp == NULL)
203 		return(ENXIO);
204 	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
205 	    gp->name, flags, fmt, td);
206 	DROP_GIANT();
207 	g_topology_lock();
208 	g_silence();
209 	r = flags & FREAD ? -1 : 0;
210 	w = flags & FWRITE ? -1 : 0;
211 #ifdef notyet
212 	e = flags & O_EXCL ? -1 : 0;
213 #else
214 	e = 0;
215 #endif
216 	error = g_access_rel(cp, r, w, e);
217 	g_topology_unlock();
218 	PICKUP_GIANT();
219 	g_waitidle();
220 	return (error);
221 }
222 
223 static int
224 g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
225 {
226 	struct g_geom *gp, *gp2;
227 	struct g_consumer *cp;
228 	struct g_provider *pp2;
229 	struct g_kerneldump kd;
230 	int i, error;
231 	u_int u;
232 	struct g_ioctl *gio;
233 	struct sbuf *usb, *sb;
234 
235 	gp = dev->si_drv1;
236 	cp = dev->si_drv2;
237 	pp2 = cp->provider;
238 	gp2 = pp2->geom;
239 
240 	error = 0;
241 	DROP_GIANT();
242 
243 	i = IOCPARM_LEN(cmd);
244 	switch (cmd) {
245 	case DIOCGSECTORSIZE:
246 		error = g_io_getattr("GEOM::sectorsize", cp, &i, data);
247 		break;
248 	case DIOCGMEDIASIZE:
249 		error = g_io_getattr("GEOM::mediasize", cp, &i, data);
250 		break;
251 	case DIOCGFWSECTORS:
252 		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
253 		break;
254 	case DIOCGFWHEADS:
255 		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
256 		break;
257 	case DIOCGFRONTSTUFF:
258 		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
259 		break;
260 	case DIOCSKERNELDUMP:
261 		u = *((u_int *)data);
262 		if (!u) {
263 			set_dumper(NULL);
264 			error = 0;
265 			break;
266 		}
267 		kd.offset = 0;
268 		kd.length = OFF_MAX;
269 		i = sizeof kd;
270 		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
271 		if (!error)
272 			dev->si_flags |= SI_DUMPDEV;
273 		break;
274 	case GEOMGETCONF:
275 		/* we bogusly pass cp to avoid getting any consumers listed */
276 		sb = g_conf_specific(gp2->class, gp2, pp2, cp);
277 		usb = (struct sbuf *)data;
278 		if (usb->s_size - 1 < sbuf_len(sb))
279 			error = ENOMEM;
280 		else
281 			error = copyout(sbuf_data(sb), usb->s_buf, sbuf_len(sb) + 1);
282 		if (!error)
283 			usb->s_len = sbuf_len(sb);
284 		break;
285 	default:
286 		gio = g_malloc(sizeof *gio, M_WAITOK);
287 		gio->cmd = cmd;
288 		gio->data = data;
289 		gio->fflag = fflag;
290 		gio->td = td;
291 		i = sizeof *gio;
292 		if (cmd & IOC_IN)
293 			error = g_io_setattr("GEOM::ioctl", cp, i, gio);
294 		else
295 			error = g_io_getattr("GEOM::ioctl", cp, &i, gio);
296 		g_free(gio);
297 		break;
298 	}
299 
300 	PICKUP_GIANT();
301 	g_waitidle();
302 	if (error == ENOIOCTL) {
303 		i = IOCGROUP(cmd);
304 		printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name);
305 		if (i > ' ' && i <= '~')
306 			printf(" '%c'", (int)IOCGROUP(cmd));
307 		else
308 			printf(" 0x%lx", IOCGROUP(cmd));
309 		printf("/%ld ", cmd & 0xff);
310 		if (cmd & IOC_IN)
311 			printf("I");
312 		if (cmd & IOC_OUT)
313 			printf("O");
314 		printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd));
315 		error = ENOTTY;
316 	}
317 	return (error);
318 }
319 
320 static int
321 g_dev_psize(dev_t dev)
322 {
323 	struct g_consumer *cp;
324 	int i, error;
325 	off_t mediasize;
326 
327 	cp = dev->si_drv2;
328 
329 	i = sizeof mediasize;
330 	error = g_io_getattr("GEOM::mediasize", cp, &i, &mediasize);
331 	if (error)
332 		return (-1);
333 	return (mediasize >> DEV_BSHIFT);
334 }
335 
336 static void
337 g_dev_done(struct bio *bp2)
338 {
339 	struct bio *bp;
340 
341 	bp = bp2->bio_linkage;
342 	bp->bio_error = bp2->bio_error;
343 	if (bp->bio_error != 0) {
344 		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
345 		    bp2, bp->bio_error);
346 		bp->bio_flags |= BIO_ERROR;
347 	} else {
348 		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %lld",
349 		    bp2, bp, bp->bio_resid, bp2->bio_completed);
350 	}
351 	bp->bio_resid = bp->bio_bcount - bp2->bio_completed;
352 	g_destroy_bio(bp2);
353 	mtx_lock(&Giant);
354 	biodone(bp);
355 	mtx_unlock(&Giant);
356 }
357 
358 static void
359 g_dev_strategy(struct bio *bp)
360 {
361 	struct g_geom *gp;
362 	struct g_consumer *cp;
363 	struct bio *bp2;
364 	dev_t dev;
365 
366 	dev = bp->bio_dev;
367 	gp = dev->si_drv1;
368 	cp = dev->si_drv2;
369 	bp2 = g_clone_bio(bp);
370 	bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT;
371 	bp2->bio_length = (off_t)bp->bio_bcount;
372 	bp2->bio_done = g_dev_done;
373 	g_trace(G_T_BIO,
374 	    "g_dev_strategy(%p/%p) offset %lld length %lld data %p cmd %d",
375 	    bp, bp2, bp->bio_offset, bp2->bio_length, bp2->bio_data,
376 	    bp2->bio_cmd);
377 	g_io_request(bp2, cp);
378 }
379 
380 /*
381  * g_dev_orphan()
382  *
383  * Called from below when the provider orphaned us.  It is our responsibility
384  * to get the access counts back to zero, until we do so the stack below will
385  * not unravel.  We must clear the kernel-dump settings, if this is the
386  * current dumpdev.  We call destroy_dev(9) to send our dev_t the way of
387  * punched cards and if we have non-zero access counts, we call down with
388  * them negated before we detattch and selfdestruct.
389  */
390 
391 static void
392 g_dev_orphan(struct g_consumer *cp)
393 {
394 	struct g_geom *gp;
395 	dev_t dev;
396 
397 	gp = cp->geom;
398 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
399 	g_topology_assert();
400 	if (cp->biocount > 0)
401 		return;
402 	dev = gp->softc;
403 	if (dev->si_flags & SI_DUMPDEV)
404 		set_dumper(NULL);
405 	/* XXX: we may need Giant for now */
406 	destroy_dev(dev);
407 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
408 		g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace);
409 	g_detach(cp);
410 	g_destroy_consumer(cp);
411 	g_destroy_geom(gp);
412 }
413 
414 DECLARE_GEOM_CLASS(g_dev_class, g_dev);
415