2008-04-29 00:22:51 +04:00
|
|
|
/* $NetBSD: subr_disk.c,v 1.93 2008/04/28 20:24:04 martin Exp $ */
|
1997-10-05 22:37:01 +04:00
|
|
|
|
|
|
|
/*-
|
2000-01-22 02:20:51 +03:00
|
|
|
* Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
|
1997-10-05 22:37:01 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
|
|
|
* NASA Ames Research Center.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1994-05-19 07:43:13 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1988, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-19 07:43:13 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-06-29 10:29:24 +04:00
|
|
|
* @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
|
1994-05-19 07:43:13 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-13 16:33:44 +03:00
|
|
|
#include <sys/cdefs.h>
|
2008-04-29 00:22:51 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.93 2008/04/28 20:24:04 martin Exp $");
|
2001-11-13 16:33:44 +03:00
|
|
|
|
1994-05-19 07:43:13 +04:00
|
|
|
#include <sys/param.h>
|
1996-01-08 01:01:38 +03:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
1994-05-19 07:43:13 +04:00
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/syslog.h>
|
1996-01-08 01:01:38 +03:00
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/disk.h>
|
2002-01-27 15:41:07 +03:00
|
|
|
#include <sys/sysctl.h>
|
2002-11-04 06:50:07 +03:00
|
|
|
#include <lib/libkern/libkern.h>
|
1995-12-28 22:16:31 +03:00
|
|
|
|
1994-05-19 07:43:13 +04:00
|
|
|
/*
|
|
|
|
* Compute checksum for disk label.
|
|
|
|
*/
|
|
|
|
u_int
|
2001-07-09 14:54:12 +04:00
|
|
|
dkcksum(struct disklabel *lp)
|
2007-03-02 00:30:50 +03:00
|
|
|
{
|
|
|
|
return dkcksum_sized(lp, lp->d_npartitions);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
|
|
|
dkcksum_sized(struct disklabel *lp, size_t npartitions)
|
1994-05-19 07:43:13 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
u_short *start, *end;
|
|
|
|
u_short sum = 0;
|
1994-05-19 07:43:13 +04:00
|
|
|
|
|
|
|
start = (u_short *)lp;
|
2007-03-02 00:30:50 +03:00
|
|
|
end = (u_short *)&lp->d_partitions[npartitions];
|
1994-05-19 07:43:13 +04:00
|
|
|
while (start < end)
|
|
|
|
sum ^= *start++;
|
|
|
|
return (sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disk error is the preface to plaintive error messages
|
|
|
|
* about failing disk transfers. It prints messages of the form
|
|
|
|
|
|
|
|
hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
|
|
|
|
|
|
|
|
* if the offset of the error in the transfer and a disk label
|
|
|
|
* are both available. blkdone should be -1 if the position of the error
|
|
|
|
* is unknown; the disklabel pointer may be null from drivers that have not
|
1996-10-13 06:32:29 +04:00
|
|
|
* been converted to use them. The message is printed with printf
|
1994-05-19 07:43:13 +04:00
|
|
|
* if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
|
1996-10-13 06:32:29 +04:00
|
|
|
* The message should be completed (with at least a newline) with printf
|
1994-05-19 07:43:13 +04:00
|
|
|
* or addlog, respectively. There is no trailing space.
|
|
|
|
*/
|
2003-04-13 11:51:30 +04:00
|
|
|
#ifndef PRIdaddr
|
|
|
|
#define PRIdaddr PRId64
|
|
|
|
#endif
|
1994-05-19 07:43:13 +04:00
|
|
|
void
|
2002-06-28 20:37:20 +04:00
|
|
|
diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
|
|
|
|
int blkdone, const struct disklabel *lp)
|
1994-05-19 07:43:13 +04:00
|
|
|
{
|
1999-02-22 19:00:01 +03:00
|
|
|
int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
|
2001-07-09 14:54:12 +04:00
|
|
|
void (*pr)(const char *, ...);
|
1994-05-19 07:43:13 +04:00
|
|
|
char partname = 'a' + part;
|
2003-04-13 11:51:30 +04:00
|
|
|
daddr_t sn;
|
|
|
|
|
2003-04-13 13:08:04 +04:00
|
|
|
if (/*CONSTCOND*/0)
|
2003-04-13 11:51:30 +04:00
|
|
|
/* Compiler will error this is the format is wrong... */
|
|
|
|
printf("%" PRIdaddr, bp->b_blkno);
|
1994-05-19 07:43:13 +04:00
|
|
|
|
|
|
|
if (pri != LOG_PRINTF) {
|
1996-03-17 02:17:04 +03:00
|
|
|
static const char fmt[] = "";
|
|
|
|
log(pri, fmt);
|
1994-05-19 07:43:13 +04:00
|
|
|
pr = addlog;
|
|
|
|
} else
|
1996-10-13 06:32:29 +04:00
|
|
|
pr = printf;
|
1994-05-19 07:43:13 +04:00
|
|
|
(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
|
|
|
|
bp->b_flags & B_READ ? "read" : "writ");
|
|
|
|
sn = bp->b_blkno;
|
|
|
|
if (bp->b_bcount <= DEV_BSIZE)
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)("%" PRIdaddr, sn);
|
1994-05-19 07:43:13 +04:00
|
|
|
else {
|
|
|
|
if (blkdone >= 0) {
|
|
|
|
sn += blkdone;
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)("%" PRIdaddr " of ", sn);
|
1994-05-19 07:43:13 +04:00
|
|
|
}
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
|
1994-05-19 07:43:13 +04:00
|
|
|
bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
|
|
|
|
}
|
|
|
|
if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
|
|
|
|
sn += lp->d_partitions[part].p_offset;
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
|
|
|
|
dname, unit, sn, sn / lp->d_secpercyl);
|
1994-05-19 07:43:13 +04:00
|
|
|
sn %= lp->d_secpercyl;
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
|
|
|
|
sn / lp->d_nsectors, sn % lp->d_nsectors);
|
1994-05-19 07:43:13 +04:00
|
|
|
}
|
|
|
|
}
|
1996-01-08 01:01:38 +03:00
|
|
|
|
|
|
|
/*
|
2006-04-14 17:09:05 +04:00
|
|
|
* Searches the iostatlist for the disk corresponding to the
|
1996-01-08 01:01:38 +03:00
|
|
|
* name provided.
|
|
|
|
*/
|
|
|
|
struct disk *
|
2006-04-21 17:53:30 +04:00
|
|
|
disk_find(const char *name)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
2006-04-14 17:09:05 +04:00
|
|
|
struct io_stats *stat;
|
1996-01-08 01:01:38 +03:00
|
|
|
|
2006-04-14 17:09:05 +04:00
|
|
|
stat = iostat_find(name);
|
1996-01-08 01:01:38 +03:00
|
|
|
|
2006-04-20 16:13:51 +04:00
|
|
|
if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
|
|
|
|
return stat->io_parent;
|
1996-01-08 01:01:38 +03:00
|
|
|
|
2006-04-21 17:48:57 +04:00
|
|
|
return (NULL);
|
1996-01-08 01:01:38 +03:00
|
|
|
}
|
|
|
|
|
2007-10-08 20:41:05 +04:00
|
|
|
void
|
2008-02-28 07:55:47 +03:00
|
|
|
disk_init(struct disk *diskp, const char *name, const struct dkdriver *driver)
|
2005-08-20 16:00:01 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the wedge-related locks and other fields.
|
|
|
|
*/
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_init(&diskp->dk_rawlock, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
mutex_init(&diskp->dk_openlock, MUTEX_DEFAULT, IPL_NONE);
|
2005-08-20 16:00:01 +04:00
|
|
|
LIST_INIT(&diskp->dk_wedges);
|
|
|
|
diskp->dk_nwedges = 0;
|
2006-11-25 14:59:55 +03:00
|
|
|
diskp->dk_labelsector = LABELSECTOR;
|
|
|
|
disk_blocksize(diskp, DEV_BSIZE);
|
2007-10-08 20:41:05 +04:00
|
|
|
diskp->dk_name = name;
|
|
|
|
diskp->dk_driver = driver;
|
2005-08-20 16:00:01 +04:00
|
|
|
}
|
|
|
|
|
2007-10-08 20:41:05 +04:00
|
|
|
/*
|
|
|
|
* Attach a disk.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
disk_attach(struct disk *diskp)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
2006-06-08 02:33:33 +04:00
|
|
|
|
1996-01-08 01:01:38 +03:00
|
|
|
/*
|
|
|
|
* Allocate and initialize the disklabel structures. Note that
|
|
|
|
* it's not safe to sleep here, since we're probably going to be
|
|
|
|
* called during autoconfiguration.
|
|
|
|
*/
|
|
|
|
diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
|
|
|
|
diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
|
|
|
|
M_NOWAIT);
|
|
|
|
if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
|
|
|
|
panic("disk_attach: can't allocate storage for disklabel");
|
|
|
|
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memset(diskp->dk_label, 0, sizeof(struct disklabel));
|
|
|
|
memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
|
1996-01-08 01:01:38 +03:00
|
|
|
|
|
|
|
/*
|
2006-04-14 17:09:05 +04:00
|
|
|
* Set up the stats collection.
|
1996-01-08 01:01:38 +03:00
|
|
|
*/
|
2006-08-23 21:19:32 +04:00
|
|
|
diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
|
1996-01-08 01:01:38 +03:00
|
|
|
}
|
|
|
|
|
2007-10-08 20:41:05 +04:00
|
|
|
/*
|
|
|
|
* Detach a disk.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
disk_detach(struct disk *diskp)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
2006-04-14 17:09:05 +04:00
|
|
|
* Remove from the drivelist.
|
1996-01-08 01:01:38 +03:00
|
|
|
*/
|
2006-04-21 17:48:57 +04:00
|
|
|
iostat_free(diskp->dk_stats);
|
1997-12-30 12:51:24 +03:00
|
|
|
|
2006-09-22 08:48:38 +04:00
|
|
|
/*
|
|
|
|
* Release the disk-info dictionary.
|
|
|
|
*/
|
|
|
|
if (diskp->dk_info) {
|
|
|
|
prop_object_release(diskp->dk_info);
|
|
|
|
diskp->dk_info = NULL;
|
|
|
|
}
|
|
|
|
|
1997-12-30 12:51:24 +03:00
|
|
|
/*
|
|
|
|
* Free the space used by the disklabel structures.
|
|
|
|
*/
|
|
|
|
free(diskp->dk_label, M_DEVBUF);
|
|
|
|
free(diskp->dk_cpulabel, M_DEVBUF);
|
1996-01-08 01:01:38 +03:00
|
|
|
}
|
|
|
|
|
2005-08-20 16:00:01 +04:00
|
|
|
void
|
2007-10-08 20:41:05 +04:00
|
|
|
disk_destroy(struct disk *diskp)
|
2005-08-20 16:00:01 +04:00
|
|
|
{
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_destroy(&diskp->dk_openlock);
|
|
|
|
mutex_destroy(&diskp->dk_rawlock);
|
2005-08-20 16:00:01 +04:00
|
|
|
}
|
|
|
|
|
1996-01-08 01:01:38 +03:00
|
|
|
/*
|
2006-04-14 17:09:05 +04:00
|
|
|
* Mark the disk as busy for metrics collection.
|
1996-01-08 01:01:38 +03:00
|
|
|
*/
|
|
|
|
void
|
2001-07-09 14:54:12 +04:00
|
|
|
disk_busy(struct disk *diskp)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
2006-04-21 17:48:57 +04:00
|
|
|
|
2006-04-14 17:09:05 +04:00
|
|
|
iostat_busy(diskp->dk_stats);
|
1996-01-08 01:01:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-04-14 17:09:05 +04:00
|
|
|
* Finished disk operations, gather metrics.
|
1996-01-08 01:01:38 +03:00
|
|
|
*/
|
|
|
|
void
|
2002-11-01 14:31:50 +03:00
|
|
|
disk_unbusy(struct disk *diskp, long bcount, int read)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
2006-04-21 17:48:57 +04:00
|
|
|
|
2006-04-14 17:09:05 +04:00
|
|
|
iostat_unbusy(diskp->dk_stats, bcount, read);
|
2002-01-27 15:41:07 +03:00
|
|
|
}
|
2002-07-16 22:03:17 +04:00
|
|
|
|
2006-11-25 14:59:55 +03:00
|
|
|
/*
|
|
|
|
* Set the physical blocksize of a disk, in bytes.
|
|
|
|
* Only necessary if blocksize != DEV_BSIZE.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
disk_blocksize(struct disk *diskp, int blocksize)
|
|
|
|
{
|
|
|
|
|
|
|
|
diskp->dk_blkshift = DK_BSIZE2BLKSHIFT(blocksize);
|
|
|
|
diskp->dk_byteshift = DK_BSIZE2BYTESHIFT(blocksize);
|
|
|
|
}
|
|
|
|
|
2003-04-04 02:20:24 +04:00
|
|
|
/*
|
|
|
|
* Bounds checking against the media size, used for the raw partition.
|
|
|
|
* The sector size passed in should currently always be DEV_BSIZE,
|
|
|
|
* and the media size the size of the device in DEV_BSIZE sectors.
|
|
|
|
*/
|
|
|
|
int
|
2005-12-26 21:41:36 +03:00
|
|
|
bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
|
2003-04-04 02:20:24 +04:00
|
|
|
{
|
2005-02-08 11:56:21 +03:00
|
|
|
int64_t sz;
|
2003-04-04 02:20:24 +04:00
|
|
|
|
|
|
|
sz = howmany(bp->b_bcount, secsize);
|
|
|
|
|
|
|
|
if (bp->b_blkno + sz > mediasize) {
|
|
|
|
sz = mediasize - bp->b_blkno;
|
|
|
|
if (sz == 0) {
|
|
|
|
/* If exactly at end of disk, return EOF. */
|
|
|
|
bp->b_resid = bp->b_bcount;
|
2007-07-29 16:15:35 +04:00
|
|
|
return 0;
|
2003-04-04 02:20:24 +04:00
|
|
|
}
|
|
|
|
if (sz < 0) {
|
|
|
|
/* If past end of disk, return EINVAL. */
|
|
|
|
bp->b_error = EINVAL;
|
2007-07-29 16:15:35 +04:00
|
|
|
return 0;
|
2003-04-04 02:20:24 +04:00
|
|
|
}
|
|
|
|
/* Otherwise, truncate request. */
|
|
|
|
bp->b_bcount = sz << DEV_BSHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2006-10-25 08:04:45 +04:00
|
|
|
|
2006-11-25 14:59:55 +03:00
|
|
|
/*
|
|
|
|
* Determine the size of the transfer, and make sure it is
|
|
|
|
* within the boundaries of the partition. Adjust transfer
|
|
|
|
* if needed, and signal errors or early completion.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
bounds_check_with_label(struct disk *dk, struct buf *bp, int wlabel)
|
|
|
|
{
|
|
|
|
struct disklabel *lp = dk->dk_label;
|
|
|
|
struct partition *p = lp->d_partitions + DISKPART(bp->b_dev);
|
|
|
|
uint64_t p_size, p_offset, labelsector;
|
|
|
|
int64_t sz;
|
|
|
|
|
|
|
|
/* Protect against division by zero. XXX: Should never happen?!?! */
|
|
|
|
if (lp->d_secpercyl == 0) {
|
|
|
|
bp->b_error = EINVAL;
|
2007-07-29 16:15:35 +04:00
|
|
|
return -1;
|
2006-11-25 14:59:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p_size = p->p_size << dk->dk_blkshift;
|
|
|
|
p_offset = p->p_offset << dk->dk_blkshift;
|
|
|
|
#if RAW_PART == 3
|
|
|
|
labelsector = lp->d_partitions[2].p_offset;
|
|
|
|
#else
|
|
|
|
labelsector = lp->d_partitions[RAW_PART].p_offset;
|
|
|
|
#endif
|
|
|
|
labelsector = (labelsector + dk->dk_labelsector) << dk->dk_blkshift;
|
|
|
|
|
|
|
|
sz = howmany(bp->b_bcount, DEV_BSIZE);
|
|
|
|
if ((bp->b_blkno + sz) > p_size) {
|
|
|
|
sz = p_size - bp->b_blkno;
|
|
|
|
if (sz == 0) {
|
|
|
|
/* If exactly at end of disk, return EOF. */
|
|
|
|
bp->b_resid = bp->b_bcount;
|
2007-07-29 16:15:35 +04:00
|
|
|
return 0;
|
2006-11-25 14:59:55 +03:00
|
|
|
}
|
|
|
|
if (sz < 0) {
|
|
|
|
/* If past end of disk, return EINVAL. */
|
|
|
|
bp->b_error = EINVAL;
|
2007-07-29 16:15:35 +04:00
|
|
|
return -1;
|
2006-11-25 14:59:55 +03:00
|
|
|
}
|
|
|
|
/* Otherwise, truncate request. */
|
|
|
|
bp->b_bcount = sz << DEV_BSHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Overwriting disk label? */
|
|
|
|
if (bp->b_blkno + p_offset <= labelsector &&
|
|
|
|
bp->b_blkno + p_offset + sz > labelsector &&
|
|
|
|
(bp->b_flags & B_READ) == 0 && !wlabel) {
|
|
|
|
bp->b_error = EROFS;
|
2007-07-29 16:15:35 +04:00
|
|
|
return -1;
|
2006-11-25 14:59:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate cylinder for disksort to order transfers with */
|
|
|
|
bp->b_cylinder = (bp->b_blkno + p->p_offset) /
|
|
|
|
(lp->d_secsize / DEV_BSIZE) / lp->d_secpercyl;
|
2007-07-29 16:15:35 +04:00
|
|
|
return 1;
|
2006-11-25 14:59:55 +03:00
|
|
|
}
|
|
|
|
|
Extract common code from i386, xen, and sparc64, creating
config_handle_wedges() and read_disk_sectors(). On x86, handle_wedges()
is a thin wrapper for config_handle_wedges(). Share opendisk()
across architectures.
Add kernel code in support of specifying a root partition by wedge
name. E.g., root specifications "wedge:wd0a", "wedge:David's Root
Volume" are possible. (Patches for config(1) coming soon.)
In support of moving disks between architectures (esp. i386 <->
evbmips), I've written a routine convertdisklabel() that ensures
that the raw partition is at RAW_DISK by following these steps:
0 If we have read a disklabel that has a RAW_PART with
p_offset == 0 and p_size != 0, then use that raw partition.
1 If we have read a disklabel that has both partitions 'c'
and 'd', and RAW_PART has p_offset != 0 or p_size == 0,
but the other partition is suitable for a raw partition
(p_offset == 0, p_size != 0), then swap the two partitions
and use the new raw partition.
2 If the architecture's raw partition is 'd', and if there
is no partition 'd', but there is a partition 'c' that
is suitable for a raw partition, then copy partition 'c'
to partition 'd'.
3 Determine the drive's last sector, using either the
d_secperunit the drive reported, or by guessing (0x1fffffff).
If we cannot read the drive's last sector, then fail.
4 If we have read a disklabel that has no partition slot
RAW_PART, then create a partition RAW_PART. Make it span
the whole drive.
5 If there are fewer than MAXPARTITIONS partitions,
then "slide" the unsuitable raw partition RAW_PART, and
subsequent partitions, into partition slots RAW_PART+1
and subsequent slots. Create a raw partition at RAW_PART.
Make it span the whole drive.
The convertdisklabel() procedure can probably stand to be simplified,
but it ought to deal with all but an extraordinarily broken disklabel,
now.
i386: compiled and tested, sparc64: compiled, evbmips: compiled.
2007-06-24 05:43:34 +04:00
|
|
|
int
|
|
|
|
disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
|
|
|
|
struct buf *bp, unsigned int sector, int count)
|
|
|
|
{
|
|
|
|
bp->b_blkno = sector;
|
|
|
|
bp->b_bcount = count * lp->d_secsize;
|
2008-01-02 14:48:20 +03:00
|
|
|
bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;
|
|
|
|
bp->b_oflags &= ~BO_DONE;
|
Extract common code from i386, xen, and sparc64, creating
config_handle_wedges() and read_disk_sectors(). On x86, handle_wedges()
is a thin wrapper for config_handle_wedges(). Share opendisk()
across architectures.
Add kernel code in support of specifying a root partition by wedge
name. E.g., root specifications "wedge:wd0a", "wedge:David's Root
Volume" are possible. (Patches for config(1) coming soon.)
In support of moving disks between architectures (esp. i386 <->
evbmips), I've written a routine convertdisklabel() that ensures
that the raw partition is at RAW_DISK by following these steps:
0 If we have read a disklabel that has a RAW_PART with
p_offset == 0 and p_size != 0, then use that raw partition.
1 If we have read a disklabel that has both partitions 'c'
and 'd', and RAW_PART has p_offset != 0 or p_size == 0,
but the other partition is suitable for a raw partition
(p_offset == 0, p_size != 0), then swap the two partitions
and use the new raw partition.
2 If the architecture's raw partition is 'd', and if there
is no partition 'd', but there is a partition 'c' that
is suitable for a raw partition, then copy partition 'c'
to partition 'd'.
3 Determine the drive's last sector, using either the
d_secperunit the drive reported, or by guessing (0x1fffffff).
If we cannot read the drive's last sector, then fail.
4 If we have read a disklabel that has no partition slot
RAW_PART, then create a partition RAW_PART. Make it span
the whole drive.
5 If there are fewer than MAXPARTITIONS partitions,
then "slide" the unsuitable raw partition RAW_PART, and
subsequent partitions, into partition slots RAW_PART+1
and subsequent slots. Create a raw partition at RAW_PART.
Make it span the whole drive.
The convertdisklabel() procedure can probably stand to be simplified,
but it ought to deal with all but an extraordinarily broken disklabel,
now.
i386: compiled and tested, sparc64: compiled, evbmips: compiled.
2007-06-24 05:43:34 +04:00
|
|
|
bp->b_cylinder = sector / lp->d_secpercyl;
|
|
|
|
(*strat)(bp);
|
|
|
|
return biowait(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
convertdisklabel(struct disklabel *lp, void (*strat)(struct buf *),
|
|
|
|
struct buf *bp, uint32_t secperunit)
|
|
|
|
{
|
|
|
|
struct partition rp, *altp, *p;
|
|
|
|
int geom_ok;
|
|
|
|
|
|
|
|
memset(&rp, 0, sizeof(rp));
|
|
|
|
rp.p_size = secperunit;
|
|
|
|
rp.p_fstype = FS_UNUSED;
|
|
|
|
|
|
|
|
/* If we can seek to d_secperunit - 1, believe the disk geometry. */
|
|
|
|
if (secperunit != 0 &&
|
|
|
|
disk_read_sectors(strat, lp, bp, secperunit - 1, 1) == 0)
|
|
|
|
geom_ok = 1;
|
|
|
|
else
|
|
|
|
geom_ok = 0;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("%s: secperunit (%" PRIu32 ") %s\n", __func__,
|
|
|
|
secperunit, geom_ok ? "ok" : "not ok");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p = &lp->d_partitions[RAW_PART];
|
|
|
|
if (RAW_PART == 'c' - 'a')
|
|
|
|
altp = &lp->d_partitions['d' - 'a'];
|
|
|
|
else
|
|
|
|
altp = &lp->d_partitions['c' - 'a'];
|
|
|
|
|
|
|
|
if (lp->d_npartitions > RAW_PART && p->p_offset == 0 && p->p_size != 0)
|
|
|
|
; /* already a raw partition */
|
|
|
|
else if (lp->d_npartitions > MAX('c', 'd') - 'a' &&
|
|
|
|
altp->p_offset == 0 && altp->p_size != 0) {
|
|
|
|
/* alternate partition ('c' or 'd') is suitable for raw slot,
|
|
|
|
* swap with 'd' or 'c'.
|
|
|
|
*/
|
|
|
|
rp = *p;
|
|
|
|
*p = *altp;
|
|
|
|
*altp = rp;
|
|
|
|
} else if (lp->d_npartitions <= RAW_PART &&
|
|
|
|
lp->d_npartitions > 'c' - 'a') {
|
|
|
|
/* No raw partition is present, but the alternate is present.
|
|
|
|
* Copy alternate to raw partition.
|
|
|
|
*/
|
|
|
|
lp->d_npartitions = RAW_PART + 1;
|
|
|
|
*p = *altp;
|
|
|
|
} else if (!geom_ok)
|
|
|
|
return "no raw partition and disk reports bad geometry";
|
|
|
|
else if (lp->d_npartitions <= RAW_PART) {
|
|
|
|
memset(&lp->d_partitions[lp->d_npartitions], 0,
|
|
|
|
sizeof(struct partition) * (RAW_PART - lp->d_npartitions));
|
|
|
|
*p = rp;
|
|
|
|
lp->d_npartitions = RAW_PART + 1;
|
|
|
|
} else if (lp->d_npartitions < MAXPARTITIONS) {
|
|
|
|
memmove(p + 1, p,
|
|
|
|
sizeof(struct partition) * (lp->d_npartitions - RAW_PART));
|
|
|
|
*p = rp;
|
|
|
|
lp->d_npartitions++;
|
|
|
|
} else
|
|
|
|
return "no raw partition and partition table is full";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-10-25 08:04:45 +04:00
|
|
|
/*
|
|
|
|
* disk_ioctl --
|
|
|
|
* Generic disk ioctl handling.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-04 08:59:00 +03:00
|
|
|
disk_ioctl(struct disk *diskp, u_long cmd, void *data, int flag,
|
2006-10-25 08:04:45 +04:00
|
|
|
struct lwp *l)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case DIOCGDISKINFO:
|
|
|
|
{
|
|
|
|
struct plistref *pref = (struct plistref *) data;
|
|
|
|
|
|
|
|
if (diskp->dk_info == NULL)
|
|
|
|
error = ENOTSUP;
|
|
|
|
else
|
|
|
|
error = prop_dictionary_copyout_ioctl(pref, cmd,
|
|
|
|
diskp->dk_info);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EPASSTHROUGH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|