Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.17
      1 /*	$NetBSD: subr_disk.c,v 1.17 1996/03/16 23:17:08 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Jason R. Thorpe.  All rights reserved.
      5  * Copyright (c) 1982, 1986, 1988, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  * (c) UNIX System Laboratories, Inc.
      8  * All or some portions of this file are derived from material licensed
      9  * to the University of California by American Telephone and Telegraph
     10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11  * the permission of UNIX System Laboratories, Inc.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the University of
     24  *	California, Berkeley and its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/malloc.h>
     48 #include <sys/buf.h>
     49 #include <sys/syslog.h>
     50 #include <sys/time.h>
     51 #include <sys/disklabel.h>
     52 #include <sys/disk.h>
     53 #include <sys/dkstat.h>		/* XXX */
     54 
     55 /*
     56  * A global list of all disks attached to the system.  May grow or
     57  * shrink over time.
     58  */
     59 struct	disklist_head disklist;	/* TAILQ_HEAD */
     60 int	disk_count;		/* number of drives in global disklist */
     61 
     62 /*
     63  * Old-style disk instrumentation structures.  These will go away
     64  * someday.
     65  */
     66 long	dk_seek[DK_NDRIVE];
     67 long	dk_time[DK_NDRIVE];
     68 long	dk_wds[DK_NDRIVE];
     69 long	dk_wpms[DK_NDRIVE];
     70 long	dk_xfer[DK_NDRIVE];
     71 int	dk_busy;
     72 int	dk_ndrive;
     73 int	dkn;			/* number of slots filled so far */
     74 
     75 /*
     76  * Seek sort for disks.  We depend on the driver which calls us using b_resid
     77  * as the current cylinder number.
     78  *
     79  * The argument ap structure holds a b_actf activity chain pointer on which we
     80  * keep two queues, sorted in ascending cylinder order.  The first queue holds
     81  * those requests which are positioned after the current cylinder (in the first
     82  * request); the second holds requests which came in after their cylinder number
     83  * was passed.  Thus we implement a one way scan, retracting after reaching the
     84  * end of the drive to the first request on the second queue, at which time it
     85  * becomes the first queue.
     86  *
     87  * A one-way scan is natural because of the way UNIX read-ahead blocks are
     88  * allocated.
     89  */
     90 
     91 void
     92 disksort(ap, bp)
     93 	register struct buf *ap, *bp;
     94 {
     95 	register struct buf *bq;
     96 
     97 	/* If the queue is empty, then it's easy. */
     98 	if (ap->b_actf == NULL) {
     99 		bp->b_actf = NULL;
    100 		ap->b_actf = bp;
    101 		return;
    102 	}
    103 
    104 	/*
    105 	 * If we lie after the first (currently active) request, then we
    106 	 * must locate the second request list and add ourselves to it.
    107 	 */
    108 	bq = ap->b_actf;
    109 	if (bp->b_cylinder < bq->b_cylinder) {
    110 		while (bq->b_actf) {
    111 			/*
    112 			 * Check for an ``inversion'' in the normally ascending
    113 			 * cylinder numbers, indicating the start of the second
    114 			 * request list.
    115 			 */
    116 			if (bq->b_actf->b_cylinder < bq->b_cylinder) {
    117 				/*
    118 				 * Search the second request list for the first
    119 				 * request at a larger cylinder number.  We go
    120 				 * before that; if there is no such request, we
    121 				 * go at end.
    122 				 */
    123 				do {
    124 					if (bp->b_cylinder <
    125 					    bq->b_actf->b_cylinder)
    126 						goto insert;
    127 					if (bp->b_cylinder ==
    128 					    bq->b_actf->b_cylinder &&
    129 					    bp->b_blkno < bq->b_actf->b_blkno)
    130 						goto insert;
    131 					bq = bq->b_actf;
    132 				} while (bq->b_actf);
    133 				goto insert;		/* after last */
    134 			}
    135 			bq = bq->b_actf;
    136 		}
    137 		/*
    138 		 * No inversions... we will go after the last, and
    139 		 * be the first request in the second request list.
    140 		 */
    141 		goto insert;
    142 	}
    143 	/*
    144 	 * Request is at/after the current request...
    145 	 * sort in the first request list.
    146 	 */
    147 	while (bq->b_actf) {
    148 		/*
    149 		 * We want to go after the current request if there is an
    150 		 * inversion after it (i.e. it is the end of the first
    151 		 * request list), or if the next request is a larger cylinder
    152 		 * than our request.
    153 		 */
    154 		if (bq->b_actf->b_cylinder < bq->b_cylinder ||
    155 		    bp->b_cylinder < bq->b_actf->b_cylinder ||
    156 		    (bp->b_cylinder == bq->b_actf->b_cylinder &&
    157 		    bp->b_blkno < bq->b_actf->b_blkno))
    158 			goto insert;
    159 		bq = bq->b_actf;
    160 	}
    161 	/*
    162 	 * Neither a second list nor a larger request... we go at the end of
    163 	 * the first list, which is the same as the end of the whole schebang.
    164 	 */
    165 insert:	bp->b_actf = bq->b_actf;
    166 	bq->b_actf = bp;
    167 }
    168 
    169 /* encoding of disk minor numbers, should be elsewhere... */
    170 #define dkunit(dev)		(minor(dev) >> 3)
    171 #define dkpart(dev)		(minor(dev) & 07)
    172 #define dkminor(unit, part)	(((unit) << 3) | (part))
    173 
    174 /*
    175  * Compute checksum for disk label.
    176  */
    177 u_int
    178 dkcksum(lp)
    179 	register struct disklabel *lp;
    180 {
    181 	register u_short *start, *end;
    182 	register u_short sum = 0;
    183 
    184 	start = (u_short *)lp;
    185 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
    186 	while (start < end)
    187 		sum ^= *start++;
    188 	return (sum);
    189 }
    190 
    191 /*
    192  * Disk error is the preface to plaintive error messages
    193  * about failing disk transfers.  It prints messages of the form
    194 
    195 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
    196 
    197  * if the offset of the error in the transfer and a disk label
    198  * are both available.  blkdone should be -1 if the position of the error
    199  * is unknown; the disklabel pointer may be null from drivers that have not
    200  * been converted to use them.  The message is printed with printf
    201  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
    202  * The message should be completed (with at least a newline) with printf
    203  * or addlog, respectively.  There is no trailing space.
    204  */
    205 void
    206 diskerr(bp, dname, what, pri, blkdone, lp)
    207 	register struct buf *bp;
    208 	char *dname, *what;
    209 	int pri, blkdone;
    210 	register struct disklabel *lp;
    211 {
    212 	int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev);
    213 	register void (*pr) __P((const char *, ...));
    214 	char partname = 'a' + part;
    215 	int sn;
    216 
    217 	if (pri != LOG_PRINTF) {
    218 		static const char fmt[] = "";
    219 		log(pri, fmt);
    220 		pr = addlog;
    221 	} else
    222 		pr = printf;
    223 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    224 	    bp->b_flags & B_READ ? "read" : "writ");
    225 	sn = bp->b_blkno;
    226 	if (bp->b_bcount <= DEV_BSIZE)
    227 		(*pr)("%d", sn);
    228 	else {
    229 		if (blkdone >= 0) {
    230 			sn += blkdone;
    231 			(*pr)("%d of ", sn);
    232 		}
    233 		(*pr)("%d-%d", bp->b_blkno,
    234 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    235 	}
    236 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    237 #ifdef tahoe
    238 		sn *= DEV_BSIZE / lp->d_secsize;		/* XXX */
    239 #endif
    240 		sn += lp->d_partitions[part].p_offset;
    241 		(*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
    242 		    sn / lp->d_secpercyl);
    243 		sn %= lp->d_secpercyl;
    244 		(*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
    245 	}
    246 }
    247 
    248 /*
    249  * Initialize the disklist.  Called by main() before autoconfiguration.
    250  */
    251 void
    252 disk_init()
    253 {
    254 
    255 	TAILQ_INIT(&disklist);
    256 	disk_count = 0;
    257 	dk_ndrive = DK_NDRIVE;		/* XXX */
    258 }
    259 
    260 /*
    261  * Searches the disklist for the disk corresponding to the
    262  * name provided.
    263  */
    264 struct disk *
    265 disk_find(name)
    266 	char *name;
    267 {
    268 	struct disk *diskp;
    269 
    270 	if ((name == NULL) || (disk_count <= 0))
    271 		return (NULL);
    272 
    273 	for (diskp = disklist.tqh_first; diskp != NULL;
    274 	    diskp = diskp->dk_link.tqe_next)
    275 		if (strcmp(diskp->dk_name, name) == 0)
    276 			return (diskp);
    277 
    278 	return (NULL);
    279 }
    280 
    281 /*
    282  * Attach a disk.
    283  */
    284 void
    285 disk_attach(diskp)
    286 	struct disk *diskp;
    287 {
    288 	int s;
    289 
    290 	/*
    291 	 * Allocate and initialize the disklabel structures.  Note that
    292 	 * it's not safe to sleep here, since we're probably going to be
    293 	 * called during autoconfiguration.
    294 	 */
    295 	diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
    296 	diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
    297 	    M_NOWAIT);
    298 	if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
    299 		panic("disk_attach: can't allocate storage for disklabel");
    300 
    301 	bzero(diskp->dk_label, sizeof(struct disklabel));
    302 	bzero(diskp->dk_cpulabel, sizeof(struct cpu_disklabel));
    303 
    304 	/*
    305 	 * Set the attached timestamp.
    306 	 */
    307 	s = splclock();
    308 	diskp->dk_attachtime = mono_time;
    309 	splx(s);
    310 
    311 	/*
    312 	 * Link into the disklist.
    313 	 */
    314 	TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
    315 	++disk_count;
    316 }
    317 
    318 /*
    319  * Detach a disk.
    320  */
    321 void
    322 disk_detach(diskp)
    323 	struct disk *diskp;
    324 {
    325 
    326 	/*
    327 	 * Free the space used by the disklabel structures.
    328 	 */
    329 	free(diskp->dk_label, M_DEVBUF);
    330 	free(diskp->dk_cpulabel, M_DEVBUF);
    331 
    332 	/*
    333 	 * Remove from the disklist.
    334 	 */
    335 	TAILQ_REMOVE(&disklist, diskp, dk_link);
    336 	if (--disk_count < 0)
    337 		panic("disk_detach: disk_count < 0");
    338 }
    339 
    340 /*
    341  * Increment a disk's busy counter.  If the counter is going from
    342  * 0 to 1, set the timestamp.
    343  */
    344 void
    345 disk_busy(diskp)
    346 	struct disk *diskp;
    347 {
    348 	int s;
    349 
    350 	/*
    351 	 * XXX We'd like to use something as accurate as microtime(),
    352 	 * but that doesn't depend on the system TOD clock.
    353 	 */
    354 	if (diskp->dk_busy++ == 0) {
    355 		s = splclock();
    356 		diskp->dk_timestamp = mono_time;
    357 		splx(s);
    358 	}
    359 }
    360 
    361 /*
    362  * Decrement a disk's busy counter, increment the byte count, total busy
    363  * time, and reset the timestamp.
    364  */
    365 void
    366 disk_unbusy(diskp, bcount)
    367 	struct disk *diskp;
    368 	long bcount;
    369 {
    370 	int s;
    371 	struct timeval dv_time, diff_time;
    372 
    373 	if (diskp->dk_busy-- == 0)
    374 		panic("disk_unbusy: %s: dk_busy < 0", diskp->dk_name);
    375 
    376 	s = splclock();
    377 	dv_time = mono_time;
    378 	splx(s);
    379 
    380 	timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
    381 	timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
    382 
    383 	diskp->dk_timestamp = dv_time;
    384 	if (bcount > 0) {
    385 		diskp->dk_bytes += bcount;
    386 		diskp->dk_xfer++;
    387 	}
    388 }
    389 
    390 /*
    391  * Reset the metrics counters on the given disk.  Note that we cannot
    392  * reset the busy counter, as it may case a panic in disk_unbusy().
    393  * We also must avoid playing with the timestamp information, as it
    394  * may skew any pending transfer results.
    395  */
    396 void
    397 disk_resetstat(diskp)
    398 	struct disk *diskp;
    399 {
    400 	int s = splbio(), t;
    401 
    402 	diskp->dk_xfer = 0;
    403 	diskp->dk_bytes = 0;
    404 
    405 	t = splclock();
    406 	diskp->dk_attachtime = mono_time;
    407 	splx(t);
    408 
    409 	timerclear(&diskp->dk_time);
    410 
    411 	splx(s);
    412 }
    413