Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.16
      1 /*	$NetBSD: subr_disk.c,v 1.16 1996/02/09 18:59:56 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 		log(pri, "");
    219 		pr = addlog;
    220 	} else
    221 		pr = printf;
    222 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    223 	    bp->b_flags & B_READ ? "read" : "writ");
    224 	sn = bp->b_blkno;
    225 	if (bp->b_bcount <= DEV_BSIZE)
    226 		(*pr)("%d", sn);
    227 	else {
    228 		if (blkdone >= 0) {
    229 			sn += blkdone;
    230 			(*pr)("%d of ", sn);
    231 		}
    232 		(*pr)("%d-%d", bp->b_blkno,
    233 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    234 	}
    235 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    236 #ifdef tahoe
    237 		sn *= DEV_BSIZE / lp->d_secsize;		/* XXX */
    238 #endif
    239 		sn += lp->d_partitions[part].p_offset;
    240 		(*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
    241 		    sn / lp->d_secpercyl);
    242 		sn %= lp->d_secpercyl;
    243 		(*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
    244 	}
    245 }
    246 
    247 /*
    248  * Initialize the disklist.  Called by main() before autoconfiguration.
    249  */
    250 void
    251 disk_init()
    252 {
    253 
    254 	TAILQ_INIT(&disklist);
    255 	disk_count = 0;
    256 	dk_ndrive = DK_NDRIVE;		/* XXX */
    257 }
    258 
    259 /*
    260  * Searches the disklist for the disk corresponding to the
    261  * name provided.
    262  */
    263 struct disk *
    264 disk_find(name)
    265 	char *name;
    266 {
    267 	struct disk *diskp;
    268 
    269 	if ((name == NULL) || (disk_count <= 0))
    270 		return (NULL);
    271 
    272 	for (diskp = disklist.tqh_first; diskp != NULL;
    273 	    diskp = diskp->dk_link.tqe_next)
    274 		if (strcmp(diskp->dk_name, name) == 0)
    275 			return (diskp);
    276 
    277 	return (NULL);
    278 }
    279 
    280 /*
    281  * Attach a disk.
    282  */
    283 void
    284 disk_attach(diskp)
    285 	struct disk *diskp;
    286 {
    287 	int s;
    288 
    289 	/*
    290 	 * Allocate and initialize the disklabel structures.  Note that
    291 	 * it's not safe to sleep here, since we're probably going to be
    292 	 * called during autoconfiguration.
    293 	 */
    294 	diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
    295 	diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
    296 	    M_NOWAIT);
    297 	if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
    298 		panic("disk_attach: can't allocate storage for disklabel");
    299 
    300 	bzero(diskp->dk_label, sizeof(struct disklabel));
    301 	bzero(diskp->dk_cpulabel, sizeof(struct cpu_disklabel));
    302 
    303 	/*
    304 	 * Set the attached timestamp.
    305 	 */
    306 	s = splclock();
    307 	diskp->dk_attachtime = mono_time;
    308 	splx(s);
    309 
    310 	/*
    311 	 * Link into the disklist.
    312 	 */
    313 	TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
    314 	++disk_count;
    315 }
    316 
    317 /*
    318  * Detach a disk.
    319  */
    320 void
    321 disk_detach(diskp)
    322 	struct disk *diskp;
    323 {
    324 
    325 	/*
    326 	 * Free the space used by the disklabel structures.
    327 	 */
    328 	free(diskp->dk_label, M_DEVBUF);
    329 	free(diskp->dk_cpulabel, M_DEVBUF);
    330 
    331 	/*
    332 	 * Remove from the disklist.
    333 	 */
    334 	TAILQ_REMOVE(&disklist, diskp, dk_link);
    335 	if (--disk_count < 0)
    336 		panic("disk_detach: disk_count < 0");
    337 }
    338 
    339 /*
    340  * Increment a disk's busy counter.  If the counter is going from
    341  * 0 to 1, set the timestamp.
    342  */
    343 void
    344 disk_busy(diskp)
    345 	struct disk *diskp;
    346 {
    347 	int s;
    348 
    349 	/*
    350 	 * XXX We'd like to use something as accurate as microtime(),
    351 	 * but that doesn't depend on the system TOD clock.
    352 	 */
    353 	if (diskp->dk_busy++ == 0) {
    354 		s = splclock();
    355 		diskp->dk_timestamp = mono_time;
    356 		splx(s);
    357 	}
    358 }
    359 
    360 /*
    361  * Decrement a disk's busy counter, increment the byte count, total busy
    362  * time, and reset the timestamp.
    363  */
    364 void
    365 disk_unbusy(diskp, bcount)
    366 	struct disk *diskp;
    367 	long bcount;
    368 {
    369 	int s;
    370 	struct timeval dv_time, diff_time;
    371 
    372 	if (diskp->dk_busy-- == 0)
    373 		panic("disk_unbusy: %s: dk_busy < 0", diskp->dk_name);
    374 
    375 	s = splclock();
    376 	dv_time = mono_time;
    377 	splx(s);
    378 
    379 	timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
    380 	timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
    381 
    382 	diskp->dk_timestamp = dv_time;
    383 	if (bcount > 0) {
    384 		diskp->dk_bytes += bcount;
    385 		diskp->dk_xfer++;
    386 	}
    387 }
    388 
    389 /*
    390  * Reset the metrics counters on the given disk.  Note that we cannot
    391  * reset the busy counter, as it may case a panic in disk_unbusy().
    392  * We also must avoid playing with the timestamp information, as it
    393  * may skew any pending transfer results.
    394  */
    395 void
    396 disk_resetstat(diskp)
    397 	struct disk *diskp;
    398 {
    399 	int s = splbio(), t;
    400 
    401 	diskp->dk_xfer = 0;
    402 	diskp->dk_bytes = 0;
    403 
    404 	t = splclock();
    405 	diskp->dk_attachtime = mono_time;
    406 	splx(t);
    407 
    408 	timerclear(&diskp->dk_time);
    409 
    410 	splx(s);
    411 }
    412