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