1999-09-21 11:00:49 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2001-01-02 09:42:47 +00:00
|
|
|
/*
|
|
|
|
* The following functions are based in the vn(4) driver: mdstart_swap(),
|
|
|
|
* mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(),
|
|
|
|
* and as such under the following copyright:
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* from: @(#)vn.c 8.6 (Berkeley) 4/1/94
|
2002-01-21 20:57:03 +00:00
|
|
|
* From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
|
2001-01-02 09:42:47 +00:00
|
|
|
*/
|
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
#include "opt_md.h"
|
1999-11-26 20:08:44 +00:00
|
|
|
|
1999-09-21 11:00:49 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2000-05-05 09:59:14 +00:00
|
|
|
#include <sys/bio.h>
|
1999-09-21 11:00:49 +00:00
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/devicestat.h>
|
1999-09-22 21:23:21 +00:00
|
|
|
#include <sys/disk.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/fcntl.h>
|
1999-09-22 21:23:21 +00:00
|
|
|
#include <sys/kernel.h>
|
2002-06-03 22:09:04 +00:00
|
|
|
#include <sys/kthread.h>
|
1999-11-20 22:43:27 +00:00
|
|
|
#include <sys/linker.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
2000-12-31 13:03:42 +00:00
|
|
|
#include <sys/mdioctl.h>
|
2001-05-21 18:52:02 +00:00
|
|
|
#include <sys/mutex.h>
|
2000-12-31 13:03:42 +00:00
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/proc.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/queue.h>
|
2002-06-24 12:07:02 +00:00
|
|
|
#include <sys/stdint.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_object.h>
|
|
|
|
#include <vm/vm_page.h>
|
|
|
|
#include <vm/vm_pager.h>
|
|
|
|
#include <vm/swap_pager.h>
|
2002-05-26 06:48:55 +00:00
|
|
|
#include <vm/uma.h>
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2001-02-24 16:26:41 +00:00
|
|
|
#define MD_MODVER 1
|
|
|
|
|
2002-06-03 22:09:04 +00:00
|
|
|
#define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
|
|
|
|
|
2000-07-17 13:13:04 +00:00
|
|
|
#ifndef MD_NSECT
|
|
|
|
#define MD_NSECT (10000 * 2)
|
1999-09-21 12:05:38 +00:00
|
|
|
#endif
|
|
|
|
|
2002-02-10 21:42:44 +00:00
|
|
|
static MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
|
|
|
|
static MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
|
1999-09-21 11:00:49 +00:00
|
|
|
|
1999-11-26 20:08:44 +00:00
|
|
|
static int md_debug;
|
1999-09-21 11:00:49 +00:00
|
|
|
SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
|
|
|
|
|
1999-11-26 20:08:44 +00:00
|
|
|
#if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
|
|
|
|
/* Image gets put here: */
|
|
|
|
static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
|
|
|
|
static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
|
|
|
|
#endif
|
|
|
|
|
2001-02-24 16:26:41 +00:00
|
|
|
static int mdrootready;
|
|
|
|
static int mdunits;
|
|
|
|
static dev_t status_dev = 0;
|
|
|
|
|
1999-09-21 11:00:49 +00:00
|
|
|
#define CDEV_MAJOR 95
|
|
|
|
|
|
|
|
static d_strategy_t mdstrategy;
|
|
|
|
static d_open_t mdopen;
|
2001-08-02 10:19:13 +00:00
|
|
|
static d_close_t mdclose;
|
2000-12-31 13:03:42 +00:00
|
|
|
static d_ioctl_t mdioctl, mdctlioctl;
|
1999-09-21 11:00:49 +00:00
|
|
|
|
|
|
|
static struct cdevsw md_cdevsw = {
|
|
|
|
/* open */ mdopen,
|
2001-08-02 10:19:13 +00:00
|
|
|
/* close */ mdclose,
|
1999-09-21 11:00:49 +00:00
|
|
|
/* read */ physread,
|
|
|
|
/* write */ physwrite,
|
|
|
|
/* ioctl */ mdioctl,
|
|
|
|
/* poll */ nopoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ mdstrategy,
|
2001-02-25 13:12:57 +00:00
|
|
|
/* name */ MD_NAME,
|
1999-09-21 11:00:49 +00:00
|
|
|
/* maj */ CDEV_MAJOR,
|
|
|
|
/* dump */ nodump,
|
|
|
|
/* psize */ nopsize,
|
1999-11-26 20:08:44 +00:00
|
|
|
/* flags */ D_DISK | D_CANFREE | D_MEMDISK,
|
2000-12-31 13:03:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct cdevsw mdctl_cdevsw = {
|
|
|
|
/* open */ nullopen,
|
|
|
|
/* close */ nullclose,
|
|
|
|
/* read */ noread,
|
|
|
|
/* write */ nowrite,
|
|
|
|
/* ioctl */ mdctlioctl,
|
|
|
|
/* poll */ nopoll,
|
|
|
|
/* mmap */ nommap,
|
|
|
|
/* strategy */ nostrategy,
|
2001-02-25 13:12:57 +00:00
|
|
|
/* name */ MD_NAME,
|
2000-12-31 13:03:42 +00:00
|
|
|
/* maj */ CDEV_MAJOR
|
1999-09-21 11:00:49 +00:00
|
|
|
};
|
|
|
|
|
2000-07-04 10:06:34 +00:00
|
|
|
static struct cdevsw mddisk_cdevsw;
|
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list);
|
|
|
|
|
2002-05-25 20:44:20 +00:00
|
|
|
#define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
|
|
|
|
#define NMASK (NINDIR-1)
|
|
|
|
static int nshift;
|
|
|
|
|
|
|
|
struct indir {
|
|
|
|
uintptr_t *array;
|
|
|
|
uint total;
|
|
|
|
uint used;
|
|
|
|
uint shift;
|
|
|
|
};
|
|
|
|
|
1999-09-21 11:00:49 +00:00
|
|
|
struct md_s {
|
|
|
|
int unit;
|
2000-08-20 21:34:39 +00:00
|
|
|
LIST_ENTRY(md_s) list;
|
1999-09-21 11:00:49 +00:00
|
|
|
struct devstat stats;
|
2000-04-15 05:54:02 +00:00
|
|
|
struct bio_queue_head bio_queue;
|
1999-09-21 11:00:49 +00:00
|
|
|
struct disk disk;
|
|
|
|
dev_t dev;
|
2000-12-31 13:03:42 +00:00
|
|
|
enum md_types type;
|
1999-09-21 11:00:49 +00:00
|
|
|
unsigned nsect;
|
2001-08-02 10:19:13 +00:00
|
|
|
unsigned opencount;
|
2000-12-31 13:03:42 +00:00
|
|
|
unsigned secsize;
|
|
|
|
unsigned flags;
|
2002-05-26 06:48:55 +00:00
|
|
|
char name[20];
|
2002-06-03 22:09:04 +00:00
|
|
|
struct proc *procp;
|
1999-11-20 22:43:27 +00:00
|
|
|
|
|
|
|
/* MD_MALLOC related fields */
|
2002-05-25 20:44:20 +00:00
|
|
|
struct indir *indir;
|
2002-05-26 06:48:55 +00:00
|
|
|
uma_zone_t uma;
|
1999-09-21 11:00:49 +00:00
|
|
|
|
1999-11-20 22:43:27 +00:00
|
|
|
/* MD_PRELOAD related fields */
|
|
|
|
u_char *pl_ptr;
|
|
|
|
unsigned pl_len;
|
1999-09-21 11:00:49 +00:00
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
/* MD_VNODE related fields */
|
|
|
|
struct vnode *vnode;
|
|
|
|
struct ucred *cred;
|
|
|
|
|
2001-08-16 03:04:49 +00:00
|
|
|
/* MD_SWAP related fields */
|
2000-12-31 13:03:42 +00:00
|
|
|
vm_object_t object;
|
|
|
|
};
|
1999-09-21 11:00:49 +00:00
|
|
|
|
2002-05-25 20:44:20 +00:00
|
|
|
static int mddestroy(struct md_s *sc, struct thread *td);
|
|
|
|
|
|
|
|
static struct indir *
|
|
|
|
new_indir(uint shift)
|
|
|
|
{
|
|
|
|
struct indir *ip;
|
|
|
|
|
|
|
|
ip = malloc(sizeof *ip, M_MD, M_NOWAIT | M_ZERO);
|
|
|
|
if (ip == NULL)
|
2002-06-10 19:25:21 +00:00
|
|
|
return (NULL);
|
2002-05-25 20:44:20 +00:00
|
|
|
ip->array = malloc(sizeof(uintptr_t) * NINDIR,
|
|
|
|
M_MDSECT, M_NOWAIT | M_ZERO);
|
|
|
|
if (ip->array == NULL) {
|
|
|
|
free(ip, M_MD);
|
2002-06-10 19:25:21 +00:00
|
|
|
return (NULL);
|
2002-05-25 20:44:20 +00:00
|
|
|
}
|
|
|
|
ip->total = NINDIR;
|
|
|
|
ip->shift = shift;
|
2002-06-10 19:25:21 +00:00
|
|
|
return (ip);
|
2002-05-25 20:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
del_indir(struct indir *ip)
|
|
|
|
{
|
|
|
|
|
2002-05-26 06:48:55 +00:00
|
|
|
free(ip->array, M_MDSECT);
|
2002-05-25 20:44:20 +00:00
|
|
|
free(ip, M_MD);
|
|
|
|
}
|
|
|
|
|
2002-05-26 06:48:55 +00:00
|
|
|
static void
|
|
|
|
destroy_indir(struct md_s *sc, struct indir *ip)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NINDIR; i++) {
|
|
|
|
if (!ip->array[i])
|
|
|
|
continue;
|
|
|
|
if (ip->shift)
|
|
|
|
destroy_indir(sc, (struct indir*)(ip->array[i]));
|
|
|
|
else if (ip->array[i] > 255)
|
|
|
|
uma_zfree(sc->uma, (void *)(ip->array[i]));
|
|
|
|
}
|
|
|
|
del_indir(ip);
|
|
|
|
}
|
|
|
|
|
2002-05-25 20:44:20 +00:00
|
|
|
/*
|
|
|
|
* This function does the math and alloctes the top level "indir" structure
|
|
|
|
* for a device of "size" sectors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct indir *
|
|
|
|
dimension(off_t size)
|
|
|
|
{
|
|
|
|
off_t rcnt;
|
|
|
|
struct indir *ip;
|
|
|
|
int i, layer;
|
|
|
|
|
|
|
|
rcnt = size;
|
|
|
|
layer = 0;
|
|
|
|
while (rcnt > NINDIR) {
|
|
|
|
rcnt /= NINDIR;
|
|
|
|
layer++;
|
|
|
|
}
|
|
|
|
/* figure out log2(NINDIR) */
|
|
|
|
for (i = NINDIR, nshift = -1; i; nshift++)
|
|
|
|
i >>= 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: the top layer is probably not fully populated, so we allocate
|
|
|
|
* too much space for ip->array in new_indir() here.
|
|
|
|
*/
|
|
|
|
ip = new_indir(layer * nshift);
|
|
|
|
return (ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a given sector
|
|
|
|
*/
|
|
|
|
|
|
|
|
static uintptr_t
|
|
|
|
s_read(struct indir *ip, off_t offset)
|
|
|
|
{
|
|
|
|
struct indir *cip;
|
|
|
|
int idx;
|
|
|
|
uintptr_t up;
|
|
|
|
|
|
|
|
if (md_debug > 1)
|
2002-06-24 12:07:02 +00:00
|
|
|
printf("s_read(%jd)\n", (intmax_t)offset);
|
2002-05-25 20:44:20 +00:00
|
|
|
up = 0;
|
|
|
|
for (cip = ip; cip != NULL;) {
|
|
|
|
if (cip->shift) {
|
|
|
|
idx = (offset >> cip->shift) & NMASK;
|
|
|
|
up = cip->array[idx];
|
|
|
|
cip = (struct indir *)up;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
idx = offset & NMASK;
|
2002-06-10 19:25:21 +00:00
|
|
|
return (cip->array[idx]);
|
2002-05-25 20:44:20 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a given sector, prune the tree if the value is 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2002-05-26 09:38:51 +00:00
|
|
|
s_write(struct indir *ip, off_t offset, uintptr_t ptr)
|
2002-05-25 20:44:20 +00:00
|
|
|
{
|
|
|
|
struct indir *cip, *lip[10];
|
|
|
|
int idx, li;
|
|
|
|
uintptr_t up;
|
|
|
|
|
|
|
|
if (md_debug > 1)
|
2002-06-24 12:07:02 +00:00
|
|
|
printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
|
2002-05-25 20:44:20 +00:00
|
|
|
up = 0;
|
|
|
|
li = 0;
|
|
|
|
cip = ip;
|
|
|
|
for (;;) {
|
|
|
|
lip[li++] = cip;
|
|
|
|
if (cip->shift) {
|
|
|
|
idx = (offset >> cip->shift) & NMASK;
|
|
|
|
up = cip->array[idx];
|
|
|
|
if (up != 0) {
|
|
|
|
cip = (struct indir *)up;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Allocate branch */
|
|
|
|
cip->array[idx] =
|
|
|
|
(uintptr_t)new_indir(cip->shift - nshift);
|
|
|
|
if (cip->array[idx] == 0)
|
2002-06-10 19:25:21 +00:00
|
|
|
return (ENOMEM);
|
2002-05-25 20:44:20 +00:00
|
|
|
cip->used++;
|
|
|
|
up = cip->array[idx];
|
|
|
|
cip = (struct indir *)up;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* leafnode */
|
|
|
|
idx = offset & NMASK;
|
|
|
|
up = cip->array[idx];
|
|
|
|
if (up != 0)
|
|
|
|
cip->used--;
|
|
|
|
cip->array[idx] = ptr;
|
|
|
|
if (ptr != 0)
|
|
|
|
cip->used++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cip->used != 0 || li == 1)
|
|
|
|
return (0);
|
|
|
|
li--;
|
|
|
|
while (cip->used == 0 && cip != ip) {
|
|
|
|
li--;
|
|
|
|
idx = (offset >> lip[li]->shift) & NMASK;
|
|
|
|
up = lip[li]->array[idx];
|
|
|
|
KASSERT(up == (uintptr_t)cip, ("md screwed up"));
|
|
|
|
del_indir(cip);
|
2002-08-22 21:24:01 +00:00
|
|
|
lip[li]->array[idx] = 0;
|
2002-05-25 20:44:20 +00:00
|
|
|
lip[li]->used--;
|
|
|
|
cip = lip[li];
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-09-21 11:00:49 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
mdopen(dev_t dev, int flag, int fmt, struct thread *td)
|
1999-09-21 11:00:49 +00:00
|
|
|
{
|
|
|
|
struct md_s *sc;
|
|
|
|
|
|
|
|
if (md_debug)
|
2002-06-10 19:25:21 +00:00
|
|
|
printf("mdopen(%s %x %x %p)\n",
|
2002-05-25 20:44:20 +00:00
|
|
|
devtoname(dev), flag, fmt, td);
|
1999-09-21 11:00:49 +00:00
|
|
|
|
|
|
|
sc = dev->si_drv1;
|
|
|
|
|
2002-09-20 19:36:05 +00:00
|
|
|
sc->disk.d_sectorsize = sc->secsize;
|
|
|
|
sc->disk.d_mediasize = (off_t)sc->nsect * sc->secsize;
|
2002-09-22 10:07:18 +00:00
|
|
|
sc->disk.d_fwsectors = sc->nsect > 63 ? 63 : sc->nsect;
|
|
|
|
sc->disk.d_fwheads = 1;
|
2001-08-02 10:19:13 +00:00
|
|
|
sc->opencount++;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
mdclose(dev_t dev, int flags, int fmt, struct thread *td)
|
2001-08-02 10:19:13 +00:00
|
|
|
{
|
|
|
|
struct md_s *sc = dev->si_drv1;
|
|
|
|
|
|
|
|
sc->opencount--;
|
1999-09-21 11:00:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
mdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
|
1999-09-21 11:00:49 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (md_debug)
|
|
|
|
printf("mdioctl(%s %lx %p %x %p)\n",
|
2001-09-12 08:38:13 +00:00
|
|
|
devtoname(dev), cmd, addr, flags, td);
|
1999-09-21 11:00:49 +00:00
|
|
|
|
|
|
|
return (ENOIOCTL);
|
|
|
|
}
|
|
|
|
|
2002-01-21 20:50:06 +00:00
|
|
|
static int
|
|
|
|
mdstart_malloc(struct md_s *sc, struct bio *bp)
|
1999-09-21 11:00:49 +00:00
|
|
|
{
|
2002-05-25 20:44:20 +00:00
|
|
|
int i, error;
|
|
|
|
u_char *dst;
|
|
|
|
unsigned secno, nsec, uc;
|
|
|
|
uintptr_t sp, osp;
|
2002-01-21 20:57:03 +00:00
|
|
|
|
|
|
|
nsec = bp->bio_bcount / sc->secsize;
|
|
|
|
secno = bp->bio_pblkno;
|
|
|
|
dst = bp->bio_data;
|
2002-05-25 20:44:20 +00:00
|
|
|
error = 0;
|
2002-01-21 20:57:03 +00:00
|
|
|
while (nsec--) {
|
2002-05-26 09:38:51 +00:00
|
|
|
osp = s_read(sc->indir, secno);
|
2002-01-21 20:57:03 +00:00
|
|
|
if (bp->bio_cmd == BIO_DELETE) {
|
2002-05-26 09:38:51 +00:00
|
|
|
if (osp != 0)
|
|
|
|
error = s_write(sc->indir, secno, 0);
|
2002-01-21 20:57:03 +00:00
|
|
|
} else if (bp->bio_cmd == BIO_READ) {
|
2002-05-26 09:38:51 +00:00
|
|
|
if (osp == 0)
|
2002-01-21 20:57:03 +00:00
|
|
|
bzero(dst, sc->secsize);
|
2002-05-26 09:38:51 +00:00
|
|
|
else if (osp <= 255)
|
2002-05-25 20:44:20 +00:00
|
|
|
for (i = 0; i < sc->secsize; i++)
|
2002-05-26 09:38:51 +00:00
|
|
|
dst[i] = osp;
|
2002-05-25 20:44:20 +00:00
|
|
|
else
|
2002-05-26 09:38:51 +00:00
|
|
|
bcopy((void *)osp, dst, sc->secsize);
|
|
|
|
osp = 0;
|
2002-05-25 20:44:20 +00:00
|
|
|
} else if (bp->bio_cmd == BIO_WRITE) {
|
2002-01-21 20:57:03 +00:00
|
|
|
if (sc->flags & MD_COMPRESS) {
|
|
|
|
uc = dst[0];
|
|
|
|
for (i = 1; i < sc->secsize; i++)
|
|
|
|
if (dst[i] != uc)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
uc = 0;
|
|
|
|
}
|
|
|
|
if (i == sc->secsize) {
|
2002-05-26 09:38:51 +00:00
|
|
|
if (osp != uc)
|
|
|
|
error = s_write(sc->indir, secno, uc);
|
1999-11-26 20:08:44 +00:00
|
|
|
} else {
|
2002-05-26 09:38:51 +00:00
|
|
|
if (osp <= 255) {
|
2002-05-26 06:48:55 +00:00
|
|
|
sp = (uintptr_t) uma_zalloc(
|
|
|
|
sc->uma, M_NOWAIT);
|
2002-05-26 09:38:51 +00:00
|
|
|
if (sp == 0) {
|
|
|
|
error = ENOSPC;
|
|
|
|
break;
|
|
|
|
}
|
2002-05-26 09:57:59 +00:00
|
|
|
bcopy(dst, (void *)sp, sc->secsize);
|
2002-05-26 09:38:51 +00:00
|
|
|
error = s_write(sc->indir, secno, sp);
|
2002-05-25 20:44:20 +00:00
|
|
|
} else {
|
2002-05-26 09:38:51 +00:00
|
|
|
bcopy(dst, (void *)osp, sc->secsize);
|
|
|
|
osp = 0;
|
2002-05-25 20:44:20 +00:00
|
|
|
}
|
1999-09-21 11:00:49 +00:00
|
|
|
}
|
2002-05-25 20:44:20 +00:00
|
|
|
} else {
|
|
|
|
error = EOPNOTSUPP;
|
1999-09-21 11:00:49 +00:00
|
|
|
}
|
2002-05-25 20:44:20 +00:00
|
|
|
if (osp > 255)
|
2002-05-26 06:48:55 +00:00
|
|
|
uma_zfree(sc->uma, (void*)osp);
|
2002-05-25 20:44:20 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2002-01-21 20:57:03 +00:00
|
|
|
secno++;
|
|
|
|
dst += sc->secsize;
|
|
|
|
}
|
|
|
|
bp->bio_resid = 0;
|
2002-05-25 20:44:20 +00:00
|
|
|
return (error);
|
1999-09-21 11:00:49 +00:00
|
|
|
}
|
|
|
|
|
2002-01-21 20:50:06 +00:00
|
|
|
static int
|
|
|
|
mdstart_preload(struct md_s *sc, struct bio *bp)
|
1999-11-26 20:08:44 +00:00
|
|
|
{
|
|
|
|
|
2002-01-21 20:57:03 +00:00
|
|
|
if (bp->bio_cmd == BIO_DELETE) {
|
|
|
|
} else if (bp->bio_cmd == BIO_READ) {
|
|
|
|
bcopy(sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_data, bp->bio_bcount);
|
|
|
|
} else {
|
|
|
|
bcopy(bp->bio_data, sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_bcount);
|
|
|
|
}
|
|
|
|
bp->bio_resid = 0;
|
2002-01-21 20:50:06 +00:00
|
|
|
return (0);
|
1999-11-26 20:08:44 +00:00
|
|
|
}
|
|
|
|
|
2002-01-21 20:50:06 +00:00
|
|
|
static int
|
|
|
|
mdstart_vnode(struct md_s *sc, struct bio *bp)
|
2000-12-31 13:03:42 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
|
|
|
struct mount *mp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* VNODE I/O
|
|
|
|
*
|
2002-01-21 20:57:03 +00:00
|
|
|
* If an error occurs, we set BIO_ERROR but we do not set
|
|
|
|
* B_INVAL because (for a write anyway), the buffer is
|
2000-12-31 13:03:42 +00:00
|
|
|
* still valid.
|
|
|
|
*/
|
|
|
|
|
2002-01-21 20:57:03 +00:00
|
|
|
bzero(&auio, sizeof(auio));
|
2000-12-31 13:03:42 +00:00
|
|
|
|
2002-01-21 20:57:03 +00:00
|
|
|
aiov.iov_base = bp->bio_data;
|
|
|
|
aiov.iov_len = bp->bio_bcount;
|
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
auio.uio_offset = (vm_ooffset_t)bp->bio_pblkno * sc->secsize;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
if(bp->bio_cmd == BIO_READ)
|
|
|
|
auio.uio_rw = UIO_READ;
|
|
|
|
else
|
|
|
|
auio.uio_rw = UIO_WRITE;
|
|
|
|
auio.uio_resid = bp->bio_bcount;
|
|
|
|
auio.uio_td = curthread;
|
|
|
|
/*
|
|
|
|
* When reading set IO_DIRECT to try to avoid double-caching
|
|
|
|
* the data. When writing IO_DIRECT is not optimal, but we
|
|
|
|
* must set IO_NOWDRAIN to avoid a wdrain deadlock.
|
|
|
|
*/
|
|
|
|
if (bp->bio_cmd == BIO_READ) {
|
|
|
|
vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
|
|
|
|
error = VOP_READ(sc->vnode, &auio, IO_DIRECT, sc->cred);
|
|
|
|
} else {
|
|
|
|
(void) vn_start_write(sc->vnode, &mp, V_WAIT);
|
|
|
|
vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
|
|
|
|
error = VOP_WRITE(sc->vnode, &auio, IO_NOWDRAIN, sc->cred);
|
|
|
|
vn_finished_write(mp);
|
|
|
|
}
|
|
|
|
VOP_UNLOCK(sc->vnode, 0, curthread);
|
|
|
|
bp->bio_resid = auio.uio_resid;
|
2002-01-21 20:50:06 +00:00
|
|
|
return (error);
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
|
|
|
|
2002-01-21 20:50:06 +00:00
|
|
|
static int
|
|
|
|
mdstart_swap(struct md_s *sc, struct bio *bp)
|
2000-12-31 13:03:42 +00:00
|
|
|
{
|
|
|
|
|
2002-01-21 20:57:03 +00:00
|
|
|
if ((bp->bio_cmd == BIO_DELETE) && (sc->flags & MD_RESERVE))
|
|
|
|
biodone(bp);
|
|
|
|
else
|
|
|
|
vm_pager_strategy(sc->object, bp);
|
2002-01-21 20:50:06 +00:00
|
|
|
return (-1);
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mdstrategy(struct bio *bp)
|
|
|
|
{
|
|
|
|
struct md_s *sc;
|
|
|
|
|
|
|
|
if (md_debug > 1)
|
2002-03-15 18:49:47 +00:00
|
|
|
printf("mdstrategy(%p) %s %x, %lld, %ld, %p)\n",
|
2002-03-19 04:07:29 +00:00
|
|
|
(void *)bp, devtoname(bp->bio_dev), bp->bio_flags,
|
|
|
|
(long long)bp->bio_blkno, bp->bio_bcount / DEV_BSIZE,
|
|
|
|
(void *)bp->bio_data);
|
2000-12-31 13:03:42 +00:00
|
|
|
|
|
|
|
sc = bp->bio_dev->si_drv1;
|
|
|
|
|
|
|
|
/* XXX: LOCK(sc->lock) */
|
|
|
|
bioqdisksort(&sc->bio_queue, bp);
|
|
|
|
/* XXX: UNLOCK(sc->lock) */
|
|
|
|
|
2002-06-03 22:09:04 +00:00
|
|
|
wakeup(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
md_kthread(void *arg)
|
|
|
|
{
|
|
|
|
struct md_s *sc;
|
|
|
|
struct bio *bp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
sc = arg;
|
|
|
|
curthread->td_base_pri = PRIBIO;
|
2000-12-31 13:03:42 +00:00
|
|
|
|
2002-06-03 22:09:04 +00:00
|
|
|
mtx_lock(&Giant);
|
2002-01-21 20:50:06 +00:00
|
|
|
for (;;) {
|
|
|
|
/* XXX: LOCK(unique unit numbers) */
|
|
|
|
bp = bioq_first(&sc->bio_queue);
|
|
|
|
if (bp)
|
|
|
|
bioq_remove(&sc->bio_queue, bp);
|
|
|
|
/* XXX: UNLOCK(unique unit numbers) */
|
2002-06-03 22:09:04 +00:00
|
|
|
if (!bp) {
|
|
|
|
tsleep(sc, PRIBIO, "mdwait", 0);
|
|
|
|
if (sc->flags & MD_SHUTDOWN) {
|
|
|
|
sc->procp = NULL;
|
|
|
|
wakeup(&sc->procp);
|
|
|
|
kthread_exit(0);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2002-01-21 20:50:06 +00:00
|
|
|
|
2002-01-21 20:57:03 +00:00
|
|
|
switch (sc->type) {
|
|
|
|
case MD_MALLOC:
|
2002-01-21 20:50:06 +00:00
|
|
|
devstat_start_transaction(&sc->stats);
|
|
|
|
error = mdstart_malloc(sc, bp);
|
2002-01-21 20:57:03 +00:00
|
|
|
break;
|
|
|
|
case MD_PRELOAD:
|
2002-01-21 20:50:06 +00:00
|
|
|
devstat_start_transaction(&sc->stats);
|
|
|
|
error = mdstart_preload(sc, bp);
|
2002-01-21 20:57:03 +00:00
|
|
|
break;
|
|
|
|
case MD_VNODE:
|
2002-01-21 20:50:06 +00:00
|
|
|
devstat_start_transaction(&sc->stats);
|
|
|
|
error = mdstart_vnode(sc, bp);
|
2002-01-21 20:57:03 +00:00
|
|
|
break;
|
|
|
|
case MD_SWAP:
|
2002-01-21 20:50:06 +00:00
|
|
|
error = mdstart_swap(sc, bp);
|
2002-01-21 20:57:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
panic("Impossible md(type)");
|
|
|
|
break;
|
2002-01-21 20:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error != -1)
|
|
|
|
biofinish(bp, &sc->stats, error);
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-26 20:08:44 +00:00
|
|
|
static struct md_s *
|
2000-12-31 13:03:42 +00:00
|
|
|
mdfind(int unit)
|
1999-09-21 11:00:49 +00:00
|
|
|
{
|
|
|
|
struct md_s *sc;
|
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
/* XXX: LOCK(unique unit numbers) */
|
2000-08-20 21:34:39 +00:00
|
|
|
LIST_FOREACH(sc, &md_softc_list, list) {
|
|
|
|
if (sc->unit == unit)
|
2000-12-31 13:03:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* XXX: UNLOCK(unique unit numbers) */
|
|
|
|
return (sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct md_s *
|
|
|
|
mdnew(int unit)
|
|
|
|
{
|
|
|
|
struct md_s *sc;
|
2002-06-03 22:09:04 +00:00
|
|
|
int error, max = -1;
|
2000-12-31 13:03:42 +00:00
|
|
|
|
|
|
|
/* XXX: LOCK(unique unit numbers) */
|
|
|
|
LIST_FOREACH(sc, &md_softc_list, list) {
|
|
|
|
if (sc->unit == unit) {
|
|
|
|
/* XXX: UNLOCK(unique unit numbers) */
|
2000-08-20 21:34:39 +00:00
|
|
|
return (NULL);
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
|
|
|
if (sc->unit > max)
|
|
|
|
max = sc->unit;
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|
2000-12-31 13:03:42 +00:00
|
|
|
if (unit == -1)
|
|
|
|
unit = max + 1;
|
2002-09-20 19:36:05 +00:00
|
|
|
if (unit > 255)
|
2000-12-31 13:03:42 +00:00
|
|
|
return (NULL);
|
2002-05-25 20:44:20 +00:00
|
|
|
sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
|
2000-08-20 21:34:39 +00:00
|
|
|
sc->unit = unit;
|
2002-05-26 06:48:55 +00:00
|
|
|
sprintf(sc->name, "md%d", unit);
|
2002-06-03 22:09:04 +00:00
|
|
|
error = kthread_create(md_kthread, sc, &sc->procp, 0, "%s", sc->name);
|
|
|
|
if (error) {
|
|
|
|
free(sc, M_MD);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2000-12-31 13:03:42 +00:00
|
|
|
LIST_INSERT_HEAD(&md_softc_list, sc, list);
|
|
|
|
/* XXX: UNLOCK(unique unit numbers) */
|
|
|
|
return (sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mdinit(struct md_s *sc)
|
|
|
|
{
|
|
|
|
|
2000-04-15 05:54:02 +00:00
|
|
|
bioq_init(&sc->bio_queue);
|
2001-02-25 13:12:57 +00:00
|
|
|
devstat_add_entry(&sc->stats, MD_NAME, sc->unit, sc->secsize,
|
2002-01-21 20:57:03 +00:00
|
|
|
DEVSTAT_NO_ORDERED_TAGS,
|
1999-11-26 20:08:44 +00:00
|
|
|
DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
|
|
|
|
DEVSTAT_PRIORITY_OTHER);
|
2000-07-04 10:06:34 +00:00
|
|
|
sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw, &mddisk_cdevsw);
|
1999-11-20 22:43:27 +00:00
|
|
|
sc->dev->si_drv1 = sc;
|
1999-11-26 20:08:44 +00:00
|
|
|
}
|
|
|
|
|
2001-01-21 22:57:56 +00:00
|
|
|
/*
|
|
|
|
* XXX: we should check that the range they feed us is mapped.
|
|
|
|
* XXX: we should implement read-only.
|
|
|
|
*/
|
|
|
|
|
2001-01-02 09:42:47 +00:00
|
|
|
static int
|
|
|
|
mdcreate_preload(struct md_ioctl *mdio)
|
1999-11-26 20:08:44 +00:00
|
|
|
{
|
|
|
|
struct md_s *sc;
|
|
|
|
|
2001-01-02 09:42:47 +00:00
|
|
|
if (mdio->md_size == 0)
|
2001-08-27 13:25:47 +00:00
|
|
|
return (EINVAL);
|
2001-01-02 09:42:47 +00:00
|
|
|
if (mdio->md_options & ~(MD_AUTOUNIT))
|
2001-08-27 13:25:47 +00:00
|
|
|
return (EINVAL);
|
2001-01-02 09:42:47 +00:00
|
|
|
if (mdio->md_options & MD_AUTOUNIT) {
|
|
|
|
sc = mdnew(-1);
|
|
|
|
if (sc == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
mdio->md_unit = sc->unit;
|
|
|
|
} else {
|
|
|
|
sc = mdnew(mdio->md_unit);
|
|
|
|
if (sc == NULL)
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
1999-12-01 10:20:21 +00:00
|
|
|
sc->type = MD_PRELOAD;
|
2000-12-31 13:03:42 +00:00
|
|
|
sc->secsize = DEV_BSIZE;
|
2001-01-02 09:42:47 +00:00
|
|
|
sc->nsect = mdio->md_size;
|
2001-08-07 19:23:16 +00:00
|
|
|
sc->flags = mdio->md_options & MD_FORCE;
|
2001-01-21 22:57:56 +00:00
|
|
|
/* Cast to pointer size, then to pointer to avoid warning */
|
2001-08-27 13:25:47 +00:00
|
|
|
sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
|
2001-01-02 09:42:47 +00:00
|
|
|
sc->pl_len = (mdio->md_size << DEV_BSHIFT);
|
2000-12-31 13:03:42 +00:00
|
|
|
mdinit(sc);
|
2001-01-02 09:42:47 +00:00
|
|
|
return (0);
|
1999-11-20 22:43:27 +00:00
|
|
|
}
|
|
|
|
|
2001-01-02 09:42:47 +00:00
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
static int
|
|
|
|
mdcreate_malloc(struct md_ioctl *mdio)
|
|
|
|
{
|
|
|
|
struct md_s *sc;
|
2002-05-25 20:44:20 +00:00
|
|
|
off_t u;
|
|
|
|
uintptr_t sp;
|
|
|
|
int error;
|
2000-12-31 13:03:42 +00:00
|
|
|
|
2002-05-25 20:44:20 +00:00
|
|
|
error = 0;
|
2000-12-31 13:03:42 +00:00
|
|
|
if (mdio->md_size == 0)
|
2001-08-27 13:25:47 +00:00
|
|
|
return (EINVAL);
|
2000-12-31 13:03:42 +00:00
|
|
|
if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
|
2001-08-27 13:25:47 +00:00
|
|
|
return (EINVAL);
|
2000-12-31 13:03:42 +00:00
|
|
|
/* Compression doesn't make sense if we have reserved space */
|
|
|
|
if (mdio->md_options & MD_RESERVE)
|
|
|
|
mdio->md_options &= ~MD_COMPRESS;
|
|
|
|
if (mdio->md_options & MD_AUTOUNIT) {
|
|
|
|
sc = mdnew(-1);
|
|
|
|
if (sc == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
mdio->md_unit = sc->unit;
|
|
|
|
} else {
|
|
|
|
sc = mdnew(mdio->md_unit);
|
|
|
|
if (sc == NULL)
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
sc->type = MD_MALLOC;
|
|
|
|
sc->secsize = DEV_BSIZE;
|
|
|
|
sc->nsect = mdio->md_size;
|
2001-08-07 19:23:16 +00:00
|
|
|
sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
|
2002-05-25 20:44:20 +00:00
|
|
|
sc->indir = dimension(sc->nsect);
|
2002-05-26 06:48:55 +00:00
|
|
|
sc->uma = uma_zcreate(sc->name, sc->secsize,
|
|
|
|
NULL, NULL, NULL, NULL, 0x1ff, 0);
|
2001-01-21 22:57:56 +00:00
|
|
|
if (mdio->md_options & MD_RESERVE) {
|
2002-05-25 20:44:20 +00:00
|
|
|
for (u = 0; u < sc->nsect; u++) {
|
2002-05-26 06:48:55 +00:00
|
|
|
sp = (uintptr_t) uma_zalloc(sc->uma, M_NOWAIT | M_ZERO);
|
2002-05-25 20:44:20 +00:00
|
|
|
if (sp != 0)
|
2002-05-26 09:38:51 +00:00
|
|
|
error = s_write(sc->indir, u, sp);
|
2002-05-25 20:44:20 +00:00
|
|
|
else
|
|
|
|
error = ENOMEM;
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
2002-05-25 20:44:20 +00:00
|
|
|
if (!error) {
|
|
|
|
printf("%s%d: Malloc disk\n", MD_NAME, sc->unit);
|
|
|
|
mdinit(sc);
|
|
|
|
} else
|
|
|
|
mddestroy(sc, NULL);
|
|
|
|
return (error);
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
mdsetcred(struct md_s *sc, struct ucred *cred)
|
|
|
|
{
|
|
|
|
char *tmpbuf;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set credits in our softc
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sc->cred)
|
|
|
|
crfree(sc->cred);
|
2001-10-11 23:38:17 +00:00
|
|
|
sc->cred = crhold(cred);
|
2000-12-31 13:03:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Horrible kludge to establish credentials for NFS XXX.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sc->vnode) {
|
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
|
|
|
|
|
|
|
tmpbuf = malloc(sc->secsize, M_TEMP, M_WAITOK);
|
|
|
|
bzero(&auio, sizeof(auio));
|
|
|
|
|
|
|
|
aiov.iov_base = tmpbuf;
|
|
|
|
aiov.iov_len = 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;
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
|
2000-12-31 13:03:42 +00:00
|
|
|
error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(sc->vnode, 0, curthread);
|
2000-12-31 13:03:42 +00:00
|
|
|
free(tmpbuf, M_TEMP);
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
mdcreate_vnode(struct md_ioctl *mdio, struct thread *td)
|
1999-11-20 22:43:27 +00:00
|
|
|
{
|
|
|
|
struct md_s *sc;
|
2000-12-31 13:03:42 +00:00
|
|
|
struct vattr vattr;
|
|
|
|
struct nameidata nd;
|
|
|
|
int error, flags;
|
1999-11-20 22:43:27 +00:00
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
flags = FREAD|FWRITE;
|
2001-09-12 08:38:13 +00:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td);
|
2000-12-31 13:03:42 +00:00
|
|
|
error = vn_open(&nd, &flags, 0);
|
|
|
|
if (error) {
|
|
|
|
if (error != EACCES && error != EPERM && error != EROFS)
|
|
|
|
return (error);
|
|
|
|
flags &= ~FWRITE;
|
2001-09-12 08:38:13 +00:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td);
|
2000-12-31 13:03:42 +00:00
|
|
|
error = vn_open(&nd, &flags, 0);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
|
|
|
if (nd.ni_vp->v_type != VREG ||
|
2002-02-27 18:32:23 +00:00
|
|
|
(error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred, td))) {
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(nd.ni_vp, 0, td);
|
2002-02-27 18:32:23 +00:00
|
|
|
(void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
|
2000-12-31 13:03:42 +00:00
|
|
|
return (error ? error : EINVAL);
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(nd.ni_vp, 0, td);
|
2002-05-03 17:55:10 +00:00
|
|
|
|
|
|
|
if (mdio->md_options & MD_AUTOUNIT) {
|
|
|
|
sc = mdnew(-1);
|
|
|
|
mdio->md_unit = sc->unit;
|
|
|
|
} else {
|
|
|
|
sc = mdnew(mdio->md_unit);
|
|
|
|
}
|
|
|
|
if (sc == NULL) {
|
|
|
|
(void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->type = MD_VNODE;
|
|
|
|
sc->flags = mdio->md_options & MD_FORCE;
|
|
|
|
if (!(flags & FWRITE))
|
|
|
|
sc->flags |= MD_READONLY;
|
2000-12-31 13:03:42 +00:00
|
|
|
sc->secsize = DEV_BSIZE;
|
|
|
|
sc->vnode = nd.ni_vp;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
/*
|
|
|
|
* If the size is specified, override the file attributes.
|
|
|
|
*/
|
|
|
|
if (mdio->md_size)
|
|
|
|
sc->nsect = mdio->md_size;
|
|
|
|
else
|
|
|
|
sc->nsect = vattr.va_size / sc->secsize; /* XXX: round up ? */
|
2001-07-26 20:05:20 +00:00
|
|
|
if (sc->nsect == 0) {
|
2002-06-15 19:18:43 +00:00
|
|
|
mddestroy(sc, td);
|
2001-07-26 20:05:20 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-02-27 18:32:23 +00:00
|
|
|
error = mdsetcred(sc, td->td_ucred);
|
2000-12-31 13:03:42 +00:00
|
|
|
if (error) {
|
2002-06-15 19:18:43 +00:00
|
|
|
mddestroy(sc, td);
|
2001-08-27 13:25:47 +00:00
|
|
|
return (error);
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
|
|
|
mdinit(sc);
|
|
|
|
return (0);
|
|
|
|
}
|
1999-09-21 11:00:49 +00:00
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
mddestroy(struct md_s *sc, struct thread *td)
|
2000-12-31 13:03:42 +00:00
|
|
|
{
|
|
|
|
|
2001-07-04 16:20:28 +00:00
|
|
|
GIANT_REQUIRED;
|
|
|
|
|
2001-05-06 17:17:23 +00:00
|
|
|
if (sc->dev != NULL) {
|
|
|
|
devstat_remove_entry(&sc->stats);
|
2000-12-31 13:03:42 +00:00
|
|
|
disk_destroy(sc->dev);
|
2001-05-06 17:17:23 +00:00
|
|
|
}
|
2002-06-03 22:09:04 +00:00
|
|
|
sc->flags |= MD_SHUTDOWN;
|
|
|
|
wakeup(sc);
|
|
|
|
while (sc->procp != NULL)
|
|
|
|
tsleep(&sc->procp, PRIBIO, "mddestroy", hz / 10);
|
2000-12-31 13:03:42 +00:00
|
|
|
if (sc->vnode != NULL)
|
2001-08-27 13:25:47 +00:00
|
|
|
(void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
|
2001-09-12 08:38:13 +00:00
|
|
|
FREAD : (FREAD|FWRITE), sc->cred, td);
|
2000-12-31 13:03:42 +00:00
|
|
|
if (sc->cred != NULL)
|
|
|
|
crfree(sc->cred);
|
2001-05-27 00:42:46 +00:00
|
|
|
if (sc->object != NULL) {
|
2000-12-31 13:03:42 +00:00
|
|
|
vm_pager_deallocate(sc->object);
|
2001-05-27 00:42:46 +00:00
|
|
|
}
|
2002-05-26 06:48:55 +00:00
|
|
|
if (sc->indir)
|
|
|
|
destroy_indir(sc, sc->indir);
|
|
|
|
if (sc->uma)
|
|
|
|
uma_zdestroy(sc->uma);
|
2001-05-06 17:17:23 +00:00
|
|
|
|
|
|
|
/* XXX: LOCK(unique unit numbers) */
|
|
|
|
LIST_REMOVE(sc, list);
|
|
|
|
/* XXX: UNLOCK(unique unit numbers) */
|
2002-05-25 20:44:20 +00:00
|
|
|
free(sc, M_MD);
|
2000-12-31 13:03:42 +00:00
|
|
|
return (0);
|
1999-09-21 11:00:49 +00:00
|
|
|
}
|
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
mdcreate_swap(struct md_ioctl *mdio, struct thread *td)
|
2000-08-20 21:34:39 +00:00
|
|
|
{
|
2000-12-31 13:03:42 +00:00
|
|
|
int error;
|
|
|
|
struct md_s *sc;
|
|
|
|
|
2001-07-04 16:20:28 +00:00
|
|
|
GIANT_REQUIRED;
|
|
|
|
|
2000-12-31 13:03:42 +00:00
|
|
|
if (mdio->md_options & MD_AUTOUNIT) {
|
|
|
|
sc = mdnew(-1);
|
|
|
|
mdio->md_unit = sc->unit;
|
|
|
|
} else {
|
|
|
|
sc = mdnew(mdio->md_unit);
|
|
|
|
}
|
|
|
|
if (sc == NULL)
|
|
|
|
return (EBUSY);
|
|
|
|
|
|
|
|
sc->type = MD_SWAP;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
|
|
|
/*
|
2000-12-31 13:03:42 +00:00
|
|
|
* Range check. Disallow negative sizes or any size less then the
|
|
|
|
* size of a page. Then round to a page.
|
2000-08-20 21:34:39 +00:00
|
|
|
*/
|
2000-12-31 13:03:42 +00:00
|
|
|
|
2001-05-06 17:17:23 +00:00
|
|
|
if (mdio->md_size == 0) {
|
2001-09-12 08:38:13 +00:00
|
|
|
mddestroy(sc, td);
|
2001-08-27 13:25:47 +00:00
|
|
|
return (EDOM);
|
2001-05-06 17:17:23 +00:00
|
|
|
}
|
2000-12-31 13:03:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate an OBJT_SWAP object.
|
|
|
|
*
|
|
|
|
* sc_secsize is PAGE_SIZE'd
|
|
|
|
*
|
|
|
|
* mdio->size is in DEV_BSIZE'd chunks.
|
|
|
|
* Note the truncation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sc->secsize = PAGE_SIZE;
|
|
|
|
sc->nsect = mdio->md_size / (PAGE_SIZE / DEV_BSIZE);
|
|
|
|
sc->object = vm_pager_allocate(OBJT_SWAP, NULL, sc->secsize * (vm_offset_t)sc->nsect, VM_PROT_DEFAULT, 0);
|
2001-08-07 19:23:16 +00:00
|
|
|
sc->flags = mdio->md_options & MD_FORCE;
|
2000-12-31 13:03:42 +00:00
|
|
|
if (mdio->md_options & MD_RESERVE) {
|
|
|
|
if (swap_pager_reserve(sc->object, 0, sc->nsect) < 0) {
|
|
|
|
vm_pager_deallocate(sc->object);
|
|
|
|
sc->object = NULL;
|
2001-09-12 08:38:13 +00:00
|
|
|
mddestroy(sc, td);
|
2001-08-27 13:25:47 +00:00
|
|
|
return (EDOM);
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-27 18:32:23 +00:00
|
|
|
error = mdsetcred(sc, td->td_ucred);
|
2000-12-31 13:03:42 +00:00
|
|
|
if (error)
|
2001-09-12 08:38:13 +00:00
|
|
|
mddestroy(sc, td);
|
2000-12-31 13:03:42 +00:00
|
|
|
else
|
|
|
|
mdinit(sc);
|
2001-08-27 13:25:47 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
mddetach(int unit, struct thread *td)
|
2001-08-27 13:25:47 +00:00
|
|
|
{
|
|
|
|
struct md_s *sc;
|
|
|
|
|
|
|
|
sc = mdfind(unit);
|
|
|
|
if (sc == NULL)
|
|
|
|
return (ENOENT);
|
|
|
|
if (sc->opencount != 0 && !(sc->flags & MD_FORCE))
|
|
|
|
return (EBUSY);
|
|
|
|
switch(sc->type) {
|
|
|
|
case MD_VNODE:
|
|
|
|
case MD_SWAP:
|
|
|
|
case MD_MALLOC:
|
|
|
|
case MD_PRELOAD:
|
2001-09-12 08:38:13 +00:00
|
|
|
return (mddestroy(sc, td));
|
2001-08-27 13:25:47 +00:00
|
|
|
default:
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
}
|
2000-12-31 13:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
mdctlioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
|
2000-12-31 13:03:42 +00:00
|
|
|
{
|
|
|
|
struct md_ioctl *mdio;
|
|
|
|
struct md_s *sc;
|
|
|
|
|
|
|
|
if (md_debug)
|
|
|
|
printf("mdctlioctl(%s %lx %p %x %p)\n",
|
2001-09-12 08:38:13 +00:00
|
|
|
devtoname(dev), cmd, addr, flags, td);
|
2000-12-31 13:03:42 +00:00
|
|
|
|
2001-12-20 06:38:21 +00:00
|
|
|
/*
|
|
|
|
* We assert the version number in the individual ioctl
|
|
|
|
* handlers instead of out here because (a) it is possible we
|
|
|
|
* may add another ioctl in the future which doesn't read an
|
|
|
|
* mdio, and (b) the correct return value for an unknown ioctl
|
|
|
|
* is ENOIOCTL, not EINVAL.
|
|
|
|
*/
|
2000-12-31 13:03:42 +00:00
|
|
|
mdio = (struct md_ioctl *)addr;
|
|
|
|
switch (cmd) {
|
|
|
|
case MDIOCATTACH:
|
2001-12-20 06:38:21 +00:00
|
|
|
if (mdio->md_version != MDIOVERSION)
|
|
|
|
return (EINVAL);
|
2000-12-31 13:03:42 +00:00
|
|
|
switch (mdio->md_type) {
|
|
|
|
case MD_MALLOC:
|
2001-08-27 13:25:47 +00:00
|
|
|
return (mdcreate_malloc(mdio));
|
2000-12-31 13:03:42 +00:00
|
|
|
case MD_PRELOAD:
|
2001-08-27 13:25:47 +00:00
|
|
|
return (mdcreate_preload(mdio));
|
2000-12-31 13:03:42 +00:00
|
|
|
case MD_VNODE:
|
2001-09-12 08:38:13 +00:00
|
|
|
return (mdcreate_vnode(mdio, td));
|
2000-12-31 13:03:42 +00:00
|
|
|
case MD_SWAP:
|
2001-09-12 08:38:13 +00:00
|
|
|
return (mdcreate_swap(mdio, td));
|
2000-12-31 13:03:42 +00:00
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
case MDIOCDETACH:
|
2001-12-20 06:38:21 +00:00
|
|
|
if (mdio->md_version != MDIOVERSION)
|
|
|
|
return (EINVAL);
|
2001-08-27 13:25:47 +00:00
|
|
|
if (mdio->md_file != NULL || mdio->md_size != 0 ||
|
|
|
|
mdio->md_options != 0)
|
|
|
|
return (EINVAL);
|
2001-09-12 08:38:13 +00:00
|
|
|
return (mddetach(mdio->md_unit, td));
|
2001-02-25 13:12:57 +00:00
|
|
|
case MDIOCQUERY:
|
2001-12-20 06:38:21 +00:00
|
|
|
if (mdio->md_version != MDIOVERSION)
|
|
|
|
return (EINVAL);
|
2001-02-25 13:12:57 +00:00
|
|
|
sc = mdfind(mdio->md_unit);
|
|
|
|
if (sc == NULL)
|
|
|
|
return (ENOENT);
|
|
|
|
mdio->md_type = sc->type;
|
|
|
|
mdio->md_options = sc->flags;
|
|
|
|
switch (sc->type) {
|
|
|
|
case MD_MALLOC:
|
|
|
|
mdio->md_size = sc->nsect;
|
|
|
|
break;
|
|
|
|
case MD_PRELOAD:
|
|
|
|
mdio->md_size = sc->nsect;
|
|
|
|
(u_char *)(uintptr_t)mdio->md_base = sc->pl_ptr;
|
|
|
|
break;
|
|
|
|
case MD_SWAP:
|
|
|
|
mdio->md_size = sc->nsect * (PAGE_SIZE / DEV_BSIZE);
|
|
|
|
break;
|
|
|
|
case MD_VNODE:
|
|
|
|
mdio->md_size = sc->nsect;
|
|
|
|
/* XXX fill this in */
|
2002-01-21 20:57:03 +00:00
|
|
|
mdio->md_file = NULL;
|
2001-02-25 13:12:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (0);
|
2000-12-31 13:03:42 +00:00
|
|
|
default:
|
|
|
|
return (ENOIOCTL);
|
|
|
|
};
|
|
|
|
return (ENOIOCTL);
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|
|
|
|
|
2001-01-02 09:42:47 +00:00
|
|
|
static void
|
|
|
|
md_preloaded(u_char *image, unsigned length)
|
|
|
|
{
|
|
|
|
struct md_s *sc;
|
|
|
|
|
|
|
|
sc = mdnew(-1);
|
|
|
|
if (sc == NULL)
|
|
|
|
return;
|
|
|
|
sc->type = MD_PRELOAD;
|
|
|
|
sc->secsize = DEV_BSIZE;
|
|
|
|
sc->nsect = length / DEV_BSIZE;
|
|
|
|
sc->pl_ptr = image;
|
|
|
|
sc->pl_len = length;
|
2002-01-21 20:57:03 +00:00
|
|
|
if (sc->unit == 0)
|
2001-01-02 09:42:47 +00:00
|
|
|
mdrootready = 1;
|
|
|
|
mdinit(sc);
|
|
|
|
}
|
|
|
|
|
1999-09-21 11:00:49 +00:00
|
|
|
static void
|
|
|
|
md_drvinit(void *unused)
|
|
|
|
{
|
|
|
|
|
1999-11-20 22:43:27 +00:00
|
|
|
caddr_t mod;
|
|
|
|
caddr_t c;
|
|
|
|
u_char *ptr, *name, *type;
|
|
|
|
unsigned len;
|
|
|
|
|
1999-11-26 20:08:44 +00:00
|
|
|
#ifdef MD_ROOT_SIZE
|
2001-01-02 09:42:47 +00:00
|
|
|
md_preloaded(mfs_root, MD_ROOT_SIZE*1024);
|
1999-11-26 20:08:44 +00:00
|
|
|
#endif
|
1999-11-20 22:43:27 +00:00
|
|
|
mod = NULL;
|
|
|
|
while ((mod = preload_search_next_name(mod)) != NULL) {
|
|
|
|
name = (char *)preload_search_info(mod, MODINFO_NAME);
|
|
|
|
type = (char *)preload_search_info(mod, MODINFO_TYPE);
|
|
|
|
if (name == NULL)
|
|
|
|
continue;
|
|
|
|
if (type == NULL)
|
|
|
|
continue;
|
1999-11-26 20:08:44 +00:00
|
|
|
if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
|
1999-11-20 22:43:27 +00:00
|
|
|
continue;
|
|
|
|
c = preload_search_info(mod, MODINFO_ADDR);
|
|
|
|
ptr = *(u_char **)c;
|
|
|
|
c = preload_search_info(mod, MODINFO_SIZE);
|
|
|
|
len = *(unsigned *)c;
|
2001-08-02 10:19:13 +00:00
|
|
|
printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
|
|
|
|
MD_NAME, mdunits, name, len, ptr);
|
2001-01-02 09:42:47 +00:00
|
|
|
md_preloaded(ptr, len);
|
2002-01-21 20:57:03 +00:00
|
|
|
}
|
2001-07-18 13:32:38 +00:00
|
|
|
status_dev = make_dev(&mdctl_cdevsw, 0xffff00ff, UID_ROOT, GID_WHEEL,
|
|
|
|
0600, MDCTL_NAME);
|
2001-02-24 16:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
md_modevent(module_t mod, int type, void *data)
|
|
|
|
{
|
2001-08-27 13:25:47 +00:00
|
|
|
int error;
|
|
|
|
struct md_s *sc;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
2001-02-24 16:26:41 +00:00
|
|
|
md_drvinit(NULL);
|
2001-08-27 13:25:47 +00:00
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
LIST_FOREACH(sc, &md_softc_list, list) {
|
2001-09-12 08:38:13 +00:00
|
|
|
error = mddetach(sc->unit, curthread);
|
2001-08-27 13:25:47 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
if (status_dev)
|
|
|
|
destroy_dev(status_dev);
|
|
|
|
status_dev = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (0);
|
1999-09-21 11:00:49 +00:00
|
|
|
}
|
|
|
|
|
2001-02-24 16:26:41 +00:00
|
|
|
static moduledata_t md_mod = {
|
2001-08-27 13:25:47 +00:00
|
|
|
MD_NAME,
|
|
|
|
md_modevent,
|
|
|
|
NULL
|
2001-02-24 16:26:41 +00:00
|
|
|
};
|
|
|
|
DECLARE_MODULE(md, md_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR);
|
|
|
|
MODULE_VERSION(md, MD_MODVER);
|
|
|
|
|
1999-09-21 11:00:49 +00:00
|
|
|
|
1999-11-26 20:08:44 +00:00
|
|
|
#ifdef MD_ROOT
|
|
|
|
static void
|
|
|
|
md_takeroot(void *junk)
|
|
|
|
{
|
|
|
|
if (mdrootready)
|
|
|
|
rootdevnames[0] = "ufs:/dev/md0c";
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
|
|
|
|
#endif
|