geom_disk.c revision 131207
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
36116196Sobrien#include <sys/cdefs.h>
37116196Sobrien__FBSDID("$FreeBSD: head/sys/geom/geom_disk.c 131207 2004-06-27 20:53:20Z phk $");
38116196Sobrien
39104519Sphk#include "opt_geom.h"
40104519Sphk
4192108Sphk#include <sys/param.h>
4292108Sphk#include <sys/systm.h>
4392108Sphk#include <sys/kernel.h>
4492108Sphk#include <sys/sysctl.h>
4592108Sphk#include <sys/bio.h>
4692108Sphk#include <sys/conf.h>
47110119Sphk#include <sys/fcntl.h>
4892108Sphk#include <sys/malloc.h>
4992108Sphk#include <sys/sysctl.h>
50111979Sphk#include <sys/devicestat.h>
5192108Sphk#include <machine/md_var.h>
5292108Sphk
5392108Sphk#include <sys/lock.h>
5492108Sphk#include <sys/mutex.h>
5592108Sphk#include <geom/geom.h>
56112952Sphk#include <geom/geom_disk.h>
57112476Sphk#include <geom/geom_int.h>
5892108Sphk
59110720Sphkstatic struct mtx g_disk_done_mtx;
60110720Sphk
6192108Sphkstatic g_access_t g_disk_access;
62115473Sphkstatic g_init_t g_disk_init;
63115473Sphkstatic g_fini_t g_disk_fini;
6492108Sphk
6593248Sphkstruct g_class g_disk_class = {
66112552Sphk	.name = "DISK",
67115473Sphk	.init = g_disk_init,
68115473Sphk	.fini = g_disk_fini,
6992108Sphk};
7092108Sphk
71110720Sphkstatic void
72115473Sphkg_disk_init(struct g_class *mp __unused)
73110720Sphk{
74115473Sphk
75123271Struckman	mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
76110720Sphk}
77104936Sphk
78115473Sphkstatic void
79115473Sphkg_disk_fini(struct g_class *mp __unused)
80115473Sphk{
81110720Sphk
82115473Sphk	mtx_destroy(&g_disk_done_mtx);
83115473Sphk}
84115473Sphk
85115473SphkDECLARE_GEOM_CLASS(g_disk_class, g_disk);
86115473Sphk
87110052Sphkstatic void __inline
88110052Sphkg_disk_lock_giant(struct disk *dp)
89110052Sphk{
90125975Sphk	if (dp->d_flags & DISKFLAG_NEEDSGIANT)
91125975Sphk		mtx_lock(&Giant);
92110052Sphk}
93110052Sphk
94110052Sphkstatic void __inline
95110052Sphkg_disk_unlock_giant(struct disk *dp)
96110052Sphk{
97125975Sphk	if (dp->d_flags & DISKFLAG_NEEDSGIANT)
98125975Sphk		mtx_unlock(&Giant);
99110052Sphk}
100110052Sphk
10192108Sphkstatic int
10292108Sphkg_disk_access(struct g_provider *pp, int r, int w, int e)
10392108Sphk{
10492108Sphk	struct disk *dp;
10592108Sphk	int error;
10692108Sphk
10792108Sphk	g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
10892108Sphk	    pp->name, r, w, e);
10992108Sphk	g_topology_assert();
110125539Spjd	dp = pp->geom->softc;
111125975Sphk	if (dp == NULL || dp->d_destroyed) {
112125539Spjd		/*
113125539Spjd		 * Allow decreasing access count even if disk is not
114125539Spjd		 * avaliable anymore.
115125539Spjd		 */
116125539Spjd		if (r <= 0 && w <= 0 && e <= 0)
117125539Spjd			return (0);
118125975Sphk		return (ENXIO);
119125539Spjd	}
12092108Sphk	r += pp->acr;
12192108Sphk	w += pp->acw;
12292108Sphk	e += pp->ace;
123110119Sphk	error = 0;
12492108Sphk	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
125111668Sphk		if (dp->d_open != NULL) {
126110119Sphk			g_disk_lock_giant(dp);
127111668Sphk			error = dp->d_open(dp);
128110119Sphk			if (error != 0)
129110119Sphk				printf("Opened disk %s -> %d\n",
130110119Sphk				    pp->name, error);
131110119Sphk			g_disk_unlock_giant(dp);
132110119Sphk		}
133105551Sphk		pp->mediasize = dp->d_mediasize;
134105551Sphk		pp->sectorsize = dp->d_sectorsize;
135110119Sphk		dp->d_flags |= DISKFLAG_OPEN;
136110727Sphk		if (dp->d_maxsize == 0) {
137110727Sphk			printf("WARNING: Disk drive %s%d has no d_maxsize\n",
138110727Sphk			    dp->d_name, dp->d_unit);
139110727Sphk			dp->d_maxsize = DFLTPHYS;
140110727Sphk		}
14192108Sphk	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
142111668Sphk		if (dp->d_close != NULL) {
143110119Sphk			g_disk_lock_giant(dp);
144111668Sphk			error = dp->d_close(dp);
145110119Sphk			if (error != 0)
146110119Sphk				printf("Closed disk %s -> %d\n",
147110119Sphk				    pp->name, error);
148110119Sphk			g_disk_unlock_giant(dp);
149110119Sphk		}
150110119Sphk		dp->d_flags &= ~DISKFLAG_OPEN;
15192108Sphk	}
15292108Sphk	return (error);
15392108Sphk}
15492108Sphk
15592108Sphkstatic void
15695038Sphkg_disk_kerneldump(struct bio *bp, struct disk *dp)
15795038Sphk{
15895038Sphk	int error;
15995038Sphk	struct g_kerneldump *gkd;
16095038Sphk	struct dumperinfo di;
161104450Sphk	struct g_geom *gp;
16295038Sphk
16395038Sphk	gkd = (struct g_kerneldump*)bp->bio_data;
164104450Sphk	gp = bp->bio_to->geom;
165104450Sphk	g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
166104450Sphk		gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
167120572Sphk	if (dp->d_dump == NULL) {
168120572Sphk		g_io_deliver(bp, ENODEV);
169120572Sphk		return;
170120572Sphk	}
171110119Sphk	di.dumper = dp->d_dump;
172111220Sphk	di.priv = dp;
173103714Sphk	di.blocksize = dp->d_sectorsize;
17495038Sphk	di.mediaoffset = gkd->offset;
17595038Sphk	di.mediasize = gkd->length;
17695038Sphk	error = set_dumper(&di);
177104195Sphk	g_io_deliver(bp, error);
17895038Sphk}
17995038Sphk
18095038Sphkstatic void
18192108Sphkg_disk_done(struct bio *bp)
18292108Sphk{
183111979Sphk	struct bio *bp2;
184111979Sphk	struct disk *dp;
18592108Sphk
186110720Sphk	/* See "notes" for why we need a mutex here */
187110720Sphk	/* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
188110720Sphk	mtx_lock(&g_disk_done_mtx);
18992108Sphk	bp->bio_completed = bp->bio_length - bp->bio_resid;
190111979Sphk
191111979Sphk	bp2 = bp->bio_parent;
192111979Sphk	if (bp2->bio_error == 0)
193111979Sphk		bp2->bio_error = bp->bio_error;
194111979Sphk	bp2->bio_completed += bp->bio_completed;
195111979Sphk	g_destroy_bio(bp);
196111979Sphk	bp2->bio_inbed++;
197111979Sphk	if (bp2->bio_children == bp2->bio_inbed) {
198112259Sphk		bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
199129116Ssos		if ((dp = bp2->bio_to->geom->softc))
200129116Ssos			devstat_end_transaction_bio(dp->d_devstat, bp2);
201111979Sphk		g_io_deliver(bp2, bp2->bio_error);
202111979Sphk	}
203110720Sphk	mtx_unlock(&g_disk_done_mtx);
20492108Sphk}
20592108Sphk
206119660Sphkstatic int
207119660Sphkg_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, struct thread *td)
208119660Sphk{
209119660Sphk	struct g_geom *gp;
210119660Sphk	struct disk *dp;
211119660Sphk	int error;
212119660Sphk
213119660Sphk	gp = pp->geom;
214119660Sphk	dp = gp->softc;
215119660Sphk
216119660Sphk	if (dp->d_ioctl == NULL)
217119660Sphk		return (ENOIOCTL);
218119660Sphk	g_disk_lock_giant(dp);
219119660Sphk	error = dp->d_ioctl(dp, cmd, data, 0, td);
220119660Sphk	g_disk_unlock_giant(dp);
221119660Sphk	return(error);
222119660Sphk}
223119660Sphk
22492108Sphkstatic void
22592108Sphkg_disk_start(struct bio *bp)
22692108Sphk{
227110720Sphk	struct bio *bp2, *bp3;
22892108Sphk	struct disk *dp;
22992403Sphk	int error;
230110720Sphk	off_t off;
23192108Sphk
23292108Sphk	dp = bp->bio_to->geom->softc;
233125975Sphk	if (dp == NULL || dp->d_destroyed)
234115214Sphk		g_io_deliver(bp, ENXIO);
235104609Sphk	error = EJUSTRETURN;
23692108Sphk	switch(bp->bio_cmd) {
237104609Sphk	case BIO_DELETE:
238110119Sphk		if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
239104609Sphk			error = 0;
240104609Sphk			break;
241104609Sphk		}
242104609Sphk		/* fall-through */
24392108Sphk	case BIO_READ:
24492108Sphk	case BIO_WRITE:
245110720Sphk		off = 0;
246110720Sphk		bp3 = NULL;
24792108Sphk		bp2 = g_clone_bio(bp);
248110477Sphk		if (bp2 == NULL) {
249110477Sphk			error = ENOMEM;
250110477Sphk			break;
251110477Sphk		}
252112259Sphk		devstat_start_transaction_bio(dp->d_devstat, bp);
253110720Sphk		do {
254110720Sphk			bp2->bio_offset += off;
255110720Sphk			bp2->bio_length -= off;
256110766Stegge			bp2->bio_data += off;
257110720Sphk			if (bp2->bio_length > dp->d_maxsize) {
258110720Sphk				/*
259110720Sphk				 * XXX: If we have a stripesize we should really
260110720Sphk				 * use it here.
261110720Sphk				 */
262110720Sphk				bp2->bio_length = dp->d_maxsize;
263110720Sphk				off += dp->d_maxsize;
264110720Sphk				/*
265110720Sphk				 * To avoid a race, we need to grab the next bio
266110720Sphk				 * before we schedule this one.  See "notes".
267110720Sphk				 */
268110720Sphk				bp3 = g_clone_bio(bp);
269110720Sphk				if (bp3 == NULL)
270110720Sphk					bp->bio_error = ENOMEM;
271110720Sphk			}
272110720Sphk			bp2->bio_done = g_disk_done;
273110720Sphk			bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
274110720Sphk			bp2->bio_bcount = bp2->bio_length;
275110720Sphk			bp2->bio_disk = dp;
276110720Sphk			g_disk_lock_giant(dp);
277110720Sphk			dp->d_strategy(bp2);
278110720Sphk			g_disk_unlock_giant(dp);
279110720Sphk			bp2 = bp3;
280110720Sphk			bp3 = NULL;
281110720Sphk		} while (bp2 != NULL);
28292108Sphk		break;
28392108Sphk	case BIO_GETATTR:
284105551Sphk		if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
28592403Sphk			break;
286103714Sphk		else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
28792403Sphk			break;
28898066Sphk		else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
28994287Sphk			break;
29095038Sphk		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
29195038Sphk			g_disk_kerneldump(bp, dp);
292119660Sphk		else
29392403Sphk			error = ENOIOCTL;
29492403Sphk		break;
29592108Sphk	default:
29692403Sphk		error = EOPNOTSUPP;
29792403Sphk		break;
29892403Sphk	}
299104609Sphk	if (error != EJUSTRETURN)
300104195Sphk		g_io_deliver(bp, error);
30192403Sphk	return;
30292108Sphk}
30392108Sphk
304104936Sphkstatic void
305107953Sphkg_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
306105537Sphk{
307105537Sphk	struct disk *dp;
308105537Sphk
309105537Sphk	dp = gp->softc;
310120374Sphk	if (dp == NULL)
311120374Sphk		return;
312106101Sphk	if (indent == NULL) {
313106101Sphk		sbuf_printf(sb, " hd %u", dp->d_fwheads);
314106101Sphk		sbuf_printf(sb, " sc %u", dp->d_fwsectors);
315106101Sphk		return;
316106101Sphk	}
317105539Sphk	if (pp != NULL) {
318105537Sphk		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
319105537Sphk		    indent, dp->d_fwheads);
320105537Sphk		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
321105537Sphk		    indent, dp->d_fwsectors);
322105537Sphk	}
323105537Sphk}
324105537Sphk
325105537Sphkstatic void
326115309Sphkg_disk_create(void *arg, int flag)
32792108Sphk{
32892108Sphk	struct g_geom *gp;
32992108Sphk	struct g_provider *pp;
330110708Sphk	struct disk *dp;
33192108Sphk
332115309Sphk	if (flag == EV_CANCEL)
333115309Sphk		return;
334104936Sphk	g_topology_assert();
335110708Sphk	dp = arg;
336110708Sphk	gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
337104936Sphk	gp->start = g_disk_start;
338104936Sphk	gp->access = g_disk_access;
339119660Sphk	gp->ioctl = g_disk_ioctl;
340110708Sphk	gp->softc = dp;
341105537Sphk	gp->dumpconf = g_disk_dumpconf;
342104936Sphk	pp = g_new_providerf(gp, "%s", gp->name);
343110708Sphk	pp->mediasize = dp->d_mediasize;
344110708Sphk	pp->sectorsize = dp->d_sectorsize;
345110708Sphk	if (dp->d_flags & DISKFLAG_CANDELETE)
346110708Sphk		pp->flags |= G_PF_CANDELETE;
347110710Sphk	pp->stripeoffset = dp->d_stripeoffset;
348110710Sphk	pp->stripesize = dp->d_stripesize;
349105957Sphk	if (bootverbose)
350105957Sphk		printf("GEOM: new disk %s\n", gp->name);
351112989Sphk	dp->d_geom = gp;
352112989Sphk	g_error_provider(pp, 0);
353104936Sphk}
354104936Sphk
355119652Sphkstatic void
356119652Sphkg_disk_destroy(void *ptr, int flag)
357119652Sphk{
358125975Sphk	struct disk *dp;
359119652Sphk	struct g_geom *gp;
360104936Sphk
361119652Sphk	g_topology_assert();
362125975Sphk	dp = ptr;
363125975Sphk	gp = dp->d_geom;
364119652Sphk	gp->softc = NULL;
365119652Sphk	g_wither_geom(gp, ENXIO);
366125975Sphk	g_free(dp);
367119652Sphk}
368104936Sphk
369125975Sphkstruct disk *
370125975Sphkdisk_alloc()
371125975Sphk{
372125975Sphk	struct disk *dp;
373125975Sphk
374125975Sphk	dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO);
375125975Sphk	return (dp);
376125975Sphk}
377125975Sphk
378111668Sphkvoid
379125975Sphkdisk_create(struct disk *dp, int version)
380104936Sphk{
381125975Sphk	if (version != DISK_VERSION_00) {
382125975Sphk		printf("WARNING: Attempt to add disk %s%d %s",
383125975Sphk		    dp->d_name, dp->d_unit,
384125975Sphk		    " using incompatible ABI version of disk(9)\n");
385125975Sphk		printf("WARNING: Ignoring disk %s%d\n",
386125975Sphk		    dp->d_name, dp->d_unit);
387125975Sphk		return;
388125975Sphk	}
389110119Sphk	KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
390110119Sphk	KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
391110119Sphk	KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
392110317Sphk	KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
393120374Sphk	if (dp->d_devstat == NULL)
394120374Sphk		dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
395120374Sphk		    dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
396120374Sphk		    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
397112989Sphk	dp->d_geom = NULL;
398113937Sphk	g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
39992108Sphk}
40092108Sphk
40192108Sphkvoid
402111216Sphkdisk_destroy(struct disk *dp)
40392108Sphk{
40492108Sphk
405112989Sphk	g_cancel_event(dp);
406125975Sphk	dp->d_destroyed = 1;
407131207Sphk	if (dp->d_devstat != NULL)
408131207Sphk		devstat_remove_entry(dp->d_devstat);
409131207Sphk	g_post_event(g_disk_destroy, dp, M_WAITOK, NULL, NULL);
41092108Sphk}
41192108Sphk
412104451Sphkstatic void
413112988Sphkg_kern_disks(void *p, int flag __unused)
414104451Sphk{
415104451Sphk	struct sbuf *sb;
416104451Sphk	struct g_geom *gp;
417104451Sphk	char *sp;
418104451Sphk
419104451Sphk	sb = p;
420104451Sphk	sp = "";
421104451Sphk	g_topology_assert();
422104451Sphk	LIST_FOREACH(gp, &g_disk_class.geom, geom) {
423104451Sphk		sbuf_printf(sb, "%s%s", sp, gp->name);
424104451Sphk		sp = " ";
425104451Sphk	}
426104451Sphk	sbuf_finish(sb);
427104451Sphk}
428104451Sphk
429104451Sphkstatic int
430104451Sphksysctl_disks(SYSCTL_HANDLER_ARGS)
431104451Sphk{
432104451Sphk	int error;
433104451Sphk	struct sbuf *sb;
434104451Sphk
435104451Sphk	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
436113940Sphk	g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
437113937Sphk	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
438104451Sphk	sbuf_delete(sb);
439104451Sphk	return error;
440104451Sphk}
441104451Sphk
442104451SphkSYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
443104451Sphk    sysctl_disks, "A", "names of available disks");
444104519Sphk
445