2015-08-16 20:26:16 +03:00
|
|
|
/* $NetBSD: dksubr.c,v 1.69 2015/08/16 17:26:16 mlelstv Exp $ */
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
/*-
|
2008-01-30 18:30:12 +03:00
|
|
|
* Copyright (c) 1996, 1997, 1998, 1999, 2002, 2008 The NetBSD Foundation, Inc.
|
2002-10-04 22:02:00 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe and Roland C. Dowdeswell.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-07-14 19:47:00 +04:00
|
|
|
#include <sys/cdefs.h>
|
2015-08-16 20:26:16 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.69 2015/08/16 17:26:16 mlelstv Exp $");
|
2003-07-14 19:47:00 +04:00
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/disklabel.h>
|
2004-10-28 11:07:35 +04:00
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/bufq.h>
|
2002-10-04 22:02:00 +04:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/namei.h>
|
2013-12-28 23:25:07 +04:00
|
|
|
#include <sys/module.h>
|
2015-05-02 11:00:08 +03:00
|
|
|
#include <sys/syslog.h>
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
#include <dev/dkvar.h>
|
2014-06-14 11:39:00 +04:00
|
|
|
#include <miscfs/specfs/specdev.h> /* for v_rdev */
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2012-05-25 18:25:39 +04:00
|
|
|
int dkdebug = 0;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define DKDB_FOLLOW 0x1
|
|
|
|
#define DKDB_INIT 0x2
|
|
|
|
#define DKDB_VNODE 0x4
|
|
|
|
|
|
|
|
#define IFDEBUG(x,y) if (dkdebug & (x)) y
|
|
|
|
#define DPRINTF(x,y) IFDEBUG(x, printf y)
|
|
|
|
#define DPRINTF_FOLLOW(y) DPRINTF(DKDB_FOLLOW, y)
|
|
|
|
#else
|
|
|
|
#define IFDEBUG(x,y)
|
|
|
|
#define DPRINTF(x,y)
|
|
|
|
#define DPRINTF_FOLLOW(y)
|
|
|
|
#endif
|
|
|
|
|
2013-12-28 23:25:07 +04:00
|
|
|
static int dk_subr_modcmd(modcmd_t, void *);
|
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
#define DKLABELDEV(dev) \
|
|
|
|
(MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
|
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
static void dk_makedisklabel(struct dk_softc *);
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
void
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_init(struct dk_softc *dksc, device_t dev, int dtype)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
memset(dksc, 0x0, sizeof(*dksc));
|
2015-05-02 11:00:08 +03:00
|
|
|
dksc->sc_dtype = dtype;
|
|
|
|
dksc->sc_dev = dev;
|
|
|
|
|
2015-05-09 14:53:34 +03:00
|
|
|
strlcpy(dksc->sc_xname, device_xname(dev), DK_XNAME_SIZE);
|
2002-10-04 22:02:00 +04:00
|
|
|
dksc->sc_dkdev.dk_name = dksc->sc_xname;
|
|
|
|
}
|
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
void
|
|
|
|
dk_attach(struct dk_softc *dksc)
|
|
|
|
{
|
2015-05-06 00:52:10 +03:00
|
|
|
dksc->sc_flags |= DKF_INITED;
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
dksc->sc_flags |= DKF_WARNLABEL | DKF_LABELSANITY;
|
|
|
|
#endif
|
2015-05-02 11:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dk_detach(struct dk_softc *dksc)
|
|
|
|
{
|
|
|
|
dksc->sc_flags &= ~DKF_INITED;
|
|
|
|
}
|
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_open(struct dk_softc *dksc, dev_t dev,
|
2006-11-16 04:32:37 +03:00
|
|
|
int flags, int fmt, struct lwp *l)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
|
|
|
struct disklabel *lp = dksc->sc_dkdev.dk_label;
|
|
|
|
int part = DISKPART(dev);
|
|
|
|
int pmask = 1 << part;
|
|
|
|
int ret = 0;
|
2005-08-20 16:03:52 +04:00
|
|
|
struct disk *dk = &dksc->sc_dkdev;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2009-01-11 12:51:38 +03:00
|
|
|
DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%"PRIx64", 0x%x)\n",
|
2015-05-02 11:00:08 +03:00
|
|
|
dksc->sc_xname, dksc, dev, flags));
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&dk->dk_openlock);
|
2005-08-20 16:03:52 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there are wedges, and this is not RAW_PART, then we
|
|
|
|
* need to fail.
|
|
|
|
*/
|
|
|
|
if (dk->dk_nwedges != 0 && part != RAW_PART) {
|
|
|
|
ret = EBUSY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
/*
|
|
|
|
* If we're init'ed and there are no other open partitions then
|
|
|
|
* update the in-core disklabel.
|
|
|
|
*/
|
2005-08-20 16:03:52 +04:00
|
|
|
if ((dksc->sc_flags & DKF_INITED)) {
|
2015-05-02 11:00:08 +03:00
|
|
|
if ((dksc->sc_flags & DKF_VLABEL) == 0) {
|
|
|
|
dksc->sc_flags |= DKF_VLABEL;
|
|
|
|
dk_getdisklabel(dksc, dev);
|
2005-08-20 16:03:52 +04:00
|
|
|
}
|
|
|
|
}
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
/* Fail if we can't find the partition. */
|
2015-05-02 11:00:08 +03:00
|
|
|
if (part != RAW_PART &&
|
|
|
|
((dksc->sc_flags & DKF_VLABEL) == 0 ||
|
|
|
|
part >= lp->d_npartitions ||
|
|
|
|
lp->d_partitions[part].p_fstype == FS_UNUSED)) {
|
2002-10-04 22:02:00 +04:00
|
|
|
ret = ENXIO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark our unit as open. */
|
|
|
|
switch (fmt) {
|
|
|
|
case S_IFCHR:
|
2005-08-20 16:03:52 +04:00
|
|
|
dk->dk_copenmask |= pmask;
|
2002-10-04 22:02:00 +04:00
|
|
|
break;
|
|
|
|
case S_IFBLK:
|
2005-08-20 16:03:52 +04:00
|
|
|
dk->dk_bopenmask |= pmask;
|
2002-10-04 22:02:00 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-08-20 16:03:52 +04:00
|
|
|
dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
done:
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&dk->dk_openlock);
|
2002-10-04 22:02:00 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_close(struct dk_softc *dksc, dev_t dev,
|
2006-11-16 04:32:37 +03:00
|
|
|
int flags, int fmt, struct lwp *l)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
2015-05-02 11:00:08 +03:00
|
|
|
const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
|
2002-10-04 22:02:00 +04:00
|
|
|
int part = DISKPART(dev);
|
|
|
|
int pmask = 1 << part;
|
2005-08-20 16:03:52 +04:00
|
|
|
struct disk *dk = &dksc->sc_dkdev;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2009-01-11 12:51:38 +03:00
|
|
|
DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%"PRIx64", 0x%x)\n",
|
2015-05-02 11:00:08 +03:00
|
|
|
dksc->sc_xname, dksc, dev, flags));
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&dk->dk_openlock);
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
switch (fmt) {
|
|
|
|
case S_IFCHR:
|
2005-08-20 16:03:52 +04:00
|
|
|
dk->dk_copenmask &= ~pmask;
|
2002-10-04 22:02:00 +04:00
|
|
|
break;
|
|
|
|
case S_IFBLK:
|
2005-08-20 16:03:52 +04:00
|
|
|
dk->dk_bopenmask &= ~pmask;
|
2002-10-04 22:02:00 +04:00
|
|
|
break;
|
|
|
|
}
|
2005-08-20 16:03:52 +04:00
|
|
|
dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
if (dk->dk_openmask == 0) {
|
|
|
|
if (dkd->d_lastclose != NULL)
|
|
|
|
(*dkd->d_lastclose)(dksc->sc_dev);
|
2015-07-11 12:45:16 +03:00
|
|
|
if ((dksc->sc_flags & DKF_KLABEL) == 0)
|
|
|
|
dksc->sc_flags &= ~DKF_VLABEL;
|
2015-05-02 11:00:08 +03:00
|
|
|
}
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&dk->dk_openlock);
|
2002-10-04 22:02:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_strategy(struct dk_softc *dksc, struct buf *bp)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
2015-05-02 11:00:08 +03:00
|
|
|
const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
|
2014-12-29 15:03:39 +03:00
|
|
|
int s, part;
|
2002-10-04 22:02:00 +04:00
|
|
|
int wlabel;
|
2005-10-18 04:14:43 +04:00
|
|
|
daddr_t blkno;
|
2014-12-29 15:03:39 +03:00
|
|
|
struct disklabel *lp;
|
|
|
|
struct disk *dk;
|
|
|
|
uint64_t numsecs;
|
|
|
|
unsigned secsize;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
|
2015-05-02 11:00:08 +03:00
|
|
|
dksc->sc_xname, dksc, bp));
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
if (!(dksc->sc_flags & DKF_INITED)) {
|
2006-09-05 02:51:42 +04:00
|
|
|
DPRINTF_FOLLOW(("dk_strategy: not inited\n"));
|
2002-10-04 22:02:00 +04:00
|
|
|
bp->b_error = ENXIO;
|
2002-10-09 18:04:08 +04:00
|
|
|
biodone(bp);
|
2002-10-04 22:02:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:03:39 +03:00
|
|
|
lp = dksc->sc_dkdev.dk_label;
|
|
|
|
dk = &dksc->sc_dkdev;
|
|
|
|
|
|
|
|
part = DISKPART(bp->b_dev);
|
|
|
|
numsecs = dk->dk_geom.dg_secperunit;
|
|
|
|
secsize = dk->dk_geom.dg_secsize;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
|
2014-12-29 15:03:39 +03:00
|
|
|
/*
|
|
|
|
* The transfer must be a whole number of blocks and the offset must
|
|
|
|
* not be negative.
|
2015-07-22 13:32:16 +03:00
|
|
|
*/
|
2014-12-29 15:03:39 +03:00
|
|
|
if ((bp->b_bcount % secsize) != 0 || bp->b_blkno < 0) {
|
|
|
|
bp->b_error = EINVAL;
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
2015-07-22 13:32:16 +03:00
|
|
|
}
|
2014-12-29 15:03:39 +03:00
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
/* If there is nothing to do, then we are done */
|
|
|
|
if (bp->b_bcount == 0) {
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
|
2014-12-29 15:03:39 +03:00
|
|
|
if (part == RAW_PART) {
|
|
|
|
if (bounds_check_with_mediasize(bp, DEV_BSIZE, numsecs) <= 0) {
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
2002-10-04 22:02:00 +04:00
|
|
|
}
|
|
|
|
|
2015-07-22 13:32:16 +03:00
|
|
|
/*
|
2015-07-19 11:05:24 +03:00
|
|
|
* Convert the block number to absolute and put it in terms
|
|
|
|
* of the device's logical block size.
|
|
|
|
*/
|
|
|
|
if (secsize >= DEV_BSIZE)
|
|
|
|
blkno = bp->b_blkno / (secsize / DEV_BSIZE);
|
|
|
|
else
|
|
|
|
blkno = bp->b_blkno * (DEV_BSIZE / secsize);
|
|
|
|
|
2014-12-29 15:03:39 +03:00
|
|
|
if (part != RAW_PART)
|
|
|
|
blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
|
2005-10-18 04:14:43 +04:00
|
|
|
bp->b_rawblkno = blkno;
|
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
/*
|
|
|
|
* Start the unit by calling the start routine
|
|
|
|
* provided by the individual driver.
|
|
|
|
*/
|
|
|
|
s = splbio();
|
2009-01-13 16:33:58 +03:00
|
|
|
bufq_put(dksc->sc_bufq, bp);
|
2015-05-02 11:00:08 +03:00
|
|
|
dkd->d_diskstart(dksc->sc_dev);
|
2002-10-04 22:02:00 +04:00
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
void
|
|
|
|
dk_done(struct dk_softc *dksc, struct buf *bp)
|
|
|
|
{
|
|
|
|
struct disk *dk = &dksc->sc_dkdev;
|
|
|
|
|
|
|
|
if (bp->b_error != 0) {
|
2015-08-02 10:25:40 +03:00
|
|
|
struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
|
|
|
|
|
|
|
|
diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
|
2015-05-02 11:00:08 +03:00
|
|
|
dk->dk_label);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
disk_unbusy(dk, bp->b_bcount - bp->b_resid, (bp->b_flags & B_READ));
|
|
|
|
#ifdef notyet
|
|
|
|
rnd_add_uint(&dksc->sc_rnd_source, bp->b_rawblkno);
|
|
|
|
#endif
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
int
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_size(struct dk_softc *dksc, dev_t dev)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
2015-05-02 11:00:08 +03:00
|
|
|
const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
|
2002-10-04 22:02:00 +04:00
|
|
|
struct disklabel *lp;
|
|
|
|
int is_open;
|
|
|
|
int part;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if ((dksc->sc_flags & DKF_INITED) == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
part = DISKPART(dev);
|
|
|
|
is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
|
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
if (!is_open && dkd->d_open(dev, 0, S_IFBLK, curlwp))
|
2002-10-04 22:02:00 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
lp = dksc->sc_dkdev.dk_label;
|
|
|
|
if (lp->d_partitions[part].p_fstype != FS_SWAP)
|
|
|
|
size = -1;
|
|
|
|
else
|
|
|
|
size = lp->d_partitions[part].p_size *
|
|
|
|
(lp->d_secsize / DEV_BSIZE);
|
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
if (!is_open && dkd->d_close(dev, 0, S_IFBLK, curlwp))
|
2015-05-01 15:30:28 +03:00
|
|
|
return -1;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_ioctl(struct dk_softc *dksc, dev_t dev,
|
2007-03-04 08:59:00 +03:00
|
|
|
u_long cmd, void *data, int flag, struct lwp *l)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
2015-05-02 11:00:08 +03:00
|
|
|
const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
|
2002-10-04 22:02:00 +04:00
|
|
|
struct disklabel *lp;
|
2014-12-31 20:06:48 +03:00
|
|
|
struct disk *dk = &dksc->sc_dkdev;
|
2002-10-04 22:02:00 +04:00
|
|
|
#ifdef __HAVE_OLD_DISKLABEL
|
|
|
|
struct disklabel newlabel;
|
|
|
|
#endif
|
2014-12-31 20:06:48 +03:00
|
|
|
int error;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2009-01-11 12:51:38 +03:00
|
|
|
DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%"PRIx64", 0x%lx)\n",
|
2015-05-02 11:00:08 +03:00
|
|
|
dksc->sc_xname, dksc, dev, cmd));
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
/* ensure that the pseudo disk is open for writes for these commands */
|
|
|
|
switch (cmd) {
|
|
|
|
case DIOCSDINFO:
|
|
|
|
case DIOCWDINFO:
|
|
|
|
#ifdef __HAVE_OLD_DISKLABEL
|
|
|
|
case ODIOCSDINFO:
|
|
|
|
case ODIOCWDINFO:
|
|
|
|
#endif
|
2015-08-16 20:26:16 +03:00
|
|
|
case DIOCKLABEL:
|
2002-10-04 22:02:00 +04:00
|
|
|
case DIOCWLABEL:
|
2012-05-25 14:53:46 +04:00
|
|
|
case DIOCAWEDGE:
|
2015-08-16 20:26:16 +03:00
|
|
|
case DIOCSSTRATEGY:
|
2002-10-04 22:02:00 +04:00
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
return EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure that the pseudo-disk is initialized for these */
|
|
|
|
switch (cmd) {
|
|
|
|
case DIOCGDINFO:
|
|
|
|
case DIOCSDINFO:
|
|
|
|
case DIOCWDINFO:
|
|
|
|
case DIOCGPART:
|
2015-05-02 11:00:08 +03:00
|
|
|
case DIOCKLABEL:
|
2002-10-04 22:02:00 +04:00
|
|
|
case DIOCWLABEL:
|
|
|
|
case DIOCGDEFLABEL:
|
2012-05-25 14:53:46 +04:00
|
|
|
case DIOCAWEDGE:
|
|
|
|
case DIOCDWEDGE:
|
|
|
|
case DIOCLWEDGES:
|
2014-11-04 10:51:54 +03:00
|
|
|
case DIOCMWEDGES:
|
2012-05-25 14:53:46 +04:00
|
|
|
case DIOCCACHESYNC:
|
2002-10-04 22:02:00 +04:00
|
|
|
#ifdef __HAVE_OLD_DISKLABEL
|
|
|
|
case ODIOCGDINFO:
|
|
|
|
case ODIOCSDINFO:
|
|
|
|
case ODIOCWDINFO:
|
|
|
|
case ODIOCGDEFLABEL:
|
|
|
|
#endif
|
|
|
|
if ((dksc->sc_flags & DKF_INITED) == 0)
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:52:04 +03:00
|
|
|
error = disk_ioctl(dk, dev, cmd, data, flag, l);
|
2014-12-31 20:06:48 +03:00
|
|
|
if (error != EPASSTHROUGH)
|
|
|
|
return error;
|
|
|
|
else
|
|
|
|
error = 0;
|
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
switch (cmd) {
|
|
|
|
case DIOCWDINFO:
|
|
|
|
case DIOCSDINFO:
|
|
|
|
#ifdef __HAVE_OLD_DISKLABEL
|
|
|
|
case ODIOCWDINFO:
|
|
|
|
case ODIOCSDINFO:
|
|
|
|
#endif
|
|
|
|
#ifdef __HAVE_OLD_DISKLABEL
|
|
|
|
if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
|
|
|
|
memset(&newlabel, 0, sizeof newlabel);
|
|
|
|
memcpy(&newlabel, data, sizeof (struct olddisklabel));
|
|
|
|
lp = &newlabel;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
lp = (struct disklabel *)data;
|
|
|
|
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_enter(&dk->dk_openlock);
|
2002-10-04 22:02:00 +04:00
|
|
|
dksc->sc_flags |= DKF_LABELLING;
|
|
|
|
|
|
|
|
error = setdisklabel(dksc->sc_dkdev.dk_label,
|
|
|
|
lp, 0, dksc->sc_dkdev.dk_cpulabel);
|
|
|
|
if (error == 0) {
|
|
|
|
if (cmd == DIOCWDINFO
|
|
|
|
#ifdef __HAVE_OLD_DISKLABEL
|
|
|
|
|| cmd == ODIOCWDINFO
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
error = writedisklabel(DKLABELDEV(dev),
|
2015-05-02 11:00:08 +03:00
|
|
|
dkd->d_strategy, dksc->sc_dkdev.dk_label,
|
2002-10-04 22:02:00 +04:00
|
|
|
dksc->sc_dkdev.dk_cpulabel);
|
|
|
|
}
|
|
|
|
|
|
|
|
dksc->sc_flags &= ~DKF_LABELLING;
|
2007-07-21 23:51:47 +04:00
|
|
|
mutex_exit(&dk->dk_openlock);
|
2002-10-04 22:02:00 +04:00
|
|
|
break;
|
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
case DIOCKLABEL:
|
|
|
|
if (*(int *)data != 0)
|
|
|
|
dksc->sc_flags |= DKF_KLABEL;
|
|
|
|
else
|
|
|
|
dksc->sc_flags &= ~DKF_KLABEL;
|
|
|
|
break;
|
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
case DIOCWLABEL:
|
|
|
|
if (*(int *)data != 0)
|
|
|
|
dksc->sc_flags |= DKF_WLABEL;
|
|
|
|
else
|
|
|
|
dksc->sc_flags &= ~DKF_WLABEL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIOCGDEFLABEL:
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_getdefaultlabel(dksc, (struct disklabel *)data);
|
2002-10-04 22:02:00 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef __HAVE_OLD_DISKLABEL
|
|
|
|
case ODIOCGDEFLABEL:
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_getdefaultlabel(dksc, &newlabel);
|
2002-10-04 22:02:00 +04:00
|
|
|
if (newlabel.d_npartitions > OLDMAXPARTITIONS)
|
|
|
|
return ENOTTY;
|
|
|
|
memcpy(data, &newlabel, sizeof (struct olddisklabel));
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2005-12-26 13:36:47 +03:00
|
|
|
case DIOCGSTRATEGY:
|
|
|
|
{
|
|
|
|
struct disk_strategy *dks = (void *)data;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
strlcpy(dks->dks_name, bufq_getstrategyname(dksc->sc_bufq),
|
|
|
|
sizeof(dks->dks_name));
|
|
|
|
splx(s);
|
|
|
|
dks->dks_paramlen = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-22 13:32:16 +03:00
|
|
|
|
2005-12-26 13:36:47 +03:00
|
|
|
case DIOCSSTRATEGY:
|
|
|
|
{
|
|
|
|
struct disk_strategy *dks = (void *)data;
|
|
|
|
struct bufq_state *new;
|
|
|
|
struct bufq_state *old;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (dks->dks_param != NULL) {
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
|
|
|
|
error = bufq_alloc(&new, dks->dks_name,
|
|
|
|
BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
|
|
|
|
if (error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
s = splbio();
|
|
|
|
old = dksc->sc_bufq;
|
|
|
|
bufq_move(new, old);
|
|
|
|
dksc->sc_bufq = new;
|
|
|
|
splx(s);
|
|
|
|
bufq_free(old);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-04 22:02:00 +04:00
|
|
|
default:
|
|
|
|
error = ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dk_dump dumps all of physical memory into the partition specified.
|
|
|
|
* This requires substantially more framework than {s,w}ddump, and hence
|
|
|
|
* is probably much more fragile.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DKF_READYFORDUMP (DKF_INITED|DKF_TAKEDUMP)
|
|
|
|
#define DKFF_READYFORDUMP(x) (((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
|
|
|
|
static volatile int dk_dumping = 0;
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_dump(struct dk_softc *dksc, dev_t dev,
|
|
|
|
daddr_t blkno, void *vav, size_t size)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
2015-05-02 11:00:08 +03:00
|
|
|
const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
|
|
|
|
char *va = vav;
|
|
|
|
struct disklabel *lp;
|
|
|
|
int part, towrt, nsects, sectoff, maxblkcnt, nblk;
|
|
|
|
int maxxfer, rv = 0;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ensure that we consider this device to be safe for dumping,
|
|
|
|
* and that the device is configured.
|
|
|
|
*/
|
|
|
|
if (!DKFF_READYFORDUMP(dksc->sc_flags))
|
|
|
|
return ENXIO;
|
|
|
|
|
|
|
|
/* ensure that we are not already dumping */
|
|
|
|
if (dk_dumping)
|
|
|
|
return EFAULT;
|
|
|
|
dk_dumping = 1;
|
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
if (dkd->d_dumpblocks == NULL)
|
|
|
|
return ENXIO;
|
|
|
|
|
|
|
|
/* device specific max transfer size */
|
|
|
|
maxxfer = MAXPHYS;
|
|
|
|
if (dkd->d_iosize != NULL)
|
|
|
|
(*dkd->d_iosize)(dksc->sc_dev, &maxxfer);
|
|
|
|
|
|
|
|
/* Convert to disk sectors. Request must be a multiple of size. */
|
|
|
|
part = DISKPART(dev);
|
|
|
|
lp = dksc->sc_dkdev.dk_label;
|
|
|
|
if ((size % lp->d_secsize) != 0)
|
|
|
|
return (EFAULT);
|
|
|
|
towrt = size / lp->d_secsize;
|
|
|
|
blkno = dbtob(blkno) / lp->d_secsize; /* blkno in secsize units */
|
|
|
|
|
|
|
|
nsects = lp->d_partitions[part].p_size;
|
|
|
|
sectoff = lp->d_partitions[part].p_offset;
|
|
|
|
|
|
|
|
/* Check transfer bounds against partition size. */
|
|
|
|
if ((blkno < 0) || ((blkno + towrt) > nsects))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Offset block number to start of partition. */
|
|
|
|
blkno += sectoff;
|
|
|
|
|
|
|
|
/* Start dumping and return when done. */
|
|
|
|
maxblkcnt = howmany(maxxfer, lp->d_secsize);
|
|
|
|
while (towrt > 0) {
|
|
|
|
nblk = min(maxblkcnt, towrt);
|
|
|
|
|
|
|
|
if ((rv = (*dkd->d_dumpblocks)(dksc->sc_dev, va, blkno, nblk)) != 0)
|
|
|
|
return (rv);
|
|
|
|
|
|
|
|
towrt -= nblk;
|
|
|
|
blkno += nblk;
|
|
|
|
va += nblk * lp->d_secsize;
|
|
|
|
}
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
dk_dumping = 0;
|
|
|
|
|
2015-05-02 11:00:08 +03:00
|
|
|
return 0;
|
2002-10-04 22:02:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2015-05-09 16:07:20 +03:00
|
|
|
dk_getdefaultlabel(struct dk_softc *dksc, struct disklabel *lp)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
2013-05-30 03:25:55 +04:00
|
|
|
struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2002-12-17 04:55:44 +03:00
|
|
|
memset(lp, 0, sizeof(*lp));
|
|
|
|
|
2014-10-11 16:01:27 +04:00
|
|
|
if (dg->dg_secperunit > UINT32_MAX)
|
|
|
|
lp->d_secperunit = UINT32_MAX;
|
|
|
|
else
|
|
|
|
lp->d_secperunit = dg->dg_secperunit;
|
2013-05-30 03:25:55 +04:00
|
|
|
lp->d_secsize = dg->dg_secsize;
|
|
|
|
lp->d_nsectors = dg->dg_nsectors;
|
|
|
|
lp->d_ntracks = dg->dg_ntracks;
|
|
|
|
lp->d_ncylinders = dg->dg_ncylinders;
|
2002-10-04 22:02:00 +04:00
|
|
|
lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
|
|
|
|
|
2015-05-09 16:07:20 +03:00
|
|
|
strlcpy(lp->d_typename, dksc->sc_xname, sizeof(lp->d_typename));
|
2015-05-02 11:00:08 +03:00
|
|
|
lp->d_type = dksc->sc_dtype;
|
2015-05-09 16:07:20 +03:00
|
|
|
strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
|
2002-10-04 22:02:00 +04:00
|
|
|
lp->d_rpm = 3600;
|
|
|
|
lp->d_interleave = 1;
|
|
|
|
lp->d_flags = 0;
|
|
|
|
|
|
|
|
lp->d_partitions[RAW_PART].p_offset = 0;
|
2014-10-11 16:01:27 +04:00
|
|
|
lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
|
2002-10-04 22:02:00 +04:00
|
|
|
lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
|
|
|
|
lp->d_npartitions = RAW_PART + 1;
|
|
|
|
|
|
|
|
lp->d_magic = DISKMAGIC;
|
|
|
|
lp->d_magic2 = DISKMAGIC;
|
|
|
|
lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_getdisklabel(struct dk_softc *dksc, dev_t dev)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
2015-05-02 11:00:08 +03:00
|
|
|
const struct dkdriver *dkd = dksc->sc_dkdev.dk_driver;
|
2002-10-04 22:02:00 +04:00
|
|
|
struct disklabel *lp = dksc->sc_dkdev.dk_label;
|
|
|
|
struct cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
|
2015-05-02 11:00:08 +03:00
|
|
|
struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
|
2002-10-04 22:02:00 +04:00
|
|
|
struct partition *pp;
|
|
|
|
int i;
|
2003-05-02 12:45:10 +04:00
|
|
|
const char *errstring;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
memset(clp, 0x0, sizeof(*clp));
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_getdefaultlabel(dksc, lp);
|
|
|
|
errstring = readdisklabel(DKLABELDEV(dev), dkd->d_strategy,
|
2002-10-04 22:02:00 +04:00
|
|
|
dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
|
|
|
|
if (errstring) {
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_makedisklabel(dksc);
|
2002-10-04 22:02:00 +04:00
|
|
|
if (dksc->sc_flags & DKF_WARNLABEL)
|
|
|
|
printf("%s: %s\n", dksc->sc_xname, errstring);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Sanity check */
|
2014-10-11 16:36:25 +04:00
|
|
|
if (lp->d_secperunit < UINT32_MAX ?
|
|
|
|
lp->d_secperunit != dg->dg_secperunit :
|
|
|
|
lp->d_secperunit > dg->dg_secperunit)
|
|
|
|
printf("WARNING: %s: total sector size in disklabel (%ju) "
|
|
|
|
"!= the size of %s (%ju)\n", dksc->sc_xname,
|
2015-05-02 11:00:08 +03:00
|
|
|
(uintmax_t)lp->d_secperunit, dksc->sc_xname,
|
2014-10-11 16:36:25 +04:00
|
|
|
(uintmax_t)dg->dg_secperunit);
|
2002-10-04 22:02:00 +04:00
|
|
|
|
|
|
|
for (i=0; i < lp->d_npartitions; i++) {
|
|
|
|
pp = &lp->d_partitions[i];
|
2013-05-30 03:37:10 +04:00
|
|
|
if (pp->p_offset + pp->p_size > dg->dg_secperunit)
|
2002-10-04 22:02:00 +04:00
|
|
|
printf("WARNING: %s: end of partition `%c' exceeds "
|
2014-10-11 16:36:25 +04:00
|
|
|
"the size of %s (%ju)\n", dksc->sc_xname,
|
2015-05-02 11:00:08 +03:00
|
|
|
'a' + i, dksc->sc_xname,
|
2014-10-11 16:36:25 +04:00
|
|
|
(uintmax_t)dg->dg_secperunit);
|
2002-10-04 22:02:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
2004-08-23 09:39:46 +04:00
|
|
|
static void
|
2015-05-02 11:00:08 +03:00
|
|
|
dk_makedisklabel(struct dk_softc *dksc)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
|
|
|
struct disklabel *lp = dksc->sc_dkdev.dk_label;
|
|
|
|
|
|
|
|
lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
|
2015-05-09 16:07:20 +03:00
|
|
|
strlcpy(lp->d_packname, "default label", sizeof(lp->d_packname));
|
2002-10-04 22:02:00 +04:00
|
|
|
lp->d_checksum = dkcksum(lp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function is taken from ccd.c:1.76 --rcd */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX this function looks too generic for dksubr.c, shouldn't we
|
|
|
|
* put it somewhere better?
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup the provided name in the filesystem. If the file exists,
|
|
|
|
* is a valid block device, and isn't being used by anyone else,
|
|
|
|
* set *vpp to the file's vnode.
|
|
|
|
*/
|
|
|
|
int
|
2010-11-19 09:44:33 +03:00
|
|
|
dk_lookup(struct pathbuf *pb, struct lwp *l, struct vnode **vpp)
|
2002-10-04 22:02:00 +04:00
|
|
|
{
|
|
|
|
struct nameidata nd;
|
|
|
|
struct vnode *vp;
|
2006-08-27 09:00:00 +04:00
|
|
|
int error;
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2006-08-27 09:00:00 +04:00
|
|
|
if (l == NULL)
|
|
|
|
return ESRCH; /* Is ESRCH the best choice? */
|
|
|
|
|
2010-11-19 09:44:33 +03:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, pb);
|
2006-08-27 09:00:00 +04:00
|
|
|
if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
|
2002-10-04 22:02:00 +04:00
|
|
|
DPRINTF((DKDB_FOLLOW|DKDB_INIT),
|
|
|
|
("dk_lookup: vn_open error = %d\n", error));
|
2006-08-27 09:00:00 +04:00
|
|
|
return error;
|
2002-10-04 22:02:00 +04:00
|
|
|
}
|
|
|
|
|
2006-08-27 09:00:00 +04:00
|
|
|
vp = nd.ni_vp;
|
2014-06-14 11:39:00 +04:00
|
|
|
if (vp->v_type != VBLK) {
|
|
|
|
error = ENOTBLK;
|
2006-08-27 09:00:00 +04:00
|
|
|
goto out;
|
2002-10-04 22:02:00 +04:00
|
|
|
}
|
|
|
|
|
2014-06-14 11:39:00 +04:00
|
|
|
/* Reopen as anonymous vnode to protect against forced unmount. */
|
|
|
|
if ((error = bdevvp(vp->v_rdev, vpp)) != 0)
|
2006-08-27 09:00:00 +04:00
|
|
|
goto out;
|
2014-06-14 11:39:00 +04:00
|
|
|
VOP_UNLOCK(vp);
|
|
|
|
if ((error = vn_close(vp, FREAD | FWRITE, l->l_cred)) != 0) {
|
|
|
|
vrele(*vpp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
|
|
|
|
vrele(*vpp);
|
|
|
|
return error;
|
2006-08-27 09:00:00 +04:00
|
|
|
}
|
2014-06-14 11:39:00 +04:00
|
|
|
mutex_enter((*vpp)->v_interlock);
|
|
|
|
(*vpp)->v_writecount++;
|
|
|
|
mutex_exit((*vpp)->v_interlock);
|
2006-08-27 09:00:00 +04:00
|
|
|
|
2014-06-14 11:39:00 +04:00
|
|
|
IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", *vpp));
|
2002-10-04 22:02:00 +04:00
|
|
|
|
2006-08-27 09:00:00 +04:00
|
|
|
return 0;
|
|
|
|
out:
|
2010-06-24 16:58:48 +04:00
|
|
|
VOP_UNLOCK(vp);
|
2008-03-22 00:54:58 +03:00
|
|
|
(void) vn_close(vp, FREAD | FWRITE, l->l_cred);
|
2006-08-27 09:00:00 +04:00
|
|
|
return error;
|
2002-10-04 22:02:00 +04:00
|
|
|
}
|
2013-12-28 23:25:07 +04:00
|
|
|
|
|
|
|
MODULE(MODULE_CLASS_MISC, dk_subr, NULL);
|
|
|
|
|
|
|
|
static int
|
|
|
|
dk_subr_modcmd(modcmd_t cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
|
|
|
case MODULE_CMD_FINI:
|
|
|
|
return 0;
|
|
|
|
case MODULE_CMD_STAT:
|
|
|
|
case MODULE_CMD_AUTOUNLOAD:
|
|
|
|
default:
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
}
|