/*-
* Copyright (c) 1996, 1997, 1999, 2000, 2009 The NetBSD Foundation, Inc.
* 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.
*/
/*
* 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.
* 3. Neither the name of the University nor the names of its contributors
* 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.
*
* @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
*/
/*
* 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
* been converted to use them. The message is printed with printf
* if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
* The message should be completed (with at least a newline) with printf
* or addlog, respectively. There is no trailing space.
*/
#ifndef PRIdaddr
#define PRIdaddr PRId64
#endif
void
diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
int blkdone, const struct disklabel *lp)
{
int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
void (*pr)(const char *, ...) __printflike(1, 2);
char partname = 'a' + part;
daddr_t sn;
if (/*CONSTCOND*/0)
/* Compiler will error this if the format is wrong... */
printf("%" PRIdaddr, bp->b_blkno);
if (pri != LOG_PRINTF) {
static const char fmt[] = "";
log(pri, fmt);
pr = addlog;
} else
pr = printf;
(*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)
(*pr)("%" PRIdaddr, sn);
else {
if (blkdone >= 0) {
sn += blkdone;
(*pr)("%" PRIdaddr " of ", sn);
}
(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
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;
(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
dname, unit, sn, sn / lp->d_secpercyl);
sn %= lp->d_secpercyl;
(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
sn / lp->d_nsectors, sn % lp->d_nsectors);
}
}
/*
* Searches the iostatlist for the disk corresponding to the
* name provided.
*/
struct disk *
disk_find(const char *name)
{
struct io_stats *stat;
stat = iostat_find(name);
if ((stat != NULL) && (stat->io_type == IOSTAT_DISK))
return stat->io_parent;
/*
* Attach a disk.
*/
void
disk_attach(struct disk *diskp)
{
/*
* Allocate and initialize the disklabel structures.
*/
diskp->dk_label = kmem_zalloc(sizeof(struct disklabel), KM_SLEEP);
diskp->dk_cpulabel = kmem_zalloc(sizeof(struct cpu_disklabel),
KM_SLEEP);
/*
* Set up the stats collection.
*/
diskp->dk_stats = iostat_alloc(IOSTAT_DISK, diskp, diskp->dk_name);
}
int
disk_begindetach(struct disk *dk, int (*lastclose)(device_t),
device_t self, int flags)
{
int rc;
rc = 0;
mutex_enter(&dk->dk_openlock);
if (dk->dk_openmask == 0)
; /* nothing to do */
else if ((flags & DETACH_FORCE) == 0)
rc = EBUSY;
else if (lastclose != NULL)
rc = (*lastclose)(self);
mutex_exit(&dk->dk_openlock);
return rc;
}
/*
* Detach a disk.
*/
void
disk_detach(struct disk *diskp)
{
/*
* Remove from the drivelist.
*/
iostat_free(diskp->dk_stats);
/*
* Release the disk-info dictionary.
*/
if (diskp->dk_info) {
prop_object_release(diskp->dk_info);
diskp->dk_info = NULL;
}
/*
* Free the space used by the disklabel structures.
*/
kmem_free(diskp->dk_label, sizeof(*diskp->dk_label));
kmem_free(diskp->dk_cpulabel, sizeof(*diskp->dk_cpulabel));
}
/*
* Mark the disk as having work queued for metrics collection.
*/
void
disk_wait(struct disk *diskp)
{
iostat_wait(diskp->dk_stats);
}
/*
* Mark the disk as busy for metrics collection.
*/
void
disk_busy(struct disk *diskp)
{
iostat_busy(diskp->dk_stats);
}
/*
* Finished disk operations, gather metrics.
*/
void
disk_unbusy(struct disk *diskp, long bcount, int read)
{
iostat_unbusy(diskp->dk_stats, bcount, read);
}
/*
* Return true if disk has an I/O operation in flight.
*/
bool
disk_isbusy(struct disk *diskp)
{
return iostat_isbusy(diskp->dk_stats);
}
/*
* Bounds checking against the media size, used for the raw partition.
* secsize, mediasize and b_blkno must all be the same units.
* Possibly this has to be DEV_BSIZE (512).
*/
int
bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
{
int64_t sz;
/*
* bp->b_bcount is a 32-bit value, and we rejected a negative
* bp->b_blkno already, so "bp->b_blkno + sz" cannot overflow.
*/
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;
return 0;
}
if (sz < 0) {
/* If past end of disk, return EINVAL. */
bp->b_error = EINVAL;
return 0;
}
/* Otherwise, truncate request. */
bp->b_bcount = sz * secsize;
}
return 1;
}
/*
* 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;
/* 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;
/* The following should be moved to dk_ioctl */
switch (cmd) {
case DIOCGDINFO:
if (dk->dk_label == NULL)
return EBUSY;
memcpy(data, dk->dk_label, sizeof (*dk->dk_label));
return 0;
#ifdef __HAVE_OLD_DISKLABEL
case ODIOCGDINFO:
if (dk->dk_label == NULL)
return EBUSY;
memcpy(&newlabel, dk->dk_label, sizeof(newlabel));
if (newlabel.d_npartitions > OLDMAXPARTITIONS)
return ENOTTY;
memcpy(data, &newlabel, sizeof(struct olddisklabel));
return 0;
#endif
case DIOCGPARTINFO:
pi = data;
memset(pi, 0, sizeof(*pi));
pi->pi_secsize = dk->dk_geom.dg_secsize;
pi->pi_bsize = MAX(BLKDEV_IOSIZE, pi->pi_secsize);
/*
* dholland 20130616: XXX this logic should not be
* here. It is here because the old buffer cache
* demands that all accesses to the same blocks need
* to be the same size; but it only works for FFS and
* nowadays I think it'll fail silently if the size
* info in the disklabel is wrong. (Or missing.) The
* buffer cache needs to be smarter; or failing that
* we need a reliable way here to get the right block
* size; or a reliable way to guarantee that (a) the
* fs is not mounted when we get here and (b) any
* buffers generated here will get purged when the fs
* does get mounted.
*/
if (dp->p_fstype == FS_BSDFFS &&
dp->p_frag != 0 && dp->p_fsize != 0)
pi->pi_bsize = dp->p_frag * dp->p_fsize;
return 0;
case DIOCAWEDGE:
if ((flag & FWRITE) == 0)
return EBADF;
/*
* disk_set_info --
* Canonicalize dk->dk_geom and set some parameters.
*
* If disk_set_info can happen concurrently with disk_ioctl in a
* driver, the driver must serialize calls to disk_set_info with
* dk_openlock.
*/
void
disk_set_info(device_t dev, struct disk *dk, const char *type)
{
struct disk_geom *dg = &dk->dk_geom;
if (dev)
prop_dictionary_set(device_properties(dev), "disk-info",
disk_info);
/*
* Don't release disk_info here; we keep a reference to it.
* disk_detach() will release it when we go away.
*/
if (odisk_info)
prop_object_release(odisk_info);
}