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