xref: /freebsd/sys/vm/swap_pager.c (revision 07c348ea7b45226f64d280c186745ba5ab901492)
160727d8bSWarner Losh /*-
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 
7269921123SKonstantin Belousov #include "opt_compat.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>
80acd3428bSRobert Watson #include <sys/priv.h>
81df8bae1dSRodney W. Grimes #include <sys/proc.h>
829626b608SPoul-Henning Kamp #include <sys/bio.h>
83df8bae1dSRodney W. Grimes #include <sys/buf.h>
84e9c0cc15SPoul-Henning Kamp #include <sys/disk.h>
85e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h>
86e9c0cc15SPoul-Henning Kamp #include <sys/mount.h>
87e9c0cc15SPoul-Henning Kamp #include <sys/namei.h>
88df8bae1dSRodney W. Grimes #include <sys/vnode.h>
89df8bae1dSRodney W. Grimes #include <sys/malloc.h>
901ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
913364c323SKonstantin Belousov #include <sys/resource.h>
923364c323SKonstantin Belousov #include <sys/resourcevar.h>
9389f6b863SAttilio Rao #include <sys/rwlock.h>
94327f4e83SMatthew Dillon #include <sys/sysctl.h>
95e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
961c7c3c6aSMatthew Dillon #include <sys/blist.h>
971c7c3c6aSMatthew Dillon #include <sys/lock.h>
980cddd8f0SMatthew Dillon #include <sys/sx.h>
99936524aaSMatthew Dillon #include <sys/vmmeter.h>
100df8bae1dSRodney W. Grimes 
101aed55708SRobert Watson #include <security/mac/mac_framework.h>
102aed55708SRobert Watson 
103df8bae1dSRodney W. Grimes #include <vm/vm.h>
10421cd6e62SSeigo Tanimura #include <vm/pmap.h>
10521cd6e62SSeigo Tanimura #include <vm/vm_map.h>
10621cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
107efeaf95aSDavid Greenman #include <vm/vm_object.h>
108df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
109efeaf95aSDavid Greenman #include <vm/vm_pager.h>
110df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
111e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
112df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
113efeaf95aSDavid Greenman #include <vm/vm_extern.h>
114670d17b5SJeff Roberson #include <vm/uma.h>
115df8bae1dSRodney W. Grimes 
116dee34ca4SPoul-Henning Kamp #include <geom/geom.h>
117dee34ca4SPoul-Henning Kamp 
118ec38b344SPoul-Henning Kamp /*
11915523cf7SKonstantin Belousov  * SWB_NPAGES must be a power of 2.  It may be set to 1, 2, 4, 8, 16
12015523cf7SKonstantin Belousov  * or 32 pages per allocation.
12115523cf7SKonstantin Belousov  * The 32-page limit is due to the radix code (kern/subr_blist.c).
122ec38b344SPoul-Henning Kamp  */
123ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
124ec38b344SPoul-Henning Kamp #define MAX_PAGEOUT_CLUSTER 16
125ec38b344SPoul-Henning Kamp #endif
126ec38b344SPoul-Henning Kamp 
127ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
128ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
129ec38b344SPoul-Henning Kamp #endif
130ec38b344SPoul-Henning Kamp 
131ec38b344SPoul-Henning Kamp /*
13215523cf7SKonstantin Belousov  * The swblock structure maps an object and a small, fixed-size range
13315523cf7SKonstantin Belousov  * of page indices to disk addresses within a swap area.
13415523cf7SKonstantin Belousov  * The collection of these mappings is implemented as a hash table.
13515523cf7SKonstantin Belousov  * Unused disk addresses within a swap area are allocated and managed
13615523cf7SKonstantin Belousov  * using a blist.
137ec38b344SPoul-Henning Kamp  */
138ec38b344SPoul-Henning Kamp #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t))
139ec38b344SPoul-Henning Kamp #define SWAP_META_PAGES		(SWB_NPAGES * 2)
140ec38b344SPoul-Henning Kamp #define SWAP_META_MASK		(SWAP_META_PAGES - 1)
141ec38b344SPoul-Henning Kamp 
142e9c0cc15SPoul-Henning Kamp struct swblock {
143e9c0cc15SPoul-Henning Kamp 	struct swblock	*swb_hnext;
144e9c0cc15SPoul-Henning Kamp 	vm_object_t	swb_object;
145e9c0cc15SPoul-Henning Kamp 	vm_pindex_t	swb_index;
146e9c0cc15SPoul-Henning Kamp 	int		swb_count;
147e9c0cc15SPoul-Henning Kamp 	daddr_t		swb_pages[SWAP_META_PAGES];
148e9c0cc15SPoul-Henning Kamp };
149e9c0cc15SPoul-Henning Kamp 
1502c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
15120da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx;
1528f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1538f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1548f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1558f60c087SPoul-Henning Kamp int swap_pager_avail;
15604533e1eSKonstantin Belousov static struct sx swdev_syscall_lock;	/* serialize swap(on|off) */
157e9c0cc15SPoul-Henning Kamp 
1583364c323SKonstantin Belousov static vm_ooffset_t swap_total;
1598a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0,
1608a9c731fSIvan Voras     "Total amount of available swap storage.");
1613364c323SKonstantin Belousov static vm_ooffset_t swap_reserved;
1628a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0,
1638a9c731fSIvan Voras     "Amount of swap storage needed to back all allocated anonymous memory.");
1643364c323SKonstantin Belousov static int overcommit = 0;
1658a9c731fSIvan Voras SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0,
1668a9c731fSIvan Voras     "Configure virtual memory overcommit behavior. See tuning(7) "
1678a9c731fSIvan Voras     "for details.");
16861203277SDag-Erling Smørgrav static unsigned long swzone;
16961203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
17061203277SDag-Erling Smørgrav     "Actual size of swap metadata zone");
17161203277SDag-Erling Smørgrav static unsigned long swap_maxpages;
17261203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
17361203277SDag-Erling Smørgrav     "Maximum amount of swap supported");
1743364c323SKonstantin Belousov 
1753364c323SKonstantin Belousov /* bits from overcommit */
1763364c323SKonstantin Belousov #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
1773364c323SKonstantin Belousov #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
1783364c323SKonstantin Belousov #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
1793364c323SKonstantin Belousov 
1803364c323SKonstantin Belousov int
1813364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr)
1823364c323SKonstantin Belousov {
1833364c323SKonstantin Belousov 
184ef694c1aSEdward Tomasz Napierala 	return (swap_reserve_by_cred(incr, curthread->td_ucred));
1853364c323SKonstantin Belousov }
1863364c323SKonstantin Belousov 
1873364c323SKonstantin Belousov int
188ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
1893364c323SKonstantin Belousov {
1905c0e1c11SKonstantin Belousov 	vm_ooffset_t r, s;
1913364c323SKonstantin Belousov 	int res, error;
1923364c323SKonstantin Belousov 	static int curfail;
1933364c323SKonstantin Belousov 	static struct timeval lastfail;
194ef694c1aSEdward Tomasz Napierala 	struct uidinfo *uip;
195ef694c1aSEdward Tomasz Napierala 
196ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
1973364c323SKonstantin Belousov 
1983364c323SKonstantin Belousov 	if (incr & PAGE_MASK)
1993364c323SKonstantin Belousov 		panic("swap_reserve: & PAGE_MASK");
2003364c323SKonstantin Belousov 
201afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2024b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
2031ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2041ba5ad42SEdward Tomasz Napierala 		error = racct_add(curproc, RACCT_SWAP, incr);
2051ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2061ba5ad42SEdward Tomasz Napierala 		if (error != 0)
2071ba5ad42SEdward Tomasz Napierala 			return (0);
2084b5c9cf6SEdward Tomasz Napierala 	}
209afcc55f3SEdward Tomasz Napierala #endif
2101ba5ad42SEdward Tomasz Napierala 
2113364c323SKonstantin Belousov 	res = 0;
2123364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2133364c323SKonstantin Belousov 	r = swap_reserved + incr;
2143364c323SKonstantin Belousov 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
21544f1c916SBryan Drewery 		s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - vm_cnt.v_wire_count;
2163364c323SKonstantin Belousov 		s *= PAGE_SIZE;
2173364c323SKonstantin Belousov 	} else
2183364c323SKonstantin Belousov 		s = 0;
2193364c323SKonstantin Belousov 	s += swap_total;
2203364c323SKonstantin Belousov 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
2213364c323SKonstantin Belousov 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
2223364c323SKonstantin Belousov 		res = 1;
2233364c323SKonstantin Belousov 		swap_reserved = r;
2243364c323SKonstantin Belousov 	}
2253364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2263364c323SKonstantin Belousov 
2273364c323SKonstantin Belousov 	if (res) {
2283364c323SKonstantin Belousov 		UIDINFO_VMSIZE_LOCK(uip);
2295c0e1c11SKonstantin Belousov 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
230f6f6d240SMateusz Guzik 		    uip->ui_vmsize + incr > lim_cur(curthread, RLIMIT_SWAP) &&
2315c0e1c11SKonstantin Belousov 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT))
2323364c323SKonstantin Belousov 			res = 0;
2333364c323SKonstantin Belousov 		else
2343364c323SKonstantin Belousov 			uip->ui_vmsize += incr;
2353364c323SKonstantin Belousov 		UIDINFO_VMSIZE_UNLOCK(uip);
2363364c323SKonstantin Belousov 		if (!res) {
2373364c323SKonstantin Belousov 			mtx_lock(&sw_dev_mtx);
2383364c323SKonstantin Belousov 			swap_reserved -= incr;
2393364c323SKonstantin Belousov 			mtx_unlock(&sw_dev_mtx);
2403364c323SKonstantin Belousov 		}
2413364c323SKonstantin Belousov 	}
2423364c323SKonstantin Belousov 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
2433364c323SKonstantin Belousov 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
244134465d7SKonstantin Belousov 		    uip->ui_uid, curproc->p_pid, incr);
2453364c323SKonstantin Belousov 	}
2463364c323SKonstantin Belousov 
247afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2481ba5ad42SEdward Tomasz Napierala 	if (!res) {
2491ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2501ba5ad42SEdward Tomasz Napierala 		racct_sub(curproc, RACCT_SWAP, incr);
2511ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2521ba5ad42SEdward Tomasz Napierala 	}
253afcc55f3SEdward Tomasz Napierala #endif
2541ba5ad42SEdward Tomasz Napierala 
2553364c323SKonstantin Belousov 	return (res);
2563364c323SKonstantin Belousov }
2573364c323SKonstantin Belousov 
2583364c323SKonstantin Belousov void
2593364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr)
2603364c323SKonstantin Belousov {
2613364c323SKonstantin Belousov 	struct uidinfo *uip;
2623364c323SKonstantin Belousov 
2633364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2643364c323SKonstantin Belousov 	swap_reserved += incr;
2653364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2663364c323SKonstantin Belousov 
267afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2681ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(curproc);
2691ba5ad42SEdward Tomasz Napierala 	racct_add_force(curproc, RACCT_SWAP, incr);
2701ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(curproc);
271afcc55f3SEdward Tomasz Napierala #endif
2721ba5ad42SEdward Tomasz Napierala 
2733364c323SKonstantin Belousov 	uip = curthread->td_ucred->cr_ruidinfo;
2743364c323SKonstantin Belousov 	PROC_LOCK(curproc);
2753364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
2763364c323SKonstantin Belousov 	uip->ui_vmsize += incr;
2773364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
2783364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2793364c323SKonstantin Belousov }
2803364c323SKonstantin Belousov 
2813364c323SKonstantin Belousov void
2823364c323SKonstantin Belousov swap_release(vm_ooffset_t decr)
2833364c323SKonstantin Belousov {
284ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
2853364c323SKonstantin Belousov 
2863364c323SKonstantin Belousov 	PROC_LOCK(curproc);
287ef694c1aSEdward Tomasz Napierala 	cred = curthread->td_ucred;
288ef694c1aSEdward Tomasz Napierala 	swap_release_by_cred(decr, cred);
2893364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2903364c323SKonstantin Belousov }
2913364c323SKonstantin Belousov 
2923364c323SKonstantin Belousov void
293ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
2943364c323SKonstantin Belousov {
295ef694c1aSEdward Tomasz Napierala  	struct uidinfo *uip;
296ef694c1aSEdward Tomasz Napierala 
297ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
2983364c323SKonstantin Belousov 
2993364c323SKonstantin Belousov 	if (decr & PAGE_MASK)
3003364c323SKonstantin Belousov 		panic("swap_release: & PAGE_MASK");
3013364c323SKonstantin Belousov 
3023364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
3033364c323SKonstantin Belousov 	if (swap_reserved < decr)
3043364c323SKonstantin Belousov 		panic("swap_reserved < decr");
3053364c323SKonstantin Belousov 	swap_reserved -= decr;
3063364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
3073364c323SKonstantin Belousov 
3083364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
3093364c323SKonstantin Belousov 	if (uip->ui_vmsize < decr)
3103364c323SKonstantin Belousov 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
3113364c323SKonstantin Belousov 	uip->ui_vmsize -= decr;
3123364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
3131ba5ad42SEdward Tomasz Napierala 
3141ba5ad42SEdward Tomasz Napierala 	racct_sub_cred(cred, RACCT_SWAP, decr);
3153364c323SKonstantin Belousov }
3163364c323SKonstantin Belousov 
3171c7c3c6aSMatthew Dillon #define SWM_FREE	0x02	/* free, period			*/
3181c7c3c6aSMatthew Dillon #define SWM_POP		0x04	/* pop out			*/
31926f9a767SRodney W. Grimes 
3207dea2c2eSAlan Cox int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
3217dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
3221c7c3c6aSMatthew Dillon static int nsw_rcount;		/* free read buffers			*/
323327f4e83SMatthew Dillon static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
324327f4e83SMatthew Dillon static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
325327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
326327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
3271c7c3c6aSMatthew Dillon 
32889c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
3294c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
3304c36e917SKonstantin Belousov     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
3314c36e917SKonstantin Belousov     "Maximum running async swap ops");
33289c241d1SGleb Smirnoff 
3331c7c3c6aSMatthew Dillon static struct swblock **swhash;
3341c7c3c6aSMatthew Dillon static int swhash_mask;
3357827d9b0SAlan Cox static struct mtx swhash_mtx;
3367827d9b0SAlan Cox 
3370cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
338327f4e83SMatthew Dillon 
3391c7c3c6aSMatthew Dillon /*
3401c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
3411c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
3421c7c3c6aSMatthew Dillon  */
3431c7c3c6aSMatthew Dillon 
3441c7c3c6aSMatthew Dillon #define NOBJLISTS		8
3451c7c3c6aSMatthew Dillon 
3461c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
347af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
3481c7c3c6aSMatthew Dillon 
3491c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
350e9c0cc15SPoul-Henning Kamp static uma_zone_t	swap_zone;
3511c7c3c6aSMatthew Dillon 
3521c7c3c6aSMatthew Dillon /*
3531c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
3541c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
3551c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
3561c7c3c6aSMatthew Dillon  */
357ff98689dSBruce Evans static vm_object_t
35811caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
3593364c323SKonstantin Belousov 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
36011caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
361b0cd2017SGleb Smirnoff static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
362b0cd2017SGleb Smirnoff     int *);
363b0cd2017SGleb Smirnoff static int	swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
364b0cd2017SGleb Smirnoff     int *, pgo_getpages_iodone_t, void *);
365751221fdSPoul-Henning Kamp static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
3665ea4972cSAlan Cox static boolean_t
3675ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
36811caded3SAlfred Perlstein static void	swap_pager_init(void);
36911caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
370b3fed13eSDavid Schultz static void	swap_pager_swapoff(struct swdevt *sp);
371f708ef1bSPoul-Henning Kamp 
372df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
373e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
374e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
375e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
376e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
37790effb23SGleb Smirnoff 	.pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)		*/
378e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
379e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
380e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
381df8bae1dSRodney W. Grimes };
382df8bae1dSRodney W. Grimes 
3831c7c3c6aSMatthew Dillon /*
3841c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
3851c7c3c6aSMatthew Dillon  * internal.
3861c7c3c6aSMatthew Dillon  */
387e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
388e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
38926f9a767SRodney W. Grimes 
390*07c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
391*07c348eaSAlan Cox     "Maximum size of a swap block in pages");
392cee313c4SRobert Watson 
393a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
39411caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
39588ad2d7bSKonstantin Belousov static int	swapongeom(struct vnode *);
39659efee01SPoul-Henning Kamp static int	swaponvp(struct thread *, struct vnode *, u_long);
39735918c55SChristian S.J. Peron static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
39824a1cce3SDavid Greenman 
3991c7c3c6aSMatthew Dillon /*
4001c7c3c6aSMatthew Dillon  * Swap bitmap functions
4011c7c3c6aSMatthew Dillon  */
402a5edd34aSPoul-Henning Kamp static void	swp_pager_freeswapspace(daddr_t blk, int npages);
403a5edd34aSPoul-Henning Kamp static daddr_t	swp_pager_getswapspace(int npages);
4041c7c3c6aSMatthew Dillon 
4051c7c3c6aSMatthew Dillon /*
4061c7c3c6aSMatthew Dillon  * Metadata functions
4071c7c3c6aSMatthew Dillon  */
408a5edd34aSPoul-Henning Kamp static struct swblock **swp_pager_hash(vm_object_t object, vm_pindex_t index);
40911caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
4102e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
41111caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
41211caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
4131c7c3c6aSMatthew Dillon 
4141c7c3c6aSMatthew Dillon /*
4151c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
4161c7c3c6aSMatthew Dillon  *
41720d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
41820d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
41920d3034fSMatthew Dillon  *
42020d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
4211c7c3c6aSMatthew Dillon  *
4221c7c3c6aSMatthew Dillon  *	No restrictions on call
4231c7c3c6aSMatthew Dillon  *	This routine may not block.
4241c7c3c6aSMatthew Dillon  */
425a5edd34aSPoul-Henning Kamp static void
4262f249180SPoul-Henning Kamp swp_sizecheck(void)
4270d94caffSDavid Greenman {
42823955314SAlfred Perlstein 
4298f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
43020d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
4311af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
43220d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
4332b0d37a4SMatthew Dillon 		}
43420d3034fSMatthew Dillon 	} else {
43526f9a767SRodney W. Grimes 		swap_pager_full = 0;
4368f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
43720d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
43826f9a767SRodney W. Grimes 	}
4391c7c3c6aSMatthew Dillon }
4401c7c3c6aSMatthew Dillon 
4411c7c3c6aSMatthew Dillon /*
442da5fd145SPeter Wemm  * SWP_PAGER_HASH() -	hash swap meta data
443da5fd145SPeter Wemm  *
444a5edd34aSPoul-Henning Kamp  *	This is an helper function which hashes the swapblk given
445da5fd145SPeter Wemm  *	the object and page index.  It returns a pointer to a pointer
446da5fd145SPeter Wemm  *	to the object, or a pointer to a NULL pointer if it could not
447da5fd145SPeter Wemm  *	find a swapblk.
448da5fd145SPeter Wemm  */
449a5edd34aSPoul-Henning Kamp static struct swblock **
450da5fd145SPeter Wemm swp_pager_hash(vm_object_t object, vm_pindex_t index)
451da5fd145SPeter Wemm {
452da5fd145SPeter Wemm 	struct swblock **pswap;
453da5fd145SPeter Wemm 	struct swblock *swap;
454da5fd145SPeter Wemm 
455da5fd145SPeter Wemm 	index &= ~(vm_pindex_t)SWAP_META_MASK;
456da5fd145SPeter Wemm 	pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask];
457da5fd145SPeter Wemm 	while ((swap = *pswap) != NULL) {
458da5fd145SPeter Wemm 		if (swap->swb_object == object &&
459da5fd145SPeter Wemm 		    swap->swb_index == index
460da5fd145SPeter Wemm 		) {
461da5fd145SPeter Wemm 			break;
462da5fd145SPeter Wemm 		}
463da5fd145SPeter Wemm 		pswap = &swap->swb_hnext;
464da5fd145SPeter Wemm 	}
465da5fd145SPeter Wemm 	return (pswap);
466da5fd145SPeter Wemm }
467da5fd145SPeter Wemm 
468da5fd145SPeter Wemm /*
4691c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
4701c7c3c6aSMatthew Dillon  *
4711c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
4721c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
4731c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
4741c7c3c6aSMatthew Dillon  */
475f5a12711SPoul-Henning Kamp static void
4762f249180SPoul-Henning Kamp swap_pager_init(void)
477df8bae1dSRodney W. Grimes {
4781c7c3c6aSMatthew Dillon 	/*
4791c7c3c6aSMatthew Dillon 	 * Initialize object lists
4801c7c3c6aSMatthew Dillon 	 */
4811c7c3c6aSMatthew Dillon 	int i;
4821c7c3c6aSMatthew Dillon 
4831c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
4841c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
48520da9c2eSPoul-Henning Kamp 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
48615719273SKonstantin Belousov 	sx_init(&sw_alloc_sx, "swspsx");
48704533e1eSKonstantin Belousov 	sx_init(&swdev_syscall_lock, "swsysc");
4881c7c3c6aSMatthew Dillon }
48926f9a767SRodney W. Grimes 
490df8bae1dSRodney W. Grimes /*
4911c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
4921c7c3c6aSMatthew Dillon  *
4931c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
4941c7c3c6aSMatthew Dillon  *	its main loop.
495df8bae1dSRodney W. Grimes  */
49624a1cce3SDavid Greenman void
4972f249180SPoul-Henning Kamp swap_pager_swap_init(void)
498df8bae1dSRodney W. Grimes {
49961203277SDag-Erling Smørgrav 	unsigned long n, n2;
5000d94caffSDavid Greenman 
50126f9a767SRodney W. Grimes 	/*
5021c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
5031c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
5041c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
5051c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
5061c7c3c6aSMatthew Dillon 	 *
507327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
5081c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
5091c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
5101c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
511327f4e83SMatthew Dillon 	 *
512327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
513327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
514327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
515327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
516327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
517327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
518327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
519327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
520327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
521327f4e83SMatthew Dillon 	 * So it all works out pretty well.
52226f9a767SRodney W. Grimes 	 */
523ad3cce20SMatthew Dillon 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
524327f4e83SMatthew Dillon 
5256d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
5261c7c3c6aSMatthew Dillon 	nsw_rcount = (nswbuf + 1) / 2;
527327f4e83SMatthew Dillon 	nsw_wcount_sync = (nswbuf + 3) / 4;
528327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
529327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
5306d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
53124a1cce3SDavid Greenman 
5321c7c3c6aSMatthew Dillon 	/*
5331c7c3c6aSMatthew Dillon 	 * Initialize our zone.  Right now I'm just guessing on the number
5341c7c3c6aSMatthew Dillon 	 * we need based on the number of pages in the system.  Each swblock
53561203277SDag-Erling Smørgrav 	 * can hold 32 pages, so this is probably overkill.  This reservation
536ec61f55dSMatthew Dillon 	 * is typically limited to around 32MB by default.
5371c7c3c6aSMatthew Dillon 	 */
53844f1c916SBryan Drewery 	n = vm_cnt.v_page_count / 2;
5392f9e4e80SMatthew Dillon 	if (maxswzone && n > maxswzone / sizeof(struct swblock))
5402f9e4e80SMatthew Dillon 		n = maxswzone / sizeof(struct swblock);
54122a5e6b9SDag-Erling Smørgrav 	n2 = n;
542670d17b5SJeff Roberson 	swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL,
5437827d9b0SAlan Cox 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
544010b1ca1SDavid Schultz 	if (swap_zone == NULL)
545010b1ca1SDavid Schultz 		panic("failed to create swap_zone.");
5468355f576SJeff Roberson 	do {
547a4915c21SAttilio Rao 		if (uma_zone_reserve_kva(swap_zone, n))
54861ce6eeeSAlfred Perlstein 			break;
54961ce6eeeSAlfred Perlstein 		/*
55061ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
55161ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
55261ce6eeeSAlfred Perlstein 		 */
55361ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
55461ce6eeeSAlfred Perlstein 	} while (n > 0);
55521cd6e62SSeigo Tanimura 	if (n2 != n)
55661203277SDag-Erling Smørgrav 		printf("Swap zone entries reduced from %lu to %lu.\n", n2, n);
55761203277SDag-Erling Smørgrav 	swap_maxpages = n * SWAP_META_PAGES;
55861203277SDag-Erling Smørgrav 	swzone = n * sizeof(struct swblock);
55922a5e6b9SDag-Erling Smørgrav 	n2 = n;
56024a1cce3SDavid Greenman 
5611c7c3c6aSMatthew Dillon 	/*
5621c7c3c6aSMatthew Dillon 	 * Initialize our meta-data hash table.  The swapper does not need to
5631c7c3c6aSMatthew Dillon 	 * be quite as efficient as the VM system, so we do not use an
5641c7c3c6aSMatthew Dillon 	 * oversized hash table.
5651c7c3c6aSMatthew Dillon 	 *
5661c7c3c6aSMatthew Dillon 	 * 	n: 		size of hash table, must be power of 2
5671c7c3c6aSMatthew Dillon 	 *	swhash_mask:	hash table index mask
5681c7c3c6aSMatthew Dillon 	 */
56961ce6eeeSAlfred Perlstein 	for (n = 1; n < n2 / 8; n *= 2)
5701c7c3c6aSMatthew Dillon 		;
571a163d034SWarner Losh 	swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO);
5721c7c3c6aSMatthew Dillon 	swhash_mask = n - 1;
5737827d9b0SAlan Cox 	mtx_init(&swhash_mtx, "swap_pager swhash", NULL, MTX_DEF);
57424a1cce3SDavid Greenman }
57524a1cce3SDavid Greenman 
576eb4d6a1bSKonstantin Belousov static vm_object_t
577eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size,
578eb4d6a1bSKonstantin Belousov     vm_ooffset_t offset)
579eb4d6a1bSKonstantin Belousov {
580eb4d6a1bSKonstantin Belousov 	vm_object_t object;
581eb4d6a1bSKonstantin Belousov 
582eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
583eb4d6a1bSKonstantin Belousov 		if (!swap_reserve_by_cred(size, cred))
584eb4d6a1bSKonstantin Belousov 			return (NULL);
585eb4d6a1bSKonstantin Belousov 		crhold(cred);
586eb4d6a1bSKonstantin Belousov 	}
587eb4d6a1bSKonstantin Belousov 	object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset +
588eb4d6a1bSKonstantin Belousov 	    PAGE_MASK + size));
589eb4d6a1bSKonstantin Belousov 	object->handle = handle;
590eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
591eb4d6a1bSKonstantin Belousov 		object->cred = cred;
592eb4d6a1bSKonstantin Belousov 		object->charge = size;
593eb4d6a1bSKonstantin Belousov 	}
594eb4d6a1bSKonstantin Belousov 	object->un_pager.swp.swp_bcount = 0;
595eb4d6a1bSKonstantin Belousov 	return (object);
596eb4d6a1bSKonstantin Belousov }
597eb4d6a1bSKonstantin Belousov 
59824a1cce3SDavid Greenman /*
5991c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
6001c7c3c6aSMatthew Dillon  *			its metadata structures.
6011c7c3c6aSMatthew Dillon  *
6021c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
603eb4d6a1bSKonstantin Belousov  *	OBJT_SWAP object.
6041c7c3c6aSMatthew Dillon  *
605eb4d6a1bSKonstantin Belousov  *	This routine must ensure that no live duplicate is created for
606eb4d6a1bSKonstantin Belousov  *	the named object request, which is protected against by
607eb4d6a1bSKonstantin Belousov  *	holding the sw_alloc_sx lock in case handle != NULL.
60824a1cce3SDavid Greenman  */
609f5a12711SPoul-Henning Kamp static vm_object_t
6106cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
6113364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
61224a1cce3SDavid Greenman {
61324a1cce3SDavid Greenman 	vm_object_t object;
6142f7af3dbSAlan Cox 
615eb4d6a1bSKonstantin Belousov 	if (handle != NULL) {
6161c7c3c6aSMatthew Dillon 		/*
6171c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
6181c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
6191c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
6201c7c3c6aSMatthew Dillon 		 * of the handle.
6211c7c3c6aSMatthew Dillon 		 */
6220cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
6231c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
624b5e8f167SAlan Cox 		if (object == NULL) {
625eb4d6a1bSKonstantin Belousov 			object = swap_pager_alloc_init(handle, cred, size,
626eb4d6a1bSKonstantin Belousov 			    offset);
627eb4d6a1bSKonstantin Belousov 			if (object != NULL) {
628eb4d6a1bSKonstantin Belousov 				TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
629eb4d6a1bSKonstantin Belousov 				    object, pager_object_list);
6303364c323SKonstantin Belousov 			}
63124a1cce3SDavid Greenman 		}
6320cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
63324a1cce3SDavid Greenman 	} else {
634eb4d6a1bSKonstantin Belousov 		object = swap_pager_alloc_init(handle, cred, size, offset);
63524a1cce3SDavid Greenman 	}
63624a1cce3SDavid Greenman 	return (object);
637df8bae1dSRodney W. Grimes }
638df8bae1dSRodney W. Grimes 
63926f9a767SRodney W. Grimes /*
6401c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
6411c7c3c6aSMatthew Dillon  *
6421c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
6431c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
6441c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
6451c7c3c6aSMatthew Dillon  *	about to be destroyed.
6461c7c3c6aSMatthew Dillon  *
64715523cf7SKonstantin Belousov  *	The object must be locked.
64826f9a767SRodney W. Grimes  */
649df8bae1dSRodney W. Grimes static void
6502f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object)
65126f9a767SRodney W. Grimes {
6524dcc5c2dSMatthew Dillon 
653eb4d6a1bSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
654eb4d6a1bSKonstantin Belousov 	KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
655eb4d6a1bSKonstantin Belousov 
65626f9a767SRodney W. Grimes 	/*
6571c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
6581c7c3c6aSMatthew Dillon 	 * pageout completion.
65926f9a767SRodney W. Grimes 	 */
660bd228075SAlan Cox 	if (object->handle != NULL) {
661eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
662eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
663eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(object->handle), object,
664eb4d6a1bSKonstantin Belousov 		    pager_object_list);
665eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
666eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(object);
667bd228075SAlan Cox 	}
6681c7c3c6aSMatthew Dillon 
6691c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
6701c7c3c6aSMatthew Dillon 
6711c7c3c6aSMatthew Dillon 	/*
6721c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
6731c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
6741c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
6751c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
6761c7c3c6aSMatthew Dillon 	 */
6771c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
678e735691bSJohn Baldwin 	object->handle = NULL;
679e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
6801c7c3c6aSMatthew Dillon }
6811c7c3c6aSMatthew Dillon 
6821c7c3c6aSMatthew Dillon /************************************************************************
6831c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
6841c7c3c6aSMatthew Dillon  ************************************************************************/
6851c7c3c6aSMatthew Dillon 
6861c7c3c6aSMatthew Dillon /*
6871c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
6881c7c3c6aSMatthew Dillon  *
6891c7c3c6aSMatthew Dillon  *	Allocate swap for the requested number of pages.  The starting
6901c7c3c6aSMatthew Dillon  *	swap block number (a page index) is returned or SWAPBLK_NONE
6911c7c3c6aSMatthew Dillon  *	if the allocation failed.
6921c7c3c6aSMatthew Dillon  *
6931c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
6941c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
6951c7c3c6aSMatthew Dillon  *
69615523cf7SKonstantin Belousov  *	This routine may not sleep.
6978f60c087SPoul-Henning Kamp  *
6988f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
6991c7c3c6aSMatthew Dillon  */
700a5edd34aSPoul-Henning Kamp static daddr_t
7012f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages)
7021c7c3c6aSMatthew Dillon {
7031c7c3c6aSMatthew Dillon 	daddr_t blk;
7048f60c087SPoul-Henning Kamp 	struct swdevt *sp;
7058f60c087SPoul-Henning Kamp 	int i;
7061c7c3c6aSMatthew Dillon 
7078f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
70820da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7098f60c087SPoul-Henning Kamp 	sp = swdevhd;
7108f60c087SPoul-Henning Kamp 	for (i = 0; i < nswapdev; i++) {
7118f60c087SPoul-Henning Kamp 		if (sp == NULL)
7128f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
7138f60c087SPoul-Henning Kamp 		if (!(sp->sw_flags & SW_CLOSING)) {
7148f60c087SPoul-Henning Kamp 			blk = blist_alloc(sp->sw_blist, npages);
7158f60c087SPoul-Henning Kamp 			if (blk != SWAPBLK_NONE) {
7168f60c087SPoul-Henning Kamp 				blk += sp->sw_first;
7178f60c087SPoul-Henning Kamp 				sp->sw_used += npages;
718d05bc129SAlan Cox 				swap_pager_avail -= npages;
7198f60c087SPoul-Henning Kamp 				swp_sizecheck();
7208f60c087SPoul-Henning Kamp 				swdevhd = TAILQ_NEXT(sp, sw_list);
721d05bc129SAlan Cox 				goto done;
7228f60c087SPoul-Henning Kamp 			}
7238f60c087SPoul-Henning Kamp 		}
7248f60c087SPoul-Henning Kamp 		sp = TAILQ_NEXT(sp, sw_list);
7258f60c087SPoul-Henning Kamp 	}
7262b0d37a4SMatthew Dillon 	if (swap_pager_full != 2) {
72720da9c2eSPoul-Henning Kamp 		printf("swap_pager_getswapspace(%d): failed\n", npages);
7282b0d37a4SMatthew Dillon 		swap_pager_full = 2;
72920d3034fSMatthew Dillon 		swap_pager_almost_full = 1;
7302b0d37a4SMatthew Dillon 	}
7318f60c087SPoul-Henning Kamp 	swdevhd = NULL;
732d05bc129SAlan Cox done:
733d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
7341c7c3c6aSMatthew Dillon 	return (blk);
73526f9a767SRodney W. Grimes }
73626f9a767SRodney W. Grimes 
737b3fed13eSDavid Schultz static int
738b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp)
7398f60c087SPoul-Henning Kamp {
7408f60c087SPoul-Henning Kamp 
741b3fed13eSDavid Schultz 	return (blk >= sp->sw_first && blk < sp->sw_end);
7428f60c087SPoul-Henning Kamp }
7438f60c087SPoul-Henning Kamp 
7444b03903aSPoul-Henning Kamp static void
7454b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp)
7464b03903aSPoul-Henning Kamp {
7474b03903aSPoul-Henning Kamp 	struct swdevt *sp;
7484b03903aSPoul-Henning Kamp 
74920da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7504b03903aSPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7514b03903aSPoul-Henning Kamp 		if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) {
75220da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
7532cc718a1SKonstantin Belousov 			if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
7542cc718a1SKonstantin Belousov 			    unmapped_buf_allowed) {
7552cc718a1SKonstantin Belousov 				bp->b_data = unmapped_buf;
7562cc718a1SKonstantin Belousov 				bp->b_offset = 0;
7572cc718a1SKonstantin Belousov 			} else {
7582cc718a1SKonstantin Belousov 				pmap_qenter((vm_offset_t)bp->b_data,
7592cc718a1SKonstantin Belousov 				    &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
7602cc718a1SKonstantin Belousov 			}
7614b03903aSPoul-Henning Kamp 			sp->sw_strategy(bp, sp);
7624b03903aSPoul-Henning Kamp 			return;
7634b03903aSPoul-Henning Kamp 		}
7644b03903aSPoul-Henning Kamp 	}
76520da9c2eSPoul-Henning Kamp 	panic("Swapdev not found");
7664b03903aSPoul-Henning Kamp }
7674b03903aSPoul-Henning Kamp 
7688f60c087SPoul-Henning Kamp 
76926f9a767SRodney W. Grimes /*
7701c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
7711c7c3c6aSMatthew Dillon  *
7721c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
7731c7c3c6aSMatthew Dillon  *
77415523cf7SKonstantin Belousov  *	This routine may not sleep.
77526f9a767SRodney W. Grimes  */
776a5edd34aSPoul-Henning Kamp static void
7778f60c087SPoul-Henning Kamp swp_pager_freeswapspace(daddr_t blk, int npages)
7780d94caffSDavid Greenman {
7798f60c087SPoul-Henning Kamp 	struct swdevt *sp;
78092da00bbSMatthew Dillon 
7817645e885SAlan Cox 	mtx_lock(&sw_dev_mtx);
7827645e885SAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7837645e885SAlan Cox 		if (blk >= sp->sw_first && blk < sp->sw_end) {
78492da00bbSMatthew Dillon 			sp->sw_used -= npages;
78592da00bbSMatthew Dillon 			/*
7867645e885SAlan Cox 			 * If we are attempting to stop swapping on
7877645e885SAlan Cox 			 * this device, we don't want to mark any
7887645e885SAlan Cox 			 * blocks free lest they be reused.
78992da00bbSMatthew Dillon 			 */
7907645e885SAlan Cox 			if ((sp->sw_flags & SW_CLOSING) == 0) {
7917645e885SAlan Cox 				blist_free(sp->sw_blist, blk - sp->sw_first,
7927645e885SAlan Cox 				    npages);
7938f60c087SPoul-Henning Kamp 				swap_pager_avail += npages;
7941c7c3c6aSMatthew Dillon 				swp_sizecheck();
79526f9a767SRodney W. Grimes 			}
7967645e885SAlan Cox 			mtx_unlock(&sw_dev_mtx);
7977645e885SAlan Cox 			return;
7987645e885SAlan Cox 		}
7997645e885SAlan Cox 	}
8007645e885SAlan Cox 	panic("Swapdev not found");
8017645e885SAlan Cox }
8021c7c3c6aSMatthew Dillon 
80326f9a767SRodney W. Grimes /*
8041c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
8051c7c3c6aSMatthew Dillon  *				range within an object.
8061c7c3c6aSMatthew Dillon  *
8071c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
8081c7c3c6aSMatthew Dillon  *
8091c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
8101c7c3c6aSMatthew Dillon  *
8111c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
8121c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
8131c7c3c6aSMatthew Dillon  *	we should be ok.
814c25673ffSAttilio Rao  *
815c25673ffSAttilio Rao  *	The object must be locked.
81626f9a767SRodney W. Grimes  */
81726f9a767SRodney W. Grimes void
8182f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
81926f9a767SRodney W. Grimes {
82023955314SAlfred Perlstein 
8211c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
8224dcc5c2dSMatthew Dillon }
8234dcc5c2dSMatthew Dillon 
8244dcc5c2dSMatthew Dillon /*
8254dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
8264dcc5c2dSMatthew Dillon  *
8274dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
82856ce850bSKonstantin Belousov  *	swap blocks are not zeroed.  Any previous swap assignment is destroyed.
8294dcc5c2dSMatthew Dillon  *
8304dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
8314dcc5c2dSMatthew Dillon  */
8324dcc5c2dSMatthew Dillon int
8334dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
8344dcc5c2dSMatthew Dillon {
8354dcc5c2dSMatthew Dillon 	int n = 0;
8364dcc5c2dSMatthew Dillon 	daddr_t blk = SWAPBLK_NONE;
8374dcc5c2dSMatthew Dillon 	vm_pindex_t beg = start;	/* save start index */
8384dcc5c2dSMatthew Dillon 
83989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
8404dcc5c2dSMatthew Dillon 	while (size) {
8414dcc5c2dSMatthew Dillon 		if (n == 0) {
8424dcc5c2dSMatthew Dillon 			n = BLIST_MAX_ALLOC;
8434dcc5c2dSMatthew Dillon 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
8444dcc5c2dSMatthew Dillon 				n >>= 1;
8454dcc5c2dSMatthew Dillon 				if (n == 0) {
8464dcc5c2dSMatthew Dillon 					swp_pager_meta_free(object, beg, start - beg);
84789f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
8484dcc5c2dSMatthew Dillon 					return (-1);
8494dcc5c2dSMatthew Dillon 				}
8504dcc5c2dSMatthew Dillon 			}
8514dcc5c2dSMatthew Dillon 		}
8524dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, start, blk);
8534dcc5c2dSMatthew Dillon 		--size;
8544dcc5c2dSMatthew Dillon 		++start;
8554dcc5c2dSMatthew Dillon 		++blk;
8564dcc5c2dSMatthew Dillon 		--n;
8574dcc5c2dSMatthew Dillon 	}
8584dcc5c2dSMatthew Dillon 	swp_pager_meta_free(object, start, n);
85989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
8604dcc5c2dSMatthew Dillon 	return (0);
86126f9a767SRodney W. Grimes }
86226f9a767SRodney W. Grimes 
8630a47b48bSJohn Dyson /*
8641c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
8651c7c3c6aSMatthew Dillon  *			and destroy the source.
8661c7c3c6aSMatthew Dillon  *
8671c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
8681c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
8691c7c3c6aSMatthew Dillon  *	we keep the destination's.
8701c7c3c6aSMatthew Dillon  *
87115523cf7SKonstantin Belousov  *	This routine is allowed to sleep.  It may sleep allocating metadata
8721c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
8731c7c3c6aSMatthew Dillon  *	progress on the source.
8741c7c3c6aSMatthew Dillon  *
8751c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
8761c7c3c6aSMatthew Dillon  *
8771c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
8781c7c3c6aSMatthew Dillon  *
87915523cf7SKonstantin Belousov  *	The source and destination objects must be locked.
88015523cf7SKonstantin Belousov  *	Both object locks may temporarily be released.
88126f9a767SRodney W. Grimes  */
88226f9a767SRodney W. Grimes void
8832f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
8842f249180SPoul-Henning Kamp     vm_pindex_t offset, int destroysource)
88526f9a767SRodney W. Grimes {
886a316d390SJohn Dyson 	vm_pindex_t i;
8874dcc5c2dSMatthew Dillon 
88889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(srcobject);
88989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(dstobject);
8900cddd8f0SMatthew Dillon 
89126f9a767SRodney W. Grimes 	/*
8921c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
8931c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
89426f9a767SRodney W. Grimes 	 */
895eb4d6a1bSKonstantin Belousov 	if (destroysource && srcobject->handle != NULL) {
896eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(srcobject, 1);
897eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(srcobject);
898eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(dstobject, 1);
899eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(dstobject);
900eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
901eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
902eb4d6a1bSKonstantin Belousov 		    pager_object_list);
903eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
904eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(dstobject);
905eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(dstobject);
906eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(srcobject);
907eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(srcobject);
908bd228075SAlan Cox 	}
90926f9a767SRodney W. Grimes 
9101c7c3c6aSMatthew Dillon 	/*
9111c7c3c6aSMatthew Dillon 	 * transfer source to destination.
9121c7c3c6aSMatthew Dillon 	 */
9131c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
9141c7c3c6aSMatthew Dillon 		daddr_t dstaddr;
9151c7c3c6aSMatthew Dillon 
9161c7c3c6aSMatthew Dillon 		/*
9171c7c3c6aSMatthew Dillon 		 * Locate (without changing) the swapblk on the destination,
9181c7c3c6aSMatthew Dillon 		 * unless it is invalid in which case free it silently, or
9191c7c3c6aSMatthew Dillon 		 * if the destination is a resident page, in which case the
9201c7c3c6aSMatthew Dillon 		 * source is thrown away.
9211c7c3c6aSMatthew Dillon 		 */
9221c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
9231c7c3c6aSMatthew Dillon 
9241c7c3c6aSMatthew Dillon 		if (dstaddr == SWAPBLK_NONE) {
9251c7c3c6aSMatthew Dillon 			/*
9261c7c3c6aSMatthew Dillon 			 * Destination has no swapblk and is not resident,
9271c7c3c6aSMatthew Dillon 			 * copy source.
9281c7c3c6aSMatthew Dillon 			 */
9291c7c3c6aSMatthew Dillon 			daddr_t srcaddr;
9301c7c3c6aSMatthew Dillon 
9311c7c3c6aSMatthew Dillon 			srcaddr = swp_pager_meta_ctl(
9321c7c3c6aSMatthew Dillon 			    srcobject,
9331c7c3c6aSMatthew Dillon 			    i + offset,
9341c7c3c6aSMatthew Dillon 			    SWM_POP
9351c7c3c6aSMatthew Dillon 			);
9361c7c3c6aSMatthew Dillon 
937ee3dc7d7SAlan Cox 			if (srcaddr != SWAPBLK_NONE) {
938c7c8dd7eSAlan Cox 				/*
939c7c8dd7eSAlan Cox 				 * swp_pager_meta_build() can sleep.
940c7c8dd7eSAlan Cox 				 */
941c7c8dd7eSAlan Cox 				vm_object_pip_add(srcobject, 1);
94289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(srcobject);
943c7c8dd7eSAlan Cox 				vm_object_pip_add(dstobject, 1);
9444dcc5c2dSMatthew Dillon 				swp_pager_meta_build(dstobject, i, srcaddr);
945c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(dstobject);
94689f6b863SAttilio Rao 				VM_OBJECT_WLOCK(srcobject);
947c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(srcobject);
948ee3dc7d7SAlan Cox 			}
9491c7c3c6aSMatthew Dillon 		} else {
9501c7c3c6aSMatthew Dillon 			/*
9511c7c3c6aSMatthew Dillon 			 * Destination has valid swapblk or it is represented
9521c7c3c6aSMatthew Dillon 			 * by a resident page.  We destroy the sourceblock.
9531c7c3c6aSMatthew Dillon 			 */
9541c7c3c6aSMatthew Dillon 
9551c7c3c6aSMatthew Dillon 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
9561c7c3c6aSMatthew Dillon 		}
95726f9a767SRodney W. Grimes 	}
95826f9a767SRodney W. Grimes 
95926f9a767SRodney W. Grimes 	/*
9601c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
9611c7c3c6aSMatthew Dillon 	 *
962763df3ecSPedro F. Giffuni 	 * We have to revert the type to OBJT_DEFAULT so we do not accidentally
9631c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
96426f9a767SRodney W. Grimes 	 */
965c0877f10SJohn Dyson 	if (destroysource) {
9661c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
9671c7c3c6aSMatthew Dillon 		/*
9681c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
9691c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
970956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
9711c7c3c6aSMatthew Dillon 		 * queues.
9721c7c3c6aSMatthew Dillon 		 */
9731c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
974c0877f10SJohn Dyson 	}
97526f9a767SRodney W. Grimes }
97626f9a767SRodney W. Grimes 
977df8bae1dSRodney W. Grimes /*
9781c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
9791c7c3c6aSMatthew Dillon  *				the requested page.
9801c7c3c6aSMatthew Dillon  *
9811c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
9821c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
9831c7c3c6aSMatthew Dillon  *
9841c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
985915d1b71SMark Johnston  *	store exists before and after the requested page.
986df8bae1dSRodney W. Grimes  */
9875ea4972cSAlan Cox static boolean_t
988915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
989915d1b71SMark Johnston     int *after)
99026f9a767SRodney W. Grimes {
991915d1b71SMark Johnston 	daddr_t blk, blk0;
992915d1b71SMark Johnston 	int i;
99326f9a767SRodney W. Grimes 
994c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
995915d1b71SMark Johnston 
9961c7c3c6aSMatthew Dillon 	/*
9971c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
9981c7c3c6aSMatthew Dillon 	 */
9991c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
10004dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
10011c7c3c6aSMatthew Dillon 		if (before)
100224a1cce3SDavid Greenman 			*before = 0;
10031c7c3c6aSMatthew Dillon 		if (after)
100424a1cce3SDavid Greenman 			*after = 0;
100526f9a767SRodney W. Grimes 		return (FALSE);
100626f9a767SRodney W. Grimes 	}
100726f9a767SRodney W. Grimes 
100826f9a767SRodney W. Grimes 	/*
10091c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
1010e47ed70bSJohn Dyson 	 */
10111c7c3c6aSMatthew Dillon 	if (before != NULL) {
1012915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10131c7c3c6aSMatthew Dillon 			if (i > pindex)
10141c7c3c6aSMatthew Dillon 				break;
10151c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
10161c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
10171c7c3c6aSMatthew Dillon 				break;
1018ffc82b0aSJohn Dyson 		}
1019915d1b71SMark Johnston 		*before = i - 1;
102026f9a767SRodney W. Grimes 	}
102126f9a767SRodney W. Grimes 
102226f9a767SRodney W. Grimes 	/*
10231c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
102426f9a767SRodney W. Grimes 	 */
10251c7c3c6aSMatthew Dillon 	if (after != NULL) {
1026915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10271c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
10281c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
10291c7c3c6aSMatthew Dillon 				break;
103026f9a767SRodney W. Grimes 		}
1031915d1b71SMark Johnston 		*after = i - 1;
10321c7c3c6aSMatthew Dillon 	}
10331c7c3c6aSMatthew Dillon 	return (TRUE);
10341c7c3c6aSMatthew Dillon }
10351c7c3c6aSMatthew Dillon 
10361c7c3c6aSMatthew Dillon /*
10371c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
10381c7c3c6aSMatthew Dillon  *
10391c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
10401c7c3c6aSMatthew Dillon  *	not, from the page.
10411c7c3c6aSMatthew Dillon  *
10421c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
10431c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
10441c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
10451c7c3c6aSMatthew Dillon  *
10461c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
10471c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
10481c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
10491c7c3c6aSMatthew Dillon  *	depends on it.
10501c7c3c6aSMatthew Dillon  *
105115523cf7SKonstantin Belousov  *	This routine may not sleep.
1052c25673ffSAttilio Rao  *
1053c25673ffSAttilio Rao  *	The object containing the page must be locked.
10541c7c3c6aSMatthew Dillon  */
10551c7c3c6aSMatthew Dillon static void
10562f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m)
10571c7c3c6aSMatthew Dillon {
10582f249180SPoul-Henning Kamp 
10591c7c3c6aSMatthew Dillon 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
10601c7c3c6aSMatthew Dillon }
10611c7c3c6aSMatthew Dillon 
10621c7c3c6aSMatthew Dillon /*
1063915d1b71SMark Johnston  * swap_pager_getpages() - bring pages in from swap
10641c7c3c6aSMatthew Dillon  *
1065915d1b71SMark Johnston  *	Attempt to page in the pages in array "m" of length "count".  The caller
1066915d1b71SMark Johnston  *	may optionally specify that additional pages preceding and succeeding
1067915d1b71SMark Johnston  *	the specified range be paged in.  The number of such pages is returned
1068915d1b71SMark Johnston  *	in the "rbehind" and "rahead" parameters, and they will be in the
1069915d1b71SMark Johnston  *	inactive queue upon return.
10701c7c3c6aSMatthew Dillon  *
1071915d1b71SMark Johnston  *	The pages in "m" must be busied and will remain busied upon return.
10721c7c3c6aSMatthew Dillon  */
1073f708ef1bSPoul-Henning Kamp static int
1074b0cd2017SGleb Smirnoff swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
1075b0cd2017SGleb Smirnoff     int *rahead)
1076df8bae1dSRodney W. Grimes {
10771c7c3c6aSMatthew Dillon 	struct buf *bp;
1078915d1b71SMark Johnston 	vm_page_t mpred, msucc, p;
1079915d1b71SMark Johnston 	vm_pindex_t pindex;
10801c7c3c6aSMatthew Dillon 	daddr_t blk;
1081dd9cb6daSMark Johnston 	int i, j, maxahead, maxbehind, reqcount, shift;
10820d94caffSDavid Greenman 
1083915d1b71SMark Johnston 	reqcount = count;
10841c7c3c6aSMatthew Dillon 
108589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
10861c7c3c6aSMatthew Dillon 	bp = getpbuf(&nsw_rcount);
1087915d1b71SMark Johnston 	VM_OBJECT_WLOCK(object);
108826f9a767SRodney W. Grimes 
1089dd9cb6daSMark Johnston 	if (!swap_pager_haspage(object, m[0]->pindex, &maxbehind, &maxahead)) {
1090915d1b71SMark Johnston 		relpbuf(bp, &nsw_rcount);
1091915d1b71SMark Johnston 		return (VM_PAGER_FAIL);
1092915d1b71SMark Johnston 	}
1093915d1b71SMark Johnston 
1094915d1b71SMark Johnston 	/*
1095915d1b71SMark Johnston 	 * Clip the readahead and readbehind ranges to exclude resident pages.
1096915d1b71SMark Johnston 	 */
1097915d1b71SMark Johnston 	if (rahead != NULL) {
1098dd9cb6daSMark Johnston 		KASSERT(reqcount - 1 <= maxahead,
1099915d1b71SMark Johnston 		    ("page count %d extends beyond swap block", reqcount));
1100dd9cb6daSMark Johnston 		*rahead = imin(*rahead, maxahead - (reqcount - 1));
1101915d1b71SMark Johnston 		pindex = m[reqcount - 1]->pindex;
1102915d1b71SMark Johnston 		msucc = TAILQ_NEXT(m[reqcount - 1], listq);
1103915d1b71SMark Johnston 		if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1104915d1b71SMark Johnston 			*rahead = msucc->pindex - pindex - 1;
1105915d1b71SMark Johnston 	}
1106915d1b71SMark Johnston 	if (rbehind != NULL) {
1107dd9cb6daSMark Johnston 		*rbehind = imin(*rbehind, maxbehind);
1108915d1b71SMark Johnston 		pindex = m[0]->pindex;
1109915d1b71SMark Johnston 		mpred = TAILQ_PREV(m[0], pglist, listq);
1110915d1b71SMark Johnston 		if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1111915d1b71SMark Johnston 			*rbehind = pindex - mpred->pindex - 1;
1112915d1b71SMark Johnston 	}
1113915d1b71SMark Johnston 
1114915d1b71SMark Johnston 	/*
1115915d1b71SMark Johnston 	 * Allocate readahead and readbehind pages.
1116915d1b71SMark Johnston 	 */
1117915d1b71SMark Johnston 	shift = rbehind != NULL ? *rbehind : 0;
1118915d1b71SMark Johnston 	if (shift != 0) {
1119915d1b71SMark Johnston 		for (i = 1; i <= shift; i++) {
1120915d1b71SMark Johnston 			p = vm_page_alloc(object, m[0]->pindex - i,
11217667839aSAlan Cox 			    VM_ALLOC_NORMAL);
1122915d1b71SMark Johnston 			if (p == NULL) {
1123915d1b71SMark Johnston 				/* Shift allocated pages to the left. */
1124915d1b71SMark Johnston 				for (j = 0; j < i - 1; j++)
1125915d1b71SMark Johnston 					bp->b_pages[j] =
1126915d1b71SMark Johnston 					    bp->b_pages[j + shift - i + 1];
1127915d1b71SMark Johnston 				break;
1128915d1b71SMark Johnston 			}
1129915d1b71SMark Johnston 			bp->b_pages[shift - i] = p;
1130915d1b71SMark Johnston 		}
1131915d1b71SMark Johnston 		shift = i - 1;
1132915d1b71SMark Johnston 		*rbehind = shift;
1133915d1b71SMark Johnston 	}
1134915d1b71SMark Johnston 	for (i = 0; i < reqcount; i++)
1135915d1b71SMark Johnston 		bp->b_pages[i + shift] = m[i];
1136915d1b71SMark Johnston 	if (rahead != NULL) {
1137915d1b71SMark Johnston 		for (i = 0; i < *rahead; i++) {
1138915d1b71SMark Johnston 			p = vm_page_alloc(object,
11397667839aSAlan Cox 			    m[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1140915d1b71SMark Johnston 			if (p == NULL)
1141915d1b71SMark Johnston 				break;
1142915d1b71SMark Johnston 			bp->b_pages[shift + reqcount + i] = p;
1143915d1b71SMark Johnston 		}
1144915d1b71SMark Johnston 		*rahead = i;
1145915d1b71SMark Johnston 	}
1146915d1b71SMark Johnston 	if (rbehind != NULL)
1147915d1b71SMark Johnston 		count += *rbehind;
1148915d1b71SMark Johnston 	if (rahead != NULL)
1149915d1b71SMark Johnston 		count += *rahead;
1150915d1b71SMark Johnston 
1151915d1b71SMark Johnston 	vm_object_pip_add(object, count);
1152915d1b71SMark Johnston 
1153915d1b71SMark Johnston 	for (i = 0; i < count; i++)
1154915d1b71SMark Johnston 		bp->b_pages[i]->oflags |= VPO_SWAPINPROG;
1155915d1b71SMark Johnston 
1156915d1b71SMark Johnston 	pindex = bp->b_pages[0]->pindex;
1157915d1b71SMark Johnston 	blk = swp_pager_meta_ctl(object, pindex, 0);
1158915d1b71SMark Johnston 	KASSERT(blk != SWAPBLK_NONE,
1159915d1b71SMark Johnston 	    ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1160915d1b71SMark Johnston 
1161915d1b71SMark Johnston 	VM_OBJECT_WUNLOCK(object);
1162915d1b71SMark Johnston 
1163915d1b71SMark Johnston 	bp->b_flags |= B_PAGING;
116421144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
11651c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1166fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1167fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
1168b0cd2017SGleb Smirnoff 	bp->b_blkno = blk;
1169b0cd2017SGleb Smirnoff 	bp->b_bcount = PAGE_SIZE * count;
1170b0cd2017SGleb Smirnoff 	bp->b_bufsize = PAGE_SIZE * count;
1171b0cd2017SGleb Smirnoff 	bp->b_npages = count;
1172915d1b71SMark Johnston 	bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1173915d1b71SMark Johnston 	bp->b_pgafter = rahead != NULL ? *rahead : 0;
117426f9a767SRodney W. Grimes 
117583c9dea1SGleb Smirnoff 	VM_CNT_INC(v_swapin);
117683c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_swappgsin, count);
11771c7c3c6aSMatthew Dillon 
11781c7c3c6aSMatthew Dillon 	/*
11791c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
11801c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
11811c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
11821c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
11831c7c3c6aSMatthew Dillon 	 *
11841c7c3c6aSMatthew Dillon 	 * The other pages in our m[] array are also released on completion,
11851c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
11861c7c3c6aSMatthew Dillon 	 *
1187c37a77eeSPoul-Henning Kamp 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
11881c7c3c6aSMatthew Dillon 	 */
1189b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
11904b03903aSPoul-Henning Kamp 	swp_pager_strategy(bp);
119126f9a767SRodney W. Grimes 
119226f9a767SRodney W. Grimes 	/*
1193915d1b71SMark Johnston 	 * Wait for the pages we want to complete.  VPO_SWAPINPROG is always
11941c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1195915d1b71SMark Johnston 	 * is set in the metadata for each page in the request.
119626f9a767SRodney W. Grimes 	 */
119789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1198b0cd2017SGleb Smirnoff 	while ((m[0]->oflags & VPO_SWAPINPROG) != 0) {
1199b0cd2017SGleb Smirnoff 		m[0]->oflags |= VPO_SWAPSLEEP;
120083c9dea1SGleb Smirnoff 		VM_CNT_INC(v_intrans);
1201c7aebda8SAttilio Rao 		if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP,
1202c7aebda8SAttilio Rao 		    "swread", hz * 20)) {
12039bd86a98SBruce M Simpson 			printf(
1204c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1205c5690651SPoul-Henning Kamp 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
12061c7c3c6aSMatthew Dillon 		}
12071b119d9dSDavid Greenman 	}
120826f9a767SRodney W. Grimes 
120926f9a767SRodney W. Grimes 	/*
1210b0cd2017SGleb Smirnoff 	 * If we had an unrecoverable read error pages will not be valid.
121126f9a767SRodney W. Grimes 	 */
1212915d1b71SMark Johnston 	for (i = 0; i < reqcount; i++)
1213b0cd2017SGleb Smirnoff 		if (m[i]->valid != VM_PAGE_BITS_ALL)
12141c7c3c6aSMatthew Dillon 			return (VM_PAGER_ERROR);
1215b0cd2017SGleb Smirnoff 
12161c7c3c6aSMatthew Dillon 	return (VM_PAGER_OK);
12171c7c3c6aSMatthew Dillon 
12181c7c3c6aSMatthew Dillon 	/*
12191c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
12201c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
12211c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
12221c7c3c6aSMatthew Dillon 	 * to all-zero's later.
12231c7c3c6aSMatthew Dillon 	 */
1224df8bae1dSRodney W. Grimes }
1225df8bae1dSRodney W. Grimes 
12261c7c3c6aSMatthew Dillon /*
122790effb23SGleb Smirnoff  * 	swap_pager_getpages_async():
122890effb23SGleb Smirnoff  *
122990effb23SGleb Smirnoff  *	Right now this is emulation of asynchronous operation on top of
123090effb23SGleb Smirnoff  *	swap_pager_getpages().
123190effb23SGleb Smirnoff  */
123290effb23SGleb Smirnoff static int
123390effb23SGleb Smirnoff swap_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
1234b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
123590effb23SGleb Smirnoff {
123690effb23SGleb Smirnoff 	int r, error;
123790effb23SGleb Smirnoff 
1238b0cd2017SGleb Smirnoff 	r = swap_pager_getpages(object, m, count, rbehind, rahead);
123990effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
124090effb23SGleb Smirnoff 	switch (r) {
124190effb23SGleb Smirnoff 	case VM_PAGER_OK:
124290effb23SGleb Smirnoff 		error = 0;
124390effb23SGleb Smirnoff 		break;
124490effb23SGleb Smirnoff 	case VM_PAGER_ERROR:
124590effb23SGleb Smirnoff 		error = EIO;
124690effb23SGleb Smirnoff 		break;
124790effb23SGleb Smirnoff 	case VM_PAGER_FAIL:
124890effb23SGleb Smirnoff 		error = EINVAL;
124990effb23SGleb Smirnoff 		break;
125090effb23SGleb Smirnoff 	default:
1251d9328101SGleb Smirnoff 		panic("unhandled swap_pager_getpages() error %d", r);
125290effb23SGleb Smirnoff 	}
125390effb23SGleb Smirnoff 	(iodone)(arg, m, count, error);
125490effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
125590effb23SGleb Smirnoff 
125690effb23SGleb Smirnoff 	return (r);
125790effb23SGleb Smirnoff }
125890effb23SGleb Smirnoff 
125990effb23SGleb Smirnoff /*
12601c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
12611c7c3c6aSMatthew Dillon  *
12621c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
12631c7c3c6aSMatthew Dillon  *
12641c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
12651c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
12661c7c3c6aSMatthew Dillon  *
1267ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
12681c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
12691c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
12701c7c3c6aSMatthew Dillon  *	which needs work.
12711c7c3c6aSMatthew Dillon  *
12721c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
12731c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
12741c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
12751c7c3c6aSMatthew Dillon  *	completion.
12761c7c3c6aSMatthew Dillon  *
12771c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
12781c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
12791c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
12801c7c3c6aSMatthew Dillon  */
1281d635a37fSWarner Losh static void
12822f249180SPoul-Henning Kamp swap_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1283e065e87cSKonstantin Belousov     int flags, int *rtvals)
1284df8bae1dSRodney W. Grimes {
1285e065e87cSKonstantin Belousov 	int i, n;
1286e065e87cSKonstantin Belousov 	boolean_t sync;
1287df8bae1dSRodney W. Grimes 
12881c7c3c6aSMatthew Dillon 	if (count && m[0]->object != object) {
12897036145bSMaxim Konovalov 		panic("swap_pager_putpages: object mismatch %p/%p",
12901c7c3c6aSMatthew Dillon 		    object,
12911c7c3c6aSMatthew Dillon 		    m[0]->object
12921c7c3c6aSMatthew Dillon 		);
12931c7c3c6aSMatthew Dillon 	}
1294ee3dc7d7SAlan Cox 
12951c7c3c6aSMatthew Dillon 	/*
12961c7c3c6aSMatthew Dillon 	 * Step 1
12971c7c3c6aSMatthew Dillon 	 *
12981c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
12991c7c3c6aSMatthew Dillon 	 * check for bogus sysops
13001c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
13011c7c3c6aSMatthew Dillon 	 */
13024dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
13034dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
130489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1305e47ed70bSJohn Dyson 
1306e065e87cSKonstantin Belousov 	n = 0;
1307e47ed70bSJohn Dyson 	if (curproc != pageproc)
1308e47ed70bSJohn Dyson 		sync = TRUE;
1309e065e87cSKonstantin Belousov 	else
1310e065e87cSKonstantin Belousov 		sync = (flags & VM_PAGER_PUT_SYNC) != 0;
131126f9a767SRodney W. Grimes 
13121c7c3c6aSMatthew Dillon 	/*
13131c7c3c6aSMatthew Dillon 	 * Step 2
13141c7c3c6aSMatthew Dillon 	 *
13151c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
13161c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
13171c7c3c6aSMatthew Dillon 	 * successfully.
13181c7c3c6aSMatthew Dillon 	 */
13191c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
13201c7c3c6aSMatthew Dillon 		int j;
13211c7c3c6aSMatthew Dillon 		struct buf *bp;
1322a316d390SJohn Dyson 		daddr_t blk;
132326f9a767SRodney W. Grimes 
1324df8bae1dSRodney W. Grimes 		/*
13251c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1326df8bae1dSRodney W. Grimes 		 */
13271c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1328327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
13291c7c3c6aSMatthew Dillon 
133026f9a767SRodney W. Grimes 		/*
13311c7c3c6aSMatthew Dillon 		 * Get biggest block of swap we can.  If we fail, fall
13321c7c3c6aSMatthew Dillon 		 * back and try to allocate a smaller block.  Don't go
13331c7c3c6aSMatthew Dillon 		 * overboard trying to allocate space if it would overly
13341c7c3c6aSMatthew Dillon 		 * fragment swap.
133526f9a767SRodney W. Grimes 		 */
13361c7c3c6aSMatthew Dillon 		while (
13371c7c3c6aSMatthew Dillon 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
13381c7c3c6aSMatthew Dillon 		    n > 4
13391c7c3c6aSMatthew Dillon 		) {
13401c7c3c6aSMatthew Dillon 			n >>= 1;
134126f9a767SRodney W. Grimes 		}
13421c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
13434dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
13441c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
13451c7c3c6aSMatthew Dillon 			continue;
134626f9a767SRodney W. Grimes 		}
134726f9a767SRodney W. Grimes 
134826f9a767SRodney W. Grimes 		/*
13491c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
13501c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
135126f9a767SRodney W. Grimes 		 */
1352327f4e83SMatthew Dillon 		if (sync == TRUE) {
1353327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_sync);
1354327f4e83SMatthew Dillon 		} else {
1355327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_async);
135621144e3bSPoul-Henning Kamp 			bp->b_flags = B_ASYNC;
1357327f4e83SMatthew Dillon 		}
13585e04322aSPoul-Henning Kamp 		bp->b_flags |= B_PAGING;
1359912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
136026f9a767SRodney W. Grimes 
1361fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1362fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
13631c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
13641c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
13651c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1366e47ed70bSJohn Dyson 
136789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
13681c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
13691c7c3c6aSMatthew Dillon 			vm_page_t mreq = m[i+j];
13701c7c3c6aSMatthew Dillon 
13711c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
13721c7c3c6aSMatthew Dillon 			    mreq->object,
13731c7c3c6aSMatthew Dillon 			    mreq->pindex,
13744dcc5c2dSMatthew Dillon 			    blk + j
13751c7c3c6aSMatthew Dillon 			);
13767dbf82dcSMatthew Dillon 			vm_page_dirty(mreq);
13775786be7cSAlan Cox 			mreq->oflags |= VPO_SWAPINPROG;
13781c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
13791c7c3c6aSMatthew Dillon 		}
138089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
13811c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1382a5296b05SJulian Elischer 		/*
1383a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1384a5296b05SJulian Elischer 		 */
1385a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1386a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
13871c7c3c6aSMatthew Dillon 
138883c9dea1SGleb Smirnoff 		VM_CNT_INC(v_swapout);
138983c9dea1SGleb Smirnoff 		VM_CNT_ADD(v_swappgsout, bp->b_npages);
139026f9a767SRodney W. Grimes 
139126f9a767SRodney W. Grimes 		/*
139277923df2SAlan Cox 		 * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
139377923df2SAlan Cox 		 * can call the async completion routine at the end of a
139477923df2SAlan Cox 		 * synchronous I/O operation.  Otherwise, our caller would
139577923df2SAlan Cox 		 * perform duplicate unbusy and wakeup operations on the page
139677923df2SAlan Cox 		 * and object, respectively.
139777923df2SAlan Cox 		 */
139877923df2SAlan Cox 		for (j = 0; j < n; j++)
139977923df2SAlan Cox 			rtvals[i + j] = VM_PAGER_PEND;
140077923df2SAlan Cox 
140177923df2SAlan Cox 		/*
14021c7c3c6aSMatthew Dillon 		 * asynchronous
14031c7c3c6aSMatthew Dillon 		 *
1404c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
140526f9a767SRodney W. Grimes 		 */
14061c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
14071c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
140867812eacSKirk McKusick 			BUF_KERNPROC(bp);
14094b03903aSPoul-Henning Kamp 			swp_pager_strategy(bp);
14101c7c3c6aSMatthew Dillon 			continue;
141126f9a767SRodney W. Grimes 		}
1412e47ed70bSJohn Dyson 
141326f9a767SRodney W. Grimes 		/*
14141c7c3c6aSMatthew Dillon 		 * synchronous
14151c7c3c6aSMatthew Dillon 		 *
1416c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
14171c7c3c6aSMatthew Dillon 		 */
14182c840b1fSAlan Cox 		bp->b_iodone = bdone;
14194b03903aSPoul-Henning Kamp 		swp_pager_strategy(bp);
14201c7c3c6aSMatthew Dillon 
14211c7c3c6aSMatthew Dillon 		/*
142277923df2SAlan Cox 		 * Wait for the sync I/O to complete.
142326f9a767SRodney W. Grimes 		 */
14242c840b1fSAlan Cox 		bwait(bp, PVM, "swwrt");
142577923df2SAlan Cox 
14261c7c3c6aSMatthew Dillon 		/*
14271c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
14281c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
14291c7c3c6aSMatthew Dillon 		 */
14301c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
14311c7c3c6aSMatthew Dillon 	}
143289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
14331c7c3c6aSMatthew Dillon }
14341c7c3c6aSMatthew Dillon 
14351c7c3c6aSMatthew Dillon /*
14361c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
14371c7c3c6aSMatthew Dillon  *
14381c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
14391c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
14401c7c3c6aSMatthew Dillon  *
144115523cf7SKonstantin Belousov  *	This routine may not sleep.
14421c7c3c6aSMatthew Dillon  */
14431c7c3c6aSMatthew Dillon static void
14442f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp)
14451c7c3c6aSMatthew Dillon {
14461c7c3c6aSMatthew Dillon 	int i;
14471c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
14481c7c3c6aSMatthew Dillon 
14491c7c3c6aSMatthew Dillon 	/*
14501c7c3c6aSMatthew Dillon 	 * report error
14511c7c3c6aSMatthew Dillon 	 */
1452c244d2deSPoul-Henning Kamp 	if (bp->b_ioflags & BIO_ERROR) {
14531c7c3c6aSMatthew Dillon 		printf(
14541c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
14551c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
145621144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
14571c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
14581c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
14591c7c3c6aSMatthew Dillon 		    bp->b_error
14601c7c3c6aSMatthew Dillon 		);
14611c7c3c6aSMatthew Dillon 	}
14621c7c3c6aSMatthew Dillon 
14631c7c3c6aSMatthew Dillon 	/*
146426f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
146526f9a767SRodney W. Grimes 	 */
1466fade8dd7SJeff Roberson 	if (buf_mapped(bp))
14671c7c3c6aSMatthew Dillon 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1468fade8dd7SJeff Roberson 	else
1469fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
147026f9a767SRodney W. Grimes 
147133a609ecSAlan Cox 	if (bp->b_npages) {
147233a609ecSAlan Cox 		object = bp->b_pages[0]->object;
147389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
147433a609ecSAlan Cox 	}
14752965a453SKip Macy 
147626f9a767SRodney W. Grimes 	/*
14771c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
14781c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
14791c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
14801c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
14811c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
14821c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
148326f9a767SRodney W. Grimes 	 */
14841c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
14851c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1486e47ed70bSJohn Dyson 
14875786be7cSAlan Cox 		m->oflags &= ~VPO_SWAPINPROG;
1488c7aebda8SAttilio Rao 		if (m->oflags & VPO_SWAPSLEEP) {
1489c7aebda8SAttilio Rao 			m->oflags &= ~VPO_SWAPSLEEP;
1490c7aebda8SAttilio Rao 			wakeup(&object->paging_in_progress);
1491c7aebda8SAttilio Rao 		}
1492e47ed70bSJohn Dyson 
1493c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1494ffc82b0aSJohn Dyson 			/*
14951c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
14961c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
14971c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
14981c7c3c6aSMatthew Dillon 			 * interrupt.
1499ffc82b0aSJohn Dyson 			 */
150021144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
15011c7c3c6aSMatthew Dillon 				/*
15021c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1503956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15041c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15051c7c3c6aSMatthew Dillon 				 */
15061c7c3c6aSMatthew Dillon 				m->valid = 0;
15071c7c3c6aSMatthew Dillon 			} else {
15081c7c3c6aSMatthew Dillon 				/*
15091c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
15101c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
15111c7c3c6aSMatthew Dillon 				 * then finish the I/O.
15121c7c3c6aSMatthew Dillon 				 */
15137dbf82dcSMatthew Dillon 				vm_page_dirty(m);
15143c4a2440SAlan Cox 				vm_page_lock(m);
15151c7c3c6aSMatthew Dillon 				vm_page_activate(m);
15163c4a2440SAlan Cox 				vm_page_unlock(m);
1517c7aebda8SAttilio Rao 				vm_page_sunbusy(m);
15181c7c3c6aSMatthew Dillon 			}
151921144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
15201c7c3c6aSMatthew Dillon 			/*
15211c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1522956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
15231c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
15241c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
15251c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
15261c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
15271c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
15281c7c3c6aSMatthew Dillon 			 */
1529016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1530016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is mapped", m));
1531016a3c93SAlan Cox 			KASSERT(m->dirty == 0,
1532016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is dirty", m));
1533915d1b71SMark Johnston 
1534b0cd2017SGleb Smirnoff 			m->valid = VM_PAGE_BITS_ALL;
1535915d1b71SMark Johnston 			if (i < bp->b_pgbefore ||
1536915d1b71SMark Johnston 			    i >= bp->b_npages - bp->b_pgafter)
1537915d1b71SMark Johnston 				vm_page_readahead_finish(m);
15381c7c3c6aSMatthew Dillon 		} else {
15391c7c3c6aSMatthew Dillon 			/*
1540016a3c93SAlan Cox 			 * For write success, clear the dirty
15411c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
15421c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
1543ebcddc72SAlan Cox 			 * A page is only written to swap after a period of
1544ebcddc72SAlan Cox 			 * inactivity.  Therefore, we do not expect it to be
1545ebcddc72SAlan Cox 			 * reused.
15461c7c3c6aSMatthew Dillon 			 */
15476031c68dSAlan Cox 			KASSERT(!pmap_page_is_write_mapped(m),
1548016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is not write"
1549016a3c93SAlan Cox 			    " protected", m));
1550c52e7044SAlan Cox 			vm_page_undirty(m);
15513c4a2440SAlan Cox 			vm_page_lock(m);
1552ebcddc72SAlan Cox 			vm_page_deactivate_noreuse(m);
15532965a453SKip Macy 			vm_page_unlock(m);
1554ebcddc72SAlan Cox 			vm_page_sunbusy(m);
15553c4a2440SAlan Cox 		}
15563c4a2440SAlan Cox 	}
155726f9a767SRodney W. Grimes 
15581c7c3c6aSMatthew Dillon 	/*
15591c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
15601c7c3c6aSMatthew Dillon 	 * pip refs on the object.
15611c7c3c6aSMatthew Dillon 	 */
15620d420ad3SAlan Cox 	if (object != NULL) {
15631c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
156489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
15650d420ad3SAlan Cox 	}
156626f9a767SRodney W. Grimes 
15671c7c3c6aSMatthew Dillon 	/*
1568100650deSOlivier Houchard 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1569100650deSOlivier Houchard 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1570100650deSOlivier Houchard 	 * trigger a KASSERT in relpbuf().
1571100650deSOlivier Houchard 	 */
1572100650deSOlivier Houchard 	if (bp->b_vp) {
1573100650deSOlivier Houchard 		    bp->b_vp = NULL;
1574100650deSOlivier Houchard 		    bp->b_bufobj = NULL;
1575100650deSOlivier Houchard 	}
1576100650deSOlivier Houchard 	/*
15771c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
15781c7c3c6aSMatthew Dillon 	 */
1579327f4e83SMatthew Dillon 	relpbuf(
1580327f4e83SMatthew Dillon 	    bp,
158121144e3bSPoul-Henning Kamp 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1582327f4e83SMatthew Dillon 		((bp->b_flags & B_ASYNC) ?
1583327f4e83SMatthew Dillon 		    &nsw_wcount_async :
1584327f4e83SMatthew Dillon 		    &nsw_wcount_sync
1585327f4e83SMatthew Dillon 		)
1586327f4e83SMatthew Dillon 	    )
1587327f4e83SMatthew Dillon 	);
158826f9a767SRodney W. Grimes }
15891c7c3c6aSMatthew Dillon 
159092da00bbSMatthew Dillon /*
159192da00bbSMatthew Dillon  *	swap_pager_isswapped:
159292da00bbSMatthew Dillon  *
159392da00bbSMatthew Dillon  *	Return 1 if at least one page in the given object is paged
159492da00bbSMatthew Dillon  *	out to the given swap device.
159592da00bbSMatthew Dillon  *
159615523cf7SKonstantin Belousov  *	This routine may not sleep.
159792da00bbSMatthew Dillon  */
15988f60c087SPoul-Henning Kamp int
15998f60c087SPoul-Henning Kamp swap_pager_isswapped(vm_object_t object, struct swdevt *sp)
16008f60c087SPoul-Henning Kamp {
160192da00bbSMatthew Dillon 	daddr_t index = 0;
160292da00bbSMatthew Dillon 	int bcount;
160392da00bbSMatthew Dillon 	int i;
160492da00bbSMatthew Dillon 
160589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
1606f6bcadc4SDavid Schultz 	if (object->type != OBJT_SWAP)
1607f6bcadc4SDavid Schultz 		return (0);
1608f6bcadc4SDavid Schultz 
1609b3fed13eSDavid Schultz 	mtx_lock(&swhash_mtx);
161092da00bbSMatthew Dillon 	for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) {
161192da00bbSMatthew Dillon 		struct swblock *swap;
161292da00bbSMatthew Dillon 
161392da00bbSMatthew Dillon 		if ((swap = *swp_pager_hash(object, index)) != NULL) {
161492da00bbSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
1615b3fed13eSDavid Schultz 				if (swp_pager_isondev(swap->swb_pages[i], sp)) {
16167827d9b0SAlan Cox 					mtx_unlock(&swhash_mtx);
1617b3fed13eSDavid Schultz 					return (1);
161892da00bbSMatthew Dillon 				}
161992da00bbSMatthew Dillon 			}
16207827d9b0SAlan Cox 		}
162192da00bbSMatthew Dillon 		index += SWAP_META_PAGES;
162292da00bbSMatthew Dillon 	}
1623b3fed13eSDavid Schultz 	mtx_unlock(&swhash_mtx);
1624b3fed13eSDavid Schultz 	return (0);
162592da00bbSMatthew Dillon }
162692da00bbSMatthew Dillon 
1627b1fd102eSMark Johnston int
1628b1fd102eSMark Johnston swap_pager_nswapdev(void)
1629b1fd102eSMark Johnston {
1630b1fd102eSMark Johnston 
1631b1fd102eSMark Johnston 	return (nswapdev);
1632b1fd102eSMark Johnston }
1633b1fd102eSMark Johnston 
163492da00bbSMatthew Dillon /*
163592da00bbSMatthew Dillon  * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in
163692da00bbSMatthew Dillon  *
1637ebcddc72SAlan Cox  *	This routine dissociates the page at the given index within an object
1638ebcddc72SAlan Cox  *	from its backing store, paging it in if it does not reside in memory.
1639ebcddc72SAlan Cox  *	If the page is paged in, it is marked dirty and placed in the laundry
1640ebcddc72SAlan Cox  *	queue.  The page is marked dirty because it no longer has backing
1641ebcddc72SAlan Cox  *	store.  It is placed in the laundry queue because it has not been
1642ebcddc72SAlan Cox  *	accessed recently.  Otherwise, it would already reside in memory.
1643ebcddc72SAlan Cox  *
1644ebcddc72SAlan Cox  *	We also attempt to swap in all other pages in the swap block.
1645ebcddc72SAlan Cox  *	However, we only guarantee that the one at the specified index is
164692da00bbSMatthew Dillon  *	paged in.
164792da00bbSMatthew Dillon  *
164892da00bbSMatthew Dillon  *	XXX - The code to page the whole block in doesn't work, so we
164992da00bbSMatthew Dillon  *	      revert to the one-by-one behavior for now.  Sigh.
165092da00bbSMatthew Dillon  */
165162a59e8fSWarner Losh static inline void
1652b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex)
165392da00bbSMatthew Dillon {
165492da00bbSMatthew Dillon 	vm_page_t m;
165592da00bbSMatthew Dillon 
165692da00bbSMatthew Dillon 	vm_object_pip_add(object, 1);
16575944de8eSKonstantin Belousov 	m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
165892da00bbSMatthew Dillon 	if (m->valid == VM_PAGE_BITS_ALL) {
16590d8243ccSAttilio Rao 		vm_object_pip_wakeup(object);
166092da00bbSMatthew Dillon 		vm_page_dirty(m);
1661d061cdd5SAlan Cox 		vm_page_lock(m);
1662d061cdd5SAlan Cox 		vm_page_activate(m);
16632965a453SKip Macy 		vm_page_unlock(m);
1664c7aebda8SAttilio Rao 		vm_page_xunbusy(m);
166592da00bbSMatthew Dillon 		vm_pager_page_unswapped(m);
166692da00bbSMatthew Dillon 		return;
166792da00bbSMatthew Dillon 	}
166892da00bbSMatthew Dillon 
1669b0cd2017SGleb Smirnoff 	if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK)
167092da00bbSMatthew Dillon 		panic("swap_pager_force_pagein: read from swap failed");/*XXX*/
16710d8243ccSAttilio Rao 	vm_object_pip_wakeup(object);
167292da00bbSMatthew Dillon 	vm_page_dirty(m);
1673db1f085eSAlan Cox 	vm_page_lock(m);
1674ebcddc72SAlan Cox 	vm_page_launder(m);
16752965a453SKip Macy 	vm_page_unlock(m);
1676c7aebda8SAttilio Rao 	vm_page_xunbusy(m);
167792da00bbSMatthew Dillon 	vm_pager_page_unswapped(m);
167892da00bbSMatthew Dillon }
167992da00bbSMatthew Dillon 
168092da00bbSMatthew Dillon /*
168192da00bbSMatthew Dillon  *	swap_pager_swapoff:
168292da00bbSMatthew Dillon  *
168392da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
168492da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
168592da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
168692da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
168792da00bbSMatthew Dillon  *
168892da00bbSMatthew Dillon  *	This routine may block.
168992da00bbSMatthew Dillon  */
1690e9c0cc15SPoul-Henning Kamp static void
1691b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp)
169292da00bbSMatthew Dillon {
169392da00bbSMatthew Dillon 	struct swblock *swap;
169498150664SKonstantin Belousov 	vm_object_t locked_obj, object;
169598150664SKonstantin Belousov 	vm_pindex_t pindex;
16968bc61209SDavid Schultz 	int i, j, retries;
169792da00bbSMatthew Dillon 
169804533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
169992da00bbSMatthew Dillon 
17008bc61209SDavid Schultz 	retries = 0;
170198150664SKonstantin Belousov 	locked_obj = NULL;
170292da00bbSMatthew Dillon full_rescan:
1703b3fed13eSDavid Schultz 	mtx_lock(&swhash_mtx);
170492da00bbSMatthew Dillon 	for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */
170592da00bbSMatthew Dillon restart:
1706b3fed13eSDavid Schultz 		for (swap = swhash[i]; swap != NULL; swap = swap->swb_hnext) {
170798150664SKonstantin Belousov 			object = swap->swb_object;
170898150664SKonstantin Belousov 			pindex = swap->swb_index;
170992da00bbSMatthew Dillon 			for (j = 0; j < SWAP_META_PAGES; ++j) {
171098150664SKonstantin Belousov 				if (!swp_pager_isondev(swap->swb_pages[j], sp))
171198150664SKonstantin Belousov 					continue;
171298150664SKonstantin Belousov 				if (locked_obj != object) {
171398150664SKonstantin Belousov 					if (locked_obj != NULL)
171498150664SKonstantin Belousov 						VM_OBJECT_WUNLOCK(locked_obj);
171598150664SKonstantin Belousov 					locked_obj = object;
171689f6b863SAttilio Rao 					if (!VM_OBJECT_TRYWLOCK(object)) {
1717b3fed13eSDavid Schultz 						mtx_unlock(&swhash_mtx);
171898150664SKonstantin Belousov 						/* Depends on type-stability. */
171998150664SKonstantin Belousov 						VM_OBJECT_WLOCK(object);
172098150664SKonstantin Belousov 						mtx_lock(&swhash_mtx);
172198150664SKonstantin Belousov 						goto restart;
172298150664SKonstantin Belousov 					}
172398150664SKonstantin Belousov 				}
172498150664SKonstantin Belousov 				MPASS(locked_obj == object);
172598150664SKonstantin Belousov 				mtx_unlock(&swhash_mtx);
172698150664SKonstantin Belousov 				swp_pager_force_pagein(object, pindex + j);
1727b3fed13eSDavid Schultz 				mtx_lock(&swhash_mtx);
172892da00bbSMatthew Dillon 				goto restart;
172992da00bbSMatthew Dillon 			}
1730b3fed13eSDavid Schultz 		}
1731b3fed13eSDavid Schultz 	}
1732d536c58fSAlan Cox 	mtx_unlock(&swhash_mtx);
173398150664SKonstantin Belousov 	if (locked_obj != NULL) {
173498150664SKonstantin Belousov 		VM_OBJECT_WUNLOCK(locked_obj);
173598150664SKonstantin Belousov 		locked_obj = NULL;
173698150664SKonstantin Belousov 	}
17378bc61209SDavid Schultz 	if (sp->sw_used) {
173892da00bbSMatthew Dillon 		/*
17398bc61209SDavid Schultz 		 * Objects may be locked or paging to the device being
17408bc61209SDavid Schultz 		 * removed, so we will miss their pages and need to
17418bc61209SDavid Schultz 		 * make another pass.  We have marked this device as
17428bc61209SDavid Schultz 		 * SW_CLOSING, so the activity should finish soon.
174392da00bbSMatthew Dillon 		 */
17448bc61209SDavid Schultz 		retries++;
17458bc61209SDavid Schultz 		if (retries > 100) {
17468bc61209SDavid Schultz 			panic("swapoff: failed to locate %d swap blocks",
17478bc61209SDavid Schultz 			    sp->sw_used);
17488bc61209SDavid Schultz 		}
17494d70511aSJohn Baldwin 		pause("swpoff", hz / 20);
175092da00bbSMatthew Dillon 		goto full_rescan;
175192da00bbSMatthew Dillon 	}
1752b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapoff, sp);
175392da00bbSMatthew Dillon }
175492da00bbSMatthew Dillon 
17551c7c3c6aSMatthew Dillon /************************************************************************
17561c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
17571c7c3c6aSMatthew Dillon  ************************************************************************
17581c7c3c6aSMatthew Dillon  *
17591c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
1760cec9f109SDavid E. O'Brien  *	OBJT_SWAP object.
17611c7c3c6aSMatthew Dillon  *
17624dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
17634dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
17644dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
17651c7c3c6aSMatthew Dillon  */
17661c7c3c6aSMatthew Dillon 
17671c7c3c6aSMatthew Dillon /*
17681c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
17691c7c3c6aSMatthew Dillon  *
17701c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
17711c7c3c6aSMatthew Dillon  *	object.
17721c7c3c6aSMatthew Dillon  *
17731c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
17741c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
17751c7c3c6aSMatthew Dillon  *	assigned swapblk is freed.
17761c7c3c6aSMatthew Dillon  */
17771c7c3c6aSMatthew Dillon static void
17782f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
17792f249180SPoul-Henning Kamp {
17803ff863f1SDag-Erling Smørgrav 	static volatile int exhausted;
17811c7c3c6aSMatthew Dillon 	struct swblock *swap;
17821c7c3c6aSMatthew Dillon 	struct swblock **pswap;
178323f09d50SIan Dowse 	int idx;
17841c7c3c6aSMatthew Dillon 
178589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
17861c7c3c6aSMatthew Dillon 	/*
17871c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
17881c7c3c6aSMatthew Dillon 	 */
17891c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
17901c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
17911c7c3c6aSMatthew Dillon 		object->un_pager.swp.swp_bcount = 0;
1792eb4d6a1bSKonstantin Belousov 		KASSERT(object->handle == NULL, ("default pager with handle"));
1793bd228075SAlan Cox 	}
17941c7c3c6aSMatthew Dillon 
17951c7c3c6aSMatthew Dillon 	/*
17961c7c3c6aSMatthew Dillon 	 * Locate hash entry.  If not found create, but if we aren't adding
17974dcc5c2dSMatthew Dillon 	 * anything just return.  If we run out of space in the map we wait
17984dcc5c2dSMatthew Dillon 	 * and, since the hash table may have changed, retry.
17991c7c3c6aSMatthew Dillon 	 */
18004dcc5c2dSMatthew Dillon retry:
18017827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
180223f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
18031c7c3c6aSMatthew Dillon 
18041c7c3c6aSMatthew Dillon 	if ((swap = *pswap) == NULL) {
18051c7c3c6aSMatthew Dillon 		int i;
18061c7c3c6aSMatthew Dillon 
18071c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
18087827d9b0SAlan Cox 			goto done;
18091c7c3c6aSMatthew Dillon 
18105a3c920fSKonstantin Belousov 		swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT |
18115a3c920fSKonstantin Belousov 		    (curproc == pageproc ? M_USE_RESERVE : 0));
18124dcc5c2dSMatthew Dillon 		if (swap == NULL) {
18137827d9b0SAlan Cox 			mtx_unlock(&swhash_mtx);
181489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
18152025d69bSKonstantin Belousov 			if (uma_zone_exhausted(swap_zone)) {
1816dc1b35b5SDag-Erling Smørgrav 				if (atomic_cmpset_int(&exhausted, 0, 1))
18173ff863f1SDag-Erling Smørgrav 					printf("swap zone exhausted, "
18183ff863f1SDag-Erling Smørgrav 					    "increase kern.maxswzone\n");
18192025d69bSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
18202025d69bSKonstantin Belousov 				pause("swzonex", 10);
18212025d69bSKonstantin Belousov 			} else
18224dcc5c2dSMatthew Dillon 				VM_WAIT;
182389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
18244dcc5c2dSMatthew Dillon 			goto retry;
18254dcc5c2dSMatthew Dillon 		}
1826670d17b5SJeff Roberson 
1827dc1b35b5SDag-Erling Smørgrav 		if (atomic_cmpset_int(&exhausted, 1, 0))
18283ff863f1SDag-Erling Smørgrav 			printf("swap zone ok\n");
18293ff863f1SDag-Erling Smørgrav 
18301c7c3c6aSMatthew Dillon 		swap->swb_hnext = NULL;
18311c7c3c6aSMatthew Dillon 		swap->swb_object = object;
183223f09d50SIan Dowse 		swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK;
18331c7c3c6aSMatthew Dillon 		swap->swb_count = 0;
18341c7c3c6aSMatthew Dillon 
18351c7c3c6aSMatthew Dillon 		++object->un_pager.swp.swp_bcount;
18361c7c3c6aSMatthew Dillon 
18371c7c3c6aSMatthew Dillon 		for (i = 0; i < SWAP_META_PAGES; ++i)
18381c7c3c6aSMatthew Dillon 			swap->swb_pages[i] = SWAPBLK_NONE;
18391c7c3c6aSMatthew Dillon 	}
18401c7c3c6aSMatthew Dillon 
18411c7c3c6aSMatthew Dillon 	/*
18421c7c3c6aSMatthew Dillon 	 * Delete prior contents of metadata
18431c7c3c6aSMatthew Dillon 	 */
184423f09d50SIan Dowse 	idx = pindex & SWAP_META_MASK;
18451c7c3c6aSMatthew Dillon 
184623f09d50SIan Dowse 	if (swap->swb_pages[idx] != SWAPBLK_NONE) {
184723f09d50SIan Dowse 		swp_pager_freeswapspace(swap->swb_pages[idx], 1);
18481c7c3c6aSMatthew Dillon 		--swap->swb_count;
18491c7c3c6aSMatthew Dillon 	}
18501c7c3c6aSMatthew Dillon 
18511c7c3c6aSMatthew Dillon 	/*
18521c7c3c6aSMatthew Dillon 	 * Enter block into metadata
18531c7c3c6aSMatthew Dillon 	 */
185423f09d50SIan Dowse 	swap->swb_pages[idx] = swapblk;
18554dcc5c2dSMatthew Dillon 	if (swapblk != SWAPBLK_NONE)
18561c7c3c6aSMatthew Dillon 		++swap->swb_count;
18577827d9b0SAlan Cox done:
18587827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
18591c7c3c6aSMatthew Dillon }
18601c7c3c6aSMatthew Dillon 
18611c7c3c6aSMatthew Dillon /*
18621c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
18631c7c3c6aSMatthew Dillon  *
18641c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
18651c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
18661c7c3c6aSMatthew Dillon  *
18671c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
18681c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
18691c7c3c6aSMatthew Dillon  *	with resident pages.
18701c7c3c6aSMatthew Dillon  */
18711c7c3c6aSMatthew Dillon static void
18722e56b64fSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t index, vm_pindex_t count)
18731c7c3c6aSMatthew Dillon {
18742e56b64fSKonstantin Belousov 	struct swblock **pswap, *swap;
18752e56b64fSKonstantin Belousov 	vm_pindex_t c;
18762e56b64fSKonstantin Belousov 	daddr_t v;
18772e56b64fSKonstantin Belousov 	int n, sidx;
18782928cef7SAlan Cox 
1879c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
18802e56b64fSKonstantin Belousov 	if (object->type != OBJT_SWAP || count == 0)
18811c7c3c6aSMatthew Dillon 		return;
18821c7c3c6aSMatthew Dillon 
18837827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
18842e56b64fSKonstantin Belousov 	for (c = 0; c < count;) {
18851c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
18862e56b64fSKonstantin Belousov 		sidx = index & SWAP_META_MASK;
18872e56b64fSKonstantin Belousov 		n = SWAP_META_PAGES - sidx;
18882e56b64fSKonstantin Belousov 		index += n;
18892e56b64fSKonstantin Belousov 		if ((swap = *pswap) == NULL) {
18902e56b64fSKonstantin Belousov 			c += n;
18912e56b64fSKonstantin Belousov 			continue;
18922e56b64fSKonstantin Belousov 		}
18932e56b64fSKonstantin Belousov 		for (; c < count && sidx < SWAP_META_PAGES; ++c, ++sidx) {
18942e56b64fSKonstantin Belousov 			if ((v = swap->swb_pages[sidx]) == SWAPBLK_NONE)
18952e56b64fSKonstantin Belousov 				continue;
18961c7c3c6aSMatthew Dillon 			swp_pager_freeswapspace(v, 1);
18972e56b64fSKonstantin Belousov 			swap->swb_pages[sidx] = SWAPBLK_NONE;
18981c7c3c6aSMatthew Dillon 			if (--swap->swb_count == 0) {
18991c7c3c6aSMatthew Dillon 				*pswap = swap->swb_hnext;
1900670d17b5SJeff Roberson 				uma_zfree(swap_zone, swap);
19011c7c3c6aSMatthew Dillon 				--object->un_pager.swp.swp_bcount;
19022e56b64fSKonstantin Belousov 				c += SWAP_META_PAGES - sidx;
19032e56b64fSKonstantin Belousov 				break;
19041c7c3c6aSMatthew Dillon 			}
19051c7c3c6aSMatthew Dillon 		}
19061c7c3c6aSMatthew Dillon 	}
19077827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
19081c7c3c6aSMatthew Dillon }
19091c7c3c6aSMatthew Dillon 
19101c7c3c6aSMatthew Dillon /*
19111c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
19121c7c3c6aSMatthew Dillon  *
19131c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
19141c7c3c6aSMatthew Dillon  *	an object.
19151c7c3c6aSMatthew Dillon  */
19161c7c3c6aSMatthew Dillon static void
19171c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
19181c7c3c6aSMatthew Dillon {
191971057cd2SKonstantin Belousov 	struct swblock **pswap, *swap;
192071057cd2SKonstantin Belousov 	vm_pindex_t index;
192171057cd2SKonstantin Belousov 	daddr_t v;
192271057cd2SKonstantin Belousov 	int i;
19231c7c3c6aSMatthew Dillon 
192489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
19251c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19261c7c3c6aSMatthew Dillon 		return;
19271c7c3c6aSMatthew Dillon 
192871057cd2SKonstantin Belousov 	index = 0;
192971057cd2SKonstantin Belousov 	while (object->un_pager.swp.swp_bcount != 0) {
19307827d9b0SAlan Cox 		mtx_lock(&swhash_mtx);
19311c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
19321c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
19331c7c3c6aSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
193471057cd2SKonstantin Belousov 				v = swap->swb_pages[i];
19351c7c3c6aSMatthew Dillon 				if (v != SWAPBLK_NONE) {
19361c7c3c6aSMatthew Dillon 					--swap->swb_count;
19374dcc5c2dSMatthew Dillon 					swp_pager_freeswapspace(v, 1);
19381c7c3c6aSMatthew Dillon 				}
19391c7c3c6aSMatthew Dillon 			}
19401c7c3c6aSMatthew Dillon 			if (swap->swb_count != 0)
194171057cd2SKonstantin Belousov 				panic(
194271057cd2SKonstantin Belousov 				    "swap_pager_meta_free_all: swb_count != 0");
19431c7c3c6aSMatthew Dillon 			*pswap = swap->swb_hnext;
1944670d17b5SJeff Roberson 			uma_zfree(swap_zone, swap);
19451c7c3c6aSMatthew Dillon 			--object->un_pager.swp.swp_bcount;
19461c7c3c6aSMatthew Dillon 		}
19477827d9b0SAlan Cox 		mtx_unlock(&swhash_mtx);
19481c7c3c6aSMatthew Dillon 		index += SWAP_META_PAGES;
19491c7c3c6aSMatthew Dillon 	}
19501c7c3c6aSMatthew Dillon }
19511c7c3c6aSMatthew Dillon 
19521c7c3c6aSMatthew Dillon /*
19531c7c3c6aSMatthew Dillon  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
19541c7c3c6aSMatthew Dillon  *
19551c7c3c6aSMatthew Dillon  *	This routine is capable of looking up, popping, or freeing
19561c7c3c6aSMatthew Dillon  *	swapblk assignments in the swap meta data or in the vm_page_t.
19571c7c3c6aSMatthew Dillon  *	The routine typically returns the swapblk being looked-up, or popped,
19581c7c3c6aSMatthew Dillon  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
19591c7c3c6aSMatthew Dillon  *	was invalid.  This routine will automatically free any invalid
19601c7c3c6aSMatthew Dillon  *	meta-data swapblks.
19611c7c3c6aSMatthew Dillon  *
19621c7c3c6aSMatthew Dillon  *	It is not possible to store invalid swapblks in the swap meta data
19631c7c3c6aSMatthew Dillon  *	(other then a literal 'SWAPBLK_NONE'), so we don't bother checking.
19641c7c3c6aSMatthew Dillon  *
19651c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
19661c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
19671c7c3c6aSMatthew Dillon  *	busy page.
19681c7c3c6aSMatthew Dillon  *
19694dcc5c2dSMatthew Dillon  *	SWM_FREE	remove and free swap block from metadata
19701c7c3c6aSMatthew Dillon  *	SWM_POP		remove from meta data but do not free.. pop it out
19711c7c3c6aSMatthew Dillon  */
19721c7c3c6aSMatthew Dillon static daddr_t
19732f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags)
19742f249180SPoul-Henning Kamp {
19754dcc5c2dSMatthew Dillon 	struct swblock **pswap;
19764dcc5c2dSMatthew Dillon 	struct swblock *swap;
19774dcc5c2dSMatthew Dillon 	daddr_t r1;
197823f09d50SIan Dowse 	int idx;
19794dcc5c2dSMatthew Dillon 
1980c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
19811c7c3c6aSMatthew Dillon 	/*
19821c7c3c6aSMatthew Dillon 	 * The meta data only exists of the object is OBJT_SWAP
19831c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
19841c7c3c6aSMatthew Dillon 	 */
19854dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
19861c7c3c6aSMatthew Dillon 		return (SWAPBLK_NONE);
19871c7c3c6aSMatthew Dillon 
19884dcc5c2dSMatthew Dillon 	r1 = SWAPBLK_NONE;
19897827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
199023f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
19911c7c3c6aSMatthew Dillon 
19921c7c3c6aSMatthew Dillon 	if ((swap = *pswap) != NULL) {
199323f09d50SIan Dowse 		idx = pindex & SWAP_META_MASK;
199423f09d50SIan Dowse 		r1 = swap->swb_pages[idx];
19951c7c3c6aSMatthew Dillon 
19961c7c3c6aSMatthew Dillon 		if (r1 != SWAPBLK_NONE) {
19971c7c3c6aSMatthew Dillon 			if (flags & SWM_FREE) {
19984dcc5c2dSMatthew Dillon 				swp_pager_freeswapspace(r1, 1);
19991c7c3c6aSMatthew Dillon 				r1 = SWAPBLK_NONE;
20001c7c3c6aSMatthew Dillon 			}
20011c7c3c6aSMatthew Dillon 			if (flags & (SWM_FREE|SWM_POP)) {
200223f09d50SIan Dowse 				swap->swb_pages[idx] = SWAPBLK_NONE;
20031c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
20041c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
2005670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
20061c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
20071c7c3c6aSMatthew Dillon 				}
20081c7c3c6aSMatthew Dillon 			}
20091c7c3c6aSMatthew Dillon 		}
20101c7c3c6aSMatthew Dillon 	}
20117827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
20121c7c3c6aSMatthew Dillon 	return (r1);
20131c7c3c6aSMatthew Dillon }
20141c7c3c6aSMatthew Dillon 
2015e9c0cc15SPoul-Henning Kamp /*
201677d6fd97SKonstantin Belousov  * Returns the least page index which is greater than or equal to the
201777d6fd97SKonstantin Belousov  * parameter pindex and for which there is a swap block allocated.
201877d6fd97SKonstantin Belousov  * Returns object's size if the object's type is not swap or if there
201977d6fd97SKonstantin Belousov  * are no allocated swap blocks for the object after the requested
202077d6fd97SKonstantin Belousov  * pindex.
202177d6fd97SKonstantin Belousov  */
202277d6fd97SKonstantin Belousov vm_pindex_t
202377d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
202477d6fd97SKonstantin Belousov {
202577d6fd97SKonstantin Belousov 	struct swblock **pswap, *swap;
202677d6fd97SKonstantin Belousov 	vm_pindex_t i, j, lim;
202777d6fd97SKonstantin Belousov 	int idx;
202877d6fd97SKonstantin Belousov 
202977d6fd97SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(object);
203077d6fd97SKonstantin Belousov 	if (object->type != OBJT_SWAP || object->un_pager.swp.swp_bcount == 0)
203177d6fd97SKonstantin Belousov 		return (object->size);
203277d6fd97SKonstantin Belousov 
203377d6fd97SKonstantin Belousov 	mtx_lock(&swhash_mtx);
203477d6fd97SKonstantin Belousov 	for (j = pindex; j < object->size; j = lim) {
203577d6fd97SKonstantin Belousov 		pswap = swp_pager_hash(object, j);
203677d6fd97SKonstantin Belousov 		lim = rounddown2(j + SWAP_META_PAGES, SWAP_META_PAGES);
203777d6fd97SKonstantin Belousov 		if (lim > object->size)
203877d6fd97SKonstantin Belousov 			lim = object->size;
203977d6fd97SKonstantin Belousov 		if ((swap = *pswap) != NULL) {
204077d6fd97SKonstantin Belousov 			for (idx = j & SWAP_META_MASK, i = j; i < lim;
204177d6fd97SKonstantin Belousov 			    i++, idx++) {
204277d6fd97SKonstantin Belousov 				if (swap->swb_pages[idx] != SWAPBLK_NONE)
204377d6fd97SKonstantin Belousov 					goto found;
204477d6fd97SKonstantin Belousov 			}
204577d6fd97SKonstantin Belousov 		}
204677d6fd97SKonstantin Belousov 	}
204777d6fd97SKonstantin Belousov 	i = object->size;
204877d6fd97SKonstantin Belousov found:
204977d6fd97SKonstantin Belousov 	mtx_unlock(&swhash_mtx);
205077d6fd97SKonstantin Belousov 	return (i);
205177d6fd97SKonstantin Belousov }
205277d6fd97SKonstantin Belousov 
205377d6fd97SKonstantin Belousov /*
2054e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2055e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2056e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2057e9c0cc15SPoul-Henning Kamp  */
2058e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2059e9c0cc15SPoul-Henning Kamp struct swapon_args {
2060e9c0cc15SPoul-Henning Kamp 	char *name;
2061e9c0cc15SPoul-Henning Kamp };
2062e9c0cc15SPoul-Henning Kamp #endif
2063e9c0cc15SPoul-Henning Kamp 
2064e9c0cc15SPoul-Henning Kamp /*
2065e9c0cc15SPoul-Henning Kamp  * MPSAFE
2066e9c0cc15SPoul-Henning Kamp  */
2067e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2068e9c0cc15SPoul-Henning Kamp int
20698451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap)
2070e9c0cc15SPoul-Henning Kamp {
2071e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2072e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2073e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2074e9c0cc15SPoul-Henning Kamp 	int error;
2075e9c0cc15SPoul-Henning Kamp 
2076acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPON);
2077e9c0cc15SPoul-Henning Kamp 	if (error)
2078acd3428bSRobert Watson 		return (error);
2079e9c0cc15SPoul-Henning Kamp 
208004533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2081e9c0cc15SPoul-Henning Kamp 
2082e9c0cc15SPoul-Henning Kamp 	/*
2083e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2084e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2085e9c0cc15SPoul-Henning Kamp 	 */
2086e9c0cc15SPoul-Henning Kamp 	if (swap_zone == NULL) {
2087e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2088e9c0cc15SPoul-Henning Kamp 		goto done;
2089e9c0cc15SPoul-Henning Kamp 	}
2090e9c0cc15SPoul-Henning Kamp 
2091d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2092d9135e72SRobert Watson 	    uap->name, td);
2093e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2094e9c0cc15SPoul-Henning Kamp 	if (error)
2095e9c0cc15SPoul-Henning Kamp 		goto done;
2096e9c0cc15SPoul-Henning Kamp 
2097e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2098e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2099e9c0cc15SPoul-Henning Kamp 
210020da9c2eSPoul-Henning Kamp 	if (vn_isdisk(vp, &error)) {
210188ad2d7bSKonstantin Belousov 		error = swapongeom(vp);
210220da9c2eSPoul-Henning Kamp 	} else if (vp->v_type == VREG &&
2103e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
21040359a12eSAttilio Rao 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2105e9c0cc15SPoul-Henning Kamp 		/*
2106e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2107e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2108e9c0cc15SPoul-Henning Kamp 		 */
210959efee01SPoul-Henning Kamp 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2110e9c0cc15SPoul-Henning Kamp 	}
2111e9c0cc15SPoul-Henning Kamp 
2112e9c0cc15SPoul-Henning Kamp 	if (error)
2113e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2114e9c0cc15SPoul-Henning Kamp done:
211504533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2116e9c0cc15SPoul-Henning Kamp 	return (error);
2117e9c0cc15SPoul-Henning Kamp }
2118e9c0cc15SPoul-Henning Kamp 
21193ff863f1SDag-Erling Smørgrav /*
21203ff863f1SDag-Erling Smørgrav  * Check that the total amount of swap currently configured does not
21213ff863f1SDag-Erling Smørgrav  * exceed half the theoretical maximum.  If it does, print a warning
21223ff863f1SDag-Erling Smørgrav  * message and return -1; otherwise, return 0.
21233ff863f1SDag-Erling Smørgrav  */
21243ff863f1SDag-Erling Smørgrav static int
21253ff863f1SDag-Erling Smørgrav swapon_check_swzone(unsigned long npages)
21263ff863f1SDag-Erling Smørgrav {
21273ff863f1SDag-Erling Smørgrav 	unsigned long maxpages;
21283ff863f1SDag-Erling Smørgrav 
21293ff863f1SDag-Erling Smørgrav 	/* absolute maximum we can handle assuming 100% efficiency */
21303ff863f1SDag-Erling Smørgrav 	maxpages = uma_zone_get_max(swap_zone) * SWAP_META_PAGES;
21313ff863f1SDag-Erling Smørgrav 
21323ff863f1SDag-Erling Smørgrav 	/* recommend using no more than half that amount */
21333ff863f1SDag-Erling Smørgrav 	if (npages > maxpages / 2) {
21343ff863f1SDag-Erling Smørgrav 		printf("warning: total configured swap (%lu pages) "
21353ff863f1SDag-Erling Smørgrav 		    "exceeds maximum recommended amount (%lu pages).\n",
21369462305cSSergey Kandaurov 		    npages, maxpages / 2);
21373ff863f1SDag-Erling Smørgrav 		printf("warning: increase kern.maxswzone "
21383ff863f1SDag-Erling Smørgrav 		    "or reduce amount of swap.\n");
21393ff863f1SDag-Erling Smørgrav 		return (-1);
21403ff863f1SDag-Erling Smørgrav 	}
21413ff863f1SDag-Erling Smørgrav 	return (0);
21423ff863f1SDag-Erling Smørgrav }
21433ff863f1SDag-Erling Smørgrav 
214459efee01SPoul-Henning Kamp static void
21452cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks,
21462cc718a1SKonstantin Belousov     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2147e9c0cc15SPoul-Henning Kamp {
21482d9974c1SAlan Cox 	struct swdevt *sp, *tsp;
2149e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
21508f60c087SPoul-Henning Kamp 	u_long mblocks;
2151e9c0cc15SPoul-Henning Kamp 
2152e9c0cc15SPoul-Henning Kamp 	/*
2153e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2154e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2155e9c0cc15SPoul-Henning Kamp 	 *
2156e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2157e9c0cc15SPoul-Henning Kamp 	 */
2158e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2159e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2160e9c0cc15SPoul-Henning Kamp 
21616e903bd0SKonstantin Belousov 	/*
21626e903bd0SKonstantin Belousov 	 * If we go beyond this, we get overflows in the radix
21636e903bd0SKonstantin Belousov 	 * tree bitmap code.
21646e903bd0SKonstantin Belousov 	 */
21656e903bd0SKonstantin Belousov 	mblocks = 0x40000000 / BLIST_META_RADIX;
21666e903bd0SKonstantin Belousov 	if (nblks > mblocks) {
21676e903bd0SKonstantin Belousov 		printf(
21686e903bd0SKonstantin Belousov     "WARNING: reducing swap size to maximum of %luMB per unit\n",
21696e903bd0SKonstantin Belousov 		    mblocks / 1024 / 1024 * PAGE_SIZE);
21706e903bd0SKonstantin Belousov 		nblks = mblocks;
21716e903bd0SKonstantin Belousov 	}
21726e903bd0SKonstantin Belousov 
21738f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2174dee34ca4SPoul-Henning Kamp 	sp->sw_vp = vp;
2175dee34ca4SPoul-Henning Kamp 	sp->sw_id = id;
2176f3732fd1SPoul-Henning Kamp 	sp->sw_dev = dev;
21778d677ef9SPoul-Henning Kamp 	sp->sw_flags = 0;
2178e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2179e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
218059efee01SPoul-Henning Kamp 	sp->sw_strategy = strategy;
2181dee34ca4SPoul-Henning Kamp 	sp->sw_close = close;
21822cc718a1SKonstantin Belousov 	sp->sw_flags = flags;
2183e9c0cc15SPoul-Henning Kamp 
2184c8c7ad92SKip Macy 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2185e9c0cc15SPoul-Henning Kamp 	/*
2186ef3c5abdSPoul-Henning Kamp 	 * Do not free the first two block in order to avoid overwriting
21878f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2188e9c0cc15SPoul-Henning Kamp 	 */
2189ef3c5abdSPoul-Henning Kamp 	blist_free(sp->sw_blist, 2, nblks - 2);
2190e9c0cc15SPoul-Henning Kamp 
21912d9974c1SAlan Cox 	dvbase = 0;
219220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
21932d9974c1SAlan Cox 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
21942d9974c1SAlan Cox 		if (tsp->sw_end >= dvbase) {
21952d9974c1SAlan Cox 			/*
21962d9974c1SAlan Cox 			 * We put one uncovered page between the devices
21972d9974c1SAlan Cox 			 * in order to definitively prevent any cross-device
21982d9974c1SAlan Cox 			 * I/O requests
21992d9974c1SAlan Cox 			 */
22002d9974c1SAlan Cox 			dvbase = tsp->sw_end + 1;
22012d9974c1SAlan Cox 		}
22022d9974c1SAlan Cox 	}
22032d9974c1SAlan Cox 	sp->sw_first = dvbase;
22042d9974c1SAlan Cox 	sp->sw_end = dvbase + nblks;
22058f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
22068f60c087SPoul-Henning Kamp 	nswapdev++;
22078f60c087SPoul-Henning Kamp 	swap_pager_avail += nblks;
22083364c323SKonstantin Belousov 	swap_total += (vm_ooffset_t)nblks * PAGE_SIZE;
22093ff863f1SDag-Erling Smørgrav 	swapon_check_swzone(swap_total / PAGE_SIZE);
2210d05bc129SAlan Cox 	swp_sizecheck();
2211d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2212b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapon, sp);
221359efee01SPoul-Henning Kamp }
2214e9c0cc15SPoul-Henning Kamp 
2215e9c0cc15SPoul-Henning Kamp /*
2216e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2217e9c0cc15SPoul-Henning Kamp  *
2218e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2219dee34ca4SPoul-Henning Kamp  *
2220dee34ca4SPoul-Henning Kamp  * XXX: Badly designed system call: it should use a device index
2221dee34ca4SPoul-Henning Kamp  * rather than filename as specification.  We keep sw_vp around
2222dee34ca4SPoul-Henning Kamp  * only to make this work.
2223e9c0cc15SPoul-Henning Kamp  */
2224e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2225e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2226e9c0cc15SPoul-Henning Kamp 	char *name;
2227e9c0cc15SPoul-Henning Kamp };
2228e9c0cc15SPoul-Henning Kamp #endif
2229e9c0cc15SPoul-Henning Kamp 
2230e9c0cc15SPoul-Henning Kamp /*
2231e9c0cc15SPoul-Henning Kamp  * MPSAFE
2232e9c0cc15SPoul-Henning Kamp  */
2233e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2234e9c0cc15SPoul-Henning Kamp int
22358451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap)
2236e9c0cc15SPoul-Henning Kamp {
2237e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2238e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2239e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
22408f60c087SPoul-Henning Kamp 	int error;
2241e9c0cc15SPoul-Henning Kamp 
2242acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPOFF);
2243e9c0cc15SPoul-Henning Kamp 	if (error)
22440909f38aSPawel Jakub Dawidek 		return (error);
2245e9c0cc15SPoul-Henning Kamp 
224604533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2247e9c0cc15SPoul-Henning Kamp 
2248d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2249d9135e72SRobert Watson 	    td);
2250e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2251e9c0cc15SPoul-Henning Kamp 	if (error)
2252e9c0cc15SPoul-Henning Kamp 		goto done;
2253e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2254e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2255e9c0cc15SPoul-Henning Kamp 
225620da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
22578f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2258dee34ca4SPoul-Henning Kamp 		if (sp->sw_vp == vp)
22590909f38aSPawel Jakub Dawidek 			break;
2260e9c0cc15SPoul-Henning Kamp 	}
226120da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
22620909f38aSPawel Jakub Dawidek 	if (sp == NULL) {
2263e9c0cc15SPoul-Henning Kamp 		error = EINVAL;
2264e9c0cc15SPoul-Henning Kamp 		goto done;
22650909f38aSPawel Jakub Dawidek 	}
226635918c55SChristian S.J. Peron 	error = swapoff_one(sp, td->td_ucred);
22670909f38aSPawel Jakub Dawidek done:
226804533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
22690909f38aSPawel Jakub Dawidek 	return (error);
22700909f38aSPawel Jakub Dawidek }
22710909f38aSPawel Jakub Dawidek 
22720909f38aSPawel Jakub Dawidek static int
227335918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred)
22740909f38aSPawel Jakub Dawidek {
22750909f38aSPawel Jakub Dawidek 	u_long nblks, dvbase;
2276e9c0cc15SPoul-Henning Kamp #ifdef MAC
22770909f38aSPawel Jakub Dawidek 	int error;
2278e9c0cc15SPoul-Henning Kamp #endif
2279e9c0cc15SPoul-Henning Kamp 
228004533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
22810909f38aSPawel Jakub Dawidek #ifdef MAC
2282cb05b60aSAttilio Rao 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
228335918c55SChristian S.J. Peron 	error = mac_system_check_swapoff(cred, sp->sw_vp);
228422db15c0SAttilio Rao 	(void) VOP_UNLOCK(sp->sw_vp, 0);
22850909f38aSPawel Jakub Dawidek 	if (error != 0)
22860909f38aSPawel Jakub Dawidek 		return (error);
22870909f38aSPawel Jakub Dawidek #endif
2288e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2289e9c0cc15SPoul-Henning Kamp 
2290e9c0cc15SPoul-Henning Kamp 	/*
2291e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2292e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2293e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2294e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2295e9c0cc15SPoul-Henning Kamp 	 */
2296bba39b9aSAlan Cox 	if (vm_cnt.v_free_count + swap_pager_avail < nblks + nswap_lowat)
22970909f38aSPawel Jakub Dawidek 		return (ENOMEM);
2298e9c0cc15SPoul-Henning Kamp 
2299e9c0cc15SPoul-Henning Kamp 	/*
2300e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2301e9c0cc15SPoul-Henning Kamp 	 */
23022928cef7SAlan Cox 	mtx_lock(&sw_dev_mtx);
2303e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
2304fe71561aSAlan Cox 	for (dvbase = 0; dvbase < nblks; dvbase += BLIST_BMAP_RADIX) {
2305fe71561aSAlan Cox 		/*
2306fe71561aSAlan Cox 		 * blist_fill() cannot allocate more than BLIST_BMAP_RADIX
2307fe71561aSAlan Cox 		 * blocks per call.
2308fe71561aSAlan Cox 		 */
23098f60c087SPoul-Henning Kamp 		swap_pager_avail -= blist_fill(sp->sw_blist,
2310fe71561aSAlan Cox 		    dvbase, ulmin(nblks - dvbase, BLIST_BMAP_RADIX));
2311e9c0cc15SPoul-Henning Kamp 	}
23123364c323SKonstantin Belousov 	swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE;
23132928cef7SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2314e9c0cc15SPoul-Henning Kamp 
2315e9c0cc15SPoul-Henning Kamp 	/*
2316e9c0cc15SPoul-Henning Kamp 	 * Page in the contents of the device and close it.
2317e9c0cc15SPoul-Henning Kamp 	 */
2318b3fed13eSDavid Schultz 	swap_pager_swapoff(sp);
2319e9c0cc15SPoul-Henning Kamp 
232035918c55SChristian S.J. Peron 	sp->sw_close(curthread, sp);
232120da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23229e3e3fe5SWarner Losh 	sp->sw_id = NULL;
23238f60c087SPoul-Henning Kamp 	TAILQ_REMOVE(&swtailq, sp, sw_list);
23240676a140SAlan Cox 	nswapdev--;
23257dea2c2eSAlan Cox 	if (nswapdev == 0) {
23267dea2c2eSAlan Cox 		swap_pager_full = 2;
23277dea2c2eSAlan Cox 		swap_pager_almost_full = 1;
23287dea2c2eSAlan Cox 	}
23298f60c087SPoul-Henning Kamp 	if (swdevhd == sp)
23308f60c087SPoul-Henning Kamp 		swdevhd = NULL;
2331d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
23328f60c087SPoul-Henning Kamp 	blist_destroy(sp->sw_blist);
23338f60c087SPoul-Henning Kamp 	free(sp, M_VMPGDATA);
23340909f38aSPawel Jakub Dawidek 	return (0);
23350909f38aSPawel Jakub Dawidek }
2336e9c0cc15SPoul-Henning Kamp 
23370909f38aSPawel Jakub Dawidek void
23380909f38aSPawel Jakub Dawidek swapoff_all(void)
23390909f38aSPawel Jakub Dawidek {
23400909f38aSPawel Jakub Dawidek 	struct swdevt *sp, *spt;
23410909f38aSPawel Jakub Dawidek 	const char *devname;
23420909f38aSPawel Jakub Dawidek 	int error;
23430909f38aSPawel Jakub Dawidek 
234404533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
23450909f38aSPawel Jakub Dawidek 
23460909f38aSPawel Jakub Dawidek 	mtx_lock(&sw_dev_mtx);
23470909f38aSPawel Jakub Dawidek 	TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
23480909f38aSPawel Jakub Dawidek 		mtx_unlock(&sw_dev_mtx);
23490909f38aSPawel Jakub Dawidek 		if (vn_isdisk(sp->sw_vp, NULL))
23507870adb6SEd Schouten 			devname = devtoname(sp->sw_vp->v_rdev);
23510909f38aSPawel Jakub Dawidek 		else
23520909f38aSPawel Jakub Dawidek 			devname = "[file]";
235335918c55SChristian S.J. Peron 		error = swapoff_one(sp, thread0.td_ucred);
23540909f38aSPawel Jakub Dawidek 		if (error != 0) {
23550909f38aSPawel Jakub Dawidek 			printf("Cannot remove swap device %s (error=%d), "
23560909f38aSPawel Jakub Dawidek 			    "skipping.\n", devname, error);
23570909f38aSPawel Jakub Dawidek 		} else if (bootverbose) {
23580909f38aSPawel Jakub Dawidek 			printf("Swap device %s removed.\n", devname);
23590909f38aSPawel Jakub Dawidek 		}
23600909f38aSPawel Jakub Dawidek 		mtx_lock(&sw_dev_mtx);
23610909f38aSPawel Jakub Dawidek 	}
23620909f38aSPawel Jakub Dawidek 	mtx_unlock(&sw_dev_mtx);
23630909f38aSPawel Jakub Dawidek 
236404533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2365e9c0cc15SPoul-Henning Kamp }
2366e9c0cc15SPoul-Henning Kamp 
2367567104a1SPoul-Henning Kamp void
2368567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used)
2369567104a1SPoul-Henning Kamp {
2370567104a1SPoul-Henning Kamp 	struct swdevt *sp;
2371567104a1SPoul-Henning Kamp 
2372567104a1SPoul-Henning Kamp 	*total = 0;
2373567104a1SPoul-Henning Kamp 	*used = 0;
237420da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23758f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2376567104a1SPoul-Henning Kamp 		*total += sp->sw_nblks;
2377567104a1SPoul-Henning Kamp 		*used += sp->sw_used;
2378567104a1SPoul-Henning Kamp 	}
237920da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2380567104a1SPoul-Henning Kamp }
2381567104a1SPoul-Henning Kamp 
2382dda4f960SKonstantin Belousov int
2383dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2384dda4f960SKonstantin Belousov {
2385dda4f960SKonstantin Belousov 	struct swdevt *sp;
23867870adb6SEd Schouten 	const char *tmp_devname;
2387dda4f960SKonstantin Belousov 	int error, n;
2388dda4f960SKonstantin Belousov 
2389dda4f960SKonstantin Belousov 	n = 0;
2390dda4f960SKonstantin Belousov 	error = ENOENT;
2391dda4f960SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2392dda4f960SKonstantin Belousov 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2393dda4f960SKonstantin Belousov 		if (n != name) {
2394dda4f960SKonstantin Belousov 			n++;
2395dda4f960SKonstantin Belousov 			continue;
2396dda4f960SKonstantin Belousov 		}
2397dda4f960SKonstantin Belousov 		xs->xsw_version = XSWDEV_VERSION;
2398dda4f960SKonstantin Belousov 		xs->xsw_dev = sp->sw_dev;
2399dda4f960SKonstantin Belousov 		xs->xsw_flags = sp->sw_flags;
2400dda4f960SKonstantin Belousov 		xs->xsw_nblks = sp->sw_nblks;
2401dda4f960SKonstantin Belousov 		xs->xsw_used = sp->sw_used;
2402dda4f960SKonstantin Belousov 		if (devname != NULL) {
2403dda4f960SKonstantin Belousov 			if (vn_isdisk(sp->sw_vp, NULL))
24047870adb6SEd Schouten 				tmp_devname = devtoname(sp->sw_vp->v_rdev);
2405dda4f960SKonstantin Belousov 			else
2406dda4f960SKonstantin Belousov 				tmp_devname = "[file]";
2407dda4f960SKonstantin Belousov 			strncpy(devname, tmp_devname, len);
2408dda4f960SKonstantin Belousov 		}
2409dda4f960SKonstantin Belousov 		error = 0;
2410dda4f960SKonstantin Belousov 		break;
2411dda4f960SKonstantin Belousov 	}
2412dda4f960SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2413dda4f960SKonstantin Belousov 	return (error);
2414dda4f960SKonstantin Belousov }
2415dda4f960SKonstantin Belousov 
241669921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
241769921123SKonstantin Belousov #define XSWDEV_VERSION_11	1
241869921123SKonstantin Belousov struct xswdev11 {
241969921123SKonstantin Belousov 	u_int	xsw_version;
242069921123SKonstantin Belousov 	uint32_t xsw_dev;
242169921123SKonstantin Belousov 	int	xsw_flags;
242269921123SKonstantin Belousov 	int	xsw_nblks;
242369921123SKonstantin Belousov 	int     xsw_used;
242469921123SKonstantin Belousov };
242569921123SKonstantin Belousov #endif
242669921123SKonstantin Belousov 
2427e9c0cc15SPoul-Henning Kamp static int
2428e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2429e9c0cc15SPoul-Henning Kamp {
2430e9c0cc15SPoul-Henning Kamp 	struct xswdev xs;
243169921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
243269921123SKonstantin Belousov 	struct xswdev11 xs11;
243369921123SKonstantin Belousov #endif
2434dda4f960SKonstantin Belousov 	int error;
2435e9c0cc15SPoul-Henning Kamp 
2436e9c0cc15SPoul-Henning Kamp 	if (arg2 != 1)			/* name length */
2437e9c0cc15SPoul-Henning Kamp 		return (EINVAL);
2438dda4f960SKonstantin Belousov 	error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2439dda4f960SKonstantin Belousov 	if (error != 0)
2440dda4f960SKonstantin Belousov 		return (error);
244169921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
244269921123SKonstantin Belousov 	if (req->oldlen == sizeof(xs11)) {
244369921123SKonstantin Belousov 		xs11.xsw_version = XSWDEV_VERSION_11;
244469921123SKonstantin Belousov 		xs11.xsw_dev = xs.xsw_dev; /* truncation */
244569921123SKonstantin Belousov 		xs11.xsw_flags = xs.xsw_flags;
244669921123SKonstantin Belousov 		xs11.xsw_nblks = xs.xsw_nblks;
244769921123SKonstantin Belousov 		xs11.xsw_used = xs.xsw_used;
244869921123SKonstantin Belousov 		error = SYSCTL_OUT(req, &xs11, sizeof(xs11));
244969921123SKonstantin Belousov 	} else
245069921123SKonstantin Belousov #endif
2451e9c0cc15SPoul-Henning Kamp 		error = SYSCTL_OUT(req, &xs, sizeof(xs));
2452e9c0cc15SPoul-Henning Kamp 	return (error);
2453e9c0cc15SPoul-Henning Kamp }
2454e9c0cc15SPoul-Henning Kamp 
24558f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2456e9c0cc15SPoul-Henning Kamp     "Number of swap devices");
24574c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE,
24584c36e917SKonstantin Belousov     sysctl_vm_swap_info,
2459e9c0cc15SPoul-Henning Kamp     "Swap statistics by device");
2460ec38b344SPoul-Henning Kamp 
2461ec38b344SPoul-Henning Kamp /*
24628eb5a1cdSKonstantin Belousov  * vmspace_swap_count() - count the approximate swap usage in pages for a
2463ec38b344SPoul-Henning Kamp  *			  vmspace.
2464ec38b344SPoul-Henning Kamp  *
2465ec38b344SPoul-Henning Kamp  *	The map must be locked.
2466ec38b344SPoul-Henning Kamp  *
24678eb5a1cdSKonstantin Belousov  *	Swap usage is determined by taking the proportional swap used by
2468ec38b344SPoul-Henning Kamp  *	VM objects backing the VM map.  To make up for fractional losses,
2469ec38b344SPoul-Henning Kamp  *	if the VM object has any swap use at all the associated map entries
2470ec38b344SPoul-Henning Kamp  *	count for at least 1 swap page.
2471ec38b344SPoul-Henning Kamp  */
24722860553aSRebecca Cran long
2473ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2474ec38b344SPoul-Henning Kamp {
247565d8409cSRebecca Cran 	vm_map_t map;
2476ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
247765d8409cSRebecca Cran 	vm_object_t object;
24782860553aSRebecca Cran 	long count, n;
247965d8409cSRebecca Cran 
248065d8409cSRebecca Cran 	map = &vmspace->vm_map;
248165d8409cSRebecca Cran 	count = 0;
2482ec38b344SPoul-Henning Kamp 
2483ec38b344SPoul-Henning Kamp 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2484ec38b344SPoul-Henning Kamp 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2485ec38b344SPoul-Henning Kamp 		    (object = cur->object.vm_object) != NULL) {
248689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
2487ec38b344SPoul-Henning Kamp 			if (object->type == OBJT_SWAP &&
2488ec38b344SPoul-Henning Kamp 			    object->un_pager.swp.swp_bcount != 0) {
248965d8409cSRebecca Cran 				n = (cur->end - cur->start) / PAGE_SIZE;
2490ec38b344SPoul-Henning Kamp 				count += object->un_pager.swp.swp_bcount *
2491ec38b344SPoul-Henning Kamp 				    SWAP_META_PAGES * n / object->size + 1;
2492ec38b344SPoul-Henning Kamp 			}
249389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
2494ec38b344SPoul-Henning Kamp 		}
2495ec38b344SPoul-Henning Kamp 	}
2496ec38b344SPoul-Henning Kamp 	return (count);
2497ec38b344SPoul-Henning Kamp }
2498dee34ca4SPoul-Henning Kamp 
2499dee34ca4SPoul-Henning Kamp /*
2500dee34ca4SPoul-Henning Kamp  * GEOM backend
2501dee34ca4SPoul-Henning Kamp  *
2502dee34ca4SPoul-Henning Kamp  * Swapping onto disk devices.
2503dee34ca4SPoul-Henning Kamp  *
2504dee34ca4SPoul-Henning Kamp  */
2505dee34ca4SPoul-Henning Kamp 
25065721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan;
25075721c9c7SPoul-Henning Kamp 
2508dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = {
2509dee34ca4SPoul-Henning Kamp 	.name = "SWAP",
25105721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
25115721c9c7SPoul-Henning Kamp 	.orphan = swapgeom_orphan,
2512dee34ca4SPoul-Henning Kamp };
2513dee34ca4SPoul-Henning Kamp 
2514dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class);
2515dee34ca4SPoul-Henning Kamp 
2516dee34ca4SPoul-Henning Kamp 
2517dee34ca4SPoul-Henning Kamp static void
25183398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags)
25193398491bSAlexander Motin {
25203398491bSAlexander Motin 	struct g_consumer *cp;
25213398491bSAlexander Motin 
25223398491bSAlexander Motin 	cp = arg;
25233398491bSAlexander Motin 	g_access(cp, -1, -1, 0);
25243398491bSAlexander Motin 	g_detach(cp);
25253398491bSAlexander Motin 	g_destroy_consumer(cp);
25263398491bSAlexander Motin }
25273398491bSAlexander Motin 
25289e3e3fe5SWarner Losh /*
25299e3e3fe5SWarner Losh  * Add a reference to the g_consumer for an inflight transaction.
25309e3e3fe5SWarner Losh  */
25319e3e3fe5SWarner Losh static void
25329e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp)
25339e3e3fe5SWarner Losh {
25349e3e3fe5SWarner Losh 
25359e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
25369e3e3fe5SWarner Losh 	cp->index++;
25379e3e3fe5SWarner Losh }
25389e3e3fe5SWarner Losh 
25399e3e3fe5SWarner Losh /*
25400c657d22SKonstantin Belousov  * Remove a reference from the g_consumer.  Post a close event if all
25410c657d22SKonstantin Belousov  * references go away, since the function might be called from the
25420c657d22SKonstantin Belousov  * biodone context.
25439e3e3fe5SWarner Losh  */
25449e3e3fe5SWarner Losh static void
25459e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
25469e3e3fe5SWarner Losh {
25479e3e3fe5SWarner Losh 
25489e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
25499e3e3fe5SWarner Losh 	cp->index--;
25509e3e3fe5SWarner Losh 	if (cp->index == 0) {
25519e3e3fe5SWarner Losh 		if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
25529e3e3fe5SWarner Losh 			sp->sw_id = NULL;
25539e3e3fe5SWarner Losh 	}
25549e3e3fe5SWarner Losh }
25559e3e3fe5SWarner Losh 
25563398491bSAlexander Motin static void
2557dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2)
2558dee34ca4SPoul-Henning Kamp {
25593398491bSAlexander Motin 	struct swdevt *sp;
2560dee34ca4SPoul-Henning Kamp 	struct buf *bp;
25613398491bSAlexander Motin 	struct g_consumer *cp;
2562dee34ca4SPoul-Henning Kamp 
2563dee34ca4SPoul-Henning Kamp 	bp = bp2->bio_caller2;
25643398491bSAlexander Motin 	cp = bp2->bio_from;
2565c5d3d25eSPoul-Henning Kamp 	bp->b_ioflags = bp2->bio_flags;
2566dee34ca4SPoul-Henning Kamp 	if (bp2->bio_error)
2567dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2568c5d3d25eSPoul-Henning Kamp 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2569c5d3d25eSPoul-Henning Kamp 	bp->b_error = bp2->bio_error;
2570dee34ca4SPoul-Henning Kamp 	bufdone(bp);
25713398491bSAlexander Motin 	sp = bp2->bio_caller1;
25729e3e3fe5SWarner Losh 	mtx_lock(&sw_dev_mtx);
25739e3e3fe5SWarner Losh 	swapgeom_release(cp, sp);
25743398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
2575dee34ca4SPoul-Henning Kamp 	g_destroy_bio(bp2);
2576dee34ca4SPoul-Henning Kamp }
2577dee34ca4SPoul-Henning Kamp 
2578dee34ca4SPoul-Henning Kamp static void
2579dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2580dee34ca4SPoul-Henning Kamp {
2581dee34ca4SPoul-Henning Kamp 	struct bio *bio;
2582dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2583dee34ca4SPoul-Henning Kamp 
25843398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
2585dee34ca4SPoul-Henning Kamp 	cp = sp->sw_id;
2586dee34ca4SPoul-Henning Kamp 	if (cp == NULL) {
25873398491bSAlexander Motin 		mtx_unlock(&sw_dev_mtx);
2588dee34ca4SPoul-Henning Kamp 		bp->b_error = ENXIO;
2589dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2590dee34ca4SPoul-Henning Kamp 		bufdone(bp);
2591dee34ca4SPoul-Henning Kamp 		return;
2592dee34ca4SPoul-Henning Kamp 	}
25939e3e3fe5SWarner Losh 	swapgeom_acquire(cp);
25943398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
259511041003SKonstantin Belousov 	if (bp->b_iocmd == BIO_WRITE)
259611041003SKonstantin Belousov 		bio = g_new_bio();
259711041003SKonstantin Belousov 	else
25984f8205e5SPoul-Henning Kamp 		bio = g_alloc_bio();
25994f8205e5SPoul-Henning Kamp 	if (bio == NULL) {
26009e3e3fe5SWarner Losh 		mtx_lock(&sw_dev_mtx);
26019e3e3fe5SWarner Losh 		swapgeom_release(cp, sp);
26029e3e3fe5SWarner Losh 		mtx_unlock(&sw_dev_mtx);
26033e5b6861SPoul-Henning Kamp 		bp->b_error = ENOMEM;
26043e5b6861SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
26053e5b6861SPoul-Henning Kamp 		bufdone(bp);
26063e5b6861SPoul-Henning Kamp 		return;
26073e5b6861SPoul-Henning Kamp 	}
260811041003SKonstantin Belousov 
26093398491bSAlexander Motin 	bio->bio_caller1 = sp;
2610dee34ca4SPoul-Henning Kamp 	bio->bio_caller2 = bp;
2611c5d3d25eSPoul-Henning Kamp 	bio->bio_cmd = bp->b_iocmd;
2612dee34ca4SPoul-Henning Kamp 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2613dee34ca4SPoul-Henning Kamp 	bio->bio_length = bp->b_bcount;
2614dee34ca4SPoul-Henning Kamp 	bio->bio_done = swapgeom_done;
2615fade8dd7SJeff Roberson 	if (!buf_mapped(bp)) {
26162cc718a1SKonstantin Belousov 		bio->bio_ma = bp->b_pages;
26172cc718a1SKonstantin Belousov 		bio->bio_data = unmapped_buf;
26182cc718a1SKonstantin Belousov 		bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
26192cc718a1SKonstantin Belousov 		bio->bio_ma_n = bp->b_npages;
26202cc718a1SKonstantin Belousov 		bio->bio_flags |= BIO_UNMAPPED;
26212cc718a1SKonstantin Belousov 	} else {
26222cc718a1SKonstantin Belousov 		bio->bio_data = bp->b_data;
26232cc718a1SKonstantin Belousov 		bio->bio_ma = NULL;
26242cc718a1SKonstantin Belousov 	}
2625dee34ca4SPoul-Henning Kamp 	g_io_request(bio, cp);
2626dee34ca4SPoul-Henning Kamp 	return;
2627dee34ca4SPoul-Henning Kamp }
2628dee34ca4SPoul-Henning Kamp 
2629dee34ca4SPoul-Henning Kamp static void
2630dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp)
2631dee34ca4SPoul-Henning Kamp {
2632dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
26333398491bSAlexander Motin 	int destroy;
2634dee34ca4SPoul-Henning Kamp 
2635dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
26363398491bSAlexander Motin 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
26373398491bSAlexander Motin 		if (sp->sw_id == cp) {
26388f12d83aSAlexander Motin 			sp->sw_flags |= SW_CLOSING;
26393398491bSAlexander Motin 			break;
2640dee34ca4SPoul-Henning Kamp 		}
26413398491bSAlexander Motin 	}
26429e3e3fe5SWarner Losh 	/*
26439e3e3fe5SWarner Losh 	 * Drop reference we were created with. Do directly since we're in a
26449e3e3fe5SWarner Losh 	 * special context where we don't have to queue the call to
26459e3e3fe5SWarner Losh 	 * swapgeom_close_ev().
26469e3e3fe5SWarner Losh 	 */
26479e3e3fe5SWarner Losh 	cp->index--;
26483398491bSAlexander Motin 	destroy = ((sp != NULL) && (cp->index == 0));
26493398491bSAlexander Motin 	if (destroy)
26503398491bSAlexander Motin 		sp->sw_id = NULL;
26513398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
26523398491bSAlexander Motin 	if (destroy)
26533398491bSAlexander Motin 		swapgeom_close_ev(cp, 0);
2654dee34ca4SPoul-Henning Kamp }
2655dee34ca4SPoul-Henning Kamp 
2656dee34ca4SPoul-Henning Kamp static void
2657dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw)
2658dee34ca4SPoul-Henning Kamp {
26593398491bSAlexander Motin 	struct g_consumer *cp;
2660dee34ca4SPoul-Henning Kamp 
26613398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
26623398491bSAlexander Motin 	cp = sw->sw_id;
26633398491bSAlexander Motin 	sw->sw_id = NULL;
26643398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
26650c657d22SKonstantin Belousov 
26660c657d22SKonstantin Belousov 	/*
26670c657d22SKonstantin Belousov 	 * swapgeom_close() may be called from the biodone context,
26680c657d22SKonstantin Belousov 	 * where we cannot perform topology changes.  Delegate the
26690c657d22SKonstantin Belousov 	 * work to the events thread.
26700c657d22SKonstantin Belousov 	 */
26713398491bSAlexander Motin 	if (cp != NULL)
26723398491bSAlexander Motin 		g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2673dee34ca4SPoul-Henning Kamp }
2674dee34ca4SPoul-Henning Kamp 
267588ad2d7bSKonstantin Belousov static int
267688ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp)
2677dee34ca4SPoul-Henning Kamp {
2678dee34ca4SPoul-Henning Kamp 	struct g_provider *pp;
2679dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2680dee34ca4SPoul-Henning Kamp 	static struct g_geom *gp;
2681dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2682dee34ca4SPoul-Henning Kamp 	u_long nblks;
2683dee34ca4SPoul-Henning Kamp 	int error;
2684dee34ca4SPoul-Henning Kamp 
268588ad2d7bSKonstantin Belousov 	pp = g_dev_getprovider(dev);
268688ad2d7bSKonstantin Belousov 	if (pp == NULL)
268788ad2d7bSKonstantin Belousov 		return (ENODEV);
2688dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2689dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2690dee34ca4SPoul-Henning Kamp 		cp = sp->sw_id;
2691dee34ca4SPoul-Henning Kamp 		if (cp != NULL && cp->provider == pp) {
2692dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
269388ad2d7bSKonstantin Belousov 			return (EBUSY);
2694dee34ca4SPoul-Henning Kamp 		}
2695dee34ca4SPoul-Henning Kamp 	}
2696dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
26975721c9c7SPoul-Henning Kamp 	if (gp == NULL)
269802c62349SJaakko Heinonen 		gp = g_new_geomf(&g_swap_class, "swap");
2699dee34ca4SPoul-Henning Kamp 	cp = g_new_consumer(gp);
27009e3e3fe5SWarner Losh 	cp->index = 1;	/* Number of active I/Os, plus one for being active. */
27019e3e3fe5SWarner Losh 	cp->flags |=  G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2702dee34ca4SPoul-Henning Kamp 	g_attach(cp, pp);
2703afeb65e6SPoul-Henning Kamp 	/*
2704afeb65e6SPoul-Henning Kamp 	 * XXX: Every time you think you can improve the margin for
2705afeb65e6SPoul-Henning Kamp 	 * footshooting, somebody depends on the ability to do so:
2706afeb65e6SPoul-Henning Kamp 	 * savecore(8) wants to write to our swapdev so we cannot
2707afeb65e6SPoul-Henning Kamp 	 * set an exclusive count :-(
2708afeb65e6SPoul-Henning Kamp 	 */
2709d2bae332SPoul-Henning Kamp 	error = g_access(cp, 1, 1, 0);
271088ad2d7bSKonstantin Belousov 	if (error != 0) {
2711dee34ca4SPoul-Henning Kamp 		g_detach(cp);
2712dee34ca4SPoul-Henning Kamp 		g_destroy_consumer(cp);
271388ad2d7bSKonstantin Belousov 		return (error);
2714dee34ca4SPoul-Henning Kamp 	}
2715dee34ca4SPoul-Henning Kamp 	nblks = pp->mediasize / DEV_BSIZE;
271688ad2d7bSKonstantin Belousov 	swaponsomething(vp, cp, nblks, swapgeom_strategy,
271788ad2d7bSKonstantin Belousov 	    swapgeom_close, dev2udev(dev),
27182cc718a1SKonstantin Belousov 	    (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
271988ad2d7bSKonstantin Belousov 	return (0);
2720dee34ca4SPoul-Henning Kamp }
2721dee34ca4SPoul-Henning Kamp 
2722dee34ca4SPoul-Henning Kamp static int
272388ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp)
2724dee34ca4SPoul-Henning Kamp {
2725dee34ca4SPoul-Henning Kamp 	int error;
2726dee34ca4SPoul-Henning Kamp 
2727cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
272888ad2d7bSKonstantin Belousov 	if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) {
272988ad2d7bSKonstantin Belousov 		error = ENOENT;
273088ad2d7bSKonstantin Belousov 	} else {
273188ad2d7bSKonstantin Belousov 		g_topology_lock();
273288ad2d7bSKonstantin Belousov 		error = swapongeom_locked(vp->v_rdev, vp);
273388ad2d7bSKonstantin Belousov 		g_topology_unlock();
273488ad2d7bSKonstantin Belousov 	}
273522db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
2736dee34ca4SPoul-Henning Kamp 	return (error);
2737dee34ca4SPoul-Henning Kamp }
2738dee34ca4SPoul-Henning Kamp 
2739dee34ca4SPoul-Henning Kamp /*
2740dee34ca4SPoul-Henning Kamp  * VNODE backend
2741dee34ca4SPoul-Henning Kamp  *
2742dee34ca4SPoul-Henning Kamp  * This is used mainly for network filesystem (read: probably only tested
2743dee34ca4SPoul-Henning Kamp  * with NFS) swapfiles.
2744dee34ca4SPoul-Henning Kamp  *
2745dee34ca4SPoul-Henning Kamp  */
2746dee34ca4SPoul-Henning Kamp 
2747dee34ca4SPoul-Henning Kamp static void
2748dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp)
2749dee34ca4SPoul-Henning Kamp {
2750494eb176SPoul-Henning Kamp 	struct vnode *vp2;
2751dee34ca4SPoul-Henning Kamp 
2752dee34ca4SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2753dee34ca4SPoul-Henning Kamp 
2754dee34ca4SPoul-Henning Kamp 	vp2 = sp->sw_id;
2755dee34ca4SPoul-Henning Kamp 	vhold(vp2);
2756dee34ca4SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
27573cfc7651SOlivier Houchard 		if (bp->b_bufobj)
2758494eb176SPoul-Henning Kamp 			bufobj_wdrop(bp->b_bufobj);
2759a76d8f4eSPoul-Henning Kamp 		bufobj_wref(&vp2->v_bufobj);
2760dee34ca4SPoul-Henning Kamp 	}
27613cfc7651SOlivier Houchard 	if (bp->b_bufobj != &vp2->v_bufobj)
27623cfc7651SOlivier Houchard 		bp->b_bufobj = &vp2->v_bufobj;
2763dee34ca4SPoul-Henning Kamp 	bp->b_vp = vp2;
27642c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
2765b792bebeSPoul-Henning Kamp 	bstrategy(bp);
2766dee34ca4SPoul-Henning Kamp 	return;
2767dee34ca4SPoul-Henning Kamp }
2768dee34ca4SPoul-Henning Kamp 
2769dee34ca4SPoul-Henning Kamp static void
2770dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp)
2771dee34ca4SPoul-Henning Kamp {
2772dee34ca4SPoul-Henning Kamp 
2773dee34ca4SPoul-Henning Kamp 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2774dee34ca4SPoul-Henning Kamp 	vrele(sp->sw_vp);
2775dee34ca4SPoul-Henning Kamp }
2776dee34ca4SPoul-Henning Kamp 
2777dee34ca4SPoul-Henning Kamp 
2778dee34ca4SPoul-Henning Kamp static int
2779dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2780dee34ca4SPoul-Henning Kamp {
2781dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2782dee34ca4SPoul-Henning Kamp 	int error;
2783dee34ca4SPoul-Henning Kamp 
2784dee34ca4SPoul-Henning Kamp 	if (nblks == 0)
2785dee34ca4SPoul-Henning Kamp 		return (ENXIO);
2786dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2787dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2788dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == vp) {
2789dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2790dee34ca4SPoul-Henning Kamp 			return (EBUSY);
2791dee34ca4SPoul-Henning Kamp 		}
2792dee34ca4SPoul-Henning Kamp 	}
2793dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2794dee34ca4SPoul-Henning Kamp 
2795cb05b60aSAttilio Rao 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2796dee34ca4SPoul-Henning Kamp #ifdef MAC
279730d239bcSRobert Watson 	error = mac_system_check_swapon(td->td_ucred, vp);
2798dee34ca4SPoul-Henning Kamp 	if (error == 0)
2799dee34ca4SPoul-Henning Kamp #endif
28009e223287SKonstantin Belousov 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
280122db15c0SAttilio Rao 	(void) VOP_UNLOCK(vp, 0);
2802dee34ca4SPoul-Henning Kamp 	if (error)
2803dee34ca4SPoul-Henning Kamp 		return (error);
2804dee34ca4SPoul-Henning Kamp 
2805dee34ca4SPoul-Henning Kamp 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
28062cc718a1SKonstantin Belousov 	    NODEV, 0);
2807dee34ca4SPoul-Henning Kamp 	return (0);
2808dee34ca4SPoul-Henning Kamp }
280989c241d1SGleb Smirnoff 
281089c241d1SGleb Smirnoff static int
281189c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
281289c241d1SGleb Smirnoff {
281389c241d1SGleb Smirnoff 	int error, new, n;
281489c241d1SGleb Smirnoff 
281589c241d1SGleb Smirnoff 	new = nsw_wcount_async_max;
281689c241d1SGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
281789c241d1SGleb Smirnoff 	if (error != 0 || req->newptr == NULL)
281889c241d1SGleb Smirnoff 		return (error);
281989c241d1SGleb Smirnoff 
282089c241d1SGleb Smirnoff 	if (new > nswbuf / 2 || new < 1)
282189c241d1SGleb Smirnoff 		return (EINVAL);
282289c241d1SGleb Smirnoff 
282389c241d1SGleb Smirnoff 	mtx_lock(&pbuf_mtx);
282489c241d1SGleb Smirnoff 	while (nsw_wcount_async_max != new) {
282589c241d1SGleb Smirnoff 		/*
282689c241d1SGleb Smirnoff 		 * Adjust difference.  If the current async count is too low,
282789c241d1SGleb Smirnoff 		 * we will need to sqeeze our update slowly in.  Sleep with a
282889c241d1SGleb Smirnoff 		 * higher priority than getpbuf() to finish faster.
282989c241d1SGleb Smirnoff 		 */
283089c241d1SGleb Smirnoff 		n = new - nsw_wcount_async_max;
283189c241d1SGleb Smirnoff 		if (nsw_wcount_async + n >= 0) {
283289c241d1SGleb Smirnoff 			nsw_wcount_async += n;
283389c241d1SGleb Smirnoff 			nsw_wcount_async_max += n;
283489c241d1SGleb Smirnoff 			wakeup(&nsw_wcount_async);
283589c241d1SGleb Smirnoff 		} else {
283689c241d1SGleb Smirnoff 			nsw_wcount_async_max -= nsw_wcount_async;
283789c241d1SGleb Smirnoff 			nsw_wcount_async = 0;
283889c241d1SGleb Smirnoff 			msleep(&nsw_wcount_async, &pbuf_mtx, PSWP,
283989c241d1SGleb Smirnoff 			    "swpsysctl", 0);
284089c241d1SGleb Smirnoff 		}
284189c241d1SGleb Smirnoff 	}
284289c241d1SGleb Smirnoff 	mtx_unlock(&pbuf_mtx);
284389c241d1SGleb Smirnoff 
284489c241d1SGleb Smirnoff 	return (0);
284589c241d1SGleb Smirnoff }
2846