xref: /freebsd/sys/geom/geom_disk.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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 "opt_geom.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/bio.h>
46 #include <sys/conf.h>
47 #include <sys/fcntl.h>
48 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
50 #include <sys/devicestat.h>
51 #include <machine/md_var.h>
52 
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <geom/geom.h>
56 #include <geom/geom_disk.h>
57 #include <geom/geom_int.h>
58 
59 static struct mtx g_disk_done_mtx;
60 
61 static g_access_t g_disk_access;
62 static g_init_t g_disk_init;
63 static g_fini_t g_disk_fini;
64 static g_start_t g_disk_start;
65 static g_ioctl_t g_disk_ioctl;
66 static g_dumpconf_t g_disk_dumpconf;
67 
68 static struct g_class g_disk_class = {
69 	.name = "DISK",
70 	.version = G_VERSION,
71 	.init = g_disk_init,
72 	.fini = g_disk_fini,
73 	.start = g_disk_start,
74 	.access = g_disk_access,
75 	.ioctl = g_disk_ioctl,
76 	.dumpconf = g_disk_dumpconf,
77 };
78 
79 static void
80 g_disk_init(struct g_class *mp __unused)
81 {
82 
83 	mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
84 }
85 
86 static void
87 g_disk_fini(struct g_class *mp __unused)
88 {
89 
90 	mtx_destroy(&g_disk_done_mtx);
91 }
92 
93 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
94 
95 static void __inline
96 g_disk_lock_giant(struct disk *dp)
97 {
98 	if (dp->d_flags & DISKFLAG_NEEDSGIANT)
99 		mtx_lock(&Giant);
100 }
101 
102 static void __inline
103 g_disk_unlock_giant(struct disk *dp)
104 {
105 	if (dp->d_flags & DISKFLAG_NEEDSGIANT)
106 		mtx_unlock(&Giant);
107 }
108 
109 static int
110 g_disk_access(struct g_provider *pp, int r, int w, int e)
111 {
112 	struct disk *dp;
113 	int error;
114 
115 	g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
116 	    pp->name, r, w, e);
117 	g_topology_assert();
118 	dp = pp->geom->softc;
119 	if (dp == NULL || dp->d_destroyed) {
120 		/*
121 		 * Allow decreasing access count even if disk is not
122 		 * avaliable anymore.
123 		 */
124 		if (r <= 0 && w <= 0 && e <= 0)
125 			return (0);
126 		return (ENXIO);
127 	}
128 	r += pp->acr;
129 	w += pp->acw;
130 	e += pp->ace;
131 	error = 0;
132 	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
133 		if (dp->d_open != NULL) {
134 			g_disk_lock_giant(dp);
135 			error = dp->d_open(dp);
136 			if (bootverbose && error != 0)
137 				printf("Opened disk %s -> %d\n",
138 				    pp->name, error);
139 			g_disk_unlock_giant(dp);
140 		}
141 		pp->mediasize = dp->d_mediasize;
142 		pp->sectorsize = dp->d_sectorsize;
143 		dp->d_flags |= DISKFLAG_OPEN;
144 		if (dp->d_maxsize == 0) {
145 			printf("WARNING: Disk drive %s%d has no d_maxsize\n",
146 			    dp->d_name, dp->d_unit);
147 			dp->d_maxsize = DFLTPHYS;
148 		}
149 	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
150 		if (dp->d_close != NULL) {
151 			g_disk_lock_giant(dp);
152 			error = dp->d_close(dp);
153 			if (error != 0)
154 				printf("Closed disk %s -> %d\n",
155 				    pp->name, error);
156 			g_disk_unlock_giant(dp);
157 		}
158 		dp->d_flags &= ~DISKFLAG_OPEN;
159 	}
160 	return (error);
161 }
162 
163 static void
164 g_disk_kerneldump(struct bio *bp, struct disk *dp)
165 {
166 	int error;
167 	struct g_kerneldump *gkd;
168 	struct dumperinfo di;
169 	struct g_geom *gp;
170 
171 	gkd = (struct g_kerneldump*)bp->bio_data;
172 	gp = bp->bio_to->geom;
173 	g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
174 		gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
175 	if (dp->d_dump == NULL) {
176 		g_io_deliver(bp, ENODEV);
177 		return;
178 	}
179 	di.dumper = dp->d_dump;
180 	di.priv = dp;
181 	di.blocksize = dp->d_sectorsize;
182 	di.maxiosize = dp->d_maxsize;
183 	di.mediaoffset = gkd->offset;
184 	if ((gkd->offset + gkd->length) > dp->d_mediasize)
185 		gkd->length = dp->d_mediasize - gkd->offset;
186 	di.mediasize = gkd->length;
187 	error = set_dumper(&di);
188 	g_io_deliver(bp, error);
189 }
190 
191 static void
192 g_disk_done(struct bio *bp)
193 {
194 	struct bio *bp2;
195 	struct disk *dp;
196 
197 	/* See "notes" for why we need a mutex here */
198 	/* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
199 	mtx_lock(&g_disk_done_mtx);
200 	bp->bio_completed = bp->bio_length - bp->bio_resid;
201 
202 	bp2 = bp->bio_parent;
203 	if (bp2->bio_error == 0)
204 		bp2->bio_error = bp->bio_error;
205 	bp2->bio_completed += bp->bio_completed;
206 	if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) &&
207 	    (dp = bp2->bio_to->geom->softc)) {
208 		devstat_end_transaction_bio(dp->d_devstat, bp);
209 	}
210 	g_destroy_bio(bp);
211 	bp2->bio_inbed++;
212 	if (bp2->bio_children == bp2->bio_inbed) {
213 		bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
214 		g_io_deliver(bp2, bp2->bio_error);
215 	}
216 	mtx_unlock(&g_disk_done_mtx);
217 }
218 
219 static int
220 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
221 {
222 	struct g_geom *gp;
223 	struct disk *dp;
224 	int error;
225 
226 	gp = pp->geom;
227 	dp = gp->softc;
228 
229 	if (dp->d_ioctl == NULL)
230 		return (ENOIOCTL);
231 	g_disk_lock_giant(dp);
232 	error = dp->d_ioctl(dp, cmd, data, fflag, td);
233 	g_disk_unlock_giant(dp);
234 	return(error);
235 }
236 
237 static void
238 g_disk_start(struct bio *bp)
239 {
240 	struct bio *bp2, *bp3;
241 	struct disk *dp;
242 	int error;
243 	off_t off;
244 
245 	dp = bp->bio_to->geom->softc;
246 	if (dp == NULL || dp->d_destroyed) {
247 		g_io_deliver(bp, ENXIO);
248 		return;
249 	}
250 	error = EJUSTRETURN;
251 	switch(bp->bio_cmd) {
252 	case BIO_DELETE:
253 		if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
254 			error = 0;
255 			break;
256 		}
257 		/* fall-through */
258 	case BIO_READ:
259 	case BIO_WRITE:
260 		off = 0;
261 		bp3 = NULL;
262 		bp2 = g_clone_bio(bp);
263 		if (bp2 == NULL) {
264 			error = ENOMEM;
265 			break;
266 		}
267 		do {
268 			bp2->bio_offset += off;
269 			bp2->bio_length -= off;
270 			bp2->bio_data += off;
271 			if (bp2->bio_length > dp->d_maxsize) {
272 				/*
273 				 * XXX: If we have a stripesize we should really
274 				 * use it here.
275 				 */
276 				bp2->bio_length = dp->d_maxsize;
277 				off += dp->d_maxsize;
278 				/*
279 				 * To avoid a race, we need to grab the next bio
280 				 * before we schedule this one.  See "notes".
281 				 */
282 				bp3 = g_clone_bio(bp);
283 				if (bp3 == NULL)
284 					bp->bio_error = ENOMEM;
285 			}
286 			bp2->bio_done = g_disk_done;
287 			bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
288 			bp2->bio_bcount = bp2->bio_length;
289 			bp2->bio_disk = dp;
290 			devstat_start_transaction_bio(dp->d_devstat, bp2);
291 			g_disk_lock_giant(dp);
292 			dp->d_strategy(bp2);
293 			g_disk_unlock_giant(dp);
294 			bp2 = bp3;
295 			bp3 = NULL;
296 		} while (bp2 != NULL);
297 		break;
298 	case BIO_GETATTR:
299 		if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
300 			break;
301 		else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
302 			break;
303 		else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
304 			break;
305 		else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
306 			break;
307 		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
308 			g_disk_kerneldump(bp, dp);
309 		else
310 			error = ENOIOCTL;
311 		break;
312 	case BIO_FLUSH:
313 		g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)",
314 		    bp->bio_to->name);
315 		if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
316 			g_io_deliver(bp, ENODEV);
317 			return;
318 		}
319 		bp2 = g_clone_bio(bp);
320 		if (bp2 == NULL) {
321 			g_io_deliver(bp, ENOMEM);
322 			return;
323 		}
324 		bp2->bio_done = g_disk_done;
325 		bp2->bio_disk = dp;
326 		g_disk_lock_giant(dp);
327 		dp->d_strategy(bp2);
328 		g_disk_unlock_giant(dp);
329 		break;
330 	default:
331 		error = EOPNOTSUPP;
332 		break;
333 	}
334 	if (error != EJUSTRETURN)
335 		g_io_deliver(bp, error);
336 	return;
337 }
338 
339 static void
340 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
341 {
342 	struct disk *dp;
343 
344 	dp = gp->softc;
345 	if (dp == NULL)
346 		return;
347 	if (indent == NULL) {
348 		sbuf_printf(sb, " hd %u", dp->d_fwheads);
349 		sbuf_printf(sb, " sc %u", dp->d_fwsectors);
350 		return;
351 	}
352 	if (pp != NULL) {
353 		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
354 		    indent, dp->d_fwheads);
355 		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
356 		    indent, dp->d_fwsectors);
357 	}
358 }
359 
360 static void
361 g_disk_create(void *arg, int flag)
362 {
363 	struct g_geom *gp;
364 	struct g_provider *pp;
365 	struct disk *dp;
366 
367 	if (flag == EV_CANCEL)
368 		return;
369 	g_topology_assert();
370 	dp = arg;
371 	gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
372 	gp->softc = dp;
373 	pp = g_new_providerf(gp, "%s", gp->name);
374 	pp->mediasize = dp->d_mediasize;
375 	pp->sectorsize = dp->d_sectorsize;
376 	if (dp->d_flags & DISKFLAG_CANDELETE)
377 		pp->flags |= G_PF_CANDELETE;
378 	pp->stripeoffset = dp->d_stripeoffset;
379 	pp->stripesize = dp->d_stripesize;
380 	if (bootverbose)
381 		printf("GEOM: new disk %s\n", gp->name);
382 	dp->d_geom = gp;
383 	g_error_provider(pp, 0);
384 }
385 
386 static void
387 g_disk_destroy(void *ptr, int flag)
388 {
389 	struct disk *dp;
390 	struct g_geom *gp;
391 
392 	g_topology_assert();
393 	dp = ptr;
394 	gp = dp->d_geom;
395 	if (gp != NULL) {
396 		gp->softc = NULL;
397 		g_wither_geom(gp, ENXIO);
398 	}
399 	g_free(dp);
400 }
401 
402 /*
403  * We only allow [a-zA-Z0-9-_@#%.:] characters, the rest is converted to 'x<HH>'.
404  */
405 static void
406 g_disk_ident_adjust(char *ident, size_t size)
407 {
408 	char newid[DISK_IDENT_SIZE], tmp[4];
409 	size_t len;
410 	char *p;
411 
412 	bzero(newid, sizeof(newid));
413 	len = 0;
414 	for (p = ident; *p != '\0' && len < sizeof(newid) - 1; p++) {
415 		switch (*p) {
416 		default:
417 			if ((*p < 'a' || *p > 'z') &&
418 			    (*p < 'A' || *p > 'Z') &&
419 			    (*p < '0' || *p > '9')) {
420 				snprintf(tmp, sizeof(tmp), "x%02hhx", *p);
421 				strlcat(newid, tmp, sizeof(newid));
422 				len += 3;
423 				break;
424 			}
425 			/* FALLTHROUGH */
426 		case '-':
427 		case '_':
428 		case '@':
429 		case '#':
430 		case '%':
431 		case '.':
432 		case ':':
433 			newid[len++] = *p;
434 			break;
435 		}
436 	}
437 	bzero(ident, size);
438 	strlcpy(ident, newid, size);
439 }
440 
441 struct disk *
442 disk_alloc()
443 {
444 	struct disk *dp;
445 
446 	dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO);
447 	return (dp);
448 }
449 
450 void
451 disk_create(struct disk *dp, int version)
452 {
453 	if (version != DISK_VERSION_00 && version != DISK_VERSION_01) {
454 		printf("WARNING: Attempt to add disk %s%d %s",
455 		    dp->d_name, dp->d_unit,
456 		    " using incompatible ABI version of disk(9)\n");
457 		printf("WARNING: Ignoring disk %s%d\n",
458 		    dp->d_name, dp->d_unit);
459 		return;
460 	}
461 	KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
462 	KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
463 	KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
464 	KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
465 	if (dp->d_devstat == NULL)
466 		dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
467 		    dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
468 		    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
469 	dp->d_geom = NULL;
470 	g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
471 	g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
472 }
473 
474 void
475 disk_destroy(struct disk *dp)
476 {
477 
478 	g_cancel_event(dp);
479 	dp->d_destroyed = 1;
480 	if (dp->d_devstat != NULL)
481 		devstat_remove_entry(dp->d_devstat);
482 	g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
483 }
484 
485 void
486 disk_gone(struct disk *dp)
487 {
488 	struct g_geom *gp;
489 	struct g_provider *pp;
490 
491 	gp = dp->d_geom;
492 	if (gp != NULL)
493 		LIST_FOREACH(pp, &gp->provider, provider)
494 			g_wither_provider(pp, ENXIO);
495 }
496 
497 static void
498 g_kern_disks(void *p, int flag __unused)
499 {
500 	struct sbuf *sb;
501 	struct g_geom *gp;
502 	char *sp;
503 
504 	sb = p;
505 	sp = "";
506 	g_topology_assert();
507 	LIST_FOREACH(gp, &g_disk_class.geom, geom) {
508 		sbuf_printf(sb, "%s%s", sp, gp->name);
509 		sp = " ";
510 	}
511 	sbuf_finish(sb);
512 }
513 
514 static int
515 sysctl_disks(SYSCTL_HANDLER_ARGS)
516 {
517 	int error;
518 	struct sbuf *sb;
519 
520 	sb = sbuf_new_auto();
521 	g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
522 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
523 	sbuf_delete(sb);
524 	return error;
525 }
526 
527 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
528     sysctl_disks, "A", "names of available disks");
529 
530