xref: /freebsd/sys/geom/geom_dev.c (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
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, 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 	e = flags & O_EXCL ? 1 : 0;
182 	error = g_access_rel(cp, r, w, e);
183 	g_topology_unlock();
184 	PICKUP_GIANT();
185 	g_waitidle();
186 	return(error);
187 }
188 
189 static int
190 g_dev_close(dev_t dev, int flags, int fmt, struct thread *td)
191 {
192 	struct g_geom *gp;
193 	struct g_consumer *cp;
194 	int error, r, w, e;
195 
196 	gp = dev->si_drv1;
197 	cp = dev->si_drv2;
198 	if (gp == NULL || cp == NULL)
199 		return(ENXIO);
200 	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
201 	    gp->name, flags, fmt, td);
202 	DROP_GIANT();
203 	g_topology_lock();
204 	g_silence();
205 	r = flags & FREAD ? -1 : 0;
206 	w = flags & FWRITE ? -1 : 0;
207 	e = flags & O_EXCL ? -1 : 0;
208 	error = g_access_rel(cp, r, w, e);
209 	g_topology_unlock();
210 	PICKUP_GIANT();
211 	g_waitidle();
212 	return (error);
213 }
214 
215 static int
216 g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
217 {
218 	struct g_geom *gp, *gp2;
219 	struct g_consumer *cp;
220 	struct g_provider *pp2;
221 	struct g_kerneldump kd;
222 	int i, error;
223 	u_int u;
224 	struct g_ioctl *gio;
225 	struct sbuf *usb, *sb;
226 
227 	gp = dev->si_drv1;
228 	cp = dev->si_drv2;
229 	pp2 = cp->provider;
230 	gp2 = pp2->geom;
231 
232 	error = 0;
233 	DROP_GIANT();
234 
235 	i = IOCPARM_LEN(cmd);
236 	switch (cmd) {
237 	case DIOCGSECTORSIZE:
238 		error = g_io_getattr("GEOM::sectorsize", cp, &i, data);
239 		break;
240 	case DIOCGMEDIASIZE:
241 		error = g_io_getattr("GEOM::mediasize", cp, &i, data);
242 		break;
243 	case DIOCGFWSECTORS:
244 		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
245 		break;
246 	case DIOCGFWHEADS:
247 		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
248 		break;
249 	case DIOCGFRONTSTUFF:
250 		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
251 		break;
252 	case DIOCSKERNELDUMP:
253 		u = *((u_int *)data);
254 		if (!u) {
255 			set_dumper(NULL);
256 			error = 0;
257 			break;
258 		}
259 		kd.offset = 0;
260 		kd.length = OFF_MAX;
261 		i = sizeof kd;
262 		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
263 		if (!error)
264 			dev->si_flags |= SI_DUMPDEV;
265 		break;
266 	case GEOMGETCONF:
267 		/* we bogusly pass cp to avoid getting any consumers listed */
268 		sb = g_conf_specific(gp2->class, gp2, pp2, cp);
269 		usb = (struct sbuf *)data;
270 		if (usb->s_size - 1 < sbuf_len(sb))
271 			error = ENOMEM;
272 		else
273 			error = copyout(sbuf_data(sb), usb->s_buf, sbuf_len(sb) + 1);
274 		if (!error)
275 			usb->s_len = sbuf_len(sb);
276 		break;
277 	default:
278 		gio = g_malloc(sizeof *gio, M_WAITOK);
279 		gio->cmd = cmd;
280 		gio->data = data;
281 		gio->fflag = fflag;
282 		gio->td = td;
283 		i = sizeof *gio;
284 		if (cmd & IOC_IN)
285 			error = g_io_setattr("GEOM::ioctl", cp, i, gio);
286 		else
287 			error = g_io_getattr("GEOM::ioctl", cp, &i, gio);
288 		g_free(gio);
289 		break;
290 	}
291 
292 	if (error != 0 && cmd == DIOCGDVIRGIN) {
293 		g_topology_lock();
294 		gp = g_create_geomf("BSD", cp->provider, NULL);
295 		g_topology_unlock();
296 	}
297 	PICKUP_GIANT();
298 	g_waitidle();
299 	if (error == ENOIOCTL) {
300 		i = IOCGROUP(cmd);
301 		printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name);
302 		if (i > ' ' && i <= '~')
303 			printf(" '%c'", (int)IOCGROUP(cmd));
304 		else
305 			printf(" 0x%lx", IOCGROUP(cmd));
306 		printf("/%ld ", cmd & 0xff);
307 		if (cmd & IOC_IN)
308 			printf("I");
309 		if (cmd & IOC_OUT)
310 			printf("O");
311 		printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd));
312 		error = ENOTTY;
313 	}
314 	return (error);
315 }
316 
317 static int
318 g_dev_psize(dev_t dev)
319 {
320 	struct g_consumer *cp;
321 	int i, error;
322 	off_t mediasize;
323 
324 	cp = dev->si_drv2;
325 
326 	i = sizeof mediasize;
327 	error = g_io_getattr("GEOM::mediasize", cp, &i, &mediasize);
328 	if (error)
329 		return (-1);
330 	return (mediasize >> DEV_BSHIFT);
331 }
332 
333 static void
334 g_dev_done(struct bio *bp2)
335 {
336 	struct bio *bp;
337 
338 	bp = bp2->bio_linkage;
339 	bp->bio_error = bp2->bio_error;
340 	if (bp->bio_error != 0) {
341 		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
342 		    bp2, bp->bio_error);
343 		bp->bio_flags |= BIO_ERROR;
344 	} else {
345 		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %lld",
346 		    bp2, bp, bp->bio_resid, bp2->bio_completed);
347 	}
348 	bp->bio_resid = bp->bio_bcount - bp2->bio_completed;
349 	g_destroy_bio(bp2);
350 	mtx_lock(&Giant);
351 	biodone(bp);
352 	mtx_unlock(&Giant);
353 }
354 
355 static void
356 g_dev_strategy(struct bio *bp)
357 {
358 	struct g_geom *gp;
359 	struct g_consumer *cp;
360 	struct bio *bp2;
361 	dev_t dev;
362 
363 	dev = bp->bio_dev;
364 	gp = dev->si_drv1;
365 	cp = dev->si_drv2;
366 	bp2 = g_clone_bio(bp);
367 	bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT;
368 	bp2->bio_length = (off_t)bp->bio_bcount;
369 	bp2->bio_done = g_dev_done;
370 	g_trace(G_T_BIO,
371 	    "g_dev_strategy(%p/%p) offset %lld length %lld data %p cmd %d",
372 	    bp, bp2, bp->bio_offset, bp2->bio_length, bp2->bio_data,
373 	    bp2->bio_cmd);
374 	g_io_request(bp2, cp);
375 }
376 
377 /*
378  * g_dev_orphan()
379  *
380  * Called from below when the provider orphaned us.  It is our responsibility
381  * to get the access counts back to zero, until we do so the stack below will
382  * not unravel.  We must clear the kernel-dump settings, if this is the
383  * current dumpdev.  We call destroy_dev(9) to send our dev_t the way of
384  * punched cards and if we have non-zero access counts, we call down with
385  * them negated before we detattch and selfdestruct.
386  */
387 
388 static void
389 g_dev_orphan(struct g_consumer *cp)
390 {
391 	struct g_geom *gp;
392 	dev_t dev;
393 
394 	gp = cp->geom;
395 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
396 	g_topology_assert();
397 	if (cp->biocount > 0)
398 		return;
399 	dev = gp->softc;
400 	if (dev->si_flags & SI_DUMPDEV)
401 		set_dumper(NULL);
402 	/* XXX: we may need Giant for now */
403 	destroy_dev(dev);
404 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
405 		g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace);
406 	g_detach(cp);
407 	g_destroy_consumer(cp);
408 	g_destroy_geom(gp);
409 }
410 
411 DECLARE_GEOM_CLASS(g_dev_class, g_dev);
412