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