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