Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.80.2.2
      1 /*	$NetBSD: subr_disk.c,v 1.80.2.2 2007/01/12 01:04:07 ad 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.80.2.2 2007/01/12 01:04:07 ad Exp $");
     78 
     79 #include <sys/param.h>
     80 #include <sys/kernel.h>
     81 #include <sys/malloc.h>
     82 #include <sys/buf.h>
     83 #include <sys/syslog.h>
     84 #include <sys/disklabel.h>
     85 #include <sys/disk.h>
     86 #include <sys/sysctl.h>
     87 #include <lib/libkern/libkern.h>
     88 
     89 /*
     90  * Compute checksum for disk label.
     91  */
     92 u_int
     93 dkcksum(struct disklabel *lp)
     94 {
     95 	u_short *start, *end;
     96 	u_short sum = 0;
     97 
     98 	start = (u_short *)lp;
     99 	end = (u_short *)&lp->d_partitions[lp->d_npartitions];
    100 	while (start < end)
    101 		sum ^= *start++;
    102 	return (sum);
    103 }
    104 
    105 /*
    106  * Disk error is the preface to plaintive error messages
    107  * about failing disk transfers.  It prints messages of the form
    108 
    109 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
    110 
    111  * if the offset of the error in the transfer and a disk label
    112  * are both available.  blkdone should be -1 if the position of the error
    113  * is unknown; the disklabel pointer may be null from drivers that have not
    114  * been converted to use them.  The message is printed with printf
    115  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
    116  * The message should be completed (with at least a newline) with printf
    117  * or addlog, respectively.  There is no trailing space.
    118  */
    119 #ifndef PRIdaddr
    120 #define PRIdaddr PRId64
    121 #endif
    122 void
    123 diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
    124     int blkdone, const struct disklabel *lp)
    125 {
    126 	int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
    127 	void (*pr)(const char *, ...);
    128 	char partname = 'a' + part;
    129 	daddr_t sn;
    130 
    131 	if (/*CONSTCOND*/0)
    132 		/* Compiler will error this is the format is wrong... */
    133 		printf("%" PRIdaddr, bp->b_blkno);
    134 
    135 	if (pri != LOG_PRINTF) {
    136 		static const char fmt[] = "";
    137 		log(pri, fmt);
    138 		pr = addlog;
    139 	} else
    140 		pr = printf;
    141 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    142 	    bp->b_flags & B_READ ? "read" : "writ");
    143 	sn = bp->b_blkno;
    144 	if (bp->b_bcount <= DEV_BSIZE)
    145 		(*pr)("%" PRIdaddr, sn);
    146 	else {
    147 		if (blkdone >= 0) {
    148 			sn += blkdone;
    149 			(*pr)("%" PRIdaddr " of ", sn);
    150 		}
    151 		(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
    152 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    153 	}
    154 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    155 		sn += lp->d_partitions[part].p_offset;
    156 		(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
    157 		    dname, unit, sn, sn / lp->d_secpercyl);
    158 		sn %= lp->d_secpercyl;
    159 		(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
    160 		    sn / lp->d_nsectors, sn % lp->d_nsectors);
    161 	}
    162 }
    163 
    164 /*
    165  * Searches the iostatlist for the disk corresponding to the
    166  * name provided.
    167  */
    168 struct disk *
    169 disk_find(const char *name)
    170 {
    171 	struct io_stats *stat;
    172 
    173 	stat = iostat_find(name);
    174 
    175 	if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
    176 		return stat->io_parent;
    177 
    178 	return (NULL);
    179 }
    180 
    181 static void
    182 disk_init0(struct disk *diskp)
    183 {
    184 
    185 	/*
    186 	 * Initialize the wedge-related locks and other fields.
    187 	 */
    188 	lockinit(&diskp->dk_rawlock, PRIBIO, "dkrawlk", 0, 0);
    189 	lockinit(&diskp->dk_openlock, PRIBIO, "dkoplk", 0, 0);
    190 	LIST_INIT(&diskp->dk_wedges);
    191 	diskp->dk_nwedges = 0;
    192 	diskp->dk_labelsector = LABELSECTOR;
    193 	disk_blocksize(diskp, DEV_BSIZE);
    194 }
    195 
    196 static void
    197 disk_attach0(struct disk *diskp)
    198 {
    199 
    200 	/*
    201 	 * Allocate and initialize the disklabel structures.  Note that
    202 	 * it's not safe to sleep here, since we're probably going to be
    203 	 * called during autoconfiguration.
    204 	 */
    205 	diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
    206 	diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
    207 	    M_NOWAIT);
    208 	if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
    209 		panic("disk_attach: can't allocate storage for disklabel");
    210 
    211 	memset(diskp->dk_label, 0, sizeof(struct disklabel));
    212 	memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    213 
    214 	/*
    215 	 * Set up the stats collection.
    216 	 */
    217 	diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
    218 }
    219 
    220 static void
    221 disk_detach0(struct disk *diskp)
    222 {
    223 
    224 	/*
    225 	 * Remove from the drivelist.
    226 	 */
    227 	iostat_free(diskp->dk_stats);
    228 
    229 	/*
    230 	 * Release the disk-info dictionary.
    231 	 */
    232 	if (diskp->dk_info) {
    233 		prop_object_release(diskp->dk_info);
    234 		diskp->dk_info = NULL;
    235 	}
    236 
    237 	/*
    238 	 * Free the space used by the disklabel structures.
    239 	 */
    240 	free(diskp->dk_label, M_DEVBUF);
    241 	free(diskp->dk_cpulabel, M_DEVBUF);
    242 }
    243 
    244 /*
    245  * Attach a disk.
    246  */
    247 void
    248 disk_attach(struct disk *diskp)
    249 {
    250 
    251 	disk_init0(diskp);
    252 	disk_attach0(diskp);
    253 }
    254 
    255 /*
    256  * Detach a disk.
    257  */
    258 void
    259 disk_detach(struct disk *diskp)
    260 {
    261 
    262 	(void) lockmgr(&diskp->dk_openlock, LK_DRAIN, NULL);
    263 	disk_detach0(diskp);
    264 }
    265 
    266 /*
    267  * Initialize a pseudo disk.
    268  */
    269 void
    270 pseudo_disk_init(struct disk *diskp)
    271 {
    272 
    273 	disk_init0(diskp);
    274 }
    275 
    276 /*
    277  * Attach a pseudo disk.
    278  */
    279 void
    280 pseudo_disk_attach(struct disk *diskp)
    281 {
    282 
    283 	disk_attach0(diskp);
    284 }
    285 
    286 /*
    287  * Detach a pseudo disk.
    288  */
    289 void
    290 pseudo_disk_detach(struct disk *diskp)
    291 {
    292 
    293 	disk_detach0(diskp);
    294 }
    295 
    296 /*
    297  * Mark the disk as busy for metrics collection.
    298  */
    299 void
    300 disk_busy(struct disk *diskp)
    301 {
    302 
    303 	iostat_busy(diskp->dk_stats);
    304 }
    305 
    306 /*
    307  * Finished disk operations, gather metrics.
    308  */
    309 void
    310 disk_unbusy(struct disk *diskp, long bcount, int read)
    311 {
    312 
    313 	iostat_unbusy(diskp->dk_stats, bcount, read);
    314 }
    315 
    316 /*
    317  * Set the physical blocksize of a disk, in bytes.
    318  * Only necessary if blocksize != DEV_BSIZE.
    319  */
    320 void
    321 disk_blocksize(struct disk *diskp, int blocksize)
    322 {
    323 
    324 	diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
    325 	diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
    326 }
    327 
    328 /*
    329  * Bounds checking against the media size, used for the raw partition.
    330  * The sector size passed in should currently always be DEV_BSIZE,
    331  * and the media size the size of the device in DEV_BSIZE sectors.
    332  */
    333 int
    334 bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
    335 {
    336 	int64_t sz;
    337 
    338 	sz = howmany(bp->b_bcount, secsize);
    339 
    340 	if (bp->b_blkno + sz > mediasize) {
    341 		sz = mediasize - bp->b_blkno;
    342 		if (sz == 0) {
    343 			/* If exactly at end of disk, return EOF. */
    344 			bp->b_resid = bp->b_bcount;
    345 			goto done;
    346 		}
    347 		if (sz < 0) {
    348 			/* If past end of disk, return EINVAL. */
    349 			bp->b_error = EINVAL;
    350 			goto bad;
    351 		}
    352 		/* Otherwise, truncate request. */
    353 		bp->b_bcount = sz << DEV_BSHIFT;
    354 	}
    355 
    356 	return 1;
    357 
    358 bad:
    359 	bp->b_flags |= B_ERROR;
    360 done:
    361 	return 0;
    362 }
    363 
    364 /*
    365  * Determine the size of the transfer, and make sure it is
    366  * within the boundaries of the partition. Adjust transfer
    367  * if needed, and signal errors or early completion.
    368  */
    369 int
    370 bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
    371 {
    372 	struct disklabel *lp = dk->dk_label;
    373 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    374 	uint64_t p_size, p_offset, labelsector;
    375 	int64_t sz;
    376 
    377 	/* Protect against division by zero. XXX: Should never happen?!?! */
    378 	if (lp->d_secpercyl == 0) {
    379 		bp->b_error = EINVAL;
    380 		goto bad;
    381 	}
    382 
    383 	p_size = p->p_size << dk->dk_blkshift;
    384 	p_offset = p->p_offset << dk->dk_blkshift;
    385 #if RAW_PART == 3
    386 	labelsector = lp->d_partitions[2].p_offset;
    387 #else
    388 	labelsector = lp->d_partitions[RAW_PART].p_offset;
    389 #endif
    390 	labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
    391 
    392 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    393 	if ((bp->b_blkno + sz) > p_size) {
    394 		sz = p_size - bp->b_blkno;
    395 		if (sz == 0) {
    396 			/* If exactly at end of disk, return EOF. */
    397 			bp->b_resid = bp->b_bcount;
    398 			return (0);
    399 		}
    400 		if (sz < 0) {
    401 			/* If past end of disk, return EINVAL. */
    402 			bp->b_error = EINVAL;
    403 			goto bad;
    404 		}
    405 		/* Otherwise, truncate request. */
    406 		bp->b_bcount = sz << DEV_BSHIFT;
    407 	}
    408 
    409 	/* Overwriting disk label? */
    410 	if (bp->b_blkno + p_offset <= labelsector &&
    411 	    bp->b_blkno + p_offset + sz > labelsector &&
    412 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
    413 		bp->b_error = EROFS;
    414 		goto bad;
    415 	}
    416 
    417 	/* calculate cylinder for disksort to order transfers with */
    418 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
    419 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
    420 	return (1);
    421 
    422 bad:
    423 	bp->b_flags |= B_ERROR;
    424 	return (-1);
    425 }
    426 
    427 /*
    428  * disk_ioctl --
    429  *	Generic disk ioctl handling.
    430  */
    431 int
    432 disk_ioctl(struct disk *diskp, u_long cmd, caddr_t data, int flag,
    433 	   struct lwp *l)
    434 {
    435 	int error;
    436 
    437 	switch (cmd) {
    438 	case DIOCGDISKINFO:
    439 	    {
    440 		struct plistref *pref = (struct plistref *) data;
    441 
    442 		if (diskp->dk_info == NULL)
    443 			error = ENOTSUP;
    444 		else
    445 			error = prop_dictionary_copyout_ioctl(pref, cmd,
    446 							diskp->dk_info);
    447 		break;
    448 	    }
    449 
    450 	default:
    451 		error = EPASSTHROUGH;
    452 	}
    453 
    454 	return (error);
    455 }
    456