1997-04-17 04:12:50 +04:00
|
|
|
/* $NetBSD: vm_swap.c,v 1.37 1997/04/17 00:12:50 thorpej Exp $ */
|
1994-06-29 10:39:25 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-04-26 03:53:51 +04:00
|
|
|
* Copyright (c) 1982, 1986, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
1994-06-27 08:55:46 +04:00
|
|
|
* @(#)vm_swap.c 8.5 (Berkeley) 2/17/94
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
1993-12-17 10:56:32 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/map.h>
|
|
|
|
#include <sys/file.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-10-20 07:22:35 +03:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
1996-02-05 04:53:47 +03:00
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_conf.h>
|
|
|
|
|
1994-04-26 03:53:51 +04:00
|
|
|
#include <miscfs/specfs/specdev.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Indirect driver for multi-controller paging.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int nswap, nswdev;
|
1994-04-26 03:53:51 +04:00
|
|
|
#ifdef SEQSWAP
|
|
|
|
int niswdev; /* number of interleaved swap devices */
|
|
|
|
int niswap; /* size of interleaved swap area */
|
|
|
|
#endif
|
1997-04-17 04:12:50 +04:00
|
|
|
extern int dmmax;
|
1994-01-08 01:34:37 +03:00
|
|
|
|
1996-02-05 04:53:47 +03:00
|
|
|
int swfree __P((struct proc *, int));
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Set up swap devices.
|
|
|
|
* Initialize linked list of free swap
|
|
|
|
* headers. These do not actually point
|
|
|
|
* to buffers, but rather to pages that
|
|
|
|
* are being swapped in and out.
|
|
|
|
*/
|
1993-06-27 10:27:29 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
swapinit()
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register struct buf *sp = swbuf;
|
1994-04-21 11:47:31 +04:00
|
|
|
register struct proc *p = &proc0; /* XXX */
|
1993-03-21 12:45:37 +03:00
|
|
|
struct swdevt *swp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Count swap devices, and adjust total swap space available.
|
1994-04-26 03:53:51 +04:00
|
|
|
* Some of the space will not be countable until later (dynamically
|
|
|
|
* configurable devices) and some of the counted space will not be
|
|
|
|
* available until a swapon() system call is issued, both usually
|
|
|
|
* happen when the system goes multi-user.
|
|
|
|
*
|
|
|
|
* If using NFS for swap, swdevt[0] will already be bdevvp'd. XXX
|
|
|
|
*/
|
|
|
|
#ifdef SEQSWAP
|
|
|
|
nswdev = niswdev = 0;
|
|
|
|
nswap = niswap = 0;
|
|
|
|
/*
|
|
|
|
* All interleaved devices must come first
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1994-04-26 03:53:51 +04:00
|
|
|
for (swp = swdevt; swp->sw_dev != NODEV || swp->sw_vp != NULL; swp++) {
|
|
|
|
if (swp->sw_flags & SW_SEQUENTIAL)
|
|
|
|
break;
|
|
|
|
niswdev++;
|
|
|
|
if (swp->sw_nblks > niswap)
|
|
|
|
niswap = swp->sw_nblks;
|
|
|
|
}
|
|
|
|
niswap = roundup(niswap, dmmax);
|
|
|
|
niswap *= niswdev;
|
1997-01-22 10:28:21 +03:00
|
|
|
if (niswdev != 0 && swdevt[0].sw_vp == NULL &&
|
1994-04-26 03:53:51 +04:00
|
|
|
bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
|
|
|
|
panic("swapvp");
|
|
|
|
/*
|
|
|
|
* The remainder must be sequential
|
|
|
|
*/
|
|
|
|
for ( ; swp->sw_dev != NODEV; swp++) {
|
|
|
|
if ((swp->sw_flags & SW_SEQUENTIAL) == 0)
|
|
|
|
panic("binit: mis-ordered swap devices");
|
|
|
|
nswdev++;
|
|
|
|
if (swp->sw_nblks > 0) {
|
|
|
|
if (swp->sw_nblks % dmmax)
|
|
|
|
swp->sw_nblks -= (swp->sw_nblks % dmmax);
|
|
|
|
nswap += swp->sw_nblks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nswdev += niswdev;
|
|
|
|
nswap += niswap;
|
|
|
|
#else
|
1993-03-21 12:45:37 +03:00
|
|
|
nswdev = 0;
|
|
|
|
nswap = 0;
|
1994-04-26 03:53:51 +04:00
|
|
|
for (swp = swdevt; swp->sw_dev != NODEV || swp->sw_vp != NULL; swp++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
nswdev++;
|
|
|
|
if (swp->sw_nblks > nswap)
|
|
|
|
nswap = swp->sw_nblks;
|
|
|
|
}
|
|
|
|
if (nswdev > 1)
|
|
|
|
nswap = ((nswap + dmmax - 1) / dmmax) * dmmax;
|
|
|
|
nswap *= nswdev;
|
1997-01-22 10:28:21 +03:00
|
|
|
if (nswdev != 0 && swdevt[0].sw_vp == NULL &&
|
1994-04-26 03:53:51 +04:00
|
|
|
bdevvp(swdevt[0].sw_dev, &swdevt[0].sw_vp))
|
1993-03-21 12:45:37 +03:00
|
|
|
panic("swapvp");
|
1994-04-26 03:53:51 +04:00
|
|
|
#endif
|
|
|
|
if (nswap == 0)
|
1996-10-13 01:50:05 +04:00
|
|
|
printf("WARNING: no swap space found\n");
|
1994-07-21 11:12:13 +04:00
|
|
|
else if ((error = swfree(p, 0)) == ENXIO)
|
1996-10-13 01:50:05 +04:00
|
|
|
printf("WARNING: primary swap device not configured\n");
|
1994-07-21 11:12:13 +04:00
|
|
|
else if (error) {
|
1996-10-13 01:50:05 +04:00
|
|
|
printf("swfree errno %d\n", error); /* XXX */
|
1993-07-07 10:04:12 +04:00
|
|
|
panic("swapinit swfree 0");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now set up swap buffer headers.
|
|
|
|
*/
|
1994-02-06 13:13:02 +03:00
|
|
|
bswlist.b_actf = sp;
|
1994-04-21 11:47:31 +04:00
|
|
|
for (i = 0; i < nswbuf - 1; i++, sp++) {
|
1994-02-06 13:13:02 +03:00
|
|
|
sp->b_actf = sp + 1;
|
1994-04-21 11:47:31 +04:00
|
|
|
sp->b_rcred = sp->b_wcred = p->p_ucred;
|
|
|
|
sp->b_vnbufs.le_next = NOLIST;
|
|
|
|
}
|
|
|
|
sp->b_rcred = sp->b_wcred = p->p_ucred;
|
|
|
|
sp->b_vnbufs.le_next = NOLIST;
|
1994-04-26 03:53:51 +04:00
|
|
|
sp->b_actf = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-01-08 01:34:37 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
swstrategy(bp)
|
|
|
|
register struct buf *bp;
|
|
|
|
{
|
|
|
|
int sz, off, seg, index;
|
|
|
|
register struct swdevt *sp;
|
|
|
|
struct vnode *vp;
|
|
|
|
|
|
|
|
sz = howmany(bp->b_bcount, DEV_BSIZE);
|
|
|
|
if (bp->b_blkno + sz > nswap) {
|
1993-08-29 05:50:20 +04:00
|
|
|
bp->b_error = EINVAL;
|
1994-04-26 03:53:51 +04:00
|
|
|
bp->b_flags |= B_ERROR;
|
1993-03-21 12:45:37 +03:00
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (nswdev > 1) {
|
1994-04-26 03:53:51 +04:00
|
|
|
#ifdef SEQSWAP
|
|
|
|
if (bp->b_blkno < niswap) {
|
|
|
|
if (niswdev > 1) {
|
|
|
|
off = bp->b_blkno % dmmax;
|
|
|
|
if (off+sz > dmmax) {
|
|
|
|
bp->b_error = EINVAL;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
seg = bp->b_blkno / dmmax;
|
|
|
|
index = seg % niswdev;
|
|
|
|
seg /= niswdev;
|
|
|
|
bp->b_blkno = seg*dmmax + off;
|
|
|
|
} else
|
|
|
|
index = 0;
|
|
|
|
} else {
|
|
|
|
register struct swdevt *swp;
|
|
|
|
|
|
|
|
bp->b_blkno -= niswap;
|
|
|
|
for (index = niswdev, swp = &swdevt[niswdev];
|
|
|
|
swp->sw_dev != NODEV;
|
|
|
|
swp++, index++) {
|
|
|
|
if (bp->b_blkno < swp->sw_nblks)
|
|
|
|
break;
|
|
|
|
bp->b_blkno -= swp->sw_nblks;
|
|
|
|
}
|
|
|
|
if (swp->sw_dev == NODEV ||
|
|
|
|
bp->b_blkno+sz > swp->sw_nblks) {
|
|
|
|
bp->b_error = swp->sw_dev == NODEV ?
|
|
|
|
ENODEV : EINVAL;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
1993-03-21 12:45:37 +03:00
|
|
|
off = bp->b_blkno % dmmax;
|
|
|
|
if (off+sz > dmmax) {
|
1993-08-29 05:50:20 +04:00
|
|
|
bp->b_error = EINVAL;
|
1994-04-26 03:53:51 +04:00
|
|
|
bp->b_flags |= B_ERROR;
|
1993-03-21 12:45:37 +03:00
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
seg = bp->b_blkno / dmmax;
|
|
|
|
index = seg % nswdev;
|
|
|
|
seg /= nswdev;
|
|
|
|
bp->b_blkno = seg*dmmax + off;
|
1994-04-26 03:53:51 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
} else
|
|
|
|
index = 0;
|
|
|
|
sp = &swdevt[index];
|
|
|
|
if (sp->sw_vp == NULL) {
|
1993-08-29 05:50:20 +04:00
|
|
|
bp->b_error = ENODEV;
|
1994-04-26 03:53:51 +04:00
|
|
|
bp->b_flags |= B_ERROR;
|
1993-03-21 12:45:37 +03:00
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
1994-06-22 18:01:45 +04:00
|
|
|
if ((bp->b_dev = sp->sw_dev) == NODEV && sp->sw_vp->v_type != VREG)
|
|
|
|
panic("swstrategy");
|
1993-03-21 12:45:37 +03:00
|
|
|
VHOLD(sp->sw_vp);
|
|
|
|
if ((bp->b_flags & B_READ) == 0) {
|
1996-02-05 04:53:47 +03:00
|
|
|
if ((vp = bp->b_vp) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
vp->v_numoutput--;
|
|
|
|
if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
|
|
|
|
vp->v_flag &= ~VBWAIT;
|
|
|
|
wakeup((caddr_t)&vp->v_numoutput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sp->sw_vp->v_numoutput++;
|
|
|
|
}
|
|
|
|
if (bp->b_vp != NULL)
|
|
|
|
brelvp(bp);
|
|
|
|
bp->b_vp = sp->sw_vp;
|
|
|
|
VOP_STRATEGY(bp);
|
|
|
|
}
|
|
|
|
|
1996-02-05 04:53:47 +03:00
|
|
|
/*ARGSUSED*/
|
1995-07-04 11:15:28 +04:00
|
|
|
int
|
1996-02-05 04:53:47 +03:00
|
|
|
swread(dev, uio, ioflag)
|
1995-07-04 11:15:28 +04:00
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
1996-02-05 04:53:47 +03:00
|
|
|
int ioflag;
|
1995-07-04 11:15:28 +04:00
|
|
|
{
|
|
|
|
|
1995-07-19 17:04:02 +04:00
|
|
|
return (physio(swstrategy, NULL, dev, B_READ, minphys, uio));
|
1995-07-04 11:15:28 +04:00
|
|
|
}
|
|
|
|
|
1996-02-05 04:53:47 +03:00
|
|
|
/*ARGSUSED*/
|
1995-07-04 11:15:28 +04:00
|
|
|
int
|
1996-02-05 04:53:47 +03:00
|
|
|
swwrite(dev, uio, ioflag)
|
1995-07-04 11:15:28 +04:00
|
|
|
dev_t dev;
|
|
|
|
struct uio *uio;
|
1996-02-05 04:53:47 +03:00
|
|
|
int ioflag;
|
1995-07-04 11:15:28 +04:00
|
|
|
{
|
|
|
|
|
1995-07-19 17:04:02 +04:00
|
|
|
return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio));
|
1995-07-04 11:15:28 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* System call swapon(name) enables swapping on device name,
|
|
|
|
* which must be in the swdevsw. Return EBUSY
|
|
|
|
* if already swapping on this device.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
1994-01-08 01:34:37 +03:00
|
|
|
int
|
1995-10-07 09:25:19 +03:00
|
|
|
sys_swapon(p, v, retval)
|
1993-03-21 12:45:37 +03:00
|
|
|
struct proc *p;
|
1995-09-20 01:57:36 +04:00
|
|
|
void *v;
|
1994-10-20 07:22:35 +03:00
|
|
|
register_t *retval;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-10-07 09:25:19 +03:00
|
|
|
struct sys_swapon_args /* {
|
1995-09-20 01:57:36 +04:00
|
|
|
syscallarg(char *) name;
|
|
|
|
} */ *uap = v;
|
1993-03-21 12:45:37 +03:00
|
|
|
register struct vnode *vp;
|
|
|
|
register struct swdevt *sp;
|
|
|
|
dev_t dev;
|
|
|
|
int error;
|
|
|
|
struct nameidata nd;
|
|
|
|
|
1996-02-05 04:53:47 +03:00
|
|
|
if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (error);
|
1994-10-20 07:22:35 +03:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, name), p);
|
1996-02-05 04:53:47 +03:00
|
|
|
if ((error = namei(&nd)) != 0)
|
1995-01-09 05:47:38 +03:00
|
|
|
return (error);
|
1994-04-26 03:53:51 +04:00
|
|
|
vp = nd.ni_vp;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (vp->v_type != VBLK) {
|
|
|
|
vrele(vp);
|
|
|
|
return (ENOTBLK);
|
|
|
|
}
|
|
|
|
dev = (dev_t)vp->v_rdev;
|
|
|
|
if (major(dev) >= nblkdev) {
|
|
|
|
vrele(vp);
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
1994-04-26 03:53:51 +04:00
|
|
|
for (sp = &swdevt[0]; sp->sw_dev != NODEV; sp++) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (sp->sw_dev == dev) {
|
1994-04-26 03:53:51 +04:00
|
|
|
if (sp->sw_flags & SW_FREED) {
|
1993-03-21 12:45:37 +03:00
|
|
|
vrele(vp);
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
sp->sw_vp = vp;
|
1996-02-05 04:53:47 +03:00
|
|
|
if ((error = swfree(p, sp - swdevt)) != 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
vrele(vp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
1994-04-26 03:53:51 +04:00
|
|
|
#ifdef SEQSWAP
|
|
|
|
/*
|
|
|
|
* If we have reached a non-freed sequential device without
|
|
|
|
* finding what we are looking for, it is an error.
|
|
|
|
* That is because all interleaved devices must come first
|
|
|
|
* and sequential devices must be freed in order.
|
|
|
|
*/
|
|
|
|
if ((sp->sw_flags & (SW_SEQUENTIAL|SW_FREED)) == SW_SEQUENTIAL)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
vrele(vp);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Swfree(index) frees the index'th portion of the swap map.
|
|
|
|
* Each of the nswdev devices provides 1/nswdev'th of the swap
|
|
|
|
* space, which is laid out with blocks of dmmax pages circularly
|
|
|
|
* among the devices.
|
|
|
|
*/
|
1994-04-26 03:53:51 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
swfree(p, index)
|
|
|
|
struct proc *p;
|
|
|
|
int index;
|
|
|
|
{
|
|
|
|
register struct swdevt *sp;
|
|
|
|
register swblk_t vsbase;
|
|
|
|
register long blk;
|
|
|
|
struct vnode *vp;
|
|
|
|
register swblk_t dvbase;
|
|
|
|
register int nblks;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
sp = &swdevt[index];
|
|
|
|
vp = sp->sw_vp;
|
1995-09-19 01:21:00 +04:00
|
|
|
/* If root on swap, then the skip open/close operations. */
|
|
|
|
if (vp != rootvp) {
|
1996-02-05 04:53:47 +03:00
|
|
|
if ((error = VOP_OPEN(vp, FREAD|FWRITE, p->p_ucred, p)) != 0)
|
1995-09-19 01:21:00 +04:00
|
|
|
return (error);
|
|
|
|
}
|
1994-04-26 03:53:51 +04:00
|
|
|
sp->sw_flags |= SW_FREED;
|
1993-07-07 10:04:12 +04:00
|
|
|
nblks = sp->sw_nblks;
|
1994-04-26 03:53:51 +04:00
|
|
|
/*
|
|
|
|
* Some devices may not exist til after boot time.
|
|
|
|
* If so, their nblk count will be 0.
|
|
|
|
*/
|
|
|
|
if (nblks <= 0) {
|
|
|
|
int perdev;
|
|
|
|
dev_t dev = sp->sw_dev;
|
|
|
|
|
|
|
|
if (bdevsw[major(dev)].d_psize == 0 ||
|
|
|
|
(nblks = (*bdevsw[major(dev)].d_psize)(dev)) == -1) {
|
1995-09-19 01:21:00 +04:00
|
|
|
if (vp != rootvp)
|
|
|
|
(void) VOP_CLOSE(vp, FREAD|FWRITE, p->p_ucred, p);
|
1994-04-26 03:53:51 +04:00
|
|
|
sp->sw_flags &= ~SW_FREED;
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
#ifdef SEQSWAP
|
|
|
|
if (index < niswdev) {
|
|
|
|
perdev = niswap / niswdev;
|
|
|
|
if (nblks > perdev)
|
|
|
|
nblks = perdev;
|
|
|
|
} else {
|
|
|
|
if (nblks % dmmax)
|
|
|
|
nblks -= (nblks % dmmax);
|
|
|
|
nswap += nblks;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
perdev = nswap / nswdev;
|
|
|
|
if (nblks > perdev)
|
|
|
|
nblks = perdev;
|
|
|
|
#endif
|
|
|
|
sp->sw_nblks = nblks;
|
|
|
|
}
|
|
|
|
if (nblks == 0) {
|
1995-09-19 01:21:00 +04:00
|
|
|
if (vp != rootvp)
|
|
|
|
(void) VOP_CLOSE(vp, FREAD|FWRITE, p->p_ucred, p);
|
1994-04-26 03:53:51 +04:00
|
|
|
sp->sw_flags &= ~SW_FREED;
|
|
|
|
return (0); /* XXX error? */
|
|
|
|
}
|
|
|
|
#ifdef SEQSWAP
|
|
|
|
if (sp->sw_flags & SW_SEQUENTIAL) {
|
|
|
|
register struct swdevt *swp;
|
|
|
|
|
|
|
|
blk = niswap;
|
|
|
|
for (swp = &swdevt[niswdev]; swp != sp; swp++)
|
|
|
|
blk += swp->sw_nblks;
|
|
|
|
rmfree(swapmap, nblks, blk);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif
|
1993-04-28 08:00:49 +04:00
|
|
|
for (dvbase = 0; dvbase < nblks; dvbase += dmmax) {
|
1993-03-21 12:45:37 +03:00
|
|
|
blk = nblks - dvbase;
|
1994-04-26 03:53:51 +04:00
|
|
|
#ifdef SEQSWAP
|
|
|
|
if ((vsbase = index*dmmax + dvbase*niswdev) >= niswap)
|
|
|
|
panic("swfree");
|
|
|
|
#else
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((vsbase = index*dmmax + dvbase*nswdev) >= nswap)
|
|
|
|
panic("swfree");
|
1994-04-26 03:53:51 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
if (blk > dmmax)
|
|
|
|
blk = dmmax;
|
1993-07-07 10:04:12 +04:00
|
|
|
if (vsbase == 0) {
|
|
|
|
/*
|
|
|
|
* First of all chunks... initialize the swapmap.
|
|
|
|
* Don't use the first cluster of the device
|
|
|
|
* in case it starts with a label or boot block.
|
|
|
|
*/
|
|
|
|
rminit(swapmap, blk - ctod(CLSIZE),
|
1994-04-26 03:53:51 +04:00
|
|
|
vsbase + ctod(CLSIZE), "swap", nswapmap);
|
1993-07-07 10:04:12 +04:00
|
|
|
} else if (dvbase == 0) {
|
|
|
|
/*
|
|
|
|
* Don't use the first cluster of the device
|
|
|
|
* in case it starts with a label or boot block.
|
|
|
|
*/
|
|
|
|
rmfree(swapmap, blk - ctod(CLSIZE),
|
|
|
|
vsbase + ctod(CLSIZE));
|
|
|
|
} else
|
|
|
|
rmfree(swapmap, blk, vsbase);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-09-19 01:21:00 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Preserve the mini-root if appropriate:
|
|
|
|
* Note: this requires !SEQSWAP && nswdev==1
|
|
|
|
*
|
|
|
|
* A mini-root gets copied into the front of the swap
|
|
|
|
* and we run over top of the swap area just long
|
|
|
|
* enough for us to do a mkfs and restor of the real
|
|
|
|
* root (sure beats rewriting standalone restor).
|
|
|
|
*/
|
|
|
|
if (vp == rootvp) {
|
1996-10-21 05:32:38 +04:00
|
|
|
#ifndef MINIROOTSIZE
|
1995-09-19 01:21:00 +04:00
|
|
|
struct mount *mp;
|
|
|
|
struct statfs *sp;
|
1996-10-21 05:32:38 +04:00
|
|
|
#endif
|
1995-09-19 01:21:00 +04:00
|
|
|
long firstblk;
|
|
|
|
int rootblks;
|
|
|
|
|
|
|
|
#ifdef MINIROOTSIZE
|
|
|
|
rootblks = MINIROOTSIZE;
|
|
|
|
#else
|
|
|
|
/* Get size from root FS (mountroot did statfs) */
|
|
|
|
mp = rootvnode->v_mount;
|
|
|
|
sp = &mp->mnt_stat;
|
|
|
|
rootblks = sp->f_blocks * (sp->f_bsize / DEV_BSIZE);
|
|
|
|
#endif
|
|
|
|
if (rootblks > nblks)
|
|
|
|
panic("swfree miniroot size");
|
|
|
|
/* First ctod(CLSIZE) blocks are not in the map. */
|
|
|
|
firstblk = rmalloc(swapmap, rootblks - ctod(CLSIZE));
|
|
|
|
if (firstblk != ctod(CLSIZE))
|
|
|
|
panic("swfree miniroot save");
|
1996-10-13 01:50:05 +04:00
|
|
|
printf("Preserved %d blocks of miniroot leaving %d pages of swap\n",
|
1996-10-10 21:16:17 +04:00
|
|
|
rootblks, dtoc(nblks - rootblks));
|
1995-09-19 01:21:00 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
|
|
|
}
|