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