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