geom_disk.c revision 115214
192108Sphk/*-
292108Sphk * Copyright (c) 2002 Poul-Henning Kamp
392108Sphk * Copyright (c) 2002 Networks Associates Technology, Inc.
492108Sphk * All rights reserved.
592108Sphk *
692108Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp
792108Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc.
892108Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
992108Sphk * DARPA CHATS research program.
1092108Sphk *
1192108Sphk * Redistribution and use in source and binary forms, with or without
1292108Sphk * modification, are permitted provided that the following conditions
1392108Sphk * are met:
1492108Sphk * 1. Redistributions of source code must retain the above copyright
1592108Sphk *    notice, this list of conditions and the following disclaimer.
1692108Sphk * 2. Redistributions in binary form must reproduce the above copyright
1792108Sphk *    notice, this list of conditions and the following disclaimer in the
1892108Sphk *    documentation and/or other materials provided with the distribution.
1992108Sphk * 3. The names of the authors may not be used to endorse or promote
2092108Sphk *    products derived from this software without specific prior written
2192108Sphk *    permission.
2292108Sphk *
2392108Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2492108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2592108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2692108Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2792108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2892108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2992108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3092108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3192108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3292108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3392108Sphk * SUCH DAMAGE.
3492108Sphk *
3592108Sphk * $FreeBSD: head/sys/geom/geom_disk.c 115214 2003-05-21 18:52:29Z phk $
3692108Sphk */
3792108Sphk
38104519Sphk#include "opt_geom.h"
39104519Sphk
4092108Sphk#include <sys/param.h>
4192108Sphk#include <sys/systm.h>
4292108Sphk#include <sys/kernel.h>
4392108Sphk#include <sys/sysctl.h>
4492108Sphk#include <sys/bio.h>
4592108Sphk#include <sys/conf.h>
46110119Sphk#include <sys/fcntl.h>
4792108Sphk#include <sys/malloc.h>
4892108Sphk#include <sys/sysctl.h>
49111979Sphk#include <sys/devicestat.h>
5092108Sphk#include <machine/md_var.h>
5192108Sphk
5292108Sphk#include <sys/lock.h>
5392108Sphk#include <sys/mutex.h>
5492108Sphk#include <geom/geom.h>
55112952Sphk#include <geom/geom_disk.h>
56112476Sphk#include <geom/geom_int.h>
5792108Sphk
58110720Sphkstatic struct mtx g_disk_done_mtx;
59110720Sphk
6092108Sphkstatic g_access_t g_disk_access;
6192108Sphk
6293248Sphkstruct g_class g_disk_class = {
63112552Sphk	.name = "DISK",
6498066Sphk	G_CLASS_INITIALIZER
6592108Sphk};
6692108Sphk
67110720Sphkstatic void
68110720Sphkg_disk_init(void)
69110720Sphk{
70110720Sphk	mtx_unlock(&Giant);
71110720Sphk	g_add_class(&g_disk_class);
72110720Sphk	mtx_init(&g_disk_done_mtx, "g_disk_done", MTX_DEF, 0);
73110720Sphk	mtx_lock(&Giant);
74110720Sphk}
75104936Sphk
76110720SphkDECLARE_GEOM_CLASS_INIT(g_disk_class, g_disk, g_disk_init);
77110720Sphk
78110052Sphkstatic void __inline
79110052Sphkg_disk_lock_giant(struct disk *dp)
80110052Sphk{
81110119Sphk	if (dp->d_flags & DISKFLAG_NOGIANT)
82110119Sphk		return;
83110119Sphk	mtx_lock(&Giant);
84110052Sphk}
85110052Sphk
86110052Sphkstatic void __inline
87110052Sphkg_disk_unlock_giant(struct disk *dp)
88110052Sphk{
89110119Sphk	if (dp->d_flags & DISKFLAG_NOGIANT)
90110119Sphk		return;
91110119Sphk	mtx_unlock(&Giant);
92110052Sphk}
93110052Sphk
9492108Sphkstatic int
9592108Sphkg_disk_access(struct g_provider *pp, int r, int w, int e)
9692108Sphk{
9792108Sphk	struct disk *dp;
9892108Sphk	int error;
9992108Sphk
10092108Sphk	g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
10192108Sphk	    pp->name, r, w, e);
10292108Sphk	g_topology_assert();
10392108Sphk	r += pp->acr;
10492108Sphk	w += pp->acw;
10592108Sphk	e += pp->ace;
10692108Sphk	dp = pp->geom->softc;
107115214Sphk	if (dp == NULL)
108115214Sphk		return (ENXIO);
109110119Sphk	error = 0;
11092108Sphk	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
111111668Sphk		if (dp->d_open != NULL) {
112110119Sphk			g_disk_lock_giant(dp);
113111668Sphk			error = dp->d_open(dp);
114110119Sphk			if (error != 0)
115110119Sphk				printf("Opened disk %s -> %d\n",
116110119Sphk				    pp->name, error);
117110119Sphk			g_disk_unlock_giant(dp);
118110119Sphk		}
119105551Sphk		pp->mediasize = dp->d_mediasize;
120105551Sphk		pp->sectorsize = dp->d_sectorsize;
121110119Sphk		dp->d_flags |= DISKFLAG_OPEN;
122110727Sphk		if (dp->d_maxsize == 0) {
123110727Sphk			printf("WARNING: Disk drive %s%d has no d_maxsize\n",
124110727Sphk			    dp->d_name, dp->d_unit);
125110727Sphk			dp->d_maxsize = DFLTPHYS;
126110727Sphk		}
12792108Sphk	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
128111668Sphk		if (dp->d_close != NULL) {
129110119Sphk			g_disk_lock_giant(dp);
130111668Sphk			error = dp->d_close(dp);
131110119Sphk			if (error != 0)
132110119Sphk				printf("Closed disk %s -> %d\n",
133110119Sphk				    pp->name, error);
134110119Sphk			g_disk_unlock_giant(dp);
135110119Sphk		}
136110119Sphk		dp->d_flags &= ~DISKFLAG_OPEN;
13792108Sphk	}
13892108Sphk	return (error);
13992108Sphk}
14092108Sphk
14192108Sphkstatic void
14295038Sphkg_disk_kerneldump(struct bio *bp, struct disk *dp)
14395038Sphk{
14495038Sphk	int error;
14595038Sphk	struct g_kerneldump *gkd;
14695038Sphk	struct dumperinfo di;
147104450Sphk	struct g_geom *gp;
14895038Sphk
14995038Sphk	gkd = (struct g_kerneldump*)bp->bio_data;
150104450Sphk	gp = bp->bio_to->geom;
151104450Sphk	g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
152104450Sphk		gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
153110119Sphk	di.dumper = dp->d_dump;
154111220Sphk	di.priv = dp;
155103714Sphk	di.blocksize = dp->d_sectorsize;
15695038Sphk	di.mediaoffset = gkd->offset;
15795038Sphk	di.mediasize = gkd->length;
15895038Sphk	error = set_dumper(&di);
159104195Sphk	g_io_deliver(bp, error);
16095038Sphk}
16195038Sphk
16295038Sphkstatic void
16392108Sphkg_disk_done(struct bio *bp)
16492108Sphk{
165111979Sphk	struct bio *bp2;
166111979Sphk	struct disk *dp;
16792108Sphk
168110720Sphk	/* See "notes" for why we need a mutex here */
169110720Sphk	/* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
170110720Sphk	mtx_lock(&g_disk_done_mtx);
17192108Sphk	bp->bio_completed = bp->bio_length - bp->bio_resid;
172111979Sphk
173111979Sphk	bp2 = bp->bio_parent;
174111979Sphk	dp = bp2->bio_to->geom->softc;
175111979Sphk	if (bp2->bio_error == 0)
176111979Sphk		bp2->bio_error = bp->bio_error;
177111979Sphk	bp2->bio_completed += bp->bio_completed;
178111979Sphk	g_destroy_bio(bp);
179111979Sphk	bp2->bio_inbed++;
180111979Sphk	if (bp2->bio_children == bp2->bio_inbed) {
181112259Sphk		bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
182112259Sphk		devstat_end_transaction_bio(dp->d_devstat, bp2);
183111979Sphk		g_io_deliver(bp2, bp2->bio_error);
184111979Sphk	}
185110720Sphk	mtx_unlock(&g_disk_done_mtx);
18692108Sphk}
18792108Sphk
18892108Sphkstatic void
18992108Sphkg_disk_start(struct bio *bp)
19092108Sphk{
191110720Sphk	struct bio *bp2, *bp3;
19292108Sphk	struct disk *dp;
19392403Sphk	struct g_ioctl *gio;
19492403Sphk	int error;
195110720Sphk	off_t off;
19692108Sphk
19792108Sphk	dp = bp->bio_to->geom->softc;
198115214Sphk	if (dp == NULL)
199115214Sphk		g_io_deliver(bp, ENXIO);
200104609Sphk	error = EJUSTRETURN;
20192108Sphk	switch(bp->bio_cmd) {
202104609Sphk	case BIO_DELETE:
203110119Sphk		if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
204104609Sphk			error = 0;
205104609Sphk			break;
206104609Sphk		}
207104609Sphk		/* fall-through */
20892108Sphk	case BIO_READ:
20992108Sphk	case BIO_WRITE:
210110720Sphk		off = 0;
211110720Sphk		bp3 = NULL;
21292108Sphk		bp2 = g_clone_bio(bp);
213110477Sphk		if (bp2 == NULL) {
214110477Sphk			error = ENOMEM;
215110477Sphk			break;
216110477Sphk		}
217112259Sphk		devstat_start_transaction_bio(dp->d_devstat, bp);
218110720Sphk		do {
219110720Sphk			bp2->bio_offset += off;
220110720Sphk			bp2->bio_length -= off;
221110766Stegge			bp2->bio_data += off;
222110720Sphk			if (bp2->bio_length > dp->d_maxsize) {
223110720Sphk				/*
224110720Sphk				 * XXX: If we have a stripesize we should really
225110720Sphk				 * use it here.
226110720Sphk				 */
227110720Sphk				bp2->bio_length = dp->d_maxsize;
228110720Sphk				off += dp->d_maxsize;
229110720Sphk				/*
230110720Sphk				 * To avoid a race, we need to grab the next bio
231110720Sphk				 * before we schedule this one.  See "notes".
232110720Sphk				 */
233110720Sphk				bp3 = g_clone_bio(bp);
234110720Sphk				if (bp3 == NULL)
235110720Sphk					bp->bio_error = ENOMEM;
236110720Sphk			}
237110720Sphk			bp2->bio_done = g_disk_done;
238110720Sphk			bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
239110720Sphk			bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
240110720Sphk			bp2->bio_bcount = bp2->bio_length;
241110720Sphk			bp2->bio_disk = dp;
242110720Sphk			g_disk_lock_giant(dp);
243110720Sphk			dp->d_strategy(bp2);
244110720Sphk			g_disk_unlock_giant(dp);
245110720Sphk			bp2 = bp3;
246110720Sphk			bp3 = NULL;
247110720Sphk		} while (bp2 != NULL);
24892108Sphk		break;
24992108Sphk	case BIO_GETATTR:
250105551Sphk		if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
25192403Sphk			break;
252103714Sphk		else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
25392403Sphk			break;
25498066Sphk		else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
25594287Sphk			break;
25695038Sphk		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
25795038Sphk			g_disk_kerneldump(bp, dp);
258112476Sphk		else if ((g_debugflags & G_F_DISKIOCTL) &&
259112476Sphk		    (dp->d_ioctl != NULL) &&
260110119Sphk		    !strcmp(bp->bio_attribute, "GEOM::ioctl") &&
26192403Sphk		    bp->bio_length == sizeof *gio) {
262104602Sphk			gio = (struct g_ioctl *)bp->bio_data;
263111668Sphk			gio->dev =  dp;
264111668Sphk			gio->func = (d_ioctl_t *)(dp->d_ioctl);
265104602Sphk			error = EDIRIOCTL;
26692403Sphk		} else
26792403Sphk			error = ENOIOCTL;
26892403Sphk		break;
26992108Sphk	default:
27092403Sphk		error = EOPNOTSUPP;
27192403Sphk		break;
27292403Sphk	}
273104609Sphk	if (error != EJUSTRETURN)
274104195Sphk		g_io_deliver(bp, error);
27592403Sphk	return;
27692108Sphk}
27792108Sphk
278104936Sphkstatic void
279107953Sphkg_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
280105537Sphk{
281105537Sphk	struct disk *dp;
282105537Sphk
283105537Sphk	dp = gp->softc;
284106101Sphk	if (indent == NULL) {
285106101Sphk		sbuf_printf(sb, " hd %u", dp->d_fwheads);
286106101Sphk		sbuf_printf(sb, " sc %u", dp->d_fwsectors);
287106101Sphk		return;
288106101Sphk	}
289105539Sphk	if (pp != NULL) {
290105537Sphk		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
291105537Sphk		    indent, dp->d_fwheads);
292105537Sphk		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
293105537Sphk		    indent, dp->d_fwsectors);
294105537Sphk	}
295105537Sphk}
296105537Sphk
297105537Sphkstatic void
298112988Sphkg_disk_create(void *arg, int flag __unused)
29992108Sphk{
30092108Sphk	struct g_geom *gp;
30192108Sphk	struct g_provider *pp;
302110708Sphk	struct disk *dp;
30392108Sphk
304104936Sphk	g_topology_assert();
305110708Sphk	dp = arg;
306110708Sphk	gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
307104936Sphk	gp->start = g_disk_start;
308104936Sphk	gp->access = g_disk_access;
309110708Sphk	gp->softc = dp;
310105537Sphk	gp->dumpconf = g_disk_dumpconf;
311104936Sphk	pp = g_new_providerf(gp, "%s", gp->name);
312110708Sphk	pp->mediasize = dp->d_mediasize;
313110708Sphk	pp->sectorsize = dp->d_sectorsize;
314110708Sphk	if (dp->d_flags & DISKFLAG_CANDELETE)
315110708Sphk		pp->flags |= G_PF_CANDELETE;
316110710Sphk	pp->stripeoffset = dp->d_stripeoffset;
317110710Sphk	pp->stripesize = dp->d_stripesize;
318105957Sphk	if (bootverbose)
319105957Sphk		printf("GEOM: new disk %s\n", gp->name);
320112989Sphk	dp->d_geom = gp;
321112989Sphk	g_error_provider(pp, 0);
322104936Sphk}
323104936Sphk
324104936Sphk
325104936Sphk
326111668Sphkvoid
327111668Sphkdisk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused)
328104936Sphk{
329104936Sphk
330110708Sphk	dp->d_unit = unit;
331110119Sphk	dp->d_flags = flags;
332110119Sphk	KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
333110119Sphk	KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
334110119Sphk	KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
335110317Sphk	KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
336112002Sphk	dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
337111979Sphk	    dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
338111979Sphk	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
339112989Sphk	dp->d_geom = NULL;
340113937Sphk	g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
34192108Sphk}
34292108Sphk
343112989Sphk/*
344112989Sphk * XXX: There is a race if disk_destroy() is called while the g_disk_create()
345112989Sphk * XXX: event is running.  I belive the current result is that disk_destroy()
346112989Sphk * XXX: actually doesn't do anything.  Considering that the driver owns the
347112989Sphk * XXX: struct disk and is likely to free it in a few moments, this can
348112989Sphk * XXX: hardly be said to be optimal.  To what extent we can sleep in
349112989Sphk * XXX: disk_create() and disk_destroy() is currently undefined (but generally
350112989Sphk * XXX: undesirable) so any solution seems to involve an intrusive decision.
351112989Sphk */
352114958Sphk
353114958Sphkstatic void
354114958Sphkdisk_destroy_event(void *ptr, int flag)
355114958Sphk{
356114958Sphk
357114958Sphk	g_topology_assert();
358114958Sphk	g_wither_geom(ptr, ENXIO);
359114958Sphk}
360114958Sphk
36192108Sphkvoid
362111216Sphkdisk_destroy(struct disk *dp)
36392108Sphk{
36492108Sphk	struct g_geom *gp;
36592108Sphk
366112989Sphk	g_cancel_event(dp);
367110419Sphk	gp = dp->d_geom;
368112989Sphk	if (gp == NULL)
369112989Sphk		return;
370110119Sphk	gp->softc = NULL;
371111979Sphk	devstat_remove_entry(dp->d_devstat);
372114958Sphk	g_post_event(disk_destroy_event, gp, M_WAITOK, NULL, NULL);
37392108Sphk}
37492108Sphk
375104451Sphkstatic void
376112988Sphkg_kern_disks(void *p, int flag __unused)
377104451Sphk{
378104451Sphk	struct sbuf *sb;
379104451Sphk	struct g_geom *gp;
380104451Sphk	char *sp;
381104451Sphk
382104451Sphk	sb = p;
383104451Sphk	sp = "";
384104451Sphk	g_topology_assert();
385104451Sphk	LIST_FOREACH(gp, &g_disk_class.geom, geom) {
386104451Sphk		sbuf_printf(sb, "%s%s", sp, gp->name);
387104451Sphk		sp = " ";
388104451Sphk	}
389104451Sphk	sbuf_finish(sb);
390104451Sphk}
391104451Sphk
392104451Sphkstatic int
393104451Sphksysctl_disks(SYSCTL_HANDLER_ARGS)
394104451Sphk{
395104451Sphk	int error;
396104451Sphk	struct sbuf *sb;
397104451Sphk
398104451Sphk	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
399104451Sphk	sbuf_clear(sb);
400113940Sphk	g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
401113937Sphk	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
402104451Sphk	sbuf_delete(sb);
403104451Sphk	return error;
404104451Sphk}
405104451Sphk
406104451SphkSYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
407104451Sphk    sysctl_disks, "A", "names of available disks");
408104519Sphk
409