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