Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.73.6.1
      1  1.73.6.1    simonb /*	$NetBSD: subr_disk.c,v 1.73.6.1 2006/02/04 14:30:17 simonb 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.53       agc  * 3. Neither the name of the University nor the names of its contributors
     58      1.11   mycroft  *    may be used to endorse or promote products derived from this software
     59      1.11   mycroft  *    without specific prior written permission.
     60      1.11   mycroft  *
     61      1.11   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62      1.11   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63      1.11   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64      1.11   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65      1.11   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66      1.11   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67      1.11   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68      1.11   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69      1.11   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70      1.11   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71      1.11   mycroft  * SUCH DAMAGE.
     72      1.11   mycroft  *
     73      1.12       cgd  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     74      1.11   mycroft  */
     75      1.31     lukem 
     76      1.31     lukem #include <sys/cdefs.h>
     77  1.73.6.1    simonb __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.73.6.1 2006/02/04 14:30:17 simonb Exp $");
     78      1.48       mrg 
     79      1.48       mrg #include "opt_compat_netbsd.h"
     80      1.11   mycroft 
     81      1.11   mycroft #include <sys/param.h>
     82      1.15   thorpej #include <sys/kernel.h>
     83      1.15   thorpej #include <sys/malloc.h>
     84      1.11   mycroft #include <sys/buf.h>
     85      1.15   thorpej #include <sys/syslog.h>
     86      1.11   mycroft #include <sys/disklabel.h>
     87      1.15   thorpej #include <sys/disk.h>
     88      1.33    simonb #include <sys/sysctl.h>
     89      1.47       mrg #include <lib/libkern/libkern.h>
     90      1.14   thorpej 
     91      1.14   thorpej /*
     92      1.15   thorpej  * A global list of all disks attached to the system.  May grow or
     93      1.15   thorpej  * shrink over time.
     94      1.15   thorpej  */
     95      1.63   thorpej struct	disklist_head disklist = TAILQ_HEAD_INITIALIZER(disklist);
     96      1.15   thorpej int	disk_count;		/* number of drives in global disklist */
     97      1.33    simonb struct simplelock disklist_slock = SIMPLELOCK_INITIALIZER;
     98      1.39   hannken 
     99      1.39   hannken /*
    100      1.11   mycroft  * Compute checksum for disk label.
    101      1.11   mycroft  */
    102      1.11   mycroft u_int
    103      1.30    simonb dkcksum(struct disklabel *lp)
    104      1.11   mycroft {
    105      1.29  augustss 	u_short *start, *end;
    106      1.29  augustss 	u_short sum = 0;
    107      1.11   mycroft 
    108      1.11   mycroft 	start = (u_short *)lp;
    109      1.11   mycroft 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
    110      1.11   mycroft 	while (start < end)
    111      1.11   mycroft 		sum ^= *start++;
    112      1.11   mycroft 	return (sum);
    113      1.11   mycroft }
    114      1.11   mycroft 
    115      1.11   mycroft /*
    116      1.11   mycroft  * Disk error is the preface to plaintive error messages
    117      1.11   mycroft  * about failing disk transfers.  It prints messages of the form
    118      1.11   mycroft 
    119      1.11   mycroft hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
    120      1.11   mycroft 
    121      1.11   mycroft  * if the offset of the error in the transfer and a disk label
    122      1.11   mycroft  * are both available.  blkdone should be -1 if the position of the error
    123      1.11   mycroft  * is unknown; the disklabel pointer may be null from drivers that have not
    124      1.20  christos  * been converted to use them.  The message is printed with printf
    125      1.11   mycroft  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
    126      1.20  christos  * The message should be completed (with at least a newline) with printf
    127      1.11   mycroft  * or addlog, respectively.  There is no trailing space.
    128      1.11   mycroft  */
    129      1.51       dsl #ifndef PRIdaddr
    130      1.51       dsl #define PRIdaddr PRId64
    131      1.51       dsl #endif
    132      1.11   mycroft void
    133      1.38      yamt diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
    134      1.38      yamt     int blkdone, const struct disklabel *lp)
    135      1.11   mycroft {
    136      1.25  drochner 	int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
    137      1.30    simonb 	void (*pr)(const char *, ...);
    138      1.11   mycroft 	char partname = 'a' + part;
    139      1.51       dsl 	daddr_t sn;
    140      1.51       dsl 
    141      1.52       dsl 	if (/*CONSTCOND*/0)
    142      1.51       dsl 		/* Compiler will error this is the format is wrong... */
    143      1.51       dsl 		printf("%" PRIdaddr, bp->b_blkno);
    144      1.11   mycroft 
    145      1.11   mycroft 	if (pri != LOG_PRINTF) {
    146      1.17  christos 		static const char fmt[] = "";
    147      1.17  christos 		log(pri, fmt);
    148      1.11   mycroft 		pr = addlog;
    149      1.11   mycroft 	} else
    150      1.20  christos 		pr = printf;
    151      1.11   mycroft 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    152      1.11   mycroft 	    bp->b_flags & B_READ ? "read" : "writ");
    153      1.11   mycroft 	sn = bp->b_blkno;
    154      1.11   mycroft 	if (bp->b_bcount <= DEV_BSIZE)
    155      1.51       dsl 		(*pr)("%" PRIdaddr, sn);
    156      1.11   mycroft 	else {
    157      1.11   mycroft 		if (blkdone >= 0) {
    158      1.11   mycroft 			sn += blkdone;
    159      1.51       dsl 			(*pr)("%" PRIdaddr " of ", sn);
    160      1.11   mycroft 		}
    161      1.51       dsl 		(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
    162      1.11   mycroft 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    163      1.11   mycroft 	}
    164      1.11   mycroft 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    165      1.11   mycroft 		sn += lp->d_partitions[part].p_offset;
    166      1.51       dsl 		(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
    167      1.51       dsl 		    dname, unit, sn, sn / lp->d_secpercyl);
    168      1.11   mycroft 		sn %= lp->d_secpercyl;
    169      1.51       dsl 		(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
    170      1.51       dsl 		    sn / lp->d_nsectors, sn % lp->d_nsectors);
    171      1.11   mycroft 	}
    172      1.15   thorpej }
    173      1.15   thorpej 
    174      1.15   thorpej /*
    175      1.15   thorpej  * Searches the disklist for the disk corresponding to the
    176      1.15   thorpej  * name provided.
    177      1.15   thorpej  */
    178      1.15   thorpej struct disk *
    179      1.30    simonb disk_find(char *name)
    180      1.15   thorpej {
    181      1.15   thorpej 	struct disk *diskp;
    182      1.15   thorpej 
    183      1.15   thorpej 	if ((name == NULL) || (disk_count <= 0))
    184      1.15   thorpej 		return (NULL);
    185      1.15   thorpej 
    186      1.33    simonb 	simple_lock(&disklist_slock);
    187      1.33    simonb 	for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
    188      1.33    simonb 	    diskp = TAILQ_NEXT(diskp, dk_link))
    189      1.33    simonb 		if (strcmp(diskp->dk_name, name) == 0) {
    190      1.33    simonb 			simple_unlock(&disklist_slock);
    191      1.15   thorpej 			return (diskp);
    192      1.33    simonb 		}
    193      1.33    simonb 	simple_unlock(&disklist_slock);
    194      1.15   thorpej 
    195      1.15   thorpej 	return (NULL);
    196      1.15   thorpej }
    197      1.15   thorpej 
    198      1.70      yamt static void
    199      1.70      yamt disk_init0(struct disk *diskp)
    200      1.70      yamt {
    201      1.70      yamt 
    202      1.70      yamt 	/*
    203      1.70      yamt 	 * Initialize the wedge-related locks and other fields.
    204      1.70      yamt 	 */
    205      1.70      yamt 	lockinit(&diskp->dk_rawlock, PRIBIO, "dkrawlk", 0, 0);
    206      1.70      yamt 	lockinit(&diskp->dk_openlock, PRIBIO, "dkoplk", 0, 0);
    207      1.70      yamt 	LIST_INIT(&diskp->dk_wedges);
    208      1.70      yamt 	diskp->dk_nwedges = 0;
    209      1.70      yamt }
    210      1.70      yamt 
    211      1.70      yamt static void
    212      1.70      yamt disk_attach0(struct disk *diskp)
    213      1.15   thorpej {
    214      1.15   thorpej 
    215      1.15   thorpej 	/*
    216      1.15   thorpej 	 * Allocate and initialize the disklabel structures.  Note that
    217      1.15   thorpej 	 * it's not safe to sleep here, since we're probably going to be
    218      1.15   thorpej 	 * called during autoconfiguration.
    219      1.15   thorpej 	 */
    220      1.15   thorpej 	diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
    221      1.15   thorpej 	diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
    222      1.15   thorpej 	    M_NOWAIT);
    223      1.15   thorpej 	if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
    224      1.15   thorpej 		panic("disk_attach: can't allocate storage for disklabel");
    225      1.15   thorpej 
    226      1.24     perry 	memset(diskp->dk_label, 0, sizeof(struct disklabel));
    227      1.24     perry 	memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    228      1.15   thorpej 
    229      1.15   thorpej 	/*
    230      1.15   thorpej 	 * Set the attached timestamp.
    231      1.15   thorpej 	 */
    232  1.73.6.1    simonb 	getmicrouptime(&diskp->dk_attachtime);
    233      1.15   thorpej 
    234      1.15   thorpej 	/*
    235      1.15   thorpej 	 * Link into the disklist.
    236      1.15   thorpej 	 */
    237      1.33    simonb 	simple_lock(&disklist_slock);
    238      1.15   thorpej 	TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
    239      1.63   thorpej 	disk_count++;
    240      1.33    simonb 	simple_unlock(&disklist_slock);
    241      1.15   thorpej }
    242      1.15   thorpej 
    243      1.70      yamt static void
    244      1.70      yamt disk_detach0(struct disk *diskp)
    245      1.15   thorpej {
    246      1.15   thorpej 
    247      1.15   thorpej 	/*
    248      1.23   thorpej 	 * Remove from the disklist.
    249      1.23   thorpej 	 */
    250      1.63   thorpej 	if (disk_count == 0)
    251      1.63   thorpej 		panic("disk_detach: disk_count == 0");
    252      1.33    simonb 	simple_lock(&disklist_slock);
    253      1.23   thorpej 	TAILQ_REMOVE(&disklist, diskp, dk_link);
    254      1.63   thorpej 	disk_count--;
    255      1.33    simonb 	simple_unlock(&disklist_slock);
    256      1.23   thorpej 
    257      1.23   thorpej 	/*
    258      1.15   thorpej 	 * Free the space used by the disklabel structures.
    259      1.15   thorpej 	 */
    260      1.15   thorpej 	free(diskp->dk_label, M_DEVBUF);
    261      1.15   thorpej 	free(diskp->dk_cpulabel, M_DEVBUF);
    262      1.15   thorpej }
    263      1.15   thorpej 
    264      1.15   thorpej /*
    265      1.70      yamt  * Attach a disk.
    266      1.70      yamt  */
    267      1.70      yamt void
    268      1.70      yamt disk_attach(struct disk *diskp)
    269      1.70      yamt {
    270      1.70      yamt 
    271      1.70      yamt 	disk_init0(diskp);
    272      1.70      yamt 	disk_attach0(diskp);
    273      1.70      yamt }
    274      1.70      yamt 
    275      1.70      yamt /*
    276      1.70      yamt  * Detach a disk.
    277      1.70      yamt  */
    278      1.70      yamt void
    279      1.70      yamt disk_detach(struct disk *diskp)
    280      1.70      yamt {
    281      1.70      yamt 
    282      1.70      yamt 	(void) lockmgr(&diskp->dk_openlock, LK_DRAIN, NULL);
    283      1.70      yamt 	disk_detach0(diskp);
    284      1.70      yamt }
    285      1.70      yamt 
    286      1.70      yamt /*
    287      1.70      yamt  * Initialize a pseudo disk.
    288      1.70      yamt  */
    289      1.70      yamt void
    290      1.70      yamt pseudo_disk_init(struct disk *diskp)
    291      1.70      yamt {
    292      1.70      yamt 
    293      1.70      yamt 	disk_init0(diskp);
    294      1.70      yamt }
    295      1.70      yamt 
    296      1.70      yamt /*
    297      1.70      yamt  * Attach a pseudo disk.
    298      1.70      yamt  */
    299      1.70      yamt void
    300      1.70      yamt pseudo_disk_attach(struct disk *diskp)
    301      1.70      yamt {
    302      1.70      yamt 
    303      1.70      yamt 	disk_attach0(diskp);
    304      1.70      yamt }
    305      1.70      yamt 
    306      1.70      yamt /*
    307      1.70      yamt  * Detach a pseudo disk.
    308      1.70      yamt  */
    309      1.70      yamt void
    310      1.70      yamt pseudo_disk_detach(struct disk *diskp)
    311      1.70      yamt {
    312      1.70      yamt 
    313      1.70      yamt 	disk_detach0(diskp);
    314      1.70      yamt }
    315      1.70      yamt 
    316      1.70      yamt 
    317      1.70      yamt /*
    318      1.15   thorpej  * Increment a disk's busy counter.  If the counter is going from
    319      1.15   thorpej  * 0 to 1, set the timestamp.
    320      1.15   thorpej  */
    321      1.15   thorpej void
    322      1.30    simonb disk_busy(struct disk *diskp)
    323      1.15   thorpej {
    324      1.15   thorpej 
    325  1.73.6.1    simonb 	if (diskp->dk_busy++ == 0)
    326  1.73.6.1    simonb 		getmicrouptime(&diskp->dk_timestamp);
    327      1.15   thorpej }
    328      1.15   thorpej 
    329      1.15   thorpej /*
    330      1.15   thorpej  * Decrement a disk's busy counter, increment the byte count, total busy
    331      1.15   thorpej  * time, and reset the timestamp.
    332      1.15   thorpej  */
    333      1.15   thorpej void
    334      1.45       mrg disk_unbusy(struct disk *diskp, long bcount, int read)
    335      1.15   thorpej {
    336      1.15   thorpej 	struct timeval dv_time, diff_time;
    337      1.15   thorpej 
    338      1.23   thorpej 	if (diskp->dk_busy-- == 0) {
    339      1.23   thorpej 		printf("%s: dk_busy < 0\n", diskp->dk_name);
    340      1.23   thorpej 		panic("disk_unbusy");
    341      1.23   thorpej 	}
    342      1.15   thorpej 
    343  1.73.6.1    simonb 	getmicrouptime(&dv_time);
    344      1.15   thorpej 	timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
    345      1.15   thorpej 	timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
    346      1.15   thorpej 
    347      1.15   thorpej 	diskp->dk_timestamp = dv_time;
    348      1.15   thorpej 	if (bcount > 0) {
    349      1.45       mrg 		if (read) {
    350      1.45       mrg 			diskp->dk_rbytes += bcount;
    351      1.45       mrg 			diskp->dk_rxfer++;
    352      1.45       mrg 		} else {
    353      1.45       mrg 			diskp->dk_wbytes += bcount;
    354      1.45       mrg 			diskp->dk_wxfer++;
    355      1.45       mrg 		}
    356      1.15   thorpej 	}
    357      1.15   thorpej }
    358      1.15   thorpej 
    359      1.15   thorpej /*
    360      1.15   thorpej  * Reset the metrics counters on the given disk.  Note that we cannot
    361      1.15   thorpej  * reset the busy counter, as it may case a panic in disk_unbusy().
    362      1.15   thorpej  * We also must avoid playing with the timestamp information, as it
    363      1.15   thorpej  * may skew any pending transfer results.
    364      1.15   thorpej  */
    365      1.15   thorpej void
    366      1.30    simonb disk_resetstat(struct disk *diskp)
    367      1.15   thorpej {
    368  1.73.6.1    simonb 	int s = splbio();
    369      1.15   thorpej 
    370      1.45       mrg 	diskp->dk_rxfer = 0;
    371      1.45       mrg 	diskp->dk_rbytes = 0;
    372      1.45       mrg 	diskp->dk_wxfer = 0;
    373      1.45       mrg 	diskp->dk_wbytes = 0;
    374      1.15   thorpej 
    375  1.73.6.1    simonb 	getmicrouptime(&diskp->dk_attachtime);
    376      1.15   thorpej 	timerclear(&diskp->dk_time);
    377      1.15   thorpej 
    378      1.15   thorpej 	splx(s);
    379      1.33    simonb }
    380      1.33    simonb 
    381      1.33    simonb int
    382      1.54    atatat sysctl_hw_disknames(SYSCTLFN_ARGS)
    383      1.33    simonb {
    384      1.69  christos 	char bf[DK_DISKNAMELEN + 1];
    385      1.54    atatat 	char *where = oldp;
    386      1.33    simonb 	struct disk *diskp;
    387      1.33    simonb 	size_t needed, left, slen;
    388      1.33    simonb 	int error, first;
    389      1.33    simonb 
    390      1.54    atatat 	if (newp != NULL)
    391      1.54    atatat 		return (EPERM);
    392      1.54    atatat 	if (namelen != 0)
    393      1.54    atatat 		return (EINVAL);
    394      1.54    atatat 
    395      1.33    simonb 	first = 1;
    396      1.33    simonb 	error = 0;
    397      1.33    simonb 	needed = 0;
    398      1.54    atatat 	left = *oldlenp;
    399      1.35    simonb 
    400      1.33    simonb 	simple_lock(&disklist_slock);
    401      1.33    simonb 	for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
    402      1.33    simonb 	    diskp = TAILQ_NEXT(diskp, dk_link)) {
    403      1.33    simonb 		if (where == NULL)
    404      1.33    simonb 			needed += strlen(diskp->dk_name) + 1;
    405      1.33    simonb 		else {
    406      1.69  christos 			memset(bf, 0, sizeof(bf));
    407      1.33    simonb 			if (first) {
    408      1.69  christos 				strncpy(bf, diskp->dk_name, sizeof(bf));
    409      1.33    simonb 				first = 0;
    410      1.33    simonb 			} else {
    411      1.69  christos 				bf[0] = ' ';
    412      1.69  christos 				strncpy(bf + 1, diskp->dk_name,
    413      1.69  christos 				    sizeof(bf) - 1);
    414      1.33    simonb 			}
    415      1.69  christos 			bf[DK_DISKNAMELEN] = '\0';
    416      1.69  christos 			slen = strlen(bf);
    417      1.33    simonb 			if (left < slen + 1)
    418      1.33    simonb 				break;
    419      1.33    simonb 			/* +1 to copy out the trailing NUL byte */
    420      1.69  christos 			error = copyout(bf, where, slen + 1);
    421      1.33    simonb 			if (error)
    422      1.33    simonb 				break;
    423      1.33    simonb 			where += slen;
    424      1.33    simonb 			needed += slen;
    425      1.33    simonb 			left -= slen;
    426      1.33    simonb 		}
    427      1.33    simonb 	}
    428      1.33    simonb 	simple_unlock(&disklist_slock);
    429      1.54    atatat 	*oldlenp = needed;
    430      1.33    simonb 	return (error);
    431      1.33    simonb }
    432      1.33    simonb 
    433      1.33    simonb int
    434      1.54    atatat sysctl_hw_diskstats(SYSCTLFN_ARGS)
    435      1.33    simonb {
    436      1.33    simonb 	struct disk_sysctl sdisk;
    437      1.33    simonb 	struct disk *diskp;
    438      1.54    atatat 	char *where = oldp;
    439      1.33    simonb 	size_t tocopy, left;
    440      1.33    simonb 	int error;
    441      1.33    simonb 
    442      1.54    atatat 	if (newp != NULL)
    443      1.54    atatat 		return (EPERM);
    444      1.54    atatat 
    445      1.48       mrg 	/*
    446      1.48       mrg 	 * The original hw.diskstats call was broken and did not require
    447      1.48       mrg 	 * the userland to pass in it's size of struct disk_sysctl.  This
    448      1.48       mrg 	 * was fixed after NetBSD 1.6 was released, and any applications
    449      1.48       mrg 	 * that do not pass in the size are given an error only, unless
    450      1.48       mrg 	 * we care about 1.6 compatibility.
    451      1.48       mrg 	 */
    452      1.33    simonb 	if (namelen == 0)
    453      1.47       mrg #ifdef COMPAT_16
    454      1.49     enami 		tocopy = offsetof(struct disk_sysctl, dk_rxfer);
    455      1.47       mrg #else
    456      1.47       mrg 		return (EINVAL);
    457      1.47       mrg #endif
    458      1.33    simonb 	else
    459      1.33    simonb 		tocopy = name[0];
    460      1.49     enami 
    461      1.49     enami 	if (where == NULL) {
    462      1.54    atatat 		*oldlenp = disk_count * tocopy;
    463      1.49     enami 		return (0);
    464      1.49     enami 	}
    465      1.33    simonb 
    466      1.33    simonb 	error = 0;
    467      1.54    atatat 	left = *oldlenp;
    468      1.33    simonb 	memset(&sdisk, 0, sizeof(sdisk));
    469      1.54    atatat 	*oldlenp = 0;
    470      1.33    simonb 
    471      1.33    simonb 	simple_lock(&disklist_slock);
    472      1.34    simonb 	TAILQ_FOREACH(diskp, &disklist, dk_link) {
    473      1.46    simonb 		if (left < tocopy)
    474      1.33    simonb 			break;
    475      1.36     enami 		strncpy(sdisk.dk_name, diskp->dk_name, sizeof(sdisk.dk_name));
    476      1.45       mrg 		sdisk.dk_xfer = diskp->dk_rxfer + diskp->dk_wxfer;
    477      1.45       mrg 		sdisk.dk_rxfer = diskp->dk_rxfer;
    478      1.45       mrg 		sdisk.dk_wxfer = diskp->dk_wxfer;
    479      1.33    simonb 		sdisk.dk_seek = diskp->dk_seek;
    480      1.45       mrg 		sdisk.dk_bytes = diskp->dk_rbytes + diskp->dk_wbytes;
    481      1.45       mrg 		sdisk.dk_rbytes = diskp->dk_rbytes;
    482      1.45       mrg 		sdisk.dk_wbytes = diskp->dk_wbytes;
    483      1.33    simonb 		sdisk.dk_attachtime_sec = diskp->dk_attachtime.tv_sec;
    484      1.33    simonb 		sdisk.dk_attachtime_usec = diskp->dk_attachtime.tv_usec;
    485      1.33    simonb 		sdisk.dk_timestamp_sec = diskp->dk_timestamp.tv_sec;
    486      1.33    simonb 		sdisk.dk_timestamp_usec = diskp->dk_timestamp.tv_usec;
    487      1.33    simonb 		sdisk.dk_time_sec = diskp->dk_time.tv_sec;
    488      1.33    simonb 		sdisk.dk_time_usec = diskp->dk_time.tv_usec;
    489      1.33    simonb 		sdisk.dk_busy = diskp->dk_busy;
    490      1.35    simonb 
    491      1.33    simonb 		error = copyout(&sdisk, where, min(tocopy, sizeof(sdisk)));
    492      1.33    simonb 		if (error)
    493      1.33    simonb 			break;
    494      1.33    simonb 		where += tocopy;
    495      1.54    atatat 		*oldlenp += tocopy;
    496      1.33    simonb 		left -= tocopy;
    497      1.33    simonb 	}
    498      1.33    simonb 	simple_unlock(&disklist_slock);
    499      1.33    simonb 	return (error);
    500      1.39   hannken }
    501      1.39   hannken 
    502      1.40   hannken /*
    503      1.50      fvdl  * Bounds checking against the media size, used for the raw partition.
    504      1.50      fvdl  * The sector size passed in should currently always be DEV_BSIZE,
    505      1.50      fvdl  * and the media size the size of the device in DEV_BSIZE sectors.
    506      1.50      fvdl  */
    507      1.50      fvdl int
    508      1.73     perry bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
    509      1.50      fvdl {
    510      1.67      fvdl 	int64_t sz;
    511      1.50      fvdl 
    512      1.50      fvdl 	sz = howmany(bp->b_bcount, secsize);
    513      1.50      fvdl 
    514      1.50      fvdl 	if (bp->b_blkno + sz > mediasize) {
    515      1.50      fvdl 		sz = mediasize - bp->b_blkno;
    516      1.50      fvdl 		if (sz == 0) {
    517      1.50      fvdl 			/* If exactly at end of disk, return EOF. */
    518      1.50      fvdl 			bp->b_resid = bp->b_bcount;
    519      1.50      fvdl 			goto done;
    520      1.50      fvdl 		}
    521      1.50      fvdl 		if (sz < 0) {
    522      1.50      fvdl 			/* If past end of disk, return EINVAL. */
    523      1.50      fvdl 			bp->b_error = EINVAL;
    524      1.50      fvdl 			goto bad;
    525      1.50      fvdl 		}
    526      1.50      fvdl 		/* Otherwise, truncate request. */
    527      1.50      fvdl 		bp->b_bcount = sz << DEV_BSHIFT;
    528      1.50      fvdl 	}
    529      1.50      fvdl 
    530      1.50      fvdl 	return 1;
    531      1.50      fvdl 
    532      1.50      fvdl bad:
    533      1.50      fvdl 	bp->b_flags |= B_ERROR;
    534      1.50      fvdl done:
    535      1.50      fvdl 	return 0;
    536      1.11   mycroft }
    537