Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.73.10.1
      1 /*	$NetBSD: subr_disk.c,v 1.73.10.1 2006/04/19 05:13:59 elad 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.73.10.1 2006/04/19 05:13:59 elad Exp $");
     78 
     79 #include "opt_compat_netbsd.h"
     80 
     81 #include <sys/param.h>
     82 #include <sys/kernel.h>
     83 #include <sys/malloc.h>
     84 #include <sys/buf.h>
     85 #include <sys/syslog.h>
     86 #include <sys/disklabel.h>
     87 #include <sys/disk.h>
     88 #include <sys/sysctl.h>
     89 #include <lib/libkern/libkern.h>
     90 
     91 /*
     92  * Compute checksum for disk label.
     93  */
     94 u_int
     95 dkcksum(struct disklabel *lp)
     96 {
     97 	u_short *start, *end;
     98 	u_short sum = 0;
     99 
    100 	start = (u_short *)lp;
    101 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
    102 	while (start < end)
    103 		sum ^= *start++;
    104 	return (sum);
    105 }
    106 
    107 /*
    108  * Disk error is the preface to plaintive error messages
    109  * about failing disk transfers.  It prints messages of the form
    110 
    111 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
    112 
    113  * if the offset of the error in the transfer and a disk label
    114  * are both available.  blkdone should be -1 if the position of the error
    115  * is unknown; the disklabel pointer may be null from drivers that have not
    116  * been converted to use them.  The message is printed with printf
    117  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
    118  * The message should be completed (with at least a newline) with printf
    119  * or addlog, respectively.  There is no trailing space.
    120  */
    121 #ifndef PRIdaddr
    122 #define PRIdaddr PRId64
    123 #endif
    124 void
    125 diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
    126     int blkdone, const struct disklabel *lp)
    127 {
    128 	int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
    129 	void (*pr)(const char *, ...);
    130 	char partname = 'a' + part;
    131 	daddr_t sn;
    132 
    133 	if (/*CONSTCOND*/0)
    134 		/* Compiler will error this is the format is wrong... */
    135 		printf("%" PRIdaddr, bp->b_blkno);
    136 
    137 	if (pri != LOG_PRINTF) {
    138 		static const char fmt[] = "";
    139 		log(pri, fmt);
    140 		pr = addlog;
    141 	} else
    142 		pr = printf;
    143 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    144 	    bp->b_flags & B_READ ? "read" : "writ");
    145 	sn = bp->b_blkno;
    146 	if (bp->b_bcount <= DEV_BSIZE)
    147 		(*pr)("%" PRIdaddr, sn);
    148 	else {
    149 		if (blkdone >= 0) {
    150 			sn += blkdone;
    151 			(*pr)("%" PRIdaddr " of ", sn);
    152 		}
    153 		(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
    154 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    155 	}
    156 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    157 		sn += lp->d_partitions[part].p_offset;
    158 		(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
    159 		    dname, unit, sn, sn / lp->d_secpercyl);
    160 		sn %= lp->d_secpercyl;
    161 		(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
    162 		    sn / lp->d_nsectors, sn % lp->d_nsectors);
    163 	}
    164 }
    165 
    166 /*
    167  * Searches the iostatlist for the disk corresponding to the
    168  * name provided.
    169  */
    170 struct disk *
    171 disk_find(char *name)
    172 {
    173 	struct io_stats *stat;
    174 
    175 	stat = iostat_find(name);
    176 
    177 	if ((stat != NULL) && (stat->type == IOSTAT_DISK))
    178 		return stat->parent;
    179 
    180         return (NULL);
    181 }
    182 
    183 static void
    184 disk_init0(struct disk *diskp)
    185 {
    186 
    187 	/*
    188 	 * Initialize the wedge-related locks and other fields.
    189 	 */
    190 	lockinit(&diskp->dk_rawlock, PRIBIO, "dkrawlk", 0, 0);
    191 	lockinit(&diskp->dk_openlock, PRIBIO, "dkoplk", 0, 0);
    192 	LIST_INIT(&diskp->dk_wedges);
    193 	diskp->dk_nwedges = 0;
    194 }
    195 
    196 static void
    197 disk_attach0(struct disk *diskp)
    198 {
    199 	/*
    200 	 * Allocate and initialize the disklabel structures.  Note that
    201 	 * it's not safe to sleep here, since we're probably going to be
    202 	 * called during autoconfiguration.
    203 	 */
    204 	diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
    205 	diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
    206 	    M_NOWAIT);
    207 	if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
    208 		panic("disk_attach: can't allocate storage for disklabel");
    209 
    210 	memset(diskp->dk_label, 0, sizeof(struct disklabel));
    211 	memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    212 
    213 	/*
    214 	 * Set up the stats collection.
    215 	 */
    216 	diskp->dk_stats = iostat_alloc(IOSTAT_DISK);
    217         diskp->dk_stats->parent = (void *) diskp;
    218         diskp->dk_stats->name = diskp->dk_name;
    219 
    220 }
    221 
    222 static void
    223 disk_detach0(struct disk *diskp)
    224 {
    225 
    226 	/*
    227 	 * Remove from the drivelist.
    228 	 */
    229         iostat_free(diskp->dk_stats);
    230 
    231 	/*
    232 	 * Free the space used by the disklabel structures.
    233 	 */
    234 	free(diskp->dk_label, M_DEVBUF);
    235 	free(diskp->dk_cpulabel, M_DEVBUF);
    236 }
    237 
    238 /*
    239  * Attach a disk.
    240  */
    241 void
    242 disk_attach(struct disk *diskp)
    243 {
    244 
    245 	disk_init0(diskp);
    246 	disk_attach0(diskp);
    247 }
    248 
    249 /*
    250  * Detach a disk.
    251  */
    252 void
    253 disk_detach(struct disk *diskp)
    254 {
    255 
    256 	(void) lockmgr(&diskp->dk_openlock, LK_DRAIN, NULL);
    257 	disk_detach0(diskp);
    258 }
    259 
    260 /*
    261  * Initialize a pseudo disk.
    262  */
    263 void
    264 pseudo_disk_init(struct disk *diskp)
    265 {
    266 
    267 	disk_init0(diskp);
    268 }
    269 
    270 /*
    271  * Attach a pseudo disk.
    272  */
    273 void
    274 pseudo_disk_attach(struct disk *diskp)
    275 {
    276 
    277 	disk_attach0(diskp);
    278 }
    279 
    280 /*
    281  * Detach a pseudo disk.
    282  */
    283 void
    284 pseudo_disk_detach(struct disk *diskp)
    285 {
    286 
    287 	disk_detach0(diskp);
    288 }
    289 
    290 /*
    291  * Mark the disk as busy for metrics collection.
    292  */
    293 void
    294 disk_busy(struct disk *diskp)
    295 {
    296 	iostat_busy(diskp->dk_stats);
    297 }
    298 
    299 /*
    300  * Finished disk operations, gather metrics.
    301  */
    302 void
    303 disk_unbusy(struct disk *diskp, long bcount, int read)
    304 {
    305 	iostat_unbusy(diskp->dk_stats, bcount, read);
    306 }
    307 
    308 /*
    309  * Bounds checking against the media size, used for the raw partition.
    310  * The sector size passed in should currently always be DEV_BSIZE,
    311  * and the media size the size of the device in DEV_BSIZE sectors.
    312  */
    313 int
    314 bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
    315 {
    316 	int64_t sz;
    317 
    318 	sz = howmany(bp->b_bcount, secsize);
    319 
    320 	if (bp->b_blkno + sz > mediasize) {
    321 		sz = mediasize - bp->b_blkno;
    322 		if (sz == 0) {
    323 			/* If exactly at end of disk, return EOF. */
    324 			bp->b_resid = bp->b_bcount;
    325 			goto done;
    326 		}
    327 		if (sz < 0) {
    328 			/* If past end of disk, return EINVAL. */
    329 			bp->b_error = EINVAL;
    330 			goto bad;
    331 		}
    332 		/* Otherwise, truncate request. */
    333 		bp->b_bcount = sz << DEV_BSHIFT;
    334 	}
    335 
    336 	return 1;
    337 
    338 bad:
    339 	bp->b_flags |= B_ERROR;
    340 done:
    341 	return 0;
    342 }
    343