xref: /freebsd/sys/geom/geom_dev.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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/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 
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 
61 static struct cdevsw g_dev_cdevsw = {
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,
71 };
72 
73 static g_taste_t g_dev_taste;
74 static g_orphan_t g_dev_orphan;
75 
76 static struct g_class g_dev_class	= {
77 	"DEV",
78 	g_dev_taste,
79 	NULL,
80 	G_CLASS_INITIALIZER
81 };
82 
83 int
84 g_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  */
104 static void
105 g_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 
126 static void
127 g_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 
138 SYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL);
139 
140 static struct g_geom *
141 g_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 
181 static int
182 g_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 
211 static int
212 g_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 
240 static int
241 g_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 
346 static void
347 g_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 
368 static void
369 g_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 
409 static void
410 g_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 
432 DECLARE_GEOM_CLASS(g_dev_class, g_dev);
433