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