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