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