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