Home | History | Annotate | Line # | Download | only in uvm
uvm_swap.c revision 1.134
      1 /*	$NetBSD: uvm_swap.c,v 1.134 2008/01/02 11:49:21 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.134 2008/01/02 11:49:21 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, const struct sys_swapctl_args *uap, register_t *retval)
    429 {
    430 	/* {
    431 		syscallarg(int) cmd;
    432 		syscallarg(void *) arg;
    433 		syscallarg(int) misc;
    434 	} */
    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,
    552 		    space, where);
    553 		if ((error = namei(&nd)))
    554 			goto out;
    555 		vp = nd.ni_vp;
    556 	}
    557 	/* note: "vp" is referenced and locked */
    558 
    559 	error = 0;		/* assume no error */
    560 	switch(SCARG(uap, cmd)) {
    561 
    562 	case SWAP_DUMPDEV:
    563 		if (vp->v_type != VBLK) {
    564 			error = ENOTBLK;
    565 			break;
    566 		}
    567 		if (bdevsw_lookup(vp->v_rdev))
    568 			dumpdev = vp->v_rdev;
    569 		else
    570 			dumpdev = NODEV;
    571 		cpu_dumpconf();
    572 		break;
    573 
    574 	case SWAP_CTL:
    575 		/*
    576 		 * get new priority, remove old entry (if any) and then
    577 		 * reinsert it in the correct place.  finally, prune out
    578 		 * any empty priority structures.
    579 		 */
    580 		priority = SCARG(uap, misc);
    581 		spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
    582 		mutex_enter(&uvm_swap_data_lock);
    583 		if ((sdp = swaplist_find(vp, true)) == NULL) {
    584 			error = ENOENT;
    585 		} else {
    586 			swaplist_insert(sdp, spp, priority);
    587 			swaplist_trim();
    588 		}
    589 		mutex_exit(&uvm_swap_data_lock);
    590 		if (error)
    591 			free(spp, M_VMSWAP);
    592 		break;
    593 
    594 	case SWAP_ON:
    595 
    596 		/*
    597 		 * check for duplicates.   if none found, then insert a
    598 		 * dummy entry on the list to prevent someone else from
    599 		 * trying to enable this device while we are working on
    600 		 * it.
    601 		 */
    602 
    603 		priority = SCARG(uap, misc);
    604 		sdp = malloc(sizeof *sdp, M_VMSWAP, M_WAITOK);
    605 		spp = malloc(sizeof *spp, M_VMSWAP, M_WAITOK);
    606 		memset(sdp, 0, sizeof(*sdp));
    607 		sdp->swd_flags = SWF_FAKE;
    608 		sdp->swd_vp = vp;
    609 		sdp->swd_dev = (vp->v_type == VBLK) ? vp->v_rdev : NODEV;
    610 		bufq_alloc(&sdp->swd_tab, "disksort", BUFQ_SORT_RAWBLOCK);
    611 		mutex_enter(&uvm_swap_data_lock);
    612 		if (swaplist_find(vp, false) != NULL) {
    613 			error = EBUSY;
    614 			mutex_exit(&uvm_swap_data_lock);
    615 			bufq_free(sdp->swd_tab);
    616 			free(sdp, M_VMSWAP);
    617 			free(spp, M_VMSWAP);
    618 			break;
    619 		}
    620 		swaplist_insert(sdp, spp, priority);
    621 		mutex_exit(&uvm_swap_data_lock);
    622 
    623 		sdp->swd_pathlen = len;
    624 		sdp->swd_path = malloc(sdp->swd_pathlen, M_VMSWAP, M_WAITOK);
    625 		if (copystr(userpath, sdp->swd_path, sdp->swd_pathlen, 0) != 0)
    626 			panic("swapctl: copystr");
    627 
    628 		/*
    629 		 * we've now got a FAKE placeholder in the swap list.
    630 		 * now attempt to enable swap on it.  if we fail, undo
    631 		 * what we've done and kill the fake entry we just inserted.
    632 		 * if swap_on is a success, it will clear the SWF_FAKE flag
    633 		 */
    634 
    635 		if ((error = swap_on(l, sdp)) != 0) {
    636 			mutex_enter(&uvm_swap_data_lock);
    637 			(void) swaplist_find(vp, true);  /* kill fake entry */
    638 			swaplist_trim();
    639 			mutex_exit(&uvm_swap_data_lock);
    640 			bufq_free(sdp->swd_tab);
    641 			free(sdp->swd_path, M_VMSWAP);
    642 			free(sdp, M_VMSWAP);
    643 			break;
    644 		}
    645 		break;
    646 
    647 	case SWAP_OFF:
    648 		mutex_enter(&uvm_swap_data_lock);
    649 		if ((sdp = swaplist_find(vp, false)) == NULL) {
    650 			mutex_exit(&uvm_swap_data_lock);
    651 			error = ENXIO;
    652 			break;
    653 		}
    654 
    655 		/*
    656 		 * If a device isn't in use or enabled, we
    657 		 * can't stop swapping from it (again).
    658 		 */
    659 		if ((sdp->swd_flags & (SWF_INUSE|SWF_ENABLE)) == 0) {
    660 			mutex_exit(&uvm_swap_data_lock);
    661 			error = EBUSY;
    662 			break;
    663 		}
    664 
    665 		/*
    666 		 * do the real work.
    667 		 */
    668 		error = swap_off(l, sdp);
    669 		break;
    670 
    671 	default:
    672 		error = EINVAL;
    673 	}
    674 
    675 	/*
    676 	 * done!  release the ref gained by namei() and unlock.
    677 	 */
    678 	vput(vp);
    679 
    680 out:
    681 	free(userpath, M_TEMP);
    682 	rw_exit(&swap_syscall_lock);
    683 
    684 	UVMHIST_LOG(pdhist, "<- done!  error=%d", error, 0, 0, 0);
    685 	return (error);
    686 }
    687 
    688 /*
    689  * swap_stats: implements swapctl(SWAP_STATS). The function is kept
    690  * away from sys_swapctl() in order to allow COMPAT_* swapctl()
    691  * emulation to use it directly without going through sys_swapctl().
    692  * The problem with using sys_swapctl() there is that it involves
    693  * copying the swapent array to the stackgap, and this array's size
    694  * is not known at build time. Hence it would not be possible to
    695  * ensure it would fit in the stackgap in any case.
    696  */
    697 void
    698 uvm_swap_stats(int cmd, struct swapent *sep, int sec, register_t *retval)
    699 {
    700 
    701 	rw_enter(&swap_syscall_lock, RW_READER);
    702 	uvm_swap_stats_locked(cmd, sep, sec, retval);
    703 	rw_exit(&swap_syscall_lock);
    704 }
    705 
    706 static void
    707 uvm_swap_stats_locked(int cmd, struct swapent *sep, int sec, register_t *retval)
    708 {
    709 	struct swappri *spp;
    710 	struct swapdev *sdp;
    711 	int count = 0;
    712 
    713 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
    714 		for (sdp = CIRCLEQ_FIRST(&spp->spi_swapdev);
    715 		     sdp != (void *)&spp->spi_swapdev && sec-- > 0;
    716 		     sdp = CIRCLEQ_NEXT(sdp, swd_next)) {
    717 		  	/*
    718 			 * backwards compatibility for system call.
    719 			 * note that we use 'struct oswapent' as an
    720 			 * overlay into both 'struct swapdev' and
    721 			 * the userland 'struct swapent', as we
    722 			 * want to retain backwards compatibility
    723 			 * with NetBSD 1.3.
    724 			 */
    725 			sdp->swd_ose.ose_inuse =
    726 			    btodb((uint64_t)sdp->swd_npginuse <<
    727 			    PAGE_SHIFT);
    728 			(void)memcpy(sep, &sdp->swd_ose,
    729 			    sizeof(struct oswapent));
    730 
    731 			/* now copy out the path if necessary */
    732 #if !defined(COMPAT_13)
    733 			(void) cmd;
    734 #endif
    735 #if defined(COMPAT_13)
    736 			if (cmd == SWAP_STATS)
    737 #endif
    738 				(void)memcpy(&sep->se_path, sdp->swd_path,
    739 				    sdp->swd_pathlen);
    740 
    741 			count++;
    742 #if defined(COMPAT_13)
    743 			if (cmd == SWAP_OSTATS)
    744 				sep = (struct swapent *)
    745 				    ((struct oswapent *)sep + 1);
    746 			else
    747 #endif
    748 				sep++;
    749 		}
    750 	}
    751 
    752 	*retval = count;
    753 	return;
    754 }
    755 
    756 /*
    757  * swap_on: attempt to enable a swapdev for swapping.   note that the
    758  *	swapdev is already on the global list, but disabled (marked
    759  *	SWF_FAKE).
    760  *
    761  * => we avoid the start of the disk (to protect disk labels)
    762  * => we also avoid the miniroot, if we are swapping to root.
    763  * => caller should leave uvm_swap_data_lock unlocked, we may lock it
    764  *	if needed.
    765  */
    766 static int
    767 swap_on(struct lwp *l, struct swapdev *sdp)
    768 {
    769 	struct vnode *vp;
    770 	int error, npages, nblocks, size;
    771 	long addr;
    772 	u_long result;
    773 	struct vattr va;
    774 #ifdef NFS
    775 	extern int (**nfsv2_vnodeop_p)(void *);
    776 #endif /* NFS */
    777 	const struct bdevsw *bdev;
    778 	dev_t dev;
    779 	UVMHIST_FUNC("swap_on"); UVMHIST_CALLED(pdhist);
    780 
    781 	/*
    782 	 * we want to enable swapping on sdp.   the swd_vp contains
    783 	 * the vnode we want (locked and ref'd), and the swd_dev
    784 	 * contains the dev_t of the file, if it a block device.
    785 	 */
    786 
    787 	vp = sdp->swd_vp;
    788 	dev = sdp->swd_dev;
    789 
    790 	/*
    791 	 * open the swap file (mostly useful for block device files to
    792 	 * let device driver know what is up).
    793 	 *
    794 	 * we skip the open/close for root on swap because the root
    795 	 * has already been opened when root was mounted (mountroot).
    796 	 */
    797 	if (vp != rootvp) {
    798 		if ((error = VOP_OPEN(vp, FREAD|FWRITE, l->l_cred)))
    799 			return (error);
    800 	}
    801 
    802 	/* XXX this only works for block devices */
    803 	UVMHIST_LOG(pdhist, "  dev=%d, major(dev)=%d", dev, major(dev), 0,0);
    804 
    805 	/*
    806 	 * we now need to determine the size of the swap area.   for
    807 	 * block specials we can call the d_psize function.
    808 	 * for normal files, we must stat [get attrs].
    809 	 *
    810 	 * we put the result in nblks.
    811 	 * for normal files, we also want the filesystem block size
    812 	 * (which we get with statfs).
    813 	 */
    814 	switch (vp->v_type) {
    815 	case VBLK:
    816 		bdev = bdevsw_lookup(dev);
    817 		if (bdev == NULL || bdev->d_psize == NULL ||
    818 		    (nblocks = (*bdev->d_psize)(dev)) == -1) {
    819 			error = ENXIO;
    820 			goto bad;
    821 		}
    822 		break;
    823 
    824 	case VREG:
    825 		if ((error = VOP_GETATTR(vp, &va, l->l_cred)))
    826 			goto bad;
    827 		nblocks = (int)btodb(va.va_size);
    828 		if ((error =
    829 		     VFS_STATVFS(vp->v_mount, &vp->v_mount->mnt_stat)) != 0)
    830 			goto bad;
    831 
    832 		sdp->swd_bsize = vp->v_mount->mnt_stat.f_iosize;
    833 		/*
    834 		 * limit the max # of outstanding I/O requests we issue
    835 		 * at any one time.   take it easy on NFS servers.
    836 		 */
    837 #ifdef NFS
    838 		if (vp->v_op == nfsv2_vnodeop_p)
    839 			sdp->swd_maxactive = 2; /* XXX */
    840 		else
    841 #endif /* NFS */
    842 			sdp->swd_maxactive = 8; /* XXX */
    843 		break;
    844 
    845 	default:
    846 		error = ENXIO;
    847 		goto bad;
    848 	}
    849 
    850 	/*
    851 	 * save nblocks in a safe place and convert to pages.
    852 	 */
    853 
    854 	sdp->swd_ose.ose_nblks = nblocks;
    855 	npages = dbtob((uint64_t)nblocks) >> PAGE_SHIFT;
    856 
    857 	/*
    858 	 * for block special files, we want to make sure that leave
    859 	 * the disklabel and bootblocks alone, so we arrange to skip
    860 	 * over them (arbitrarily choosing to skip PAGE_SIZE bytes).
    861 	 * note that because of this the "size" can be less than the
    862 	 * actual number of blocks on the device.
    863 	 */
    864 	if (vp->v_type == VBLK) {
    865 		/* we use pages 1 to (size - 1) [inclusive] */
    866 		size = npages - 1;
    867 		addr = 1;
    868 	} else {
    869 		/* we use pages 0 to (size - 1) [inclusive] */
    870 		size = npages;
    871 		addr = 0;
    872 	}
    873 
    874 	/*
    875 	 * make sure we have enough blocks for a reasonable sized swap
    876 	 * area.   we want at least one page.
    877 	 */
    878 
    879 	if (size < 1) {
    880 		UVMHIST_LOG(pdhist, "  size <= 1!!", 0, 0, 0, 0);
    881 		error = EINVAL;
    882 		goto bad;
    883 	}
    884 
    885 	UVMHIST_LOG(pdhist, "  dev=%x: size=%d addr=%ld\n", dev, size, addr, 0);
    886 
    887 	/*
    888 	 * now we need to allocate an extent to manage this swap device
    889 	 */
    890 
    891 	sdp->swd_blist = blist_create(npages);
    892 	/* mark all expect the `saved' region free. */
    893 	blist_free(sdp->swd_blist, addr, size);
    894 
    895 	/*
    896 	 * if the vnode we are swapping to is the root vnode
    897 	 * (i.e. we are swapping to the miniroot) then we want
    898 	 * to make sure we don't overwrite it.   do a statfs to
    899 	 * find its size and skip over it.
    900 	 */
    901 	if (vp == rootvp) {
    902 		struct mount *mp;
    903 		struct statvfs *sp;
    904 		int rootblocks, rootpages;
    905 
    906 		mp = rootvnode->v_mount;
    907 		sp = &mp->mnt_stat;
    908 		rootblocks = sp->f_blocks * btodb(sp->f_frsize);
    909 		/*
    910 		 * XXX: sp->f_blocks isn't the total number of
    911 		 * blocks in the filesystem, it's the number of
    912 		 * data blocks.  so, our rootblocks almost
    913 		 * definitely underestimates the total size
    914 		 * of the filesystem - how badly depends on the
    915 		 * details of the filesystem type.  there isn't
    916 		 * an obvious way to deal with this cleanly
    917 		 * and perfectly, so for now we just pad our
    918 		 * rootblocks estimate with an extra 5 percent.
    919 		 */
    920 		rootblocks += (rootblocks >> 5) +
    921 			(rootblocks >> 6) +
    922 			(rootblocks >> 7);
    923 		rootpages = round_page(dbtob(rootblocks)) >> PAGE_SHIFT;
    924 		if (rootpages > size)
    925 			panic("swap_on: miniroot larger than swap?");
    926 
    927 		if (rootpages != blist_fill(sdp->swd_blist, addr, rootpages)) {
    928 			panic("swap_on: unable to preserve miniroot");
    929 		}
    930 
    931 		size -= rootpages;
    932 		printf("Preserved %d pages of miniroot ", rootpages);
    933 		printf("leaving %d pages of swap\n", size);
    934 	}
    935 
    936 	/*
    937 	 * add a ref to vp to reflect usage as a swap device.
    938 	 */
    939 	vref(vp);
    940 
    941 	/*
    942 	 * now add the new swapdev to the drum and enable.
    943 	 */
    944 	result = vmem_alloc(swapmap, npages, VM_BESTFIT | VM_SLEEP);
    945 	if (result == 0)
    946 		panic("swapdrum_add");
    947 	/*
    948 	 * If this is the first regular swap create the workqueue.
    949 	 * => Protected by swap_syscall_lock.
    950 	 */
    951 	if (vp->v_type != VBLK) {
    952 		if (sw_reg_count++ == 0) {
    953 			KASSERT(sw_reg_workqueue == NULL);
    954 			if (workqueue_create(&sw_reg_workqueue, "swapiod",
    955 			    sw_reg_iodone, NULL, PRIBIO, IPL_BIO, 0) != 0)
    956 				panic("swap_add: workqueue_create failed");
    957 		}
    958 	}
    959 
    960 	sdp->swd_drumoffset = (int)result;
    961 	sdp->swd_drumsize = npages;
    962 	sdp->swd_npages = size;
    963 	mutex_enter(&uvm_swap_data_lock);
    964 	sdp->swd_flags &= ~SWF_FAKE;	/* going live */
    965 	sdp->swd_flags |= (SWF_INUSE|SWF_ENABLE);
    966 	uvmexp.swpages += size;
    967 	uvmexp.swpgavail += size;
    968 	mutex_exit(&uvm_swap_data_lock);
    969 	return (0);
    970 
    971 	/*
    972 	 * failure: clean up and return error.
    973 	 */
    974 
    975 bad:
    976 	if (sdp->swd_blist) {
    977 		blist_destroy(sdp->swd_blist);
    978 	}
    979 	if (vp != rootvp) {
    980 		(void)VOP_CLOSE(vp, FREAD|FWRITE, l->l_cred);
    981 	}
    982 	return (error);
    983 }
    984 
    985 /*
    986  * swap_off: stop swapping on swapdev
    987  *
    988  * => swap data should be locked, we will unlock.
    989  */
    990 static int
    991 swap_off(struct lwp *l, struct swapdev *sdp)
    992 {
    993 	int npages = sdp->swd_npages;
    994 	int error = 0;
    995 
    996 	UVMHIST_FUNC("swap_off"); UVMHIST_CALLED(pdhist);
    997 	UVMHIST_LOG(pdhist, "  dev=%x, npages=%d", sdp->swd_dev,npages,0,0);
    998 
    999 	/* disable the swap area being removed */
   1000 	sdp->swd_flags &= ~SWF_ENABLE;
   1001 	uvmexp.swpgavail -= npages;
   1002 	mutex_exit(&uvm_swap_data_lock);
   1003 
   1004 	/*
   1005 	 * the idea is to find all the pages that are paged out to this
   1006 	 * device, and page them all in.  in uvm, swap-backed pageable
   1007 	 * memory can take two forms: aobjs and anons.  call the
   1008 	 * swapoff hook for each subsystem to bring in pages.
   1009 	 */
   1010 
   1011 	if (uao_swap_off(sdp->swd_drumoffset,
   1012 			 sdp->swd_drumoffset + sdp->swd_drumsize) ||
   1013 	    amap_swap_off(sdp->swd_drumoffset,
   1014 			  sdp->swd_drumoffset + sdp->swd_drumsize)) {
   1015 		error = ENOMEM;
   1016 	} else if (sdp->swd_npginuse > sdp->swd_npgbad) {
   1017 		error = EBUSY;
   1018 	}
   1019 
   1020 	if (error) {
   1021 		mutex_enter(&uvm_swap_data_lock);
   1022 		sdp->swd_flags |= SWF_ENABLE;
   1023 		uvmexp.swpgavail += npages;
   1024 		mutex_exit(&uvm_swap_data_lock);
   1025 
   1026 		return error;
   1027 	}
   1028 
   1029 	/*
   1030 	 * If this is the last regular swap destroy the workqueue.
   1031 	 * => Protected by swap_syscall_lock.
   1032 	 */
   1033 	if (sdp->swd_vp->v_type != VBLK) {
   1034 		KASSERT(sw_reg_count > 0);
   1035 		KASSERT(sw_reg_workqueue != NULL);
   1036 		if (--sw_reg_count == 0) {
   1037 			workqueue_destroy(sw_reg_workqueue);
   1038 			sw_reg_workqueue = NULL;
   1039 		}
   1040 	}
   1041 
   1042 	/*
   1043 	 * done with the vnode.
   1044 	 * drop our ref on the vnode before calling VOP_CLOSE()
   1045 	 * so that spec_close() can tell if this is the last close.
   1046 	 */
   1047 	vrele(sdp->swd_vp);
   1048 	if (sdp->swd_vp != rootvp) {
   1049 		(void) VOP_CLOSE(sdp->swd_vp, FREAD|FWRITE, l->l_cred);
   1050 	}
   1051 
   1052 	mutex_enter(&uvm_swap_data_lock);
   1053 	uvmexp.swpages -= npages;
   1054 	uvmexp.swpginuse -= sdp->swd_npgbad;
   1055 
   1056 	if (swaplist_find(sdp->swd_vp, true) == NULL)
   1057 		panic("swap_off: swapdev not in list");
   1058 	swaplist_trim();
   1059 	mutex_exit(&uvm_swap_data_lock);
   1060 
   1061 	/*
   1062 	 * free all resources!
   1063 	 */
   1064 	vmem_free(swapmap, sdp->swd_drumoffset, sdp->swd_drumsize);
   1065 	blist_destroy(sdp->swd_blist);
   1066 	bufq_free(sdp->swd_tab);
   1067 	free(sdp, M_VMSWAP);
   1068 	return (0);
   1069 }
   1070 
   1071 /*
   1072  * /dev/drum interface and i/o functions
   1073  */
   1074 
   1075 /*
   1076  * swstrategy: perform I/O on the drum
   1077  *
   1078  * => we must map the i/o request from the drum to the correct swapdev.
   1079  */
   1080 static void
   1081 swstrategy(struct buf *bp)
   1082 {
   1083 	struct swapdev *sdp;
   1084 	struct vnode *vp;
   1085 	int pageno, bn;
   1086 	UVMHIST_FUNC("swstrategy"); UVMHIST_CALLED(pdhist);
   1087 
   1088 	/*
   1089 	 * convert block number to swapdev.   note that swapdev can't
   1090 	 * be yanked out from under us because we are holding resources
   1091 	 * in it (i.e. the blocks we are doing I/O on).
   1092 	 */
   1093 	pageno = dbtob((int64_t)bp->b_blkno) >> PAGE_SHIFT;
   1094 	mutex_enter(&uvm_swap_data_lock);
   1095 	sdp = swapdrum_getsdp(pageno);
   1096 	mutex_exit(&uvm_swap_data_lock);
   1097 	if (sdp == NULL) {
   1098 		bp->b_error = EINVAL;
   1099 		biodone(bp);
   1100 		UVMHIST_LOG(pdhist, "  failed to get swap device", 0, 0, 0, 0);
   1101 		return;
   1102 	}
   1103 
   1104 	/*
   1105 	 * convert drum page number to block number on this swapdev.
   1106 	 */
   1107 
   1108 	pageno -= sdp->swd_drumoffset;	/* page # on swapdev */
   1109 	bn = btodb((uint64_t)pageno << PAGE_SHIFT); /* convert to diskblock */
   1110 
   1111 	UVMHIST_LOG(pdhist, "  %s: mapoff=%x bn=%x bcount=%ld",
   1112 		((bp->b_flags & B_READ) == 0) ? "write" : "read",
   1113 		sdp->swd_drumoffset, bn, bp->b_bcount);
   1114 
   1115 	/*
   1116 	 * for block devices we finish up here.
   1117 	 * for regular files we have to do more work which we delegate
   1118 	 * to sw_reg_strategy().
   1119 	 */
   1120 
   1121 	vp = sdp->swd_vp;		/* swapdev vnode pointer */
   1122 	switch (vp->v_type) {
   1123 	default:
   1124 		panic("swstrategy: vnode type 0x%x", vp->v_type);
   1125 
   1126 	case VBLK:
   1127 
   1128 		/*
   1129 		 * must convert "bp" from an I/O on /dev/drum to an I/O
   1130 		 * on the swapdev (sdp).
   1131 		 */
   1132 		bp->b_blkno = bn;		/* swapdev block number */
   1133 		bp->b_dev = sdp->swd_dev;	/* swapdev dev_t */
   1134 
   1135 		/*
   1136 		 * if we are doing a write, we have to redirect the i/o on
   1137 		 * drum's v_numoutput counter to the swapdevs.
   1138 		 */
   1139 		if ((bp->b_flags & B_READ) == 0) {
   1140 			mutex_enter(bp->b_objlock);
   1141 			vwakeup(bp);	/* kills one 'v_numoutput' on drum */
   1142 			mutex_exit(bp->b_objlock);
   1143 			mutex_enter(&vp->v_interlock);
   1144 			vp->v_numoutput++;	/* put it on swapdev */
   1145 			mutex_exit(&vp->v_interlock);
   1146 		}
   1147 
   1148 		/*
   1149 		 * finally plug in swapdev vnode and start I/O
   1150 		 */
   1151 		bp->b_vp = vp;
   1152 		bp->b_objlock = &vp->v_interlock;
   1153 		VOP_STRATEGY(vp, bp);
   1154 		return;
   1155 
   1156 	case VREG:
   1157 		/*
   1158 		 * delegate to sw_reg_strategy function.
   1159 		 */
   1160 		sw_reg_strategy(sdp, bp, bn);
   1161 		return;
   1162 	}
   1163 	/* NOTREACHED */
   1164 }
   1165 
   1166 /*
   1167  * swread: the read function for the drum (just a call to physio)
   1168  */
   1169 /*ARGSUSED*/
   1170 static int
   1171 swread(dev_t dev, struct uio *uio, int ioflag)
   1172 {
   1173 	UVMHIST_FUNC("swread"); UVMHIST_CALLED(pdhist);
   1174 
   1175 	UVMHIST_LOG(pdhist, "  dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
   1176 	return (physio(swstrategy, NULL, dev, B_READ, minphys, uio));
   1177 }
   1178 
   1179 /*
   1180  * swwrite: the write function for the drum (just a call to physio)
   1181  */
   1182 /*ARGSUSED*/
   1183 static int
   1184 swwrite(dev_t dev, struct uio *uio, int ioflag)
   1185 {
   1186 	UVMHIST_FUNC("swwrite"); UVMHIST_CALLED(pdhist);
   1187 
   1188 	UVMHIST_LOG(pdhist, "  dev=%x offset=%qx", dev, uio->uio_offset, 0, 0);
   1189 	return (physio(swstrategy, NULL, dev, B_WRITE, minphys, uio));
   1190 }
   1191 
   1192 const struct bdevsw swap_bdevsw = {
   1193 	noopen, noclose, swstrategy, noioctl, nodump, nosize, D_OTHER,
   1194 };
   1195 
   1196 const struct cdevsw swap_cdevsw = {
   1197 	nullopen, nullclose, swread, swwrite, noioctl,
   1198 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
   1199 };
   1200 
   1201 /*
   1202  * sw_reg_strategy: handle swap i/o to regular files
   1203  */
   1204 static void
   1205 sw_reg_strategy(struct swapdev *sdp, struct buf *bp, int bn)
   1206 {
   1207 	struct vnode	*vp;
   1208 	struct vndxfer	*vnx;
   1209 	daddr_t		nbn;
   1210 	char 		*addr;
   1211 	off_t		byteoff;
   1212 	int		s, off, nra, error, sz, resid;
   1213 	UVMHIST_FUNC("sw_reg_strategy"); UVMHIST_CALLED(pdhist);
   1214 
   1215 	/*
   1216 	 * allocate a vndxfer head for this transfer and point it to
   1217 	 * our buffer.
   1218 	 */
   1219 	vnx = pool_get(&vndxfer_pool, PR_WAITOK);
   1220 	vnx->vx_flags = VX_BUSY;
   1221 	vnx->vx_error = 0;
   1222 	vnx->vx_pending = 0;
   1223 	vnx->vx_bp = bp;
   1224 	vnx->vx_sdp = sdp;
   1225 
   1226 	/*
   1227 	 * setup for main loop where we read filesystem blocks into
   1228 	 * our buffer.
   1229 	 */
   1230 	error = 0;
   1231 	bp->b_resid = bp->b_bcount;	/* nothing transfered yet! */
   1232 	addr = bp->b_data;		/* current position in buffer */
   1233 	byteoff = dbtob((uint64_t)bn);
   1234 
   1235 	for (resid = bp->b_resid; resid; resid -= sz) {
   1236 		struct vndbuf	*nbp;
   1237 
   1238 		/*
   1239 		 * translate byteoffset into block number.  return values:
   1240 		 *   vp = vnode of underlying device
   1241 		 *  nbn = new block number (on underlying vnode dev)
   1242 		 *  nra = num blocks we can read-ahead (excludes requested
   1243 		 *	block)
   1244 		 */
   1245 		nra = 0;
   1246 		error = VOP_BMAP(sdp->swd_vp, byteoff / sdp->swd_bsize,
   1247 				 	&vp, &nbn, &nra);
   1248 
   1249 		if (error == 0 && nbn == (daddr_t)-1) {
   1250 			/*
   1251 			 * this used to just set error, but that doesn't
   1252 			 * do the right thing.  Instead, it causes random
   1253 			 * memory errors.  The panic() should remain until
   1254 			 * this condition doesn't destabilize the system.
   1255 			 */
   1256 #if 1
   1257 			panic("sw_reg_strategy: swap to sparse file");
   1258 #else
   1259 			error = EIO;	/* failure */
   1260 #endif
   1261 		}
   1262 
   1263 		/*
   1264 		 * punt if there was an error or a hole in the file.
   1265 		 * we must wait for any i/o ops we have already started
   1266 		 * to finish before returning.
   1267 		 *
   1268 		 * XXX we could deal with holes here but it would be
   1269 		 * a hassle (in the write case).
   1270 		 */
   1271 		if (error) {
   1272 			s = splbio();
   1273 			vnx->vx_error = error;	/* pass error up */
   1274 			goto out;
   1275 		}
   1276 
   1277 		/*
   1278 		 * compute the size ("sz") of this transfer (in bytes).
   1279 		 */
   1280 		off = byteoff % sdp->swd_bsize;
   1281 		sz = (1 + nra) * sdp->swd_bsize - off;
   1282 		if (sz > resid)
   1283 			sz = resid;
   1284 
   1285 		UVMHIST_LOG(pdhist, "sw_reg_strategy: "
   1286 			    "vp %p/%p offset 0x%x/0x%x",
   1287 			    sdp->swd_vp, vp, byteoff, nbn);
   1288 
   1289 		/*
   1290 		 * now get a buf structure.   note that the vb_buf is
   1291 		 * at the front of the nbp structure so that you can
   1292 		 * cast pointers between the two structure easily.
   1293 		 */
   1294 		nbp = pool_get(&vndbuf_pool, PR_WAITOK);
   1295 		buf_init(&nbp->vb_buf);
   1296 		nbp->vb_buf.b_flags    = bp->b_flags;
   1297 		nbp->vb_buf.b_cflags   = bp->b_cflags;
   1298 		nbp->vb_buf.b_oflags   = bp->b_oflags;
   1299 		nbp->vb_buf.b_bcount   = sz;
   1300 		nbp->vb_buf.b_bufsize  = sz;
   1301 		nbp->vb_buf.b_error    = 0;
   1302 		nbp->vb_buf.b_data     = addr;
   1303 		nbp->vb_buf.b_lblkno   = 0;
   1304 		nbp->vb_buf.b_blkno    = nbn + btodb(off);
   1305 		nbp->vb_buf.b_rawblkno = nbp->vb_buf.b_blkno;
   1306 		nbp->vb_buf.b_iodone   = sw_reg_biodone;
   1307 		nbp->vb_buf.b_vp       = vp;
   1308 		nbp->vb_buf.b_objlock  = &vp->v_interlock;
   1309 		if (vp->v_type == VBLK) {
   1310 			nbp->vb_buf.b_dev = vp->v_rdev;
   1311 		}
   1312 
   1313 		nbp->vb_xfer = vnx;	/* patch it back in to vnx */
   1314 
   1315 		/*
   1316 		 * Just sort by block number
   1317 		 */
   1318 		s = splbio();
   1319 		if (vnx->vx_error != 0) {
   1320 			buf_destroy(&nbp->vb_buf);
   1321 			pool_put(&vndbuf_pool, nbp);
   1322 			goto out;
   1323 		}
   1324 		vnx->vx_pending++;
   1325 
   1326 		/* sort it in and start I/O if we are not over our limit */
   1327 		/* XXXAD locking */
   1328 		BUFQ_PUT(sdp->swd_tab, &nbp->vb_buf);
   1329 		sw_reg_start(sdp);
   1330 		splx(s);
   1331 
   1332 		/*
   1333 		 * advance to the next I/O
   1334 		 */
   1335 		byteoff += sz;
   1336 		addr += sz;
   1337 	}
   1338 
   1339 	s = splbio();
   1340 
   1341 out: /* Arrive here at splbio */
   1342 	vnx->vx_flags &= ~VX_BUSY;
   1343 	if (vnx->vx_pending == 0) {
   1344 		error = vnx->vx_error;
   1345 		pool_put(&vndxfer_pool, vnx);
   1346 		bp->b_error = error;
   1347 		biodone(bp);
   1348 	}
   1349 	splx(s);
   1350 }
   1351 
   1352 /*
   1353  * sw_reg_start: start an I/O request on the requested swapdev
   1354  *
   1355  * => reqs are sorted by b_rawblkno (above)
   1356  */
   1357 static void
   1358 sw_reg_start(struct swapdev *sdp)
   1359 {
   1360 	struct buf	*bp;
   1361 	struct vnode	*vp;
   1362 	UVMHIST_FUNC("sw_reg_start"); UVMHIST_CALLED(pdhist);
   1363 
   1364 	/* recursion control */
   1365 	if ((sdp->swd_flags & SWF_BUSY) != 0)
   1366 		return;
   1367 
   1368 	sdp->swd_flags |= SWF_BUSY;
   1369 
   1370 	while (sdp->swd_active < sdp->swd_maxactive) {
   1371 		bp = BUFQ_GET(sdp->swd_tab);
   1372 		if (bp == NULL)
   1373 			break;
   1374 		sdp->swd_active++;
   1375 
   1376 		UVMHIST_LOG(pdhist,
   1377 		    "sw_reg_start:  bp %p vp %p blkno %p cnt %lx",
   1378 		    bp, bp->b_vp, bp->b_blkno, bp->b_bcount);
   1379 		vp = bp->b_vp;
   1380 		KASSERT(bp->b_objlock == &vp->v_interlock);
   1381 		if ((bp->b_flags & B_READ) == 0) {
   1382 			mutex_enter(&vp->v_interlock);
   1383 			vp->v_numoutput++;
   1384 			mutex_exit(&vp->v_interlock);
   1385 		}
   1386 		VOP_STRATEGY(vp, bp);
   1387 	}
   1388 	sdp->swd_flags &= ~SWF_BUSY;
   1389 }
   1390 
   1391 /*
   1392  * sw_reg_biodone: one of our i/o's has completed
   1393  */
   1394 static void
   1395 sw_reg_biodone(struct buf *bp)
   1396 {
   1397 	workqueue_enqueue(sw_reg_workqueue, &bp->b_work, NULL);
   1398 }
   1399 
   1400 /*
   1401  * sw_reg_iodone: one of our i/o's has completed and needs post-i/o cleanup
   1402  *
   1403  * => note that we can recover the vndbuf struct by casting the buf ptr
   1404  */
   1405 static void
   1406 sw_reg_iodone(struct work *wk, void *dummy)
   1407 {
   1408 	struct vndbuf *vbp = (void *)wk;
   1409 	struct vndxfer *vnx = vbp->vb_xfer;
   1410 	struct buf *pbp = vnx->vx_bp;		/* parent buffer */
   1411 	struct swapdev	*sdp = vnx->vx_sdp;
   1412 	int s, resid, error;
   1413 	KASSERT(&vbp->vb_buf.b_work == wk);
   1414 	UVMHIST_FUNC("sw_reg_iodone"); UVMHIST_CALLED(pdhist);
   1415 
   1416 	UVMHIST_LOG(pdhist, "  vbp=%p vp=%p blkno=%x addr=%p",
   1417 	    vbp, vbp->vb_buf.b_vp, vbp->vb_buf.b_blkno, vbp->vb_buf.b_data);
   1418 	UVMHIST_LOG(pdhist, "  cnt=%lx resid=%lx",
   1419 	    vbp->vb_buf.b_bcount, vbp->vb_buf.b_resid, 0, 0);
   1420 
   1421 	/*
   1422 	 * protect vbp at splbio and update.
   1423 	 */
   1424 
   1425 	s = splbio();
   1426 	resid = vbp->vb_buf.b_bcount - vbp->vb_buf.b_resid;
   1427 	pbp->b_resid -= resid;
   1428 	vnx->vx_pending--;
   1429 
   1430 	if (vbp->vb_buf.b_error != 0) {
   1431 		/* pass error upward */
   1432 		error = vbp->vb_buf.b_error ? vbp->vb_buf.b_error : EIO;
   1433 		UVMHIST_LOG(pdhist, "  got error=%d !", error, 0, 0, 0);
   1434 		vnx->vx_error = error;
   1435 	}
   1436 
   1437 	/*
   1438 	 * kill vbp structure
   1439 	 */
   1440 	buf_destroy(&vbp->vb_buf);
   1441 	pool_put(&vndbuf_pool, vbp);
   1442 
   1443 	/*
   1444 	 * wrap up this transaction if it has run to completion or, in
   1445 	 * case of an error, when all auxiliary buffers have returned.
   1446 	 */
   1447 	if (vnx->vx_error != 0) {
   1448 		/* pass error upward */
   1449 		error = vnx->vx_error;
   1450 		if ((vnx->vx_flags & VX_BUSY) == 0 && vnx->vx_pending == 0) {
   1451 			pbp->b_error = error;
   1452 			biodone(pbp);
   1453 			pool_put(&vndxfer_pool, vnx);
   1454 		}
   1455 	} else if (pbp->b_resid == 0) {
   1456 		KASSERT(vnx->vx_pending == 0);
   1457 		if ((vnx->vx_flags & VX_BUSY) == 0) {
   1458 			UVMHIST_LOG(pdhist, "  iodone error=%d !",
   1459 			    pbp, vnx->vx_error, 0, 0);
   1460 			biodone(pbp);
   1461 			pool_put(&vndxfer_pool, vnx);
   1462 		}
   1463 	}
   1464 
   1465 	/*
   1466 	 * done!   start next swapdev I/O if one is pending
   1467 	 */
   1468 	sdp->swd_active--;
   1469 	sw_reg_start(sdp);
   1470 	splx(s);
   1471 }
   1472 
   1473 
   1474 /*
   1475  * uvm_swap_alloc: allocate space on swap
   1476  *
   1477  * => allocation is done "round robin" down the priority list, as we
   1478  *	allocate in a priority we "rotate" the circle queue.
   1479  * => space can be freed with uvm_swap_free
   1480  * => we return the page slot number in /dev/drum (0 == invalid slot)
   1481  * => we lock uvm_swap_data_lock
   1482  * => XXXMRG: "LESSOK" INTERFACE NEEDED TO EXTENT SYSTEM
   1483  */
   1484 int
   1485 uvm_swap_alloc(int *nslots /* IN/OUT */, bool lessok)
   1486 {
   1487 	struct swapdev *sdp;
   1488 	struct swappri *spp;
   1489 	UVMHIST_FUNC("uvm_swap_alloc"); UVMHIST_CALLED(pdhist);
   1490 
   1491 	/*
   1492 	 * no swap devices configured yet?   definite failure.
   1493 	 */
   1494 	if (uvmexp.nswapdev < 1)
   1495 		return 0;
   1496 
   1497 	/*
   1498 	 * lock data lock, convert slots into blocks, and enter loop
   1499 	 */
   1500 	mutex_enter(&uvm_swap_data_lock);
   1501 
   1502 ReTry:	/* XXXMRG */
   1503 	LIST_FOREACH(spp, &swap_priority, spi_swappri) {
   1504 		CIRCLEQ_FOREACH(sdp, &spp->spi_swapdev, swd_next) {
   1505 			uint64_t result;
   1506 
   1507 			/* if it's not enabled, then we can't swap from it */
   1508 			if ((sdp->swd_flags & SWF_ENABLE) == 0)
   1509 				continue;
   1510 			if (sdp->swd_npginuse + *nslots > sdp->swd_npages)
   1511 				continue;
   1512 			result = blist_alloc(sdp->swd_blist, *nslots);
   1513 			if (result == BLIST_NONE) {
   1514 				continue;
   1515 			}
   1516 			KASSERT(result < sdp->swd_drumsize);
   1517 
   1518 			/*
   1519 			 * successful allocation!  now rotate the circleq.
   1520 			 */
   1521 			CIRCLEQ_REMOVE(&spp->spi_swapdev, sdp, swd_next);
   1522 			CIRCLEQ_INSERT_TAIL(&spp->spi_swapdev, sdp, swd_next);
   1523 			sdp->swd_npginuse += *nslots;
   1524 			uvmexp.swpginuse += *nslots;
   1525 			mutex_exit(&uvm_swap_data_lock);
   1526 			/* done!  return drum slot number */
   1527 			UVMHIST_LOG(pdhist,
   1528 			    "success!  returning %d slots starting at %d",
   1529 			    *nslots, result + sdp->swd_drumoffset, 0, 0);
   1530 			return (result + sdp->swd_drumoffset);
   1531 		}
   1532 	}
   1533 
   1534 	/* XXXMRG: BEGIN HACK */
   1535 	if (*nslots > 1 && lessok) {
   1536 		*nslots = 1;
   1537 		/* XXXMRG: ugh!  blist should support this for us */
   1538 		goto ReTry;
   1539 	}
   1540 	/* XXXMRG: END HACK */
   1541 
   1542 	mutex_exit(&uvm_swap_data_lock);
   1543 	return 0;
   1544 }
   1545 
   1546 bool
   1547 uvm_swapisfull(void)
   1548 {
   1549 	bool rv;
   1550 
   1551 	mutex_enter(&uvm_swap_data_lock);
   1552 	KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
   1553 	rv = (uvmexp.swpgonly >= uvmexp.swpgavail);
   1554 	mutex_exit(&uvm_swap_data_lock);
   1555 
   1556 	return (rv);
   1557 }
   1558 
   1559 /*
   1560  * uvm_swap_markbad: keep track of swap ranges where we've had i/o errors
   1561  *
   1562  * => we lock uvm_swap_data_lock
   1563  */
   1564 void
   1565 uvm_swap_markbad(int startslot, int nslots)
   1566 {
   1567 	struct swapdev *sdp;
   1568 	UVMHIST_FUNC("uvm_swap_markbad"); UVMHIST_CALLED(pdhist);
   1569 
   1570 	mutex_enter(&uvm_swap_data_lock);
   1571 	sdp = swapdrum_getsdp(startslot);
   1572 	KASSERT(sdp != NULL);
   1573 
   1574 	/*
   1575 	 * we just keep track of how many pages have been marked bad
   1576 	 * in this device, to make everything add up in swap_off().
   1577 	 * we assume here that the range of slots will all be within
   1578 	 * one swap device.
   1579 	 */
   1580 
   1581 	KASSERT(uvmexp.swpgonly >= nslots);
   1582 	uvmexp.swpgonly -= nslots;
   1583 	sdp->swd_npgbad += nslots;
   1584 	UVMHIST_LOG(pdhist, "now %d bad", sdp->swd_npgbad, 0,0,0);
   1585 	mutex_exit(&uvm_swap_data_lock);
   1586 }
   1587 
   1588 /*
   1589  * uvm_swap_free: free swap slots
   1590  *
   1591  * => this can be all or part of an allocation made by uvm_swap_alloc
   1592  * => we lock uvm_swap_data_lock
   1593  */
   1594 void
   1595 uvm_swap_free(int startslot, int nslots)
   1596 {
   1597 	struct swapdev *sdp;
   1598 	UVMHIST_FUNC("uvm_swap_free"); UVMHIST_CALLED(pdhist);
   1599 
   1600 	UVMHIST_LOG(pdhist, "freeing %d slots starting at %d", nslots,
   1601 	    startslot, 0, 0);
   1602 
   1603 	/*
   1604 	 * ignore attempts to free the "bad" slot.
   1605 	 */
   1606 
   1607 	if (startslot == SWSLOT_BAD) {
   1608 		return;
   1609 	}
   1610 
   1611 	/*
   1612 	 * convert drum slot offset back to sdp, free the blocks
   1613 	 * in the extent, and return.   must hold pri lock to do
   1614 	 * lookup and access the extent.
   1615 	 */
   1616 
   1617 	mutex_enter(&uvm_swap_data_lock);
   1618 	sdp = swapdrum_getsdp(startslot);
   1619 	KASSERT(uvmexp.nswapdev >= 1);
   1620 	KASSERT(sdp != NULL);
   1621 	KASSERT(sdp->swd_npginuse >= nslots);
   1622 	blist_free(sdp->swd_blist, startslot - sdp->swd_drumoffset, nslots);
   1623 	sdp->swd_npginuse -= nslots;
   1624 	uvmexp.swpginuse -= nslots;
   1625 	mutex_exit(&uvm_swap_data_lock);
   1626 }
   1627 
   1628 /*
   1629  * uvm_swap_put: put any number of pages into a contig place on swap
   1630  *
   1631  * => can be sync or async
   1632  */
   1633 
   1634 int
   1635 uvm_swap_put(int swslot, struct vm_page **ppsp, int npages, int flags)
   1636 {
   1637 	int error;
   1638 
   1639 	error = uvm_swap_io(ppsp, swslot, npages, B_WRITE |
   1640 	    ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
   1641 	return error;
   1642 }
   1643 
   1644 /*
   1645  * uvm_swap_get: get a single page from swap
   1646  *
   1647  * => usually a sync op (from fault)
   1648  */
   1649 
   1650 int
   1651 uvm_swap_get(struct vm_page *page, int swslot, int flags)
   1652 {
   1653 	int error;
   1654 
   1655 	uvmexp.nswget++;
   1656 	KASSERT(flags & PGO_SYNCIO);
   1657 	if (swslot == SWSLOT_BAD) {
   1658 		return EIO;
   1659 	}
   1660 
   1661 	error = uvm_swap_io(&page, swslot, 1, B_READ |
   1662 	    ((flags & PGO_SYNCIO) ? 0 : B_ASYNC));
   1663 	if (error == 0) {
   1664 
   1665 		/*
   1666 		 * this page is no longer only in swap.
   1667 		 */
   1668 
   1669 		mutex_enter(&uvm_swap_data_lock);
   1670 		KASSERT(uvmexp.swpgonly > 0);
   1671 		uvmexp.swpgonly--;
   1672 		mutex_exit(&uvm_swap_data_lock);
   1673 	}
   1674 	return error;
   1675 }
   1676 
   1677 /*
   1678  * uvm_swap_io: do an i/o operation to swap
   1679  */
   1680 
   1681 static int
   1682 uvm_swap_io(struct vm_page **pps, int startslot, int npages, int flags)
   1683 {
   1684 	daddr_t startblk;
   1685 	struct	buf *bp;
   1686 	vaddr_t kva;
   1687 	int	error, mapinflags;
   1688 	bool write, async;
   1689 	UVMHIST_FUNC("uvm_swap_io"); UVMHIST_CALLED(pdhist);
   1690 
   1691 	UVMHIST_LOG(pdhist, "<- called, startslot=%d, npages=%d, flags=%d",
   1692 	    startslot, npages, flags, 0);
   1693 
   1694 	write = (flags & B_READ) == 0;
   1695 	async = (flags & B_ASYNC) != 0;
   1696 
   1697 	/*
   1698 	 * convert starting drum slot to block number
   1699 	 */
   1700 
   1701 	startblk = btodb((uint64_t)startslot << PAGE_SHIFT);
   1702 
   1703 	/*
   1704 	 * first, map the pages into the kernel.
   1705 	 */
   1706 
   1707 	mapinflags = !write ?
   1708 		UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_READ :
   1709 		UVMPAGER_MAPIN_WAITOK|UVMPAGER_MAPIN_WRITE;
   1710 	kva = uvm_pagermapin(pps, npages, mapinflags);
   1711 
   1712 	/*
   1713 	 * now allocate a buf for the i/o.
   1714 	 */
   1715 
   1716 	bp = getiobuf(swapdev_vp, true);
   1717 
   1718 	/*
   1719 	 * fill in the bp/sbp.   we currently route our i/o through
   1720 	 * /dev/drum's vnode [swapdev_vp].
   1721 	 */
   1722 
   1723 	bp->b_cflags = BC_BUSY | BC_NOCACHE;
   1724 	bp->b_flags = (flags & (B_READ|B_ASYNC));
   1725 	bp->b_proc = &proc0;	/* XXX */
   1726 	bp->b_vnbufs.le_next = NOLIST;
   1727 	bp->b_data = (void *)kva;
   1728 	bp->b_blkno = startblk;
   1729 	bp->b_bufsize = bp->b_bcount = npages << PAGE_SHIFT;
   1730 
   1731 	/*
   1732 	 * bump v_numoutput (counter of number of active outputs).
   1733 	 */
   1734 
   1735 	if (write) {
   1736 		mutex_enter(&swapdev_vp->v_interlock);
   1737 		swapdev_vp->v_numoutput++;
   1738 		mutex_exit(&swapdev_vp->v_interlock);
   1739 	}
   1740 
   1741 	/*
   1742 	 * for async ops we must set up the iodone handler.
   1743 	 */
   1744 
   1745 	if (async) {
   1746 		bp->b_iodone = uvm_aio_biodone;
   1747 		UVMHIST_LOG(pdhist, "doing async!", 0, 0, 0, 0);
   1748 		if (curlwp == uvm.pagedaemon_lwp)
   1749 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1750 		else
   1751 			BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
   1752 	} else {
   1753 		bp->b_iodone = NULL;
   1754 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1755 	}
   1756 	UVMHIST_LOG(pdhist,
   1757 	    "about to start io: data = %p blkno = 0x%x, bcount = %ld",
   1758 	    bp->b_data, bp->b_blkno, bp->b_bcount, 0);
   1759 
   1760 	/*
   1761 	 * now we start the I/O, and if async, return.
   1762 	 */
   1763 
   1764 	VOP_STRATEGY(swapdev_vp, bp);
   1765 	if (async)
   1766 		return 0;
   1767 
   1768 	/*
   1769 	 * must be sync i/o.   wait for it to finish
   1770 	 */
   1771 
   1772 	error = biowait(bp);
   1773 
   1774 	/*
   1775 	 * kill the pager mapping
   1776 	 */
   1777 
   1778 	uvm_pagermapout(kva, npages);
   1779 
   1780 	/*
   1781 	 * now dispose of the buf and we're done.
   1782 	 */
   1783 
   1784 	if (write) {
   1785 		mutex_enter(&swapdev_vp->v_interlock);
   1786 		vwakeup(bp);
   1787 		mutex_exit(&swapdev_vp->v_interlock);
   1788 	}
   1789 	putiobuf(bp);
   1790 	UVMHIST_LOG(pdhist, "<- done (sync)  error=%d", error, 0, 0, 0);
   1791 
   1792 	return (error);
   1793 }
   1794