geom_disk.c revision 115473
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: head/sys/geom/geom_disk.c 115473 2003-05-31 18:13:07Z phk $
36 */
37
38#include "opt_geom.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/bio.h>
45#include <sys/conf.h>
46#include <sys/fcntl.h>
47#include <sys/malloc.h>
48#include <sys/sysctl.h>
49#include <sys/devicestat.h>
50#include <machine/md_var.h>
51
52#include <sys/lock.h>
53#include <sys/mutex.h>
54#include <geom/geom.h>
55#include <geom/geom_disk.h>
56#include <geom/geom_int.h>
57
58static struct mtx g_disk_done_mtx;
59
60static g_access_t g_disk_access;
61static g_init_t g_disk_init;
62static g_fini_t g_disk_fini;
63
64struct g_class g_disk_class = {
65	.name = "DISK",
66	.init = g_disk_init,
67	.fini = g_disk_fini,
68};
69
70static void
71g_disk_init(struct g_class *mp __unused)
72{
73
74	mtx_init(&g_disk_done_mtx, "g_disk_done", MTX_DEF, 0);
75}
76
77static void
78g_disk_fini(struct g_class *mp __unused)
79{
80
81	mtx_destroy(&g_disk_done_mtx);
82}
83
84DECLARE_GEOM_CLASS(g_disk_class, g_disk);
85
86static void __inline
87g_disk_lock_giant(struct disk *dp)
88{
89	if (dp->d_flags & DISKFLAG_NOGIANT)
90		return;
91	mtx_lock(&Giant);
92}
93
94static void __inline
95g_disk_unlock_giant(struct disk *dp)
96{
97	if (dp->d_flags & DISKFLAG_NOGIANT)
98		return;
99	mtx_unlock(&Giant);
100}
101
102static int
103g_disk_access(struct g_provider *pp, int r, int w, int e)
104{
105	struct disk *dp;
106	int error;
107
108	g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
109	    pp->name, r, w, e);
110	g_topology_assert();
111	r += pp->acr;
112	w += pp->acw;
113	e += pp->ace;
114	dp = pp->geom->softc;
115	if (dp == NULL)
116		return (ENXIO);
117	error = 0;
118	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
119		if (dp->d_open != NULL) {
120			g_disk_lock_giant(dp);
121			error = dp->d_open(dp);
122			if (error != 0)
123				printf("Opened disk %s -> %d\n",
124				    pp->name, error);
125			g_disk_unlock_giant(dp);
126		}
127		pp->mediasize = dp->d_mediasize;
128		pp->sectorsize = dp->d_sectorsize;
129		dp->d_flags |= DISKFLAG_OPEN;
130		if (dp->d_maxsize == 0) {
131			printf("WARNING: Disk drive %s%d has no d_maxsize\n",
132			    dp->d_name, dp->d_unit);
133			dp->d_maxsize = DFLTPHYS;
134		}
135	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
136		if (dp->d_close != NULL) {
137			g_disk_lock_giant(dp);
138			error = dp->d_close(dp);
139			if (error != 0)
140				printf("Closed disk %s -> %d\n",
141				    pp->name, error);
142			g_disk_unlock_giant(dp);
143		}
144		dp->d_flags &= ~DISKFLAG_OPEN;
145	}
146	return (error);
147}
148
149static void
150g_disk_kerneldump(struct bio *bp, struct disk *dp)
151{
152	int error;
153	struct g_kerneldump *gkd;
154	struct dumperinfo di;
155	struct g_geom *gp;
156
157	gkd = (struct g_kerneldump*)bp->bio_data;
158	gp = bp->bio_to->geom;
159	g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
160		gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
161	di.dumper = dp->d_dump;
162	di.priv = dp;
163	di.blocksize = dp->d_sectorsize;
164	di.mediaoffset = gkd->offset;
165	di.mediasize = gkd->length;
166	error = set_dumper(&di);
167	g_io_deliver(bp, error);
168}
169
170static void
171g_disk_done(struct bio *bp)
172{
173	struct bio *bp2;
174	struct disk *dp;
175
176	/* See "notes" for why we need a mutex here */
177	/* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
178	mtx_lock(&g_disk_done_mtx);
179	bp->bio_completed = bp->bio_length - bp->bio_resid;
180
181	bp2 = bp->bio_parent;
182	dp = bp2->bio_to->geom->softc;
183	if (bp2->bio_error == 0)
184		bp2->bio_error = bp->bio_error;
185	bp2->bio_completed += bp->bio_completed;
186	g_destroy_bio(bp);
187	bp2->bio_inbed++;
188	if (bp2->bio_children == bp2->bio_inbed) {
189		bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
190		devstat_end_transaction_bio(dp->d_devstat, bp2);
191		g_io_deliver(bp2, bp2->bio_error);
192	}
193	mtx_unlock(&g_disk_done_mtx);
194}
195
196static void
197g_disk_start(struct bio *bp)
198{
199	struct bio *bp2, *bp3;
200	struct disk *dp;
201	struct g_ioctl *gio;
202	int error;
203	off_t off;
204
205	dp = bp->bio_to->geom->softc;
206	if (dp == NULL)
207		g_io_deliver(bp, ENXIO);
208	error = EJUSTRETURN;
209	switch(bp->bio_cmd) {
210	case BIO_DELETE:
211		if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
212			error = 0;
213			break;
214		}
215		/* fall-through */
216	case BIO_READ:
217	case BIO_WRITE:
218		off = 0;
219		bp3 = NULL;
220		bp2 = g_clone_bio(bp);
221		if (bp2 == NULL) {
222			error = ENOMEM;
223			break;
224		}
225		devstat_start_transaction_bio(dp->d_devstat, bp);
226		do {
227			bp2->bio_offset += off;
228			bp2->bio_length -= off;
229			bp2->bio_data += off;
230			if (bp2->bio_length > dp->d_maxsize) {
231				/*
232				 * XXX: If we have a stripesize we should really
233				 * use it here.
234				 */
235				bp2->bio_length = dp->d_maxsize;
236				off += dp->d_maxsize;
237				/*
238				 * To avoid a race, we need to grab the next bio
239				 * before we schedule this one.  See "notes".
240				 */
241				bp3 = g_clone_bio(bp);
242				if (bp3 == NULL)
243					bp->bio_error = ENOMEM;
244			}
245			bp2->bio_done = g_disk_done;
246			bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
247			bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
248			bp2->bio_bcount = bp2->bio_length;
249			bp2->bio_disk = dp;
250			g_disk_lock_giant(dp);
251			dp->d_strategy(bp2);
252			g_disk_unlock_giant(dp);
253			bp2 = bp3;
254			bp3 = NULL;
255		} while (bp2 != NULL);
256		break;
257	case BIO_GETATTR:
258		if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
259			break;
260		else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
261			break;
262		else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
263			break;
264		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
265			g_disk_kerneldump(bp, dp);
266		else if ((g_debugflags & G_F_DISKIOCTL) &&
267		    (dp->d_ioctl != NULL) &&
268		    !strcmp(bp->bio_attribute, "GEOM::ioctl") &&
269		    bp->bio_length == sizeof *gio) {
270			gio = (struct g_ioctl *)bp->bio_data;
271			gio->dev =  dp;
272			gio->func = (d_ioctl_t *)(dp->d_ioctl);
273			error = EDIRIOCTL;
274		} else
275			error = ENOIOCTL;
276		break;
277	default:
278		error = EOPNOTSUPP;
279		break;
280	}
281	if (error != EJUSTRETURN)
282		g_io_deliver(bp, error);
283	return;
284}
285
286static void
287g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
288{
289	struct disk *dp;
290
291	dp = gp->softc;
292	if (indent == NULL) {
293		sbuf_printf(sb, " hd %u", dp->d_fwheads);
294		sbuf_printf(sb, " sc %u", dp->d_fwsectors);
295		return;
296	}
297	if (pp != NULL) {
298		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
299		    indent, dp->d_fwheads);
300		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
301		    indent, dp->d_fwsectors);
302	}
303}
304
305static void
306g_disk_create(void *arg, int flag)
307{
308	struct g_geom *gp;
309	struct g_provider *pp;
310	struct disk *dp;
311
312	if (flag == EV_CANCEL)
313		return;
314	g_topology_assert();
315	dp = arg;
316	gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
317	gp->start = g_disk_start;
318	gp->access = g_disk_access;
319	gp->softc = dp;
320	gp->dumpconf = g_disk_dumpconf;
321	pp = g_new_providerf(gp, "%s", gp->name);
322	pp->mediasize = dp->d_mediasize;
323	pp->sectorsize = dp->d_sectorsize;
324	if (dp->d_flags & DISKFLAG_CANDELETE)
325		pp->flags |= G_PF_CANDELETE;
326	pp->stripeoffset = dp->d_stripeoffset;
327	pp->stripesize = dp->d_stripesize;
328	if (bootverbose)
329		printf("GEOM: new disk %s\n", gp->name);
330	dp->d_geom = gp;
331	g_error_provider(pp, 0);
332}
333
334
335
336void
337disk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused)
338{
339
340	dp->d_unit = unit;
341	dp->d_flags = flags;
342	KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
343	KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
344	KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
345	KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
346	dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
347	    dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
348	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
349	dp->d_geom = NULL;
350	g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
351}
352
353/*
354 * XXX: There is a race if disk_destroy() is called while the g_disk_create()
355 * XXX: event is running.  I belive the current result is that disk_destroy()
356 * XXX: actually doesn't do anything.  Considering that the driver owns the
357 * XXX: struct disk and is likely to free it in a few moments, this can
358 * XXX: hardly be said to be optimal.  To what extent we can sleep in
359 * XXX: disk_create() and disk_destroy() is currently undefined (but generally
360 * XXX: undesirable) so any solution seems to involve an intrusive decision.
361 */
362
363static void
364disk_destroy_event(void *ptr, int flag)
365{
366
367	g_topology_assert();
368	g_wither_geom(ptr, ENXIO);
369}
370
371void
372disk_destroy(struct disk *dp)
373{
374	struct g_geom *gp;
375
376	g_cancel_event(dp);
377	gp = dp->d_geom;
378	if (gp == NULL)
379		return;
380	gp->softc = NULL;
381	devstat_remove_entry(dp->d_devstat);
382	g_post_event(disk_destroy_event, gp, M_WAITOK, NULL, NULL);
383}
384
385static void
386g_kern_disks(void *p, int flag __unused)
387{
388	struct sbuf *sb;
389	struct g_geom *gp;
390	char *sp;
391
392	sb = p;
393	sp = "";
394	g_topology_assert();
395	LIST_FOREACH(gp, &g_disk_class.geom, geom) {
396		sbuf_printf(sb, "%s%s", sp, gp->name);
397		sp = " ";
398	}
399	sbuf_finish(sb);
400}
401
402static int
403sysctl_disks(SYSCTL_HANDLER_ARGS)
404{
405	int error;
406	struct sbuf *sb;
407
408	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
409	sbuf_clear(sb);
410	g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
411	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
412	sbuf_delete(sb);
413	return error;
414}
415
416SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
417    sysctl_disks, "A", "names of available disks");
418
419