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