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