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