xref: /freebsd/sys/vm/swap_pager.c (revision 56948d177edd978923350a5e052cfaf30116210d)
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>
82e2e050c8SConrad Meyer #include <sys/eventhandler.h>
83e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h>
84e2e050c8SConrad Meyer #include <sys/lock.h>
85e2e050c8SConrad Meyer #include <sys/kernel.h>
86e9c0cc15SPoul-Henning Kamp #include <sys/mount.h>
87e9c0cc15SPoul-Henning Kamp #include <sys/namei.h>
88df8bae1dSRodney W. Grimes #include <sys/malloc.h>
89f425ab8eSKonstantin Belousov #include <sys/pctrie.h>
90e2e050c8SConrad Meyer #include <sys/priv.h>
91e2e050c8SConrad Meyer #include <sys/proc.h>
921ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
933364c323SKonstantin Belousov #include <sys/resource.h>
943364c323SKonstantin Belousov #include <sys/resourcevar.h>
9589f6b863SAttilio Rao #include <sys/rwlock.h>
96d027ed2eSAlan Cox #include <sys/sbuf.h>
97327f4e83SMatthew Dillon #include <sys/sysctl.h>
98e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
99e2e050c8SConrad Meyer #include <sys/systm.h>
1000cddd8f0SMatthew Dillon #include <sys/sx.h>
101936524aaSMatthew Dillon #include <sys/vmmeter.h>
102e2e050c8SConrad Meyer #include <sys/vnode.h>
103df8bae1dSRodney W. Grimes 
104aed55708SRobert Watson #include <security/mac/mac_framework.h>
105aed55708SRobert Watson 
106df8bae1dSRodney W. Grimes #include <vm/vm.h>
10721cd6e62SSeigo Tanimura #include <vm/pmap.h>
10821cd6e62SSeigo Tanimura #include <vm/vm_map.h>
10921cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
110efeaf95aSDavid Greenman #include <vm/vm_object.h>
111df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
112efeaf95aSDavid Greenman #include <vm/vm_pager.h>
113df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
114e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
115df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
116efeaf95aSDavid Greenman #include <vm/vm_extern.h>
117670d17b5SJeff Roberson #include <vm/uma.h>
118df8bae1dSRodney W. Grimes 
119dee34ca4SPoul-Henning Kamp #include <geom/geom.h>
120dee34ca4SPoul-Henning Kamp 
121ec38b344SPoul-Henning Kamp /*
122064650c1SAlan Cox  * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64.
123064650c1SAlan Cox  * The 64-page limit is due to the radix code (kern/subr_blist.c).
124ec38b344SPoul-Henning Kamp  */
125ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
126e2241590SAlan Cox #define	MAX_PAGEOUT_CLUSTER	32
127ec38b344SPoul-Henning Kamp #endif
128ec38b344SPoul-Henning Kamp 
129ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
130ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
131ec38b344SPoul-Henning Kamp #endif
132ec38b344SPoul-Henning Kamp 
133f425ab8eSKonstantin Belousov #define	SWAP_META_PAGES		PCTRIE_COUNT
134ec38b344SPoul-Henning Kamp 
135f425ab8eSKonstantin Belousov /*
136f425ab8eSKonstantin Belousov  * A swblk structure maps each page index within a
137f425ab8eSKonstantin Belousov  * SWAP_META_PAGES-aligned and sized range to the address of an
138f425ab8eSKonstantin Belousov  * on-disk swap block (or SWAPBLK_NONE). The collection of these
139f425ab8eSKonstantin Belousov  * mappings for an entire vm object is implemented as a pc-trie.
140f425ab8eSKonstantin Belousov  */
141f425ab8eSKonstantin Belousov struct swblk {
142f425ab8eSKonstantin Belousov 	vm_pindex_t	p;
143f425ab8eSKonstantin Belousov 	daddr_t		d[SWAP_META_PAGES];
144e9c0cc15SPoul-Henning Kamp };
145e9c0cc15SPoul-Henning Kamp 
1462c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
14720da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx;
1488f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1498f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1508f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1518f60c087SPoul-Henning Kamp int swap_pager_avail;
15204533e1eSKonstantin Belousov static struct sx swdev_syscall_lock;	/* serialize swap(on|off) */
153e9c0cc15SPoul-Henning Kamp 
154e8bb589dSMatt Macy static u_long swap_reserved;
155e8bb589dSMatt Macy static u_long swap_total;
156e8bb589dSMatt Macy static int sysctl_page_shift(SYSCTL_HANDLER_ARGS);
157e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
158e8bb589dSMatt Macy     &swap_reserved, 0, sysctl_page_shift, "A",
1598a9c731fSIvan Voras     "Amount of swap storage needed to back all allocated anonymous memory.");
160e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
161e8bb589dSMatt Macy     &swap_total, 0, sysctl_page_shift, "A",
162e8bb589dSMatt Macy     "Total amount of available swap storage.");
163e8bb589dSMatt Macy 
1643364c323SKonstantin Belousov static int overcommit = 0;
165be7d4ac5SEdward Tomasz Napierala SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0,
1668a9c731fSIvan Voras     "Configure virtual memory overcommit behavior. See tuning(7) "
1678a9c731fSIvan Voras     "for details.");
16861203277SDag-Erling Smørgrav static unsigned long swzone;
16961203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
17061203277SDag-Erling Smørgrav     "Actual size of swap metadata zone");
17161203277SDag-Erling Smørgrav static unsigned long swap_maxpages;
17261203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
17361203277SDag-Erling Smørgrav     "Maximum amount of swap supported");
1743364c323SKonstantin Belousov 
1753364c323SKonstantin Belousov /* bits from overcommit */
1763364c323SKonstantin Belousov #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
1773364c323SKonstantin Belousov #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
1783364c323SKonstantin Belousov #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
1793364c323SKonstantin Belousov 
180e8bb589dSMatt Macy static int
181e8bb589dSMatt Macy sysctl_page_shift(SYSCTL_HANDLER_ARGS)
182e8bb589dSMatt Macy {
183e8bb589dSMatt Macy 	uint64_t newval;
184e8bb589dSMatt Macy 	u_long value = *(u_long *)arg1;
185e8bb589dSMatt Macy 
186e8bb589dSMatt Macy 	newval = ((uint64_t)value) << PAGE_SHIFT;
187e8bb589dSMatt Macy 	return (sysctl_handle_64(oidp, &newval, 0, req));
188e8bb589dSMatt Macy }
189e8bb589dSMatt Macy 
1903364c323SKonstantin Belousov int
1913364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr)
1923364c323SKonstantin Belousov {
1933364c323SKonstantin Belousov 
194ef694c1aSEdward Tomasz Napierala 	return (swap_reserve_by_cred(incr, curthread->td_ucred));
1953364c323SKonstantin Belousov }
1963364c323SKonstantin Belousov 
1973364c323SKonstantin Belousov int
198ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
1993364c323SKonstantin Belousov {
200e8bb589dSMatt Macy 	u_long r, s, prev, pincr;
2013364c323SKonstantin Belousov 	int res, error;
2023364c323SKonstantin Belousov 	static int curfail;
2033364c323SKonstantin Belousov 	static struct timeval lastfail;
204ef694c1aSEdward Tomasz Napierala 	struct uidinfo *uip;
205ef694c1aSEdward Tomasz Napierala 
206ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
2073364c323SKonstantin Belousov 
208e8bb589dSMatt Macy 	KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
209e8bb589dSMatt Macy 	    (uintmax_t)incr));
2103364c323SKonstantin Belousov 
211afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2124b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
2131ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2141ba5ad42SEdward Tomasz Napierala 		error = racct_add(curproc, RACCT_SWAP, incr);
2151ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2161ba5ad42SEdward Tomasz Napierala 		if (error != 0)
2171ba5ad42SEdward Tomasz Napierala 			return (0);
2184b5c9cf6SEdward Tomasz Napierala 	}
219afcc55f3SEdward Tomasz Napierala #endif
2201ba5ad42SEdward Tomasz Napierala 
221e8bb589dSMatt Macy 	pincr = atop(incr);
2223364c323SKonstantin Belousov 	res = 0;
223e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&swap_reserved, pincr);
224e8bb589dSMatt Macy 	r = prev + pincr;
2253364c323SKonstantin Belousov 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
226e958ad4cSJeff Roberson 		s = vm_cnt.v_page_count - vm_cnt.v_free_reserved -
227e958ad4cSJeff Roberson 		    vm_wire_count();
2283364c323SKonstantin Belousov 	} else
2293364c323SKonstantin Belousov 		s = 0;
2303364c323SKonstantin Belousov 	s += swap_total;
2313364c323SKonstantin Belousov 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
2323364c323SKonstantin Belousov 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
2333364c323SKonstantin Belousov 		res = 1;
234e8bb589dSMatt Macy 	} else {
235e8bb589dSMatt Macy 		prev = atomic_fetchadd_long(&swap_reserved, -pincr);
236e8bb589dSMatt Macy 		if (prev < pincr)
237e8bb589dSMatt Macy 			panic("swap_reserved < incr on overcommit fail");
2383364c323SKonstantin Belousov 	}
2393364c323SKonstantin Belousov 	if (res) {
240e8bb589dSMatt Macy 		prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr);
2415c0e1c11SKonstantin Belousov 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
242e8bb589dSMatt Macy 		    prev + pincr > lim_cur(curthread, RLIMIT_SWAP) &&
243e8bb589dSMatt Macy 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) {
2443364c323SKonstantin Belousov 			res = 0;
245e8bb589dSMatt Macy 			prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr);
246e8bb589dSMatt Macy 			if (prev < pincr)
247e8bb589dSMatt Macy 				panic("uip->ui_vmsize < incr on overcommit fail");
2483364c323SKonstantin Belousov 		}
2493364c323SKonstantin Belousov 	}
2503364c323SKonstantin Belousov 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
2513364c323SKonstantin Belousov 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
252134465d7SKonstantin Belousov 		    uip->ui_uid, curproc->p_pid, incr);
2533364c323SKonstantin Belousov 	}
2543364c323SKonstantin Belousov 
255afcc55f3SEdward Tomasz Napierala #ifdef RACCT
256e8bb589dSMatt Macy 	if (racct_enable && !res) {
2571ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2581ba5ad42SEdward Tomasz Napierala 		racct_sub(curproc, RACCT_SWAP, incr);
2591ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2601ba5ad42SEdward Tomasz Napierala 	}
261afcc55f3SEdward Tomasz Napierala #endif
2621ba5ad42SEdward Tomasz Napierala 
2633364c323SKonstantin Belousov 	return (res);
2643364c323SKonstantin Belousov }
2653364c323SKonstantin Belousov 
2663364c323SKonstantin Belousov void
2673364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr)
2683364c323SKonstantin Belousov {
2693364c323SKonstantin Belousov 	struct uidinfo *uip;
270e8bb589dSMatt Macy 	u_long pincr;
2713364c323SKonstantin Belousov 
272e8bb589dSMatt Macy 	KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
273e8bb589dSMatt Macy 	    (uintmax_t)incr));
2743364c323SKonstantin Belousov 
275e8bb589dSMatt Macy 	PROC_LOCK(curproc);
276afcc55f3SEdward Tomasz Napierala #ifdef RACCT
277e8bb589dSMatt Macy 	if (racct_enable)
2781ba5ad42SEdward Tomasz Napierala 		racct_add_force(curproc, RACCT_SWAP, incr);
279afcc55f3SEdward Tomasz Napierala #endif
280e8bb589dSMatt Macy 	pincr = atop(incr);
281e8bb589dSMatt Macy 	atomic_add_long(&swap_reserved, pincr);
282e8bb589dSMatt Macy 	uip = curproc->p_ucred->cr_ruidinfo;
283e8bb589dSMatt Macy 	atomic_add_long(&uip->ui_vmsize, pincr);
2843364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2853364c323SKonstantin Belousov }
2863364c323SKonstantin Belousov 
2873364c323SKonstantin Belousov void
2883364c323SKonstantin Belousov swap_release(vm_ooffset_t decr)
2893364c323SKonstantin Belousov {
290ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
2913364c323SKonstantin Belousov 
2923364c323SKonstantin Belousov 	PROC_LOCK(curproc);
293e8bb589dSMatt Macy 	cred = curproc->p_ucred;
294ef694c1aSEdward Tomasz Napierala 	swap_release_by_cred(decr, cred);
2953364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2963364c323SKonstantin Belousov }
2973364c323SKonstantin Belousov 
2983364c323SKonstantin Belousov void
299ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
3003364c323SKonstantin Belousov {
301e8bb589dSMatt Macy 	u_long prev, pdecr;
302ef694c1aSEdward Tomasz Napierala  	struct uidinfo *uip;
303ef694c1aSEdward Tomasz Napierala 
304ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
3053364c323SKonstantin Belousov 
306e8bb589dSMatt Macy 	KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", __func__,
307e8bb589dSMatt Macy 	    (uintmax_t)decr));
3083364c323SKonstantin Belousov 
309e8bb589dSMatt Macy 	pdecr = atop(decr);
310e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&swap_reserved, -pdecr);
311e8bb589dSMatt Macy 	if (prev < pdecr)
3123364c323SKonstantin Belousov 		panic("swap_reserved < decr");
3133364c323SKonstantin Belousov 
314e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr);
315e8bb589dSMatt Macy 	if (prev < pdecr)
3163364c323SKonstantin Belousov 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
317e8bb589dSMatt Macy #ifdef RACCT
318e8bb589dSMatt Macy 	if (racct_enable)
3191ba5ad42SEdward Tomasz Napierala 		racct_sub_cred(cred, RACCT_SWAP, decr);
320e8bb589dSMatt Macy #endif
3213364c323SKonstantin Belousov }
3223364c323SKonstantin Belousov 
3234abca9bbSAlan Cox #define SWM_POP		0x01	/* pop out			*/
32426f9a767SRodney W. Grimes 
325f08b3099SKonstantin Belousov static int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
3267dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
327756a5412SGleb Smirnoff static struct mtx swbuf_mtx;	/* to sync nsw_wcount_async */
328756a5412SGleb Smirnoff static int nsw_wcount_async;	/* limit async write buffers */
329327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
330327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
3311c7c3c6aSMatthew Dillon 
33289c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
3334c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
3344c36e917SKonstantin Belousov     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
3354c36e917SKonstantin Belousov     "Maximum running async swap ops");
336d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS);
337d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD |
338d027ed2eSAlan Cox     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A",
339d027ed2eSAlan Cox     "Swap Fragmentation Info");
34089c241d1SGleb Smirnoff 
3410cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
342327f4e83SMatthew Dillon 
3431c7c3c6aSMatthew Dillon /*
3441c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
3451c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
3461c7c3c6aSMatthew Dillon  */
3471c7c3c6aSMatthew Dillon 
3481c7c3c6aSMatthew Dillon #define NOBJLISTS		8
3491c7c3c6aSMatthew Dillon 
3501c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
351af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
3521c7c3c6aSMatthew Dillon 
3531c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
354756a5412SGleb Smirnoff static uma_zone_t swwbuf_zone;
355756a5412SGleb Smirnoff static uma_zone_t swrbuf_zone;
356f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone;
357f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone;
3581c7c3c6aSMatthew Dillon 
3591c7c3c6aSMatthew Dillon /*
3601c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
3611c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
3621c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
3631c7c3c6aSMatthew Dillon  */
364ff98689dSBruce Evans static vm_object_t
36511caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
3663364c323SKonstantin Belousov 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
36711caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
368b0cd2017SGleb Smirnoff static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
369b0cd2017SGleb Smirnoff     int *);
370b0cd2017SGleb Smirnoff static int	swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
371b0cd2017SGleb Smirnoff     int *, pgo_getpages_iodone_t, void *);
372751221fdSPoul-Henning Kamp static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
3735ea4972cSAlan Cox static boolean_t
3745ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
37511caded3SAlfred Perlstein static void	swap_pager_init(void);
37611caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
377b3fed13eSDavid Schultz static void	swap_pager_swapoff(struct swdevt *sp);
378f708ef1bSPoul-Henning Kamp 
379df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
380e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
381e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
382e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
383e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
38490effb23SGleb Smirnoff 	.pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)		*/
385e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
386e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
387e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
388df8bae1dSRodney W. Grimes };
389df8bae1dSRodney W. Grimes 
3901c7c3c6aSMatthew Dillon /*
3911c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
3921c7c3c6aSMatthew Dillon  * internal.
3931c7c3c6aSMatthew Dillon  */
394e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
395e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
39626f9a767SRodney W. Grimes 
39707c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
39807c348eaSAlan Cox     "Maximum size of a swap block in pages");
399cee313c4SRobert Watson 
400a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
40111caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
402230869e0SAlan Cox static bool	swp_pager_swblk_empty(struct swblk *sb, int start, int limit);
40388ad2d7bSKonstantin Belousov static int	swapongeom(struct vnode *);
40459efee01SPoul-Henning Kamp static int	swaponvp(struct thread *, struct vnode *, u_long);
40535918c55SChristian S.J. Peron static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
40624a1cce3SDavid Greenman 
4071c7c3c6aSMatthew Dillon /*
4081c7c3c6aSMatthew Dillon  * Swap bitmap functions
4091c7c3c6aSMatthew Dillon  */
410230869e0SAlan Cox static void	swp_pager_freeswapspace(daddr_t blk, daddr_t npages);
41148e98a2aSDoug Moore static daddr_t	swp_pager_getswapspace(int *npages, int limit);
4121c7c3c6aSMatthew Dillon 
4131c7c3c6aSMatthew Dillon /*
4141c7c3c6aSMatthew Dillon  * Metadata functions
4151c7c3c6aSMatthew Dillon  */
41678f1deefSAlan Cox static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
4172e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
41811caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
41911caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
4201c7c3c6aSMatthew Dillon 
42178f1deefSAlan Cox static void
42278f1deefSAlan Cox swp_pager_init_freerange(daddr_t *start, daddr_t *num)
42378f1deefSAlan Cox {
42478f1deefSAlan Cox 
42578f1deefSAlan Cox 	*start = SWAPBLK_NONE;
42678f1deefSAlan Cox 	*num = 0;
42778f1deefSAlan Cox }
42878f1deefSAlan Cox 
42978f1deefSAlan Cox static void
43078f1deefSAlan Cox swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr)
43178f1deefSAlan Cox {
43278f1deefSAlan Cox 
43378f1deefSAlan Cox 	if (*start + *num == addr) {
43478f1deefSAlan Cox 		(*num)++;
43578f1deefSAlan Cox 	} else {
43678f1deefSAlan Cox 		swp_pager_freeswapspace(*start, *num);
43778f1deefSAlan Cox 		*start = addr;
43878f1deefSAlan Cox 		*num = 1;
43978f1deefSAlan Cox 	}
44078f1deefSAlan Cox }
44178f1deefSAlan Cox 
442f425ab8eSKonstantin Belousov static void *
443f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree)
444f425ab8eSKonstantin Belousov {
445f425ab8eSKonstantin Belousov 
446f425ab8eSKonstantin Belousov 	return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ?
447f425ab8eSKonstantin Belousov 	    M_USE_RESERVE : 0)));
448f425ab8eSKonstantin Belousov }
449f425ab8eSKonstantin Belousov 
450f425ab8eSKonstantin Belousov static void
451f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node)
452f425ab8eSKonstantin Belousov {
453f425ab8eSKonstantin Belousov 
454f425ab8eSKonstantin Belousov 	uma_zfree(swpctrie_zone, node);
455f425ab8eSKonstantin Belousov }
456f425ab8eSKonstantin Belousov 
457f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free);
458f425ab8eSKonstantin Belousov 
4591c7c3c6aSMatthew Dillon /*
4601c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
4611c7c3c6aSMatthew Dillon  *
46220d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
46320d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
46420d3034fSMatthew Dillon  *
46520d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
4661c7c3c6aSMatthew Dillon  *
4671c7c3c6aSMatthew Dillon  *	No restrictions on call
4681c7c3c6aSMatthew Dillon  *	This routine may not block.
4691c7c3c6aSMatthew Dillon  */
470a5edd34aSPoul-Henning Kamp static void
4712f249180SPoul-Henning Kamp swp_sizecheck(void)
4720d94caffSDavid Greenman {
47323955314SAlfred Perlstein 
4748f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
47520d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
4761af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
47720d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
4782b0d37a4SMatthew Dillon 		}
47920d3034fSMatthew Dillon 	} else {
48026f9a767SRodney W. Grimes 		swap_pager_full = 0;
4818f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
48220d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
48326f9a767SRodney W. Grimes 	}
4841c7c3c6aSMatthew Dillon }
4851c7c3c6aSMatthew Dillon 
4861c7c3c6aSMatthew Dillon /*
4871c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
4881c7c3c6aSMatthew Dillon  *
4891c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
4901c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
4911c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
4921c7c3c6aSMatthew Dillon  */
493f5a12711SPoul-Henning Kamp static void
4942f249180SPoul-Henning Kamp swap_pager_init(void)
495df8bae1dSRodney W. Grimes {
4961c7c3c6aSMatthew Dillon 	/*
4971c7c3c6aSMatthew Dillon 	 * Initialize object lists
4981c7c3c6aSMatthew Dillon 	 */
4991c7c3c6aSMatthew Dillon 	int i;
5001c7c3c6aSMatthew Dillon 
5011c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
5021c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
50320da9c2eSPoul-Henning Kamp 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
50415719273SKonstantin Belousov 	sx_init(&sw_alloc_sx, "swspsx");
50504533e1eSKonstantin Belousov 	sx_init(&swdev_syscall_lock, "swsysc");
5061c7c3c6aSMatthew Dillon }
50726f9a767SRodney W. Grimes 
508df8bae1dSRodney W. Grimes /*
5091c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
5101c7c3c6aSMatthew Dillon  *
5111c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
5121c7c3c6aSMatthew Dillon  *	its main loop.
513df8bae1dSRodney W. Grimes  */
51424a1cce3SDavid Greenman void
5152f249180SPoul-Henning Kamp swap_pager_swap_init(void)
516df8bae1dSRodney W. Grimes {
51761203277SDag-Erling Smørgrav 	unsigned long n, n2;
5180d94caffSDavid Greenman 
51926f9a767SRodney W. Grimes 	/*
5201c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
5211c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
5221c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
5231c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
5241c7c3c6aSMatthew Dillon 	 *
525327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
5261c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
5271c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
5281c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
529327f4e83SMatthew Dillon 	 *
530327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
531327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
532327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
533327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
534327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
535327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
536327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
537327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
538327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
539327f4e83SMatthew Dillon 	 * So it all works out pretty well.
54026f9a767SRodney W. Grimes 	 */
541ad3cce20SMatthew Dillon 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
542327f4e83SMatthew Dillon 
543327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
544327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
545756a5412SGleb Smirnoff 	mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF);
546756a5412SGleb Smirnoff 
547756a5412SGleb Smirnoff 	swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4);
548756a5412SGleb Smirnoff 	swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2);
54924a1cce3SDavid Greenman 
5501c7c3c6aSMatthew Dillon 	/*
551a8233027SKonstantin Belousov 	 * Initialize our zone, taking the user's requested size or
552a8233027SKonstantin Belousov 	 * estimating the number we need based on the number of pages
553a8233027SKonstantin Belousov 	 * in the system.
5541c7c3c6aSMatthew Dillon 	 */
555a8233027SKonstantin Belousov 	n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) :
556a8233027SKonstantin Belousov 	    vm_cnt.v_page_count / 2;
557f425ab8eSKonstantin Belousov 	swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL,
558f5fbe90dSAlan Cox 	    pctrie_zone_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM);
559f425ab8eSKonstantin Belousov 	if (swpctrie_zone == NULL)
560f425ab8eSKonstantin Belousov 		panic("failed to create swap pctrie zone.");
561f425ab8eSKonstantin Belousov 	swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL,
562f5fbe90dSAlan Cox 	    NULL, NULL, _Alignof(struct swblk) - 1, UMA_ZONE_VM);
563f425ab8eSKonstantin Belousov 	if (swblk_zone == NULL)
564f425ab8eSKonstantin Belousov 		panic("failed to create swap blk zone.");
56522a5e6b9SDag-Erling Smørgrav 	n2 = n;
5668355f576SJeff Roberson 	do {
567f425ab8eSKonstantin Belousov 		if (uma_zone_reserve_kva(swblk_zone, n))
56861ce6eeeSAlfred Perlstein 			break;
56961ce6eeeSAlfred Perlstein 		/*
57061ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
57161ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
57261ce6eeeSAlfred Perlstein 		 */
57361ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
57461ce6eeeSAlfred Perlstein 	} while (n > 0);
57553faf5a7SKonstantin Belousov 
57653faf5a7SKonstantin Belousov 	/*
57753faf5a7SKonstantin Belousov 	 * Often uma_zone_reserve_kva() cannot reserve exactly the
57853faf5a7SKonstantin Belousov 	 * requested size.  Account for the difference when
57953faf5a7SKonstantin Belousov 	 * calculating swap_maxpages.
58053faf5a7SKonstantin Belousov 	 */
58153faf5a7SKonstantin Belousov 	n = uma_zone_get_max(swblk_zone);
58253faf5a7SKonstantin Belousov 
5831fffcd75SKonstantin Belousov 	if (n < n2)
584a8233027SKonstantin Belousov 		printf("Swap blk zone entries changed from %lu to %lu.\n",
585f425ab8eSKonstantin Belousov 		    n2, n);
58661203277SDag-Erling Smørgrav 	swap_maxpages = n * SWAP_META_PAGES;
587f425ab8eSKonstantin Belousov 	swzone = n * sizeof(struct swblk);
588f425ab8eSKonstantin Belousov 	if (!uma_zone_reserve_kva(swpctrie_zone, n))
589f425ab8eSKonstantin Belousov 		printf("Cannot reserve swap pctrie zone, "
590f425ab8eSKonstantin Belousov 		    "reduce kern.maxswzone.\n");
59124a1cce3SDavid Greenman }
59224a1cce3SDavid Greenman 
593eb4d6a1bSKonstantin Belousov static vm_object_t
594eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size,
595eb4d6a1bSKonstantin Belousov     vm_ooffset_t offset)
596eb4d6a1bSKonstantin Belousov {
597eb4d6a1bSKonstantin Belousov 	vm_object_t object;
598eb4d6a1bSKonstantin Belousov 
599eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
600eb4d6a1bSKonstantin Belousov 		if (!swap_reserve_by_cred(size, cred))
601eb4d6a1bSKonstantin Belousov 			return (NULL);
602eb4d6a1bSKonstantin Belousov 		crhold(cred);
603eb4d6a1bSKonstantin Belousov 	}
604f425ab8eSKonstantin Belousov 
605f425ab8eSKonstantin Belousov 	/*
606f425ab8eSKonstantin Belousov 	 * The un_pager.swp.swp_blks trie is initialized by
607f425ab8eSKonstantin Belousov 	 * vm_object_allocate() to ensure the correct order of
608f425ab8eSKonstantin Belousov 	 * visibility to other threads.
609f425ab8eSKonstantin Belousov 	 */
610eb4d6a1bSKonstantin Belousov 	object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset +
611eb4d6a1bSKonstantin Belousov 	    PAGE_MASK + size));
612f425ab8eSKonstantin Belousov 
613eb4d6a1bSKonstantin Belousov 	object->handle = handle;
614eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
615eb4d6a1bSKonstantin Belousov 		object->cred = cred;
616eb4d6a1bSKonstantin Belousov 		object->charge = size;
617eb4d6a1bSKonstantin Belousov 	}
618eb4d6a1bSKonstantin Belousov 	return (object);
619eb4d6a1bSKonstantin Belousov }
620eb4d6a1bSKonstantin Belousov 
62124a1cce3SDavid Greenman /*
6221c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
6231c7c3c6aSMatthew Dillon  *			its metadata structures.
6241c7c3c6aSMatthew Dillon  *
6251c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
626eb4d6a1bSKonstantin Belousov  *	OBJT_SWAP object.
6271c7c3c6aSMatthew Dillon  *
628eb4d6a1bSKonstantin Belousov  *	This routine must ensure that no live duplicate is created for
629eb4d6a1bSKonstantin Belousov  *	the named object request, which is protected against by
630eb4d6a1bSKonstantin Belousov  *	holding the sw_alloc_sx lock in case handle != NULL.
63124a1cce3SDavid Greenman  */
632f5a12711SPoul-Henning Kamp static vm_object_t
6336cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
6343364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
63524a1cce3SDavid Greenman {
63624a1cce3SDavid Greenman 	vm_object_t object;
6372f7af3dbSAlan Cox 
638eb4d6a1bSKonstantin Belousov 	if (handle != NULL) {
6391c7c3c6aSMatthew Dillon 		/*
6401c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
6411c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
6421c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
6431c7c3c6aSMatthew Dillon 		 * of the handle.
6441c7c3c6aSMatthew Dillon 		 */
6450cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
6461c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
647b5e8f167SAlan Cox 		if (object == NULL) {
648eb4d6a1bSKonstantin Belousov 			object = swap_pager_alloc_init(handle, cred, size,
649eb4d6a1bSKonstantin Belousov 			    offset);
650eb4d6a1bSKonstantin Belousov 			if (object != NULL) {
651eb4d6a1bSKonstantin Belousov 				TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
652eb4d6a1bSKonstantin Belousov 				    object, pager_object_list);
6533364c323SKonstantin Belousov 			}
65424a1cce3SDavid Greenman 		}
6550cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
65624a1cce3SDavid Greenman 	} else {
657eb4d6a1bSKonstantin Belousov 		object = swap_pager_alloc_init(handle, cred, size, offset);
65824a1cce3SDavid Greenman 	}
65924a1cce3SDavid Greenman 	return (object);
660df8bae1dSRodney W. Grimes }
661df8bae1dSRodney W. Grimes 
66226f9a767SRodney W. Grimes /*
6631c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
6641c7c3c6aSMatthew Dillon  *
6651c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
6661c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
6671c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
6681c7c3c6aSMatthew Dillon  *	about to be destroyed.
6691c7c3c6aSMatthew Dillon  *
67015523cf7SKonstantin Belousov  *	The object must be locked.
67126f9a767SRodney W. Grimes  */
672df8bae1dSRodney W. Grimes static void
6732f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object)
67426f9a767SRodney W. Grimes {
6754dcc5c2dSMatthew Dillon 
676eb4d6a1bSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
677eb4d6a1bSKonstantin Belousov 	KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
678eb4d6a1bSKonstantin Belousov 
67926f9a767SRodney W. Grimes 	/*
6801c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
6811c7c3c6aSMatthew Dillon 	 * pageout completion.
68226f9a767SRodney W. Grimes 	 */
683bd228075SAlan Cox 	if (object->handle != NULL) {
684eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
685eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
686eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(object->handle), object,
687eb4d6a1bSKonstantin Belousov 		    pager_object_list);
688eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
689eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(object);
690bd228075SAlan Cox 	}
6911c7c3c6aSMatthew Dillon 
6921c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
6931c7c3c6aSMatthew Dillon 
6941c7c3c6aSMatthew Dillon 	/*
6951c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
6961c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
6971c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
6981c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
6991c7c3c6aSMatthew Dillon 	 */
7001c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
701e735691bSJohn Baldwin 	object->handle = NULL;
702e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
7031c7c3c6aSMatthew Dillon }
7041c7c3c6aSMatthew Dillon 
7051c7c3c6aSMatthew Dillon /************************************************************************
7061c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
7071c7c3c6aSMatthew Dillon  ************************************************************************/
7081c7c3c6aSMatthew Dillon 
7091c7c3c6aSMatthew Dillon /*
7101c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
7111c7c3c6aSMatthew Dillon  *
71248e98a2aSDoug Moore  *	Allocate swap for up to the requested number of pages, and at
71348e98a2aSDoug Moore  *	least a minimum number of pages.  The starting swap block number
71448e98a2aSDoug Moore  *	(a page index) is returned or SWAPBLK_NONE if the allocation
71548e98a2aSDoug Moore  *	failed.
7161c7c3c6aSMatthew Dillon  *
7171c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
7181c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
7191c7c3c6aSMatthew Dillon  *
72015523cf7SKonstantin Belousov  *	This routine may not sleep.
7218f60c087SPoul-Henning Kamp  *
7228f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
7231c7c3c6aSMatthew Dillon  */
724a5edd34aSPoul-Henning Kamp static daddr_t
72548e98a2aSDoug Moore swp_pager_getswapspace(int *io_npages, int limit)
7261c7c3c6aSMatthew Dillon {
7271c7c3c6aSMatthew Dillon 	daddr_t blk;
7288f60c087SPoul-Henning Kamp 	struct swdevt *sp;
72987ae0686SDoug Moore 	int mpages, npages;
7301c7c3c6aSMatthew Dillon 
7318f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
73287ae0686SDoug Moore 	npages = mpages = *io_npages;
73320da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7348f60c087SPoul-Henning Kamp 	sp = swdevhd;
73548e98a2aSDoug Moore 	while (!TAILQ_EMPTY(&swtailq)) {
7368f60c087SPoul-Henning Kamp 		if (sp == NULL)
7378f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
73848e98a2aSDoug Moore 		if ((sp->sw_flags & SW_CLOSING) == 0)
73987ae0686SDoug Moore 			blk = blist_alloc(sp->sw_blist, &npages, mpages);
74048e98a2aSDoug Moore 		if (blk != SWAPBLK_NONE)
74148e98a2aSDoug Moore 			break;
74248e98a2aSDoug Moore 		sp = TAILQ_NEXT(sp, sw_list);
74348e98a2aSDoug Moore 		if (swdevhd == sp) {
74448e98a2aSDoug Moore 			if (npages <= limit)
74548e98a2aSDoug Moore 				break;
74687ae0686SDoug Moore 			mpages = npages - 1;
74748e98a2aSDoug Moore 			npages >>= 1;
74848e98a2aSDoug Moore 		}
74948e98a2aSDoug Moore 	}
7508f60c087SPoul-Henning Kamp 	if (blk != SWAPBLK_NONE) {
75148e98a2aSDoug Moore 		*io_npages = npages;
7528f60c087SPoul-Henning Kamp 		blk += sp->sw_first;
7538f60c087SPoul-Henning Kamp 		sp->sw_used += npages;
754d05bc129SAlan Cox 		swap_pager_avail -= npages;
7558f60c087SPoul-Henning Kamp 		swp_sizecheck();
7568f60c087SPoul-Henning Kamp 		swdevhd = TAILQ_NEXT(sp, sw_list);
75748e98a2aSDoug Moore 	} else {
7582b0d37a4SMatthew Dillon 		if (swap_pager_full != 2) {
75948e98a2aSDoug Moore 			printf("swp_pager_getswapspace(%d): failed\n",
76048e98a2aSDoug Moore 			    *io_npages);
7612b0d37a4SMatthew Dillon 			swap_pager_full = 2;
76220d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
7632b0d37a4SMatthew Dillon 		}
7648f60c087SPoul-Henning Kamp 		swdevhd = NULL;
76548e98a2aSDoug Moore 	}
766d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
7671c7c3c6aSMatthew Dillon 	return (blk);
76826f9a767SRodney W. Grimes }
76926f9a767SRodney W. Grimes 
770541a1175SAlan Cox static bool
771b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp)
7728f60c087SPoul-Henning Kamp {
7738f60c087SPoul-Henning Kamp 
774b3fed13eSDavid Schultz 	return (blk >= sp->sw_first && blk < sp->sw_end);
7758f60c087SPoul-Henning Kamp }
7768f60c087SPoul-Henning Kamp 
7774b03903aSPoul-Henning Kamp static void
7784b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp)
7794b03903aSPoul-Henning Kamp {
7804b03903aSPoul-Henning Kamp 	struct swdevt *sp;
7814b03903aSPoul-Henning Kamp 
78220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7834b03903aSPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
784541a1175SAlan Cox 		if (swp_pager_isondev(bp->b_blkno, sp)) {
78520da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
7862cc718a1SKonstantin Belousov 			if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
7872cc718a1SKonstantin Belousov 			    unmapped_buf_allowed) {
7882cc718a1SKonstantin Belousov 				bp->b_data = unmapped_buf;
7892cc718a1SKonstantin Belousov 				bp->b_offset = 0;
7902cc718a1SKonstantin Belousov 			} else {
7912cc718a1SKonstantin Belousov 				pmap_qenter((vm_offset_t)bp->b_data,
7922cc718a1SKonstantin Belousov 				    &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
7932cc718a1SKonstantin Belousov 			}
7944b03903aSPoul-Henning Kamp 			sp->sw_strategy(bp, sp);
7954b03903aSPoul-Henning Kamp 			return;
7964b03903aSPoul-Henning Kamp 		}
7974b03903aSPoul-Henning Kamp 	}
79820da9c2eSPoul-Henning Kamp 	panic("Swapdev not found");
7994b03903aSPoul-Henning Kamp }
8004b03903aSPoul-Henning Kamp 
8018f60c087SPoul-Henning Kamp 
80226f9a767SRodney W. Grimes /*
8031c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
8041c7c3c6aSMatthew Dillon  *
8051c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
8061c7c3c6aSMatthew Dillon  *
80715523cf7SKonstantin Belousov  *	This routine may not sleep.
80826f9a767SRodney W. Grimes  */
809a5edd34aSPoul-Henning Kamp static void
810230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages)
8110d94caffSDavid Greenman {
8128f60c087SPoul-Henning Kamp 	struct swdevt *sp;
81392da00bbSMatthew Dillon 
814230869e0SAlan Cox 	if (npages == 0)
815230869e0SAlan Cox 		return;
8167645e885SAlan Cox 	mtx_lock(&sw_dev_mtx);
8177645e885SAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
818541a1175SAlan Cox 		if (swp_pager_isondev(blk, sp)) {
81992da00bbSMatthew Dillon 			sp->sw_used -= npages;
82092da00bbSMatthew Dillon 			/*
8217645e885SAlan Cox 			 * If we are attempting to stop swapping on
8227645e885SAlan Cox 			 * this device, we don't want to mark any
8237645e885SAlan Cox 			 * blocks free lest they be reused.
82492da00bbSMatthew Dillon 			 */
8257645e885SAlan Cox 			if ((sp->sw_flags & SW_CLOSING) == 0) {
8267645e885SAlan Cox 				blist_free(sp->sw_blist, blk - sp->sw_first,
8277645e885SAlan Cox 				    npages);
8288f60c087SPoul-Henning Kamp 				swap_pager_avail += npages;
8291c7c3c6aSMatthew Dillon 				swp_sizecheck();
83026f9a767SRodney W. Grimes 			}
8317645e885SAlan Cox 			mtx_unlock(&sw_dev_mtx);
8327645e885SAlan Cox 			return;
8337645e885SAlan Cox 		}
8347645e885SAlan Cox 	}
8357645e885SAlan Cox 	panic("Swapdev not found");
8367645e885SAlan Cox }
8371c7c3c6aSMatthew Dillon 
83826f9a767SRodney W. Grimes /*
839d027ed2eSAlan Cox  * SYSCTL_SWAP_FRAGMENTATION() -	produce raw swap space stats
840d027ed2eSAlan Cox  */
841d027ed2eSAlan Cox static int
842d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS)
843d027ed2eSAlan Cox {
844d027ed2eSAlan Cox 	struct sbuf sbuf;
845d027ed2eSAlan Cox 	struct swdevt *sp;
846d027ed2eSAlan Cox 	const char *devname;
847d027ed2eSAlan Cox 	int error;
848d027ed2eSAlan Cox 
849d027ed2eSAlan Cox 	error = sysctl_wire_old_buffer(req, 0);
850d027ed2eSAlan Cox 	if (error != 0)
851d027ed2eSAlan Cox 		return (error);
852d027ed2eSAlan Cox 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
853d027ed2eSAlan Cox 	mtx_lock(&sw_dev_mtx);
854d027ed2eSAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
855d027ed2eSAlan Cox 		if (vn_isdisk(sp->sw_vp, NULL))
856d027ed2eSAlan Cox 			devname = devtoname(sp->sw_vp->v_rdev);
857d027ed2eSAlan Cox 		else
858d027ed2eSAlan Cox 			devname = "[file]";
859d027ed2eSAlan Cox 		sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname);
860d027ed2eSAlan Cox 		blist_stats(sp->sw_blist, &sbuf);
861d027ed2eSAlan Cox 	}
862d027ed2eSAlan Cox 	mtx_unlock(&sw_dev_mtx);
863d027ed2eSAlan Cox 	error = sbuf_finish(&sbuf);
864d027ed2eSAlan Cox 	sbuf_delete(&sbuf);
865d027ed2eSAlan Cox 	return (error);
866d027ed2eSAlan Cox }
867d027ed2eSAlan Cox 
868d027ed2eSAlan Cox /*
8691c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
8701c7c3c6aSMatthew Dillon  *				range within an object.
8711c7c3c6aSMatthew Dillon  *
8721c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
8731c7c3c6aSMatthew Dillon  *
8741c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
8751c7c3c6aSMatthew Dillon  *
8761c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
8771c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
8781c7c3c6aSMatthew Dillon  *	we should be ok.
879c25673ffSAttilio Rao  *
880c25673ffSAttilio Rao  *	The object must be locked.
88126f9a767SRodney W. Grimes  */
88226f9a767SRodney W. Grimes void
8832f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
88426f9a767SRodney W. Grimes {
88523955314SAlfred Perlstein 
8861c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
8874dcc5c2dSMatthew Dillon }
8884dcc5c2dSMatthew Dillon 
8894dcc5c2dSMatthew Dillon /*
8904dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
8914dcc5c2dSMatthew Dillon  *
8924dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
89356ce850bSKonstantin Belousov  *	swap blocks are not zeroed.  Any previous swap assignment is destroyed.
8944dcc5c2dSMatthew Dillon  *
8954dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
8964dcc5c2dSMatthew Dillon  */
8974dcc5c2dSMatthew Dillon int
8984dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
8994dcc5c2dSMatthew Dillon {
90048e98a2aSDoug Moore 	daddr_t addr, blk, n_free, s_free;
90148e98a2aSDoug Moore 	int i, j, n;
9024dcc5c2dSMatthew Dillon 
90378f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
90489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
90548e98a2aSDoug Moore 	for (i = 0; i < size; i += n) {
90648e98a2aSDoug Moore 		n = min(BLIST_MAX_ALLOC, size - i);
90748e98a2aSDoug Moore 		blk = swp_pager_getswapspace(&n, 1);
90848e98a2aSDoug Moore 		if (blk == SWAPBLK_NONE) {
90948e98a2aSDoug Moore 			swp_pager_meta_free(object, start, i);
91089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
9114dcc5c2dSMatthew Dillon 			return (-1);
9124dcc5c2dSMatthew Dillon 		}
91348e98a2aSDoug Moore 		for (j = 0; j < n; ++j) {
91448e98a2aSDoug Moore 			addr = swp_pager_meta_build(object,
91548e98a2aSDoug Moore 			    start + i + j, blk + j);
91678f1deefSAlan Cox 			if (addr != SWAPBLK_NONE)
91748e98a2aSDoug Moore 				swp_pager_update_freerange(&s_free, &n_free,
91848e98a2aSDoug Moore 				    addr);
91948e98a2aSDoug Moore 		}
9204dcc5c2dSMatthew Dillon 	}
92178f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
92289f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
9234dcc5c2dSMatthew Dillon 	return (0);
92426f9a767SRodney W. Grimes }
92526f9a767SRodney W. Grimes 
9260a47b48bSJohn Dyson /*
9271c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
9281c7c3c6aSMatthew Dillon  *			and destroy the source.
9291c7c3c6aSMatthew Dillon  *
9301c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
9311c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
9321c7c3c6aSMatthew Dillon  *	we keep the destination's.
9331c7c3c6aSMatthew Dillon  *
93415523cf7SKonstantin Belousov  *	This routine is allowed to sleep.  It may sleep allocating metadata
9351c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
9361c7c3c6aSMatthew Dillon  *	progress on the source.
9371c7c3c6aSMatthew Dillon  *
9381c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
9391c7c3c6aSMatthew Dillon  *
9401c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
9411c7c3c6aSMatthew Dillon  *
94215523cf7SKonstantin Belousov  *	The source and destination objects must be locked.
94315523cf7SKonstantin Belousov  *	Both object locks may temporarily be released.
94426f9a767SRodney W. Grimes  */
94526f9a767SRodney W. Grimes void
9462f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
9472f249180SPoul-Henning Kamp     vm_pindex_t offset, int destroysource)
94826f9a767SRodney W. Grimes {
949a316d390SJohn Dyson 	vm_pindex_t i;
95078f1deefSAlan Cox 	daddr_t dstaddr, n_free, s_free, srcaddr;
9514dcc5c2dSMatthew Dillon 
95289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(srcobject);
95389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(dstobject);
9540cddd8f0SMatthew Dillon 
95526f9a767SRodney W. Grimes 	/*
9561c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
9571c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
95826f9a767SRodney W. Grimes 	 */
959eb4d6a1bSKonstantin Belousov 	if (destroysource && srcobject->handle != NULL) {
960eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(srcobject, 1);
961eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(srcobject);
962eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(dstobject, 1);
963eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(dstobject);
964eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
965eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
966eb4d6a1bSKonstantin Belousov 		    pager_object_list);
967eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
968eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(dstobject);
969eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(dstobject);
970eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(srcobject);
971eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(srcobject);
972bd228075SAlan Cox 	}
97326f9a767SRodney W. Grimes 
9741c7c3c6aSMatthew Dillon 	/*
9754abca9bbSAlan Cox 	 * Transfer source to destination.
9761c7c3c6aSMatthew Dillon 	 */
97778f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
9781c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
9794abca9bbSAlan Cox 		srcaddr = swp_pager_meta_ctl(srcobject, i + offset, SWM_POP);
9804abca9bbSAlan Cox 		if (srcaddr == SWAPBLK_NONE)
9814abca9bbSAlan Cox 			continue;
9821c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
98378f1deefSAlan Cox 		if (dstaddr != SWAPBLK_NONE) {
98478f1deefSAlan Cox 			/*
98578f1deefSAlan Cox 			 * Destination has valid swapblk or it is represented
98678f1deefSAlan Cox 			 * by a resident page.  We destroy the source block.
98778f1deefSAlan Cox 			 */
98878f1deefSAlan Cox 			swp_pager_update_freerange(&s_free, &n_free, srcaddr);
98978f1deefSAlan Cox 			continue;
99078f1deefSAlan Cox 		}
99178f1deefSAlan Cox 
9921c7c3c6aSMatthew Dillon 		/*
9931c7c3c6aSMatthew Dillon 		 * Destination has no swapblk and is not resident,
9941c7c3c6aSMatthew Dillon 		 * copy source.
9954abca9bbSAlan Cox 		 *
996c7c8dd7eSAlan Cox 		 * swp_pager_meta_build() can sleep.
997c7c8dd7eSAlan Cox 		 */
998c7c8dd7eSAlan Cox 		vm_object_pip_add(srcobject, 1);
99989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(srcobject);
1000c7c8dd7eSAlan Cox 		vm_object_pip_add(dstobject, 1);
100178f1deefSAlan Cox 		dstaddr = swp_pager_meta_build(dstobject, i, srcaddr);
100278f1deefSAlan Cox 		KASSERT(dstaddr == SWAPBLK_NONE,
100378f1deefSAlan Cox 		    ("Unexpected destination swapblk"));
1004c7c8dd7eSAlan Cox 		vm_object_pip_wakeup(dstobject);
100589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(srcobject);
1006c7c8dd7eSAlan Cox 		vm_object_pip_wakeup(srcobject);
10071c7c3c6aSMatthew Dillon 	}
100878f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
100926f9a767SRodney W. Grimes 
101026f9a767SRodney W. Grimes 	/*
10111c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
10121c7c3c6aSMatthew Dillon 	 *
1013763df3ecSPedro F. Giffuni 	 * We have to revert the type to OBJT_DEFAULT so we do not accidentally
10141c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
101526f9a767SRodney W. Grimes 	 */
1016c0877f10SJohn Dyson 	if (destroysource) {
10171c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
10181c7c3c6aSMatthew Dillon 		/*
10191c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
10201c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
1021956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
10221c7c3c6aSMatthew Dillon 		 * queues.
10231c7c3c6aSMatthew Dillon 		 */
10241c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
1025c0877f10SJohn Dyson 	}
102626f9a767SRodney W. Grimes }
102726f9a767SRodney W. Grimes 
1028df8bae1dSRodney W. Grimes /*
10291c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
10301c7c3c6aSMatthew Dillon  *				the requested page.
10311c7c3c6aSMatthew Dillon  *
10321c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
10331c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
10341c7c3c6aSMatthew Dillon  *
10351c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
1036915d1b71SMark Johnston  *	store exists before and after the requested page.
1037df8bae1dSRodney W. Grimes  */
10385ea4972cSAlan Cox static boolean_t
1039915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
1040915d1b71SMark Johnston     int *after)
104126f9a767SRodney W. Grimes {
1042915d1b71SMark Johnston 	daddr_t blk, blk0;
1043915d1b71SMark Johnston 	int i;
104426f9a767SRodney W. Grimes 
1045c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
1046915d1b71SMark Johnston 
10471c7c3c6aSMatthew Dillon 	/*
10481c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
10491c7c3c6aSMatthew Dillon 	 */
10501c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
10514dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
10521c7c3c6aSMatthew Dillon 		if (before)
105324a1cce3SDavid Greenman 			*before = 0;
10541c7c3c6aSMatthew Dillon 		if (after)
105524a1cce3SDavid Greenman 			*after = 0;
105626f9a767SRodney W. Grimes 		return (FALSE);
105726f9a767SRodney W. Grimes 	}
105826f9a767SRodney W. Grimes 
105926f9a767SRodney W. Grimes 	/*
10601c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
1061e47ed70bSJohn Dyson 	 */
10621c7c3c6aSMatthew Dillon 	if (before != NULL) {
1063915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10641c7c3c6aSMatthew Dillon 			if (i > pindex)
10651c7c3c6aSMatthew Dillon 				break;
10661c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
10671c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
10681c7c3c6aSMatthew Dillon 				break;
1069ffc82b0aSJohn Dyson 		}
1070915d1b71SMark Johnston 		*before = i - 1;
107126f9a767SRodney W. Grimes 	}
107226f9a767SRodney W. Grimes 
107326f9a767SRodney W. Grimes 	/*
10741c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
107526f9a767SRodney W. Grimes 	 */
10761c7c3c6aSMatthew Dillon 	if (after != NULL) {
1077915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10781c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
10791c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
10801c7c3c6aSMatthew Dillon 				break;
108126f9a767SRodney W. Grimes 		}
1082915d1b71SMark Johnston 		*after = i - 1;
10831c7c3c6aSMatthew Dillon 	}
10841c7c3c6aSMatthew Dillon 	return (TRUE);
10851c7c3c6aSMatthew Dillon }
10861c7c3c6aSMatthew Dillon 
10871c7c3c6aSMatthew Dillon /*
10881c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
10891c7c3c6aSMatthew Dillon  *
10901c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
10911c7c3c6aSMatthew Dillon  *	not, from the page.
10921c7c3c6aSMatthew Dillon  *
10931c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
10941c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
10951c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
10961c7c3c6aSMatthew Dillon  *
10971c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
10981c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
10991c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
11001c7c3c6aSMatthew Dillon  *	depends on it.
11011c7c3c6aSMatthew Dillon  *
110215523cf7SKonstantin Belousov  *	This routine may not sleep.
1103c25673ffSAttilio Rao  *
1104c25673ffSAttilio Rao  *	The object containing the page must be locked.
11051c7c3c6aSMatthew Dillon  */
11061c7c3c6aSMatthew Dillon static void
11072f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m)
11081c7c3c6aSMatthew Dillon {
11094abca9bbSAlan Cox 	daddr_t srcaddr;
11102f249180SPoul-Henning Kamp 
11114abca9bbSAlan Cox 	srcaddr = swp_pager_meta_ctl(m->object, m->pindex, SWM_POP);
11124abca9bbSAlan Cox 	if (srcaddr != SWAPBLK_NONE)
11134abca9bbSAlan Cox 		swp_pager_freeswapspace(srcaddr, 1);
11141c7c3c6aSMatthew Dillon }
11151c7c3c6aSMatthew Dillon 
11161c7c3c6aSMatthew Dillon /*
1117915d1b71SMark Johnston  * swap_pager_getpages() - bring pages in from swap
11181c7c3c6aSMatthew Dillon  *
11193f060b60SMark Johnston  *	Attempt to page in the pages in array "ma" of length "count".  The
11203f060b60SMark Johnston  *	caller may optionally specify that additional pages preceding and
11213f060b60SMark Johnston  *	succeeding the specified range be paged in.  The number of such pages
11223f060b60SMark Johnston  *	is returned in the "rbehind" and "rahead" parameters, and they will
11233f060b60SMark Johnston  *	be in the inactive queue upon return.
11241c7c3c6aSMatthew Dillon  *
11253f060b60SMark Johnston  *	The pages in "ma" must be busied and will remain busied upon return.
11261c7c3c6aSMatthew Dillon  */
1127f708ef1bSPoul-Henning Kamp static int
11283f060b60SMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
1129b0cd2017SGleb Smirnoff     int *rahead)
1130df8bae1dSRodney W. Grimes {
11311c7c3c6aSMatthew Dillon 	struct buf *bp;
1132b7b8a096SKonstantin Belousov 	vm_page_t bm, mpred, msucc, p;
1133915d1b71SMark Johnston 	vm_pindex_t pindex;
11341c7c3c6aSMatthew Dillon 	daddr_t blk;
1135b7b8a096SKonstantin Belousov 	int i, maxahead, maxbehind, reqcount;
11360d94caffSDavid Greenman 
1137915d1b71SMark Johnston 	reqcount = count;
11381c7c3c6aSMatthew Dillon 
1139b7b8a096SKonstantin Belousov 	/*
1140b7b8a096SKonstantin Belousov 	 * Determine the final number of read-behind pages and
1141b7b8a096SKonstantin Belousov 	 * allocate them BEFORE releasing the object lock.  Otherwise,
1142b7b8a096SKonstantin Belousov 	 * there can be a problematic race with vm_object_split().
1143b7b8a096SKonstantin Belousov 	 * Specifically, vm_object_split() might first transfer pages
1144b7b8a096SKonstantin Belousov 	 * that precede ma[0] in the current object to a new object,
1145b7b8a096SKonstantin Belousov 	 * and then this function incorrectly recreates those pages as
1146b7b8a096SKonstantin Belousov 	 * read-behind pages in the current object.
1147b7b8a096SKonstantin Belousov 	 */
1148b7b8a096SKonstantin Belousov 	if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead))
1149915d1b71SMark Johnston 		return (VM_PAGER_FAIL);
1150915d1b71SMark Johnston 
1151915d1b71SMark Johnston 	/*
1152915d1b71SMark Johnston 	 * Clip the readahead and readbehind ranges to exclude resident pages.
1153915d1b71SMark Johnston 	 */
1154915d1b71SMark Johnston 	if (rahead != NULL) {
1155dd9cb6daSMark Johnston 		KASSERT(reqcount - 1 <= maxahead,
1156915d1b71SMark Johnston 		    ("page count %d extends beyond swap block", reqcount));
1157dd9cb6daSMark Johnston 		*rahead = imin(*rahead, maxahead - (reqcount - 1));
11583f060b60SMark Johnston 		pindex = ma[reqcount - 1]->pindex;
11593f060b60SMark Johnston 		msucc = TAILQ_NEXT(ma[reqcount - 1], listq);
1160915d1b71SMark Johnston 		if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1161915d1b71SMark Johnston 			*rahead = msucc->pindex - pindex - 1;
1162915d1b71SMark Johnston 	}
1163915d1b71SMark Johnston 	if (rbehind != NULL) {
1164dd9cb6daSMark Johnston 		*rbehind = imin(*rbehind, maxbehind);
11653f060b60SMark Johnston 		pindex = ma[0]->pindex;
11663f060b60SMark Johnston 		mpred = TAILQ_PREV(ma[0], pglist, listq);
1167915d1b71SMark Johnston 		if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1168915d1b71SMark Johnston 			*rbehind = pindex - mpred->pindex - 1;
1169915d1b71SMark Johnston 	}
1170915d1b71SMark Johnston 
1171b7b8a096SKonstantin Belousov 	bm = ma[0];
1172b7b8a096SKonstantin Belousov 	for (i = 0; i < count; i++)
1173b7b8a096SKonstantin Belousov 		ma[i]->oflags |= VPO_SWAPINPROG;
1174b7b8a096SKonstantin Belousov 
1175915d1b71SMark Johnston 	/*
1176915d1b71SMark Johnston 	 * Allocate readahead and readbehind pages.
1177915d1b71SMark Johnston 	 */
1178b7b8a096SKonstantin Belousov 	if (rbehind != NULL) {
1179b7b8a096SKonstantin Belousov 		for (i = 1; i <= *rbehind; i++) {
11803f060b60SMark Johnston 			p = vm_page_alloc(object, ma[0]->pindex - i,
11817667839aSAlan Cox 			    VM_ALLOC_NORMAL);
1182b7b8a096SKonstantin Belousov 			if (p == NULL)
1183915d1b71SMark Johnston 				break;
1184b7b8a096SKonstantin Belousov 			p->oflags |= VPO_SWAPINPROG;
1185b7b8a096SKonstantin Belousov 			bm = p;
1186915d1b71SMark Johnston 		}
1187b7b8a096SKonstantin Belousov 		*rbehind = i - 1;
1188915d1b71SMark Johnston 	}
1189915d1b71SMark Johnston 	if (rahead != NULL) {
1190915d1b71SMark Johnston 		for (i = 0; i < *rahead; i++) {
1191915d1b71SMark Johnston 			p = vm_page_alloc(object,
11923f060b60SMark Johnston 			    ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1193915d1b71SMark Johnston 			if (p == NULL)
1194915d1b71SMark Johnston 				break;
1195b7b8a096SKonstantin Belousov 			p->oflags |= VPO_SWAPINPROG;
1196915d1b71SMark Johnston 		}
1197915d1b71SMark Johnston 		*rahead = i;
1198915d1b71SMark Johnston 	}
1199915d1b71SMark Johnston 	if (rbehind != NULL)
1200915d1b71SMark Johnston 		count += *rbehind;
1201915d1b71SMark Johnston 	if (rahead != NULL)
1202915d1b71SMark Johnston 		count += *rahead;
1203915d1b71SMark Johnston 
1204915d1b71SMark Johnston 	vm_object_pip_add(object, count);
1205915d1b71SMark Johnston 
1206b7b8a096SKonstantin Belousov 	pindex = bm->pindex;
1207915d1b71SMark Johnston 	blk = swp_pager_meta_ctl(object, pindex, 0);
1208915d1b71SMark Johnston 	KASSERT(blk != SWAPBLK_NONE,
1209915d1b71SMark Johnston 	    ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1210915d1b71SMark Johnston 
1211915d1b71SMark Johnston 	VM_OBJECT_WUNLOCK(object);
1212756a5412SGleb Smirnoff 	bp = uma_zalloc(swrbuf_zone, M_WAITOK);
1213b7b8a096SKonstantin Belousov 	/* Pages cannot leave the object while busy. */
1214b7b8a096SKonstantin Belousov 	for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) {
1215b7b8a096SKonstantin Belousov 		MPASS(p->pindex == bm->pindex + i);
1216b7b8a096SKonstantin Belousov 		bp->b_pages[i] = p;
1217b7b8a096SKonstantin Belousov 	}
1218915d1b71SMark Johnston 
1219915d1b71SMark Johnston 	bp->b_flags |= B_PAGING;
122021144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
12211c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1222fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1223fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
1224b0cd2017SGleb Smirnoff 	bp->b_blkno = blk;
1225b0cd2017SGleb Smirnoff 	bp->b_bcount = PAGE_SIZE * count;
1226b0cd2017SGleb Smirnoff 	bp->b_bufsize = PAGE_SIZE * count;
1227b0cd2017SGleb Smirnoff 	bp->b_npages = count;
1228915d1b71SMark Johnston 	bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1229915d1b71SMark Johnston 	bp->b_pgafter = rahead != NULL ? *rahead : 0;
123026f9a767SRodney W. Grimes 
123183c9dea1SGleb Smirnoff 	VM_CNT_INC(v_swapin);
123283c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_swappgsin, count);
12331c7c3c6aSMatthew Dillon 
12341c7c3c6aSMatthew Dillon 	/*
12351c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
12361c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
12371c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
12381c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
12391c7c3c6aSMatthew Dillon 	 *
12403f060b60SMark Johnston 	 * The other pages in our ma[] array are also released on completion,
12411c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
12421c7c3c6aSMatthew Dillon 	 *
1243c37a77eeSPoul-Henning Kamp 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
12441c7c3c6aSMatthew Dillon 	 */
1245b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
12464b03903aSPoul-Henning Kamp 	swp_pager_strategy(bp);
124726f9a767SRodney W. Grimes 
124826f9a767SRodney W. Grimes 	/*
1249915d1b71SMark Johnston 	 * Wait for the pages we want to complete.  VPO_SWAPINPROG is always
12501c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1251915d1b71SMark Johnston 	 * is set in the metadata for each page in the request.
125226f9a767SRodney W. Grimes 	 */
125389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
12543f060b60SMark Johnston 	while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) {
12553f060b60SMark Johnston 		ma[0]->oflags |= VPO_SWAPSLEEP;
125683c9dea1SGleb Smirnoff 		VM_CNT_INC(v_intrans);
1257c7aebda8SAttilio Rao 		if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP,
1258c7aebda8SAttilio Rao 		    "swread", hz * 20)) {
12599bd86a98SBruce M Simpson 			printf(
1260c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1261c5690651SPoul-Henning Kamp 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
12621c7c3c6aSMatthew Dillon 		}
12631b119d9dSDavid Greenman 	}
126426f9a767SRodney W. Grimes 
126526f9a767SRodney W. Grimes 	/*
1266b0cd2017SGleb Smirnoff 	 * If we had an unrecoverable read error pages will not be valid.
126726f9a767SRodney W. Grimes 	 */
1268915d1b71SMark Johnston 	for (i = 0; i < reqcount; i++)
12693f060b60SMark Johnston 		if (ma[i]->valid != VM_PAGE_BITS_ALL)
12701c7c3c6aSMatthew Dillon 			return (VM_PAGER_ERROR);
1271b0cd2017SGleb Smirnoff 
12721c7c3c6aSMatthew Dillon 	return (VM_PAGER_OK);
12731c7c3c6aSMatthew Dillon 
12741c7c3c6aSMatthew Dillon 	/*
12751c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
12761c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
12771c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
12781c7c3c6aSMatthew Dillon 	 * to all-zero's later.
12791c7c3c6aSMatthew Dillon 	 */
1280df8bae1dSRodney W. Grimes }
1281df8bae1dSRodney W. Grimes 
12821c7c3c6aSMatthew Dillon /*
128390effb23SGleb Smirnoff  * 	swap_pager_getpages_async():
128490effb23SGleb Smirnoff  *
128590effb23SGleb Smirnoff  *	Right now this is emulation of asynchronous operation on top of
128690effb23SGleb Smirnoff  *	swap_pager_getpages().
128790effb23SGleb Smirnoff  */
128890effb23SGleb Smirnoff static int
12893f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count,
1290b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
129190effb23SGleb Smirnoff {
129290effb23SGleb Smirnoff 	int r, error;
129390effb23SGleb Smirnoff 
12943f060b60SMark Johnston 	r = swap_pager_getpages(object, ma, count, rbehind, rahead);
129590effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
129690effb23SGleb Smirnoff 	switch (r) {
129790effb23SGleb Smirnoff 	case VM_PAGER_OK:
129890effb23SGleb Smirnoff 		error = 0;
129990effb23SGleb Smirnoff 		break;
130090effb23SGleb Smirnoff 	case VM_PAGER_ERROR:
130190effb23SGleb Smirnoff 		error = EIO;
130290effb23SGleb Smirnoff 		break;
130390effb23SGleb Smirnoff 	case VM_PAGER_FAIL:
130490effb23SGleb Smirnoff 		error = EINVAL;
130590effb23SGleb Smirnoff 		break;
130690effb23SGleb Smirnoff 	default:
1307d9328101SGleb Smirnoff 		panic("unhandled swap_pager_getpages() error %d", r);
130890effb23SGleb Smirnoff 	}
13093f060b60SMark Johnston 	(iodone)(arg, ma, count, error);
131090effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
131190effb23SGleb Smirnoff 
131290effb23SGleb Smirnoff 	return (r);
131390effb23SGleb Smirnoff }
131490effb23SGleb Smirnoff 
131590effb23SGleb Smirnoff /*
13161c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
13171c7c3c6aSMatthew Dillon  *
13181c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
13191c7c3c6aSMatthew Dillon  *
13201c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
13211c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
13221c7c3c6aSMatthew Dillon  *
1323ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
13241c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
13251c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
13261c7c3c6aSMatthew Dillon  *	which needs work.
13271c7c3c6aSMatthew Dillon  *
13281c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
13291c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
13301c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
13311c7c3c6aSMatthew Dillon  *	completion.
13321c7c3c6aSMatthew Dillon  *
13331c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
13341c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
13351c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
13361c7c3c6aSMatthew Dillon  */
1337d635a37fSWarner Losh static void
13383f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count,
1339e065e87cSKonstantin Belousov     int flags, int *rtvals)
1340df8bae1dSRodney W. Grimes {
1341e065e87cSKonstantin Belousov 	int i, n;
1342e065e87cSKonstantin Belousov 	boolean_t sync;
134378f1deefSAlan Cox 	daddr_t addr, n_free, s_free;
1344df8bae1dSRodney W. Grimes 
134578f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
13463f060b60SMark Johnston 	if (count && ma[0]->object != object) {
13477036145bSMaxim Konovalov 		panic("swap_pager_putpages: object mismatch %p/%p",
13481c7c3c6aSMatthew Dillon 		    object,
13493f060b60SMark Johnston 		    ma[0]->object
13501c7c3c6aSMatthew Dillon 		);
13511c7c3c6aSMatthew Dillon 	}
1352ee3dc7d7SAlan Cox 
13531c7c3c6aSMatthew Dillon 	/*
13541c7c3c6aSMatthew Dillon 	 * Step 1
13551c7c3c6aSMatthew Dillon 	 *
13561c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
13571c7c3c6aSMatthew Dillon 	 * check for bogus sysops
13581c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
13591c7c3c6aSMatthew Dillon 	 */
136078f1deefSAlan Cox 	if (object->type != OBJT_SWAP) {
136178f1deefSAlan Cox 		addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE);
136278f1deefSAlan Cox 		KASSERT(addr == SWAPBLK_NONE,
136378f1deefSAlan Cox 		    ("unexpected object swap block"));
136478f1deefSAlan Cox 	}
136589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1366e47ed70bSJohn Dyson 
1367e065e87cSKonstantin Belousov 	n = 0;
1368e47ed70bSJohn Dyson 	if (curproc != pageproc)
1369e47ed70bSJohn Dyson 		sync = TRUE;
1370e065e87cSKonstantin Belousov 	else
1371e065e87cSKonstantin Belousov 		sync = (flags & VM_PAGER_PUT_SYNC) != 0;
137226f9a767SRodney W. Grimes 
13731c7c3c6aSMatthew Dillon 	/*
13741c7c3c6aSMatthew Dillon 	 * Step 2
13751c7c3c6aSMatthew Dillon 	 *
13761c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
13771c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
13781c7c3c6aSMatthew Dillon 	 * successfully.
13791c7c3c6aSMatthew Dillon 	 */
13801c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
13811c7c3c6aSMatthew Dillon 		int j;
13821c7c3c6aSMatthew Dillon 		struct buf *bp;
1383a316d390SJohn Dyson 		daddr_t blk;
138426f9a767SRodney W. Grimes 
1385df8bae1dSRodney W. Grimes 		/*
13861c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1387df8bae1dSRodney W. Grimes 		 */
13881c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1389327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
13901c7c3c6aSMatthew Dillon 
139148e98a2aSDoug Moore 		/* Get a block of swap of size up to size n. */
139248e98a2aSDoug Moore 		blk = swp_pager_getswapspace(&n, 4);
13931c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
13944dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
13951c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
13961c7c3c6aSMatthew Dillon 			continue;
139726f9a767SRodney W. Grimes 		}
139826f9a767SRodney W. Grimes 
139926f9a767SRodney W. Grimes 		/*
14001c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
14011c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
140226f9a767SRodney W. Grimes 		 */
1403756a5412SGleb Smirnoff 		if (sync != TRUE) {
1404756a5412SGleb Smirnoff 			mtx_lock(&swbuf_mtx);
1405756a5412SGleb Smirnoff 			while (nsw_wcount_async == 0)
1406756a5412SGleb Smirnoff 				msleep(&nsw_wcount_async, &swbuf_mtx, PVM,
1407756a5412SGleb Smirnoff 				    "swbufa", 0);
1408756a5412SGleb Smirnoff 			nsw_wcount_async--;
1409756a5412SGleb Smirnoff 			mtx_unlock(&swbuf_mtx);
1410327f4e83SMatthew Dillon 		}
1411756a5412SGleb Smirnoff 		bp = uma_zalloc(swwbuf_zone, M_WAITOK);
1412756a5412SGleb Smirnoff 		if (sync != TRUE)
1413756a5412SGleb Smirnoff 			bp->b_flags = B_ASYNC;
14145e04322aSPoul-Henning Kamp 		bp->b_flags |= B_PAGING;
1415912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
141626f9a767SRodney W. Grimes 
1417fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1418fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
14191c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
14201c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
14211c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1422e47ed70bSJohn Dyson 
142389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
14241c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
14253f060b60SMark Johnston 			vm_page_t mreq = ma[i+j];
14261c7c3c6aSMatthew Dillon 
142778f1deefSAlan Cox 			addr = swp_pager_meta_build(mreq->object, mreq->pindex,
142878f1deefSAlan Cox 			    blk + j);
142978f1deefSAlan Cox 			if (addr != SWAPBLK_NONE)
143078f1deefSAlan Cox 				swp_pager_update_freerange(&s_free, &n_free,
143178f1deefSAlan Cox 				    addr);
143287b0ab69SAlan Cox 			MPASS(mreq->dirty == VM_PAGE_BITS_ALL);
14335786be7cSAlan Cox 			mreq->oflags |= VPO_SWAPINPROG;
14341c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
14351c7c3c6aSMatthew Dillon 		}
143689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
14371c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1438a5296b05SJulian Elischer 		/*
1439a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1440a5296b05SJulian Elischer 		 */
1441a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1442a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
14431c7c3c6aSMatthew Dillon 
144483c9dea1SGleb Smirnoff 		VM_CNT_INC(v_swapout);
144583c9dea1SGleb Smirnoff 		VM_CNT_ADD(v_swappgsout, bp->b_npages);
144626f9a767SRodney W. Grimes 
144726f9a767SRodney W. Grimes 		/*
144877923df2SAlan Cox 		 * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
144977923df2SAlan Cox 		 * can call the async completion routine at the end of a
145077923df2SAlan Cox 		 * synchronous I/O operation.  Otherwise, our caller would
145177923df2SAlan Cox 		 * perform duplicate unbusy and wakeup operations on the page
145277923df2SAlan Cox 		 * and object, respectively.
145377923df2SAlan Cox 		 */
145477923df2SAlan Cox 		for (j = 0; j < n; j++)
145577923df2SAlan Cox 			rtvals[i + j] = VM_PAGER_PEND;
145677923df2SAlan Cox 
145777923df2SAlan Cox 		/*
14581c7c3c6aSMatthew Dillon 		 * asynchronous
14591c7c3c6aSMatthew Dillon 		 *
1460c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
146126f9a767SRodney W. Grimes 		 */
14621c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
14631c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
146467812eacSKirk McKusick 			BUF_KERNPROC(bp);
14654b03903aSPoul-Henning Kamp 			swp_pager_strategy(bp);
14661c7c3c6aSMatthew Dillon 			continue;
146726f9a767SRodney W. Grimes 		}
1468e47ed70bSJohn Dyson 
146926f9a767SRodney W. Grimes 		/*
14701c7c3c6aSMatthew Dillon 		 * synchronous
14711c7c3c6aSMatthew Dillon 		 *
1472c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
14731c7c3c6aSMatthew Dillon 		 */
14742c840b1fSAlan Cox 		bp->b_iodone = bdone;
14754b03903aSPoul-Henning Kamp 		swp_pager_strategy(bp);
14761c7c3c6aSMatthew Dillon 
14771c7c3c6aSMatthew Dillon 		/*
147877923df2SAlan Cox 		 * Wait for the sync I/O to complete.
147926f9a767SRodney W. Grimes 		 */
14802c840b1fSAlan Cox 		bwait(bp, PVM, "swwrt");
148177923df2SAlan Cox 
14821c7c3c6aSMatthew Dillon 		/*
14831c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
14841c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
14851c7c3c6aSMatthew Dillon 		 */
14861c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
14871c7c3c6aSMatthew Dillon 	}
148889f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
148978f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
14901c7c3c6aSMatthew Dillon }
14911c7c3c6aSMatthew Dillon 
14921c7c3c6aSMatthew Dillon /*
14931c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
14941c7c3c6aSMatthew Dillon  *
14951c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
14961c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
14971c7c3c6aSMatthew Dillon  *
149815523cf7SKonstantin Belousov  *	This routine may not sleep.
14991c7c3c6aSMatthew Dillon  */
15001c7c3c6aSMatthew Dillon static void
15012f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp)
15021c7c3c6aSMatthew Dillon {
15031c7c3c6aSMatthew Dillon 	int i;
15041c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
15051c7c3c6aSMatthew Dillon 
15061c7c3c6aSMatthew Dillon 	/*
15070b208315SEdward Tomasz Napierala 	 * Report error - unless we ran out of memory, in which case
15080b208315SEdward Tomasz Napierala 	 * we've already logged it in swapgeom_strategy().
15091c7c3c6aSMatthew Dillon 	 */
15100b208315SEdward Tomasz Napierala 	if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) {
15111c7c3c6aSMatthew Dillon 		printf(
15121c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
15131c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
151421144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
15151c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
15161c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
15171c7c3c6aSMatthew Dillon 		    bp->b_error
15181c7c3c6aSMatthew Dillon 		);
15191c7c3c6aSMatthew Dillon 	}
15201c7c3c6aSMatthew Dillon 
15211c7c3c6aSMatthew Dillon 	/*
152226f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
152326f9a767SRodney W. Grimes 	 */
1524fade8dd7SJeff Roberson 	if (buf_mapped(bp))
15251c7c3c6aSMatthew Dillon 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1526fade8dd7SJeff Roberson 	else
1527fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
152826f9a767SRodney W. Grimes 
152933a609ecSAlan Cox 	if (bp->b_npages) {
153033a609ecSAlan Cox 		object = bp->b_pages[0]->object;
153189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
153233a609ecSAlan Cox 	}
15332965a453SKip Macy 
153426f9a767SRodney W. Grimes 	/*
15351c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
15361c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
15371c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
15381c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
15391c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
15401c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
154126f9a767SRodney W. Grimes 	 */
15421c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
15431c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1544e47ed70bSJohn Dyson 
15455786be7cSAlan Cox 		m->oflags &= ~VPO_SWAPINPROG;
1546c7aebda8SAttilio Rao 		if (m->oflags & VPO_SWAPSLEEP) {
1547c7aebda8SAttilio Rao 			m->oflags &= ~VPO_SWAPSLEEP;
1548c7aebda8SAttilio Rao 			wakeup(&object->paging_in_progress);
1549c7aebda8SAttilio Rao 		}
1550e47ed70bSJohn Dyson 
1551c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1552ffc82b0aSJohn Dyson 			/*
15531c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
15541c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
15551c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
15561c7c3c6aSMatthew Dillon 			 * interrupt.
1557ffc82b0aSJohn Dyson 			 */
155821144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
15591c7c3c6aSMatthew Dillon 				/*
15601c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1561956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15621c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15631c7c3c6aSMatthew Dillon 				 */
15641c7c3c6aSMatthew Dillon 				m->valid = 0;
15651c7c3c6aSMatthew Dillon 			} else {
15661c7c3c6aSMatthew Dillon 				/*
15671c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
15681c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
15691c7c3c6aSMatthew Dillon 				 * then finish the I/O.
15701c7c3c6aSMatthew Dillon 				 */
157141e5a226SAlan Cox 				MPASS(m->dirty == VM_PAGE_BITS_ALL);
15723c4a2440SAlan Cox 				vm_page_lock(m);
15731c7c3c6aSMatthew Dillon 				vm_page_activate(m);
15743c4a2440SAlan Cox 				vm_page_unlock(m);
1575c7aebda8SAttilio Rao 				vm_page_sunbusy(m);
15761c7c3c6aSMatthew Dillon 			}
157721144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
15781c7c3c6aSMatthew Dillon 			/*
15791c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1580956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
15811c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
15821c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
15831c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
15841c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
15851c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
15861c7c3c6aSMatthew Dillon 			 */
1587016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1588016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is mapped", m));
1589016a3c93SAlan Cox 			KASSERT(m->dirty == 0,
1590016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is dirty", m));
1591915d1b71SMark Johnston 
1592b0cd2017SGleb Smirnoff 			m->valid = VM_PAGE_BITS_ALL;
1593915d1b71SMark Johnston 			if (i < bp->b_pgbefore ||
1594915d1b71SMark Johnston 			    i >= bp->b_npages - bp->b_pgafter)
1595915d1b71SMark Johnston 				vm_page_readahead_finish(m);
15961c7c3c6aSMatthew Dillon 		} else {
15971c7c3c6aSMatthew Dillon 			/*
1598016a3c93SAlan Cox 			 * For write success, clear the dirty
15991c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
16001c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
1601ebcddc72SAlan Cox 			 * A page is only written to swap after a period of
1602ebcddc72SAlan Cox 			 * inactivity.  Therefore, we do not expect it to be
1603ebcddc72SAlan Cox 			 * reused.
16041c7c3c6aSMatthew Dillon 			 */
16056031c68dSAlan Cox 			KASSERT(!pmap_page_is_write_mapped(m),
1606016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is not write"
1607016a3c93SAlan Cox 			    " protected", m));
1608c52e7044SAlan Cox 			vm_page_undirty(m);
16093c4a2440SAlan Cox 			vm_page_lock(m);
1610ebcddc72SAlan Cox 			vm_page_deactivate_noreuse(m);
16112965a453SKip Macy 			vm_page_unlock(m);
1612ebcddc72SAlan Cox 			vm_page_sunbusy(m);
16133c4a2440SAlan Cox 		}
16143c4a2440SAlan Cox 	}
161526f9a767SRodney W. Grimes 
16161c7c3c6aSMatthew Dillon 	/*
16171c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
16181c7c3c6aSMatthew Dillon 	 * pip refs on the object.
16191c7c3c6aSMatthew Dillon 	 */
16200d420ad3SAlan Cox 	if (object != NULL) {
16211c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
162289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
16230d420ad3SAlan Cox 	}
162426f9a767SRodney W. Grimes 
16251c7c3c6aSMatthew Dillon 	/*
1626100650deSOlivier Houchard 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1627100650deSOlivier Houchard 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1628100650deSOlivier Houchard 	 * trigger a KASSERT in relpbuf().
1629100650deSOlivier Houchard 	 */
1630100650deSOlivier Houchard 	if (bp->b_vp) {
1631100650deSOlivier Houchard 		    bp->b_vp = NULL;
1632100650deSOlivier Houchard 		    bp->b_bufobj = NULL;
1633100650deSOlivier Houchard 	}
1634100650deSOlivier Houchard 	/*
16351c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
16361c7c3c6aSMatthew Dillon 	 */
1637756a5412SGleb Smirnoff 	if (bp->b_flags & B_ASYNC) {
1638756a5412SGleb Smirnoff 		mtx_lock(&swbuf_mtx);
1639756a5412SGleb Smirnoff 		if (++nsw_wcount_async == 1)
1640756a5412SGleb Smirnoff 			wakeup(&nsw_wcount_async);
1641756a5412SGleb Smirnoff 		mtx_unlock(&swbuf_mtx);
1642756a5412SGleb Smirnoff 	}
1643756a5412SGleb Smirnoff 	uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp);
164426f9a767SRodney W. Grimes }
16451c7c3c6aSMatthew Dillon 
1646b1fd102eSMark Johnston int
1647b1fd102eSMark Johnston swap_pager_nswapdev(void)
1648b1fd102eSMark Johnston {
1649b1fd102eSMark Johnston 
1650b1fd102eSMark Johnston 	return (nswapdev);
1651b1fd102eSMark Johnston }
1652b1fd102eSMark Johnston 
16537c022327SDoug Moore static void
16547c022327SDoug Moore swp_pager_force_dirty(vm_page_t m)
16557c022327SDoug Moore {
16567c022327SDoug Moore 
16577c022327SDoug Moore 	vm_page_dirty(m);
16587c022327SDoug Moore #ifdef INVARIANTS
16597c022327SDoug Moore 	vm_page_lock(m);
16607c022327SDoug Moore 	if (!vm_page_wired(m) && m->queue == PQ_NONE)
16617c022327SDoug Moore 		panic("page %p is neither wired nor queued", m);
16627c022327SDoug Moore 	vm_page_unlock(m);
16637c022327SDoug Moore #endif
16647c022327SDoug Moore 	vm_page_xunbusy(m);
1665*56948d17SDoug Moore 	swap_pager_unswapped(m);
16667c022327SDoug Moore }
16677c022327SDoug Moore 
16687c022327SDoug Moore static void
16697c022327SDoug Moore swp_pager_force_launder(vm_page_t m)
16707c022327SDoug Moore {
16717c022327SDoug Moore 
16727c022327SDoug Moore 	vm_page_dirty(m);
16737c022327SDoug Moore 	vm_page_lock(m);
16747c022327SDoug Moore 	vm_page_launder(m);
16757c022327SDoug Moore 	vm_page_unlock(m);
16767c022327SDoug Moore 	vm_page_xunbusy(m);
1677*56948d17SDoug Moore 	swap_pager_unswapped(m);
16787c022327SDoug Moore }
16797c022327SDoug Moore 
168092da00bbSMatthew Dillon /*
1681*56948d17SDoug Moore  * SWP_PAGER_FORCE_PAGEIN() - force swap blocks to be paged in
168292da00bbSMatthew Dillon  *
1683*56948d17SDoug Moore  *	This routine dissociates pages starting at the given index within an
1684*56948d17SDoug Moore  *	object from their backing store, paging them in if they do not reside
1685*56948d17SDoug Moore  *	in memory.  Pages that are paged in are marked dirty and placed in the
1686*56948d17SDoug Moore  *	laundry queue.  Pages are marked dirty because they no longer have
1687*56948d17SDoug Moore  *	backing store.  They are placed in the laundry queue because they have
1688*56948d17SDoug Moore  *	not been accessed recently.  Otherwise, they would already reside in
1689*56948d17SDoug Moore  *	memory.
169092da00bbSMatthew Dillon  */
16917c022327SDoug Moore static void
1692*56948d17SDoug Moore swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex, int npages)
169392da00bbSMatthew Dillon {
1694*56948d17SDoug Moore 	vm_page_t ma[npages];
1695*56948d17SDoug Moore 	int i, j;
169692da00bbSMatthew Dillon 
1697*56948d17SDoug Moore 	KASSERT(npages > 0, ("%s: No pages", __func__));
1698*56948d17SDoug Moore 	KASSERT(npages <= MAXPHYS / PAGE_SIZE,
1699*56948d17SDoug Moore 	    ("%s: Too many pages: %d", __func__, npages));
1700*56948d17SDoug Moore 	vm_object_pip_add(object, npages);
1701*56948d17SDoug Moore 	vm_page_grab_pages(object, pindex, VM_ALLOC_NORMAL, ma, npages);
1702*56948d17SDoug Moore 	for (i = j = 0;; i++) {
1703*56948d17SDoug Moore 		/* Count nonresident pages, to page-in all at once. */
1704*56948d17SDoug Moore 		if (i < npages && ma[i]->valid != VM_PAGE_BITS_ALL)
1705*56948d17SDoug Moore 			continue;
1706*56948d17SDoug Moore 		if (j < i) {
1707*56948d17SDoug Moore 			/* Page-in nonresident pages. Mark for laundering. */
1708*56948d17SDoug Moore 			if (swap_pager_getpages(object, &ma[j], i - j, NULL,
1709*56948d17SDoug Moore 			    NULL) != VM_PAGER_OK)
1710*56948d17SDoug Moore 				panic("%s: read from swap failed", __func__);
1711*56948d17SDoug Moore 			do {
1712*56948d17SDoug Moore 				swp_pager_force_launder(ma[j]);
1713*56948d17SDoug Moore 			} while (++j < i);
171492da00bbSMatthew Dillon 		}
1715*56948d17SDoug Moore 		if (i == npages)
1716*56948d17SDoug Moore 			break;
1717*56948d17SDoug Moore 		/* Mark dirty a resident page. */
1718*56948d17SDoug Moore 		swp_pager_force_dirty(ma[j++]);
1719*56948d17SDoug Moore 	}
1720*56948d17SDoug Moore 	vm_object_pip_wakeupn(object, npages);
172192da00bbSMatthew Dillon }
172292da00bbSMatthew Dillon 
172392da00bbSMatthew Dillon /*
17247c022327SDoug Moore  *	swap_pager_swapoff_object:
17257c022327SDoug Moore  *
17267c022327SDoug Moore  *	Page in all of the pages that have been paged out for an object
1727*56948d17SDoug Moore  *	to a swap device.
17287c022327SDoug Moore  */
17297c022327SDoug Moore static void
17307c022327SDoug Moore swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object)
17317c022327SDoug Moore {
17327c022327SDoug Moore 	struct swblk *sb;
1733*56948d17SDoug Moore 	vm_pindex_t pi, s_pindex;
1734*56948d17SDoug Moore 	daddr_t blk, n_blks, s_blk;
17357c022327SDoug Moore 	int i;
17367c022327SDoug Moore 
1737*56948d17SDoug Moore 	n_blks = 0;
17387c022327SDoug Moore 	for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
17397c022327SDoug Moore 	    &object->un_pager.swp.swp_blks, pi)) != NULL; ) {
17407c022327SDoug Moore 		for (i = 0; i < SWAP_META_PAGES; i++) {
1741*56948d17SDoug Moore 			blk = sb->d[i];
1742*56948d17SDoug Moore 			if (!swp_pager_isondev(blk, sp))
1743*56948d17SDoug Moore 				blk = SWAPBLK_NONE;
1744*56948d17SDoug Moore 
1745*56948d17SDoug Moore 			/*
1746*56948d17SDoug Moore 			 * If there are no blocks/pages accumulated, start a new
1747*56948d17SDoug Moore 			 * accumulation here.
1748*56948d17SDoug Moore 			 */
1749*56948d17SDoug Moore 			if (n_blks == 0) {
1750*56948d17SDoug Moore 				if (blk != SWAPBLK_NONE) {
1751*56948d17SDoug Moore 					s_blk = blk;
1752*56948d17SDoug Moore 					s_pindex = sb->p + i;
1753*56948d17SDoug Moore 					n_blks = 1;
1754*56948d17SDoug Moore 				}
17557c022327SDoug Moore 				continue;
17567c022327SDoug Moore 			}
1757*56948d17SDoug Moore 
1758*56948d17SDoug Moore 			/*
1759*56948d17SDoug Moore 			 * If the accumulation can be extended without breaking
1760*56948d17SDoug Moore 			 * the sequence of consecutive blocks and pages that
1761*56948d17SDoug Moore 			 * swp_pager_force_pagein() depends on, do so.
1762*56948d17SDoug Moore 			 */
1763*56948d17SDoug Moore 			if (n_blks < MAXPHYS / PAGE_SIZE &&
1764*56948d17SDoug Moore 			    s_blk + n_blks == blk &&
1765*56948d17SDoug Moore 			    s_pindex + n_blks == sb->p + i) {
1766*56948d17SDoug Moore 				++n_blks;
1767*56948d17SDoug Moore 				continue;
17687c022327SDoug Moore 			}
1769*56948d17SDoug Moore 
1770*56948d17SDoug Moore 			/*
1771*56948d17SDoug Moore 			 * The sequence of consecutive blocks and pages cannot
1772*56948d17SDoug Moore 			 * be extended, so page them all in here.  Then,
1773*56948d17SDoug Moore 			 * because doing so involves releasing and reacquiring
1774*56948d17SDoug Moore 			 * a lock that protects the swap block pctrie, do not
1775*56948d17SDoug Moore 			 * rely on the current swap block.  Break this loop and
1776*56948d17SDoug Moore 			 * re-fetch the same pindex from the pctrie again.
1777*56948d17SDoug Moore 			 */
1778*56948d17SDoug Moore 			swp_pager_force_pagein(object, s_pindex, n_blks);
1779*56948d17SDoug Moore 			n_blks = 0;
1780*56948d17SDoug Moore 			break;
1781*56948d17SDoug Moore 		}
1782*56948d17SDoug Moore 		if (i == SWAP_META_PAGES)
1783*56948d17SDoug Moore 			pi = sb->p + SWAP_META_PAGES;
1784*56948d17SDoug Moore 	}
1785*56948d17SDoug Moore 	if (n_blks > 0)
1786*56948d17SDoug Moore 		swp_pager_force_pagein(object, s_pindex, n_blks);
17877c022327SDoug Moore }
17887c022327SDoug Moore 
17897c022327SDoug Moore /*
179092da00bbSMatthew Dillon  *	swap_pager_swapoff:
179192da00bbSMatthew Dillon  *
179292da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
179392da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
179492da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
179592da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
179692da00bbSMatthew Dillon  *
179792da00bbSMatthew Dillon  *	This routine may block.
179892da00bbSMatthew Dillon  */
1799e9c0cc15SPoul-Henning Kamp static void
1800b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp)
180192da00bbSMatthew Dillon {
1802f425ab8eSKonstantin Belousov 	vm_object_t object;
18037c022327SDoug Moore 	int retries;
180492da00bbSMatthew Dillon 
180504533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
180692da00bbSMatthew Dillon 
18078bc61209SDavid Schultz 	retries = 0;
180892da00bbSMatthew Dillon full_rescan:
1809f425ab8eSKonstantin Belousov 	mtx_lock(&vm_object_list_mtx);
1810f425ab8eSKonstantin Belousov 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
1811f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
181298150664SKonstantin Belousov 			continue;
1813f425ab8eSKonstantin Belousov 		mtx_unlock(&vm_object_list_mtx);
181498150664SKonstantin Belousov 		/* Depends on type-stability. */
181598150664SKonstantin Belousov 		VM_OBJECT_WLOCK(object);
1816f425ab8eSKonstantin Belousov 
1817f425ab8eSKonstantin Belousov 		/*
1818f425ab8eSKonstantin Belousov 		 * Dead objects are eventually terminated on their own.
1819f425ab8eSKonstantin Belousov 		 */
1820f425ab8eSKonstantin Belousov 		if ((object->flags & OBJ_DEAD) != 0)
1821f425ab8eSKonstantin Belousov 			goto next_obj;
1822f425ab8eSKonstantin Belousov 
1823f425ab8eSKonstantin Belousov 		/*
1824f425ab8eSKonstantin Belousov 		 * Sync with fences placed after pctrie
1825f425ab8eSKonstantin Belousov 		 * initialization.  We must not access pctrie below
1826f425ab8eSKonstantin Belousov 		 * unless we checked that our object is swap and not
1827f425ab8eSKonstantin Belousov 		 * dead.
1828f425ab8eSKonstantin Belousov 		 */
1829f425ab8eSKonstantin Belousov 		atomic_thread_fence_acq();
1830f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
1831f425ab8eSKonstantin Belousov 			goto next_obj;
1832f425ab8eSKonstantin Belousov 
18337c022327SDoug Moore 		swap_pager_swapoff_object(sp, object);
1834f425ab8eSKonstantin Belousov next_obj:
1835f425ab8eSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
1836f425ab8eSKonstantin Belousov 		mtx_lock(&vm_object_list_mtx);
183792da00bbSMatthew Dillon 	}
1838f425ab8eSKonstantin Belousov 	mtx_unlock(&vm_object_list_mtx);
1839f425ab8eSKonstantin Belousov 
18408bc61209SDavid Schultz 	if (sp->sw_used) {
184192da00bbSMatthew Dillon 		/*
18428bc61209SDavid Schultz 		 * Objects may be locked or paging to the device being
18438bc61209SDavid Schultz 		 * removed, so we will miss their pages and need to
18448bc61209SDavid Schultz 		 * make another pass.  We have marked this device as
18458bc61209SDavid Schultz 		 * SW_CLOSING, so the activity should finish soon.
184692da00bbSMatthew Dillon 		 */
18478bc61209SDavid Schultz 		retries++;
18488bc61209SDavid Schultz 		if (retries > 100) {
18498bc61209SDavid Schultz 			panic("swapoff: failed to locate %d swap blocks",
18508bc61209SDavid Schultz 			    sp->sw_used);
18518bc61209SDavid Schultz 		}
18524d70511aSJohn Baldwin 		pause("swpoff", hz / 20);
185392da00bbSMatthew Dillon 		goto full_rescan;
185492da00bbSMatthew Dillon 	}
1855b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapoff, sp);
185692da00bbSMatthew Dillon }
185792da00bbSMatthew Dillon 
18581c7c3c6aSMatthew Dillon /************************************************************************
18591c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
18601c7c3c6aSMatthew Dillon  ************************************************************************
18611c7c3c6aSMatthew Dillon  *
18621c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
1863cec9f109SDavid E. O'Brien  *	OBJT_SWAP object.
18641c7c3c6aSMatthew Dillon  *
18654dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
18664dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
18674dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
18681c7c3c6aSMatthew Dillon  */
18691c7c3c6aSMatthew Dillon 
18701c7c3c6aSMatthew Dillon /*
1871230869e0SAlan Cox  * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free?
1872230869e0SAlan Cox  */
1873230869e0SAlan Cox static bool
1874230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit)
1875230869e0SAlan Cox {
1876230869e0SAlan Cox 	int i;
1877230869e0SAlan Cox 
1878230869e0SAlan Cox 	MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES);
1879230869e0SAlan Cox 	for (i = start; i < limit; i++) {
1880230869e0SAlan Cox 		if (sb->d[i] != SWAPBLK_NONE)
1881230869e0SAlan Cox 			return (false);
1882230869e0SAlan Cox 	}
1883230869e0SAlan Cox 	return (true);
1884230869e0SAlan Cox }
1885230869e0SAlan Cox 
1886230869e0SAlan Cox /*
18871c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
18881c7c3c6aSMatthew Dillon  *
18891c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
18901c7c3c6aSMatthew Dillon  *	object.
18911c7c3c6aSMatthew Dillon  *
18921c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
18931c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
189478f1deefSAlan Cox  *	assigned swapblk is returned.
18951c7c3c6aSMatthew Dillon  */
189678f1deefSAlan Cox static daddr_t
18972f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
18982f249180SPoul-Henning Kamp {
1899f425ab8eSKonstantin Belousov 	static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted;
1900eed99cb8SKonstantin Belousov 	struct swblk *sb, *sb1;
1901f425ab8eSKonstantin Belousov 	vm_pindex_t modpi, rdpi;
190278f1deefSAlan Cox 	daddr_t prev_swapblk;
1903f425ab8eSKonstantin Belousov 	int error, i;
19041c7c3c6aSMatthew Dillon 
190589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
1906f425ab8eSKonstantin Belousov 
19071c7c3c6aSMatthew Dillon 	/*
19081c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
19091c7c3c6aSMatthew Dillon 	 */
19101c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
1911f425ab8eSKonstantin Belousov 		pctrie_init(&object->un_pager.swp.swp_blks);
1912f425ab8eSKonstantin Belousov 
1913f425ab8eSKonstantin Belousov 		/*
1914f425ab8eSKonstantin Belousov 		 * Ensure that swap_pager_swapoff()'s iteration over
1915f425ab8eSKonstantin Belousov 		 * object_list does not see a garbage pctrie.
1916f425ab8eSKonstantin Belousov 		 */
1917f425ab8eSKonstantin Belousov 		atomic_thread_fence_rel();
1918f425ab8eSKonstantin Belousov 
19191c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
1920eb4d6a1bSKonstantin Belousov 		KASSERT(object->handle == NULL, ("default pager with handle"));
1921bd228075SAlan Cox 	}
19221c7c3c6aSMatthew Dillon 
1923f425ab8eSKonstantin Belousov 	rdpi = rounddown(pindex, SWAP_META_PAGES);
1924f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi);
1925f425ab8eSKonstantin Belousov 	if (sb == NULL) {
19261c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
192778f1deefSAlan Cox 			return (SWAPBLK_NONE);
1928f425ab8eSKonstantin Belousov 		for (;;) {
1929f425ab8eSKonstantin Belousov 			sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc ==
1930f425ab8eSKonstantin Belousov 			    pageproc ? M_USE_RESERVE : 0));
1931f425ab8eSKonstantin Belousov 			if (sb != NULL) {
1932f425ab8eSKonstantin Belousov 				sb->p = rdpi;
1933f425ab8eSKonstantin Belousov 				for (i = 0; i < SWAP_META_PAGES; i++)
1934f425ab8eSKonstantin Belousov 					sb->d[i] = SWAPBLK_NONE;
1935f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swblk_zone_exhausted,
1936f425ab8eSKonstantin Belousov 				    1, 0))
1937f425ab8eSKonstantin Belousov 					printf("swblk zone ok\n");
1938f425ab8eSKonstantin Belousov 				break;
1939f425ab8eSKonstantin Belousov 			}
194089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
1941f425ab8eSKonstantin Belousov 			if (uma_zone_exhausted(swblk_zone)) {
1942f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swblk_zone_exhausted,
1943f425ab8eSKonstantin Belousov 				    0, 1))
1944f425ab8eSKonstantin Belousov 					printf("swap blk zone exhausted, "
19453ff863f1SDag-Erling Smørgrav 					    "increase kern.maxswzone\n");
19462025d69bSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
1947f425ab8eSKonstantin Belousov 				pause("swzonxb", 10);
19482025d69bSKonstantin Belousov 			} else
19498d6fbbb8SJeff Roberson 				uma_zwait(swblk_zone);
195089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
1951eed99cb8SKonstantin Belousov 			sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1952eed99cb8SKonstantin Belousov 			    rdpi);
1953eed99cb8SKonstantin Belousov 			if (sb != NULL)
1954eed99cb8SKonstantin Belousov 				/*
1955eed99cb8SKonstantin Belousov 				 * Somebody swapped out a nearby page,
1956eed99cb8SKonstantin Belousov 				 * allocating swblk at the rdpi index,
1957eed99cb8SKonstantin Belousov 				 * while we dropped the object lock.
1958eed99cb8SKonstantin Belousov 				 */
1959eed99cb8SKonstantin Belousov 				goto allocated;
19604dcc5c2dSMatthew Dillon 		}
1961f425ab8eSKonstantin Belousov 		for (;;) {
1962f425ab8eSKonstantin Belousov 			error = SWAP_PCTRIE_INSERT(
1963f425ab8eSKonstantin Belousov 			    &object->un_pager.swp.swp_blks, sb);
1964f425ab8eSKonstantin Belousov 			if (error == 0) {
1965f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1966f425ab8eSKonstantin Belousov 				    1, 0))
1967f425ab8eSKonstantin Belousov 					printf("swpctrie zone ok\n");
1968f425ab8eSKonstantin Belousov 				break;
19691c7c3c6aSMatthew Dillon 			}
1970f425ab8eSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
1971f425ab8eSKonstantin Belousov 			if (uma_zone_exhausted(swpctrie_zone)) {
1972f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1973f425ab8eSKonstantin Belousov 				    0, 1))
1974f425ab8eSKonstantin Belousov 					printf("swap pctrie zone exhausted, "
1975f425ab8eSKonstantin Belousov 					    "increase kern.maxswzone\n");
1976f425ab8eSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
1977f425ab8eSKonstantin Belousov 				pause("swzonxp", 10);
1978f425ab8eSKonstantin Belousov 			} else
19798d6fbbb8SJeff Roberson 				uma_zwait(swpctrie_zone);
1980f425ab8eSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
1981eed99cb8SKonstantin Belousov 			sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1982eed99cb8SKonstantin Belousov 			    rdpi);
1983eed99cb8SKonstantin Belousov 			if (sb1 != NULL) {
1984eed99cb8SKonstantin Belousov 				uma_zfree(swblk_zone, sb);
1985eed99cb8SKonstantin Belousov 				sb = sb1;
1986eed99cb8SKonstantin Belousov 				goto allocated;
19871c7c3c6aSMatthew Dillon 			}
1988f425ab8eSKonstantin Belousov 		}
1989eed99cb8SKonstantin Belousov 	}
1990eed99cb8SKonstantin Belousov allocated:
1991f425ab8eSKonstantin Belousov 	MPASS(sb->p == rdpi);
19921c7c3c6aSMatthew Dillon 
1993f425ab8eSKonstantin Belousov 	modpi = pindex % SWAP_META_PAGES;
199478f1deefSAlan Cox 	/* Return prior contents of metadata. */
199578f1deefSAlan Cox 	prev_swapblk = sb->d[modpi];
1996f425ab8eSKonstantin Belousov 	/* Enter block into metadata. */
1997f425ab8eSKonstantin Belousov 	sb->d[modpi] = swapblk;
199885d88d87SKonstantin Belousov 
199985d88d87SKonstantin Belousov 	/*
200085d88d87SKonstantin Belousov 	 * Free the swblk if we end up with the empty page run.
200185d88d87SKonstantin Belousov 	 */
2002230869e0SAlan Cox 	if (swapblk == SWAPBLK_NONE &&
2003230869e0SAlan Cox 	    swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
2004230869e0SAlan Cox 		SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, rdpi);
200585d88d87SKonstantin Belousov 		uma_zfree(swblk_zone, sb);
200685d88d87SKonstantin Belousov 	}
200778f1deefSAlan Cox 	return (prev_swapblk);
200885d88d87SKonstantin Belousov }
20091c7c3c6aSMatthew Dillon 
20101c7c3c6aSMatthew Dillon /*
20111c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
20121c7c3c6aSMatthew Dillon  *
20131c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
20141c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
20151c7c3c6aSMatthew Dillon  *
20161c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
20171c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
20181c7c3c6aSMatthew Dillon  *	with resident pages.
20191c7c3c6aSMatthew Dillon  */
20201c7c3c6aSMatthew Dillon static void
2021f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count)
20221c7c3c6aSMatthew Dillon {
2023f425ab8eSKonstantin Belousov 	struct swblk *sb;
202478f1deefSAlan Cox 	daddr_t n_free, s_free;
2025f425ab8eSKonstantin Belousov 	vm_pindex_t last;
2026230869e0SAlan Cox 	int i, limit, start;
20272928cef7SAlan Cox 
2028ee620ea4SAlan Cox 	VM_OBJECT_ASSERT_WLOCKED(object);
20292e56b64fSKonstantin Belousov 	if (object->type != OBJT_SWAP || count == 0)
20301c7c3c6aSMatthew Dillon 		return;
20311c7c3c6aSMatthew Dillon 
203278f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
2033230869e0SAlan Cox 	last = pindex + count;
2034f425ab8eSKonstantin Belousov 	for (;;) {
2035f425ab8eSKonstantin Belousov 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2036f425ab8eSKonstantin Belousov 		    rounddown(pindex, SWAP_META_PAGES));
2037230869e0SAlan Cox 		if (sb == NULL || sb->p >= last)
20382e56b64fSKonstantin Belousov 			break;
2039230869e0SAlan Cox 		start = pindex > sb->p ? pindex - sb->p : 0;
2040230869e0SAlan Cox 		limit = last - sb->p < SWAP_META_PAGES ? last - sb->p :
2041230869e0SAlan Cox 		    SWAP_META_PAGES;
2042230869e0SAlan Cox 		for (i = start; i < limit; i++) {
2043f425ab8eSKonstantin Belousov 			if (sb->d[i] == SWAPBLK_NONE)
2044f425ab8eSKonstantin Belousov 				continue;
204578f1deefSAlan Cox 			swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
2046230869e0SAlan Cox 			sb->d[i] = SWAPBLK_NONE;
2047230869e0SAlan Cox 		}
2048150d384eSMark Johnston 		pindex = sb->p + SWAP_META_PAGES;
2049230869e0SAlan Cox 		if (swp_pager_swblk_empty(sb, 0, start) &&
2050230869e0SAlan Cox 		    swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) {
2051f425ab8eSKonstantin Belousov 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
2052f425ab8eSKonstantin Belousov 			    sb->p);
2053f425ab8eSKonstantin Belousov 			uma_zfree(swblk_zone, sb);
20541c7c3c6aSMatthew Dillon 		}
20551c7c3c6aSMatthew Dillon 	}
205678f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
20571c7c3c6aSMatthew Dillon }
20581c7c3c6aSMatthew Dillon 
20591c7c3c6aSMatthew Dillon /*
20601c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
20611c7c3c6aSMatthew Dillon  *
20621c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
20631c7c3c6aSMatthew Dillon  *	an object.
20641c7c3c6aSMatthew Dillon  */
20651c7c3c6aSMatthew Dillon static void
20661c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
20671c7c3c6aSMatthew Dillon {
2068f425ab8eSKonstantin Belousov 	struct swblk *sb;
206978f1deefSAlan Cox 	daddr_t n_free, s_free;
2070f425ab8eSKonstantin Belousov 	vm_pindex_t pindex;
207171057cd2SKonstantin Belousov 	int i;
20721c7c3c6aSMatthew Dillon 
207389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
20741c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
20751c7c3c6aSMatthew Dillon 		return;
20761c7c3c6aSMatthew Dillon 
207778f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
2078f425ab8eSKonstantin Belousov 	for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
2079f425ab8eSKonstantin Belousov 	    &object->un_pager.swp.swp_blks, pindex)) != NULL;) {
2080f425ab8eSKonstantin Belousov 		pindex = sb->p + SWAP_META_PAGES;
2081f425ab8eSKonstantin Belousov 		for (i = 0; i < SWAP_META_PAGES; i++) {
2082230869e0SAlan Cox 			if (sb->d[i] == SWAPBLK_NONE)
2083230869e0SAlan Cox 				continue;
208478f1deefSAlan Cox 			swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
20851c7c3c6aSMatthew Dillon 		}
2086f425ab8eSKonstantin Belousov 		SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
2087f425ab8eSKonstantin Belousov 		uma_zfree(swblk_zone, sb);
20881c7c3c6aSMatthew Dillon 	}
208978f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
20901c7c3c6aSMatthew Dillon }
20911c7c3c6aSMatthew Dillon 
20921c7c3c6aSMatthew Dillon /*
20934abca9bbSAlan Cox  * SWP_PAGER_METACTL() -  misc control of swap meta data.
20941c7c3c6aSMatthew Dillon  *
20954abca9bbSAlan Cox  *	This routine is capable of looking up, or removing swapblk
20964abca9bbSAlan Cox  *	assignments in the swap meta data.  It returns the swapblk being
20974abca9bbSAlan Cox  *	looked-up, popped, or SWAPBLK_NONE if the block was invalid.
20981c7c3c6aSMatthew Dillon  *
20991c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
21001c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
21011c7c3c6aSMatthew Dillon  *	busy page.
21021c7c3c6aSMatthew Dillon  *
21034abca9bbSAlan Cox  *	SWM_POP		remove from meta data but do not free it
21041c7c3c6aSMatthew Dillon  */
21051c7c3c6aSMatthew Dillon static daddr_t
21062f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags)
21072f249180SPoul-Henning Kamp {
2108f425ab8eSKonstantin Belousov 	struct swblk *sb;
21094dcc5c2dSMatthew Dillon 	daddr_t r1;
21104dcc5c2dSMatthew Dillon 
21114abca9bbSAlan Cox 	if ((flags & SWM_POP) != 0)
2112ee620ea4SAlan Cox 		VM_OBJECT_ASSERT_WLOCKED(object);
2113ee620ea4SAlan Cox 	else
2114c25673ffSAttilio Rao 		VM_OBJECT_ASSERT_LOCKED(object);
2115ee620ea4SAlan Cox 
21161c7c3c6aSMatthew Dillon 	/*
2117ee620ea4SAlan Cox 	 * The meta data only exists if the object is OBJT_SWAP
21181c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
21191c7c3c6aSMatthew Dillon 	 */
21204dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
21211c7c3c6aSMatthew Dillon 		return (SWAPBLK_NONE);
21221c7c3c6aSMatthew Dillon 
2123f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2124f425ab8eSKonstantin Belousov 	    rounddown(pindex, SWAP_META_PAGES));
2125f425ab8eSKonstantin Belousov 	if (sb == NULL)
2126f425ab8eSKonstantin Belousov 		return (SWAPBLK_NONE);
2127f425ab8eSKonstantin Belousov 	r1 = sb->d[pindex % SWAP_META_PAGES];
2128f425ab8eSKonstantin Belousov 	if (r1 == SWAPBLK_NONE)
2129f425ab8eSKonstantin Belousov 		return (SWAPBLK_NONE);
21304abca9bbSAlan Cox 	if ((flags & SWM_POP) != 0) {
2131f425ab8eSKonstantin Belousov 		sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE;
2132230869e0SAlan Cox 		if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
2133f425ab8eSKonstantin Belousov 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
2134f425ab8eSKonstantin Belousov 			    rounddown(pindex, SWAP_META_PAGES));
2135f425ab8eSKonstantin Belousov 			uma_zfree(swblk_zone, sb);
2136f425ab8eSKonstantin Belousov 		}
2137f425ab8eSKonstantin Belousov 	}
21381c7c3c6aSMatthew Dillon 	return (r1);
21391c7c3c6aSMatthew Dillon }
21401c7c3c6aSMatthew Dillon 
2141e9c0cc15SPoul-Henning Kamp /*
214277d6fd97SKonstantin Belousov  * Returns the least page index which is greater than or equal to the
214377d6fd97SKonstantin Belousov  * parameter pindex and for which there is a swap block allocated.
214477d6fd97SKonstantin Belousov  * Returns object's size if the object's type is not swap or if there
214577d6fd97SKonstantin Belousov  * are no allocated swap blocks for the object after the requested
214677d6fd97SKonstantin Belousov  * pindex.
214777d6fd97SKonstantin Belousov  */
214877d6fd97SKonstantin Belousov vm_pindex_t
214977d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
215077d6fd97SKonstantin Belousov {
2151f425ab8eSKonstantin Belousov 	struct swblk *sb;
2152f425ab8eSKonstantin Belousov 	int i;
215377d6fd97SKonstantin Belousov 
215477d6fd97SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(object);
2155f425ab8eSKonstantin Belousov 	if (object->type != OBJT_SWAP)
215677d6fd97SKonstantin Belousov 		return (object->size);
215777d6fd97SKonstantin Belousov 
2158f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2159f425ab8eSKonstantin Belousov 	    rounddown(pindex, SWAP_META_PAGES));
2160f425ab8eSKonstantin Belousov 	if (sb == NULL)
2161f425ab8eSKonstantin Belousov 		return (object->size);
2162f425ab8eSKonstantin Belousov 	if (sb->p < pindex) {
2163f425ab8eSKonstantin Belousov 		for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) {
2164f425ab8eSKonstantin Belousov 			if (sb->d[i] != SWAPBLK_NONE)
2165f425ab8eSKonstantin Belousov 				return (sb->p + i);
216677d6fd97SKonstantin Belousov 		}
2167f425ab8eSKonstantin Belousov 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2168f425ab8eSKonstantin Belousov 		    roundup(pindex, SWAP_META_PAGES));
2169f425ab8eSKonstantin Belousov 		if (sb == NULL)
2170f425ab8eSKonstantin Belousov 			return (object->size);
217177d6fd97SKonstantin Belousov 	}
2172f425ab8eSKonstantin Belousov 	for (i = 0; i < SWAP_META_PAGES; i++) {
2173f425ab8eSKonstantin Belousov 		if (sb->d[i] != SWAPBLK_NONE)
2174f425ab8eSKonstantin Belousov 			return (sb->p + i);
217577d6fd97SKonstantin Belousov 	}
2176f425ab8eSKonstantin Belousov 
2177f425ab8eSKonstantin Belousov 	/*
2178f425ab8eSKonstantin Belousov 	 * We get here if a swblk is present in the trie but it
2179f425ab8eSKonstantin Belousov 	 * doesn't map any blocks.
2180f425ab8eSKonstantin Belousov 	 */
2181f425ab8eSKonstantin Belousov 	MPASS(0);
2182f425ab8eSKonstantin Belousov 	return (object->size);
218377d6fd97SKonstantin Belousov }
218477d6fd97SKonstantin Belousov 
218577d6fd97SKonstantin Belousov /*
2186e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2187e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2188e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2189e9c0cc15SPoul-Henning Kamp  */
2190e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2191e9c0cc15SPoul-Henning Kamp struct swapon_args {
2192e9c0cc15SPoul-Henning Kamp 	char *name;
2193e9c0cc15SPoul-Henning Kamp };
2194e9c0cc15SPoul-Henning Kamp #endif
2195e9c0cc15SPoul-Henning Kamp 
2196e9c0cc15SPoul-Henning Kamp /*
2197e9c0cc15SPoul-Henning Kamp  * MPSAFE
2198e9c0cc15SPoul-Henning Kamp  */
2199e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2200e9c0cc15SPoul-Henning Kamp int
22018451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap)
2202e9c0cc15SPoul-Henning Kamp {
2203e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2204e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2205e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2206e9c0cc15SPoul-Henning Kamp 	int error;
2207e9c0cc15SPoul-Henning Kamp 
2208acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPON);
2209e9c0cc15SPoul-Henning Kamp 	if (error)
2210acd3428bSRobert Watson 		return (error);
2211e9c0cc15SPoul-Henning Kamp 
221204533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2213e9c0cc15SPoul-Henning Kamp 
2214e9c0cc15SPoul-Henning Kamp 	/*
2215e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2216e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2217e9c0cc15SPoul-Henning Kamp 	 */
2218f425ab8eSKonstantin Belousov 	if (swblk_zone == NULL) {
2219e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2220e9c0cc15SPoul-Henning Kamp 		goto done;
2221e9c0cc15SPoul-Henning Kamp 	}
2222e9c0cc15SPoul-Henning Kamp 
2223d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2224d9135e72SRobert Watson 	    uap->name, td);
2225e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2226e9c0cc15SPoul-Henning Kamp 	if (error)
2227e9c0cc15SPoul-Henning Kamp 		goto done;
2228e9c0cc15SPoul-Henning Kamp 
2229e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2230e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2231e9c0cc15SPoul-Henning Kamp 
223220da9c2eSPoul-Henning Kamp 	if (vn_isdisk(vp, &error)) {
223388ad2d7bSKonstantin Belousov 		error = swapongeom(vp);
223420da9c2eSPoul-Henning Kamp 	} else if (vp->v_type == VREG &&
2235e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
22360359a12eSAttilio Rao 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2237e9c0cc15SPoul-Henning Kamp 		/*
2238e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2239e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2240e9c0cc15SPoul-Henning Kamp 		 */
224159efee01SPoul-Henning Kamp 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2242e9c0cc15SPoul-Henning Kamp 	}
2243e9c0cc15SPoul-Henning Kamp 
2244e9c0cc15SPoul-Henning Kamp 	if (error)
2245e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2246e9c0cc15SPoul-Henning Kamp done:
224704533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2248e9c0cc15SPoul-Henning Kamp 	return (error);
2249e9c0cc15SPoul-Henning Kamp }
2250e9c0cc15SPoul-Henning Kamp 
22513ff863f1SDag-Erling Smørgrav /*
22523ff863f1SDag-Erling Smørgrav  * Check that the total amount of swap currently configured does not
22533ff863f1SDag-Erling Smørgrav  * exceed half the theoretical maximum.  If it does, print a warning
225435872e79SKonstantin Belousov  * message.
22553ff863f1SDag-Erling Smørgrav  */
225635872e79SKonstantin Belousov static void
225735872e79SKonstantin Belousov swapon_check_swzone(void)
22583ff863f1SDag-Erling Smørgrav {
225935872e79SKonstantin Belousov 	unsigned long maxpages, npages;
22603ff863f1SDag-Erling Smørgrav 
2261e8bb589dSMatt Macy 	npages = swap_total;
22623ff863f1SDag-Erling Smørgrav 	/* absolute maximum we can handle assuming 100% efficiency */
2263f425ab8eSKonstantin Belousov 	maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES;
22643ff863f1SDag-Erling Smørgrav 
22653ff863f1SDag-Erling Smørgrav 	/* recommend using no more than half that amount */
22663ff863f1SDag-Erling Smørgrav 	if (npages > maxpages / 2) {
22673ff863f1SDag-Erling Smørgrav 		printf("warning: total configured swap (%lu pages) "
22683ff863f1SDag-Erling Smørgrav 		    "exceeds maximum recommended amount (%lu pages).\n",
22699462305cSSergey Kandaurov 		    npages, maxpages / 2);
22703ff863f1SDag-Erling Smørgrav 		printf("warning: increase kern.maxswzone "
22713ff863f1SDag-Erling Smørgrav 		    "or reduce amount of swap.\n");
22723ff863f1SDag-Erling Smørgrav 	}
22733ff863f1SDag-Erling Smørgrav }
22743ff863f1SDag-Erling Smørgrav 
227559efee01SPoul-Henning Kamp static void
22762cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks,
22772cc718a1SKonstantin Belousov     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2278e9c0cc15SPoul-Henning Kamp {
22792d9974c1SAlan Cox 	struct swdevt *sp, *tsp;
2280e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
22818f60c087SPoul-Henning Kamp 	u_long mblocks;
2282e9c0cc15SPoul-Henning Kamp 
2283e9c0cc15SPoul-Henning Kamp 	/*
2284e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2285e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2286e9c0cc15SPoul-Henning Kamp 	 *
2287e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2288e9c0cc15SPoul-Henning Kamp 	 */
2289e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2290e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2291e9c0cc15SPoul-Henning Kamp 
22926e903bd0SKonstantin Belousov 	/*
22936e903bd0SKonstantin Belousov 	 * If we go beyond this, we get overflows in the radix
22946e903bd0SKonstantin Belousov 	 * tree bitmap code.
22956e903bd0SKonstantin Belousov 	 */
22966e903bd0SKonstantin Belousov 	mblocks = 0x40000000 / BLIST_META_RADIX;
22976e903bd0SKonstantin Belousov 	if (nblks > mblocks) {
22986e903bd0SKonstantin Belousov 		printf(
22996e903bd0SKonstantin Belousov     "WARNING: reducing swap size to maximum of %luMB per unit\n",
23006e903bd0SKonstantin Belousov 		    mblocks / 1024 / 1024 * PAGE_SIZE);
23016e903bd0SKonstantin Belousov 		nblks = mblocks;
23026e903bd0SKonstantin Belousov 	}
23036e903bd0SKonstantin Belousov 
23048f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2305dee34ca4SPoul-Henning Kamp 	sp->sw_vp = vp;
2306dee34ca4SPoul-Henning Kamp 	sp->sw_id = id;
2307f3732fd1SPoul-Henning Kamp 	sp->sw_dev = dev;
2308e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2309e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
231059efee01SPoul-Henning Kamp 	sp->sw_strategy = strategy;
2311dee34ca4SPoul-Henning Kamp 	sp->sw_close = close;
23122cc718a1SKonstantin Belousov 	sp->sw_flags = flags;
2313e9c0cc15SPoul-Henning Kamp 
2314c8c7ad92SKip Macy 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2315e9c0cc15SPoul-Henning Kamp 	/*
2316ef3c5abdSPoul-Henning Kamp 	 * Do not free the first two block in order to avoid overwriting
23178f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2318e9c0cc15SPoul-Henning Kamp 	 */
2319ef3c5abdSPoul-Henning Kamp 	blist_free(sp->sw_blist, 2, nblks - 2);
2320e9c0cc15SPoul-Henning Kamp 
23212d9974c1SAlan Cox 	dvbase = 0;
232220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23232d9974c1SAlan Cox 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
23242d9974c1SAlan Cox 		if (tsp->sw_end >= dvbase) {
23252d9974c1SAlan Cox 			/*
23262d9974c1SAlan Cox 			 * We put one uncovered page between the devices
23272d9974c1SAlan Cox 			 * in order to definitively prevent any cross-device
23282d9974c1SAlan Cox 			 * I/O requests
23292d9974c1SAlan Cox 			 */
23302d9974c1SAlan Cox 			dvbase = tsp->sw_end + 1;
23312d9974c1SAlan Cox 		}
23322d9974c1SAlan Cox 	}
23332d9974c1SAlan Cox 	sp->sw_first = dvbase;
23342d9974c1SAlan Cox 	sp->sw_end = dvbase + nblks;
23358f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
23368f60c087SPoul-Henning Kamp 	nswapdev++;
2337761097c8SAlan Cox 	swap_pager_avail += nblks - 2;
2338e8bb589dSMatt Macy 	swap_total += nblks;
233935872e79SKonstantin Belousov 	swapon_check_swzone();
2340d05bc129SAlan Cox 	swp_sizecheck();
2341d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2342b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapon, sp);
234359efee01SPoul-Henning Kamp }
2344e9c0cc15SPoul-Henning Kamp 
2345e9c0cc15SPoul-Henning Kamp /*
2346e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2347e9c0cc15SPoul-Henning Kamp  *
2348e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2349dee34ca4SPoul-Henning Kamp  *
2350dee34ca4SPoul-Henning Kamp  * XXX: Badly designed system call: it should use a device index
2351dee34ca4SPoul-Henning Kamp  * rather than filename as specification.  We keep sw_vp around
2352dee34ca4SPoul-Henning Kamp  * only to make this work.
2353e9c0cc15SPoul-Henning Kamp  */
2354e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2355e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2356e9c0cc15SPoul-Henning Kamp 	char *name;
2357e9c0cc15SPoul-Henning Kamp };
2358e9c0cc15SPoul-Henning Kamp #endif
2359e9c0cc15SPoul-Henning Kamp 
2360e9c0cc15SPoul-Henning Kamp /*
2361e9c0cc15SPoul-Henning Kamp  * MPSAFE
2362e9c0cc15SPoul-Henning Kamp  */
2363e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2364e9c0cc15SPoul-Henning Kamp int
23658451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap)
2366e9c0cc15SPoul-Henning Kamp {
2367e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2368e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2369e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
23708f60c087SPoul-Henning Kamp 	int error;
2371e9c0cc15SPoul-Henning Kamp 
2372acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPOFF);
2373e9c0cc15SPoul-Henning Kamp 	if (error)
23740909f38aSPawel Jakub Dawidek 		return (error);
2375e9c0cc15SPoul-Henning Kamp 
237604533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2377e9c0cc15SPoul-Henning Kamp 
2378d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2379d9135e72SRobert Watson 	    td);
2380e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2381e9c0cc15SPoul-Henning Kamp 	if (error)
2382e9c0cc15SPoul-Henning Kamp 		goto done;
2383e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2384e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2385e9c0cc15SPoul-Henning Kamp 
238620da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23878f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2388dee34ca4SPoul-Henning Kamp 		if (sp->sw_vp == vp)
23890909f38aSPawel Jakub Dawidek 			break;
2390e9c0cc15SPoul-Henning Kamp 	}
239120da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
23920909f38aSPawel Jakub Dawidek 	if (sp == NULL) {
2393e9c0cc15SPoul-Henning Kamp 		error = EINVAL;
2394e9c0cc15SPoul-Henning Kamp 		goto done;
23950909f38aSPawel Jakub Dawidek 	}
239635918c55SChristian S.J. Peron 	error = swapoff_one(sp, td->td_ucred);
23970909f38aSPawel Jakub Dawidek done:
239804533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
23990909f38aSPawel Jakub Dawidek 	return (error);
24000909f38aSPawel Jakub Dawidek }
24010909f38aSPawel Jakub Dawidek 
24020909f38aSPawel Jakub Dawidek static int
240335918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred)
24040909f38aSPawel Jakub Dawidek {
240503bdd65fSAlan Cox 	u_long nblks;
2406e9c0cc15SPoul-Henning Kamp #ifdef MAC
24070909f38aSPawel Jakub Dawidek 	int error;
2408e9c0cc15SPoul-Henning Kamp #endif
2409e9c0cc15SPoul-Henning Kamp 
241004533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
24110909f38aSPawel Jakub Dawidek #ifdef MAC
2412cb05b60aSAttilio Rao 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
241335918c55SChristian S.J. Peron 	error = mac_system_check_swapoff(cred, sp->sw_vp);
241422db15c0SAttilio Rao 	(void) VOP_UNLOCK(sp->sw_vp, 0);
24150909f38aSPawel Jakub Dawidek 	if (error != 0)
24160909f38aSPawel Jakub Dawidek 		return (error);
24170909f38aSPawel Jakub Dawidek #endif
2418e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2419e9c0cc15SPoul-Henning Kamp 
2420e9c0cc15SPoul-Henning Kamp 	/*
2421e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2422e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2423e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2424e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2425e9c0cc15SPoul-Henning Kamp 	 */
2426e2068d0bSJeff Roberson 	if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat)
24270909f38aSPawel Jakub Dawidek 		return (ENOMEM);
2428e9c0cc15SPoul-Henning Kamp 
2429e9c0cc15SPoul-Henning Kamp 	/*
2430e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2431e9c0cc15SPoul-Henning Kamp 	 */
24322928cef7SAlan Cox 	mtx_lock(&sw_dev_mtx);
2433e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
243403bdd65fSAlan Cox 	swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks);
2435e8bb589dSMatt Macy 	swap_total -= nblks;
24362928cef7SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2437e9c0cc15SPoul-Henning Kamp 
2438e9c0cc15SPoul-Henning Kamp 	/*
2439e9c0cc15SPoul-Henning Kamp 	 * Page in the contents of the device and close it.
2440e9c0cc15SPoul-Henning Kamp 	 */
2441b3fed13eSDavid Schultz 	swap_pager_swapoff(sp);
2442e9c0cc15SPoul-Henning Kamp 
244335918c55SChristian S.J. Peron 	sp->sw_close(curthread, sp);
244420da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
24459e3e3fe5SWarner Losh 	sp->sw_id = NULL;
24468f60c087SPoul-Henning Kamp 	TAILQ_REMOVE(&swtailq, sp, sw_list);
24470676a140SAlan Cox 	nswapdev--;
24487dea2c2eSAlan Cox 	if (nswapdev == 0) {
24497dea2c2eSAlan Cox 		swap_pager_full = 2;
24507dea2c2eSAlan Cox 		swap_pager_almost_full = 1;
24517dea2c2eSAlan Cox 	}
24528f60c087SPoul-Henning Kamp 	if (swdevhd == sp)
24538f60c087SPoul-Henning Kamp 		swdevhd = NULL;
2454d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
24558f60c087SPoul-Henning Kamp 	blist_destroy(sp->sw_blist);
24568f60c087SPoul-Henning Kamp 	free(sp, M_VMPGDATA);
24570909f38aSPawel Jakub Dawidek 	return (0);
24580909f38aSPawel Jakub Dawidek }
2459e9c0cc15SPoul-Henning Kamp 
24600909f38aSPawel Jakub Dawidek void
24610909f38aSPawel Jakub Dawidek swapoff_all(void)
24620909f38aSPawel Jakub Dawidek {
24630909f38aSPawel Jakub Dawidek 	struct swdevt *sp, *spt;
24640909f38aSPawel Jakub Dawidek 	const char *devname;
24650909f38aSPawel Jakub Dawidek 	int error;
24660909f38aSPawel Jakub Dawidek 
246704533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
24680909f38aSPawel Jakub Dawidek 
24690909f38aSPawel Jakub Dawidek 	mtx_lock(&sw_dev_mtx);
24700909f38aSPawel Jakub Dawidek 	TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
24710909f38aSPawel Jakub Dawidek 		mtx_unlock(&sw_dev_mtx);
24720909f38aSPawel Jakub Dawidek 		if (vn_isdisk(sp->sw_vp, NULL))
24737870adb6SEd Schouten 			devname = devtoname(sp->sw_vp->v_rdev);
24740909f38aSPawel Jakub Dawidek 		else
24750909f38aSPawel Jakub Dawidek 			devname = "[file]";
247635918c55SChristian S.J. Peron 		error = swapoff_one(sp, thread0.td_ucred);
24770909f38aSPawel Jakub Dawidek 		if (error != 0) {
24780909f38aSPawel Jakub Dawidek 			printf("Cannot remove swap device %s (error=%d), "
24790909f38aSPawel Jakub Dawidek 			    "skipping.\n", devname, error);
24800909f38aSPawel Jakub Dawidek 		} else if (bootverbose) {
24810909f38aSPawel Jakub Dawidek 			printf("Swap device %s removed.\n", devname);
24820909f38aSPawel Jakub Dawidek 		}
24830909f38aSPawel Jakub Dawidek 		mtx_lock(&sw_dev_mtx);
24840909f38aSPawel Jakub Dawidek 	}
24850909f38aSPawel Jakub Dawidek 	mtx_unlock(&sw_dev_mtx);
24860909f38aSPawel Jakub Dawidek 
248704533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2488e9c0cc15SPoul-Henning Kamp }
2489e9c0cc15SPoul-Henning Kamp 
2490567104a1SPoul-Henning Kamp void
2491567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used)
2492567104a1SPoul-Henning Kamp {
2493567104a1SPoul-Henning Kamp 	struct swdevt *sp;
2494567104a1SPoul-Henning Kamp 
2495567104a1SPoul-Henning Kamp 	*total = 0;
2496567104a1SPoul-Henning Kamp 	*used = 0;
249720da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
24988f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2499567104a1SPoul-Henning Kamp 		*total += sp->sw_nblks;
2500567104a1SPoul-Henning Kamp 		*used += sp->sw_used;
2501567104a1SPoul-Henning Kamp 	}
250220da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2503567104a1SPoul-Henning Kamp }
2504567104a1SPoul-Henning Kamp 
2505dda4f960SKonstantin Belousov int
2506dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2507dda4f960SKonstantin Belousov {
2508dda4f960SKonstantin Belousov 	struct swdevt *sp;
25097870adb6SEd Schouten 	const char *tmp_devname;
2510dda4f960SKonstantin Belousov 	int error, n;
2511dda4f960SKonstantin Belousov 
2512dda4f960SKonstantin Belousov 	n = 0;
2513dda4f960SKonstantin Belousov 	error = ENOENT;
2514dda4f960SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2515dda4f960SKonstantin Belousov 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2516dda4f960SKonstantin Belousov 		if (n != name) {
2517dda4f960SKonstantin Belousov 			n++;
2518dda4f960SKonstantin Belousov 			continue;
2519dda4f960SKonstantin Belousov 		}
2520dda4f960SKonstantin Belousov 		xs->xsw_version = XSWDEV_VERSION;
2521dda4f960SKonstantin Belousov 		xs->xsw_dev = sp->sw_dev;
2522dda4f960SKonstantin Belousov 		xs->xsw_flags = sp->sw_flags;
2523dda4f960SKonstantin Belousov 		xs->xsw_nblks = sp->sw_nblks;
2524dda4f960SKonstantin Belousov 		xs->xsw_used = sp->sw_used;
2525dda4f960SKonstantin Belousov 		if (devname != NULL) {
2526dda4f960SKonstantin Belousov 			if (vn_isdisk(sp->sw_vp, NULL))
25277870adb6SEd Schouten 				tmp_devname = devtoname(sp->sw_vp->v_rdev);
2528dda4f960SKonstantin Belousov 			else
2529dda4f960SKonstantin Belousov 				tmp_devname = "[file]";
2530dda4f960SKonstantin Belousov 			strncpy(devname, tmp_devname, len);
2531dda4f960SKonstantin Belousov 		}
2532dda4f960SKonstantin Belousov 		error = 0;
2533dda4f960SKonstantin Belousov 		break;
2534dda4f960SKonstantin Belousov 	}
2535dda4f960SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2536dda4f960SKonstantin Belousov 	return (error);
2537dda4f960SKonstantin Belousov }
2538dda4f960SKonstantin Belousov 
253969921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
254069921123SKonstantin Belousov #define XSWDEV_VERSION_11	1
254169921123SKonstantin Belousov struct xswdev11 {
254269921123SKonstantin Belousov 	u_int	xsw_version;
254369921123SKonstantin Belousov 	uint32_t xsw_dev;
254469921123SKonstantin Belousov 	int	xsw_flags;
254569921123SKonstantin Belousov 	int	xsw_nblks;
254669921123SKonstantin Belousov 	int     xsw_used;
254769921123SKonstantin Belousov };
254869921123SKonstantin Belousov #endif
254969921123SKonstantin Belousov 
2550f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2551f6d281e8SKonstantin Belousov struct xswdev32 {
2552f6d281e8SKonstantin Belousov 	u_int	xsw_version;
2553f6d281e8SKonstantin Belousov 	u_int	xsw_dev1, xsw_dev2;
2554f6d281e8SKonstantin Belousov 	int	xsw_flags;
2555f6d281e8SKonstantin Belousov 	int	xsw_nblks;
2556f6d281e8SKonstantin Belousov 	int     xsw_used;
2557f6d281e8SKonstantin Belousov };
2558f6d281e8SKonstantin Belousov #endif
2559f6d281e8SKonstantin Belousov 
2560e9c0cc15SPoul-Henning Kamp static int
2561e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2562e9c0cc15SPoul-Henning Kamp {
2563e9c0cc15SPoul-Henning Kamp 	struct xswdev xs;
2564f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2565f6d281e8SKonstantin Belousov 	struct xswdev32 xs32;
2566f6d281e8SKonstantin Belousov #endif
256769921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
256869921123SKonstantin Belousov 	struct xswdev11 xs11;
256969921123SKonstantin Belousov #endif
2570dda4f960SKonstantin Belousov 	int error;
2571e9c0cc15SPoul-Henning Kamp 
2572e9c0cc15SPoul-Henning Kamp 	if (arg2 != 1)			/* name length */
2573e9c0cc15SPoul-Henning Kamp 		return (EINVAL);
2574dda4f960SKonstantin Belousov 	error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2575dda4f960SKonstantin Belousov 	if (error != 0)
2576dda4f960SKonstantin Belousov 		return (error);
2577f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2578f6d281e8SKonstantin Belousov 	if (req->oldlen == sizeof(xs32)) {
2579f6d281e8SKonstantin Belousov 		xs32.xsw_version = XSWDEV_VERSION;
2580f6d281e8SKonstantin Belousov 		xs32.xsw_dev1 = xs.xsw_dev;
2581f6d281e8SKonstantin Belousov 		xs32.xsw_dev2 = xs.xsw_dev >> 32;
2582f6d281e8SKonstantin Belousov 		xs32.xsw_flags = xs.xsw_flags;
2583f6d281e8SKonstantin Belousov 		xs32.xsw_nblks = xs.xsw_nblks;
2584f6d281e8SKonstantin Belousov 		xs32.xsw_used = xs.xsw_used;
2585f6d281e8SKonstantin Belousov 		error = SYSCTL_OUT(req, &xs32, sizeof(xs32));
2586f6d281e8SKonstantin Belousov 		return (error);
2587f6d281e8SKonstantin Belousov 	}
2588f6d281e8SKonstantin Belousov #endif
258969921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
259069921123SKonstantin Belousov 	if (req->oldlen == sizeof(xs11)) {
259169921123SKonstantin Belousov 		xs11.xsw_version = XSWDEV_VERSION_11;
259269921123SKonstantin Belousov 		xs11.xsw_dev = xs.xsw_dev; /* truncation */
259369921123SKonstantin Belousov 		xs11.xsw_flags = xs.xsw_flags;
259469921123SKonstantin Belousov 		xs11.xsw_nblks = xs.xsw_nblks;
259569921123SKonstantin Belousov 		xs11.xsw_used = xs.xsw_used;
259669921123SKonstantin Belousov 		error = SYSCTL_OUT(req, &xs11, sizeof(xs11));
2597f6d281e8SKonstantin Belousov 		return (error);
2598f6d281e8SKonstantin Belousov 	}
259969921123SKonstantin Belousov #endif
2600e9c0cc15SPoul-Henning Kamp 	error = SYSCTL_OUT(req, &xs, sizeof(xs));
2601e9c0cc15SPoul-Henning Kamp 	return (error);
2602e9c0cc15SPoul-Henning Kamp }
2603e9c0cc15SPoul-Henning Kamp 
26048f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2605e9c0cc15SPoul-Henning Kamp     "Number of swap devices");
26064c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE,
26074c36e917SKonstantin Belousov     sysctl_vm_swap_info,
2608e9c0cc15SPoul-Henning Kamp     "Swap statistics by device");
2609ec38b344SPoul-Henning Kamp 
2610ec38b344SPoul-Henning Kamp /*
2611f425ab8eSKonstantin Belousov  * Count the approximate swap usage in pages for a vmspace.  The
2612f425ab8eSKonstantin Belousov  * shadowed or not yet copied on write swap blocks are not accounted.
2613ec38b344SPoul-Henning Kamp  * The map must be locked.
2614ec38b344SPoul-Henning Kamp  */
26152860553aSRebecca Cran long
2616ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2617ec38b344SPoul-Henning Kamp {
261865d8409cSRebecca Cran 	vm_map_t map;
2619ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
262065d8409cSRebecca Cran 	vm_object_t object;
2621f425ab8eSKonstantin Belousov 	struct swblk *sb;
2622f425ab8eSKonstantin Belousov 	vm_pindex_t e, pi;
2623f425ab8eSKonstantin Belousov 	long count;
2624f425ab8eSKonstantin Belousov 	int i;
262565d8409cSRebecca Cran 
262665d8409cSRebecca Cran 	map = &vmspace->vm_map;
262765d8409cSRebecca Cran 	count = 0;
2628ec38b344SPoul-Henning Kamp 
2629ec38b344SPoul-Henning Kamp 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2630f425ab8eSKonstantin Belousov 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2631f425ab8eSKonstantin Belousov 			continue;
2632f425ab8eSKonstantin Belousov 		object = cur->object.vm_object;
2633f425ab8eSKonstantin Belousov 		if (object == NULL || object->type != OBJT_SWAP)
2634f425ab8eSKonstantin Belousov 			continue;
2635f425ab8eSKonstantin Belousov 		VM_OBJECT_RLOCK(object);
2636f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
2637f425ab8eSKonstantin Belousov 			goto unlock;
2638f425ab8eSKonstantin Belousov 		pi = OFF_TO_IDX(cur->offset);
2639f425ab8eSKonstantin Belousov 		e = pi + OFF_TO_IDX(cur->end - cur->start);
2640f425ab8eSKonstantin Belousov 		for (;; pi = sb->p + SWAP_META_PAGES) {
2641f425ab8eSKonstantin Belousov 			sb = SWAP_PCTRIE_LOOKUP_GE(
2642f425ab8eSKonstantin Belousov 			    &object->un_pager.swp.swp_blks, pi);
2643f425ab8eSKonstantin Belousov 			if (sb == NULL || sb->p >= e)
2644f425ab8eSKonstantin Belousov 				break;
2645f425ab8eSKonstantin Belousov 			for (i = 0; i < SWAP_META_PAGES; i++) {
2646f425ab8eSKonstantin Belousov 				if (sb->p + i < e &&
2647f425ab8eSKonstantin Belousov 				    sb->d[i] != SWAPBLK_NONE)
2648f425ab8eSKonstantin Belousov 					count++;
2649ec38b344SPoul-Henning Kamp 			}
2650ec38b344SPoul-Henning Kamp 		}
2651f425ab8eSKonstantin Belousov unlock:
2652f425ab8eSKonstantin Belousov 		VM_OBJECT_RUNLOCK(object);
2653ec38b344SPoul-Henning Kamp 	}
2654ec38b344SPoul-Henning Kamp 	return (count);
2655ec38b344SPoul-Henning Kamp }
2656dee34ca4SPoul-Henning Kamp 
2657dee34ca4SPoul-Henning Kamp /*
2658dee34ca4SPoul-Henning Kamp  * GEOM backend
2659dee34ca4SPoul-Henning Kamp  *
2660dee34ca4SPoul-Henning Kamp  * Swapping onto disk devices.
2661dee34ca4SPoul-Henning Kamp  *
2662dee34ca4SPoul-Henning Kamp  */
2663dee34ca4SPoul-Henning Kamp 
26645721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan;
26655721c9c7SPoul-Henning Kamp 
2666dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = {
2667dee34ca4SPoul-Henning Kamp 	.name = "SWAP",
26685721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
26695721c9c7SPoul-Henning Kamp 	.orphan = swapgeom_orphan,
2670dee34ca4SPoul-Henning Kamp };
2671dee34ca4SPoul-Henning Kamp 
2672dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class);
2673dee34ca4SPoul-Henning Kamp 
2674dee34ca4SPoul-Henning Kamp 
2675dee34ca4SPoul-Henning Kamp static void
26763398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags)
26773398491bSAlexander Motin {
26783398491bSAlexander Motin 	struct g_consumer *cp;
26793398491bSAlexander Motin 
26803398491bSAlexander Motin 	cp = arg;
26813398491bSAlexander Motin 	g_access(cp, -1, -1, 0);
26823398491bSAlexander Motin 	g_detach(cp);
26833398491bSAlexander Motin 	g_destroy_consumer(cp);
26843398491bSAlexander Motin }
26853398491bSAlexander Motin 
26869e3e3fe5SWarner Losh /*
26879e3e3fe5SWarner Losh  * Add a reference to the g_consumer for an inflight transaction.
26889e3e3fe5SWarner Losh  */
26899e3e3fe5SWarner Losh static void
26909e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp)
26919e3e3fe5SWarner Losh {
26929e3e3fe5SWarner Losh 
26939e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
26949e3e3fe5SWarner Losh 	cp->index++;
26959e3e3fe5SWarner Losh }
26969e3e3fe5SWarner Losh 
26979e3e3fe5SWarner Losh /*
26980c657d22SKonstantin Belousov  * Remove a reference from the g_consumer.  Post a close event if all
26990c657d22SKonstantin Belousov  * references go away, since the function might be called from the
27000c657d22SKonstantin Belousov  * biodone context.
27019e3e3fe5SWarner Losh  */
27029e3e3fe5SWarner Losh static void
27039e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
27049e3e3fe5SWarner Losh {
27059e3e3fe5SWarner Losh 
27069e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
27079e3e3fe5SWarner Losh 	cp->index--;
27089e3e3fe5SWarner Losh 	if (cp->index == 0) {
27099e3e3fe5SWarner Losh 		if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
27109e3e3fe5SWarner Losh 			sp->sw_id = NULL;
27119e3e3fe5SWarner Losh 	}
27129e3e3fe5SWarner Losh }
27139e3e3fe5SWarner Losh 
27143398491bSAlexander Motin static void
2715dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2)
2716dee34ca4SPoul-Henning Kamp {
27173398491bSAlexander Motin 	struct swdevt *sp;
2718dee34ca4SPoul-Henning Kamp 	struct buf *bp;
27193398491bSAlexander Motin 	struct g_consumer *cp;
2720dee34ca4SPoul-Henning Kamp 
2721dee34ca4SPoul-Henning Kamp 	bp = bp2->bio_caller2;
27223398491bSAlexander Motin 	cp = bp2->bio_from;
2723c5d3d25eSPoul-Henning Kamp 	bp->b_ioflags = bp2->bio_flags;
2724dee34ca4SPoul-Henning Kamp 	if (bp2->bio_error)
2725dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2726c5d3d25eSPoul-Henning Kamp 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2727c5d3d25eSPoul-Henning Kamp 	bp->b_error = bp2->bio_error;
2728756a5412SGleb Smirnoff 	bp->b_caller1 = NULL;
2729dee34ca4SPoul-Henning Kamp 	bufdone(bp);
27303398491bSAlexander Motin 	sp = bp2->bio_caller1;
27319e3e3fe5SWarner Losh 	mtx_lock(&sw_dev_mtx);
27329e3e3fe5SWarner Losh 	swapgeom_release(cp, sp);
27333398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
2734dee34ca4SPoul-Henning Kamp 	g_destroy_bio(bp2);
2735dee34ca4SPoul-Henning Kamp }
2736dee34ca4SPoul-Henning Kamp 
2737dee34ca4SPoul-Henning Kamp static void
2738dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2739dee34ca4SPoul-Henning Kamp {
2740dee34ca4SPoul-Henning Kamp 	struct bio *bio;
2741dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2742dee34ca4SPoul-Henning Kamp 
27433398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
2744dee34ca4SPoul-Henning Kamp 	cp = sp->sw_id;
2745dee34ca4SPoul-Henning Kamp 	if (cp == NULL) {
27463398491bSAlexander Motin 		mtx_unlock(&sw_dev_mtx);
2747dee34ca4SPoul-Henning Kamp 		bp->b_error = ENXIO;
2748dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2749dee34ca4SPoul-Henning Kamp 		bufdone(bp);
2750dee34ca4SPoul-Henning Kamp 		return;
2751dee34ca4SPoul-Henning Kamp 	}
27529e3e3fe5SWarner Losh 	swapgeom_acquire(cp);
27533398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
275411041003SKonstantin Belousov 	if (bp->b_iocmd == BIO_WRITE)
275511041003SKonstantin Belousov 		bio = g_new_bio();
275611041003SKonstantin Belousov 	else
27574f8205e5SPoul-Henning Kamp 		bio = g_alloc_bio();
27584f8205e5SPoul-Henning Kamp 	if (bio == NULL) {
27599e3e3fe5SWarner Losh 		mtx_lock(&sw_dev_mtx);
27609e3e3fe5SWarner Losh 		swapgeom_release(cp, sp);
27619e3e3fe5SWarner Losh 		mtx_unlock(&sw_dev_mtx);
27623e5b6861SPoul-Henning Kamp 		bp->b_error = ENOMEM;
27633e5b6861SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
27640b208315SEdward Tomasz Napierala 		printf("swap_pager: cannot allocate bio\n");
27653e5b6861SPoul-Henning Kamp 		bufdone(bp);
27663e5b6861SPoul-Henning Kamp 		return;
27673e5b6861SPoul-Henning Kamp 	}
276811041003SKonstantin Belousov 
2769756a5412SGleb Smirnoff 	bp->b_caller1 = bio;
27703398491bSAlexander Motin 	bio->bio_caller1 = sp;
2771dee34ca4SPoul-Henning Kamp 	bio->bio_caller2 = bp;
2772c5d3d25eSPoul-Henning Kamp 	bio->bio_cmd = bp->b_iocmd;
2773dee34ca4SPoul-Henning Kamp 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2774dee34ca4SPoul-Henning Kamp 	bio->bio_length = bp->b_bcount;
2775dee34ca4SPoul-Henning Kamp 	bio->bio_done = swapgeom_done;
2776fade8dd7SJeff Roberson 	if (!buf_mapped(bp)) {
27772cc718a1SKonstantin Belousov 		bio->bio_ma = bp->b_pages;
27782cc718a1SKonstantin Belousov 		bio->bio_data = unmapped_buf;
27792cc718a1SKonstantin Belousov 		bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
27802cc718a1SKonstantin Belousov 		bio->bio_ma_n = bp->b_npages;
27812cc718a1SKonstantin Belousov 		bio->bio_flags |= BIO_UNMAPPED;
27822cc718a1SKonstantin Belousov 	} else {
27832cc718a1SKonstantin Belousov 		bio->bio_data = bp->b_data;
27842cc718a1SKonstantin Belousov 		bio->bio_ma = NULL;
27852cc718a1SKonstantin Belousov 	}
2786dee34ca4SPoul-Henning Kamp 	g_io_request(bio, cp);
2787dee34ca4SPoul-Henning Kamp 	return;
2788dee34ca4SPoul-Henning Kamp }
2789dee34ca4SPoul-Henning Kamp 
2790dee34ca4SPoul-Henning Kamp static void
2791dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp)
2792dee34ca4SPoul-Henning Kamp {
2793dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
27943398491bSAlexander Motin 	int destroy;
2795dee34ca4SPoul-Henning Kamp 
2796dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
27973398491bSAlexander Motin 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
27983398491bSAlexander Motin 		if (sp->sw_id == cp) {
27998f12d83aSAlexander Motin 			sp->sw_flags |= SW_CLOSING;
28003398491bSAlexander Motin 			break;
2801dee34ca4SPoul-Henning Kamp 		}
28023398491bSAlexander Motin 	}
28039e3e3fe5SWarner Losh 	/*
28049e3e3fe5SWarner Losh 	 * Drop reference we were created with. Do directly since we're in a
28059e3e3fe5SWarner Losh 	 * special context where we don't have to queue the call to
28069e3e3fe5SWarner Losh 	 * swapgeom_close_ev().
28079e3e3fe5SWarner Losh 	 */
28089e3e3fe5SWarner Losh 	cp->index--;
28093398491bSAlexander Motin 	destroy = ((sp != NULL) && (cp->index == 0));
28103398491bSAlexander Motin 	if (destroy)
28113398491bSAlexander Motin 		sp->sw_id = NULL;
28123398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
28133398491bSAlexander Motin 	if (destroy)
28143398491bSAlexander Motin 		swapgeom_close_ev(cp, 0);
2815dee34ca4SPoul-Henning Kamp }
2816dee34ca4SPoul-Henning Kamp 
2817dee34ca4SPoul-Henning Kamp static void
2818dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw)
2819dee34ca4SPoul-Henning Kamp {
28203398491bSAlexander Motin 	struct g_consumer *cp;
2821dee34ca4SPoul-Henning Kamp 
28223398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
28233398491bSAlexander Motin 	cp = sw->sw_id;
28243398491bSAlexander Motin 	sw->sw_id = NULL;
28253398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
28260c657d22SKonstantin Belousov 
28270c657d22SKonstantin Belousov 	/*
28280c657d22SKonstantin Belousov 	 * swapgeom_close() may be called from the biodone context,
28290c657d22SKonstantin Belousov 	 * where we cannot perform topology changes.  Delegate the
28300c657d22SKonstantin Belousov 	 * work to the events thread.
28310c657d22SKonstantin Belousov 	 */
28323398491bSAlexander Motin 	if (cp != NULL)
28333398491bSAlexander Motin 		g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2834dee34ca4SPoul-Henning Kamp }
2835dee34ca4SPoul-Henning Kamp 
283688ad2d7bSKonstantin Belousov static int
283788ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp)
2838dee34ca4SPoul-Henning Kamp {
2839dee34ca4SPoul-Henning Kamp 	struct g_provider *pp;
2840dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2841dee34ca4SPoul-Henning Kamp 	static struct g_geom *gp;
2842dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2843dee34ca4SPoul-Henning Kamp 	u_long nblks;
2844dee34ca4SPoul-Henning Kamp 	int error;
2845dee34ca4SPoul-Henning Kamp 
284688ad2d7bSKonstantin Belousov 	pp = g_dev_getprovider(dev);
284788ad2d7bSKonstantin Belousov 	if (pp == NULL)
284888ad2d7bSKonstantin Belousov 		return (ENODEV);
2849dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2850dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2851dee34ca4SPoul-Henning Kamp 		cp = sp->sw_id;
2852dee34ca4SPoul-Henning Kamp 		if (cp != NULL && cp->provider == pp) {
2853dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
285488ad2d7bSKonstantin Belousov 			return (EBUSY);
2855dee34ca4SPoul-Henning Kamp 		}
2856dee34ca4SPoul-Henning Kamp 	}
2857dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
28585721c9c7SPoul-Henning Kamp 	if (gp == NULL)
285902c62349SJaakko Heinonen 		gp = g_new_geomf(&g_swap_class, "swap");
2860dee34ca4SPoul-Henning Kamp 	cp = g_new_consumer(gp);
28619e3e3fe5SWarner Losh 	cp->index = 1;	/* Number of active I/Os, plus one for being active. */
28629e3e3fe5SWarner Losh 	cp->flags |=  G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2863dee34ca4SPoul-Henning Kamp 	g_attach(cp, pp);
2864afeb65e6SPoul-Henning Kamp 	/*
2865afeb65e6SPoul-Henning Kamp 	 * XXX: Every time you think you can improve the margin for
2866afeb65e6SPoul-Henning Kamp 	 * footshooting, somebody depends on the ability to do so:
2867afeb65e6SPoul-Henning Kamp 	 * savecore(8) wants to write to our swapdev so we cannot
2868afeb65e6SPoul-Henning Kamp 	 * set an exclusive count :-(
2869afeb65e6SPoul-Henning Kamp 	 */
2870d2bae332SPoul-Henning Kamp 	error = g_access(cp, 1, 1, 0);
287188ad2d7bSKonstantin Belousov 	if (error != 0) {
2872dee34ca4SPoul-Henning Kamp 		g_detach(cp);
2873dee34ca4SPoul-Henning Kamp 		g_destroy_consumer(cp);
287488ad2d7bSKonstantin Belousov 		return (error);
2875dee34ca4SPoul-Henning Kamp 	}
2876dee34ca4SPoul-Henning Kamp 	nblks = pp->mediasize / DEV_BSIZE;
287788ad2d7bSKonstantin Belousov 	swaponsomething(vp, cp, nblks, swapgeom_strategy,
287888ad2d7bSKonstantin Belousov 	    swapgeom_close, dev2udev(dev),
28792cc718a1SKonstantin Belousov 	    (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
288088ad2d7bSKonstantin Belousov 	return (0);
2881dee34ca4SPoul-Henning Kamp }
2882dee34ca4SPoul-Henning Kamp 
2883dee34ca4SPoul-Henning Kamp static int
288488ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp)
2885dee34ca4SPoul-Henning Kamp {
2886dee34ca4SPoul-Henning Kamp 	int error;
2887dee34ca4SPoul-Henning Kamp 
2888cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
288988ad2d7bSKonstantin Belousov 	if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) {
289088ad2d7bSKonstantin Belousov 		error = ENOENT;
289188ad2d7bSKonstantin Belousov 	} else {
289288ad2d7bSKonstantin Belousov 		g_topology_lock();
289388ad2d7bSKonstantin Belousov 		error = swapongeom_locked(vp->v_rdev, vp);
289488ad2d7bSKonstantin Belousov 		g_topology_unlock();
289588ad2d7bSKonstantin Belousov 	}
289622db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
2897dee34ca4SPoul-Henning Kamp 	return (error);
2898dee34ca4SPoul-Henning Kamp }
2899dee34ca4SPoul-Henning Kamp 
2900dee34ca4SPoul-Henning Kamp /*
2901dee34ca4SPoul-Henning Kamp  * VNODE backend
2902dee34ca4SPoul-Henning Kamp  *
2903dee34ca4SPoul-Henning Kamp  * This is used mainly for network filesystem (read: probably only tested
2904dee34ca4SPoul-Henning Kamp  * with NFS) swapfiles.
2905dee34ca4SPoul-Henning Kamp  *
2906dee34ca4SPoul-Henning Kamp  */
2907dee34ca4SPoul-Henning Kamp 
2908dee34ca4SPoul-Henning Kamp static void
2909dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp)
2910dee34ca4SPoul-Henning Kamp {
2911494eb176SPoul-Henning Kamp 	struct vnode *vp2;
2912dee34ca4SPoul-Henning Kamp 
2913dee34ca4SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2914dee34ca4SPoul-Henning Kamp 
2915dee34ca4SPoul-Henning Kamp 	vp2 = sp->sw_id;
2916dee34ca4SPoul-Henning Kamp 	vhold(vp2);
2917dee34ca4SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
29183cfc7651SOlivier Houchard 		if (bp->b_bufobj)
2919494eb176SPoul-Henning Kamp 			bufobj_wdrop(bp->b_bufobj);
2920a76d8f4eSPoul-Henning Kamp 		bufobj_wref(&vp2->v_bufobj);
2921dee34ca4SPoul-Henning Kamp 	}
29223cfc7651SOlivier Houchard 	if (bp->b_bufobj != &vp2->v_bufobj)
29233cfc7651SOlivier Houchard 		bp->b_bufobj = &vp2->v_bufobj;
2924dee34ca4SPoul-Henning Kamp 	bp->b_vp = vp2;
29252c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
2926b792bebeSPoul-Henning Kamp 	bstrategy(bp);
2927dee34ca4SPoul-Henning Kamp 	return;
2928dee34ca4SPoul-Henning Kamp }
2929dee34ca4SPoul-Henning Kamp 
2930dee34ca4SPoul-Henning Kamp static void
2931dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp)
2932dee34ca4SPoul-Henning Kamp {
2933dee34ca4SPoul-Henning Kamp 
2934dee34ca4SPoul-Henning Kamp 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2935dee34ca4SPoul-Henning Kamp 	vrele(sp->sw_vp);
2936dee34ca4SPoul-Henning Kamp }
2937dee34ca4SPoul-Henning Kamp 
2938dee34ca4SPoul-Henning Kamp 
2939dee34ca4SPoul-Henning Kamp static int
2940dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2941dee34ca4SPoul-Henning Kamp {
2942dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2943dee34ca4SPoul-Henning Kamp 	int error;
2944dee34ca4SPoul-Henning Kamp 
2945dee34ca4SPoul-Henning Kamp 	if (nblks == 0)
2946dee34ca4SPoul-Henning Kamp 		return (ENXIO);
2947dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2948dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2949dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == vp) {
2950dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2951dee34ca4SPoul-Henning Kamp 			return (EBUSY);
2952dee34ca4SPoul-Henning Kamp 		}
2953dee34ca4SPoul-Henning Kamp 	}
2954dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2955dee34ca4SPoul-Henning Kamp 
2956cb05b60aSAttilio Rao 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2957dee34ca4SPoul-Henning Kamp #ifdef MAC
295830d239bcSRobert Watson 	error = mac_system_check_swapon(td->td_ucred, vp);
2959dee34ca4SPoul-Henning Kamp 	if (error == 0)
2960dee34ca4SPoul-Henning Kamp #endif
29619e223287SKonstantin Belousov 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
296222db15c0SAttilio Rao 	(void) VOP_UNLOCK(vp, 0);
2963dee34ca4SPoul-Henning Kamp 	if (error)
2964dee34ca4SPoul-Henning Kamp 		return (error);
2965dee34ca4SPoul-Henning Kamp 
2966dee34ca4SPoul-Henning Kamp 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
29672cc718a1SKonstantin Belousov 	    NODEV, 0);
2968dee34ca4SPoul-Henning Kamp 	return (0);
2969dee34ca4SPoul-Henning Kamp }
297089c241d1SGleb Smirnoff 
297189c241d1SGleb Smirnoff static int
297289c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
297389c241d1SGleb Smirnoff {
297489c241d1SGleb Smirnoff 	int error, new, n;
297589c241d1SGleb Smirnoff 
297689c241d1SGleb Smirnoff 	new = nsw_wcount_async_max;
297789c241d1SGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
297889c241d1SGleb Smirnoff 	if (error != 0 || req->newptr == NULL)
297989c241d1SGleb Smirnoff 		return (error);
298089c241d1SGleb Smirnoff 
298189c241d1SGleb Smirnoff 	if (new > nswbuf / 2 || new < 1)
298289c241d1SGleb Smirnoff 		return (EINVAL);
298389c241d1SGleb Smirnoff 
2984756a5412SGleb Smirnoff 	mtx_lock(&swbuf_mtx);
298589c241d1SGleb Smirnoff 	while (nsw_wcount_async_max != new) {
298689c241d1SGleb Smirnoff 		/*
298789c241d1SGleb Smirnoff 		 * Adjust difference.  If the current async count is too low,
298889c241d1SGleb Smirnoff 		 * we will need to sqeeze our update slowly in.  Sleep with a
298989c241d1SGleb Smirnoff 		 * higher priority than getpbuf() to finish faster.
299089c241d1SGleb Smirnoff 		 */
299189c241d1SGleb Smirnoff 		n = new - nsw_wcount_async_max;
299289c241d1SGleb Smirnoff 		if (nsw_wcount_async + n >= 0) {
299389c241d1SGleb Smirnoff 			nsw_wcount_async += n;
299489c241d1SGleb Smirnoff 			nsw_wcount_async_max += n;
299589c241d1SGleb Smirnoff 			wakeup(&nsw_wcount_async);
299689c241d1SGleb Smirnoff 		} else {
299789c241d1SGleb Smirnoff 			nsw_wcount_async_max -= nsw_wcount_async;
299889c241d1SGleb Smirnoff 			nsw_wcount_async = 0;
2999756a5412SGleb Smirnoff 			msleep(&nsw_wcount_async, &swbuf_mtx, PSWP,
300089c241d1SGleb Smirnoff 			    "swpsysctl", 0);
300189c241d1SGleb Smirnoff 		}
300289c241d1SGleb Smirnoff 	}
3003756a5412SGleb Smirnoff 	mtx_unlock(&swbuf_mtx);
300489c241d1SGleb Smirnoff 
300589c241d1SGleb Smirnoff 	return (0);
300689c241d1SGleb Smirnoff }
3007