xref: /freebsd/sys/vm/swap_pager.c (revision 41e5a22698438d23a02bab6a8701d5495e32fb60)
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>
90f425ab8eSKonstantin Belousov #include <sys/pctrie.h>
911ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
923364c323SKonstantin Belousov #include <sys/resource.h>
933364c323SKonstantin Belousov #include <sys/resourcevar.h>
9489f6b863SAttilio Rao #include <sys/rwlock.h>
95d027ed2eSAlan Cox #include <sys/sbuf.h>
96327f4e83SMatthew Dillon #include <sys/sysctl.h>
97e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
981c7c3c6aSMatthew Dillon #include <sys/blist.h>
991c7c3c6aSMatthew Dillon #include <sys/lock.h>
1000cddd8f0SMatthew Dillon #include <sys/sx.h>
101936524aaSMatthew Dillon #include <sys/vmmeter.h>
102df8bae1dSRodney W. Grimes 
103aed55708SRobert Watson #include <security/mac/mac_framework.h>
104aed55708SRobert Watson 
105df8bae1dSRodney W. Grimes #include <vm/vm.h>
10621cd6e62SSeigo Tanimura #include <vm/pmap.h>
10721cd6e62SSeigo Tanimura #include <vm/vm_map.h>
10821cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
109efeaf95aSDavid Greenman #include <vm/vm_object.h>
110df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
111efeaf95aSDavid Greenman #include <vm/vm_pager.h>
112df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
113e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
114df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
115efeaf95aSDavid Greenman #include <vm/vm_extern.h>
116670d17b5SJeff Roberson #include <vm/uma.h>
117df8bae1dSRodney W. Grimes 
118dee34ca4SPoul-Henning Kamp #include <geom/geom.h>
119dee34ca4SPoul-Henning Kamp 
120ec38b344SPoul-Henning Kamp /*
121064650c1SAlan Cox  * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64.
122064650c1SAlan Cox  * The 64-page limit is due to the radix code (kern/subr_blist.c).
123ec38b344SPoul-Henning Kamp  */
124ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
125e2241590SAlan Cox #define	MAX_PAGEOUT_CLUSTER	32
126ec38b344SPoul-Henning Kamp #endif
127ec38b344SPoul-Henning Kamp 
128ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
129ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
130ec38b344SPoul-Henning Kamp #endif
131ec38b344SPoul-Henning Kamp 
132f425ab8eSKonstantin Belousov #define	SWAP_META_PAGES		PCTRIE_COUNT
133ec38b344SPoul-Henning Kamp 
134f425ab8eSKonstantin Belousov /*
135f425ab8eSKonstantin Belousov  * A swblk structure maps each page index within a
136f425ab8eSKonstantin Belousov  * SWAP_META_PAGES-aligned and sized range to the address of an
137f425ab8eSKonstantin Belousov  * on-disk swap block (or SWAPBLK_NONE). The collection of these
138f425ab8eSKonstantin Belousov  * mappings for an entire vm object is implemented as a pc-trie.
139f425ab8eSKonstantin Belousov  */
140f425ab8eSKonstantin Belousov struct swblk {
141f425ab8eSKonstantin Belousov 	vm_pindex_t	p;
142f425ab8eSKonstantin Belousov 	daddr_t		d[SWAP_META_PAGES];
143e9c0cc15SPoul-Henning Kamp };
144e9c0cc15SPoul-Henning Kamp 
1452c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
14620da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx;
1478f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1488f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1498f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1508f60c087SPoul-Henning Kamp int swap_pager_avail;
15104533e1eSKonstantin Belousov static struct sx swdev_syscall_lock;	/* serialize swap(on|off) */
152e9c0cc15SPoul-Henning Kamp 
1533364c323SKonstantin Belousov static vm_ooffset_t swap_total;
1548a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0,
1558a9c731fSIvan Voras     "Total amount of available swap storage.");
1563364c323SKonstantin Belousov static vm_ooffset_t swap_reserved;
1578a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0,
1588a9c731fSIvan Voras     "Amount of swap storage needed to back all allocated anonymous memory.");
1593364c323SKonstantin Belousov static int overcommit = 0;
1608a9c731fSIvan Voras SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0,
1618a9c731fSIvan Voras     "Configure virtual memory overcommit behavior. See tuning(7) "
1628a9c731fSIvan Voras     "for details.");
16361203277SDag-Erling Smørgrav static unsigned long swzone;
16461203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
16561203277SDag-Erling Smørgrav     "Actual size of swap metadata zone");
16661203277SDag-Erling Smørgrav static unsigned long swap_maxpages;
16761203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
16861203277SDag-Erling Smørgrav     "Maximum amount of swap supported");
1693364c323SKonstantin Belousov 
1703364c323SKonstantin Belousov /* bits from overcommit */
1713364c323SKonstantin Belousov #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
1723364c323SKonstantin Belousov #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
1733364c323SKonstantin Belousov #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
1743364c323SKonstantin Belousov 
1753364c323SKonstantin Belousov int
1763364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr)
1773364c323SKonstantin Belousov {
1783364c323SKonstantin Belousov 
179ef694c1aSEdward Tomasz Napierala 	return (swap_reserve_by_cred(incr, curthread->td_ucred));
1803364c323SKonstantin Belousov }
1813364c323SKonstantin Belousov 
1823364c323SKonstantin Belousov int
183ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
1843364c323SKonstantin Belousov {
1855c0e1c11SKonstantin Belousov 	vm_ooffset_t r, s;
1863364c323SKonstantin Belousov 	int res, error;
1873364c323SKonstantin Belousov 	static int curfail;
1883364c323SKonstantin Belousov 	static struct timeval lastfail;
189ef694c1aSEdward Tomasz Napierala 	struct uidinfo *uip;
190ef694c1aSEdward Tomasz Napierala 
191ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
1923364c323SKonstantin Belousov 
1933364c323SKonstantin Belousov 	if (incr & PAGE_MASK)
1943364c323SKonstantin Belousov 		panic("swap_reserve: & PAGE_MASK");
1953364c323SKonstantin Belousov 
196afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1974b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
1981ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
1991ba5ad42SEdward Tomasz Napierala 		error = racct_add(curproc, RACCT_SWAP, incr);
2001ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2011ba5ad42SEdward Tomasz Napierala 		if (error != 0)
2021ba5ad42SEdward Tomasz Napierala 			return (0);
2034b5c9cf6SEdward Tomasz Napierala 	}
204afcc55f3SEdward Tomasz Napierala #endif
2051ba5ad42SEdward Tomasz Napierala 
2063364c323SKonstantin Belousov 	res = 0;
2073364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2083364c323SKonstantin Belousov 	r = swap_reserved + incr;
2093364c323SKonstantin Belousov 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
21044f1c916SBryan Drewery 		s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - vm_cnt.v_wire_count;
2113364c323SKonstantin Belousov 		s *= PAGE_SIZE;
2123364c323SKonstantin Belousov 	} else
2133364c323SKonstantin Belousov 		s = 0;
2143364c323SKonstantin Belousov 	s += swap_total;
2153364c323SKonstantin Belousov 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
2163364c323SKonstantin Belousov 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
2173364c323SKonstantin Belousov 		res = 1;
2183364c323SKonstantin Belousov 		swap_reserved = r;
2193364c323SKonstantin Belousov 	}
2203364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2213364c323SKonstantin Belousov 
2223364c323SKonstantin Belousov 	if (res) {
2233364c323SKonstantin Belousov 		UIDINFO_VMSIZE_LOCK(uip);
2245c0e1c11SKonstantin Belousov 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
225f6f6d240SMateusz Guzik 		    uip->ui_vmsize + incr > lim_cur(curthread, RLIMIT_SWAP) &&
2265c0e1c11SKonstantin Belousov 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT))
2273364c323SKonstantin Belousov 			res = 0;
2283364c323SKonstantin Belousov 		else
2293364c323SKonstantin Belousov 			uip->ui_vmsize += incr;
2303364c323SKonstantin Belousov 		UIDINFO_VMSIZE_UNLOCK(uip);
2313364c323SKonstantin Belousov 		if (!res) {
2323364c323SKonstantin Belousov 			mtx_lock(&sw_dev_mtx);
2333364c323SKonstantin Belousov 			swap_reserved -= incr;
2343364c323SKonstantin Belousov 			mtx_unlock(&sw_dev_mtx);
2353364c323SKonstantin Belousov 		}
2363364c323SKonstantin Belousov 	}
2373364c323SKonstantin Belousov 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
2383364c323SKonstantin Belousov 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
239134465d7SKonstantin Belousov 		    uip->ui_uid, curproc->p_pid, incr);
2403364c323SKonstantin Belousov 	}
2413364c323SKonstantin Belousov 
242afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2431ba5ad42SEdward Tomasz Napierala 	if (!res) {
2441ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2451ba5ad42SEdward Tomasz Napierala 		racct_sub(curproc, RACCT_SWAP, incr);
2461ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2471ba5ad42SEdward Tomasz Napierala 	}
248afcc55f3SEdward Tomasz Napierala #endif
2491ba5ad42SEdward Tomasz Napierala 
2503364c323SKonstantin Belousov 	return (res);
2513364c323SKonstantin Belousov }
2523364c323SKonstantin Belousov 
2533364c323SKonstantin Belousov void
2543364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr)
2553364c323SKonstantin Belousov {
2563364c323SKonstantin Belousov 	struct uidinfo *uip;
2573364c323SKonstantin Belousov 
2583364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2593364c323SKonstantin Belousov 	swap_reserved += incr;
2603364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2613364c323SKonstantin Belousov 
262afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2631ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(curproc);
2641ba5ad42SEdward Tomasz Napierala 	racct_add_force(curproc, RACCT_SWAP, incr);
2651ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(curproc);
266afcc55f3SEdward Tomasz Napierala #endif
2671ba5ad42SEdward Tomasz Napierala 
2683364c323SKonstantin Belousov 	uip = curthread->td_ucred->cr_ruidinfo;
2693364c323SKonstantin Belousov 	PROC_LOCK(curproc);
2703364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
2713364c323SKonstantin Belousov 	uip->ui_vmsize += incr;
2723364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
2733364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2743364c323SKonstantin Belousov }
2753364c323SKonstantin Belousov 
2763364c323SKonstantin Belousov void
2773364c323SKonstantin Belousov swap_release(vm_ooffset_t decr)
2783364c323SKonstantin Belousov {
279ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
2803364c323SKonstantin Belousov 
2813364c323SKonstantin Belousov 	PROC_LOCK(curproc);
282ef694c1aSEdward Tomasz Napierala 	cred = curthread->td_ucred;
283ef694c1aSEdward Tomasz Napierala 	swap_release_by_cred(decr, cred);
2843364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2853364c323SKonstantin Belousov }
2863364c323SKonstantin Belousov 
2873364c323SKonstantin Belousov void
288ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
2893364c323SKonstantin Belousov {
290ef694c1aSEdward Tomasz Napierala  	struct uidinfo *uip;
291ef694c1aSEdward Tomasz Napierala 
292ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
2933364c323SKonstantin Belousov 
2943364c323SKonstantin Belousov 	if (decr & PAGE_MASK)
2953364c323SKonstantin Belousov 		panic("swap_release: & PAGE_MASK");
2963364c323SKonstantin Belousov 
2973364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2983364c323SKonstantin Belousov 	if (swap_reserved < decr)
2993364c323SKonstantin Belousov 		panic("swap_reserved < decr");
3003364c323SKonstantin Belousov 	swap_reserved -= decr;
3013364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
3023364c323SKonstantin Belousov 
3033364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
3043364c323SKonstantin Belousov 	if (uip->ui_vmsize < decr)
3053364c323SKonstantin Belousov 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
3063364c323SKonstantin Belousov 	uip->ui_vmsize -= decr;
3073364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
3081ba5ad42SEdward Tomasz Napierala 
3091ba5ad42SEdward Tomasz Napierala 	racct_sub_cred(cred, RACCT_SWAP, decr);
3103364c323SKonstantin Belousov }
3113364c323SKonstantin Belousov 
3121c7c3c6aSMatthew Dillon #define SWM_FREE	0x02	/* free, period			*/
3131c7c3c6aSMatthew Dillon #define SWM_POP		0x04	/* pop out			*/
31426f9a767SRodney W. Grimes 
315f08b3099SKonstantin Belousov static int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
3167dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
3171c7c3c6aSMatthew Dillon static int nsw_rcount;		/* free read buffers			*/
318327f4e83SMatthew Dillon static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
319327f4e83SMatthew Dillon static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
320327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
321327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
3221c7c3c6aSMatthew Dillon 
32389c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
3244c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
3254c36e917SKonstantin Belousov     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
3264c36e917SKonstantin Belousov     "Maximum running async swap ops");
327d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS);
328d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD |
329d027ed2eSAlan Cox     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A",
330d027ed2eSAlan Cox     "Swap Fragmentation Info");
33189c241d1SGleb Smirnoff 
3320cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
333327f4e83SMatthew Dillon 
3341c7c3c6aSMatthew Dillon /*
3351c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
3361c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
3371c7c3c6aSMatthew Dillon  */
3381c7c3c6aSMatthew Dillon 
3391c7c3c6aSMatthew Dillon #define NOBJLISTS		8
3401c7c3c6aSMatthew Dillon 
3411c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
342af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
3431c7c3c6aSMatthew Dillon 
3441c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
345f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone;
346f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone;
3471c7c3c6aSMatthew Dillon 
3481c7c3c6aSMatthew Dillon /*
3491c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
3501c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
3511c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
3521c7c3c6aSMatthew Dillon  */
353ff98689dSBruce Evans static vm_object_t
35411caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
3553364c323SKonstantin Belousov 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
35611caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
357b0cd2017SGleb Smirnoff static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
358b0cd2017SGleb Smirnoff     int *);
359b0cd2017SGleb Smirnoff static int	swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
360b0cd2017SGleb Smirnoff     int *, pgo_getpages_iodone_t, void *);
361751221fdSPoul-Henning Kamp static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
3625ea4972cSAlan Cox static boolean_t
3635ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
36411caded3SAlfred Perlstein static void	swap_pager_init(void);
36511caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
366b3fed13eSDavid Schultz static void	swap_pager_swapoff(struct swdevt *sp);
367f708ef1bSPoul-Henning Kamp 
368df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
369e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
370e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
371e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
372e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
37390effb23SGleb Smirnoff 	.pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)		*/
374e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
375e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
376e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
377df8bae1dSRodney W. Grimes };
378df8bae1dSRodney W. Grimes 
3791c7c3c6aSMatthew Dillon /*
3801c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
3811c7c3c6aSMatthew Dillon  * internal.
3821c7c3c6aSMatthew Dillon  */
383e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
384e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
38526f9a767SRodney W. Grimes 
38607c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
38707c348eaSAlan Cox     "Maximum size of a swap block in pages");
388cee313c4SRobert Watson 
389a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
39011caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
39188ad2d7bSKonstantin Belousov static int	swapongeom(struct vnode *);
39259efee01SPoul-Henning Kamp static int	swaponvp(struct thread *, struct vnode *, u_long);
39335918c55SChristian S.J. Peron static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
39424a1cce3SDavid Greenman 
3951c7c3c6aSMatthew Dillon /*
3961c7c3c6aSMatthew Dillon  * Swap bitmap functions
3971c7c3c6aSMatthew Dillon  */
398a5edd34aSPoul-Henning Kamp static void	swp_pager_freeswapspace(daddr_t blk, int npages);
399a5edd34aSPoul-Henning Kamp static daddr_t	swp_pager_getswapspace(int npages);
4001c7c3c6aSMatthew Dillon 
4011c7c3c6aSMatthew Dillon /*
4021c7c3c6aSMatthew Dillon  * Metadata functions
4031c7c3c6aSMatthew Dillon  */
40411caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
4052e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
40611caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
40711caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
4081c7c3c6aSMatthew Dillon 
409f425ab8eSKonstantin Belousov static void *
410f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree)
411f425ab8eSKonstantin Belousov {
412f425ab8eSKonstantin Belousov 
413f425ab8eSKonstantin Belousov 	return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ?
414f425ab8eSKonstantin Belousov 	    M_USE_RESERVE : 0)));
415f425ab8eSKonstantin Belousov }
416f425ab8eSKonstantin Belousov 
417f425ab8eSKonstantin Belousov static void
418f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node)
419f425ab8eSKonstantin Belousov {
420f425ab8eSKonstantin Belousov 
421f425ab8eSKonstantin Belousov 	uma_zfree(swpctrie_zone, node);
422f425ab8eSKonstantin Belousov }
423f425ab8eSKonstantin Belousov 
424f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free);
425f425ab8eSKonstantin Belousov 
4261c7c3c6aSMatthew Dillon /*
4271c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
4281c7c3c6aSMatthew Dillon  *
42920d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
43020d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
43120d3034fSMatthew Dillon  *
43220d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
4331c7c3c6aSMatthew Dillon  *
4341c7c3c6aSMatthew Dillon  *	No restrictions on call
4351c7c3c6aSMatthew Dillon  *	This routine may not block.
4361c7c3c6aSMatthew Dillon  */
437a5edd34aSPoul-Henning Kamp static void
4382f249180SPoul-Henning Kamp swp_sizecheck(void)
4390d94caffSDavid Greenman {
44023955314SAlfred Perlstein 
4418f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
44220d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
4431af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
44420d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
4452b0d37a4SMatthew Dillon 		}
44620d3034fSMatthew Dillon 	} else {
44726f9a767SRodney W. Grimes 		swap_pager_full = 0;
4488f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
44920d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
45026f9a767SRodney W. Grimes 	}
4511c7c3c6aSMatthew Dillon }
4521c7c3c6aSMatthew Dillon 
4531c7c3c6aSMatthew Dillon /*
4541c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
4551c7c3c6aSMatthew Dillon  *
4561c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
4571c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
4581c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
4591c7c3c6aSMatthew Dillon  */
460f5a12711SPoul-Henning Kamp static void
4612f249180SPoul-Henning Kamp swap_pager_init(void)
462df8bae1dSRodney W. Grimes {
4631c7c3c6aSMatthew Dillon 	/*
4641c7c3c6aSMatthew Dillon 	 * Initialize object lists
4651c7c3c6aSMatthew Dillon 	 */
4661c7c3c6aSMatthew Dillon 	int i;
4671c7c3c6aSMatthew Dillon 
4681c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
4691c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
47020da9c2eSPoul-Henning Kamp 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
47115719273SKonstantin Belousov 	sx_init(&sw_alloc_sx, "swspsx");
47204533e1eSKonstantin Belousov 	sx_init(&swdev_syscall_lock, "swsysc");
4731c7c3c6aSMatthew Dillon }
47426f9a767SRodney W. Grimes 
475df8bae1dSRodney W. Grimes /*
4761c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
4771c7c3c6aSMatthew Dillon  *
4781c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
4791c7c3c6aSMatthew Dillon  *	its main loop.
480df8bae1dSRodney W. Grimes  */
48124a1cce3SDavid Greenman void
4822f249180SPoul-Henning Kamp swap_pager_swap_init(void)
483df8bae1dSRodney W. Grimes {
48461203277SDag-Erling Smørgrav 	unsigned long n, n2;
4850d94caffSDavid Greenman 
48626f9a767SRodney W. Grimes 	/*
4871c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
4881c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
4891c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
4901c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
4911c7c3c6aSMatthew Dillon 	 *
492327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
4931c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
4941c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
4951c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
496327f4e83SMatthew Dillon 	 *
497327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
498327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
499327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
500327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
501327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
502327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
503327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
504327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
505327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
506327f4e83SMatthew Dillon 	 * So it all works out pretty well.
50726f9a767SRodney W. Grimes 	 */
508ad3cce20SMatthew Dillon 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
509327f4e83SMatthew Dillon 
5106d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
5111c7c3c6aSMatthew Dillon 	nsw_rcount = (nswbuf + 1) / 2;
512327f4e83SMatthew Dillon 	nsw_wcount_sync = (nswbuf + 3) / 4;
513327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
514327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
5156d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
51624a1cce3SDavid Greenman 
5171c7c3c6aSMatthew Dillon 	/*
518f425ab8eSKonstantin Belousov 	 * Initialize our zone, guessing on the number we need based
519f425ab8eSKonstantin Belousov 	 * on the number of pages in the system.
5201c7c3c6aSMatthew Dillon 	 */
52144f1c916SBryan Drewery 	n = vm_cnt.v_page_count / 2;
522f425ab8eSKonstantin Belousov 	if (maxswzone && n > maxswzone / sizeof(struct swblk))
523f425ab8eSKonstantin Belousov 		n = maxswzone / sizeof(struct swblk);
524f425ab8eSKonstantin Belousov 	swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL,
525f425ab8eSKonstantin Belousov 	    pctrie_zone_init, NULL, UMA_ALIGN_PTR,
526f425ab8eSKonstantin Belousov 	    UMA_ZONE_NOFREE | UMA_ZONE_VM);
527f425ab8eSKonstantin Belousov 	if (swpctrie_zone == NULL)
528f425ab8eSKonstantin Belousov 		panic("failed to create swap pctrie zone.");
529f425ab8eSKonstantin Belousov 	swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL,
530f425ab8eSKonstantin Belousov 	    NULL, NULL, _Alignof(struct swblk) - 1,
531f425ab8eSKonstantin Belousov 	    UMA_ZONE_NOFREE | UMA_ZONE_VM);
532f425ab8eSKonstantin Belousov 	if (swblk_zone == NULL)
533f425ab8eSKonstantin Belousov 		panic("failed to create swap blk zone.");
53422a5e6b9SDag-Erling Smørgrav 	n2 = n;
5358355f576SJeff Roberson 	do {
536f425ab8eSKonstantin Belousov 		if (uma_zone_reserve_kva(swblk_zone, n))
53761ce6eeeSAlfred Perlstein 			break;
53861ce6eeeSAlfred Perlstein 		/*
53961ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
54061ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
54161ce6eeeSAlfred Perlstein 		 */
54261ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
54361ce6eeeSAlfred Perlstein 	} while (n > 0);
54421cd6e62SSeigo Tanimura 	if (n2 != n)
545f425ab8eSKonstantin Belousov 		printf("Swap blk zone entries reduced from %lu to %lu.\n",
546f425ab8eSKonstantin Belousov 		    n2, n);
54761203277SDag-Erling Smørgrav 	swap_maxpages = n * SWAP_META_PAGES;
548f425ab8eSKonstantin Belousov 	swzone = n * sizeof(struct swblk);
549f425ab8eSKonstantin Belousov 	if (!uma_zone_reserve_kva(swpctrie_zone, n))
550f425ab8eSKonstantin Belousov 		printf("Cannot reserve swap pctrie zone, "
551f425ab8eSKonstantin Belousov 		    "reduce kern.maxswzone.\n");
55224a1cce3SDavid Greenman }
55324a1cce3SDavid Greenman 
554eb4d6a1bSKonstantin Belousov static vm_object_t
555eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size,
556eb4d6a1bSKonstantin Belousov     vm_ooffset_t offset)
557eb4d6a1bSKonstantin Belousov {
558eb4d6a1bSKonstantin Belousov 	vm_object_t object;
559eb4d6a1bSKonstantin Belousov 
560eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
561eb4d6a1bSKonstantin Belousov 		if (!swap_reserve_by_cred(size, cred))
562eb4d6a1bSKonstantin Belousov 			return (NULL);
563eb4d6a1bSKonstantin Belousov 		crhold(cred);
564eb4d6a1bSKonstantin Belousov 	}
565f425ab8eSKonstantin Belousov 
566f425ab8eSKonstantin Belousov 	/*
567f425ab8eSKonstantin Belousov 	 * The un_pager.swp.swp_blks trie is initialized by
568f425ab8eSKonstantin Belousov 	 * vm_object_allocate() to ensure the correct order of
569f425ab8eSKonstantin Belousov 	 * visibility to other threads.
570f425ab8eSKonstantin Belousov 	 */
571eb4d6a1bSKonstantin Belousov 	object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset +
572eb4d6a1bSKonstantin Belousov 	    PAGE_MASK + size));
573f425ab8eSKonstantin Belousov 
574eb4d6a1bSKonstantin Belousov 	object->handle = handle;
575eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
576eb4d6a1bSKonstantin Belousov 		object->cred = cred;
577eb4d6a1bSKonstantin Belousov 		object->charge = size;
578eb4d6a1bSKonstantin Belousov 	}
579eb4d6a1bSKonstantin Belousov 	return (object);
580eb4d6a1bSKonstantin Belousov }
581eb4d6a1bSKonstantin Belousov 
58224a1cce3SDavid Greenman /*
5831c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
5841c7c3c6aSMatthew Dillon  *			its metadata structures.
5851c7c3c6aSMatthew Dillon  *
5861c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
587eb4d6a1bSKonstantin Belousov  *	OBJT_SWAP object.
5881c7c3c6aSMatthew Dillon  *
589eb4d6a1bSKonstantin Belousov  *	This routine must ensure that no live duplicate is created for
590eb4d6a1bSKonstantin Belousov  *	the named object request, which is protected against by
591eb4d6a1bSKonstantin Belousov  *	holding the sw_alloc_sx lock in case handle != NULL.
59224a1cce3SDavid Greenman  */
593f5a12711SPoul-Henning Kamp static vm_object_t
5946cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
5953364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
59624a1cce3SDavid Greenman {
59724a1cce3SDavid Greenman 	vm_object_t object;
5982f7af3dbSAlan Cox 
599eb4d6a1bSKonstantin Belousov 	if (handle != NULL) {
6001c7c3c6aSMatthew Dillon 		/*
6011c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
6021c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
6031c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
6041c7c3c6aSMatthew Dillon 		 * of the handle.
6051c7c3c6aSMatthew Dillon 		 */
6060cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
6071c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
608b5e8f167SAlan Cox 		if (object == NULL) {
609eb4d6a1bSKonstantin Belousov 			object = swap_pager_alloc_init(handle, cred, size,
610eb4d6a1bSKonstantin Belousov 			    offset);
611eb4d6a1bSKonstantin Belousov 			if (object != NULL) {
612eb4d6a1bSKonstantin Belousov 				TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
613eb4d6a1bSKonstantin Belousov 				    object, pager_object_list);
6143364c323SKonstantin Belousov 			}
61524a1cce3SDavid Greenman 		}
6160cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
61724a1cce3SDavid Greenman 	} else {
618eb4d6a1bSKonstantin Belousov 		object = swap_pager_alloc_init(handle, cred, size, offset);
61924a1cce3SDavid Greenman 	}
62024a1cce3SDavid Greenman 	return (object);
621df8bae1dSRodney W. Grimes }
622df8bae1dSRodney W. Grimes 
62326f9a767SRodney W. Grimes /*
6241c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
6251c7c3c6aSMatthew Dillon  *
6261c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
6271c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
6281c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
6291c7c3c6aSMatthew Dillon  *	about to be destroyed.
6301c7c3c6aSMatthew Dillon  *
63115523cf7SKonstantin Belousov  *	The object must be locked.
63226f9a767SRodney W. Grimes  */
633df8bae1dSRodney W. Grimes static void
6342f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object)
63526f9a767SRodney W. Grimes {
6364dcc5c2dSMatthew Dillon 
637eb4d6a1bSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
638eb4d6a1bSKonstantin Belousov 	KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
639eb4d6a1bSKonstantin Belousov 
64026f9a767SRodney W. Grimes 	/*
6411c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
6421c7c3c6aSMatthew Dillon 	 * pageout completion.
64326f9a767SRodney W. Grimes 	 */
644bd228075SAlan Cox 	if (object->handle != NULL) {
645eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
646eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
647eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(object->handle), object,
648eb4d6a1bSKonstantin Belousov 		    pager_object_list);
649eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
650eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(object);
651bd228075SAlan Cox 	}
6521c7c3c6aSMatthew Dillon 
6531c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
6541c7c3c6aSMatthew Dillon 
6551c7c3c6aSMatthew Dillon 	/*
6561c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
6571c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
6581c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
6591c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
6601c7c3c6aSMatthew Dillon 	 */
6611c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
662e735691bSJohn Baldwin 	object->handle = NULL;
663e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
6641c7c3c6aSMatthew Dillon }
6651c7c3c6aSMatthew Dillon 
6661c7c3c6aSMatthew Dillon /************************************************************************
6671c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
6681c7c3c6aSMatthew Dillon  ************************************************************************/
6691c7c3c6aSMatthew Dillon 
6701c7c3c6aSMatthew Dillon /*
6711c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
6721c7c3c6aSMatthew Dillon  *
6731c7c3c6aSMatthew Dillon  *	Allocate swap for the requested number of pages.  The starting
6741c7c3c6aSMatthew Dillon  *	swap block number (a page index) is returned or SWAPBLK_NONE
6751c7c3c6aSMatthew Dillon  *	if the allocation failed.
6761c7c3c6aSMatthew Dillon  *
6771c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
6781c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
6791c7c3c6aSMatthew Dillon  *
68015523cf7SKonstantin Belousov  *	This routine may not sleep.
6818f60c087SPoul-Henning Kamp  *
6828f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
6831c7c3c6aSMatthew Dillon  */
684a5edd34aSPoul-Henning Kamp static daddr_t
6852f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages)
6861c7c3c6aSMatthew Dillon {
6871c7c3c6aSMatthew Dillon 	daddr_t blk;
6888f60c087SPoul-Henning Kamp 	struct swdevt *sp;
6898f60c087SPoul-Henning Kamp 	int i;
6901c7c3c6aSMatthew Dillon 
6918f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
69220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
6938f60c087SPoul-Henning Kamp 	sp = swdevhd;
6948f60c087SPoul-Henning Kamp 	for (i = 0; i < nswapdev; i++) {
6958f60c087SPoul-Henning Kamp 		if (sp == NULL)
6968f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
6978f60c087SPoul-Henning Kamp 		if (!(sp->sw_flags & SW_CLOSING)) {
6988f60c087SPoul-Henning Kamp 			blk = blist_alloc(sp->sw_blist, npages);
6998f60c087SPoul-Henning Kamp 			if (blk != SWAPBLK_NONE) {
7008f60c087SPoul-Henning Kamp 				blk += sp->sw_first;
7018f60c087SPoul-Henning Kamp 				sp->sw_used += npages;
702d05bc129SAlan Cox 				swap_pager_avail -= npages;
7038f60c087SPoul-Henning Kamp 				swp_sizecheck();
7048f60c087SPoul-Henning Kamp 				swdevhd = TAILQ_NEXT(sp, sw_list);
705d05bc129SAlan Cox 				goto done;
7068f60c087SPoul-Henning Kamp 			}
7078f60c087SPoul-Henning Kamp 		}
7088f60c087SPoul-Henning Kamp 		sp = TAILQ_NEXT(sp, sw_list);
7098f60c087SPoul-Henning Kamp 	}
7102b0d37a4SMatthew Dillon 	if (swap_pager_full != 2) {
71120da9c2eSPoul-Henning Kamp 		printf("swap_pager_getswapspace(%d): failed\n", npages);
7122b0d37a4SMatthew Dillon 		swap_pager_full = 2;
71320d3034fSMatthew Dillon 		swap_pager_almost_full = 1;
7142b0d37a4SMatthew Dillon 	}
7158f60c087SPoul-Henning Kamp 	swdevhd = NULL;
716d05bc129SAlan Cox done:
717d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
7181c7c3c6aSMatthew Dillon 	return (blk);
71926f9a767SRodney W. Grimes }
72026f9a767SRodney W. Grimes 
721b3fed13eSDavid Schultz static int
722b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp)
7238f60c087SPoul-Henning Kamp {
7248f60c087SPoul-Henning Kamp 
725b3fed13eSDavid Schultz 	return (blk >= sp->sw_first && blk < sp->sw_end);
7268f60c087SPoul-Henning Kamp }
7278f60c087SPoul-Henning Kamp 
7284b03903aSPoul-Henning Kamp static void
7294b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp)
7304b03903aSPoul-Henning Kamp {
7314b03903aSPoul-Henning Kamp 	struct swdevt *sp;
7324b03903aSPoul-Henning Kamp 
73320da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7344b03903aSPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7354b03903aSPoul-Henning Kamp 		if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) {
73620da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
7372cc718a1SKonstantin Belousov 			if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
7382cc718a1SKonstantin Belousov 			    unmapped_buf_allowed) {
7392cc718a1SKonstantin Belousov 				bp->b_data = unmapped_buf;
7402cc718a1SKonstantin Belousov 				bp->b_offset = 0;
7412cc718a1SKonstantin Belousov 			} else {
7422cc718a1SKonstantin Belousov 				pmap_qenter((vm_offset_t)bp->b_data,
7432cc718a1SKonstantin Belousov 				    &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
7442cc718a1SKonstantin Belousov 			}
7454b03903aSPoul-Henning Kamp 			sp->sw_strategy(bp, sp);
7464b03903aSPoul-Henning Kamp 			return;
7474b03903aSPoul-Henning Kamp 		}
7484b03903aSPoul-Henning Kamp 	}
74920da9c2eSPoul-Henning Kamp 	panic("Swapdev not found");
7504b03903aSPoul-Henning Kamp }
7514b03903aSPoul-Henning Kamp 
7528f60c087SPoul-Henning Kamp 
75326f9a767SRodney W. Grimes /*
7541c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
7551c7c3c6aSMatthew Dillon  *
7561c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
7571c7c3c6aSMatthew Dillon  *
75815523cf7SKonstantin Belousov  *	This routine may not sleep.
75926f9a767SRodney W. Grimes  */
760a5edd34aSPoul-Henning Kamp static void
7618f60c087SPoul-Henning Kamp swp_pager_freeswapspace(daddr_t blk, int npages)
7620d94caffSDavid Greenman {
7638f60c087SPoul-Henning Kamp 	struct swdevt *sp;
76492da00bbSMatthew Dillon 
7657645e885SAlan Cox 	mtx_lock(&sw_dev_mtx);
7667645e885SAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7677645e885SAlan Cox 		if (blk >= sp->sw_first && blk < sp->sw_end) {
76892da00bbSMatthew Dillon 			sp->sw_used -= npages;
76992da00bbSMatthew Dillon 			/*
7707645e885SAlan Cox 			 * If we are attempting to stop swapping on
7717645e885SAlan Cox 			 * this device, we don't want to mark any
7727645e885SAlan Cox 			 * blocks free lest they be reused.
77392da00bbSMatthew Dillon 			 */
7747645e885SAlan Cox 			if ((sp->sw_flags & SW_CLOSING) == 0) {
7757645e885SAlan Cox 				blist_free(sp->sw_blist, blk - sp->sw_first,
7767645e885SAlan Cox 				    npages);
7778f60c087SPoul-Henning Kamp 				swap_pager_avail += npages;
7781c7c3c6aSMatthew Dillon 				swp_sizecheck();
77926f9a767SRodney W. Grimes 			}
7807645e885SAlan Cox 			mtx_unlock(&sw_dev_mtx);
7817645e885SAlan Cox 			return;
7827645e885SAlan Cox 		}
7837645e885SAlan Cox 	}
7847645e885SAlan Cox 	panic("Swapdev not found");
7857645e885SAlan Cox }
7861c7c3c6aSMatthew Dillon 
78726f9a767SRodney W. Grimes /*
788d027ed2eSAlan Cox  * SYSCTL_SWAP_FRAGMENTATION() -	produce raw swap space stats
789d027ed2eSAlan Cox  */
790d027ed2eSAlan Cox static int
791d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS)
792d027ed2eSAlan Cox {
793d027ed2eSAlan Cox 	struct sbuf sbuf;
794d027ed2eSAlan Cox 	struct swdevt *sp;
795d027ed2eSAlan Cox 	const char *devname;
796d027ed2eSAlan Cox 	int error;
797d027ed2eSAlan Cox 
798d027ed2eSAlan Cox 	error = sysctl_wire_old_buffer(req, 0);
799d027ed2eSAlan Cox 	if (error != 0)
800d027ed2eSAlan Cox 		return (error);
801d027ed2eSAlan Cox 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
802d027ed2eSAlan Cox 	mtx_lock(&sw_dev_mtx);
803d027ed2eSAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
804d027ed2eSAlan Cox 		if (vn_isdisk(sp->sw_vp, NULL))
805d027ed2eSAlan Cox 			devname = devtoname(sp->sw_vp->v_rdev);
806d027ed2eSAlan Cox 		else
807d027ed2eSAlan Cox 			devname = "[file]";
808d027ed2eSAlan Cox 		sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname);
809d027ed2eSAlan Cox 		blist_stats(sp->sw_blist, &sbuf);
810d027ed2eSAlan Cox 	}
811d027ed2eSAlan Cox 	mtx_unlock(&sw_dev_mtx);
812d027ed2eSAlan Cox 	error = sbuf_finish(&sbuf);
813d027ed2eSAlan Cox 	sbuf_delete(&sbuf);
814d027ed2eSAlan Cox 	return (error);
815d027ed2eSAlan Cox }
816d027ed2eSAlan Cox 
817d027ed2eSAlan Cox /*
8181c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
8191c7c3c6aSMatthew Dillon  *				range within an object.
8201c7c3c6aSMatthew Dillon  *
8211c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
8221c7c3c6aSMatthew Dillon  *
8231c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
8241c7c3c6aSMatthew Dillon  *
8251c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
8261c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
8271c7c3c6aSMatthew Dillon  *	we should be ok.
828c25673ffSAttilio Rao  *
829c25673ffSAttilio Rao  *	The object must be locked.
83026f9a767SRodney W. Grimes  */
83126f9a767SRodney W. Grimes void
8322f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
83326f9a767SRodney W. Grimes {
83423955314SAlfred Perlstein 
8351c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
8364dcc5c2dSMatthew Dillon }
8374dcc5c2dSMatthew Dillon 
8384dcc5c2dSMatthew Dillon /*
8394dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
8404dcc5c2dSMatthew Dillon  *
8414dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
84256ce850bSKonstantin Belousov  *	swap blocks are not zeroed.  Any previous swap assignment is destroyed.
8434dcc5c2dSMatthew Dillon  *
8444dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
8454dcc5c2dSMatthew Dillon  */
8464dcc5c2dSMatthew Dillon int
8474dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
8484dcc5c2dSMatthew Dillon {
8494dcc5c2dSMatthew Dillon 	int n = 0;
8504dcc5c2dSMatthew Dillon 	daddr_t blk = SWAPBLK_NONE;
8514dcc5c2dSMatthew Dillon 	vm_pindex_t beg = start;	/* save start index */
8524dcc5c2dSMatthew Dillon 
85389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
8544dcc5c2dSMatthew Dillon 	while (size) {
8554dcc5c2dSMatthew Dillon 		if (n == 0) {
8564dcc5c2dSMatthew Dillon 			n = BLIST_MAX_ALLOC;
8574dcc5c2dSMatthew Dillon 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
8584dcc5c2dSMatthew Dillon 				n >>= 1;
8594dcc5c2dSMatthew Dillon 				if (n == 0) {
8604dcc5c2dSMatthew Dillon 					swp_pager_meta_free(object, beg, start - beg);
86189f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
8624dcc5c2dSMatthew Dillon 					return (-1);
8634dcc5c2dSMatthew Dillon 				}
8644dcc5c2dSMatthew Dillon 			}
8654dcc5c2dSMatthew Dillon 		}
8664dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, start, blk);
8674dcc5c2dSMatthew Dillon 		--size;
8684dcc5c2dSMatthew Dillon 		++start;
8694dcc5c2dSMatthew Dillon 		++blk;
8704dcc5c2dSMatthew Dillon 		--n;
8714dcc5c2dSMatthew Dillon 	}
8724dcc5c2dSMatthew Dillon 	swp_pager_meta_free(object, start, n);
87389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
8744dcc5c2dSMatthew Dillon 	return (0);
87526f9a767SRodney W. Grimes }
87626f9a767SRodney W. Grimes 
8770a47b48bSJohn Dyson /*
8781c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
8791c7c3c6aSMatthew Dillon  *			and destroy the source.
8801c7c3c6aSMatthew Dillon  *
8811c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
8821c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
8831c7c3c6aSMatthew Dillon  *	we keep the destination's.
8841c7c3c6aSMatthew Dillon  *
88515523cf7SKonstantin Belousov  *	This routine is allowed to sleep.  It may sleep allocating metadata
8861c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
8871c7c3c6aSMatthew Dillon  *	progress on the source.
8881c7c3c6aSMatthew Dillon  *
8891c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
8901c7c3c6aSMatthew Dillon  *
8911c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
8921c7c3c6aSMatthew Dillon  *
89315523cf7SKonstantin Belousov  *	The source and destination objects must be locked.
89415523cf7SKonstantin Belousov  *	Both object locks may temporarily be released.
89526f9a767SRodney W. Grimes  */
89626f9a767SRodney W. Grimes void
8972f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
8982f249180SPoul-Henning Kamp     vm_pindex_t offset, int destroysource)
89926f9a767SRodney W. Grimes {
900a316d390SJohn Dyson 	vm_pindex_t i;
9014dcc5c2dSMatthew Dillon 
90289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(srcobject);
90389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(dstobject);
9040cddd8f0SMatthew Dillon 
90526f9a767SRodney W. Grimes 	/*
9061c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
9071c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
90826f9a767SRodney W. Grimes 	 */
909eb4d6a1bSKonstantin Belousov 	if (destroysource && srcobject->handle != NULL) {
910eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(srcobject, 1);
911eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(srcobject);
912eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(dstobject, 1);
913eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(dstobject);
914eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
915eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
916eb4d6a1bSKonstantin Belousov 		    pager_object_list);
917eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
918eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(dstobject);
919eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(dstobject);
920eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(srcobject);
921eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(srcobject);
922bd228075SAlan Cox 	}
92326f9a767SRodney W. Grimes 
9241c7c3c6aSMatthew Dillon 	/*
9251c7c3c6aSMatthew Dillon 	 * transfer source to destination.
9261c7c3c6aSMatthew Dillon 	 */
9271c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
9281c7c3c6aSMatthew Dillon 		daddr_t dstaddr;
9291c7c3c6aSMatthew Dillon 
9301c7c3c6aSMatthew Dillon 		/*
9311c7c3c6aSMatthew Dillon 		 * Locate (without changing) the swapblk on the destination,
9321c7c3c6aSMatthew Dillon 		 * unless it is invalid in which case free it silently, or
9331c7c3c6aSMatthew Dillon 		 * if the destination is a resident page, in which case the
9341c7c3c6aSMatthew Dillon 		 * source is thrown away.
9351c7c3c6aSMatthew Dillon 		 */
9361c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
9371c7c3c6aSMatthew Dillon 
9381c7c3c6aSMatthew Dillon 		if (dstaddr == SWAPBLK_NONE) {
9391c7c3c6aSMatthew Dillon 			/*
9401c7c3c6aSMatthew Dillon 			 * Destination has no swapblk and is not resident,
9411c7c3c6aSMatthew Dillon 			 * copy source.
9421c7c3c6aSMatthew Dillon 			 */
9431c7c3c6aSMatthew Dillon 			daddr_t srcaddr;
9441c7c3c6aSMatthew Dillon 
9451c7c3c6aSMatthew Dillon 			srcaddr = swp_pager_meta_ctl(
9461c7c3c6aSMatthew Dillon 			    srcobject,
9471c7c3c6aSMatthew Dillon 			    i + offset,
9481c7c3c6aSMatthew Dillon 			    SWM_POP
9491c7c3c6aSMatthew Dillon 			);
9501c7c3c6aSMatthew Dillon 
951ee3dc7d7SAlan Cox 			if (srcaddr != SWAPBLK_NONE) {
952c7c8dd7eSAlan Cox 				/*
953c7c8dd7eSAlan Cox 				 * swp_pager_meta_build() can sleep.
954c7c8dd7eSAlan Cox 				 */
955c7c8dd7eSAlan Cox 				vm_object_pip_add(srcobject, 1);
95689f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(srcobject);
957c7c8dd7eSAlan Cox 				vm_object_pip_add(dstobject, 1);
9584dcc5c2dSMatthew Dillon 				swp_pager_meta_build(dstobject, i, srcaddr);
959c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(dstobject);
96089f6b863SAttilio Rao 				VM_OBJECT_WLOCK(srcobject);
961c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(srcobject);
962ee3dc7d7SAlan Cox 			}
9631c7c3c6aSMatthew Dillon 		} else {
9641c7c3c6aSMatthew Dillon 			/*
9651c7c3c6aSMatthew Dillon 			 * Destination has valid swapblk or it is represented
9661c7c3c6aSMatthew Dillon 			 * by a resident page.  We destroy the sourceblock.
9671c7c3c6aSMatthew Dillon 			 */
9681c7c3c6aSMatthew Dillon 
9691c7c3c6aSMatthew Dillon 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
9701c7c3c6aSMatthew Dillon 		}
97126f9a767SRodney W. Grimes 	}
97226f9a767SRodney W. Grimes 
97326f9a767SRodney W. Grimes 	/*
9741c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
9751c7c3c6aSMatthew Dillon 	 *
976763df3ecSPedro F. Giffuni 	 * We have to revert the type to OBJT_DEFAULT so we do not accidentally
9771c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
97826f9a767SRodney W. Grimes 	 */
979c0877f10SJohn Dyson 	if (destroysource) {
9801c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
9811c7c3c6aSMatthew Dillon 		/*
9821c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
9831c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
984956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
9851c7c3c6aSMatthew Dillon 		 * queues.
9861c7c3c6aSMatthew Dillon 		 */
9871c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
988c0877f10SJohn Dyson 	}
98926f9a767SRodney W. Grimes }
99026f9a767SRodney W. Grimes 
991df8bae1dSRodney W. Grimes /*
9921c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
9931c7c3c6aSMatthew Dillon  *				the requested page.
9941c7c3c6aSMatthew Dillon  *
9951c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
9961c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
9971c7c3c6aSMatthew Dillon  *
9981c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
999915d1b71SMark Johnston  *	store exists before and after the requested page.
1000df8bae1dSRodney W. Grimes  */
10015ea4972cSAlan Cox static boolean_t
1002915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
1003915d1b71SMark Johnston     int *after)
100426f9a767SRodney W. Grimes {
1005915d1b71SMark Johnston 	daddr_t blk, blk0;
1006915d1b71SMark Johnston 	int i;
100726f9a767SRodney W. Grimes 
1008c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
1009915d1b71SMark Johnston 
10101c7c3c6aSMatthew Dillon 	/*
10111c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
10121c7c3c6aSMatthew Dillon 	 */
10131c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
10144dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
10151c7c3c6aSMatthew Dillon 		if (before)
101624a1cce3SDavid Greenman 			*before = 0;
10171c7c3c6aSMatthew Dillon 		if (after)
101824a1cce3SDavid Greenman 			*after = 0;
101926f9a767SRodney W. Grimes 		return (FALSE);
102026f9a767SRodney W. Grimes 	}
102126f9a767SRodney W. Grimes 
102226f9a767SRodney W. Grimes 	/*
10231c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
1024e47ed70bSJohn Dyson 	 */
10251c7c3c6aSMatthew Dillon 	if (before != NULL) {
1026915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10271c7c3c6aSMatthew Dillon 			if (i > pindex)
10281c7c3c6aSMatthew Dillon 				break;
10291c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
10301c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
10311c7c3c6aSMatthew Dillon 				break;
1032ffc82b0aSJohn Dyson 		}
1033915d1b71SMark Johnston 		*before = i - 1;
103426f9a767SRodney W. Grimes 	}
103526f9a767SRodney W. Grimes 
103626f9a767SRodney W. Grimes 	/*
10371c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
103826f9a767SRodney W. Grimes 	 */
10391c7c3c6aSMatthew Dillon 	if (after != NULL) {
1040915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10411c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
10421c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
10431c7c3c6aSMatthew Dillon 				break;
104426f9a767SRodney W. Grimes 		}
1045915d1b71SMark Johnston 		*after = i - 1;
10461c7c3c6aSMatthew Dillon 	}
10471c7c3c6aSMatthew Dillon 	return (TRUE);
10481c7c3c6aSMatthew Dillon }
10491c7c3c6aSMatthew Dillon 
10501c7c3c6aSMatthew Dillon /*
10511c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
10521c7c3c6aSMatthew Dillon  *
10531c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
10541c7c3c6aSMatthew Dillon  *	not, from the page.
10551c7c3c6aSMatthew Dillon  *
10561c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
10571c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
10581c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
10591c7c3c6aSMatthew Dillon  *
10601c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
10611c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
10621c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
10631c7c3c6aSMatthew Dillon  *	depends on it.
10641c7c3c6aSMatthew Dillon  *
106515523cf7SKonstantin Belousov  *	This routine may not sleep.
1066c25673ffSAttilio Rao  *
1067c25673ffSAttilio Rao  *	The object containing the page must be locked.
10681c7c3c6aSMatthew Dillon  */
10691c7c3c6aSMatthew Dillon static void
10702f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m)
10711c7c3c6aSMatthew Dillon {
10722f249180SPoul-Henning Kamp 
10731c7c3c6aSMatthew Dillon 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
10741c7c3c6aSMatthew Dillon }
10751c7c3c6aSMatthew Dillon 
10761c7c3c6aSMatthew Dillon /*
1077915d1b71SMark Johnston  * swap_pager_getpages() - bring pages in from swap
10781c7c3c6aSMatthew Dillon  *
1079915d1b71SMark Johnston  *	Attempt to page in the pages in array "m" of length "count".  The caller
1080915d1b71SMark Johnston  *	may optionally specify that additional pages preceding and succeeding
1081915d1b71SMark Johnston  *	the specified range be paged in.  The number of such pages is returned
1082915d1b71SMark Johnston  *	in the "rbehind" and "rahead" parameters, and they will be in the
1083915d1b71SMark Johnston  *	inactive queue upon return.
10841c7c3c6aSMatthew Dillon  *
1085915d1b71SMark Johnston  *	The pages in "m" must be busied and will remain busied upon return.
10861c7c3c6aSMatthew Dillon  */
1087f708ef1bSPoul-Henning Kamp static int
1088b0cd2017SGleb Smirnoff swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
1089b0cd2017SGleb Smirnoff     int *rahead)
1090df8bae1dSRodney W. Grimes {
10911c7c3c6aSMatthew Dillon 	struct buf *bp;
1092915d1b71SMark Johnston 	vm_page_t mpred, msucc, p;
1093915d1b71SMark Johnston 	vm_pindex_t pindex;
10941c7c3c6aSMatthew Dillon 	daddr_t blk;
1095dd9cb6daSMark Johnston 	int i, j, maxahead, maxbehind, reqcount, shift;
10960d94caffSDavid Greenman 
1097915d1b71SMark Johnston 	reqcount = count;
10981c7c3c6aSMatthew Dillon 
109989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
11001c7c3c6aSMatthew Dillon 	bp = getpbuf(&nsw_rcount);
1101915d1b71SMark Johnston 	VM_OBJECT_WLOCK(object);
110226f9a767SRodney W. Grimes 
1103dd9cb6daSMark Johnston 	if (!swap_pager_haspage(object, m[0]->pindex, &maxbehind, &maxahead)) {
1104915d1b71SMark Johnston 		relpbuf(bp, &nsw_rcount);
1105915d1b71SMark Johnston 		return (VM_PAGER_FAIL);
1106915d1b71SMark Johnston 	}
1107915d1b71SMark Johnston 
1108915d1b71SMark Johnston 	/*
1109915d1b71SMark Johnston 	 * Clip the readahead and readbehind ranges to exclude resident pages.
1110915d1b71SMark Johnston 	 */
1111915d1b71SMark Johnston 	if (rahead != NULL) {
1112dd9cb6daSMark Johnston 		KASSERT(reqcount - 1 <= maxahead,
1113915d1b71SMark Johnston 		    ("page count %d extends beyond swap block", reqcount));
1114dd9cb6daSMark Johnston 		*rahead = imin(*rahead, maxahead - (reqcount - 1));
1115915d1b71SMark Johnston 		pindex = m[reqcount - 1]->pindex;
1116915d1b71SMark Johnston 		msucc = TAILQ_NEXT(m[reqcount - 1], listq);
1117915d1b71SMark Johnston 		if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1118915d1b71SMark Johnston 			*rahead = msucc->pindex - pindex - 1;
1119915d1b71SMark Johnston 	}
1120915d1b71SMark Johnston 	if (rbehind != NULL) {
1121dd9cb6daSMark Johnston 		*rbehind = imin(*rbehind, maxbehind);
1122915d1b71SMark Johnston 		pindex = m[0]->pindex;
1123915d1b71SMark Johnston 		mpred = TAILQ_PREV(m[0], pglist, listq);
1124915d1b71SMark Johnston 		if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1125915d1b71SMark Johnston 			*rbehind = pindex - mpred->pindex - 1;
1126915d1b71SMark Johnston 	}
1127915d1b71SMark Johnston 
1128915d1b71SMark Johnston 	/*
1129915d1b71SMark Johnston 	 * Allocate readahead and readbehind pages.
1130915d1b71SMark Johnston 	 */
1131915d1b71SMark Johnston 	shift = rbehind != NULL ? *rbehind : 0;
1132915d1b71SMark Johnston 	if (shift != 0) {
1133915d1b71SMark Johnston 		for (i = 1; i <= shift; i++) {
1134915d1b71SMark Johnston 			p = vm_page_alloc(object, m[0]->pindex - i,
11357667839aSAlan Cox 			    VM_ALLOC_NORMAL);
1136915d1b71SMark Johnston 			if (p == NULL) {
1137915d1b71SMark Johnston 				/* Shift allocated pages to the left. */
1138915d1b71SMark Johnston 				for (j = 0; j < i - 1; j++)
1139915d1b71SMark Johnston 					bp->b_pages[j] =
1140915d1b71SMark Johnston 					    bp->b_pages[j + shift - i + 1];
1141915d1b71SMark Johnston 				break;
1142915d1b71SMark Johnston 			}
1143915d1b71SMark Johnston 			bp->b_pages[shift - i] = p;
1144915d1b71SMark Johnston 		}
1145915d1b71SMark Johnston 		shift = i - 1;
1146915d1b71SMark Johnston 		*rbehind = shift;
1147915d1b71SMark Johnston 	}
1148915d1b71SMark Johnston 	for (i = 0; i < reqcount; i++)
1149915d1b71SMark Johnston 		bp->b_pages[i + shift] = m[i];
1150915d1b71SMark Johnston 	if (rahead != NULL) {
1151915d1b71SMark Johnston 		for (i = 0; i < *rahead; i++) {
1152915d1b71SMark Johnston 			p = vm_page_alloc(object,
11537667839aSAlan Cox 			    m[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1154915d1b71SMark Johnston 			if (p == NULL)
1155915d1b71SMark Johnston 				break;
1156915d1b71SMark Johnston 			bp->b_pages[shift + reqcount + i] = p;
1157915d1b71SMark Johnston 		}
1158915d1b71SMark Johnston 		*rahead = i;
1159915d1b71SMark Johnston 	}
1160915d1b71SMark Johnston 	if (rbehind != NULL)
1161915d1b71SMark Johnston 		count += *rbehind;
1162915d1b71SMark Johnston 	if (rahead != NULL)
1163915d1b71SMark Johnston 		count += *rahead;
1164915d1b71SMark Johnston 
1165915d1b71SMark Johnston 	vm_object_pip_add(object, count);
1166915d1b71SMark Johnston 
1167915d1b71SMark Johnston 	for (i = 0; i < count; i++)
1168915d1b71SMark Johnston 		bp->b_pages[i]->oflags |= VPO_SWAPINPROG;
1169915d1b71SMark Johnston 
1170915d1b71SMark Johnston 	pindex = bp->b_pages[0]->pindex;
1171915d1b71SMark Johnston 	blk = swp_pager_meta_ctl(object, pindex, 0);
1172915d1b71SMark Johnston 	KASSERT(blk != SWAPBLK_NONE,
1173915d1b71SMark Johnston 	    ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1174915d1b71SMark Johnston 
1175915d1b71SMark Johnston 	VM_OBJECT_WUNLOCK(object);
1176915d1b71SMark Johnston 
1177915d1b71SMark Johnston 	bp->b_flags |= B_PAGING;
117821144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
11791c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1180fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1181fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
1182b0cd2017SGleb Smirnoff 	bp->b_blkno = blk;
1183b0cd2017SGleb Smirnoff 	bp->b_bcount = PAGE_SIZE * count;
1184b0cd2017SGleb Smirnoff 	bp->b_bufsize = PAGE_SIZE * count;
1185b0cd2017SGleb Smirnoff 	bp->b_npages = count;
1186915d1b71SMark Johnston 	bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1187915d1b71SMark Johnston 	bp->b_pgafter = rahead != NULL ? *rahead : 0;
118826f9a767SRodney W. Grimes 
118983c9dea1SGleb Smirnoff 	VM_CNT_INC(v_swapin);
119083c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_swappgsin, count);
11911c7c3c6aSMatthew Dillon 
11921c7c3c6aSMatthew Dillon 	/*
11931c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
11941c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
11951c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
11961c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
11971c7c3c6aSMatthew Dillon 	 *
11981c7c3c6aSMatthew Dillon 	 * The other pages in our m[] array are also released on completion,
11991c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
12001c7c3c6aSMatthew Dillon 	 *
1201c37a77eeSPoul-Henning Kamp 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
12021c7c3c6aSMatthew Dillon 	 */
1203b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
12044b03903aSPoul-Henning Kamp 	swp_pager_strategy(bp);
120526f9a767SRodney W. Grimes 
120626f9a767SRodney W. Grimes 	/*
1207915d1b71SMark Johnston 	 * Wait for the pages we want to complete.  VPO_SWAPINPROG is always
12081c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1209915d1b71SMark Johnston 	 * is set in the metadata for each page in the request.
121026f9a767SRodney W. Grimes 	 */
121189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
1212b0cd2017SGleb Smirnoff 	while ((m[0]->oflags & VPO_SWAPINPROG) != 0) {
1213b0cd2017SGleb Smirnoff 		m[0]->oflags |= VPO_SWAPSLEEP;
121483c9dea1SGleb Smirnoff 		VM_CNT_INC(v_intrans);
1215c7aebda8SAttilio Rao 		if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP,
1216c7aebda8SAttilio Rao 		    "swread", hz * 20)) {
12179bd86a98SBruce M Simpson 			printf(
1218c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1219c5690651SPoul-Henning Kamp 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
12201c7c3c6aSMatthew Dillon 		}
12211b119d9dSDavid Greenman 	}
122226f9a767SRodney W. Grimes 
122326f9a767SRodney W. Grimes 	/*
1224b0cd2017SGleb Smirnoff 	 * If we had an unrecoverable read error pages will not be valid.
122526f9a767SRodney W. Grimes 	 */
1226915d1b71SMark Johnston 	for (i = 0; i < reqcount; i++)
1227b0cd2017SGleb Smirnoff 		if (m[i]->valid != VM_PAGE_BITS_ALL)
12281c7c3c6aSMatthew Dillon 			return (VM_PAGER_ERROR);
1229b0cd2017SGleb Smirnoff 
12301c7c3c6aSMatthew Dillon 	return (VM_PAGER_OK);
12311c7c3c6aSMatthew Dillon 
12321c7c3c6aSMatthew Dillon 	/*
12331c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
12341c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
12351c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
12361c7c3c6aSMatthew Dillon 	 * to all-zero's later.
12371c7c3c6aSMatthew Dillon 	 */
1238df8bae1dSRodney W. Grimes }
1239df8bae1dSRodney W. Grimes 
12401c7c3c6aSMatthew Dillon /*
124190effb23SGleb Smirnoff  * 	swap_pager_getpages_async():
124290effb23SGleb Smirnoff  *
124390effb23SGleb Smirnoff  *	Right now this is emulation of asynchronous operation on top of
124490effb23SGleb Smirnoff  *	swap_pager_getpages().
124590effb23SGleb Smirnoff  */
124690effb23SGleb Smirnoff static int
124790effb23SGleb Smirnoff swap_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
1248b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
124990effb23SGleb Smirnoff {
125090effb23SGleb Smirnoff 	int r, error;
125190effb23SGleb Smirnoff 
1252b0cd2017SGleb Smirnoff 	r = swap_pager_getpages(object, m, count, rbehind, rahead);
125390effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
125490effb23SGleb Smirnoff 	switch (r) {
125590effb23SGleb Smirnoff 	case VM_PAGER_OK:
125690effb23SGleb Smirnoff 		error = 0;
125790effb23SGleb Smirnoff 		break;
125890effb23SGleb Smirnoff 	case VM_PAGER_ERROR:
125990effb23SGleb Smirnoff 		error = EIO;
126090effb23SGleb Smirnoff 		break;
126190effb23SGleb Smirnoff 	case VM_PAGER_FAIL:
126290effb23SGleb Smirnoff 		error = EINVAL;
126390effb23SGleb Smirnoff 		break;
126490effb23SGleb Smirnoff 	default:
1265d9328101SGleb Smirnoff 		panic("unhandled swap_pager_getpages() error %d", r);
126690effb23SGleb Smirnoff 	}
126790effb23SGleb Smirnoff 	(iodone)(arg, m, count, error);
126890effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
126990effb23SGleb Smirnoff 
127090effb23SGleb Smirnoff 	return (r);
127190effb23SGleb Smirnoff }
127290effb23SGleb Smirnoff 
127390effb23SGleb Smirnoff /*
12741c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
12751c7c3c6aSMatthew Dillon  *
12761c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
12771c7c3c6aSMatthew Dillon  *
12781c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
12791c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
12801c7c3c6aSMatthew Dillon  *
1281ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
12821c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
12831c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
12841c7c3c6aSMatthew Dillon  *	which needs work.
12851c7c3c6aSMatthew Dillon  *
12861c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
12871c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
12881c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
12891c7c3c6aSMatthew Dillon  *	completion.
12901c7c3c6aSMatthew Dillon  *
12911c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
12921c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
12931c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
12941c7c3c6aSMatthew Dillon  */
1295d635a37fSWarner Losh static void
12962f249180SPoul-Henning Kamp swap_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1297e065e87cSKonstantin Belousov     int flags, int *rtvals)
1298df8bae1dSRodney W. Grimes {
1299e065e87cSKonstantin Belousov 	int i, n;
1300e065e87cSKonstantin Belousov 	boolean_t sync;
1301df8bae1dSRodney W. Grimes 
13021c7c3c6aSMatthew Dillon 	if (count && m[0]->object != object) {
13037036145bSMaxim Konovalov 		panic("swap_pager_putpages: object mismatch %p/%p",
13041c7c3c6aSMatthew Dillon 		    object,
13051c7c3c6aSMatthew Dillon 		    m[0]->object
13061c7c3c6aSMatthew Dillon 		);
13071c7c3c6aSMatthew Dillon 	}
1308ee3dc7d7SAlan Cox 
13091c7c3c6aSMatthew Dillon 	/*
13101c7c3c6aSMatthew Dillon 	 * Step 1
13111c7c3c6aSMatthew Dillon 	 *
13121c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
13131c7c3c6aSMatthew Dillon 	 * check for bogus sysops
13141c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
13151c7c3c6aSMatthew Dillon 	 */
13164dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
13174dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
131889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1319e47ed70bSJohn Dyson 
1320e065e87cSKonstantin Belousov 	n = 0;
1321e47ed70bSJohn Dyson 	if (curproc != pageproc)
1322e47ed70bSJohn Dyson 		sync = TRUE;
1323e065e87cSKonstantin Belousov 	else
1324e065e87cSKonstantin Belousov 		sync = (flags & VM_PAGER_PUT_SYNC) != 0;
132526f9a767SRodney W. Grimes 
13261c7c3c6aSMatthew Dillon 	/*
13271c7c3c6aSMatthew Dillon 	 * Step 2
13281c7c3c6aSMatthew Dillon 	 *
13291c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
13301c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
13311c7c3c6aSMatthew Dillon 	 * successfully.
13321c7c3c6aSMatthew Dillon 	 */
13331c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
13341c7c3c6aSMatthew Dillon 		int j;
13351c7c3c6aSMatthew Dillon 		struct buf *bp;
1336a316d390SJohn Dyson 		daddr_t blk;
133726f9a767SRodney W. Grimes 
1338df8bae1dSRodney W. Grimes 		/*
13391c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1340df8bae1dSRodney W. Grimes 		 */
13411c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1342327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
13431c7c3c6aSMatthew Dillon 
134426f9a767SRodney W. Grimes 		/*
13451c7c3c6aSMatthew Dillon 		 * Get biggest block of swap we can.  If we fail, fall
13461c7c3c6aSMatthew Dillon 		 * back and try to allocate a smaller block.  Don't go
13471c7c3c6aSMatthew Dillon 		 * overboard trying to allocate space if it would overly
13481c7c3c6aSMatthew Dillon 		 * fragment swap.
134926f9a767SRodney W. Grimes 		 */
13501c7c3c6aSMatthew Dillon 		while (
13511c7c3c6aSMatthew Dillon 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
13521c7c3c6aSMatthew Dillon 		    n > 4
13531c7c3c6aSMatthew Dillon 		) {
13541c7c3c6aSMatthew Dillon 			n >>= 1;
135526f9a767SRodney W. Grimes 		}
13561c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
13574dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
13581c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
13591c7c3c6aSMatthew Dillon 			continue;
136026f9a767SRodney W. Grimes 		}
136126f9a767SRodney W. Grimes 
136226f9a767SRodney W. Grimes 		/*
13631c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
13641c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
136526f9a767SRodney W. Grimes 		 */
1366327f4e83SMatthew Dillon 		if (sync == TRUE) {
1367327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_sync);
1368327f4e83SMatthew Dillon 		} else {
1369327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_async);
137021144e3bSPoul-Henning Kamp 			bp->b_flags = B_ASYNC;
1371327f4e83SMatthew Dillon 		}
13725e04322aSPoul-Henning Kamp 		bp->b_flags |= B_PAGING;
1373912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
137426f9a767SRodney W. Grimes 
1375fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1376fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
13771c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
13781c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
13791c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1380e47ed70bSJohn Dyson 
138189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
13821c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
13831c7c3c6aSMatthew Dillon 			vm_page_t mreq = m[i+j];
13841c7c3c6aSMatthew Dillon 
13851c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
13861c7c3c6aSMatthew Dillon 			    mreq->object,
13871c7c3c6aSMatthew Dillon 			    mreq->pindex,
13884dcc5c2dSMatthew Dillon 			    blk + j
13891c7c3c6aSMatthew Dillon 			);
139087b0ab69SAlan Cox 			MPASS(mreq->dirty == VM_PAGE_BITS_ALL);
13915786be7cSAlan Cox 			mreq->oflags |= VPO_SWAPINPROG;
13921c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
13931c7c3c6aSMatthew Dillon 		}
139489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
13951c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1396a5296b05SJulian Elischer 		/*
1397a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1398a5296b05SJulian Elischer 		 */
1399a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1400a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
14011c7c3c6aSMatthew Dillon 
140283c9dea1SGleb Smirnoff 		VM_CNT_INC(v_swapout);
140383c9dea1SGleb Smirnoff 		VM_CNT_ADD(v_swappgsout, bp->b_npages);
140426f9a767SRodney W. Grimes 
140526f9a767SRodney W. Grimes 		/*
140677923df2SAlan Cox 		 * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
140777923df2SAlan Cox 		 * can call the async completion routine at the end of a
140877923df2SAlan Cox 		 * synchronous I/O operation.  Otherwise, our caller would
140977923df2SAlan Cox 		 * perform duplicate unbusy and wakeup operations on the page
141077923df2SAlan Cox 		 * and object, respectively.
141177923df2SAlan Cox 		 */
141277923df2SAlan Cox 		for (j = 0; j < n; j++)
141377923df2SAlan Cox 			rtvals[i + j] = VM_PAGER_PEND;
141477923df2SAlan Cox 
141577923df2SAlan Cox 		/*
14161c7c3c6aSMatthew Dillon 		 * asynchronous
14171c7c3c6aSMatthew Dillon 		 *
1418c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
141926f9a767SRodney W. Grimes 		 */
14201c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
14211c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
142267812eacSKirk McKusick 			BUF_KERNPROC(bp);
14234b03903aSPoul-Henning Kamp 			swp_pager_strategy(bp);
14241c7c3c6aSMatthew Dillon 			continue;
142526f9a767SRodney W. Grimes 		}
1426e47ed70bSJohn Dyson 
142726f9a767SRodney W. Grimes 		/*
14281c7c3c6aSMatthew Dillon 		 * synchronous
14291c7c3c6aSMatthew Dillon 		 *
1430c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
14311c7c3c6aSMatthew Dillon 		 */
14322c840b1fSAlan Cox 		bp->b_iodone = bdone;
14334b03903aSPoul-Henning Kamp 		swp_pager_strategy(bp);
14341c7c3c6aSMatthew Dillon 
14351c7c3c6aSMatthew Dillon 		/*
143677923df2SAlan Cox 		 * Wait for the sync I/O to complete.
143726f9a767SRodney W. Grimes 		 */
14382c840b1fSAlan Cox 		bwait(bp, PVM, "swwrt");
143977923df2SAlan Cox 
14401c7c3c6aSMatthew Dillon 		/*
14411c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
14421c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
14431c7c3c6aSMatthew Dillon 		 */
14441c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
14451c7c3c6aSMatthew Dillon 	}
144689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
14471c7c3c6aSMatthew Dillon }
14481c7c3c6aSMatthew Dillon 
14491c7c3c6aSMatthew Dillon /*
14501c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
14511c7c3c6aSMatthew Dillon  *
14521c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
14531c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
14541c7c3c6aSMatthew Dillon  *
145515523cf7SKonstantin Belousov  *	This routine may not sleep.
14561c7c3c6aSMatthew Dillon  */
14571c7c3c6aSMatthew Dillon static void
14582f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp)
14591c7c3c6aSMatthew Dillon {
14601c7c3c6aSMatthew Dillon 	int i;
14611c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
14621c7c3c6aSMatthew Dillon 
14631c7c3c6aSMatthew Dillon 	/*
14641c7c3c6aSMatthew Dillon 	 * report error
14651c7c3c6aSMatthew Dillon 	 */
1466c244d2deSPoul-Henning Kamp 	if (bp->b_ioflags & BIO_ERROR) {
14671c7c3c6aSMatthew Dillon 		printf(
14681c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
14691c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
147021144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
14711c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
14721c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
14731c7c3c6aSMatthew Dillon 		    bp->b_error
14741c7c3c6aSMatthew Dillon 		);
14751c7c3c6aSMatthew Dillon 	}
14761c7c3c6aSMatthew Dillon 
14771c7c3c6aSMatthew Dillon 	/*
147826f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
147926f9a767SRodney W. Grimes 	 */
1480fade8dd7SJeff Roberson 	if (buf_mapped(bp))
14811c7c3c6aSMatthew Dillon 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1482fade8dd7SJeff Roberson 	else
1483fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
148426f9a767SRodney W. Grimes 
148533a609ecSAlan Cox 	if (bp->b_npages) {
148633a609ecSAlan Cox 		object = bp->b_pages[0]->object;
148789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
148833a609ecSAlan Cox 	}
14892965a453SKip Macy 
149026f9a767SRodney W. Grimes 	/*
14911c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
14921c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
14931c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
14941c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
14951c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
14961c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
149726f9a767SRodney W. Grimes 	 */
14981c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
14991c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1500e47ed70bSJohn Dyson 
15015786be7cSAlan Cox 		m->oflags &= ~VPO_SWAPINPROG;
1502c7aebda8SAttilio Rao 		if (m->oflags & VPO_SWAPSLEEP) {
1503c7aebda8SAttilio Rao 			m->oflags &= ~VPO_SWAPSLEEP;
1504c7aebda8SAttilio Rao 			wakeup(&object->paging_in_progress);
1505c7aebda8SAttilio Rao 		}
1506e47ed70bSJohn Dyson 
1507c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1508ffc82b0aSJohn Dyson 			/*
15091c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
15101c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
15111c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
15121c7c3c6aSMatthew Dillon 			 * interrupt.
1513ffc82b0aSJohn Dyson 			 */
151421144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
15151c7c3c6aSMatthew Dillon 				/*
15161c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1517956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15181c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15191c7c3c6aSMatthew Dillon 				 */
15201c7c3c6aSMatthew Dillon 				m->valid = 0;
15211c7c3c6aSMatthew Dillon 			} else {
15221c7c3c6aSMatthew Dillon 				/*
15231c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
15241c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
15251c7c3c6aSMatthew Dillon 				 * then finish the I/O.
15261c7c3c6aSMatthew Dillon 				 */
1527*41e5a226SAlan Cox 				MPASS(m->dirty == VM_PAGE_BITS_ALL);
15283c4a2440SAlan Cox 				vm_page_lock(m);
15291c7c3c6aSMatthew Dillon 				vm_page_activate(m);
15303c4a2440SAlan Cox 				vm_page_unlock(m);
1531c7aebda8SAttilio Rao 				vm_page_sunbusy(m);
15321c7c3c6aSMatthew Dillon 			}
153321144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
15341c7c3c6aSMatthew Dillon 			/*
15351c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1536956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
15371c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
15381c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
15391c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
15401c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
15411c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
15421c7c3c6aSMatthew Dillon 			 */
1543016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1544016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is mapped", m));
1545016a3c93SAlan Cox 			KASSERT(m->dirty == 0,
1546016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is dirty", m));
1547915d1b71SMark Johnston 
1548b0cd2017SGleb Smirnoff 			m->valid = VM_PAGE_BITS_ALL;
1549915d1b71SMark Johnston 			if (i < bp->b_pgbefore ||
1550915d1b71SMark Johnston 			    i >= bp->b_npages - bp->b_pgafter)
1551915d1b71SMark Johnston 				vm_page_readahead_finish(m);
15521c7c3c6aSMatthew Dillon 		} else {
15531c7c3c6aSMatthew Dillon 			/*
1554016a3c93SAlan Cox 			 * For write success, clear the dirty
15551c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
15561c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
1557ebcddc72SAlan Cox 			 * A page is only written to swap after a period of
1558ebcddc72SAlan Cox 			 * inactivity.  Therefore, we do not expect it to be
1559ebcddc72SAlan Cox 			 * reused.
15601c7c3c6aSMatthew Dillon 			 */
15616031c68dSAlan Cox 			KASSERT(!pmap_page_is_write_mapped(m),
1562016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is not write"
1563016a3c93SAlan Cox 			    " protected", m));
1564c52e7044SAlan Cox 			vm_page_undirty(m);
15653c4a2440SAlan Cox 			vm_page_lock(m);
1566ebcddc72SAlan Cox 			vm_page_deactivate_noreuse(m);
15672965a453SKip Macy 			vm_page_unlock(m);
1568ebcddc72SAlan Cox 			vm_page_sunbusy(m);
15693c4a2440SAlan Cox 		}
15703c4a2440SAlan Cox 	}
157126f9a767SRodney W. Grimes 
15721c7c3c6aSMatthew Dillon 	/*
15731c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
15741c7c3c6aSMatthew Dillon 	 * pip refs on the object.
15751c7c3c6aSMatthew Dillon 	 */
15760d420ad3SAlan Cox 	if (object != NULL) {
15771c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
157889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
15790d420ad3SAlan Cox 	}
158026f9a767SRodney W. Grimes 
15811c7c3c6aSMatthew Dillon 	/*
1582100650deSOlivier Houchard 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1583100650deSOlivier Houchard 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1584100650deSOlivier Houchard 	 * trigger a KASSERT in relpbuf().
1585100650deSOlivier Houchard 	 */
1586100650deSOlivier Houchard 	if (bp->b_vp) {
1587100650deSOlivier Houchard 		    bp->b_vp = NULL;
1588100650deSOlivier Houchard 		    bp->b_bufobj = NULL;
1589100650deSOlivier Houchard 	}
1590100650deSOlivier Houchard 	/*
15911c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
15921c7c3c6aSMatthew Dillon 	 */
1593327f4e83SMatthew Dillon 	relpbuf(
1594327f4e83SMatthew Dillon 	    bp,
159521144e3bSPoul-Henning Kamp 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1596327f4e83SMatthew Dillon 		((bp->b_flags & B_ASYNC) ?
1597327f4e83SMatthew Dillon 		    &nsw_wcount_async :
1598327f4e83SMatthew Dillon 		    &nsw_wcount_sync
1599327f4e83SMatthew Dillon 		)
1600327f4e83SMatthew Dillon 	    )
1601327f4e83SMatthew Dillon 	);
160226f9a767SRodney W. Grimes }
16031c7c3c6aSMatthew Dillon 
1604b1fd102eSMark Johnston int
1605b1fd102eSMark Johnston swap_pager_nswapdev(void)
1606b1fd102eSMark Johnston {
1607b1fd102eSMark Johnston 
1608b1fd102eSMark Johnston 	return (nswapdev);
1609b1fd102eSMark Johnston }
1610b1fd102eSMark Johnston 
161192da00bbSMatthew Dillon /*
161292da00bbSMatthew Dillon  * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in
161392da00bbSMatthew Dillon  *
1614ebcddc72SAlan Cox  *	This routine dissociates the page at the given index within an object
1615ebcddc72SAlan Cox  *	from its backing store, paging it in if it does not reside in memory.
1616ebcddc72SAlan Cox  *	If the page is paged in, it is marked dirty and placed in the laundry
1617ebcddc72SAlan Cox  *	queue.  The page is marked dirty because it no longer has backing
1618ebcddc72SAlan Cox  *	store.  It is placed in the laundry queue because it has not been
1619ebcddc72SAlan Cox  *	accessed recently.  Otherwise, it would already reside in memory.
1620ebcddc72SAlan Cox  *
1621ebcddc72SAlan Cox  *	We also attempt to swap in all other pages in the swap block.
1622ebcddc72SAlan Cox  *	However, we only guarantee that the one at the specified index is
162392da00bbSMatthew Dillon  *	paged in.
162492da00bbSMatthew Dillon  *
162592da00bbSMatthew Dillon  *	XXX - The code to page the whole block in doesn't work, so we
162692da00bbSMatthew Dillon  *	      revert to the one-by-one behavior for now.  Sigh.
162792da00bbSMatthew Dillon  */
162862a59e8fSWarner Losh static inline void
1629b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex)
163092da00bbSMatthew Dillon {
163192da00bbSMatthew Dillon 	vm_page_t m;
163292da00bbSMatthew Dillon 
163392da00bbSMatthew Dillon 	vm_object_pip_add(object, 1);
16345944de8eSKonstantin Belousov 	m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
163592da00bbSMatthew Dillon 	if (m->valid == VM_PAGE_BITS_ALL) {
16360d8243ccSAttilio Rao 		vm_object_pip_wakeup(object);
163792da00bbSMatthew Dillon 		vm_page_dirty(m);
1638d061cdd5SAlan Cox 		vm_page_lock(m);
1639d061cdd5SAlan Cox 		vm_page_activate(m);
16402965a453SKip Macy 		vm_page_unlock(m);
1641c7aebda8SAttilio Rao 		vm_page_xunbusy(m);
164292da00bbSMatthew Dillon 		vm_pager_page_unswapped(m);
164392da00bbSMatthew Dillon 		return;
164492da00bbSMatthew Dillon 	}
164592da00bbSMatthew Dillon 
1646b0cd2017SGleb Smirnoff 	if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK)
164792da00bbSMatthew Dillon 		panic("swap_pager_force_pagein: read from swap failed");/*XXX*/
16480d8243ccSAttilio Rao 	vm_object_pip_wakeup(object);
164992da00bbSMatthew Dillon 	vm_page_dirty(m);
1650db1f085eSAlan Cox 	vm_page_lock(m);
1651ebcddc72SAlan Cox 	vm_page_launder(m);
16522965a453SKip Macy 	vm_page_unlock(m);
1653c7aebda8SAttilio Rao 	vm_page_xunbusy(m);
165492da00bbSMatthew Dillon 	vm_pager_page_unswapped(m);
165592da00bbSMatthew Dillon }
165692da00bbSMatthew Dillon 
165792da00bbSMatthew Dillon /*
165892da00bbSMatthew Dillon  *	swap_pager_swapoff:
165992da00bbSMatthew Dillon  *
166092da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
166192da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
166292da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
166392da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
166492da00bbSMatthew Dillon  *
166592da00bbSMatthew Dillon  *	This routine may block.
166692da00bbSMatthew Dillon  */
1667e9c0cc15SPoul-Henning Kamp static void
1668b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp)
166992da00bbSMatthew Dillon {
1670f425ab8eSKonstantin Belousov 	struct swblk *sb;
1671f425ab8eSKonstantin Belousov 	vm_object_t object;
1672f425ab8eSKonstantin Belousov 	vm_pindex_t pi;
1673f425ab8eSKonstantin Belousov 	int i, retries;
167492da00bbSMatthew Dillon 
167504533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
167692da00bbSMatthew Dillon 
16778bc61209SDavid Schultz 	retries = 0;
167892da00bbSMatthew Dillon full_rescan:
1679f425ab8eSKonstantin Belousov 	mtx_lock(&vm_object_list_mtx);
1680f425ab8eSKonstantin Belousov 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
1681f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
168298150664SKonstantin Belousov 			continue;
1683f425ab8eSKonstantin Belousov 		mtx_unlock(&vm_object_list_mtx);
168498150664SKonstantin Belousov 		/* Depends on type-stability. */
168598150664SKonstantin Belousov 		VM_OBJECT_WLOCK(object);
1686f425ab8eSKonstantin Belousov 
1687f425ab8eSKonstantin Belousov 		/*
1688f425ab8eSKonstantin Belousov 		 * Dead objects are eventually terminated on their own.
1689f425ab8eSKonstantin Belousov 		 */
1690f425ab8eSKonstantin Belousov 		if ((object->flags & OBJ_DEAD) != 0)
1691f425ab8eSKonstantin Belousov 			goto next_obj;
1692f425ab8eSKonstantin Belousov 
1693f425ab8eSKonstantin Belousov 		/*
1694f425ab8eSKonstantin Belousov 		 * Sync with fences placed after pctrie
1695f425ab8eSKonstantin Belousov 		 * initialization.  We must not access pctrie below
1696f425ab8eSKonstantin Belousov 		 * unless we checked that our object is swap and not
1697f425ab8eSKonstantin Belousov 		 * dead.
1698f425ab8eSKonstantin Belousov 		 */
1699f425ab8eSKonstantin Belousov 		atomic_thread_fence_acq();
1700f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
1701f425ab8eSKonstantin Belousov 			goto next_obj;
1702f425ab8eSKonstantin Belousov 
1703f425ab8eSKonstantin Belousov 		for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
1704f425ab8eSKonstantin Belousov 		    &object->un_pager.swp.swp_blks, pi)) != NULL; ) {
1705f425ab8eSKonstantin Belousov 			pi = sb->p + SWAP_META_PAGES;
1706f425ab8eSKonstantin Belousov 			for (i = 0; i < SWAP_META_PAGES; i++) {
1707f425ab8eSKonstantin Belousov 				if (sb->d[i] == SWAPBLK_NONE)
1708f425ab8eSKonstantin Belousov 					continue;
1709f425ab8eSKonstantin Belousov 				if (swp_pager_isondev(sb->d[i], sp))
1710f425ab8eSKonstantin Belousov 					swp_pager_force_pagein(object,
1711f425ab8eSKonstantin Belousov 					    sb->p + i);
171298150664SKonstantin Belousov 			}
171398150664SKonstantin Belousov 		}
1714f425ab8eSKonstantin Belousov next_obj:
1715f425ab8eSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
1716f425ab8eSKonstantin Belousov 		mtx_lock(&vm_object_list_mtx);
171792da00bbSMatthew Dillon 	}
1718f425ab8eSKonstantin Belousov 	mtx_unlock(&vm_object_list_mtx);
1719f425ab8eSKonstantin Belousov 
17208bc61209SDavid Schultz 	if (sp->sw_used) {
172192da00bbSMatthew Dillon 		/*
17228bc61209SDavid Schultz 		 * Objects may be locked or paging to the device being
17238bc61209SDavid Schultz 		 * removed, so we will miss their pages and need to
17248bc61209SDavid Schultz 		 * make another pass.  We have marked this device as
17258bc61209SDavid Schultz 		 * SW_CLOSING, so the activity should finish soon.
172692da00bbSMatthew Dillon 		 */
17278bc61209SDavid Schultz 		retries++;
17288bc61209SDavid Schultz 		if (retries > 100) {
17298bc61209SDavid Schultz 			panic("swapoff: failed to locate %d swap blocks",
17308bc61209SDavid Schultz 			    sp->sw_used);
17318bc61209SDavid Schultz 		}
17324d70511aSJohn Baldwin 		pause("swpoff", hz / 20);
173392da00bbSMatthew Dillon 		goto full_rescan;
173492da00bbSMatthew Dillon 	}
1735b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapoff, sp);
173692da00bbSMatthew Dillon }
173792da00bbSMatthew Dillon 
17381c7c3c6aSMatthew Dillon /************************************************************************
17391c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
17401c7c3c6aSMatthew Dillon  ************************************************************************
17411c7c3c6aSMatthew Dillon  *
17421c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
1743cec9f109SDavid E. O'Brien  *	OBJT_SWAP object.
17441c7c3c6aSMatthew Dillon  *
17454dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
17464dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
17474dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
17481c7c3c6aSMatthew Dillon  */
17491c7c3c6aSMatthew Dillon 
17501c7c3c6aSMatthew Dillon /*
17511c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
17521c7c3c6aSMatthew Dillon  *
17531c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
17541c7c3c6aSMatthew Dillon  *	object.
17551c7c3c6aSMatthew Dillon  *
17561c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
17571c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
17581c7c3c6aSMatthew Dillon  *	assigned swapblk is freed.
17591c7c3c6aSMatthew Dillon  */
17601c7c3c6aSMatthew Dillon static void
17612f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
17622f249180SPoul-Henning Kamp {
1763f425ab8eSKonstantin Belousov 	static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted;
1764eed99cb8SKonstantin Belousov 	struct swblk *sb, *sb1;
1765f425ab8eSKonstantin Belousov 	vm_pindex_t modpi, rdpi;
1766f425ab8eSKonstantin Belousov 	int error, i;
17671c7c3c6aSMatthew Dillon 
176889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
1769f425ab8eSKonstantin Belousov 
17701c7c3c6aSMatthew Dillon 	/*
17711c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
17721c7c3c6aSMatthew Dillon 	 */
17731c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
1774f425ab8eSKonstantin Belousov 		pctrie_init(&object->un_pager.swp.swp_blks);
1775f425ab8eSKonstantin Belousov 
1776f425ab8eSKonstantin Belousov 		/*
1777f425ab8eSKonstantin Belousov 		 * Ensure that swap_pager_swapoff()'s iteration over
1778f425ab8eSKonstantin Belousov 		 * object_list does not see a garbage pctrie.
1779f425ab8eSKonstantin Belousov 		 */
1780f425ab8eSKonstantin Belousov 		atomic_thread_fence_rel();
1781f425ab8eSKonstantin Belousov 
17821c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
1783eb4d6a1bSKonstantin Belousov 		KASSERT(object->handle == NULL, ("default pager with handle"));
1784bd228075SAlan Cox 	}
17851c7c3c6aSMatthew Dillon 
1786f425ab8eSKonstantin Belousov 	rdpi = rounddown(pindex, SWAP_META_PAGES);
1787f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi);
1788f425ab8eSKonstantin Belousov 	if (sb == NULL) {
17891c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
1790f425ab8eSKonstantin Belousov 			return;
1791f425ab8eSKonstantin Belousov 		for (;;) {
1792f425ab8eSKonstantin Belousov 			sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc ==
1793f425ab8eSKonstantin Belousov 			    pageproc ? M_USE_RESERVE : 0));
1794f425ab8eSKonstantin Belousov 			if (sb != NULL) {
1795f425ab8eSKonstantin Belousov 				sb->p = rdpi;
1796f425ab8eSKonstantin Belousov 				for (i = 0; i < SWAP_META_PAGES; i++)
1797f425ab8eSKonstantin Belousov 					sb->d[i] = SWAPBLK_NONE;
1798f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swblk_zone_exhausted,
1799f425ab8eSKonstantin Belousov 				    1, 0))
1800f425ab8eSKonstantin Belousov 					printf("swblk zone ok\n");
1801f425ab8eSKonstantin Belousov 				break;
1802f425ab8eSKonstantin Belousov 			}
180389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1804f425ab8eSKonstantin Belousov 			if (uma_zone_exhausted(swblk_zone)) {
1805f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swblk_zone_exhausted,
1806f425ab8eSKonstantin Belousov 				    0, 1))
1807f425ab8eSKonstantin Belousov 					printf("swap blk zone exhausted, "
18083ff863f1SDag-Erling Smørgrav 					    "increase kern.maxswzone\n");
18092025d69bSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
1810f425ab8eSKonstantin Belousov 				pause("swzonxb", 10);
18112025d69bSKonstantin Belousov 			} else
18124dcc5c2dSMatthew Dillon 				VM_WAIT;
181389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
1814eed99cb8SKonstantin Belousov 			sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1815eed99cb8SKonstantin Belousov 			    rdpi);
1816eed99cb8SKonstantin Belousov 			if (sb != NULL)
1817eed99cb8SKonstantin Belousov 				/*
1818eed99cb8SKonstantin Belousov 				 * Somebody swapped out a nearby page,
1819eed99cb8SKonstantin Belousov 				 * allocating swblk at the rdpi index,
1820eed99cb8SKonstantin Belousov 				 * while we dropped the object lock.
1821eed99cb8SKonstantin Belousov 				 */
1822eed99cb8SKonstantin Belousov 				goto allocated;
18234dcc5c2dSMatthew Dillon 		}
1824f425ab8eSKonstantin Belousov 		for (;;) {
1825f425ab8eSKonstantin Belousov 			error = SWAP_PCTRIE_INSERT(
1826f425ab8eSKonstantin Belousov 			    &object->un_pager.swp.swp_blks, sb);
1827f425ab8eSKonstantin Belousov 			if (error == 0) {
1828f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1829f425ab8eSKonstantin Belousov 				    1, 0))
1830f425ab8eSKonstantin Belousov 					printf("swpctrie zone ok\n");
1831f425ab8eSKonstantin Belousov 				break;
18321c7c3c6aSMatthew Dillon 			}
1833f425ab8eSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
1834f425ab8eSKonstantin Belousov 			if (uma_zone_exhausted(swpctrie_zone)) {
1835f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1836f425ab8eSKonstantin Belousov 				    0, 1))
1837f425ab8eSKonstantin Belousov 					printf("swap pctrie zone exhausted, "
1838f425ab8eSKonstantin Belousov 					    "increase kern.maxswzone\n");
1839f425ab8eSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
1840f425ab8eSKonstantin Belousov 				pause("swzonxp", 10);
1841f425ab8eSKonstantin Belousov 			} else
1842f425ab8eSKonstantin Belousov 				VM_WAIT;
1843f425ab8eSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
1844eed99cb8SKonstantin Belousov 			sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1845eed99cb8SKonstantin Belousov 			    rdpi);
1846eed99cb8SKonstantin Belousov 			if (sb1 != NULL) {
1847eed99cb8SKonstantin Belousov 				uma_zfree(swblk_zone, sb);
1848eed99cb8SKonstantin Belousov 				sb = sb1;
1849eed99cb8SKonstantin Belousov 				goto allocated;
18501c7c3c6aSMatthew Dillon 			}
1851f425ab8eSKonstantin Belousov 		}
1852eed99cb8SKonstantin Belousov 	}
1853eed99cb8SKonstantin Belousov allocated:
1854f425ab8eSKonstantin Belousov 	MPASS(sb->p == rdpi);
18551c7c3c6aSMatthew Dillon 
1856f425ab8eSKonstantin Belousov 	modpi = pindex % SWAP_META_PAGES;
1857f425ab8eSKonstantin Belousov 	/* Delete prior contents of metadata. */
1858f425ab8eSKonstantin Belousov 	if (sb->d[modpi] != SWAPBLK_NONE)
1859f425ab8eSKonstantin Belousov 		swp_pager_freeswapspace(sb->d[modpi], 1);
1860f425ab8eSKonstantin Belousov 	/* Enter block into metadata. */
1861f425ab8eSKonstantin Belousov 	sb->d[modpi] = swapblk;
186285d88d87SKonstantin Belousov 
186385d88d87SKonstantin Belousov 	/*
186485d88d87SKonstantin Belousov 	 * Free the swblk if we end up with the empty page run.
186585d88d87SKonstantin Belousov 	 */
186685d88d87SKonstantin Belousov 	if (swapblk == SWAPBLK_NONE) {
186785d88d87SKonstantin Belousov 		for (i = 0; i < SWAP_META_PAGES; i++) {
186885d88d87SKonstantin Belousov 			if (sb->d[i] != SWAPBLK_NONE)
186985d88d87SKonstantin Belousov 				break;
187085d88d87SKonstantin Belousov 		}
187185d88d87SKonstantin Belousov 		if (i == SWAP_META_PAGES) {
187285d88d87SKonstantin Belousov 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
187385d88d87SKonstantin Belousov 			    rdpi);
187485d88d87SKonstantin Belousov 			uma_zfree(swblk_zone, sb);
187585d88d87SKonstantin Belousov 		}
187685d88d87SKonstantin Belousov 	}
18771c7c3c6aSMatthew Dillon }
18781c7c3c6aSMatthew Dillon 
18791c7c3c6aSMatthew Dillon /*
18801c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
18811c7c3c6aSMatthew Dillon  *
18821c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
18831c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
18841c7c3c6aSMatthew Dillon  *
18851c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
18861c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
18871c7c3c6aSMatthew Dillon  *	with resident pages.
18881c7c3c6aSMatthew Dillon  */
18891c7c3c6aSMatthew Dillon static void
1890f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count)
18911c7c3c6aSMatthew Dillon {
1892f425ab8eSKonstantin Belousov 	struct swblk *sb;
1893f425ab8eSKonstantin Belousov 	vm_pindex_t last;
1894f425ab8eSKonstantin Belousov 	int i;
1895f425ab8eSKonstantin Belousov 	bool empty;
18962928cef7SAlan Cox 
1897ee620ea4SAlan Cox 	VM_OBJECT_ASSERT_WLOCKED(object);
18982e56b64fSKonstantin Belousov 	if (object->type != OBJT_SWAP || count == 0)
18991c7c3c6aSMatthew Dillon 		return;
19001c7c3c6aSMatthew Dillon 
1901f425ab8eSKonstantin Belousov 	last = pindex + count - 1;
1902f425ab8eSKonstantin Belousov 	for (;;) {
1903f425ab8eSKonstantin Belousov 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
1904f425ab8eSKonstantin Belousov 		    rounddown(pindex, SWAP_META_PAGES));
1905f425ab8eSKonstantin Belousov 		if (sb == NULL || sb->p > last)
19062e56b64fSKonstantin Belousov 			break;
1907f425ab8eSKonstantin Belousov 		empty = true;
1908f425ab8eSKonstantin Belousov 		for (i = 0; i < SWAP_META_PAGES; i++) {
1909f425ab8eSKonstantin Belousov 			if (sb->d[i] == SWAPBLK_NONE)
1910f425ab8eSKonstantin Belousov 				continue;
1911f425ab8eSKonstantin Belousov 			if (pindex <= sb->p + i && sb->p + i <= last) {
1912f425ab8eSKonstantin Belousov 				swp_pager_freeswapspace(sb->d[i], 1);
1913f425ab8eSKonstantin Belousov 				sb->d[i] = SWAPBLK_NONE;
1914f425ab8eSKonstantin Belousov 			} else
1915f425ab8eSKonstantin Belousov 				empty = false;
1916f425ab8eSKonstantin Belousov 		}
1917f425ab8eSKonstantin Belousov 		pindex = sb->p + SWAP_META_PAGES;
1918f425ab8eSKonstantin Belousov 		if (empty) {
1919f425ab8eSKonstantin Belousov 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
1920f425ab8eSKonstantin Belousov 			    sb->p);
1921f425ab8eSKonstantin Belousov 			uma_zfree(swblk_zone, sb);
19221c7c3c6aSMatthew Dillon 		}
19231c7c3c6aSMatthew Dillon 	}
19241c7c3c6aSMatthew Dillon }
19251c7c3c6aSMatthew Dillon 
19261c7c3c6aSMatthew Dillon /*
19271c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
19281c7c3c6aSMatthew Dillon  *
19291c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
19301c7c3c6aSMatthew Dillon  *	an object.
19311c7c3c6aSMatthew Dillon  */
19321c7c3c6aSMatthew Dillon static void
19331c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
19341c7c3c6aSMatthew Dillon {
1935f425ab8eSKonstantin Belousov 	struct swblk *sb;
1936f425ab8eSKonstantin Belousov 	vm_pindex_t pindex;
193771057cd2SKonstantin Belousov 	int i;
19381c7c3c6aSMatthew Dillon 
193989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
19401c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19411c7c3c6aSMatthew Dillon 		return;
19421c7c3c6aSMatthew Dillon 
1943f425ab8eSKonstantin Belousov 	for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
1944f425ab8eSKonstantin Belousov 	    &object->un_pager.swp.swp_blks, pindex)) != NULL;) {
1945f425ab8eSKonstantin Belousov 		pindex = sb->p + SWAP_META_PAGES;
1946f425ab8eSKonstantin Belousov 		for (i = 0; i < SWAP_META_PAGES; i++) {
1947f425ab8eSKonstantin Belousov 			if (sb->d[i] != SWAPBLK_NONE)
1948f425ab8eSKonstantin Belousov 				swp_pager_freeswapspace(sb->d[i], 1);
19491c7c3c6aSMatthew Dillon 		}
1950f425ab8eSKonstantin Belousov 		SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
1951f425ab8eSKonstantin Belousov 		uma_zfree(swblk_zone, sb);
19521c7c3c6aSMatthew Dillon 	}
19531c7c3c6aSMatthew Dillon }
19541c7c3c6aSMatthew Dillon 
19551c7c3c6aSMatthew Dillon /*
19561c7c3c6aSMatthew Dillon  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
19571c7c3c6aSMatthew Dillon  *
19581c7c3c6aSMatthew Dillon  *	This routine is capable of looking up, popping, or freeing
19591c7c3c6aSMatthew Dillon  *	swapblk assignments in the swap meta data or in the vm_page_t.
19601c7c3c6aSMatthew Dillon  *	The routine typically returns the swapblk being looked-up, or popped,
19611c7c3c6aSMatthew Dillon  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
19621c7c3c6aSMatthew Dillon  *	was invalid.  This routine will automatically free any invalid
19631c7c3c6aSMatthew Dillon  *	meta-data swapblks.
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 {
1975f425ab8eSKonstantin Belousov 	struct swblk *sb;
19764dcc5c2dSMatthew Dillon 	daddr_t r1;
1977f425ab8eSKonstantin Belousov 	int i;
19784dcc5c2dSMatthew Dillon 
1979ee620ea4SAlan Cox 	if ((flags & (SWM_FREE | SWM_POP)) != 0)
1980ee620ea4SAlan Cox 		VM_OBJECT_ASSERT_WLOCKED(object);
1981ee620ea4SAlan Cox 	else
1982c25673ffSAttilio Rao 		VM_OBJECT_ASSERT_LOCKED(object);
1983ee620ea4SAlan Cox 
19841c7c3c6aSMatthew Dillon 	/*
1985ee620ea4SAlan Cox 	 * The meta data only exists if the object is OBJT_SWAP
19861c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
19871c7c3c6aSMatthew Dillon 	 */
19884dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
19891c7c3c6aSMatthew Dillon 		return (SWAPBLK_NONE);
19901c7c3c6aSMatthew Dillon 
1991f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1992f425ab8eSKonstantin Belousov 	    rounddown(pindex, SWAP_META_PAGES));
1993f425ab8eSKonstantin Belousov 	if (sb == NULL)
1994f425ab8eSKonstantin Belousov 		return (SWAPBLK_NONE);
1995f425ab8eSKonstantin Belousov 	r1 = sb->d[pindex % SWAP_META_PAGES];
1996f425ab8eSKonstantin Belousov 	if (r1 == SWAPBLK_NONE)
1997f425ab8eSKonstantin Belousov 		return (SWAPBLK_NONE);
1998f425ab8eSKonstantin Belousov 	if ((flags & (SWM_FREE | SWM_POP)) != 0) {
1999f425ab8eSKonstantin Belousov 		sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE;
2000f425ab8eSKonstantin Belousov 		for (i = 0; i < SWAP_META_PAGES; i++) {
2001f425ab8eSKonstantin Belousov 			if (sb->d[i] != SWAPBLK_NONE)
2002f425ab8eSKonstantin Belousov 				break;
2003f425ab8eSKonstantin Belousov 		}
2004f425ab8eSKonstantin Belousov 		if (i == SWAP_META_PAGES) {
2005f425ab8eSKonstantin Belousov 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
2006f425ab8eSKonstantin Belousov 			    rounddown(pindex, SWAP_META_PAGES));
2007f425ab8eSKonstantin Belousov 			uma_zfree(swblk_zone, sb);
2008f425ab8eSKonstantin Belousov 		}
2009f425ab8eSKonstantin Belousov 	}
2010f425ab8eSKonstantin Belousov 	if ((flags & SWM_FREE) != 0) {
20114dcc5c2dSMatthew Dillon 		swp_pager_freeswapspace(r1, 1);
20121c7c3c6aSMatthew Dillon 		r1 = SWAPBLK_NONE;
20131c7c3c6aSMatthew Dillon 	}
20141c7c3c6aSMatthew Dillon 	return (r1);
20151c7c3c6aSMatthew Dillon }
20161c7c3c6aSMatthew Dillon 
2017e9c0cc15SPoul-Henning Kamp /*
201877d6fd97SKonstantin Belousov  * Returns the least page index which is greater than or equal to the
201977d6fd97SKonstantin Belousov  * parameter pindex and for which there is a swap block allocated.
202077d6fd97SKonstantin Belousov  * Returns object's size if the object's type is not swap or if there
202177d6fd97SKonstantin Belousov  * are no allocated swap blocks for the object after the requested
202277d6fd97SKonstantin Belousov  * pindex.
202377d6fd97SKonstantin Belousov  */
202477d6fd97SKonstantin Belousov vm_pindex_t
202577d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
202677d6fd97SKonstantin Belousov {
2027f425ab8eSKonstantin Belousov 	struct swblk *sb;
2028f425ab8eSKonstantin Belousov 	int i;
202977d6fd97SKonstantin Belousov 
203077d6fd97SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(object);
2031f425ab8eSKonstantin Belousov 	if (object->type != OBJT_SWAP)
203277d6fd97SKonstantin Belousov 		return (object->size);
203377d6fd97SKonstantin Belousov 
2034f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2035f425ab8eSKonstantin Belousov 	    rounddown(pindex, SWAP_META_PAGES));
2036f425ab8eSKonstantin Belousov 	if (sb == NULL)
2037f425ab8eSKonstantin Belousov 		return (object->size);
2038f425ab8eSKonstantin Belousov 	if (sb->p < pindex) {
2039f425ab8eSKonstantin Belousov 		for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) {
2040f425ab8eSKonstantin Belousov 			if (sb->d[i] != SWAPBLK_NONE)
2041f425ab8eSKonstantin Belousov 				return (sb->p + i);
204277d6fd97SKonstantin Belousov 		}
2043f425ab8eSKonstantin Belousov 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2044f425ab8eSKonstantin Belousov 		    roundup(pindex, SWAP_META_PAGES));
2045f425ab8eSKonstantin Belousov 		if (sb == NULL)
2046f425ab8eSKonstantin Belousov 			return (object->size);
204777d6fd97SKonstantin Belousov 	}
2048f425ab8eSKonstantin Belousov 	for (i = 0; i < SWAP_META_PAGES; i++) {
2049f425ab8eSKonstantin Belousov 		if (sb->d[i] != SWAPBLK_NONE)
2050f425ab8eSKonstantin Belousov 			return (sb->p + i);
205177d6fd97SKonstantin Belousov 	}
2052f425ab8eSKonstantin Belousov 
2053f425ab8eSKonstantin Belousov 	/*
2054f425ab8eSKonstantin Belousov 	 * We get here if a swblk is present in the trie but it
2055f425ab8eSKonstantin Belousov 	 * doesn't map any blocks.
2056f425ab8eSKonstantin Belousov 	 */
2057f425ab8eSKonstantin Belousov 	MPASS(0);
2058f425ab8eSKonstantin Belousov 	return (object->size);
205977d6fd97SKonstantin Belousov }
206077d6fd97SKonstantin Belousov 
206177d6fd97SKonstantin Belousov /*
2062e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2063e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2064e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2065e9c0cc15SPoul-Henning Kamp  */
2066e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2067e9c0cc15SPoul-Henning Kamp struct swapon_args {
2068e9c0cc15SPoul-Henning Kamp 	char *name;
2069e9c0cc15SPoul-Henning Kamp };
2070e9c0cc15SPoul-Henning Kamp #endif
2071e9c0cc15SPoul-Henning Kamp 
2072e9c0cc15SPoul-Henning Kamp /*
2073e9c0cc15SPoul-Henning Kamp  * MPSAFE
2074e9c0cc15SPoul-Henning Kamp  */
2075e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2076e9c0cc15SPoul-Henning Kamp int
20778451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap)
2078e9c0cc15SPoul-Henning Kamp {
2079e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2080e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2081e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2082e9c0cc15SPoul-Henning Kamp 	int error;
2083e9c0cc15SPoul-Henning Kamp 
2084acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPON);
2085e9c0cc15SPoul-Henning Kamp 	if (error)
2086acd3428bSRobert Watson 		return (error);
2087e9c0cc15SPoul-Henning Kamp 
208804533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2089e9c0cc15SPoul-Henning Kamp 
2090e9c0cc15SPoul-Henning Kamp 	/*
2091e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2092e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2093e9c0cc15SPoul-Henning Kamp 	 */
2094f425ab8eSKonstantin Belousov 	if (swblk_zone == NULL) {
2095e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2096e9c0cc15SPoul-Henning Kamp 		goto done;
2097e9c0cc15SPoul-Henning Kamp 	}
2098e9c0cc15SPoul-Henning Kamp 
2099d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2100d9135e72SRobert Watson 	    uap->name, td);
2101e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2102e9c0cc15SPoul-Henning Kamp 	if (error)
2103e9c0cc15SPoul-Henning Kamp 		goto done;
2104e9c0cc15SPoul-Henning Kamp 
2105e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2106e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2107e9c0cc15SPoul-Henning Kamp 
210820da9c2eSPoul-Henning Kamp 	if (vn_isdisk(vp, &error)) {
210988ad2d7bSKonstantin Belousov 		error = swapongeom(vp);
211020da9c2eSPoul-Henning Kamp 	} else if (vp->v_type == VREG &&
2111e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
21120359a12eSAttilio Rao 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2113e9c0cc15SPoul-Henning Kamp 		/*
2114e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2115e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2116e9c0cc15SPoul-Henning Kamp 		 */
211759efee01SPoul-Henning Kamp 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2118e9c0cc15SPoul-Henning Kamp 	}
2119e9c0cc15SPoul-Henning Kamp 
2120e9c0cc15SPoul-Henning Kamp 	if (error)
2121e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2122e9c0cc15SPoul-Henning Kamp done:
212304533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2124e9c0cc15SPoul-Henning Kamp 	return (error);
2125e9c0cc15SPoul-Henning Kamp }
2126e9c0cc15SPoul-Henning Kamp 
21273ff863f1SDag-Erling Smørgrav /*
21283ff863f1SDag-Erling Smørgrav  * Check that the total amount of swap currently configured does not
21293ff863f1SDag-Erling Smørgrav  * exceed half the theoretical maximum.  If it does, print a warning
213035872e79SKonstantin Belousov  * message.
21313ff863f1SDag-Erling Smørgrav  */
213235872e79SKonstantin Belousov static void
213335872e79SKonstantin Belousov swapon_check_swzone(void)
21343ff863f1SDag-Erling Smørgrav {
213535872e79SKonstantin Belousov 	unsigned long maxpages, npages;
21363ff863f1SDag-Erling Smørgrav 
213735872e79SKonstantin Belousov 	npages = swap_total / PAGE_SIZE;
21383ff863f1SDag-Erling Smørgrav 	/* absolute maximum we can handle assuming 100% efficiency */
2139f425ab8eSKonstantin Belousov 	maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES;
21403ff863f1SDag-Erling Smørgrav 
21413ff863f1SDag-Erling Smørgrav 	/* recommend using no more than half that amount */
21423ff863f1SDag-Erling Smørgrav 	if (npages > maxpages / 2) {
21433ff863f1SDag-Erling Smørgrav 		printf("warning: total configured swap (%lu pages) "
21443ff863f1SDag-Erling Smørgrav 		    "exceeds maximum recommended amount (%lu pages).\n",
21459462305cSSergey Kandaurov 		    npages, maxpages / 2);
21463ff863f1SDag-Erling Smørgrav 		printf("warning: increase kern.maxswzone "
21473ff863f1SDag-Erling Smørgrav 		    "or reduce amount of swap.\n");
21483ff863f1SDag-Erling Smørgrav 	}
21493ff863f1SDag-Erling Smørgrav }
21503ff863f1SDag-Erling Smørgrav 
215159efee01SPoul-Henning Kamp static void
21522cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks,
21532cc718a1SKonstantin Belousov     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2154e9c0cc15SPoul-Henning Kamp {
21552d9974c1SAlan Cox 	struct swdevt *sp, *tsp;
2156e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
21578f60c087SPoul-Henning Kamp 	u_long mblocks;
2158e9c0cc15SPoul-Henning Kamp 
2159e9c0cc15SPoul-Henning Kamp 	/*
2160e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2161e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2162e9c0cc15SPoul-Henning Kamp 	 *
2163e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2164e9c0cc15SPoul-Henning Kamp 	 */
2165e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2166e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2167e9c0cc15SPoul-Henning Kamp 
21686e903bd0SKonstantin Belousov 	/*
21696e903bd0SKonstantin Belousov 	 * If we go beyond this, we get overflows in the radix
21706e903bd0SKonstantin Belousov 	 * tree bitmap code.
21716e903bd0SKonstantin Belousov 	 */
21726e903bd0SKonstantin Belousov 	mblocks = 0x40000000 / BLIST_META_RADIX;
21736e903bd0SKonstantin Belousov 	if (nblks > mblocks) {
21746e903bd0SKonstantin Belousov 		printf(
21756e903bd0SKonstantin Belousov     "WARNING: reducing swap size to maximum of %luMB per unit\n",
21766e903bd0SKonstantin Belousov 		    mblocks / 1024 / 1024 * PAGE_SIZE);
21776e903bd0SKonstantin Belousov 		nblks = mblocks;
21786e903bd0SKonstantin Belousov 	}
21796e903bd0SKonstantin Belousov 
21808f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2181dee34ca4SPoul-Henning Kamp 	sp->sw_vp = vp;
2182dee34ca4SPoul-Henning Kamp 	sp->sw_id = id;
2183f3732fd1SPoul-Henning Kamp 	sp->sw_dev = dev;
21848d677ef9SPoul-Henning Kamp 	sp->sw_flags = 0;
2185e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2186e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
218759efee01SPoul-Henning Kamp 	sp->sw_strategy = strategy;
2188dee34ca4SPoul-Henning Kamp 	sp->sw_close = close;
21892cc718a1SKonstantin Belousov 	sp->sw_flags = flags;
2190e9c0cc15SPoul-Henning Kamp 
2191c8c7ad92SKip Macy 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2192e9c0cc15SPoul-Henning Kamp 	/*
2193ef3c5abdSPoul-Henning Kamp 	 * Do not free the first two block in order to avoid overwriting
21948f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2195e9c0cc15SPoul-Henning Kamp 	 */
2196ef3c5abdSPoul-Henning Kamp 	blist_free(sp->sw_blist, 2, nblks - 2);
2197e9c0cc15SPoul-Henning Kamp 
21982d9974c1SAlan Cox 	dvbase = 0;
219920da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
22002d9974c1SAlan Cox 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
22012d9974c1SAlan Cox 		if (tsp->sw_end >= dvbase) {
22022d9974c1SAlan Cox 			/*
22032d9974c1SAlan Cox 			 * We put one uncovered page between the devices
22042d9974c1SAlan Cox 			 * in order to definitively prevent any cross-device
22052d9974c1SAlan Cox 			 * I/O requests
22062d9974c1SAlan Cox 			 */
22072d9974c1SAlan Cox 			dvbase = tsp->sw_end + 1;
22082d9974c1SAlan Cox 		}
22092d9974c1SAlan Cox 	}
22102d9974c1SAlan Cox 	sp->sw_first = dvbase;
22112d9974c1SAlan Cox 	sp->sw_end = dvbase + nblks;
22128f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
22138f60c087SPoul-Henning Kamp 	nswapdev++;
2214761097c8SAlan Cox 	swap_pager_avail += nblks - 2;
22153364c323SKonstantin Belousov 	swap_total += (vm_ooffset_t)nblks * PAGE_SIZE;
221635872e79SKonstantin Belousov 	swapon_check_swzone();
2217d05bc129SAlan Cox 	swp_sizecheck();
2218d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2219b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapon, sp);
222059efee01SPoul-Henning Kamp }
2221e9c0cc15SPoul-Henning Kamp 
2222e9c0cc15SPoul-Henning Kamp /*
2223e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2224e9c0cc15SPoul-Henning Kamp  *
2225e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2226dee34ca4SPoul-Henning Kamp  *
2227dee34ca4SPoul-Henning Kamp  * XXX: Badly designed system call: it should use a device index
2228dee34ca4SPoul-Henning Kamp  * rather than filename as specification.  We keep sw_vp around
2229dee34ca4SPoul-Henning Kamp  * only to make this work.
2230e9c0cc15SPoul-Henning Kamp  */
2231e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2232e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2233e9c0cc15SPoul-Henning Kamp 	char *name;
2234e9c0cc15SPoul-Henning Kamp };
2235e9c0cc15SPoul-Henning Kamp #endif
2236e9c0cc15SPoul-Henning Kamp 
2237e9c0cc15SPoul-Henning Kamp /*
2238e9c0cc15SPoul-Henning Kamp  * MPSAFE
2239e9c0cc15SPoul-Henning Kamp  */
2240e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2241e9c0cc15SPoul-Henning Kamp int
22428451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap)
2243e9c0cc15SPoul-Henning Kamp {
2244e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2245e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2246e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
22478f60c087SPoul-Henning Kamp 	int error;
2248e9c0cc15SPoul-Henning Kamp 
2249acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPOFF);
2250e9c0cc15SPoul-Henning Kamp 	if (error)
22510909f38aSPawel Jakub Dawidek 		return (error);
2252e9c0cc15SPoul-Henning Kamp 
225304533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2254e9c0cc15SPoul-Henning Kamp 
2255d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2256d9135e72SRobert Watson 	    td);
2257e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2258e9c0cc15SPoul-Henning Kamp 	if (error)
2259e9c0cc15SPoul-Henning Kamp 		goto done;
2260e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2261e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2262e9c0cc15SPoul-Henning Kamp 
226320da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
22648f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2265dee34ca4SPoul-Henning Kamp 		if (sp->sw_vp == vp)
22660909f38aSPawel Jakub Dawidek 			break;
2267e9c0cc15SPoul-Henning Kamp 	}
226820da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
22690909f38aSPawel Jakub Dawidek 	if (sp == NULL) {
2270e9c0cc15SPoul-Henning Kamp 		error = EINVAL;
2271e9c0cc15SPoul-Henning Kamp 		goto done;
22720909f38aSPawel Jakub Dawidek 	}
227335918c55SChristian S.J. Peron 	error = swapoff_one(sp, td->td_ucred);
22740909f38aSPawel Jakub Dawidek done:
227504533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
22760909f38aSPawel Jakub Dawidek 	return (error);
22770909f38aSPawel Jakub Dawidek }
22780909f38aSPawel Jakub Dawidek 
22790909f38aSPawel Jakub Dawidek static int
228035918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred)
22810909f38aSPawel Jakub Dawidek {
228203bdd65fSAlan Cox 	u_long nblks;
2283e9c0cc15SPoul-Henning Kamp #ifdef MAC
22840909f38aSPawel Jakub Dawidek 	int error;
2285e9c0cc15SPoul-Henning Kamp #endif
2286e9c0cc15SPoul-Henning Kamp 
228704533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
22880909f38aSPawel Jakub Dawidek #ifdef MAC
2289cb05b60aSAttilio Rao 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
229035918c55SChristian S.J. Peron 	error = mac_system_check_swapoff(cred, sp->sw_vp);
229122db15c0SAttilio Rao 	(void) VOP_UNLOCK(sp->sw_vp, 0);
22920909f38aSPawel Jakub Dawidek 	if (error != 0)
22930909f38aSPawel Jakub Dawidek 		return (error);
22940909f38aSPawel Jakub Dawidek #endif
2295e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2296e9c0cc15SPoul-Henning Kamp 
2297e9c0cc15SPoul-Henning Kamp 	/*
2298e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2299e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2300e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2301e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2302e9c0cc15SPoul-Henning Kamp 	 */
2303bba39b9aSAlan Cox 	if (vm_cnt.v_free_count + swap_pager_avail < nblks + nswap_lowat)
23040909f38aSPawel Jakub Dawidek 		return (ENOMEM);
2305e9c0cc15SPoul-Henning Kamp 
2306e9c0cc15SPoul-Henning Kamp 	/*
2307e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2308e9c0cc15SPoul-Henning Kamp 	 */
23092928cef7SAlan Cox 	mtx_lock(&sw_dev_mtx);
2310e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
231103bdd65fSAlan Cox 	swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks);
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 /*
2462f425ab8eSKonstantin Belousov  * Count the approximate swap usage in pages for a vmspace.  The
2463f425ab8eSKonstantin Belousov  * shadowed or not yet copied on write swap blocks are not accounted.
2464ec38b344SPoul-Henning Kamp  * The map must be locked.
2465ec38b344SPoul-Henning Kamp  */
24662860553aSRebecca Cran long
2467ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2468ec38b344SPoul-Henning Kamp {
246965d8409cSRebecca Cran 	vm_map_t map;
2470ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
247165d8409cSRebecca Cran 	vm_object_t object;
2472f425ab8eSKonstantin Belousov 	struct swblk *sb;
2473f425ab8eSKonstantin Belousov 	vm_pindex_t e, pi;
2474f425ab8eSKonstantin Belousov 	long count;
2475f425ab8eSKonstantin Belousov 	int i;
247665d8409cSRebecca Cran 
247765d8409cSRebecca Cran 	map = &vmspace->vm_map;
247865d8409cSRebecca Cran 	count = 0;
2479ec38b344SPoul-Henning Kamp 
2480ec38b344SPoul-Henning Kamp 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2481f425ab8eSKonstantin Belousov 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2482f425ab8eSKonstantin Belousov 			continue;
2483f425ab8eSKonstantin Belousov 		object = cur->object.vm_object;
2484f425ab8eSKonstantin Belousov 		if (object == NULL || object->type != OBJT_SWAP)
2485f425ab8eSKonstantin Belousov 			continue;
2486f425ab8eSKonstantin Belousov 		VM_OBJECT_RLOCK(object);
2487f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
2488f425ab8eSKonstantin Belousov 			goto unlock;
2489f425ab8eSKonstantin Belousov 		pi = OFF_TO_IDX(cur->offset);
2490f425ab8eSKonstantin Belousov 		e = pi + OFF_TO_IDX(cur->end - cur->start);
2491f425ab8eSKonstantin Belousov 		for (;; pi = sb->p + SWAP_META_PAGES) {
2492f425ab8eSKonstantin Belousov 			sb = SWAP_PCTRIE_LOOKUP_GE(
2493f425ab8eSKonstantin Belousov 			    &object->un_pager.swp.swp_blks, pi);
2494f425ab8eSKonstantin Belousov 			if (sb == NULL || sb->p >= e)
2495f425ab8eSKonstantin Belousov 				break;
2496f425ab8eSKonstantin Belousov 			for (i = 0; i < SWAP_META_PAGES; i++) {
2497f425ab8eSKonstantin Belousov 				if (sb->p + i < e &&
2498f425ab8eSKonstantin Belousov 				    sb->d[i] != SWAPBLK_NONE)
2499f425ab8eSKonstantin Belousov 					count++;
2500ec38b344SPoul-Henning Kamp 			}
2501ec38b344SPoul-Henning Kamp 		}
2502f425ab8eSKonstantin Belousov unlock:
2503f425ab8eSKonstantin Belousov 		VM_OBJECT_RUNLOCK(object);
2504ec38b344SPoul-Henning Kamp 	}
2505ec38b344SPoul-Henning Kamp 	return (count);
2506ec38b344SPoul-Henning Kamp }
2507dee34ca4SPoul-Henning Kamp 
2508dee34ca4SPoul-Henning Kamp /*
2509dee34ca4SPoul-Henning Kamp  * GEOM backend
2510dee34ca4SPoul-Henning Kamp  *
2511dee34ca4SPoul-Henning Kamp  * Swapping onto disk devices.
2512dee34ca4SPoul-Henning Kamp  *
2513dee34ca4SPoul-Henning Kamp  */
2514dee34ca4SPoul-Henning Kamp 
25155721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan;
25165721c9c7SPoul-Henning Kamp 
2517dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = {
2518dee34ca4SPoul-Henning Kamp 	.name = "SWAP",
25195721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
25205721c9c7SPoul-Henning Kamp 	.orphan = swapgeom_orphan,
2521dee34ca4SPoul-Henning Kamp };
2522dee34ca4SPoul-Henning Kamp 
2523dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class);
2524dee34ca4SPoul-Henning Kamp 
2525dee34ca4SPoul-Henning Kamp 
2526dee34ca4SPoul-Henning Kamp static void
25273398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags)
25283398491bSAlexander Motin {
25293398491bSAlexander Motin 	struct g_consumer *cp;
25303398491bSAlexander Motin 
25313398491bSAlexander Motin 	cp = arg;
25323398491bSAlexander Motin 	g_access(cp, -1, -1, 0);
25333398491bSAlexander Motin 	g_detach(cp);
25343398491bSAlexander Motin 	g_destroy_consumer(cp);
25353398491bSAlexander Motin }
25363398491bSAlexander Motin 
25379e3e3fe5SWarner Losh /*
25389e3e3fe5SWarner Losh  * Add a reference to the g_consumer for an inflight transaction.
25399e3e3fe5SWarner Losh  */
25409e3e3fe5SWarner Losh static void
25419e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp)
25429e3e3fe5SWarner Losh {
25439e3e3fe5SWarner Losh 
25449e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
25459e3e3fe5SWarner Losh 	cp->index++;
25469e3e3fe5SWarner Losh }
25479e3e3fe5SWarner Losh 
25489e3e3fe5SWarner Losh /*
25490c657d22SKonstantin Belousov  * Remove a reference from the g_consumer.  Post a close event if all
25500c657d22SKonstantin Belousov  * references go away, since the function might be called from the
25510c657d22SKonstantin Belousov  * biodone context.
25529e3e3fe5SWarner Losh  */
25539e3e3fe5SWarner Losh static void
25549e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
25559e3e3fe5SWarner Losh {
25569e3e3fe5SWarner Losh 
25579e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
25589e3e3fe5SWarner Losh 	cp->index--;
25599e3e3fe5SWarner Losh 	if (cp->index == 0) {
25609e3e3fe5SWarner Losh 		if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
25619e3e3fe5SWarner Losh 			sp->sw_id = NULL;
25629e3e3fe5SWarner Losh 	}
25639e3e3fe5SWarner Losh }
25649e3e3fe5SWarner Losh 
25653398491bSAlexander Motin static void
2566dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2)
2567dee34ca4SPoul-Henning Kamp {
25683398491bSAlexander Motin 	struct swdevt *sp;
2569dee34ca4SPoul-Henning Kamp 	struct buf *bp;
25703398491bSAlexander Motin 	struct g_consumer *cp;
2571dee34ca4SPoul-Henning Kamp 
2572dee34ca4SPoul-Henning Kamp 	bp = bp2->bio_caller2;
25733398491bSAlexander Motin 	cp = bp2->bio_from;
2574c5d3d25eSPoul-Henning Kamp 	bp->b_ioflags = bp2->bio_flags;
2575dee34ca4SPoul-Henning Kamp 	if (bp2->bio_error)
2576dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2577c5d3d25eSPoul-Henning Kamp 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2578c5d3d25eSPoul-Henning Kamp 	bp->b_error = bp2->bio_error;
2579dee34ca4SPoul-Henning Kamp 	bufdone(bp);
25803398491bSAlexander Motin 	sp = bp2->bio_caller1;
25819e3e3fe5SWarner Losh 	mtx_lock(&sw_dev_mtx);
25829e3e3fe5SWarner Losh 	swapgeom_release(cp, sp);
25833398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
2584dee34ca4SPoul-Henning Kamp 	g_destroy_bio(bp2);
2585dee34ca4SPoul-Henning Kamp }
2586dee34ca4SPoul-Henning Kamp 
2587dee34ca4SPoul-Henning Kamp static void
2588dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2589dee34ca4SPoul-Henning Kamp {
2590dee34ca4SPoul-Henning Kamp 	struct bio *bio;
2591dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2592dee34ca4SPoul-Henning Kamp 
25933398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
2594dee34ca4SPoul-Henning Kamp 	cp = sp->sw_id;
2595dee34ca4SPoul-Henning Kamp 	if (cp == NULL) {
25963398491bSAlexander Motin 		mtx_unlock(&sw_dev_mtx);
2597dee34ca4SPoul-Henning Kamp 		bp->b_error = ENXIO;
2598dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2599dee34ca4SPoul-Henning Kamp 		bufdone(bp);
2600dee34ca4SPoul-Henning Kamp 		return;
2601dee34ca4SPoul-Henning Kamp 	}
26029e3e3fe5SWarner Losh 	swapgeom_acquire(cp);
26033398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
260411041003SKonstantin Belousov 	if (bp->b_iocmd == BIO_WRITE)
260511041003SKonstantin Belousov 		bio = g_new_bio();
260611041003SKonstantin Belousov 	else
26074f8205e5SPoul-Henning Kamp 		bio = g_alloc_bio();
26084f8205e5SPoul-Henning Kamp 	if (bio == NULL) {
26099e3e3fe5SWarner Losh 		mtx_lock(&sw_dev_mtx);
26109e3e3fe5SWarner Losh 		swapgeom_release(cp, sp);
26119e3e3fe5SWarner Losh 		mtx_unlock(&sw_dev_mtx);
26123e5b6861SPoul-Henning Kamp 		bp->b_error = ENOMEM;
26133e5b6861SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
26143e5b6861SPoul-Henning Kamp 		bufdone(bp);
26153e5b6861SPoul-Henning Kamp 		return;
26163e5b6861SPoul-Henning Kamp 	}
261711041003SKonstantin Belousov 
26183398491bSAlexander Motin 	bio->bio_caller1 = sp;
2619dee34ca4SPoul-Henning Kamp 	bio->bio_caller2 = bp;
2620c5d3d25eSPoul-Henning Kamp 	bio->bio_cmd = bp->b_iocmd;
2621dee34ca4SPoul-Henning Kamp 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2622dee34ca4SPoul-Henning Kamp 	bio->bio_length = bp->b_bcount;
2623dee34ca4SPoul-Henning Kamp 	bio->bio_done = swapgeom_done;
2624fade8dd7SJeff Roberson 	if (!buf_mapped(bp)) {
26252cc718a1SKonstantin Belousov 		bio->bio_ma = bp->b_pages;
26262cc718a1SKonstantin Belousov 		bio->bio_data = unmapped_buf;
26272cc718a1SKonstantin Belousov 		bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
26282cc718a1SKonstantin Belousov 		bio->bio_ma_n = bp->b_npages;
26292cc718a1SKonstantin Belousov 		bio->bio_flags |= BIO_UNMAPPED;
26302cc718a1SKonstantin Belousov 	} else {
26312cc718a1SKonstantin Belousov 		bio->bio_data = bp->b_data;
26322cc718a1SKonstantin Belousov 		bio->bio_ma = NULL;
26332cc718a1SKonstantin Belousov 	}
2634dee34ca4SPoul-Henning Kamp 	g_io_request(bio, cp);
2635dee34ca4SPoul-Henning Kamp 	return;
2636dee34ca4SPoul-Henning Kamp }
2637dee34ca4SPoul-Henning Kamp 
2638dee34ca4SPoul-Henning Kamp static void
2639dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp)
2640dee34ca4SPoul-Henning Kamp {
2641dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
26423398491bSAlexander Motin 	int destroy;
2643dee34ca4SPoul-Henning Kamp 
2644dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
26453398491bSAlexander Motin 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
26463398491bSAlexander Motin 		if (sp->sw_id == cp) {
26478f12d83aSAlexander Motin 			sp->sw_flags |= SW_CLOSING;
26483398491bSAlexander Motin 			break;
2649dee34ca4SPoul-Henning Kamp 		}
26503398491bSAlexander Motin 	}
26519e3e3fe5SWarner Losh 	/*
26529e3e3fe5SWarner Losh 	 * Drop reference we were created with. Do directly since we're in a
26539e3e3fe5SWarner Losh 	 * special context where we don't have to queue the call to
26549e3e3fe5SWarner Losh 	 * swapgeom_close_ev().
26559e3e3fe5SWarner Losh 	 */
26569e3e3fe5SWarner Losh 	cp->index--;
26573398491bSAlexander Motin 	destroy = ((sp != NULL) && (cp->index == 0));
26583398491bSAlexander Motin 	if (destroy)
26593398491bSAlexander Motin 		sp->sw_id = NULL;
26603398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
26613398491bSAlexander Motin 	if (destroy)
26623398491bSAlexander Motin 		swapgeom_close_ev(cp, 0);
2663dee34ca4SPoul-Henning Kamp }
2664dee34ca4SPoul-Henning Kamp 
2665dee34ca4SPoul-Henning Kamp static void
2666dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw)
2667dee34ca4SPoul-Henning Kamp {
26683398491bSAlexander Motin 	struct g_consumer *cp;
2669dee34ca4SPoul-Henning Kamp 
26703398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
26713398491bSAlexander Motin 	cp = sw->sw_id;
26723398491bSAlexander Motin 	sw->sw_id = NULL;
26733398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
26740c657d22SKonstantin Belousov 
26750c657d22SKonstantin Belousov 	/*
26760c657d22SKonstantin Belousov 	 * swapgeom_close() may be called from the biodone context,
26770c657d22SKonstantin Belousov 	 * where we cannot perform topology changes.  Delegate the
26780c657d22SKonstantin Belousov 	 * work to the events thread.
26790c657d22SKonstantin Belousov 	 */
26803398491bSAlexander Motin 	if (cp != NULL)
26813398491bSAlexander Motin 		g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2682dee34ca4SPoul-Henning Kamp }
2683dee34ca4SPoul-Henning Kamp 
268488ad2d7bSKonstantin Belousov static int
268588ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp)
2686dee34ca4SPoul-Henning Kamp {
2687dee34ca4SPoul-Henning Kamp 	struct g_provider *pp;
2688dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2689dee34ca4SPoul-Henning Kamp 	static struct g_geom *gp;
2690dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2691dee34ca4SPoul-Henning Kamp 	u_long nblks;
2692dee34ca4SPoul-Henning Kamp 	int error;
2693dee34ca4SPoul-Henning Kamp 
269488ad2d7bSKonstantin Belousov 	pp = g_dev_getprovider(dev);
269588ad2d7bSKonstantin Belousov 	if (pp == NULL)
269688ad2d7bSKonstantin Belousov 		return (ENODEV);
2697dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2698dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2699dee34ca4SPoul-Henning Kamp 		cp = sp->sw_id;
2700dee34ca4SPoul-Henning Kamp 		if (cp != NULL && cp->provider == pp) {
2701dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
270288ad2d7bSKonstantin Belousov 			return (EBUSY);
2703dee34ca4SPoul-Henning Kamp 		}
2704dee34ca4SPoul-Henning Kamp 	}
2705dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
27065721c9c7SPoul-Henning Kamp 	if (gp == NULL)
270702c62349SJaakko Heinonen 		gp = g_new_geomf(&g_swap_class, "swap");
2708dee34ca4SPoul-Henning Kamp 	cp = g_new_consumer(gp);
27099e3e3fe5SWarner Losh 	cp->index = 1;	/* Number of active I/Os, plus one for being active. */
27109e3e3fe5SWarner Losh 	cp->flags |=  G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2711dee34ca4SPoul-Henning Kamp 	g_attach(cp, pp);
2712afeb65e6SPoul-Henning Kamp 	/*
2713afeb65e6SPoul-Henning Kamp 	 * XXX: Every time you think you can improve the margin for
2714afeb65e6SPoul-Henning Kamp 	 * footshooting, somebody depends on the ability to do so:
2715afeb65e6SPoul-Henning Kamp 	 * savecore(8) wants to write to our swapdev so we cannot
2716afeb65e6SPoul-Henning Kamp 	 * set an exclusive count :-(
2717afeb65e6SPoul-Henning Kamp 	 */
2718d2bae332SPoul-Henning Kamp 	error = g_access(cp, 1, 1, 0);
271988ad2d7bSKonstantin Belousov 	if (error != 0) {
2720dee34ca4SPoul-Henning Kamp 		g_detach(cp);
2721dee34ca4SPoul-Henning Kamp 		g_destroy_consumer(cp);
272288ad2d7bSKonstantin Belousov 		return (error);
2723dee34ca4SPoul-Henning Kamp 	}
2724dee34ca4SPoul-Henning Kamp 	nblks = pp->mediasize / DEV_BSIZE;
272588ad2d7bSKonstantin Belousov 	swaponsomething(vp, cp, nblks, swapgeom_strategy,
272688ad2d7bSKonstantin Belousov 	    swapgeom_close, dev2udev(dev),
27272cc718a1SKonstantin Belousov 	    (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
272888ad2d7bSKonstantin Belousov 	return (0);
2729dee34ca4SPoul-Henning Kamp }
2730dee34ca4SPoul-Henning Kamp 
2731dee34ca4SPoul-Henning Kamp static int
273288ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp)
2733dee34ca4SPoul-Henning Kamp {
2734dee34ca4SPoul-Henning Kamp 	int error;
2735dee34ca4SPoul-Henning Kamp 
2736cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
273788ad2d7bSKonstantin Belousov 	if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) {
273888ad2d7bSKonstantin Belousov 		error = ENOENT;
273988ad2d7bSKonstantin Belousov 	} else {
274088ad2d7bSKonstantin Belousov 		g_topology_lock();
274188ad2d7bSKonstantin Belousov 		error = swapongeom_locked(vp->v_rdev, vp);
274288ad2d7bSKonstantin Belousov 		g_topology_unlock();
274388ad2d7bSKonstantin Belousov 	}
274422db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
2745dee34ca4SPoul-Henning Kamp 	return (error);
2746dee34ca4SPoul-Henning Kamp }
2747dee34ca4SPoul-Henning Kamp 
2748dee34ca4SPoul-Henning Kamp /*
2749dee34ca4SPoul-Henning Kamp  * VNODE backend
2750dee34ca4SPoul-Henning Kamp  *
2751dee34ca4SPoul-Henning Kamp  * This is used mainly for network filesystem (read: probably only tested
2752dee34ca4SPoul-Henning Kamp  * with NFS) swapfiles.
2753dee34ca4SPoul-Henning Kamp  *
2754dee34ca4SPoul-Henning Kamp  */
2755dee34ca4SPoul-Henning Kamp 
2756dee34ca4SPoul-Henning Kamp static void
2757dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp)
2758dee34ca4SPoul-Henning Kamp {
2759494eb176SPoul-Henning Kamp 	struct vnode *vp2;
2760dee34ca4SPoul-Henning Kamp 
2761dee34ca4SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2762dee34ca4SPoul-Henning Kamp 
2763dee34ca4SPoul-Henning Kamp 	vp2 = sp->sw_id;
2764dee34ca4SPoul-Henning Kamp 	vhold(vp2);
2765dee34ca4SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
27663cfc7651SOlivier Houchard 		if (bp->b_bufobj)
2767494eb176SPoul-Henning Kamp 			bufobj_wdrop(bp->b_bufobj);
2768a76d8f4eSPoul-Henning Kamp 		bufobj_wref(&vp2->v_bufobj);
2769dee34ca4SPoul-Henning Kamp 	}
27703cfc7651SOlivier Houchard 	if (bp->b_bufobj != &vp2->v_bufobj)
27713cfc7651SOlivier Houchard 		bp->b_bufobj = &vp2->v_bufobj;
2772dee34ca4SPoul-Henning Kamp 	bp->b_vp = vp2;
27732c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
2774b792bebeSPoul-Henning Kamp 	bstrategy(bp);
2775dee34ca4SPoul-Henning Kamp 	return;
2776dee34ca4SPoul-Henning Kamp }
2777dee34ca4SPoul-Henning Kamp 
2778dee34ca4SPoul-Henning Kamp static void
2779dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp)
2780dee34ca4SPoul-Henning Kamp {
2781dee34ca4SPoul-Henning Kamp 
2782dee34ca4SPoul-Henning Kamp 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2783dee34ca4SPoul-Henning Kamp 	vrele(sp->sw_vp);
2784dee34ca4SPoul-Henning Kamp }
2785dee34ca4SPoul-Henning Kamp 
2786dee34ca4SPoul-Henning Kamp 
2787dee34ca4SPoul-Henning Kamp static int
2788dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2789dee34ca4SPoul-Henning Kamp {
2790dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2791dee34ca4SPoul-Henning Kamp 	int error;
2792dee34ca4SPoul-Henning Kamp 
2793dee34ca4SPoul-Henning Kamp 	if (nblks == 0)
2794dee34ca4SPoul-Henning Kamp 		return (ENXIO);
2795dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2796dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2797dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == vp) {
2798dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2799dee34ca4SPoul-Henning Kamp 			return (EBUSY);
2800dee34ca4SPoul-Henning Kamp 		}
2801dee34ca4SPoul-Henning Kamp 	}
2802dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2803dee34ca4SPoul-Henning Kamp 
2804cb05b60aSAttilio Rao 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2805dee34ca4SPoul-Henning Kamp #ifdef MAC
280630d239bcSRobert Watson 	error = mac_system_check_swapon(td->td_ucred, vp);
2807dee34ca4SPoul-Henning Kamp 	if (error == 0)
2808dee34ca4SPoul-Henning Kamp #endif
28099e223287SKonstantin Belousov 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
281022db15c0SAttilio Rao 	(void) VOP_UNLOCK(vp, 0);
2811dee34ca4SPoul-Henning Kamp 	if (error)
2812dee34ca4SPoul-Henning Kamp 		return (error);
2813dee34ca4SPoul-Henning Kamp 
2814dee34ca4SPoul-Henning Kamp 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
28152cc718a1SKonstantin Belousov 	    NODEV, 0);
2816dee34ca4SPoul-Henning Kamp 	return (0);
2817dee34ca4SPoul-Henning Kamp }
281889c241d1SGleb Smirnoff 
281989c241d1SGleb Smirnoff static int
282089c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
282189c241d1SGleb Smirnoff {
282289c241d1SGleb Smirnoff 	int error, new, n;
282389c241d1SGleb Smirnoff 
282489c241d1SGleb Smirnoff 	new = nsw_wcount_async_max;
282589c241d1SGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
282689c241d1SGleb Smirnoff 	if (error != 0 || req->newptr == NULL)
282789c241d1SGleb Smirnoff 		return (error);
282889c241d1SGleb Smirnoff 
282989c241d1SGleb Smirnoff 	if (new > nswbuf / 2 || new < 1)
283089c241d1SGleb Smirnoff 		return (EINVAL);
283189c241d1SGleb Smirnoff 
283289c241d1SGleb Smirnoff 	mtx_lock(&pbuf_mtx);
283389c241d1SGleb Smirnoff 	while (nsw_wcount_async_max != new) {
283489c241d1SGleb Smirnoff 		/*
283589c241d1SGleb Smirnoff 		 * Adjust difference.  If the current async count is too low,
283689c241d1SGleb Smirnoff 		 * we will need to sqeeze our update slowly in.  Sleep with a
283789c241d1SGleb Smirnoff 		 * higher priority than getpbuf() to finish faster.
283889c241d1SGleb Smirnoff 		 */
283989c241d1SGleb Smirnoff 		n = new - nsw_wcount_async_max;
284089c241d1SGleb Smirnoff 		if (nsw_wcount_async + n >= 0) {
284189c241d1SGleb Smirnoff 			nsw_wcount_async += n;
284289c241d1SGleb Smirnoff 			nsw_wcount_async_max += n;
284389c241d1SGleb Smirnoff 			wakeup(&nsw_wcount_async);
284489c241d1SGleb Smirnoff 		} else {
284589c241d1SGleb Smirnoff 			nsw_wcount_async_max -= nsw_wcount_async;
284689c241d1SGleb Smirnoff 			nsw_wcount_async = 0;
284789c241d1SGleb Smirnoff 			msleep(&nsw_wcount_async, &pbuf_mtx, PSWP,
284889c241d1SGleb Smirnoff 			    "swpsysctl", 0);
284989c241d1SGleb Smirnoff 		}
285089c241d1SGleb Smirnoff 	}
285189c241d1SGleb Smirnoff 	mtx_unlock(&pbuf_mtx);
285289c241d1SGleb Smirnoff 
285389c241d1SGleb Smirnoff 	return (0);
285489c241d1SGleb Smirnoff }
2855