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