geom_disk.c revision 112989
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 112989 2003-04-02 21:10:04Z 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	case BIO_SETATTR:
266		error = ENOIOCTL;
267		break;
268	default:
269		error = EOPNOTSUPP;
270		break;
271	}
272	if (error != EJUSTRETURN)
273		g_io_deliver(bp, error);
274	return;
275}
276
277static void
278g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
279{
280	struct disk *dp;
281
282	dp = gp->softc;
283	if (indent == NULL) {
284		sbuf_printf(sb, " hd %u", dp->d_fwheads);
285		sbuf_printf(sb, " sc %u", dp->d_fwsectors);
286		return;
287	}
288	if (pp != NULL) {
289		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
290		    indent, dp->d_fwheads);
291		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
292		    indent, dp->d_fwsectors);
293	}
294}
295
296static void
297g_disk_create(void *arg, int flag __unused)
298{
299	struct g_geom *gp;
300	struct g_provider *pp;
301	struct disk *dp;
302
303	g_topology_assert();
304	dp = arg;
305	gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
306	gp->start = g_disk_start;
307	gp->access = g_disk_access;
308	gp->softc = dp;
309	gp->dumpconf = g_disk_dumpconf;
310	pp = g_new_providerf(gp, "%s", gp->name);
311	pp->mediasize = dp->d_mediasize;
312	pp->sectorsize = dp->d_sectorsize;
313	if (dp->d_flags & DISKFLAG_CANDELETE)
314		pp->flags |= G_PF_CANDELETE;
315	pp->stripeoffset = dp->d_stripeoffset;
316	pp->stripesize = dp->d_stripesize;
317	if (bootverbose)
318		printf("GEOM: new disk %s\n", gp->name);
319	dp->d_geom = gp;
320	g_error_provider(pp, 0);
321}
322
323
324
325void
326disk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused)
327{
328
329	dp->d_unit = unit;
330	dp->d_flags = flags;
331	KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
332	KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
333	KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
334	KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
335	dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
336	    dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
337	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
338	dp->d_geom = NULL;
339	g_call_me(g_disk_create, dp, dp, NULL);
340}
341
342/*
343 * XXX: There is a race if disk_destroy() is called while the g_disk_create()
344 * XXX: event is running.  I belive the current result is that disk_destroy()
345 * XXX: actually doesn't do anything.  Considering that the driver owns the
346 * XXX: struct disk and is likely to free it in a few moments, this can
347 * XXX: hardly be said to be optimal.  To what extent we can sleep in
348 * XXX: disk_create() and disk_destroy() is currently undefined (but generally
349 * XXX: undesirable) so any solution seems to involve an intrusive decision.
350 */
351void
352disk_destroy(struct disk *dp)
353{
354	struct g_geom *gp;
355
356	g_cancel_event(dp);
357	gp = dp->d_geom;
358	if (gp == NULL)
359		return;
360	gp->flags |= G_GEOM_WITHER;
361	gp->softc = NULL;
362	g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
363	devstat_remove_entry(dp->d_devstat);
364}
365
366static void
367g_kern_disks(void *p, int flag __unused)
368{
369	struct sbuf *sb;
370	struct g_geom *gp;
371	char *sp;
372
373	sb = p;
374	sp = "";
375	g_topology_assert();
376	LIST_FOREACH(gp, &g_disk_class.geom, geom) {
377		sbuf_printf(sb, "%s%s", sp, gp->name);
378		sp = " ";
379	}
380	sbuf_finish(sb);
381	wakeup(sb);
382}
383
384static int
385sysctl_disks(SYSCTL_HANDLER_ARGS)
386{
387	int error;
388	struct sbuf *sb;
389
390	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
391	sbuf_clear(sb);
392	error = g_call_me(g_kern_disks, sb, NULL);
393	while (!error && !sbuf_done(sb)) {
394		tsleep(sb, PZERO, "kern.disks", hz);
395	}
396	if (!error)
397		error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
398	sbuf_delete(sb);
399	return error;
400}
401
402SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
403    sysctl_disks, "A", "names of available disks");
404
405