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