geom_disk.c revision 114958
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 114958 2003-05-12 20:15:28Z 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;
107110119Sphk	error = 0;
10892108Sphk	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
109111668Sphk		if (dp->d_open != NULL) {
110110119Sphk			g_disk_lock_giant(dp);
111111668Sphk			error = dp->d_open(dp);
112110119Sphk			if (error != 0)
113110119Sphk				printf("Opened disk %s -> %d\n",
114110119Sphk				    pp->name, error);
115110119Sphk			g_disk_unlock_giant(dp);
116110119Sphk		}
117105551Sphk		pp->mediasize = dp->d_mediasize;
118105551Sphk		pp->sectorsize = dp->d_sectorsize;
119110119Sphk		dp->d_flags |= DISKFLAG_OPEN;
120110727Sphk		if (dp->d_maxsize == 0) {
121110727Sphk			printf("WARNING: Disk drive %s%d has no d_maxsize\n",
122110727Sphk			    dp->d_name, dp->d_unit);
123110727Sphk			dp->d_maxsize = DFLTPHYS;
124110727Sphk		}
12592108Sphk	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
126111668Sphk		if (dp->d_close != NULL) {
127110119Sphk			g_disk_lock_giant(dp);
128111668Sphk			error = dp->d_close(dp);
129110119Sphk			if (error != 0)
130110119Sphk				printf("Closed disk %s -> %d\n",
131110119Sphk				    pp->name, error);
132110119Sphk			g_disk_unlock_giant(dp);
133110119Sphk		}
134110119Sphk		dp->d_flags &= ~DISKFLAG_OPEN;
13592108Sphk	}
13692108Sphk	return (error);
13792108Sphk}
13892108Sphk
13992108Sphkstatic void
14095038Sphkg_disk_kerneldump(struct bio *bp, struct disk *dp)
14195038Sphk{
14295038Sphk	int error;
14395038Sphk	struct g_kerneldump *gkd;
14495038Sphk	struct dumperinfo di;
145104450Sphk	struct g_geom *gp;
14695038Sphk
14795038Sphk	gkd = (struct g_kerneldump*)bp->bio_data;
148104450Sphk	gp = bp->bio_to->geom;
149104450Sphk	g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
150104450Sphk		gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
151110119Sphk	di.dumper = dp->d_dump;
152111220Sphk	di.priv = dp;
153103714Sphk	di.blocksize = dp->d_sectorsize;
15495038Sphk	di.mediaoffset = gkd->offset;
15595038Sphk	di.mediasize = gkd->length;
15695038Sphk	error = set_dumper(&di);
157104195Sphk	g_io_deliver(bp, error);
15895038Sphk}
15995038Sphk
16095038Sphkstatic void
16192108Sphkg_disk_done(struct bio *bp)
16292108Sphk{
163111979Sphk	struct bio *bp2;
164111979Sphk	struct disk *dp;
16592108Sphk
166110720Sphk	/* See "notes" for why we need a mutex here */
167110720Sphk	/* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
168110720Sphk	mtx_lock(&g_disk_done_mtx);
16992108Sphk	bp->bio_completed = bp->bio_length - bp->bio_resid;
170111979Sphk
171111979Sphk	bp2 = bp->bio_parent;
172111979Sphk	dp = bp2->bio_to->geom->softc;
173111979Sphk	if (bp2->bio_error == 0)
174111979Sphk		bp2->bio_error = bp->bio_error;
175111979Sphk	bp2->bio_completed += bp->bio_completed;
176111979Sphk	g_destroy_bio(bp);
177111979Sphk	bp2->bio_inbed++;
178111979Sphk	if (bp2->bio_children == bp2->bio_inbed) {
179112259Sphk		bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
180112259Sphk		devstat_end_transaction_bio(dp->d_devstat, bp2);
181111979Sphk		g_io_deliver(bp2, bp2->bio_error);
182111979Sphk	}
183110720Sphk	mtx_unlock(&g_disk_done_mtx);
18492108Sphk}
18592108Sphk
18692108Sphkstatic void
18792108Sphkg_disk_start(struct bio *bp)
18892108Sphk{
189110720Sphk	struct bio *bp2, *bp3;
19092108Sphk	struct disk *dp;
19192403Sphk	struct g_ioctl *gio;
19292403Sphk	int error;
193110720Sphk	off_t off;
19492108Sphk
19592108Sphk	dp = bp->bio_to->geom->softc;
196104609Sphk	error = EJUSTRETURN;
19792108Sphk	switch(bp->bio_cmd) {
198104609Sphk	case BIO_DELETE:
199110119Sphk		if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
200104609Sphk			error = 0;
201104609Sphk			break;
202104609Sphk		}
203104609Sphk		/* fall-through */
20492108Sphk	case BIO_READ:
20592108Sphk	case BIO_WRITE:
206110720Sphk		off = 0;
207110720Sphk		bp3 = NULL;
20892108Sphk		bp2 = g_clone_bio(bp);
209110477Sphk		if (bp2 == NULL) {
210110477Sphk			error = ENOMEM;
211110477Sphk			break;
212110477Sphk		}
213112259Sphk		devstat_start_transaction_bio(dp->d_devstat, bp);
214110720Sphk		do {
215110720Sphk			bp2->bio_offset += off;
216110720Sphk			bp2->bio_length -= off;
217110766Stegge			bp2->bio_data += off;
218110720Sphk			if (bp2->bio_length > dp->d_maxsize) {
219110720Sphk				/*
220110720Sphk				 * XXX: If we have a stripesize we should really
221110720Sphk				 * use it here.
222110720Sphk				 */
223110720Sphk				bp2->bio_length = dp->d_maxsize;
224110720Sphk				off += dp->d_maxsize;
225110720Sphk				/*
226110720Sphk				 * To avoid a race, we need to grab the next bio
227110720Sphk				 * before we schedule this one.  See "notes".
228110720Sphk				 */
229110720Sphk				bp3 = g_clone_bio(bp);
230110720Sphk				if (bp3 == NULL)
231110720Sphk					bp->bio_error = ENOMEM;
232110720Sphk			}
233110720Sphk			bp2->bio_done = g_disk_done;
234110720Sphk			bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
235110720Sphk			bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
236110720Sphk			bp2->bio_bcount = bp2->bio_length;
237110720Sphk			bp2->bio_disk = dp;
238110720Sphk			g_disk_lock_giant(dp);
239110720Sphk			dp->d_strategy(bp2);
240110720Sphk			g_disk_unlock_giant(dp);
241110720Sphk			bp2 = bp3;
242110720Sphk			bp3 = NULL;
243110720Sphk		} while (bp2 != NULL);
24492108Sphk		break;
24592108Sphk	case BIO_GETATTR:
246105551Sphk		if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
24792403Sphk			break;
248103714Sphk		else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
24992403Sphk			break;
25098066Sphk		else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
25194287Sphk			break;
25295038Sphk		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
25395038Sphk			g_disk_kerneldump(bp, dp);
254112476Sphk		else if ((g_debugflags & G_F_DISKIOCTL) &&
255112476Sphk		    (dp->d_ioctl != NULL) &&
256110119Sphk		    !strcmp(bp->bio_attribute, "GEOM::ioctl") &&
25792403Sphk		    bp->bio_length == sizeof *gio) {
258104602Sphk			gio = (struct g_ioctl *)bp->bio_data;
259111668Sphk			gio->dev =  dp;
260111668Sphk			gio->func = (d_ioctl_t *)(dp->d_ioctl);
261104602Sphk			error = EDIRIOCTL;
26292403Sphk		} else
26392403Sphk			error = ENOIOCTL;
26492403Sphk		break;
26592108Sphk	default:
26692403Sphk		error = EOPNOTSUPP;
26792403Sphk		break;
26892403Sphk	}
269104609Sphk	if (error != EJUSTRETURN)
270104195Sphk		g_io_deliver(bp, error);
27192403Sphk	return;
27292108Sphk}
27392108Sphk
274104936Sphkstatic void
275107953Sphkg_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
276105537Sphk{
277105537Sphk	struct disk *dp;
278105537Sphk
279105537Sphk	dp = gp->softc;
280106101Sphk	if (indent == NULL) {
281106101Sphk		sbuf_printf(sb, " hd %u", dp->d_fwheads);
282106101Sphk		sbuf_printf(sb, " sc %u", dp->d_fwsectors);
283106101Sphk		return;
284106101Sphk	}
285105539Sphk	if (pp != NULL) {
286105537Sphk		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
287105537Sphk		    indent, dp->d_fwheads);
288105537Sphk		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
289105537Sphk		    indent, dp->d_fwsectors);
290105537Sphk	}
291105537Sphk}
292105537Sphk
293105537Sphkstatic void
294112988Sphkg_disk_create(void *arg, int flag __unused)
29592108Sphk{
29692108Sphk	struct g_geom *gp;
29792108Sphk	struct g_provider *pp;
298110708Sphk	struct disk *dp;
29992108Sphk
300104936Sphk	g_topology_assert();
301110708Sphk	dp = arg;
302110708Sphk	gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
303104936Sphk	gp->start = g_disk_start;
304104936Sphk	gp->access = g_disk_access;
305110708Sphk	gp->softc = dp;
306105537Sphk	gp->dumpconf = g_disk_dumpconf;
307104936Sphk	pp = g_new_providerf(gp, "%s", gp->name);
308110708Sphk	pp->mediasize = dp->d_mediasize;
309110708Sphk	pp->sectorsize = dp->d_sectorsize;
310110708Sphk	if (dp->d_flags & DISKFLAG_CANDELETE)
311110708Sphk		pp->flags |= G_PF_CANDELETE;
312110710Sphk	pp->stripeoffset = dp->d_stripeoffset;
313110710Sphk	pp->stripesize = dp->d_stripesize;
314105957Sphk	if (bootverbose)
315105957Sphk		printf("GEOM: new disk %s\n", gp->name);
316112989Sphk	dp->d_geom = gp;
317112989Sphk	g_error_provider(pp, 0);
318104936Sphk}
319104936Sphk
320104936Sphk
321104936Sphk
322111668Sphkvoid
323111668Sphkdisk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused)
324104936Sphk{
325104936Sphk
326110708Sphk	dp->d_unit = unit;
327110119Sphk	dp->d_flags = flags;
328110119Sphk	KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
329110119Sphk	KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
330110119Sphk	KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
331110317Sphk	KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
332112002Sphk	dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
333111979Sphk	    dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
334111979Sphk	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
335112989Sphk	dp->d_geom = NULL;
336113937Sphk	g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
33792108Sphk}
33892108Sphk
339112989Sphk/*
340112989Sphk * XXX: There is a race if disk_destroy() is called while the g_disk_create()
341112989Sphk * XXX: event is running.  I belive the current result is that disk_destroy()
342112989Sphk * XXX: actually doesn't do anything.  Considering that the driver owns the
343112989Sphk * XXX: struct disk and is likely to free it in a few moments, this can
344112989Sphk * XXX: hardly be said to be optimal.  To what extent we can sleep in
345112989Sphk * XXX: disk_create() and disk_destroy() is currently undefined (but generally
346112989Sphk * XXX: undesirable) so any solution seems to involve an intrusive decision.
347112989Sphk */
348114958Sphk
349114958Sphkstatic void
350114958Sphkdisk_destroy_event(void *ptr, int flag)
351114958Sphk{
352114958Sphk
353114958Sphk	g_topology_assert();
354114958Sphk	g_wither_geom(ptr, ENXIO);
355114958Sphk}
356114958Sphk
35792108Sphkvoid
358111216Sphkdisk_destroy(struct disk *dp)
35992108Sphk{
36092108Sphk	struct g_geom *gp;
36192108Sphk
362112989Sphk	g_cancel_event(dp);
363110419Sphk	gp = dp->d_geom;
364112989Sphk	if (gp == NULL)
365112989Sphk		return;
366110119Sphk	gp->softc = NULL;
367111979Sphk	devstat_remove_entry(dp->d_devstat);
368114958Sphk	g_post_event(disk_destroy_event, gp, M_WAITOK, NULL, NULL);
36992108Sphk}
37092108Sphk
371104451Sphkstatic void
372112988Sphkg_kern_disks(void *p, int flag __unused)
373104451Sphk{
374104451Sphk	struct sbuf *sb;
375104451Sphk	struct g_geom *gp;
376104451Sphk	char *sp;
377104451Sphk
378104451Sphk	sb = p;
379104451Sphk	sp = "";
380104451Sphk	g_topology_assert();
381104451Sphk	LIST_FOREACH(gp, &g_disk_class.geom, geom) {
382104451Sphk		sbuf_printf(sb, "%s%s", sp, gp->name);
383104451Sphk		sp = " ";
384104451Sphk	}
385104451Sphk	sbuf_finish(sb);
386104451Sphk}
387104451Sphk
388104451Sphkstatic int
389104451Sphksysctl_disks(SYSCTL_HANDLER_ARGS)
390104451Sphk{
391104451Sphk	int error;
392104451Sphk	struct sbuf *sb;
393104451Sphk
394104451Sphk	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
395104451Sphk	sbuf_clear(sb);
396113940Sphk	g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
397113937Sphk	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
398104451Sphk	sbuf_delete(sb);
399104451Sphk	return error;
400104451Sphk}
401104451Sphk
402104451SphkSYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
403104451Sphk    sysctl_disks, "A", "names of available disks");
404104519Sphk
405