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