xref: /freebsd/sys/vm/swap_pager.c (revision 327f4e8394e50c169b001a70973a3f89cd5a5d36)
1df8bae1dSRodney W. Grimes /*
21c7c3c6aSMatthew Dillon  * Copyright (c) 1998 Matthew Dillon,
326f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
4df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
5df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
6df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
21df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
401c7c3c6aSMatthew Dillon  *				New Swap System
411c7c3c6aSMatthew Dillon  *				Matthew Dillon
421c7c3c6aSMatthew Dillon  *
431c7c3c6aSMatthew Dillon  * Radix Bitmap 'blists'.
441c7c3c6aSMatthew Dillon  *
451c7c3c6aSMatthew Dillon  *	- The new swapper uses the new radix bitmap code.  This should scale
461c7c3c6aSMatthew Dillon  *	  to arbitrarily small or arbitrarily large swap spaces and an almost
471c7c3c6aSMatthew Dillon  *	  arbitrary degree of fragmentation.
481c7c3c6aSMatthew Dillon  *
491c7c3c6aSMatthew Dillon  * Features:
501c7c3c6aSMatthew Dillon  *
511c7c3c6aSMatthew Dillon  *	- on the fly reallocation of swap during putpages.  The new system
521c7c3c6aSMatthew Dillon  *	  does not try to keep previously allocated swap blocks for dirty
531c7c3c6aSMatthew Dillon  *	  pages.
541c7c3c6aSMatthew Dillon  *
551c7c3c6aSMatthew Dillon  *	- on the fly deallocation of swap
561c7c3c6aSMatthew Dillon  *
571c7c3c6aSMatthew Dillon  *	- No more garbage collection required.  Unnecessarily allocated swap
581c7c3c6aSMatthew Dillon  *	  blocks only exist for dirty vm_page_t's now and these are already
591c7c3c6aSMatthew Dillon  *	  cycled (in a high-load system) by the pager.  We also do on-the-fly
601c7c3c6aSMatthew Dillon  *	  removal of invalidated swap blocks when a page is destroyed
611c7c3c6aSMatthew Dillon  *	  or renamed.
621c7c3c6aSMatthew Dillon  *
63df8bae1dSRodney W. Grimes  * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
64df8bae1dSRodney W. Grimes  *
65df8bae1dSRodney W. Grimes  *	@(#)swap_pager.c	8.9 (Berkeley) 3/21/94
661c7c3c6aSMatthew Dillon  *
67327f4e83SMatthew Dillon  * $Id: swap_pager.c,v 1.113 1999/02/06 07:22:21 dillon Exp $
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <sys/param.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
7264abb5a5SDavid Greenman #include <sys/kernel.h>
73df8bae1dSRodney W. Grimes #include <sys/proc.h>
74df8bae1dSRodney W. Grimes #include <sys/buf.h>
75df8bae1dSRodney W. Grimes #include <sys/vnode.h>
76df8bae1dSRodney W. Grimes #include <sys/malloc.h>
77efeaf95aSDavid Greenman #include <sys/vmmeter.h>
78327f4e83SMatthew Dillon #include <sys/sysctl.h>
791c7c3c6aSMatthew Dillon #include <sys/blist.h>
801c7c3c6aSMatthew Dillon #include <sys/lock.h>
81df8bae1dSRodney W. Grimes 
82e47ed70bSJohn Dyson #ifndef MAX_PAGEOUT_CLUSTER
83ffc82b0aSJohn Dyson #define MAX_PAGEOUT_CLUSTER 16
84e47ed70bSJohn Dyson #endif
85e47ed70bSJohn Dyson 
86e47ed70bSJohn Dyson #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
87e47ed70bSJohn Dyson 
881c7c3c6aSMatthew Dillon #include "opt_swap.h"
89df8bae1dSRodney W. Grimes #include <vm/vm.h>
90efeaf95aSDavid Greenman #include <vm/vm_prot.h>
91efeaf95aSDavid Greenman #include <vm/vm_object.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
93efeaf95aSDavid Greenman #include <vm/vm_pager.h>
94df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
95df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
96efeaf95aSDavid Greenman #include <vm/vm_extern.h>
971c7c3c6aSMatthew Dillon #include <vm/vm_zone.h>
98df8bae1dSRodney W. Grimes 
991c7c3c6aSMatthew Dillon #define SWM_FREE	0x02	/* free, period			*/
1001c7c3c6aSMatthew Dillon #define SWM_POP		0x04	/* pop out			*/
10126f9a767SRodney W. Grimes 
10224a1cce3SDavid Greenman /*
1031c7c3c6aSMatthew Dillon  * vm_swap_size is in page-sized chunks now.  It was DEV_BSIZE'd chunks
1041c7c3c6aSMatthew Dillon  * in the old system.
10524a1cce3SDavid Greenman  */
1061c7c3c6aSMatthew Dillon 
1071c7c3c6aSMatthew Dillon extern int vm_swap_size;	/* number of free swap blocks, in pages */
1081c7c3c6aSMatthew Dillon 
1091c7c3c6aSMatthew Dillon int swap_pager_full;		/* swap space exhaustion (w/ hysteresis)*/
1101c7c3c6aSMatthew Dillon static int nsw_rcount;		/* free read buffers			*/
111327f4e83SMatthew Dillon static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
112327f4e83SMatthew Dillon static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
113327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
114327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
1151c7c3c6aSMatthew Dillon static int sw_alloc_interlock;	/* swap pager allocation interlock	*/
1161c7c3c6aSMatthew Dillon 
1171c7c3c6aSMatthew Dillon struct blist *swapblist;
1181c7c3c6aSMatthew Dillon static struct swblock **swhash;
1191c7c3c6aSMatthew Dillon static int swhash_mask;
120327f4e83SMatthew Dillon static int swap_async_max = 4;	/* maximum in-progress async I/O's	*/
121327f4e83SMatthew Dillon static int swap_cluster_max;	/* maximum VOP I/O allowed		*/
1221c7c3c6aSMatthew Dillon 
123327f4e83SMatthew Dillon #ifndef DISALLOW_SWAP_TUNE
124327f4e83SMatthew Dillon 
125327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_async_max,
126327f4e83SMatthew Dillon         CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops");
127327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_cluster_max,
128327f4e83SMatthew Dillon         CTLFLAG_RW, &swap_cluster_max, 0, "Maximum swap I/O cluster (pages)");
129327f4e83SMatthew Dillon 
130327f4e83SMatthew Dillon #else
131327f4e83SMatthew Dillon 
132327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_async_max,
133327f4e83SMatthew Dillon         CTLFLAG_RD, &swap_async_max, 0, "");
134327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_cluster_max,
135327f4e83SMatthew Dillon         CTLFLAG_RD, &swap_cluster_max, 0, "");
136327f4e83SMatthew Dillon 
137327f4e83SMatthew Dillon #endif
1381c7c3c6aSMatthew Dillon 
1391c7c3c6aSMatthew Dillon /*
1401c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
1411c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
1421c7c3c6aSMatthew Dillon  */
1431c7c3c6aSMatthew Dillon 
1441c7c3c6aSMatthew Dillon #define NOBJLISTS		8
1451c7c3c6aSMatthew Dillon 
1461c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
1471c7c3c6aSMatthew Dillon 	(&swap_pager_object_list[((int)(long)handle >> 4) & (NOBJLISTS-1)])
1481c7c3c6aSMatthew Dillon 
1491c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
1501c7c3c6aSMatthew Dillon struct pagerlst		swap_pager_un_object_list;
1511c7c3c6aSMatthew Dillon vm_zone_t		swap_zone;
1521c7c3c6aSMatthew Dillon 
1531c7c3c6aSMatthew Dillon /*
1541c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
1551c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
1561c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
1571c7c3c6aSMatthew Dillon  */
1581c7c3c6aSMatthew Dillon 
159ff98689dSBruce Evans static vm_object_t
1606cde7a16SDavid Greenman 		swap_pager_alloc __P((void *handle, vm_ooffset_t size,
161a316d390SJohn Dyson 				      vm_prot_t prot, vm_ooffset_t offset));
162ff98689dSBruce Evans static void	swap_pager_dealloc __P((vm_object_t object));
163f708ef1bSPoul-Henning Kamp static int	swap_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
164ff98689dSBruce Evans static void	swap_pager_init __P((void));
1651c7c3c6aSMatthew Dillon static void	swap_pager_unswapped __P((vm_page_t));
166f708ef1bSPoul-Henning Kamp 
167df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
1681c7c3c6aSMatthew Dillon 	swap_pager_init,	/* early system initialization of pager	*/
1691c7c3c6aSMatthew Dillon 	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
1701c7c3c6aSMatthew Dillon 	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
1711c7c3c6aSMatthew Dillon 	swap_pager_getpages,	/* pagein				*/
1721c7c3c6aSMatthew Dillon 	swap_pager_putpages,	/* pageout				*/
1731c7c3c6aSMatthew Dillon 	swap_pager_haspage,	/* get backing store status for page	*/
1741c7c3c6aSMatthew Dillon 	swap_pager_unswapped	/* remove swap related to page		*/
175df8bae1dSRodney W. Grimes };
176df8bae1dSRodney W. Grimes 
1771c7c3c6aSMatthew Dillon /*
1781c7c3c6aSMatthew Dillon  * dmmax is in page-sized chunks with the new swap system.  It was
1791c7c3c6aSMatthew Dillon  * dev-bsized chunks in the old.
1801c7c3c6aSMatthew Dillon  *
1811c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
1821c7c3c6aSMatthew Dillon  * internal.
1831c7c3c6aSMatthew Dillon  */
1841c7c3c6aSMatthew Dillon 
185f708ef1bSPoul-Henning Kamp int dmmax;
1861c7c3c6aSMatthew Dillon static int dmmax_mask;
1871c7c3c6aSMatthew Dillon int nswap_lowat = 128;		/* in pages, swap_pager_full warning	*/
1882b0d37a4SMatthew Dillon int nswap_hiwat = 512;		/* in pages, swap_pager_full warning	*/
18926f9a767SRodney W. Grimes 
1901c7c3c6aSMatthew Dillon static __inline void	swp_sizecheck __P((void));
1911c7c3c6aSMatthew Dillon static void	swp_pager_sync_iodone __P((struct buf *bp));
1921c7c3c6aSMatthew Dillon static void	swp_pager_async_iodone __P((struct buf *bp));
19324a1cce3SDavid Greenman 
1941c7c3c6aSMatthew Dillon /*
1951c7c3c6aSMatthew Dillon  * Swap bitmap functions
1961c7c3c6aSMatthew Dillon  */
1971c7c3c6aSMatthew Dillon 
1981c7c3c6aSMatthew Dillon static __inline void	swp_pager_freeswapspace __P((daddr_t blk, int npages));
1991c7c3c6aSMatthew Dillon static __inline daddr_t	swp_pager_getswapspace __P((int npages));
2001c7c3c6aSMatthew Dillon 
2011c7c3c6aSMatthew Dillon /*
2021c7c3c6aSMatthew Dillon  * Metadata functions
2031c7c3c6aSMatthew Dillon  */
2041c7c3c6aSMatthew Dillon 
2051c7c3c6aSMatthew Dillon static void swp_pager_meta_build __P((vm_object_t, daddr_t, daddr_t, int));
2061c7c3c6aSMatthew Dillon static void swp_pager_meta_free __P((vm_object_t, daddr_t, daddr_t));
2071c7c3c6aSMatthew Dillon static void swp_pager_meta_free_all __P((vm_object_t));
2081c7c3c6aSMatthew Dillon static daddr_t swp_pager_meta_ctl __P((vm_object_t, vm_pindex_t, int));
2091c7c3c6aSMatthew Dillon 
2101c7c3c6aSMatthew Dillon /*
2111c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
2121c7c3c6aSMatthew Dillon  *
2131c7c3c6aSMatthew Dillon  *	update the swap_pager_full indication and warn when we are
2141c7c3c6aSMatthew Dillon  *	about to run out of swap space.
2151c7c3c6aSMatthew Dillon  *
2161c7c3c6aSMatthew Dillon  *	No restrictions on call
2171c7c3c6aSMatthew Dillon  *	This routine may not block.
2181c7c3c6aSMatthew Dillon  *	This routine must be called at splvm()
2191c7c3c6aSMatthew Dillon  */
220de5f6a77SJohn Dyson 
221c1087c13SBruce Evans static __inline void
2221c7c3c6aSMatthew Dillon swp_sizecheck()
2230d94caffSDavid Greenman {
2241c7c3c6aSMatthew Dillon 	if (vm_swap_size < nswap_lowat) {
2252b0d37a4SMatthew Dillon 		if (swap_pager_full == 0) {
2261af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
22726f9a767SRodney W. Grimes 			swap_pager_full = 1;
2282b0d37a4SMatthew Dillon 		}
2291c7c3c6aSMatthew Dillon 	} else if (vm_swap_size > nswap_hiwat) {
23026f9a767SRodney W. Grimes 		swap_pager_full = 0;
23126f9a767SRodney W. Grimes 	}
2321c7c3c6aSMatthew Dillon }
2331c7c3c6aSMatthew Dillon 
2341c7c3c6aSMatthew Dillon /*
2351c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
2361c7c3c6aSMatthew Dillon  *
2371c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
2381c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
2391c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
2401c7c3c6aSMatthew Dillon  */
24126f9a767SRodney W. Grimes 
242f5a12711SPoul-Henning Kamp static void
243df8bae1dSRodney W. Grimes swap_pager_init()
244df8bae1dSRodney W. Grimes {
2451c7c3c6aSMatthew Dillon 	/*
2461c7c3c6aSMatthew Dillon 	 * Initialize object lists
2471c7c3c6aSMatthew Dillon 	 */
2481c7c3c6aSMatthew Dillon 	int i;
2491c7c3c6aSMatthew Dillon 
2501c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
2511c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
25224a1cce3SDavid Greenman 	TAILQ_INIT(&swap_pager_un_object_list);
253df8bae1dSRodney W. Grimes 
254df8bae1dSRodney W. Grimes 	/*
2551c7c3c6aSMatthew Dillon 	 * Device Stripe, in PAGE_SIZE'd blocks
256df8bae1dSRodney W. Grimes 	 */
2571c7c3c6aSMatthew Dillon 
2581c7c3c6aSMatthew Dillon 	dmmax = SWB_NPAGES * 2;
2591c7c3c6aSMatthew Dillon 	dmmax_mask = ~(dmmax - 1);
2601c7c3c6aSMatthew Dillon }
26126f9a767SRodney W. Grimes 
262df8bae1dSRodney W. Grimes /*
2631c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
2641c7c3c6aSMatthew Dillon  *
2651c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
2661c7c3c6aSMatthew Dillon  *	its main loop.
267df8bae1dSRodney W. Grimes  */
268df8bae1dSRodney W. Grimes 
26924a1cce3SDavid Greenman void
27024a1cce3SDavid Greenman swap_pager_swap_init()
271df8bae1dSRodney W. Grimes {
2721c7c3c6aSMatthew Dillon 	int n;
2730d94caffSDavid Greenman 
27426f9a767SRodney W. Grimes 	/*
2751c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
2761c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
2771c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
2781c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
2791c7c3c6aSMatthew Dillon 	 *
280327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
2811c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
2821c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
2831c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
284327f4e83SMatthew Dillon 	 *
285327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
286327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
287327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
288327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
289327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
290327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
291327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
292327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
293327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
294327f4e83SMatthew Dillon 	 * So it all works out pretty well.
29526f9a767SRodney W. Grimes 	 */
29624a1cce3SDavid Greenman 
297327f4e83SMatthew Dillon 	swap_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
298327f4e83SMatthew Dillon 
2991c7c3c6aSMatthew Dillon 	nsw_rcount = (nswbuf + 1) / 2;
300327f4e83SMatthew Dillon 	nsw_wcount_sync = (nswbuf + 3) / 4;
301327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
302327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
303327f4e83SMatthew Dillon 	nsw_cluster_max = swap_cluster_max;
30424a1cce3SDavid Greenman 
3051c7c3c6aSMatthew Dillon 	/*
3061c7c3c6aSMatthew Dillon 	 * Initialize our zone.  Right now I'm just guessing on the number
3071c7c3c6aSMatthew Dillon 	 * we need based on the number of pages in the system.  Each swblock
3081c7c3c6aSMatthew Dillon 	 * can hold 16 pages, so this is probably overkill.
3091c7c3c6aSMatthew Dillon 	 */
31024a1cce3SDavid Greenman 
3111c7c3c6aSMatthew Dillon 	n = cnt.v_page_count * 2;
31226f9a767SRodney W. Grimes 
3131c7c3c6aSMatthew Dillon 	swap_zone = zinit(
3141c7c3c6aSMatthew Dillon 	    "SWAPMETA",
3151c7c3c6aSMatthew Dillon 	    sizeof(struct swblock),
3161c7c3c6aSMatthew Dillon 	    n,
3171c7c3c6aSMatthew Dillon 	    ZONE_INTERRUPT,
3181c7c3c6aSMatthew Dillon 	    1
3191c7c3c6aSMatthew Dillon 	);
32024a1cce3SDavid Greenman 
3211c7c3c6aSMatthew Dillon 	/*
3221c7c3c6aSMatthew Dillon 	 * Initialize our meta-data hash table.  The swapper does not need to
3231c7c3c6aSMatthew Dillon 	 * be quite as efficient as the VM system, so we do not use an
3241c7c3c6aSMatthew Dillon 	 * oversized hash table.
3251c7c3c6aSMatthew Dillon 	 *
3261c7c3c6aSMatthew Dillon 	 * 	n: 		size of hash table, must be power of 2
3271c7c3c6aSMatthew Dillon 	 *	swhash_mask:	hash table index mask
3281c7c3c6aSMatthew Dillon 	 */
329df8bae1dSRodney W. Grimes 
3301c7c3c6aSMatthew Dillon 	for (n = 1; n < cnt.v_page_count / 4; n <<= 1)
3311c7c3c6aSMatthew Dillon 		;
3321c7c3c6aSMatthew Dillon 
3331c7c3c6aSMatthew Dillon 	swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK);
3341c7c3c6aSMatthew Dillon 	bzero(swhash, sizeof(struct swblock *) * n);
3351c7c3c6aSMatthew Dillon 
3361c7c3c6aSMatthew Dillon 	swhash_mask = n - 1;
33724a1cce3SDavid Greenman }
33824a1cce3SDavid Greenman 
33924a1cce3SDavid Greenman /*
3401c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
3411c7c3c6aSMatthew Dillon  *			its metadata structures.
3421c7c3c6aSMatthew Dillon  *
3431c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
3441c7c3c6aSMatthew Dillon  *	OBJT_SWAP object.  We do this by creating an OBJT_DEFAULT object
3451c7c3c6aSMatthew Dillon  *	and then converting it with swp_pager_meta_build().
3461c7c3c6aSMatthew Dillon  *
3471c7c3c6aSMatthew Dillon  *	This routine may block in vm_object_allocate() and create a named
3481c7c3c6aSMatthew Dillon  *	object lookup race, so we must interlock.   We must also run at
3491c7c3c6aSMatthew Dillon  *	splvm() for the object lookup to handle races with interrupts, but
3501c7c3c6aSMatthew Dillon  *	we do not have to maintain splvm() in between the lookup and the
3511c7c3c6aSMatthew Dillon  *	add because (I believe) it is not possible to attempt to create
3521c7c3c6aSMatthew Dillon  *	a new swap object w/handle when a default object with that handle
3531c7c3c6aSMatthew Dillon  *	already exists.
35424a1cce3SDavid Greenman  */
3551c7c3c6aSMatthew Dillon 
356f5a12711SPoul-Henning Kamp static vm_object_t
3576cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
358b9dcd593SBruce Evans 		 vm_ooffset_t offset)
35924a1cce3SDavid Greenman {
36024a1cce3SDavid Greenman 	vm_object_t object;
36124a1cce3SDavid Greenman 
36224a1cce3SDavid Greenman 	if (handle) {
3631c7c3c6aSMatthew Dillon 		/*
3641c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
3651c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
3661c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
3671c7c3c6aSMatthew Dillon 		 * of the handle.
3681c7c3c6aSMatthew Dillon 		 */
3691c7c3c6aSMatthew Dillon 
3701c7c3c6aSMatthew Dillon 		while (sw_alloc_interlock) {
3711c7c3c6aSMatthew Dillon 			sw_alloc_interlock = -1;
3721c7c3c6aSMatthew Dillon 			tsleep(&sw_alloc_interlock, PVM, "swpalc", 0);
3731c7c3c6aSMatthew Dillon 		}
3741c7c3c6aSMatthew Dillon 		sw_alloc_interlock = 1;
3751c7c3c6aSMatthew Dillon 
3761c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
3771c7c3c6aSMatthew Dillon 
37824a1cce3SDavid Greenman 		if (object != NULL) {
37924a1cce3SDavid Greenman 			vm_object_reference(object);
38024a1cce3SDavid Greenman 		} else {
3811c7c3c6aSMatthew Dillon 			object = vm_object_allocate(OBJT_DEFAULT,
3826cde7a16SDavid Greenman 				OFF_TO_IDX(offset + PAGE_MASK + size));
38324a1cce3SDavid Greenman 			object->handle = handle;
3841c7c3c6aSMatthew Dillon 
3851c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
3861c7c3c6aSMatthew Dillon 			    object,
3871c7c3c6aSMatthew Dillon 			    0,
3881c7c3c6aSMatthew Dillon 			    SWAPBLK_NONE,
3891c7c3c6aSMatthew Dillon 			    0
3901c7c3c6aSMatthew Dillon 			);
39124a1cce3SDavid Greenman 		}
3921c7c3c6aSMatthew Dillon 
3931c7c3c6aSMatthew Dillon 		if (sw_alloc_interlock < 0)
3941c7c3c6aSMatthew Dillon 			wakeup(&sw_alloc_interlock);
3951c7c3c6aSMatthew Dillon 
3961c7c3c6aSMatthew Dillon 		sw_alloc_interlock = 0;
39724a1cce3SDavid Greenman 	} else {
3981c7c3c6aSMatthew Dillon 		object = vm_object_allocate(OBJT_DEFAULT,
3996cde7a16SDavid Greenman 			OFF_TO_IDX(offset + PAGE_MASK + size));
4001c7c3c6aSMatthew Dillon 
4011c7c3c6aSMatthew Dillon 		swp_pager_meta_build(
4021c7c3c6aSMatthew Dillon 		    object,
4031c7c3c6aSMatthew Dillon 		    0,
4041c7c3c6aSMatthew Dillon 		    SWAPBLK_NONE,
4051c7c3c6aSMatthew Dillon 		    0
4061c7c3c6aSMatthew Dillon 		);
40724a1cce3SDavid Greenman 	}
40824a1cce3SDavid Greenman 
40924a1cce3SDavid Greenman 	return (object);
410df8bae1dSRodney W. Grimes }
411df8bae1dSRodney W. Grimes 
41226f9a767SRodney W. Grimes /*
4131c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
4141c7c3c6aSMatthew Dillon  *
4151c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
4161c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
4171c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
4181c7c3c6aSMatthew Dillon  *	about to be destroyed.
4191c7c3c6aSMatthew Dillon  *
4201c7c3c6aSMatthew Dillon  *	This routine may block, but no longer does.
4211c7c3c6aSMatthew Dillon  *
4221c7c3c6aSMatthew Dillon  *	The object must be locked or unreferenceable.
42326f9a767SRodney W. Grimes  */
42426f9a767SRodney W. Grimes 
425df8bae1dSRodney W. Grimes static void
4261c7c3c6aSMatthew Dillon swap_pager_dealloc(object)
4272a4895f4SDavid Greenman 	vm_object_t object;
42826f9a767SRodney W. Grimes {
42926f9a767SRodney W. Grimes 	/*
4301c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
4311c7c3c6aSMatthew Dillon 	 * pageout completion.
43226f9a767SRodney W. Grimes 	 */
433b44e4b7aSJohn Dyson 
4341c7c3c6aSMatthew Dillon 	if (object->handle == NULL) {
4351c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(&swap_pager_un_object_list, object, pager_object_list);
43624ea4a96SDavid Greenman 	} else {
4371c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list);
43826f9a767SRodney W. Grimes 	}
4391c7c3c6aSMatthew Dillon 
4401c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
4411c7c3c6aSMatthew Dillon 
4421c7c3c6aSMatthew Dillon 	/*
4431c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
4441c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
4451c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
4461c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
4471c7c3c6aSMatthew Dillon 	 */
4481c7c3c6aSMatthew Dillon 
4491c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
4501c7c3c6aSMatthew Dillon }
4511c7c3c6aSMatthew Dillon 
4521c7c3c6aSMatthew Dillon /************************************************************************
4531c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
4541c7c3c6aSMatthew Dillon  ************************************************************************/
4551c7c3c6aSMatthew Dillon 
4561c7c3c6aSMatthew Dillon /*
4571c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
4581c7c3c6aSMatthew Dillon  *
4591c7c3c6aSMatthew Dillon  *	Allocate swap for the requested number of pages.  The starting
4601c7c3c6aSMatthew Dillon  *	swap block number (a page index) is returned or SWAPBLK_NONE
4611c7c3c6aSMatthew Dillon  *	if the allocation failed.
4621c7c3c6aSMatthew Dillon  *
4631c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
4641c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
4651c7c3c6aSMatthew Dillon  *
4661c7c3c6aSMatthew Dillon  *	Must be called at splvm() to avoid races with bitmap frees from
4671c7c3c6aSMatthew Dillon  *	vm_page_remove() aka swap_pager_page_removed().
4681c7c3c6aSMatthew Dillon  *
4691c7c3c6aSMatthew Dillon  *	This routine may not block
4701c7c3c6aSMatthew Dillon  *	This routine must be called at splvm().
4711c7c3c6aSMatthew Dillon  */
4721c7c3c6aSMatthew Dillon 
4731c7c3c6aSMatthew Dillon static __inline daddr_t
4741c7c3c6aSMatthew Dillon swp_pager_getswapspace(npages)
4751c7c3c6aSMatthew Dillon 	int npages;
4761c7c3c6aSMatthew Dillon {
4771c7c3c6aSMatthew Dillon 	daddr_t blk;
4781c7c3c6aSMatthew Dillon 
4791c7c3c6aSMatthew Dillon 	if ((blk = blist_alloc(swapblist, npages)) == SWAPBLK_NONE) {
4802b0d37a4SMatthew Dillon 		if (swap_pager_full != 2) {
4811c7c3c6aSMatthew Dillon 			printf("swap_pager_getswapspace: failed\n");
4822b0d37a4SMatthew Dillon 			swap_pager_full = 2;
4832b0d37a4SMatthew Dillon 		}
4841c7c3c6aSMatthew Dillon 	} else {
4851c7c3c6aSMatthew Dillon 		vm_swap_size -= npages;
4861c7c3c6aSMatthew Dillon 		swp_sizecheck();
4871c7c3c6aSMatthew Dillon 	}
4881c7c3c6aSMatthew Dillon 	return(blk);
48926f9a767SRodney W. Grimes }
49026f9a767SRodney W. Grimes 
49126f9a767SRodney W. Grimes /*
4921c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
4931c7c3c6aSMatthew Dillon  *
4941c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
4951c7c3c6aSMatthew Dillon  *
4961c7c3c6aSMatthew Dillon  *	Note:  This routine may not block (it could in the old swap code),
4971c7c3c6aSMatthew Dillon  *	and through the use of the new blist routines it does not block.
4981c7c3c6aSMatthew Dillon  *
4991c7c3c6aSMatthew Dillon  *	We must be called at splvm() to avoid races with bitmap frees from
5001c7c3c6aSMatthew Dillon  *	vm_page_remove() aka swap_pager_page_removed().
5011c7c3c6aSMatthew Dillon  *
5021c7c3c6aSMatthew Dillon  *	This routine may not block
5031c7c3c6aSMatthew Dillon  *	This routine must be called at splvm().
50426f9a767SRodney W. Grimes  */
5051c7c3c6aSMatthew Dillon 
5061c7c3c6aSMatthew Dillon static __inline void
5071c7c3c6aSMatthew Dillon swp_pager_freeswapspace(blk, npages)
5081c7c3c6aSMatthew Dillon 	daddr_t blk;
5091c7c3c6aSMatthew Dillon 	int npages;
5100d94caffSDavid Greenman {
5111c7c3c6aSMatthew Dillon 	blist_free(swapblist, blk, npages);
5121c7c3c6aSMatthew Dillon 	vm_swap_size += npages;
5131c7c3c6aSMatthew Dillon 	swp_sizecheck();
51426f9a767SRodney W. Grimes }
5151c7c3c6aSMatthew Dillon 
51626f9a767SRodney W. Grimes /*
5171c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
5181c7c3c6aSMatthew Dillon  *				range within an object.
5191c7c3c6aSMatthew Dillon  *
5201c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
5211c7c3c6aSMatthew Dillon  *
5221c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
5231c7c3c6aSMatthew Dillon  *
5241c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
5251c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
5261c7c3c6aSMatthew Dillon  *	we should be ok.
52726f9a767SRodney W. Grimes  */
5281c7c3c6aSMatthew Dillon 
52926f9a767SRodney W. Grimes void
53024a1cce3SDavid Greenman swap_pager_freespace(object, start, size)
53124a1cce3SDavid Greenman 	vm_object_t object;
532a316d390SJohn Dyson 	vm_pindex_t start;
533a316d390SJohn Dyson 	vm_size_t size;
53426f9a767SRodney W. Grimes {
5351c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
53626f9a767SRodney W. Grimes }
53726f9a767SRodney W. Grimes 
5380a47b48bSJohn Dyson /*
5391c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
5401c7c3c6aSMatthew Dillon  *			and destroy the source.
5411c7c3c6aSMatthew Dillon  *
5421c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
5431c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
5441c7c3c6aSMatthew Dillon  *	we keep the destination's.
5451c7c3c6aSMatthew Dillon  *
5461c7c3c6aSMatthew Dillon  *	This routine is allowed to block.  It may block allocating metadata
5471c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
5481c7c3c6aSMatthew Dillon  *	progress on the source.
5491c7c3c6aSMatthew Dillon  *
5501c7c3c6aSMatthew Dillon  *	XXX vm_page_collapse() kinda expects us not to block because we
5511c7c3c6aSMatthew Dillon  *	supposedly do not need to allocate memory, but for the moment we
5521c7c3c6aSMatthew Dillon  *	*may* have to get a little memory from the zone allocator, but
5531c7c3c6aSMatthew Dillon  *	it is taken from the interrupt memory.  We should be ok.
5541c7c3c6aSMatthew Dillon  *
5551c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
5561c7c3c6aSMatthew Dillon  *
5571c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
5581c7c3c6aSMatthew Dillon  *
5591c7c3c6aSMatthew Dillon  *	The source and destination objects must be
5605e24f1a2SMatthew Dillon  *	locked or inaccessible (XXX are they ?)
56126f9a767SRodney W. Grimes  */
56226f9a767SRodney W. Grimes 
56326f9a767SRodney W. Grimes void
5641c7c3c6aSMatthew Dillon swap_pager_copy(srcobject, dstobject, offset, destroysource)
56524a1cce3SDavid Greenman 	vm_object_t srcobject;
56624a1cce3SDavid Greenman 	vm_object_t dstobject;
567a316d390SJohn Dyson 	vm_pindex_t offset;
568c0877f10SJohn Dyson 	int destroysource;
56926f9a767SRodney W. Grimes {
570a316d390SJohn Dyson 	vm_pindex_t i;
57126f9a767SRodney W. Grimes 
57226f9a767SRodney W. Grimes 	/*
5731c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
5741c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
57526f9a767SRodney W. Grimes 	 */
5761c7c3c6aSMatthew Dillon 
577cbd8ec09SJohn Dyson 	if (destroysource) {
57824a1cce3SDavid Greenman 		if (srcobject->handle == NULL) {
5791c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
5801c7c3c6aSMatthew Dillon 			    &swap_pager_un_object_list,
5811c7c3c6aSMatthew Dillon 			    srcobject,
5821c7c3c6aSMatthew Dillon 			    pager_object_list
5831c7c3c6aSMatthew Dillon 			);
58426f9a767SRodney W. Grimes 		} else {
5851c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
5861c7c3c6aSMatthew Dillon 			    NOBJLIST(srcobject->handle),
5871c7c3c6aSMatthew Dillon 			    srcobject,
5881c7c3c6aSMatthew Dillon 			    pager_object_list
5891c7c3c6aSMatthew Dillon 			);
59026f9a767SRodney W. Grimes 		}
591cbd8ec09SJohn Dyson 	}
59226f9a767SRodney W. Grimes 
5931c7c3c6aSMatthew Dillon 	/*
5941c7c3c6aSMatthew Dillon 	 * transfer source to destination.
5951c7c3c6aSMatthew Dillon 	 */
5961c7c3c6aSMatthew Dillon 
5971c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
5981c7c3c6aSMatthew Dillon 		daddr_t dstaddr;
5991c7c3c6aSMatthew Dillon 
6001c7c3c6aSMatthew Dillon 		/*
6011c7c3c6aSMatthew Dillon 		 * Locate (without changing) the swapblk on the destination,
6021c7c3c6aSMatthew Dillon 		 * unless it is invalid in which case free it silently, or
6031c7c3c6aSMatthew Dillon 		 * if the destination is a resident page, in which case the
6041c7c3c6aSMatthew Dillon 		 * source is thrown away.
6051c7c3c6aSMatthew Dillon 		 */
6061c7c3c6aSMatthew Dillon 
6071c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
6081c7c3c6aSMatthew Dillon 
6091c7c3c6aSMatthew Dillon 		if (dstaddr == SWAPBLK_NONE) {
6101c7c3c6aSMatthew Dillon 			/*
6111c7c3c6aSMatthew Dillon 			 * Destination has no swapblk and is not resident,
6121c7c3c6aSMatthew Dillon 			 * copy source.
6131c7c3c6aSMatthew Dillon 			 */
6141c7c3c6aSMatthew Dillon 			daddr_t srcaddr;
6151c7c3c6aSMatthew Dillon 
6161c7c3c6aSMatthew Dillon 			srcaddr = swp_pager_meta_ctl(
6171c7c3c6aSMatthew Dillon 			    srcobject,
6181c7c3c6aSMatthew Dillon 			    i + offset,
6191c7c3c6aSMatthew Dillon 			    SWM_POP
6201c7c3c6aSMatthew Dillon 			);
6211c7c3c6aSMatthew Dillon 
6221c7c3c6aSMatthew Dillon 			if (srcaddr != SWAPBLK_NONE)
6231c7c3c6aSMatthew Dillon 				swp_pager_meta_build(dstobject, i, srcaddr, 1);
6241c7c3c6aSMatthew Dillon 		} else {
6251c7c3c6aSMatthew Dillon 			/*
6261c7c3c6aSMatthew Dillon 			 * Destination has valid swapblk or it is represented
6271c7c3c6aSMatthew Dillon 			 * by a resident page.  We destroy the sourceblock.
6281c7c3c6aSMatthew Dillon 			 */
6291c7c3c6aSMatthew Dillon 
6301c7c3c6aSMatthew Dillon 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
6311c7c3c6aSMatthew Dillon 		}
63226f9a767SRodney W. Grimes 	}
63326f9a767SRodney W. Grimes 
63426f9a767SRodney W. Grimes 	/*
6351c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
6361c7c3c6aSMatthew Dillon 	 *
6371c7c3c6aSMatthew Dillon 	 * We have to revert the type to OBJT_DEFAULT so we do not accidently
6381c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
63926f9a767SRodney W. Grimes 	 */
64026f9a767SRodney W. Grimes 
641c0877f10SJohn Dyson 	if (destroysource) {
6421c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
6431c7c3c6aSMatthew Dillon 		/*
6441c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
6451c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
6461c7c3c6aSMatthew Dillon 		 * for consistancy since we've removed the object from its
6471c7c3c6aSMatthew Dillon 		 * queues.
6481c7c3c6aSMatthew Dillon 		 */
6491c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
650c0877f10SJohn Dyson 	}
65126f9a767SRodney W. Grimes 	return;
65226f9a767SRodney W. Grimes }
65326f9a767SRodney W. Grimes 
654df8bae1dSRodney W. Grimes /*
6551c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
6561c7c3c6aSMatthew Dillon  *				the requested page.
6571c7c3c6aSMatthew Dillon  *
6581c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
6591c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
6601c7c3c6aSMatthew Dillon  *
6611c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
6621c7c3c6aSMatthew Dillon  *	store exists before and after the requested page within a reasonable
6631c7c3c6aSMatthew Dillon  *	distance.  We do not try to restrict it to the swap device stripe
6641c7c3c6aSMatthew Dillon  *	(that is handled in getpages/putpages).  It probably isn't worth
6651c7c3c6aSMatthew Dillon  *	doing here.
666df8bae1dSRodney W. Grimes  */
66726f9a767SRodney W. Grimes 
6681c7c3c6aSMatthew Dillon boolean_t
669a316d390SJohn Dyson swap_pager_haspage(object, pindex, before, after)
67024a1cce3SDavid Greenman 	vm_object_t object;
671a316d390SJohn Dyson 	vm_pindex_t pindex;
67224a1cce3SDavid Greenman 	int *before;
67324a1cce3SDavid Greenman 	int *after;
67426f9a767SRodney W. Grimes {
6751c7c3c6aSMatthew Dillon 	daddr_t blk0;
67626f9a767SRodney W. Grimes 
6771c7c3c6aSMatthew Dillon 	/*
6781c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
6791c7c3c6aSMatthew Dillon 	 */
6801c7c3c6aSMatthew Dillon 
6811c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
6821c7c3c6aSMatthew Dillon 
6831c7c3c6aSMatthew Dillon 	if (blk0 & SWAPBLK_NONE) {
6841c7c3c6aSMatthew Dillon 		if (before)
68524a1cce3SDavid Greenman 			*before = 0;
6861c7c3c6aSMatthew Dillon 		if (after)
68724a1cce3SDavid Greenman 			*after = 0;
68826f9a767SRodney W. Grimes 		return (FALSE);
68926f9a767SRodney W. Grimes 	}
69026f9a767SRodney W. Grimes 
69126f9a767SRodney W. Grimes 	/*
6921c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
693e47ed70bSJohn Dyson 	 */
694e47ed70bSJohn Dyson 
6951c7c3c6aSMatthew Dillon 	if (before != NULL) {
69626f9a767SRodney W. Grimes 		int i;
6970d94caffSDavid Greenman 
6981c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
6991c7c3c6aSMatthew Dillon 			daddr_t blk;
7001c7c3c6aSMatthew Dillon 
7011c7c3c6aSMatthew Dillon 			if (i > pindex)
7021c7c3c6aSMatthew Dillon 				break;
7031c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
7041c7c3c6aSMatthew Dillon 			if (blk & SWAPBLK_NONE)
7051c7c3c6aSMatthew Dillon 				break;
7061c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
7071c7c3c6aSMatthew Dillon 				break;
708ffc82b0aSJohn Dyson 		}
7091c7c3c6aSMatthew Dillon 		*before = (i - 1);
71026f9a767SRodney W. Grimes 	}
71126f9a767SRodney W. Grimes 
71226f9a767SRodney W. Grimes 	/*
7131c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
71426f9a767SRodney W. Grimes 	 */
7151c7c3c6aSMatthew Dillon 
7161c7c3c6aSMatthew Dillon 	if (after != NULL) {
7171c7c3c6aSMatthew Dillon 		int i;
7181c7c3c6aSMatthew Dillon 
7191c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
7201c7c3c6aSMatthew Dillon 			daddr_t blk;
7211c7c3c6aSMatthew Dillon 
7221c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
7231c7c3c6aSMatthew Dillon 			if (blk & SWAPBLK_NONE)
7241c7c3c6aSMatthew Dillon 				break;
7251c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
7261c7c3c6aSMatthew Dillon 				break;
72726f9a767SRodney W. Grimes 		}
7281c7c3c6aSMatthew Dillon 		*after = (i - 1);
7291c7c3c6aSMatthew Dillon 	}
7301c7c3c6aSMatthew Dillon 
7311c7c3c6aSMatthew Dillon 	return (TRUE);
7321c7c3c6aSMatthew Dillon }
7331c7c3c6aSMatthew Dillon 
7341c7c3c6aSMatthew Dillon /*
7351c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
7361c7c3c6aSMatthew Dillon  *
7371c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
7381c7c3c6aSMatthew Dillon  *	not, from the page.
7391c7c3c6aSMatthew Dillon  *
7401c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
7411c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
7421c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
7431c7c3c6aSMatthew Dillon  *
7441c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
7451c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
7461c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
7471c7c3c6aSMatthew Dillon  *	depends on it.
7481c7c3c6aSMatthew Dillon  *
7491c7c3c6aSMatthew Dillon  *	This routine may not block
7501c7c3c6aSMatthew Dillon  */
7511c7c3c6aSMatthew Dillon 
7521c7c3c6aSMatthew Dillon static void
7531c7c3c6aSMatthew Dillon swap_pager_unswapped(m)
7541c7c3c6aSMatthew Dillon 	vm_page_t m;
7551c7c3c6aSMatthew Dillon {
7561c7c3c6aSMatthew Dillon 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
7571c7c3c6aSMatthew Dillon }
7581c7c3c6aSMatthew Dillon 
7591c7c3c6aSMatthew Dillon /*
7601c7c3c6aSMatthew Dillon  * SWAP_PAGER_GETPAGES() - bring pages in from swap
7611c7c3c6aSMatthew Dillon  *
7621c7c3c6aSMatthew Dillon  *	Attempt to retrieve (m, count) pages from backing store, but make
7631c7c3c6aSMatthew Dillon  *	sure we retrieve at least m[reqpage].  We try to load in as large
7641c7c3c6aSMatthew Dillon  *	a chunk surrounding m[reqpage] as is contiguous in swap and which
7651c7c3c6aSMatthew Dillon  *	belongs to the same object.
7661c7c3c6aSMatthew Dillon  *
7671c7c3c6aSMatthew Dillon  *	The code is designed for asynchronous operation and
7681c7c3c6aSMatthew Dillon  *	immediate-notification of 'reqpage' but tends not to be
7691c7c3c6aSMatthew Dillon  *	used that way.  Please do not optimize-out this algorithmic
7701c7c3c6aSMatthew Dillon  *	feature, I intend to improve on it in the future.
7711c7c3c6aSMatthew Dillon  *
7721c7c3c6aSMatthew Dillon  *	The parent has a single vm_object_pip_add() reference prior to
7731c7c3c6aSMatthew Dillon  *	calling us and we should return with the same.
7741c7c3c6aSMatthew Dillon  *
7751c7c3c6aSMatthew Dillon  *	The parent has BUSY'd the pages.  We should return with 'm'
7761c7c3c6aSMatthew Dillon  *	left busy, but the others adjusted.
7771c7c3c6aSMatthew Dillon  */
77826f9a767SRodney W. Grimes 
779f708ef1bSPoul-Henning Kamp static int
78024a1cce3SDavid Greenman swap_pager_getpages(object, m, count, reqpage)
78124a1cce3SDavid Greenman 	vm_object_t object;
78226f9a767SRodney W. Grimes 	vm_page_t *m;
78326f9a767SRodney W. Grimes 	int count, reqpage;
784df8bae1dSRodney W. Grimes {
7851c7c3c6aSMatthew Dillon 	struct buf *bp;
7861c7c3c6aSMatthew Dillon 	vm_page_t mreq;
7871c7c3c6aSMatthew Dillon 	int s;
78826f9a767SRodney W. Grimes 	int i;
78926f9a767SRodney W. Grimes 	int j;
7901c7c3c6aSMatthew Dillon 	daddr_t blk;
7911c7c3c6aSMatthew Dillon 	vm_offset_t kva;
7921c7c3c6aSMatthew Dillon 	vm_pindex_t lastpindex;
7930d94caffSDavid Greenman 
7941c7c3c6aSMatthew Dillon 	mreq = m[reqpage];
7951c7c3c6aSMatthew Dillon 
7961c7c3c6aSMatthew Dillon #if !defined(MAX_PERF)
7971c7c3c6aSMatthew Dillon 	if (mreq->object != object) {
7981c7c3c6aSMatthew Dillon 		panic("swap_pager_getpages: object mismatch %p/%p",
7991c7c3c6aSMatthew Dillon 		    object,
8001c7c3c6aSMatthew Dillon 		    mreq->object
8011c7c3c6aSMatthew Dillon 		);
80226f9a767SRodney W. Grimes 	}
8031c7c3c6aSMatthew Dillon #endif
8041c7c3c6aSMatthew Dillon 	/*
8051c7c3c6aSMatthew Dillon 	 * Calculate range to retrieve.  The pages have already been assigned
8061c7c3c6aSMatthew Dillon 	 * their swapblks.  We require a *contiguous* range that falls entirely
8071c7c3c6aSMatthew Dillon 	 * within a single device stripe.   If we do not supply it, bad things
8081c7c3c6aSMatthew Dillon 	 * happen.
8091c7c3c6aSMatthew Dillon 	 */
8101c7c3c6aSMatthew Dillon 
8111c7c3c6aSMatthew Dillon 
8121c7c3c6aSMatthew Dillon 	blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0);
8131c7c3c6aSMatthew Dillon 
8141c7c3c6aSMatthew Dillon 	for (i = reqpage - 1; i >= 0; --i) {
8151c7c3c6aSMatthew Dillon 		daddr_t iblk;
8161c7c3c6aSMatthew Dillon 
8171c7c3c6aSMatthew Dillon 		iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0);
8181c7c3c6aSMatthew Dillon 		if (iblk & SWAPBLK_NONE)
8191c7c3c6aSMatthew Dillon 			break;
8201c7c3c6aSMatthew Dillon 
8211c7c3c6aSMatthew Dillon 		if ((blk ^ iblk) & dmmax_mask)
8221c7c3c6aSMatthew Dillon 			break;
8231c7c3c6aSMatthew Dillon 
8241c7c3c6aSMatthew Dillon 		if (blk != iblk + (reqpage - i))
82526f9a767SRodney W. Grimes 			break;
82626f9a767SRodney W. Grimes 	}
8271c7c3c6aSMatthew Dillon 	++i;
8281c7c3c6aSMatthew Dillon 
8291c7c3c6aSMatthew Dillon 	for (j = reqpage + 1; j < count; ++j) {
8301c7c3c6aSMatthew Dillon 		daddr_t jblk;
8311c7c3c6aSMatthew Dillon 
8321c7c3c6aSMatthew Dillon 		jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0);
8331c7c3c6aSMatthew Dillon 		if (jblk & SWAPBLK_NONE)
8341c7c3c6aSMatthew Dillon 			break;
8351c7c3c6aSMatthew Dillon 
8361c7c3c6aSMatthew Dillon 		if ((blk ^ jblk) & dmmax_mask)
8371c7c3c6aSMatthew Dillon 			break;
8381c7c3c6aSMatthew Dillon 
8391c7c3c6aSMatthew Dillon 		if (blk != jblk - (j - reqpage))
8401c7c3c6aSMatthew Dillon 			break;
84126f9a767SRodney W. Grimes 	}
84226f9a767SRodney W. Grimes 
8431c7c3c6aSMatthew Dillon 	/*
8441c7c3c6aSMatthew Dillon 	 * If blk itself is bad, well, we can't do any I/O.  This should
8451c7c3c6aSMatthew Dillon 	 * already be covered as a side effect, but I'm making sure.
8461c7c3c6aSMatthew Dillon 	 */
84726f9a767SRodney W. Grimes 
8481c7c3c6aSMatthew Dillon 	if (blk & SWAPBLK_NONE) {
8491c7c3c6aSMatthew Dillon 		i = reqpage;
8501c7c3c6aSMatthew Dillon 		j = reqpage + 1;
8511c7c3c6aSMatthew Dillon 	}
8521c7c3c6aSMatthew Dillon 
8531c7c3c6aSMatthew Dillon 	/*
8541c7c3c6aSMatthew Dillon 	 * free pages outside our collection range.   Note: we never free
8551c7c3c6aSMatthew Dillon 	 * mreq, it must remain busy throughout.
8561c7c3c6aSMatthew Dillon 	 */
8571c7c3c6aSMatthew Dillon 
8581c7c3c6aSMatthew Dillon 	{
8591c7c3c6aSMatthew Dillon 		int k;
8601c7c3c6aSMatthew Dillon 
8611c7c3c6aSMatthew Dillon 		for (k = 0; k < i; ++k) {
8621c7c3c6aSMatthew Dillon 			vm_page_free(m[k]);
8631c7c3c6aSMatthew Dillon 		}
8641c7c3c6aSMatthew Dillon 		for (k = j; k < count; ++k) {
8651c7c3c6aSMatthew Dillon 			vm_page_free(m[k]);
8661c7c3c6aSMatthew Dillon 		}
8671c7c3c6aSMatthew Dillon 	}
8681c7c3c6aSMatthew Dillon 
8691c7c3c6aSMatthew Dillon 	/*
8701c7c3c6aSMatthew Dillon 	 * Return VM_PAGER_FAIL if we have nothing
8711c7c3c6aSMatthew Dillon 	 * to do.  Return mreq still busy, but the
8721c7c3c6aSMatthew Dillon 	 * others unbusied.
8731c7c3c6aSMatthew Dillon 	 */
8741c7c3c6aSMatthew Dillon 
8751c7c3c6aSMatthew Dillon 	if (blk & SWAPBLK_NONE)
87626f9a767SRodney W. Grimes 		return(VM_PAGER_FAIL);
877df8bae1dSRodney W. Grimes 
87826f9a767SRodney W. Grimes 
87916f62314SDavid Greenman 	/*
88016f62314SDavid Greenman 	 * Get a swap buffer header to perform the IO
88116f62314SDavid Greenman 	 */
8821c7c3c6aSMatthew Dillon 
8831c7c3c6aSMatthew Dillon 	bp = getpbuf(&nsw_rcount);
88416f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
88526f9a767SRodney W. Grimes 
88616f62314SDavid Greenman 	/*
88716f62314SDavid Greenman 	 * map our page(s) into kva for input
8881c7c3c6aSMatthew Dillon 	 *
8891c7c3c6aSMatthew Dillon 	 * NOTE: B_PAGING is set by pbgetvp()
89016f62314SDavid Greenman 	 */
89116f62314SDavid Greenman 
8921c7c3c6aSMatthew Dillon 	pmap_qenter(kva, m + i, j - i);
8931c7c3c6aSMatthew Dillon 
8941c7c3c6aSMatthew Dillon 	bp->b_flags = B_BUSY | B_READ | B_CALL;
8951c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
896df8bae1dSRodney W. Grimes 	bp->b_proc = &proc0;	/* XXX (but without B_PHYS set this is ok) */
89726f9a767SRodney W. Grimes 	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
89826f9a767SRodney W. Grimes 	crhold(bp->b_rcred);
89926f9a767SRodney W. Grimes 	crhold(bp->b_wcred);
900ab3f7469SPoul-Henning Kamp 	bp->b_data = (caddr_t) kva;
9011c7c3c6aSMatthew Dillon 	/*
9021c7c3c6aSMatthew Dillon 	 * b_blkno is in page-sized chunks.  swapblk is valid, too, so
9031c7c3c6aSMatthew Dillon 	 * we don't have to mask it against SWAPBLK_MASK.
9041c7c3c6aSMatthew Dillon 	 */
9051c7c3c6aSMatthew Dillon 	bp->b_blkno = blk - (reqpage - i);
9061c7c3c6aSMatthew Dillon 	bp->b_bcount = PAGE_SIZE * (j - i);
9071c7c3c6aSMatthew Dillon 	bp->b_bufsize = PAGE_SIZE * (j - i);
9081c7c3c6aSMatthew Dillon 	bp->b_pager.pg_reqpage = reqpage - i;
9091c7c3c6aSMatthew Dillon 
9101c7c3c6aSMatthew Dillon 	{
9111c7c3c6aSMatthew Dillon 		int k;
9121c7c3c6aSMatthew Dillon 
9131c7c3c6aSMatthew Dillon 		for (k = i; k < j; ++k) {
9141c7c3c6aSMatthew Dillon 			bp->b_pages[k - i] = m[k];
9151c7c3c6aSMatthew Dillon 			vm_page_flag_set(m[k], PG_SWAPINPROG);
9161c7c3c6aSMatthew Dillon 		}
9171c7c3c6aSMatthew Dillon 	}
9181c7c3c6aSMatthew Dillon 	bp->b_npages = j - i;
91926f9a767SRodney W. Grimes 
9200d94caffSDavid Greenman 	pbgetvp(swapdev_vp, bp);
921df8bae1dSRodney W. Grimes 
922976e77fcSDavid Greenman 	cnt.v_swapin++;
9231c7c3c6aSMatthew Dillon 	cnt.v_swappgsin += bp->b_npages;
9241c7c3c6aSMatthew Dillon 
925df8bae1dSRodney W. Grimes 	/*
9261c7c3c6aSMatthew Dillon 	 * We still hold the lock on mreq, and our automatic completion routine
9271c7c3c6aSMatthew Dillon 	 * does not remove it.
928df8bae1dSRodney W. Grimes 	 */
9291c7c3c6aSMatthew Dillon 
9301c7c3c6aSMatthew Dillon 	vm_object_pip_add(mreq->object, bp->b_npages);
9311c7c3c6aSMatthew Dillon 	lastpindex = m[j-1]->pindex;
9321c7c3c6aSMatthew Dillon 
9331c7c3c6aSMatthew Dillon 	/*
9341c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
9351c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
9361c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
9371c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
9381c7c3c6aSMatthew Dillon 	 *
9391c7c3c6aSMatthew Dillon 	 * The other pages in our m[] array are also released on completion,
9401c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
9411c7c3c6aSMatthew Dillon 	 *
9421c7c3c6aSMatthew Dillon 	 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
9431c7c3c6aSMatthew Dillon 	 */
9441c7c3c6aSMatthew Dillon 
945fd5d1124SJulian Elischer 	VOP_STRATEGY(bp->b_vp, bp);
94626f9a767SRodney W. Grimes 
94726f9a767SRodney W. Grimes 	/*
9481c7c3c6aSMatthew Dillon 	 * wait for the page we want to complete.  PG_SWAPINPROG is always
9491c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
9501c7c3c6aSMatthew Dillon 	 * is set in the meta-data.
95126f9a767SRodney W. Grimes 	 */
9521b119d9dSDavid Greenman 
9531c7c3c6aSMatthew Dillon 	s = splvm();
9541c7c3c6aSMatthew Dillon 
9551c7c3c6aSMatthew Dillon 	while ((mreq->flags & PG_SWAPINPROG) != 0) {
9561c7c3c6aSMatthew Dillon 		vm_page_flag_set(mreq, PG_WANTED | PG_REFERENCED);
9571c7c3c6aSMatthew Dillon 		cnt.v_intrans++;
9581c7c3c6aSMatthew Dillon 		if (tsleep(mreq, PSWP, "swread", hz*20)) {
959ac1e407bSBruce Evans 			printf(
9601c7c3c6aSMatthew Dillon 			    "swap_pager: indefinite wait buffer: device:"
9611c7c3c6aSMatthew Dillon 				" %#lx, blkno: %ld, size: %ld\n",
9621c7c3c6aSMatthew Dillon 			    (u_long)bp->b_dev, (long)bp->b_blkno,
9631c7c3c6aSMatthew Dillon 			    (long)bp->b_bcount
9641c7c3c6aSMatthew Dillon 			);
9651c7c3c6aSMatthew Dillon 		}
9661b119d9dSDavid Greenman 	}
96726f9a767SRodney W. Grimes 
968df8bae1dSRodney W. Grimes 	splx(s);
96926f9a767SRodney W. Grimes 
97026f9a767SRodney W. Grimes 	/*
9711c7c3c6aSMatthew Dillon 	 * mreq is left bussied after completion, but all the other pages
9721c7c3c6aSMatthew Dillon 	 * are freed.  If we had an unrecoverable read error the page will
9731c7c3c6aSMatthew Dillon 	 * not be valid.
97426f9a767SRodney W. Grimes 	 */
97526f9a767SRodney W. Grimes 
9761c7c3c6aSMatthew Dillon 	if (mreq->valid != VM_PAGE_BITS_ALL) {
9771c7c3c6aSMatthew Dillon 		return(VM_PAGER_ERROR);
97826f9a767SRodney W. Grimes 	} else {
9791c7c3c6aSMatthew Dillon 		mreq->object->last_read = lastpindex;
9801c7c3c6aSMatthew Dillon 		return(VM_PAGER_OK);
98126f9a767SRodney W. Grimes 	}
9821c7c3c6aSMatthew Dillon 
9831c7c3c6aSMatthew Dillon 	/*
9841c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
9851c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
9861c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
9871c7c3c6aSMatthew Dillon 	 * to all-zero's later.
9881c7c3c6aSMatthew Dillon 	 */
989df8bae1dSRodney W. Grimes }
990df8bae1dSRodney W. Grimes 
9911c7c3c6aSMatthew Dillon /*
9921c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
9931c7c3c6aSMatthew Dillon  *
9941c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
9951c7c3c6aSMatthew Dillon  *
9961c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
9971c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
9981c7c3c6aSMatthew Dillon  *
9991c7c3c6aSMatthew Dillon  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
10001c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
10011c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
10021c7c3c6aSMatthew Dillon  *	which needs work.
10031c7c3c6aSMatthew Dillon  *
10041c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
10051c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
10061c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
10071c7c3c6aSMatthew Dillon  *	completion.
10081c7c3c6aSMatthew Dillon  *
10091c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
10101c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
10111c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
10121c7c3c6aSMatthew Dillon  */
10131c7c3c6aSMatthew Dillon 
1014e4542174SMatthew Dillon void
101524a1cce3SDavid Greenman swap_pager_putpages(object, m, count, sync, rtvals)
101624a1cce3SDavid Greenman 	vm_object_t object;
101726f9a767SRodney W. Grimes 	vm_page_t *m;
101826f9a767SRodney W. Grimes 	int count;
101924a1cce3SDavid Greenman 	boolean_t sync;
102026f9a767SRodney W. Grimes 	int *rtvals;
1021df8bae1dSRodney W. Grimes {
10221c7c3c6aSMatthew Dillon 	int i;
10231c7c3c6aSMatthew Dillon 	int n = 0;
1024df8bae1dSRodney W. Grimes 
10251c7c3c6aSMatthew Dillon #if !defined(MAX_PERF)
10261c7c3c6aSMatthew Dillon 	if (count && m[0]->object != object) {
10271c7c3c6aSMatthew Dillon 		panic("swap_pager_getpages: object mismatch %p/%p",
10281c7c3c6aSMatthew Dillon 		    object,
10291c7c3c6aSMatthew Dillon 		    m[0]->object
10301c7c3c6aSMatthew Dillon 		);
10311c7c3c6aSMatthew Dillon 	}
10321c7c3c6aSMatthew Dillon #endif
10331c7c3c6aSMatthew Dillon 	/*
10341c7c3c6aSMatthew Dillon 	 * Step 1
10351c7c3c6aSMatthew Dillon 	 *
10361c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
10371c7c3c6aSMatthew Dillon 	 * check for bogus sysops
10381c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
10391c7c3c6aSMatthew Dillon 	 */
1040e736cd05SJohn Dyson 
10411c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
10421c7c3c6aSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE, 0);
10435663e6deSDavid Greenman 	}
1044e47ed70bSJohn Dyson 
1045e47ed70bSJohn Dyson 	if (curproc != pageproc)
1046e47ed70bSJohn Dyson 		sync = TRUE;
104726f9a767SRodney W. Grimes 
10481c7c3c6aSMatthew Dillon 	/*
10491c7c3c6aSMatthew Dillon 	 * Step 2
10501c7c3c6aSMatthew Dillon 	 *
1051327f4e83SMatthew Dillon 	 * Update nsw parameters from swap_async_max and swap_cluster_max
1052327f4e83SMatthew Dillon 	 * sysctl values.  Do not let the sysop crash the machine with bogus
1053327f4e83SMatthew Dillon 	 * numbers.
1054327f4e83SMatthew Dillon 	 */
1055327f4e83SMatthew Dillon 
1056327f4e83SMatthew Dillon #ifndef DISALLOW_SWAP_TUNE
1057327f4e83SMatthew Dillon 
1058327f4e83SMatthew Dillon 	if (swap_async_max != nsw_wcount_async_max) {
1059327f4e83SMatthew Dillon 		int n;
1060327f4e83SMatthew Dillon 		int s;
1061327f4e83SMatthew Dillon 
1062327f4e83SMatthew Dillon 		/*
1063327f4e83SMatthew Dillon 		 * limit range
1064327f4e83SMatthew Dillon 		 */
1065327f4e83SMatthew Dillon 		if ((n = swap_async_max) > nswbuf / 2)
1066327f4e83SMatthew Dillon 			n = nswbuf / 2;
1067327f4e83SMatthew Dillon 		if (n < 1)
1068327f4e83SMatthew Dillon 			n = 1;
1069327f4e83SMatthew Dillon 		swap_async_max = n;
1070327f4e83SMatthew Dillon 
1071327f4e83SMatthew Dillon 		/*
1072327f4e83SMatthew Dillon 		 * Adjust difference ( if possible ).  If the current async
1073327f4e83SMatthew Dillon 		 * count is too low, we may not be able to make the adjustment
1074327f4e83SMatthew Dillon 		 * at this time.
1075327f4e83SMatthew Dillon 		 */
1076327f4e83SMatthew Dillon 		s = splvm();
1077327f4e83SMatthew Dillon 		n -= nsw_wcount_async_max;
1078327f4e83SMatthew Dillon 		if (nsw_wcount_async + n >= 0) {
1079327f4e83SMatthew Dillon 			nsw_wcount_async += n;
1080327f4e83SMatthew Dillon 			nsw_wcount_async_max += n;
1081327f4e83SMatthew Dillon 			wakeup(&nsw_wcount_async);
1082327f4e83SMatthew Dillon 		}
1083327f4e83SMatthew Dillon 		splx(s);
1084327f4e83SMatthew Dillon 	}
1085327f4e83SMatthew Dillon 
1086327f4e83SMatthew Dillon 	if (swap_cluster_max != nsw_cluster_max) {
1087327f4e83SMatthew Dillon 		int n;
1088327f4e83SMatthew Dillon 
1089327f4e83SMatthew Dillon 		if ((n = swap_cluster_max) < 1)
1090327f4e83SMatthew Dillon 			n = 1;
1091327f4e83SMatthew Dillon 		if (n > min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER))
1092327f4e83SMatthew Dillon 			n = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
1093327f4e83SMatthew Dillon 		swap_cluster_max = n;
1094327f4e83SMatthew Dillon 		nsw_cluster_max = n;
1095327f4e83SMatthew Dillon 	}
1096327f4e83SMatthew Dillon 
1097327f4e83SMatthew Dillon #endif
1098327f4e83SMatthew Dillon 
1099327f4e83SMatthew Dillon 	/*
1100327f4e83SMatthew Dillon 	 * Step 3
1101327f4e83SMatthew Dillon 	 *
11021c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
11031c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
11041c7c3c6aSMatthew Dillon 	 * successfully.
11051c7c3c6aSMatthew Dillon 	 */
110626f9a767SRodney W. Grimes 
11071c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
11081c7c3c6aSMatthew Dillon 		int s;
11091c7c3c6aSMatthew Dillon 		int j;
11101c7c3c6aSMatthew Dillon 		struct buf *bp;
1111a316d390SJohn Dyson 		daddr_t blk;
111226f9a767SRodney W. Grimes 
1113df8bae1dSRodney W. Grimes 		/*
11141c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1115df8bae1dSRodney W. Grimes 		 */
111626f9a767SRodney W. Grimes 
11171c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1118327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
11191c7c3c6aSMatthew Dillon 
112026f9a767SRodney W. Grimes 		/*
11211c7c3c6aSMatthew Dillon 		 * Get biggest block of swap we can.  If we fail, fall
11221c7c3c6aSMatthew Dillon 		 * back and try to allocate a smaller block.  Don't go
11231c7c3c6aSMatthew Dillon 		 * overboard trying to allocate space if it would overly
11241c7c3c6aSMatthew Dillon 		 * fragment swap.
112526f9a767SRodney W. Grimes 		 */
11261c7c3c6aSMatthew Dillon 		while (
11271c7c3c6aSMatthew Dillon 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
11281c7c3c6aSMatthew Dillon 		    n > 4
11291c7c3c6aSMatthew Dillon 		) {
11301c7c3c6aSMatthew Dillon 			n >>= 1;
113126f9a767SRodney W. Grimes 		}
11321c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
11331c7c3c6aSMatthew Dillon 			for (j = 0; j < n; ++j) {
11341c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
113526f9a767SRodney W. Grimes 			}
11361c7c3c6aSMatthew Dillon 			continue;
113726f9a767SRodney W. Grimes 		}
113826f9a767SRodney W. Grimes 
113926f9a767SRodney W. Grimes 		/*
11401c7c3c6aSMatthew Dillon 		 * Oops, too big if it crosses a stripe
11411c7c3c6aSMatthew Dillon 		 *
11421c7c3c6aSMatthew Dillon 		 * 1111000000
11431c7c3c6aSMatthew Dillon 		 *     111111
11441c7c3c6aSMatthew Dillon 		 *    1000001
114526f9a767SRodney W. Grimes 		 */
11461c7c3c6aSMatthew Dillon 		if ((blk ^ (blk + n)) & dmmax_mask) {
11471c7c3c6aSMatthew Dillon 			j = ((blk + dmmax) & dmmax_mask) - blk;
11481c7c3c6aSMatthew Dillon 			swp_pager_freeswapspace(blk + j, n - j);
11491c7c3c6aSMatthew Dillon 			n = j;
1150e47ed70bSJohn Dyson 		}
115126f9a767SRodney W. Grimes 
115226f9a767SRodney W. Grimes 		/*
11531c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
11541c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
11551c7c3c6aSMatthew Dillon 		 *
11561c7c3c6aSMatthew Dillon 		 * NOTE: B_PAGING is set by pbgetvp()
115726f9a767SRodney W. Grimes 		 */
115826f9a767SRodney W. Grimes 
1159327f4e83SMatthew Dillon 		if (sync == TRUE) {
1160327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_sync);
1161327f4e83SMatthew Dillon 			bp->b_flags = B_BUSY;
1162327f4e83SMatthew Dillon 		} else {
1163327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_async);
1164327f4e83SMatthew Dillon 			bp->b_flags = B_BUSY | B_ASYNC;
1165327f4e83SMatthew Dillon 		}
11661c7c3c6aSMatthew Dillon 		bp->b_spc = NULL;	/* not used, but NULL-out anyway */
116726f9a767SRodney W. Grimes 
11681c7c3c6aSMatthew Dillon 		pmap_qenter((vm_offset_t)bp->b_data, &m[i], n);
11691c7c3c6aSMatthew Dillon 
11701c7c3c6aSMatthew Dillon 		bp->b_proc = &proc0; /* XXX (but without B_PHYS this is ok) */
117126f9a767SRodney W. Grimes 		bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
11721c7c3c6aSMatthew Dillon 
1173a481f200SDavid Greenman 		if (bp->b_rcred != NOCRED)
117426f9a767SRodney W. Grimes 			crhold(bp->b_rcred);
1175a481f200SDavid Greenman 		if (bp->b_wcred != NOCRED)
117626f9a767SRodney W. Grimes 			crhold(bp->b_wcred);
11770d94caffSDavid Greenman 		pbgetvp(swapdev_vp, bp);
117816f62314SDavid Greenman 
11791c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
11801c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
11811c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1182e47ed70bSJohn Dyson 
1183e47ed70bSJohn Dyson 		s = splvm();
11841c7c3c6aSMatthew Dillon 
11851c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
11861c7c3c6aSMatthew Dillon 			vm_page_t mreq = m[i+j];
11871c7c3c6aSMatthew Dillon 
11881c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
11891c7c3c6aSMatthew Dillon 			    mreq->object,
11901c7c3c6aSMatthew Dillon 			    mreq->pindex,
11911c7c3c6aSMatthew Dillon 			    blk + j,
11921c7c3c6aSMatthew Dillon 			    0
11931c7c3c6aSMatthew Dillon 			);
11947dbf82dcSMatthew Dillon 			vm_page_dirty(mreq);
11951c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_OK;
11961c7c3c6aSMatthew Dillon 
11971c7c3c6aSMatthew Dillon 			vm_page_flag_set(mreq, PG_SWAPINPROG);
11981c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
11991c7c3c6aSMatthew Dillon 		}
12001c7c3c6aSMatthew Dillon 		bp->b_flags |= B_CALL;
12011c7c3c6aSMatthew Dillon 		bp->b_npages = n;
12021c7c3c6aSMatthew Dillon 
12031c7c3c6aSMatthew Dillon 		cnt.v_swapout++;
12041c7c3c6aSMatthew Dillon 		cnt.v_swappgsout += bp->b_npages;
120526f9a767SRodney W. Grimes 		swapdev_vp->v_numoutput++;
120626f9a767SRodney W. Grimes 
120726f9a767SRodney W. Grimes 		/*
12081c7c3c6aSMatthew Dillon 		 * asynchronous
12091c7c3c6aSMatthew Dillon 		 *
12101c7c3c6aSMatthew Dillon 		 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
121126f9a767SRodney W. Grimes 		 */
1212e47ed70bSJohn Dyson 
12131c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
12141c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
121526f9a767SRodney W. Grimes 			bp->b_dirtyoff = 0;
121626f9a767SRodney W. Grimes 			bp->b_dirtyend = bp->b_bcount;
1217fd5d1124SJulian Elischer 			VOP_STRATEGY(bp->b_vp, bp);
12181c7c3c6aSMatthew Dillon 
12191c7c3c6aSMatthew Dillon 			for (j = 0; j < n; ++j)
12201c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_PEND;
12211c7c3c6aSMatthew Dillon 
1222ccbbd927SBruce Evans 			splx(s);
12231c7c3c6aSMatthew Dillon 			continue;
122426f9a767SRodney W. Grimes 		}
1225e47ed70bSJohn Dyson 
122626f9a767SRodney W. Grimes 		/*
12271c7c3c6aSMatthew Dillon 		 * synchronous
12281c7c3c6aSMatthew Dillon 		 *
12291c7c3c6aSMatthew Dillon 		 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
12301c7c3c6aSMatthew Dillon 		 */
12311c7c3c6aSMatthew Dillon 
12321c7c3c6aSMatthew Dillon 		bp->b_iodone = swp_pager_sync_iodone;
12331c7c3c6aSMatthew Dillon 		VOP_STRATEGY(bp->b_vp, bp);
12341c7c3c6aSMatthew Dillon 
12351c7c3c6aSMatthew Dillon 		/*
12361c7c3c6aSMatthew Dillon 		 * Wait for the sync I/O to complete, then update rtvals.
12371c7c3c6aSMatthew Dillon 		 * We just set the rtvals[] to VM_PAGER_PEND so we can call
12381c7c3c6aSMatthew Dillon 		 * our async completion routine at the end, thus avoiding a
12391c7c3c6aSMatthew Dillon 		 * double-free.
124026f9a767SRodney W. Grimes 		 */
124126f9a767SRodney W. Grimes 		while ((bp->b_flags & B_DONE) == 0) {
124224a1cce3SDavid Greenman 			tsleep(bp, PVM, "swwrt", 0);
124326f9a767SRodney W. Grimes 		}
1244e47ed70bSJohn Dyson 
1245e4542174SMatthew Dillon #if 0
12461b119d9dSDavid Greenman 		if (bp->b_flags & B_ERROR) {
12471c7c3c6aSMatthew Dillon 			grv = VM_PAGER_ERROR;
12481b119d9dSDavid Greenman 		}
1249e4542174SMatthew Dillon #endif
125026f9a767SRodney W. Grimes 
12511c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j)
12521c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_PEND;
125326f9a767SRodney W. Grimes 
1254e4542174SMatthew Dillon #if 0
12551c7c3c6aSMatthew Dillon 		if (bp->b_flags & B_ERROR) {
12561c7c3c6aSMatthew Dillon 			grv = VM_PAGER_ERROR;
12571c7c3c6aSMatthew Dillon 		}
1258e4542174SMatthew Dillon #endif
12591c7c3c6aSMatthew Dillon 
12601c7c3c6aSMatthew Dillon 		/*
12611c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
12621c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
12631c7c3c6aSMatthew Dillon 		 */
12641c7c3c6aSMatthew Dillon 
12651c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
126626f9a767SRodney W. Grimes 
126726f9a767SRodney W. Grimes 		splx(s);
12681c7c3c6aSMatthew Dillon 	}
12691c7c3c6aSMatthew Dillon }
12701c7c3c6aSMatthew Dillon 
12711c7c3c6aSMatthew Dillon /*
12721c7c3c6aSMatthew Dillon  *	swap_pager_sync_iodone:
12731c7c3c6aSMatthew Dillon  *
12741c7c3c6aSMatthew Dillon  *	Completion routine for synchronous reads and writes from/to swap.
12751c7c3c6aSMatthew Dillon  *	We just mark the bp is complete and wake up anyone waiting on it.
12761c7c3c6aSMatthew Dillon  *
12771c7c3c6aSMatthew Dillon  *	This routine may not block.
12781c7c3c6aSMatthew Dillon  */
12791c7c3c6aSMatthew Dillon 
12801c7c3c6aSMatthew Dillon static void
12811c7c3c6aSMatthew Dillon swp_pager_sync_iodone(bp)
12821c7c3c6aSMatthew Dillon 	struct buf *bp;
12831c7c3c6aSMatthew Dillon {
12841c7c3c6aSMatthew Dillon 	bp->b_flags |= B_DONE;
12851c7c3c6aSMatthew Dillon 	bp->b_flags &= ~B_ASYNC;
12861c7c3c6aSMatthew Dillon 	wakeup(bp);
12871c7c3c6aSMatthew Dillon }
12881c7c3c6aSMatthew Dillon 
12891c7c3c6aSMatthew Dillon /*
12901c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
12911c7c3c6aSMatthew Dillon  *
12921c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
12931c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
12941c7c3c6aSMatthew Dillon  *
12951c7c3c6aSMatthew Dillon  *	WARNING!  This routine may be called from an interrupt.  We cannot
12961c7c3c6aSMatthew Dillon  *	mess with swap metadata unless we want to run all our other routines
12971c7c3c6aSMatthew Dillon  *	at splbio() too, which I'd rather not do.  We up ourselves
12981c7c3c6aSMatthew Dillon  * 	to splvm() because we may call vm_page_free(), which can unlink a
12991c7c3c6aSMatthew Dillon  *	page from an object.
13001c7c3c6aSMatthew Dillon  *
13011c7c3c6aSMatthew Dillon  *	XXX currently I do not believe any object routines protect
13021c7c3c6aSMatthew Dillon  *	object->memq at splvm().  The code must be gone over to determine
13031c7c3c6aSMatthew Dillon  *	the actual state of the problem.
13041c7c3c6aSMatthew Dillon  *
13051c7c3c6aSMatthew Dillon  *	For READ operations, the pages are PG_BUSY'd.  For WRITE operations,
13061c7c3c6aSMatthew Dillon  *	the pages are vm_page_t->busy'd.  For READ operations, we PG_BUSY
13071c7c3c6aSMatthew Dillon  *	unbusy all pages except the 'main' request page.  For WRITE
13081c7c3c6aSMatthew Dillon  *	operations, we vm_page_t->busy'd unbusy all pages ( we can do this
13091c7c3c6aSMatthew Dillon  *	because we marked them all VM_PAGER_PEND on return from putpages ).
13101c7c3c6aSMatthew Dillon  *
13111c7c3c6aSMatthew Dillon  *	This routine may not block.
13121c7c3c6aSMatthew Dillon  *	This routine is called at splbio()
13131c7c3c6aSMatthew Dillon  */
13141c7c3c6aSMatthew Dillon 
13151c7c3c6aSMatthew Dillon static void
13161c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp)
13171c7c3c6aSMatthew Dillon 	register struct buf *bp;
13181c7c3c6aSMatthew Dillon {
13191c7c3c6aSMatthew Dillon 	int s;
13201c7c3c6aSMatthew Dillon 	int i;
13211c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
13221c7c3c6aSMatthew Dillon 
13231c7c3c6aSMatthew Dillon 	s = splvm();
13241c7c3c6aSMatthew Dillon 
13251c7c3c6aSMatthew Dillon 	bp->b_flags |= B_DONE;
13261c7c3c6aSMatthew Dillon 
13271c7c3c6aSMatthew Dillon 	/*
13281c7c3c6aSMatthew Dillon 	 * report error
13291c7c3c6aSMatthew Dillon 	 */
13301c7c3c6aSMatthew Dillon 
13311c7c3c6aSMatthew Dillon 	if (bp->b_flags & B_ERROR) {
13321c7c3c6aSMatthew Dillon 		printf(
13331c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
13341c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
13351c7c3c6aSMatthew Dillon 		    ((bp->b_flags & B_READ) ? "pagein" : "pageout"),
13361c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
13371c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
13381c7c3c6aSMatthew Dillon 		    bp->b_error
13391c7c3c6aSMatthew Dillon 		);
13401c7c3c6aSMatthew Dillon 	}
13411c7c3c6aSMatthew Dillon 
13421c7c3c6aSMatthew Dillon 	/*
13431c7c3c6aSMatthew Dillon 	 * set object.
13441c7c3c6aSMatthew Dillon 	 */
13451c7c3c6aSMatthew Dillon 
13461c7c3c6aSMatthew Dillon 	if (bp->b_npages)
13471c7c3c6aSMatthew Dillon 		object = bp->b_pages[0]->object;
134826f9a767SRodney W. Grimes 
134926f9a767SRodney W. Grimes 	/*
135026f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
135126f9a767SRodney W. Grimes 	 */
13521c7c3c6aSMatthew Dillon 
13531c7c3c6aSMatthew Dillon 	pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
135426f9a767SRodney W. Grimes 
135526f9a767SRodney W. Grimes 	/*
13561c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
13571c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
13581c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
13591c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
13601c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
13611c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
136226f9a767SRodney W. Grimes 	 */
136326f9a767SRodney W. Grimes 
13641c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
13651c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1366e47ed70bSJohn Dyson 
13671c7c3c6aSMatthew Dillon 		vm_page_flag_clear(m, PG_SWAPINPROG);
1368e47ed70bSJohn Dyson 
136926f9a767SRodney W. Grimes 		if (bp->b_flags & B_ERROR) {
1370ffc82b0aSJohn Dyson 			/*
13711c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
13721c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
13731c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
13741c7c3c6aSMatthew Dillon 			 * interrupt.
1375ffc82b0aSJohn Dyson 			 */
13761c7c3c6aSMatthew Dillon 
13771c7c3c6aSMatthew Dillon 			if (bp->b_flags & B_READ) {
13781c7c3c6aSMatthew Dillon 				/*
13791c7c3c6aSMatthew Dillon 				 * When reading, reqpage needs to stay
13801c7c3c6aSMatthew Dillon 				 * locked for the parent, but all other
13811c7c3c6aSMatthew Dillon 				 * pages can be freed.  We still want to
13821c7c3c6aSMatthew Dillon 				 * wakeup the parent waiting on the page,
13831c7c3c6aSMatthew Dillon 				 * though.  ( also: pg_reqpage can be -1 and
13841c7c3c6aSMatthew Dillon 				 * not match anything ).
13851c7c3c6aSMatthew Dillon 				 *
13861c7c3c6aSMatthew Dillon 				 * We have to wake specifically requested pages
13871c7c3c6aSMatthew Dillon 				 * up too because we cleared PG_SWAPINPROG and
13881c7c3c6aSMatthew Dillon 				 * someone may be waiting for that.
13891c7c3c6aSMatthew Dillon 				 *
13901c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
13911c7c3c6aSMatthew Dillon 				 * be overriden by the original caller of
13921c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
13931c7c3c6aSMatthew Dillon 				 *
13941c7c3c6aSMatthew Dillon 				 * XXX it may not be legal to free the page
13951c7c3c6aSMatthew Dillon 				 * here as this messes with the object->memq's.
13961c7c3c6aSMatthew Dillon 				 */
13971c7c3c6aSMatthew Dillon 
13981c7c3c6aSMatthew Dillon 				m->valid = 0;
13991c7c3c6aSMatthew Dillon 				vm_page_flag_clear(m, PG_ZERO);
14001c7c3c6aSMatthew Dillon 
14011c7c3c6aSMatthew Dillon 				if (i != bp->b_pager.pg_reqpage)
14021c7c3c6aSMatthew Dillon 					vm_page_free(m);
14031c7c3c6aSMatthew Dillon 				else
14041c7c3c6aSMatthew Dillon 					vm_page_flash(m);
14051c7c3c6aSMatthew Dillon 				/*
14061c7c3c6aSMatthew Dillon 				 * If i == bp->b_pager.pg_reqpage, do not wake
14071c7c3c6aSMatthew Dillon 				 * the page up.  The caller needs to.
14081c7c3c6aSMatthew Dillon 				 */
14091c7c3c6aSMatthew Dillon 			} else {
14101c7c3c6aSMatthew Dillon 				/*
14111c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
14121c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
14131c7c3c6aSMatthew Dillon 				 * then finish the I/O.
14141c7c3c6aSMatthew Dillon 				 */
14157dbf82dcSMatthew Dillon 				vm_page_dirty(m);
14161c7c3c6aSMatthew Dillon 				vm_page_activate(m);
14171c7c3c6aSMatthew Dillon 				vm_page_io_finish(m);
14181c7c3c6aSMatthew Dillon 			}
14191c7c3c6aSMatthew Dillon 		} else if (bp->b_flags & B_READ) {
14201c7c3c6aSMatthew Dillon 			/*
14211c7c3c6aSMatthew Dillon 			 * For read success, clear dirty bits.  Nobody should
14221c7c3c6aSMatthew Dillon 			 * have this page mapped but don't take any chances,
14231c7c3c6aSMatthew Dillon 			 * make sure the pmap modify bits are also cleared.
14241c7c3c6aSMatthew Dillon 			 *
14251c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
14261c7c3c6aSMatthew Dillon 			 * overriden by the original caller of getpages so
14271c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
14281c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
14291c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
14301c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
14311c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
14321c7c3c6aSMatthew Dillon 			 *
14331c7c3c6aSMatthew Dillon 			 * clear PG_ZERO in page.
14341c7c3c6aSMatthew Dillon 			 *
14351c7c3c6aSMatthew Dillon 			 * If not the requested page then deactivate it.
14361c7c3c6aSMatthew Dillon 			 *
14371c7c3c6aSMatthew Dillon 			 * Note that the requested page, reqpage, is left
14381c7c3c6aSMatthew Dillon 			 * busied, but we still have to wake it up.  The
14391c7c3c6aSMatthew Dillon 			 * other pages are released (unbusied) by
14401c7c3c6aSMatthew Dillon 			 * vm_page_wakeup().  We do not set reqpage's
14411c7c3c6aSMatthew Dillon 			 * valid bits here, it is up to the caller.
14421c7c3c6aSMatthew Dillon 			 */
14431c7c3c6aSMatthew Dillon 
14441c7c3c6aSMatthew Dillon 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
14451c7c3c6aSMatthew Dillon 			m->valid = VM_PAGE_BITS_ALL;
14461c7c3c6aSMatthew Dillon 			m->dirty = 0;
14471c7c3c6aSMatthew Dillon 			vm_page_flag_clear(m, PG_ZERO);
14481c7c3c6aSMatthew Dillon 
14491c7c3c6aSMatthew Dillon 			/*
14501c7c3c6aSMatthew Dillon 			 * We have to wake specifically requested pages
14511c7c3c6aSMatthew Dillon 			 * up too because we cleared PG_SWAPINPROG and
14521c7c3c6aSMatthew Dillon 			 * could be waiting for it in getpages.  However,
14531c7c3c6aSMatthew Dillon 			 * be sure to not unbusy getpages specifically
14541c7c3c6aSMatthew Dillon 			 * requested page - getpages expects it to be
14551c7c3c6aSMatthew Dillon 			 * left busy.
14561c7c3c6aSMatthew Dillon 			 */
14571c7c3c6aSMatthew Dillon 			if (i != bp->b_pager.pg_reqpage) {
14581c7c3c6aSMatthew Dillon 				vm_page_deactivate(m);
14591c7c3c6aSMatthew Dillon 				vm_page_wakeup(m);
14601c7c3c6aSMatthew Dillon 			} else {
14611c7c3c6aSMatthew Dillon 				vm_page_flash(m);
14621c7c3c6aSMatthew Dillon 			}
14631c7c3c6aSMatthew Dillon 		} else {
14641c7c3c6aSMatthew Dillon 			/*
14651c7c3c6aSMatthew Dillon 			 * For write success, clear the modify and dirty
14661c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
14671c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
14681c7c3c6aSMatthew Dillon 			 */
14691c7c3c6aSMatthew Dillon 			vm_page_protect(m, VM_PROT_READ);
14701c7c3c6aSMatthew Dillon 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
14711c7c3c6aSMatthew Dillon 			m->dirty = 0;
14721c7c3c6aSMatthew Dillon 			vm_page_io_finish(m);
1473ffc82b0aSJohn Dyson 		}
1474df8bae1dSRodney W. Grimes 	}
147526f9a767SRodney W. Grimes 
14761c7c3c6aSMatthew Dillon 	/*
14771c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
14781c7c3c6aSMatthew Dillon 	 * pip refs on the object.
14791c7c3c6aSMatthew Dillon 	 */
14800d94caffSDavid Greenman 
14811c7c3c6aSMatthew Dillon 	if (object)
14821c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
148326f9a767SRodney W. Grimes 
14841c7c3c6aSMatthew Dillon 	/*
14851c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
14861c7c3c6aSMatthew Dillon 	 */
1487e47ed70bSJohn Dyson 
1488327f4e83SMatthew Dillon 	relpbuf(
1489327f4e83SMatthew Dillon 	    bp,
1490327f4e83SMatthew Dillon 	    ((bp->b_flags & B_READ) ? &nsw_rcount :
1491327f4e83SMatthew Dillon 		((bp->b_flags & B_ASYNC) ?
1492327f4e83SMatthew Dillon 		    &nsw_wcount_async :
1493327f4e83SMatthew Dillon 		    &nsw_wcount_sync
1494327f4e83SMatthew Dillon 		)
1495327f4e83SMatthew Dillon 	    )
1496327f4e83SMatthew Dillon 	);
149726f9a767SRodney W. Grimes 	splx(s);
149826f9a767SRodney W. Grimes }
14991c7c3c6aSMatthew Dillon 
15001c7c3c6aSMatthew Dillon /************************************************************************
15011c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
15021c7c3c6aSMatthew Dillon  ************************************************************************
15031c7c3c6aSMatthew Dillon  *
15041c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
15051c7c3c6aSMatthew Dillon  *	OBJT_SWAP object.
15061c7c3c6aSMatthew Dillon  *
15071c7c3c6aSMatthew Dillon  *	In fact, we just have a few counters in the vm_object_t.  The
15081c7c3c6aSMatthew Dillon  *	metadata is actually stored in a hash table.
15091c7c3c6aSMatthew Dillon  */
15101c7c3c6aSMatthew Dillon 
15111c7c3c6aSMatthew Dillon /*
15121c7c3c6aSMatthew Dillon  * SWP_PAGER_HASH() -	hash swap meta data
15131c7c3c6aSMatthew Dillon  *
15141c7c3c6aSMatthew Dillon  *	This is an inline helper function which hash the swapblk given
15151c7c3c6aSMatthew Dillon  *	the object and page index.  It returns a pointer to a pointer
15161c7c3c6aSMatthew Dillon  *	to the object, or a pointer to a NULL pointer if it could not
15171c7c3c6aSMatthew Dillon  *	find a swapblk.
15181c7c3c6aSMatthew Dillon  */
15191c7c3c6aSMatthew Dillon 
15201c7c3c6aSMatthew Dillon static __inline struct swblock **
15211c7c3c6aSMatthew Dillon swp_pager_hash(vm_object_t object, daddr_t index)
15221c7c3c6aSMatthew Dillon {
15231c7c3c6aSMatthew Dillon 	struct swblock **pswap;
15241c7c3c6aSMatthew Dillon 	struct swblock *swap;
15251c7c3c6aSMatthew Dillon 
15261c7c3c6aSMatthew Dillon 	index &= ~SWAP_META_MASK;
15271c7c3c6aSMatthew Dillon 	pswap = &swhash[(index ^ (int)(long)object) & swhash_mask];
15281c7c3c6aSMatthew Dillon 
15291c7c3c6aSMatthew Dillon 	while ((swap = *pswap) != NULL) {
15301c7c3c6aSMatthew Dillon 		if (swap->swb_object == object &&
15311c7c3c6aSMatthew Dillon 		    swap->swb_index == index
15321c7c3c6aSMatthew Dillon 		) {
15331c7c3c6aSMatthew Dillon 			break;
15341c7c3c6aSMatthew Dillon 		}
15351c7c3c6aSMatthew Dillon 		pswap = &swap->swb_hnext;
15361c7c3c6aSMatthew Dillon 	}
15371c7c3c6aSMatthew Dillon 	return(pswap);
15381c7c3c6aSMatthew Dillon }
15391c7c3c6aSMatthew Dillon 
15401c7c3c6aSMatthew Dillon /*
15411c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
15421c7c3c6aSMatthew Dillon  *
15431c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
15441c7c3c6aSMatthew Dillon  *	object.
15451c7c3c6aSMatthew Dillon  *
15461c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
15471c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
15481c7c3c6aSMatthew Dillon  *	assigned swapblk is freed.
15491c7c3c6aSMatthew Dillon  */
15501c7c3c6aSMatthew Dillon 
15511c7c3c6aSMatthew Dillon static void
15521c7c3c6aSMatthew Dillon swp_pager_meta_build(
15531c7c3c6aSMatthew Dillon 	vm_object_t object,
15541c7c3c6aSMatthew Dillon 	daddr_t index,
15551c7c3c6aSMatthew Dillon 	daddr_t swapblk,
15561c7c3c6aSMatthew Dillon 	int waitok
15571c7c3c6aSMatthew Dillon ) {
15581c7c3c6aSMatthew Dillon 	struct swblock *swap;
15591c7c3c6aSMatthew Dillon 	struct swblock **pswap;
15601c7c3c6aSMatthew Dillon 
15611c7c3c6aSMatthew Dillon 	/*
15621c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
15631c7c3c6aSMatthew Dillon 	 */
15641c7c3c6aSMatthew Dillon 
15651c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
15661c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
15671c7c3c6aSMatthew Dillon 		object->un_pager.swp.swp_bcount = 0;
15681c7c3c6aSMatthew Dillon 
15691c7c3c6aSMatthew Dillon 		if (object->handle != NULL) {
15701c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
15711c7c3c6aSMatthew Dillon 			    NOBJLIST(object->handle),
15721c7c3c6aSMatthew Dillon 			    object,
15731c7c3c6aSMatthew Dillon 			    pager_object_list
15741c7c3c6aSMatthew Dillon 			);
15751c7c3c6aSMatthew Dillon 		} else {
15761c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
15771c7c3c6aSMatthew Dillon 			    &swap_pager_un_object_list,
15781c7c3c6aSMatthew Dillon 			    object,
15791c7c3c6aSMatthew Dillon 			    pager_object_list
15801c7c3c6aSMatthew Dillon 			);
15811c7c3c6aSMatthew Dillon 		}
15821c7c3c6aSMatthew Dillon 	}
15831c7c3c6aSMatthew Dillon 
15841c7c3c6aSMatthew Dillon 	/*
15851c7c3c6aSMatthew Dillon 	 * Wait for free memory when waitok is TRUE prior to calling the
15861c7c3c6aSMatthew Dillon 	 * zone allocator.
15871c7c3c6aSMatthew Dillon 	 */
15881c7c3c6aSMatthew Dillon 
15891c7c3c6aSMatthew Dillon 	while (waitok && cnt.v_free_count == 0) {
15901c7c3c6aSMatthew Dillon 		VM_WAIT;
15911c7c3c6aSMatthew Dillon 	}
15921c7c3c6aSMatthew Dillon 
15931c7c3c6aSMatthew Dillon 	/*
15941c7c3c6aSMatthew Dillon 	 * If swapblk being added is invalid, just free it.
15951c7c3c6aSMatthew Dillon 	 */
15961c7c3c6aSMatthew Dillon 
15971c7c3c6aSMatthew Dillon 	if (swapblk & SWAPBLK_NONE) {
15981c7c3c6aSMatthew Dillon 		if (swapblk != SWAPBLK_NONE) {
15991c7c3c6aSMatthew Dillon 			swp_pager_freeswapspace(
16001c7c3c6aSMatthew Dillon 			    index,
16011c7c3c6aSMatthew Dillon 			    1
16021c7c3c6aSMatthew Dillon 			);
16031c7c3c6aSMatthew Dillon 			swapblk = SWAPBLK_NONE;
16041c7c3c6aSMatthew Dillon 		}
16051c7c3c6aSMatthew Dillon 	}
16061c7c3c6aSMatthew Dillon 
16071c7c3c6aSMatthew Dillon 	/*
16081c7c3c6aSMatthew Dillon 	 * Locate hash entry.  If not found create, but if we aren't adding
16091c7c3c6aSMatthew Dillon 	 * anything just return.
16101c7c3c6aSMatthew Dillon 	 */
16111c7c3c6aSMatthew Dillon 
16121c7c3c6aSMatthew Dillon 	pswap = swp_pager_hash(object, index);
16131c7c3c6aSMatthew Dillon 
16141c7c3c6aSMatthew Dillon 	if ((swap = *pswap) == NULL) {
16151c7c3c6aSMatthew Dillon 		int i;
16161c7c3c6aSMatthew Dillon 
16171c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
16181c7c3c6aSMatthew Dillon 			return;
16191c7c3c6aSMatthew Dillon 
16201c7c3c6aSMatthew Dillon 		swap = *pswap = zalloc(swap_zone);
16211c7c3c6aSMatthew Dillon 
16221c7c3c6aSMatthew Dillon 		swap->swb_hnext = NULL;
16231c7c3c6aSMatthew Dillon 		swap->swb_object = object;
16241c7c3c6aSMatthew Dillon 		swap->swb_index = index & ~SWAP_META_MASK;
16251c7c3c6aSMatthew Dillon 		swap->swb_count = 0;
16261c7c3c6aSMatthew Dillon 
16271c7c3c6aSMatthew Dillon 		++object->un_pager.swp.swp_bcount;
16281c7c3c6aSMatthew Dillon 
16291c7c3c6aSMatthew Dillon 		for (i = 0; i < SWAP_META_PAGES; ++i)
16301c7c3c6aSMatthew Dillon 			swap->swb_pages[i] = SWAPBLK_NONE;
16311c7c3c6aSMatthew Dillon 	}
16321c7c3c6aSMatthew Dillon 
16331c7c3c6aSMatthew Dillon 	/*
16341c7c3c6aSMatthew Dillon 	 * Delete prior contents of metadata
16351c7c3c6aSMatthew Dillon 	 */
16361c7c3c6aSMatthew Dillon 
16371c7c3c6aSMatthew Dillon 	index &= SWAP_META_MASK;
16381c7c3c6aSMatthew Dillon 
16391c7c3c6aSMatthew Dillon 	if (swap->swb_pages[index] != SWAPBLK_NONE) {
16401c7c3c6aSMatthew Dillon 		swp_pager_freeswapspace(
16411c7c3c6aSMatthew Dillon 		    swap->swb_pages[index] & SWAPBLK_MASK,
16421c7c3c6aSMatthew Dillon 		    1
16431c7c3c6aSMatthew Dillon 		);
16441c7c3c6aSMatthew Dillon 		--swap->swb_count;
16451c7c3c6aSMatthew Dillon 	}
16461c7c3c6aSMatthew Dillon 
16471c7c3c6aSMatthew Dillon 	/*
16481c7c3c6aSMatthew Dillon 	 * Enter block into metadata
16491c7c3c6aSMatthew Dillon 	 */
16501c7c3c6aSMatthew Dillon 
16511c7c3c6aSMatthew Dillon 	swap->swb_pages[index] = swapblk;
16521c7c3c6aSMatthew Dillon 	++swap->swb_count;
16531c7c3c6aSMatthew Dillon }
16541c7c3c6aSMatthew Dillon 
16551c7c3c6aSMatthew Dillon /*
16561c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
16571c7c3c6aSMatthew Dillon  *
16581c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
16591c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
16601c7c3c6aSMatthew Dillon  *
16611c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
16621c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
16631c7c3c6aSMatthew Dillon  *	with resident pages.
16641c7c3c6aSMatthew Dillon  *
16651c7c3c6aSMatthew Dillon  *	This routine must be called at splvm()
16661c7c3c6aSMatthew Dillon  */
16671c7c3c6aSMatthew Dillon 
16681c7c3c6aSMatthew Dillon static void
16691c7c3c6aSMatthew Dillon swp_pager_meta_free(vm_object_t object, daddr_t index, daddr_t count)
16701c7c3c6aSMatthew Dillon {
16711c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
16721c7c3c6aSMatthew Dillon 		return;
16731c7c3c6aSMatthew Dillon 
16741c7c3c6aSMatthew Dillon 	while (count > 0) {
16751c7c3c6aSMatthew Dillon 		struct swblock **pswap;
16761c7c3c6aSMatthew Dillon 		struct swblock *swap;
16771c7c3c6aSMatthew Dillon 
16781c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
16791c7c3c6aSMatthew Dillon 
16801c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
16811c7c3c6aSMatthew Dillon 			daddr_t v = swap->swb_pages[index & SWAP_META_MASK];
16821c7c3c6aSMatthew Dillon 
16831c7c3c6aSMatthew Dillon 			if (v != SWAPBLK_NONE) {
16841c7c3c6aSMatthew Dillon 				swp_pager_freeswapspace(v, 1);
16851c7c3c6aSMatthew Dillon 				swap->swb_pages[index & SWAP_META_MASK] =
16861c7c3c6aSMatthew Dillon 					SWAPBLK_NONE;
16871c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
16881c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
16891c7c3c6aSMatthew Dillon 					zfree(swap_zone, swap);
16901c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
16911c7c3c6aSMatthew Dillon 				}
16921c7c3c6aSMatthew Dillon 			}
16931c7c3c6aSMatthew Dillon 			--count;
16941c7c3c6aSMatthew Dillon 			++index;
16951c7c3c6aSMatthew Dillon 		} else {
16961c7c3c6aSMatthew Dillon 			daddr_t n = SWAP_META_PAGES - (index & SWAP_META_MASK);
16971c7c3c6aSMatthew Dillon 			count -= n;
16981c7c3c6aSMatthew Dillon 			index += n;
16991c7c3c6aSMatthew Dillon 		}
17001c7c3c6aSMatthew Dillon 	}
17011c7c3c6aSMatthew Dillon }
17021c7c3c6aSMatthew Dillon 
17031c7c3c6aSMatthew Dillon /*
17041c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
17051c7c3c6aSMatthew Dillon  *
17061c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
17071c7c3c6aSMatthew Dillon  *	an object.
17081c7c3c6aSMatthew Dillon  */
17091c7c3c6aSMatthew Dillon 
17101c7c3c6aSMatthew Dillon static void
17111c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
17121c7c3c6aSMatthew Dillon {
17131c7c3c6aSMatthew Dillon 	daddr_t index = 0;
17141c7c3c6aSMatthew Dillon 
17151c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
17161c7c3c6aSMatthew Dillon 		return;
17171c7c3c6aSMatthew Dillon 
17181c7c3c6aSMatthew Dillon 	while (object->un_pager.swp.swp_bcount) {
17191c7c3c6aSMatthew Dillon 		struct swblock **pswap;
17201c7c3c6aSMatthew Dillon 		struct swblock *swap;
17211c7c3c6aSMatthew Dillon 
17221c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
17231c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
17241c7c3c6aSMatthew Dillon 			int i;
17251c7c3c6aSMatthew Dillon 
17261c7c3c6aSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
17271c7c3c6aSMatthew Dillon 				daddr_t v = swap->swb_pages[i];
17281c7c3c6aSMatthew Dillon 				if (v != SWAPBLK_NONE) {
17291c7c3c6aSMatthew Dillon #if !defined(MAX_PERF)
17301c7c3c6aSMatthew Dillon 					--swap->swb_count;
17311c7c3c6aSMatthew Dillon #endif
17321c7c3c6aSMatthew Dillon 					swp_pager_freeswapspace(
17331c7c3c6aSMatthew Dillon 					    v,
17341c7c3c6aSMatthew Dillon 					    1
17351c7c3c6aSMatthew Dillon 					);
17361c7c3c6aSMatthew Dillon 				}
17371c7c3c6aSMatthew Dillon 			}
17381c7c3c6aSMatthew Dillon #if !defined(MAX_PERF)
17391c7c3c6aSMatthew Dillon 			if (swap->swb_count != 0)
17401c7c3c6aSMatthew Dillon 				panic("swap_pager_meta_free_all: swb_count != 0");
17411c7c3c6aSMatthew Dillon #endif
17421c7c3c6aSMatthew Dillon 			*pswap = swap->swb_hnext;
17431c7c3c6aSMatthew Dillon 			zfree(swap_zone, swap);
17441c7c3c6aSMatthew Dillon 			--object->un_pager.swp.swp_bcount;
17451c7c3c6aSMatthew Dillon 		}
17461c7c3c6aSMatthew Dillon 		index += SWAP_META_PAGES;
17471c7c3c6aSMatthew Dillon #if !defined(MAX_PERF)
17481c7c3c6aSMatthew Dillon 		if (index > 0x20000000)
17491c7c3c6aSMatthew Dillon 			panic("swp_pager_meta_free_all: failed to locate all swap meta blocks");
17501c7c3c6aSMatthew Dillon #endif
17511c7c3c6aSMatthew Dillon 	}
17521c7c3c6aSMatthew Dillon }
17531c7c3c6aSMatthew Dillon 
17541c7c3c6aSMatthew Dillon /*
17551c7c3c6aSMatthew Dillon  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
17561c7c3c6aSMatthew Dillon  *
17571c7c3c6aSMatthew Dillon  *	This routine is capable of looking up, popping, or freeing
17581c7c3c6aSMatthew Dillon  *	swapblk assignments in the swap meta data or in the vm_page_t.
17591c7c3c6aSMatthew Dillon  *	The routine typically returns the swapblk being looked-up, or popped,
17601c7c3c6aSMatthew Dillon  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
17611c7c3c6aSMatthew Dillon  *	was invalid.  This routine will automatically free any invalid
17621c7c3c6aSMatthew Dillon  *	meta-data swapblks.
17631c7c3c6aSMatthew Dillon  *
17641c7c3c6aSMatthew Dillon  *	It is not possible to store invalid swapblks in the swap meta data
17651c7c3c6aSMatthew Dillon  *	(other then a literal 'SWAPBLK_NONE'), so we don't bother checking.
17661c7c3c6aSMatthew Dillon  *
17671c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
17681c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
17691c7c3c6aSMatthew Dillon  *	busy page.
17701c7c3c6aSMatthew Dillon  *
17711c7c3c6aSMatthew Dillon  *	SWM_FREE	remove and free swap block from metadata
17721c7c3c6aSMatthew Dillon  *
17731c7c3c6aSMatthew Dillon  *	SWM_POP		remove from meta data but do not free.. pop it out
17741c7c3c6aSMatthew Dillon  */
17751c7c3c6aSMatthew Dillon 
17761c7c3c6aSMatthew Dillon static daddr_t
17771c7c3c6aSMatthew Dillon swp_pager_meta_ctl(
17781c7c3c6aSMatthew Dillon 	vm_object_t object,
17791c7c3c6aSMatthew Dillon 	vm_pindex_t index,
17801c7c3c6aSMatthew Dillon 	int flags
17811c7c3c6aSMatthew Dillon ) {
17821c7c3c6aSMatthew Dillon 	/*
17831c7c3c6aSMatthew Dillon 	 * The meta data only exists of the object is OBJT_SWAP
17841c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
17851c7c3c6aSMatthew Dillon 	 */
17861c7c3c6aSMatthew Dillon 
17871c7c3c6aSMatthew Dillon 	if (
17881c7c3c6aSMatthew Dillon 	    object->type != OBJT_SWAP ||
17891c7c3c6aSMatthew Dillon 	    object->un_pager.swp.swp_bcount == 0
17901c7c3c6aSMatthew Dillon 	) {
17911c7c3c6aSMatthew Dillon 		return(SWAPBLK_NONE);
17921c7c3c6aSMatthew Dillon 	}
17931c7c3c6aSMatthew Dillon 
17941c7c3c6aSMatthew Dillon 	{
17951c7c3c6aSMatthew Dillon 		struct swblock **pswap;
17961c7c3c6aSMatthew Dillon 		struct swblock *swap;
17971c7c3c6aSMatthew Dillon 		daddr_t r1 = SWAPBLK_NONE;
17981c7c3c6aSMatthew Dillon 
17991c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
18001c7c3c6aSMatthew Dillon 
18011c7c3c6aSMatthew Dillon 		index &= SWAP_META_MASK;
18021c7c3c6aSMatthew Dillon 
18031c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
18041c7c3c6aSMatthew Dillon 			r1 = swap->swb_pages[index];
18051c7c3c6aSMatthew Dillon 
18061c7c3c6aSMatthew Dillon 			if (r1 != SWAPBLK_NONE) {
18071c7c3c6aSMatthew Dillon 				if (flags & SWM_FREE) {
18081c7c3c6aSMatthew Dillon 					swp_pager_freeswapspace(
18091c7c3c6aSMatthew Dillon 					    r1,
18101c7c3c6aSMatthew Dillon 					    1
18111c7c3c6aSMatthew Dillon 					);
18121c7c3c6aSMatthew Dillon 					r1 = SWAPBLK_NONE;
18131c7c3c6aSMatthew Dillon 				}
18141c7c3c6aSMatthew Dillon 				if (flags & (SWM_FREE|SWM_POP)) {
18151c7c3c6aSMatthew Dillon 					swap->swb_pages[index] = SWAPBLK_NONE;
18161c7c3c6aSMatthew Dillon 					if (--swap->swb_count == 0) {
18171c7c3c6aSMatthew Dillon 						*pswap = swap->swb_hnext;
18181c7c3c6aSMatthew Dillon 						zfree(swap_zone, swap);
18191c7c3c6aSMatthew Dillon 						--object->un_pager.swp.swp_bcount;
18201c7c3c6aSMatthew Dillon 					}
18211c7c3c6aSMatthew Dillon 				}
18221c7c3c6aSMatthew Dillon 	 		}
18231c7c3c6aSMatthew Dillon 		}
18241c7c3c6aSMatthew Dillon 
18251c7c3c6aSMatthew Dillon 		return(r1);
18261c7c3c6aSMatthew Dillon 	}
18271c7c3c6aSMatthew Dillon 	/* not reached */
18281c7c3c6aSMatthew Dillon }
18291c7c3c6aSMatthew Dillon 
1830