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