xref: /freebsd/sys/geom/geom_dev.c (revision 39beb93c)
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 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.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/proc.h>
48 #include <sys/errno.h>
49 #include <sys/time.h>
50 #include <sys/disk.h>
51 #include <sys/fcntl.h>
52 #include <sys/limits.h>
53 #include <geom/geom.h>
54 #include <geom/geom_int.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_version =	D_VERSION,
63 	.d_open =	g_dev_open,
64 	.d_close =	g_dev_close,
65 	.d_read =	physread,
66 	.d_write =	physwrite,
67 	.d_ioctl =	g_dev_ioctl,
68 	.d_strategy =	g_dev_strategy,
69 	.d_name =	"g_dev",
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 	.name = "DEV",
78 	.version = G_VERSION,
79 	.taste = g_dev_taste,
80 	.orphan = g_dev_orphan,
81 };
82 
83 void
84 g_dev_print(void)
85 {
86 	struct g_geom *gp;
87 	char const *p = "";
88 
89 	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
90 		printf("%s%s", p, gp->name);
91 		p = " ";
92 	}
93 	printf("\n");
94 }
95 
96 struct g_provider *
97 g_dev_getprovider(struct cdev *dev)
98 {
99 	struct g_consumer *cp;
100 
101 	g_topology_assert();
102 	if (dev == NULL)
103 		return (NULL);
104 	if (dev->si_devsw != &g_dev_cdevsw)
105 		return (NULL);
106 	cp = dev->si_drv2;
107 	return (cp->provider);
108 }
109 
110 
111 static struct g_geom *
112 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
113 {
114 	struct g_geom *gp;
115 	struct g_consumer *cp;
116 	char *alias;
117 	int error;
118 	struct cdev *dev;
119 
120 	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
121 	g_topology_assert();
122 	LIST_FOREACH(cp, &pp->consumers, consumers)
123 		if (cp->geom->class == mp)
124 			return (NULL);
125 	gp = g_new_geomf(mp, pp->name);
126 	cp = g_new_consumer(gp);
127 	error = g_attach(cp, pp);
128 	KASSERT(error == 0,
129 	    ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error));
130 	dev = make_dev(&g_dev_cdevsw, 0,
131 	    UID_ROOT, GID_OPERATOR, 0640, gp->name);
132 	if (pp->flags & G_PF_CANDELETE)
133 		dev->si_flags |= SI_CANDELETE;
134 	dev->si_iosize_max = MAXPHYS;
135 	gp->softc = dev;
136 	dev->si_drv1 = gp;
137 	dev->si_drv2 = cp;
138 
139 	g_topology_unlock();
140 
141 	alias = g_malloc(MAXPATHLEN, M_WAITOK | M_ZERO);
142 	error = (pp->geom->ioctl == NULL) ? ENODEV :
143 	    pp->geom->ioctl(pp, DIOCGPROVIDERALIAS, alias, 0, curthread);
144 	if (!error && alias[0] != '\0')
145 		make_dev_alias(dev, "%s", alias);
146 	g_free(alias);
147 
148 	g_topology_lock();
149 	return (gp);
150 }
151 
152 static int
153 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
154 {
155 	struct g_geom *gp;
156 	struct g_consumer *cp;
157 	int error, r, w, e;
158 
159 	gp = dev->si_drv1;
160 	cp = dev->si_drv2;
161 	if (gp == NULL || cp == NULL || gp->softc != dev)
162 		return(ENXIO);		/* g_dev_taste() not done yet */
163 
164 	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
165 	    gp->name, flags, fmt, td);
166 
167 	r = flags & FREAD ? 1 : 0;
168 	w = flags & FWRITE ? 1 : 0;
169 #ifdef notyet
170 	e = flags & O_EXCL ? 1 : 0;
171 #else
172 	e = 0;
173 #endif
174 	if (w) {
175 		/*
176 		 * When running in very secure mode, do not allow
177 		 * opens for writing of any disks.
178 		 */
179 		error = securelevel_ge(td->td_ucred, 2);
180 		if (error)
181 			return (error);
182 	}
183 	g_topology_lock();
184 	if (dev->si_devsw == NULL)
185 		error = ENXIO;		/* We were orphaned */
186 	else
187 		error = g_access(cp, r, w, e);
188 	g_topology_unlock();
189 	return(error);
190 }
191 
192 static int
193 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
194 {
195 	struct g_geom *gp;
196 	struct g_consumer *cp;
197 	int error, r, w, e, i;
198 
199 	gp = dev->si_drv1;
200 	cp = dev->si_drv2;
201 	if (gp == NULL || cp == NULL)
202 		return(ENXIO);
203 	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
204 	    gp->name, flags, fmt, td);
205 	r = flags & FREAD ? -1 : 0;
206 	w = flags & FWRITE ? -1 : 0;
207 #ifdef notyet
208 	e = flags & O_EXCL ? -1 : 0;
209 #else
210 	e = 0;
211 #endif
212 	g_topology_lock();
213 	if (dev->si_devsw == NULL)
214 		error = ENXIO;		/* We were orphaned */
215 	else
216 		error = g_access(cp, r, w, e);
217 	for (i = 0; i < 10 * hz;) {
218 		if (cp->acr != 0 || cp->acw != 0)
219 			break;
220  		if (cp->nstart == cp->nend)
221 			break;
222 		pause("gdevwclose", hz / 10);
223 		i += hz / 10;
224 	}
225 	if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) {
226 		printf("WARNING: Final close of geom_dev(%s) %s %s\n",
227 		    gp->name,
228 		    "still has outstanding I/O after 10 seconds.",
229 		    "Completing close anyway, panic may happen later.");
230 	}
231 	g_topology_unlock();
232 	return (error);
233 }
234 
235 /*
236  * XXX: Until we have unmessed the ioctl situation, there is a race against
237  * XXX: a concurrent orphanization.  We cannot close it by holding topology
238  * XXX: since that would prevent us from doing our job, and stalling events
239  * XXX: will break (actually: stall) the BSD disklabel hacks.
240  */
241 static int
242 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
243 {
244 	struct g_geom *gp;
245 	struct g_consumer *cp;
246 	struct g_provider *pp;
247 	struct g_kerneldump kd;
248 	off_t offset, length, chunk;
249 	int i, error;
250 	u_int u;
251 
252 	gp = dev->si_drv1;
253 	cp = dev->si_drv2;
254 	pp = cp->provider;
255 
256 	error = 0;
257 	KASSERT(cp->acr || cp->acw,
258 	    ("Consumer with zero access count in g_dev_ioctl"));
259 
260 	i = IOCPARM_LEN(cmd);
261 	switch (cmd) {
262 	case DIOCGSECTORSIZE:
263 		*(u_int *)data = cp->provider->sectorsize;
264 		if (*(u_int *)data == 0)
265 			error = ENOENT;
266 		break;
267 	case DIOCGMEDIASIZE:
268 		*(off_t *)data = cp->provider->mediasize;
269 		if (*(off_t *)data == 0)
270 			error = ENOENT;
271 		break;
272 	case DIOCGFWSECTORS:
273 		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
274 		if (error == 0 && *(u_int *)data == 0)
275 			error = ENOENT;
276 		break;
277 	case DIOCGFWHEADS:
278 		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
279 		if (error == 0 && *(u_int *)data == 0)
280 			error = ENOENT;
281 		break;
282 	case DIOCGFRONTSTUFF:
283 		error = g_io_getattr("GEOM::frontstuff", cp, &i, data);
284 		break;
285 	case DIOCSKERNELDUMP:
286 		u = *((u_int *)data);
287 		if (!u) {
288 			set_dumper(NULL);
289 			error = 0;
290 			break;
291 		}
292 		kd.offset = 0;
293 		kd.length = OFF_MAX;
294 		i = sizeof kd;
295 		error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd);
296 		if (!error)
297 			dev->si_flags |= SI_DUMPDEV;
298 		break;
299 	case DIOCGFLUSH:
300 		error = g_io_flush(cp);
301 		break;
302 	case DIOCGDELETE:
303 		offset = ((off_t *)data)[0];
304 		length = ((off_t *)data)[1];
305 		if ((offset % cp->provider->sectorsize) != 0 ||
306 		    (length % cp->provider->sectorsize) != 0 || length <= 0) {
307 			printf("%s: offset=%jd length=%jd\n", __func__, offset,
308 			    length);
309 			error = EINVAL;
310 			break;
311 		}
312 		while (length > 0) {
313 			chunk = length;
314 			if (chunk > 1024 * cp->provider->sectorsize)
315 				chunk = 1024 * cp->provider->sectorsize;
316 			error = g_delete_data(cp, offset, chunk);
317 			length -= chunk;
318 			offset += chunk;
319 			if (error)
320 				break;
321 			/*
322 			 * Since the request size is unbounded, the service
323 			 * time is likewise.  We make this ioctl interruptible
324 			 * by checking for signals for each bio.
325 			 */
326 			if (SIGPENDING(td))
327 				break;
328 		}
329 		break;
330 	case DIOCGIDENT:
331 		error = g_io_getattr("GEOM::ident", cp, &i, data);
332 		break;
333 	case DIOCGPROVIDERNAME:
334 		if (pp == NULL)
335 			return (ENOENT);
336 		strlcpy(data, pp->name, i);
337 		break;
338 
339 	default:
340 		if (cp->provider->geom->ioctl != NULL) {
341 			error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td);
342 		} else {
343 			error = ENOIOCTL;
344 		}
345 	}
346 
347 	return (error);
348 }
349 
350 static void
351 g_dev_done(struct bio *bp2)
352 {
353 	struct bio *bp;
354 
355 	bp = bp2->bio_parent;
356 	bp->bio_error = bp2->bio_error;
357 	if (bp->bio_error != 0) {
358 		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
359 		    bp2, bp->bio_error);
360 		bp->bio_flags |= BIO_ERROR;
361 	} else {
362 		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
363 		    bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed);
364 	}
365 	bp->bio_resid = bp->bio_length - bp2->bio_completed;
366 	bp->bio_completed = bp2->bio_completed;
367 	g_destroy_bio(bp2);
368 	biodone(bp);
369 }
370 
371 static void
372 g_dev_strategy(struct bio *bp)
373 {
374 	struct g_consumer *cp;
375 	struct bio *bp2;
376 	struct cdev *dev;
377 
378 	KASSERT(bp->bio_cmd == BIO_READ ||
379 	        bp->bio_cmd == BIO_WRITE ||
380 	        bp->bio_cmd == BIO_DELETE,
381 		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
382 	dev = bp->bio_dev;
383 	cp = dev->si_drv2;
384 	KASSERT(cp->acr || cp->acw,
385 	    ("Consumer with zero access count in g_dev_strategy"));
386 
387 	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
388 	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
389 		bp->bio_resid = bp->bio_bcount;
390 		biofinish(bp, NULL, EINVAL);
391 		return;
392 	}
393 
394 	for (;;) {
395 		/*
396 		 * XXX: This is not an ideal solution, but I belive it to
397 		 * XXX: deadlock safe, all things considered.
398 		 */
399 		bp2 = g_clone_bio(bp);
400 		if (bp2 != NULL)
401 			break;
402 		pause("gdstrat", hz / 10);
403 	}
404 	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
405 	bp2->bio_done = g_dev_done;
406 	g_trace(G_T_BIO,
407 	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
408 	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
409 	    bp2->bio_data, bp2->bio_cmd);
410 	g_io_request(bp2, cp);
411 	KASSERT(cp->acr || cp->acw,
412 	    ("g_dev_strategy raced with g_dev_close and lost"));
413 
414 }
415 
416 /*
417  * g_dev_orphan()
418  *
419  * Called from below when the provider orphaned us.
420  * - Clear any dump settings.
421  * - Destroy the struct cdev *to prevent any more request from coming in.  The
422  *   provider is already marked with an error, so anything which comes in
423  *   in the interrim will be returned immediately.
424  * - Wait for any outstanding I/O to finish.
425  * - Set our access counts to zero, whatever they were.
426  * - Detach and self-destruct.
427  */
428 
429 static void
430 g_dev_orphan(struct g_consumer *cp)
431 {
432 	struct g_geom *gp;
433 	struct cdev *dev;
434 
435 	g_topology_assert();
436 	gp = cp->geom;
437 	dev = gp->softc;
438 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name);
439 
440 	/* Reset any dump-area set on this device */
441 	if (dev->si_flags & SI_DUMPDEV)
442 		set_dumper(NULL);
443 
444 	/* Destroy the struct cdev *so we get no more requests */
445 	destroy_dev(dev);
446 
447 	/* Wait for the cows to come home */
448 	while (cp->nstart != cp->nend)
449 		pause("gdevorphan", hz / 10);
450 
451 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
452 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
453 
454 	g_detach(cp);
455 	g_destroy_consumer(cp);
456 	g_destroy_geom(gp);
457 }
458 
459 DECLARE_GEOM_CLASS(g_dev_class, g_dev);
460