xref: /freebsd/sys/vm/swap_pager.c (revision 2395531439bb140427dff4dfd6d67856f907c15e)
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
215929bcfaSPhilippe Charnier  *    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  *
67c3aac50fSPeter Wemm  * $FreeBSD$
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <sys/param.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
72af647ddeSBruce Evans #include <sys/conf.h>
7364abb5a5SDavid Greenman #include <sys/kernel.h>
74df8bae1dSRodney W. Grimes #include <sys/proc.h>
759626b608SPoul-Henning Kamp #include <sys/bio.h>
76df8bae1dSRodney W. Grimes #include <sys/buf.h>
77df8bae1dSRodney W. Grimes #include <sys/vnode.h>
78df8bae1dSRodney W. Grimes #include <sys/malloc.h>
79efeaf95aSDavid Greenman #include <sys/vmmeter.h>
80327f4e83SMatthew Dillon #include <sys/sysctl.h>
811c7c3c6aSMatthew Dillon #include <sys/blist.h>
821c7c3c6aSMatthew Dillon #include <sys/lock.h>
83936524aaSMatthew Dillon #include <sys/vmmeter.h>
84df8bae1dSRodney W. Grimes 
85e47ed70bSJohn Dyson #ifndef MAX_PAGEOUT_CLUSTER
86ffc82b0aSJohn Dyson #define MAX_PAGEOUT_CLUSTER 16
87e47ed70bSJohn Dyson #endif
88e47ed70bSJohn Dyson 
89e47ed70bSJohn Dyson #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
90e47ed70bSJohn Dyson 
911c7c3c6aSMatthew Dillon #include "opt_swap.h"
92df8bae1dSRodney W. Grimes #include <vm/vm.h>
9321cd6e62SSeigo Tanimura #include <vm/pmap.h>
9421cd6e62SSeigo Tanimura #include <vm/vm_map.h>
9521cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
96efeaf95aSDavid Greenman #include <vm/vm_object.h>
97df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
98efeaf95aSDavid Greenman #include <vm/vm_pager.h>
99df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
10021cd6e62SSeigo Tanimura #include <vm/vm_zone.h>
101df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
102efeaf95aSDavid Greenman #include <vm/vm_extern.h>
103df8bae1dSRodney W. Grimes 
1041c7c3c6aSMatthew Dillon #define SWM_FREE	0x02	/* free, period			*/
1051c7c3c6aSMatthew Dillon #define SWM_POP		0x04	/* pop out			*/
10626f9a767SRodney W. Grimes 
10724a1cce3SDavid Greenman /*
1081c7c3c6aSMatthew Dillon  * vm_swap_size is in page-sized chunks now.  It was DEV_BSIZE'd chunks
1091c7c3c6aSMatthew Dillon  * in the old system.
11024a1cce3SDavid Greenman  */
1111c7c3c6aSMatthew Dillon 
1121c7c3c6aSMatthew Dillon extern int vm_swap_size;	/* number of free swap blocks, in pages */
1131c7c3c6aSMatthew Dillon 
11420d3034fSMatthew Dillon int swap_pager_full;		/* swap space exhaustion (task killing) */
11520d3034fSMatthew Dillon static int swap_pager_almost_full; /* swap space exhaustion (w/ hysteresis)*/
1161c7c3c6aSMatthew Dillon static int nsw_rcount;		/* free read buffers			*/
117327f4e83SMatthew Dillon static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
118327f4e83SMatthew Dillon static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
119327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
120327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
12123955314SAlfred Perlstein static int sw_alloc_interlock;	/* swap pager allocation interlock	*/
1221c7c3c6aSMatthew Dillon 
1231c7c3c6aSMatthew Dillon struct blist *swapblist;
1241c7c3c6aSMatthew Dillon static struct swblock **swhash;
1251c7c3c6aSMatthew Dillon static int swhash_mask;
126327f4e83SMatthew Dillon static int swap_async_max = 4;	/* maximum in-progress async I/O's	*/
127327f4e83SMatthew Dillon 
128edfa785aSRobert Watson /* from vm_swap.c */
129edfa785aSRobert Watson extern struct vnode *swapdev_vp;
130edfa785aSRobert Watson extern struct swdevt *swdevt;
131edfa785aSRobert Watson extern int nswdev;
13224e7ab7cSPoul-Henning Kamp 
133327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_async_max,
134327f4e83SMatthew Dillon         CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops");
1351c7c3c6aSMatthew Dillon 
136edfa785aSRobert Watson #define BLK2DEVIDX(blk) (nswdev > 1 ? blk / dmmax % nswdev : 0)
137edfa785aSRobert Watson 
1381c7c3c6aSMatthew Dillon /*
1391c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
1401c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
1411c7c3c6aSMatthew Dillon  */
1421c7c3c6aSMatthew Dillon 
1431c7c3c6aSMatthew Dillon #define NOBJLISTS		8
1441c7c3c6aSMatthew Dillon 
1451c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
146af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
1471c7c3c6aSMatthew Dillon 
148a9fa2c05SAlfred Perlstein static struct mtx sw_alloc_mtx;	/* protect list manipulation */
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));
1660b441832SPoul-Henning Kamp static void	swap_pager_strategy __P((vm_object_t, struct bio *));
167f708ef1bSPoul-Henning Kamp 
168df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
1691c7c3c6aSMatthew Dillon 	swap_pager_init,	/* early system initialization of pager	*/
1701c7c3c6aSMatthew Dillon 	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
1711c7c3c6aSMatthew Dillon 	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
1721c7c3c6aSMatthew Dillon 	swap_pager_getpages,	/* pagein				*/
1731c7c3c6aSMatthew Dillon 	swap_pager_putpages,	/* pageout				*/
1741c7c3c6aSMatthew Dillon 	swap_pager_haspage,	/* get backing store status for page	*/
175a5296b05SJulian Elischer 	swap_pager_unswapped,	/* remove swap related to page		*/
176a5296b05SJulian Elischer 	swap_pager_strategy	/* pager strategy call			*/
177df8bae1dSRodney W. Grimes };
178df8bae1dSRodney W. Grimes 
1790b441832SPoul-Henning Kamp static struct buf *getchainbuf(struct bio *bp, struct vnode *vp, int flags);
180e4057dbdSPoul-Henning Kamp static void flushchainbuf(struct buf *nbp);
1810b441832SPoul-Henning Kamp static void waitchainbuf(struct bio *bp, int count, int done);
182e4057dbdSPoul-Henning Kamp 
1831c7c3c6aSMatthew Dillon /*
1841c7c3c6aSMatthew Dillon  * dmmax is in page-sized chunks with the new swap system.  It was
18564bcb9c8SMatthew Dillon  * dev-bsized chunks in the old.  dmmax is always a power of 2.
1861c7c3c6aSMatthew Dillon  *
1871c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
1881c7c3c6aSMatthew Dillon  * internal.
1891c7c3c6aSMatthew Dillon  */
1901c7c3c6aSMatthew Dillon 
191f708ef1bSPoul-Henning Kamp int dmmax;
1921c7c3c6aSMatthew Dillon static int dmmax_mask;
19320d3034fSMatthew Dillon int nswap_lowat = 128;		/* in pages, swap_pager_almost_full warn */
19420d3034fSMatthew Dillon int nswap_hiwat = 512;		/* in pages, swap_pager_almost_full warn */
19526f9a767SRodney W. Grimes 
196cee313c4SRobert Watson SYSCTL_INT(_vm, OID_AUTO, dmmax,
197cee313c4SRobert Watson 	CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block");
198cee313c4SRobert Watson 
1991c7c3c6aSMatthew Dillon static __inline void	swp_sizecheck __P((void));
2001c7c3c6aSMatthew Dillon static void	swp_pager_sync_iodone __P((struct buf *bp));
2011c7c3c6aSMatthew Dillon static void	swp_pager_async_iodone __P((struct buf *bp));
20224a1cce3SDavid Greenman 
2031c7c3c6aSMatthew Dillon /*
2041c7c3c6aSMatthew Dillon  * Swap bitmap functions
2051c7c3c6aSMatthew Dillon  */
2061c7c3c6aSMatthew Dillon 
2071c7c3c6aSMatthew Dillon static __inline void	swp_pager_freeswapspace __P((daddr_t blk, int npages));
2081c7c3c6aSMatthew Dillon static __inline daddr_t	swp_pager_getswapspace __P((int npages));
2091c7c3c6aSMatthew Dillon 
2101c7c3c6aSMatthew Dillon /*
2111c7c3c6aSMatthew Dillon  * Metadata functions
2121c7c3c6aSMatthew Dillon  */
2131c7c3c6aSMatthew Dillon 
2144dcc5c2dSMatthew Dillon static void swp_pager_meta_build __P((vm_object_t, vm_pindex_t, daddr_t));
2154dcc5c2dSMatthew Dillon static void swp_pager_meta_free __P((vm_object_t, vm_pindex_t, daddr_t));
2161c7c3c6aSMatthew Dillon static void swp_pager_meta_free_all __P((vm_object_t));
2171c7c3c6aSMatthew Dillon static daddr_t swp_pager_meta_ctl __P((vm_object_t, vm_pindex_t, int));
2181c7c3c6aSMatthew Dillon 
2191c7c3c6aSMatthew Dillon /*
2201c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
2211c7c3c6aSMatthew Dillon  *
22220d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
22320d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
22420d3034fSMatthew Dillon  *
22520d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
2261c7c3c6aSMatthew Dillon  *
2271c7c3c6aSMatthew Dillon  *	No restrictions on call
2281c7c3c6aSMatthew Dillon  *	This routine may not block.
2291c7c3c6aSMatthew Dillon  *	This routine must be called at splvm()
2301c7c3c6aSMatthew Dillon  */
231de5f6a77SJohn Dyson 
232c1087c13SBruce Evans static __inline void
2331c7c3c6aSMatthew Dillon swp_sizecheck()
2340d94caffSDavid Greenman {
23523955314SAlfred Perlstein 
23623955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
2371c7c3c6aSMatthew Dillon 	if (vm_swap_size < nswap_lowat) {
23820d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
2391af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
24020d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
2412b0d37a4SMatthew Dillon 		}
24220d3034fSMatthew Dillon 	} else {
24326f9a767SRodney W. Grimes 		swap_pager_full = 0;
24420d3034fSMatthew Dillon 		if (vm_swap_size > nswap_hiwat)
24520d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
24626f9a767SRodney W. Grimes 	}
2471c7c3c6aSMatthew Dillon }
2481c7c3c6aSMatthew Dillon 
2491c7c3c6aSMatthew Dillon /*
2501c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
2511c7c3c6aSMatthew Dillon  *
2521c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
2531c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
2541c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
2551c7c3c6aSMatthew Dillon  */
25626f9a767SRodney W. Grimes 
257f5a12711SPoul-Henning Kamp static void
258df8bae1dSRodney W. Grimes swap_pager_init()
259df8bae1dSRodney W. Grimes {
2601c7c3c6aSMatthew Dillon 	/*
2611c7c3c6aSMatthew Dillon 	 * Initialize object lists
2621c7c3c6aSMatthew Dillon 	 */
2631c7c3c6aSMatthew Dillon 	int i;
2641c7c3c6aSMatthew Dillon 
2651c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
2661c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
26724a1cce3SDavid Greenman 	TAILQ_INIT(&swap_pager_un_object_list);
268a9fa2c05SAlfred Perlstein 	mtx_init(&sw_alloc_mtx, "swap_pager list", MTX_DEF);
269df8bae1dSRodney W. Grimes 
270df8bae1dSRodney W. Grimes 	/*
2711c7c3c6aSMatthew Dillon 	 * Device Stripe, in PAGE_SIZE'd blocks
272df8bae1dSRodney W. Grimes 	 */
2731c7c3c6aSMatthew Dillon 
2741c7c3c6aSMatthew Dillon 	dmmax = SWB_NPAGES * 2;
2751c7c3c6aSMatthew Dillon 	dmmax_mask = ~(dmmax - 1);
2761c7c3c6aSMatthew Dillon }
27726f9a767SRodney W. Grimes 
278df8bae1dSRodney W. Grimes /*
2791c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
2801c7c3c6aSMatthew Dillon  *
2811c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
2821c7c3c6aSMatthew Dillon  *	its main loop.
283df8bae1dSRodney W. Grimes  */
284df8bae1dSRodney W. Grimes 
28524a1cce3SDavid Greenman void
28624a1cce3SDavid Greenman swap_pager_swap_init()
287df8bae1dSRodney W. Grimes {
28821cd6e62SSeigo Tanimura 	int n, n2;
2890d94caffSDavid Greenman 
29026f9a767SRodney W. Grimes 	/*
2911c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
2921c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
2931c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
2941c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
2951c7c3c6aSMatthew Dillon 	 *
296327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
2971c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
2981c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
2991c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
300327f4e83SMatthew Dillon 	 *
301327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
302327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
303327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
304327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
305327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
306327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
307327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
308327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
309327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
310327f4e83SMatthew Dillon 	 * So it all works out pretty well.
31126f9a767SRodney W. Grimes 	 */
31224a1cce3SDavid Greenman 
313ad3cce20SMatthew Dillon 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
314327f4e83SMatthew Dillon 
3151c7c3c6aSMatthew Dillon 	nsw_rcount = (nswbuf + 1) / 2;
316327f4e83SMatthew Dillon 	nsw_wcount_sync = (nswbuf + 3) / 4;
317327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
318327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
31924a1cce3SDavid Greenman 
3201c7c3c6aSMatthew Dillon 	/*
3211c7c3c6aSMatthew Dillon 	 * Initialize our zone.  Right now I'm just guessing on the number
3221c7c3c6aSMatthew Dillon 	 * we need based on the number of pages in the system.  Each swblock
3231c7c3c6aSMatthew Dillon 	 * can hold 16 pages, so this is probably overkill.
3241c7c3c6aSMatthew Dillon 	 */
32524a1cce3SDavid Greenman 
32621cd6e62SSeigo Tanimura 	n = min(cnt.v_page_count, (kernel_map->max_offset - kernel_map->min_offset) / PAGE_SIZE) * 2;
32721cd6e62SSeigo Tanimura 	n2 = n;
32826f9a767SRodney W. Grimes 
32921cd6e62SSeigo Tanimura 	while (n > 0
33021cd6e62SSeigo Tanimura 	       && (swap_zone = zinit(
3311c7c3c6aSMatthew Dillon 		       "SWAPMETA",
3321c7c3c6aSMatthew Dillon 		       sizeof(struct swblock),
3331c7c3c6aSMatthew Dillon 		       n,
3341c7c3c6aSMatthew Dillon 		       ZONE_INTERRUPT,
3351c7c3c6aSMatthew Dillon 		       1
33621cd6e62SSeigo Tanimura 		       )) == NULL)
33721cd6e62SSeigo Tanimura 		n >>= 1;
33821cd6e62SSeigo Tanimura 	if (swap_zone == NULL)
33921cd6e62SSeigo Tanimura 		printf("WARNING: failed to init swap_zone!\n");
34021cd6e62SSeigo Tanimura 	if (n2 != n)
34121cd6e62SSeigo Tanimura 		printf("Swap zone entries reduced to %d.\n", n);
34221cd6e62SSeigo Tanimura 	n2 = n;
34324a1cce3SDavid Greenman 
3441c7c3c6aSMatthew Dillon 	/*
3451c7c3c6aSMatthew Dillon 	 * Initialize our meta-data hash table.  The swapper does not need to
3461c7c3c6aSMatthew Dillon 	 * be quite as efficient as the VM system, so we do not use an
3471c7c3c6aSMatthew Dillon 	 * oversized hash table.
3481c7c3c6aSMatthew Dillon 	 *
3491c7c3c6aSMatthew Dillon 	 * 	n: 		size of hash table, must be power of 2
3501c7c3c6aSMatthew Dillon 	 *	swhash_mask:	hash table index mask
3511c7c3c6aSMatthew Dillon 	 */
352df8bae1dSRodney W. Grimes 
35321cd6e62SSeigo Tanimura 	for (n = 1; n < n2 ; n <<= 1)
3541c7c3c6aSMatthew Dillon 		;
3551c7c3c6aSMatthew Dillon 
3567cc0979fSDavid Malone 	swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO);
3571c7c3c6aSMatthew Dillon 
3581c7c3c6aSMatthew Dillon 	swhash_mask = n - 1;
35924a1cce3SDavid Greenman }
36024a1cce3SDavid Greenman 
36124a1cce3SDavid Greenman /*
3621c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
3631c7c3c6aSMatthew Dillon  *			its metadata structures.
3641c7c3c6aSMatthew Dillon  *
3651c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
3661c7c3c6aSMatthew Dillon  *	OBJT_SWAP object.  We do this by creating an OBJT_DEFAULT object
3671c7c3c6aSMatthew Dillon  *	and then converting it with swp_pager_meta_build().
3681c7c3c6aSMatthew Dillon  *
3691c7c3c6aSMatthew Dillon  *	This routine may block in vm_object_allocate() and create a named
3701c7c3c6aSMatthew Dillon  *	object lookup race, so we must interlock.   We must also run at
3711c7c3c6aSMatthew Dillon  *	splvm() for the object lookup to handle races with interrupts, but
3721c7c3c6aSMatthew Dillon  *	we do not have to maintain splvm() in between the lookup and the
3731c7c3c6aSMatthew Dillon  *	add because (I believe) it is not possible to attempt to create
3741c7c3c6aSMatthew Dillon  *	a new swap object w/handle when a default object with that handle
3751c7c3c6aSMatthew Dillon  *	already exists.
37624a1cce3SDavid Greenman  */
3771c7c3c6aSMatthew Dillon 
378f5a12711SPoul-Henning Kamp static vm_object_t
3796cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
380b9dcd593SBruce Evans 		 vm_ooffset_t offset)
38124a1cce3SDavid Greenman {
38224a1cce3SDavid Greenman 	vm_object_t object;
38324a1cce3SDavid Greenman 
38424a1cce3SDavid Greenman 	if (handle) {
3851c7c3c6aSMatthew Dillon 		/*
3861c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
3871c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
3881c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
3891c7c3c6aSMatthew Dillon 		 * of the handle.
3901c7c3c6aSMatthew Dillon 		 */
3911c7c3c6aSMatthew Dillon 
39223955314SAlfred Perlstein 		while (sw_alloc_interlock) {
39323955314SAlfred Perlstein 			sw_alloc_interlock = -1;
39423955314SAlfred Perlstein 			msleep(&sw_alloc_interlock, &vm_mtx, PVM, "swpalc", 0);
39523955314SAlfred Perlstein 		}
3961c7c3c6aSMatthew Dillon 
3971c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
3981c7c3c6aSMatthew Dillon 
39924a1cce3SDavid Greenman 		if (object != NULL) {
40024a1cce3SDavid Greenman 			vm_object_reference(object);
40124a1cce3SDavid Greenman 		} else {
4021c7c3c6aSMatthew Dillon 			object = vm_object_allocate(OBJT_DEFAULT,
4036cde7a16SDavid Greenman 				OFF_TO_IDX(offset + PAGE_MASK + size));
40424a1cce3SDavid Greenman 			object->handle = handle;
4051c7c3c6aSMatthew Dillon 
4064dcc5c2dSMatthew Dillon 			swp_pager_meta_build(object, 0, SWAPBLK_NONE);
40724a1cce3SDavid Greenman 		}
4081c7c3c6aSMatthew Dillon 
40923955314SAlfred Perlstein 		if (sw_alloc_interlock < 0)
41023955314SAlfred Perlstein 			wakeup(&sw_alloc_interlock);
41123955314SAlfred Perlstein 		sw_alloc_interlock = 0;
41224a1cce3SDavid Greenman 	} else {
4131c7c3c6aSMatthew Dillon 		object = vm_object_allocate(OBJT_DEFAULT,
4146cde7a16SDavid Greenman 			OFF_TO_IDX(offset + PAGE_MASK + size));
4151c7c3c6aSMatthew Dillon 
4164dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
41724a1cce3SDavid Greenman 	}
41824a1cce3SDavid Greenman 
41924a1cce3SDavid Greenman 	return (object);
420df8bae1dSRodney W. Grimes }
421df8bae1dSRodney W. Grimes 
42226f9a767SRodney W. Grimes /*
4231c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
4241c7c3c6aSMatthew Dillon  *
4251c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
4261c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
4271c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
4281c7c3c6aSMatthew Dillon  *	about to be destroyed.
4291c7c3c6aSMatthew Dillon  *
4301c7c3c6aSMatthew Dillon  *	This routine may block, but no longer does.
4311c7c3c6aSMatthew Dillon  *
4321c7c3c6aSMatthew Dillon  *	The object must be locked or unreferenceable.
43326f9a767SRodney W. Grimes  */
43426f9a767SRodney W. Grimes 
435df8bae1dSRodney W. Grimes static void
4361c7c3c6aSMatthew Dillon swap_pager_dealloc(object)
4372a4895f4SDavid Greenman 	vm_object_t object;
43826f9a767SRodney W. Grimes {
4394dcc5c2dSMatthew Dillon 	int s;
4404dcc5c2dSMatthew Dillon 
44126f9a767SRodney W. Grimes 	/*
4421c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
4431c7c3c6aSMatthew Dillon 	 * pageout completion.
44426f9a767SRodney W. Grimes 	 */
445b44e4b7aSJohn Dyson 
446a9fa2c05SAlfred Perlstein 	mtx_lock(&sw_alloc_mtx);
4471c7c3c6aSMatthew Dillon 	if (object->handle == NULL) {
4481c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(&swap_pager_un_object_list, object, pager_object_list);
44924ea4a96SDavid Greenman 	} else {
4501c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list);
45126f9a767SRodney W. Grimes 	}
452a9fa2c05SAlfred Perlstein 	mtx_unlock(&sw_alloc_mtx);
4531c7c3c6aSMatthew Dillon 
4541c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
4551c7c3c6aSMatthew Dillon 
4561c7c3c6aSMatthew Dillon 	/*
4571c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
4581c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
4591c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
4601c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
4611c7c3c6aSMatthew Dillon 	 */
4624dcc5c2dSMatthew Dillon 	s = splvm();
4631c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
4644dcc5c2dSMatthew Dillon 	splx(s);
4651c7c3c6aSMatthew Dillon }
4661c7c3c6aSMatthew Dillon 
4671c7c3c6aSMatthew Dillon /************************************************************************
4681c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
4691c7c3c6aSMatthew Dillon  ************************************************************************/
4701c7c3c6aSMatthew Dillon 
4711c7c3c6aSMatthew Dillon /*
4721c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
4731c7c3c6aSMatthew Dillon  *
4741c7c3c6aSMatthew Dillon  *	Allocate swap for the requested number of pages.  The starting
4751c7c3c6aSMatthew Dillon  *	swap block number (a page index) is returned or SWAPBLK_NONE
4761c7c3c6aSMatthew Dillon  *	if the allocation failed.
4771c7c3c6aSMatthew Dillon  *
4781c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
4791c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
4801c7c3c6aSMatthew Dillon  *
4811c7c3c6aSMatthew Dillon  *	Must be called at splvm() to avoid races with bitmap frees from
4821c7c3c6aSMatthew Dillon  *	vm_page_remove() aka swap_pager_page_removed().
4831c7c3c6aSMatthew Dillon  *
4841c7c3c6aSMatthew Dillon  *	This routine may not block
4851c7c3c6aSMatthew Dillon  *	This routine must be called at splvm().
48623955314SAlfred Perlstein  *	vm_mtx should be held
4871c7c3c6aSMatthew Dillon  */
4881c7c3c6aSMatthew Dillon 
4891c7c3c6aSMatthew Dillon static __inline daddr_t
4901c7c3c6aSMatthew Dillon swp_pager_getswapspace(npages)
4911c7c3c6aSMatthew Dillon 	int npages;
4921c7c3c6aSMatthew Dillon {
4931c7c3c6aSMatthew Dillon 	daddr_t blk;
4941c7c3c6aSMatthew Dillon 
49523955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
4961c7c3c6aSMatthew Dillon 	if ((blk = blist_alloc(swapblist, npages)) == SWAPBLK_NONE) {
4972b0d37a4SMatthew Dillon 		if (swap_pager_full != 2) {
4981c7c3c6aSMatthew Dillon 			printf("swap_pager_getswapspace: failed\n");
4992b0d37a4SMatthew Dillon 			swap_pager_full = 2;
50020d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
5012b0d37a4SMatthew Dillon 		}
5021c7c3c6aSMatthew Dillon 	} else {
5031c7c3c6aSMatthew Dillon 		vm_swap_size -= npages;
504edfa785aSRobert Watson 		/* per-swap area stats */
505edfa785aSRobert Watson 		swdevt[BLK2DEVIDX(blk)].sw_used += npages;
5061c7c3c6aSMatthew Dillon 		swp_sizecheck();
5071c7c3c6aSMatthew Dillon 	}
5081c7c3c6aSMatthew Dillon 	return(blk);
50926f9a767SRodney W. Grimes }
51026f9a767SRodney W. Grimes 
51126f9a767SRodney W. Grimes /*
5121c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
5131c7c3c6aSMatthew Dillon  *
5141c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
5151c7c3c6aSMatthew Dillon  *
5161c7c3c6aSMatthew Dillon  *	Note:  This routine may not block (it could in the old swap code),
5171c7c3c6aSMatthew Dillon  *	and through the use of the new blist routines it does not block.
5181c7c3c6aSMatthew Dillon  *
5191c7c3c6aSMatthew Dillon  *	We must be called at splvm() to avoid races with bitmap frees from
5201c7c3c6aSMatthew Dillon  *	vm_page_remove() aka swap_pager_page_removed().
5211c7c3c6aSMatthew Dillon  *
5221c7c3c6aSMatthew Dillon  *	This routine may not block
5231c7c3c6aSMatthew Dillon  *	This routine must be called at splvm().
52423955314SAlfred Perlstein  *	vm_mtx should be held
52526f9a767SRodney W. Grimes  */
5261c7c3c6aSMatthew Dillon 
5271c7c3c6aSMatthew Dillon static __inline void
5281c7c3c6aSMatthew Dillon swp_pager_freeswapspace(blk, npages)
5291c7c3c6aSMatthew Dillon 	daddr_t blk;
5301c7c3c6aSMatthew Dillon 	int npages;
5310d94caffSDavid Greenman {
53223955314SAlfred Perlstein 
53323955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
5341c7c3c6aSMatthew Dillon 	blist_free(swapblist, blk, npages);
5351c7c3c6aSMatthew Dillon 	vm_swap_size += npages;
536edfa785aSRobert Watson 	/* per-swap area stats */
537edfa785aSRobert Watson 	swdevt[BLK2DEVIDX(blk)].sw_used -= npages;
5381c7c3c6aSMatthew Dillon 	swp_sizecheck();
53926f9a767SRodney W. Grimes }
5401c7c3c6aSMatthew Dillon 
54126f9a767SRodney W. Grimes /*
5421c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
5431c7c3c6aSMatthew Dillon  *				range within an object.
5441c7c3c6aSMatthew Dillon  *
5451c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
5461c7c3c6aSMatthew Dillon  *
5471c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
5481c7c3c6aSMatthew Dillon  *
5491c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
5501c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
5511c7c3c6aSMatthew Dillon  *	we should be ok.
5524dcc5c2dSMatthew Dillon  *
5534dcc5c2dSMatthew Dillon  *	This routine may be called at any spl.  We up our spl to splvm temporarily
5544dcc5c2dSMatthew Dillon  *	in order to perform the metadata removal.
55526f9a767SRodney W. Grimes  */
5561c7c3c6aSMatthew Dillon 
55726f9a767SRodney W. Grimes void
55824a1cce3SDavid Greenman swap_pager_freespace(object, start, size)
55924a1cce3SDavid Greenman 	vm_object_t object;
560a316d390SJohn Dyson 	vm_pindex_t start;
561a316d390SJohn Dyson 	vm_size_t size;
56226f9a767SRodney W. Grimes {
5634dcc5c2dSMatthew Dillon 	int s = splvm();
56423955314SAlfred Perlstein 
56523955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
56623955314SAlfred Perlstein 
5671c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
5684dcc5c2dSMatthew Dillon 	splx(s);
5694dcc5c2dSMatthew Dillon }
5704dcc5c2dSMatthew Dillon 
5714dcc5c2dSMatthew Dillon /*
5724dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
5734dcc5c2dSMatthew Dillon  *
5744dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
5754dcc5c2dSMatthew Dillon  *	swap blocks are not zerod.  Any previous swap assignment is destroyed.
5764dcc5c2dSMatthew Dillon  *
5774dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
5784dcc5c2dSMatthew Dillon  */
5794dcc5c2dSMatthew Dillon 
5804dcc5c2dSMatthew Dillon int
5814dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
5824dcc5c2dSMatthew Dillon {
5834dcc5c2dSMatthew Dillon 	int s;
5844dcc5c2dSMatthew Dillon 	int n = 0;
5854dcc5c2dSMatthew Dillon 	daddr_t blk = SWAPBLK_NONE;
5864dcc5c2dSMatthew Dillon 	vm_pindex_t beg = start;	/* save start index */
5874dcc5c2dSMatthew Dillon 
5884dcc5c2dSMatthew Dillon 	s = splvm();
5894dcc5c2dSMatthew Dillon 	while (size) {
5904dcc5c2dSMatthew Dillon 		if (n == 0) {
5914dcc5c2dSMatthew Dillon 			n = BLIST_MAX_ALLOC;
5924dcc5c2dSMatthew Dillon 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
5934dcc5c2dSMatthew Dillon 				n >>= 1;
5944dcc5c2dSMatthew Dillon 				if (n == 0) {
5954dcc5c2dSMatthew Dillon 					swp_pager_meta_free(object, beg, start - beg);
5964dcc5c2dSMatthew Dillon 					splx(s);
5974dcc5c2dSMatthew Dillon 					return(-1);
5984dcc5c2dSMatthew Dillon 				}
5994dcc5c2dSMatthew Dillon 			}
6004dcc5c2dSMatthew Dillon 		}
6014dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, start, blk);
6024dcc5c2dSMatthew Dillon 		--size;
6034dcc5c2dSMatthew Dillon 		++start;
6044dcc5c2dSMatthew Dillon 		++blk;
6054dcc5c2dSMatthew Dillon 		--n;
6064dcc5c2dSMatthew Dillon 	}
6074dcc5c2dSMatthew Dillon 	swp_pager_meta_free(object, start, n);
6084dcc5c2dSMatthew Dillon 	splx(s);
6094dcc5c2dSMatthew Dillon 	return(0);
61026f9a767SRodney W. Grimes }
61126f9a767SRodney W. Grimes 
6120a47b48bSJohn Dyson /*
6131c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
6141c7c3c6aSMatthew Dillon  *			and destroy the source.
6151c7c3c6aSMatthew Dillon  *
6161c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
6171c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
6181c7c3c6aSMatthew Dillon  *	we keep the destination's.
6191c7c3c6aSMatthew Dillon  *
6201c7c3c6aSMatthew Dillon  *	This routine is allowed to block.  It may block allocating metadata
6211c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
6221c7c3c6aSMatthew Dillon  *	progress on the source.
6231c7c3c6aSMatthew Dillon  *
6244dcc5c2dSMatthew Dillon  *	This routine can be called at any spl
6254dcc5c2dSMatthew Dillon  *
6261c7c3c6aSMatthew Dillon  *	XXX vm_page_collapse() kinda expects us not to block because we
6271c7c3c6aSMatthew Dillon  *	supposedly do not need to allocate memory, but for the moment we
6281c7c3c6aSMatthew Dillon  *	*may* have to get a little memory from the zone allocator, but
6291c7c3c6aSMatthew Dillon  *	it is taken from the interrupt memory.  We should be ok.
6301c7c3c6aSMatthew Dillon  *
6311c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
6321c7c3c6aSMatthew Dillon  *
6331c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
6341c7c3c6aSMatthew Dillon  *
6354dcc5c2dSMatthew Dillon  *	The source and destination objects must be locked or
6364dcc5c2dSMatthew Dillon  *	inaccessible (XXX are they ?)
63726f9a767SRodney W. Grimes  */
63826f9a767SRodney W. Grimes 
63926f9a767SRodney W. Grimes void
6401c7c3c6aSMatthew Dillon swap_pager_copy(srcobject, dstobject, offset, destroysource)
64124a1cce3SDavid Greenman 	vm_object_t srcobject;
64224a1cce3SDavid Greenman 	vm_object_t dstobject;
643a316d390SJohn Dyson 	vm_pindex_t offset;
644c0877f10SJohn Dyson 	int destroysource;
64526f9a767SRodney W. Grimes {
646a316d390SJohn Dyson 	vm_pindex_t i;
6474dcc5c2dSMatthew Dillon 	int s;
6484dcc5c2dSMatthew Dillon 
6494dcc5c2dSMatthew Dillon 	s = splvm();
65026f9a767SRodney W. Grimes 
65123955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
65223955314SAlfred Perlstein 
65326f9a767SRodney W. Grimes 	/*
6541c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
6551c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
65626f9a767SRodney W. Grimes 	 */
6571c7c3c6aSMatthew Dillon 
658cbd8ec09SJohn Dyson 	if (destroysource) {
659a9fa2c05SAlfred Perlstein 		mtx_lock(&sw_alloc_mtx);
66024a1cce3SDavid Greenman 		if (srcobject->handle == NULL) {
6611c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
6621c7c3c6aSMatthew Dillon 			    &swap_pager_un_object_list,
6631c7c3c6aSMatthew Dillon 			    srcobject,
6641c7c3c6aSMatthew Dillon 			    pager_object_list
6651c7c3c6aSMatthew Dillon 			);
66626f9a767SRodney W. Grimes 		} else {
6671c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
6681c7c3c6aSMatthew Dillon 			    NOBJLIST(srcobject->handle),
6691c7c3c6aSMatthew Dillon 			    srcobject,
6701c7c3c6aSMatthew Dillon 			    pager_object_list
6711c7c3c6aSMatthew Dillon 			);
67226f9a767SRodney W. Grimes 		}
673a9fa2c05SAlfred Perlstein 		mtx_unlock(&sw_alloc_mtx);
674cbd8ec09SJohn Dyson 	}
67526f9a767SRodney W. Grimes 
6761c7c3c6aSMatthew Dillon 	/*
6771c7c3c6aSMatthew Dillon 	 * transfer source to destination.
6781c7c3c6aSMatthew Dillon 	 */
6791c7c3c6aSMatthew Dillon 
6801c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
6811c7c3c6aSMatthew Dillon 		daddr_t dstaddr;
6821c7c3c6aSMatthew Dillon 
6831c7c3c6aSMatthew Dillon 		/*
6841c7c3c6aSMatthew Dillon 		 * Locate (without changing) the swapblk on the destination,
6851c7c3c6aSMatthew Dillon 		 * unless it is invalid in which case free it silently, or
6861c7c3c6aSMatthew Dillon 		 * if the destination is a resident page, in which case the
6871c7c3c6aSMatthew Dillon 		 * source is thrown away.
6881c7c3c6aSMatthew Dillon 		 */
6891c7c3c6aSMatthew Dillon 
6901c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
6911c7c3c6aSMatthew Dillon 
6921c7c3c6aSMatthew Dillon 		if (dstaddr == SWAPBLK_NONE) {
6931c7c3c6aSMatthew Dillon 			/*
6941c7c3c6aSMatthew Dillon 			 * Destination has no swapblk and is not resident,
6951c7c3c6aSMatthew Dillon 			 * copy source.
6961c7c3c6aSMatthew Dillon 			 */
6971c7c3c6aSMatthew Dillon 			daddr_t srcaddr;
6981c7c3c6aSMatthew Dillon 
6991c7c3c6aSMatthew Dillon 			srcaddr = swp_pager_meta_ctl(
7001c7c3c6aSMatthew Dillon 			    srcobject,
7011c7c3c6aSMatthew Dillon 			    i + offset,
7021c7c3c6aSMatthew Dillon 			    SWM_POP
7031c7c3c6aSMatthew Dillon 			);
7041c7c3c6aSMatthew Dillon 
7051c7c3c6aSMatthew Dillon 			if (srcaddr != SWAPBLK_NONE)
7064dcc5c2dSMatthew Dillon 				swp_pager_meta_build(dstobject, i, srcaddr);
7071c7c3c6aSMatthew Dillon 		} else {
7081c7c3c6aSMatthew Dillon 			/*
7091c7c3c6aSMatthew Dillon 			 * Destination has valid swapblk or it is represented
7101c7c3c6aSMatthew Dillon 			 * by a resident page.  We destroy the sourceblock.
7111c7c3c6aSMatthew Dillon 			 */
7121c7c3c6aSMatthew Dillon 
7131c7c3c6aSMatthew Dillon 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
7141c7c3c6aSMatthew Dillon 		}
71526f9a767SRodney W. Grimes 	}
71626f9a767SRodney W. Grimes 
71726f9a767SRodney W. Grimes 	/*
7181c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
7191c7c3c6aSMatthew Dillon 	 *
7201c7c3c6aSMatthew Dillon 	 * We have to revert the type to OBJT_DEFAULT so we do not accidently
7211c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
72226f9a767SRodney W. Grimes 	 */
72326f9a767SRodney W. Grimes 
724c0877f10SJohn Dyson 	if (destroysource) {
7251c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
7261c7c3c6aSMatthew Dillon 		/*
7271c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
7281c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
729956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
7301c7c3c6aSMatthew Dillon 		 * queues.
7311c7c3c6aSMatthew Dillon 		 */
7321c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
733c0877f10SJohn Dyson 	}
7344dcc5c2dSMatthew Dillon 	splx(s);
73526f9a767SRodney W. Grimes }
73626f9a767SRodney W. Grimes 
737df8bae1dSRodney W. Grimes /*
7381c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
7391c7c3c6aSMatthew Dillon  *				the requested page.
7401c7c3c6aSMatthew Dillon  *
7411c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
7421c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
7431c7c3c6aSMatthew Dillon  *
7441c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
7451c7c3c6aSMatthew Dillon  *	store exists before and after the requested page within a reasonable
7461c7c3c6aSMatthew Dillon  *	distance.  We do not try to restrict it to the swap device stripe
7471c7c3c6aSMatthew Dillon  *	(that is handled in getpages/putpages).  It probably isn't worth
7481c7c3c6aSMatthew Dillon  *	doing here.
749df8bae1dSRodney W. Grimes  */
75026f9a767SRodney W. Grimes 
7511c7c3c6aSMatthew Dillon boolean_t
752a316d390SJohn Dyson swap_pager_haspage(object, pindex, before, after)
75324a1cce3SDavid Greenman 	vm_object_t object;
754a316d390SJohn Dyson 	vm_pindex_t pindex;
75524a1cce3SDavid Greenman 	int *before;
75624a1cce3SDavid Greenman 	int *after;
75726f9a767SRodney W. Grimes {
7581c7c3c6aSMatthew Dillon 	daddr_t blk0;
75925db2c54SMatthew Dillon 	int s;
76026f9a767SRodney W. Grimes 
7611c7c3c6aSMatthew Dillon 	/*
7621c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
7631c7c3c6aSMatthew Dillon 	 */
7641c7c3c6aSMatthew Dillon 
76525db2c54SMatthew Dillon 	s = splvm();
7661c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
7671c7c3c6aSMatthew Dillon 
7684dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
76925db2c54SMatthew Dillon 		splx(s);
7701c7c3c6aSMatthew Dillon 		if (before)
77124a1cce3SDavid Greenman 			*before = 0;
7721c7c3c6aSMatthew Dillon 		if (after)
77324a1cce3SDavid Greenman 			*after = 0;
77426f9a767SRodney W. Grimes 		return (FALSE);
77526f9a767SRodney W. Grimes 	}
77626f9a767SRodney W. Grimes 
77726f9a767SRodney W. Grimes 	/*
7781c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
779e47ed70bSJohn Dyson 	 */
780e47ed70bSJohn Dyson 
7811c7c3c6aSMatthew Dillon 	if (before != NULL) {
78226f9a767SRodney W. Grimes 		int i;
7830d94caffSDavid Greenman 
7841c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
7851c7c3c6aSMatthew Dillon 			daddr_t blk;
7861c7c3c6aSMatthew Dillon 
7871c7c3c6aSMatthew Dillon 			if (i > pindex)
7881c7c3c6aSMatthew Dillon 				break;
7891c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
7901c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
7911c7c3c6aSMatthew Dillon 				break;
792ffc82b0aSJohn Dyson 		}
7931c7c3c6aSMatthew Dillon 		*before = (i - 1);
79426f9a767SRodney W. Grimes 	}
79526f9a767SRodney W. Grimes 
79626f9a767SRodney W. Grimes 	/*
7971c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
79826f9a767SRodney W. Grimes 	 */
7991c7c3c6aSMatthew Dillon 
8001c7c3c6aSMatthew Dillon 	if (after != NULL) {
8011c7c3c6aSMatthew Dillon 		int i;
8021c7c3c6aSMatthew Dillon 
8031c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
8041c7c3c6aSMatthew Dillon 			daddr_t blk;
8051c7c3c6aSMatthew Dillon 
8061c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
8071c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
8081c7c3c6aSMatthew Dillon 				break;
80926f9a767SRodney W. Grimes 		}
8101c7c3c6aSMatthew Dillon 		*after = (i - 1);
8111c7c3c6aSMatthew Dillon 	}
81225db2c54SMatthew Dillon 	splx(s);
8131c7c3c6aSMatthew Dillon 	return (TRUE);
8141c7c3c6aSMatthew Dillon }
8151c7c3c6aSMatthew Dillon 
8161c7c3c6aSMatthew Dillon /*
8171c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
8181c7c3c6aSMatthew Dillon  *
8191c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
8201c7c3c6aSMatthew Dillon  *	not, from the page.
8211c7c3c6aSMatthew Dillon  *
8221c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
8231c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
8241c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
8251c7c3c6aSMatthew Dillon  *
8261c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
8271c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
8281c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
8291c7c3c6aSMatthew Dillon  *	depends on it.
8301c7c3c6aSMatthew Dillon  *
8311c7c3c6aSMatthew Dillon  *	This routine may not block
8324dcc5c2dSMatthew Dillon  *	This routine must be called at splvm()
8331c7c3c6aSMatthew Dillon  */
8341c7c3c6aSMatthew Dillon 
8351c7c3c6aSMatthew Dillon static void
8361c7c3c6aSMatthew Dillon swap_pager_unswapped(m)
8371c7c3c6aSMatthew Dillon 	vm_page_t m;
8381c7c3c6aSMatthew Dillon {
8391c7c3c6aSMatthew Dillon 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
8401c7c3c6aSMatthew Dillon }
8411c7c3c6aSMatthew Dillon 
8421c7c3c6aSMatthew Dillon /*
843a5296b05SJulian Elischer  * SWAP_PAGER_STRATEGY() - read, write, free blocks
844a5296b05SJulian Elischer  *
845a5296b05SJulian Elischer  *	This implements the vm_pager_strategy() interface to swap and allows
846a5296b05SJulian Elischer  *	other parts of the system to directly access swap as backing store
847a5296b05SJulian Elischer  *	through vm_objects of type OBJT_SWAP.  This is intended to be a
848a5296b05SJulian Elischer  *	cacheless interface ( i.e. caching occurs at higher levels ).
849a5296b05SJulian Elischer  *	Therefore we do not maintain any resident pages.  All I/O goes
8504dcc5c2dSMatthew Dillon  *	directly to and from the swap device.
851a5296b05SJulian Elischer  *
852a5296b05SJulian Elischer  *	Note that b_blkno is scaled for PAGE_SIZE
853a5296b05SJulian Elischer  *
854a5296b05SJulian Elischer  *	We currently attempt to run I/O synchronously or asynchronously as
855a5296b05SJulian Elischer  *	the caller requests.  This isn't perfect because we loose error
856a5296b05SJulian Elischer  *	sequencing when we run multiple ops in parallel to satisfy a request.
857a5296b05SJulian Elischer  *	But this is swap, so we let it all hang out.
858a5296b05SJulian Elischer  */
859a5296b05SJulian Elischer 
860a5296b05SJulian Elischer static void
8610b441832SPoul-Henning Kamp swap_pager_strategy(vm_object_t object, struct bio *bp)
862a5296b05SJulian Elischer {
863a5296b05SJulian Elischer 	vm_pindex_t start;
864a5296b05SJulian Elischer 	int count;
8654dcc5c2dSMatthew Dillon 	int s;
866a5296b05SJulian Elischer 	char *data;
867a5296b05SJulian Elischer 	struct buf *nbp = NULL;
868a5296b05SJulian Elischer 
8690b441832SPoul-Henning Kamp 	/* XXX: KASSERT instead ? */
8700b441832SPoul-Henning Kamp 	if (bp->bio_bcount & PAGE_MASK) {
871a468031cSPoul-Henning Kamp 		biofinish(bp, NULL, EINVAL);
8720b441832SPoul-Henning Kamp 		printf("swap_pager_strategy: bp %p blk %d size %d, not page bounded\n", bp, (int)bp->bio_pblkno, (int)bp->bio_bcount);
873a5296b05SJulian Elischer 		return;
874a5296b05SJulian Elischer 	}
875a5296b05SJulian Elischer 
876a5296b05SJulian Elischer 	/*
877a5296b05SJulian Elischer 	 * Clear error indication, initialize page index, count, data pointer.
878a5296b05SJulian Elischer 	 */
879a5296b05SJulian Elischer 
8800b441832SPoul-Henning Kamp 	bp->bio_error = 0;
8810b441832SPoul-Henning Kamp 	bp->bio_flags &= ~BIO_ERROR;
8820b441832SPoul-Henning Kamp 	bp->bio_resid = bp->bio_bcount;
883a5296b05SJulian Elischer 
8840b441832SPoul-Henning Kamp 	start = bp->bio_pblkno;
8850b441832SPoul-Henning Kamp 	count = howmany(bp->bio_bcount, PAGE_SIZE);
8860b441832SPoul-Henning Kamp 	data = bp->bio_data;
887a5296b05SJulian Elischer 
8884dcc5c2dSMatthew Dillon 	s = splvm();
8894dcc5c2dSMatthew Dillon 
890a5296b05SJulian Elischer 	/*
89121144e3bSPoul-Henning Kamp 	 * Deal with BIO_DELETE
892a5296b05SJulian Elischer 	 */
893a5296b05SJulian Elischer 
8940b441832SPoul-Henning Kamp 	if (bp->bio_cmd == BIO_DELETE) {
895a5296b05SJulian Elischer 		/*
896a5296b05SJulian Elischer 		 * FREE PAGE(s) - destroy underlying swap that is no longer
897a5296b05SJulian Elischer 		 *		  needed.
898a5296b05SJulian Elischer 		 */
89923955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
900a5296b05SJulian Elischer 		swp_pager_meta_free(object, start, count);
90123955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
902a5296b05SJulian Elischer 		splx(s);
9030b441832SPoul-Henning Kamp 		bp->bio_resid = 0;
9040b441832SPoul-Henning Kamp 		biodone(bp);
9054dcc5c2dSMatthew Dillon 		return;
9064dcc5c2dSMatthew Dillon 	}
9074dcc5c2dSMatthew Dillon 
908a5296b05SJulian Elischer 	/*
9094dcc5c2dSMatthew Dillon 	 * Execute read or write
910a5296b05SJulian Elischer 	 */
911a5296b05SJulian Elischer 
91223955314SAlfred Perlstein 	mtx_lock(&vm_mtx);
913a5296b05SJulian Elischer 	while (count > 0) {
914a5296b05SJulian Elischer 		daddr_t blk;
915a5296b05SJulian Elischer 
916a5296b05SJulian Elischer 		/*
9174dcc5c2dSMatthew Dillon 		 * Obtain block.  If block not found and writing, allocate a
9184dcc5c2dSMatthew Dillon 		 * new block and build it into the object.
9194dcc5c2dSMatthew Dillon 		 */
9204dcc5c2dSMatthew Dillon 
9214dcc5c2dSMatthew Dillon 		blk = swp_pager_meta_ctl(object, start, 0);
9220b441832SPoul-Henning Kamp 		if ((blk == SWAPBLK_NONE) && (bp->bio_cmd == BIO_WRITE)) {
9234dcc5c2dSMatthew Dillon 			blk = swp_pager_getswapspace(1);
9244dcc5c2dSMatthew Dillon 			if (blk == SWAPBLK_NONE) {
9250b441832SPoul-Henning Kamp 				bp->bio_error = ENOMEM;
9260b441832SPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
9274dcc5c2dSMatthew Dillon 				break;
9284dcc5c2dSMatthew Dillon 			}
9294dcc5c2dSMatthew Dillon 			swp_pager_meta_build(object, start, blk);
9304dcc5c2dSMatthew Dillon 		}
9314dcc5c2dSMatthew Dillon 
9324dcc5c2dSMatthew Dillon 		/*
9334dcc5c2dSMatthew Dillon 		 * Do we have to flush our current collection?  Yes if:
9344dcc5c2dSMatthew Dillon 		 *
9354dcc5c2dSMatthew Dillon 		 *	- no swap block at this index
9364dcc5c2dSMatthew Dillon 		 *	- swap block is not contiguous
9374dcc5c2dSMatthew Dillon 		 *	- we cross a physical disk boundry in the
9384dcc5c2dSMatthew Dillon 		 *	  stripe.
939a5296b05SJulian Elischer 		 */
940a5296b05SJulian Elischer 
941a5296b05SJulian Elischer 		if (
9424dcc5c2dSMatthew Dillon 		    nbp && (nbp->b_blkno + btoc(nbp->b_bcount) != blk ||
9434dcc5c2dSMatthew Dillon 		     ((nbp->b_blkno ^ blk) & dmmax_mask)
944a5296b05SJulian Elischer 		    )
945a5296b05SJulian Elischer 		) {
9464dcc5c2dSMatthew Dillon 			splx(s);
9470b441832SPoul-Henning Kamp 			if (bp->bio_cmd == BIO_READ) {
948a5296b05SJulian Elischer 				++cnt.v_swapin;
949a5296b05SJulian Elischer 				cnt.v_swappgsin += btoc(nbp->b_bcount);
9504dcc5c2dSMatthew Dillon 			} else {
9514dcc5c2dSMatthew Dillon 				++cnt.v_swapout;
9524dcc5c2dSMatthew Dillon 				cnt.v_swappgsout += btoc(nbp->b_bcount);
9534dcc5c2dSMatthew Dillon 				nbp->b_dirtyend = nbp->b_bcount;
9544dcc5c2dSMatthew Dillon 			}
955a5296b05SJulian Elischer 			flushchainbuf(nbp);
9564dcc5c2dSMatthew Dillon 			s = splvm();
957a5296b05SJulian Elischer 			nbp = NULL;
958a5296b05SJulian Elischer 		}
959a5296b05SJulian Elischer 
960a5296b05SJulian Elischer 		/*
9614dcc5c2dSMatthew Dillon 		 * Add new swapblk to nbp, instantiating nbp if necessary.
9624dcc5c2dSMatthew Dillon 		 * Zero-fill reads are able to take a shortcut.
963a5296b05SJulian Elischer 		 */
9644dcc5c2dSMatthew Dillon 
9654dcc5c2dSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
9664dcc5c2dSMatthew Dillon 			/*
9674dcc5c2dSMatthew Dillon 			 * We can only get here if we are reading.  Since
9684dcc5c2dSMatthew Dillon 			 * we are at splvm() we can safely modify b_resid,
9694dcc5c2dSMatthew Dillon 			 * even if chain ops are in progress.
9704dcc5c2dSMatthew Dillon 			 */
971a5296b05SJulian Elischer 			bzero(data, PAGE_SIZE);
9720b441832SPoul-Henning Kamp 			bp->bio_resid -= PAGE_SIZE;
973a5296b05SJulian Elischer 		} else {
974a5296b05SJulian Elischer 			if (nbp == NULL) {
97523955314SAlfred Perlstein 				mtx_unlock(&vm_mtx);
9760b441832SPoul-Henning Kamp 				nbp = getchainbuf(bp, swapdev_vp, B_ASYNC);
97723955314SAlfred Perlstein 				mtx_lock(&vm_mtx);
978a5296b05SJulian Elischer 				nbp->b_blkno = blk;
9794dcc5c2dSMatthew Dillon 				nbp->b_bcount = 0;
980a5296b05SJulian Elischer 				nbp->b_data = data;
981a5296b05SJulian Elischer 			}
982a5296b05SJulian Elischer 			nbp->b_bcount += PAGE_SIZE;
983a5296b05SJulian Elischer 		}
984a5296b05SJulian Elischer 		--count;
985a5296b05SJulian Elischer 		++start;
986a5296b05SJulian Elischer 		data += PAGE_SIZE;
987a5296b05SJulian Elischer 	}
988a5296b05SJulian Elischer 
989a5296b05SJulian Elischer 	/*
9904dcc5c2dSMatthew Dillon 	 *  Flush out last buffer
991a5296b05SJulian Elischer 	 */
992a5296b05SJulian Elischer 
993a5296b05SJulian Elischer 	splx(s);
994a5296b05SJulian Elischer 
995a5296b05SJulian Elischer 	if (nbp) {
99621144e3bSPoul-Henning Kamp 		if (nbp->b_iocmd == BIO_READ) {
997a5296b05SJulian Elischer 			++cnt.v_swapin;
998a5296b05SJulian Elischer 			cnt.v_swappgsin += btoc(nbp->b_bcount);
999a5296b05SJulian Elischer 		} else {
1000a5296b05SJulian Elischer 			++cnt.v_swapout;
1001a5296b05SJulian Elischer 			cnt.v_swappgsout += btoc(nbp->b_bcount);
10024dcc5c2dSMatthew Dillon 			nbp->b_dirtyend = nbp->b_bcount;
1003a5296b05SJulian Elischer 		}
1004a5296b05SJulian Elischer 		flushchainbuf(nbp);
10054dcc5c2dSMatthew Dillon 		/* nbp = NULL; */
1006a5296b05SJulian Elischer 	}
10074dcc5c2dSMatthew Dillon 
100823955314SAlfred Perlstein 	mtx_unlock(&vm_mtx);
10094dcc5c2dSMatthew Dillon 	/*
10104dcc5c2dSMatthew Dillon 	 * Wait for completion.
10114dcc5c2dSMatthew Dillon 	 */
10124dcc5c2dSMatthew Dillon 
1013a5296b05SJulian Elischer 	waitchainbuf(bp, 0, 1);
1014a5296b05SJulian Elischer }
1015a5296b05SJulian Elischer 
1016a5296b05SJulian Elischer /*
10171c7c3c6aSMatthew Dillon  * SWAP_PAGER_GETPAGES() - bring pages in from swap
10181c7c3c6aSMatthew Dillon  *
10191c7c3c6aSMatthew Dillon  *	Attempt to retrieve (m, count) pages from backing store, but make
10201c7c3c6aSMatthew Dillon  *	sure we retrieve at least m[reqpage].  We try to load in as large
10211c7c3c6aSMatthew Dillon  *	a chunk surrounding m[reqpage] as is contiguous in swap and which
10221c7c3c6aSMatthew Dillon  *	belongs to the same object.
10231c7c3c6aSMatthew Dillon  *
10241c7c3c6aSMatthew Dillon  *	The code is designed for asynchronous operation and
10251c7c3c6aSMatthew Dillon  *	immediate-notification of 'reqpage' but tends not to be
10261c7c3c6aSMatthew Dillon  *	used that way.  Please do not optimize-out this algorithmic
10271c7c3c6aSMatthew Dillon  *	feature, I intend to improve on it in the future.
10281c7c3c6aSMatthew Dillon  *
10291c7c3c6aSMatthew Dillon  *	The parent has a single vm_object_pip_add() reference prior to
10301c7c3c6aSMatthew Dillon  *	calling us and we should return with the same.
10311c7c3c6aSMatthew Dillon  *
10321c7c3c6aSMatthew Dillon  *	The parent has BUSY'd the pages.  We should return with 'm'
10331c7c3c6aSMatthew Dillon  *	left busy, but the others adjusted.
10341c7c3c6aSMatthew Dillon  */
103526f9a767SRodney W. Grimes 
1036f708ef1bSPoul-Henning Kamp static int
103724a1cce3SDavid Greenman swap_pager_getpages(object, m, count, reqpage)
103824a1cce3SDavid Greenman 	vm_object_t object;
103926f9a767SRodney W. Grimes 	vm_page_t *m;
104026f9a767SRodney W. Grimes 	int count, reqpage;
1041df8bae1dSRodney W. Grimes {
10421c7c3c6aSMatthew Dillon 	struct buf *bp;
10431c7c3c6aSMatthew Dillon 	vm_page_t mreq;
10441c7c3c6aSMatthew Dillon 	int s;
104526f9a767SRodney W. Grimes 	int i;
104626f9a767SRodney W. Grimes 	int j;
10471c7c3c6aSMatthew Dillon 	daddr_t blk;
10481c7c3c6aSMatthew Dillon 	vm_offset_t kva;
10491c7c3c6aSMatthew Dillon 	vm_pindex_t lastpindex;
10500d94caffSDavid Greenman 
10511c7c3c6aSMatthew Dillon 	mreq = m[reqpage];
10521c7c3c6aSMatthew Dillon 
10531c7c3c6aSMatthew Dillon 	if (mreq->object != object) {
10541c7c3c6aSMatthew Dillon 		panic("swap_pager_getpages: object mismatch %p/%p",
10551c7c3c6aSMatthew Dillon 		    object,
10561c7c3c6aSMatthew Dillon 		    mreq->object
10571c7c3c6aSMatthew Dillon 		);
105826f9a767SRodney W. Grimes 	}
10591c7c3c6aSMatthew Dillon 	/*
10601c7c3c6aSMatthew Dillon 	 * Calculate range to retrieve.  The pages have already been assigned
10611c7c3c6aSMatthew Dillon 	 * their swapblks.  We require a *contiguous* range that falls entirely
10621c7c3c6aSMatthew Dillon 	 * within a single device stripe.   If we do not supply it, bad things
10634dcc5c2dSMatthew Dillon 	 * happen.  Note that blk, iblk & jblk can be SWAPBLK_NONE, but the
10644dcc5c2dSMatthew Dillon 	 * loops are set up such that the case(s) are handled implicitly.
10654dcc5c2dSMatthew Dillon 	 *
10664dcc5c2dSMatthew Dillon 	 * The swp_*() calls must be made at splvm().  vm_page_free() does
10674dcc5c2dSMatthew Dillon 	 * not need to be, but it will go a little faster if it is.
10681c7c3c6aSMatthew Dillon 	 */
10691c7c3c6aSMatthew Dillon 
10704dcc5c2dSMatthew Dillon 	s = splvm();
10711c7c3c6aSMatthew Dillon 	blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0);
10721c7c3c6aSMatthew Dillon 
10731c7c3c6aSMatthew Dillon 	for (i = reqpage - 1; i >= 0; --i) {
10741c7c3c6aSMatthew Dillon 		daddr_t iblk;
10751c7c3c6aSMatthew Dillon 
10761c7c3c6aSMatthew Dillon 		iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0);
10771c7c3c6aSMatthew Dillon 		if (blk != iblk + (reqpage - i))
107826f9a767SRodney W. Grimes 			break;
10794dcc5c2dSMatthew Dillon 		if ((blk ^ iblk) & dmmax_mask)
10804dcc5c2dSMatthew Dillon 			break;
108126f9a767SRodney W. Grimes 	}
10821c7c3c6aSMatthew Dillon 	++i;
10831c7c3c6aSMatthew Dillon 
10841c7c3c6aSMatthew Dillon 	for (j = reqpage + 1; j < count; ++j) {
10851c7c3c6aSMatthew Dillon 		daddr_t jblk;
10861c7c3c6aSMatthew Dillon 
10871c7c3c6aSMatthew Dillon 		jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0);
10881c7c3c6aSMatthew Dillon 		if (blk != jblk - (j - reqpage))
10891c7c3c6aSMatthew Dillon 			break;
10904dcc5c2dSMatthew Dillon 		if ((blk ^ jblk) & dmmax_mask)
10914dcc5c2dSMatthew Dillon 			break;
10921c7c3c6aSMatthew Dillon 	}
10931c7c3c6aSMatthew Dillon 
10941c7c3c6aSMatthew Dillon 	/*
10951c7c3c6aSMatthew Dillon 	 * free pages outside our collection range.   Note: we never free
10961c7c3c6aSMatthew Dillon 	 * mreq, it must remain busy throughout.
10971c7c3c6aSMatthew Dillon 	 */
10981c7c3c6aSMatthew Dillon 
10991c7c3c6aSMatthew Dillon 	{
11001c7c3c6aSMatthew Dillon 		int k;
11011c7c3c6aSMatthew Dillon 
11024dcc5c2dSMatthew Dillon 		for (k = 0; k < i; ++k)
11034dcc5c2dSMatthew Dillon 			vm_page_free(m[k]);
11044dcc5c2dSMatthew Dillon 		for (k = j; k < count; ++k)
11051c7c3c6aSMatthew Dillon 			vm_page_free(m[k]);
11061c7c3c6aSMatthew Dillon 	}
11074dcc5c2dSMatthew Dillon 	splx(s);
11084dcc5c2dSMatthew Dillon 
11091c7c3c6aSMatthew Dillon 
11101c7c3c6aSMatthew Dillon 	/*
11114dcc5c2dSMatthew Dillon 	 * Return VM_PAGER_FAIL if we have nothing to do.  Return mreq
11124dcc5c2dSMatthew Dillon 	 * still busy, but the others unbusied.
11131c7c3c6aSMatthew Dillon 	 */
11141c7c3c6aSMatthew Dillon 
11154dcc5c2dSMatthew Dillon 	if (blk == SWAPBLK_NONE)
111626f9a767SRodney W. Grimes 		return(VM_PAGER_FAIL);
1117df8bae1dSRodney W. Grimes 
111816f62314SDavid Greenman 	/*
111916f62314SDavid Greenman 	 * Get a swap buffer header to perform the IO
112016f62314SDavid Greenman 	 */
11211c7c3c6aSMatthew Dillon 
11221c7c3c6aSMatthew Dillon 	bp = getpbuf(&nsw_rcount);
112316f62314SDavid Greenman 	kva = (vm_offset_t) bp->b_data;
112426f9a767SRodney W. Grimes 
112516f62314SDavid Greenman 	/*
112616f62314SDavid Greenman 	 * map our page(s) into kva for input
11271c7c3c6aSMatthew Dillon 	 *
11281c7c3c6aSMatthew Dillon 	 * NOTE: B_PAGING is set by pbgetvp()
112916f62314SDavid Greenman 	 */
113016f62314SDavid Greenman 
11311c7c3c6aSMatthew Dillon 	pmap_qenter(kva, m + i, j - i);
11321c7c3c6aSMatthew Dillon 
113321144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
11341c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1135b0eeea20SPoul-Henning Kamp 	bp->b_rcred = bp->b_wcred = proc0.p_ucred;
1136a5296b05SJulian Elischer 	bp->b_data = (caddr_t) kva;
113726f9a767SRodney W. Grimes 	crhold(bp->b_rcred);
113826f9a767SRodney W. Grimes 	crhold(bp->b_wcred);
11391c7c3c6aSMatthew Dillon 	bp->b_blkno = blk - (reqpage - i);
11401c7c3c6aSMatthew Dillon 	bp->b_bcount = PAGE_SIZE * (j - i);
11411c7c3c6aSMatthew Dillon 	bp->b_bufsize = PAGE_SIZE * (j - i);
11421c7c3c6aSMatthew Dillon 	bp->b_pager.pg_reqpage = reqpage - i;
11431c7c3c6aSMatthew Dillon 
11441c7c3c6aSMatthew Dillon 	{
11451c7c3c6aSMatthew Dillon 		int k;
11461c7c3c6aSMatthew Dillon 
11471c7c3c6aSMatthew Dillon 		for (k = i; k < j; ++k) {
11481c7c3c6aSMatthew Dillon 			bp->b_pages[k - i] = m[k];
11491c7c3c6aSMatthew Dillon 			vm_page_flag_set(m[k], PG_SWAPINPROG);
11501c7c3c6aSMatthew Dillon 		}
11511c7c3c6aSMatthew Dillon 	}
11521c7c3c6aSMatthew Dillon 	bp->b_npages = j - i;
115326f9a767SRodney W. Grimes 
11540d94caffSDavid Greenman 	pbgetvp(swapdev_vp, bp);
1155df8bae1dSRodney W. Grimes 
1156976e77fcSDavid Greenman 	cnt.v_swapin++;
11571c7c3c6aSMatthew Dillon 	cnt.v_swappgsin += bp->b_npages;
11581c7c3c6aSMatthew Dillon 
1159df8bae1dSRodney W. Grimes 	/*
11601c7c3c6aSMatthew Dillon 	 * We still hold the lock on mreq, and our automatic completion routine
11611c7c3c6aSMatthew Dillon 	 * does not remove it.
1162df8bae1dSRodney W. Grimes 	 */
11631c7c3c6aSMatthew Dillon 
11641c7c3c6aSMatthew Dillon 	vm_object_pip_add(mreq->object, bp->b_npages);
11651c7c3c6aSMatthew Dillon 	lastpindex = m[j-1]->pindex;
11661c7c3c6aSMatthew Dillon 
11671c7c3c6aSMatthew Dillon 	/*
11681c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
11691c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
11701c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
11711c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
11721c7c3c6aSMatthew Dillon 	 *
11731c7c3c6aSMatthew Dillon 	 * The other pages in our m[] array are also released on completion,
11741c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
11751c7c3c6aSMatthew Dillon 	 *
1176ea3aecf5SPeter Wemm 	 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
11771c7c3c6aSMatthew Dillon 	 */
11781c7c3c6aSMatthew Dillon 
1179b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
1180b99c307aSPoul-Henning Kamp 	BUF_STRATEGY(bp);
118126f9a767SRodney W. Grimes 
118226f9a767SRodney W. Grimes 	/*
11831c7c3c6aSMatthew Dillon 	 * wait for the page we want to complete.  PG_SWAPINPROG is always
11841c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
11851c7c3c6aSMatthew Dillon 	 * is set in the meta-data.
118626f9a767SRodney W. Grimes 	 */
11871b119d9dSDavid Greenman 
11881c7c3c6aSMatthew Dillon 	s = splvm();
11891c7c3c6aSMatthew Dillon 
11901c7c3c6aSMatthew Dillon 	while ((mreq->flags & PG_SWAPINPROG) != 0) {
11911c7c3c6aSMatthew Dillon 		vm_page_flag_set(mreq, PG_WANTED | PG_REFERENCED);
11921c7c3c6aSMatthew Dillon 		cnt.v_intrans++;
11931c7c3c6aSMatthew Dillon 		if (tsleep(mreq, PSWP, "swread", hz*20)) {
1194ac1e407bSBruce Evans 			printf(
11951c7c3c6aSMatthew Dillon 			    "swap_pager: indefinite wait buffer: device:"
1196af647ddeSBruce Evans 				" %s, blkno: %ld, size: %ld\n",
1197af647ddeSBruce Evans 			    devtoname(bp->b_dev), (long)bp->b_blkno,
1198af647ddeSBruce Evans 			    bp->b_bcount
11991c7c3c6aSMatthew Dillon 			);
12001c7c3c6aSMatthew Dillon 		}
12011b119d9dSDavid Greenman 	}
120226f9a767SRodney W. Grimes 
1203df8bae1dSRodney W. Grimes 	splx(s);
120426f9a767SRodney W. Grimes 
120526f9a767SRodney W. Grimes 	/*
12061c7c3c6aSMatthew Dillon 	 * mreq is left bussied after completion, but all the other pages
12071c7c3c6aSMatthew Dillon 	 * are freed.  If we had an unrecoverable read error the page will
12081c7c3c6aSMatthew Dillon 	 * not be valid.
120926f9a767SRodney W. Grimes 	 */
121026f9a767SRodney W. Grimes 
12111c7c3c6aSMatthew Dillon 	if (mreq->valid != VM_PAGE_BITS_ALL) {
12121c7c3c6aSMatthew Dillon 		return(VM_PAGER_ERROR);
121326f9a767SRodney W. Grimes 	} else {
12141c7c3c6aSMatthew Dillon 		return(VM_PAGER_OK);
121526f9a767SRodney W. Grimes 	}
12161c7c3c6aSMatthew Dillon 
12171c7c3c6aSMatthew Dillon 	/*
12181c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
12191c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
12201c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
12211c7c3c6aSMatthew Dillon 	 * to all-zero's later.
12221c7c3c6aSMatthew Dillon 	 */
1223df8bae1dSRodney W. Grimes }
1224df8bae1dSRodney W. Grimes 
12251c7c3c6aSMatthew Dillon /*
12261c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
12271c7c3c6aSMatthew Dillon  *
12281c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
12291c7c3c6aSMatthew Dillon  *
12301c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
12311c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
12321c7c3c6aSMatthew Dillon  *
1233ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
12341c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
12351c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
12361c7c3c6aSMatthew Dillon  *	which needs work.
12371c7c3c6aSMatthew Dillon  *
12381c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
12391c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
12401c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
12411c7c3c6aSMatthew Dillon  *	completion.
12421c7c3c6aSMatthew Dillon  *
12431c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
12441c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
12451c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
12461c7c3c6aSMatthew Dillon  */
12471c7c3c6aSMatthew Dillon 
1248e4542174SMatthew Dillon void
124924a1cce3SDavid Greenman swap_pager_putpages(object, m, count, sync, rtvals)
125024a1cce3SDavid Greenman 	vm_object_t object;
125126f9a767SRodney W. Grimes 	vm_page_t *m;
125226f9a767SRodney W. Grimes 	int count;
125324a1cce3SDavid Greenman 	boolean_t sync;
125426f9a767SRodney W. Grimes 	int *rtvals;
1255df8bae1dSRodney W. Grimes {
12561c7c3c6aSMatthew Dillon 	int i;
12571c7c3c6aSMatthew Dillon 	int n = 0;
1258df8bae1dSRodney W. Grimes 
12591c7c3c6aSMatthew Dillon 	if (count && m[0]->object != object) {
12601c7c3c6aSMatthew Dillon 		panic("swap_pager_getpages: object mismatch %p/%p",
12611c7c3c6aSMatthew Dillon 		    object,
12621c7c3c6aSMatthew Dillon 		    m[0]->object
12631c7c3c6aSMatthew Dillon 		);
12641c7c3c6aSMatthew Dillon 	}
12651c7c3c6aSMatthew Dillon 	/*
12661c7c3c6aSMatthew Dillon 	 * Step 1
12671c7c3c6aSMatthew Dillon 	 *
12681c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
12691c7c3c6aSMatthew Dillon 	 * check for bogus sysops
12701c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
12711c7c3c6aSMatthew Dillon 	 */
1272e736cd05SJohn Dyson 
12734dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
12744dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
1275e47ed70bSJohn Dyson 
1276e47ed70bSJohn Dyson 	if (curproc != pageproc)
1277e47ed70bSJohn Dyson 		sync = TRUE;
127826f9a767SRodney W. Grimes 
12791c7c3c6aSMatthew Dillon 	/*
12801c7c3c6aSMatthew Dillon 	 * Step 2
12811c7c3c6aSMatthew Dillon 	 *
1282ad3cce20SMatthew Dillon 	 * Update nsw parameters from swap_async_max sysctl values.
1283ad3cce20SMatthew Dillon 	 * Do not let the sysop crash the machine with bogus numbers.
1284327f4e83SMatthew Dillon 	 */
1285327f4e83SMatthew Dillon 
1286327f4e83SMatthew Dillon 	if (swap_async_max != nsw_wcount_async_max) {
1287327f4e83SMatthew Dillon 		int n;
1288327f4e83SMatthew Dillon 		int s;
1289327f4e83SMatthew Dillon 
1290327f4e83SMatthew Dillon 		/*
1291327f4e83SMatthew Dillon 		 * limit range
1292327f4e83SMatthew Dillon 		 */
1293327f4e83SMatthew Dillon 		if ((n = swap_async_max) > nswbuf / 2)
1294327f4e83SMatthew Dillon 			n = nswbuf / 2;
1295327f4e83SMatthew Dillon 		if (n < 1)
1296327f4e83SMatthew Dillon 			n = 1;
1297327f4e83SMatthew Dillon 		swap_async_max = n;
1298327f4e83SMatthew Dillon 
1299327f4e83SMatthew Dillon 		/*
1300327f4e83SMatthew Dillon 		 * Adjust difference ( if possible ).  If the current async
1301327f4e83SMatthew Dillon 		 * count is too low, we may not be able to make the adjustment
1302327f4e83SMatthew Dillon 		 * at this time.
1303327f4e83SMatthew Dillon 		 */
1304327f4e83SMatthew Dillon 		s = splvm();
130523955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
13062a758ebeSAlfred Perlstein 		mtx_lock(&pbuf_mtx);
1307327f4e83SMatthew Dillon 		n -= nsw_wcount_async_max;
1308327f4e83SMatthew Dillon 		if (nsw_wcount_async + n >= 0) {
1309327f4e83SMatthew Dillon 			nsw_wcount_async += n;
1310327f4e83SMatthew Dillon 			nsw_wcount_async_max += n;
1311327f4e83SMatthew Dillon 			wakeup(&nsw_wcount_async);
1312327f4e83SMatthew Dillon 		}
13132a758ebeSAlfred Perlstein 		mtx_unlock(&pbuf_mtx);
131423955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
1315327f4e83SMatthew Dillon 		splx(s);
1316327f4e83SMatthew Dillon 	}
1317327f4e83SMatthew Dillon 
1318327f4e83SMatthew Dillon 	/*
1319327f4e83SMatthew Dillon 	 * Step 3
1320327f4e83SMatthew Dillon 	 *
13211c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
13221c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
13231c7c3c6aSMatthew Dillon 	 * successfully.
13241c7c3c6aSMatthew Dillon 	 */
132526f9a767SRodney W. Grimes 
13261c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
13271c7c3c6aSMatthew Dillon 		int s;
13281c7c3c6aSMatthew Dillon 		int j;
13291c7c3c6aSMatthew Dillon 		struct buf *bp;
1330a316d390SJohn Dyson 		daddr_t blk;
133126f9a767SRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 		/*
13331c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1334df8bae1dSRodney W. Grimes 		 */
133526f9a767SRodney W. Grimes 
13361c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1337327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
13381c7c3c6aSMatthew Dillon 
13394dcc5c2dSMatthew Dillon 		s = splvm();
13404dcc5c2dSMatthew Dillon 
134126f9a767SRodney W. Grimes 		/*
13421c7c3c6aSMatthew Dillon 		 * Get biggest block of swap we can.  If we fail, fall
13431c7c3c6aSMatthew Dillon 		 * back and try to allocate a smaller block.  Don't go
13441c7c3c6aSMatthew Dillon 		 * overboard trying to allocate space if it would overly
13451c7c3c6aSMatthew Dillon 		 * fragment swap.
134626f9a767SRodney W. Grimes 		 */
13471c7c3c6aSMatthew Dillon 		while (
13481c7c3c6aSMatthew Dillon 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
13491c7c3c6aSMatthew Dillon 		    n > 4
13501c7c3c6aSMatthew Dillon 		) {
13511c7c3c6aSMatthew Dillon 			n >>= 1;
135226f9a767SRodney W. Grimes 		}
13531c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
13544dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
13551c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
13564dcc5c2dSMatthew Dillon 			splx(s);
13571c7c3c6aSMatthew Dillon 			continue;
135826f9a767SRodney W. Grimes 		}
135926f9a767SRodney W. Grimes 
136026f9a767SRodney W. Grimes 		/*
13614dcc5c2dSMatthew Dillon 		 * The I/O we are constructing cannot cross a physical
13624dcc5c2dSMatthew Dillon 		 * disk boundry in the swap stripe.  Note: we are still
13634dcc5c2dSMatthew Dillon 		 * at splvm().
136426f9a767SRodney W. Grimes 		 */
13651c7c3c6aSMatthew Dillon 		if ((blk ^ (blk + n)) & dmmax_mask) {
13661c7c3c6aSMatthew Dillon 			j = ((blk + dmmax) & dmmax_mask) - blk;
13671c7c3c6aSMatthew Dillon 			swp_pager_freeswapspace(blk + j, n - j);
13681c7c3c6aSMatthew Dillon 			n = j;
1369e47ed70bSJohn Dyson 		}
137026f9a767SRodney W. Grimes 
137126f9a767SRodney W. Grimes 		/*
13721c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
13731c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
13741c7c3c6aSMatthew Dillon 		 *
13751c7c3c6aSMatthew Dillon 		 * NOTE: B_PAGING is set by pbgetvp()
137626f9a767SRodney W. Grimes 		 */
137726f9a767SRodney W. Grimes 
1378327f4e83SMatthew Dillon 		if (sync == TRUE) {
1379327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_sync);
1380327f4e83SMatthew Dillon 		} else {
1381327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_async);
138221144e3bSPoul-Henning Kamp 			bp->b_flags = B_ASYNC;
1383327f4e83SMatthew Dillon 		}
1384912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
13851c7c3c6aSMatthew Dillon 		bp->b_spc = NULL;	/* not used, but NULL-out anyway */
138626f9a767SRodney W. Grimes 
13871c7c3c6aSMatthew Dillon 		pmap_qenter((vm_offset_t)bp->b_data, &m[i], n);
13881c7c3c6aSMatthew Dillon 
1389b0eeea20SPoul-Henning Kamp 		bp->b_rcred = bp->b_wcred = proc0.p_ucred;
13901c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
13911c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
13921c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1393e47ed70bSJohn Dyson 
1394a5296b05SJulian Elischer 		crhold(bp->b_rcred);
1395a5296b05SJulian Elischer 		crhold(bp->b_wcred);
1396a5296b05SJulian Elischer 
1397a5296b05SJulian Elischer 		pbgetvp(swapdev_vp, bp);
1398a5296b05SJulian Elischer 
13991c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
14001c7c3c6aSMatthew Dillon 			vm_page_t mreq = m[i+j];
14011c7c3c6aSMatthew Dillon 
14021c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
14031c7c3c6aSMatthew Dillon 			    mreq->object,
14041c7c3c6aSMatthew Dillon 			    mreq->pindex,
14054dcc5c2dSMatthew Dillon 			    blk + j
14061c7c3c6aSMatthew Dillon 			);
14077dbf82dcSMatthew Dillon 			vm_page_dirty(mreq);
14081c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_OK;
14091c7c3c6aSMatthew Dillon 
14101c7c3c6aSMatthew Dillon 			vm_page_flag_set(mreq, PG_SWAPINPROG);
14111c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
14121c7c3c6aSMatthew Dillon 		}
14131c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1414a5296b05SJulian Elischer 		/*
1415a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1416a5296b05SJulian Elischer 		 */
1417a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1418a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
14191c7c3c6aSMatthew Dillon 
14201c7c3c6aSMatthew Dillon 		cnt.v_swapout++;
14211c7c3c6aSMatthew Dillon 		cnt.v_swappgsout += bp->b_npages;
142226f9a767SRodney W. Grimes 		swapdev_vp->v_numoutput++;
142326f9a767SRodney W. Grimes 
14244dcc5c2dSMatthew Dillon 		splx(s);
142523955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
142623955314SAlfred Perlstein 		mtx_lock(&Giant);
14274dcc5c2dSMatthew Dillon 
142826f9a767SRodney W. Grimes 		/*
14291c7c3c6aSMatthew Dillon 		 * asynchronous
14301c7c3c6aSMatthew Dillon 		 *
1431ea3aecf5SPeter Wemm 		 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
143226f9a767SRodney W. Grimes 		 */
1433e47ed70bSJohn Dyson 
14341c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
14351c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
143667812eacSKirk McKusick 			BUF_KERNPROC(bp);
1437b99c307aSPoul-Henning Kamp 			BUF_STRATEGY(bp);
143823955314SAlfred Perlstein 			mtx_unlock(&Giant);
143923955314SAlfred Perlstein 			mtx_lock(&vm_mtx);
14401c7c3c6aSMatthew Dillon 
14411c7c3c6aSMatthew Dillon 			for (j = 0; j < n; ++j)
14421c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_PEND;
144323955314SAlfred Perlstein 			/* restart outter loop */
14441c7c3c6aSMatthew Dillon 			continue;
144526f9a767SRodney W. Grimes 		}
1446e47ed70bSJohn Dyson 
144726f9a767SRodney W. Grimes 		/*
14481c7c3c6aSMatthew Dillon 		 * synchronous
14491c7c3c6aSMatthew Dillon 		 *
1450ea3aecf5SPeter Wemm 		 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
14511c7c3c6aSMatthew Dillon 		 */
14521c7c3c6aSMatthew Dillon 
14531c7c3c6aSMatthew Dillon 		bp->b_iodone = swp_pager_sync_iodone;
1454b99c307aSPoul-Henning Kamp 		BUF_STRATEGY(bp);
14551c7c3c6aSMatthew Dillon 
14561c7c3c6aSMatthew Dillon 		/*
14571c7c3c6aSMatthew Dillon 		 * Wait for the sync I/O to complete, then update rtvals.
14581c7c3c6aSMatthew Dillon 		 * We just set the rtvals[] to VM_PAGER_PEND so we can call
14591c7c3c6aSMatthew Dillon 		 * our async completion routine at the end, thus avoiding a
14601c7c3c6aSMatthew Dillon 		 * double-free.
146126f9a767SRodney W. Grimes 		 */
14624dcc5c2dSMatthew Dillon 		s = splbio();
14634dcc5c2dSMatthew Dillon 
146426f9a767SRodney W. Grimes 		while ((bp->b_flags & B_DONE) == 0) {
146524a1cce3SDavid Greenman 			tsleep(bp, PVM, "swwrt", 0);
146626f9a767SRodney W. Grimes 		}
1467e47ed70bSJohn Dyson 
14681c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j)
14691c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_PEND;
147026f9a767SRodney W. Grimes 
14711c7c3c6aSMatthew Dillon 		/*
14721c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
14731c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
14741c7c3c6aSMatthew Dillon 		 */
14751c7c3c6aSMatthew Dillon 
147623955314SAlfred Perlstein 		mtx_unlock(&Giant);
147723955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
14781c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
147926f9a767SRodney W. Grimes 
148026f9a767SRodney W. Grimes 		splx(s);
14811c7c3c6aSMatthew Dillon 	}
14821c7c3c6aSMatthew Dillon }
14831c7c3c6aSMatthew Dillon 
14841c7c3c6aSMatthew Dillon /*
14851c7c3c6aSMatthew Dillon  *	swap_pager_sync_iodone:
14861c7c3c6aSMatthew Dillon  *
14871c7c3c6aSMatthew Dillon  *	Completion routine for synchronous reads and writes from/to swap.
14881c7c3c6aSMatthew Dillon  *	We just mark the bp is complete and wake up anyone waiting on it.
14891c7c3c6aSMatthew Dillon  *
14904dcc5c2dSMatthew Dillon  *	This routine may not block.  This routine is called at splbio() or better.
14911c7c3c6aSMatthew Dillon  */
14921c7c3c6aSMatthew Dillon 
14931c7c3c6aSMatthew Dillon static void
14941c7c3c6aSMatthew Dillon swp_pager_sync_iodone(bp)
14951c7c3c6aSMatthew Dillon 	struct buf *bp;
14961c7c3c6aSMatthew Dillon {
14971c7c3c6aSMatthew Dillon 	bp->b_flags |= B_DONE;
14981c7c3c6aSMatthew Dillon 	bp->b_flags &= ~B_ASYNC;
14991c7c3c6aSMatthew Dillon 	wakeup(bp);
15001c7c3c6aSMatthew Dillon }
15011c7c3c6aSMatthew Dillon 
15021c7c3c6aSMatthew Dillon /*
15031c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
15041c7c3c6aSMatthew Dillon  *
15051c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
15061c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
15071c7c3c6aSMatthew Dillon  *
15081c7c3c6aSMatthew Dillon  *	For READ operations, the pages are PG_BUSY'd.  For WRITE operations,
15091c7c3c6aSMatthew Dillon  *	the pages are vm_page_t->busy'd.  For READ operations, we PG_BUSY
15101c7c3c6aSMatthew Dillon  *	unbusy all pages except the 'main' request page.  For WRITE
15111c7c3c6aSMatthew Dillon  *	operations, we vm_page_t->busy'd unbusy all pages ( we can do this
15121c7c3c6aSMatthew Dillon  *	because we marked them all VM_PAGER_PEND on return from putpages ).
15131c7c3c6aSMatthew Dillon  *
15141c7c3c6aSMatthew Dillon  *	This routine may not block.
15154dcc5c2dSMatthew Dillon  *	This routine is called at splbio() or better
15164dcc5c2dSMatthew Dillon  *
15174dcc5c2dSMatthew Dillon  *	We up ourselves to splvm() as required for various vm_page related
15184dcc5c2dSMatthew Dillon  *	calls.
15191c7c3c6aSMatthew Dillon  */
15201c7c3c6aSMatthew Dillon 
15211c7c3c6aSMatthew Dillon static void
15221c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp)
15231c7c3c6aSMatthew Dillon 	register struct buf *bp;
15241c7c3c6aSMatthew Dillon {
15251c7c3c6aSMatthew Dillon 	int s;
15261c7c3c6aSMatthew Dillon 	int i;
15271c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
15281c7c3c6aSMatthew Dillon 
15291c7c3c6aSMatthew Dillon 	bp->b_flags |= B_DONE;
15301c7c3c6aSMatthew Dillon 
15311c7c3c6aSMatthew Dillon 	/*
15321c7c3c6aSMatthew Dillon 	 * report error
15331c7c3c6aSMatthew Dillon 	 */
15341c7c3c6aSMatthew Dillon 
1535c244d2deSPoul-Henning Kamp 	if (bp->b_ioflags & BIO_ERROR) {
15361c7c3c6aSMatthew Dillon 		printf(
15371c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
15381c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
153921144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
15401c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
15411c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
15421c7c3c6aSMatthew Dillon 		    bp->b_error
15431c7c3c6aSMatthew Dillon 		);
15441c7c3c6aSMatthew Dillon 	}
15451c7c3c6aSMatthew Dillon 
15461c7c3c6aSMatthew Dillon 	/*
15474dcc5c2dSMatthew Dillon 	 * set object, raise to splvm().
15481c7c3c6aSMatthew Dillon 	 */
15491c7c3c6aSMatthew Dillon 
15501c7c3c6aSMatthew Dillon 	if (bp->b_npages)
15511c7c3c6aSMatthew Dillon 		object = bp->b_pages[0]->object;
15524dcc5c2dSMatthew Dillon 	s = splvm();
155326f9a767SRodney W. Grimes 
155426f9a767SRodney W. Grimes 	/*
155526f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
155626f9a767SRodney W. Grimes 	 */
15571c7c3c6aSMatthew Dillon 
15581c7c3c6aSMatthew Dillon 	pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
155926f9a767SRodney W. Grimes 
156026f9a767SRodney W. Grimes 	/*
15611c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
15621c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
15631c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
15641c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
15651c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
15661c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
156726f9a767SRodney W. Grimes 	 */
156826f9a767SRodney W. Grimes 
15691c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
15701c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1571e47ed70bSJohn Dyson 
15721c7c3c6aSMatthew Dillon 		vm_page_flag_clear(m, PG_SWAPINPROG);
1573e47ed70bSJohn Dyson 
1574c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1575ffc82b0aSJohn Dyson 			/*
15761c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
15771c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
15781c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
15791c7c3c6aSMatthew Dillon 			 * interrupt.
1580ffc82b0aSJohn Dyson 			 */
15811c7c3c6aSMatthew Dillon 
158221144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
15831c7c3c6aSMatthew Dillon 				/*
15841c7c3c6aSMatthew Dillon 				 * When reading, reqpage needs to stay
15851c7c3c6aSMatthew Dillon 				 * locked for the parent, but all other
15861c7c3c6aSMatthew Dillon 				 * pages can be freed.  We still want to
15871c7c3c6aSMatthew Dillon 				 * wakeup the parent waiting on the page,
15881c7c3c6aSMatthew Dillon 				 * though.  ( also: pg_reqpage can be -1 and
15891c7c3c6aSMatthew Dillon 				 * not match anything ).
15901c7c3c6aSMatthew Dillon 				 *
15911c7c3c6aSMatthew Dillon 				 * We have to wake specifically requested pages
15921c7c3c6aSMatthew Dillon 				 * up too because we cleared PG_SWAPINPROG and
15931c7c3c6aSMatthew Dillon 				 * someone may be waiting for that.
15941c7c3c6aSMatthew Dillon 				 *
15951c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1596956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15971c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15981c7c3c6aSMatthew Dillon 				 *
1599279d7226SMatthew Dillon 				 * XXX IT IS NOT LEGAL TO FREE THE PAGE HERE
1600279d7226SMatthew Dillon 				 * AS THIS MESSES WITH object->memq, and it is
1601279d7226SMatthew Dillon 				 * not legal to mess with object->memq from an
1602279d7226SMatthew Dillon 				 * interrupt.
16031c7c3c6aSMatthew Dillon 				 */
16041c7c3c6aSMatthew Dillon 
16051c7c3c6aSMatthew Dillon 				m->valid = 0;
16061c7c3c6aSMatthew Dillon 				vm_page_flag_clear(m, PG_ZERO);
16071c7c3c6aSMatthew Dillon 
16081c7c3c6aSMatthew Dillon 				if (i != bp->b_pager.pg_reqpage)
16091c7c3c6aSMatthew Dillon 					vm_page_free(m);
16101c7c3c6aSMatthew Dillon 				else
16111c7c3c6aSMatthew Dillon 					vm_page_flash(m);
16121c7c3c6aSMatthew Dillon 				/*
16131c7c3c6aSMatthew Dillon 				 * If i == bp->b_pager.pg_reqpage, do not wake
16141c7c3c6aSMatthew Dillon 				 * the page up.  The caller needs to.
16151c7c3c6aSMatthew Dillon 				 */
16161c7c3c6aSMatthew Dillon 			} else {
16171c7c3c6aSMatthew Dillon 				/*
16181c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
16191c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
16201c7c3c6aSMatthew Dillon 				 * then finish the I/O.
16211c7c3c6aSMatthew Dillon 				 */
16227dbf82dcSMatthew Dillon 				vm_page_dirty(m);
16231c7c3c6aSMatthew Dillon 				vm_page_activate(m);
16241c7c3c6aSMatthew Dillon 				vm_page_io_finish(m);
16251c7c3c6aSMatthew Dillon 			}
162621144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
16271c7c3c6aSMatthew Dillon 			/*
16281c7c3c6aSMatthew Dillon 			 * For read success, clear dirty bits.  Nobody should
16291c7c3c6aSMatthew Dillon 			 * have this page mapped but don't take any chances,
16301c7c3c6aSMatthew Dillon 			 * make sure the pmap modify bits are also cleared.
16311c7c3c6aSMatthew Dillon 			 *
16321c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1633956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
16341c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
16351c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
16361c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
16371c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
16381c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
16391c7c3c6aSMatthew Dillon 			 *
16401c7c3c6aSMatthew Dillon 			 * clear PG_ZERO in page.
16411c7c3c6aSMatthew Dillon 			 *
16421c7c3c6aSMatthew Dillon 			 * If not the requested page then deactivate it.
16431c7c3c6aSMatthew Dillon 			 *
16441c7c3c6aSMatthew Dillon 			 * Note that the requested page, reqpage, is left
16451c7c3c6aSMatthew Dillon 			 * busied, but we still have to wake it up.  The
16461c7c3c6aSMatthew Dillon 			 * other pages are released (unbusied) by
16471c7c3c6aSMatthew Dillon 			 * vm_page_wakeup().  We do not set reqpage's
16481c7c3c6aSMatthew Dillon 			 * valid bits here, it is up to the caller.
16491c7c3c6aSMatthew Dillon 			 */
16501c7c3c6aSMatthew Dillon 
16510385347cSPeter Wemm 			pmap_clear_modify(m);
16521c7c3c6aSMatthew Dillon 			m->valid = VM_PAGE_BITS_ALL;
16532c28a105SAlan Cox 			vm_page_undirty(m);
16541c7c3c6aSMatthew Dillon 			vm_page_flag_clear(m, PG_ZERO);
16551c7c3c6aSMatthew Dillon 
16561c7c3c6aSMatthew Dillon 			/*
16571c7c3c6aSMatthew Dillon 			 * We have to wake specifically requested pages
16581c7c3c6aSMatthew Dillon 			 * up too because we cleared PG_SWAPINPROG and
16591c7c3c6aSMatthew Dillon 			 * could be waiting for it in getpages.  However,
16601c7c3c6aSMatthew Dillon 			 * be sure to not unbusy getpages specifically
16611c7c3c6aSMatthew Dillon 			 * requested page - getpages expects it to be
16621c7c3c6aSMatthew Dillon 			 * left busy.
16631c7c3c6aSMatthew Dillon 			 */
16641c7c3c6aSMatthew Dillon 			if (i != bp->b_pager.pg_reqpage) {
16651c7c3c6aSMatthew Dillon 				vm_page_deactivate(m);
16661c7c3c6aSMatthew Dillon 				vm_page_wakeup(m);
16671c7c3c6aSMatthew Dillon 			} else {
16681c7c3c6aSMatthew Dillon 				vm_page_flash(m);
16691c7c3c6aSMatthew Dillon 			}
16701c7c3c6aSMatthew Dillon 		} else {
16711c7c3c6aSMatthew Dillon 			/*
16721c7c3c6aSMatthew Dillon 			 * For write success, clear the modify and dirty
16731c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
16741c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
16751c7c3c6aSMatthew Dillon 			 */
16760385347cSPeter Wemm 			pmap_clear_modify(m);
1677c52e7044SAlan Cox 			vm_page_undirty(m);
16781c7c3c6aSMatthew Dillon 			vm_page_io_finish(m);
1679936524aaSMatthew Dillon 			if (!vm_page_count_severe() || !vm_page_try_to_cache(m))
1680936524aaSMatthew Dillon 				vm_page_protect(m, VM_PROT_READ);
1681ffc82b0aSJohn Dyson 		}
1682df8bae1dSRodney W. Grimes 	}
168326f9a767SRodney W. Grimes 
16841c7c3c6aSMatthew Dillon 	/*
16851c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
16861c7c3c6aSMatthew Dillon 	 * pip refs on the object.
16871c7c3c6aSMatthew Dillon 	 */
16880d94caffSDavid Greenman 
16891c7c3c6aSMatthew Dillon 	if (object)
16901c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
169126f9a767SRodney W. Grimes 
16921c7c3c6aSMatthew Dillon 	/*
16931c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
16941c7c3c6aSMatthew Dillon 	 */
1695e47ed70bSJohn Dyson 
1696327f4e83SMatthew Dillon 	relpbuf(
1697327f4e83SMatthew Dillon 	    bp,
169821144e3bSPoul-Henning Kamp 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1699327f4e83SMatthew Dillon 		((bp->b_flags & B_ASYNC) ?
1700327f4e83SMatthew Dillon 		    &nsw_wcount_async :
1701327f4e83SMatthew Dillon 		    &nsw_wcount_sync
1702327f4e83SMatthew Dillon 		)
1703327f4e83SMatthew Dillon 	    )
1704327f4e83SMatthew Dillon 	);
170526f9a767SRodney W. Grimes 	splx(s);
170626f9a767SRodney W. Grimes }
17071c7c3c6aSMatthew Dillon 
17081c7c3c6aSMatthew Dillon /************************************************************************
17091c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
17101c7c3c6aSMatthew Dillon  ************************************************************************
17111c7c3c6aSMatthew Dillon  *
17121c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
17134dcc5c2dSMatthew Dillon  *	OBJT_SWAP object.  All swp_*() routines must be called at
17144dcc5c2dSMatthew Dillon  *	splvm() because swap can be freed up by the low level vm_page
17154dcc5c2dSMatthew Dillon  *	code which might be called from interrupts beyond what splbio() covers.
17161c7c3c6aSMatthew Dillon  *
17174dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
17184dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
17194dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
17201c7c3c6aSMatthew Dillon  */
17211c7c3c6aSMatthew Dillon 
17221c7c3c6aSMatthew Dillon /*
17231c7c3c6aSMatthew Dillon  * SWP_PAGER_HASH() -	hash swap meta data
17241c7c3c6aSMatthew Dillon  *
17254dcc5c2dSMatthew Dillon  *	This is an inline helper function which hashes the swapblk given
17261c7c3c6aSMatthew Dillon  *	the object and page index.  It returns a pointer to a pointer
17271c7c3c6aSMatthew Dillon  *	to the object, or a pointer to a NULL pointer if it could not
17281c7c3c6aSMatthew Dillon  *	find a swapblk.
17294dcc5c2dSMatthew Dillon  *
17304dcc5c2dSMatthew Dillon  *	This routine must be called at splvm().
17311c7c3c6aSMatthew Dillon  */
17321c7c3c6aSMatthew Dillon 
17331c7c3c6aSMatthew Dillon static __inline struct swblock **
17344dcc5c2dSMatthew Dillon swp_pager_hash(vm_object_t object, vm_pindex_t index)
17351c7c3c6aSMatthew Dillon {
17361c7c3c6aSMatthew Dillon 	struct swblock **pswap;
17371c7c3c6aSMatthew Dillon 	struct swblock *swap;
17381c7c3c6aSMatthew Dillon 
17391c7c3c6aSMatthew Dillon 	index &= ~SWAP_META_MASK;
1740af647ddeSBruce Evans 	pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask];
17411c7c3c6aSMatthew Dillon 
17421c7c3c6aSMatthew Dillon 	while ((swap = *pswap) != NULL) {
17431c7c3c6aSMatthew Dillon 		if (swap->swb_object == object &&
17441c7c3c6aSMatthew Dillon 		    swap->swb_index == index
17451c7c3c6aSMatthew Dillon 		) {
17461c7c3c6aSMatthew Dillon 			break;
17471c7c3c6aSMatthew Dillon 		}
17481c7c3c6aSMatthew Dillon 		pswap = &swap->swb_hnext;
17491c7c3c6aSMatthew Dillon 	}
17501c7c3c6aSMatthew Dillon 	return(pswap);
17511c7c3c6aSMatthew Dillon }
17521c7c3c6aSMatthew Dillon 
17531c7c3c6aSMatthew Dillon /*
17541c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
17551c7c3c6aSMatthew Dillon  *
17561c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
17571c7c3c6aSMatthew Dillon  *	object.
17581c7c3c6aSMatthew Dillon  *
17591c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
17601c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
17611c7c3c6aSMatthew Dillon  *	assigned swapblk is freed.
17624dcc5c2dSMatthew Dillon  *
17634dcc5c2dSMatthew Dillon  *	This routine must be called at splvm(), except when used to convert
17644dcc5c2dSMatthew Dillon  *	an OBJT_DEFAULT object into an OBJT_SWAP object.
176523955314SAlfred Perlstein  *
176623955314SAlfred Perlstein  *	Requires vm_mtx.
17671c7c3c6aSMatthew Dillon  */
17681c7c3c6aSMatthew Dillon 
17691c7c3c6aSMatthew Dillon static void
17701c7c3c6aSMatthew Dillon swp_pager_meta_build(
17711c7c3c6aSMatthew Dillon 	vm_object_t object,
17724dcc5c2dSMatthew Dillon 	vm_pindex_t index,
17734dcc5c2dSMatthew Dillon 	daddr_t swapblk
17741c7c3c6aSMatthew Dillon ) {
17751c7c3c6aSMatthew Dillon 	struct swblock *swap;
17761c7c3c6aSMatthew Dillon 	struct swblock **pswap;
17771c7c3c6aSMatthew Dillon 
177823955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
17791c7c3c6aSMatthew Dillon 	/*
17801c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
17811c7c3c6aSMatthew Dillon 	 */
17821c7c3c6aSMatthew Dillon 
17831c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
17841c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
17851c7c3c6aSMatthew Dillon 		object->un_pager.swp.swp_bcount = 0;
17861c7c3c6aSMatthew Dillon 
1787a9fa2c05SAlfred Perlstein 		mtx_lock(&sw_alloc_mtx);
17881c7c3c6aSMatthew Dillon 		if (object->handle != NULL) {
17891c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
17901c7c3c6aSMatthew Dillon 			    NOBJLIST(object->handle),
17911c7c3c6aSMatthew Dillon 			    object,
17921c7c3c6aSMatthew Dillon 			    pager_object_list
17931c7c3c6aSMatthew Dillon 			);
17941c7c3c6aSMatthew Dillon 		} else {
17951c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
17961c7c3c6aSMatthew Dillon 			    &swap_pager_un_object_list,
17971c7c3c6aSMatthew Dillon 			    object,
17981c7c3c6aSMatthew Dillon 			    pager_object_list
17991c7c3c6aSMatthew Dillon 			);
18001c7c3c6aSMatthew Dillon 		}
1801a9fa2c05SAlfred Perlstein 		mtx_unlock(&sw_alloc_mtx);
18021c7c3c6aSMatthew Dillon 	}
18031c7c3c6aSMatthew Dillon 
18041c7c3c6aSMatthew Dillon 	/*
18051c7c3c6aSMatthew Dillon 	 * Locate hash entry.  If not found create, but if we aren't adding
18064dcc5c2dSMatthew Dillon 	 * anything just return.  If we run out of space in the map we wait
18074dcc5c2dSMatthew Dillon 	 * and, since the hash table may have changed, retry.
18081c7c3c6aSMatthew Dillon 	 */
18091c7c3c6aSMatthew Dillon 
18104dcc5c2dSMatthew Dillon retry:
18111c7c3c6aSMatthew Dillon 	pswap = swp_pager_hash(object, index);
18121c7c3c6aSMatthew Dillon 
18131c7c3c6aSMatthew Dillon 	if ((swap = *pswap) == NULL) {
18141c7c3c6aSMatthew Dillon 		int i;
18151c7c3c6aSMatthew Dillon 
18161c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
18171c7c3c6aSMatthew Dillon 			return;
18181c7c3c6aSMatthew Dillon 
18191c7c3c6aSMatthew Dillon 		swap = *pswap = zalloc(swap_zone);
18204dcc5c2dSMatthew Dillon 		if (swap == NULL) {
18214dcc5c2dSMatthew Dillon 			VM_WAIT;
18224dcc5c2dSMatthew Dillon 			goto retry;
18234dcc5c2dSMatthew Dillon 		}
18241c7c3c6aSMatthew Dillon 		swap->swb_hnext = NULL;
18251c7c3c6aSMatthew Dillon 		swap->swb_object = object;
18261c7c3c6aSMatthew Dillon 		swap->swb_index = index & ~SWAP_META_MASK;
18271c7c3c6aSMatthew Dillon 		swap->swb_count = 0;
18281c7c3c6aSMatthew Dillon 
18291c7c3c6aSMatthew Dillon 		++object->un_pager.swp.swp_bcount;
18301c7c3c6aSMatthew Dillon 
18311c7c3c6aSMatthew Dillon 		for (i = 0; i < SWAP_META_PAGES; ++i)
18321c7c3c6aSMatthew Dillon 			swap->swb_pages[i] = SWAPBLK_NONE;
18331c7c3c6aSMatthew Dillon 	}
18341c7c3c6aSMatthew Dillon 
18351c7c3c6aSMatthew Dillon 	/*
18361c7c3c6aSMatthew Dillon 	 * Delete prior contents of metadata
18371c7c3c6aSMatthew Dillon 	 */
18381c7c3c6aSMatthew Dillon 
18391c7c3c6aSMatthew Dillon 	index &= SWAP_META_MASK;
18401c7c3c6aSMatthew Dillon 
18411c7c3c6aSMatthew Dillon 	if (swap->swb_pages[index] != SWAPBLK_NONE) {
18424dcc5c2dSMatthew Dillon 		swp_pager_freeswapspace(swap->swb_pages[index], 1);
18431c7c3c6aSMatthew Dillon 		--swap->swb_count;
18441c7c3c6aSMatthew Dillon 	}
18451c7c3c6aSMatthew Dillon 
18461c7c3c6aSMatthew Dillon 	/*
18471c7c3c6aSMatthew Dillon 	 * Enter block into metadata
18481c7c3c6aSMatthew Dillon 	 */
18491c7c3c6aSMatthew Dillon 
18501c7c3c6aSMatthew Dillon 	swap->swb_pages[index] = swapblk;
18514dcc5c2dSMatthew Dillon 	if (swapblk != SWAPBLK_NONE)
18521c7c3c6aSMatthew Dillon 		++swap->swb_count;
18531c7c3c6aSMatthew Dillon }
18541c7c3c6aSMatthew Dillon 
18551c7c3c6aSMatthew Dillon /*
18561c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
18571c7c3c6aSMatthew Dillon  *
18581c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
18591c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
18601c7c3c6aSMatthew Dillon  *
18611c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
18621c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
18631c7c3c6aSMatthew Dillon  *	with resident pages.
18641c7c3c6aSMatthew Dillon  *
186523955314SAlfred Perlstein  * 	mv_mtx must be held
18661c7c3c6aSMatthew Dillon  *	This routine must be called at splvm()
18671c7c3c6aSMatthew Dillon  */
18681c7c3c6aSMatthew Dillon 
18691c7c3c6aSMatthew Dillon static void
18704dcc5c2dSMatthew Dillon swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count)
18711c7c3c6aSMatthew Dillon {
187223955314SAlfred Perlstein 
187323955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
187423955314SAlfred Perlstein 
18751c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
18761c7c3c6aSMatthew Dillon 		return;
18771c7c3c6aSMatthew Dillon 
18781c7c3c6aSMatthew Dillon 	while (count > 0) {
18791c7c3c6aSMatthew Dillon 		struct swblock **pswap;
18801c7c3c6aSMatthew Dillon 		struct swblock *swap;
18811c7c3c6aSMatthew Dillon 
18821c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
18831c7c3c6aSMatthew Dillon 
18841c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
18851c7c3c6aSMatthew Dillon 			daddr_t v = swap->swb_pages[index & SWAP_META_MASK];
18861c7c3c6aSMatthew Dillon 
18871c7c3c6aSMatthew Dillon 			if (v != SWAPBLK_NONE) {
18881c7c3c6aSMatthew Dillon 				swp_pager_freeswapspace(v, 1);
18891c7c3c6aSMatthew Dillon 				swap->swb_pages[index & SWAP_META_MASK] =
18901c7c3c6aSMatthew Dillon 					SWAPBLK_NONE;
18911c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
18921c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
18931c7c3c6aSMatthew Dillon 					zfree(swap_zone, swap);
18941c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
18951c7c3c6aSMatthew Dillon 				}
18961c7c3c6aSMatthew Dillon 			}
18971c7c3c6aSMatthew Dillon 			--count;
18981c7c3c6aSMatthew Dillon 			++index;
18991c7c3c6aSMatthew Dillon 		} else {
19004dcc5c2dSMatthew Dillon 			int n = SWAP_META_PAGES - (index & SWAP_META_MASK);
19011c7c3c6aSMatthew Dillon 			count -= n;
19021c7c3c6aSMatthew Dillon 			index += n;
19031c7c3c6aSMatthew Dillon 		}
19041c7c3c6aSMatthew Dillon 	}
19051c7c3c6aSMatthew Dillon }
19061c7c3c6aSMatthew Dillon 
19071c7c3c6aSMatthew Dillon /*
19081c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
19091c7c3c6aSMatthew Dillon  *
19101c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
19111c7c3c6aSMatthew Dillon  *	an object.
19124dcc5c2dSMatthew Dillon  *
19134dcc5c2dSMatthew Dillon  *	This routine must be called at splvm()
191423955314SAlfred Perlstein  *	Requires vm_mtx.
19151c7c3c6aSMatthew Dillon  */
19161c7c3c6aSMatthew Dillon 
19171c7c3c6aSMatthew Dillon static void
19181c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
19191c7c3c6aSMatthew Dillon {
19201c7c3c6aSMatthew Dillon 	daddr_t index = 0;
19211c7c3c6aSMatthew Dillon 
192223955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
192323955314SAlfred Perlstein 
19241c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19251c7c3c6aSMatthew Dillon 		return;
19261c7c3c6aSMatthew Dillon 
19271c7c3c6aSMatthew Dillon 	while (object->un_pager.swp.swp_bcount) {
19281c7c3c6aSMatthew Dillon 		struct swblock **pswap;
19291c7c3c6aSMatthew Dillon 		struct swblock *swap;
19301c7c3c6aSMatthew Dillon 
19311c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
19321c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
19331c7c3c6aSMatthew Dillon 			int i;
19341c7c3c6aSMatthew Dillon 
19351c7c3c6aSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
19361c7c3c6aSMatthew Dillon 				daddr_t v = swap->swb_pages[i];
19371c7c3c6aSMatthew Dillon 				if (v != SWAPBLK_NONE) {
19381c7c3c6aSMatthew Dillon 					--swap->swb_count;
19394dcc5c2dSMatthew Dillon 					swp_pager_freeswapspace(v, 1);
19401c7c3c6aSMatthew Dillon 				}
19411c7c3c6aSMatthew Dillon 			}
19421c7c3c6aSMatthew Dillon 			if (swap->swb_count != 0)
19431c7c3c6aSMatthew Dillon 				panic("swap_pager_meta_free_all: swb_count != 0");
19441c7c3c6aSMatthew Dillon 			*pswap = swap->swb_hnext;
19451c7c3c6aSMatthew Dillon 			zfree(swap_zone, swap);
19461c7c3c6aSMatthew Dillon 			--object->un_pager.swp.swp_bcount;
19471c7c3c6aSMatthew Dillon 		}
19481c7c3c6aSMatthew Dillon 		index += SWAP_META_PAGES;
19491c7c3c6aSMatthew Dillon 		if (index > 0x20000000)
19501c7c3c6aSMatthew Dillon 			panic("swp_pager_meta_free_all: failed to locate all swap meta blocks");
19511c7c3c6aSMatthew Dillon 	}
19521c7c3c6aSMatthew Dillon }
19531c7c3c6aSMatthew Dillon 
19541c7c3c6aSMatthew Dillon /*
19551c7c3c6aSMatthew Dillon  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
19561c7c3c6aSMatthew Dillon  *
19571c7c3c6aSMatthew Dillon  *	This routine is capable of looking up, popping, or freeing
19581c7c3c6aSMatthew Dillon  *	swapblk assignments in the swap meta data or in the vm_page_t.
19591c7c3c6aSMatthew Dillon  *	The routine typically returns the swapblk being looked-up, or popped,
19601c7c3c6aSMatthew Dillon  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
19611c7c3c6aSMatthew Dillon  *	was invalid.  This routine will automatically free any invalid
19621c7c3c6aSMatthew Dillon  *	meta-data swapblks.
19631c7c3c6aSMatthew Dillon  *
19641c7c3c6aSMatthew Dillon  *	It is not possible to store invalid swapblks in the swap meta data
19651c7c3c6aSMatthew Dillon  *	(other then a literal 'SWAPBLK_NONE'), so we don't bother checking.
19661c7c3c6aSMatthew Dillon  *
19671c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
19681c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
19691c7c3c6aSMatthew Dillon  *	busy page.
19701c7c3c6aSMatthew Dillon  *
19714dcc5c2dSMatthew Dillon  *	This routine must be called at splvm().
197223955314SAlfred Perlstein  *	Requires vm_mtx.
19731c7c3c6aSMatthew Dillon  *
19744dcc5c2dSMatthew Dillon  *	SWM_FREE	remove and free swap block from metadata
19751c7c3c6aSMatthew Dillon  *	SWM_POP		remove from meta data but do not free.. pop it out
19761c7c3c6aSMatthew Dillon  */
19771c7c3c6aSMatthew Dillon 
19781c7c3c6aSMatthew Dillon static daddr_t
19791c7c3c6aSMatthew Dillon swp_pager_meta_ctl(
19801c7c3c6aSMatthew Dillon 	vm_object_t object,
19811c7c3c6aSMatthew Dillon 	vm_pindex_t index,
19821c7c3c6aSMatthew Dillon 	int flags
19831c7c3c6aSMatthew Dillon ) {
19844dcc5c2dSMatthew Dillon 	struct swblock **pswap;
19854dcc5c2dSMatthew Dillon 	struct swblock *swap;
19864dcc5c2dSMatthew Dillon 	daddr_t r1;
19874dcc5c2dSMatthew Dillon 
19881c7c3c6aSMatthew Dillon 	/*
19891c7c3c6aSMatthew Dillon 	 * The meta data only exists of the object is OBJT_SWAP
19901c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
19911c7c3c6aSMatthew Dillon 	 */
19921c7c3c6aSMatthew Dillon 
19934dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
19941c7c3c6aSMatthew Dillon 		return(SWAPBLK_NONE);
19951c7c3c6aSMatthew Dillon 
19964dcc5c2dSMatthew Dillon 	r1 = SWAPBLK_NONE;
19971c7c3c6aSMatthew Dillon 	pswap = swp_pager_hash(object, index);
19981c7c3c6aSMatthew Dillon 
19991c7c3c6aSMatthew Dillon 	if ((swap = *pswap) != NULL) {
20004dcc5c2dSMatthew Dillon 		index &= SWAP_META_MASK;
20011c7c3c6aSMatthew Dillon 		r1 = swap->swb_pages[index];
20021c7c3c6aSMatthew Dillon 
20031c7c3c6aSMatthew Dillon 		if (r1 != SWAPBLK_NONE) {
20041c7c3c6aSMatthew Dillon 			if (flags & SWM_FREE) {
20054dcc5c2dSMatthew Dillon 				swp_pager_freeswapspace(r1, 1);
20061c7c3c6aSMatthew Dillon 				r1 = SWAPBLK_NONE;
20071c7c3c6aSMatthew Dillon 			}
20081c7c3c6aSMatthew Dillon 			if (flags & (SWM_FREE|SWM_POP)) {
20091c7c3c6aSMatthew Dillon 				swap->swb_pages[index] = SWAPBLK_NONE;
20101c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
20111c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
20121c7c3c6aSMatthew Dillon 					zfree(swap_zone, swap);
20131c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
20141c7c3c6aSMatthew Dillon 				}
20151c7c3c6aSMatthew Dillon 			}
20161c7c3c6aSMatthew Dillon 		}
20171c7c3c6aSMatthew Dillon 	}
20181c7c3c6aSMatthew Dillon 	return(r1);
20191c7c3c6aSMatthew Dillon }
20201c7c3c6aSMatthew Dillon 
2021e4057dbdSPoul-Henning Kamp /********************************************************
2022e4057dbdSPoul-Henning Kamp  *		CHAINING FUNCTIONS			*
2023e4057dbdSPoul-Henning Kamp  ********************************************************
2024e4057dbdSPoul-Henning Kamp  *
2025e4057dbdSPoul-Henning Kamp  *	These functions support recursion of I/O operations
2026e4057dbdSPoul-Henning Kamp  *	on bp's, typically by chaining one or more 'child' bp's
2027e4057dbdSPoul-Henning Kamp  *	to the parent.  Synchronous, asynchronous, and semi-synchronous
2028e4057dbdSPoul-Henning Kamp  *	chaining is possible.
2029e4057dbdSPoul-Henning Kamp  */
2030e4057dbdSPoul-Henning Kamp 
2031e4057dbdSPoul-Henning Kamp /*
2032e4057dbdSPoul-Henning Kamp  *	vm_pager_chain_iodone:
2033e4057dbdSPoul-Henning Kamp  *
2034e4057dbdSPoul-Henning Kamp  *	io completion routine for child bp.  Currently we fudge a bit
2035e4057dbdSPoul-Henning Kamp  *	on dealing with b_resid.   Since users of these routines may issue
2036e4057dbdSPoul-Henning Kamp  *	multiple children simultaneously, sequencing of the error can be lost.
2037e4057dbdSPoul-Henning Kamp  */
2038e4057dbdSPoul-Henning Kamp 
2039e4057dbdSPoul-Henning Kamp static void
2040e4057dbdSPoul-Henning Kamp vm_pager_chain_iodone(struct buf *nbp)
2041e4057dbdSPoul-Henning Kamp {
20420b441832SPoul-Henning Kamp 	struct bio *bp;
20430b441832SPoul-Henning Kamp 	u_int *count;
2044e4057dbdSPoul-Henning Kamp 
20450b441832SPoul-Henning Kamp 	bp = nbp->b_caller1;
20460b441832SPoul-Henning Kamp 	count = (u_int *)&(bp->bio_caller1);
20470b441832SPoul-Henning Kamp 	if (bp != NULL) {
2048e4057dbdSPoul-Henning Kamp 		if (nbp->b_ioflags & BIO_ERROR) {
20490b441832SPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
20500b441832SPoul-Henning Kamp 			bp->bio_error = nbp->b_error;
2051e4057dbdSPoul-Henning Kamp 		} else if (nbp->b_resid != 0) {
20520b441832SPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
20530b441832SPoul-Henning Kamp 			bp->bio_error = EINVAL;
2054e4057dbdSPoul-Henning Kamp 		} else {
20550b441832SPoul-Henning Kamp 			bp->bio_resid -= nbp->b_bcount;
2056e4057dbdSPoul-Henning Kamp 		}
20570b441832SPoul-Henning Kamp 		nbp->b_caller1 = NULL;
20580b441832SPoul-Henning Kamp 		--(*count);
20590b441832SPoul-Henning Kamp 		if (bp->bio_flags & BIO_FLAG1) {
20600b441832SPoul-Henning Kamp 			bp->bio_flags &= ~BIO_FLAG1;
2061e4057dbdSPoul-Henning Kamp 			wakeup(bp);
2062e4057dbdSPoul-Henning Kamp 		}
2063e4057dbdSPoul-Henning Kamp 	}
2064e4057dbdSPoul-Henning Kamp 	nbp->b_flags |= B_DONE;
2065e4057dbdSPoul-Henning Kamp 	nbp->b_flags &= ~B_ASYNC;
2066e4057dbdSPoul-Henning Kamp 	relpbuf(nbp, NULL);
2067e4057dbdSPoul-Henning Kamp }
2068e4057dbdSPoul-Henning Kamp 
2069e4057dbdSPoul-Henning Kamp /*
2070e4057dbdSPoul-Henning Kamp  *	getchainbuf:
2071e4057dbdSPoul-Henning Kamp  *
2072e4057dbdSPoul-Henning Kamp  *	Obtain a physical buffer and chain it to its parent buffer.  When
2073e4057dbdSPoul-Henning Kamp  *	I/O completes, the parent buffer will be B_SIGNAL'd.  Errors are
2074e4057dbdSPoul-Henning Kamp  *	automatically propagated to the parent
207523955314SAlfred Perlstein  *
207623955314SAlfred Perlstein  *	vm_mtx can't be held
2077e4057dbdSPoul-Henning Kamp  */
2078e4057dbdSPoul-Henning Kamp 
2079e4057dbdSPoul-Henning Kamp struct buf *
20800b441832SPoul-Henning Kamp getchainbuf(struct bio *bp, struct vnode *vp, int flags)
2081e4057dbdSPoul-Henning Kamp {
208223955314SAlfred Perlstein 	struct buf *nbp;
208323955314SAlfred Perlstein 	u_int *count;
208423955314SAlfred Perlstein 
208523955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_NOTOWNED);
208623955314SAlfred Perlstein 	nbp = getpbuf(NULL);
208723955314SAlfred Perlstein 	count = (u_int *)&(bp->bio_caller1);
2088e4057dbdSPoul-Henning Kamp 
20890b441832SPoul-Henning Kamp 	nbp->b_caller1 = bp;
20900b441832SPoul-Henning Kamp 	++(*count);
2091e4057dbdSPoul-Henning Kamp 
20920b441832SPoul-Henning Kamp 	if (*count > 4)
2093e4057dbdSPoul-Henning Kamp 		waitchainbuf(bp, 4, 0);
2094e4057dbdSPoul-Henning Kamp 
20950b441832SPoul-Henning Kamp 	nbp->b_iocmd = bp->bio_cmd;
20960b441832SPoul-Henning Kamp 	nbp->b_ioflags = bp->bio_flags & BIO_ORDERED;
2097e4057dbdSPoul-Henning Kamp 	nbp->b_flags = flags;
2098e4057dbdSPoul-Henning Kamp 	nbp->b_rcred = nbp->b_wcred = proc0.p_ucred;
2099e4057dbdSPoul-Henning Kamp 	nbp->b_iodone = vm_pager_chain_iodone;
2100e4057dbdSPoul-Henning Kamp 
2101e4057dbdSPoul-Henning Kamp 	crhold(nbp->b_rcred);
2102e4057dbdSPoul-Henning Kamp 	crhold(nbp->b_wcred);
2103e4057dbdSPoul-Henning Kamp 
2104e4057dbdSPoul-Henning Kamp 	if (vp)
2105e4057dbdSPoul-Henning Kamp 		pbgetvp(vp, nbp);
2106e4057dbdSPoul-Henning Kamp 	return(nbp);
2107e4057dbdSPoul-Henning Kamp }
2108e4057dbdSPoul-Henning Kamp 
2109e4057dbdSPoul-Henning Kamp void
2110e4057dbdSPoul-Henning Kamp flushchainbuf(struct buf *nbp)
2111e4057dbdSPoul-Henning Kamp {
211223955314SAlfred Perlstein 
211323955314SAlfred Perlstein 	mtx_unlock(&vm_mtx);
211423955314SAlfred Perlstein 	mtx_lock(&Giant);
2115e4057dbdSPoul-Henning Kamp 	if (nbp->b_bcount) {
2116e4057dbdSPoul-Henning Kamp 		nbp->b_bufsize = nbp->b_bcount;
2117e4057dbdSPoul-Henning Kamp 		if (nbp->b_iocmd == BIO_WRITE)
2118e4057dbdSPoul-Henning Kamp 			nbp->b_dirtyend = nbp->b_bcount;
2119e4057dbdSPoul-Henning Kamp 		BUF_KERNPROC(nbp);
2120e4057dbdSPoul-Henning Kamp 		BUF_STRATEGY(nbp);
2121e4057dbdSPoul-Henning Kamp 	} else {
2122e4057dbdSPoul-Henning Kamp 		bufdone(nbp);
2123e4057dbdSPoul-Henning Kamp 	}
212423955314SAlfred Perlstein 	mtx_unlock(&Giant);
212523955314SAlfred Perlstein 	mtx_lock(&vm_mtx);
2126e4057dbdSPoul-Henning Kamp }
2127e4057dbdSPoul-Henning Kamp 
212823955314SAlfred Perlstein static void
21290b441832SPoul-Henning Kamp waitchainbuf(struct bio *bp, int limit, int done)
2130e4057dbdSPoul-Henning Kamp {
2131e4057dbdSPoul-Henning Kamp  	int s;
213223955314SAlfred Perlstein 	u_int *count;
2133e4057dbdSPoul-Henning Kamp 
213423955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_NOTOWNED);
213523955314SAlfred Perlstein 	mtx_lock(&Giant);
213623955314SAlfred Perlstein 	count = (u_int *)&(bp->bio_caller1);
2137e4057dbdSPoul-Henning Kamp 	s = splbio();
21380b441832SPoul-Henning Kamp 	while (*count > limit) {
21390b441832SPoul-Henning Kamp 		bp->bio_flags |= BIO_FLAG1;
2140e4057dbdSPoul-Henning Kamp 		tsleep(bp, PRIBIO + 4, "bpchain", 0);
2141e4057dbdSPoul-Henning Kamp 	}
2142e4057dbdSPoul-Henning Kamp 	if (done) {
21430b441832SPoul-Henning Kamp 		if (bp->bio_resid != 0 && !(bp->bio_flags & BIO_ERROR)) {
21440b441832SPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
21450b441832SPoul-Henning Kamp 			bp->bio_error = EINVAL;
2146e4057dbdSPoul-Henning Kamp 		}
21470b441832SPoul-Henning Kamp 		biodone(bp);
2148e4057dbdSPoul-Henning Kamp 	}
214923955314SAlfred Perlstein 	mtx_unlock(&Giant);
2150e4057dbdSPoul-Henning Kamp 	splx(s);
2151e4057dbdSPoul-Henning Kamp }
2152e4057dbdSPoul-Henning Kamp 
2153