Home | History | Annotate | Line # | Download | only in uvm
uvm_swap.c revision 1.122.2.14
      1 /*	$NetBSD: uvm_swap.c,v 1.122.2.14 2007/10/23 20:17:32 ad 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 <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: uvm_swap.c,v 1.122.2.14 2007/10/23 20:17:32 ad Exp $");
     36 
     37 #include "fs_nfs.h"
     38 #include "opt_uvmhist.h"
     39 #include "opt_compat_netbsd.h"
     40 #include "opt_ddb.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/buf.h>
     45 #include <sys/bufq.h>
     46 #include <sys/conf.h>
     47 #include <sys/proc.h>
     48 #include <sys/namei.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/errno.h>
     51 #include <sys/kernel.h>
     52 #include <sys/malloc.h>
     53 #include <sys/vnode.h>
     54 #include <sys/file.h>
     55 #include <sys/vmem.h>
     56 #include <sys/blist.h>
     57 #include <sys/mount.h>
     58 #include <sys/pool.h>
     59 #include <sys/syscallargs.h>
     60 #include <sys/swap.h>
     61 #include <sys/kauth.h>
     62 #include <sys/sysctl.h>
     63 #include <sys/workqueue.h>
     64 
     65 #include <uvm/uvm.h>
     66 
     67 #include <miscfs/specfs/specdev.h>
     68 
     69 /*
     70  * uvm_swap.c: manage configuration and i/o to swap space.
     71  */
     72 
     73 /*
     74  * swap space is managed in the following way:
     75  *
     76  * each swap partition or file is described by a "swapdev" structure.
     77  * each "swapdev" structure contains a "swapent" structure which contains
     78  * information that is passed up to the user (via system calls).
     79  *
     80  * each swap partition is assigned a "priority" (int) which controls
     81  * swap parition usage.
     82  *
     83  * the system maintains a global data structure describing all swap
     84  * partitions/files.   there is a sorted LIST of "swappri" structures
     85  * which describe "swapdev"'s at that priority.   this LIST is headed
     86  * by the "swap_priority" global var.    each "swappri" contains a
     87  * CIRCLEQ of "swapdev" structures at that priority.
     88  *
     89  * locking:
     90  *  - swap_syscall_lock (krwlock_t): this lock serializes the swapctl
     91  *    system call and prevents the swap priority list from changing
     92  *    while we are in the middle of a system call (e.g. SWAP_STATS).
     93  *  - uvm_swap_data_lock (kmutex_t): this lock protects all swap data
     94  *    structures including the priority list, the swapdev structures,
     95  *    and the swapmap arena.
     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  *
    107  * userland controls and configures swap with the swapctl(2) system call.
    108  * the sys_swapctl performs the following operations:
    109  *  [1] SWAP_NSWAP: returns the number of swap devices currently configured
    110  *  [2] SWAP_STATS: given a pointer to an array of swapent structures
    111  *	(passed in via "arg") of a size passed in via "misc" ... we load
    112  *	the current swap config into the array. The actual work is done
    113  *	in the uvm_swap_stats(9) function.
    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  * swapdev: describes a single swap partition/file
    123  *
    124  * note the following should be true:
    125  * swd_inuse <= swd_nblks  [number of blocks in use is <= total blocks]
    126  * swd_nblks <= swd_mapsize [because mapsize includes miniroot+disklabel]
    127  */
    128 struct swapdev {
    129 	struct oswapent swd_ose;
    130 #define	swd_dev		swd_ose.ose_dev		/* device id */
    131 #define	swd_flags	swd_ose.ose_flags	/* flags:inuse/enable/fake */
    132 #define	swd_priority	swd_ose.ose_priority	/* our priority */
    133 	/* also: swd_ose.ose_nblks, swd_ose.ose_inuse */
    134 	char			*swd_path;	/* saved pathname of device */
    135 	int			swd_pathlen;	/* length of pathname */
    136 	int			swd_npages;	/* #pages we can use */
    137 	int			swd_npginuse;	/* #pages in use */
    138 	int			swd_npgbad;	/* #pages bad */
    139 	int			swd_drumoffset;	/* page0 offset in drum */
    140 	int			swd_drumsize;	/* #pages in drum */
    141 	blist_t			swd_blist;	/* blist for this swapdev */
    142 	struct vnode		*swd_vp;	/* backing vnode */
    143 	CIRCLEQ_ENTRY(swapdev)	swd_next;	/* priority circleq */
    144 
    145 	int			swd_bsize;	/* blocksize (bytes) */
    146 	int			swd_maxactive;	/* max active i/o reqs */
    147 	struct bufq_state	*swd_tab;	/* buffer list */
    148 	int			swd_active;	/* number of active buffers */
    149 };
    150 
    151 /*
    152  * swap device priority entry; the list is kept sorted on `spi_priority'.
    153  */
    154 struct swappri {
    155 	int			spi_priority;     /* priority */
    156 	CIRCLEQ_HEAD(spi_swapdev, swapdev)	spi_swapdev;
    157 	/* circleq of swapdevs at this priority */
    158 	LIST_ENTRY(swappri)	spi_swappri;      /* global list of pri's */
    159 };
    160 
    161 /*
    162  * The following two structures are used to keep track of data transfers
    163  * on swap devices associated with regular files.
    164  * NOTE: this code is more or less a copy of vnd.c; we use the same
    165  * structure names here to ease porting..
    166  */
    167 struct vndxfer {
    168 	struct buf	*vx_bp;		/* Pointer to parent buffer */
    169 	struct swapdev	*vx_sdp;
    170 	int		vx_error;
    171 	int		vx_pending;	/* # of pending aux buffers */
    172 	int		vx_flags;
    173 #define VX_BUSY		1
    174 #define VX_DEAD		2
    175 };
    176 
    177 struct vndbuf {
    178 	struct buf	vb_buf;
    179 	struct vndxfer	*vb_xfer;
    180 };
    181 
    182 
    183 /*
    184  * We keep a of pool vndbuf's and vndxfer structures.
    185  */
    186 POOL_INIT(vndxfer_pool, sizeof(struct vndxfer), 0, 0, 0, "swp vnx", NULL,
    187     IPL_BIO);
    188 POOL_INIT(vndbuf_pool, sizeof(struct vndbuf), 0, 0, 0, "swp vnd", NULL,
    189     IPL_BIO);
    190 
    191 /*
    192  * local variables
    193  */
    194 MALLOC_DEFINE(M_VMSWAP, "VM swap", "VM swap structures");
    195 static vmem_t *swapmap;	/* controls the mapping of /dev/drum */
    196 
    197 /* list of all active swap devices [by priority] */
    198 LIST_HEAD(swap_priority, swappri);
    199 static struct swap_priority swap_priority;
    200 
    201 /* locks */
    202 static krwlock_t swap_syscall_lock;
    203 
    204 /* workqueue and use counter for swap to regular files */
    205 static int sw_reg_count = 0;
    206 static struct workqueue *sw_reg_workqueue;
    207 
    208 /*
    209  * prototypes
    210  */
    211 static struct swapdev	*swapdrum_getsdp(int);
    212 
    213 static struct swapdev	*swaplist_find(struct vnode *, bool);
    214 static void		 swaplist_insert(struct swapdev *,
    215 					 struct swappri *, int);
    216 static void		 swaplist_trim(void);
    217 
    218 static int swap_on(struct lwp *, struct swapdev *);
    219 static int swap_off(struct lwp *, struct swapdev *);
    220 
    221 static void uvm_swap_stats_locked(int, struct swapent *, int, register_t *);
    222 
    223 static void sw_reg_strategy(struct swapdev *, struct buf *, int);
    224 static void sw_reg_biodone(struct buf *);
    225 static void sw_reg_iodone(struct work *wk, void *dummy);
    226 static void sw_reg_start(struct swapdev *);
    227 
    228 static int uvm_swap_io(struct vm_page **, int, int, int);
    229 
    230 /*
    231  * uvm_swap_init: init the swap system data structures and locks
    232  *
    233  * => called at boot time from init_main.c after the filesystems
    234  *	are brought up (which happens after uvm_init())
    235  */
    236 void
    237 uvm_swap_init(void)
    238 {
    239 	UVMHIST_FUNC("uvm_swap_init");
    240 
    241 	UVMHIST_CALLED(pdhist);
    242 	/*
    243 	 * first, init the swap list, its counter, and its lock.
    244 	 * then get a handle on the vnode for /dev/drum by using
    245 	 * the its dev_t number ("swapdev", from MD conf.c).
    246 	 */
    247 
    248 	LIST_INIT(&swap_priority);
    249 	uvmexp.nswapdev = 0;
    250 	rw_init(&swap_syscall_lock);
    251 	cv_init(&uvm.scheduler_cv, "schedule");
    252 	mutex_init(&uvm_swap_data_lock, MUTEX_DEFAULT, IPL_NONE);
    253 
    254 	/* XXXSMP should be at IPL_VM, but for audio interrupt handlers. */
    255 	mutex_init(&uvm_scheduler_mutex, MUTEX_SPIN, IPL_SCHED);
    256 
    257 	if (bdevvp(swapdev, &swapdev_vp))
    258 		panic("uvm_swap_init: can't get vnode for swap device");
    259 
    260 	/*
    261 	 * create swap block resource map to map /dev/drum.   the range
    262 	 * from 1 to INT_MAX allows 2 gigablocks of swap space.  note
    263 	 * that block 0 is reserved (used to indicate an allocation
    264 	 * failure, or no allocation).
    265 	 */
    266 	swapmap = vmem_create("swapmap", 1, INT_MAX - 1, 1, NULL, NULL, NULL, 0,
    267 	    VM_NOSLEEP, IPL_NONE);
    268 	if (swapmap == 0)
    269 		panic("uvm_swap_init: extent_create failed");
    270 
    271 	/*
    272 	 * done!
    273 	 */
    274 	uvm.swap_running = true;
    275 	uvm.swapout_enabled = 1;
    276 	UVMHIST_LOG(pdhist, "<- done", 0, 0, 0, 0);
    277 
    278         sysctl_createv(NULL, 0, NULL, NULL,
    279             CTLFLAG_READWRITE,
    280             CTLTYPE_INT, "swapout",
    281             SYSCTL_DESCR("Set 0 to disable swapout of kernel stacks"),
    282             NULL, 0, &uvm.swapout_enabled, 0, CTL_VM, CTL_CREATE, CTL_EOL);
    283 }
    284 
    285 /*
    286  * swaplist functions: functions that operate on the list of swap
    287  * devices on the system.
    288  */
    289 
    290 /*
    291  * swaplist_insert: insert swap device "sdp" into the global list
    292  *
    293  * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
    294  * => caller must provide a newly malloc'd swappri structure (we will
    295  *	FREE it if we don't need it... this it to prevent malloc blocking
    296  *	here while adding swap)
    297  */
    298 static void
    299 swaplist_insert(struct swapdev *sdp, struct swappri *newspp, int priority)
    300 {
    301 	struct swappri *spp, *pspp;
    302 	UVMHIST_FUNC("swaplist_insert"); UVMHIST_CALLED(pdhist);
    303 
    304 	/*
    305 	 * find entry at or after which to insert the new device.
    306 	 */
    307 	pspp = NULL;
    308 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    309 		if (priority <= spp->spi_priority)
    310 			break;
    311 		pspp = spp;
    312 	}
    313 
    314 	/*
    315 	 * new priority?
    316 	 */
    317 	if (spp == NULL || spp->spi_priority != priority) {
    318 		spp = newspp;  /* use newspp! */
    319 		UVMHIST_LOG(pdhist, "created new swappri = %d",
    320 			    priority, 0, 0, 0);
    321 
    322 		spp->spi_priority = priority;
    323 		CIRCLEQ_INIT(&spp->spi_swapdev);
    324 
    325 		if (pspp)
    326 			LIST_INSERT_AFTER(pspp, spp, spi_swappri);
    327 		else
    328 			LIST_INSERT_HEAD(&swap_priority, spp, spi_swappri);
    329 	} else {
    330 	  	/* we don't need a new priority structure, free it */
    331 		FREE(newspp, M_VMSWAP);
    332 	}
    333 
    334 	/*
    335 	 * priority found (or created).   now insert on the priority's
    336 	 * circleq list and bump the total number of swapdevs.
    337 	 */
    338 	sdp->swd_priority = priority;
    339 	CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
    340 	uvmexp.nswapdev++;
    341 }
    342 
    343 /*
    344  * swaplist_find: find and optionally remove a swap device from the
    345  *	global list.
    346  *
    347  * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
    348  * => we return the swapdev we found (and removed)
    349  */
    350 static struct swapdev *
    351 swaplist_find(struct vnode *vp, bool remove)
    352 {
    353 	struct swapdev *sdp;
    354 	struct swappri *spp;
    355 
    356 	/*
    357 	 * search the lists for the requested vp
    358 	 */
    359 
    360 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    361 		CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
    362 			if (sdp->swd_vp == vp) {
    363 				if (remove) {
    364 					CIRCLEQ_REMOVE(&spp->spi_swapdev,
    365 					    sdp, swd_next);
    366 					uvmexp.nswapdev--;
    367 				}
    368 				return(sdp);
    369 			}
    370 		}
    371 	}
    372 	return (NULL);
    373 }
    374 
    375 /*
    376  * swaplist_trim: scan priority list for empty priority entries and kill
    377  *	them.
    378  *
    379  * => caller must hold both swap_syscall_lock and uvm_swap_data_lock
    380  */
    381 static void
    382 swaplist_trim(void)
    383 {
    384 	struct swappri *spp, *nextspp;
    385 
    386 	for (spp = LIST_FIRST(&swap_priority); spp != NULL; spp = nextspp) {
    387 		nextspp = LIST_NEXT(spp, spi_swappri);
    388 		if (CIRCLEQ_FIRST(&spp->spi_swapdev) !=
    389 		    (void *)&spp->spi_swapdev)
    390 			continue;
    391 		LIST_REMOVE(spp, spi_swappri);
    392 		free(spp, M_VMSWAP);
    393 	}
    394 }
    395 
    396 /*
    397  * swapdrum_getsdp: given a page offset in /dev/drum, convert it back
    398  *	to the "swapdev" that maps that section of the drum.
    399  *
    400  * => each swapdev takes one big contig chunk of the drum
    401  * => caller must hold uvm_swap_data_lock
    402  */
    403 static struct swapdev *
    404 swapdrum_getsdp(int pgno)
    405 {
    406 	struct swapdev *sdp;
    407 	struct swappri *spp;
    408 
    409 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    410 		CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
    411 			if (sdp->swd_flags & SWF_FAKE)
    412 				continue;
    413 			if (pgno >= sdp->swd_drumoffset &&
    414 			    pgno < (sdp->swd_drumoffset + sdp->swd_drumsize)) {
    415 				return sdp;
    416 			}
    417 		}
    418 	}
    419 	return NULL;
    420 }
    421 
    422 
    423 /*
    424  * sys_swapctl: main entry point for swapctl(2) system call
    425  * 	[with two helper functions: swap_on and swap_off]
    426  */
    427 int
    428 sys_swapctl(struct lwp *l, void *v, register_t *retval)
    429 {
    430 	struct sys_swapctl_args /* {
    431 		syscallarg(int) cmd;
    432 		syscallarg(void *) arg;
    433 		syscallarg(int) misc;
    434 	} */ *uap = (struct sys_swapctl_args *)v;
    435 	struct vnode *vp;
    436 	struct nameidata nd;
    437 	struct swappri *spp;
    438 	struct swapdev *sdp;
    439 	struct swapent *sep;
    440 #define SWAP_PATH_MAX (PATH_MAX + 1)
    441 	char	*userpath;
    442 	size_t	len;
    443 	int	error, misc;
    444 	int	priority;
    445 	UVMHIST_FUNC("sys_swapctl"); UVMHIST_CALLED(pdhist);
    446 
    447 	misc = SCARG(uap, misc);
    448 
    449 	/*
    450 	 * ensure serialized syscall access by grabbing the swap_syscall_lock
    451 	 */
    452 	rw_enter(&swap_syscall_lock, RW_WRITER);
    453 
    454 	userpath = malloc(SWAP_PATH_MAX, M_TEMP, M_WAITOK);
    455 	/*
    456 	 * we handle the non-priv NSWAP and STATS request first.
    457 	 *
    458 	 * SWAP_NSWAP: return number of config'd swap devices
    459 	 * [can also be obtained with uvmexp sysctl]
    460 	 */
    461 	if (SCARG(uap, cmd) == SWAP_NSWAP) {
    462 		UVMHIST_LOG(pdhist, "<- done SWAP_NSWAP=%d", uvmexp.nswapdev,
    463 		    0, 0, 0);
    464 		*retval = uvmexp.nswapdev;
    465 		error = 0;
    466 		goto out;
    467 	}
    468 
    469 	/*
    470 	 * SWAP_STATS: get stats on current # of configured swap devs
    471 	 *
    472 	 * note that the swap_priority list can't change as long
    473 	 * as we are holding the swap_syscall_lock.  we don't want
    474 	 * to grab the uvm_swap_data_lock because we may fault&sleep during
    475 	 * copyout() and we don't want to be holding that lock then!
    476 	 */
    477 	if (SCARG(uap, cmd) == SWAP_STATS
    478 #if defined(COMPAT_13)
    479 	    || SCARG(uap, cmd) == SWAP_OSTATS
    480 #endif
    481 	    ) {
    482 		if ((size_t)misc > (size_t)uvmexp.nswapdev)
    483 			misc = uvmexp.nswapdev;
    484 #if defined(COMPAT_13)
    485 		if (SCARG(uap, cmd) == SWAP_OSTATS)
    486 			len = sizeof(struct oswapent) * misc;
    487 		else
    488 #endif
    489 			len = sizeof(struct swapent) * misc;
    490 		sep = (struct swapent *)malloc(len, M_TEMP, M_WAITOK);
    491 
    492 		uvm_swap_stats_locked(SCARG(uap, cmd), sep, misc, retval);
    493 		error = copyout(sep, SCARG(uap, arg), len);
    494 
    495 		free(sep, M_TEMP);
    496 		UVMHIST_LOG(pdhist, "<- done SWAP_STATS", 0, 0, 0, 0);
    497 		goto out;
    498 	}
    499 	if (SCARG(uap, cmd) == SWAP_GETDUMPDEV) {
    500 		dev_t	*devp = (dev_t *)SCARG(uap, arg);
    501 
    502 		error = copyout(&dumpdev, devp, sizeof(dumpdev));
    503 		goto out;
    504 	}
    505 
    506 	/*
    507 	 * all other requests require superuser privs.   verify.
    508 	 */
    509 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SWAPCTL,
    510 	    0, NULL, NULL, NULL)))
    511 		goto out;
    512 
    513 	if (SCARG(uap, cmd) == SWAP_DUMPOFF) {
    514 		/* drop the current dump device */
    515 		dumpdev = NODEV;
    516 		cpu_dumpconf();
    517 		goto out;
    518 	}
    519 
    520 	/*
    521 	 * at this point we expect a path name in arg.   we will
    522 	 * use namei() to gain a vnode reference (vref), and lock
    523 	 * the vnode (VOP_LOCK).
    524 	 *
    525 	 * XXX: a NULL arg means use the root vnode pointer (e.g. for
    526 	 * miniroot)
    527 	 */
    528 	if (SCARG(uap, arg) == NULL) {
    529 		vp = rootvp;		/* miniroot */
    530 		if (vget(vp, LK_EXCLUSIVE)) {
    531 			error = EBUSY;
    532 			goto out;
    533 		}
    534 		if (SCARG(uap, cmd) == SWAP_ON &&
    535 		    copystr("miniroot", userpath, SWAP_PATH_MAX, &len))
    536 			panic("swapctl: miniroot copy failed");
    537 	} else {
    538 		int	space;
    539 		char	*where;
    540 
    541 		if (SCARG(uap, cmd) == SWAP_ON) {
    542 			if ((error = copyinstr(SCARG(uap, arg), userpath,
    543 			    SWAP_PATH_MAX, &len)))
    544 				goto out;
    545 			space = UIO_SYSSPACE;
    546 			where = userpath;
    547 		} else {
    548 			space = UIO_USERSPACE;
    549 			where = (char *)SCARG(uap, arg);
    550 		}
    551 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, space, where, l);
    552 		if ((error = namei(&nd)))
    553 			goto out;
    554 		vp = nd.ni_vp;
    555 	}
    556 	/* note: "vp" is referenced and locked */
    557 
    558 	error = 0;		/* assume no error */
    559 	switch(SCARG(uap, cmd)) {
    560 
    561 	case SWAP_DUMPDEV:
    562 		if (vp->v_type != VBLK) {
    563 			error = ENOTBLK;
    564 			break;
    565 		}
    566 		if (bdevsw_lookup(vp->v_rdev))
    567 			dumpdev = vp->v_rdev;
    568 		else
    569 			dumpdev = NODEV;
    570 		cpu_dumpconf();
    571 		break;
    572 
    573 	case SWAP_CTL:
    574 		/*
    575 		 * get new priority, remove old entry (if any) and then
    576 		 * reinsert it in the correct place.  finally, prune out
    577 		 * any empty priority structures.
    578 		 */
    579 		priority = SCARG(uap, misc);
    580 		spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
    581 		mutex_enter(&uvm_swap_data_lock);
    582 		if ((sdp = swaplist_find(vp, true)) == NULL) {
    583 			error = ENOENT;
    584 		} else {
    585 			swaplist_insert(sdp, spp, priority);
    586 			swaplist_trim();
    587 		}
    588 		mutex_exit(&uvm_swap_data_lock);
    589 		if (error)
    590 			free(spp, M_VMSWAP);
    591 		break;
    592 
    593 	case SWAP_ON:
    594 
    595 		/*
    596 		 * check for duplicates.   if none found, then insert a
    597 		 * dummy entry on the list to prevent someone else from
    598 		 * trying to enable this device while we are working on
    599 		 * it.
    600 		 */
    601 
    602 		priority = SCARG(uap, misc);
    603 		sdp = malloc(sizeof *sdp, M_VMSWAP, M_WAITOK);
    604 		spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
    605 		memset(sdp, 0, sizeof(*sdp));
    606 		sdp->swd_flags = SWF_FAKE;
    607 		sdp->swd_vp = vp;
    608 		sdp->swd_dev = (vp->v_type == VBLK) ? vp->v_rdev : NODEV;
    609 		bufq_alloc(&sdp->swd_tab, "disksort", BUFQ_SORT_RAWBLOCK);
    610 		mutex_enter(&uvm_swap_data_lock);
    611 		if (swaplist_find(vp, false) != NULL) {
    612 			error = EBUSY;
    613 			mutex_exit(&uvm_swap_data_lock);
    614 			bufq_free(sdp->swd_tab);
    615 			free(sdp, M_VMSWAP);
    616 			free(spp, M_VMSWAP);
    617 			break;
    618 		}
    619 		swaplist_insert(sdp, spp, priority);
    620 		mutex_exit(&uvm_swap_data_lock);
    621 
    622 		sdp->swd_pathlen = len;
    623 		sdp->swd_path = malloc(sdp->swd_pathlen, M_VMSWAP, M_WAITOK);
    624 		if (copystr(userpath, sdp->swd_path, sdp->swd_pathlen, 0) != 0)
    625 			panic("swapctl: copystr");
    626 
    627 		/*
    628 		 * we've now got a FAKE placeholder in the swap list.
    629 		 * now attempt to enable swap on it.  if we fail, undo
    630 		 * what we've done and kill the fake entry we just inserted.
    631 		 * if swap_on is a success, it will clear the SWF_FAKE flag
    632 		 */
    633 
    634 		if ((error = swap_on(l, sdp)) != 0) {
    635 			mutex_enter(&uvm_swap_data_lock);
    636 			(void) swaplist_find(vp, true);  /* kill fake entry */
    637 			swaplist_trim();
    638 			mutex_exit(&uvm_swap_data_lock);
    639 			bufq_free(sdp->swd_tab);
    640 			free(sdp->swd_path, M_VMSWAP);
    641 			free(sdp, M_VMSWAP);
    642 			break;
    643 		}
    644 		break;
    645 
    646 	case SWAP_OFF:
    647 		mutex_enter(&uvm_swap_data_lock);
    648 		if ((sdp = swaplist_find(vp, false)) == NULL) {
    649 			mutex_exit(&uvm_swap_data_lock);
    650 			error = ENXIO;
    651 			break;
    652 		}
    653 
    654 		/*
    655 		 * If a device isn't in use or enabled, we
    656 		 * can't stop swapping from it (again).
    657 		 */
    658 		if ((sdp->swd_flags & (SWF_INUSE|SWF_ENABLE)) == 0) {
    659 			mutex_exit(&uvm_swap_data_lock);
    660 			error = EBUSY;
    661 			break;
    662 		}
    663 
    664 		/*
    665 		 * do the real work.
    666 		 */
    667 		error = swap_off(l, sdp);
    668 		break;
    669 
    670 	default:
    671 		error = EINVAL;
    672 	}
    673 
    674 	/*
    675 	 * done!  release the ref gained by namei() and unlock.
    676 	 */
    677 	vput(vp);
    678 
    679 out:
    680 	free(userpath, M_TEMP);
    681 	rw_exit(&swap_syscall_lock);
    682 
    683 	UVMHIST_LOG(pdhist, "<- done!  error=%d", error, 0, 0, 0);
    684 	return (error);
    685 }
    686 
    687 /*
    688  * swap_stats: implements swapctl(SWAP_STATS). The function is kept
    689  * away from sys_swapctl() in order to allow COMPAT_* swapctl()
    690  * emulation to use it directly without going through sys_swapctl().
    691  * The problem with using sys_swapctl() there is that it involves
    692  * copying the swapent array to the stackgap, and this array's size
    693  * is not known at build time. Hence it would not be possible to
    694  * ensure it would fit in the stackgap in any case.
    695  */
    696 void
    697 uvm_swap_stats(int cmd, struct swapent *sep, int sec, register_t *retval)
    698 {
    699 
    700 	rw_enter(&swap_syscall_lock, RW_READER);
    701 	uvm_swap_stats_locked(cmd, sep, sec, retval);
    702 	rw_exit(&swap_syscall_lock);
    703 }
    704 
    705 static void
    706 uvm_swap_stats_locked(int cmd, struct swapent *sep, int sec, register_t *retval)
    707 {
    708 	struct swappri *spp;
    709 	struct swapdev *sdp;
    710 	int count = 0;
    711 
    712 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    713 		for (sdp = CIRCLEQ_FIRST(&spp->spi_swapdev);
    714 		     sdp != (void *)&spp->spi_swapdev && sec-- > 0;
    715 		     sdp = CIRCLEQ_NEXT(sdp, swd_next)) {
    716 		  	/*
    717 			 * backwards compatibility for system call.
    718 			 * note that we use 'struct oswapent' as an
    719 			 * overlay into both 'struct swapdev' and
    720 			 * the userland 'struct swapent', as we
    721 			 * want to retain backwards compatibility
    722 			 * with NetBSD 1.3.
    723 			 */
    724 			sdp->swd_ose.ose_inuse =
    725 			    btodb((uint64_t)sdp->swd_npginuse <<
    726 			    PAGE_SHIFT);
    727 			(void)memcpy(sep, &sdp->swd_ose,
    728 			    sizeof(struct oswapent));
    729 
    730 			/* now copy out the path if necessary */
    731 #if !defined(COMPAT_13)
    732 			(void) cmd;
    733 #endif
    734 #if defined(COMPAT_13)
    735 			if (cmd == SWAP_STATS)
    736 #endif
    737 				(void)memcpy(&sep->se_path, sdp->swd_path,
    738 				    sdp->swd_pathlen);
    739 
    740 			count++;
    741 #if defined(COMPAT_13)
    742 			if (cmd == SWAP_OSTATS)
    743 				sep = (struct swapent *)
    744 				    ((struct oswapent *)sep + 1);
    745 			else
    746 #endif
    747 				sep++;
    748 		}
    749 	}
    750 
    751 	*retval = count;
    752 	return;
    753 }
    754 
    755 /*
    756  * swap_on: attempt to enable a swapdev for swapping.   note that the
    757  *	swapdev is already on the global list, but disabled (marked
    758  *	SWF_FAKE).
    759  *
    760  * => we avoid the start of the disk (to protect disk labels)
    761  * => we also avoid the miniroot, if we are swapping to root.
    762  * => caller should leave uvm_swap_data_lock unlocked, we may lock it
    763  *	if needed.
    764  */
    765 static int
    766 swap_on(struct lwp *l, struct swapdev *sdp)
    767 {
    768 	struct vnode *vp;
    769 	int error, npages, nblocks, size;
    770 	long addr;
    771 	u_long result;
    772 	struct vattr va;
    773 #ifdef NFS
    774 	extern int (**nfsv2_vnodeop_p)(void *);
    775 #endif /* NFS */
    776 	const struct bdevsw *bdev;
    777 	dev_t dev;
    778 	UVMHIST_FUNC("swap_on"); UVMHIST_CALLED(pdhist);
    779 
    780 	/*
    781 	 * we want to enable swapping on sdp.   the swd_vp contains
    782 	 * the vnode we want (locked and ref'd), and the swd_dev
    783 	 * contains the dev_t of the file, if it a block device.
    784 	 */
    785 
    786 	vp = sdp->swd_vp;
    787 	dev = sdp->swd_dev;
    788 
    789 	/*
    790 	 * open the swap file (mostly useful for block device files to
    791 	 * let device driver know what is up).
    792 	 *
    793 	 * we skip the open/close for root on swap because the root
    794 	 * has already been opened when root was mounted (mountroot).
    795 	 */
    796 	if (vp != rootvp) {
    797 		if ((error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred, l)))
    798 			return (error);
    799 	}
    800 
    801 	/* XXX this only works for block devices */
    802 	UVMHIST_LOG(pdhist, "  dev=%d, major(dev)=%d", dev, major(dev), 0,0);
    803 
    804 	/*
    805 	 * we now need to determine the size of the swap area.   for
    806 	 * block specials we can call the d_psize function.
    807 	 * for normal files, we must stat [get attrs].
    808 	 *
    809 	 * we put the result in nblks.
    810 	 * for normal files, we also want the filesystem block size
    811 	 * (which we get with statfs).
    812 	 */
    813 	switch (vp->v_type) {
    814 	case VBLK:
    815 		bdev = bdevsw_lookup(dev);
    816 		if (bdev == NULL || bdev->d_psize == NULL ||
    817 		    (nblocks = (*bdev->d_psize)(dev)) == -1) {
    818 			error = ENXIO;
    819 			goto bad;
    820 		}
    821 		break;
    822 
    823 	case VREG:
    824 		if ((error = VOP_GETATTR(vp, &va, l->l_cred, l)))
    825 			goto bad;
    826 		nblocks = (int)btodb(va.va_size);
    827 		if ((error =
    828 		     VFS_STATVFS(vp->v_mount, &vp->v_mount->mnt_stat, l)) != 0)
    829 			goto bad;
    830 
    831 		sdp->swd_bsize = vp->v_mount->mnt_stat.f_iosize;
    832 		/*
    833 		 * limit the max # of outstanding I/O requests we issue
    834 		 * at any one time.   take it easy on NFS servers.
    835 		 */
    836 #ifdef NFS
    837 		if (vp->v_op == nfsv2_vnodeop_p)
    838 			sdp->swd_maxactive = 2; /* XXX */
    839 		else
    840 #endif /* NFS */
    841 			sdp->swd_maxactive = 8; /* XXX */
    842 		break;
    843 
    844 	default:
    845 		error = ENXIO;
    846 		goto bad;
    847 	}
    848 
    849 	/*
    850 	 * save nblocks in a safe place and convert to pages.
    851 	 */
    852 
    853 	sdp->swd_ose.ose_nblks = nblocks;
    854 	npages = dbtob((uint64_t)nblocks) >> PAGE_SHIFT;
    855 
    856 	/*
    857 	 * for block special files, we want to make sure that leave
    858 	 * the disklabel and bootblocks alone, so we arrange to skip
    859 	 * over them (arbitrarily choosing to skip PAGE_SIZE bytes).
    860 	 * note that because of this the "size" can be less than the
    861 	 * actual number of blocks on the device.
    862 	 */
    863 	if (vp->v_type == VBLK) {
    864 		/* we use pages 1 to (size - 1) [inclusive] */
    865 		size = npages - 1;
    866 		addr = 1;
    867 	} else {
    868 		/* we use pages 0 to (size - 1) [inclusive] */
    869 		size = npages;
    870 		addr = 0;
    871 	}
    872 
    873 	/*
    874 	 * make sure we have enough blocks for a reasonable sized swap
    875 	 * area.   we want at least one page.
    876 	 */
    877 
    878 	if (size < 1) {
    879 		UVMHIST_LOG(pdhist, "  size <= 1!!", 0, 0, 0, 0);
    880 		error = EINVAL;
    881 		goto bad;
    882 	}
    883 
    884 	UVMHIST_LOG(pdhist, "  dev=%x: size=%d addr=%ld\n", dev, size, addr, 0);
    885 
    886 	/*
    887 	 * now we need to allocate an extent to manage this swap device
    888 	 */
    889 
    890 	sdp->swd_blist = blist_create(npages);
    891 	/* mark all expect the `saved' region free. */
    892 	blist_free(sdp->swd_blist, addr, size);
    893 
    894 	/*
    895 	 * if the vnode we are swapping to is the root vnode
    896 	 * (i.e. we are swapping to the miniroot) then we want
    897 	 * to make sure we don't overwrite it.   do a statfs to
    898 	 * find its size and skip over it.
    899 	 */
    900 	if (vp == rootvp) {
    901 		struct mount *mp;
    902 		struct statvfs *sp;
    903 		int rootblocks, rootpages;
    904 
    905 		mp = rootvnode->v_mount;
    906 		sp = &mp->mnt_stat;
    907 		rootblocks = sp->f_blocks * btodb(sp->f_frsize);
    908 		/*
    909 		 * XXX: sp->f_blocks isn't the total number of
    910 		 * blocks in the filesystem, it's the number of
    911 		 * data blocks.  so, our rootblocks almost
    912 		 * definitely underestimates the total size
    913 		 * of the filesystem - how badly depends on the
    914 		 * details of the filesystem type.  there isn't
    915 		 * an obvious way to deal with this cleanly
    916 		 * and perfectly, so for now we just pad our
    917 		 * rootblocks estimate with an extra 5 percent.
    918 		 */
    919 		rootblocks += (rootblocks >> 5) +
    920 			(rootblocks >> 6) +
    921 			(rootblocks >> 7);
    922 		rootpages = round_page(dbtob(rootblocks)) >> PAGE_SHIFT;
    923 		if (rootpages > size)
    924 			panic("swap_on: miniroot larger than swap?");
    925 
    926 		if (rootpages != blist_fill(sdp->swd_blist, addr, rootpages)) {
    927 			panic("swap_on: unable to preserve miniroot");
    928 		}
    929 
    930 		size -= rootpages;
    931 		printf("Preserved %d pages of miniroot ", rootpages);
    932 		printf("leaving %d pages of swap\n", size);
    933 	}
    934 
    935 	/*
    936 	 * add a ref to vp to reflect usage as a swap device.
    937 	 */
    938 	vref(vp);
    939 
    940 	/*
    941 	 * now add the new swapdev to the drum and enable.
    942 	 */
    943 	result = vmem_alloc(swapmap, npages, VM_BESTFIT | VM_SLEEP);
    944 	if (result == 0)
    945 		panic("swapdrum_add");
    946 	/*
    947 	 * If this is the first regular swap create the workqueue.
    948 	 * => Protected by swap_syscall_lock.
    949 	 */
    950 	if (vp->v_type != VBLK) {
    951 		if (sw_reg_count++ == 0) {
    952 			KASSERT(sw_reg_workqueue == NULL);
    953 			if (workqueue_create(&sw_reg_workqueue, "swapiod",
    954 			    sw_reg_iodone, NULL, PRIBIO, IPL_BIO, 0) != 0)
    955 				panic("swap_add: workqueue_create failed");
    956 		}
    957 	}
    958 
    959 	sdp->swd_drumoffset = (int)result;
    960 	sdp->swd_drumsize = npages;
    961 	sdp->swd_npages = size;
    962 	mutex_enter(&uvm_swap_data_lock);
    963 	sdp->swd_flags &= ~SWF_FAKE;	/* going live */
    964 	sdp->swd_flags |= (SWF_INUSE|SWF_ENABLE);
    965 	uvmexp.swpages += size;
    966 	uvmexp.swpgavail += size;
    967 	mutex_exit(&uvm_swap_data_lock);
    968 	return (0);
    969 
    970 	/*
    971 	 * failure: clean up and return error.
    972 	 */
    973 
    974 bad:
    975 	if (sdp->swd_blist) {
    976 		blist_destroy(sdp->swd_blist);
    977 	}
    978 	if (vp != rootvp) {
    979 		(void)VOP_CLOSE(vp, FREAD|FWRITE, l->l_cred, l);
    980 	}
    981 	return (error);
    982 }
    983 
    984 /*
    985  * swap_off: stop swapping on swapdev
    986  *
    987  * => swap data should be locked, we will unlock.
    988  */
    989 static int
    990 swap_off(struct lwp *l, struct swapdev *sdp)
    991 {
    992 	int npages = sdp->swd_npages;
    993 	int error = 0;
    994 
    995 	UVMHIST_FUNC("swap_off"); UVMHIST_CALLED(pdhist);
    996 	UVMHIST_LOG(pdhist, "  dev=%x, npages=%d", sdp->swd_dev,npages,0,0);
    997 
    998 	/* disable the swap area being removed */
    999 	sdp->swd_flags &= ~SWF_ENABLE;
   1000 	uvmexp.swpgavail -= npages;
   1001 	mutex_exit(&uvm_swap_data_lock);
   1002 
   1003 	/*
   1004 	 * the idea is to find all the pages that are paged out to this
   1005 	 * device, and page them all in.  in uvm, swap-backed pageable
   1006 	 * memory can take two forms: aobjs and anons.  call the
   1007 	 * swapoff hook for each subsystem to bring in pages.
   1008 	 */
   1009 
   1010 	if (uao_swap_off(sdp->swd_drumoffset,
   1011 			 sdp->swd_drumoffset + sdp->swd_drumsize) ||
   1012 	    amap_swap_off(sdp->swd_drumoffset,
   1013 			  sdp->swd_drumoffset + sdp->swd_drumsize)) {
   1014 		error = ENOMEM;
   1015 	} else if (sdp->swd_npginuse > sdp->swd_npgbad) {
   1016 		error = EBUSY;
   1017 	}
   1018 
   1019 	if (error) {
   1020 		mutex_enter(&uvm_swap_data_lock);
   1021 		sdp->swd_flags |= SWF_ENABLE;
   1022 		uvmexp.swpgavail += npages;
   1023 		mutex_exit(&uvm_swap_data_lock);
   1024 
   1025 		return error;
   1026 	}
   1027 
   1028 	/*
   1029 	 * If this is the last regular swap destroy the workqueue.
   1030 	 * => Protected by swap_syscall_lock.
   1031 	 */
   1032 	if (sdp->swd_vp->v_type != VBLK) {
   1033 		KASSERT(sw_reg_count > 0);
   1034 		KASSERT(sw_reg_workqueue != NULL);
   1035 		if (--sw_reg_count == 0) {
   1036 			workqueue_destroy(sw_reg_workqueue);
   1037 			sw_reg_workqueue = NULL;
   1038 		}
   1039 	}
   1040 
   1041 	/*
   1042 	 * done with the vnode.
   1043 	 * drop our ref on the vnode before calling VOP_CLOSE()
   1044 	 * so that spec_close() can tell if this is the last close.
   1045 	 */
   1046 	vrele(sdp->swd_vp);
   1047 	if (sdp->swd_vp != rootvp) {
   1048 		(void) VOP_CLOSE(sdp->swd_vp, FREAD|FWRITE, l->l_cred, l);
   1049 	}
   1050 
   1051 	mutex_enter(&uvm_swap_data_lock);
   1052 	uvmexp.swpages -= npages;
   1053 	uvmexp.swpginuse -= sdp->swd_npgbad;
   1054 
   1055 	if (swaplist_find(sdp->swd_vp, true) == NULL)
   1056 		panic("swap_off: swapdev not in list");
   1057 	swaplist_trim();
   1058 	mutex_exit(&uvm_swap_data_lock);
   1059 
   1060 	/*
   1061 	 * free all resources!
   1062 	 */
   1063 	vmem_free(swapmap, sdp->swd_drumoffset, sdp->swd_drumsize);
   1064 	blist_destroy(sdp->swd_blist);
   1065 	bufq_free(sdp->swd_tab);
   1066 	free(sdp, M_VMSWAP);
   1067 	return (0);
   1068 }
   1069 
   1070 /*
   1071  * /dev/drum interface and i/o functions
   1072  */
   1073 
   1074 /*
   1075  * swstrategy: perform I/O on the drum
   1076  *
   1077  * => we must map the i/o request from the drum to the correct swapdev.
   1078  */
   1079 static void
   1080 swstrategy(struct buf *bp)
   1081 {
   1082 	struct swapdev *sdp;
   1083 	struct vnode *vp;
   1084 	int pageno, bn;
   1085 	UVMHIST_FUNC("swstrategy"); UVMHIST_CALLED(pdhist);
   1086 
   1087 	/*
   1088 	 * convert block number to swapdev.   note that swapdev can't
   1089 	 * be yanked out from under us because we are holding resources
   1090 	 * in it (i.e. the blocks we are doing I/O on).
   1091 	 */
   1092 	pageno = dbtob((int64_t)bp->b_blkno) >> PAGE_SHIFT;
   1093 	mutex_enter(&uvm_swap_data_lock);
   1094 	sdp = swapdrum_getsdp(pageno);
   1095 	mutex_exit(&uvm_swap_data_lock);
   1096 	if (sdp == NULL) {
   1097 		bp->b_error = EINVAL;
   1098 		biodone(bp);
   1099 		UVMHIST_LOG(pdhist, "  failed to get swap device", 0, 0, 0, 0);
   1100 		return;
   1101 	}
   1102 
   1103 	/*
   1104 	 * convert drum page number to block number on this swapdev.
   1105 	 */
   1106 
   1107 	pageno -= sdp->swd_drumoffset;	/* page # on swapdev */
   1108 	bn = btodb((uint64_t)pageno << PAGE_SHIFT); /* convert to diskblock */
   1109 
   1110 	UVMHIST_LOG(pdhist, "  %s: mapoff=%x bn=%x bcount=%ld",
   1111 		((bp->b_flags & B_READ) == 0) ? "write" : "read",
   1112 		sdp->swd_drumoffset, bn, bp->b_bcount);
   1113 
   1114 	/*
   1115 	 * for block devices we finish up here.
   1116 	 * for regular files we have to do more work which we delegate
   1117 	 * to sw_reg_strategy().
   1118 	 */
   1119 
   1120 	switch (sdp->swd_vp->v_type) {
   1121 	default:
   1122 		panic("swstrategy: vnode type 0x%x", sdp->swd_vp->v_type);
   1123 
   1124 	case VBLK:
   1125 
   1126 		/*
   1127 		 * must convert "bp" from an I/O on /dev/drum to an I/O
   1128 		 * on the swapdev (sdp).
   1129 		 */
   1130 		bp->b_blkno = bn;		/* swapdev block number */
   1131 		vp = sdp->swd_vp;		/* swapdev vnode pointer */
   1132 		bp->b_dev = sdp->swd_dev;	/* swapdev dev_t */
   1133 
   1134 		/*
   1135 		 * if we are doing a write, we have to redirect the i/o on
   1136 		 * drum's v_numoutput counter to the swapdevs.
   1137 		 */
   1138 		if ((bp->b_flags & B_READ) == 0) {
   1139 			mutex_enter(&vp->v_interlock);
   1140 			vwakeup(bp);	/* kills one 'v_numoutput' on drum */
   1141 			vp->v_numoutput++;	/* put it on swapdev */
   1142 			mutex_exit(&vp->v_interlock);
   1143 		}
   1144 
   1145 		/*
   1146 		 * finally plug in swapdev vnode and start I/O
   1147 		 */
   1148 		bp->b_vp = vp;
   1149 		bp->b_objlock = &vp->v_interlock;
   1150 		VOP_STRATEGY(vp, bp);
   1151 		return;
   1152 
   1153 	case VREG:
   1154 		/*
   1155 		 * delegate to sw_reg_strategy function.
   1156 		 */
   1157 		sw_reg_strategy(sdp, bp, bn);
   1158 		return;
   1159 	}
   1160 	/* NOTREACHED */
   1161 }
   1162 
   1163 /*
   1164  * swread: the read function for the drum (just a call to physio)
   1165  */
   1166 /*ARGSUSED*/
   1167 static int
   1168 swread(dev_t dev, struct uio *uio, int ioflag)
   1169 {
   1170 	UVMHIST_FUNC("swread"); UVMHIST_CALLED(pdhist);
   1171 
   1172 	UVMHIST_LOG(pdhist, "  dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
   1173 	return (physio(swstrategy, NULL, dev, B_READ, minphys, uio));
   1174 }
   1175 
   1176 /*
   1177  * swwrite: the write function for the drum (just a call to physio)
   1178  */
   1179 /*ARGSUSED*/
   1180 static int
   1181 swwrite(dev_t dev, struct uio *uio, int ioflag)
   1182 {
   1183 	UVMHIST_FUNC("swwrite"); UVMHIST_CALLED(pdhist);
   1184 
   1185 	UVMHIST_LOG(pdhist, "  dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
   1186 	return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio));
   1187 }
   1188 
   1189 const struct bdevsw swap_bdevsw = {
   1190 	noopen, noclose, swstrategy, noioctl, nodump, nosize, D_OTHER,
   1191 };
   1192 
   1193 const struct cdevsw swap_cdevsw = {
   1194 	nullopen, nullclose, swread, swwrite, noioctl,
   1195 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
   1196 };
   1197 
   1198 /*
   1199  * sw_reg_strategy: handle swap i/o to regular files
   1200  */
   1201 static void
   1202 sw_reg_strategy(struct swapdev *sdp, struct buf *bp, int bn)
   1203 {
   1204 	struct vnode	*vp;
   1205 	struct vndxfer	*vnx;
   1206 	daddr_t		nbn;
   1207 	char 		*addr;
   1208 	off_t		byteoff;
   1209 	int		s, off, nra, error, sz, resid;
   1210 	UVMHIST_FUNC("sw_reg_strategy"); UVMHIST_CALLED(pdhist);
   1211 
   1212 	/*
   1213 	 * allocate a vndxfer head for this transfer and point it to
   1214 	 * our buffer.
   1215 	 */
   1216 	vnx = pool_get(&vndxfer_pool, PR_WAITOK);
   1217 	vnx->vx_flags = VX_BUSY;
   1218 	vnx->vx_error = 0;
   1219 	vnx->vx_pending = 0;
   1220 	vnx->vx_bp = bp;
   1221 	vnx->vx_sdp = sdp;
   1222 
   1223 	/*
   1224 	 * setup for main loop where we read filesystem blocks into
   1225 	 * our buffer.
   1226 	 */
   1227 	error = 0;
   1228 	bp->b_resid = bp->b_bcount;	/* nothing transfered yet! */
   1229 	addr = bp->b_data;		/* current position in buffer */
   1230 	byteoff = dbtob((uint64_t)bn);
   1231 
   1232 	for (resid = bp->b_resid; resid; resid -= sz) {
   1233 		struct vndbuf	*nbp;
   1234 
   1235 		/*
   1236 		 * translate byteoffset into block number.  return values:
   1237 		 *   vp = vnode of underlying device
   1238 		 *  nbn = new block number (on underlying vnode dev)
   1239 		 *  nra = num blocks we can read-ahead (excludes requested
   1240 		 *	block)
   1241 		 */
   1242 		nra = 0;
   1243 		error = VOP_BMAP(sdp->swd_vp, byteoff / sdp->swd_bsize,
   1244 				 	&vp, &nbn, &nra);
   1245 
   1246 		if (error == 0 && nbn == (daddr_t)-1) {
   1247 			/*
   1248 			 * this used to just set error, but that doesn't
   1249 			 * do the right thing.  Instead, it causes random
   1250 			 * memory errors.  The panic() should remain until
   1251 			 * this condition doesn't destabilize the system.
   1252 			 */
   1253 #if 1
   1254 			panic("sw_reg_strategy: swap to sparse file");
   1255 #else
   1256 			error = EIO;	/* failure */
   1257 #endif
   1258 		}
   1259 
   1260 		/*
   1261 		 * punt if there was an error or a hole in the file.
   1262 		 * we must wait for any i/o ops we have already started
   1263 		 * to finish before returning.
   1264 		 *
   1265 		 * XXX we could deal with holes here but it would be
   1266 		 * a hassle (in the write case).
   1267 		 */
   1268 		if (error) {
   1269 			s = splbio();
   1270 			vnx->vx_error = error;	/* pass error up */
   1271 			goto out;
   1272 		}
   1273 
   1274 		/*
   1275 		 * compute the size ("sz") of this transfer (in bytes).
   1276 		 */
   1277 		off = byteoff % sdp->swd_bsize;
   1278 		sz = (1 + nra) * sdp->swd_bsize - off;
   1279 		if (sz > resid)
   1280 			sz = resid;
   1281 
   1282 		UVMHIST_LOG(pdhist, "sw_reg_strategy: "
   1283 			    "vp %p/%p offset 0x%x/0x%x",
   1284 			    sdp->swd_vp, vp, byteoff, nbn);
   1285 
   1286 		/*
   1287 		 * now get a buf structure.   note that the vb_buf is
   1288 		 * at the front of the nbp structure so that you can
   1289 		 * cast pointers between the two structure easily.
   1290 		 */
   1291 		nbp = pool_get(&vndbuf_pool, PR_WAITOK);
   1292 		buf_init(&nbp->vb_buf);
   1293 		nbp->vb_buf.b_flags    = bp->b_flags;
   1294 		nbp->vb_buf.b_cflags   = bp->b_cflags;
   1295 		nbp->vb_buf.b_oflags   = bp->b_oflags;
   1296 		nbp->vb_buf.b_bcount   = sz;
   1297 		nbp->vb_buf.b_bufsize  = sz;
   1298 		nbp->vb_buf.b_error    = 0;
   1299 		nbp->vb_buf.b_data     = addr;
   1300 		nbp->vb_buf.b_lblkno   = 0;
   1301 		nbp->vb_buf.b_blkno    = nbn + btodb(off);
   1302 		nbp->vb_buf.b_rawblkno = nbp->vb_buf.b_blkno;
   1303 		nbp->vb_buf.b_iodone   = sw_reg_biodone;
   1304 		nbp->vb_buf.b_vp       = vp;
   1305 		nbp->vb_buf.b_objlock  = &vp->v_interlock;
   1306 		if (vp->v_type == VBLK) {
   1307 			nbp->vb_buf.b_dev = vp->v_rdev;
   1308 		}
   1309 
   1310 		nbp->vb_xfer = vnx;	/* patch it back in to vnx */
   1311 
   1312 		/*
   1313 		 * Just sort by block number
   1314 		 */
   1315 		s = splbio();
   1316 		if (vnx->vx_error != 0) {
   1317 			buf_destroy(&nbp->vb_buf);
   1318 			pool_put(&vndbuf_pool, nbp);
   1319 			goto out;
   1320 		}
   1321 		vnx->vx_pending++;
   1322 
   1323 		/* sort it in and start I/O if we are not over our limit */
   1324 		BUFQ_PUT(sdp->swd_tab, &nbp->vb_buf);
   1325 		sw_reg_start(sdp);
   1326 		splx(s);
   1327 
   1328 		/*
   1329 		 * advance to the next I/O
   1330 		 */
   1331 		byteoff += sz;
   1332 		addr += sz;
   1333 	}
   1334 
   1335 	s = splbio();
   1336 
   1337 out: /* Arrive here at splbio */
   1338 	vnx->vx_flags &= ~VX_BUSY;
   1339 	if (vnx->vx_pending == 0) {
   1340 		error = vnx->vx_error;
   1341 		pool_put(&vndxfer_pool, vnx);
   1342 		bp->b_error = error;
   1343 		biodone(bp);
   1344 	}
   1345 	splx(s);
   1346 }
   1347 
   1348 /*
   1349  * sw_reg_start: start an I/O request on the requested swapdev
   1350  *
   1351  * => reqs are sorted by b_rawblkno (above)
   1352  */
   1353 static void
   1354 sw_reg_start(struct swapdev *sdp)
   1355 {
   1356 	struct buf	*bp;
   1357 	struct vnode	*vp;
   1358 	UVMHIST_FUNC("sw_reg_start"); UVMHIST_CALLED(pdhist);
   1359 
   1360 	/* recursion control */
   1361 	if ((sdp->swd_flags & SWF_BUSY) != 0)
   1362 		return;
   1363 
   1364 	sdp->swd_flags |= SWF_BUSY;
   1365 
   1366 	while (sdp->swd_active < sdp->swd_maxactive) {
   1367 		bp = BUFQ_GET(sdp->swd_tab);
   1368 		if (bp == NULL)
   1369 			break;
   1370 		sdp->swd_active++;
   1371 
   1372 		UVMHIST_LOG(pdhist,
   1373 		    "sw_reg_start:  bp %p vp %p blkno %p cnt %lx",
   1374 		    bp, bp->b_vp, bp->b_blkno, bp->b_bcount);
   1375 		vp = bp->b_vp;
   1376 		KASSERT(bp->b_objlock == &vp->v_interlock);
   1377 		if ((bp->b_flags & B_READ) == 0) {
   1378 			mutex_enter(&vp->v_interlock);
   1379 			vp->v_numoutput++;
   1380 			mutex_exit(&vp->v_interlock);
   1381 		}
   1382 		VOP_STRATEGY(vp, bp);
   1383 	}
   1384 	sdp->swd_flags &= ~SWF_BUSY;
   1385 }
   1386 
   1387 /*
   1388  * sw_reg_biodone: one of our i/o's has completed
   1389  */
   1390 static void
   1391 sw_reg_biodone(struct buf *bp)
   1392 {
   1393 	workqueue_enqueue(sw_reg_workqueue, &bp->b_work, NULL);
   1394 }
   1395 
   1396 /*
   1397  * sw_reg_iodone: one of our i/o's has completed and needs post-i/o cleanup
   1398  *
   1399  * => note that we can recover the vndbuf struct by casting the buf ptr
   1400  */
   1401 static void
   1402 sw_reg_iodone(struct work *wk, void *dummy)
   1403 {
   1404 	struct vndbuf *vbp = (void *)wk;
   1405 	struct vndxfer *vnx = vbp->vb_xfer;
   1406 	struct buf *pbp = vnx->vx_bp;		/* parent buffer */
   1407 	struct swapdev	*sdp = vnx->vx_sdp;
   1408 	int s, resid, error;
   1409 	KASSERT(&vbp->vb_buf.b_work == wk);
   1410 	UVMHIST_FUNC("sw_reg_iodone"); UVMHIST_CALLED(pdhist);
   1411 
   1412 	UVMHIST_LOG(pdhist, "  vbp=%p vp=%p blkno=%x addr=%p",
   1413 	    vbp, vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno, vbp->vb_buf.b_data);
   1414 	UVMHIST_LOG(pdhist, "  cnt=%lx resid=%lx",
   1415 	    vbp->vb_buf.b_bcount, vbp->vb_buf.b_resid, 0, 0);
   1416 
   1417 	/*
   1418 	 * protect vbp at splbio and update.
   1419 	 */
   1420 
   1421 	s = splbio();
   1422 	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
   1423 	pbp->b_resid -= resid;
   1424 	vnx->vx_pending--;
   1425 
   1426 	if (vbp->vb_buf.b_error != 0) {
   1427 		/* pass error upward */
   1428 		error = vbp->vb_buf.b_error ? vbp->vb_buf.b_error : EIO;
   1429 		UVMHIST_LOG(pdhist, "  got error=%d !", error, 0, 0, 0);
   1430 		vnx->vx_error = error;
   1431 	}
   1432 
   1433 	/*
   1434 	 * kill vbp structure
   1435 	 */
   1436 	buf_destroy(&vbp->vb_buf);
   1437 	pool_put(&vndbuf_pool, vbp);
   1438 
   1439 	/*
   1440 	 * wrap up this transaction if it has run to completion or, in
   1441 	 * case of an error, when all auxiliary buffers have returned.
   1442 	 */
   1443 	if (vnx->vx_error != 0) {
   1444 		/* pass error upward */
   1445 		error = vnx->vx_error;
   1446 		if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
   1447 			pbp->b_error = error;
   1448 			biodone(pbp);
   1449 			pool_put(&vndxfer_pool, vnx);
   1450 		}
   1451 	} else if (pbp->b_resid == 0) {
   1452 		KASSERT(vnx->vx_pending == 0);
   1453 		if ((vnx->vx_flags & VX_BUSY) == 0) {
   1454 			UVMHIST_LOG(pdhist, "  iodone error=%d !",
   1455 			    pbp, vnx->vx_error, 0, 0);
   1456 			biodone(pbp);
   1457 			pool_put(&vndxfer_pool, vnx);
   1458 		}
   1459 	}
   1460 
   1461 	/*
   1462 	 * done!   start next swapdev I/O if one is pending
   1463 	 */
   1464 	sdp->swd_active--;
   1465 	sw_reg_start(sdp);
   1466 	splx(s);
   1467 }
   1468 
   1469 
   1470 /*
   1471  * uvm_swap_alloc: allocate space on swap
   1472  *
   1473  * => allocation is done "round robin" down the priority list, as we
   1474  *	allocate in a priority we "rotate" the circle queue.
   1475  * => space can be freed with uvm_swap_free
   1476  * => we return the page slot number in /dev/drum (0 == invalid slot)
   1477  * => we lock uvm_swap_data_lock
   1478  * => XXXMRG: "LESSOK" INTERFACE NEEDED TO EXTENT SYSTEM
   1479  */
   1480 int
   1481 uvm_swap_alloc(int *nslots /* IN/OUT */, bool lessok)
   1482 {
   1483 	struct swapdev *sdp;
   1484 	struct swappri *spp;
   1485 	UVMHIST_FUNC("uvm_swap_alloc"); UVMHIST_CALLED(pdhist);
   1486 
   1487 	/*
   1488 	 * no swap devices configured yet?   definite failure.
   1489 	 */
   1490 	if (uvmexp.nswapdev < 1)
   1491 		return 0;
   1492 
   1493 	/*
   1494 	 * lock data lock, convert slots into blocks, and enter loop
   1495 	 */
   1496 	mutex_enter(&uvm_swap_data_lock);
   1497 
   1498 ReTry:	/* XXXMRG */
   1499 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
   1500 		CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
   1501 			uint64_t result;
   1502 
   1503 			/* if it's not enabled, then we can't swap from it */
   1504 			if ((sdp->swd_flags & SWF_ENABLE) == 0)
   1505 				continue;
   1506 			if (sdp->swd_npginuse + *nslots > sdp->swd_npages)
   1507 				continue;
   1508 			result = blist_alloc(sdp->swd_blist, *nslots);
   1509 			if (result == BLIST_NONE) {
   1510 				continue;
   1511 			}
   1512 			KASSERT(result < sdp->swd_drumsize);
   1513 
   1514 			/*
   1515 			 * successful allocation!  now rotate the circleq.
   1516 			 */
   1517 			CIRCLEQ_REMOVE(&spp->spi_swapdev, sdp, swd_next);
   1518 			CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
   1519 			sdp->swd_npginuse += *nslots;
   1520 			uvmexp.swpginuse += *nslots;
   1521 			mutex_exit(&uvm_swap_data_lock);
   1522 			/* done!  return drum slot number */
   1523 			UVMHIST_LOG(pdhist,
   1524 			    "success!  returning %d slots starting at %d",
   1525 			    *nslots, result + sdp->swd_drumoffset, 0, 0);
   1526 			return (result + sdp->swd_drumoffset);
   1527 		}
   1528 	}
   1529 
   1530 	/* XXXMRG: BEGIN HACK */
   1531 	if (*nslots > 1 && lessok) {
   1532 		*nslots = 1;
   1533 		/* XXXMRG: ugh!  blist should support this for us */
   1534 		goto ReTry;
   1535 	}
   1536 	/* XXXMRG: END HACK */
   1537 
   1538 	mutex_exit(&uvm_swap_data_lock);
   1539 	return 0;
   1540 }
   1541 
   1542 bool
   1543 uvm_swapisfull(void)
   1544 {
   1545 	bool rv;
   1546 
   1547 	mutex_enter(&uvm_swap_data_lock);
   1548 	KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
   1549 	rv = (uvmexp.swpgonly >= uvmexp.swpgavail);
   1550 	mutex_exit(&uvm_swap_data_lock);
   1551 
   1552 	return (rv);
   1553 }
   1554 
   1555 /*
   1556  * uvm_swap_markbad: keep track of swap ranges where we've had i/o errors
   1557  *
   1558  * => we lock uvm_swap_data_lock
   1559  */
   1560 void
   1561 uvm_swap_markbad(int startslot, int nslots)
   1562 {
   1563 	struct swapdev *sdp;
   1564 	UVMHIST_FUNC("uvm_swap_markbad"); UVMHIST_CALLED(pdhist);
   1565 
   1566 	mutex_enter(&uvm_swap_data_lock);
   1567 	sdp = swapdrum_getsdp(startslot);
   1568 	KASSERT(sdp != NULL);
   1569 
   1570 	/*
   1571 	 * we just keep track of how many pages have been marked bad
   1572 	 * in this device, to make everything add up in swap_off().
   1573 	 * we assume here that the range of slots will all be within
   1574 	 * one swap device.
   1575 	 */
   1576 
   1577 	KASSERT(uvmexp.swpgonly >= nslots);
   1578 	uvmexp.swpgonly -= nslots;
   1579 	sdp->swd_npgbad += nslots;
   1580 	UVMHIST_LOG(pdhist, "now %d bad", sdp->swd_npgbad, 0,0,0);
   1581 	mutex_exit(&uvm_swap_data_lock);
   1582 }
   1583 
   1584 /*
   1585  * uvm_swap_free: free swap slots
   1586  *
   1587  * => this can be all or part of an allocation made by uvm_swap_alloc
   1588  * => we lock uvm_swap_data_lock
   1589  */
   1590 void
   1591 uvm_swap_free(int startslot, int nslots)
   1592 {
   1593 	struct swapdev *sdp;
   1594 	UVMHIST_FUNC("uvm_swap_free"); UVMHIST_CALLED(pdhist);
   1595 
   1596 	UVMHIST_LOG(pdhist, "freeing %d slots starting at %d", nslots,
   1597 	    startslot, 0, 0);
   1598 
   1599 	/*
   1600 	 * ignore attempts to free the "bad" slot.
   1601 	 */
   1602 
   1603 	if (startslot == SWSLOT_BAD) {
   1604 		return;
   1605 	}
   1606 
   1607 	/*
   1608 	 * convert drum slot offset back to sdp, free the blocks
   1609 	 * in the extent, and return.   must hold pri lock to do
   1610 	 * lookup and access the extent.
   1611 	 */
   1612 
   1613 	mutex_enter(&uvm_swap_data_lock);
   1614 	sdp = swapdrum_getsdp(startslot);
   1615 	KASSERT(uvmexp.nswapdev >= 1);
   1616 	KASSERT(sdp != NULL);
   1617 	KASSERT(sdp->swd_npginuse >= nslots);
   1618 	blist_free(sdp->swd_blist, startslot - sdp->swd_drumoffset, nslots);
   1619 	sdp->swd_npginuse -= nslots;
   1620 	uvmexp.swpginuse -= nslots;
   1621 	mutex_exit(&uvm_swap_data_lock);
   1622 }
   1623 
   1624 /*
   1625  * uvm_swap_put: put any number of pages into a contig place on swap
   1626  *
   1627  * => can be sync or async
   1628  */
   1629 
   1630 int
   1631 uvm_swap_put(int swslot, struct vm_page **ppsp, int npages, int flags)
   1632 {
   1633 	int error;
   1634 
   1635 	error = uvm_swap_io(ppsp, swslot, npages, B_WRITE |
   1636 	    ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
   1637 	return error;
   1638 }
   1639 
   1640 /*
   1641  * uvm_swap_get: get a single page from swap
   1642  *
   1643  * => usually a sync op (from fault)
   1644  */
   1645 
   1646 int
   1647 uvm_swap_get(struct vm_page *page, int swslot, int flags)
   1648 {
   1649 	int error;
   1650 
   1651 	uvmexp.nswget++;
   1652 	KASSERT(flags & PGO_SYNCIO);
   1653 	if (swslot == SWSLOT_BAD) {
   1654 		return EIO;
   1655 	}
   1656 
   1657 	error = uvm_swap_io(&page, swslot, 1, B_READ |
   1658 	    ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
   1659 	if (error == 0) {
   1660 
   1661 		/*
   1662 		 * this page is no longer only in swap.
   1663 		 */
   1664 
   1665 		mutex_enter(&uvm_swap_data_lock);
   1666 		KASSERT(uvmexp.swpgonly > 0);
   1667 		uvmexp.swpgonly--;
   1668 		mutex_exit(&uvm_swap_data_lock);
   1669 	}
   1670 	return error;
   1671 }
   1672 
   1673 /*
   1674  * uvm_swap_io: do an i/o operation to swap
   1675  */
   1676 
   1677 static int
   1678 uvm_swap_io(struct vm_page **pps, int startslot, int npages, int flags)
   1679 {
   1680 	daddr_t startblk;
   1681 	struct	buf *bp;
   1682 	vaddr_t kva;
   1683 	int	error, mapinflags;
   1684 	bool write, async;
   1685 	UVMHIST_FUNC("uvm_swap_io"); UVMHIST_CALLED(pdhist);
   1686 
   1687 	UVMHIST_LOG(pdhist, "<- called, startslot=%d, npages=%d, flags=%d",
   1688 	    startslot, npages, flags, 0);
   1689 
   1690 	write = (flags & B_READ) == 0;
   1691 	async = (flags & B_ASYNC) != 0;
   1692 
   1693 	/*
   1694 	 * convert starting drum slot to block number
   1695 	 */
   1696 
   1697 	startblk = btodb((uint64_t)startslot << PAGE_SHIFT);
   1698 
   1699 	/*
   1700 	 * first, map the pages into the kernel.
   1701 	 */
   1702 
   1703 	mapinflags = !write ?
   1704 		UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_READ :
   1705 		UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_WRITE;
   1706 	kva = uvm_pagermapin(pps, npages, mapinflags);
   1707 
   1708 	/*
   1709 	 * now allocate a buf for the i/o.
   1710 	 */
   1711 
   1712 	bp = getiobuf(swapdev_vp, true);
   1713 
   1714 	/*
   1715 	 * fill in the bp/sbp.   we currently route our i/o through
   1716 	 * /dev/drum's vnode [swapdev_vp].
   1717 	 */
   1718 
   1719 	bp->b_cflags = BC_BUSY | BC_NOCACHE;
   1720 	bp->b_flags = (flags & (B_READ|B_ASYNC));
   1721 	bp->b_proc = &proc0;	/* XXX */
   1722 	bp->b_vnbufs.le_next = NOLIST;
   1723 	bp->b_data = (void *)kva;
   1724 	bp->b_blkno = startblk;
   1725 	bp->b_bufsize = bp->b_bcount = npages << PAGE_SHIFT;
   1726 
   1727 	/*
   1728 	 * bump v_numoutput (counter of number of active outputs).
   1729 	 */
   1730 
   1731 	if (write) {
   1732 		mutex_enter(&swapdev_vp->v_interlock);
   1733 		swapdev_vp->v_numoutput++;
   1734 		mutex_exit(&swapdev_vp->v_interlock);
   1735 	}
   1736 
   1737 	/*
   1738 	 * for async ops we must set up the iodone handler.
   1739 	 */
   1740 
   1741 	if (async) {
   1742 		bp->b_iodone = uvm_aio_biodone;
   1743 		UVMHIST_LOG(pdhist, "doing async!", 0, 0, 0, 0);
   1744 		if (curlwp == uvm.pagedaemon_lwp)
   1745 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1746 		else
   1747 			BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
   1748 	} else {
   1749 		bp->b_iodone = NULL;
   1750 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1751 	}
   1752 	UVMHIST_LOG(pdhist,
   1753 	    "about to start io: data = %p blkno = 0x%x, bcount = %ld",
   1754 	    bp->b_data, bp->b_blkno, bp->b_bcount, 0);
   1755 
   1756 	/*
   1757 	 * now we start the I/O, and if async, return.
   1758 	 */
   1759 
   1760 	VOP_STRATEGY(swapdev_vp, bp);
   1761 	if (async)
   1762 		return 0;
   1763 
   1764 	/*
   1765 	 * must be sync i/o.   wait for it to finish
   1766 	 */
   1767 
   1768 	error = biowait(bp);
   1769 
   1770 	/*
   1771 	 * kill the pager mapping
   1772 	 */
   1773 
   1774 	uvm_pagermapout(kva, npages);
   1775 
   1776 	/*
   1777 	 * now dispose of the buf and we're done.
   1778 	 */
   1779 
   1780 	if (write) {
   1781 		mutex_enter(&swapdev_vp->v_interlock);
   1782 		vwakeup(bp);
   1783 		mutex_exit(&swapdev_vp->v_interlock);
   1784 	}
   1785 	putiobuf(bp);
   1786 	UVMHIST_LOG(pdhist, "<- done (sync)  error=%d", error, 0, 0, 0);
   1787 
   1788 	return (error);
   1789 }
   1790