xref: /freebsd/sys/vm/swap_pager.c (revision e04e4bacf6681a03f6eaa3a289cede60d5df0c5e)
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.
5e9c0cc15SPoul-Henning Kamp  * Copyright (c) 1982, 1986, 1989, 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
66e9c0cc15SPoul-Henning Kamp  *	@(#)vm_swap.c	8.5 (Berkeley) 2/17/94
67df8bae1dSRodney W. Grimes  */
68df8bae1dSRodney W. Grimes 
69874651b1SDavid E. O'Brien #include <sys/cdefs.h>
70874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
71874651b1SDavid E. O'Brien 
72e9c0cc15SPoul-Henning Kamp #include "opt_mac.h"
73e9c0cc15SPoul-Henning Kamp #include "opt_swap.h"
74e9c0cc15SPoul-Henning Kamp #include "opt_vm.h"
75e9c0cc15SPoul-Henning Kamp 
76df8bae1dSRodney W. Grimes #include <sys/param.h>
77df8bae1dSRodney W. Grimes #include <sys/systm.h>
78af647ddeSBruce Evans #include <sys/conf.h>
7964abb5a5SDavid Greenman #include <sys/kernel.h>
80df8bae1dSRodney W. Grimes #include <sys/proc.h>
819626b608SPoul-Henning Kamp #include <sys/bio.h>
82df8bae1dSRodney W. Grimes #include <sys/buf.h>
83e9c0cc15SPoul-Henning Kamp #include <sys/disk.h>
84e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h>
85e9c0cc15SPoul-Henning Kamp #include <sys/mount.h>
86e9c0cc15SPoul-Henning Kamp #include <sys/namei.h>
87df8bae1dSRodney W. Grimes #include <sys/vnode.h>
88e9c0cc15SPoul-Henning Kamp #include <sys/mac.h>
89df8bae1dSRodney W. Grimes #include <sys/malloc.h>
90327f4e83SMatthew Dillon #include <sys/sysctl.h>
91e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
921c7c3c6aSMatthew Dillon #include <sys/blist.h>
931c7c3c6aSMatthew Dillon #include <sys/lock.h>
940cddd8f0SMatthew Dillon #include <sys/sx.h>
95936524aaSMatthew Dillon #include <sys/vmmeter.h>
96df8bae1dSRodney W. Grimes 
97df8bae1dSRodney W. Grimes #include <vm/vm.h>
9821cd6e62SSeigo Tanimura #include <vm/pmap.h>
9921cd6e62SSeigo Tanimura #include <vm/vm_map.h>
10021cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
101efeaf95aSDavid Greenman #include <vm/vm_object.h>
102df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
103efeaf95aSDavid Greenman #include <vm/vm_pager.h>
104df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
105e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
106df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
107efeaf95aSDavid Greenman #include <vm/vm_extern.h>
108670d17b5SJeff Roberson #include <vm/uma.h>
109df8bae1dSRodney W. Grimes 
110ec38b344SPoul-Henning Kamp /*
111ec38b344SPoul-Henning Kamp  * SWB_NPAGES must be a power of 2.  It may be set to 1, 2, 4, 8, or 16
112ec38b344SPoul-Henning Kamp  * pages per allocation.  We recommend you stick with the default of 8.
113ec38b344SPoul-Henning Kamp  * The 16-page limit is due to the radix code (kern/subr_blist.c).
114ec38b344SPoul-Henning Kamp  */
115ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
116ec38b344SPoul-Henning Kamp #define MAX_PAGEOUT_CLUSTER 16
117ec38b344SPoul-Henning Kamp #endif
118ec38b344SPoul-Henning Kamp 
119ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
120ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
121ec38b344SPoul-Henning Kamp #endif
122ec38b344SPoul-Henning Kamp 
123ec38b344SPoul-Henning Kamp /*
124ec38b344SPoul-Henning Kamp  * Piecemeal swap metadata structure.  Swap is stored in a radix tree.
125ec38b344SPoul-Henning Kamp  *
126ec38b344SPoul-Henning Kamp  * If SWB_NPAGES is 8 and sizeof(char *) == sizeof(daddr_t), our radix
127ec38b344SPoul-Henning Kamp  * is basically 8.  Assuming PAGE_SIZE == 4096, one tree level represents
128ec38b344SPoul-Henning Kamp  * 32K worth of data, two levels represent 256K, three levels represent
129ec38b344SPoul-Henning Kamp  * 2 MBytes.   This is acceptable.
130ec38b344SPoul-Henning Kamp  *
131ec38b344SPoul-Henning Kamp  * Overall memory utilization is about the same as the old swap structure.
132ec38b344SPoul-Henning Kamp  */
133ec38b344SPoul-Henning Kamp #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t))
134ec38b344SPoul-Henning Kamp #define SWAP_META_PAGES		(SWB_NPAGES * 2)
135ec38b344SPoul-Henning Kamp #define SWAP_META_MASK		(SWAP_META_PAGES - 1)
136ec38b344SPoul-Henning Kamp 
1378f60c087SPoul-Henning Kamp typedef	int32_t	swblk_t;	/*
1388f60c087SPoul-Henning Kamp 				 * swap offset.  This is the type used to
1398f60c087SPoul-Henning Kamp 				 * address the "virtual swap device" and
1408f60c087SPoul-Henning Kamp 				 * therefore the maximum swap space is
1418f60c087SPoul-Henning Kamp 				 * 2^32 pages.
1428f60c087SPoul-Henning Kamp 				 */
143e9c0cc15SPoul-Henning Kamp 
1448d677ef9SPoul-Henning Kamp /*
1458d677ef9SPoul-Henning Kamp  * Swap device table
1468d677ef9SPoul-Henning Kamp  */
1478d677ef9SPoul-Henning Kamp struct swdevt {
1488d677ef9SPoul-Henning Kamp 	udev_t	sw_dev;			/* For quasibogus swapdev reporting */
1498d677ef9SPoul-Henning Kamp 	int	sw_flags;
1508d677ef9SPoul-Henning Kamp 	int	sw_nblks;
1518d677ef9SPoul-Henning Kamp 	int     sw_used;
1528d677ef9SPoul-Henning Kamp 	struct	vnode *sw_vp;
1538d677ef9SPoul-Henning Kamp 	dev_t	sw_device;
1548f60c087SPoul-Henning Kamp 	swblk_t	sw_first;
1558f60c087SPoul-Henning Kamp 	swblk_t	sw_end;
1568f60c087SPoul-Henning Kamp 	struct blist *sw_blist;
1578f60c087SPoul-Henning Kamp 	TAILQ_ENTRY(swdevt)	sw_list;
1588d677ef9SPoul-Henning Kamp };
1598d677ef9SPoul-Henning Kamp 
1608d677ef9SPoul-Henning Kamp #define	SW_CLOSING	0x04
1618d677ef9SPoul-Henning Kamp 
162e9c0cc15SPoul-Henning Kamp struct swblock {
163e9c0cc15SPoul-Henning Kamp 	struct swblock	*swb_hnext;
164e9c0cc15SPoul-Henning Kamp 	vm_object_t	swb_object;
165e9c0cc15SPoul-Henning Kamp 	vm_pindex_t	swb_index;
166e9c0cc15SPoul-Henning Kamp 	int		swb_count;
167e9c0cc15SPoul-Henning Kamp 	daddr_t		swb_pages[SWAP_META_PAGES];
168e9c0cc15SPoul-Henning Kamp };
169e9c0cc15SPoul-Henning Kamp 
1708f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1718f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1728f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1738f60c087SPoul-Henning Kamp int swap_pager_avail;
174e9c0cc15SPoul-Henning Kamp static int swdev_syscall_active = 0; /* serialize swap(on|off) */
175e9c0cc15SPoul-Henning Kamp 
176e9c0cc15SPoul-Henning Kamp static int swapdev_strategy(struct vop_strategy_args *ap);
177e9c0cc15SPoul-Henning Kamp static struct vnode *swapdev_vp;
178e9c0cc15SPoul-Henning Kamp 
1791c7c3c6aSMatthew Dillon #define SWM_FREE	0x02	/* free, period			*/
1801c7c3c6aSMatthew Dillon #define SWM_POP		0x04	/* pop out			*/
18126f9a767SRodney W. Grimes 
18220d3034fSMatthew Dillon int swap_pager_full;		/* swap space exhaustion (task killing) */
18320d3034fSMatthew Dillon static int swap_pager_almost_full; /* swap space exhaustion (w/ hysteresis)*/
1841c7c3c6aSMatthew Dillon static int nsw_rcount;		/* free read buffers			*/
185327f4e83SMatthew Dillon static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
186327f4e83SMatthew Dillon static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
187327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
188327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
1891c7c3c6aSMatthew Dillon 
1901c7c3c6aSMatthew Dillon static struct swblock **swhash;
1911c7c3c6aSMatthew Dillon static int swhash_mask;
192327f4e83SMatthew Dillon static int swap_async_max = 4;	/* maximum in-progress async I/O's	*/
1930cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
194327f4e83SMatthew Dillon 
19524e7ab7cSPoul-Henning Kamp 
196327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_async_max,
197327f4e83SMatthew Dillon         CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops");
1981c7c3c6aSMatthew Dillon 
1991c7c3c6aSMatthew Dillon /*
2001c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
2011c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
2021c7c3c6aSMatthew Dillon  */
2031c7c3c6aSMatthew Dillon 
2041c7c3c6aSMatthew Dillon #define NOBJLISTS		8
2051c7c3c6aSMatthew Dillon 
2061c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
207af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
2081c7c3c6aSMatthew Dillon 
209a9fa2c05SAlfred Perlstein static struct mtx sw_alloc_mtx;	/* protect list manipulation */
2101c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
211e9c0cc15SPoul-Henning Kamp static struct pagerlst	swap_pager_un_object_list;
212e9c0cc15SPoul-Henning Kamp static uma_zone_t	swap_zone;
2131c7c3c6aSMatthew Dillon 
2141c7c3c6aSMatthew Dillon /*
2151c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
2161c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
2171c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
2181c7c3c6aSMatthew Dillon  */
219ff98689dSBruce Evans static vm_object_t
22011caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
22111caded3SAlfred Perlstein 				      vm_prot_t prot, vm_ooffset_t offset);
22211caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
22311caded3SAlfred Perlstein static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int);
2245ea4972cSAlan Cox static boolean_t
2255ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
22611caded3SAlfred Perlstein static void	swap_pager_init(void);
22711caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
2288f60c087SPoul-Henning Kamp static void	swap_pager_swapoff(struct swdevt *sp, int *sw_used);
229f708ef1bSPoul-Henning Kamp 
230df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
231e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
232e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
233e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
234e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
235e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
236e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
237e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
238df8bae1dSRodney W. Grimes };
239df8bae1dSRodney W. Grimes 
2401c7c3c6aSMatthew Dillon /*
2411c7c3c6aSMatthew Dillon  * dmmax is in page-sized chunks with the new swap system.  It was
24264bcb9c8SMatthew Dillon  * dev-bsized chunks in the old.  dmmax is always a power of 2.
2431c7c3c6aSMatthew Dillon  *
2441c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
2451c7c3c6aSMatthew Dillon  * internal.
2461c7c3c6aSMatthew Dillon  */
247e9c0cc15SPoul-Henning Kamp static int dmmax, dmmax_mask;
248e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
249e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
25026f9a767SRodney W. Grimes 
251cee313c4SRobert Watson SYSCTL_INT(_vm, OID_AUTO, dmmax,
252cee313c4SRobert Watson 	CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block");
253cee313c4SRobert Watson 
254a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
25511caded3SAlfred Perlstein static void	swp_pager_sync_iodone(struct buf *bp);
25611caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
25724a1cce3SDavid Greenman 
2581c7c3c6aSMatthew Dillon /*
2591c7c3c6aSMatthew Dillon  * Swap bitmap functions
2601c7c3c6aSMatthew Dillon  */
261a5edd34aSPoul-Henning Kamp static void	swp_pager_freeswapspace(daddr_t blk, int npages);
262a5edd34aSPoul-Henning Kamp static daddr_t	swp_pager_getswapspace(int npages);
2631c7c3c6aSMatthew Dillon 
2641c7c3c6aSMatthew Dillon /*
2651c7c3c6aSMatthew Dillon  * Metadata functions
2661c7c3c6aSMatthew Dillon  */
267a5edd34aSPoul-Henning Kamp static struct swblock **swp_pager_hash(vm_object_t object, vm_pindex_t index);
26811caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
26911caded3SAlfred Perlstein static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t);
27011caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
27111caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
2721c7c3c6aSMatthew Dillon 
2731c7c3c6aSMatthew Dillon /*
2741c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
2751c7c3c6aSMatthew Dillon  *
27620d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
27720d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
27820d3034fSMatthew Dillon  *
27920d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
2801c7c3c6aSMatthew Dillon  *
2811c7c3c6aSMatthew Dillon  *	No restrictions on call
2821c7c3c6aSMatthew Dillon  *	This routine may not block.
2831c7c3c6aSMatthew Dillon  *	This routine must be called at splvm()
2841c7c3c6aSMatthew Dillon  */
285a5edd34aSPoul-Henning Kamp static void
2861c7c3c6aSMatthew Dillon swp_sizecheck()
2870d94caffSDavid Greenman {
2880cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
28923955314SAlfred Perlstein 
2908f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
29120d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
2921af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
29320d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
2942b0d37a4SMatthew Dillon 		}
29520d3034fSMatthew Dillon 	} else {
29626f9a767SRodney W. Grimes 		swap_pager_full = 0;
2978f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
29820d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
29926f9a767SRodney W. Grimes 	}
3001c7c3c6aSMatthew Dillon }
3011c7c3c6aSMatthew Dillon 
3021c7c3c6aSMatthew Dillon /*
303da5fd145SPeter Wemm  * SWP_PAGER_HASH() -	hash swap meta data
304da5fd145SPeter Wemm  *
305a5edd34aSPoul-Henning Kamp  *	This is an helper function which hashes the swapblk given
306da5fd145SPeter Wemm  *	the object and page index.  It returns a pointer to a pointer
307da5fd145SPeter Wemm  *	to the object, or a pointer to a NULL pointer if it could not
308da5fd145SPeter Wemm  *	find a swapblk.
309da5fd145SPeter Wemm  *
310da5fd145SPeter Wemm  *	This routine must be called at splvm().
311da5fd145SPeter Wemm  */
312a5edd34aSPoul-Henning Kamp static struct swblock **
313da5fd145SPeter Wemm swp_pager_hash(vm_object_t object, vm_pindex_t index)
314da5fd145SPeter Wemm {
315da5fd145SPeter Wemm 	struct swblock **pswap;
316da5fd145SPeter Wemm 	struct swblock *swap;
317da5fd145SPeter Wemm 
318da5fd145SPeter Wemm 	index &= ~(vm_pindex_t)SWAP_META_MASK;
319da5fd145SPeter Wemm 	pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask];
320da5fd145SPeter Wemm 	while ((swap = *pswap) != NULL) {
321da5fd145SPeter Wemm 		if (swap->swb_object == object &&
322da5fd145SPeter Wemm 		    swap->swb_index == index
323da5fd145SPeter Wemm 		) {
324da5fd145SPeter Wemm 			break;
325da5fd145SPeter Wemm 		}
326da5fd145SPeter Wemm 		pswap = &swap->swb_hnext;
327da5fd145SPeter Wemm 	}
328da5fd145SPeter Wemm 	return (pswap);
329da5fd145SPeter Wemm }
330da5fd145SPeter Wemm 
331da5fd145SPeter Wemm /*
3321c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
3331c7c3c6aSMatthew Dillon  *
3341c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
3351c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
3361c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
3371c7c3c6aSMatthew Dillon  */
338f5a12711SPoul-Henning Kamp static void
339df8bae1dSRodney W. Grimes swap_pager_init()
340df8bae1dSRodney W. Grimes {
3411c7c3c6aSMatthew Dillon 	/*
3421c7c3c6aSMatthew Dillon 	 * Initialize object lists
3431c7c3c6aSMatthew Dillon 	 */
3441c7c3c6aSMatthew Dillon 	int i;
3451c7c3c6aSMatthew Dillon 
3461c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
3471c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
34824a1cce3SDavid Greenman 	TAILQ_INIT(&swap_pager_un_object_list);
3496008862bSJohn Baldwin 	mtx_init(&sw_alloc_mtx, "swap_pager list", NULL, MTX_DEF);
350df8bae1dSRodney W. Grimes 
351df8bae1dSRodney W. Grimes 	/*
3521c7c3c6aSMatthew Dillon 	 * Device Stripe, in PAGE_SIZE'd blocks
353df8bae1dSRodney W. Grimes 	 */
3541c7c3c6aSMatthew Dillon 	dmmax = SWB_NPAGES * 2;
3551c7c3c6aSMatthew Dillon 	dmmax_mask = ~(dmmax - 1);
3561c7c3c6aSMatthew Dillon }
35726f9a767SRodney W. Grimes 
358df8bae1dSRodney W. Grimes /*
3591c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
3601c7c3c6aSMatthew Dillon  *
3611c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
3621c7c3c6aSMatthew Dillon  *	its main loop.
363df8bae1dSRodney W. Grimes  */
36424a1cce3SDavid Greenman void
36524a1cce3SDavid Greenman swap_pager_swap_init()
366df8bae1dSRodney W. Grimes {
36721cd6e62SSeigo Tanimura 	int n, n2;
3680d94caffSDavid Greenman 
36926f9a767SRodney W. Grimes 	/*
3701c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
3711c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
3721c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
3731c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
3741c7c3c6aSMatthew Dillon 	 *
375327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
3761c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
3771c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
3781c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
379327f4e83SMatthew Dillon 	 *
380327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
381327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
382327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
383327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
384327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
385327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
386327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
387327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
388327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
389327f4e83SMatthew Dillon 	 * So it all works out pretty well.
39026f9a767SRodney W. Grimes 	 */
391ad3cce20SMatthew Dillon 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
392327f4e83SMatthew Dillon 
3936d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
3941c7c3c6aSMatthew Dillon 	nsw_rcount = (nswbuf + 1) / 2;
395327f4e83SMatthew Dillon 	nsw_wcount_sync = (nswbuf + 3) / 4;
396327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
397327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
3986d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
39924a1cce3SDavid Greenman 
4001c7c3c6aSMatthew Dillon 	/*
4011c7c3c6aSMatthew Dillon 	 * Initialize our zone.  Right now I'm just guessing on the number
4021c7c3c6aSMatthew Dillon 	 * we need based on the number of pages in the system.  Each swblock
4032f9e4e80SMatthew Dillon 	 * can hold 16 pages, so this is probably overkill.  This reservation
404ec61f55dSMatthew Dillon 	 * is typically limited to around 32MB by default.
4051c7c3c6aSMatthew Dillon 	 */
406ec61f55dSMatthew Dillon 	n = cnt.v_page_count / 2;
4072f9e4e80SMatthew Dillon 	if (maxswzone && n > maxswzone / sizeof(struct swblock))
4082f9e4e80SMatthew Dillon 		n = maxswzone / sizeof(struct swblock);
40921cd6e62SSeigo Tanimura 	n2 = n;
410670d17b5SJeff Roberson 	swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL,
411670d17b5SJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
4128355f576SJeff Roberson 	do {
4138355f576SJeff Roberson 		if (uma_zone_set_obj(swap_zone, NULL, n))
41461ce6eeeSAlfred Perlstein 			break;
41561ce6eeeSAlfred Perlstein 		/*
41661ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
41761ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
41861ce6eeeSAlfred Perlstein 		 */
41961ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
42061ce6eeeSAlfred Perlstein 	} while (n > 0);
42121cd6e62SSeigo Tanimura 	if (swap_zone == NULL)
422670d17b5SJeff Roberson 		panic("failed to create swap_zone.");
42321cd6e62SSeigo Tanimura 	if (n2 != n)
42461ce6eeeSAlfred Perlstein 		printf("Swap zone entries reduced from %d to %d.\n", n2, n);
42521cd6e62SSeigo Tanimura 	n2 = n;
42624a1cce3SDavid Greenman 
4271c7c3c6aSMatthew Dillon 	/*
4281c7c3c6aSMatthew Dillon 	 * Initialize our meta-data hash table.  The swapper does not need to
4291c7c3c6aSMatthew Dillon 	 * be quite as efficient as the VM system, so we do not use an
4301c7c3c6aSMatthew Dillon 	 * oversized hash table.
4311c7c3c6aSMatthew Dillon 	 *
4321c7c3c6aSMatthew Dillon 	 * 	n: 		size of hash table, must be power of 2
4331c7c3c6aSMatthew Dillon 	 *	swhash_mask:	hash table index mask
4341c7c3c6aSMatthew Dillon 	 */
43561ce6eeeSAlfred Perlstein 	for (n = 1; n < n2 / 8; n *= 2)
4361c7c3c6aSMatthew Dillon 		;
437a163d034SWarner Losh 	swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO);
4381c7c3c6aSMatthew Dillon 	swhash_mask = n - 1;
43924a1cce3SDavid Greenman }
44024a1cce3SDavid Greenman 
44124a1cce3SDavid Greenman /*
4421c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
4431c7c3c6aSMatthew Dillon  *			its metadata structures.
4441c7c3c6aSMatthew Dillon  *
4451c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
4461c7c3c6aSMatthew Dillon  *	OBJT_SWAP object.  We do this by creating an OBJT_DEFAULT object
4471c7c3c6aSMatthew Dillon  *	and then converting it with swp_pager_meta_build().
4481c7c3c6aSMatthew Dillon  *
4491c7c3c6aSMatthew Dillon  *	This routine may block in vm_object_allocate() and create a named
4501c7c3c6aSMatthew Dillon  *	object lookup race, so we must interlock.   We must also run at
4511c7c3c6aSMatthew Dillon  *	splvm() for the object lookup to handle races with interrupts, but
4521c7c3c6aSMatthew Dillon  *	we do not have to maintain splvm() in between the lookup and the
4531c7c3c6aSMatthew Dillon  *	add because (I believe) it is not possible to attempt to create
4541c7c3c6aSMatthew Dillon  *	a new swap object w/handle when a default object with that handle
4551c7c3c6aSMatthew Dillon  *	already exists.
45624c46d03SAlan Cox  *
45724c46d03SAlan Cox  * MPSAFE
45824a1cce3SDavid Greenman  */
459f5a12711SPoul-Henning Kamp static vm_object_t
4606cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
461b9dcd593SBruce Evans 		 vm_ooffset_t offset)
46224a1cce3SDavid Greenman {
46324a1cce3SDavid Greenman 	vm_object_t object;
46424a1cce3SDavid Greenman 
46524c46d03SAlan Cox 	mtx_lock(&Giant);
46624a1cce3SDavid Greenman 	if (handle) {
4671c7c3c6aSMatthew Dillon 		/*
4681c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
4691c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
4701c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
4711c7c3c6aSMatthew Dillon 		 * of the handle.
4721c7c3c6aSMatthew Dillon 		 */
4730cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
4741c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
4751c7c3c6aSMatthew Dillon 
47624a1cce3SDavid Greenman 		if (object != NULL) {
47724a1cce3SDavid Greenman 			vm_object_reference(object);
47824a1cce3SDavid Greenman 		} else {
4791c7c3c6aSMatthew Dillon 			object = vm_object_allocate(OBJT_DEFAULT,
4806cde7a16SDavid Greenman 				OFF_TO_IDX(offset + PAGE_MASK + size));
48124a1cce3SDavid Greenman 			object->handle = handle;
4821c7c3c6aSMatthew Dillon 
4834dcc5c2dSMatthew Dillon 			swp_pager_meta_build(object, 0, SWAPBLK_NONE);
48424a1cce3SDavid Greenman 		}
4850cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
48624a1cce3SDavid Greenman 	} else {
4871c7c3c6aSMatthew Dillon 		object = vm_object_allocate(OBJT_DEFAULT,
4886cde7a16SDavid Greenman 			OFF_TO_IDX(offset + PAGE_MASK + size));
4891c7c3c6aSMatthew Dillon 
4904dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
49124a1cce3SDavid Greenman 	}
49224c46d03SAlan Cox 	mtx_unlock(&Giant);
49324a1cce3SDavid Greenman 	return (object);
494df8bae1dSRodney W. Grimes }
495df8bae1dSRodney W. Grimes 
49626f9a767SRodney W. Grimes /*
4971c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
4981c7c3c6aSMatthew Dillon  *
4991c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
5001c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
5011c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
5021c7c3c6aSMatthew Dillon  *	about to be destroyed.
5031c7c3c6aSMatthew Dillon  *
5041c7c3c6aSMatthew Dillon  *	This routine may block, but no longer does.
5051c7c3c6aSMatthew Dillon  *
5061c7c3c6aSMatthew Dillon  *	The object must be locked or unreferenceable.
50726f9a767SRodney W. Grimes  */
508df8bae1dSRodney W. Grimes static void
5091c7c3c6aSMatthew Dillon swap_pager_dealloc(object)
5102a4895f4SDavid Greenman 	vm_object_t object;
51126f9a767SRodney W. Grimes {
5124dcc5c2dSMatthew Dillon 	int s;
5134dcc5c2dSMatthew Dillon 
5140cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5150cddd8f0SMatthew Dillon 
51626f9a767SRodney W. Grimes 	/*
5171c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
5181c7c3c6aSMatthew Dillon 	 * pageout completion.
51926f9a767SRodney W. Grimes 	 */
520a9fa2c05SAlfred Perlstein 	mtx_lock(&sw_alloc_mtx);
5211c7c3c6aSMatthew Dillon 	if (object->handle == NULL) {
5221c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(&swap_pager_un_object_list, object, pager_object_list);
52324ea4a96SDavid Greenman 	} else {
5241c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list);
52526f9a767SRodney W. Grimes 	}
526a9fa2c05SAlfred Perlstein 	mtx_unlock(&sw_alloc_mtx);
5271c7c3c6aSMatthew Dillon 
528658ad5ffSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
5291c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
5301c7c3c6aSMatthew Dillon 
5311c7c3c6aSMatthew Dillon 	/*
5321c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
5331c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
5341c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
5351c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
5361c7c3c6aSMatthew Dillon 	 */
5374dcc5c2dSMatthew Dillon 	s = splvm();
5381c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
5394dcc5c2dSMatthew Dillon 	splx(s);
5401c7c3c6aSMatthew Dillon }
5411c7c3c6aSMatthew Dillon 
5421c7c3c6aSMatthew Dillon /************************************************************************
5431c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
5441c7c3c6aSMatthew Dillon  ************************************************************************/
5451c7c3c6aSMatthew Dillon 
5461c7c3c6aSMatthew Dillon /*
5471c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
5481c7c3c6aSMatthew Dillon  *
5491c7c3c6aSMatthew Dillon  *	Allocate swap for the requested number of pages.  The starting
5501c7c3c6aSMatthew Dillon  *	swap block number (a page index) is returned or SWAPBLK_NONE
5511c7c3c6aSMatthew Dillon  *	if the allocation failed.
5521c7c3c6aSMatthew Dillon  *
5531c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
5541c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
5551c7c3c6aSMatthew Dillon  *
5561c7c3c6aSMatthew Dillon  *	Must be called at splvm() to avoid races with bitmap frees from
5571c7c3c6aSMatthew Dillon  *	vm_page_remove() aka swap_pager_page_removed().
5581c7c3c6aSMatthew Dillon  *
5591c7c3c6aSMatthew Dillon  *	This routine may not block
5601c7c3c6aSMatthew Dillon  *	This routine must be called at splvm().
5618f60c087SPoul-Henning Kamp  *
5628f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
5631c7c3c6aSMatthew Dillon  */
564a5edd34aSPoul-Henning Kamp static daddr_t
5651c7c3c6aSMatthew Dillon swp_pager_getswapspace(npages)
5661c7c3c6aSMatthew Dillon 	int npages;
5671c7c3c6aSMatthew Dillon {
5681c7c3c6aSMatthew Dillon 	daddr_t blk;
5698f60c087SPoul-Henning Kamp 	struct swdevt *sp;
5708f60c087SPoul-Henning Kamp 	int i;
5711c7c3c6aSMatthew Dillon 
5720cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
5730cddd8f0SMatthew Dillon 
5748f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
5758f60c087SPoul-Henning Kamp 	sp = swdevhd;
5768f60c087SPoul-Henning Kamp 	for (i = 0; i < nswapdev; i++) {
5778f60c087SPoul-Henning Kamp 		if (sp == NULL)
5788f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
5798f60c087SPoul-Henning Kamp 		if (!(sp->sw_flags & SW_CLOSING)) {
5808f60c087SPoul-Henning Kamp 			blk = blist_alloc(sp->sw_blist, npages);
5818f60c087SPoul-Henning Kamp 			if (blk != SWAPBLK_NONE) {
5828f60c087SPoul-Henning Kamp 				blk += sp->sw_first;
5838f60c087SPoul-Henning Kamp 				swap_pager_avail -= npages;
5848f60c087SPoul-Henning Kamp 				sp->sw_used += npages;
5858f60c087SPoul-Henning Kamp 				swp_sizecheck();
5868f60c087SPoul-Henning Kamp 				swdevhd = TAILQ_NEXT(sp, sw_list);
5878f60c087SPoul-Henning Kamp 				return(blk);
5888f60c087SPoul-Henning Kamp 			}
5898f60c087SPoul-Henning Kamp 		}
5908f60c087SPoul-Henning Kamp 		sp = TAILQ_NEXT(sp, sw_list);
5918f60c087SPoul-Henning Kamp 	}
5922b0d37a4SMatthew Dillon 	if (swap_pager_full != 2) {
5931c7c3c6aSMatthew Dillon 		printf("swap_pager_getswapspace: failed\n");
5942b0d37a4SMatthew Dillon 		swap_pager_full = 2;
59520d3034fSMatthew Dillon 		swap_pager_almost_full = 1;
5962b0d37a4SMatthew Dillon 	}
5978f60c087SPoul-Henning Kamp 	swdevhd = NULL;
5981c7c3c6aSMatthew Dillon 	return (blk);
59926f9a767SRodney W. Grimes }
60026f9a767SRodney W. Grimes 
6018f60c087SPoul-Henning Kamp static struct swdevt *
60212692209SPoul-Henning Kamp swp_pager_find_dev(daddr_t blk, int npages)
6038f60c087SPoul-Henning Kamp {
6048f60c087SPoul-Henning Kamp 	struct swdevt *sp;
6058f60c087SPoul-Henning Kamp 
6068f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
6078f60c087SPoul-Henning Kamp 		if (blk >= sp->sw_first && blk + npages <= sp->sw_end)
6088f60c087SPoul-Henning Kamp 			return (sp);
6098f60c087SPoul-Henning Kamp 	}
6108f60c087SPoul-Henning Kamp 	printf("Failed to find swapdev blk %ju, %d pages\n",
6118f60c087SPoul-Henning Kamp 	    (uintmax_t)blk, npages);
6128f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list)
6138f60c087SPoul-Henning Kamp 		printf("has %ju...%ju\n",
6148f60c087SPoul-Henning Kamp 		    (uintmax_t)sp->sw_first, (uintmax_t)sp->sw_end);
6158f60c087SPoul-Henning Kamp 	return (NULL);
6168f60c087SPoul-Henning Kamp }
6178f60c087SPoul-Henning Kamp 
6188f60c087SPoul-Henning Kamp 
61926f9a767SRodney W. Grimes /*
6201c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
6211c7c3c6aSMatthew Dillon  *
6221c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
6231c7c3c6aSMatthew Dillon  *
6241c7c3c6aSMatthew Dillon  *	Note:  This routine may not block (it could in the old swap code),
6251c7c3c6aSMatthew Dillon  *	and through the use of the new blist routines it does not block.
6261c7c3c6aSMatthew Dillon  *
6271c7c3c6aSMatthew Dillon  *	We must be called at splvm() to avoid races with bitmap frees from
6281c7c3c6aSMatthew Dillon  *	vm_page_remove() aka swap_pager_page_removed().
6291c7c3c6aSMatthew Dillon  *
6301c7c3c6aSMatthew Dillon  *	This routine may not block
6311c7c3c6aSMatthew Dillon  *	This routine must be called at splvm().
63226f9a767SRodney W. Grimes  */
633a5edd34aSPoul-Henning Kamp static void
6348f60c087SPoul-Henning Kamp swp_pager_freeswapspace(daddr_t blk, int npages)
6350d94caffSDavid Greenman {
6368f60c087SPoul-Henning Kamp 	struct swdevt *sp;
63792da00bbSMatthew Dillon 
6380cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
63923955314SAlfred Perlstein 
64012692209SPoul-Henning Kamp 	sp = swp_pager_find_dev(blk, npages);
6418f60c087SPoul-Henning Kamp 
64292da00bbSMatthew Dillon 	/* per-swap area stats */
64392da00bbSMatthew Dillon 	sp->sw_used -= npages;
64492da00bbSMatthew Dillon 
64592da00bbSMatthew Dillon 	/*
64692da00bbSMatthew Dillon 	 * If we are attempting to stop swapping on this device, we
64792da00bbSMatthew Dillon 	 * don't want to mark any blocks free lest they be reused.
64892da00bbSMatthew Dillon 	 */
64992da00bbSMatthew Dillon 	if (sp->sw_flags & SW_CLOSING)
65092da00bbSMatthew Dillon 		return;
65192da00bbSMatthew Dillon 
6528f60c087SPoul-Henning Kamp 	blist_free(sp->sw_blist, blk - sp->sw_first, npages);
6538f60c087SPoul-Henning Kamp 	swap_pager_avail += npages;
6541c7c3c6aSMatthew Dillon 	swp_sizecheck();
65526f9a767SRodney W. Grimes }
6561c7c3c6aSMatthew Dillon 
65726f9a767SRodney W. Grimes /*
6581c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
6591c7c3c6aSMatthew Dillon  *				range within an object.
6601c7c3c6aSMatthew Dillon  *
6611c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
6621c7c3c6aSMatthew Dillon  *
6631c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
6641c7c3c6aSMatthew Dillon  *
6651c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
6661c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
6671c7c3c6aSMatthew Dillon  *	we should be ok.
6684dcc5c2dSMatthew Dillon  *
6694dcc5c2dSMatthew Dillon  *	This routine may be called at any spl.  We up our spl to splvm temporarily
6704dcc5c2dSMatthew Dillon  *	in order to perform the metadata removal.
67126f9a767SRodney W. Grimes  */
67226f9a767SRodney W. Grimes void
67324a1cce3SDavid Greenman swap_pager_freespace(object, start, size)
67424a1cce3SDavid Greenman 	vm_object_t object;
675a316d390SJohn Dyson 	vm_pindex_t start;
676a316d390SJohn Dyson 	vm_size_t size;
67726f9a767SRodney W. Grimes {
6784dcc5c2dSMatthew Dillon 	int s = splvm();
67923955314SAlfred Perlstein 
68019ba4c8eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
6811c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
6824dcc5c2dSMatthew Dillon 	splx(s);
6834dcc5c2dSMatthew Dillon }
6844dcc5c2dSMatthew Dillon 
6854dcc5c2dSMatthew Dillon /*
6864dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
6874dcc5c2dSMatthew Dillon  *
6884dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
6894dcc5c2dSMatthew Dillon  *	swap blocks are not zerod.  Any previous swap assignment is destroyed.
6904dcc5c2dSMatthew Dillon  *
6914dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
6924dcc5c2dSMatthew Dillon  */
6934dcc5c2dSMatthew Dillon int
6944dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
6954dcc5c2dSMatthew Dillon {
6964dcc5c2dSMatthew Dillon 	int s;
6974dcc5c2dSMatthew Dillon 	int n = 0;
6984dcc5c2dSMatthew Dillon 	daddr_t blk = SWAPBLK_NONE;
6994dcc5c2dSMatthew Dillon 	vm_pindex_t beg = start;	/* save start index */
7004dcc5c2dSMatthew Dillon 
7014dcc5c2dSMatthew Dillon 	s = splvm();
7024dcc5c2dSMatthew Dillon 	while (size) {
7034dcc5c2dSMatthew Dillon 		if (n == 0) {
7044dcc5c2dSMatthew Dillon 			n = BLIST_MAX_ALLOC;
7054dcc5c2dSMatthew Dillon 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
7064dcc5c2dSMatthew Dillon 				n >>= 1;
7074dcc5c2dSMatthew Dillon 				if (n == 0) {
7084dcc5c2dSMatthew Dillon 					swp_pager_meta_free(object, beg, start - beg);
7094dcc5c2dSMatthew Dillon 					splx(s);
7104dcc5c2dSMatthew Dillon 					return (-1);
7114dcc5c2dSMatthew Dillon 				}
7124dcc5c2dSMatthew Dillon 			}
7134dcc5c2dSMatthew Dillon 		}
7144dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, start, blk);
7154dcc5c2dSMatthew Dillon 		--size;
7164dcc5c2dSMatthew Dillon 		++start;
7174dcc5c2dSMatthew Dillon 		++blk;
7184dcc5c2dSMatthew Dillon 		--n;
7194dcc5c2dSMatthew Dillon 	}
7204dcc5c2dSMatthew Dillon 	swp_pager_meta_free(object, start, n);
7214dcc5c2dSMatthew Dillon 	splx(s);
7224dcc5c2dSMatthew Dillon 	return (0);
72326f9a767SRodney W. Grimes }
72426f9a767SRodney W. Grimes 
7250a47b48bSJohn Dyson /*
7261c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
7271c7c3c6aSMatthew Dillon  *			and destroy the source.
7281c7c3c6aSMatthew Dillon  *
7291c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
7301c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
7311c7c3c6aSMatthew Dillon  *	we keep the destination's.
7321c7c3c6aSMatthew Dillon  *
7331c7c3c6aSMatthew Dillon  *	This routine is allowed to block.  It may block allocating metadata
7341c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
7351c7c3c6aSMatthew Dillon  *	progress on the source.
7361c7c3c6aSMatthew Dillon  *
7374dcc5c2dSMatthew Dillon  *	This routine can be called at any spl
7384dcc5c2dSMatthew Dillon  *
7391c7c3c6aSMatthew Dillon  *	XXX vm_page_collapse() kinda expects us not to block because we
7401c7c3c6aSMatthew Dillon  *	supposedly do not need to allocate memory, but for the moment we
7411c7c3c6aSMatthew Dillon  *	*may* have to get a little memory from the zone allocator, but
7421c7c3c6aSMatthew Dillon  *	it is taken from the interrupt memory.  We should be ok.
7431c7c3c6aSMatthew Dillon  *
7441c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
7451c7c3c6aSMatthew Dillon  *
7461c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
7471c7c3c6aSMatthew Dillon  *
7484dcc5c2dSMatthew Dillon  *	The source and destination objects must be locked or
7494dcc5c2dSMatthew Dillon  *	inaccessible (XXX are they ?)
75026f9a767SRodney W. Grimes  */
75126f9a767SRodney W. Grimes void
7521c7c3c6aSMatthew Dillon swap_pager_copy(srcobject, dstobject, offset, destroysource)
75324a1cce3SDavid Greenman 	vm_object_t srcobject;
75424a1cce3SDavid Greenman 	vm_object_t dstobject;
755a316d390SJohn Dyson 	vm_pindex_t offset;
756c0877f10SJohn Dyson 	int destroysource;
75726f9a767SRodney W. Grimes {
758a316d390SJohn Dyson 	vm_pindex_t i;
7594dcc5c2dSMatthew Dillon 	int s;
7604dcc5c2dSMatthew Dillon 
7610cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
7620cddd8f0SMatthew Dillon 
7634dcc5c2dSMatthew Dillon 	s = splvm();
76426f9a767SRodney W. Grimes 	/*
7651c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
7661c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
76726f9a767SRodney W. Grimes 	 */
768cbd8ec09SJohn Dyson 	if (destroysource) {
769a9fa2c05SAlfred Perlstein 		mtx_lock(&sw_alloc_mtx);
77024a1cce3SDavid Greenman 		if (srcobject->handle == NULL) {
7711c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
7721c7c3c6aSMatthew Dillon 			    &swap_pager_un_object_list,
7731c7c3c6aSMatthew Dillon 			    srcobject,
7741c7c3c6aSMatthew Dillon 			    pager_object_list
7751c7c3c6aSMatthew Dillon 			);
77626f9a767SRodney W. Grimes 		} else {
7771c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
7781c7c3c6aSMatthew Dillon 			    NOBJLIST(srcobject->handle),
7791c7c3c6aSMatthew Dillon 			    srcobject,
7801c7c3c6aSMatthew Dillon 			    pager_object_list
7811c7c3c6aSMatthew Dillon 			);
78226f9a767SRodney W. Grimes 		}
783a9fa2c05SAlfred Perlstein 		mtx_unlock(&sw_alloc_mtx);
784cbd8ec09SJohn Dyson 	}
78526f9a767SRodney W. Grimes 
7861c7c3c6aSMatthew Dillon 	/*
7871c7c3c6aSMatthew Dillon 	 * transfer source to destination.
7881c7c3c6aSMatthew Dillon 	 */
7891c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
7901c7c3c6aSMatthew Dillon 		daddr_t dstaddr;
7911c7c3c6aSMatthew Dillon 
7921c7c3c6aSMatthew Dillon 		/*
7931c7c3c6aSMatthew Dillon 		 * Locate (without changing) the swapblk on the destination,
7941c7c3c6aSMatthew Dillon 		 * unless it is invalid in which case free it silently, or
7951c7c3c6aSMatthew Dillon 		 * if the destination is a resident page, in which case the
7961c7c3c6aSMatthew Dillon 		 * source is thrown away.
7971c7c3c6aSMatthew Dillon 		 */
7981c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
7991c7c3c6aSMatthew Dillon 
8001c7c3c6aSMatthew Dillon 		if (dstaddr == SWAPBLK_NONE) {
8011c7c3c6aSMatthew Dillon 			/*
8021c7c3c6aSMatthew Dillon 			 * Destination has no swapblk and is not resident,
8031c7c3c6aSMatthew Dillon 			 * copy source.
8041c7c3c6aSMatthew Dillon 			 */
8051c7c3c6aSMatthew Dillon 			daddr_t srcaddr;
8061c7c3c6aSMatthew Dillon 
8071c7c3c6aSMatthew Dillon 			srcaddr = swp_pager_meta_ctl(
8081c7c3c6aSMatthew Dillon 			    srcobject,
8091c7c3c6aSMatthew Dillon 			    i + offset,
8101c7c3c6aSMatthew Dillon 			    SWM_POP
8111c7c3c6aSMatthew Dillon 			);
8121c7c3c6aSMatthew Dillon 
8131c7c3c6aSMatthew Dillon 			if (srcaddr != SWAPBLK_NONE)
8144dcc5c2dSMatthew Dillon 				swp_pager_meta_build(dstobject, i, srcaddr);
8151c7c3c6aSMatthew Dillon 		} else {
8161c7c3c6aSMatthew Dillon 			/*
8171c7c3c6aSMatthew Dillon 			 * Destination has valid swapblk or it is represented
8181c7c3c6aSMatthew Dillon 			 * by a resident page.  We destroy the sourceblock.
8191c7c3c6aSMatthew Dillon 			 */
8201c7c3c6aSMatthew Dillon 
8211c7c3c6aSMatthew Dillon 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
8221c7c3c6aSMatthew Dillon 		}
82326f9a767SRodney W. Grimes 	}
82426f9a767SRodney W. Grimes 
82526f9a767SRodney W. Grimes 	/*
8261c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
8271c7c3c6aSMatthew Dillon 	 *
8281c7c3c6aSMatthew Dillon 	 * We have to revert the type to OBJT_DEFAULT so we do not accidently
8291c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
83026f9a767SRodney W. Grimes 	 */
831c0877f10SJohn Dyson 	if (destroysource) {
8321c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
8331c7c3c6aSMatthew Dillon 		/*
8341c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
8351c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
836956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
8371c7c3c6aSMatthew Dillon 		 * queues.
8381c7c3c6aSMatthew Dillon 		 */
8391c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
840c0877f10SJohn Dyson 	}
8414dcc5c2dSMatthew Dillon 	splx(s);
84226f9a767SRodney W. Grimes }
84326f9a767SRodney W. Grimes 
844df8bae1dSRodney W. Grimes /*
8451c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
8461c7c3c6aSMatthew Dillon  *				the requested page.
8471c7c3c6aSMatthew Dillon  *
8481c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
8491c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
8501c7c3c6aSMatthew Dillon  *
8511c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
8521c7c3c6aSMatthew Dillon  *	store exists before and after the requested page within a reasonable
8531c7c3c6aSMatthew Dillon  *	distance.  We do not try to restrict it to the swap device stripe
8541c7c3c6aSMatthew Dillon  *	(that is handled in getpages/putpages).  It probably isn't worth
8551c7c3c6aSMatthew Dillon  *	doing here.
856df8bae1dSRodney W. Grimes  */
8575ea4972cSAlan Cox static boolean_t
858a316d390SJohn Dyson swap_pager_haspage(object, pindex, before, after)
85924a1cce3SDavid Greenman 	vm_object_t object;
860a316d390SJohn Dyson 	vm_pindex_t pindex;
86124a1cce3SDavid Greenman 	int *before;
86224a1cce3SDavid Greenman 	int *after;
86326f9a767SRodney W. Grimes {
8641c7c3c6aSMatthew Dillon 	daddr_t blk0;
86525db2c54SMatthew Dillon 	int s;
86626f9a767SRodney W. Grimes 
8671c7c3c6aSMatthew Dillon 	/*
8681c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
8691c7c3c6aSMatthew Dillon 	 */
87025db2c54SMatthew Dillon 	s = splvm();
8711c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
8721c7c3c6aSMatthew Dillon 
8734dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
87425db2c54SMatthew Dillon 		splx(s);
8751c7c3c6aSMatthew Dillon 		if (before)
87624a1cce3SDavid Greenman 			*before = 0;
8771c7c3c6aSMatthew Dillon 		if (after)
87824a1cce3SDavid Greenman 			*after = 0;
87926f9a767SRodney W. Grimes 		return (FALSE);
88026f9a767SRodney W. Grimes 	}
88126f9a767SRodney W. Grimes 
88226f9a767SRodney W. Grimes 	/*
8831c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
884e47ed70bSJohn Dyson 	 */
8851c7c3c6aSMatthew Dillon 	if (before != NULL) {
88626f9a767SRodney W. Grimes 		int i;
8870d94caffSDavid Greenman 
8881c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
8891c7c3c6aSMatthew Dillon 			daddr_t blk;
8901c7c3c6aSMatthew Dillon 
8911c7c3c6aSMatthew Dillon 			if (i > pindex)
8921c7c3c6aSMatthew Dillon 				break;
8931c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
8941c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
8951c7c3c6aSMatthew Dillon 				break;
896ffc82b0aSJohn Dyson 		}
8971c7c3c6aSMatthew Dillon 		*before = (i - 1);
89826f9a767SRodney W. Grimes 	}
89926f9a767SRodney W. Grimes 
90026f9a767SRodney W. Grimes 	/*
9011c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
90226f9a767SRodney W. Grimes 	 */
9031c7c3c6aSMatthew Dillon 	if (after != NULL) {
9041c7c3c6aSMatthew Dillon 		int i;
9051c7c3c6aSMatthew Dillon 
9061c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
9071c7c3c6aSMatthew Dillon 			daddr_t blk;
9081c7c3c6aSMatthew Dillon 
9091c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
9101c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
9111c7c3c6aSMatthew Dillon 				break;
91226f9a767SRodney W. Grimes 		}
9131c7c3c6aSMatthew Dillon 		*after = (i - 1);
9141c7c3c6aSMatthew Dillon 	}
91525db2c54SMatthew Dillon 	splx(s);
9161c7c3c6aSMatthew Dillon 	return (TRUE);
9171c7c3c6aSMatthew Dillon }
9181c7c3c6aSMatthew Dillon 
9191c7c3c6aSMatthew Dillon /*
9201c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
9211c7c3c6aSMatthew Dillon  *
9221c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
9231c7c3c6aSMatthew Dillon  *	not, from the page.
9241c7c3c6aSMatthew Dillon  *
9251c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
9261c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
9271c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
9281c7c3c6aSMatthew Dillon  *
9291c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
9301c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
9311c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
9321c7c3c6aSMatthew Dillon  *	depends on it.
9331c7c3c6aSMatthew Dillon  *
9341c7c3c6aSMatthew Dillon  *	This routine may not block
9354dcc5c2dSMatthew Dillon  *	This routine must be called at splvm()
9361c7c3c6aSMatthew Dillon  */
9371c7c3c6aSMatthew Dillon static void
9381c7c3c6aSMatthew Dillon swap_pager_unswapped(m)
9391c7c3c6aSMatthew Dillon 	vm_page_t m;
9401c7c3c6aSMatthew Dillon {
9411c7c3c6aSMatthew Dillon 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
9421c7c3c6aSMatthew Dillon }
9431c7c3c6aSMatthew Dillon 
9441c7c3c6aSMatthew Dillon /*
9451c7c3c6aSMatthew Dillon  * SWAP_PAGER_GETPAGES() - bring pages in from swap
9461c7c3c6aSMatthew Dillon  *
9471c7c3c6aSMatthew Dillon  *	Attempt to retrieve (m, count) pages from backing store, but make
9481c7c3c6aSMatthew Dillon  *	sure we retrieve at least m[reqpage].  We try to load in as large
9491c7c3c6aSMatthew Dillon  *	a chunk surrounding m[reqpage] as is contiguous in swap and which
9501c7c3c6aSMatthew Dillon  *	belongs to the same object.
9511c7c3c6aSMatthew Dillon  *
9521c7c3c6aSMatthew Dillon  *	The code is designed for asynchronous operation and
9531c7c3c6aSMatthew Dillon  *	immediate-notification of 'reqpage' but tends not to be
9541c7c3c6aSMatthew Dillon  *	used that way.  Please do not optimize-out this algorithmic
9551c7c3c6aSMatthew Dillon  *	feature, I intend to improve on it in the future.
9561c7c3c6aSMatthew Dillon  *
9571c7c3c6aSMatthew Dillon  *	The parent has a single vm_object_pip_add() reference prior to
9581c7c3c6aSMatthew Dillon  *	calling us and we should return with the same.
9591c7c3c6aSMatthew Dillon  *
9601c7c3c6aSMatthew Dillon  *	The parent has BUSY'd the pages.  We should return with 'm'
9611c7c3c6aSMatthew Dillon  *	left busy, but the others adjusted.
9621c7c3c6aSMatthew Dillon  */
963f708ef1bSPoul-Henning Kamp static int
96424a1cce3SDavid Greenman swap_pager_getpages(object, m, count, reqpage)
96524a1cce3SDavid Greenman 	vm_object_t object;
96626f9a767SRodney W. Grimes 	vm_page_t *m;
96726f9a767SRodney W. Grimes 	int count, reqpage;
968df8bae1dSRodney W. Grimes {
9691c7c3c6aSMatthew Dillon 	struct buf *bp;
9701c7c3c6aSMatthew Dillon 	vm_page_t mreq;
9711c7c3c6aSMatthew Dillon 	int s;
97226f9a767SRodney W. Grimes 	int i;
97326f9a767SRodney W. Grimes 	int j;
9741c7c3c6aSMatthew Dillon 	daddr_t blk;
9751c7c3c6aSMatthew Dillon 	vm_pindex_t lastpindex;
9760d94caffSDavid Greenman 
9771c7c3c6aSMatthew Dillon 	mreq = m[reqpage];
9781c7c3c6aSMatthew Dillon 
979e04e4bacSPoul-Henning Kamp 	KASSERT(mreq->object == object,
980e04e4bacSPoul-Henning Kamp 	    ("swap_pager_getpages: object mismatch %p/%p",
981e04e4bacSPoul-Henning Kamp 	    object, mreq->object));
982e04e4bacSPoul-Henning Kamp 
9831c7c3c6aSMatthew Dillon 	/*
9841c7c3c6aSMatthew Dillon 	 * Calculate range to retrieve.  The pages have already been assigned
9851c7c3c6aSMatthew Dillon 	 * their swapblks.  We require a *contiguous* range that falls entirely
9861c7c3c6aSMatthew Dillon 	 * within a single device stripe.   If we do not supply it, bad things
9874dcc5c2dSMatthew Dillon 	 * happen.  Note that blk, iblk & jblk can be SWAPBLK_NONE, but the
9884dcc5c2dSMatthew Dillon 	 * loops are set up such that the case(s) are handled implicitly.
9894dcc5c2dSMatthew Dillon 	 *
9904dcc5c2dSMatthew Dillon 	 * The swp_*() calls must be made at splvm().  vm_page_free() does
9914dcc5c2dSMatthew Dillon 	 * not need to be, but it will go a little faster if it is.
9921c7c3c6aSMatthew Dillon 	 */
9934dcc5c2dSMatthew Dillon 	s = splvm();
9941c7c3c6aSMatthew Dillon 	blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0);
9951c7c3c6aSMatthew Dillon 
9961c7c3c6aSMatthew Dillon 	for (i = reqpage - 1; i >= 0; --i) {
9971c7c3c6aSMatthew Dillon 		daddr_t iblk;
9981c7c3c6aSMatthew Dillon 
9991c7c3c6aSMatthew Dillon 		iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0);
10001c7c3c6aSMatthew Dillon 		if (blk != iblk + (reqpage - i))
100126f9a767SRodney W. Grimes 			break;
10024dcc5c2dSMatthew Dillon 		if ((blk ^ iblk) & dmmax_mask)
10034dcc5c2dSMatthew Dillon 			break;
100426f9a767SRodney W. Grimes 	}
10051c7c3c6aSMatthew Dillon 	++i;
10061c7c3c6aSMatthew Dillon 
10071c7c3c6aSMatthew Dillon 	for (j = reqpage + 1; j < count; ++j) {
10081c7c3c6aSMatthew Dillon 		daddr_t jblk;
10091c7c3c6aSMatthew Dillon 
10101c7c3c6aSMatthew Dillon 		jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0);
10111c7c3c6aSMatthew Dillon 		if (blk != jblk - (j - reqpage))
10121c7c3c6aSMatthew Dillon 			break;
10134dcc5c2dSMatthew Dillon 		if ((blk ^ jblk) & dmmax_mask)
10144dcc5c2dSMatthew Dillon 			break;
10151c7c3c6aSMatthew Dillon 	}
10161c7c3c6aSMatthew Dillon 
10171c7c3c6aSMatthew Dillon 	/*
10181c7c3c6aSMatthew Dillon 	 * free pages outside our collection range.   Note: we never free
10191c7c3c6aSMatthew Dillon 	 * mreq, it must remain busy throughout.
10201c7c3c6aSMatthew Dillon 	 */
1021ab9abe5dSAlan Cox 	vm_page_lock_queues();
10221c7c3c6aSMatthew Dillon 	{
10231c7c3c6aSMatthew Dillon 		int k;
10241c7c3c6aSMatthew Dillon 
10254dcc5c2dSMatthew Dillon 		for (k = 0; k < i; ++k)
10264dcc5c2dSMatthew Dillon 			vm_page_free(m[k]);
10274dcc5c2dSMatthew Dillon 		for (k = j; k < count; ++k)
10281c7c3c6aSMatthew Dillon 			vm_page_free(m[k]);
10291c7c3c6aSMatthew Dillon 	}
1030ab9abe5dSAlan Cox 	vm_page_unlock_queues();
10314dcc5c2dSMatthew Dillon 	splx(s);
10324dcc5c2dSMatthew Dillon 
10331c7c3c6aSMatthew Dillon 
10341c7c3c6aSMatthew Dillon 	/*
10354dcc5c2dSMatthew Dillon 	 * Return VM_PAGER_FAIL if we have nothing to do.  Return mreq
10364dcc5c2dSMatthew Dillon 	 * still busy, but the others unbusied.
10371c7c3c6aSMatthew Dillon 	 */
10384dcc5c2dSMatthew Dillon 	if (blk == SWAPBLK_NONE)
103926f9a767SRodney W. Grimes 		return (VM_PAGER_FAIL);
1040df8bae1dSRodney W. Grimes 
104116f62314SDavid Greenman 	/*
10428630c117SAlan Cox 	 * Getpbuf() can sleep.
10438630c117SAlan Cox 	 */
10448630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
10458630c117SAlan Cox 	/*
104616f62314SDavid Greenman 	 * Get a swap buffer header to perform the IO
104716f62314SDavid Greenman 	 */
10481c7c3c6aSMatthew Dillon 	bp = getpbuf(&nsw_rcount);
104926f9a767SRodney W. Grimes 
105016f62314SDavid Greenman 	/*
105116f62314SDavid Greenman 	 * map our page(s) into kva for input
10521c7c3c6aSMatthew Dillon 	 *
10531c7c3c6aSMatthew Dillon 	 * NOTE: B_PAGING is set by pbgetvp()
105416f62314SDavid Greenman 	 */
1055d68d828bSAlan Cox 	pmap_qenter((vm_offset_t)bp->b_data, m + i, j - i);
10561c7c3c6aSMatthew Dillon 
105721144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
10581c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1059fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1060fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
10611c7c3c6aSMatthew Dillon 	bp->b_blkno = blk - (reqpage - i);
10621c7c3c6aSMatthew Dillon 	bp->b_bcount = PAGE_SIZE * (j - i);
10631c7c3c6aSMatthew Dillon 	bp->b_bufsize = PAGE_SIZE * (j - i);
10641c7c3c6aSMatthew Dillon 	bp->b_pager.pg_reqpage = reqpage - i;
10651c7c3c6aSMatthew Dillon 
10668630c117SAlan Cox 	VM_OBJECT_LOCK(object);
1067b365ea9eSAlan Cox 	vm_page_lock_queues();
10681c7c3c6aSMatthew Dillon 	{
10691c7c3c6aSMatthew Dillon 		int k;
10701c7c3c6aSMatthew Dillon 
10711c7c3c6aSMatthew Dillon 		for (k = i; k < j; ++k) {
10721c7c3c6aSMatthew Dillon 			bp->b_pages[k - i] = m[k];
10731c7c3c6aSMatthew Dillon 			vm_page_flag_set(m[k], PG_SWAPINPROG);
10741c7c3c6aSMatthew Dillon 		}
10751c7c3c6aSMatthew Dillon 	}
1076b365ea9eSAlan Cox 	vm_page_unlock_queues();
10778630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
10781c7c3c6aSMatthew Dillon 	bp->b_npages = j - i;
107926f9a767SRodney W. Grimes 
10800d94caffSDavid Greenman 	pbgetvp(swapdev_vp, bp);
1081df8bae1dSRodney W. Grimes 
1082976e77fcSDavid Greenman 	cnt.v_swapin++;
10831c7c3c6aSMatthew Dillon 	cnt.v_swappgsin += bp->b_npages;
10841c7c3c6aSMatthew Dillon 
1085df8bae1dSRodney W. Grimes 	/*
10861c7c3c6aSMatthew Dillon 	 * We still hold the lock on mreq, and our automatic completion routine
10871c7c3c6aSMatthew Dillon 	 * does not remove it.
1088df8bae1dSRodney W. Grimes 	 */
1089d68d828bSAlan Cox 	VM_OBJECT_LOCK(mreq->object);
10901c7c3c6aSMatthew Dillon 	vm_object_pip_add(mreq->object, bp->b_npages);
1091d68d828bSAlan Cox 	VM_OBJECT_UNLOCK(mreq->object);
10921c7c3c6aSMatthew Dillon 	lastpindex = m[j-1]->pindex;
10931c7c3c6aSMatthew Dillon 
10941c7c3c6aSMatthew Dillon 	/*
10951c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
10961c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
10971c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
10981c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
10991c7c3c6aSMatthew Dillon 	 *
11001c7c3c6aSMatthew Dillon 	 * The other pages in our m[] array are also released on completion,
11011c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
11021c7c3c6aSMatthew Dillon 	 *
1103ea3aecf5SPeter Wemm 	 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
11041c7c3c6aSMatthew Dillon 	 */
1105b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
110686270230SPoul-Henning Kamp 	VOP_STRATEGY(bp->b_vp, bp);
110726f9a767SRodney W. Grimes 
110826f9a767SRodney W. Grimes 	/*
11091c7c3c6aSMatthew Dillon 	 * wait for the page we want to complete.  PG_SWAPINPROG is always
11101c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
11111c7c3c6aSMatthew Dillon 	 * is set in the meta-data.
111226f9a767SRodney W. Grimes 	 */
11131c7c3c6aSMatthew Dillon 	s = splvm();
1114b365ea9eSAlan Cox 	vm_page_lock_queues();
11151c7c3c6aSMatthew Dillon 	while ((mreq->flags & PG_SWAPINPROG) != 0) {
11161c7c3c6aSMatthew Dillon 		vm_page_flag_set(mreq, PG_WANTED | PG_REFERENCED);
11171c7c3c6aSMatthew Dillon 		cnt.v_intrans++;
1118b365ea9eSAlan Cox 		if (msleep(mreq, &vm_page_queue_mtx, PSWP, "swread", hz*20)) {
1119ac1e407bSBruce Evans 			printf(
11201c7c3c6aSMatthew Dillon 			    "swap_pager: indefinite wait buffer: device:"
1121af647ddeSBruce Evans 				" %s, blkno: %ld, size: %ld\n",
1122af647ddeSBruce Evans 			    devtoname(bp->b_dev), (long)bp->b_blkno,
1123af647ddeSBruce Evans 			    bp->b_bcount
11241c7c3c6aSMatthew Dillon 			);
11251c7c3c6aSMatthew Dillon 		}
11261b119d9dSDavid Greenman 	}
1127b365ea9eSAlan Cox 	vm_page_unlock_queues();
1128df8bae1dSRodney W. Grimes 	splx(s);
112926f9a767SRodney W. Grimes 
11308630c117SAlan Cox 	VM_OBJECT_LOCK(mreq->object);
113126f9a767SRodney W. Grimes 	/*
1132a1287949SEivind Eklund 	 * mreq is left busied after completion, but all the other pages
11331c7c3c6aSMatthew Dillon 	 * are freed.  If we had an unrecoverable read error the page will
11341c7c3c6aSMatthew Dillon 	 * not be valid.
113526f9a767SRodney W. Grimes 	 */
11361c7c3c6aSMatthew Dillon 	if (mreq->valid != VM_PAGE_BITS_ALL) {
11371c7c3c6aSMatthew Dillon 		return (VM_PAGER_ERROR);
113826f9a767SRodney W. Grimes 	} else {
11391c7c3c6aSMatthew Dillon 		return (VM_PAGER_OK);
114026f9a767SRodney W. Grimes 	}
11411c7c3c6aSMatthew Dillon 
11421c7c3c6aSMatthew Dillon 	/*
11431c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
11441c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
11451c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
11461c7c3c6aSMatthew Dillon 	 * to all-zero's later.
11471c7c3c6aSMatthew Dillon 	 */
1148df8bae1dSRodney W. Grimes }
1149df8bae1dSRodney W. Grimes 
11501c7c3c6aSMatthew Dillon /*
11511c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
11521c7c3c6aSMatthew Dillon  *
11531c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
11541c7c3c6aSMatthew Dillon  *
11551c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
11561c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
11571c7c3c6aSMatthew Dillon  *
1158ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
11591c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
11601c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
11611c7c3c6aSMatthew Dillon  *	which needs work.
11621c7c3c6aSMatthew Dillon  *
11631c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
11641c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
11651c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
11661c7c3c6aSMatthew Dillon  *	completion.
11671c7c3c6aSMatthew Dillon  *
11681c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
11691c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
11701c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
11711c7c3c6aSMatthew Dillon  */
1172e4542174SMatthew Dillon void
117324a1cce3SDavid Greenman swap_pager_putpages(object, m, count, sync, rtvals)
117424a1cce3SDavid Greenman 	vm_object_t object;
117526f9a767SRodney W. Grimes 	vm_page_t *m;
117626f9a767SRodney W. Grimes 	int count;
117724a1cce3SDavid Greenman 	boolean_t sync;
117826f9a767SRodney W. Grimes 	int *rtvals;
1179df8bae1dSRodney W. Grimes {
11801c7c3c6aSMatthew Dillon 	int i;
11811c7c3c6aSMatthew Dillon 	int n = 0;
1182df8bae1dSRodney W. Grimes 
11830cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
11841c7c3c6aSMatthew Dillon 	if (count && m[0]->object != object) {
11851c7c3c6aSMatthew Dillon 		panic("swap_pager_getpages: object mismatch %p/%p",
11861c7c3c6aSMatthew Dillon 		    object,
11871c7c3c6aSMatthew Dillon 		    m[0]->object
11881c7c3c6aSMatthew Dillon 		);
11891c7c3c6aSMatthew Dillon 	}
11901c7c3c6aSMatthew Dillon 	/*
11911c7c3c6aSMatthew Dillon 	 * Step 1
11921c7c3c6aSMatthew Dillon 	 *
11931c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
11941c7c3c6aSMatthew Dillon 	 * check for bogus sysops
11951c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
11961c7c3c6aSMatthew Dillon 	 */
11974dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
11984dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
1199e47ed70bSJohn Dyson 
1200e47ed70bSJohn Dyson 	if (curproc != pageproc)
1201e47ed70bSJohn Dyson 		sync = TRUE;
120226f9a767SRodney W. Grimes 
12031c7c3c6aSMatthew Dillon 	/*
12041c7c3c6aSMatthew Dillon 	 * Step 2
12051c7c3c6aSMatthew Dillon 	 *
1206ad3cce20SMatthew Dillon 	 * Update nsw parameters from swap_async_max sysctl values.
1207ad3cce20SMatthew Dillon 	 * Do not let the sysop crash the machine with bogus numbers.
1208327f4e83SMatthew Dillon 	 */
12096d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
1210327f4e83SMatthew Dillon 	if (swap_async_max != nsw_wcount_async_max) {
1211327f4e83SMatthew Dillon 		int n;
1212327f4e83SMatthew Dillon 		int s;
1213327f4e83SMatthew Dillon 
1214327f4e83SMatthew Dillon 		/*
1215327f4e83SMatthew Dillon 		 * limit range
1216327f4e83SMatthew Dillon 		 */
1217327f4e83SMatthew Dillon 		if ((n = swap_async_max) > nswbuf / 2)
1218327f4e83SMatthew Dillon 			n = nswbuf / 2;
1219327f4e83SMatthew Dillon 		if (n < 1)
1220327f4e83SMatthew Dillon 			n = 1;
1221327f4e83SMatthew Dillon 		swap_async_max = n;
1222327f4e83SMatthew Dillon 
1223327f4e83SMatthew Dillon 		/*
1224327f4e83SMatthew Dillon 		 * Adjust difference ( if possible ).  If the current async
1225327f4e83SMatthew Dillon 		 * count is too low, we may not be able to make the adjustment
1226327f4e83SMatthew Dillon 		 * at this time.
1227327f4e83SMatthew Dillon 		 */
1228327f4e83SMatthew Dillon 		s = splvm();
1229327f4e83SMatthew Dillon 		n -= nsw_wcount_async_max;
1230327f4e83SMatthew Dillon 		if (nsw_wcount_async + n >= 0) {
1231327f4e83SMatthew Dillon 			nsw_wcount_async += n;
1232327f4e83SMatthew Dillon 			nsw_wcount_async_max += n;
1233327f4e83SMatthew Dillon 			wakeup(&nsw_wcount_async);
1234327f4e83SMatthew Dillon 		}
1235327f4e83SMatthew Dillon 		splx(s);
1236327f4e83SMatthew Dillon 	}
12376d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
1238327f4e83SMatthew Dillon 
1239327f4e83SMatthew Dillon 	/*
1240327f4e83SMatthew Dillon 	 * Step 3
1241327f4e83SMatthew Dillon 	 *
12421c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
12431c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
12441c7c3c6aSMatthew Dillon 	 * successfully.
12451c7c3c6aSMatthew Dillon 	 */
12461c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
12471c7c3c6aSMatthew Dillon 		int s;
12481c7c3c6aSMatthew Dillon 		int j;
12491c7c3c6aSMatthew Dillon 		struct buf *bp;
1250a316d390SJohn Dyson 		daddr_t blk;
125126f9a767SRodney W. Grimes 
1252df8bae1dSRodney W. Grimes 		/*
12531c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1254df8bae1dSRodney W. Grimes 		 */
12551c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1256327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
12571c7c3c6aSMatthew Dillon 
12584dcc5c2dSMatthew Dillon 		s = splvm();
12594dcc5c2dSMatthew Dillon 
126026f9a767SRodney W. Grimes 		/*
12611c7c3c6aSMatthew Dillon 		 * Get biggest block of swap we can.  If we fail, fall
12621c7c3c6aSMatthew Dillon 		 * back and try to allocate a smaller block.  Don't go
12631c7c3c6aSMatthew Dillon 		 * overboard trying to allocate space if it would overly
12641c7c3c6aSMatthew Dillon 		 * fragment swap.
126526f9a767SRodney W. Grimes 		 */
12661c7c3c6aSMatthew Dillon 		while (
12671c7c3c6aSMatthew Dillon 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
12681c7c3c6aSMatthew Dillon 		    n > 4
12691c7c3c6aSMatthew Dillon 		) {
12701c7c3c6aSMatthew Dillon 			n >>= 1;
127126f9a767SRodney W. Grimes 		}
12721c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
12734dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
12741c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
12754dcc5c2dSMatthew Dillon 			splx(s);
12761c7c3c6aSMatthew Dillon 			continue;
127726f9a767SRodney W. Grimes 		}
127826f9a767SRodney W. Grimes 
127926f9a767SRodney W. Grimes 		/*
12804dcc5c2dSMatthew Dillon 		 * The I/O we are constructing cannot cross a physical
12814dcc5c2dSMatthew Dillon 		 * disk boundry in the swap stripe.  Note: we are still
12824dcc5c2dSMatthew Dillon 		 * at splvm().
128326f9a767SRodney W. Grimes 		 */
12841c7c3c6aSMatthew Dillon 		if ((blk ^ (blk + n)) & dmmax_mask) {
12851c7c3c6aSMatthew Dillon 			j = ((blk + dmmax) & dmmax_mask) - blk;
12861c7c3c6aSMatthew Dillon 			swp_pager_freeswapspace(blk + j, n - j);
12871c7c3c6aSMatthew Dillon 			n = j;
1288e47ed70bSJohn Dyson 		}
128926f9a767SRodney W. Grimes 
129026f9a767SRodney W. Grimes 		/*
12911c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
12921c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
12931c7c3c6aSMatthew Dillon 		 *
12941c7c3c6aSMatthew Dillon 		 * NOTE: B_PAGING is set by pbgetvp()
129526f9a767SRodney W. Grimes 		 */
1296327f4e83SMatthew Dillon 		if (sync == TRUE) {
1297327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_sync);
1298327f4e83SMatthew Dillon 		} else {
1299327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_async);
130021144e3bSPoul-Henning Kamp 			bp->b_flags = B_ASYNC;
1301327f4e83SMatthew Dillon 		}
1302912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
130326f9a767SRodney W. Grimes 
13041c7c3c6aSMatthew Dillon 		pmap_qenter((vm_offset_t)bp->b_data, &m[i], n);
13051c7c3c6aSMatthew Dillon 
1306fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1307fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
13081c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
13091c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
13101c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1311e47ed70bSJohn Dyson 
1312a5296b05SJulian Elischer 		pbgetvp(swapdev_vp, bp);
1313a5296b05SJulian Elischer 
13141c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
13151c7c3c6aSMatthew Dillon 			vm_page_t mreq = m[i+j];
13161c7c3c6aSMatthew Dillon 
13171c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
13181c7c3c6aSMatthew Dillon 			    mreq->object,
13191c7c3c6aSMatthew Dillon 			    mreq->pindex,
13204dcc5c2dSMatthew Dillon 			    blk + j
13211c7c3c6aSMatthew Dillon 			);
13227dbf82dcSMatthew Dillon 			vm_page_dirty(mreq);
13231c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_OK;
13241c7c3c6aSMatthew Dillon 
1325b365ea9eSAlan Cox 			vm_page_lock_queues();
13261c7c3c6aSMatthew Dillon 			vm_page_flag_set(mreq, PG_SWAPINPROG);
1327b365ea9eSAlan Cox 			vm_page_unlock_queues();
13281c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
13291c7c3c6aSMatthew Dillon 		}
13301c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1331a5296b05SJulian Elischer 		/*
1332a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1333a5296b05SJulian Elischer 		 */
1334a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1335a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
13361c7c3c6aSMatthew Dillon 
13371c7c3c6aSMatthew Dillon 		cnt.v_swapout++;
13381c7c3c6aSMatthew Dillon 		cnt.v_swappgsout += bp->b_npages;
13396a2eac8aSJeff Roberson 		VI_LOCK(swapdev_vp);
134026f9a767SRodney W. Grimes 		swapdev_vp->v_numoutput++;
13416a2eac8aSJeff Roberson 		VI_UNLOCK(swapdev_vp);
134226f9a767SRodney W. Grimes 
13434dcc5c2dSMatthew Dillon 		splx(s);
13444dcc5c2dSMatthew Dillon 
134526f9a767SRodney W. Grimes 		/*
13461c7c3c6aSMatthew Dillon 		 * asynchronous
13471c7c3c6aSMatthew Dillon 		 *
1348ea3aecf5SPeter Wemm 		 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
134926f9a767SRodney W. Grimes 		 */
13501c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
13511c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
135267812eacSKirk McKusick 			BUF_KERNPROC(bp);
135386270230SPoul-Henning Kamp 			VOP_STRATEGY(bp->b_vp, bp);
13541c7c3c6aSMatthew Dillon 
13551c7c3c6aSMatthew Dillon 			for (j = 0; j < n; ++j)
13561c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_PEND;
135723955314SAlfred Perlstein 			/* restart outter loop */
13581c7c3c6aSMatthew Dillon 			continue;
135926f9a767SRodney W. Grimes 		}
1360e47ed70bSJohn Dyson 
136126f9a767SRodney W. Grimes 		/*
13621c7c3c6aSMatthew Dillon 		 * synchronous
13631c7c3c6aSMatthew Dillon 		 *
1364ea3aecf5SPeter Wemm 		 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
13651c7c3c6aSMatthew Dillon 		 */
13661c7c3c6aSMatthew Dillon 		bp->b_iodone = swp_pager_sync_iodone;
136786270230SPoul-Henning Kamp 		VOP_STRATEGY(bp->b_vp, bp);
13681c7c3c6aSMatthew Dillon 
13691c7c3c6aSMatthew Dillon 		/*
13701c7c3c6aSMatthew Dillon 		 * Wait for the sync I/O to complete, then update rtvals.
13711c7c3c6aSMatthew Dillon 		 * We just set the rtvals[] to VM_PAGER_PEND so we can call
13721c7c3c6aSMatthew Dillon 		 * our async completion routine at the end, thus avoiding a
13731c7c3c6aSMatthew Dillon 		 * double-free.
137426f9a767SRodney W. Grimes 		 */
13754dcc5c2dSMatthew Dillon 		s = splbio();
137626f9a767SRodney W. Grimes 		while ((bp->b_flags & B_DONE) == 0) {
137724a1cce3SDavid Greenman 			tsleep(bp, PVM, "swwrt", 0);
137826f9a767SRodney W. Grimes 		}
13791c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j)
13801c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_PEND;
13811c7c3c6aSMatthew Dillon 		/*
13821c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
13831c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
13841c7c3c6aSMatthew Dillon 		 */
13851c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
138626f9a767SRodney W. Grimes 		splx(s);
13871c7c3c6aSMatthew Dillon 	}
13881c7c3c6aSMatthew Dillon }
13891c7c3c6aSMatthew Dillon 
13901c7c3c6aSMatthew Dillon /*
13911c7c3c6aSMatthew Dillon  *	swap_pager_sync_iodone:
13921c7c3c6aSMatthew Dillon  *
13931c7c3c6aSMatthew Dillon  *	Completion routine for synchronous reads and writes from/to swap.
13941c7c3c6aSMatthew Dillon  *	We just mark the bp is complete and wake up anyone waiting on it.
13951c7c3c6aSMatthew Dillon  *
13964dcc5c2dSMatthew Dillon  *	This routine may not block.  This routine is called at splbio() or better.
13971c7c3c6aSMatthew Dillon  */
13981c7c3c6aSMatthew Dillon static void
13991c7c3c6aSMatthew Dillon swp_pager_sync_iodone(bp)
14001c7c3c6aSMatthew Dillon 	struct buf *bp;
14011c7c3c6aSMatthew Dillon {
14021c7c3c6aSMatthew Dillon 	bp->b_flags |= B_DONE;
14031c7c3c6aSMatthew Dillon 	bp->b_flags &= ~B_ASYNC;
14041c7c3c6aSMatthew Dillon 	wakeup(bp);
14051c7c3c6aSMatthew Dillon }
14061c7c3c6aSMatthew Dillon 
14071c7c3c6aSMatthew Dillon /*
14081c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
14091c7c3c6aSMatthew Dillon  *
14101c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
14111c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
14121c7c3c6aSMatthew Dillon  *
14131c7c3c6aSMatthew Dillon  *	For READ operations, the pages are PG_BUSY'd.  For WRITE operations,
14141c7c3c6aSMatthew Dillon  *	the pages are vm_page_t->busy'd.  For READ operations, we PG_BUSY
14151c7c3c6aSMatthew Dillon  *	unbusy all pages except the 'main' request page.  For WRITE
14161c7c3c6aSMatthew Dillon  *	operations, we vm_page_t->busy'd unbusy all pages ( we can do this
14171c7c3c6aSMatthew Dillon  *	because we marked them all VM_PAGER_PEND on return from putpages ).
14181c7c3c6aSMatthew Dillon  *
14191c7c3c6aSMatthew Dillon  *	This routine may not block.
14204dcc5c2dSMatthew Dillon  *	This routine is called at splbio() or better
14214dcc5c2dSMatthew Dillon  *
14224dcc5c2dSMatthew Dillon  *	We up ourselves to splvm() as required for various vm_page related
14234dcc5c2dSMatthew Dillon  *	calls.
14241c7c3c6aSMatthew Dillon  */
14251c7c3c6aSMatthew Dillon static void
14261c7c3c6aSMatthew Dillon swp_pager_async_iodone(bp)
142754d92145SMatthew Dillon 	struct buf *bp;
14281c7c3c6aSMatthew Dillon {
14291c7c3c6aSMatthew Dillon 	int s;
14301c7c3c6aSMatthew Dillon 	int i;
14311c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
14321c7c3c6aSMatthew Dillon 
14330cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
14341c7c3c6aSMatthew Dillon 	bp->b_flags |= B_DONE;
14351c7c3c6aSMatthew Dillon 
14361c7c3c6aSMatthew Dillon 	/*
14371c7c3c6aSMatthew Dillon 	 * report error
14381c7c3c6aSMatthew Dillon 	 */
1439c244d2deSPoul-Henning Kamp 	if (bp->b_ioflags & BIO_ERROR) {
14401c7c3c6aSMatthew Dillon 		printf(
14411c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
14421c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
144321144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
14441c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
14451c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
14461c7c3c6aSMatthew Dillon 		    bp->b_error
14471c7c3c6aSMatthew Dillon 		);
14481c7c3c6aSMatthew Dillon 	}
14491c7c3c6aSMatthew Dillon 
14501c7c3c6aSMatthew Dillon 	/*
14514dcc5c2dSMatthew Dillon 	 * set object, raise to splvm().
14521c7c3c6aSMatthew Dillon 	 */
14534dcc5c2dSMatthew Dillon 	s = splvm();
145426f9a767SRodney W. Grimes 
145526f9a767SRodney W. Grimes 	/*
145626f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
145726f9a767SRodney W. Grimes 	 */
14581c7c3c6aSMatthew Dillon 	pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
145926f9a767SRodney W. Grimes 
146033a609ecSAlan Cox 	if (bp->b_npages) {
146133a609ecSAlan Cox 		object = bp->b_pages[0]->object;
146233a609ecSAlan Cox 		VM_OBJECT_LOCK(object);
146333a609ecSAlan Cox 	}
146440eab1e9SAlan Cox 	vm_page_lock_queues();
146526f9a767SRodney W. Grimes 	/*
14661c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
14671c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
14681c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
14691c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
14701c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
14711c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
147226f9a767SRodney W. Grimes 	 */
14731c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
14741c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1475e47ed70bSJohn Dyson 
14761c7c3c6aSMatthew Dillon 		vm_page_flag_clear(m, PG_SWAPINPROG);
1477e47ed70bSJohn Dyson 
1478c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1479ffc82b0aSJohn Dyson 			/*
14801c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
14811c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
14821c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
14831c7c3c6aSMatthew Dillon 			 * interrupt.
1484ffc82b0aSJohn Dyson 			 */
148521144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
14861c7c3c6aSMatthew Dillon 				/*
14871c7c3c6aSMatthew Dillon 				 * When reading, reqpage needs to stay
14881c7c3c6aSMatthew Dillon 				 * locked for the parent, but all other
14891c7c3c6aSMatthew Dillon 				 * pages can be freed.  We still want to
14901c7c3c6aSMatthew Dillon 				 * wakeup the parent waiting on the page,
14911c7c3c6aSMatthew Dillon 				 * though.  ( also: pg_reqpage can be -1 and
14921c7c3c6aSMatthew Dillon 				 * not match anything ).
14931c7c3c6aSMatthew Dillon 				 *
14941c7c3c6aSMatthew Dillon 				 * We have to wake specifically requested pages
14951c7c3c6aSMatthew Dillon 				 * up too because we cleared PG_SWAPINPROG and
14961c7c3c6aSMatthew Dillon 				 * someone may be waiting for that.
14971c7c3c6aSMatthew Dillon 				 *
14981c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1499956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15001c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15011c7c3c6aSMatthew Dillon 				 *
1502279d7226SMatthew Dillon 				 * XXX IT IS NOT LEGAL TO FREE THE PAGE HERE
1503279d7226SMatthew Dillon 				 * AS THIS MESSES WITH object->memq, and it is
1504279d7226SMatthew Dillon 				 * not legal to mess with object->memq from an
1505279d7226SMatthew Dillon 				 * interrupt.
15061c7c3c6aSMatthew Dillon 				 */
15071c7c3c6aSMatthew Dillon 				m->valid = 0;
15081c7c3c6aSMatthew Dillon 				vm_page_flag_clear(m, PG_ZERO);
15091c7c3c6aSMatthew Dillon 				if (i != bp->b_pager.pg_reqpage)
15101c7c3c6aSMatthew Dillon 					vm_page_free(m);
15111c7c3c6aSMatthew Dillon 				else
15121c7c3c6aSMatthew Dillon 					vm_page_flash(m);
15131c7c3c6aSMatthew Dillon 				/*
15141c7c3c6aSMatthew Dillon 				 * If i == bp->b_pager.pg_reqpage, do not wake
15151c7c3c6aSMatthew Dillon 				 * the page up.  The caller needs to.
15161c7c3c6aSMatthew Dillon 				 */
15171c7c3c6aSMatthew Dillon 			} else {
15181c7c3c6aSMatthew Dillon 				/*
15191c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
15201c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
15211c7c3c6aSMatthew Dillon 				 * then finish the I/O.
15221c7c3c6aSMatthew Dillon 				 */
15237dbf82dcSMatthew Dillon 				vm_page_dirty(m);
15241c7c3c6aSMatthew Dillon 				vm_page_activate(m);
15251c7c3c6aSMatthew Dillon 				vm_page_io_finish(m);
15261c7c3c6aSMatthew Dillon 			}
152721144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
15281c7c3c6aSMatthew Dillon 			/*
15291c7c3c6aSMatthew Dillon 			 * For read success, clear dirty bits.  Nobody should
15301c7c3c6aSMatthew Dillon 			 * have this page mapped but don't take any chances,
15311c7c3c6aSMatthew Dillon 			 * make sure the pmap modify bits are also cleared.
15321c7c3c6aSMatthew Dillon 			 *
15331c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1534956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
15351c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
15361c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
15371c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
15381c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
15391c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
15401c7c3c6aSMatthew Dillon 			 *
15411c7c3c6aSMatthew Dillon 			 * clear PG_ZERO in page.
15421c7c3c6aSMatthew Dillon 			 *
15431c7c3c6aSMatthew Dillon 			 * If not the requested page then deactivate it.
15441c7c3c6aSMatthew Dillon 			 *
15451c7c3c6aSMatthew Dillon 			 * Note that the requested page, reqpage, is left
15461c7c3c6aSMatthew Dillon 			 * busied, but we still have to wake it up.  The
15471c7c3c6aSMatthew Dillon 			 * other pages are released (unbusied) by
15481c7c3c6aSMatthew Dillon 			 * vm_page_wakeup().  We do not set reqpage's
15491c7c3c6aSMatthew Dillon 			 * valid bits here, it is up to the caller.
15501c7c3c6aSMatthew Dillon 			 */
15510385347cSPeter Wemm 			pmap_clear_modify(m);
15521c7c3c6aSMatthew Dillon 			m->valid = VM_PAGE_BITS_ALL;
15532c28a105SAlan Cox 			vm_page_undirty(m);
15541c7c3c6aSMatthew Dillon 			vm_page_flag_clear(m, PG_ZERO);
15551c7c3c6aSMatthew Dillon 
15561c7c3c6aSMatthew Dillon 			/*
15571c7c3c6aSMatthew Dillon 			 * We have to wake specifically requested pages
15581c7c3c6aSMatthew Dillon 			 * up too because we cleared PG_SWAPINPROG and
15591c7c3c6aSMatthew Dillon 			 * could be waiting for it in getpages.  However,
15601c7c3c6aSMatthew Dillon 			 * be sure to not unbusy getpages specifically
15611c7c3c6aSMatthew Dillon 			 * requested page - getpages expects it to be
15621c7c3c6aSMatthew Dillon 			 * left busy.
15631c7c3c6aSMatthew Dillon 			 */
15641c7c3c6aSMatthew Dillon 			if (i != bp->b_pager.pg_reqpage) {
15651c7c3c6aSMatthew Dillon 				vm_page_deactivate(m);
15661c7c3c6aSMatthew Dillon 				vm_page_wakeup(m);
15671c7c3c6aSMatthew Dillon 			} else {
15681c7c3c6aSMatthew Dillon 				vm_page_flash(m);
15691c7c3c6aSMatthew Dillon 			}
15701c7c3c6aSMatthew Dillon 		} else {
15711c7c3c6aSMatthew Dillon 			/*
15721c7c3c6aSMatthew Dillon 			 * For write success, clear the modify and dirty
15731c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
15741c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
15751c7c3c6aSMatthew Dillon 			 */
15760385347cSPeter Wemm 			pmap_clear_modify(m);
1577c52e7044SAlan Cox 			vm_page_undirty(m);
15781c7c3c6aSMatthew Dillon 			vm_page_io_finish(m);
1579936524aaSMatthew Dillon 			if (!vm_page_count_severe() || !vm_page_try_to_cache(m))
1580a12cc0e4SAlan Cox 				pmap_page_protect(m, VM_PROT_READ);
1581ffc82b0aSJohn Dyson 		}
1582df8bae1dSRodney W. Grimes 	}
158340eab1e9SAlan Cox 	vm_page_unlock_queues();
158426f9a767SRodney W. Grimes 
15851c7c3c6aSMatthew Dillon 	/*
15861c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
15871c7c3c6aSMatthew Dillon 	 * pip refs on the object.
15881c7c3c6aSMatthew Dillon 	 */
15890d420ad3SAlan Cox 	if (object != NULL) {
15901c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
15910d420ad3SAlan Cox 		VM_OBJECT_UNLOCK(object);
15920d420ad3SAlan Cox 	}
159326f9a767SRodney W. Grimes 
15941c7c3c6aSMatthew Dillon 	/*
15951c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
15961c7c3c6aSMatthew Dillon 	 */
1597327f4e83SMatthew Dillon 	relpbuf(
1598327f4e83SMatthew Dillon 	    bp,
159921144e3bSPoul-Henning Kamp 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1600327f4e83SMatthew Dillon 		((bp->b_flags & B_ASYNC) ?
1601327f4e83SMatthew Dillon 		    &nsw_wcount_async :
1602327f4e83SMatthew Dillon 		    &nsw_wcount_sync
1603327f4e83SMatthew Dillon 		)
1604327f4e83SMatthew Dillon 	    )
1605327f4e83SMatthew Dillon 	);
160626f9a767SRodney W. Grimes 	splx(s);
160726f9a767SRodney W. Grimes }
16081c7c3c6aSMatthew Dillon 
160992da00bbSMatthew Dillon /*
161092da00bbSMatthew Dillon  *	swap_pager_isswapped:
161192da00bbSMatthew Dillon  *
161292da00bbSMatthew Dillon  *	Return 1 if at least one page in the given object is paged
161392da00bbSMatthew Dillon  *	out to the given swap device.
161492da00bbSMatthew Dillon  *
161592da00bbSMatthew Dillon  *	This routine may not block.
161692da00bbSMatthew Dillon  */
16178f60c087SPoul-Henning Kamp int
16188f60c087SPoul-Henning Kamp swap_pager_isswapped(vm_object_t object, struct swdevt *sp)
16198f60c087SPoul-Henning Kamp {
162092da00bbSMatthew Dillon 	daddr_t index = 0;
162192da00bbSMatthew Dillon 	int bcount;
162292da00bbSMatthew Dillon 	int i;
162392da00bbSMatthew Dillon 
162417cd3642SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
162592da00bbSMatthew Dillon 	for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) {
162692da00bbSMatthew Dillon 		struct swblock *swap;
162792da00bbSMatthew Dillon 
162892da00bbSMatthew Dillon 		if ((swap = *swp_pager_hash(object, index)) != NULL) {
162992da00bbSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
163092da00bbSMatthew Dillon 				daddr_t v = swap->swb_pages[i];
16318f60c087SPoul-Henning Kamp 				if (v == SWAPBLK_NONE)
16328f60c087SPoul-Henning Kamp 					continue;
163312692209SPoul-Henning Kamp 				if (swp_pager_find_dev(v, 1) == sp)
163492da00bbSMatthew Dillon 					return 1;
163592da00bbSMatthew Dillon 			}
163692da00bbSMatthew Dillon 		}
163792da00bbSMatthew Dillon 
163892da00bbSMatthew Dillon 		index += SWAP_META_PAGES;
163992da00bbSMatthew Dillon 		if (index > 0x20000000)
164092da00bbSMatthew Dillon 			panic("swap_pager_isswapped: failed to locate all swap meta blocks");
164192da00bbSMatthew Dillon 	}
164292da00bbSMatthew Dillon 	return 0;
164392da00bbSMatthew Dillon }
164492da00bbSMatthew Dillon 
164592da00bbSMatthew Dillon /*
164692da00bbSMatthew Dillon  * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in
164792da00bbSMatthew Dillon  *
164892da00bbSMatthew Dillon  *	This routine dissociates the page at the given index within a
164992da00bbSMatthew Dillon  *	swap block from its backing store, paging it in if necessary.
165092da00bbSMatthew Dillon  *	If the page is paged in, it is placed in the inactive queue,
165192da00bbSMatthew Dillon  *	since it had its backing store ripped out from under it.
165292da00bbSMatthew Dillon  *	We also attempt to swap in all other pages in the swap block,
165392da00bbSMatthew Dillon  *	we only guarantee that the one at the specified index is
165492da00bbSMatthew Dillon  *	paged in.
165592da00bbSMatthew Dillon  *
165692da00bbSMatthew Dillon  *	XXX - The code to page the whole block in doesn't work, so we
165792da00bbSMatthew Dillon  *	      revert to the one-by-one behavior for now.  Sigh.
165892da00bbSMatthew Dillon  */
165992da00bbSMatthew Dillon static __inline void
166092da00bbSMatthew Dillon swp_pager_force_pagein(struct swblock *swap, int idx)
166192da00bbSMatthew Dillon {
166292da00bbSMatthew Dillon 	vm_object_t object;
166392da00bbSMatthew Dillon 	vm_page_t m;
166492da00bbSMatthew Dillon 	vm_pindex_t pindex;
166592da00bbSMatthew Dillon 
166692da00bbSMatthew Dillon 	object = swap->swb_object;
166792da00bbSMatthew Dillon 	pindex = swap->swb_index;
166892da00bbSMatthew Dillon 
1669d22bc710SAlan Cox 	VM_OBJECT_LOCK(object);
167092da00bbSMatthew Dillon 	vm_object_pip_add(object, 1);
167192da00bbSMatthew Dillon 	m = vm_page_grab(object, pindex + idx, VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
167292da00bbSMatthew Dillon 	if (m->valid == VM_PAGE_BITS_ALL) {
167392da00bbSMatthew Dillon 		vm_object_pip_subtract(object, 1);
16740fa05eaeSAlan Cox 		VM_OBJECT_UNLOCK(object);
167592da00bbSMatthew Dillon 		vm_page_lock_queues();
167692da00bbSMatthew Dillon 		vm_page_activate(m);
167792da00bbSMatthew Dillon 		vm_page_dirty(m);
167892da00bbSMatthew Dillon 		vm_page_wakeup(m);
167992da00bbSMatthew Dillon 		vm_page_unlock_queues();
168092da00bbSMatthew Dillon 		vm_pager_page_unswapped(m);
168192da00bbSMatthew Dillon 		return;
168292da00bbSMatthew Dillon 	}
168392da00bbSMatthew Dillon 
168492da00bbSMatthew Dillon 	if (swap_pager_getpages(object, &m, 1, 0) !=
168592da00bbSMatthew Dillon 	    VM_PAGER_OK)
168692da00bbSMatthew Dillon 		panic("swap_pager_force_pagein: read from swap failed");/*XXX*/
168792da00bbSMatthew Dillon 	vm_object_pip_subtract(object, 1);
16880fa05eaeSAlan Cox 	VM_OBJECT_UNLOCK(object);
168992da00bbSMatthew Dillon 
169092da00bbSMatthew Dillon 	vm_page_lock_queues();
169192da00bbSMatthew Dillon 	vm_page_dirty(m);
169292da00bbSMatthew Dillon 	vm_page_dontneed(m);
169392da00bbSMatthew Dillon 	vm_page_wakeup(m);
169492da00bbSMatthew Dillon 	vm_page_unlock_queues();
169592da00bbSMatthew Dillon 	vm_pager_page_unswapped(m);
169692da00bbSMatthew Dillon }
169792da00bbSMatthew Dillon 
169892da00bbSMatthew Dillon 
169992da00bbSMatthew Dillon /*
170092da00bbSMatthew Dillon  *	swap_pager_swapoff:
170192da00bbSMatthew Dillon  *
170292da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
170392da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
170492da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
170592da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
170692da00bbSMatthew Dillon  *
170792da00bbSMatthew Dillon  *	The sw_used parameter points to the field in the swdev structure
170892da00bbSMatthew Dillon  *	that contains a count of the number of blocks still allocated
170992da00bbSMatthew Dillon  *	on the device.  If we encounter objects with a nonzero pip count
171092da00bbSMatthew Dillon  *	in our scan, we use this number to determine if we're really done.
171192da00bbSMatthew Dillon  *
171292da00bbSMatthew Dillon  *	This routine may block.
171392da00bbSMatthew Dillon  */
1714e9c0cc15SPoul-Henning Kamp static void
17158f60c087SPoul-Henning Kamp swap_pager_swapoff(struct swdevt *sp, int *sw_used)
171692da00bbSMatthew Dillon {
171792da00bbSMatthew Dillon 	struct swblock **pswap;
171892da00bbSMatthew Dillon 	struct swblock *swap;
171992da00bbSMatthew Dillon 	vm_object_t waitobj;
172092da00bbSMatthew Dillon 	daddr_t v;
172192da00bbSMatthew Dillon 	int i, j;
172292da00bbSMatthew Dillon 
172392da00bbSMatthew Dillon 	GIANT_REQUIRED;
172492da00bbSMatthew Dillon 
172592da00bbSMatthew Dillon full_rescan:
172692da00bbSMatthew Dillon 	waitobj = NULL;
172792da00bbSMatthew Dillon 	for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */
172892da00bbSMatthew Dillon restart:
172992da00bbSMatthew Dillon 		pswap = &swhash[i];
173092da00bbSMatthew Dillon 		while ((swap = *pswap) != NULL) {
173192da00bbSMatthew Dillon                         for (j = 0; j < SWAP_META_PAGES; ++j) {
173292da00bbSMatthew Dillon                                 v = swap->swb_pages[j];
173392da00bbSMatthew Dillon                                 if (v != SWAPBLK_NONE &&
173412692209SPoul-Henning Kamp 				    swp_pager_find_dev(v, 1) == sp)
173592da00bbSMatthew Dillon                                         break;
173692da00bbSMatthew Dillon                         }
173792da00bbSMatthew Dillon 			if (j < SWAP_META_PAGES) {
173892da00bbSMatthew Dillon 				swp_pager_force_pagein(swap, j);
173992da00bbSMatthew Dillon 				goto restart;
174092da00bbSMatthew Dillon 			} else if (swap->swb_object->paging_in_progress) {
174192da00bbSMatthew Dillon 				if (!waitobj)
174292da00bbSMatthew Dillon 					waitobj = swap->swb_object;
174392da00bbSMatthew Dillon 			}
174492da00bbSMatthew Dillon 			pswap = &swap->swb_hnext;
174592da00bbSMatthew Dillon 		}
174692da00bbSMatthew Dillon 	}
174792da00bbSMatthew Dillon 	if (waitobj && *sw_used) {
174892da00bbSMatthew Dillon 	    /*
174992da00bbSMatthew Dillon 	     * We wait on an arbitrary object to clock our rescans
175092da00bbSMatthew Dillon 	     * to the rate of paging completion.
175192da00bbSMatthew Dillon 	     */
17521ca58953SAlan Cox 	    VM_OBJECT_LOCK(waitobj);
175392da00bbSMatthew Dillon 	    vm_object_pip_wait(waitobj, "swpoff");
17541ca58953SAlan Cox 	    VM_OBJECT_UNLOCK(waitobj);
175592da00bbSMatthew Dillon 	    goto full_rescan;
175692da00bbSMatthew Dillon 	}
175792da00bbSMatthew Dillon 	if (*sw_used)
175892da00bbSMatthew Dillon 	    panic("swapoff: failed to locate %d swap blocks", *sw_used);
175992da00bbSMatthew Dillon }
176092da00bbSMatthew Dillon 
17611c7c3c6aSMatthew Dillon /************************************************************************
17621c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
17631c7c3c6aSMatthew Dillon  ************************************************************************
17641c7c3c6aSMatthew Dillon  *
17651c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
17664dcc5c2dSMatthew Dillon  *	OBJT_SWAP object.  All swp_*() routines must be called at
17674dcc5c2dSMatthew Dillon  *	splvm() because swap can be freed up by the low level vm_page
17684dcc5c2dSMatthew Dillon  *	code which might be called from interrupts beyond what splbio() covers.
17691c7c3c6aSMatthew Dillon  *
17704dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
17714dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
17724dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
17731c7c3c6aSMatthew Dillon  */
17741c7c3c6aSMatthew Dillon 
17751c7c3c6aSMatthew Dillon /*
17761c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
17771c7c3c6aSMatthew Dillon  *
17781c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
17791c7c3c6aSMatthew Dillon  *	object.
17801c7c3c6aSMatthew Dillon  *
17811c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
17821c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
17831c7c3c6aSMatthew Dillon  *	assigned swapblk is freed.
17844dcc5c2dSMatthew Dillon  *
17854dcc5c2dSMatthew Dillon  *	This routine must be called at splvm(), except when used to convert
17864dcc5c2dSMatthew Dillon  *	an OBJT_DEFAULT object into an OBJT_SWAP object.
17871c7c3c6aSMatthew Dillon  */
17881c7c3c6aSMatthew Dillon static void
17891c7c3c6aSMatthew Dillon swp_pager_meta_build(
17901c7c3c6aSMatthew Dillon 	vm_object_t object,
179123f09d50SIan Dowse 	vm_pindex_t pindex,
17924dcc5c2dSMatthew Dillon 	daddr_t swapblk
17931c7c3c6aSMatthew Dillon ) {
17941c7c3c6aSMatthew Dillon 	struct swblock *swap;
17951c7c3c6aSMatthew Dillon 	struct swblock **pswap;
179623f09d50SIan Dowse 	int idx;
17971c7c3c6aSMatthew Dillon 
17980cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
17991c7c3c6aSMatthew Dillon 	/*
18001c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
18011c7c3c6aSMatthew Dillon 	 */
18021c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
18031c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
18041c7c3c6aSMatthew Dillon 		object->un_pager.swp.swp_bcount = 0;
18051c7c3c6aSMatthew Dillon 
1806a9fa2c05SAlfred Perlstein 		mtx_lock(&sw_alloc_mtx);
18071c7c3c6aSMatthew Dillon 		if (object->handle != NULL) {
18081c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
18091c7c3c6aSMatthew Dillon 			    NOBJLIST(object->handle),
18101c7c3c6aSMatthew Dillon 			    object,
18111c7c3c6aSMatthew Dillon 			    pager_object_list
18121c7c3c6aSMatthew Dillon 			);
18131c7c3c6aSMatthew Dillon 		} else {
18141c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
18151c7c3c6aSMatthew Dillon 			    &swap_pager_un_object_list,
18161c7c3c6aSMatthew Dillon 			    object,
18171c7c3c6aSMatthew Dillon 			    pager_object_list
18181c7c3c6aSMatthew Dillon 			);
18191c7c3c6aSMatthew Dillon 		}
1820a9fa2c05SAlfred Perlstein 		mtx_unlock(&sw_alloc_mtx);
18211c7c3c6aSMatthew Dillon 	}
18221c7c3c6aSMatthew Dillon 
18231c7c3c6aSMatthew Dillon 	/*
18241c7c3c6aSMatthew Dillon 	 * Locate hash entry.  If not found create, but if we aren't adding
18254dcc5c2dSMatthew Dillon 	 * anything just return.  If we run out of space in the map we wait
18264dcc5c2dSMatthew Dillon 	 * and, since the hash table may have changed, retry.
18271c7c3c6aSMatthew Dillon 	 */
18284dcc5c2dSMatthew Dillon retry:
182923f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
18301c7c3c6aSMatthew Dillon 
18311c7c3c6aSMatthew Dillon 	if ((swap = *pswap) == NULL) {
18321c7c3c6aSMatthew Dillon 		int i;
18331c7c3c6aSMatthew Dillon 
18341c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
18351c7c3c6aSMatthew Dillon 			return;
18361c7c3c6aSMatthew Dillon 
1837670d17b5SJeff Roberson 		swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT);
18384dcc5c2dSMatthew Dillon 		if (swap == NULL) {
18394dcc5c2dSMatthew Dillon 			VM_WAIT;
18404dcc5c2dSMatthew Dillon 			goto retry;
18414dcc5c2dSMatthew Dillon 		}
1842670d17b5SJeff Roberson 
18431c7c3c6aSMatthew Dillon 		swap->swb_hnext = NULL;
18441c7c3c6aSMatthew Dillon 		swap->swb_object = object;
184523f09d50SIan Dowse 		swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK;
18461c7c3c6aSMatthew Dillon 		swap->swb_count = 0;
18471c7c3c6aSMatthew Dillon 
18481c7c3c6aSMatthew Dillon 		++object->un_pager.swp.swp_bcount;
18491c7c3c6aSMatthew Dillon 
18501c7c3c6aSMatthew Dillon 		for (i = 0; i < SWAP_META_PAGES; ++i)
18511c7c3c6aSMatthew Dillon 			swap->swb_pages[i] = SWAPBLK_NONE;
18521c7c3c6aSMatthew Dillon 	}
18531c7c3c6aSMatthew Dillon 
18541c7c3c6aSMatthew Dillon 	/*
18551c7c3c6aSMatthew Dillon 	 * Delete prior contents of metadata
18561c7c3c6aSMatthew Dillon 	 */
185723f09d50SIan Dowse 	idx = pindex & SWAP_META_MASK;
18581c7c3c6aSMatthew Dillon 
185923f09d50SIan Dowse 	if (swap->swb_pages[idx] != SWAPBLK_NONE) {
186023f09d50SIan Dowse 		swp_pager_freeswapspace(swap->swb_pages[idx], 1);
18611c7c3c6aSMatthew Dillon 		--swap->swb_count;
18621c7c3c6aSMatthew Dillon 	}
18631c7c3c6aSMatthew Dillon 
18641c7c3c6aSMatthew Dillon 	/*
18651c7c3c6aSMatthew Dillon 	 * Enter block into metadata
18661c7c3c6aSMatthew Dillon 	 */
186723f09d50SIan Dowse 	swap->swb_pages[idx] = swapblk;
18684dcc5c2dSMatthew Dillon 	if (swapblk != SWAPBLK_NONE)
18691c7c3c6aSMatthew Dillon 		++swap->swb_count;
18701c7c3c6aSMatthew Dillon }
18711c7c3c6aSMatthew Dillon 
18721c7c3c6aSMatthew Dillon /*
18731c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
18741c7c3c6aSMatthew Dillon  *
18751c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
18761c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
18771c7c3c6aSMatthew Dillon  *
18781c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
18791c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
18801c7c3c6aSMatthew Dillon  *	with resident pages.
18811c7c3c6aSMatthew Dillon  *
18821c7c3c6aSMatthew Dillon  *	This routine must be called at splvm()
18831c7c3c6aSMatthew Dillon  */
18841c7c3c6aSMatthew Dillon static void
18854dcc5c2dSMatthew Dillon swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count)
18861c7c3c6aSMatthew Dillon {
18870cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
188823955314SAlfred Perlstein 
18891c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
18901c7c3c6aSMatthew Dillon 		return;
18911c7c3c6aSMatthew Dillon 
18921c7c3c6aSMatthew Dillon 	while (count > 0) {
18931c7c3c6aSMatthew Dillon 		struct swblock **pswap;
18941c7c3c6aSMatthew Dillon 		struct swblock *swap;
18951c7c3c6aSMatthew Dillon 
18961c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
18971c7c3c6aSMatthew Dillon 
18981c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
18991c7c3c6aSMatthew Dillon 			daddr_t v = swap->swb_pages[index & SWAP_META_MASK];
19001c7c3c6aSMatthew Dillon 
19011c7c3c6aSMatthew Dillon 			if (v != SWAPBLK_NONE) {
19021c7c3c6aSMatthew Dillon 				swp_pager_freeswapspace(v, 1);
19031c7c3c6aSMatthew Dillon 				swap->swb_pages[index & SWAP_META_MASK] =
19041c7c3c6aSMatthew Dillon 					SWAPBLK_NONE;
19051c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
19061c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
1907670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
19081c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
19091c7c3c6aSMatthew Dillon 				}
19101c7c3c6aSMatthew Dillon 			}
19111c7c3c6aSMatthew Dillon 			--count;
19121c7c3c6aSMatthew Dillon 			++index;
19131c7c3c6aSMatthew Dillon 		} else {
19144dcc5c2dSMatthew Dillon 			int n = SWAP_META_PAGES - (index & SWAP_META_MASK);
19151c7c3c6aSMatthew Dillon 			count -= n;
19161c7c3c6aSMatthew Dillon 			index += n;
19171c7c3c6aSMatthew Dillon 		}
19181c7c3c6aSMatthew Dillon 	}
19191c7c3c6aSMatthew Dillon }
19201c7c3c6aSMatthew Dillon 
19211c7c3c6aSMatthew Dillon /*
19221c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
19231c7c3c6aSMatthew Dillon  *
19241c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
19251c7c3c6aSMatthew Dillon  *	an object.
19264dcc5c2dSMatthew Dillon  *
19274dcc5c2dSMatthew Dillon  *	This routine must be called at splvm()
19281c7c3c6aSMatthew Dillon  */
19291c7c3c6aSMatthew Dillon static void
19301c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
19311c7c3c6aSMatthew Dillon {
19321c7c3c6aSMatthew Dillon 	daddr_t index = 0;
19331c7c3c6aSMatthew Dillon 
19340cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
193523955314SAlfred Perlstein 
19361c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19371c7c3c6aSMatthew Dillon 		return;
19381c7c3c6aSMatthew Dillon 
19391c7c3c6aSMatthew Dillon 	while (object->un_pager.swp.swp_bcount) {
19401c7c3c6aSMatthew Dillon 		struct swblock **pswap;
19411c7c3c6aSMatthew Dillon 		struct swblock *swap;
19421c7c3c6aSMatthew Dillon 
19431c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
19441c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
19451c7c3c6aSMatthew Dillon 			int i;
19461c7c3c6aSMatthew Dillon 
19471c7c3c6aSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
19481c7c3c6aSMatthew Dillon 				daddr_t v = swap->swb_pages[i];
19491c7c3c6aSMatthew Dillon 				if (v != SWAPBLK_NONE) {
19501c7c3c6aSMatthew Dillon 					--swap->swb_count;
19514dcc5c2dSMatthew Dillon 					swp_pager_freeswapspace(v, 1);
19521c7c3c6aSMatthew Dillon 				}
19531c7c3c6aSMatthew Dillon 			}
19541c7c3c6aSMatthew Dillon 			if (swap->swb_count != 0)
19551c7c3c6aSMatthew Dillon 				panic("swap_pager_meta_free_all: swb_count != 0");
19561c7c3c6aSMatthew Dillon 			*pswap = swap->swb_hnext;
1957670d17b5SJeff Roberson 			uma_zfree(swap_zone, swap);
19581c7c3c6aSMatthew Dillon 			--object->un_pager.swp.swp_bcount;
19591c7c3c6aSMatthew Dillon 		}
19601c7c3c6aSMatthew Dillon 		index += SWAP_META_PAGES;
19611c7c3c6aSMatthew Dillon 		if (index > 0x20000000)
19621c7c3c6aSMatthew Dillon 			panic("swp_pager_meta_free_all: failed to locate all swap meta blocks");
19631c7c3c6aSMatthew Dillon 	}
19641c7c3c6aSMatthew Dillon }
19651c7c3c6aSMatthew Dillon 
19661c7c3c6aSMatthew Dillon /*
19671c7c3c6aSMatthew Dillon  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
19681c7c3c6aSMatthew Dillon  *
19691c7c3c6aSMatthew Dillon  *	This routine is capable of looking up, popping, or freeing
19701c7c3c6aSMatthew Dillon  *	swapblk assignments in the swap meta data or in the vm_page_t.
19711c7c3c6aSMatthew Dillon  *	The routine typically returns the swapblk being looked-up, or popped,
19721c7c3c6aSMatthew Dillon  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
19731c7c3c6aSMatthew Dillon  *	was invalid.  This routine will automatically free any invalid
19741c7c3c6aSMatthew Dillon  *	meta-data swapblks.
19751c7c3c6aSMatthew Dillon  *
19761c7c3c6aSMatthew Dillon  *	It is not possible to store invalid swapblks in the swap meta data
19771c7c3c6aSMatthew Dillon  *	(other then a literal 'SWAPBLK_NONE'), so we don't bother checking.
19781c7c3c6aSMatthew Dillon  *
19791c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
19801c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
19811c7c3c6aSMatthew Dillon  *	busy page.
19821c7c3c6aSMatthew Dillon  *
19834dcc5c2dSMatthew Dillon  *	This routine must be called at splvm().
19841c7c3c6aSMatthew Dillon  *
19854dcc5c2dSMatthew Dillon  *	SWM_FREE	remove and free swap block from metadata
19861c7c3c6aSMatthew Dillon  *	SWM_POP		remove from meta data but do not free.. pop it out
19871c7c3c6aSMatthew Dillon  */
19881c7c3c6aSMatthew Dillon static daddr_t
19891c7c3c6aSMatthew Dillon swp_pager_meta_ctl(
19901c7c3c6aSMatthew Dillon 	vm_object_t object,
199123f09d50SIan Dowse 	vm_pindex_t pindex,
19921c7c3c6aSMatthew Dillon 	int flags
19931c7c3c6aSMatthew Dillon ) {
19944dcc5c2dSMatthew Dillon 	struct swblock **pswap;
19954dcc5c2dSMatthew Dillon 	struct swblock *swap;
19964dcc5c2dSMatthew Dillon 	daddr_t r1;
199723f09d50SIan Dowse 	int idx;
19984dcc5c2dSMatthew Dillon 
19990cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
20001c7c3c6aSMatthew Dillon 	/*
20011c7c3c6aSMatthew Dillon 	 * The meta data only exists of the object is OBJT_SWAP
20021c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
20031c7c3c6aSMatthew Dillon 	 */
20044dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
20051c7c3c6aSMatthew Dillon 		return (SWAPBLK_NONE);
20061c7c3c6aSMatthew Dillon 
20074dcc5c2dSMatthew Dillon 	r1 = SWAPBLK_NONE;
200823f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
20091c7c3c6aSMatthew Dillon 
20101c7c3c6aSMatthew Dillon 	if ((swap = *pswap) != NULL) {
201123f09d50SIan Dowse 		idx = pindex & SWAP_META_MASK;
201223f09d50SIan Dowse 		r1 = swap->swb_pages[idx];
20131c7c3c6aSMatthew Dillon 
20141c7c3c6aSMatthew Dillon 		if (r1 != SWAPBLK_NONE) {
20151c7c3c6aSMatthew Dillon 			if (flags & SWM_FREE) {
20164dcc5c2dSMatthew Dillon 				swp_pager_freeswapspace(r1, 1);
20171c7c3c6aSMatthew Dillon 				r1 = SWAPBLK_NONE;
20181c7c3c6aSMatthew Dillon 			}
20191c7c3c6aSMatthew Dillon 			if (flags & (SWM_FREE|SWM_POP)) {
202023f09d50SIan Dowse 				swap->swb_pages[idx] = SWAPBLK_NONE;
20211c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
20221c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
2023670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
20241c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
20251c7c3c6aSMatthew Dillon 				}
20261c7c3c6aSMatthew Dillon 			}
20271c7c3c6aSMatthew Dillon 		}
20281c7c3c6aSMatthew Dillon 	}
20291c7c3c6aSMatthew Dillon 	return (r1);
20301c7c3c6aSMatthew Dillon }
20311c7c3c6aSMatthew Dillon 
2032e4057dbdSPoul-Henning Kamp /********************************************************
2033e4057dbdSPoul-Henning Kamp  *		CHAINING FUNCTIONS			*
2034e4057dbdSPoul-Henning Kamp  ********************************************************
2035e4057dbdSPoul-Henning Kamp  *
2036e4057dbdSPoul-Henning Kamp  *	These functions support recursion of I/O operations
2037e4057dbdSPoul-Henning Kamp  *	on bp's, typically by chaining one or more 'child' bp's
2038e4057dbdSPoul-Henning Kamp  *	to the parent.  Synchronous, asynchronous, and semi-synchronous
2039e4057dbdSPoul-Henning Kamp  *	chaining is possible.
2040e4057dbdSPoul-Henning Kamp  */
2041e4057dbdSPoul-Henning Kamp 
2042e4057dbdSPoul-Henning Kamp 
2043e9c0cc15SPoul-Henning Kamp /*
2044e9c0cc15SPoul-Henning Kamp  *	swapdev_strategy:
2045e9c0cc15SPoul-Henning Kamp  *
2046e9c0cc15SPoul-Henning Kamp  *	VOP_STRATEGY() for swapdev_vp.
2047e9c0cc15SPoul-Henning Kamp  *	Perform swap strategy interleave device selection.
2048e9c0cc15SPoul-Henning Kamp  *
2049e9c0cc15SPoul-Henning Kamp  *	The bp is expected to be locked and *not* B_DONE on call.
2050e9c0cc15SPoul-Henning Kamp  */
2051e9c0cc15SPoul-Henning Kamp static int
2052e9c0cc15SPoul-Henning Kamp swapdev_strategy(ap)
2053e9c0cc15SPoul-Henning Kamp 	struct vop_strategy_args /* {
2054e9c0cc15SPoul-Henning Kamp 		struct vnode *a_vp;
2055e9c0cc15SPoul-Henning Kamp 		struct buf *a_bp;
2056e9c0cc15SPoul-Henning Kamp 	} */ *ap;
2057e9c0cc15SPoul-Henning Kamp {
20588f60c087SPoul-Henning Kamp 	int s, sz;
2059e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
2060e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2061e9c0cc15SPoul-Henning Kamp 	struct buf *bp;
2062e9c0cc15SPoul-Henning Kamp 
2063e9c0cc15SPoul-Henning Kamp 	KASSERT(ap->a_vp == ap->a_bp->b_vp, ("%s(%p != %p)",
2064e9c0cc15SPoul-Henning Kamp 	    __func__, ap->a_vp, ap->a_bp->b_vp));
2065e9c0cc15SPoul-Henning Kamp 	bp = ap->a_bp;
2066e9c0cc15SPoul-Henning Kamp 	sz = howmany(bp->b_bcount, PAGE_SIZE);
2067e9c0cc15SPoul-Henning Kamp 
2068e9c0cc15SPoul-Henning Kamp 	/*
2069e9c0cc15SPoul-Henning Kamp 	 * Convert interleaved swap into per-device swap.  Note that
2070e9c0cc15SPoul-Henning Kamp 	 * the block size is left in PAGE_SIZE'd chunks (for the newswap)
2071e9c0cc15SPoul-Henning Kamp 	 * here.
2072e9c0cc15SPoul-Henning Kamp 	 */
207312692209SPoul-Henning Kamp 	sp = swp_pager_find_dev(bp->b_blkno, sz);
2074e9c0cc15SPoul-Henning Kamp 	bp->b_dev = sp->sw_device;
2075e9c0cc15SPoul-Henning Kamp 	/*
2076e9c0cc15SPoul-Henning Kamp 	 * Convert from PAGE_SIZE'd to DEV_BSIZE'd chunks for the actual I/O
2077e9c0cc15SPoul-Henning Kamp 	 */
20788f60c087SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2079e9c0cc15SPoul-Henning Kamp 
2080e9c0cc15SPoul-Henning Kamp 	vhold(sp->sw_vp);
2081e9c0cc15SPoul-Henning Kamp 	s = splvm();
2082e9c0cc15SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
2083e9c0cc15SPoul-Henning Kamp 		vp = bp->b_vp;
2084e9c0cc15SPoul-Henning Kamp 		if (vp) {
2085e9c0cc15SPoul-Henning Kamp 			VI_LOCK(vp);
2086e9c0cc15SPoul-Henning Kamp 			vp->v_numoutput--;
2087e9c0cc15SPoul-Henning Kamp 			if ((vp->v_iflag & VI_BWAIT) && vp->v_numoutput <= 0) {
2088e9c0cc15SPoul-Henning Kamp 				vp->v_iflag &= ~VI_BWAIT;
2089e9c0cc15SPoul-Henning Kamp 				wakeup(&vp->v_numoutput);
2090e9c0cc15SPoul-Henning Kamp 			}
2091e9c0cc15SPoul-Henning Kamp 			VI_UNLOCK(vp);
2092e9c0cc15SPoul-Henning Kamp 		}
2093e9c0cc15SPoul-Henning Kamp 		VI_LOCK(sp->sw_vp);
2094e9c0cc15SPoul-Henning Kamp 		sp->sw_vp->v_numoutput++;
2095e9c0cc15SPoul-Henning Kamp 		VI_UNLOCK(sp->sw_vp);
2096e9c0cc15SPoul-Henning Kamp 	}
2097e9c0cc15SPoul-Henning Kamp 	bp->b_vp = sp->sw_vp;
2098e9c0cc15SPoul-Henning Kamp 	splx(s);
2099e04e4bacSPoul-Henning Kamp 	bp->b_flags |= B_KEEPGIANT;
2100e9c0cc15SPoul-Henning Kamp 	if (bp->b_vp->v_type == VCHR)
2101e9c0cc15SPoul-Henning Kamp 		VOP_SPECSTRATEGY(bp->b_vp, bp);
2102e9c0cc15SPoul-Henning Kamp 	else
2103e9c0cc15SPoul-Henning Kamp 		VOP_STRATEGY(bp->b_vp, bp);
2104e9c0cc15SPoul-Henning Kamp 	return 0;
2105e9c0cc15SPoul-Henning Kamp }
2106e9c0cc15SPoul-Henning Kamp 
2107e9c0cc15SPoul-Henning Kamp /*
2108e9c0cc15SPoul-Henning Kamp  * Create a special vnode op vector for swapdev_vp - we only use
2109e9c0cc15SPoul-Henning Kamp  * VOP_STRATEGY() and reclaim; everything else returns an error.
2110e9c0cc15SPoul-Henning Kamp  */
2111e9c0cc15SPoul-Henning Kamp vop_t **swapdev_vnodeop_p;
2112e9c0cc15SPoul-Henning Kamp static struct vnodeopv_entry_desc swapdev_vnodeop_entries[] = {
2113e9c0cc15SPoul-Henning Kamp 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
2114e9c0cc15SPoul-Henning Kamp 	{ &vop_reclaim_desc,		(vop_t *) vop_null },
2115e9c0cc15SPoul-Henning Kamp 	{ &vop_strategy_desc,		(vop_t *) swapdev_strategy },
2116e9c0cc15SPoul-Henning Kamp 	{ NULL, NULL }
2117e9c0cc15SPoul-Henning Kamp };
2118e9c0cc15SPoul-Henning Kamp static struct vnodeopv_desc swapdev_vnodeop_opv_desc =
2119e9c0cc15SPoul-Henning Kamp 	{ &swapdev_vnodeop_p, swapdev_vnodeop_entries };
2120e9c0cc15SPoul-Henning Kamp 
2121e9c0cc15SPoul-Henning Kamp VNODEOP_SET(swapdev_vnodeop_opv_desc);
2122e9c0cc15SPoul-Henning Kamp 
2123e9c0cc15SPoul-Henning Kamp /*
2124e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2125e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2126e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2127e9c0cc15SPoul-Henning Kamp  */
2128e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2129e9c0cc15SPoul-Henning Kamp struct swapon_args {
2130e9c0cc15SPoul-Henning Kamp 	char *name;
2131e9c0cc15SPoul-Henning Kamp };
2132e9c0cc15SPoul-Henning Kamp #endif
2133e9c0cc15SPoul-Henning Kamp 
2134e9c0cc15SPoul-Henning Kamp /*
2135e9c0cc15SPoul-Henning Kamp  * MPSAFE
2136e9c0cc15SPoul-Henning Kamp  */
2137e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2138e9c0cc15SPoul-Henning Kamp int
2139e9c0cc15SPoul-Henning Kamp swapon(td, uap)
2140e9c0cc15SPoul-Henning Kamp 	struct thread *td;
2141e9c0cc15SPoul-Henning Kamp 	struct swapon_args *uap;
2142e9c0cc15SPoul-Henning Kamp {
2143e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2144e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2145e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2146e9c0cc15SPoul-Henning Kamp 	int error;
2147e9c0cc15SPoul-Henning Kamp 
2148e9c0cc15SPoul-Henning Kamp 	mtx_lock(&Giant);
2149e9c0cc15SPoul-Henning Kamp 	error = suser(td);
2150e9c0cc15SPoul-Henning Kamp 	if (error)
2151e9c0cc15SPoul-Henning Kamp 		goto done2;
2152e9c0cc15SPoul-Henning Kamp 
2153e9c0cc15SPoul-Henning Kamp 	while (swdev_syscall_active)
2154e9c0cc15SPoul-Henning Kamp 	    tsleep(&swdev_syscall_active, PUSER - 1, "swpon", 0);
2155e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 1;
2156e9c0cc15SPoul-Henning Kamp 
2157e9c0cc15SPoul-Henning Kamp 	/*
2158e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2159e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2160e9c0cc15SPoul-Henning Kamp 	 */
2161e9c0cc15SPoul-Henning Kamp 	if (swap_zone == NULL) {
2162e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2163e9c0cc15SPoul-Henning Kamp 		goto done;
2164e9c0cc15SPoul-Henning Kamp 	}
2165e9c0cc15SPoul-Henning Kamp 
2166e9c0cc15SPoul-Henning Kamp 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->name, td);
2167e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2168e9c0cc15SPoul-Henning Kamp 	if (error)
2169e9c0cc15SPoul-Henning Kamp 		goto done;
2170e9c0cc15SPoul-Henning Kamp 
2171e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2172e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2173e9c0cc15SPoul-Henning Kamp 
2174e9c0cc15SPoul-Henning Kamp 	if (vn_isdisk(vp, &error))
2175e9c0cc15SPoul-Henning Kamp 		error = swaponvp(td, vp, vp->v_rdev, 0);
2176e9c0cc15SPoul-Henning Kamp 	else if (vp->v_type == VREG &&
2177e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
2178e9c0cc15SPoul-Henning Kamp 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred, td)) == 0) {
2179e9c0cc15SPoul-Henning Kamp 		/*
2180e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2181e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2182e9c0cc15SPoul-Henning Kamp 		 */
2183e9c0cc15SPoul-Henning Kamp 		error = swaponvp(td, vp, NODEV, attr.va_size / DEV_BSIZE);
2184e9c0cc15SPoul-Henning Kamp 	}
2185e9c0cc15SPoul-Henning Kamp 
2186e9c0cc15SPoul-Henning Kamp 	if (error)
2187e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2188e9c0cc15SPoul-Henning Kamp done:
2189e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 0;
2190e9c0cc15SPoul-Henning Kamp 	wakeup_one(&swdev_syscall_active);
2191e9c0cc15SPoul-Henning Kamp done2:
2192e9c0cc15SPoul-Henning Kamp 	mtx_unlock(&Giant);
2193e9c0cc15SPoul-Henning Kamp 	return (error);
2194e9c0cc15SPoul-Henning Kamp }
2195e9c0cc15SPoul-Henning Kamp 
2196e9c0cc15SPoul-Henning Kamp /*
2197e9c0cc15SPoul-Henning Kamp  * Swfree(index) frees the index'th portion of the swap map.
219885fdafb9SPoul-Henning Kamp  * Each of the NSWAPDEV devices provides 1/NSWAPDEV'th of the swap
2199e9c0cc15SPoul-Henning Kamp  * space, which is laid out with blocks of dmmax pages circularly
2200e9c0cc15SPoul-Henning Kamp  * among the devices.
2201e9c0cc15SPoul-Henning Kamp  *
2202e9c0cc15SPoul-Henning Kamp  * The new swap code uses page-sized blocks.  The old swap code used
2203e9c0cc15SPoul-Henning Kamp  * DEV_BSIZE'd chunks.
2204e9c0cc15SPoul-Henning Kamp  */
2205e9c0cc15SPoul-Henning Kamp int
2206e9c0cc15SPoul-Henning Kamp swaponvp(td, vp, dev, nblks)
2207e9c0cc15SPoul-Henning Kamp 	struct thread *td;
2208e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2209e9c0cc15SPoul-Henning Kamp 	dev_t dev;
2210e9c0cc15SPoul-Henning Kamp 	u_long nblks;
2211e9c0cc15SPoul-Henning Kamp {
2212e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
2213e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
2214e9c0cc15SPoul-Henning Kamp 	int error;
22158f60c087SPoul-Henning Kamp 	u_long mblocks;
2216e9c0cc15SPoul-Henning Kamp 	off_t mediasize;
2217e9c0cc15SPoul-Henning Kamp 
2218e9c0cc15SPoul-Henning Kamp 	if (!swapdev_vp) {
2219e9c0cc15SPoul-Henning Kamp 		error = getnewvnode("none", NULL, swapdev_vnodeop_p,
2220e9c0cc15SPoul-Henning Kamp 		    &swapdev_vp);
2221e9c0cc15SPoul-Henning Kamp 		if (error)
2222e9c0cc15SPoul-Henning Kamp 			panic("Cannot get vnode for swapdev");
2223e9c0cc15SPoul-Henning Kamp 		swapdev_vp->v_type = VNON;	/* Untyped */
2224e9c0cc15SPoul-Henning Kamp 	}
2225e9c0cc15SPoul-Henning Kamp 
2226e9c0cc15SPoul-Henning Kamp 	ASSERT_VOP_UNLOCKED(vp, "swaponvp");
22278f60c087SPoul-Henning Kamp 	dvbase = 0;
22288f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2229e9c0cc15SPoul-Henning Kamp 		if (sp->sw_vp == vp)
22308f60c087SPoul-Henning Kamp 			return (EBUSY);
2231665c0cafSPoul-Henning Kamp 		if (sp->sw_end >= dvbase) {
2232665c0cafSPoul-Henning Kamp 			/*
2233665c0cafSPoul-Henning Kamp 			 * We put one uncovered page between the devices
2234665c0cafSPoul-Henning Kamp 			 * in order to definitively prevent any cross-device
2235665c0cafSPoul-Henning Kamp 			 * I/O requests
2236665c0cafSPoul-Henning Kamp 			 */
2237665c0cafSPoul-Henning Kamp 			dvbase = sp->sw_end + 1;
2238665c0cafSPoul-Henning Kamp 		}
2239e9c0cc15SPoul-Henning Kamp 	}
22408f60c087SPoul-Henning Kamp 
2241e9c0cc15SPoul-Henning Kamp 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2242e9c0cc15SPoul-Henning Kamp #ifdef MAC
2243e9c0cc15SPoul-Henning Kamp 	error = mac_check_system_swapon(td->td_ucred, vp);
2244e9c0cc15SPoul-Henning Kamp 	if (error == 0)
2245e9c0cc15SPoul-Henning Kamp #endif
2246a8d43c90SPoul-Henning Kamp 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, -1);
2247e9c0cc15SPoul-Henning Kamp 	(void) VOP_UNLOCK(vp, 0, td);
2248e9c0cc15SPoul-Henning Kamp 	if (error)
2249e9c0cc15SPoul-Henning Kamp 		return (error);
2250e9c0cc15SPoul-Henning Kamp 
2251e9c0cc15SPoul-Henning Kamp 	if (nblks == 0) {
2252e9c0cc15SPoul-Henning Kamp 		error = VOP_IOCTL(vp, DIOCGMEDIASIZE, (caddr_t)&mediasize,
2253e9c0cc15SPoul-Henning Kamp 		    FREAD, td->td_ucred, td);
2254e9c0cc15SPoul-Henning Kamp 		if (error == 0)
2255e9c0cc15SPoul-Henning Kamp 		    nblks = mediasize / DEV_BSIZE;
2256e9c0cc15SPoul-Henning Kamp 	}
2257e9c0cc15SPoul-Henning Kamp 	/*
2258e9c0cc15SPoul-Henning Kamp 	 * XXX: We should also check that the sectorsize makes sense
2259e9c0cc15SPoul-Henning Kamp 	 * XXX: it should be a power of two, no larger than the page size.
2260e9c0cc15SPoul-Henning Kamp 	 */
2261e9c0cc15SPoul-Henning Kamp 	if (nblks == 0) {
2262e9c0cc15SPoul-Henning Kamp 		(void) VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td);
2263e9c0cc15SPoul-Henning Kamp 		return (ENXIO);
2264e9c0cc15SPoul-Henning Kamp 	}
2265e9c0cc15SPoul-Henning Kamp 
2266e9c0cc15SPoul-Henning Kamp 	/*
2267e9c0cc15SPoul-Henning Kamp 	 * If we go beyond this, we get overflows in the radix
2268e9c0cc15SPoul-Henning Kamp 	 * tree bitmap code.
2269e9c0cc15SPoul-Henning Kamp 	 */
22708f60c087SPoul-Henning Kamp 	mblocks = 0x40000000 / BLIST_META_RADIX;
2271d3dd89abSPoul-Henning Kamp 	if (nblks > mblocks) {
227285fdafb9SPoul-Henning Kamp 		printf("WARNING: reducing size to maximum of %lu blocks per swap unit\n",
2273d3dd89abSPoul-Henning Kamp 			mblocks);
2274d3dd89abSPoul-Henning Kamp 		nblks = mblocks;
2275e9c0cc15SPoul-Henning Kamp 	}
2276e9c0cc15SPoul-Henning Kamp 	/*
2277e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2278e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2279e9c0cc15SPoul-Henning Kamp 	 *
2280e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2281e9c0cc15SPoul-Henning Kamp 	 */
2282e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2283e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2284e9c0cc15SPoul-Henning Kamp 
22858f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2286e9c0cc15SPoul-Henning Kamp 	sp->sw_vp = vp;
2287e9c0cc15SPoul-Henning Kamp 	sp->sw_dev = dev2udev(dev);
2288e9c0cc15SPoul-Henning Kamp 	sp->sw_device = dev;
22898d677ef9SPoul-Henning Kamp 	sp->sw_flags = 0;
2290e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2291e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
22928f60c087SPoul-Henning Kamp 	sp->sw_first = dvbase;
22938f60c087SPoul-Henning Kamp 	sp->sw_end = dvbase + nblks;
2294e9c0cc15SPoul-Henning Kamp 
22958f60c087SPoul-Henning Kamp 	sp->sw_blist = blist_create(nblks);
2296e9c0cc15SPoul-Henning Kamp 	/*
22978f60c087SPoul-Henning Kamp 	 * Do not free the first block in order to avoid overwriting
22988f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2299e9c0cc15SPoul-Henning Kamp 	 */
23005ff0108dSPoul-Henning Kamp 	blist_free(sp->sw_blist, 1, nblks - 1);
2301e9c0cc15SPoul-Henning Kamp 
23028f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
23038f60c087SPoul-Henning Kamp 	nswapdev++;
23048f60c087SPoul-Henning Kamp 	swap_pager_avail += nblks;
2305e9c0cc15SPoul-Henning Kamp 	swap_pager_full = 0;
2306e9c0cc15SPoul-Henning Kamp 
2307e9c0cc15SPoul-Henning Kamp 	return (0);
2308e9c0cc15SPoul-Henning Kamp }
2309e9c0cc15SPoul-Henning Kamp 
2310e9c0cc15SPoul-Henning Kamp /*
2311e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2312e9c0cc15SPoul-Henning Kamp  *
2313e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2314e9c0cc15SPoul-Henning Kamp  */
2315e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2316e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2317e9c0cc15SPoul-Henning Kamp 	char *name;
2318e9c0cc15SPoul-Henning Kamp };
2319e9c0cc15SPoul-Henning Kamp #endif
2320e9c0cc15SPoul-Henning Kamp 
2321e9c0cc15SPoul-Henning Kamp /*
2322e9c0cc15SPoul-Henning Kamp  * MPSAFE
2323e9c0cc15SPoul-Henning Kamp  */
2324e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2325e9c0cc15SPoul-Henning Kamp int
2326e9c0cc15SPoul-Henning Kamp swapoff(td, uap)
2327e9c0cc15SPoul-Henning Kamp 	struct thread *td;
2328e9c0cc15SPoul-Henning Kamp 	struct swapoff_args *uap;
2329e9c0cc15SPoul-Henning Kamp {
2330e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2331e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2332e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
23338f60c087SPoul-Henning Kamp 	u_long nblks, dvbase;
23348f60c087SPoul-Henning Kamp 	int error;
2335e9c0cc15SPoul-Henning Kamp 
2336e9c0cc15SPoul-Henning Kamp 	mtx_lock(&Giant);
2337e9c0cc15SPoul-Henning Kamp 
2338e9c0cc15SPoul-Henning Kamp 	error = suser(td);
2339e9c0cc15SPoul-Henning Kamp 	if (error)
2340e9c0cc15SPoul-Henning Kamp 		goto done2;
2341e9c0cc15SPoul-Henning Kamp 
2342e9c0cc15SPoul-Henning Kamp 	while (swdev_syscall_active)
2343e9c0cc15SPoul-Henning Kamp 	    tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0);
2344e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 1;
2345e9c0cc15SPoul-Henning Kamp 
2346e9c0cc15SPoul-Henning Kamp 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->name, td);
2347e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2348e9c0cc15SPoul-Henning Kamp 	if (error)
2349e9c0cc15SPoul-Henning Kamp 		goto done;
2350e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2351e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2352e9c0cc15SPoul-Henning Kamp 
23538f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2354e9c0cc15SPoul-Henning Kamp 		if (sp->sw_vp == vp)
2355e9c0cc15SPoul-Henning Kamp 			goto found;
2356e9c0cc15SPoul-Henning Kamp 	}
2357e9c0cc15SPoul-Henning Kamp 	error = EINVAL;
2358e9c0cc15SPoul-Henning Kamp 	goto done;
2359e9c0cc15SPoul-Henning Kamp found:
2360e9c0cc15SPoul-Henning Kamp #ifdef MAC
2361e9c0cc15SPoul-Henning Kamp 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2362e9c0cc15SPoul-Henning Kamp 	error = mac_check_system_swapoff(td->td_ucred, vp);
2363e9c0cc15SPoul-Henning Kamp 	(void) VOP_UNLOCK(vp, 0, td);
2364e9c0cc15SPoul-Henning Kamp 	if (error != 0)
2365e9c0cc15SPoul-Henning Kamp 		goto done;
2366e9c0cc15SPoul-Henning Kamp #endif
2367e9c0cc15SPoul-Henning Kamp 
2368e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2369e9c0cc15SPoul-Henning Kamp 
2370e9c0cc15SPoul-Henning Kamp 	/*
2371e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2372e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2373e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2374e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2375e9c0cc15SPoul-Henning Kamp 	 */
23768f60c087SPoul-Henning Kamp 	if (cnt.v_free_count + cnt.v_cache_count + swap_pager_avail <
2377e9c0cc15SPoul-Henning Kamp 	    nblks + nswap_lowat) {
2378e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2379e9c0cc15SPoul-Henning Kamp 		goto done;
2380e9c0cc15SPoul-Henning Kamp 	}
2381e9c0cc15SPoul-Henning Kamp 
2382e9c0cc15SPoul-Henning Kamp 	/*
2383e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2384e9c0cc15SPoul-Henning Kamp 	 */
2385e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
23868f60c087SPoul-Henning Kamp 	for (dvbase = 0; dvbase < sp->sw_end; dvbase += dmmax) {
23878f60c087SPoul-Henning Kamp 		swap_pager_avail -= blist_fill(sp->sw_blist,
23888f60c087SPoul-Henning Kamp 		     dvbase, dmmax);
2389e9c0cc15SPoul-Henning Kamp 	}
2390e9c0cc15SPoul-Henning Kamp 
2391e9c0cc15SPoul-Henning Kamp 	/*
2392e9c0cc15SPoul-Henning Kamp 	 * Page in the contents of the device and close it.
2393e9c0cc15SPoul-Henning Kamp 	 */
2394e9c0cc15SPoul-Henning Kamp #ifndef NO_SWAPPING
23958f60c087SPoul-Henning Kamp        	vm_proc_swapin_all(sp);
2396e9c0cc15SPoul-Henning Kamp #endif /* !NO_SWAPPING */
23978f60c087SPoul-Henning Kamp 	swap_pager_swapoff(sp, &sp->sw_used);
2398e9c0cc15SPoul-Henning Kamp 
2399e9c0cc15SPoul-Henning Kamp 	VOP_CLOSE(vp, FREAD | FWRITE, td->td_ucred, td);
2400e9c0cc15SPoul-Henning Kamp 	vrele(vp);
2401e9c0cc15SPoul-Henning Kamp 	sp->sw_vp = NULL;
24028f60c087SPoul-Henning Kamp 	TAILQ_REMOVE(&swtailq, sp, sw_list);
24038f60c087SPoul-Henning Kamp 	if (swdevhd == sp)
24048f60c087SPoul-Henning Kamp 		swdevhd = NULL;
24058f60c087SPoul-Henning Kamp 	nswapdev--;
24068f60c087SPoul-Henning Kamp 	blist_destroy(sp->sw_blist);
24078f60c087SPoul-Henning Kamp 	free(sp, M_VMPGDATA);
2408e9c0cc15SPoul-Henning Kamp 
2409e9c0cc15SPoul-Henning Kamp done:
2410e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 0;
2411e9c0cc15SPoul-Henning Kamp 	wakeup_one(&swdev_syscall_active);
2412e9c0cc15SPoul-Henning Kamp done2:
2413e9c0cc15SPoul-Henning Kamp 	mtx_unlock(&Giant);
2414e9c0cc15SPoul-Henning Kamp 	return (error);
2415e9c0cc15SPoul-Henning Kamp }
2416e9c0cc15SPoul-Henning Kamp 
2417567104a1SPoul-Henning Kamp void
2418567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used)
2419567104a1SPoul-Henning Kamp {
2420567104a1SPoul-Henning Kamp 	struct swdevt *sp;
2421567104a1SPoul-Henning Kamp 
2422567104a1SPoul-Henning Kamp 	*total = 0;
2423567104a1SPoul-Henning Kamp 	*used = 0;
24248f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2425567104a1SPoul-Henning Kamp 		*total += sp->sw_nblks;
2426567104a1SPoul-Henning Kamp 		*used += sp->sw_used;
2427567104a1SPoul-Henning Kamp 	}
2428567104a1SPoul-Henning Kamp }
2429567104a1SPoul-Henning Kamp 
2430e9c0cc15SPoul-Henning Kamp static int
2431e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2432e9c0cc15SPoul-Henning Kamp {
2433e9c0cc15SPoul-Henning Kamp 	int	*name = (int *)arg1;
24348f60c087SPoul-Henning Kamp 	int	error, n;
2435e9c0cc15SPoul-Henning Kamp 	struct xswdev xs;
2436e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
2437e9c0cc15SPoul-Henning Kamp 
2438e9c0cc15SPoul-Henning Kamp 	if (arg2 != 1) /* name length */
2439e9c0cc15SPoul-Henning Kamp 		return (EINVAL);
2440e9c0cc15SPoul-Henning Kamp 
24418f60c087SPoul-Henning Kamp 	n = 0;
24428f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2443e9c0cc15SPoul-Henning Kamp 		if (n == *name) {
2444e9c0cc15SPoul-Henning Kamp 			xs.xsw_version = XSWDEV_VERSION;
2445e9c0cc15SPoul-Henning Kamp 			xs.xsw_dev = sp->sw_dev;
2446e9c0cc15SPoul-Henning Kamp 			xs.xsw_flags = sp->sw_flags;
2447e9c0cc15SPoul-Henning Kamp 			xs.xsw_nblks = sp->sw_nblks;
2448e9c0cc15SPoul-Henning Kamp 			xs.xsw_used = sp->sw_used;
2449e9c0cc15SPoul-Henning Kamp 
2450e9c0cc15SPoul-Henning Kamp 			error = SYSCTL_OUT(req, &xs, sizeof(xs));
2451e9c0cc15SPoul-Henning Kamp 			return (error);
2452e9c0cc15SPoul-Henning Kamp 		}
2453e9c0cc15SPoul-Henning Kamp 		n++;
2454e9c0cc15SPoul-Henning Kamp 	}
2455e9c0cc15SPoul-Henning Kamp 	return (ENOENT);
2456e9c0cc15SPoul-Henning Kamp }
2457e9c0cc15SPoul-Henning Kamp 
24588f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2459e9c0cc15SPoul-Henning Kamp     "Number of swap devices");
2460e9c0cc15SPoul-Henning Kamp SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info,
2461e9c0cc15SPoul-Henning Kamp     "Swap statistics by device");
2462ec38b344SPoul-Henning Kamp 
2463ec38b344SPoul-Henning Kamp /*
2464ec38b344SPoul-Henning Kamp  * vmspace_swap_count() - count the approximate swap useage in pages for a
2465ec38b344SPoul-Henning Kamp  *			  vmspace.
2466ec38b344SPoul-Henning Kamp  *
2467ec38b344SPoul-Henning Kamp  *	The map must be locked.
2468ec38b344SPoul-Henning Kamp  *
2469ec38b344SPoul-Henning Kamp  *	Swap useage is determined by taking the proportional swap used by
2470ec38b344SPoul-Henning Kamp  *	VM objects backing the VM map.  To make up for fractional losses,
2471ec38b344SPoul-Henning Kamp  *	if the VM object has any swap use at all the associated map entries
2472ec38b344SPoul-Henning Kamp  *	count for at least 1 swap page.
2473ec38b344SPoul-Henning Kamp  */
2474ec38b344SPoul-Henning Kamp int
2475ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2476ec38b344SPoul-Henning Kamp {
2477ec38b344SPoul-Henning Kamp 	vm_map_t map = &vmspace->vm_map;
2478ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
2479ec38b344SPoul-Henning Kamp 	int count = 0;
2480ec38b344SPoul-Henning Kamp 
2481ec38b344SPoul-Henning Kamp 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2482ec38b344SPoul-Henning Kamp 		vm_object_t object;
2483ec38b344SPoul-Henning Kamp 
2484ec38b344SPoul-Henning Kamp 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2485ec38b344SPoul-Henning Kamp 		    (object = cur->object.vm_object) != NULL) {
2486ec38b344SPoul-Henning Kamp 			VM_OBJECT_LOCK(object);
2487ec38b344SPoul-Henning Kamp 			if (object->type == OBJT_SWAP &&
2488ec38b344SPoul-Henning Kamp 			    object->un_pager.swp.swp_bcount != 0) {
2489ec38b344SPoul-Henning Kamp 				int n = (cur->end - cur->start) / PAGE_SIZE;
2490ec38b344SPoul-Henning Kamp 
2491ec38b344SPoul-Henning Kamp 				count += object->un_pager.swp.swp_bcount *
2492ec38b344SPoul-Henning Kamp 				    SWAP_META_PAGES * n / object->size + 1;
2493ec38b344SPoul-Henning Kamp 			}
2494ec38b344SPoul-Henning Kamp 			VM_OBJECT_UNLOCK(object);
2495ec38b344SPoul-Henning Kamp 		}
2496ec38b344SPoul-Henning Kamp 	}
2497ec38b344SPoul-Henning Kamp 	return (count);
2498ec38b344SPoul-Henning Kamp }
2499