Home | History | Annotate | Line # | Download | only in uvm
uvm_swap.c revision 1.80.2.6
      1 /*	$NetBSD: uvm_swap.c,v 1.80.2.6 2004/11/02 07:53:37 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996, 1997 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  *
     30  * from: NetBSD: vm_swap.c,v 1.52 1997/12/02 13:47:37 pk Exp
     31  * from: Id: uvm_swap.c,v 1.1.2.42 1998/02/02 20:38:06 chuck Exp
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: uvm_swap.c,v 1.80.2.6 2004/11/02 07:53:37 skrll Exp $");
     36 
     37 #include "fs_nfs.h"
     38 #include "opt_uvmhist.h"
     39 #include "opt_compat_netbsd.h"
     40 #include "opt_ddb.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/buf.h>
     45 #include <sys/bufq.h>
     46 #include <sys/conf.h>
     47 #include <sys/proc.h>
     48 #include <sys/namei.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/errno.h>
     51 #include <sys/kernel.h>
     52 #include <sys/malloc.h>
     53 #include <sys/vnode.h>
     54 #include <sys/file.h>
     55 #include <sys/extent.h>
     56 #include <sys/mount.h>
     57 #include <sys/pool.h>
     58 #include <sys/sa.h>
     59 #include <sys/syscallargs.h>
     60 #include <sys/swap.h>
     61 
     62 #include <uvm/uvm.h>
     63 
     64 #include <miscfs/specfs/specdev.h>
     65 
     66 /*
     67  * uvm_swap.c: manage configuration and i/o to swap space.
     68  */
     69 
     70 /*
     71  * swap space is managed in the following way:
     72  *
     73  * each swap partition or file is described by a "swapdev" structure.
     74  * each "swapdev" structure contains a "swapent" structure which contains
     75  * information that is passed up to the user (via system calls).
     76  *
     77  * each swap partition is assigned a "priority" (int) which controls
     78  * swap parition usage.
     79  *
     80  * the system maintains a global data structure describing all swap
     81  * partitions/files.   there is a sorted LIST of "swappri" structures
     82  * which describe "swapdev"'s at that priority.   this LIST is headed
     83  * by the "swap_priority" global var.    each "swappri" contains a
     84  * CIRCLEQ of "swapdev" structures at that priority.
     85  *
     86  * locking:
     87  *  - swap_syscall_lock (sleep lock): this lock serializes the swapctl
     88  *    system call and prevents the swap priority list from changing
     89  *    while we are in the middle of a system call (e.g. SWAP_STATS).
     90  *  - uvm.swap_data_lock (simple_lock): this lock protects all swap data
     91  *    structures including the priority list, the swapdev structures,
     92  *    and the swapmap extent.
     93  *
     94  * each swap device has the following info:
     95  *  - swap device in use (could be disabled, preventing future use)
     96  *  - swap enabled (allows new allocations on swap)
     97  *  - map info in /dev/drum
     98  *  - vnode pointer
     99  * for swap files only:
    100  *  - block size
    101  *  - max byte count in buffer
    102  *  - buffer
    103  *
    104  * userland controls and configures swap with the swapctl(2) system call.
    105  * the sys_swapctl performs the following operations:
    106  *  [1] SWAP_NSWAP: returns the number of swap devices currently configured
    107  *  [2] SWAP_STATS: given a pointer to an array of swapent structures
    108  *	(passed in via "arg") of a size passed in via "misc" ... we load
    109  *	the current swap config into the array. The actual work is done
    110  *	in the uvm_swap_stats(9) function.
    111  *  [3] SWAP_ON: given a pathname in arg (could be device or file) and a
    112  *	priority in "misc", start swapping on it.
    113  *  [4] SWAP_OFF: as SWAP_ON, but stops swapping to a device
    114  *  [5] SWAP_CTL: changes the priority of a swap device (new priority in
    115  *	"misc")
    116  */
    117 
    118 /*
    119  * swapdev: describes a single swap partition/file
    120  *
    121  * note the following should be true:
    122  * swd_inuse <= swd_nblks  [number of blocks in use is <= total blocks]
    123  * swd_nblks <= swd_mapsize [because mapsize includes miniroot+disklabel]
    124  */
    125 struct swapdev {
    126 	struct oswapent swd_ose;
    127 #define	swd_dev		swd_ose.ose_dev		/* device id */
    128 #define	swd_flags	swd_ose.ose_flags	/* flags:inuse/enable/fake */
    129 #define	swd_priority	swd_ose.ose_priority	/* our priority */
    130 	/* also: swd_ose.ose_nblks, swd_ose.ose_inuse */
    131 	char			*swd_path;	/* saved pathname of device */
    132 	int			swd_pathlen;	/* length of pathname */
    133 	int			swd_npages;	/* #pages we can use */
    134 	int			swd_npginuse;	/* #pages in use */
    135 	int			swd_npgbad;	/* #pages bad */
    136 	int			swd_drumoffset;	/* page0 offset in drum */
    137 	int			swd_drumsize;	/* #pages in drum */
    138 	struct extent		*swd_ex;	/* extent for this swapdev */
    139 	char			swd_exname[12];	/* name of extent above */
    140 	struct vnode		*swd_vp;	/* backing vnode */
    141 	CIRCLEQ_ENTRY(swapdev)	swd_next;	/* priority circleq */
    142 
    143 	int			swd_bsize;	/* blocksize (bytes) */
    144 	int			swd_maxactive;	/* max active i/o reqs */
    145 	struct bufq_state	swd_tab;	/* buffer list */
    146 	int			swd_active;	/* number of active buffers */
    147 };
    148 
    149 /*
    150  * swap device priority entry; the list is kept sorted on `spi_priority'.
    151  */
    152 struct swappri {
    153 	int			spi_priority;     /* priority */
    154 	CIRCLEQ_HEAD(spi_swapdev, swapdev)	spi_swapdev;
    155 	/* circleq of swapdevs at this priority */
    156 	LIST_ENTRY(swappri)	spi_swappri;      /* global list of pri's */
    157 };
    158 
    159 /*
    160  * The following two structures are used to keep track of data transfers
    161  * on swap devices associated with regular files.
    162  * NOTE: this code is more or less a copy of vnd.c; we use the same
    163  * structure names here to ease porting..
    164  */
    165 struct vndxfer {
    166 	struct buf	*vx_bp;		/* Pointer to parent buffer */
    167 	struct swapdev	*vx_sdp;
    168 	int		vx_error;
    169 	int		vx_pending;	/* # of pending aux buffers */
    170 	int		vx_flags;
    171 #define VX_BUSY		1
    172 #define VX_DEAD		2
    173 };
    174 
    175 struct vndbuf {
    176 	struct buf	vb_buf;
    177 	struct vndxfer	*vb_xfer;
    178 };
    179 
    180 
    181 /*
    182  * We keep a of pool vndbuf's and vndxfer structures.
    183  */
    184 POOL_INIT(vndxfer_pool, sizeof(struct vndxfer), 0, 0, 0, "swp vnx", NULL);
    185 POOL_INIT(vndbuf_pool, sizeof(struct vndbuf), 0, 0, 0, "swp vnd", NULL);
    186 
    187 #define	getvndxfer(vnx)	do {						\
    188 	int s = splbio();						\
    189 	vnx = pool_get(&vndxfer_pool, PR_WAITOK);			\
    190 	splx(s);							\
    191 } while (/*CONSTCOND*/ 0)
    192 
    193 #define putvndxfer(vnx) {						\
    194 	pool_put(&vndxfer_pool, (void *)(vnx));				\
    195 }
    196 
    197 #define	getvndbuf(vbp)	do {						\
    198 	int s = splbio();						\
    199 	vbp = pool_get(&vndbuf_pool, PR_WAITOK);			\
    200 	splx(s);							\
    201 } while (/*CONSTCOND*/ 0)
    202 
    203 #define putvndbuf(vbp) {						\
    204 	pool_put(&vndbuf_pool, (void *)(vbp));				\
    205 }
    206 
    207 /*
    208  * local variables
    209  */
    210 static struct extent *swapmap;		/* controls the mapping of /dev/drum */
    211 
    212 MALLOC_DEFINE(M_VMSWAP, "VM swap", "VM swap structures");
    213 
    214 /* list of all active swap devices [by priority] */
    215 LIST_HEAD(swap_priority, swappri);
    216 static struct swap_priority swap_priority;
    217 
    218 /* locks */
    219 struct lock swap_syscall_lock;
    220 
    221 /*
    222  * prototypes
    223  */
    224 static struct swapdev	*swapdrum_getsdp(int);
    225 
    226 static struct swapdev	*swaplist_find(struct vnode *, int);
    227 static void		 swaplist_insert(struct swapdev *,
    228 					 struct swappri *, int);
    229 static void		 swaplist_trim(void);
    230 
    231 static int swap_on(struct lwp *, struct swapdev *);
    232 static int swap_off(struct lwp *, struct swapdev *);
    233 
    234 static void sw_reg_strategy(struct swapdev *, struct buf *, int);
    235 static void sw_reg_iodone(struct buf *);
    236 static void sw_reg_start(struct swapdev *);
    237 
    238 static int uvm_swap_io(struct vm_page **, int, int, int);
    239 
    240 dev_type_read(swread);
    241 dev_type_write(swwrite);
    242 dev_type_strategy(swstrategy);
    243 
    244 const struct bdevsw swap_bdevsw = {
    245 	noopen, noclose, swstrategy, noioctl, nodump, nosize,
    246 };
    247 
    248 const struct cdevsw swap_cdevsw = {
    249 	nullopen, nullclose, swread, swwrite, noioctl,
    250 	nostop, notty, nopoll, nommap, nokqfilter
    251 };
    252 
    253 /*
    254  * uvm_swap_init: init the swap system data structures and locks
    255  *
    256  * => called at boot time from init_main.c after the filesystems
    257  *	are brought up (which happens after uvm_init())
    258  */
    259 void
    260 uvm_swap_init()
    261 {
    262 	UVMHIST_FUNC("uvm_swap_init");
    263 
    264 	UVMHIST_CALLED(pdhist);
    265 	/*
    266 	 * first, init the swap list, its counter, and its lock.
    267 	 * then get a handle on the vnode for /dev/drum by using
    268 	 * the its dev_t number ("swapdev", from MD conf.c).
    269 	 */
    270 
    271 	LIST_INIT(&swap_priority);
    272 	uvmexp.nswapdev = 0;
    273 	lockinit(&swap_syscall_lock, PVM, "swapsys", 0, 0);
    274 	simple_lock_init(&uvm.swap_data_lock);
    275 
    276 	if (bdevvp(swapdev, &swapdev_vp))
    277 		panic("uvm_swap_init: can't get vnode for swap device");
    278 
    279 	/*
    280 	 * create swap block resource map to map /dev/drum.   the range
    281 	 * from 1 to INT_MAX allows 2 gigablocks of swap space.  note
    282 	 * that block 0 is reserved (used to indicate an allocation
    283 	 * failure, or no allocation).
    284 	 */
    285 	swapmap = extent_create("swapmap", 1, INT_MAX,
    286 				M_VMSWAP, 0, 0, EX_NOWAIT);
    287 	if (swapmap == 0)
    288 		panic("uvm_swap_init: extent_create failed");
    289 
    290 	/*
    291 	 * done!
    292 	 */
    293 	UVMHIST_LOG(pdhist, "<- done", 0, 0, 0, 0);
    294 }
    295 
    296 /*
    297  * swaplist functions: functions that operate on the list of swap
    298  * devices on the system.
    299  */
    300 
    301 /*
    302  * swaplist_insert: insert swap device "sdp" into the global list
    303  *
    304  * => caller must hold both swap_syscall_lock and uvm.swap_data_lock
    305  * => caller must provide a newly malloc'd swappri structure (we will
    306  *	FREE it if we don't need it... this it to prevent malloc blocking
    307  *	here while adding swap)
    308  */
    309 static void
    310 swaplist_insert(sdp, newspp, priority)
    311 	struct swapdev *sdp;
    312 	struct swappri *newspp;
    313 	int priority;
    314 {
    315 	struct swappri *spp, *pspp;
    316 	UVMHIST_FUNC("swaplist_insert"); UVMHIST_CALLED(pdhist);
    317 
    318 	/*
    319 	 * find entry at or after which to insert the new device.
    320 	 */
    321 	pspp = NULL;
    322 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    323 		if (priority <= spp->spi_priority)
    324 			break;
    325 		pspp = spp;
    326 	}
    327 
    328 	/*
    329 	 * new priority?
    330 	 */
    331 	if (spp == NULL || spp->spi_priority != priority) {
    332 		spp = newspp;  /* use newspp! */
    333 		UVMHIST_LOG(pdhist, "created new swappri = %d",
    334 			    priority, 0, 0, 0);
    335 
    336 		spp->spi_priority = priority;
    337 		CIRCLEQ_INIT(&spp->spi_swapdev);
    338 
    339 		if (pspp)
    340 			LIST_INSERT_AFTER(pspp, spp, spi_swappri);
    341 		else
    342 			LIST_INSERT_HEAD(&swap_priority, spp, spi_swappri);
    343 	} else {
    344 	  	/* we don't need a new priority structure, free it */
    345 		FREE(newspp, M_VMSWAP);
    346 	}
    347 
    348 	/*
    349 	 * priority found (or created).   now insert on the priority's
    350 	 * circleq list and bump the total number of swapdevs.
    351 	 */
    352 	sdp->swd_priority = priority;
    353 	CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
    354 	uvmexp.nswapdev++;
    355 }
    356 
    357 /*
    358  * swaplist_find: find and optionally remove a swap device from the
    359  *	global list.
    360  *
    361  * => caller must hold both swap_syscall_lock and uvm.swap_data_lock
    362  * => we return the swapdev we found (and removed)
    363  */
    364 static struct swapdev *
    365 swaplist_find(vp, remove)
    366 	struct vnode *vp;
    367 	boolean_t remove;
    368 {
    369 	struct swapdev *sdp;
    370 	struct swappri *spp;
    371 
    372 	/*
    373 	 * search the lists for the requested vp
    374 	 */
    375 
    376 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    377 		CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
    378 			if (sdp->swd_vp == vp) {
    379 				if (remove) {
    380 					CIRCLEQ_REMOVE(&spp->spi_swapdev,
    381 					    sdp, swd_next);
    382 					uvmexp.nswapdev--;
    383 				}
    384 				return(sdp);
    385 			}
    386 		}
    387 	}
    388 	return (NULL);
    389 }
    390 
    391 
    392 /*
    393  * swaplist_trim: scan priority list for empty priority entries and kill
    394  *	them.
    395  *
    396  * => caller must hold both swap_syscall_lock and uvm.swap_data_lock
    397  */
    398 static void
    399 swaplist_trim()
    400 {
    401 	struct swappri *spp, *nextspp;
    402 
    403 	for (spp = LIST_FIRST(&swap_priority); spp != NULL; spp = nextspp) {
    404 		nextspp = LIST_NEXT(spp, spi_swappri);
    405 		if (CIRCLEQ_FIRST(&spp->spi_swapdev) !=
    406 		    (void *)&spp->spi_swapdev)
    407 			continue;
    408 		LIST_REMOVE(spp, spi_swappri);
    409 		free(spp, M_VMSWAP);
    410 	}
    411 }
    412 
    413 /*
    414  * swapdrum_getsdp: given a page offset in /dev/drum, convert it back
    415  *	to the "swapdev" that maps that section of the drum.
    416  *
    417  * => each swapdev takes one big contig chunk of the drum
    418  * => caller must hold uvm.swap_data_lock
    419  */
    420 static struct swapdev *
    421 swapdrum_getsdp(pgno)
    422 	int pgno;
    423 {
    424 	struct swapdev *sdp;
    425 	struct swappri *spp;
    426 
    427 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    428 		CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
    429 			if (sdp->swd_flags & SWF_FAKE)
    430 				continue;
    431 			if (pgno >= sdp->swd_drumoffset &&
    432 			    pgno < (sdp->swd_drumoffset + sdp->swd_drumsize)) {
    433 				return sdp;
    434 			}
    435 		}
    436 	}
    437 	return NULL;
    438 }
    439 
    440 
    441 /*
    442  * sys_swapctl: main entry point for swapctl(2) system call
    443  * 	[with two helper functions: swap_on and swap_off]
    444  */
    445 int
    446 sys_swapctl(l, v, retval)
    447 	struct lwp *l;
    448 	void *v;
    449 	register_t *retval;
    450 {
    451 	struct sys_swapctl_args /* {
    452 		syscallarg(int) cmd;
    453 		syscallarg(void *) arg;
    454 		syscallarg(int) misc;
    455 	} */ *uap = (struct sys_swapctl_args *)v;
    456 	struct proc *p = l->l_proc;
    457 	struct vnode *vp;
    458 	struct nameidata nd;
    459 	struct swappri *spp;
    460 	struct swapdev *sdp;
    461 	struct swapent *sep;
    462 	char	userpath[PATH_MAX + 1];
    463 	size_t	len;
    464 	int	error, misc;
    465 	int	priority;
    466 	UVMHIST_FUNC("sys_swapctl"); UVMHIST_CALLED(pdhist);
    467 
    468 	misc = SCARG(uap, misc);
    469 
    470 	/*
    471 	 * ensure serialized syscall access by grabbing the swap_syscall_lock
    472 	 */
    473 	lockmgr(&swap_syscall_lock, LK_EXCLUSIVE, NULL);
    474 
    475 	/*
    476 	 * we handle the non-priv NSWAP and STATS request first.
    477 	 *
    478 	 * SWAP_NSWAP: return number of config'd swap devices
    479 	 * [can also be obtained with uvmexp sysctl]
    480 	 */
    481 	if (SCARG(uap, cmd) == SWAP_NSWAP) {
    482 		UVMHIST_LOG(pdhist, "<- done SWAP_NSWAP=%d", uvmexp.nswapdev,
    483 		    0, 0, 0);
    484 		*retval = uvmexp.nswapdev;
    485 		error = 0;
    486 		goto out;
    487 	}
    488 
    489 	/*
    490 	 * SWAP_STATS: get stats on current # of configured swap devs
    491 	 *
    492 	 * note that the swap_priority list can't change as long
    493 	 * as we are holding the swap_syscall_lock.  we don't want
    494 	 * to grab the uvm.swap_data_lock because we may fault&sleep during
    495 	 * copyout() and we don't want to be holding that lock then!
    496 	 */
    497 	if (SCARG(uap, cmd) == SWAP_STATS
    498 #if defined(COMPAT_13)
    499 	    || SCARG(uap, cmd) == SWAP_OSTATS
    500 #endif
    501 	    ) {
    502 		if ((size_t)misc > (size_t)uvmexp.nswapdev)
    503 			misc = uvmexp.nswapdev;
    504 #if defined(COMPAT_13)
    505 		if (SCARG(uap, cmd) == SWAP_OSTATS)
    506 			len = sizeof(struct oswapent) * misc;
    507 		else
    508 #endif
    509 			len = sizeof(struct swapent) * misc;
    510 		sep = (struct swapent *)malloc(len, M_TEMP, M_WAITOK);
    511 
    512 		uvm_swap_stats(SCARG(uap, cmd), sep, misc, retval);
    513 		error = copyout(sep, (void *)SCARG(uap, arg), len);
    514 
    515 		free(sep, M_TEMP);
    516 		UVMHIST_LOG(pdhist, "<- done SWAP_STATS", 0, 0, 0, 0);
    517 		goto out;
    518 	}
    519 	if (SCARG(uap, cmd) == SWAP_GETDUMPDEV) {
    520 		dev_t	*devp = (dev_t *)SCARG(uap, arg);
    521 
    522 		error = copyout(&dumpdev, devp, sizeof(dumpdev));
    523 		goto out;
    524 	}
    525 
    526 	/*
    527 	 * all other requests require superuser privs.   verify.
    528 	 */
    529 	if ((error = suser(p->p_ucred, &p->p_acflag)))
    530 		goto out;
    531 
    532 	/*
    533 	 * at this point we expect a path name in arg.   we will
    534 	 * use namei() to gain a vnode reference (vref), and lock
    535 	 * the vnode (VOP_LOCK).
    536 	 *
    537 	 * XXX: a NULL arg means use the root vnode pointer (e.g. for
    538 	 * miniroot)
    539 	 */
    540 	if (SCARG(uap, arg) == NULL) {
    541 		vp = rootvp;		/* miniroot */
    542 		if (vget(vp, LK_EXCLUSIVE)) {
    543 			error = EBUSY;
    544 			goto out;
    545 		}
    546 		if (SCARG(uap, cmd) == SWAP_ON &&
    547 		    copystr("miniroot", userpath, sizeof userpath, &len))
    548 			panic("swapctl: miniroot copy failed");
    549 	} else {
    550 		int	space;
    551 		char	*where;
    552 
    553 		if (SCARG(uap, cmd) == SWAP_ON) {
    554 			if ((error = copyinstr(SCARG(uap, arg), userpath,
    555 			    sizeof userpath, &len)))
    556 				goto out;
    557 			space = UIO_SYSSPACE;
    558 			where = userpath;
    559 		} else {
    560 			space = UIO_USERSPACE;
    561 			where = (char *)SCARG(uap, arg);
    562 		}
    563 		NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, space, where, l);
    564 		if ((error = namei(&nd)))
    565 			goto out;
    566 		vp = nd.ni_vp;
    567 	}
    568 	/* note: "vp" is referenced and locked */
    569 
    570 	error = 0;		/* assume no error */
    571 	switch(SCARG(uap, cmd)) {
    572 
    573 	case SWAP_DUMPDEV:
    574 		if (vp->v_type != VBLK) {
    575 			error = ENOTBLK;
    576 			break;
    577 		}
    578 		dumpdev = vp->v_rdev;
    579 		cpu_dumpconf();
    580 		break;
    581 
    582 	case SWAP_CTL:
    583 		/*
    584 		 * get new priority, remove old entry (if any) and then
    585 		 * reinsert it in the correct place.  finally, prune out
    586 		 * any empty priority structures.
    587 		 */
    588 		priority = SCARG(uap, misc);
    589 		spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
    590 		simple_lock(&uvm.swap_data_lock);
    591 		if ((sdp = swaplist_find(vp, 1)) == NULL) {
    592 			error = ENOENT;
    593 		} else {
    594 			swaplist_insert(sdp, spp, priority);
    595 			swaplist_trim();
    596 		}
    597 		simple_unlock(&uvm.swap_data_lock);
    598 		if (error)
    599 			free(spp, M_VMSWAP);
    600 		break;
    601 
    602 	case SWAP_ON:
    603 
    604 		/*
    605 		 * check for duplicates.   if none found, then insert a
    606 		 * dummy entry on the list to prevent someone else from
    607 		 * trying to enable this device while we are working on
    608 		 * it.
    609 		 */
    610 
    611 		priority = SCARG(uap, misc);
    612 		sdp = malloc(sizeof *sdp, M_VMSWAP, M_WAITOK);
    613 		spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
    614 		memset(sdp, 0, sizeof(*sdp));
    615 		sdp->swd_flags = SWF_FAKE;
    616 		sdp->swd_vp = vp;
    617 		sdp->swd_dev = (vp->v_type == VBLK) ? vp->v_rdev : NODEV;
    618 		bufq_alloc(&sdp->swd_tab, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
    619 		simple_lock(&uvm.swap_data_lock);
    620 		if (swaplist_find(vp, 0) != NULL) {
    621 			error = EBUSY;
    622 			simple_unlock(&uvm.swap_data_lock);
    623 			bufq_free(&sdp->swd_tab);
    624 			free(sdp, M_VMSWAP);
    625 			free(spp, M_VMSWAP);
    626 			break;
    627 		}
    628 		swaplist_insert(sdp, spp, priority);
    629 		simple_unlock(&uvm.swap_data_lock);
    630 
    631 		sdp->swd_pathlen = len;
    632 		sdp->swd_path = malloc(sdp->swd_pathlen, M_VMSWAP, M_WAITOK);
    633 		if (copystr(userpath, sdp->swd_path, sdp->swd_pathlen, 0) != 0)
    634 			panic("swapctl: copystr");
    635 
    636 		/*
    637 		 * we've now got a FAKE placeholder in the swap list.
    638 		 * now attempt to enable swap on it.  if we fail, undo
    639 		 * what we've done and kill the fake entry we just inserted.
    640 		 * if swap_on is a success, it will clear the SWF_FAKE flag
    641 		 */
    642 
    643 		if ((error = swap_on(l, sdp)) != 0) {
    644 			simple_lock(&uvm.swap_data_lock);
    645 			(void) swaplist_find(vp, 1);  /* kill fake entry */
    646 			swaplist_trim();
    647 			simple_unlock(&uvm.swap_data_lock);
    648 			bufq_free(&sdp->swd_tab);
    649 			free(sdp->swd_path, M_VMSWAP);
    650 			free(sdp, M_VMSWAP);
    651 			break;
    652 		}
    653 		break;
    654 
    655 	case SWAP_OFF:
    656 		simple_lock(&uvm.swap_data_lock);
    657 		if ((sdp = swaplist_find(vp, 0)) == NULL) {
    658 			simple_unlock(&uvm.swap_data_lock);
    659 			error = ENXIO;
    660 			break;
    661 		}
    662 
    663 		/*
    664 		 * If a device isn't in use or enabled, we
    665 		 * can't stop swapping from it (again).
    666 		 */
    667 		if ((sdp->swd_flags & (SWF_INUSE|SWF_ENABLE)) == 0) {
    668 			simple_unlock(&uvm.swap_data_lock);
    669 			error = EBUSY;
    670 			break;
    671 		}
    672 
    673 		/*
    674 		 * do the real work.
    675 		 */
    676 		error = swap_off(l, sdp);
    677 		break;
    678 
    679 	default:
    680 		error = EINVAL;
    681 	}
    682 
    683 	/*
    684 	 * done!  release the ref gained by namei() and unlock.
    685 	 */
    686 	vput(vp);
    687 
    688 out:
    689 	lockmgr(&swap_syscall_lock, LK_RELEASE, NULL);
    690 
    691 	UVMHIST_LOG(pdhist, "<- done!  error=%d", error, 0, 0, 0);
    692 	return (error);
    693 }
    694 
    695 /*
    696  * swap_stats: implements swapctl(SWAP_STATS). The function is kept
    697  * away from sys_swapctl() in order to allow COMPAT_* swapctl()
    698  * emulation to use it directly without going through sys_swapctl().
    699  * The problem with using sys_swapctl() there is that it involves
    700  * copying the swapent array to the stackgap, and this array's size
    701  * is not known at build time. Hence it would not be possible to
    702  * ensure it would fit in the stackgap in any case.
    703  */
    704 void
    705 uvm_swap_stats(cmd, sep, sec, retval)
    706 	int cmd;
    707 	struct swapent *sep;
    708 	int sec;
    709 	register_t *retval;
    710 {
    711 	struct swappri *spp;
    712 	struct swapdev *sdp;
    713 	int count = 0;
    714 
    715 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    716 		for (sdp = CIRCLEQ_FIRST(&spp->spi_swapdev);
    717 		     sdp != (void *)&spp->spi_swapdev && sec-- > 0;
    718 		     sdp = CIRCLEQ_NEXT(sdp, swd_next)) {
    719 		  	/*
    720 			 * backwards compatibility for system call.
    721 			 * note that we use 'struct oswapent' as an
    722 			 * overlay into both 'struct swapdev' and
    723 			 * the userland 'struct swapent', as we
    724 			 * want to retain backwards compatibility
    725 			 * with NetBSD 1.3.
    726 			 */
    727 			sdp->swd_ose.ose_inuse =
    728 			    btodb((u_int64_t)sdp->swd_npginuse <<
    729 			    PAGE_SHIFT);
    730 			(void)memcpy(sep, &sdp->swd_ose,
    731 			    sizeof(struct oswapent));
    732 
    733 			/* now copy out the path if necessary */
    734 #if defined(COMPAT_13)
    735 			if (cmd == SWAP_STATS)
    736 #endif
    737 				(void)memcpy(&sep->se_path, sdp->swd_path,
    738 				    sdp->swd_pathlen);
    739 
    740 			count++;
    741 #if defined(COMPAT_13)
    742 			if (cmd == SWAP_OSTATS)
    743 				sep = (struct swapent *)
    744 				    ((struct oswapent *)sep + 1);
    745 			else
    746 #endif
    747 				sep++;
    748 		}
    749 	}
    750 
    751 	*retval = count;
    752 	return;
    753 }
    754 
    755 /*
    756  * swap_on: attempt to enable a swapdev for swapping.   note that the
    757  *	swapdev is already on the global list, but disabled (marked
    758  *	SWF_FAKE).
    759  *
    760  * => we avoid the start of the disk (to protect disk labels)
    761  * => we also avoid the miniroot, if we are swapping to root.
    762  * => caller should leave uvm.swap_data_lock unlocked, we may lock it
    763  *	if needed.
    764  */
    765 static int
    766 swap_on(l, sdp)
    767 	struct lwp *l;
    768 	struct swapdev *sdp;
    769 {
    770 	static int count = 0;	/* static */
    771 	struct vnode *vp;
    772 	struct proc *p = l->l_proc;
    773 	int error, npages, nblocks, size;
    774 	long addr;
    775 	u_long result;
    776 	struct vattr va;
    777 #ifdef NFS
    778 	extern int (**nfsv2_vnodeop_p)(void *);
    779 #endif /* NFS */
    780 	const struct bdevsw *bdev;
    781 	dev_t dev;
    782 	UVMHIST_FUNC("swap_on"); UVMHIST_CALLED(pdhist);
    783 
    784 	/*
    785 	 * we want to enable swapping on sdp.   the swd_vp contains
    786 	 * the vnode we want (locked and ref'd), and the swd_dev
    787 	 * contains the dev_t of the file, if it a block device.
    788 	 */
    789 
    790 	vp = sdp->swd_vp;
    791 	dev = sdp->swd_dev;
    792 
    793 	/*
    794 	 * open the swap file (mostly useful for block device files to
    795 	 * let device driver know what is up).
    796 	 *
    797 	 * we skip the open/close for root on swap because the root
    798 	 * has already been opened when root was mounted (mountroot).
    799 	 */
    800 	if (vp != rootvp) {
    801 		if ((error = VOP_OPEN(vp, FREAD|FWRITE, p->p_ucred, l)))
    802 			return (error);
    803 	}
    804 
    805 	/* XXX this only works for block devices */
    806 	UVMHIST_LOG(pdhist, "  dev=%d, major(dev)=%d", dev, major(dev), 0,0);
    807 
    808 	/*
    809 	 * we now need to determine the size of the swap area.   for
    810 	 * block specials we can call the d_psize function.
    811 	 * for normal files, we must stat [get attrs].
    812 	 *
    813 	 * we put the result in nblks.
    814 	 * for normal files, we also want the filesystem block size
    815 	 * (which we get with statfs).
    816 	 */
    817 	switch (vp->v_type) {
    818 	case VBLK:
    819 		bdev = bdevsw_lookup(dev);
    820 		if (bdev == NULL || bdev->d_psize == NULL ||
    821 		    (nblocks = (*bdev->d_psize)(dev)) == -1) {
    822 			error = ENXIO;
    823 			goto bad;
    824 		}
    825 		break;
    826 
    827 	case VREG:
    828 		if ((error = VOP_GETATTR(vp, &va, p->p_ucred, l)))
    829 			goto bad;
    830 		nblocks = (int)btodb(va.va_size);
    831 		if ((error =
    832 		     VFS_STATVFS(vp->v_mount, &vp->v_mount->mnt_stat, l)) != 0)
    833 			goto bad;
    834 
    835 		sdp->swd_bsize = vp->v_mount->mnt_stat.f_iosize;
    836 		/*
    837 		 * limit the max # of outstanding I/O requests we issue
    838 		 * at any one time.   take it easy on NFS servers.
    839 		 */
    840 #ifdef NFS
    841 		if (vp->v_op == nfsv2_vnodeop_p)
    842 			sdp->swd_maxactive = 2; /* XXX */
    843 		else
    844 #endif /* NFS */
    845 			sdp->swd_maxactive = 8; /* XXX */
    846 		break;
    847 
    848 	default:
    849 		error = ENXIO;
    850 		goto bad;
    851 	}
    852 
    853 	/*
    854 	 * save nblocks in a safe place and convert to pages.
    855 	 */
    856 
    857 	sdp->swd_ose.ose_nblks = nblocks;
    858 	npages = dbtob((u_int64_t)nblocks) >> PAGE_SHIFT;
    859 
    860 	/*
    861 	 * for block special files, we want to make sure that leave
    862 	 * the disklabel and bootblocks alone, so we arrange to skip
    863 	 * over them (arbitrarily choosing to skip PAGE_SIZE bytes).
    864 	 * note that because of this the "size" can be less than the
    865 	 * actual number of blocks on the device.
    866 	 */
    867 	if (vp->v_type == VBLK) {
    868 		/* we use pages 1 to (size - 1) [inclusive] */
    869 		size = npages - 1;
    870 		addr = 1;
    871 	} else {
    872 		/* we use pages 0 to (size - 1) [inclusive] */
    873 		size = npages;
    874 		addr = 0;
    875 	}
    876 
    877 	/*
    878 	 * make sure we have enough blocks for a reasonable sized swap
    879 	 * area.   we want at least one page.
    880 	 */
    881 
    882 	if (size < 1) {
    883 		UVMHIST_LOG(pdhist, "  size <= 1!!", 0, 0, 0, 0);
    884 		error = EINVAL;
    885 		goto bad;
    886 	}
    887 
    888 	UVMHIST_LOG(pdhist, "  dev=%x: size=%d addr=%ld\n", dev, size, addr, 0);
    889 
    890 	/*
    891 	 * now we need to allocate an extent to manage this swap device
    892 	 */
    893 	snprintf(sdp->swd_exname, sizeof(sdp->swd_exname), "swap0x%04x",
    894 	    count++);
    895 
    896 	/* note that extent_create's 3rd arg is inclusive, thus "- 1" */
    897 	sdp->swd_ex = extent_create(sdp->swd_exname, 0, npages - 1, M_VMSWAP,
    898 				    0, 0, EX_WAITOK);
    899 	/* allocate the `saved' region from the extent so it won't be used */
    900 	if (addr) {
    901 		if (extent_alloc_region(sdp->swd_ex, 0, addr, EX_WAITOK))
    902 			panic("disklabel region");
    903 	}
    904 
    905 	/*
    906 	 * if the vnode we are swapping to is the root vnode
    907 	 * (i.e. we are swapping to the miniroot) then we want
    908 	 * to make sure we don't overwrite it.   do a statfs to
    909 	 * find its size and skip over it.
    910 	 */
    911 	if (vp == rootvp) {
    912 		struct mount *mp;
    913 		struct statvfs *sp;
    914 		int rootblocks, rootpages;
    915 
    916 		mp = rootvnode->v_mount;
    917 		sp = &mp->mnt_stat;
    918 		rootblocks = sp->f_blocks * btodb(sp->f_frsize);
    919 		/*
    920 		 * XXX: sp->f_blocks isn't the total number of
    921 		 * blocks in the filesystem, it's the number of
    922 		 * data blocks.  so, our rootblocks almost
    923 		 * definitely underestimates the total size
    924 		 * of the filesystem - how badly depends on the
    925 		 * details of the filesystem type.  there isn't
    926 		 * an obvious way to deal with this cleanly
    927 		 * and perfectly, so for now we just pad our
    928 		 * rootblocks estimate with an extra 5 percent.
    929 		 */
    930 		rootblocks += (rootblocks >> 5) +
    931 			(rootblocks >> 6) +
    932 			(rootblocks >> 7);
    933 		rootpages = round_page(dbtob(rootblocks)) >> PAGE_SHIFT;
    934 		if (rootpages > size)
    935 			panic("swap_on: miniroot larger than swap?");
    936 
    937 		if (extent_alloc_region(sdp->swd_ex, addr,
    938 					rootpages, EX_WAITOK))
    939 			panic("swap_on: unable to preserve miniroot");
    940 
    941 		size -= rootpages;
    942 		printf("Preserved %d pages of miniroot ", rootpages);
    943 		printf("leaving %d pages of swap\n", size);
    944 	}
    945 
    946   	/*
    947 	 * try to add anons to reflect the new swap space.
    948 	 */
    949 
    950 	error = uvm_anon_add(size);
    951 	if (error) {
    952 		goto bad;
    953 	}
    954 
    955 	/*
    956 	 * add a ref to vp to reflect usage as a swap device.
    957 	 */
    958 	vref(vp);
    959 
    960 	/*
    961 	 * now add the new swapdev to the drum and enable.
    962 	 */
    963 	if (extent_alloc(swapmap, npages, EX_NOALIGN, EX_NOBOUNDARY,
    964 	    EX_WAITOK, &result))
    965 		panic("swapdrum_add");
    966 
    967 	sdp->swd_drumoffset = (int)result;
    968 	sdp->swd_drumsize = npages;
    969 	sdp->swd_npages = size;
    970 	simple_lock(&uvm.swap_data_lock);
    971 	sdp->swd_flags &= ~SWF_FAKE;	/* going live */
    972 	sdp->swd_flags |= (SWF_INUSE|SWF_ENABLE);
    973 	uvmexp.swpages += size;
    974 	uvmexp.swpgavail += size;
    975 	simple_unlock(&uvm.swap_data_lock);
    976 	return (0);
    977 
    978 	/*
    979 	 * failure: clean up and return error.
    980 	 */
    981 
    982 bad:
    983 	if (sdp->swd_ex) {
    984 		extent_destroy(sdp->swd_ex);
    985 	}
    986 	if (vp != rootvp) {
    987 		(void)VOP_CLOSE(vp, FREAD|FWRITE, p->p_ucred, l);
    988 	}
    989 	return (error);
    990 }
    991 
    992 /*
    993  * swap_off: stop swapping on swapdev
    994  *
    995  * => swap data should be locked, we will unlock.
    996  */
    997 static int
    998 swap_off(l, sdp)
    999 	struct lwp *l;
   1000 	struct swapdev *sdp;
   1001 {
   1002 	struct proc *p = l->l_proc;
   1003 	int npages =  sdp->swd_npages;
   1004 
   1005 	UVMHIST_FUNC("swap_off"); UVMHIST_CALLED(pdhist);
   1006 	UVMHIST_LOG(pdhist, "  dev=%x, npages=%d", sdp->swd_dev,npages,0,0);
   1007 
   1008 	/* disable the swap area being removed */
   1009 	sdp->swd_flags &= ~SWF_ENABLE;
   1010 	uvmexp.swpgavail -= npages;
   1011 	simple_unlock(&uvm.swap_data_lock);
   1012 
   1013 	/*
   1014 	 * the idea is to find all the pages that are paged out to this
   1015 	 * device, and page them all in.  in uvm, swap-backed pageable
   1016 	 * memory can take two forms: aobjs and anons.  call the
   1017 	 * swapoff hook for each subsystem to bring in pages.
   1018 	 */
   1019 
   1020 	if (uao_swap_off(sdp->swd_drumoffset,
   1021 			 sdp->swd_drumoffset + sdp->swd_drumsize) ||
   1022 	    anon_swap_off(sdp->swd_drumoffset,
   1023 			  sdp->swd_drumoffset + sdp->swd_drumsize)) {
   1024 
   1025 		simple_lock(&uvm.swap_data_lock);
   1026 		sdp->swd_flags |= SWF_ENABLE;
   1027 		uvmexp.swpgavail += npages;
   1028 		simple_unlock(&uvm.swap_data_lock);
   1029 		return ENOMEM;
   1030 	}
   1031 	KASSERT(sdp->swd_npginuse == sdp->swd_npgbad);
   1032 
   1033 	/*
   1034 	 * done with the vnode.
   1035 	 * drop our ref on the vnode before calling VOP_CLOSE()
   1036 	 * so that spec_close() can tell if this is the last close.
   1037 	 */
   1038 	vrele(sdp->swd_vp);
   1039 	if (sdp->swd_vp != rootvp) {
   1040 		(void) VOP_CLOSE(sdp->swd_vp, FREAD|FWRITE, p->p_ucred, l);
   1041 	}
   1042 
   1043 	/* remove anons from the system */
   1044 	uvm_anon_remove(npages);
   1045 
   1046 	simple_lock(&uvm.swap_data_lock);
   1047 	uvmexp.swpages -= npages;
   1048 	uvmexp.swpginuse -= sdp->swd_npgbad;
   1049 
   1050 	if (swaplist_find(sdp->swd_vp, 1) == NULL)
   1051 		panic("swap_off: swapdev not in list");
   1052 	swaplist_trim();
   1053 	simple_unlock(&uvm.swap_data_lock);
   1054 
   1055 	/*
   1056 	 * free all resources!
   1057 	 */
   1058 	extent_free(swapmap, sdp->swd_drumoffset, sdp->swd_drumsize,
   1059 		    EX_WAITOK);
   1060 	extent_destroy(sdp->swd_ex);
   1061 	bufq_free(&sdp->swd_tab);
   1062 	free(sdp, M_VMSWAP);
   1063 	return (0);
   1064 }
   1065 
   1066 /*
   1067  * /dev/drum interface and i/o functions
   1068  */
   1069 
   1070 /*
   1071  * swread: the read function for the drum (just a call to physio)
   1072  */
   1073 /*ARGSUSED*/
   1074 int
   1075 swread(dev, uio, ioflag)
   1076 	dev_t dev;
   1077 	struct uio *uio;
   1078 	int ioflag;
   1079 {
   1080 	UVMHIST_FUNC("swread"); UVMHIST_CALLED(pdhist);
   1081 
   1082 	UVMHIST_LOG(pdhist, "  dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
   1083 	return (physio(swstrategy, NULL, dev, B_READ, minphys, uio));
   1084 }
   1085 
   1086 /*
   1087  * swwrite: the write function for the drum (just a call to physio)
   1088  */
   1089 /*ARGSUSED*/
   1090 int
   1091 swwrite(dev, uio, ioflag)
   1092 	dev_t dev;
   1093 	struct uio *uio;
   1094 	int ioflag;
   1095 {
   1096 	UVMHIST_FUNC("swwrite"); UVMHIST_CALLED(pdhist);
   1097 
   1098 	UVMHIST_LOG(pdhist, "  dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
   1099 	return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio));
   1100 }
   1101 
   1102 /*
   1103  * swstrategy: perform I/O on the drum
   1104  *
   1105  * => we must map the i/o request from the drum to the correct swapdev.
   1106  */
   1107 void
   1108 swstrategy(bp)
   1109 	struct buf *bp;
   1110 {
   1111 	struct swapdev *sdp;
   1112 	struct vnode *vp;
   1113 	int s, pageno, bn;
   1114 	UVMHIST_FUNC("swstrategy"); UVMHIST_CALLED(pdhist);
   1115 
   1116 	/*
   1117 	 * convert block number to swapdev.   note that swapdev can't
   1118 	 * be yanked out from under us because we are holding resources
   1119 	 * in it (i.e. the blocks we are doing I/O on).
   1120 	 */
   1121 	pageno = dbtob((int64_t)bp->b_blkno) >> PAGE_SHIFT;
   1122 	simple_lock(&uvm.swap_data_lock);
   1123 	sdp = swapdrum_getsdp(pageno);
   1124 	simple_unlock(&uvm.swap_data_lock);
   1125 	if (sdp == NULL) {
   1126 		bp->b_error = EINVAL;
   1127 		bp->b_flags |= B_ERROR;
   1128 		biodone(bp);
   1129 		UVMHIST_LOG(pdhist, "  failed to get swap device", 0, 0, 0, 0);
   1130 		return;
   1131 	}
   1132 
   1133 	/*
   1134 	 * convert drum page number to block number on this swapdev.
   1135 	 */
   1136 
   1137 	pageno -= sdp->swd_drumoffset;	/* page # on swapdev */
   1138 	bn = btodb((u_int64_t)pageno << PAGE_SHIFT); /* convert to diskblock */
   1139 
   1140 	UVMHIST_LOG(pdhist, "  %s: mapoff=%x bn=%x bcount=%ld",
   1141 		((bp->b_flags & B_READ) == 0) ? "write" : "read",
   1142 		sdp->swd_drumoffset, bn, bp->b_bcount);
   1143 
   1144 	/*
   1145 	 * for block devices we finish up here.
   1146 	 * for regular files we have to do more work which we delegate
   1147 	 * to sw_reg_strategy().
   1148 	 */
   1149 
   1150 	switch (sdp->swd_vp->v_type) {
   1151 	default:
   1152 		panic("swstrategy: vnode type 0x%x", sdp->swd_vp->v_type);
   1153 
   1154 	case VBLK:
   1155 
   1156 		/*
   1157 		 * must convert "bp" from an I/O on /dev/drum to an I/O
   1158 		 * on the swapdev (sdp).
   1159 		 */
   1160 		s = splbio();
   1161 		bp->b_blkno = bn;		/* swapdev block number */
   1162 		vp = sdp->swd_vp;		/* swapdev vnode pointer */
   1163 		bp->b_dev = sdp->swd_dev;	/* swapdev dev_t */
   1164 
   1165 		/*
   1166 		 * if we are doing a write, we have to redirect the i/o on
   1167 		 * drum's v_numoutput counter to the swapdevs.
   1168 		 */
   1169 		if ((bp->b_flags & B_READ) == 0) {
   1170 			vwakeup(bp);	/* kills one 'v_numoutput' on drum */
   1171 			V_INCR_NUMOUTPUT(vp);	/* put it on swapdev */
   1172 		}
   1173 
   1174 		/*
   1175 		 * finally plug in swapdev vnode and start I/O
   1176 		 */
   1177 		bp->b_vp = vp;
   1178 		splx(s);
   1179 		VOP_STRATEGY(vp, bp);
   1180 		return;
   1181 
   1182 	case VREG:
   1183 		/*
   1184 		 * delegate to sw_reg_strategy function.
   1185 		 */
   1186 		sw_reg_strategy(sdp, bp, bn);
   1187 		return;
   1188 	}
   1189 	/* NOTREACHED */
   1190 }
   1191 
   1192 /*
   1193  * sw_reg_strategy: handle swap i/o to regular files
   1194  */
   1195 static void
   1196 sw_reg_strategy(sdp, bp, bn)
   1197 	struct swapdev	*sdp;
   1198 	struct buf	*bp;
   1199 	int		bn;
   1200 {
   1201 	struct vnode	*vp;
   1202 	struct vndxfer	*vnx;
   1203 	daddr_t		nbn;
   1204 	caddr_t		addr;
   1205 	off_t		byteoff;
   1206 	int		s, off, nra, error, sz, resid;
   1207 	UVMHIST_FUNC("sw_reg_strategy"); UVMHIST_CALLED(pdhist);
   1208 
   1209 	/*
   1210 	 * allocate a vndxfer head for this transfer and point it to
   1211 	 * our buffer.
   1212 	 */
   1213 	getvndxfer(vnx);
   1214 	vnx->vx_flags = VX_BUSY;
   1215 	vnx->vx_error = 0;
   1216 	vnx->vx_pending = 0;
   1217 	vnx->vx_bp = bp;
   1218 	vnx->vx_sdp = sdp;
   1219 
   1220 	/*
   1221 	 * setup for main loop where we read filesystem blocks into
   1222 	 * our buffer.
   1223 	 */
   1224 	error = 0;
   1225 	bp->b_resid = bp->b_bcount;	/* nothing transfered yet! */
   1226 	addr = bp->b_data;		/* current position in buffer */
   1227 	byteoff = dbtob((u_int64_t)bn);
   1228 
   1229 	for (resid = bp->b_resid; resid; resid -= sz) {
   1230 		struct vndbuf	*nbp;
   1231 
   1232 		/*
   1233 		 * translate byteoffset into block number.  return values:
   1234 		 *   vp = vnode of underlying device
   1235 		 *  nbn = new block number (on underlying vnode dev)
   1236 		 *  nra = num blocks we can read-ahead (excludes requested
   1237 		 *	block)
   1238 		 */
   1239 		nra = 0;
   1240 		error = VOP_BMAP(sdp->swd_vp, byteoff / sdp->swd_bsize,
   1241 				 	&vp, &nbn, &nra);
   1242 
   1243 		if (error == 0 && nbn == (daddr_t)-1) {
   1244 			/*
   1245 			 * this used to just set error, but that doesn't
   1246 			 * do the right thing.  Instead, it causes random
   1247 			 * memory errors.  The panic() should remain until
   1248 			 * this condition doesn't destabilize the system.
   1249 			 */
   1250 #if 1
   1251 			panic("sw_reg_strategy: swap to sparse file");
   1252 #else
   1253 			error = EIO;	/* failure */
   1254 #endif
   1255 		}
   1256 
   1257 		/*
   1258 		 * punt if there was an error or a hole in the file.
   1259 		 * we must wait for any i/o ops we have already started
   1260 		 * to finish before returning.
   1261 		 *
   1262 		 * XXX we could deal with holes here but it would be
   1263 		 * a hassle (in the write case).
   1264 		 */
   1265 		if (error) {
   1266 			s = splbio();
   1267 			vnx->vx_error = error;	/* pass error up */
   1268 			goto out;
   1269 		}
   1270 
   1271 		/*
   1272 		 * compute the size ("sz") of this transfer (in bytes).
   1273 		 */
   1274 		off = byteoff % sdp->swd_bsize;
   1275 		sz = (1 + nra) * sdp->swd_bsize - off;
   1276 		if (sz > resid)
   1277 			sz = resid;
   1278 
   1279 		UVMHIST_LOG(pdhist, "sw_reg_strategy: "
   1280 			    "vp %p/%p offset 0x%x/0x%x",
   1281 			    sdp->swd_vp, vp, byteoff, nbn);
   1282 
   1283 		/*
   1284 		 * now get a buf structure.   note that the vb_buf is
   1285 		 * at the front of the nbp structure so that you can
   1286 		 * cast pointers between the two structure easily.
   1287 		 */
   1288 		getvndbuf(nbp);
   1289 		BUF_INIT(&nbp->vb_buf);
   1290 		nbp->vb_buf.b_flags    = bp->b_flags | B_CALL;
   1291 		nbp->vb_buf.b_bcount   = sz;
   1292 		nbp->vb_buf.b_bufsize  = sz;
   1293 		nbp->vb_buf.b_error    = 0;
   1294 		nbp->vb_buf.b_data     = addr;
   1295 		nbp->vb_buf.b_lblkno   = 0;
   1296 		nbp->vb_buf.b_blkno    = nbn + btodb(off);
   1297 		nbp->vb_buf.b_rawblkno = nbp->vb_buf.b_blkno;
   1298 		nbp->vb_buf.b_iodone   = sw_reg_iodone;
   1299 		nbp->vb_buf.b_vp       = vp;
   1300 		if (vp->v_type == VBLK) {
   1301 			nbp->vb_buf.b_dev = vp->v_rdev;
   1302 		}
   1303 
   1304 		nbp->vb_xfer = vnx;	/* patch it back in to vnx */
   1305 
   1306 		/*
   1307 		 * Just sort by block number
   1308 		 */
   1309 		s = splbio();
   1310 		if (vnx->vx_error != 0) {
   1311 			putvndbuf(nbp);
   1312 			goto out;
   1313 		}
   1314 		vnx->vx_pending++;
   1315 
   1316 		/* sort it in and start I/O if we are not over our limit */
   1317 		BUFQ_PUT(&sdp->swd_tab, &nbp->vb_buf);
   1318 		sw_reg_start(sdp);
   1319 		splx(s);
   1320 
   1321 		/*
   1322 		 * advance to the next I/O
   1323 		 */
   1324 		byteoff += sz;
   1325 		addr += sz;
   1326 	}
   1327 
   1328 	s = splbio();
   1329 
   1330 out: /* Arrive here at splbio */
   1331 	vnx->vx_flags &= ~VX_BUSY;
   1332 	if (vnx->vx_pending == 0) {
   1333 		if (vnx->vx_error != 0) {
   1334 			bp->b_error = vnx->vx_error;
   1335 			bp->b_flags |= B_ERROR;
   1336 		}
   1337 		putvndxfer(vnx);
   1338 		biodone(bp);
   1339 	}
   1340 	splx(s);
   1341 }
   1342 
   1343 /*
   1344  * sw_reg_start: start an I/O request on the requested swapdev
   1345  *
   1346  * => reqs are sorted by b_rawblkno (above)
   1347  */
   1348 static void
   1349 sw_reg_start(sdp)
   1350 	struct swapdev	*sdp;
   1351 {
   1352 	struct buf	*bp;
   1353 	UVMHIST_FUNC("sw_reg_start"); UVMHIST_CALLED(pdhist);
   1354 
   1355 	/* recursion control */
   1356 	if ((sdp->swd_flags & SWF_BUSY) != 0)
   1357 		return;
   1358 
   1359 	sdp->swd_flags |= SWF_BUSY;
   1360 
   1361 	while (sdp->swd_active < sdp->swd_maxactive) {
   1362 		bp = BUFQ_GET(&sdp->swd_tab);
   1363 		if (bp == NULL)
   1364 			break;
   1365 		sdp->swd_active++;
   1366 
   1367 		UVMHIST_LOG(pdhist,
   1368 		    "sw_reg_start:  bp %p vp %p blkno %p cnt %lx",
   1369 		    bp, bp->b_vp, bp->b_blkno, bp->b_bcount);
   1370 		if ((bp->b_flags & B_READ) == 0)
   1371 			V_INCR_NUMOUTPUT(bp->b_vp);
   1372 
   1373 		VOP_STRATEGY(bp->b_vp, bp);
   1374 	}
   1375 	sdp->swd_flags &= ~SWF_BUSY;
   1376 }
   1377 
   1378 /*
   1379  * sw_reg_iodone: one of our i/o's has completed and needs post-i/o cleanup
   1380  *
   1381  * => note that we can recover the vndbuf struct by casting the buf ptr
   1382  */
   1383 static void
   1384 sw_reg_iodone(bp)
   1385 	struct buf *bp;
   1386 {
   1387 	struct vndbuf *vbp = (struct vndbuf *) bp;
   1388 	struct vndxfer *vnx = vbp->vb_xfer;
   1389 	struct buf *pbp = vnx->vx_bp;		/* parent buffer */
   1390 	struct swapdev	*sdp = vnx->vx_sdp;
   1391 	int s, resid, error;
   1392 	UVMHIST_FUNC("sw_reg_iodone"); UVMHIST_CALLED(pdhist);
   1393 
   1394 	UVMHIST_LOG(pdhist, "  vbp=%p vp=%p blkno=%x addr=%p",
   1395 	    vbp, vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno, vbp->vb_buf.b_data);
   1396 	UVMHIST_LOG(pdhist, "  cnt=%lx resid=%lx",
   1397 	    vbp->vb_buf.b_bcount, vbp->vb_buf.b_resid, 0, 0);
   1398 
   1399 	/*
   1400 	 * protect vbp at splbio and update.
   1401 	 */
   1402 
   1403 	s = splbio();
   1404 	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
   1405 	pbp->b_resid -= resid;
   1406 	vnx->vx_pending--;
   1407 
   1408 	if (vbp->vb_buf.b_flags & B_ERROR) {
   1409 		/* pass error upward */
   1410 		error = vbp->vb_buf.b_error ? vbp->vb_buf.b_error : EIO;
   1411 		UVMHIST_LOG(pdhist, "  got error=%d !", error, 0, 0, 0);
   1412 		vnx->vx_error = error;
   1413 	}
   1414 
   1415 	/*
   1416 	 * kill vbp structure
   1417 	 */
   1418 	putvndbuf(vbp);
   1419 
   1420 	/*
   1421 	 * wrap up this transaction if it has run to completion or, in
   1422 	 * case of an error, when all auxiliary buffers have returned.
   1423 	 */
   1424 	if (vnx->vx_error != 0) {
   1425 		/* pass error upward */
   1426 		pbp->b_flags |= B_ERROR;
   1427 		pbp->b_error = vnx->vx_error;
   1428 		if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
   1429 			putvndxfer(vnx);
   1430 			biodone(pbp);
   1431 		}
   1432 	} else if (pbp->b_resid == 0) {
   1433 		KASSERT(vnx->vx_pending == 0);
   1434 		if ((vnx->vx_flags & VX_BUSY) == 0) {
   1435 			UVMHIST_LOG(pdhist, "  iodone error=%d !",
   1436 			    pbp, vnx->vx_error, 0, 0);
   1437 			putvndxfer(vnx);
   1438 			biodone(pbp);
   1439 		}
   1440 	}
   1441 
   1442 	/*
   1443 	 * done!   start next swapdev I/O if one is pending
   1444 	 */
   1445 	sdp->swd_active--;
   1446 	sw_reg_start(sdp);
   1447 	splx(s);
   1448 }
   1449 
   1450 
   1451 /*
   1452  * uvm_swap_alloc: allocate space on swap
   1453  *
   1454  * => allocation is done "round robin" down the priority list, as we
   1455  *	allocate in a priority we "rotate" the circle queue.
   1456  * => space can be freed with uvm_swap_free
   1457  * => we return the page slot number in /dev/drum (0 == invalid slot)
   1458  * => we lock uvm.swap_data_lock
   1459  * => XXXMRG: "LESSOK" INTERFACE NEEDED TO EXTENT SYSTEM
   1460  */
   1461 int
   1462 uvm_swap_alloc(nslots, lessok)
   1463 	int *nslots;	/* IN/OUT */
   1464 	boolean_t lessok;
   1465 {
   1466 	struct swapdev *sdp;
   1467 	struct swappri *spp;
   1468 	u_long	result;
   1469 	UVMHIST_FUNC("uvm_swap_alloc"); UVMHIST_CALLED(pdhist);
   1470 
   1471 	/*
   1472 	 * no swap devices configured yet?   definite failure.
   1473 	 */
   1474 	if (uvmexp.nswapdev < 1)
   1475 		return 0;
   1476 
   1477 	/*
   1478 	 * lock data lock, convert slots into blocks, and enter loop
   1479 	 */
   1480 	simple_lock(&uvm.swap_data_lock);
   1481 
   1482 ReTry:	/* XXXMRG */
   1483 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
   1484 		CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
   1485 			/* if it's not enabled, then we can't swap from it */
   1486 			if ((sdp->swd_flags & SWF_ENABLE) == 0)
   1487 				continue;
   1488 			if (sdp->swd_npginuse + *nslots > sdp->swd_npages)
   1489 				continue;
   1490 			if (extent_alloc(sdp->swd_ex, *nslots, EX_NOALIGN,
   1491 					 EX_NOBOUNDARY, EX_MALLOCOK|EX_NOWAIT,
   1492 					 &result) != 0) {
   1493 				continue;
   1494 			}
   1495 
   1496 			/*
   1497 			 * successful allocation!  now rotate the circleq.
   1498 			 */
   1499 			CIRCLEQ_REMOVE(&spp->spi_swapdev, sdp, swd_next);
   1500 			CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
   1501 			sdp->swd_npginuse += *nslots;
   1502 			uvmexp.swpginuse += *nslots;
   1503 			simple_unlock(&uvm.swap_data_lock);
   1504 			/* done!  return drum slot number */
   1505 			UVMHIST_LOG(pdhist,
   1506 			    "success!  returning %d slots starting at %d",
   1507 			    *nslots, result + sdp->swd_drumoffset, 0, 0);
   1508 			return (result + sdp->swd_drumoffset);
   1509 		}
   1510 	}
   1511 
   1512 	/* XXXMRG: BEGIN HACK */
   1513 	if (*nslots > 1 && lessok) {
   1514 		*nslots = 1;
   1515 		goto ReTry;	/* XXXMRG: ugh!  extent should support this for us */
   1516 	}
   1517 	/* XXXMRG: END HACK */
   1518 
   1519 	simple_unlock(&uvm.swap_data_lock);
   1520 	return 0;
   1521 }
   1522 
   1523 boolean_t
   1524 uvm_swapisfull(void)
   1525 {
   1526 	boolean_t rv;
   1527 
   1528 	simple_lock(&uvm.swap_data_lock);
   1529 	KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
   1530 	rv = (uvmexp.swpgonly >= uvmexp.swpgavail);
   1531 	simple_unlock(&uvm.swap_data_lock);
   1532 
   1533 	return (rv);
   1534 }
   1535 
   1536 /*
   1537  * uvm_swap_markbad: keep track of swap ranges where we've had i/o errors
   1538  *
   1539  * => we lock uvm.swap_data_lock
   1540  */
   1541 void
   1542 uvm_swap_markbad(startslot, nslots)
   1543 	int startslot;
   1544 	int nslots;
   1545 {
   1546 	struct swapdev *sdp;
   1547 	UVMHIST_FUNC("uvm_swap_markbad"); UVMHIST_CALLED(pdhist);
   1548 
   1549 	simple_lock(&uvm.swap_data_lock);
   1550 	sdp = swapdrum_getsdp(startslot);
   1551 	KASSERT(sdp != NULL);
   1552 
   1553 	/*
   1554 	 * we just keep track of how many pages have been marked bad
   1555 	 * in this device, to make everything add up in swap_off().
   1556 	 * we assume here that the range of slots will all be within
   1557 	 * one swap device.
   1558 	 */
   1559 
   1560 	KASSERT(uvmexp.swpgonly >= nslots);
   1561 	uvmexp.swpgonly -= nslots;
   1562 	sdp->swd_npgbad += nslots;
   1563 	UVMHIST_LOG(pdhist, "now %d bad", sdp->swd_npgbad, 0,0,0);
   1564 	simple_unlock(&uvm.swap_data_lock);
   1565 }
   1566 
   1567 /*
   1568  * uvm_swap_free: free swap slots
   1569  *
   1570  * => this can be all or part of an allocation made by uvm_swap_alloc
   1571  * => we lock uvm.swap_data_lock
   1572  */
   1573 void
   1574 uvm_swap_free(startslot, nslots)
   1575 	int startslot;
   1576 	int nslots;
   1577 {
   1578 	struct swapdev *sdp;
   1579 	UVMHIST_FUNC("uvm_swap_free"); UVMHIST_CALLED(pdhist);
   1580 
   1581 	UVMHIST_LOG(pdhist, "freeing %d slots starting at %d", nslots,
   1582 	    startslot, 0, 0);
   1583 
   1584 	/*
   1585 	 * ignore attempts to free the "bad" slot.
   1586 	 */
   1587 
   1588 	if (startslot == SWSLOT_BAD) {
   1589 		return;
   1590 	}
   1591 
   1592 	/*
   1593 	 * convert drum slot offset back to sdp, free the blocks
   1594 	 * in the extent, and return.   must hold pri lock to do
   1595 	 * lookup and access the extent.
   1596 	 */
   1597 
   1598 	simple_lock(&uvm.swap_data_lock);
   1599 	sdp = swapdrum_getsdp(startslot);
   1600 	KASSERT(uvmexp.nswapdev >= 1);
   1601 	KASSERT(sdp != NULL);
   1602 	KASSERT(sdp->swd_npginuse >= nslots);
   1603 	if (extent_free(sdp->swd_ex, startslot - sdp->swd_drumoffset, nslots,
   1604 			EX_MALLOCOK|EX_NOWAIT) != 0) {
   1605 		printf("warning: resource shortage: %d pages of swap lost\n",
   1606 			nslots);
   1607 	}
   1608 	sdp->swd_npginuse -= nslots;
   1609 	uvmexp.swpginuse -= nslots;
   1610 	simple_unlock(&uvm.swap_data_lock);
   1611 }
   1612 
   1613 /*
   1614  * uvm_swap_put: put any number of pages into a contig place on swap
   1615  *
   1616  * => can be sync or async
   1617  */
   1618 
   1619 int
   1620 uvm_swap_put(swslot, ppsp, npages, flags)
   1621 	int swslot;
   1622 	struct vm_page **ppsp;
   1623 	int npages;
   1624 	int flags;
   1625 {
   1626 	int error;
   1627 
   1628 	error = uvm_swap_io(ppsp, swslot, npages, B_WRITE |
   1629 	    ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
   1630 	return error;
   1631 }
   1632 
   1633 /*
   1634  * uvm_swap_get: get a single page from swap
   1635  *
   1636  * => usually a sync op (from fault)
   1637  */
   1638 
   1639 int
   1640 uvm_swap_get(page, swslot, flags)
   1641 	struct vm_page *page;
   1642 	int swslot, flags;
   1643 {
   1644 	int error;
   1645 
   1646 	uvmexp.nswget++;
   1647 	KASSERT(flags & PGO_SYNCIO);
   1648 	if (swslot == SWSLOT_BAD) {
   1649 		return EIO;
   1650 	}
   1651 
   1652 	error = uvm_swap_io(&page, swslot, 1, B_READ |
   1653 	    ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
   1654 	if (error == 0) {
   1655 
   1656 		/*
   1657 		 * this page is no longer only in swap.
   1658 		 */
   1659 
   1660 		simple_lock(&uvm.swap_data_lock);
   1661 		KASSERT(uvmexp.swpgonly > 0);
   1662 		uvmexp.swpgonly--;
   1663 		simple_unlock(&uvm.swap_data_lock);
   1664 	}
   1665 	return error;
   1666 }
   1667 
   1668 /*
   1669  * uvm_swap_io: do an i/o operation to swap
   1670  */
   1671 
   1672 static int
   1673 uvm_swap_io(pps, startslot, npages, flags)
   1674 	struct vm_page **pps;
   1675 	int startslot, npages, flags;
   1676 {
   1677 	daddr_t startblk;
   1678 	struct	buf *bp;
   1679 	vaddr_t kva;
   1680 	int	error, s, mapinflags;
   1681 	boolean_t write, async;
   1682 	UVMHIST_FUNC("uvm_swap_io"); UVMHIST_CALLED(pdhist);
   1683 
   1684 	UVMHIST_LOG(pdhist, "<- called, startslot=%d, npages=%d, flags=%d",
   1685 	    startslot, npages, flags, 0);
   1686 
   1687 	write = (flags & B_READ) == 0;
   1688 	async = (flags & B_ASYNC) != 0;
   1689 
   1690 	/*
   1691 	 * convert starting drum slot to block number
   1692 	 */
   1693 
   1694 	startblk = btodb((u_int64_t)startslot << PAGE_SHIFT);
   1695 
   1696 	/*
   1697 	 * first, map the pages into the kernel.
   1698 	 */
   1699 
   1700 	mapinflags = !write ?
   1701 		UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_READ :
   1702 		UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_WRITE;
   1703 	kva = uvm_pagermapin(pps, npages, mapinflags);
   1704 
   1705 	/*
   1706 	 * now allocate a buf for the i/o.
   1707 	 */
   1708 
   1709 	s = splbio();
   1710 	bp = pool_get(&bufpool, PR_WAITOK);
   1711 	splx(s);
   1712 
   1713 	/*
   1714 	 * fill in the bp/sbp.   we currently route our i/o through
   1715 	 * /dev/drum's vnode [swapdev_vp].
   1716 	 */
   1717 
   1718 	BUF_INIT(bp);
   1719 	bp->b_flags = B_BUSY | B_NOCACHE | (flags & (B_READ|B_ASYNC));
   1720 	bp->b_proc = &proc0;	/* XXX */
   1721 	bp->b_vnbufs.le_next = NOLIST;
   1722 	bp->b_data = (caddr_t)kva;
   1723 	bp->b_blkno = startblk;
   1724 	bp->b_vp = swapdev_vp;
   1725 	bp->b_bufsize = bp->b_bcount = npages << PAGE_SHIFT;
   1726 
   1727 	/*
   1728 	 * bump v_numoutput (counter of number of active outputs).
   1729 	 */
   1730 
   1731 	if (write) {
   1732 		s = splbio();
   1733 		V_INCR_NUMOUTPUT(swapdev_vp);
   1734 		splx(s);
   1735 	}
   1736 
   1737 	/*
   1738 	 * for async ops we must set up the iodone handler.
   1739 	 */
   1740 
   1741 	if (async) {
   1742 		bp->b_flags |= B_CALL;
   1743 		bp->b_iodone = uvm_aio_biodone;
   1744 		UVMHIST_LOG(pdhist, "doing async!", 0, 0, 0, 0);
   1745 		if (curproc == uvm.pagedaemon_proc)
   1746 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1747 		else
   1748 			BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
   1749 	} else {
   1750 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1751 	}
   1752 	UVMHIST_LOG(pdhist,
   1753 	    "about to start io: data = %p blkno = 0x%x, bcount = %ld",
   1754 	    bp->b_data, bp->b_blkno, bp->b_bcount, 0);
   1755 
   1756 	/*
   1757 	 * now we start the I/O, and if async, return.
   1758 	 */
   1759 
   1760 	VOP_STRATEGY(swapdev_vp, bp);
   1761 	if (async)
   1762 		return 0;
   1763 
   1764 	/*
   1765 	 * must be sync i/o.   wait for it to finish
   1766 	 */
   1767 
   1768 	error = biowait(bp);
   1769 
   1770 	/*
   1771 	 * kill the pager mapping
   1772 	 */
   1773 
   1774 	uvm_pagermapout(kva, npages);
   1775 
   1776 	/*
   1777 	 * now dispose of the buf and we're done.
   1778 	 */
   1779 
   1780 	s = splbio();
   1781 	if (write)
   1782 		vwakeup(bp);
   1783 	pool_put(&bufpool, bp);
   1784 	splx(s);
   1785 	UVMHIST_LOG(pdhist, "<- done (sync)  error=%d", error, 0, 0, 0);
   1786 	return (error);
   1787 }
   1788