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