Home | History | Annotate | Line # | Download | only in kern
subr_disk.c revision 1.88.10.2
      1  1.88.10.2  ad /*	$NetBSD: subr_disk.c,v 1.88.10.2 2007/07/29 12:15:46 ad Exp $	*/
      2  1.88.10.2  ad 
      3  1.88.10.2  ad /*-
      4  1.88.10.2  ad  * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
      5  1.88.10.2  ad  * All rights reserved.
      6  1.88.10.2  ad  *
      7  1.88.10.2  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.88.10.2  ad  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.88.10.2  ad  * NASA Ames Research Center.
     10  1.88.10.2  ad  *
     11  1.88.10.2  ad  * Redistribution and use in source and binary forms, with or without
     12  1.88.10.2  ad  * modification, are permitted provided that the following conditions
     13  1.88.10.2  ad  * are met:
     14  1.88.10.2  ad  * 1. Redistributions of source code must retain the above copyright
     15  1.88.10.2  ad  *    notice, this list of conditions and the following disclaimer.
     16  1.88.10.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.88.10.2  ad  *    notice, this list of conditions and the following disclaimer in the
     18  1.88.10.2  ad  *    documentation and/or other materials provided with the distribution.
     19  1.88.10.2  ad  * 3. All advertising materials mentioning features or use of this software
     20  1.88.10.2  ad  *    must display the following acknowledgement:
     21  1.88.10.2  ad  *	This product includes software developed by the NetBSD
     22  1.88.10.2  ad  *	Foundation, Inc. and its contributors.
     23  1.88.10.2  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.88.10.2  ad  *    contributors may be used to endorse or promote products derived
     25  1.88.10.2  ad  *    from this software without specific prior written permission.
     26  1.88.10.2  ad  *
     27  1.88.10.2  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.88.10.2  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.88.10.2  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.88.10.2  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.88.10.2  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.88.10.2  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.88.10.2  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.88.10.2  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.88.10.2  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.88.10.2  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.88.10.2  ad  * POSSIBILITY OF SUCH DAMAGE.
     38  1.88.10.2  ad  */
     39  1.88.10.2  ad 
     40  1.88.10.2  ad /*
     41  1.88.10.2  ad  * Copyright (c) 1982, 1986, 1988, 1993
     42  1.88.10.2  ad  *	The Regents of the University of California.  All rights reserved.
     43  1.88.10.2  ad  * (c) UNIX System Laboratories, Inc.
     44  1.88.10.2  ad  * All or some portions of this file are derived from material licensed
     45  1.88.10.2  ad  * to the University of California by American Telephone and Telegraph
     46  1.88.10.2  ad  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     47  1.88.10.2  ad  * the permission of UNIX System Laboratories, Inc.
     48  1.88.10.2  ad  *
     49  1.88.10.2  ad  * Redistribution and use in source and binary forms, with or without
     50  1.88.10.2  ad  * modification, are permitted provided that the following conditions
     51  1.88.10.2  ad  * are met:
     52  1.88.10.2  ad  * 1. Redistributions of source code must retain the above copyright
     53  1.88.10.2  ad  *    notice, this list of conditions and the following disclaimer.
     54  1.88.10.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     55  1.88.10.2  ad  *    notice, this list of conditions and the following disclaimer in the
     56  1.88.10.2  ad  *    documentation and/or other materials provided with the distribution.
     57  1.88.10.2  ad  * 3. Neither the name of the University nor the names of its contributors
     58  1.88.10.2  ad  *    may be used to endorse or promote products derived from this software
     59  1.88.10.2  ad  *    without specific prior written permission.
     60  1.88.10.2  ad  *
     61  1.88.10.2  ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62  1.88.10.2  ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63  1.88.10.2  ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64  1.88.10.2  ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65  1.88.10.2  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66  1.88.10.2  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67  1.88.10.2  ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68  1.88.10.2  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69  1.88.10.2  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70  1.88.10.2  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71  1.88.10.2  ad  * SUCH DAMAGE.
     72  1.88.10.2  ad  *
     73  1.88.10.2  ad  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     74  1.88.10.2  ad  */
     75  1.88.10.2  ad 
     76  1.88.10.2  ad #include <sys/cdefs.h>
     77  1.88.10.2  ad __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.88.10.2 2007/07/29 12:15:46 ad Exp $");
     78  1.88.10.2  ad 
     79  1.88.10.2  ad #include <sys/param.h>
     80  1.88.10.2  ad #include <sys/kernel.h>
     81  1.88.10.2  ad #include <sys/malloc.h>
     82  1.88.10.2  ad #include <sys/buf.h>
     83  1.88.10.2  ad #include <sys/syslog.h>
     84  1.88.10.2  ad #include <sys/disklabel.h>
     85  1.88.10.2  ad #include <sys/disk.h>
     86  1.88.10.2  ad #include <sys/sysctl.h>
     87  1.88.10.2  ad #include <lib/libkern/libkern.h>
     88  1.88.10.2  ad 
     89  1.88.10.2  ad /*
     90  1.88.10.2  ad  * Compute checksum for disk label.
     91  1.88.10.2  ad  */
     92  1.88.10.2  ad u_int
     93  1.88.10.2  ad dkcksum(struct disklabel *lp)
     94  1.88.10.2  ad {
     95  1.88.10.2  ad 	return dkcksum_sized(lp, lp->d_npartitions);
     96  1.88.10.2  ad }
     97  1.88.10.2  ad 
     98  1.88.10.2  ad u_int
     99  1.88.10.2  ad dkcksum_sized(struct disklabel *lp, size_t npartitions)
    100  1.88.10.2  ad {
    101  1.88.10.2  ad 	u_short *start, *end;
    102  1.88.10.2  ad 	u_short sum = 0;
    103  1.88.10.2  ad 
    104  1.88.10.2  ad 	start = (u_short *)lp;
    105  1.88.10.2  ad 	end = (u_short *)&lp->d_partitions[npartitions];
    106  1.88.10.2  ad 	while (start < end)
    107  1.88.10.2  ad 		sum ^= *start++;
    108  1.88.10.2  ad 	return (sum);
    109  1.88.10.2  ad }
    110  1.88.10.2  ad 
    111  1.88.10.2  ad /*
    112  1.88.10.2  ad  * Disk error is the preface to plaintive error messages
    113  1.88.10.2  ad  * about failing disk transfers.  It prints messages of the form
    114  1.88.10.2  ad 
    115  1.88.10.2  ad hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
    116  1.88.10.2  ad 
    117  1.88.10.2  ad  * if the offset of the error in the transfer and a disk label
    118  1.88.10.2  ad  * are both available.  blkdone should be -1 if the position of the error
    119  1.88.10.2  ad  * is unknown; the disklabel pointer may be null from drivers that have not
    120  1.88.10.2  ad  * been converted to use them.  The message is printed with printf
    121  1.88.10.2  ad  * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
    122  1.88.10.2  ad  * The message should be completed (with at least a newline) with printf
    123  1.88.10.2  ad  * or addlog, respectively.  There is no trailing space.
    124  1.88.10.2  ad  */
    125  1.88.10.2  ad #ifndef PRIdaddr
    126  1.88.10.2  ad #define PRIdaddr PRId64
    127  1.88.10.2  ad #endif
    128  1.88.10.2  ad void
    129  1.88.10.2  ad diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
    130  1.88.10.2  ad     int blkdone, const struct disklabel *lp)
    131  1.88.10.2  ad {
    132  1.88.10.2  ad 	int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
    133  1.88.10.2  ad 	void (*pr)(const char *, ...);
    134  1.88.10.2  ad 	char partname = 'a' + part;
    135  1.88.10.2  ad 	daddr_t sn;
    136  1.88.10.2  ad 
    137  1.88.10.2  ad 	if (/*CONSTCOND*/0)
    138  1.88.10.2  ad 		/* Compiler will error this is the format is wrong... */
    139  1.88.10.2  ad 		printf("%" PRIdaddr, bp->b_blkno);
    140  1.88.10.2  ad 
    141  1.88.10.2  ad 	if (pri != LOG_PRINTF) {
    142  1.88.10.2  ad 		static const char fmt[] = "";
    143  1.88.10.2  ad 		log(pri, fmt);
    144  1.88.10.2  ad 		pr = addlog;
    145  1.88.10.2  ad 	} else
    146  1.88.10.2  ad 		pr = printf;
    147  1.88.10.2  ad 	(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
    148  1.88.10.2  ad 	    bp->b_flags & B_READ ? "read" : "writ");
    149  1.88.10.2  ad 	sn = bp->b_blkno;
    150  1.88.10.2  ad 	if (bp->b_bcount <= DEV_BSIZE)
    151  1.88.10.2  ad 		(*pr)("%" PRIdaddr, sn);
    152  1.88.10.2  ad 	else {
    153  1.88.10.2  ad 		if (blkdone >= 0) {
    154  1.88.10.2  ad 			sn += blkdone;
    155  1.88.10.2  ad 			(*pr)("%" PRIdaddr " of ", sn);
    156  1.88.10.2  ad 		}
    157  1.88.10.2  ad 		(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
    158  1.88.10.2  ad 		    bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
    159  1.88.10.2  ad 	}
    160  1.88.10.2  ad 	if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
    161  1.88.10.2  ad 		sn += lp->d_partitions[part].p_offset;
    162  1.88.10.2  ad 		(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
    163  1.88.10.2  ad 		    dname, unit, sn, sn / lp->d_secpercyl);
    164  1.88.10.2  ad 		sn %= lp->d_secpercyl;
    165  1.88.10.2  ad 		(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
    166  1.88.10.2  ad 		    sn / lp->d_nsectors, sn % lp->d_nsectors);
    167  1.88.10.2  ad 	}
    168  1.88.10.2  ad }
    169  1.88.10.2  ad 
    170  1.88.10.2  ad /*
    171  1.88.10.2  ad  * Searches the iostatlist for the disk corresponding to the
    172  1.88.10.2  ad  * name provided.
    173  1.88.10.2  ad  */
    174  1.88.10.2  ad struct disk *
    175  1.88.10.2  ad disk_find(const char *name)
    176  1.88.10.2  ad {
    177  1.88.10.2  ad 	struct io_stats *stat;
    178  1.88.10.2  ad 
    179  1.88.10.2  ad 	stat = iostat_find(name);
    180  1.88.10.2  ad 
    181  1.88.10.2  ad 	if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
    182  1.88.10.2  ad 		return stat->io_parent;
    183  1.88.10.2  ad 
    184  1.88.10.2  ad 	return (NULL);
    185  1.88.10.2  ad }
    186  1.88.10.2  ad 
    187  1.88.10.2  ad static void
    188  1.88.10.2  ad disk_init0(struct disk *diskp)
    189  1.88.10.2  ad {
    190  1.88.10.2  ad 
    191  1.88.10.2  ad 	/*
    192  1.88.10.2  ad 	 * Initialize the wedge-related locks and other fields.
    193  1.88.10.2  ad 	 */
    194  1.88.10.2  ad 	mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
    195  1.88.10.2  ad 	mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
    196  1.88.10.2  ad 	LIST_INIT(&diskp->dk_wedges);
    197  1.88.10.2  ad 	diskp->dk_nwedges = 0;
    198  1.88.10.2  ad 	diskp->dk_labelsector = LABELSECTOR;
    199  1.88.10.2  ad 	disk_blocksize(diskp, DEV_BSIZE);
    200  1.88.10.2  ad }
    201  1.88.10.2  ad 
    202  1.88.10.2  ad static void
    203  1.88.10.2  ad disk_attach0(struct disk *diskp)
    204  1.88.10.2  ad {
    205  1.88.10.2  ad 
    206  1.88.10.2  ad 	/*
    207  1.88.10.2  ad 	 * Allocate and initialize the disklabel structures.  Note that
    208  1.88.10.2  ad 	 * it's not safe to sleep here, since we're probably going to be
    209  1.88.10.2  ad 	 * called during autoconfiguration.
    210  1.88.10.2  ad 	 */
    211  1.88.10.2  ad 	diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
    212  1.88.10.2  ad 	diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
    213  1.88.10.2  ad 	    M_NOWAIT);
    214  1.88.10.2  ad 	if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
    215  1.88.10.2  ad 		panic("disk_attach: can't allocate storage for disklabel");
    216  1.88.10.2  ad 
    217  1.88.10.2  ad 	memset(diskp->dk_label, 0, sizeof(struct disklabel));
    218  1.88.10.2  ad 	memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    219  1.88.10.2  ad 
    220  1.88.10.2  ad 	/*
    221  1.88.10.2  ad 	 * Set up the stats collection.
    222  1.88.10.2  ad 	 */
    223  1.88.10.2  ad 	diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
    224  1.88.10.2  ad }
    225  1.88.10.2  ad 
    226  1.88.10.2  ad static void
    227  1.88.10.2  ad disk_detach0(struct disk *diskp)
    228  1.88.10.2  ad {
    229  1.88.10.2  ad 
    230  1.88.10.2  ad 	/*
    231  1.88.10.2  ad 	 * Remove from the drivelist.
    232  1.88.10.2  ad 	 */
    233  1.88.10.2  ad 	iostat_free(diskp->dk_stats);
    234  1.88.10.2  ad 
    235  1.88.10.2  ad 	/*
    236  1.88.10.2  ad 	 * Release the disk-info dictionary.
    237  1.88.10.2  ad 	 */
    238  1.88.10.2  ad 	if (diskp->dk_info) {
    239  1.88.10.2  ad 		prop_object_release(diskp->dk_info);
    240  1.88.10.2  ad 		diskp->dk_info = NULL;
    241  1.88.10.2  ad 	}
    242  1.88.10.2  ad 
    243  1.88.10.2  ad 	/*
    244  1.88.10.2  ad 	 * Free the space used by the disklabel structures.
    245  1.88.10.2  ad 	 */
    246  1.88.10.2  ad 	free(diskp->dk_label, M_DEVBUF);
    247  1.88.10.2  ad 	free(diskp->dk_cpulabel, M_DEVBUF);
    248  1.88.10.2  ad }
    249  1.88.10.2  ad 
    250  1.88.10.2  ad /*
    251  1.88.10.2  ad  * Attach a disk.
    252  1.88.10.2  ad  */
    253  1.88.10.2  ad void
    254  1.88.10.2  ad disk_attach(struct disk *diskp)
    255  1.88.10.2  ad {
    256  1.88.10.2  ad 
    257  1.88.10.2  ad 	disk_init0(diskp);
    258  1.88.10.2  ad 	disk_attach0(diskp);
    259  1.88.10.2  ad }
    260  1.88.10.2  ad 
    261  1.88.10.2  ad /*
    262  1.88.10.2  ad  * Detach a disk.
    263  1.88.10.2  ad  */
    264  1.88.10.2  ad void
    265  1.88.10.2  ad disk_detach(struct disk *diskp)
    266  1.88.10.2  ad {
    267  1.88.10.2  ad 
    268  1.88.10.2  ad 	mutex_destroy(&diskp->dk_openlock);
    269  1.88.10.2  ad 	mutex_destroy(&diskp->dk_rawlock);
    270  1.88.10.2  ad 	disk_detach0(diskp);
    271  1.88.10.2  ad }
    272  1.88.10.2  ad 
    273  1.88.10.2  ad /*
    274  1.88.10.2  ad  * Initialize a pseudo disk.
    275  1.88.10.2  ad  */
    276  1.88.10.2  ad void
    277  1.88.10.2  ad pseudo_disk_init(struct disk *diskp)
    278  1.88.10.2  ad {
    279  1.88.10.2  ad 
    280  1.88.10.2  ad 	disk_init0(diskp);
    281  1.88.10.2  ad }
    282  1.88.10.2  ad 
    283  1.88.10.2  ad /*
    284  1.88.10.2  ad  * Attach a pseudo disk.
    285  1.88.10.2  ad  */
    286  1.88.10.2  ad void
    287  1.88.10.2  ad pseudo_disk_attach(struct disk *diskp)
    288  1.88.10.2  ad {
    289  1.88.10.2  ad 
    290  1.88.10.2  ad 	disk_attach0(diskp);
    291  1.88.10.2  ad }
    292  1.88.10.2  ad 
    293  1.88.10.2  ad /*
    294  1.88.10.2  ad  * Detach a pseudo disk.
    295  1.88.10.2  ad  */
    296  1.88.10.2  ad void
    297  1.88.10.2  ad pseudo_disk_detach(struct disk *diskp)
    298  1.88.10.2  ad {
    299  1.88.10.2  ad 
    300  1.88.10.2  ad 	disk_detach0(diskp);
    301  1.88.10.2  ad }
    302  1.88.10.2  ad 
    303  1.88.10.2  ad /*
    304  1.88.10.2  ad  * Mark the disk as busy for metrics collection.
    305  1.88.10.2  ad  */
    306  1.88.10.2  ad void
    307  1.88.10.2  ad disk_busy(struct disk *diskp)
    308  1.88.10.2  ad {
    309  1.88.10.2  ad 
    310  1.88.10.2  ad 	iostat_busy(diskp->dk_stats);
    311  1.88.10.2  ad }
    312  1.88.10.2  ad 
    313  1.88.10.2  ad /*
    314  1.88.10.2  ad  * Finished disk operations, gather metrics.
    315  1.88.10.2  ad  */
    316  1.88.10.2  ad void
    317  1.88.10.2  ad disk_unbusy(struct disk *diskp, long bcount, int read)
    318  1.88.10.2  ad {
    319  1.88.10.2  ad 
    320  1.88.10.2  ad 	iostat_unbusy(diskp->dk_stats, bcount, read);
    321  1.88.10.2  ad }
    322  1.88.10.2  ad 
    323  1.88.10.2  ad /*
    324  1.88.10.2  ad  * Set the physical blocksize of a disk, in bytes.
    325  1.88.10.2  ad  * Only necessary if blocksize != DEV_BSIZE.
    326  1.88.10.2  ad  */
    327  1.88.10.2  ad void
    328  1.88.10.2  ad disk_blocksize(struct disk *diskp, int blocksize)
    329  1.88.10.2  ad {
    330  1.88.10.2  ad 
    331  1.88.10.2  ad 	diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
    332  1.88.10.2  ad 	diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
    333  1.88.10.2  ad }
    334  1.88.10.2  ad 
    335  1.88.10.2  ad /*
    336  1.88.10.2  ad  * Bounds checking against the media size, used for the raw partition.
    337  1.88.10.2  ad  * The sector size passed in should currently always be DEV_BSIZE,
    338  1.88.10.2  ad  * and the media size the size of the device in DEV_BSIZE sectors.
    339  1.88.10.2  ad  */
    340  1.88.10.2  ad int
    341  1.88.10.2  ad bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
    342  1.88.10.2  ad {
    343  1.88.10.2  ad 	int64_t sz;
    344  1.88.10.2  ad 
    345  1.88.10.2  ad 	sz = howmany(bp->b_bcount, secsize);
    346  1.88.10.2  ad 
    347  1.88.10.2  ad 	if (bp->b_blkno + sz > mediasize) {
    348  1.88.10.2  ad 		sz = mediasize - bp->b_blkno;
    349  1.88.10.2  ad 		if (sz == 0) {
    350  1.88.10.2  ad 			/* If exactly at end of disk, return EOF. */
    351  1.88.10.2  ad 			bp->b_resid = bp->b_bcount;
    352  1.88.10.2  ad 			return 0;
    353  1.88.10.2  ad 		}
    354  1.88.10.2  ad 		if (sz < 0) {
    355  1.88.10.2  ad 			/* If past end of disk, return EINVAL. */
    356  1.88.10.2  ad 			bp->b_error = EINVAL;
    357  1.88.10.2  ad 			return 0;
    358  1.88.10.2  ad 		}
    359  1.88.10.2  ad 		/* Otherwise, truncate request. */
    360  1.88.10.2  ad 		bp->b_bcount = sz << DEV_BSHIFT;
    361  1.88.10.2  ad 	}
    362  1.88.10.2  ad 
    363  1.88.10.2  ad 	return 1;
    364  1.88.10.2  ad }
    365  1.88.10.2  ad 
    366  1.88.10.2  ad /*
    367  1.88.10.2  ad  * Determine the size of the transfer, and make sure it is
    368  1.88.10.2  ad  * within the boundaries of the partition. Adjust transfer
    369  1.88.10.2  ad  * if needed, and signal errors or early completion.
    370  1.88.10.2  ad  */
    371  1.88.10.2  ad int
    372  1.88.10.2  ad bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
    373  1.88.10.2  ad {
    374  1.88.10.2  ad 	struct disklabel *lp = dk->dk_label;
    375  1.88.10.2  ad 	struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
    376  1.88.10.2  ad 	uint64_t p_size, p_offset, labelsector;
    377  1.88.10.2  ad 	int64_t sz;
    378  1.88.10.2  ad 
    379  1.88.10.2  ad 	/* Protect against division by zero. XXX: Should never happen?!?! */
    380  1.88.10.2  ad 	if (lp->d_secpercyl == 0) {
    381  1.88.10.2  ad 		bp->b_error = EINVAL;
    382  1.88.10.2  ad 		return -1;
    383  1.88.10.2  ad 	}
    384  1.88.10.2  ad 
    385  1.88.10.2  ad 	p_size = p->p_size << dk->dk_blkshift;
    386  1.88.10.2  ad 	p_offset = p->p_offset << dk->dk_blkshift;
    387  1.88.10.2  ad #if RAW_PART == 3
    388  1.88.10.2  ad 	labelsector = lp->d_partitions[2].p_offset;
    389  1.88.10.2  ad #else
    390  1.88.10.2  ad 	labelsector = lp->d_partitions[RAW_PART].p_offset;
    391  1.88.10.2  ad #endif
    392  1.88.10.2  ad 	labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
    393  1.88.10.2  ad 
    394  1.88.10.2  ad 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    395  1.88.10.2  ad 	if ((bp->b_blkno + sz) > p_size) {
    396  1.88.10.2  ad 		sz = p_size - bp->b_blkno;
    397  1.88.10.2  ad 		if (sz == 0) {
    398  1.88.10.2  ad 			/* If exactly at end of disk, return EOF. */
    399  1.88.10.2  ad 			bp->b_resid = bp->b_bcount;
    400  1.88.10.2  ad 			return 0;
    401  1.88.10.2  ad 		}
    402  1.88.10.2  ad 		if (sz < 0) {
    403  1.88.10.2  ad 			/* If past end of disk, return EINVAL. */
    404  1.88.10.2  ad 			bp->b_error = EINVAL;
    405  1.88.10.2  ad 			return -1;
    406  1.88.10.2  ad 		}
    407  1.88.10.2  ad 		/* Otherwise, truncate request. */
    408  1.88.10.2  ad 		bp->b_bcount = sz << DEV_BSHIFT;
    409  1.88.10.2  ad 	}
    410  1.88.10.2  ad 
    411  1.88.10.2  ad 	/* Overwriting disk label? */
    412  1.88.10.2  ad 	if (bp->b_blkno + p_offset <= labelsector &&
    413  1.88.10.2  ad 	    bp->b_blkno + p_offset + sz > labelsector &&
    414  1.88.10.2  ad 	    (bp->b_flags & B_READ) == 0 && !wlabel) {
    415  1.88.10.2  ad 		bp->b_error = EROFS;
    416  1.88.10.2  ad 		return -1;
    417  1.88.10.2  ad 	}
    418  1.88.10.2  ad 
    419  1.88.10.2  ad 	/* calculate cylinder for disksort to order transfers with */
    420  1.88.10.2  ad 	bp->b_cylinder = (bp->b_blkno + p->p_offset) /
    421  1.88.10.2  ad 	    (lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
    422  1.88.10.2  ad 	return 1;
    423  1.88.10.2  ad }
    424  1.88.10.2  ad 
    425  1.88.10.2  ad int
    426  1.88.10.2  ad disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
    427  1.88.10.2  ad     struct buf *bp, unsigned int sector, int count)
    428  1.88.10.2  ad {
    429  1.88.10.2  ad 	bp->b_blkno = sector;
    430  1.88.10.2  ad 	bp->b_bcount = count * lp->d_secsize;
    431  1.88.10.2  ad 	bp->b_flags = (bp->b_flags & ~(B_WRITE | B_DONE)) | B_READ;
    432  1.88.10.2  ad 	bp->b_cylinder = sector / lp->d_secpercyl;
    433  1.88.10.2  ad 	(*strat)(bp);
    434  1.88.10.2  ad 	return biowait(bp);
    435  1.88.10.2  ad }
    436  1.88.10.2  ad 
    437  1.88.10.2  ad const char *
    438  1.88.10.2  ad convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
    439  1.88.10.2  ad     struct buf *bp, uint32_t secperunit)
    440  1.88.10.2  ad {
    441  1.88.10.2  ad 	struct partition rp, *altp, *p;
    442  1.88.10.2  ad 	int geom_ok;
    443  1.88.10.2  ad 
    444  1.88.10.2  ad 	memset(&rp, 0, sizeof(rp));
    445  1.88.10.2  ad 	rp.p_size = secperunit;
    446  1.88.10.2  ad 	rp.p_fstype = FS_UNUSED;
    447  1.88.10.2  ad 
    448  1.88.10.2  ad 	/* If we can seek to d_secperunit - 1, believe the disk geometry. */
    449  1.88.10.2  ad 	if (secperunit != 0 &&
    450  1.88.10.2  ad 	    disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
    451  1.88.10.2  ad 		geom_ok = 1;
    452  1.88.10.2  ad 	else
    453  1.88.10.2  ad 		geom_ok = 0;
    454  1.88.10.2  ad 
    455  1.88.10.2  ad #if 0
    456  1.88.10.2  ad 	printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
    457  1.88.10.2  ad 	    secperunit, geom_ok ? "ok" : "not ok");
    458  1.88.10.2  ad #endif
    459  1.88.10.2  ad 
    460  1.88.10.2  ad 	p = &lp->d_partitions[RAW_PART];
    461  1.88.10.2  ad 	if (RAW_PART == 'c' - 'a')
    462  1.88.10.2  ad 		altp = &lp->d_partitions['d' - 'a'];
    463  1.88.10.2  ad 	else
    464  1.88.10.2  ad 		altp = &lp->d_partitions['c' - 'a'];
    465  1.88.10.2  ad 
    466  1.88.10.2  ad 	if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
    467  1.88.10.2  ad 		;	/* already a raw partition */
    468  1.88.10.2  ad 	else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
    469  1.88.10.2  ad 		 altp->p_offset == 0 && altp->p_size != 0) {
    470  1.88.10.2  ad 		/* alternate partition ('c' or 'd') is suitable for raw slot,
    471  1.88.10.2  ad 		 * swap with 'd' or 'c'.
    472  1.88.10.2  ad 		 */
    473  1.88.10.2  ad 		rp = *p;
    474  1.88.10.2  ad 		*p = *altp;
    475  1.88.10.2  ad 		*altp = rp;
    476  1.88.10.2  ad 	} else if (lp->d_npartitions <= RAW_PART &&
    477  1.88.10.2  ad 	           lp->d_npartitions > 'c' - 'a') {
    478  1.88.10.2  ad 		/* No raw partition is present, but the alternate is present.
    479  1.88.10.2  ad 		 * Copy alternate to raw partition.
    480  1.88.10.2  ad 		 */
    481  1.88.10.2  ad 		lp->d_npartitions = RAW_PART + 1;
    482  1.88.10.2  ad 		*p = *altp;
    483  1.88.10.2  ad 	} else if (!geom_ok)
    484  1.88.10.2  ad 		return "no raw partition and disk reports bad geometry";
    485  1.88.10.2  ad 	else if (lp->d_npartitions <= RAW_PART) {
    486  1.88.10.2  ad 		memset(&lp->d_partitions[lp->d_npartitions], 0,
    487  1.88.10.2  ad 		    sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
    488  1.88.10.2  ad 		*p = rp;
    489  1.88.10.2  ad 		lp->d_npartitions = RAW_PART + 1;
    490  1.88.10.2  ad 	} else if (lp->d_npartitions < MAXPARTITIONS) {
    491  1.88.10.2  ad 		memmove(p + 1, p,
    492  1.88.10.2  ad 		    sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
    493  1.88.10.2  ad 		*p = rp;
    494  1.88.10.2  ad 		lp->d_npartitions++;
    495  1.88.10.2  ad 	} else
    496  1.88.10.2  ad 		return "no raw partition and partition table is full";
    497  1.88.10.2  ad 	return NULL;
    498  1.88.10.2  ad }
    499  1.88.10.2  ad 
    500  1.88.10.2  ad /*
    501  1.88.10.2  ad  * disk_ioctl --
    502  1.88.10.2  ad  *	Generic disk ioctl handling.
    503  1.88.10.2  ad  */
    504  1.88.10.2  ad int
    505  1.88.10.2  ad disk_ioctl(struct disk *diskp, u_long cmd, void *data, int flag,
    506  1.88.10.2  ad 	   struct lwp *l)
    507  1.88.10.2  ad {
    508  1.88.10.2  ad 	int error;
    509  1.88.10.2  ad 
    510  1.88.10.2  ad 	switch (cmd) {
    511  1.88.10.2  ad 	case DIOCGDISKINFO:
    512  1.88.10.2  ad 	    {
    513  1.88.10.2  ad 		struct plistref *pref = (struct plistref *) data;
    514  1.88.10.2  ad 
    515  1.88.10.2  ad 		if (diskp->dk_info == NULL)
    516  1.88.10.2  ad 			error = ENOTSUP;
    517  1.88.10.2  ad 		else
    518  1.88.10.2  ad 			error = prop_dictionary_copyout_ioctl(pref, cmd,
    519  1.88.10.2  ad 							diskp->dk_info);
    520  1.88.10.2  ad 		break;
    521  1.88.10.2  ad 	    }
    522  1.88.10.2  ad 
    523  1.88.10.2  ad 	default:
    524  1.88.10.2  ad 		error = EPASSTHROUGH;
    525  1.88.10.2  ad 	}
    526  1.88.10.2  ad 
    527  1.88.10.2  ad 	return (error);
    528  1.88.10.2  ad }
    529