xref: /freebsd/sys/vm/swap_pager.c (revision 2288078c5eafcfa1905ea32b13c5f30e295f0c6d)
160727d8bSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
41c7c3c6aSMatthew Dillon  * Copyright (c) 1998 Matthew Dillon,
526f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
6df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
7e9c0cc15SPoul-Henning Kamp  * Copyright (c) 1982, 1986, 1989, 1993
8df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
12df8bae1dSRodney W. Grimes  * Science Department.
13df8bae1dSRodney W. Grimes  *
14df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
15df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
16df8bae1dSRodney W. Grimes  * are met:
17df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
19df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
20df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
21df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
22df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
235929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
24df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
25df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
26df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
27df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
28df8bae1dSRodney W. Grimes  *    without specific prior written permission.
29df8bae1dSRodney W. Grimes  *
30df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
41df8bae1dSRodney W. Grimes  *
421c7c3c6aSMatthew Dillon  *				New Swap System
431c7c3c6aSMatthew Dillon  *				Matthew Dillon
441c7c3c6aSMatthew Dillon  *
451c7c3c6aSMatthew Dillon  * Radix Bitmap 'blists'.
461c7c3c6aSMatthew Dillon  *
471c7c3c6aSMatthew Dillon  *	- The new swapper uses the new radix bitmap code.  This should scale
481c7c3c6aSMatthew Dillon  *	  to arbitrarily small or arbitrarily large swap spaces and an almost
491c7c3c6aSMatthew Dillon  *	  arbitrary degree of fragmentation.
501c7c3c6aSMatthew Dillon  *
511c7c3c6aSMatthew Dillon  * Features:
521c7c3c6aSMatthew Dillon  *
531c7c3c6aSMatthew Dillon  *	- on the fly reallocation of swap during putpages.  The new system
541c7c3c6aSMatthew Dillon  *	  does not try to keep previously allocated swap blocks for dirty
551c7c3c6aSMatthew Dillon  *	  pages.
561c7c3c6aSMatthew Dillon  *
571c7c3c6aSMatthew Dillon  *	- on the fly deallocation of swap
581c7c3c6aSMatthew Dillon  *
591c7c3c6aSMatthew Dillon  *	- No more garbage collection required.  Unnecessarily allocated swap
601c7c3c6aSMatthew Dillon  *	  blocks only exist for dirty vm_page_t's now and these are already
611c7c3c6aSMatthew Dillon  *	  cycled (in a high-load system) by the pager.  We also do on-the-fly
621c7c3c6aSMatthew Dillon  *	  removal of invalidated swap blocks when a page is destroyed
631c7c3c6aSMatthew Dillon  *	  or renamed.
641c7c3c6aSMatthew Dillon  *
65df8bae1dSRodney W. Grimes  * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
66df8bae1dSRodney W. Grimes  *
67df8bae1dSRodney W. Grimes  *	@(#)swap_pager.c	8.9 (Berkeley) 3/21/94
68e9c0cc15SPoul-Henning Kamp  *	@(#)vm_swap.c	8.5 (Berkeley) 2/17/94
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71874651b1SDavid E. O'Brien #include <sys/cdefs.h>
72874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
73874651b1SDavid E. O'Brien 
74e9c0cc15SPoul-Henning Kamp #include "opt_vm.h"
75e9c0cc15SPoul-Henning Kamp 
76df8bae1dSRodney W. Grimes #include <sys/param.h>
779626b608SPoul-Henning Kamp #include <sys/bio.h>
78e2e050c8SConrad Meyer #include <sys/blist.h>
79df8bae1dSRodney W. Grimes #include <sys/buf.h>
80e2e050c8SConrad Meyer #include <sys/conf.h>
81e9c0cc15SPoul-Henning Kamp #include <sys/disk.h>
82504f5e29SDoug Moore #include <sys/disklabel.h>
83e2e050c8SConrad Meyer #include <sys/eventhandler.h>
84e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h>
85e2e050c8SConrad Meyer #include <sys/lock.h>
86e2e050c8SConrad Meyer #include <sys/kernel.h>
87e9c0cc15SPoul-Henning Kamp #include <sys/mount.h>
88e9c0cc15SPoul-Henning Kamp #include <sys/namei.h>
89df8bae1dSRodney W. Grimes #include <sys/malloc.h>
90f425ab8eSKonstantin Belousov #include <sys/pctrie.h>
91e2e050c8SConrad Meyer #include <sys/priv.h>
92e2e050c8SConrad Meyer #include <sys/proc.h>
931ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
943364c323SKonstantin Belousov #include <sys/resource.h>
953364c323SKonstantin Belousov #include <sys/resourcevar.h>
9689f6b863SAttilio Rao #include <sys/rwlock.h>
97d027ed2eSAlan Cox #include <sys/sbuf.h>
98327f4e83SMatthew Dillon #include <sys/sysctl.h>
99e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
100e2e050c8SConrad Meyer #include <sys/systm.h>
1010cddd8f0SMatthew Dillon #include <sys/sx.h>
102936524aaSMatthew Dillon #include <sys/vmmeter.h>
103e2e050c8SConrad Meyer #include <sys/vnode.h>
104df8bae1dSRodney W. Grimes 
105aed55708SRobert Watson #include <security/mac/mac_framework.h>
106aed55708SRobert Watson 
107df8bae1dSRodney W. Grimes #include <vm/vm.h>
10821cd6e62SSeigo Tanimura #include <vm/pmap.h>
10921cd6e62SSeigo Tanimura #include <vm/vm_map.h>
11021cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
111efeaf95aSDavid Greenman #include <vm/vm_object.h>
112df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
113efeaf95aSDavid Greenman #include <vm/vm_pager.h>
114df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
115e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
116df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
117efeaf95aSDavid Greenman #include <vm/vm_extern.h>
118670d17b5SJeff Roberson #include <vm/uma.h>
119df8bae1dSRodney W. Grimes 
120dee34ca4SPoul-Henning Kamp #include <geom/geom.h>
121dee34ca4SPoul-Henning Kamp 
122ec38b344SPoul-Henning Kamp /*
123064650c1SAlan Cox  * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64.
124064650c1SAlan Cox  * The 64-page limit is due to the radix code (kern/subr_blist.c).
125ec38b344SPoul-Henning Kamp  */
126ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
127e2241590SAlan Cox #define	MAX_PAGEOUT_CLUSTER	32
128ec38b344SPoul-Henning Kamp #endif
129ec38b344SPoul-Henning Kamp 
130ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
131ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
132ec38b344SPoul-Henning Kamp #endif
133ec38b344SPoul-Henning Kamp 
134f425ab8eSKonstantin Belousov #define	SWAP_META_PAGES		PCTRIE_COUNT
135ec38b344SPoul-Henning Kamp 
136f425ab8eSKonstantin Belousov /*
137f425ab8eSKonstantin Belousov  * A swblk structure maps each page index within a
138f425ab8eSKonstantin Belousov  * SWAP_META_PAGES-aligned and sized range to the address of an
139f425ab8eSKonstantin Belousov  * on-disk swap block (or SWAPBLK_NONE). The collection of these
140f425ab8eSKonstantin Belousov  * mappings for an entire vm object is implemented as a pc-trie.
141f425ab8eSKonstantin Belousov  */
142f425ab8eSKonstantin Belousov struct swblk {
143f425ab8eSKonstantin Belousov 	vm_pindex_t	p;
144f425ab8eSKonstantin Belousov 	daddr_t		d[SWAP_META_PAGES];
145e9c0cc15SPoul-Henning Kamp };
146e9c0cc15SPoul-Henning Kamp 
1472c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
14820da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx;
1498f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1508f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1518f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1528f60c087SPoul-Henning Kamp int swap_pager_avail;
15304533e1eSKonstantin Belousov static struct sx swdev_syscall_lock;	/* serialize swap(on|off) */
154e9c0cc15SPoul-Henning Kamp 
155e8bb589dSMatt Macy static u_long swap_reserved;
156e8bb589dSMatt Macy static u_long swap_total;
157e8bb589dSMatt Macy static int sysctl_page_shift(SYSCTL_HANDLER_ARGS);
158e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
159e8bb589dSMatt Macy     &swap_reserved, 0, sysctl_page_shift, "A",
1608a9c731fSIvan Voras     "Amount of swap storage needed to back all allocated anonymous memory.");
161e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
162e8bb589dSMatt Macy     &swap_total, 0, sysctl_page_shift, "A",
163e8bb589dSMatt Macy     "Total amount of available swap storage.");
164e8bb589dSMatt Macy 
1653364c323SKonstantin Belousov static int overcommit = 0;
166be7d4ac5SEdward Tomasz Napierala SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0,
1678a9c731fSIvan Voras     "Configure virtual memory overcommit behavior. See tuning(7) "
1688a9c731fSIvan Voras     "for details.");
16961203277SDag-Erling Smørgrav static unsigned long swzone;
17061203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
17161203277SDag-Erling Smørgrav     "Actual size of swap metadata zone");
17261203277SDag-Erling Smørgrav static unsigned long swap_maxpages;
17361203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
17461203277SDag-Erling Smørgrav     "Maximum amount of swap supported");
1753364c323SKonstantin Belousov 
1763364c323SKonstantin Belousov /* bits from overcommit */
1773364c323SKonstantin Belousov #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
1783364c323SKonstantin Belousov #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
1793364c323SKonstantin Belousov #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
1803364c323SKonstantin Belousov 
181e8bb589dSMatt Macy static int
182e8bb589dSMatt Macy sysctl_page_shift(SYSCTL_HANDLER_ARGS)
183e8bb589dSMatt Macy {
184e8bb589dSMatt Macy 	uint64_t newval;
185e8bb589dSMatt Macy 	u_long value = *(u_long *)arg1;
186e8bb589dSMatt Macy 
187e8bb589dSMatt Macy 	newval = ((uint64_t)value) << PAGE_SHIFT;
188e8bb589dSMatt Macy 	return (sysctl_handle_64(oidp, &newval, 0, req));
189e8bb589dSMatt Macy }
190e8bb589dSMatt Macy 
1913364c323SKonstantin Belousov int
1923364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr)
1933364c323SKonstantin Belousov {
1943364c323SKonstantin Belousov 
195ef694c1aSEdward Tomasz Napierala 	return (swap_reserve_by_cred(incr, curthread->td_ucred));
1963364c323SKonstantin Belousov }
1973364c323SKonstantin Belousov 
1983364c323SKonstantin Belousov int
199ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
2003364c323SKonstantin Belousov {
201e8bb589dSMatt Macy 	u_long r, s, prev, pincr;
2023364c323SKonstantin Belousov 	int res, error;
2033364c323SKonstantin Belousov 	static int curfail;
2043364c323SKonstantin Belousov 	static struct timeval lastfail;
205ef694c1aSEdward Tomasz Napierala 	struct uidinfo *uip;
206ef694c1aSEdward Tomasz Napierala 
207ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
2083364c323SKonstantin Belousov 
209e8bb589dSMatt Macy 	KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
210e8bb589dSMatt Macy 	    (uintmax_t)incr));
2113364c323SKonstantin Belousov 
212afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2134b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
2141ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2151ba5ad42SEdward Tomasz Napierala 		error = racct_add(curproc, RACCT_SWAP, incr);
2161ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2171ba5ad42SEdward Tomasz Napierala 		if (error != 0)
2181ba5ad42SEdward Tomasz Napierala 			return (0);
2194b5c9cf6SEdward Tomasz Napierala 	}
220afcc55f3SEdward Tomasz Napierala #endif
2211ba5ad42SEdward Tomasz Napierala 
222e8bb589dSMatt Macy 	pincr = atop(incr);
2233364c323SKonstantin Belousov 	res = 0;
224e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&swap_reserved, pincr);
225e8bb589dSMatt Macy 	r = prev + pincr;
2263364c323SKonstantin Belousov 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
227e958ad4cSJeff Roberson 		s = vm_cnt.v_page_count - vm_cnt.v_free_reserved -
228e958ad4cSJeff Roberson 		    vm_wire_count();
2293364c323SKonstantin Belousov 	} else
2303364c323SKonstantin Belousov 		s = 0;
2313364c323SKonstantin Belousov 	s += swap_total;
2323364c323SKonstantin Belousov 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
2333364c323SKonstantin Belousov 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
2343364c323SKonstantin Belousov 		res = 1;
235e8bb589dSMatt Macy 	} else {
236e8bb589dSMatt Macy 		prev = atomic_fetchadd_long(&swap_reserved, -pincr);
237e8bb589dSMatt Macy 		if (prev < pincr)
238e8bb589dSMatt Macy 			panic("swap_reserved < incr on overcommit fail");
2393364c323SKonstantin Belousov 	}
2403364c323SKonstantin Belousov 	if (res) {
241e8bb589dSMatt Macy 		prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr);
2425c0e1c11SKonstantin Belousov 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
243e8bb589dSMatt Macy 		    prev + pincr > lim_cur(curthread, RLIMIT_SWAP) &&
244e8bb589dSMatt Macy 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) {
2453364c323SKonstantin Belousov 			res = 0;
246e8bb589dSMatt Macy 			prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr);
247e8bb589dSMatt Macy 			if (prev < pincr)
248e8bb589dSMatt Macy 				panic("uip->ui_vmsize < incr on overcommit fail");
2493364c323SKonstantin Belousov 		}
2503364c323SKonstantin Belousov 	}
2513364c323SKonstantin Belousov 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
2523364c323SKonstantin Belousov 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
253134465d7SKonstantin Belousov 		    uip->ui_uid, curproc->p_pid, incr);
2543364c323SKonstantin Belousov 	}
2553364c323SKonstantin Belousov 
256afcc55f3SEdward Tomasz Napierala #ifdef RACCT
257e8bb589dSMatt Macy 	if (racct_enable && !res) {
2581ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2591ba5ad42SEdward Tomasz Napierala 		racct_sub(curproc, RACCT_SWAP, incr);
2601ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2611ba5ad42SEdward Tomasz Napierala 	}
262afcc55f3SEdward Tomasz Napierala #endif
2631ba5ad42SEdward Tomasz Napierala 
2643364c323SKonstantin Belousov 	return (res);
2653364c323SKonstantin Belousov }
2663364c323SKonstantin Belousov 
2673364c323SKonstantin Belousov void
2683364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr)
2693364c323SKonstantin Belousov {
2703364c323SKonstantin Belousov 	struct uidinfo *uip;
271e8bb589dSMatt Macy 	u_long pincr;
2723364c323SKonstantin Belousov 
273e8bb589dSMatt Macy 	KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
274e8bb589dSMatt Macy 	    (uintmax_t)incr));
2753364c323SKonstantin Belousov 
276e8bb589dSMatt Macy 	PROC_LOCK(curproc);
277afcc55f3SEdward Tomasz Napierala #ifdef RACCT
278e8bb589dSMatt Macy 	if (racct_enable)
2791ba5ad42SEdward Tomasz Napierala 		racct_add_force(curproc, RACCT_SWAP, incr);
280afcc55f3SEdward Tomasz Napierala #endif
281e8bb589dSMatt Macy 	pincr = atop(incr);
282e8bb589dSMatt Macy 	atomic_add_long(&swap_reserved, pincr);
283e8bb589dSMatt Macy 	uip = curproc->p_ucred->cr_ruidinfo;
284e8bb589dSMatt Macy 	atomic_add_long(&uip->ui_vmsize, pincr);
2853364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2863364c323SKonstantin Belousov }
2873364c323SKonstantin Belousov 
2883364c323SKonstantin Belousov void
2893364c323SKonstantin Belousov swap_release(vm_ooffset_t decr)
2903364c323SKonstantin Belousov {
291ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
2923364c323SKonstantin Belousov 
2933364c323SKonstantin Belousov 	PROC_LOCK(curproc);
294e8bb589dSMatt Macy 	cred = curproc->p_ucred;
295ef694c1aSEdward Tomasz Napierala 	swap_release_by_cred(decr, cred);
2963364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2973364c323SKonstantin Belousov }
2983364c323SKonstantin Belousov 
2993364c323SKonstantin Belousov void
300ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
3013364c323SKonstantin Belousov {
302e8bb589dSMatt Macy 	u_long prev, pdecr;
303ef694c1aSEdward Tomasz Napierala  	struct uidinfo *uip;
304ef694c1aSEdward Tomasz Napierala 
305ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
3063364c323SKonstantin Belousov 
307e8bb589dSMatt Macy 	KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", __func__,
308e8bb589dSMatt Macy 	    (uintmax_t)decr));
3093364c323SKonstantin Belousov 
310e8bb589dSMatt Macy 	pdecr = atop(decr);
311e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&swap_reserved, -pdecr);
312e8bb589dSMatt Macy 	if (prev < pdecr)
3133364c323SKonstantin Belousov 		panic("swap_reserved < decr");
3143364c323SKonstantin Belousov 
315e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr);
316e8bb589dSMatt Macy 	if (prev < pdecr)
3173364c323SKonstantin Belousov 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
318e8bb589dSMatt Macy #ifdef RACCT
319e8bb589dSMatt Macy 	if (racct_enable)
3201ba5ad42SEdward Tomasz Napierala 		racct_sub_cred(cred, RACCT_SWAP, decr);
321e8bb589dSMatt Macy #endif
3223364c323SKonstantin Belousov }
3233364c323SKonstantin Belousov 
3244abca9bbSAlan Cox #define SWM_POP		0x01	/* pop out			*/
32526f9a767SRodney W. Grimes 
326f08b3099SKonstantin Belousov static int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
3277dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
328756a5412SGleb Smirnoff static struct mtx swbuf_mtx;	/* to sync nsw_wcount_async */
329756a5412SGleb Smirnoff static int nsw_wcount_async;	/* limit async write buffers */
330327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
331327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
3321c7c3c6aSMatthew Dillon 
33389c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
3344c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
3354c36e917SKonstantin Belousov     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
3364c36e917SKonstantin Belousov     "Maximum running async swap ops");
337d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS);
338d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD |
339d027ed2eSAlan Cox     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A",
340d027ed2eSAlan Cox     "Swap Fragmentation Info");
34189c241d1SGleb Smirnoff 
3420cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
343327f4e83SMatthew Dillon 
3441c7c3c6aSMatthew Dillon /*
3451c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
3461c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
3471c7c3c6aSMatthew Dillon  */
3481c7c3c6aSMatthew Dillon 
3491c7c3c6aSMatthew Dillon #define NOBJLISTS		8
3501c7c3c6aSMatthew Dillon 
3511c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
352af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
3531c7c3c6aSMatthew Dillon 
3541c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
355756a5412SGleb Smirnoff static uma_zone_t swwbuf_zone;
356756a5412SGleb Smirnoff static uma_zone_t swrbuf_zone;
357f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone;
358f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone;
3591c7c3c6aSMatthew Dillon 
3601c7c3c6aSMatthew Dillon /*
3611c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
3621c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
3631c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
3641c7c3c6aSMatthew Dillon  */
365ff98689dSBruce Evans static vm_object_t
36611caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
3673364c323SKonstantin Belousov 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
36811caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
369b0cd2017SGleb Smirnoff static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
370b0cd2017SGleb Smirnoff     int *);
371b0cd2017SGleb Smirnoff static int	swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
372b0cd2017SGleb Smirnoff     int *, pgo_getpages_iodone_t, void *);
373751221fdSPoul-Henning Kamp static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
3745ea4972cSAlan Cox static boolean_t
3755ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
37611caded3SAlfred Perlstein static void	swap_pager_init(void);
37711caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
378b3fed13eSDavid Schultz static void	swap_pager_swapoff(struct swdevt *sp);
379fe7bcbafSKyle Evans static void	swap_pager_update_writecount(vm_object_t object,
380fe7bcbafSKyle Evans     vm_offset_t start, vm_offset_t end);
381fe7bcbafSKyle Evans static void	swap_pager_release_writecount(vm_object_t object,
382fe7bcbafSKyle Evans     vm_offset_t start, vm_offset_t end);
383f708ef1bSPoul-Henning Kamp 
384df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
385e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
386e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
387e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
388e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
38990effb23SGleb Smirnoff 	.pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)		*/
390e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
391e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
392e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
393fe7bcbafSKyle Evans 	.pgo_update_writecount = swap_pager_update_writecount,
394fe7bcbafSKyle Evans 	.pgo_release_writecount = swap_pager_release_writecount,
395df8bae1dSRodney W. Grimes };
396df8bae1dSRodney W. Grimes 
3971c7c3c6aSMatthew Dillon /*
3981c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
3991c7c3c6aSMatthew Dillon  * internal.
4001c7c3c6aSMatthew Dillon  */
401e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
402e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
40326f9a767SRodney W. Grimes 
40407c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
40507c348eaSAlan Cox     "Maximum size of a swap block in pages");
406cee313c4SRobert Watson 
407a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
40811caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
409230869e0SAlan Cox static bool	swp_pager_swblk_empty(struct swblk *sb, int start, int limit);
41088ad2d7bSKonstantin Belousov static int	swapongeom(struct vnode *);
41159efee01SPoul-Henning Kamp static int	swaponvp(struct thread *, struct vnode *, u_long);
41235918c55SChristian S.J. Peron static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
41324a1cce3SDavid Greenman 
4141c7c3c6aSMatthew Dillon /*
4151c7c3c6aSMatthew Dillon  * Swap bitmap functions
4161c7c3c6aSMatthew Dillon  */
417230869e0SAlan Cox static void	swp_pager_freeswapspace(daddr_t blk, daddr_t npages);
41848e98a2aSDoug Moore static daddr_t	swp_pager_getswapspace(int *npages, int limit);
4191c7c3c6aSMatthew Dillon 
4201c7c3c6aSMatthew Dillon /*
4211c7c3c6aSMatthew Dillon  * Metadata functions
4221c7c3c6aSMatthew Dillon  */
42378f1deefSAlan Cox static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
4242e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
42511caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
42611caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
4271c7c3c6aSMatthew Dillon 
42878f1deefSAlan Cox static void
42978f1deefSAlan Cox swp_pager_init_freerange(daddr_t *start, daddr_t *num)
43078f1deefSAlan Cox {
43178f1deefSAlan Cox 
43278f1deefSAlan Cox 	*start = SWAPBLK_NONE;
43378f1deefSAlan Cox 	*num = 0;
43478f1deefSAlan Cox }
43578f1deefSAlan Cox 
43678f1deefSAlan Cox static void
43778f1deefSAlan Cox swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr)
43878f1deefSAlan Cox {
43978f1deefSAlan Cox 
44078f1deefSAlan Cox 	if (*start + *num == addr) {
44178f1deefSAlan Cox 		(*num)++;
44278f1deefSAlan Cox 	} else {
44378f1deefSAlan Cox 		swp_pager_freeswapspace(*start, *num);
44478f1deefSAlan Cox 		*start = addr;
44578f1deefSAlan Cox 		*num = 1;
44678f1deefSAlan Cox 	}
44778f1deefSAlan Cox }
44878f1deefSAlan Cox 
449f425ab8eSKonstantin Belousov static void *
450f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree)
451f425ab8eSKonstantin Belousov {
452f425ab8eSKonstantin Belousov 
453f425ab8eSKonstantin Belousov 	return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ?
454f425ab8eSKonstantin Belousov 	    M_USE_RESERVE : 0)));
455f425ab8eSKonstantin Belousov }
456f425ab8eSKonstantin Belousov 
457f425ab8eSKonstantin Belousov static void
458f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node)
459f425ab8eSKonstantin Belousov {
460f425ab8eSKonstantin Belousov 
461f425ab8eSKonstantin Belousov 	uma_zfree(swpctrie_zone, node);
462f425ab8eSKonstantin Belousov }
463f425ab8eSKonstantin Belousov 
464f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free);
465f425ab8eSKonstantin Belousov 
4661c7c3c6aSMatthew Dillon /*
4671c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
4681c7c3c6aSMatthew Dillon  *
46920d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
47020d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
47120d3034fSMatthew Dillon  *
47220d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
4731c7c3c6aSMatthew Dillon  *
4741c7c3c6aSMatthew Dillon  *	No restrictions on call
4751c7c3c6aSMatthew Dillon  *	This routine may not block.
4761c7c3c6aSMatthew Dillon  */
477a5edd34aSPoul-Henning Kamp static void
4782f249180SPoul-Henning Kamp swp_sizecheck(void)
4790d94caffSDavid Greenman {
48023955314SAlfred Perlstein 
4818f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
48220d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
4831af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
48420d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
4852b0d37a4SMatthew Dillon 		}
48620d3034fSMatthew Dillon 	} else {
48726f9a767SRodney W. Grimes 		swap_pager_full = 0;
4888f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
48920d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
49026f9a767SRodney W. Grimes 	}
4911c7c3c6aSMatthew Dillon }
4921c7c3c6aSMatthew Dillon 
4931c7c3c6aSMatthew Dillon /*
4941c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
4951c7c3c6aSMatthew Dillon  *
4961c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
4971c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
4981c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
4991c7c3c6aSMatthew Dillon  */
500f5a12711SPoul-Henning Kamp static void
5012f249180SPoul-Henning Kamp swap_pager_init(void)
502df8bae1dSRodney W. Grimes {
5031c7c3c6aSMatthew Dillon 	/*
5041c7c3c6aSMatthew Dillon 	 * Initialize object lists
5051c7c3c6aSMatthew Dillon 	 */
5061c7c3c6aSMatthew Dillon 	int i;
5071c7c3c6aSMatthew Dillon 
5081c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
5091c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
51020da9c2eSPoul-Henning Kamp 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
51115719273SKonstantin Belousov 	sx_init(&sw_alloc_sx, "swspsx");
51204533e1eSKonstantin Belousov 	sx_init(&swdev_syscall_lock, "swsysc");
5131c7c3c6aSMatthew Dillon }
51426f9a767SRodney W. Grimes 
515df8bae1dSRodney W. Grimes /*
5161c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
5171c7c3c6aSMatthew Dillon  *
5181c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
5191c7c3c6aSMatthew Dillon  *	its main loop.
520df8bae1dSRodney W. Grimes  */
52124a1cce3SDavid Greenman void
5222f249180SPoul-Henning Kamp swap_pager_swap_init(void)
523df8bae1dSRodney W. Grimes {
52461203277SDag-Erling Smørgrav 	unsigned long n, n2;
5250d94caffSDavid Greenman 
52626f9a767SRodney W. Grimes 	/*
5271c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
5281c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
5291c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
5301c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
5311c7c3c6aSMatthew Dillon 	 *
532327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
5337b9bcad9SDoug Moore 	 * array, which has MAXPHYS / PAGE_SIZE entries, and our locally
5347b9bcad9SDoug Moore 	 * defined MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
5351c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
536327f4e83SMatthew Dillon 	 *
537327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
538327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
539327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
540327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
541327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
542327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
543327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
544327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
545327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
546327f4e83SMatthew Dillon 	 * So it all works out pretty well.
54726f9a767SRodney W. Grimes 	 */
5480cab71bcSDoug Moore 	nsw_cluster_max = min(MAXPHYS / PAGE_SIZE, MAX_PAGEOUT_CLUSTER);
549327f4e83SMatthew Dillon 
550327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
551327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
552756a5412SGleb Smirnoff 	mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF);
553756a5412SGleb Smirnoff 
554756a5412SGleb Smirnoff 	swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4);
555756a5412SGleb Smirnoff 	swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2);
55624a1cce3SDavid Greenman 
5571c7c3c6aSMatthew Dillon 	/*
558a8233027SKonstantin Belousov 	 * Initialize our zone, taking the user's requested size or
559a8233027SKonstantin Belousov 	 * estimating the number we need based on the number of pages
560a8233027SKonstantin Belousov 	 * in the system.
5611c7c3c6aSMatthew Dillon 	 */
562a8233027SKonstantin Belousov 	n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) :
563a8233027SKonstantin Belousov 	    vm_cnt.v_page_count / 2;
564f425ab8eSKonstantin Belousov 	swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL,
565f5fbe90dSAlan Cox 	    pctrie_zone_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM);
566f425ab8eSKonstantin Belousov 	if (swpctrie_zone == NULL)
567f425ab8eSKonstantin Belousov 		panic("failed to create swap pctrie zone.");
568f425ab8eSKonstantin Belousov 	swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL,
569f5fbe90dSAlan Cox 	    NULL, NULL, _Alignof(struct swblk) - 1, UMA_ZONE_VM);
570f425ab8eSKonstantin Belousov 	if (swblk_zone == NULL)
571f425ab8eSKonstantin Belousov 		panic("failed to create swap blk zone.");
57222a5e6b9SDag-Erling Smørgrav 	n2 = n;
5738355f576SJeff Roberson 	do {
574f425ab8eSKonstantin Belousov 		if (uma_zone_reserve_kva(swblk_zone, n))
57561ce6eeeSAlfred Perlstein 			break;
57661ce6eeeSAlfred Perlstein 		/*
57761ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
57861ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
57961ce6eeeSAlfred Perlstein 		 */
58061ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
58161ce6eeeSAlfred Perlstein 	} while (n > 0);
58253faf5a7SKonstantin Belousov 
58353faf5a7SKonstantin Belousov 	/*
58453faf5a7SKonstantin Belousov 	 * Often uma_zone_reserve_kva() cannot reserve exactly the
58553faf5a7SKonstantin Belousov 	 * requested size.  Account for the difference when
58653faf5a7SKonstantin Belousov 	 * calculating swap_maxpages.
58753faf5a7SKonstantin Belousov 	 */
58853faf5a7SKonstantin Belousov 	n = uma_zone_get_max(swblk_zone);
58953faf5a7SKonstantin Belousov 
5901fffcd75SKonstantin Belousov 	if (n < n2)
591a8233027SKonstantin Belousov 		printf("Swap blk zone entries changed from %lu to %lu.\n",
592f425ab8eSKonstantin Belousov 		    n2, n);
59361203277SDag-Erling Smørgrav 	swap_maxpages = n * SWAP_META_PAGES;
594f425ab8eSKonstantin Belousov 	swzone = n * sizeof(struct swblk);
595f425ab8eSKonstantin Belousov 	if (!uma_zone_reserve_kva(swpctrie_zone, n))
596f425ab8eSKonstantin Belousov 		printf("Cannot reserve swap pctrie zone, "
597f425ab8eSKonstantin Belousov 		    "reduce kern.maxswzone.\n");
59824a1cce3SDavid Greenman }
59924a1cce3SDavid Greenman 
600eb4d6a1bSKonstantin Belousov static vm_object_t
601eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size,
602eb4d6a1bSKonstantin Belousov     vm_ooffset_t offset)
603eb4d6a1bSKonstantin Belousov {
604eb4d6a1bSKonstantin Belousov 	vm_object_t object;
605eb4d6a1bSKonstantin Belousov 
606eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
607eb4d6a1bSKonstantin Belousov 		if (!swap_reserve_by_cred(size, cred))
608eb4d6a1bSKonstantin Belousov 			return (NULL);
609eb4d6a1bSKonstantin Belousov 		crhold(cred);
610eb4d6a1bSKonstantin Belousov 	}
611f425ab8eSKonstantin Belousov 
612f425ab8eSKonstantin Belousov 	/*
613f425ab8eSKonstantin Belousov 	 * The un_pager.swp.swp_blks trie is initialized by
614f425ab8eSKonstantin Belousov 	 * vm_object_allocate() to ensure the correct order of
615f425ab8eSKonstantin Belousov 	 * visibility to other threads.
616f425ab8eSKonstantin Belousov 	 */
617eb4d6a1bSKonstantin Belousov 	object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset +
618eb4d6a1bSKonstantin Belousov 	    PAGE_MASK + size));
619f425ab8eSKonstantin Belousov 
620fe7bcbafSKyle Evans 	object->un_pager.swp.writemappings = 0;
621eb4d6a1bSKonstantin Belousov 	object->handle = handle;
622eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
623eb4d6a1bSKonstantin Belousov 		object->cred = cred;
624eb4d6a1bSKonstantin Belousov 		object->charge = size;
625eb4d6a1bSKonstantin Belousov 	}
626eb4d6a1bSKonstantin Belousov 	return (object);
627eb4d6a1bSKonstantin Belousov }
628eb4d6a1bSKonstantin Belousov 
62924a1cce3SDavid Greenman /*
6301c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
6311c7c3c6aSMatthew Dillon  *			its metadata structures.
6321c7c3c6aSMatthew Dillon  *
6331c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
634eb4d6a1bSKonstantin Belousov  *	OBJT_SWAP object.
6351c7c3c6aSMatthew Dillon  *
636eb4d6a1bSKonstantin Belousov  *	This routine must ensure that no live duplicate is created for
637eb4d6a1bSKonstantin Belousov  *	the named object request, which is protected against by
638eb4d6a1bSKonstantin Belousov  *	holding the sw_alloc_sx lock in case handle != NULL.
63924a1cce3SDavid Greenman  */
640f5a12711SPoul-Henning Kamp static vm_object_t
6416cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
6423364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
64324a1cce3SDavid Greenman {
64424a1cce3SDavid Greenman 	vm_object_t object;
6452f7af3dbSAlan Cox 
646eb4d6a1bSKonstantin Belousov 	if (handle != NULL) {
6471c7c3c6aSMatthew Dillon 		/*
6481c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
6491c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
6501c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
6511c7c3c6aSMatthew Dillon 		 * of the handle.
6521c7c3c6aSMatthew Dillon 		 */
6530cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
6541c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
655b5e8f167SAlan Cox 		if (object == NULL) {
656eb4d6a1bSKonstantin Belousov 			object = swap_pager_alloc_init(handle, cred, size,
657eb4d6a1bSKonstantin Belousov 			    offset);
658eb4d6a1bSKonstantin Belousov 			if (object != NULL) {
659eb4d6a1bSKonstantin Belousov 				TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
660eb4d6a1bSKonstantin Belousov 				    object, pager_object_list);
6613364c323SKonstantin Belousov 			}
66224a1cce3SDavid Greenman 		}
6630cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
66424a1cce3SDavid Greenman 	} else {
665eb4d6a1bSKonstantin Belousov 		object = swap_pager_alloc_init(handle, cred, size, offset);
66624a1cce3SDavid Greenman 	}
66724a1cce3SDavid Greenman 	return (object);
668df8bae1dSRodney W. Grimes }
669df8bae1dSRodney W. Grimes 
67026f9a767SRodney W. Grimes /*
6711c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
6721c7c3c6aSMatthew Dillon  *
6731c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
6741c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
6751c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
6761c7c3c6aSMatthew Dillon  *	about to be destroyed.
6771c7c3c6aSMatthew Dillon  *
67815523cf7SKonstantin Belousov  *	The object must be locked.
67926f9a767SRodney W. Grimes  */
680df8bae1dSRodney W. Grimes static void
6812f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object)
68226f9a767SRodney W. Grimes {
6834dcc5c2dSMatthew Dillon 
684eb4d6a1bSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
685eb4d6a1bSKonstantin Belousov 	KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
686eb4d6a1bSKonstantin Belousov 
68726f9a767SRodney W. Grimes 	/*
6881c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
6891c7c3c6aSMatthew Dillon 	 * pageout completion.
69026f9a767SRodney W. Grimes 	 */
691bd228075SAlan Cox 	if (object->handle != NULL) {
692eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
693eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
694eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(object->handle), object,
695eb4d6a1bSKonstantin Belousov 		    pager_object_list);
696eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
697eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(object);
698bd228075SAlan Cox 	}
6991c7c3c6aSMatthew Dillon 
7001c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
7011c7c3c6aSMatthew Dillon 
7021c7c3c6aSMatthew Dillon 	/*
7031c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
7041c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
7051c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
7061c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
7071c7c3c6aSMatthew Dillon 	 */
7081c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
709e735691bSJohn Baldwin 	object->handle = NULL;
710e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
7111c7c3c6aSMatthew Dillon }
7121c7c3c6aSMatthew Dillon 
7131c7c3c6aSMatthew Dillon /************************************************************************
7141c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
7151c7c3c6aSMatthew Dillon  ************************************************************************/
7161c7c3c6aSMatthew Dillon 
7171c7c3c6aSMatthew Dillon /*
7181c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
7191c7c3c6aSMatthew Dillon  *
72048e98a2aSDoug Moore  *	Allocate swap for up to the requested number of pages, and at
72148e98a2aSDoug Moore  *	least a minimum number of pages.  The starting swap block number
72248e98a2aSDoug Moore  *	(a page index) is returned or SWAPBLK_NONE if the allocation
72348e98a2aSDoug Moore  *	failed.
7241c7c3c6aSMatthew Dillon  *
7251c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
7261c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
7271c7c3c6aSMatthew Dillon  *
72815523cf7SKonstantin Belousov  *	This routine may not sleep.
7298f60c087SPoul-Henning Kamp  *
7308f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
7311c7c3c6aSMatthew Dillon  */
732a5edd34aSPoul-Henning Kamp static daddr_t
73348e98a2aSDoug Moore swp_pager_getswapspace(int *io_npages, int limit)
7341c7c3c6aSMatthew Dillon {
7351c7c3c6aSMatthew Dillon 	daddr_t blk;
7368f60c087SPoul-Henning Kamp 	struct swdevt *sp;
73787ae0686SDoug Moore 	int mpages, npages;
7381c7c3c6aSMatthew Dillon 
7398f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
74031c82722SDoug Moore 	mpages = *io_npages;
74131c82722SDoug Moore 	npages = imin(BLIST_MAX_ALLOC, mpages);
74220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7438f60c087SPoul-Henning Kamp 	sp = swdevhd;
74448e98a2aSDoug Moore 	while (!TAILQ_EMPTY(&swtailq)) {
7458f60c087SPoul-Henning Kamp 		if (sp == NULL)
7468f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
74748e98a2aSDoug Moore 		if ((sp->sw_flags & SW_CLOSING) == 0)
74887ae0686SDoug Moore 			blk = blist_alloc(sp->sw_blist, &npages, mpages);
74948e98a2aSDoug Moore 		if (blk != SWAPBLK_NONE)
75048e98a2aSDoug Moore 			break;
75148e98a2aSDoug Moore 		sp = TAILQ_NEXT(sp, sw_list);
75248e98a2aSDoug Moore 		if (swdevhd == sp) {
75348e98a2aSDoug Moore 			if (npages <= limit)
75448e98a2aSDoug Moore 				break;
75587ae0686SDoug Moore 			mpages = npages - 1;
75648e98a2aSDoug Moore 			npages >>= 1;
75748e98a2aSDoug Moore 		}
75848e98a2aSDoug Moore 	}
7598f60c087SPoul-Henning Kamp 	if (blk != SWAPBLK_NONE) {
76048e98a2aSDoug Moore 		*io_npages = npages;
7618f60c087SPoul-Henning Kamp 		blk += sp->sw_first;
7628f60c087SPoul-Henning Kamp 		sp->sw_used += npages;
763d05bc129SAlan Cox 		swap_pager_avail -= npages;
7648f60c087SPoul-Henning Kamp 		swp_sizecheck();
7658f60c087SPoul-Henning Kamp 		swdevhd = TAILQ_NEXT(sp, sw_list);
76648e98a2aSDoug Moore 	} else {
7672b0d37a4SMatthew Dillon 		if (swap_pager_full != 2) {
76848e98a2aSDoug Moore 			printf("swp_pager_getswapspace(%d): failed\n",
76948e98a2aSDoug Moore 			    *io_npages);
7702b0d37a4SMatthew Dillon 			swap_pager_full = 2;
77120d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
7722b0d37a4SMatthew Dillon 		}
7738f60c087SPoul-Henning Kamp 		swdevhd = NULL;
77448e98a2aSDoug Moore 	}
775d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
7761c7c3c6aSMatthew Dillon 	return (blk);
77726f9a767SRodney W. Grimes }
77826f9a767SRodney W. Grimes 
779541a1175SAlan Cox static bool
780b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp)
7818f60c087SPoul-Henning Kamp {
7828f60c087SPoul-Henning Kamp 
783b3fed13eSDavid Schultz 	return (blk >= sp->sw_first && blk < sp->sw_end);
7848f60c087SPoul-Henning Kamp }
7858f60c087SPoul-Henning Kamp 
7864b03903aSPoul-Henning Kamp static void
7874b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp)
7884b03903aSPoul-Henning Kamp {
7894b03903aSPoul-Henning Kamp 	struct swdevt *sp;
7904b03903aSPoul-Henning Kamp 
79120da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7924b03903aSPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
793541a1175SAlan Cox 		if (swp_pager_isondev(bp->b_blkno, sp)) {
79420da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
7952cc718a1SKonstantin Belousov 			if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
7962cc718a1SKonstantin Belousov 			    unmapped_buf_allowed) {
7972cc718a1SKonstantin Belousov 				bp->b_data = unmapped_buf;
7982cc718a1SKonstantin Belousov 				bp->b_offset = 0;
7992cc718a1SKonstantin Belousov 			} else {
8002cc718a1SKonstantin Belousov 				pmap_qenter((vm_offset_t)bp->b_data,
8012cc718a1SKonstantin Belousov 				    &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
8022cc718a1SKonstantin Belousov 			}
8034b03903aSPoul-Henning Kamp 			sp->sw_strategy(bp, sp);
8044b03903aSPoul-Henning Kamp 			return;
8054b03903aSPoul-Henning Kamp 		}
8064b03903aSPoul-Henning Kamp 	}
80720da9c2eSPoul-Henning Kamp 	panic("Swapdev not found");
8084b03903aSPoul-Henning Kamp }
8094b03903aSPoul-Henning Kamp 
8108f60c087SPoul-Henning Kamp 
81126f9a767SRodney W. Grimes /*
8121c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
8131c7c3c6aSMatthew Dillon  *
8141c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
8151c7c3c6aSMatthew Dillon  *
81615523cf7SKonstantin Belousov  *	This routine may not sleep.
81726f9a767SRodney W. Grimes  */
818a5edd34aSPoul-Henning Kamp static void
819230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages)
8200d94caffSDavid Greenman {
8218f60c087SPoul-Henning Kamp 	struct swdevt *sp;
82292da00bbSMatthew Dillon 
823230869e0SAlan Cox 	if (npages == 0)
824230869e0SAlan Cox 		return;
8257645e885SAlan Cox 	mtx_lock(&sw_dev_mtx);
8267645e885SAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
827541a1175SAlan Cox 		if (swp_pager_isondev(blk, sp)) {
82892da00bbSMatthew Dillon 			sp->sw_used -= npages;
82992da00bbSMatthew Dillon 			/*
8307645e885SAlan Cox 			 * If we are attempting to stop swapping on
8317645e885SAlan Cox 			 * this device, we don't want to mark any
8327645e885SAlan Cox 			 * blocks free lest they be reused.
83392da00bbSMatthew Dillon 			 */
8347645e885SAlan Cox 			if ((sp->sw_flags & SW_CLOSING) == 0) {
8357645e885SAlan Cox 				blist_free(sp->sw_blist, blk - sp->sw_first,
8367645e885SAlan Cox 				    npages);
8378f60c087SPoul-Henning Kamp 				swap_pager_avail += npages;
8381c7c3c6aSMatthew Dillon 				swp_sizecheck();
83926f9a767SRodney W. Grimes 			}
8407645e885SAlan Cox 			mtx_unlock(&sw_dev_mtx);
8417645e885SAlan Cox 			return;
8427645e885SAlan Cox 		}
8437645e885SAlan Cox 	}
8447645e885SAlan Cox 	panic("Swapdev not found");
8457645e885SAlan Cox }
8461c7c3c6aSMatthew Dillon 
84726f9a767SRodney W. Grimes /*
848d027ed2eSAlan Cox  * SYSCTL_SWAP_FRAGMENTATION() -	produce raw swap space stats
849d027ed2eSAlan Cox  */
850d027ed2eSAlan Cox static int
851d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS)
852d027ed2eSAlan Cox {
853d027ed2eSAlan Cox 	struct sbuf sbuf;
854d027ed2eSAlan Cox 	struct swdevt *sp;
855d027ed2eSAlan Cox 	const char *devname;
856d027ed2eSAlan Cox 	int error;
857d027ed2eSAlan Cox 
858d027ed2eSAlan Cox 	error = sysctl_wire_old_buffer(req, 0);
859d027ed2eSAlan Cox 	if (error != 0)
860d027ed2eSAlan Cox 		return (error);
861d027ed2eSAlan Cox 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
862d027ed2eSAlan Cox 	mtx_lock(&sw_dev_mtx);
863d027ed2eSAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
864d027ed2eSAlan Cox 		if (vn_isdisk(sp->sw_vp, NULL))
865d027ed2eSAlan Cox 			devname = devtoname(sp->sw_vp->v_rdev);
866d027ed2eSAlan Cox 		else
867d027ed2eSAlan Cox 			devname = "[file]";
868d027ed2eSAlan Cox 		sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname);
869d027ed2eSAlan Cox 		blist_stats(sp->sw_blist, &sbuf);
870d027ed2eSAlan Cox 	}
871d027ed2eSAlan Cox 	mtx_unlock(&sw_dev_mtx);
872d027ed2eSAlan Cox 	error = sbuf_finish(&sbuf);
873d027ed2eSAlan Cox 	sbuf_delete(&sbuf);
874d027ed2eSAlan Cox 	return (error);
875d027ed2eSAlan Cox }
876d027ed2eSAlan Cox 
877d027ed2eSAlan Cox /*
8781c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
8791c7c3c6aSMatthew Dillon  *				range within an object.
8801c7c3c6aSMatthew Dillon  *
8811c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
8821c7c3c6aSMatthew Dillon  *
8831c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
8841c7c3c6aSMatthew Dillon  *
8851c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
8861c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
8871c7c3c6aSMatthew Dillon  *	we should be ok.
888c25673ffSAttilio Rao  *
889c25673ffSAttilio Rao  *	The object must be locked.
89026f9a767SRodney W. Grimes  */
89126f9a767SRodney W. Grimes void
8922f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
89326f9a767SRodney W. Grimes {
89423955314SAlfred Perlstein 
8951c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
8964dcc5c2dSMatthew Dillon }
8974dcc5c2dSMatthew Dillon 
8984dcc5c2dSMatthew Dillon /*
8994dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
9004dcc5c2dSMatthew Dillon  *
9014dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
90256ce850bSKonstantin Belousov  *	swap blocks are not zeroed.  Any previous swap assignment is destroyed.
9034dcc5c2dSMatthew Dillon  *
9044dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
9054dcc5c2dSMatthew Dillon  */
9064dcc5c2dSMatthew Dillon int
9074dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
9084dcc5c2dSMatthew Dillon {
90948e98a2aSDoug Moore 	daddr_t addr, blk, n_free, s_free;
91048e98a2aSDoug Moore 	int i, j, n;
9114dcc5c2dSMatthew Dillon 
91278f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
91389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
91448e98a2aSDoug Moore 	for (i = 0; i < size; i += n) {
91531c82722SDoug Moore 		n = size - i;
91648e98a2aSDoug Moore 		blk = swp_pager_getswapspace(&n, 1);
91748e98a2aSDoug Moore 		if (blk == SWAPBLK_NONE) {
91848e98a2aSDoug Moore 			swp_pager_meta_free(object, start, i);
91989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
9204dcc5c2dSMatthew Dillon 			return (-1);
9214dcc5c2dSMatthew Dillon 		}
92248e98a2aSDoug Moore 		for (j = 0; j < n; ++j) {
92348e98a2aSDoug Moore 			addr = swp_pager_meta_build(object,
92448e98a2aSDoug Moore 			    start + i + j, blk + j);
92578f1deefSAlan Cox 			if (addr != SWAPBLK_NONE)
92648e98a2aSDoug Moore 				swp_pager_update_freerange(&s_free, &n_free,
92748e98a2aSDoug Moore 				    addr);
92848e98a2aSDoug Moore 		}
9294dcc5c2dSMatthew Dillon 	}
93078f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
93189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
9324dcc5c2dSMatthew Dillon 	return (0);
93326f9a767SRodney W. Grimes }
93426f9a767SRodney W. Grimes 
9350a47b48bSJohn Dyson /*
9361c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
9371c7c3c6aSMatthew Dillon  *			and destroy the source.
9381c7c3c6aSMatthew Dillon  *
9391c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
9401c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
9411c7c3c6aSMatthew Dillon  *	we keep the destination's.
9421c7c3c6aSMatthew Dillon  *
94315523cf7SKonstantin Belousov  *	This routine is allowed to sleep.  It may sleep allocating metadata
9441c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
9451c7c3c6aSMatthew Dillon  *	progress on the source.
9461c7c3c6aSMatthew Dillon  *
9471c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
9481c7c3c6aSMatthew Dillon  *
9491c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
9501c7c3c6aSMatthew Dillon  *
95115523cf7SKonstantin Belousov  *	The source and destination objects must be locked.
95215523cf7SKonstantin Belousov  *	Both object locks may temporarily be released.
95326f9a767SRodney W. Grimes  */
95426f9a767SRodney W. Grimes void
9552f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
9562f249180SPoul-Henning Kamp     vm_pindex_t offset, int destroysource)
95726f9a767SRodney W. Grimes {
958a316d390SJohn Dyson 	vm_pindex_t i;
95978f1deefSAlan Cox 	daddr_t dstaddr, n_free, s_free, srcaddr;
9604dcc5c2dSMatthew Dillon 
96189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(srcobject);
96289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(dstobject);
9630cddd8f0SMatthew Dillon 
96426f9a767SRodney W. Grimes 	/*
9651c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
9661c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
96726f9a767SRodney W. Grimes 	 */
968eb4d6a1bSKonstantin Belousov 	if (destroysource && srcobject->handle != NULL) {
969eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(srcobject, 1);
970eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(srcobject);
971eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(dstobject, 1);
972eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(dstobject);
973eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
974eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
975eb4d6a1bSKonstantin Belousov 		    pager_object_list);
976eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
977eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(dstobject);
978eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(dstobject);
979eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(srcobject);
980eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(srcobject);
981bd228075SAlan Cox 	}
98226f9a767SRodney W. Grimes 
9831c7c3c6aSMatthew Dillon 	/*
9844abca9bbSAlan Cox 	 * Transfer source to destination.
9851c7c3c6aSMatthew Dillon 	 */
98678f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
9871c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
9884abca9bbSAlan Cox 		srcaddr = swp_pager_meta_ctl(srcobject, i + offset, SWM_POP);
9894abca9bbSAlan Cox 		if (srcaddr == SWAPBLK_NONE)
9904abca9bbSAlan Cox 			continue;
9911c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
99278f1deefSAlan Cox 		if (dstaddr != SWAPBLK_NONE) {
99378f1deefSAlan Cox 			/*
99478f1deefSAlan Cox 			 * Destination has valid swapblk or it is represented
99578f1deefSAlan Cox 			 * by a resident page.  We destroy the source block.
99678f1deefSAlan Cox 			 */
99778f1deefSAlan Cox 			swp_pager_update_freerange(&s_free, &n_free, srcaddr);
99878f1deefSAlan Cox 			continue;
99978f1deefSAlan Cox 		}
100078f1deefSAlan Cox 
10011c7c3c6aSMatthew Dillon 		/*
10021c7c3c6aSMatthew Dillon 		 * Destination has no swapblk and is not resident,
10031c7c3c6aSMatthew Dillon 		 * copy source.
10044abca9bbSAlan Cox 		 *
1005c7c8dd7eSAlan Cox 		 * swp_pager_meta_build() can sleep.
1006c7c8dd7eSAlan Cox 		 */
1007c7c8dd7eSAlan Cox 		vm_object_pip_add(srcobject, 1);
100889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(srcobject);
1009c7c8dd7eSAlan Cox 		vm_object_pip_add(dstobject, 1);
101078f1deefSAlan Cox 		dstaddr = swp_pager_meta_build(dstobject, i, srcaddr);
101178f1deefSAlan Cox 		KASSERT(dstaddr == SWAPBLK_NONE,
101278f1deefSAlan Cox 		    ("Unexpected destination swapblk"));
1013c7c8dd7eSAlan Cox 		vm_object_pip_wakeup(dstobject);
101489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(srcobject);
1015c7c8dd7eSAlan Cox 		vm_object_pip_wakeup(srcobject);
10161c7c3c6aSMatthew Dillon 	}
101778f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
101826f9a767SRodney W. Grimes 
101926f9a767SRodney W. Grimes 	/*
10201c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
10211c7c3c6aSMatthew Dillon 	 *
1022763df3ecSPedro F. Giffuni 	 * We have to revert the type to OBJT_DEFAULT so we do not accidentally
10231c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
102426f9a767SRodney W. Grimes 	 */
1025c0877f10SJohn Dyson 	if (destroysource) {
10261c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
10271c7c3c6aSMatthew Dillon 		/*
10281c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
10291c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
1030956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
10311c7c3c6aSMatthew Dillon 		 * queues.
10321c7c3c6aSMatthew Dillon 		 */
10331c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
1034c0877f10SJohn Dyson 	}
103526f9a767SRodney W. Grimes }
103626f9a767SRodney W. Grimes 
1037df8bae1dSRodney W. Grimes /*
10381c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
10391c7c3c6aSMatthew Dillon  *				the requested page.
10401c7c3c6aSMatthew Dillon  *
10411c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
10421c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
10431c7c3c6aSMatthew Dillon  *
10441c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
1045915d1b71SMark Johnston  *	store exists before and after the requested page.
1046df8bae1dSRodney W. Grimes  */
10475ea4972cSAlan Cox static boolean_t
1048915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
1049915d1b71SMark Johnston     int *after)
105026f9a767SRodney W. Grimes {
1051915d1b71SMark Johnston 	daddr_t blk, blk0;
1052915d1b71SMark Johnston 	int i;
105326f9a767SRodney W. Grimes 
1054c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
1055915d1b71SMark Johnston 
10561c7c3c6aSMatthew Dillon 	/*
10571c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
10581c7c3c6aSMatthew Dillon 	 */
10591c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
10604dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
10611c7c3c6aSMatthew Dillon 		if (before)
106224a1cce3SDavid Greenman 			*before = 0;
10631c7c3c6aSMatthew Dillon 		if (after)
106424a1cce3SDavid Greenman 			*after = 0;
106526f9a767SRodney W. Grimes 		return (FALSE);
106626f9a767SRodney W. Grimes 	}
106726f9a767SRodney W. Grimes 
106826f9a767SRodney W. Grimes 	/*
10691c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
1070e47ed70bSJohn Dyson 	 */
10711c7c3c6aSMatthew Dillon 	if (before != NULL) {
1072915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10731c7c3c6aSMatthew Dillon 			if (i > pindex)
10741c7c3c6aSMatthew Dillon 				break;
10751c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
10761c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
10771c7c3c6aSMatthew Dillon 				break;
1078ffc82b0aSJohn Dyson 		}
1079915d1b71SMark Johnston 		*before = i - 1;
108026f9a767SRodney W. Grimes 	}
108126f9a767SRodney W. Grimes 
108226f9a767SRodney W. Grimes 	/*
10831c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
108426f9a767SRodney W. Grimes 	 */
10851c7c3c6aSMatthew Dillon 	if (after != NULL) {
1086915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10871c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
10881c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
10891c7c3c6aSMatthew Dillon 				break;
109026f9a767SRodney W. Grimes 		}
1091915d1b71SMark Johnston 		*after = i - 1;
10921c7c3c6aSMatthew Dillon 	}
10931c7c3c6aSMatthew Dillon 	return (TRUE);
10941c7c3c6aSMatthew Dillon }
10951c7c3c6aSMatthew Dillon 
10961c7c3c6aSMatthew Dillon /*
10971c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
10981c7c3c6aSMatthew Dillon  *
10991c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
11001c7c3c6aSMatthew Dillon  *	not, from the page.
11011c7c3c6aSMatthew Dillon  *
11021c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
11031c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
11041c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
11051c7c3c6aSMatthew Dillon  *
11061c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
11071c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
11081c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
11091c7c3c6aSMatthew Dillon  *	depends on it.
11101c7c3c6aSMatthew Dillon  *
111115523cf7SKonstantin Belousov  *	This routine may not sleep.
1112c25673ffSAttilio Rao  *
1113c25673ffSAttilio Rao  *	The object containing the page must be locked.
11141c7c3c6aSMatthew Dillon  */
11151c7c3c6aSMatthew Dillon static void
11162f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m)
11171c7c3c6aSMatthew Dillon {
11184abca9bbSAlan Cox 	daddr_t srcaddr;
11192f249180SPoul-Henning Kamp 
11204abca9bbSAlan Cox 	srcaddr = swp_pager_meta_ctl(m->object, m->pindex, SWM_POP);
11214abca9bbSAlan Cox 	if (srcaddr != SWAPBLK_NONE)
11224abca9bbSAlan Cox 		swp_pager_freeswapspace(srcaddr, 1);
11231c7c3c6aSMatthew Dillon }
11241c7c3c6aSMatthew Dillon 
11251c7c3c6aSMatthew Dillon /*
1126915d1b71SMark Johnston  * swap_pager_getpages() - bring pages in from swap
11271c7c3c6aSMatthew Dillon  *
11283f060b60SMark Johnston  *	Attempt to page in the pages in array "ma" of length "count".  The
11293f060b60SMark Johnston  *	caller may optionally specify that additional pages preceding and
11303f060b60SMark Johnston  *	succeeding the specified range be paged in.  The number of such pages
11313f060b60SMark Johnston  *	is returned in the "rbehind" and "rahead" parameters, and they will
11323f060b60SMark Johnston  *	be in the inactive queue upon return.
11331c7c3c6aSMatthew Dillon  *
11343f060b60SMark Johnston  *	The pages in "ma" must be busied and will remain busied upon return.
11351c7c3c6aSMatthew Dillon  */
1136f708ef1bSPoul-Henning Kamp static int
11373f060b60SMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
1138b0cd2017SGleb Smirnoff     int *rahead)
1139df8bae1dSRodney W. Grimes {
11401c7c3c6aSMatthew Dillon 	struct buf *bp;
1141b7b8a096SKonstantin Belousov 	vm_page_t bm, mpred, msucc, p;
1142915d1b71SMark Johnston 	vm_pindex_t pindex;
11431c7c3c6aSMatthew Dillon 	daddr_t blk;
1144b7b8a096SKonstantin Belousov 	int i, maxahead, maxbehind, reqcount;
11450d94caffSDavid Greenman 
1146915d1b71SMark Johnston 	reqcount = count;
11471c7c3c6aSMatthew Dillon 
1148b7b8a096SKonstantin Belousov 	/*
1149b7b8a096SKonstantin Belousov 	 * Determine the final number of read-behind pages and
1150b7b8a096SKonstantin Belousov 	 * allocate them BEFORE releasing the object lock.  Otherwise,
1151b7b8a096SKonstantin Belousov 	 * there can be a problematic race with vm_object_split().
1152b7b8a096SKonstantin Belousov 	 * Specifically, vm_object_split() might first transfer pages
1153b7b8a096SKonstantin Belousov 	 * that precede ma[0] in the current object to a new object,
1154b7b8a096SKonstantin Belousov 	 * and then this function incorrectly recreates those pages as
1155b7b8a096SKonstantin Belousov 	 * read-behind pages in the current object.
1156b7b8a096SKonstantin Belousov 	 */
1157b7b8a096SKonstantin Belousov 	if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead))
1158915d1b71SMark Johnston 		return (VM_PAGER_FAIL);
1159915d1b71SMark Johnston 
1160915d1b71SMark Johnston 	/*
1161915d1b71SMark Johnston 	 * Clip the readahead and readbehind ranges to exclude resident pages.
1162915d1b71SMark Johnston 	 */
1163915d1b71SMark Johnston 	if (rahead != NULL) {
1164dd9cb6daSMark Johnston 		KASSERT(reqcount - 1 <= maxahead,
1165915d1b71SMark Johnston 		    ("page count %d extends beyond swap block", reqcount));
1166dd9cb6daSMark Johnston 		*rahead = imin(*rahead, maxahead - (reqcount - 1));
11673f060b60SMark Johnston 		pindex = ma[reqcount - 1]->pindex;
11683f060b60SMark Johnston 		msucc = TAILQ_NEXT(ma[reqcount - 1], listq);
1169915d1b71SMark Johnston 		if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1170915d1b71SMark Johnston 			*rahead = msucc->pindex - pindex - 1;
1171915d1b71SMark Johnston 	}
1172915d1b71SMark Johnston 	if (rbehind != NULL) {
1173dd9cb6daSMark Johnston 		*rbehind = imin(*rbehind, maxbehind);
11743f060b60SMark Johnston 		pindex = ma[0]->pindex;
11753f060b60SMark Johnston 		mpred = TAILQ_PREV(ma[0], pglist, listq);
1176915d1b71SMark Johnston 		if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1177915d1b71SMark Johnston 			*rbehind = pindex - mpred->pindex - 1;
1178915d1b71SMark Johnston 	}
1179915d1b71SMark Johnston 
1180b7b8a096SKonstantin Belousov 	bm = ma[0];
1181b7b8a096SKonstantin Belousov 	for (i = 0; i < count; i++)
1182b7b8a096SKonstantin Belousov 		ma[i]->oflags |= VPO_SWAPINPROG;
1183b7b8a096SKonstantin Belousov 
1184915d1b71SMark Johnston 	/*
1185915d1b71SMark Johnston 	 * Allocate readahead and readbehind pages.
1186915d1b71SMark Johnston 	 */
1187b7b8a096SKonstantin Belousov 	if (rbehind != NULL) {
1188b7b8a096SKonstantin Belousov 		for (i = 1; i <= *rbehind; i++) {
11893f060b60SMark Johnston 			p = vm_page_alloc(object, ma[0]->pindex - i,
11907667839aSAlan Cox 			    VM_ALLOC_NORMAL);
1191b7b8a096SKonstantin Belousov 			if (p == NULL)
1192915d1b71SMark Johnston 				break;
1193b7b8a096SKonstantin Belousov 			p->oflags |= VPO_SWAPINPROG;
1194b7b8a096SKonstantin Belousov 			bm = p;
1195915d1b71SMark Johnston 		}
1196b7b8a096SKonstantin Belousov 		*rbehind = i - 1;
1197915d1b71SMark Johnston 	}
1198915d1b71SMark Johnston 	if (rahead != NULL) {
1199915d1b71SMark Johnston 		for (i = 0; i < *rahead; i++) {
1200915d1b71SMark Johnston 			p = vm_page_alloc(object,
12013f060b60SMark Johnston 			    ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1202915d1b71SMark Johnston 			if (p == NULL)
1203915d1b71SMark Johnston 				break;
1204b7b8a096SKonstantin Belousov 			p->oflags |= VPO_SWAPINPROG;
1205915d1b71SMark Johnston 		}
1206915d1b71SMark Johnston 		*rahead = i;
1207915d1b71SMark Johnston 	}
1208915d1b71SMark Johnston 	if (rbehind != NULL)
1209915d1b71SMark Johnston 		count += *rbehind;
1210915d1b71SMark Johnston 	if (rahead != NULL)
1211915d1b71SMark Johnston 		count += *rahead;
1212915d1b71SMark Johnston 
1213915d1b71SMark Johnston 	vm_object_pip_add(object, count);
1214915d1b71SMark Johnston 
1215b7b8a096SKonstantin Belousov 	pindex = bm->pindex;
1216915d1b71SMark Johnston 	blk = swp_pager_meta_ctl(object, pindex, 0);
1217915d1b71SMark Johnston 	KASSERT(blk != SWAPBLK_NONE,
1218915d1b71SMark Johnston 	    ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1219915d1b71SMark Johnston 
1220915d1b71SMark Johnston 	VM_OBJECT_WUNLOCK(object);
1221756a5412SGleb Smirnoff 	bp = uma_zalloc(swrbuf_zone, M_WAITOK);
1222b7b8a096SKonstantin Belousov 	/* Pages cannot leave the object while busy. */
1223b7b8a096SKonstantin Belousov 	for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) {
1224b7b8a096SKonstantin Belousov 		MPASS(p->pindex == bm->pindex + i);
1225b7b8a096SKonstantin Belousov 		bp->b_pages[i] = p;
1226b7b8a096SKonstantin Belousov 	}
1227915d1b71SMark Johnston 
1228915d1b71SMark Johnston 	bp->b_flags |= B_PAGING;
122921144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
12301c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1231fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1232fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
1233b0cd2017SGleb Smirnoff 	bp->b_blkno = blk;
1234b0cd2017SGleb Smirnoff 	bp->b_bcount = PAGE_SIZE * count;
1235b0cd2017SGleb Smirnoff 	bp->b_bufsize = PAGE_SIZE * count;
1236b0cd2017SGleb Smirnoff 	bp->b_npages = count;
1237915d1b71SMark Johnston 	bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1238915d1b71SMark Johnston 	bp->b_pgafter = rahead != NULL ? *rahead : 0;
123926f9a767SRodney W. Grimes 
124083c9dea1SGleb Smirnoff 	VM_CNT_INC(v_swapin);
124183c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_swappgsin, count);
12421c7c3c6aSMatthew Dillon 
12431c7c3c6aSMatthew Dillon 	/*
12441c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
12451c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
12461c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
12471c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
12481c7c3c6aSMatthew Dillon 	 *
12493f060b60SMark Johnston 	 * The other pages in our ma[] array are also released on completion,
12501c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
12511c7c3c6aSMatthew Dillon 	 *
1252c37a77eeSPoul-Henning Kamp 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
12531c7c3c6aSMatthew Dillon 	 */
1254b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
12554b03903aSPoul-Henning Kamp 	swp_pager_strategy(bp);
125626f9a767SRodney W. Grimes 
125726f9a767SRodney W. Grimes 	/*
1258915d1b71SMark Johnston 	 * Wait for the pages we want to complete.  VPO_SWAPINPROG is always
12591c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1260915d1b71SMark Johnston 	 * is set in the metadata for each page in the request.
126126f9a767SRodney W. Grimes 	 */
126289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
12633f060b60SMark Johnston 	while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) {
12643f060b60SMark Johnston 		ma[0]->oflags |= VPO_SWAPSLEEP;
126583c9dea1SGleb Smirnoff 		VM_CNT_INC(v_intrans);
1266cf27e0d1SJeff Roberson 		if (VM_OBJECT_SLEEP(object, &object->handle, PSWP,
1267c7aebda8SAttilio Rao 		    "swread", hz * 20)) {
12689bd86a98SBruce M Simpson 			printf(
1269c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1270c5690651SPoul-Henning Kamp 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
12711c7c3c6aSMatthew Dillon 		}
12721b119d9dSDavid Greenman 	}
127326f9a767SRodney W. Grimes 
127426f9a767SRodney W. Grimes 	/*
1275b0cd2017SGleb Smirnoff 	 * If we had an unrecoverable read error pages will not be valid.
127626f9a767SRodney W. Grimes 	 */
1277915d1b71SMark Johnston 	for (i = 0; i < reqcount; i++)
12783f060b60SMark Johnston 		if (ma[i]->valid != VM_PAGE_BITS_ALL)
12791c7c3c6aSMatthew Dillon 			return (VM_PAGER_ERROR);
1280b0cd2017SGleb Smirnoff 
12811c7c3c6aSMatthew Dillon 	return (VM_PAGER_OK);
12821c7c3c6aSMatthew Dillon 
12831c7c3c6aSMatthew Dillon 	/*
12841c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
12851c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
12861c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
12871c7c3c6aSMatthew Dillon 	 * to all-zero's later.
12881c7c3c6aSMatthew Dillon 	 */
1289df8bae1dSRodney W. Grimes }
1290df8bae1dSRodney W. Grimes 
12911c7c3c6aSMatthew Dillon /*
129290effb23SGleb Smirnoff  * 	swap_pager_getpages_async():
129390effb23SGleb Smirnoff  *
129490effb23SGleb Smirnoff  *	Right now this is emulation of asynchronous operation on top of
129590effb23SGleb Smirnoff  *	swap_pager_getpages().
129690effb23SGleb Smirnoff  */
129790effb23SGleb Smirnoff static int
12983f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count,
1299b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
130090effb23SGleb Smirnoff {
130190effb23SGleb Smirnoff 	int r, error;
130290effb23SGleb Smirnoff 
13033f060b60SMark Johnston 	r = swap_pager_getpages(object, ma, count, rbehind, rahead);
130490effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
130590effb23SGleb Smirnoff 	switch (r) {
130690effb23SGleb Smirnoff 	case VM_PAGER_OK:
130790effb23SGleb Smirnoff 		error = 0;
130890effb23SGleb Smirnoff 		break;
130990effb23SGleb Smirnoff 	case VM_PAGER_ERROR:
131090effb23SGleb Smirnoff 		error = EIO;
131190effb23SGleb Smirnoff 		break;
131290effb23SGleb Smirnoff 	case VM_PAGER_FAIL:
131390effb23SGleb Smirnoff 		error = EINVAL;
131490effb23SGleb Smirnoff 		break;
131590effb23SGleb Smirnoff 	default:
1316d9328101SGleb Smirnoff 		panic("unhandled swap_pager_getpages() error %d", r);
131790effb23SGleb Smirnoff 	}
13183f060b60SMark Johnston 	(iodone)(arg, ma, count, error);
131990effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
132090effb23SGleb Smirnoff 
132190effb23SGleb Smirnoff 	return (r);
132290effb23SGleb Smirnoff }
132390effb23SGleb Smirnoff 
132490effb23SGleb Smirnoff /*
13251c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
13261c7c3c6aSMatthew Dillon  *
13271c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
13281c7c3c6aSMatthew Dillon  *
13291c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
13301c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
13311c7c3c6aSMatthew Dillon  *
1332ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
13331c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
13341c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
13351c7c3c6aSMatthew Dillon  *	which needs work.
13361c7c3c6aSMatthew Dillon  *
13371c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
13381c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
13391c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
13401c7c3c6aSMatthew Dillon  *	completion.
13411c7c3c6aSMatthew Dillon  *
13421c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
134323612f0dSDoug Moore  *	those whose rtvals[] entry is not set to VM_PAGER_PEND on return.
13441c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
13451c7c3c6aSMatthew Dillon  */
1346d635a37fSWarner Losh static void
13473f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count,
1348e065e87cSKonstantin Belousov     int flags, int *rtvals)
1349df8bae1dSRodney W. Grimes {
135023612f0dSDoug Moore 	struct buf *bp;
135123612f0dSDoug Moore 	daddr_t addr, blk, n_free, s_free;
135223612f0dSDoug Moore 	vm_page_t mreq;
135323612f0dSDoug Moore 	int i, j, n;
135423612f0dSDoug Moore 	bool async;
1355df8bae1dSRodney W. Grimes 
135623612f0dSDoug Moore 	KASSERT(count == 0 || ma[0]->object == object,
135723612f0dSDoug Moore 	    ("%s: object mismatch %p/%p",
135823612f0dSDoug Moore 	    __func__, object, ma[0]->object));
1359ee3dc7d7SAlan Cox 
13601c7c3c6aSMatthew Dillon 	/*
13611c7c3c6aSMatthew Dillon 	 * Step 1
13621c7c3c6aSMatthew Dillon 	 *
136323612f0dSDoug Moore 	 * Turn object into OBJT_SWAP.  Force sync if not a pageout process.
13641c7c3c6aSMatthew Dillon 	 */
136578f1deefSAlan Cox 	if (object->type != OBJT_SWAP) {
136678f1deefSAlan Cox 		addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE);
136778f1deefSAlan Cox 		KASSERT(addr == SWAPBLK_NONE,
136878f1deefSAlan Cox 		    ("unexpected object swap block"));
136978f1deefSAlan Cox 	}
137089f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
137123612f0dSDoug Moore 	async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0;
137223612f0dSDoug Moore 	swp_pager_init_freerange(&s_free, &n_free);
137326f9a767SRodney W. Grimes 
13741c7c3c6aSMatthew Dillon 	/*
13751c7c3c6aSMatthew Dillon 	 * Step 2
13761c7c3c6aSMatthew Dillon 	 *
13771c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
13781c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
13791c7c3c6aSMatthew Dillon 	 * successfully.
13801c7c3c6aSMatthew Dillon 	 */
13811c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
138231c82722SDoug Moore 		/* Maximum I/O size is limited by maximum swap block size. */
138331c82722SDoug Moore 		n = min(count - i, nsw_cluster_max);
13841c7c3c6aSMatthew Dillon 
138548e98a2aSDoug Moore 		/* Get a block of swap of size up to size n. */
138648e98a2aSDoug Moore 		blk = swp_pager_getswapspace(&n, 4);
13871c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
13884dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
13891c7c3c6aSMatthew Dillon 				rtvals[i + j] = VM_PAGER_FAIL;
13901c7c3c6aSMatthew Dillon 			continue;
139126f9a767SRodney W. Grimes 		}
139226f9a767SRodney W. Grimes 
139326f9a767SRodney W. Grimes 		/*
139423612f0dSDoug Moore 		 * All I/O parameters have been satisfied.  Build the I/O
13951c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
139626f9a767SRodney W. Grimes 		 */
139723612f0dSDoug Moore 		if (async) {
1398756a5412SGleb Smirnoff 			mtx_lock(&swbuf_mtx);
1399756a5412SGleb Smirnoff 			while (nsw_wcount_async == 0)
1400756a5412SGleb Smirnoff 				msleep(&nsw_wcount_async, &swbuf_mtx, PVM,
1401756a5412SGleb Smirnoff 				    "swbufa", 0);
1402756a5412SGleb Smirnoff 			nsw_wcount_async--;
1403756a5412SGleb Smirnoff 			mtx_unlock(&swbuf_mtx);
1404327f4e83SMatthew Dillon 		}
1405756a5412SGleb Smirnoff 		bp = uma_zalloc(swwbuf_zone, M_WAITOK);
140623612f0dSDoug Moore 		if (async)
1407756a5412SGleb Smirnoff 			bp->b_flags = B_ASYNC;
14085e04322aSPoul-Henning Kamp 		bp->b_flags |= B_PAGING;
1409912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
141026f9a767SRodney W. Grimes 
1411fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1412fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
14131c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
14141c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
14151c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1416e47ed70bSJohn Dyson 
141789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
14181c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
141923612f0dSDoug Moore 			mreq = ma[i + j];
142078f1deefSAlan Cox 			addr = swp_pager_meta_build(mreq->object, mreq->pindex,
142178f1deefSAlan Cox 			    blk + j);
142278f1deefSAlan Cox 			if (addr != SWAPBLK_NONE)
142378f1deefSAlan Cox 				swp_pager_update_freerange(&s_free, &n_free,
142478f1deefSAlan Cox 				    addr);
142587b0ab69SAlan Cox 			MPASS(mreq->dirty == VM_PAGE_BITS_ALL);
14265786be7cSAlan Cox 			mreq->oflags |= VPO_SWAPINPROG;
14271c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
14281c7c3c6aSMatthew Dillon 		}
142989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
14301c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1431a5296b05SJulian Elischer 		/*
1432a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1433a5296b05SJulian Elischer 		 */
1434a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1435a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
14361c7c3c6aSMatthew Dillon 
143783c9dea1SGleb Smirnoff 		VM_CNT_INC(v_swapout);
143883c9dea1SGleb Smirnoff 		VM_CNT_ADD(v_swappgsout, bp->b_npages);
143926f9a767SRodney W. Grimes 
144026f9a767SRodney W. Grimes 		/*
144177923df2SAlan Cox 		 * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
144277923df2SAlan Cox 		 * can call the async completion routine at the end of a
144377923df2SAlan Cox 		 * synchronous I/O operation.  Otherwise, our caller would
144477923df2SAlan Cox 		 * perform duplicate unbusy and wakeup operations on the page
144577923df2SAlan Cox 		 * and object, respectively.
144677923df2SAlan Cox 		 */
144777923df2SAlan Cox 		for (j = 0; j < n; j++)
144877923df2SAlan Cox 			rtvals[i + j] = VM_PAGER_PEND;
144977923df2SAlan Cox 
145077923df2SAlan Cox 		/*
14511c7c3c6aSMatthew Dillon 		 * asynchronous
14521c7c3c6aSMatthew Dillon 		 *
145323612f0dSDoug Moore 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy.
145426f9a767SRodney W. Grimes 		 */
145523612f0dSDoug Moore 		if (async) {
14561c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
145767812eacSKirk McKusick 			BUF_KERNPROC(bp);
14584b03903aSPoul-Henning Kamp 			swp_pager_strategy(bp);
14591c7c3c6aSMatthew Dillon 			continue;
146026f9a767SRodney W. Grimes 		}
1461e47ed70bSJohn Dyson 
146226f9a767SRodney W. Grimes 		/*
14631c7c3c6aSMatthew Dillon 		 * synchronous
14641c7c3c6aSMatthew Dillon 		 *
146523612f0dSDoug Moore 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy.
14661c7c3c6aSMatthew Dillon 		 */
14672c840b1fSAlan Cox 		bp->b_iodone = bdone;
14684b03903aSPoul-Henning Kamp 		swp_pager_strategy(bp);
14691c7c3c6aSMatthew Dillon 
14701c7c3c6aSMatthew Dillon 		/*
147177923df2SAlan Cox 		 * Wait for the sync I/O to complete.
147226f9a767SRodney W. Grimes 		 */
14732c840b1fSAlan Cox 		bwait(bp, PVM, "swwrt");
147477923df2SAlan Cox 
14751c7c3c6aSMatthew Dillon 		/*
14761c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
14771c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
14781c7c3c6aSMatthew Dillon 		 */
14791c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
14801c7c3c6aSMatthew Dillon 	}
148178f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
148223612f0dSDoug Moore 	VM_OBJECT_WLOCK(object);
14831c7c3c6aSMatthew Dillon }
14841c7c3c6aSMatthew Dillon 
14851c7c3c6aSMatthew Dillon /*
14861c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
14871c7c3c6aSMatthew Dillon  *
14881c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
14891c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
14901c7c3c6aSMatthew Dillon  *
149115523cf7SKonstantin Belousov  *	This routine may not sleep.
14921c7c3c6aSMatthew Dillon  */
14931c7c3c6aSMatthew Dillon static void
14942f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp)
14951c7c3c6aSMatthew Dillon {
14961c7c3c6aSMatthew Dillon 	int i;
14971c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
14981c7c3c6aSMatthew Dillon 
14991c7c3c6aSMatthew Dillon 	/*
15000b208315SEdward Tomasz Napierala 	 * Report error - unless we ran out of memory, in which case
15010b208315SEdward Tomasz Napierala 	 * we've already logged it in swapgeom_strategy().
15021c7c3c6aSMatthew Dillon 	 */
15030b208315SEdward Tomasz Napierala 	if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) {
15041c7c3c6aSMatthew Dillon 		printf(
15051c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
15061c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
150721144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
15081c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
15091c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
15101c7c3c6aSMatthew Dillon 		    bp->b_error
15111c7c3c6aSMatthew Dillon 		);
15121c7c3c6aSMatthew Dillon 	}
15131c7c3c6aSMatthew Dillon 
15141c7c3c6aSMatthew Dillon 	/*
151526f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
151626f9a767SRodney W. Grimes 	 */
1517fade8dd7SJeff Roberson 	if (buf_mapped(bp))
15181c7c3c6aSMatthew Dillon 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1519fade8dd7SJeff Roberson 	else
1520fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
152126f9a767SRodney W. Grimes 
152233a609ecSAlan Cox 	if (bp->b_npages) {
152333a609ecSAlan Cox 		object = bp->b_pages[0]->object;
152489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
152533a609ecSAlan Cox 	}
15262965a453SKip Macy 
152726f9a767SRodney W. Grimes 	/*
15281c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
15291c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
15301c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
15311c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
15321c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
15331c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
153426f9a767SRodney W. Grimes 	 */
15351c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
15361c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1537e47ed70bSJohn Dyson 
15385786be7cSAlan Cox 		m->oflags &= ~VPO_SWAPINPROG;
1539c7aebda8SAttilio Rao 		if (m->oflags & VPO_SWAPSLEEP) {
1540c7aebda8SAttilio Rao 			m->oflags &= ~VPO_SWAPSLEEP;
1541cf27e0d1SJeff Roberson 			wakeup(&object->handle);
1542c7aebda8SAttilio Rao 		}
1543e47ed70bSJohn Dyson 
1544c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1545ffc82b0aSJohn Dyson 			/*
15461c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
15471c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
15481c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
15491c7c3c6aSMatthew Dillon 			 * interrupt.
1550ffc82b0aSJohn Dyson 			 */
155121144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
15521c7c3c6aSMatthew Dillon 				/*
15531c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1554956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15551c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15561c7c3c6aSMatthew Dillon 				 */
15571c7c3c6aSMatthew Dillon 				m->valid = 0;
15581c7c3c6aSMatthew Dillon 			} else {
15591c7c3c6aSMatthew Dillon 				/*
15601c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
15611c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
15621c7c3c6aSMatthew Dillon 				 * then finish the I/O.
15631c7c3c6aSMatthew Dillon 				 */
156441e5a226SAlan Cox 				MPASS(m->dirty == VM_PAGE_BITS_ALL);
15653c4a2440SAlan Cox 				vm_page_lock(m);
15661c7c3c6aSMatthew Dillon 				vm_page_activate(m);
15673c4a2440SAlan Cox 				vm_page_unlock(m);
1568c7aebda8SAttilio Rao 				vm_page_sunbusy(m);
15691c7c3c6aSMatthew Dillon 			}
157021144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
15711c7c3c6aSMatthew Dillon 			/*
15721c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1573956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
15741c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
15751c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
15761c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
15771c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
15781c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
15791c7c3c6aSMatthew Dillon 			 */
1580016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1581016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is mapped", m));
1582016a3c93SAlan Cox 			KASSERT(m->dirty == 0,
1583016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is dirty", m));
1584915d1b71SMark Johnston 
1585b0cd2017SGleb Smirnoff 			m->valid = VM_PAGE_BITS_ALL;
1586915d1b71SMark Johnston 			if (i < bp->b_pgbefore ||
1587915d1b71SMark Johnston 			    i >= bp->b_npages - bp->b_pgafter)
1588915d1b71SMark Johnston 				vm_page_readahead_finish(m);
15891c7c3c6aSMatthew Dillon 		} else {
15901c7c3c6aSMatthew Dillon 			/*
1591016a3c93SAlan Cox 			 * For write success, clear the dirty
15921c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
15931c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
1594ebcddc72SAlan Cox 			 * A page is only written to swap after a period of
1595ebcddc72SAlan Cox 			 * inactivity.  Therefore, we do not expect it to be
1596ebcddc72SAlan Cox 			 * reused.
15971c7c3c6aSMatthew Dillon 			 */
15986031c68dSAlan Cox 			KASSERT(!pmap_page_is_write_mapped(m),
1599016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is not write"
1600016a3c93SAlan Cox 			    " protected", m));
1601c52e7044SAlan Cox 			vm_page_undirty(m);
16023c4a2440SAlan Cox 			vm_page_lock(m);
1603ebcddc72SAlan Cox 			vm_page_deactivate_noreuse(m);
16042965a453SKip Macy 			vm_page_unlock(m);
1605ebcddc72SAlan Cox 			vm_page_sunbusy(m);
16063c4a2440SAlan Cox 		}
16073c4a2440SAlan Cox 	}
160826f9a767SRodney W. Grimes 
16091c7c3c6aSMatthew Dillon 	/*
16101c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
16111c7c3c6aSMatthew Dillon 	 * pip refs on the object.
16121c7c3c6aSMatthew Dillon 	 */
16130d420ad3SAlan Cox 	if (object != NULL) {
16141c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
161589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
16160d420ad3SAlan Cox 	}
161726f9a767SRodney W. Grimes 
16181c7c3c6aSMatthew Dillon 	/*
1619100650deSOlivier Houchard 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1620100650deSOlivier Houchard 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1621100650deSOlivier Houchard 	 * trigger a KASSERT in relpbuf().
1622100650deSOlivier Houchard 	 */
1623100650deSOlivier Houchard 	if (bp->b_vp) {
1624100650deSOlivier Houchard 		    bp->b_vp = NULL;
1625100650deSOlivier Houchard 		    bp->b_bufobj = NULL;
1626100650deSOlivier Houchard 	}
1627100650deSOlivier Houchard 	/*
16281c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
16291c7c3c6aSMatthew Dillon 	 */
1630756a5412SGleb Smirnoff 	if (bp->b_flags & B_ASYNC) {
1631756a5412SGleb Smirnoff 		mtx_lock(&swbuf_mtx);
1632756a5412SGleb Smirnoff 		if (++nsw_wcount_async == 1)
1633756a5412SGleb Smirnoff 			wakeup(&nsw_wcount_async);
1634756a5412SGleb Smirnoff 		mtx_unlock(&swbuf_mtx);
1635756a5412SGleb Smirnoff 	}
1636756a5412SGleb Smirnoff 	uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp);
163726f9a767SRodney W. Grimes }
16381c7c3c6aSMatthew Dillon 
1639b1fd102eSMark Johnston int
1640b1fd102eSMark Johnston swap_pager_nswapdev(void)
1641b1fd102eSMark Johnston {
1642b1fd102eSMark Johnston 
1643b1fd102eSMark Johnston 	return (nswapdev);
1644b1fd102eSMark Johnston }
1645b1fd102eSMark Johnston 
16467c022327SDoug Moore static void
16477c022327SDoug Moore swp_pager_force_dirty(vm_page_t m)
16487c022327SDoug Moore {
16497c022327SDoug Moore 
16507c022327SDoug Moore 	vm_page_dirty(m);
1651e8bcf696SMark Johnston #ifdef INVARIANTS
1652e8bcf696SMark Johnston 	vm_page_lock(m);
1653e8bcf696SMark Johnston 	if (!vm_page_wired(m) && m->queue == PQ_NONE)
1654e8bcf696SMark Johnston 		panic("page %p is neither wired nor queued", m);
1655e8bcf696SMark Johnston 	vm_page_unlock(m);
1656e8bcf696SMark Johnston #endif
16577c022327SDoug Moore 	vm_page_xunbusy(m);
165856948d17SDoug Moore 	swap_pager_unswapped(m);
16597c022327SDoug Moore }
16607c022327SDoug Moore 
16617c022327SDoug Moore static void
16627c022327SDoug Moore swp_pager_force_launder(vm_page_t m)
16637c022327SDoug Moore {
16647c022327SDoug Moore 
16657c022327SDoug Moore 	vm_page_dirty(m);
16667c022327SDoug Moore 	vm_page_lock(m);
16677c022327SDoug Moore 	vm_page_launder(m);
16687c022327SDoug Moore 	vm_page_unlock(m);
16697c022327SDoug Moore 	vm_page_xunbusy(m);
167056948d17SDoug Moore 	swap_pager_unswapped(m);
16717c022327SDoug Moore }
16727c022327SDoug Moore 
167392da00bbSMatthew Dillon /*
167456948d17SDoug Moore  * SWP_PAGER_FORCE_PAGEIN() - force swap blocks to be paged in
167592da00bbSMatthew Dillon  *
167656948d17SDoug Moore  *	This routine dissociates pages starting at the given index within an
167756948d17SDoug Moore  *	object from their backing store, paging them in if they do not reside
167856948d17SDoug Moore  *	in memory.  Pages that are paged in are marked dirty and placed in the
167956948d17SDoug Moore  *	laundry queue.  Pages are marked dirty because they no longer have
168056948d17SDoug Moore  *	backing store.  They are placed in the laundry queue because they have
168156948d17SDoug Moore  *	not been accessed recently.  Otherwise, they would already reside in
168256948d17SDoug Moore  *	memory.
168392da00bbSMatthew Dillon  */
16847c022327SDoug Moore static void
168556948d17SDoug Moore swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex, int npages)
168692da00bbSMatthew Dillon {
168756948d17SDoug Moore 	vm_page_t ma[npages];
168856948d17SDoug Moore 	int i, j;
168992da00bbSMatthew Dillon 
169056948d17SDoug Moore 	KASSERT(npages > 0, ("%s: No pages", __func__));
169156948d17SDoug Moore 	KASSERT(npages <= MAXPHYS / PAGE_SIZE,
169256948d17SDoug Moore 	    ("%s: Too many pages: %d", __func__, npages));
169356948d17SDoug Moore 	vm_object_pip_add(object, npages);
169456948d17SDoug Moore 	vm_page_grab_pages(object, pindex, VM_ALLOC_NORMAL, ma, npages);
169556948d17SDoug Moore 	for (i = j = 0;; i++) {
169656948d17SDoug Moore 		/* Count nonresident pages, to page-in all at once. */
169756948d17SDoug Moore 		if (i < npages && ma[i]->valid != VM_PAGE_BITS_ALL)
169856948d17SDoug Moore 			continue;
169956948d17SDoug Moore 		if (j < i) {
170056948d17SDoug Moore 			/* Page-in nonresident pages. Mark for laundering. */
170156948d17SDoug Moore 			if (swap_pager_getpages(object, &ma[j], i - j, NULL,
170256948d17SDoug Moore 			    NULL) != VM_PAGER_OK)
170356948d17SDoug Moore 				panic("%s: read from swap failed", __func__);
170456948d17SDoug Moore 			do {
170556948d17SDoug Moore 				swp_pager_force_launder(ma[j]);
170656948d17SDoug Moore 			} while (++j < i);
170792da00bbSMatthew Dillon 		}
170856948d17SDoug Moore 		if (i == npages)
170956948d17SDoug Moore 			break;
171056948d17SDoug Moore 		/* Mark dirty a resident page. */
171156948d17SDoug Moore 		swp_pager_force_dirty(ma[j++]);
171256948d17SDoug Moore 	}
171356948d17SDoug Moore 	vm_object_pip_wakeupn(object, npages);
171492da00bbSMatthew Dillon }
171592da00bbSMatthew Dillon 
171692da00bbSMatthew Dillon /*
17177c022327SDoug Moore  *	swap_pager_swapoff_object:
17187c022327SDoug Moore  *
17197c022327SDoug Moore  *	Page in all of the pages that have been paged out for an object
172056948d17SDoug Moore  *	to a swap device.
17217c022327SDoug Moore  */
17227c022327SDoug Moore static void
17237c022327SDoug Moore swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object)
17247c022327SDoug Moore {
17257c022327SDoug Moore 	struct swblk *sb;
172656948d17SDoug Moore 	vm_pindex_t pi, s_pindex;
172756948d17SDoug Moore 	daddr_t blk, n_blks, s_blk;
17287c022327SDoug Moore 	int i;
17297c022327SDoug Moore 
173056948d17SDoug Moore 	n_blks = 0;
17317c022327SDoug Moore 	for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
17327c022327SDoug Moore 	    &object->un_pager.swp.swp_blks, pi)) != NULL; ) {
17337c022327SDoug Moore 		for (i = 0; i < SWAP_META_PAGES; i++) {
173456948d17SDoug Moore 			blk = sb->d[i];
173556948d17SDoug Moore 			if (!swp_pager_isondev(blk, sp))
173656948d17SDoug Moore 				blk = SWAPBLK_NONE;
173756948d17SDoug Moore 
173856948d17SDoug Moore 			/*
173956948d17SDoug Moore 			 * If there are no blocks/pages accumulated, start a new
174056948d17SDoug Moore 			 * accumulation here.
174156948d17SDoug Moore 			 */
174256948d17SDoug Moore 			if (n_blks == 0) {
174356948d17SDoug Moore 				if (blk != SWAPBLK_NONE) {
174456948d17SDoug Moore 					s_blk = blk;
174556948d17SDoug Moore 					s_pindex = sb->p + i;
174656948d17SDoug Moore 					n_blks = 1;
174756948d17SDoug Moore 				}
17487c022327SDoug Moore 				continue;
17497c022327SDoug Moore 			}
175056948d17SDoug Moore 
175156948d17SDoug Moore 			/*
175256948d17SDoug Moore 			 * If the accumulation can be extended without breaking
175356948d17SDoug Moore 			 * the sequence of consecutive blocks and pages that
175456948d17SDoug Moore 			 * swp_pager_force_pagein() depends on, do so.
175556948d17SDoug Moore 			 */
175656948d17SDoug Moore 			if (n_blks < MAXPHYS / PAGE_SIZE &&
175756948d17SDoug Moore 			    s_blk + n_blks == blk &&
175856948d17SDoug Moore 			    s_pindex + n_blks == sb->p + i) {
175956948d17SDoug Moore 				++n_blks;
176056948d17SDoug Moore 				continue;
17617c022327SDoug Moore 			}
176256948d17SDoug Moore 
176356948d17SDoug Moore 			/*
176456948d17SDoug Moore 			 * The sequence of consecutive blocks and pages cannot
176556948d17SDoug Moore 			 * be extended, so page them all in here.  Then,
176656948d17SDoug Moore 			 * because doing so involves releasing and reacquiring
176756948d17SDoug Moore 			 * a lock that protects the swap block pctrie, do not
176856948d17SDoug Moore 			 * rely on the current swap block.  Break this loop and
176956948d17SDoug Moore 			 * re-fetch the same pindex from the pctrie again.
177056948d17SDoug Moore 			 */
177156948d17SDoug Moore 			swp_pager_force_pagein(object, s_pindex, n_blks);
177256948d17SDoug Moore 			n_blks = 0;
177356948d17SDoug Moore 			break;
177456948d17SDoug Moore 		}
177556948d17SDoug Moore 		if (i == SWAP_META_PAGES)
177656948d17SDoug Moore 			pi = sb->p + SWAP_META_PAGES;
177756948d17SDoug Moore 	}
177856948d17SDoug Moore 	if (n_blks > 0)
177956948d17SDoug Moore 		swp_pager_force_pagein(object, s_pindex, n_blks);
17807c022327SDoug Moore }
17817c022327SDoug Moore 
17827c022327SDoug Moore /*
178392da00bbSMatthew Dillon  *	swap_pager_swapoff:
178492da00bbSMatthew Dillon  *
178592da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
178692da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
178792da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
178892da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
178992da00bbSMatthew Dillon  *
179092da00bbSMatthew Dillon  *	This routine may block.
179192da00bbSMatthew Dillon  */
1792e9c0cc15SPoul-Henning Kamp static void
1793b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp)
179492da00bbSMatthew Dillon {
1795f425ab8eSKonstantin Belousov 	vm_object_t object;
17967c022327SDoug Moore 	int retries;
179792da00bbSMatthew Dillon 
179804533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
179992da00bbSMatthew Dillon 
18008bc61209SDavid Schultz 	retries = 0;
180192da00bbSMatthew Dillon full_rescan:
1802f425ab8eSKonstantin Belousov 	mtx_lock(&vm_object_list_mtx);
1803f425ab8eSKonstantin Belousov 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
1804f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
180598150664SKonstantin Belousov 			continue;
1806f425ab8eSKonstantin Belousov 		mtx_unlock(&vm_object_list_mtx);
180798150664SKonstantin Belousov 		/* Depends on type-stability. */
180898150664SKonstantin Belousov 		VM_OBJECT_WLOCK(object);
1809f425ab8eSKonstantin Belousov 
1810f425ab8eSKonstantin Belousov 		/*
1811f425ab8eSKonstantin Belousov 		 * Dead objects are eventually terminated on their own.
1812f425ab8eSKonstantin Belousov 		 */
1813f425ab8eSKonstantin Belousov 		if ((object->flags & OBJ_DEAD) != 0)
1814f425ab8eSKonstantin Belousov 			goto next_obj;
1815f425ab8eSKonstantin Belousov 
1816f425ab8eSKonstantin Belousov 		/*
1817f425ab8eSKonstantin Belousov 		 * Sync with fences placed after pctrie
1818f425ab8eSKonstantin Belousov 		 * initialization.  We must not access pctrie below
1819f425ab8eSKonstantin Belousov 		 * unless we checked that our object is swap and not
1820f425ab8eSKonstantin Belousov 		 * dead.
1821f425ab8eSKonstantin Belousov 		 */
1822f425ab8eSKonstantin Belousov 		atomic_thread_fence_acq();
1823f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
1824f425ab8eSKonstantin Belousov 			goto next_obj;
1825f425ab8eSKonstantin Belousov 
18267c022327SDoug Moore 		swap_pager_swapoff_object(sp, object);
1827f425ab8eSKonstantin Belousov next_obj:
1828f425ab8eSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
1829f425ab8eSKonstantin Belousov 		mtx_lock(&vm_object_list_mtx);
183092da00bbSMatthew Dillon 	}
1831f425ab8eSKonstantin Belousov 	mtx_unlock(&vm_object_list_mtx);
1832f425ab8eSKonstantin Belousov 
18338bc61209SDavid Schultz 	if (sp->sw_used) {
183492da00bbSMatthew Dillon 		/*
18358bc61209SDavid Schultz 		 * Objects may be locked or paging to the device being
18368bc61209SDavid Schultz 		 * removed, so we will miss their pages and need to
18378bc61209SDavid Schultz 		 * make another pass.  We have marked this device as
18388bc61209SDavid Schultz 		 * SW_CLOSING, so the activity should finish soon.
183992da00bbSMatthew Dillon 		 */
18408bc61209SDavid Schultz 		retries++;
18418bc61209SDavid Schultz 		if (retries > 100) {
18428bc61209SDavid Schultz 			panic("swapoff: failed to locate %d swap blocks",
18438bc61209SDavid Schultz 			    sp->sw_used);
18448bc61209SDavid Schultz 		}
18454d70511aSJohn Baldwin 		pause("swpoff", hz / 20);
184692da00bbSMatthew Dillon 		goto full_rescan;
184792da00bbSMatthew Dillon 	}
1848b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapoff, sp);
184992da00bbSMatthew Dillon }
185092da00bbSMatthew Dillon 
18511c7c3c6aSMatthew Dillon /************************************************************************
18521c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
18531c7c3c6aSMatthew Dillon  ************************************************************************
18541c7c3c6aSMatthew Dillon  *
18551c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
1856cec9f109SDavid E. O'Brien  *	OBJT_SWAP object.
18571c7c3c6aSMatthew Dillon  *
18584dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
18594dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
18604dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
18611c7c3c6aSMatthew Dillon  */
18621c7c3c6aSMatthew Dillon 
18631c7c3c6aSMatthew Dillon /*
1864230869e0SAlan Cox  * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free?
1865230869e0SAlan Cox  */
1866230869e0SAlan Cox static bool
1867230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit)
1868230869e0SAlan Cox {
1869230869e0SAlan Cox 	int i;
1870230869e0SAlan Cox 
1871230869e0SAlan Cox 	MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES);
1872230869e0SAlan Cox 	for (i = start; i < limit; i++) {
1873230869e0SAlan Cox 		if (sb->d[i] != SWAPBLK_NONE)
1874230869e0SAlan Cox 			return (false);
1875230869e0SAlan Cox 	}
1876230869e0SAlan Cox 	return (true);
1877230869e0SAlan Cox }
1878230869e0SAlan Cox 
1879230869e0SAlan Cox /*
18801c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
18811c7c3c6aSMatthew Dillon  *
18821c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
18831c7c3c6aSMatthew Dillon  *	object.
18841c7c3c6aSMatthew Dillon  *
18851c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
18861c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
188778f1deefSAlan Cox  *	assigned swapblk is returned.
18881c7c3c6aSMatthew Dillon  */
188978f1deefSAlan Cox static daddr_t
18902f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
18912f249180SPoul-Henning Kamp {
1892f425ab8eSKonstantin Belousov 	static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted;
1893eed99cb8SKonstantin Belousov 	struct swblk *sb, *sb1;
1894f425ab8eSKonstantin Belousov 	vm_pindex_t modpi, rdpi;
189578f1deefSAlan Cox 	daddr_t prev_swapblk;
1896f425ab8eSKonstantin Belousov 	int error, i;
18971c7c3c6aSMatthew Dillon 
189889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
1899f425ab8eSKonstantin Belousov 
19001c7c3c6aSMatthew Dillon 	/*
19011c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
19021c7c3c6aSMatthew Dillon 	 */
19031c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
1904f425ab8eSKonstantin Belousov 		pctrie_init(&object->un_pager.swp.swp_blks);
1905f425ab8eSKonstantin Belousov 
1906f425ab8eSKonstantin Belousov 		/*
1907f425ab8eSKonstantin Belousov 		 * Ensure that swap_pager_swapoff()'s iteration over
1908f425ab8eSKonstantin Belousov 		 * object_list does not see a garbage pctrie.
1909f425ab8eSKonstantin Belousov 		 */
1910f425ab8eSKonstantin Belousov 		atomic_thread_fence_rel();
1911f425ab8eSKonstantin Belousov 
19121c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
1913fe7bcbafSKyle Evans 		object->un_pager.swp.writemappings = 0;
1914eb4d6a1bSKonstantin Belousov 		KASSERT(object->handle == NULL, ("default pager with handle"));
1915bd228075SAlan Cox 	}
19161c7c3c6aSMatthew Dillon 
1917f425ab8eSKonstantin Belousov 	rdpi = rounddown(pindex, SWAP_META_PAGES);
1918f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi);
1919f425ab8eSKonstantin Belousov 	if (sb == NULL) {
19201c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
192178f1deefSAlan Cox 			return (SWAPBLK_NONE);
1922f425ab8eSKonstantin Belousov 		for (;;) {
1923f425ab8eSKonstantin Belousov 			sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc ==
1924f425ab8eSKonstantin Belousov 			    pageproc ? M_USE_RESERVE : 0));
1925f425ab8eSKonstantin Belousov 			if (sb != NULL) {
1926f425ab8eSKonstantin Belousov 				sb->p = rdpi;
1927f425ab8eSKonstantin Belousov 				for (i = 0; i < SWAP_META_PAGES; i++)
1928f425ab8eSKonstantin Belousov 					sb->d[i] = SWAPBLK_NONE;
1929f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swblk_zone_exhausted,
1930f425ab8eSKonstantin Belousov 				    1, 0))
1931f425ab8eSKonstantin Belousov 					printf("swblk zone ok\n");
1932f425ab8eSKonstantin Belousov 				break;
1933f425ab8eSKonstantin Belousov 			}
193489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1935f425ab8eSKonstantin Belousov 			if (uma_zone_exhausted(swblk_zone)) {
1936f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swblk_zone_exhausted,
1937f425ab8eSKonstantin Belousov 				    0, 1))
1938f425ab8eSKonstantin Belousov 					printf("swap blk zone exhausted, "
19393ff863f1SDag-Erling Smørgrav 					    "increase kern.maxswzone\n");
19402025d69bSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
1941f425ab8eSKonstantin Belousov 				pause("swzonxb", 10);
19422025d69bSKonstantin Belousov 			} else
19438d6fbbb8SJeff Roberson 				uma_zwait(swblk_zone);
194489f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
1945eed99cb8SKonstantin Belousov 			sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1946eed99cb8SKonstantin Belousov 			    rdpi);
1947eed99cb8SKonstantin Belousov 			if (sb != NULL)
1948eed99cb8SKonstantin Belousov 				/*
1949eed99cb8SKonstantin Belousov 				 * Somebody swapped out a nearby page,
1950eed99cb8SKonstantin Belousov 				 * allocating swblk at the rdpi index,
1951eed99cb8SKonstantin Belousov 				 * while we dropped the object lock.
1952eed99cb8SKonstantin Belousov 				 */
1953eed99cb8SKonstantin Belousov 				goto allocated;
19544dcc5c2dSMatthew Dillon 		}
1955f425ab8eSKonstantin Belousov 		for (;;) {
1956f425ab8eSKonstantin Belousov 			error = SWAP_PCTRIE_INSERT(
1957f425ab8eSKonstantin Belousov 			    &object->un_pager.swp.swp_blks, sb);
1958f425ab8eSKonstantin Belousov 			if (error == 0) {
1959f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1960f425ab8eSKonstantin Belousov 				    1, 0))
1961f425ab8eSKonstantin Belousov 					printf("swpctrie zone ok\n");
1962f425ab8eSKonstantin Belousov 				break;
19631c7c3c6aSMatthew Dillon 			}
1964f425ab8eSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
1965f425ab8eSKonstantin Belousov 			if (uma_zone_exhausted(swpctrie_zone)) {
1966f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1967f425ab8eSKonstantin Belousov 				    0, 1))
1968f425ab8eSKonstantin Belousov 					printf("swap pctrie zone exhausted, "
1969f425ab8eSKonstantin Belousov 					    "increase kern.maxswzone\n");
1970f425ab8eSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
1971f425ab8eSKonstantin Belousov 				pause("swzonxp", 10);
1972f425ab8eSKonstantin Belousov 			} else
19738d6fbbb8SJeff Roberson 				uma_zwait(swpctrie_zone);
1974f425ab8eSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
1975eed99cb8SKonstantin Belousov 			sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1976eed99cb8SKonstantin Belousov 			    rdpi);
1977eed99cb8SKonstantin Belousov 			if (sb1 != NULL) {
1978eed99cb8SKonstantin Belousov 				uma_zfree(swblk_zone, sb);
1979eed99cb8SKonstantin Belousov 				sb = sb1;
1980eed99cb8SKonstantin Belousov 				goto allocated;
19811c7c3c6aSMatthew Dillon 			}
1982f425ab8eSKonstantin Belousov 		}
1983eed99cb8SKonstantin Belousov 	}
1984eed99cb8SKonstantin Belousov allocated:
1985f425ab8eSKonstantin Belousov 	MPASS(sb->p == rdpi);
19861c7c3c6aSMatthew Dillon 
1987f425ab8eSKonstantin Belousov 	modpi = pindex % SWAP_META_PAGES;
198878f1deefSAlan Cox 	/* Return prior contents of metadata. */
198978f1deefSAlan Cox 	prev_swapblk = sb->d[modpi];
1990f425ab8eSKonstantin Belousov 	/* Enter block into metadata. */
1991f425ab8eSKonstantin Belousov 	sb->d[modpi] = swapblk;
199285d88d87SKonstantin Belousov 
199385d88d87SKonstantin Belousov 	/*
199485d88d87SKonstantin Belousov 	 * Free the swblk if we end up with the empty page run.
199585d88d87SKonstantin Belousov 	 */
1996230869e0SAlan Cox 	if (swapblk == SWAPBLK_NONE &&
1997230869e0SAlan Cox 	    swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
1998230869e0SAlan Cox 		SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, rdpi);
199985d88d87SKonstantin Belousov 		uma_zfree(swblk_zone, sb);
200085d88d87SKonstantin Belousov 	}
200178f1deefSAlan Cox 	return (prev_swapblk);
200285d88d87SKonstantin Belousov }
20031c7c3c6aSMatthew Dillon 
20041c7c3c6aSMatthew Dillon /*
20051c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
20061c7c3c6aSMatthew Dillon  *
20071c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
20081c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
20091c7c3c6aSMatthew Dillon  *
20101c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
20111c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
20121c7c3c6aSMatthew Dillon  *	with resident pages.
20131c7c3c6aSMatthew Dillon  */
20141c7c3c6aSMatthew Dillon static void
2015f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count)
20161c7c3c6aSMatthew Dillon {
2017f425ab8eSKonstantin Belousov 	struct swblk *sb;
201878f1deefSAlan Cox 	daddr_t n_free, s_free;
2019f425ab8eSKonstantin Belousov 	vm_pindex_t last;
2020230869e0SAlan Cox 	int i, limit, start;
20212928cef7SAlan Cox 
2022ee620ea4SAlan Cox 	VM_OBJECT_ASSERT_WLOCKED(object);
20232e56b64fSKonstantin Belousov 	if (object->type != OBJT_SWAP || count == 0)
20241c7c3c6aSMatthew Dillon 		return;
20251c7c3c6aSMatthew Dillon 
202678f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
2027230869e0SAlan Cox 	last = pindex + count;
2028f425ab8eSKonstantin Belousov 	for (;;) {
2029f425ab8eSKonstantin Belousov 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2030f425ab8eSKonstantin Belousov 		    rounddown(pindex, SWAP_META_PAGES));
2031230869e0SAlan Cox 		if (sb == NULL || sb->p >= last)
20322e56b64fSKonstantin Belousov 			break;
2033230869e0SAlan Cox 		start = pindex > sb->p ? pindex - sb->p : 0;
2034230869e0SAlan Cox 		limit = last - sb->p < SWAP_META_PAGES ? last - sb->p :
2035230869e0SAlan Cox 		    SWAP_META_PAGES;
2036230869e0SAlan Cox 		for (i = start; i < limit; i++) {
2037f425ab8eSKonstantin Belousov 			if (sb->d[i] == SWAPBLK_NONE)
2038f425ab8eSKonstantin Belousov 				continue;
203978f1deefSAlan Cox 			swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
2040230869e0SAlan Cox 			sb->d[i] = SWAPBLK_NONE;
2041230869e0SAlan Cox 		}
2042150d384eSMark Johnston 		pindex = sb->p + SWAP_META_PAGES;
2043230869e0SAlan Cox 		if (swp_pager_swblk_empty(sb, 0, start) &&
2044230869e0SAlan Cox 		    swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) {
2045f425ab8eSKonstantin Belousov 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
2046f425ab8eSKonstantin Belousov 			    sb->p);
2047f425ab8eSKonstantin Belousov 			uma_zfree(swblk_zone, sb);
20481c7c3c6aSMatthew Dillon 		}
20491c7c3c6aSMatthew Dillon 	}
205078f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
20511c7c3c6aSMatthew Dillon }
20521c7c3c6aSMatthew Dillon 
20531c7c3c6aSMatthew Dillon /*
20541c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
20551c7c3c6aSMatthew Dillon  *
20561c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
20571c7c3c6aSMatthew Dillon  *	an object.
20581c7c3c6aSMatthew Dillon  */
20591c7c3c6aSMatthew Dillon static void
20601c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
20611c7c3c6aSMatthew Dillon {
2062f425ab8eSKonstantin Belousov 	struct swblk *sb;
206378f1deefSAlan Cox 	daddr_t n_free, s_free;
2064f425ab8eSKonstantin Belousov 	vm_pindex_t pindex;
206571057cd2SKonstantin Belousov 	int i;
20661c7c3c6aSMatthew Dillon 
206789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
20681c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
20691c7c3c6aSMatthew Dillon 		return;
20701c7c3c6aSMatthew Dillon 
207178f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
2072f425ab8eSKonstantin Belousov 	for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
2073f425ab8eSKonstantin Belousov 	    &object->un_pager.swp.swp_blks, pindex)) != NULL;) {
2074f425ab8eSKonstantin Belousov 		pindex = sb->p + SWAP_META_PAGES;
2075f425ab8eSKonstantin Belousov 		for (i = 0; i < SWAP_META_PAGES; i++) {
2076230869e0SAlan Cox 			if (sb->d[i] == SWAPBLK_NONE)
2077230869e0SAlan Cox 				continue;
207878f1deefSAlan Cox 			swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
20791c7c3c6aSMatthew Dillon 		}
2080f425ab8eSKonstantin Belousov 		SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
2081f425ab8eSKonstantin Belousov 		uma_zfree(swblk_zone, sb);
20821c7c3c6aSMatthew Dillon 	}
208378f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
20841c7c3c6aSMatthew Dillon }
20851c7c3c6aSMatthew Dillon 
20861c7c3c6aSMatthew Dillon /*
20874abca9bbSAlan Cox  * SWP_PAGER_METACTL() -  misc control of swap meta data.
20881c7c3c6aSMatthew Dillon  *
20894abca9bbSAlan Cox  *	This routine is capable of looking up, or removing swapblk
20904abca9bbSAlan Cox  *	assignments in the swap meta data.  It returns the swapblk being
20914abca9bbSAlan Cox  *	looked-up, popped, or SWAPBLK_NONE if the block was invalid.
20921c7c3c6aSMatthew Dillon  *
20931c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
20941c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
20951c7c3c6aSMatthew Dillon  *	busy page.
20961c7c3c6aSMatthew Dillon  *
20974abca9bbSAlan Cox  *	SWM_POP		remove from meta data but do not free it
20981c7c3c6aSMatthew Dillon  */
20991c7c3c6aSMatthew Dillon static daddr_t
21002f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags)
21012f249180SPoul-Henning Kamp {
2102f425ab8eSKonstantin Belousov 	struct swblk *sb;
21034dcc5c2dSMatthew Dillon 	daddr_t r1;
21044dcc5c2dSMatthew Dillon 
21054abca9bbSAlan Cox 	if ((flags & SWM_POP) != 0)
2106ee620ea4SAlan Cox 		VM_OBJECT_ASSERT_WLOCKED(object);
2107ee620ea4SAlan Cox 	else
2108c25673ffSAttilio Rao 		VM_OBJECT_ASSERT_LOCKED(object);
2109ee620ea4SAlan Cox 
21101c7c3c6aSMatthew Dillon 	/*
2111ee620ea4SAlan Cox 	 * The meta data only exists if the object is OBJT_SWAP
21121c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
21131c7c3c6aSMatthew Dillon 	 */
21144dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
21151c7c3c6aSMatthew Dillon 		return (SWAPBLK_NONE);
21161c7c3c6aSMatthew Dillon 
2117f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2118f425ab8eSKonstantin Belousov 	    rounddown(pindex, SWAP_META_PAGES));
2119f425ab8eSKonstantin Belousov 	if (sb == NULL)
2120f425ab8eSKonstantin Belousov 		return (SWAPBLK_NONE);
2121f425ab8eSKonstantin Belousov 	r1 = sb->d[pindex % SWAP_META_PAGES];
2122f425ab8eSKonstantin Belousov 	if (r1 == SWAPBLK_NONE)
2123f425ab8eSKonstantin Belousov 		return (SWAPBLK_NONE);
21244abca9bbSAlan Cox 	if ((flags & SWM_POP) != 0) {
2125f425ab8eSKonstantin Belousov 		sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE;
2126230869e0SAlan Cox 		if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
2127f425ab8eSKonstantin Belousov 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
2128f425ab8eSKonstantin Belousov 			    rounddown(pindex, SWAP_META_PAGES));
2129f425ab8eSKonstantin Belousov 			uma_zfree(swblk_zone, sb);
2130f425ab8eSKonstantin Belousov 		}
2131f425ab8eSKonstantin Belousov 	}
21321c7c3c6aSMatthew Dillon 	return (r1);
21331c7c3c6aSMatthew Dillon }
21341c7c3c6aSMatthew Dillon 
2135e9c0cc15SPoul-Henning Kamp /*
213677d6fd97SKonstantin Belousov  * Returns the least page index which is greater than or equal to the
213777d6fd97SKonstantin Belousov  * parameter pindex and for which there is a swap block allocated.
213877d6fd97SKonstantin Belousov  * Returns object's size if the object's type is not swap or if there
213977d6fd97SKonstantin Belousov  * are no allocated swap blocks for the object after the requested
214077d6fd97SKonstantin Belousov  * pindex.
214177d6fd97SKonstantin Belousov  */
214277d6fd97SKonstantin Belousov vm_pindex_t
214377d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
214477d6fd97SKonstantin Belousov {
2145f425ab8eSKonstantin Belousov 	struct swblk *sb;
2146f425ab8eSKonstantin Belousov 	int i;
214777d6fd97SKonstantin Belousov 
214877d6fd97SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(object);
2149f425ab8eSKonstantin Belousov 	if (object->type != OBJT_SWAP)
215077d6fd97SKonstantin Belousov 		return (object->size);
215177d6fd97SKonstantin Belousov 
2152f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2153f425ab8eSKonstantin Belousov 	    rounddown(pindex, SWAP_META_PAGES));
2154f425ab8eSKonstantin Belousov 	if (sb == NULL)
2155f425ab8eSKonstantin Belousov 		return (object->size);
2156f425ab8eSKonstantin Belousov 	if (sb->p < pindex) {
2157f425ab8eSKonstantin Belousov 		for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) {
2158f425ab8eSKonstantin Belousov 			if (sb->d[i] != SWAPBLK_NONE)
2159f425ab8eSKonstantin Belousov 				return (sb->p + i);
216077d6fd97SKonstantin Belousov 		}
2161f425ab8eSKonstantin Belousov 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2162f425ab8eSKonstantin Belousov 		    roundup(pindex, SWAP_META_PAGES));
2163f425ab8eSKonstantin Belousov 		if (sb == NULL)
2164f425ab8eSKonstantin Belousov 			return (object->size);
216577d6fd97SKonstantin Belousov 	}
2166f425ab8eSKonstantin Belousov 	for (i = 0; i < SWAP_META_PAGES; i++) {
2167f425ab8eSKonstantin Belousov 		if (sb->d[i] != SWAPBLK_NONE)
2168f425ab8eSKonstantin Belousov 			return (sb->p + i);
216977d6fd97SKonstantin Belousov 	}
2170f425ab8eSKonstantin Belousov 
2171f425ab8eSKonstantin Belousov 	/*
2172f425ab8eSKonstantin Belousov 	 * We get here if a swblk is present in the trie but it
2173f425ab8eSKonstantin Belousov 	 * doesn't map any blocks.
2174f425ab8eSKonstantin Belousov 	 */
2175f425ab8eSKonstantin Belousov 	MPASS(0);
2176f425ab8eSKonstantin Belousov 	return (object->size);
217777d6fd97SKonstantin Belousov }
217877d6fd97SKonstantin Belousov 
217977d6fd97SKonstantin Belousov /*
2180e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2181e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2182e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2183e9c0cc15SPoul-Henning Kamp  */
2184e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2185e9c0cc15SPoul-Henning Kamp struct swapon_args {
2186e9c0cc15SPoul-Henning Kamp 	char *name;
2187e9c0cc15SPoul-Henning Kamp };
2188e9c0cc15SPoul-Henning Kamp #endif
2189e9c0cc15SPoul-Henning Kamp 
2190e9c0cc15SPoul-Henning Kamp /*
2191e9c0cc15SPoul-Henning Kamp  * MPSAFE
2192e9c0cc15SPoul-Henning Kamp  */
2193e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2194e9c0cc15SPoul-Henning Kamp int
21958451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap)
2196e9c0cc15SPoul-Henning Kamp {
2197e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2198e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2199e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2200e9c0cc15SPoul-Henning Kamp 	int error;
2201e9c0cc15SPoul-Henning Kamp 
2202acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPON);
2203e9c0cc15SPoul-Henning Kamp 	if (error)
2204acd3428bSRobert Watson 		return (error);
2205e9c0cc15SPoul-Henning Kamp 
220604533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2207e9c0cc15SPoul-Henning Kamp 
2208e9c0cc15SPoul-Henning Kamp 	/*
2209e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2210e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2211e9c0cc15SPoul-Henning Kamp 	 */
2212f425ab8eSKonstantin Belousov 	if (swblk_zone == NULL) {
2213e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2214e9c0cc15SPoul-Henning Kamp 		goto done;
2215e9c0cc15SPoul-Henning Kamp 	}
2216e9c0cc15SPoul-Henning Kamp 
2217d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2218d9135e72SRobert Watson 	    uap->name, td);
2219e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2220e9c0cc15SPoul-Henning Kamp 	if (error)
2221e9c0cc15SPoul-Henning Kamp 		goto done;
2222e9c0cc15SPoul-Henning Kamp 
2223e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2224e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2225e9c0cc15SPoul-Henning Kamp 
222620da9c2eSPoul-Henning Kamp 	if (vn_isdisk(vp, &error)) {
222788ad2d7bSKonstantin Belousov 		error = swapongeom(vp);
222820da9c2eSPoul-Henning Kamp 	} else if (vp->v_type == VREG &&
2229e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
22300359a12eSAttilio Rao 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2231e9c0cc15SPoul-Henning Kamp 		/*
2232e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2233e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2234e9c0cc15SPoul-Henning Kamp 		 */
223559efee01SPoul-Henning Kamp 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2236e9c0cc15SPoul-Henning Kamp 	}
2237e9c0cc15SPoul-Henning Kamp 
2238e9c0cc15SPoul-Henning Kamp 	if (error)
2239e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2240e9c0cc15SPoul-Henning Kamp done:
224104533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2242e9c0cc15SPoul-Henning Kamp 	return (error);
2243e9c0cc15SPoul-Henning Kamp }
2244e9c0cc15SPoul-Henning Kamp 
22453ff863f1SDag-Erling Smørgrav /*
22463ff863f1SDag-Erling Smørgrav  * Check that the total amount of swap currently configured does not
22473ff863f1SDag-Erling Smørgrav  * exceed half the theoretical maximum.  If it does, print a warning
224835872e79SKonstantin Belousov  * message.
22493ff863f1SDag-Erling Smørgrav  */
225035872e79SKonstantin Belousov static void
225135872e79SKonstantin Belousov swapon_check_swzone(void)
22523ff863f1SDag-Erling Smørgrav {
225335872e79SKonstantin Belousov 	unsigned long maxpages, npages;
22543ff863f1SDag-Erling Smørgrav 
2255e8bb589dSMatt Macy 	npages = swap_total;
22563ff863f1SDag-Erling Smørgrav 	/* absolute maximum we can handle assuming 100% efficiency */
2257f425ab8eSKonstantin Belousov 	maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES;
22583ff863f1SDag-Erling Smørgrav 
22593ff863f1SDag-Erling Smørgrav 	/* recommend using no more than half that amount */
22603ff863f1SDag-Erling Smørgrav 	if (npages > maxpages / 2) {
22613ff863f1SDag-Erling Smørgrav 		printf("warning: total configured swap (%lu pages) "
22623ff863f1SDag-Erling Smørgrav 		    "exceeds maximum recommended amount (%lu pages).\n",
22639462305cSSergey Kandaurov 		    npages, maxpages / 2);
22643ff863f1SDag-Erling Smørgrav 		printf("warning: increase kern.maxswzone "
22653ff863f1SDag-Erling Smørgrav 		    "or reduce amount of swap.\n");
22663ff863f1SDag-Erling Smørgrav 	}
22673ff863f1SDag-Erling Smørgrav }
22683ff863f1SDag-Erling Smørgrav 
226959efee01SPoul-Henning Kamp static void
22702cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks,
22712cc718a1SKonstantin Belousov     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2272e9c0cc15SPoul-Henning Kamp {
22732d9974c1SAlan Cox 	struct swdevt *sp, *tsp;
2274e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
22758f60c087SPoul-Henning Kamp 	u_long mblocks;
2276e9c0cc15SPoul-Henning Kamp 
2277e9c0cc15SPoul-Henning Kamp 	/*
2278e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2279e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2280e9c0cc15SPoul-Henning Kamp 	 *
2281e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2282e9c0cc15SPoul-Henning Kamp 	 */
2283e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2284e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2285e9c0cc15SPoul-Henning Kamp 
22866e903bd0SKonstantin Belousov 	/*
22876e903bd0SKonstantin Belousov 	 * If we go beyond this, we get overflows in the radix
22886e903bd0SKonstantin Belousov 	 * tree bitmap code.
22896e903bd0SKonstantin Belousov 	 */
22906e903bd0SKonstantin Belousov 	mblocks = 0x40000000 / BLIST_META_RADIX;
22916e903bd0SKonstantin Belousov 	if (nblks > mblocks) {
22926e903bd0SKonstantin Belousov 		printf(
22936e903bd0SKonstantin Belousov     "WARNING: reducing swap size to maximum of %luMB per unit\n",
22946e903bd0SKonstantin Belousov 		    mblocks / 1024 / 1024 * PAGE_SIZE);
22956e903bd0SKonstantin Belousov 		nblks = mblocks;
22966e903bd0SKonstantin Belousov 	}
22976e903bd0SKonstantin Belousov 
22988f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2299dee34ca4SPoul-Henning Kamp 	sp->sw_vp = vp;
2300dee34ca4SPoul-Henning Kamp 	sp->sw_id = id;
2301f3732fd1SPoul-Henning Kamp 	sp->sw_dev = dev;
2302e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2303e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
230459efee01SPoul-Henning Kamp 	sp->sw_strategy = strategy;
2305dee34ca4SPoul-Henning Kamp 	sp->sw_close = close;
23062cc718a1SKonstantin Belousov 	sp->sw_flags = flags;
2307e9c0cc15SPoul-Henning Kamp 
2308c8c7ad92SKip Macy 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2309e9c0cc15SPoul-Henning Kamp 	/*
2310504f5e29SDoug Moore 	 * Do not free the first blocks in order to avoid overwriting
23118f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2312e9c0cc15SPoul-Henning Kamp 	 */
2313504f5e29SDoug Moore 	blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE),
2314504f5e29SDoug Moore 	    nblks - howmany(BBSIZE, PAGE_SIZE));
2315e9c0cc15SPoul-Henning Kamp 
23162d9974c1SAlan Cox 	dvbase = 0;
231720da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23182d9974c1SAlan Cox 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
23192d9974c1SAlan Cox 		if (tsp->sw_end >= dvbase) {
23202d9974c1SAlan Cox 			/*
23212d9974c1SAlan Cox 			 * We put one uncovered page between the devices
23222d9974c1SAlan Cox 			 * in order to definitively prevent any cross-device
23232d9974c1SAlan Cox 			 * I/O requests
23242d9974c1SAlan Cox 			 */
23252d9974c1SAlan Cox 			dvbase = tsp->sw_end + 1;
23262d9974c1SAlan Cox 		}
23272d9974c1SAlan Cox 	}
23282d9974c1SAlan Cox 	sp->sw_first = dvbase;
23292d9974c1SAlan Cox 	sp->sw_end = dvbase + nblks;
23308f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
23318f60c087SPoul-Henning Kamp 	nswapdev++;
2332504f5e29SDoug Moore 	swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE);
2333e8bb589dSMatt Macy 	swap_total += nblks;
233435872e79SKonstantin Belousov 	swapon_check_swzone();
2335d05bc129SAlan Cox 	swp_sizecheck();
2336d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2337b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapon, sp);
233859efee01SPoul-Henning Kamp }
2339e9c0cc15SPoul-Henning Kamp 
2340e9c0cc15SPoul-Henning Kamp /*
2341e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2342e9c0cc15SPoul-Henning Kamp  *
2343e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2344dee34ca4SPoul-Henning Kamp  *
2345dee34ca4SPoul-Henning Kamp  * XXX: Badly designed system call: it should use a device index
2346dee34ca4SPoul-Henning Kamp  * rather than filename as specification.  We keep sw_vp around
2347dee34ca4SPoul-Henning Kamp  * only to make this work.
2348e9c0cc15SPoul-Henning Kamp  */
2349e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2350e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2351e9c0cc15SPoul-Henning Kamp 	char *name;
2352e9c0cc15SPoul-Henning Kamp };
2353e9c0cc15SPoul-Henning Kamp #endif
2354e9c0cc15SPoul-Henning Kamp 
2355e9c0cc15SPoul-Henning Kamp /*
2356e9c0cc15SPoul-Henning Kamp  * MPSAFE
2357e9c0cc15SPoul-Henning Kamp  */
2358e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2359e9c0cc15SPoul-Henning Kamp int
23608451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap)
2361e9c0cc15SPoul-Henning Kamp {
2362e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2363e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2364e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
23658f60c087SPoul-Henning Kamp 	int error;
2366e9c0cc15SPoul-Henning Kamp 
2367acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPOFF);
2368e9c0cc15SPoul-Henning Kamp 	if (error)
23690909f38aSPawel Jakub Dawidek 		return (error);
2370e9c0cc15SPoul-Henning Kamp 
237104533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2372e9c0cc15SPoul-Henning Kamp 
2373d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2374d9135e72SRobert Watson 	    td);
2375e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2376e9c0cc15SPoul-Henning Kamp 	if (error)
2377e9c0cc15SPoul-Henning Kamp 		goto done;
2378e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2379e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2380e9c0cc15SPoul-Henning Kamp 
238120da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23828f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2383dee34ca4SPoul-Henning Kamp 		if (sp->sw_vp == vp)
23840909f38aSPawel Jakub Dawidek 			break;
2385e9c0cc15SPoul-Henning Kamp 	}
238620da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
23870909f38aSPawel Jakub Dawidek 	if (sp == NULL) {
2388e9c0cc15SPoul-Henning Kamp 		error = EINVAL;
2389e9c0cc15SPoul-Henning Kamp 		goto done;
23900909f38aSPawel Jakub Dawidek 	}
239135918c55SChristian S.J. Peron 	error = swapoff_one(sp, td->td_ucred);
23920909f38aSPawel Jakub Dawidek done:
239304533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
23940909f38aSPawel Jakub Dawidek 	return (error);
23950909f38aSPawel Jakub Dawidek }
23960909f38aSPawel Jakub Dawidek 
23970909f38aSPawel Jakub Dawidek static int
239835918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred)
23990909f38aSPawel Jakub Dawidek {
240003bdd65fSAlan Cox 	u_long nblks;
2401e9c0cc15SPoul-Henning Kamp #ifdef MAC
24020909f38aSPawel Jakub Dawidek 	int error;
2403e9c0cc15SPoul-Henning Kamp #endif
2404e9c0cc15SPoul-Henning Kamp 
240504533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
24060909f38aSPawel Jakub Dawidek #ifdef MAC
2407cb05b60aSAttilio Rao 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
240835918c55SChristian S.J. Peron 	error = mac_system_check_swapoff(cred, sp->sw_vp);
240922db15c0SAttilio Rao 	(void) VOP_UNLOCK(sp->sw_vp, 0);
24100909f38aSPawel Jakub Dawidek 	if (error != 0)
24110909f38aSPawel Jakub Dawidek 		return (error);
24120909f38aSPawel Jakub Dawidek #endif
2413e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2414e9c0cc15SPoul-Henning Kamp 
2415e9c0cc15SPoul-Henning Kamp 	/*
2416e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2417e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2418e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2419e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2420e9c0cc15SPoul-Henning Kamp 	 */
2421e2068d0bSJeff Roberson 	if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat)
24220909f38aSPawel Jakub Dawidek 		return (ENOMEM);
2423e9c0cc15SPoul-Henning Kamp 
2424e9c0cc15SPoul-Henning Kamp 	/*
2425e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2426e9c0cc15SPoul-Henning Kamp 	 */
24272928cef7SAlan Cox 	mtx_lock(&sw_dev_mtx);
2428e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
242903bdd65fSAlan Cox 	swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks);
2430e8bb589dSMatt Macy 	swap_total -= nblks;
24312928cef7SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2432e9c0cc15SPoul-Henning Kamp 
2433e9c0cc15SPoul-Henning Kamp 	/*
2434e9c0cc15SPoul-Henning Kamp 	 * Page in the contents of the device and close it.
2435e9c0cc15SPoul-Henning Kamp 	 */
2436b3fed13eSDavid Schultz 	swap_pager_swapoff(sp);
2437e9c0cc15SPoul-Henning Kamp 
243835918c55SChristian S.J. Peron 	sp->sw_close(curthread, sp);
243920da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
24409e3e3fe5SWarner Losh 	sp->sw_id = NULL;
24418f60c087SPoul-Henning Kamp 	TAILQ_REMOVE(&swtailq, sp, sw_list);
24420676a140SAlan Cox 	nswapdev--;
24437dea2c2eSAlan Cox 	if (nswapdev == 0) {
24447dea2c2eSAlan Cox 		swap_pager_full = 2;
24457dea2c2eSAlan Cox 		swap_pager_almost_full = 1;
24467dea2c2eSAlan Cox 	}
24478f60c087SPoul-Henning Kamp 	if (swdevhd == sp)
24488f60c087SPoul-Henning Kamp 		swdevhd = NULL;
2449d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
24508f60c087SPoul-Henning Kamp 	blist_destroy(sp->sw_blist);
24518f60c087SPoul-Henning Kamp 	free(sp, M_VMPGDATA);
24520909f38aSPawel Jakub Dawidek 	return (0);
24530909f38aSPawel Jakub Dawidek }
2454e9c0cc15SPoul-Henning Kamp 
24550909f38aSPawel Jakub Dawidek void
24560909f38aSPawel Jakub Dawidek swapoff_all(void)
24570909f38aSPawel Jakub Dawidek {
24580909f38aSPawel Jakub Dawidek 	struct swdevt *sp, *spt;
24590909f38aSPawel Jakub Dawidek 	const char *devname;
24600909f38aSPawel Jakub Dawidek 	int error;
24610909f38aSPawel Jakub Dawidek 
246204533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
24630909f38aSPawel Jakub Dawidek 
24640909f38aSPawel Jakub Dawidek 	mtx_lock(&sw_dev_mtx);
24650909f38aSPawel Jakub Dawidek 	TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
24660909f38aSPawel Jakub Dawidek 		mtx_unlock(&sw_dev_mtx);
24670909f38aSPawel Jakub Dawidek 		if (vn_isdisk(sp->sw_vp, NULL))
24687870adb6SEd Schouten 			devname = devtoname(sp->sw_vp->v_rdev);
24690909f38aSPawel Jakub Dawidek 		else
24700909f38aSPawel Jakub Dawidek 			devname = "[file]";
247135918c55SChristian S.J. Peron 		error = swapoff_one(sp, thread0.td_ucred);
24720909f38aSPawel Jakub Dawidek 		if (error != 0) {
24730909f38aSPawel Jakub Dawidek 			printf("Cannot remove swap device %s (error=%d), "
24740909f38aSPawel Jakub Dawidek 			    "skipping.\n", devname, error);
24750909f38aSPawel Jakub Dawidek 		} else if (bootverbose) {
24760909f38aSPawel Jakub Dawidek 			printf("Swap device %s removed.\n", devname);
24770909f38aSPawel Jakub Dawidek 		}
24780909f38aSPawel Jakub Dawidek 		mtx_lock(&sw_dev_mtx);
24790909f38aSPawel Jakub Dawidek 	}
24800909f38aSPawel Jakub Dawidek 	mtx_unlock(&sw_dev_mtx);
24810909f38aSPawel Jakub Dawidek 
248204533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2483e9c0cc15SPoul-Henning Kamp }
2484e9c0cc15SPoul-Henning Kamp 
2485567104a1SPoul-Henning Kamp void
2486567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used)
2487567104a1SPoul-Henning Kamp {
2488567104a1SPoul-Henning Kamp 	struct swdevt *sp;
2489567104a1SPoul-Henning Kamp 
2490567104a1SPoul-Henning Kamp 	*total = 0;
2491567104a1SPoul-Henning Kamp 	*used = 0;
249220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
24938f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2494567104a1SPoul-Henning Kamp 		*total += sp->sw_nblks;
2495567104a1SPoul-Henning Kamp 		*used += sp->sw_used;
2496567104a1SPoul-Henning Kamp 	}
249720da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2498567104a1SPoul-Henning Kamp }
2499567104a1SPoul-Henning Kamp 
2500dda4f960SKonstantin Belousov int
2501dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2502dda4f960SKonstantin Belousov {
2503dda4f960SKonstantin Belousov 	struct swdevt *sp;
25047870adb6SEd Schouten 	const char *tmp_devname;
2505dda4f960SKonstantin Belousov 	int error, n;
2506dda4f960SKonstantin Belousov 
2507dda4f960SKonstantin Belousov 	n = 0;
2508dda4f960SKonstantin Belousov 	error = ENOENT;
2509dda4f960SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2510dda4f960SKonstantin Belousov 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2511dda4f960SKonstantin Belousov 		if (n != name) {
2512dda4f960SKonstantin Belousov 			n++;
2513dda4f960SKonstantin Belousov 			continue;
2514dda4f960SKonstantin Belousov 		}
2515dda4f960SKonstantin Belousov 		xs->xsw_version = XSWDEV_VERSION;
2516dda4f960SKonstantin Belousov 		xs->xsw_dev = sp->sw_dev;
2517dda4f960SKonstantin Belousov 		xs->xsw_flags = sp->sw_flags;
2518dda4f960SKonstantin Belousov 		xs->xsw_nblks = sp->sw_nblks;
2519dda4f960SKonstantin Belousov 		xs->xsw_used = sp->sw_used;
2520dda4f960SKonstantin Belousov 		if (devname != NULL) {
2521dda4f960SKonstantin Belousov 			if (vn_isdisk(sp->sw_vp, NULL))
25227870adb6SEd Schouten 				tmp_devname = devtoname(sp->sw_vp->v_rdev);
2523dda4f960SKonstantin Belousov 			else
2524dda4f960SKonstantin Belousov 				tmp_devname = "[file]";
2525dda4f960SKonstantin Belousov 			strncpy(devname, tmp_devname, len);
2526dda4f960SKonstantin Belousov 		}
2527dda4f960SKonstantin Belousov 		error = 0;
2528dda4f960SKonstantin Belousov 		break;
2529dda4f960SKonstantin Belousov 	}
2530dda4f960SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2531dda4f960SKonstantin Belousov 	return (error);
2532dda4f960SKonstantin Belousov }
2533dda4f960SKonstantin Belousov 
253469921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
253569921123SKonstantin Belousov #define XSWDEV_VERSION_11	1
253669921123SKonstantin Belousov struct xswdev11 {
253769921123SKonstantin Belousov 	u_int	xsw_version;
253869921123SKonstantin Belousov 	uint32_t xsw_dev;
253969921123SKonstantin Belousov 	int	xsw_flags;
254069921123SKonstantin Belousov 	int	xsw_nblks;
254169921123SKonstantin Belousov 	int     xsw_used;
254269921123SKonstantin Belousov };
254369921123SKonstantin Belousov #endif
254469921123SKonstantin Belousov 
2545f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2546f6d281e8SKonstantin Belousov struct xswdev32 {
2547f6d281e8SKonstantin Belousov 	u_int	xsw_version;
2548f6d281e8SKonstantin Belousov 	u_int	xsw_dev1, xsw_dev2;
2549f6d281e8SKonstantin Belousov 	int	xsw_flags;
2550f6d281e8SKonstantin Belousov 	int	xsw_nblks;
2551f6d281e8SKonstantin Belousov 	int     xsw_used;
2552f6d281e8SKonstantin Belousov };
2553f6d281e8SKonstantin Belousov #endif
2554f6d281e8SKonstantin Belousov 
2555e9c0cc15SPoul-Henning Kamp static int
2556e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2557e9c0cc15SPoul-Henning Kamp {
2558e9c0cc15SPoul-Henning Kamp 	struct xswdev xs;
2559f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2560f6d281e8SKonstantin Belousov 	struct xswdev32 xs32;
2561f6d281e8SKonstantin Belousov #endif
256269921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
256369921123SKonstantin Belousov 	struct xswdev11 xs11;
256469921123SKonstantin Belousov #endif
2565dda4f960SKonstantin Belousov 	int error;
2566e9c0cc15SPoul-Henning Kamp 
2567e9c0cc15SPoul-Henning Kamp 	if (arg2 != 1)			/* name length */
2568e9c0cc15SPoul-Henning Kamp 		return (EINVAL);
2569dda4f960SKonstantin Belousov 	error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2570dda4f960SKonstantin Belousov 	if (error != 0)
2571dda4f960SKonstantin Belousov 		return (error);
2572f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2573f6d281e8SKonstantin Belousov 	if (req->oldlen == sizeof(xs32)) {
2574f6d281e8SKonstantin Belousov 		xs32.xsw_version = XSWDEV_VERSION;
2575f6d281e8SKonstantin Belousov 		xs32.xsw_dev1 = xs.xsw_dev;
2576f6d281e8SKonstantin Belousov 		xs32.xsw_dev2 = xs.xsw_dev >> 32;
2577f6d281e8SKonstantin Belousov 		xs32.xsw_flags = xs.xsw_flags;
2578f6d281e8SKonstantin Belousov 		xs32.xsw_nblks = xs.xsw_nblks;
2579f6d281e8SKonstantin Belousov 		xs32.xsw_used = xs.xsw_used;
2580f6d281e8SKonstantin Belousov 		error = SYSCTL_OUT(req, &xs32, sizeof(xs32));
2581f6d281e8SKonstantin Belousov 		return (error);
2582f6d281e8SKonstantin Belousov 	}
2583f6d281e8SKonstantin Belousov #endif
258469921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
258569921123SKonstantin Belousov 	if (req->oldlen == sizeof(xs11)) {
258669921123SKonstantin Belousov 		xs11.xsw_version = XSWDEV_VERSION_11;
258769921123SKonstantin Belousov 		xs11.xsw_dev = xs.xsw_dev; /* truncation */
258869921123SKonstantin Belousov 		xs11.xsw_flags = xs.xsw_flags;
258969921123SKonstantin Belousov 		xs11.xsw_nblks = xs.xsw_nblks;
259069921123SKonstantin Belousov 		xs11.xsw_used = xs.xsw_used;
259169921123SKonstantin Belousov 		error = SYSCTL_OUT(req, &xs11, sizeof(xs11));
2592f6d281e8SKonstantin Belousov 		return (error);
2593f6d281e8SKonstantin Belousov 	}
259469921123SKonstantin Belousov #endif
2595e9c0cc15SPoul-Henning Kamp 	error = SYSCTL_OUT(req, &xs, sizeof(xs));
2596e9c0cc15SPoul-Henning Kamp 	return (error);
2597e9c0cc15SPoul-Henning Kamp }
2598e9c0cc15SPoul-Henning Kamp 
25998f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2600e9c0cc15SPoul-Henning Kamp     "Number of swap devices");
26014c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE,
26024c36e917SKonstantin Belousov     sysctl_vm_swap_info,
2603e9c0cc15SPoul-Henning Kamp     "Swap statistics by device");
2604ec38b344SPoul-Henning Kamp 
2605ec38b344SPoul-Henning Kamp /*
2606f425ab8eSKonstantin Belousov  * Count the approximate swap usage in pages for a vmspace.  The
2607f425ab8eSKonstantin Belousov  * shadowed or not yet copied on write swap blocks are not accounted.
2608ec38b344SPoul-Henning Kamp  * The map must be locked.
2609ec38b344SPoul-Henning Kamp  */
26102860553aSRebecca Cran long
2611ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2612ec38b344SPoul-Henning Kamp {
261365d8409cSRebecca Cran 	vm_map_t map;
2614ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
261565d8409cSRebecca Cran 	vm_object_t object;
2616f425ab8eSKonstantin Belousov 	struct swblk *sb;
2617f425ab8eSKonstantin Belousov 	vm_pindex_t e, pi;
2618f425ab8eSKonstantin Belousov 	long count;
2619f425ab8eSKonstantin Belousov 	int i;
262065d8409cSRebecca Cran 
262165d8409cSRebecca Cran 	map = &vmspace->vm_map;
262265d8409cSRebecca Cran 	count = 0;
2623ec38b344SPoul-Henning Kamp 
2624*2288078cSDoug Moore 	VM_MAP_ENTRY_FOREACH(cur, map) {
2625f425ab8eSKonstantin Belousov 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2626f425ab8eSKonstantin Belousov 			continue;
2627f425ab8eSKonstantin Belousov 		object = cur->object.vm_object;
2628f425ab8eSKonstantin Belousov 		if (object == NULL || object->type != OBJT_SWAP)
2629f425ab8eSKonstantin Belousov 			continue;
2630f425ab8eSKonstantin Belousov 		VM_OBJECT_RLOCK(object);
2631f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
2632f425ab8eSKonstantin Belousov 			goto unlock;
2633f425ab8eSKonstantin Belousov 		pi = OFF_TO_IDX(cur->offset);
2634f425ab8eSKonstantin Belousov 		e = pi + OFF_TO_IDX(cur->end - cur->start);
2635f425ab8eSKonstantin Belousov 		for (;; pi = sb->p + SWAP_META_PAGES) {
2636f425ab8eSKonstantin Belousov 			sb = SWAP_PCTRIE_LOOKUP_GE(
2637f425ab8eSKonstantin Belousov 			    &object->un_pager.swp.swp_blks, pi);
2638f425ab8eSKonstantin Belousov 			if (sb == NULL || sb->p >= e)
2639f425ab8eSKonstantin Belousov 				break;
2640f425ab8eSKonstantin Belousov 			for (i = 0; i < SWAP_META_PAGES; i++) {
2641f425ab8eSKonstantin Belousov 				if (sb->p + i < e &&
2642f425ab8eSKonstantin Belousov 				    sb->d[i] != SWAPBLK_NONE)
2643f425ab8eSKonstantin Belousov 					count++;
2644ec38b344SPoul-Henning Kamp 			}
2645ec38b344SPoul-Henning Kamp 		}
2646f425ab8eSKonstantin Belousov unlock:
2647f425ab8eSKonstantin Belousov 		VM_OBJECT_RUNLOCK(object);
2648ec38b344SPoul-Henning Kamp 	}
2649ec38b344SPoul-Henning Kamp 	return (count);
2650ec38b344SPoul-Henning Kamp }
2651dee34ca4SPoul-Henning Kamp 
2652dee34ca4SPoul-Henning Kamp /*
2653dee34ca4SPoul-Henning Kamp  * GEOM backend
2654dee34ca4SPoul-Henning Kamp  *
2655dee34ca4SPoul-Henning Kamp  * Swapping onto disk devices.
2656dee34ca4SPoul-Henning Kamp  *
2657dee34ca4SPoul-Henning Kamp  */
2658dee34ca4SPoul-Henning Kamp 
26595721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan;
26605721c9c7SPoul-Henning Kamp 
2661dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = {
2662dee34ca4SPoul-Henning Kamp 	.name = "SWAP",
26635721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
26645721c9c7SPoul-Henning Kamp 	.orphan = swapgeom_orphan,
2665dee34ca4SPoul-Henning Kamp };
2666dee34ca4SPoul-Henning Kamp 
2667dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class);
2668dee34ca4SPoul-Henning Kamp 
2669dee34ca4SPoul-Henning Kamp 
2670dee34ca4SPoul-Henning Kamp static void
26713398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags)
26723398491bSAlexander Motin {
26733398491bSAlexander Motin 	struct g_consumer *cp;
26743398491bSAlexander Motin 
26753398491bSAlexander Motin 	cp = arg;
26763398491bSAlexander Motin 	g_access(cp, -1, -1, 0);
26773398491bSAlexander Motin 	g_detach(cp);
26783398491bSAlexander Motin 	g_destroy_consumer(cp);
26793398491bSAlexander Motin }
26803398491bSAlexander Motin 
26819e3e3fe5SWarner Losh /*
26829e3e3fe5SWarner Losh  * Add a reference to the g_consumer for an inflight transaction.
26839e3e3fe5SWarner Losh  */
26849e3e3fe5SWarner Losh static void
26859e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp)
26869e3e3fe5SWarner Losh {
26879e3e3fe5SWarner Losh 
26889e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
26899e3e3fe5SWarner Losh 	cp->index++;
26909e3e3fe5SWarner Losh }
26919e3e3fe5SWarner Losh 
26929e3e3fe5SWarner Losh /*
26930c657d22SKonstantin Belousov  * Remove a reference from the g_consumer.  Post a close event if all
26940c657d22SKonstantin Belousov  * references go away, since the function might be called from the
26950c657d22SKonstantin Belousov  * biodone context.
26969e3e3fe5SWarner Losh  */
26979e3e3fe5SWarner Losh static void
26989e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
26999e3e3fe5SWarner Losh {
27009e3e3fe5SWarner Losh 
27019e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
27029e3e3fe5SWarner Losh 	cp->index--;
27039e3e3fe5SWarner Losh 	if (cp->index == 0) {
27049e3e3fe5SWarner Losh 		if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
27059e3e3fe5SWarner Losh 			sp->sw_id = NULL;
27069e3e3fe5SWarner Losh 	}
27079e3e3fe5SWarner Losh }
27089e3e3fe5SWarner Losh 
27093398491bSAlexander Motin static void
2710dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2)
2711dee34ca4SPoul-Henning Kamp {
27123398491bSAlexander Motin 	struct swdevt *sp;
2713dee34ca4SPoul-Henning Kamp 	struct buf *bp;
27143398491bSAlexander Motin 	struct g_consumer *cp;
2715dee34ca4SPoul-Henning Kamp 
2716dee34ca4SPoul-Henning Kamp 	bp = bp2->bio_caller2;
27173398491bSAlexander Motin 	cp = bp2->bio_from;
2718c5d3d25eSPoul-Henning Kamp 	bp->b_ioflags = bp2->bio_flags;
2719dee34ca4SPoul-Henning Kamp 	if (bp2->bio_error)
2720dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2721c5d3d25eSPoul-Henning Kamp 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2722c5d3d25eSPoul-Henning Kamp 	bp->b_error = bp2->bio_error;
2723756a5412SGleb Smirnoff 	bp->b_caller1 = NULL;
2724dee34ca4SPoul-Henning Kamp 	bufdone(bp);
27253398491bSAlexander Motin 	sp = bp2->bio_caller1;
27269e3e3fe5SWarner Losh 	mtx_lock(&sw_dev_mtx);
27279e3e3fe5SWarner Losh 	swapgeom_release(cp, sp);
27283398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
2729dee34ca4SPoul-Henning Kamp 	g_destroy_bio(bp2);
2730dee34ca4SPoul-Henning Kamp }
2731dee34ca4SPoul-Henning Kamp 
2732dee34ca4SPoul-Henning Kamp static void
2733dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2734dee34ca4SPoul-Henning Kamp {
2735dee34ca4SPoul-Henning Kamp 	struct bio *bio;
2736dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2737dee34ca4SPoul-Henning Kamp 
27383398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
2739dee34ca4SPoul-Henning Kamp 	cp = sp->sw_id;
2740dee34ca4SPoul-Henning Kamp 	if (cp == NULL) {
27413398491bSAlexander Motin 		mtx_unlock(&sw_dev_mtx);
2742dee34ca4SPoul-Henning Kamp 		bp->b_error = ENXIO;
2743dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2744dee34ca4SPoul-Henning Kamp 		bufdone(bp);
2745dee34ca4SPoul-Henning Kamp 		return;
2746dee34ca4SPoul-Henning Kamp 	}
27479e3e3fe5SWarner Losh 	swapgeom_acquire(cp);
27483398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
274911041003SKonstantin Belousov 	if (bp->b_iocmd == BIO_WRITE)
275011041003SKonstantin Belousov 		bio = g_new_bio();
275111041003SKonstantin Belousov 	else
27524f8205e5SPoul-Henning Kamp 		bio = g_alloc_bio();
27534f8205e5SPoul-Henning Kamp 	if (bio == NULL) {
27549e3e3fe5SWarner Losh 		mtx_lock(&sw_dev_mtx);
27559e3e3fe5SWarner Losh 		swapgeom_release(cp, sp);
27569e3e3fe5SWarner Losh 		mtx_unlock(&sw_dev_mtx);
27573e5b6861SPoul-Henning Kamp 		bp->b_error = ENOMEM;
27583e5b6861SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
27590b208315SEdward Tomasz Napierala 		printf("swap_pager: cannot allocate bio\n");
27603e5b6861SPoul-Henning Kamp 		bufdone(bp);
27613e5b6861SPoul-Henning Kamp 		return;
27623e5b6861SPoul-Henning Kamp 	}
276311041003SKonstantin Belousov 
2764756a5412SGleb Smirnoff 	bp->b_caller1 = bio;
27653398491bSAlexander Motin 	bio->bio_caller1 = sp;
2766dee34ca4SPoul-Henning Kamp 	bio->bio_caller2 = bp;
2767c5d3d25eSPoul-Henning Kamp 	bio->bio_cmd = bp->b_iocmd;
2768dee34ca4SPoul-Henning Kamp 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2769dee34ca4SPoul-Henning Kamp 	bio->bio_length = bp->b_bcount;
2770dee34ca4SPoul-Henning Kamp 	bio->bio_done = swapgeom_done;
2771fade8dd7SJeff Roberson 	if (!buf_mapped(bp)) {
27722cc718a1SKonstantin Belousov 		bio->bio_ma = bp->b_pages;
27732cc718a1SKonstantin Belousov 		bio->bio_data = unmapped_buf;
27742cc718a1SKonstantin Belousov 		bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
27752cc718a1SKonstantin Belousov 		bio->bio_ma_n = bp->b_npages;
27762cc718a1SKonstantin Belousov 		bio->bio_flags |= BIO_UNMAPPED;
27772cc718a1SKonstantin Belousov 	} else {
27782cc718a1SKonstantin Belousov 		bio->bio_data = bp->b_data;
27792cc718a1SKonstantin Belousov 		bio->bio_ma = NULL;
27802cc718a1SKonstantin Belousov 	}
2781dee34ca4SPoul-Henning Kamp 	g_io_request(bio, cp);
2782dee34ca4SPoul-Henning Kamp 	return;
2783dee34ca4SPoul-Henning Kamp }
2784dee34ca4SPoul-Henning Kamp 
2785dee34ca4SPoul-Henning Kamp static void
2786dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp)
2787dee34ca4SPoul-Henning Kamp {
2788dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
27893398491bSAlexander Motin 	int destroy;
2790dee34ca4SPoul-Henning Kamp 
2791dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
27923398491bSAlexander Motin 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
27933398491bSAlexander Motin 		if (sp->sw_id == cp) {
27948f12d83aSAlexander Motin 			sp->sw_flags |= SW_CLOSING;
27953398491bSAlexander Motin 			break;
2796dee34ca4SPoul-Henning Kamp 		}
27973398491bSAlexander Motin 	}
27989e3e3fe5SWarner Losh 	/*
27999e3e3fe5SWarner Losh 	 * Drop reference we were created with. Do directly since we're in a
28009e3e3fe5SWarner Losh 	 * special context where we don't have to queue the call to
28019e3e3fe5SWarner Losh 	 * swapgeom_close_ev().
28029e3e3fe5SWarner Losh 	 */
28039e3e3fe5SWarner Losh 	cp->index--;
28043398491bSAlexander Motin 	destroy = ((sp != NULL) && (cp->index == 0));
28053398491bSAlexander Motin 	if (destroy)
28063398491bSAlexander Motin 		sp->sw_id = NULL;
28073398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
28083398491bSAlexander Motin 	if (destroy)
28093398491bSAlexander Motin 		swapgeom_close_ev(cp, 0);
2810dee34ca4SPoul-Henning Kamp }
2811dee34ca4SPoul-Henning Kamp 
2812dee34ca4SPoul-Henning Kamp static void
2813dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw)
2814dee34ca4SPoul-Henning Kamp {
28153398491bSAlexander Motin 	struct g_consumer *cp;
2816dee34ca4SPoul-Henning Kamp 
28173398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
28183398491bSAlexander Motin 	cp = sw->sw_id;
28193398491bSAlexander Motin 	sw->sw_id = NULL;
28203398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
28210c657d22SKonstantin Belousov 
28220c657d22SKonstantin Belousov 	/*
28230c657d22SKonstantin Belousov 	 * swapgeom_close() may be called from the biodone context,
28240c657d22SKonstantin Belousov 	 * where we cannot perform topology changes.  Delegate the
28250c657d22SKonstantin Belousov 	 * work to the events thread.
28260c657d22SKonstantin Belousov 	 */
28273398491bSAlexander Motin 	if (cp != NULL)
28283398491bSAlexander Motin 		g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2829dee34ca4SPoul-Henning Kamp }
2830dee34ca4SPoul-Henning Kamp 
283188ad2d7bSKonstantin Belousov static int
283288ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp)
2833dee34ca4SPoul-Henning Kamp {
2834dee34ca4SPoul-Henning Kamp 	struct g_provider *pp;
2835dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2836dee34ca4SPoul-Henning Kamp 	static struct g_geom *gp;
2837dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2838dee34ca4SPoul-Henning Kamp 	u_long nblks;
2839dee34ca4SPoul-Henning Kamp 	int error;
2840dee34ca4SPoul-Henning Kamp 
284188ad2d7bSKonstantin Belousov 	pp = g_dev_getprovider(dev);
284288ad2d7bSKonstantin Belousov 	if (pp == NULL)
284388ad2d7bSKonstantin Belousov 		return (ENODEV);
2844dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2845dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2846dee34ca4SPoul-Henning Kamp 		cp = sp->sw_id;
2847dee34ca4SPoul-Henning Kamp 		if (cp != NULL && cp->provider == pp) {
2848dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
284988ad2d7bSKonstantin Belousov 			return (EBUSY);
2850dee34ca4SPoul-Henning Kamp 		}
2851dee34ca4SPoul-Henning Kamp 	}
2852dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
28535721c9c7SPoul-Henning Kamp 	if (gp == NULL)
285402c62349SJaakko Heinonen 		gp = g_new_geomf(&g_swap_class, "swap");
2855dee34ca4SPoul-Henning Kamp 	cp = g_new_consumer(gp);
28569e3e3fe5SWarner Losh 	cp->index = 1;	/* Number of active I/Os, plus one for being active. */
28579e3e3fe5SWarner Losh 	cp->flags |=  G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2858dee34ca4SPoul-Henning Kamp 	g_attach(cp, pp);
2859afeb65e6SPoul-Henning Kamp 	/*
2860afeb65e6SPoul-Henning Kamp 	 * XXX: Every time you think you can improve the margin for
2861afeb65e6SPoul-Henning Kamp 	 * footshooting, somebody depends on the ability to do so:
2862afeb65e6SPoul-Henning Kamp 	 * savecore(8) wants to write to our swapdev so we cannot
2863afeb65e6SPoul-Henning Kamp 	 * set an exclusive count :-(
2864afeb65e6SPoul-Henning Kamp 	 */
2865d2bae332SPoul-Henning Kamp 	error = g_access(cp, 1, 1, 0);
286688ad2d7bSKonstantin Belousov 	if (error != 0) {
2867dee34ca4SPoul-Henning Kamp 		g_detach(cp);
2868dee34ca4SPoul-Henning Kamp 		g_destroy_consumer(cp);
286988ad2d7bSKonstantin Belousov 		return (error);
2870dee34ca4SPoul-Henning Kamp 	}
2871dee34ca4SPoul-Henning Kamp 	nblks = pp->mediasize / DEV_BSIZE;
287288ad2d7bSKonstantin Belousov 	swaponsomething(vp, cp, nblks, swapgeom_strategy,
287388ad2d7bSKonstantin Belousov 	    swapgeom_close, dev2udev(dev),
28742cc718a1SKonstantin Belousov 	    (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
287588ad2d7bSKonstantin Belousov 	return (0);
2876dee34ca4SPoul-Henning Kamp }
2877dee34ca4SPoul-Henning Kamp 
2878dee34ca4SPoul-Henning Kamp static int
287988ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp)
2880dee34ca4SPoul-Henning Kamp {
2881dee34ca4SPoul-Henning Kamp 	int error;
2882dee34ca4SPoul-Henning Kamp 
2883cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
288488ad2d7bSKonstantin Belousov 	if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) {
288588ad2d7bSKonstantin Belousov 		error = ENOENT;
288688ad2d7bSKonstantin Belousov 	} else {
288788ad2d7bSKonstantin Belousov 		g_topology_lock();
288888ad2d7bSKonstantin Belousov 		error = swapongeom_locked(vp->v_rdev, vp);
288988ad2d7bSKonstantin Belousov 		g_topology_unlock();
289088ad2d7bSKonstantin Belousov 	}
289122db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
2892dee34ca4SPoul-Henning Kamp 	return (error);
2893dee34ca4SPoul-Henning Kamp }
2894dee34ca4SPoul-Henning Kamp 
2895dee34ca4SPoul-Henning Kamp /*
2896dee34ca4SPoul-Henning Kamp  * VNODE backend
2897dee34ca4SPoul-Henning Kamp  *
2898dee34ca4SPoul-Henning Kamp  * This is used mainly for network filesystem (read: probably only tested
2899dee34ca4SPoul-Henning Kamp  * with NFS) swapfiles.
2900dee34ca4SPoul-Henning Kamp  *
2901dee34ca4SPoul-Henning Kamp  */
2902dee34ca4SPoul-Henning Kamp 
2903dee34ca4SPoul-Henning Kamp static void
2904dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp)
2905dee34ca4SPoul-Henning Kamp {
2906494eb176SPoul-Henning Kamp 	struct vnode *vp2;
2907dee34ca4SPoul-Henning Kamp 
2908dee34ca4SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2909dee34ca4SPoul-Henning Kamp 
2910dee34ca4SPoul-Henning Kamp 	vp2 = sp->sw_id;
2911dee34ca4SPoul-Henning Kamp 	vhold(vp2);
2912dee34ca4SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
29133cfc7651SOlivier Houchard 		if (bp->b_bufobj)
2914494eb176SPoul-Henning Kamp 			bufobj_wdrop(bp->b_bufobj);
2915a76d8f4eSPoul-Henning Kamp 		bufobj_wref(&vp2->v_bufobj);
2916dee34ca4SPoul-Henning Kamp 	}
29173cfc7651SOlivier Houchard 	if (bp->b_bufobj != &vp2->v_bufobj)
29183cfc7651SOlivier Houchard 		bp->b_bufobj = &vp2->v_bufobj;
2919dee34ca4SPoul-Henning Kamp 	bp->b_vp = vp2;
29202c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
2921b792bebeSPoul-Henning Kamp 	bstrategy(bp);
2922dee34ca4SPoul-Henning Kamp 	return;
2923dee34ca4SPoul-Henning Kamp }
2924dee34ca4SPoul-Henning Kamp 
2925dee34ca4SPoul-Henning Kamp static void
2926dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp)
2927dee34ca4SPoul-Henning Kamp {
2928dee34ca4SPoul-Henning Kamp 
2929dee34ca4SPoul-Henning Kamp 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2930dee34ca4SPoul-Henning Kamp 	vrele(sp->sw_vp);
2931dee34ca4SPoul-Henning Kamp }
2932dee34ca4SPoul-Henning Kamp 
2933dee34ca4SPoul-Henning Kamp 
2934dee34ca4SPoul-Henning Kamp static int
2935dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2936dee34ca4SPoul-Henning Kamp {
2937dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2938dee34ca4SPoul-Henning Kamp 	int error;
2939dee34ca4SPoul-Henning Kamp 
2940dee34ca4SPoul-Henning Kamp 	if (nblks == 0)
2941dee34ca4SPoul-Henning Kamp 		return (ENXIO);
2942dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2943dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2944dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == vp) {
2945dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2946dee34ca4SPoul-Henning Kamp 			return (EBUSY);
2947dee34ca4SPoul-Henning Kamp 		}
2948dee34ca4SPoul-Henning Kamp 	}
2949dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2950dee34ca4SPoul-Henning Kamp 
2951cb05b60aSAttilio Rao 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2952dee34ca4SPoul-Henning Kamp #ifdef MAC
295330d239bcSRobert Watson 	error = mac_system_check_swapon(td->td_ucred, vp);
2954dee34ca4SPoul-Henning Kamp 	if (error == 0)
2955dee34ca4SPoul-Henning Kamp #endif
29569e223287SKonstantin Belousov 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
295722db15c0SAttilio Rao 	(void) VOP_UNLOCK(vp, 0);
2958dee34ca4SPoul-Henning Kamp 	if (error)
2959dee34ca4SPoul-Henning Kamp 		return (error);
2960dee34ca4SPoul-Henning Kamp 
2961dee34ca4SPoul-Henning Kamp 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
29622cc718a1SKonstantin Belousov 	    NODEV, 0);
2963dee34ca4SPoul-Henning Kamp 	return (0);
2964dee34ca4SPoul-Henning Kamp }
296589c241d1SGleb Smirnoff 
296689c241d1SGleb Smirnoff static int
296789c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
296889c241d1SGleb Smirnoff {
296989c241d1SGleb Smirnoff 	int error, new, n;
297089c241d1SGleb Smirnoff 
297189c241d1SGleb Smirnoff 	new = nsw_wcount_async_max;
297289c241d1SGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
297389c241d1SGleb Smirnoff 	if (error != 0 || req->newptr == NULL)
297489c241d1SGleb Smirnoff 		return (error);
297589c241d1SGleb Smirnoff 
297689c241d1SGleb Smirnoff 	if (new > nswbuf / 2 || new < 1)
297789c241d1SGleb Smirnoff 		return (EINVAL);
297889c241d1SGleb Smirnoff 
2979756a5412SGleb Smirnoff 	mtx_lock(&swbuf_mtx);
298089c241d1SGleb Smirnoff 	while (nsw_wcount_async_max != new) {
298189c241d1SGleb Smirnoff 		/*
298289c241d1SGleb Smirnoff 		 * Adjust difference.  If the current async count is too low,
298389c241d1SGleb Smirnoff 		 * we will need to sqeeze our update slowly in.  Sleep with a
298489c241d1SGleb Smirnoff 		 * higher priority than getpbuf() to finish faster.
298589c241d1SGleb Smirnoff 		 */
298689c241d1SGleb Smirnoff 		n = new - nsw_wcount_async_max;
298789c241d1SGleb Smirnoff 		if (nsw_wcount_async + n >= 0) {
298889c241d1SGleb Smirnoff 			nsw_wcount_async += n;
298989c241d1SGleb Smirnoff 			nsw_wcount_async_max += n;
299089c241d1SGleb Smirnoff 			wakeup(&nsw_wcount_async);
299189c241d1SGleb Smirnoff 		} else {
299289c241d1SGleb Smirnoff 			nsw_wcount_async_max -= nsw_wcount_async;
299389c241d1SGleb Smirnoff 			nsw_wcount_async = 0;
2994756a5412SGleb Smirnoff 			msleep(&nsw_wcount_async, &swbuf_mtx, PSWP,
299589c241d1SGleb Smirnoff 			    "swpsysctl", 0);
299689c241d1SGleb Smirnoff 		}
299789c241d1SGleb Smirnoff 	}
2998756a5412SGleb Smirnoff 	mtx_unlock(&swbuf_mtx);
299989c241d1SGleb Smirnoff 
300089c241d1SGleb Smirnoff 	return (0);
300189c241d1SGleb Smirnoff }
3002fe7bcbafSKyle Evans 
3003fe7bcbafSKyle Evans static void
3004fe7bcbafSKyle Evans swap_pager_update_writecount(vm_object_t object, vm_offset_t start,
3005fe7bcbafSKyle Evans     vm_offset_t end)
3006fe7bcbafSKyle Evans {
3007fe7bcbafSKyle Evans 
3008fe7bcbafSKyle Evans 	VM_OBJECT_WLOCK(object);
3009fe7bcbafSKyle Evans 	KASSERT((object->flags & OBJ_NOSPLIT) != 0,
3010fe7bcbafSKyle Evans 	    ("Splittable object with writecount"));
3011fe7bcbafSKyle Evans 	object->un_pager.swp.writemappings += (vm_ooffset_t)end - start;
3012fe7bcbafSKyle Evans 	VM_OBJECT_WUNLOCK(object);
3013fe7bcbafSKyle Evans }
3014fe7bcbafSKyle Evans 
3015fe7bcbafSKyle Evans static void
3016fe7bcbafSKyle Evans swap_pager_release_writecount(vm_object_t object, vm_offset_t start,
3017fe7bcbafSKyle Evans     vm_offset_t end)
3018fe7bcbafSKyle Evans {
3019fe7bcbafSKyle Evans 
3020fe7bcbafSKyle Evans 	VM_OBJECT_WLOCK(object);
3021fe7bcbafSKyle Evans 	KASSERT((object->flags & OBJ_NOSPLIT) != 0,
3022fe7bcbafSKyle Evans 	    ("Splittable object with writecount"));
3023fe7bcbafSKyle Evans 	object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start;
3024fe7bcbafSKyle Evans 	VM_OBJECT_WUNLOCK(object);
3025fe7bcbafSKyle Evans }
3026