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