1994-12-04 20:08:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1988 University of Utah.
|
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* the Systems Programming Group of the University of Utah Computer
|
|
|
|
* Science Department.
|
|
|
|
*
|
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. 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.
|
|
|
|
*
|
|
|
|
* from: Utah Hdr: vn.c 1.13 94/04/02
|
|
|
|
*
|
1996-03-28 15:25:43 +00:00
|
|
|
* from: @(#)vn.c 8.6 (Berkeley) 4/1/94
|
1999-08-08 18:43:05 +00:00
|
|
|
* $Id: vn.c,v 1.81 1999/07/20 09:47:33 phk Exp $
|
1994-12-04 20:08:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Vnode disk driver.
|
|
|
|
*
|
|
|
|
* Block/character interface to a vnode. Allows one to treat a file
|
|
|
|
* as a disk (e.g. build a filesystem in it, mount it, etc.).
|
|
|
|
*
|
|
|
|
* NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
|
|
|
|
* instead of a simple VOP_RDWR. We do this to avoid distorting the
|
|
|
|
* local buffer cache.
|
|
|
|
*
|
|
|
|
* NOTE 2: There is a security issue involved with this driver.
|
|
|
|
* Once mounted all access to the contents of the "mapped" file via
|
|
|
|
* the special file is controlled by the permissions on the special
|
|
|
|
* file, the protection of the mapped file is ignored (effectively,
|
|
|
|
* by using root credentials in all transactions).
|
|
|
|
*
|
|
|
|
* NOTE 3: Doesn't interact with leases, should it?
|
|
|
|
*/
|
|
|
|
#include "vn.h"
|
|
|
|
#if NVN > 0
|
|
|
|
|
1995-03-01 05:05:21 +00:00
|
|
|
/* default is to have 8 VN's */
|
|
|
|
#if NVN < 8
|
|
|
|
#undef NVN
|
1995-03-04 11:08:26 +00:00
|
|
|
#define NVN 8
|
1995-03-01 05:05:21 +00:00
|
|
|
#endif
|
|
|
|
|
1998-01-24 02:54:56 +00:00
|
|
|
#include "opt_devfs.h"
|
|
|
|
|
1994-12-04 20:08:35 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1994-12-06 06:47:32 +00:00
|
|
|
#include <sys/kernel.h>
|
1994-12-04 20:08:35 +00:00
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/vnode.h>
|
1997-03-23 03:37:54 +00:00
|
|
|
#include <sys/fcntl.h>
|
Divorce "dev_t" from the "major|minor" bitmap, which is now called
udev_t in the kernel but still called dev_t in userland.
Provide functions to manipulate both types:
major() umajor()
minor() uminor()
makedev() umakedev()
dev2udev() udev2dev()
For now they're functions, they will become in-line functions
after one of the next two steps in this process.
Return major/minor/makedev to macro-hood for userland.
Register a name in cdevsw[] for the "filedescriptor" driver.
In the kernel the udev_t appears in places where we have the
major/minor number combination, (ie: a potential device: we
may not have the driver nor the device), like in inodes, vattr,
cdevsw registration and so on, whereas the dev_t appears where
we carry around a reference to a actual device.
In the future the cdevsw and the aliased-from vnode will be hung
directly from the dev_t, along with up to two softc pointers for
the device driver and a few houskeeping bits. This will essentially
replace the current "alias" check code (same buck, bigger bang).
A little stunt has been provided to try to catch places where the
wrong type is being used (dev_t vs udev_t), if you see something
not working, #undef DEVT_FASCIST in kern/kern_conf.c and see if
it makes a difference. If it does, please try to track it down
(many hands make light work) or at least try to reproduce it
as simply as possible, and describe how to do that.
Without DEVT_FASCIST I belive this patch is a no-op.
Stylistic/posixoid comments about the userland view of the <sys/*.h>
files welcome now, from userland they now contain the end result.
Next planned step: make all dev_t's refer to the same devsw[] which
means convert BLK's to CHR's at the perimeter of the vnodes and
other places where they enter the game (bootdev, mknod, sysctl).
1999-05-11 19:55:07 +00:00
|
|
|
#include <sys/conf.h>
|
1995-02-25 16:48:28 +00:00
|
|
|
#include <sys/disklabel.h>
|
1994-12-12 00:23:11 +00:00
|
|
|
#include <sys/diskslice.h>
|
1994-12-16 16:50:15 +00:00
|
|
|
#include <sys/stat.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
#include <sys/conf.h>
|
1999-01-21 17:17:02 +00:00
|
|
|
#include <sys/module.h>
|
1998-09-12 18:46:06 +00:00
|
|
|
#ifdef DEVFS
|
|
|
|
#include <sys/devfsext.h>
|
|
|
|
#endif /*DEVFS*/
|
1994-12-04 20:08:35 +00:00
|
|
|
#include <sys/vnioctl.h>
|
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_prot.h>
|
|
|
|
#include <vm/vm_object.h>
|
|
|
|
#include <vm/vm_page.h>
|
|
|
|
#include <vm/vm_pager.h>
|
|
|
|
#include <vm/vm_pageout.h>
|
|
|
|
#include <vm/swap_pager.h>
|
|
|
|
#include <vm/vm_extern.h>
|
|
|
|
#include <vm/vm_zone.h>
|
|
|
|
|
1998-04-19 23:32:49 +00:00
|
|
|
static d_ioctl_t vnioctl;
|
1995-12-08 11:19:42 +00:00
|
|
|
static d_open_t vnopen;
|
|
|
|
static d_close_t vnclose;
|
|
|
|
static d_dump_t vndump;
|
|
|
|
static d_psize_t vnsize;
|
|
|
|
static d_strategy_t vnstrategy;
|
|
|
|
|
1995-11-29 10:49:16 +00:00
|
|
|
#define CDEV_MAJOR 43
|
|
|
|
#define BDEV_MAJOR 15
|
1998-07-04 22:30:26 +00:00
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
/*
|
|
|
|
* cdevsw
|
|
|
|
* D_DISK we want to look like a disk
|
|
|
|
* ( D_NOCLUSTERRW removed - clustering should be ok )
|
|
|
|
* D_CANFREE We support B_FREEBUF
|
|
|
|
*/
|
1998-07-04 22:30:26 +00:00
|
|
|
|
|
|
|
static struct cdevsw vn_cdevsw = {
|
1999-05-30 16:53:49 +00:00
|
|
|
/* open */ vnopen,
|
|
|
|
/* close */ vnclose,
|
|
|
|
/* read */ physread,
|
|
|
|
/* write */ physwrite,
|
|
|
|
/* ioctl */ vnioctl,
|
|
|
|
/* stop */ nostop,
|
|
|
|
/* reset */ noreset,
|
|
|
|
/* devtotty */ nodevtotty,
|
|
|
|
/* poll */ nopoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ vnstrategy,
|
|
|
|
/* name */ "vn",
|
1999-07-20 09:47:55 +00:00
|
|
|
/* parms */ noparms,
|
1999-05-30 16:53:49 +00:00
|
|
|
/* maj */ CDEV_MAJOR,
|
|
|
|
/* dump */ vndump,
|
|
|
|
/* psize */ vnsize,
|
|
|
|
/* flags */ D_DISK|D_CANFREE,
|
|
|
|
/* maxio */ 0,
|
|
|
|
/* bmaj */ BDEV_MAJOR
|
|
|
|
};
|
1995-12-08 11:19:42 +00:00
|
|
|
|
1998-04-19 23:32:49 +00:00
|
|
|
|
1994-12-12 00:23:11 +00:00
|
|
|
#define vnunit(dev) dkunit(dev)
|
1994-12-04 20:08:35 +00:00
|
|
|
|
|
|
|
#define getvnbuf() \
|
|
|
|
((struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK))
|
|
|
|
|
|
|
|
#define putvnbuf(bp) \
|
|
|
|
free((caddr_t)(bp), M_DEVBUF)
|
|
|
|
|
|
|
|
struct vn_softc {
|
1999-03-14 09:20:01 +00:00
|
|
|
int sc_flags; /* flags */
|
|
|
|
int sc_size; /* size of vn, sc_secsize scale */
|
|
|
|
int sc_secsize; /* sector size */
|
1998-04-19 23:32:49 +00:00
|
|
|
struct diskslices *sc_slices;
|
1999-03-14 09:20:01 +00:00
|
|
|
struct vnode *sc_vp; /* vnode if not NULL */
|
|
|
|
vm_object_t sc_object; /* backing object if not NULL */
|
|
|
|
struct ucred *sc_cred; /* credentials */
|
|
|
|
int sc_maxactive; /* max # of active requests */
|
|
|
|
struct buf sc_tab; /* transfer queue */
|
|
|
|
u_long sc_options; /* options */
|
1998-09-12 18:46:06 +00:00
|
|
|
#ifdef DEVFS
|
|
|
|
void *r_devfs_token;
|
|
|
|
void *devfs_token;
|
|
|
|
#endif
|
1994-12-04 20:08:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* sc_flags */
|
|
|
|
#define VNF_INITED 0x01
|
|
|
|
|
1995-12-09 20:34:16 +00:00
|
|
|
static struct vn_softc *vn_softc[NVN];
|
|
|
|
static u_long vn_options;
|
1995-03-11 09:32:45 +00:00
|
|
|
|
|
|
|
#define IFOPT(vn,opt) if (((vn)->sc_options|vn_options) & (opt))
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
#if 0
|
1995-12-09 20:34:16 +00:00
|
|
|
static void vniodone (struct buf *bp);
|
1999-03-14 09:20:01 +00:00
|
|
|
#endif
|
1995-12-09 20:34:16 +00:00
|
|
|
static int vnsetcred (struct vn_softc *vn, struct ucred *cred);
|
|
|
|
static void vnclear (struct vn_softc *vn);
|
1999-01-21 17:17:02 +00:00
|
|
|
static int vn_modevent (module_t, int, void *);
|
1999-03-14 09:20:01 +00:00
|
|
|
static int vniocattach_file (struct vn_softc *, struct vn_ioctl *, dev_t dev, int flag, struct proc *p);
|
|
|
|
static int vniocattach_swap (struct vn_softc *, struct vn_ioctl *, dev_t dev, int flag, struct proc *p);
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1994-12-12 00:23:11 +00:00
|
|
|
vnclose(dev_t dev, int flags, int mode, struct proc *p)
|
|
|
|
{
|
1995-03-11 09:32:45 +00:00
|
|
|
struct vn_softc *vn = vn_softc[vnunit(dev)];
|
1994-12-12 00:23:11 +00:00
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
IFOPT(vn, VN_LABELS)
|
|
|
|
if (vn->sc_slices != NULL)
|
|
|
|
dsclose(dev, mode, vn->sc_slices);
|
1994-12-12 00:23:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1994-12-04 20:08:35 +00:00
|
|
|
vnopen(dev_t dev, int flags, int mode, struct proc *p)
|
|
|
|
{
|
1995-04-28 22:24:37 +00:00
|
|
|
int unit = vnunit(dev);
|
1995-03-01 05:05:21 +00:00
|
|
|
struct vn_softc *vn;
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
if (unit >= NVN) {
|
|
|
|
if (vn_options & VN_FOLLOW)
|
1995-05-30 08:16:23 +00:00
|
|
|
printf("vnopen(0x%lx, 0x%x, 0x%x, %p)\n",
|
1998-07-11 07:46:16 +00:00
|
|
|
(u_long)dev, flags, mode, (void *)p);
|
1995-03-01 05:05:21 +00:00
|
|
|
return(ENOENT);
|
1995-03-11 09:32:45 +00:00
|
|
|
}
|
1995-03-01 05:05:21 +00:00
|
|
|
|
1994-12-12 00:23:11 +00:00
|
|
|
vn = vn_softc[unit];
|
|
|
|
if (!vn) {
|
|
|
|
vn = malloc(sizeof *vn, M_DEVBUF, M_WAITOK);
|
|
|
|
if (!vn)
|
|
|
|
return (ENOMEM);
|
|
|
|
bzero(vn, sizeof *vn);
|
|
|
|
vn_softc[unit] = vn;
|
|
|
|
}
|
1995-03-11 09:32:45 +00:00
|
|
|
|
|
|
|
IFOPT(vn, VN_FOLLOW)
|
1998-07-11 07:46:16 +00:00
|
|
|
printf("vnopen(0x%lx, 0x%x, 0x%x, %p)\n",
|
|
|
|
(u_long)dev, flags, mode, (void *)p);
|
1995-03-11 09:32:45 +00:00
|
|
|
|
|
|
|
IFOPT(vn, VN_LABELS) {
|
|
|
|
if (vn->sc_flags & VNF_INITED) {
|
|
|
|
struct disklabel label;
|
|
|
|
|
|
|
|
/* Build label for whole disk. */
|
|
|
|
bzero(&label, sizeof label);
|
1999-03-14 09:20:01 +00:00
|
|
|
label.d_secsize = vn->sc_secsize;
|
1995-03-11 09:32:45 +00:00
|
|
|
label.d_nsectors = 32;
|
1999-03-14 09:20:01 +00:00
|
|
|
label.d_ntracks = 64 / (vn->sc_secsize / DEV_BSIZE);
|
|
|
|
label.d_secpercyl = label.d_nsectors * label.d_ntracks;
|
|
|
|
label.d_ncylinders = vn->sc_size / label.d_secpercyl;
|
|
|
|
label.d_secperunit = vn->sc_size;
|
|
|
|
label.d_partitions[RAW_PART].p_size = vn->sc_size;
|
1995-03-11 09:32:45 +00:00
|
|
|
|
1998-07-30 15:16:06 +00:00
|
|
|
return (dsopen("vn", dev, mode, 0, &vn->sc_slices,
|
|
|
|
&label, vnstrategy, (ds_setgeom_t *)NULL,
|
1998-08-23 20:16:35 +00:00
|
|
|
&vn_cdevsw));
|
1995-03-11 09:32:45 +00:00
|
|
|
}
|
1995-05-30 08:16:23 +00:00
|
|
|
if (dkslice(dev) != WHOLE_DISK_SLICE ||
|
1995-03-11 09:32:45 +00:00
|
|
|
dkpart(dev) != RAW_PART ||
|
|
|
|
mode != S_IFCHR)
|
|
|
|
return (ENXIO);
|
1994-12-04 20:08:35 +00:00
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-03-14 09:20:01 +00:00
|
|
|
* vnstrategy:
|
|
|
|
*
|
|
|
|
* Run strategy routine for VN device. We use VOP_READ/VOP_WRITE calls
|
|
|
|
* for vnode-backed vn's, and the new vm_pager_strategy() call for
|
|
|
|
* vm_object-backed vn's.
|
|
|
|
*
|
|
|
|
* Currently B_ASYNC is only partially handled - for OBJT_SWAP I/O only.
|
|
|
|
*
|
|
|
|
* NOTE: bp->b_blkno is DEV_BSIZE'd. We must generate bp->b_pblkno for
|
|
|
|
* our uio or vn_pager_strategy() call that is vn->sc_secsize'd
|
1994-12-04 20:08:35 +00:00
|
|
|
*/
|
1999-03-14 09:20:01 +00:00
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static void
|
1994-12-04 20:08:35 +00:00
|
|
|
vnstrategy(struct buf *bp)
|
|
|
|
{
|
|
|
|
int unit = vnunit(bp->b_dev);
|
1999-03-14 09:20:01 +00:00
|
|
|
struct vn_softc *vn = vn_softc[unit];
|
1995-04-19 10:31:18 +00:00
|
|
|
int error;
|
1997-10-21 09:51:47 +00:00
|
|
|
int isvplocked = 0;
|
1996-01-14 20:32:14 +00:00
|
|
|
long sz;
|
1995-04-19 10:31:18 +00:00
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
IFOPT(vn, VN_DEBUG)
|
1995-02-14 06:06:07 +00:00
|
|
|
printf("vnstrategy(%p): unit %d\n", bp, unit);
|
1995-03-11 09:32:45 +00:00
|
|
|
|
1994-12-04 20:08:35 +00:00
|
|
|
if ((vn->sc_flags & VNF_INITED) == 0) {
|
|
|
|
bp->b_error = ENXIO;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
1999-03-14 09:20:01 +00:00
|
|
|
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
IFOPT(vn, VN_LABELS) {
|
|
|
|
if (vn->sc_slices != NULL && dscheck(bp, vn->sc_slices) <= 0) {
|
1999-03-14 09:20:01 +00:00
|
|
|
bp->b_flags |= B_INVAL;
|
1995-03-11 09:32:45 +00:00
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
1999-03-14 09:20:01 +00:00
|
|
|
int pbn;
|
|
|
|
|
|
|
|
pbn = bp->b_blkno * (vn->sc_secsize / DEV_BSIZE);
|
|
|
|
sz = howmany(bp->b_bcount, vn->sc_secsize);
|
|
|
|
|
|
|
|
if (pbn < 0 || pbn + sz > vn->sc_size) {
|
|
|
|
if (pbn != vn->sc_size) {
|
1995-03-11 09:32:45 +00:00
|
|
|
bp->b_error = EINVAL;
|
1999-03-14 09:20:01 +00:00
|
|
|
bp->b_flags |= B_ERROR | B_INVAL;
|
1995-03-11 09:32:45 +00:00
|
|
|
}
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
1994-12-04 20:08:35 +00:00
|
|
|
}
|
1999-03-14 09:20:01 +00:00
|
|
|
bp->b_pblkno = pbn;
|
1994-12-04 20:08:35 +00:00
|
|
|
}
|
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
if (vn->sc_vp && (bp->b_flags & B_FREEBUF)) {
|
|
|
|
/*
|
|
|
|
* Not handled for vnode-backed element yet.
|
|
|
|
*/
|
|
|
|
biodone(bp);
|
|
|
|
} else if (vn->sc_vp) {
|
|
|
|
/*
|
|
|
|
* VNODE I/O
|
|
|
|
*/
|
1995-04-19 10:31:18 +00:00
|
|
|
aiov.iov_base = bp->b_data;
|
|
|
|
aiov.iov_len = bp->b_bcount;
|
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
1999-03-14 09:20:01 +00:00
|
|
|
auio.uio_offset = (vm_ooffset_t)bp->b_pblkno * vn->sc_secsize;
|
1995-04-19 10:31:18 +00:00
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
if( bp->b_flags & B_READ)
|
|
|
|
auio.uio_rw = UIO_READ;
|
1994-12-04 20:08:35 +00:00
|
|
|
else
|
1995-04-19 10:31:18 +00:00
|
|
|
auio.uio_rw = UIO_WRITE;
|
|
|
|
auio.uio_resid = bp->b_bcount;
|
|
|
|
auio.uio_procp = curproc;
|
1997-10-21 09:51:47 +00:00
|
|
|
if (!VOP_ISLOCKED(vn->sc_vp)) {
|
|
|
|
isvplocked = 1;
|
|
|
|
vn_lock(vn->sc_vp, LK_EXCLUSIVE | LK_RETRY, curproc);
|
|
|
|
}
|
1995-04-19 10:31:18 +00:00
|
|
|
if( bp->b_flags & B_READ)
|
|
|
|
error = VOP_READ(vn->sc_vp, &auio, 0, vn->sc_cred);
|
|
|
|
else
|
|
|
|
error = VOP_WRITE(vn->sc_vp, &auio, 0, vn->sc_cred);
|
1997-10-21 09:51:47 +00:00
|
|
|
if (isvplocked) {
|
|
|
|
VOP_UNLOCK(vn->sc_vp, 0, curproc);
|
|
|
|
isvplocked = 0;
|
|
|
|
}
|
1995-04-19 10:31:18 +00:00
|
|
|
bp->b_resid = auio.uio_resid;
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
if( error ) {
|
|
|
|
bp->b_error = error;
|
1995-04-19 10:31:18 +00:00
|
|
|
bp->b_flags |= B_ERROR;
|
1999-03-14 09:20:01 +00:00
|
|
|
}
|
1995-04-19 10:31:18 +00:00
|
|
|
biodone(bp);
|
1999-03-14 09:20:01 +00:00
|
|
|
} else if (vn->sc_object) {
|
|
|
|
/*
|
|
|
|
* OBJT_SWAP I/O
|
|
|
|
*
|
|
|
|
* ( handles read, write, freebuf )
|
|
|
|
*/
|
|
|
|
vm_pager_strategy(vn->sc_object, bp);
|
1995-04-19 10:31:18 +00:00
|
|
|
} else {
|
1999-03-14 09:20:01 +00:00
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_error = EINVAL;
|
1995-04-19 10:31:18 +00:00
|
|
|
biodone(bp);
|
1994-12-04 20:08:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-04-19 23:32:49 +00:00
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
#if 0
|
|
|
|
|
1994-12-04 20:08:35 +00:00
|
|
|
void
|
1995-04-19 10:31:18 +00:00
|
|
|
vniodone( struct buf *bp) {
|
|
|
|
bp->b_flags |= B_DONE;
|
|
|
|
wakeup((caddr_t) bp);
|
1994-12-04 20:08:35 +00:00
|
|
|
}
|
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
#endif
|
|
|
|
|
1994-12-04 20:08:35 +00:00
|
|
|
/* ARGSUSED */
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1998-06-07 17:13:14 +00:00
|
|
|
vnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
|
1994-12-04 20:08:35 +00:00
|
|
|
{
|
1995-03-11 09:32:45 +00:00
|
|
|
struct vn_softc *vn = vn_softc[vnunit(dev)];
|
1994-12-04 20:08:35 +00:00
|
|
|
struct vn_ioctl *vio;
|
|
|
|
int error;
|
1995-03-11 09:32:45 +00:00
|
|
|
u_long *f;
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
IFOPT(vn,VN_FOLLOW)
|
1998-07-11 07:46:16 +00:00
|
|
|
printf("vnioctl(0x%lx, 0x%lx, %p, 0x%x, %p): unit %d\n",
|
|
|
|
(u_long)dev, cmd, (void *)data, flag, (void *)p,
|
|
|
|
vnunit(dev));
|
1994-12-12 00:23:11 +00:00
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case VNIOCATTACH:
|
|
|
|
case VNIOCDETACH:
|
|
|
|
case VNIOCGSET:
|
|
|
|
case VNIOCGCLEAR:
|
|
|
|
case VNIOCUSET:
|
|
|
|
case VNIOCUCLEAR:
|
|
|
|
goto vn_specific;
|
1994-12-12 00:23:11 +00:00
|
|
|
}
|
1995-03-11 09:32:45 +00:00
|
|
|
|
|
|
|
IFOPT(vn,VN_LABELS) {
|
|
|
|
if (vn->sc_slices != NULL) {
|
1995-04-30 15:14:34 +00:00
|
|
|
error = dsioctl("vn", dev, cmd, data, flag,
|
|
|
|
&vn->sc_slices, vnstrategy,
|
|
|
|
(ds_setgeom_t *)NULL);
|
1997-12-06 14:27:56 +00:00
|
|
|
if (error != ENOIOCTL)
|
1995-03-11 09:32:45 +00:00
|
|
|
return (error);
|
|
|
|
}
|
1995-05-30 08:16:23 +00:00
|
|
|
if (dkslice(dev) != WHOLE_DISK_SLICE ||
|
1995-03-11 09:32:45 +00:00
|
|
|
dkpart(dev) != RAW_PART)
|
|
|
|
return (ENOTTY);
|
|
|
|
}
|
|
|
|
|
|
|
|
vn_specific:
|
1994-12-12 00:23:11 +00:00
|
|
|
|
1999-04-27 11:18:52 +00:00
|
|
|
error = suser(p);
|
1994-12-04 20:08:35 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
vio = (struct vn_ioctl *)data;
|
1995-03-11 09:32:45 +00:00
|
|
|
f = (u_long*)data;
|
1994-12-04 20:08:35 +00:00
|
|
|
switch (cmd) {
|
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
case VNIOCATTACH:
|
1994-12-04 20:08:35 +00:00
|
|
|
if (vn->sc_flags & VNF_INITED)
|
|
|
|
return(EBUSY);
|
1999-03-14 09:20:01 +00:00
|
|
|
|
|
|
|
if (vio->vn_file == NULL)
|
|
|
|
error = vniocattach_swap(vn, vio, dev, flag, p);
|
|
|
|
else
|
|
|
|
error = vniocattach_file(vn, vio, dev, flag, p);
|
1994-12-04 20:08:35 +00:00
|
|
|
break;
|
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
case VNIOCDETACH:
|
1994-12-04 20:08:35 +00:00
|
|
|
if ((vn->sc_flags & VNF_INITED) == 0)
|
|
|
|
return(ENXIO);
|
1994-12-16 16:50:15 +00:00
|
|
|
/*
|
|
|
|
* XXX handle i/o in progress. Return EBUSY, or wait, or
|
|
|
|
* flush the i/o.
|
|
|
|
* XXX handle multiple opens of the device. Return EBUSY,
|
|
|
|
* or revoke the fd's.
|
|
|
|
* How are these problems handled for removable and failing
|
|
|
|
* hardware devices?
|
|
|
|
*/
|
1994-12-04 20:08:35 +00:00
|
|
|
vnclear(vn);
|
1995-03-11 09:32:45 +00:00
|
|
|
IFOPT(vn, VN_FOLLOW)
|
1994-12-04 20:08:35 +00:00
|
|
|
printf("vnioctl: CLRed\n");
|
1995-03-11 09:32:45 +00:00
|
|
|
break;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
case VNIOCGSET:
|
|
|
|
vn_options |= *f;
|
|
|
|
*f = vn_options;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VNIOCGCLEAR:
|
|
|
|
vn_options &= ~(*f);
|
|
|
|
*f = vn_options;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VNIOCUSET:
|
|
|
|
vn->sc_options |= *f;
|
|
|
|
*f = vn->sc_options;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VNIOCUCLEAR:
|
|
|
|
vn->sc_options &= ~(*f);
|
|
|
|
*f = vn->sc_options;
|
1994-12-04 20:08:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-03-14 09:20:01 +00:00
|
|
|
error = ENOTTY;
|
|
|
|
break;
|
1994-12-04 20:08:35 +00:00
|
|
|
}
|
1999-03-14 09:20:01 +00:00
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vniocattach_file:
|
|
|
|
*
|
|
|
|
* Attach a file to a VN partition. Return the size in the vn_size
|
|
|
|
* field.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
vniocattach_file(vn, vio, dev, flag, p)
|
|
|
|
struct vn_softc *vn;
|
|
|
|
struct vn_ioctl *vio;
|
|
|
|
dev_t dev;
|
|
|
|
int flag;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
|
|
|
struct vattr vattr;
|
|
|
|
struct nameidata nd;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Always open for read and write.
|
|
|
|
* This is probably bogus, but it lets vn_open()
|
|
|
|
* weed out directories, sockets, etc. so we don't
|
|
|
|
* have to worry about them.
|
|
|
|
*/
|
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vn_file, p);
|
|
|
|
error = vn_open(&nd, FREAD|FWRITE, 0);
|
|
|
|
if (error)
|
|
|
|
return(error);
|
|
|
|
error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
|
|
|
|
if (error) {
|
|
|
|
VOP_UNLOCK(nd.ni_vp, 0, p);
|
|
|
|
(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
|
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
VOP_UNLOCK(nd.ni_vp, 0, p);
|
|
|
|
vn->sc_secsize = DEV_BSIZE;
|
|
|
|
vn->sc_vp = nd.ni_vp;
|
|
|
|
vn->sc_size = vattr.va_size / vn->sc_secsize; /* note truncation */
|
|
|
|
error = vnsetcred(vn, p->p_ucred);
|
|
|
|
if (error) {
|
|
|
|
(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
|
|
|
|
return(error);
|
|
|
|
}
|
1999-07-20 09:47:55 +00:00
|
|
|
if (dev->si_bsize_phys < vn->sc_secsize)
|
|
|
|
dev->si_bsize_phys = vn->sc_secsize;
|
|
|
|
if (dev->si_bsize_best < vn->sc_secsize)
|
|
|
|
dev->si_bsize_best = vn->sc_secsize;
|
1999-03-14 09:20:01 +00:00
|
|
|
vn->sc_flags |= VNF_INITED;
|
|
|
|
IFOPT(vn, VN_LABELS) {
|
|
|
|
/*
|
|
|
|
* Reopen so that `ds' knows which devices are open.
|
|
|
|
* If this is the first VNIOCSET, then we've
|
|
|
|
* guaranteed that the device is the cdev and that
|
|
|
|
* no other slices or labels are open. Otherwise,
|
|
|
|
* we rely on VNIOCCLR not being abused.
|
|
|
|
*/
|
|
|
|
error = vnopen(dev, flag, S_IFCHR, p);
|
|
|
|
if (error)
|
|
|
|
vnclear(vn);
|
|
|
|
}
|
|
|
|
IFOPT(vn, VN_FOLLOW)
|
|
|
|
printf("vnioctl: SET vp %p size %x blks\n",
|
|
|
|
vn->sc_vp, vn->sc_size);
|
1994-12-04 20:08:35 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
/*
|
|
|
|
* vniocattach_swap:
|
|
|
|
*
|
|
|
|
* Attach swap backing store to a VN partition of the size specified
|
|
|
|
* in vn_size.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
vniocattach_swap(vn, vio, dev, flag, p)
|
|
|
|
struct vn_softc *vn;
|
|
|
|
struct vn_ioctl *vio;
|
|
|
|
dev_t dev;
|
|
|
|
int flag;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Range check. Disallow negative sizes or any size less then the
|
|
|
|
* size of a page. Then round to a page.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (vio->vn_size <= 0)
|
|
|
|
return(EDOM);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate an OBJT_SWAP object.
|
|
|
|
*
|
|
|
|
* sc_secsize is PAGE_SIZE'd
|
|
|
|
*
|
|
|
|
* vio->vn_size is in PAGE_SIZE'd chunks.
|
|
|
|
* sc_size must be in PAGE_SIZE'd chunks.
|
|
|
|
* Note the truncation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vn->sc_secsize = PAGE_SIZE;
|
|
|
|
vn->sc_size = vio->vn_size;
|
|
|
|
vn->sc_object =
|
|
|
|
vm_pager_allocate(OBJT_SWAP, NULL, vn->sc_secsize * (vm_ooffset_t)vio->vn_size, VM_PROT_DEFAULT, 0);
|
|
|
|
vn->sc_flags |= VNF_INITED;
|
|
|
|
|
|
|
|
error = vnsetcred(vn, p->p_ucred);
|
|
|
|
if (error == 0) {
|
|
|
|
IFOPT(vn, VN_LABELS) {
|
|
|
|
/*
|
|
|
|
* Reopen so that `ds' knows which devices are open.
|
|
|
|
* If this is the first VNIOCSET, then we've
|
|
|
|
* guaranteed that the device is the cdev and that
|
|
|
|
* no other slices or labels are open. Otherwise,
|
|
|
|
* we rely on VNIOCCLR not being abused.
|
|
|
|
*/
|
|
|
|
error = vnopen(dev, flag, S_IFCHR, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error == 0) {
|
|
|
|
IFOPT(vn, VN_FOLLOW) {
|
|
|
|
printf("vnioctl: SET vp %p size %x\n",
|
|
|
|
vn->sc_vp, vn->sc_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
vnclear(vn);
|
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
|
1994-12-04 20:08:35 +00:00
|
|
|
/*
|
|
|
|
* Duplicate the current processes' credentials. Since we are called only
|
|
|
|
* as the result of a SET ioctl and only root can do that, any future access
|
|
|
|
* to this "disk" is essentially as root. Note that credentials may change
|
|
|
|
* if some other uid can write directly to the mapped file (NFS).
|
|
|
|
*/
|
1995-05-30 08:16:23 +00:00
|
|
|
int
|
1994-12-04 20:08:35 +00:00
|
|
|
vnsetcred(struct vn_softc *vn, struct ucred *cred)
|
|
|
|
{
|
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
|
|
|
char *tmpbuf;
|
1999-03-14 09:20:01 +00:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set credits in our softc
|
|
|
|
*/
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
if (vn->sc_cred)
|
|
|
|
crfree(vn->sc_cred);
|
1994-12-04 20:08:35 +00:00
|
|
|
vn->sc_cred = crdup(cred);
|
1999-03-14 09:20:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Horrible kludge to establish credentials for NFS XXX.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (vn->sc_vp) {
|
|
|
|
tmpbuf = malloc(vn->sc_secsize, M_TEMP, M_WAITOK);
|
|
|
|
|
|
|
|
aiov.iov_base = tmpbuf;
|
|
|
|
aiov.iov_len = vn->sc_secsize;
|
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
auio.uio_offset = 0;
|
|
|
|
auio.uio_rw = UIO_READ;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
auio.uio_resid = aiov.iov_len;
|
|
|
|
vn_lock(vn->sc_vp, LK_EXCLUSIVE | LK_RETRY, curproc);
|
|
|
|
error = VOP_READ(vn->sc_vp, &auio, 0, vn->sc_cred);
|
|
|
|
VOP_UNLOCK(vn->sc_vp, 0, curproc);
|
|
|
|
free(tmpbuf, M_TEMP);
|
|
|
|
}
|
1994-12-04 20:08:35 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vnclear(struct vn_softc *vn)
|
|
|
|
{
|
|
|
|
struct proc *p = curproc; /* XXX */
|
|
|
|
|
1995-03-11 09:32:45 +00:00
|
|
|
IFOPT(vn, VN_FOLLOW)
|
1999-03-14 09:20:01 +00:00
|
|
|
printf("vnclear(%p): vp=%p\n", vn, vn->sc_vp);
|
1998-04-19 23:32:49 +00:00
|
|
|
if (vn->sc_slices != NULL)
|
|
|
|
dsgone(&vn->sc_slices);
|
1994-12-04 20:08:35 +00:00
|
|
|
vn->sc_flags &= ~VNF_INITED;
|
1999-03-14 09:20:01 +00:00
|
|
|
if (vn->sc_vp != NULL) {
|
|
|
|
(void)vn_close(vn->sc_vp, FREAD|FWRITE, vn->sc_cred, p);
|
|
|
|
vn->sc_vp = NULL;
|
|
|
|
}
|
|
|
|
if (vn->sc_cred) {
|
|
|
|
crfree(vn->sc_cred);
|
|
|
|
vn->sc_cred = NULL;
|
|
|
|
}
|
|
|
|
if (vn->sc_object != NULL) {
|
|
|
|
vm_pager_deallocate(vn->sc_object);
|
|
|
|
vn->sc_object = NULL;
|
|
|
|
}
|
1994-12-04 20:08:35 +00:00
|
|
|
vn->sc_size = 0;
|
|
|
|
}
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1994-12-04 20:08:35 +00:00
|
|
|
vnsize(dev_t dev)
|
|
|
|
{
|
|
|
|
int unit = vnunit(dev);
|
1999-03-14 09:20:01 +00:00
|
|
|
struct vn_softc *vn;
|
1994-12-04 20:08:35 +00:00
|
|
|
|
1999-03-14 09:20:01 +00:00
|
|
|
if (unit < 0 || unit >= NVN)
|
1994-12-04 20:08:35 +00:00
|
|
|
return(-1);
|
1999-03-14 09:20:01 +00:00
|
|
|
|
|
|
|
vn = vn_softc[unit];
|
|
|
|
if ((vn->sc_flags & VNF_INITED) == 0)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
return(vn->sc_size);
|
|
|
|
}
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1994-12-04 20:08:35 +00:00
|
|
|
vndump(dev_t dev)
|
|
|
|
{
|
1994-12-16 16:50:15 +00:00
|
|
|
return (ENODEV);
|
1994-12-04 20:08:35 +00:00
|
|
|
}
|
1998-02-20 13:27:36 +00:00
|
|
|
|
1999-01-21 17:17:02 +00:00
|
|
|
static int
|
|
|
|
vn_modevent(module_t mod, int type, void *data)
|
1995-11-29 10:49:16 +00:00
|
|
|
{
|
1998-09-12 18:46:06 +00:00
|
|
|
int unit;
|
1999-01-23 00:28:56 +00:00
|
|
|
#ifdef DEVFS
|
1999-01-21 17:17:02 +00:00
|
|
|
struct vn_softc *vn;
|
1999-01-23 00:28:56 +00:00
|
|
|
#endif
|
1999-01-21 17:17:02 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
1998-09-12 18:46:06 +00:00
|
|
|
#ifdef DEVFS
|
1999-01-21 17:17:02 +00:00
|
|
|
for (unit = 0; unit < NVN; unit++) {
|
|
|
|
vn = malloc(sizeof *vn, M_DEVBUF, M_WAITOK);
|
|
|
|
if (!vn)
|
|
|
|
continue; /* "oops" */
|
|
|
|
bzero(vn, sizeof *vn);
|
|
|
|
vn_softc[unit] = vn;
|
|
|
|
vn->r_devfs_token = devfs_add_devswf(&vn_cdevsw,
|
|
|
|
dkmakeminor(unit, 0, 0),
|
|
|
|
DV_CHR, UID_ROOT,
|
|
|
|
GID_OPERATOR, 0640,
|
|
|
|
"rvn%d", unit);
|
|
|
|
vn->devfs_token = devfs_add_devswf(&vn_cdevsw,
|
|
|
|
dkmakeminor(unit, 0, 0),
|
|
|
|
DV_BLK, UID_ROOT,
|
|
|
|
GID_OPERATOR, 0640,
|
|
|
|
"vn%d", unit);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
1998-09-12 18:46:06 +00:00
|
|
|
|
1999-01-21 17:17:02 +00:00
|
|
|
case MOD_UNLOAD:
|
|
|
|
#ifdef DEVFS
|
|
|
|
for (unit = 0; unit < NVN; unit++) {
|
|
|
|
vn = vn_softc[unit];
|
|
|
|
if (vn->r_devfs_token) {
|
|
|
|
devfs_remove_dev(vn->r_devfs_token);
|
|
|
|
vn->r_devfs_token = 0;
|
|
|
|
}
|
|
|
|
if (vn->devfs_token) {
|
|
|
|
devfs_remove_dev(vn->devfs_token);
|
|
|
|
vn->devfs_token = 0;
|
|
|
|
}
|
|
|
|
}
|
1998-09-12 18:46:06 +00:00
|
|
|
#endif
|
1999-01-21 17:17:02 +00:00
|
|
|
/* fall through */
|
|
|
|
case MOD_SHUTDOWN:
|
|
|
|
for (unit = 0; unit < NVN; unit++)
|
|
|
|
if (vn_softc[unit] &&
|
|
|
|
vn_softc[unit]->sc_flags & VNF_INITED)
|
|
|
|
vnclear(vn_softc[unit]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
1995-11-29 10:49:16 +00:00
|
|
|
}
|
|
|
|
|
1999-05-07 10:11:40 +00:00
|
|
|
DEV_MODULE(vn, CDEV_MAJOR, BDEV_MAJOR, vn_cdevsw, vn_modevent, 0);
|
1995-11-29 10:49:16 +00:00
|
|
|
|
1994-12-04 20:08:35 +00:00
|
|
|
#endif
|