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