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