Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.14
      1 /*	$NetBSD: subr_disk.c,v 1.14 1995/12/28 19:16:39 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/buf.h>
     46 #include <sys/disklabel.h>
     47 #include <sys/syslog.h>
     48 #include <sys/dkstat.h>		/* XXX */
     49 
     50 /*
     51  * Old-style disk instrumentation structures.  These will go away
     52  * someday.
     53  */
     54 long	dk_seek[DK_NDRIVE];
     55 long	dk_time[DK_NDRIVE];
     56 long	dk_wds[DK_NDRIVE];
     57 long	dk_wpms[DK_NDRIVE];
     58 long	dk_xfer[DK_NDRIVE];
     59 int	dk_busy;
     60 int	dk_ndrive = DK_NDRIVE;
     61 int	dkn;			/* number of slots filled so far */
     62 
     63 /*
     64  * Seek sort for disks.  We depend on the driver which calls us using b_resid
     65  * as the current cylinder number.
     66  *
     67  * The argument ap structure holds a b_actf activity chain pointer on which we
     68  * keep two queues, sorted in ascending cylinder order.  The first queue holds
     69  * those requests which are positioned after the current cylinder (in the first
     70  * request); the second holds requests which came in after their cylinder number
     71  * was passed.  Thus we implement a one way scan, retracting after reaching the
     72  * end of the drive to the first request on the second queue, at which time it
     73  * becomes the first queue.
     74  *
     75  * A one-way scan is natural because of the way UNIX read-ahead blocks are
     76  * allocated.
     77  */
     78 
     79 void
     80 disksort(ap, bp)
     81 	register struct buf *ap, *bp;
     82 {
     83 	register struct buf *bq;
     84 
     85 	/* If the queue is empty, then it's easy. */
     86 	if (ap->b_actf == NULL) {
     87 		bp->b_actf = NULL;
     88 		ap->b_actf = bp;
     89 		return;
     90 	}
     91 
     92 	/*
     93 	 * If we lie after the first (currently active) request, then we
     94 	 * must locate the second request list and add ourselves to it.
     95 	 */
     96 	bq = ap->b_actf;
     97 	if (bp->b_cylinder < bq->b_cylinder) {
     98 		while (bq->b_actf) {
     99 			/*
    100 			 * Check for an ``inversion'' in the normally ascending
    101 			 * cylinder numbers, indicating the start of the second
    102 			 * request list.
    103 			 */
    104 			if (bq->b_actf->b_cylinder < bq->b_cylinder) {
    105 				/*
    106 				 * Search the second request list for the first
    107 				 * request at a larger cylinder number.  We go
    108 				 * before that; if there is no such request, we
    109 				 * go at end.
    110 				 */
    111 				do {
    112 					if (bp->b_cylinder <
    113 					    bq->b_actf->b_cylinder)
    114 						goto insert;
    115 					if (bp->b_cylinder ==
    116 					    bq->b_actf->b_cylinder &&
    117 					    bp->b_blkno < bq->b_actf->b_blkno)
    118 						goto insert;
    119 					bq = bq->b_actf;
    120 				} while (bq->b_actf);
    121 				goto insert;		/* after last */
    122 			}
    123 			bq = bq->b_actf;
    124 		}
    125 		/*
    126 		 * No inversions... we will go after the last, and
    127 		 * be the first request in the second request list.
    128 		 */
    129 		goto insert;
    130 	}
    131 	/*
    132 	 * Request is at/after the current request...
    133 	 * sort in the first request list.
    134 	 */
    135 	while (bq->b_actf) {
    136 		/*
    137 		 * We want to go after the current request if there is an
    138 		 * inversion after it (i.e. it is the end of the first
    139 		 * request list), or if the next request is a larger cylinder
    140 		 * than our request.
    141 		 */
    142 		if (bq->b_actf->b_cylinder < bq->b_cylinder ||
    143 		    bp->b_cylinder < bq->b_actf->b_cylinder ||
    144 		    (bp->b_cylinder == bq->b_actf->b_cylinder &&
    145 		    bp->b_blkno < bq->b_actf->b_blkno))
    146 			goto insert;
    147 		bq = bq->b_actf;
    148 	}
    149 	/*
    150 	 * Neither a second list nor a larger request... we go at the end of
    151 	 * the first list, which is the same as the end of the whole schebang.
    152 	 */
    153 insert:	bp->b_actf = bq->b_actf;
    154 	bq->b_actf = bp;
    155 }
    156 
    157 /* encoding of disk minor numbers, should be elsewhere... */
    158 #define dkunit(dev)		(minor(dev) >> 3)
    159 #define dkpart(dev)		(minor(dev) & 07)
    160 #define dkminor(unit, part)	(((unit) << 3) | (part))
    161 
    162 /*
    163  * Compute checksum for disk label.
    164  */
    165 u_int
    166 dkcksum(lp)
    167 	register struct disklabel *lp;
    168 {
    169 	register u_short *start, *end;
    170 	register u_short sum = 0;
    171 
    172 	start = (u_short *)lp;
    173 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
    174 	while (start < end)
    175 		sum ^= *start++;
    176 	return (sum);
    177 }
    178 
    179 /*
    180  * Disk error is the preface to plaintive error messages
    181  * about failing disk transfers.  It prints messages of the form
    182 
    183 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
    184 
    185  * if the offset of the error in the transfer and a disk label
    186  * are both available.  blkdone should be -1 if the position of the error
    187  * is unknown; the disklabel pointer may be null from drivers that have not
    188  * been converted to use them.  The message is printed with printf
    189  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
    190  * The message should be completed (with at least a newline) with printf
    191  * or addlog, respectively.  There is no trailing space.
    192  */
    193 void
    194 diskerr(bp, dname, what, pri, blkdone, lp)
    195 	register struct buf *bp;
    196 	char *dname, *what;
    197 	int pri, blkdone;
    198 	register struct disklabel *lp;
    199 {
    200 	int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev);
    201 	register void (*pr) __P((const char *, ...));
    202 	char partname = 'a' + part;
    203 	int sn;
    204 
    205 	if (pri != LOG_PRINTF) {
    206 		log(pri, "");
    207 		pr = addlog;
    208 	} else
    209 		pr = printf;
    210 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    211 	    bp->b_flags & B_READ ? "read" : "writ");
    212 	sn = bp->b_blkno;
    213 	if (bp->b_bcount <= DEV_BSIZE)
    214 		(*pr)("%d", sn);
    215 	else {
    216 		if (blkdone >= 0) {
    217 			sn += blkdone;
    218 			(*pr)("%d of ", sn);
    219 		}
    220 		(*pr)("%d-%d", bp->b_blkno,
    221 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    222 	}
    223 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    224 #ifdef tahoe
    225 		sn *= DEV_BSIZE / lp->d_secsize;		/* XXX */
    226 #endif
    227 		sn += lp->d_partitions[part].p_offset;
    228 		(*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
    229 		    sn / lp->d_secpercyl);
    230 		sn %= lp->d_secpercyl;
    231 		(*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
    232 	}
    233 }
    234