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