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