xref: /freebsd/sys/vm/swap_pager.c (revision a80817786433b8c78023e5086809bd673e704fd1)
160727d8bSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
41c7c3c6aSMatthew Dillon  * Copyright (c) 1998 Matthew Dillon,
526f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
6df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
7e9c0cc15SPoul-Henning Kamp  * Copyright (c) 1982, 1986, 1989, 1993
8df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
12df8bae1dSRodney W. Grimes  * Science Department.
13df8bae1dSRodney W. Grimes  *
14df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
15df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
16df8bae1dSRodney W. Grimes  * are met:
17df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
19df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
20df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
21df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
22df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
235929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
24df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
25df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
26df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
27df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
28df8bae1dSRodney W. Grimes  *    without specific prior written permission.
29df8bae1dSRodney W. Grimes  *
30df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
41df8bae1dSRodney W. Grimes  *
421c7c3c6aSMatthew Dillon  *				New Swap System
431c7c3c6aSMatthew Dillon  *				Matthew Dillon
441c7c3c6aSMatthew Dillon  *
451c7c3c6aSMatthew Dillon  * Radix Bitmap 'blists'.
461c7c3c6aSMatthew Dillon  *
471c7c3c6aSMatthew Dillon  *	- The new swapper uses the new radix bitmap code.  This should scale
481c7c3c6aSMatthew Dillon  *	  to arbitrarily small or arbitrarily large swap spaces and an almost
491c7c3c6aSMatthew Dillon  *	  arbitrary degree of fragmentation.
501c7c3c6aSMatthew Dillon  *
511c7c3c6aSMatthew Dillon  * Features:
521c7c3c6aSMatthew Dillon  *
531c7c3c6aSMatthew Dillon  *	- on the fly reallocation of swap during putpages.  The new system
541c7c3c6aSMatthew Dillon  *	  does not try to keep previously allocated swap blocks for dirty
551c7c3c6aSMatthew Dillon  *	  pages.
561c7c3c6aSMatthew Dillon  *
571c7c3c6aSMatthew Dillon  *	- on the fly deallocation of swap
581c7c3c6aSMatthew Dillon  *
591c7c3c6aSMatthew Dillon  *	- No more garbage collection required.  Unnecessarily allocated swap
601c7c3c6aSMatthew Dillon  *	  blocks only exist for dirty vm_page_t's now and these are already
611c7c3c6aSMatthew Dillon  *	  cycled (in a high-load system) by the pager.  We also do on-the-fly
621c7c3c6aSMatthew Dillon  *	  removal of invalidated swap blocks when a page is destroyed
631c7c3c6aSMatthew Dillon  *	  or renamed.
641c7c3c6aSMatthew Dillon  *
65df8bae1dSRodney W. Grimes  * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
66df8bae1dSRodney W. Grimes  *
67df8bae1dSRodney W. Grimes  *	@(#)swap_pager.c	8.9 (Berkeley) 3/21/94
68e9c0cc15SPoul-Henning Kamp  *	@(#)vm_swap.c	8.5 (Berkeley) 2/17/94
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71874651b1SDavid E. O'Brien #include <sys/cdefs.h>
72874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
73874651b1SDavid E. O'Brien 
74e9c0cc15SPoul-Henning Kamp #include "opt_vm.h"
75e9c0cc15SPoul-Henning Kamp 
76df8bae1dSRodney W. Grimes #include <sys/param.h>
779626b608SPoul-Henning Kamp #include <sys/bio.h>
78e2e050c8SConrad Meyer #include <sys/blist.h>
79df8bae1dSRodney W. Grimes #include <sys/buf.h>
80e2e050c8SConrad Meyer #include <sys/conf.h>
81e9c0cc15SPoul-Henning Kamp #include <sys/disk.h>
82504f5e29SDoug Moore #include <sys/disklabel.h>
83e2e050c8SConrad Meyer #include <sys/eventhandler.h>
84e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h>
85e2e050c8SConrad Meyer #include <sys/lock.h>
86e2e050c8SConrad Meyer #include <sys/kernel.h>
87e9c0cc15SPoul-Henning Kamp #include <sys/mount.h>
88e9c0cc15SPoul-Henning Kamp #include <sys/namei.h>
89df8bae1dSRodney W. Grimes #include <sys/malloc.h>
90f425ab8eSKonstantin Belousov #include <sys/pctrie.h>
91e2e050c8SConrad Meyer #include <sys/priv.h>
92e2e050c8SConrad Meyer #include <sys/proc.h>
931ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
943364c323SKonstantin Belousov #include <sys/resource.h>
953364c323SKonstantin Belousov #include <sys/resourcevar.h>
9689f6b863SAttilio Rao #include <sys/rwlock.h>
97d027ed2eSAlan Cox #include <sys/sbuf.h>
98327f4e83SMatthew Dillon #include <sys/sysctl.h>
99e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
100e2e050c8SConrad Meyer #include <sys/systm.h>
1010cddd8f0SMatthew Dillon #include <sys/sx.h>
102936524aaSMatthew Dillon #include <sys/vmmeter.h>
103e2e050c8SConrad Meyer #include <sys/vnode.h>
104df8bae1dSRodney W. Grimes 
105aed55708SRobert Watson #include <security/mac/mac_framework.h>
106aed55708SRobert Watson 
107df8bae1dSRodney W. Grimes #include <vm/vm.h>
10821cd6e62SSeigo Tanimura #include <vm/pmap.h>
10921cd6e62SSeigo Tanimura #include <vm/vm_map.h>
11021cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
111efeaf95aSDavid Greenman #include <vm/vm_object.h>
112df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
113efeaf95aSDavid Greenman #include <vm/vm_pager.h>
114df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
115e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
116df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
117efeaf95aSDavid Greenman #include <vm/vm_extern.h>
118670d17b5SJeff Roberson #include <vm/uma.h>
119df8bae1dSRodney W. Grimes 
120dee34ca4SPoul-Henning Kamp #include <geom/geom.h>
121dee34ca4SPoul-Henning Kamp 
122ec38b344SPoul-Henning Kamp /*
123064650c1SAlan Cox  * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64.
124064650c1SAlan Cox  * The 64-page limit is due to the radix code (kern/subr_blist.c).
125ec38b344SPoul-Henning Kamp  */
126ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
127e2241590SAlan Cox #define	MAX_PAGEOUT_CLUSTER	32
128ec38b344SPoul-Henning Kamp #endif
129ec38b344SPoul-Henning Kamp 
130ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
131ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
132ec38b344SPoul-Henning Kamp #endif
133ec38b344SPoul-Henning Kamp 
134f425ab8eSKonstantin Belousov #define	SWAP_META_PAGES		PCTRIE_COUNT
135ec38b344SPoul-Henning Kamp 
136f425ab8eSKonstantin Belousov /*
137f425ab8eSKonstantin Belousov  * A swblk structure maps each page index within a
138f425ab8eSKonstantin Belousov  * SWAP_META_PAGES-aligned and sized range to the address of an
139f425ab8eSKonstantin Belousov  * on-disk swap block (or SWAPBLK_NONE). The collection of these
140f425ab8eSKonstantin Belousov  * mappings for an entire vm object is implemented as a pc-trie.
141f425ab8eSKonstantin Belousov  */
142f425ab8eSKonstantin Belousov struct swblk {
143f425ab8eSKonstantin Belousov 	vm_pindex_t	p;
144f425ab8eSKonstantin Belousov 	daddr_t		d[SWAP_META_PAGES];
145e9c0cc15SPoul-Henning Kamp };
146e9c0cc15SPoul-Henning Kamp 
1472c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
14820da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx;
1498f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1508f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1518f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1528f60c087SPoul-Henning Kamp int swap_pager_avail;
15304533e1eSKonstantin Belousov static struct sx swdev_syscall_lock;	/* serialize swap(on|off) */
154e9c0cc15SPoul-Henning Kamp 
155e8bb589dSMatt Macy static u_long swap_reserved;
156e8bb589dSMatt Macy static u_long swap_total;
157e8bb589dSMatt Macy static int sysctl_page_shift(SYSCTL_HANDLER_ARGS);
158*a8081778SJeff Roberson 
159*a8081778SJeff Roberson static SYSCTL_NODE(_vm_stats, OID_AUTO, swap, CTLFLAG_RD, 0, "VM swap stats");
160*a8081778SJeff Roberson 
161e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_reserved, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
162e8bb589dSMatt Macy     &swap_reserved, 0, sysctl_page_shift, "A",
1638a9c731fSIvan Voras     "Amount of swap storage needed to back all allocated anonymous memory.");
164e8bb589dSMatt Macy SYSCTL_PROC(_vm, OID_AUTO, swap_total, CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
165e8bb589dSMatt Macy     &swap_total, 0, sysctl_page_shift, "A",
166e8bb589dSMatt Macy     "Total amount of available swap storage.");
167e8bb589dSMatt Macy 
1683364c323SKonstantin Belousov static int overcommit = 0;
169be7d4ac5SEdward Tomasz Napierala SYSCTL_INT(_vm, VM_OVERCOMMIT, overcommit, CTLFLAG_RW, &overcommit, 0,
1708a9c731fSIvan Voras     "Configure virtual memory overcommit behavior. See tuning(7) "
1718a9c731fSIvan Voras     "for details.");
17261203277SDag-Erling Smørgrav static unsigned long swzone;
17361203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
17461203277SDag-Erling Smørgrav     "Actual size of swap metadata zone");
17561203277SDag-Erling Smørgrav static unsigned long swap_maxpages;
17661203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
17761203277SDag-Erling Smørgrav     "Maximum amount of swap supported");
1783364c323SKonstantin Belousov 
179*a8081778SJeff Roberson static counter_u64_t swap_free_deferred;
180*a8081778SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_deferred,
181*a8081778SJeff Roberson     CTLFLAG_RD, &swap_free_deferred,
182*a8081778SJeff Roberson     "Number of pages that deferred freeing swap space");
183*a8081778SJeff Roberson 
184*a8081778SJeff Roberson static counter_u64_t swap_free_completed;
185*a8081778SJeff Roberson SYSCTL_COUNTER_U64(_vm_stats_swap, OID_AUTO, free_completed,
186*a8081778SJeff Roberson     CTLFLAG_RD, &swap_free_completed,
187*a8081778SJeff Roberson     "Number of deferred frees completed");
188*a8081778SJeff Roberson 
1893364c323SKonstantin Belousov /* bits from overcommit */
1903364c323SKonstantin Belousov #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
1913364c323SKonstantin Belousov #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
1923364c323SKonstantin Belousov #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
1933364c323SKonstantin Belousov 
194e8bb589dSMatt Macy static int
195e8bb589dSMatt Macy sysctl_page_shift(SYSCTL_HANDLER_ARGS)
196e8bb589dSMatt Macy {
197e8bb589dSMatt Macy 	uint64_t newval;
198e8bb589dSMatt Macy 	u_long value = *(u_long *)arg1;
199e8bb589dSMatt Macy 
200e8bb589dSMatt Macy 	newval = ((uint64_t)value) << PAGE_SHIFT;
201e8bb589dSMatt Macy 	return (sysctl_handle_64(oidp, &newval, 0, req));
202e8bb589dSMatt Macy }
203e8bb589dSMatt Macy 
2043364c323SKonstantin Belousov int
2053364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr)
2063364c323SKonstantin Belousov {
2073364c323SKonstantin Belousov 
208ef694c1aSEdward Tomasz Napierala 	return (swap_reserve_by_cred(incr, curthread->td_ucred));
2093364c323SKonstantin Belousov }
2103364c323SKonstantin Belousov 
2113364c323SKonstantin Belousov int
212ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
2133364c323SKonstantin Belousov {
214e8bb589dSMatt Macy 	u_long r, s, prev, pincr;
2153364c323SKonstantin Belousov 	int res, error;
2163364c323SKonstantin Belousov 	static int curfail;
2173364c323SKonstantin Belousov 	static struct timeval lastfail;
218ef694c1aSEdward Tomasz Napierala 	struct uidinfo *uip;
219ef694c1aSEdward Tomasz Napierala 
220ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
2213364c323SKonstantin Belousov 
222e8bb589dSMatt Macy 	KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
223e8bb589dSMatt Macy 	    (uintmax_t)incr));
2243364c323SKonstantin Belousov 
225afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2264b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
2271ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2281ba5ad42SEdward Tomasz Napierala 		error = racct_add(curproc, RACCT_SWAP, incr);
2291ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2301ba5ad42SEdward Tomasz Napierala 		if (error != 0)
2311ba5ad42SEdward Tomasz Napierala 			return (0);
2324b5c9cf6SEdward Tomasz Napierala 	}
233afcc55f3SEdward Tomasz Napierala #endif
2341ba5ad42SEdward Tomasz Napierala 
235e8bb589dSMatt Macy 	pincr = atop(incr);
2363364c323SKonstantin Belousov 	res = 0;
237e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&swap_reserved, pincr);
238e8bb589dSMatt Macy 	r = prev + pincr;
2393364c323SKonstantin Belousov 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
240e958ad4cSJeff Roberson 		s = vm_cnt.v_page_count - vm_cnt.v_free_reserved -
241e958ad4cSJeff Roberson 		    vm_wire_count();
2423364c323SKonstantin Belousov 	} else
2433364c323SKonstantin Belousov 		s = 0;
2443364c323SKonstantin Belousov 	s += swap_total;
2453364c323SKonstantin Belousov 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
2463364c323SKonstantin Belousov 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
2473364c323SKonstantin Belousov 		res = 1;
248e8bb589dSMatt Macy 	} else {
249e8bb589dSMatt Macy 		prev = atomic_fetchadd_long(&swap_reserved, -pincr);
250e8bb589dSMatt Macy 		if (prev < pincr)
251e8bb589dSMatt Macy 			panic("swap_reserved < incr on overcommit fail");
2523364c323SKonstantin Belousov 	}
2533364c323SKonstantin Belousov 	if (res) {
254e8bb589dSMatt Macy 		prev = atomic_fetchadd_long(&uip->ui_vmsize, pincr);
2555c0e1c11SKonstantin Belousov 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
256e8bb589dSMatt Macy 		    prev + pincr > lim_cur(curthread, RLIMIT_SWAP) &&
257e8bb589dSMatt Macy 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT)) {
2583364c323SKonstantin Belousov 			res = 0;
259e8bb589dSMatt Macy 			prev = atomic_fetchadd_long(&uip->ui_vmsize, -pincr);
260e8bb589dSMatt Macy 			if (prev < pincr)
261e8bb589dSMatt Macy 				panic("uip->ui_vmsize < incr on overcommit fail");
2623364c323SKonstantin Belousov 		}
2633364c323SKonstantin Belousov 	}
2643364c323SKonstantin Belousov 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
2653364c323SKonstantin Belousov 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
266134465d7SKonstantin Belousov 		    uip->ui_uid, curproc->p_pid, incr);
2673364c323SKonstantin Belousov 	}
2683364c323SKonstantin Belousov 
269afcc55f3SEdward Tomasz Napierala #ifdef RACCT
270e8bb589dSMatt Macy 	if (racct_enable && !res) {
2711ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2721ba5ad42SEdward Tomasz Napierala 		racct_sub(curproc, RACCT_SWAP, incr);
2731ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2741ba5ad42SEdward Tomasz Napierala 	}
275afcc55f3SEdward Tomasz Napierala #endif
2761ba5ad42SEdward Tomasz Napierala 
2773364c323SKonstantin Belousov 	return (res);
2783364c323SKonstantin Belousov }
2793364c323SKonstantin Belousov 
2803364c323SKonstantin Belousov void
2813364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr)
2823364c323SKonstantin Belousov {
2833364c323SKonstantin Belousov 	struct uidinfo *uip;
284e8bb589dSMatt Macy 	u_long pincr;
2853364c323SKonstantin Belousov 
286e8bb589dSMatt Macy 	KASSERT((incr & PAGE_MASK) == 0, ("%s: incr: %ju & PAGE_MASK", __func__,
287e8bb589dSMatt Macy 	    (uintmax_t)incr));
2883364c323SKonstantin Belousov 
289e8bb589dSMatt Macy 	PROC_LOCK(curproc);
290afcc55f3SEdward Tomasz Napierala #ifdef RACCT
291e8bb589dSMatt Macy 	if (racct_enable)
2921ba5ad42SEdward Tomasz Napierala 		racct_add_force(curproc, RACCT_SWAP, incr);
293afcc55f3SEdward Tomasz Napierala #endif
294e8bb589dSMatt Macy 	pincr = atop(incr);
295e8bb589dSMatt Macy 	atomic_add_long(&swap_reserved, pincr);
296e8bb589dSMatt Macy 	uip = curproc->p_ucred->cr_ruidinfo;
297e8bb589dSMatt Macy 	atomic_add_long(&uip->ui_vmsize, pincr);
2983364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2993364c323SKonstantin Belousov }
3003364c323SKonstantin Belousov 
3013364c323SKonstantin Belousov void
3023364c323SKonstantin Belousov swap_release(vm_ooffset_t decr)
3033364c323SKonstantin Belousov {
304ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
3053364c323SKonstantin Belousov 
3063364c323SKonstantin Belousov 	PROC_LOCK(curproc);
307e8bb589dSMatt Macy 	cred = curproc->p_ucred;
308ef694c1aSEdward Tomasz Napierala 	swap_release_by_cred(decr, cred);
3093364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
3103364c323SKonstantin Belousov }
3113364c323SKonstantin Belousov 
3123364c323SKonstantin Belousov void
313ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
3143364c323SKonstantin Belousov {
315e8bb589dSMatt Macy 	u_long prev, pdecr;
316ef694c1aSEdward Tomasz Napierala  	struct uidinfo *uip;
317ef694c1aSEdward Tomasz Napierala 
318ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
3193364c323SKonstantin Belousov 
320e8bb589dSMatt Macy 	KASSERT((decr & PAGE_MASK) == 0, ("%s: decr: %ju & PAGE_MASK", __func__,
321e8bb589dSMatt Macy 	    (uintmax_t)decr));
3223364c323SKonstantin Belousov 
323e8bb589dSMatt Macy 	pdecr = atop(decr);
324e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&swap_reserved, -pdecr);
325e8bb589dSMatt Macy 	if (prev < pdecr)
3263364c323SKonstantin Belousov 		panic("swap_reserved < decr");
3273364c323SKonstantin Belousov 
328e8bb589dSMatt Macy 	prev = atomic_fetchadd_long(&uip->ui_vmsize, -pdecr);
329e8bb589dSMatt Macy 	if (prev < pdecr)
3303364c323SKonstantin Belousov 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
331e8bb589dSMatt Macy #ifdef RACCT
332e8bb589dSMatt Macy 	if (racct_enable)
3331ba5ad42SEdward Tomasz Napierala 		racct_sub_cred(cred, RACCT_SWAP, decr);
334e8bb589dSMatt Macy #endif
3353364c323SKonstantin Belousov }
3363364c323SKonstantin Belousov 
337f08b3099SKonstantin Belousov static int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
3387dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
339756a5412SGleb Smirnoff static struct mtx swbuf_mtx;	/* to sync nsw_wcount_async */
340756a5412SGleb Smirnoff static int nsw_wcount_async;	/* limit async write buffers */
341327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
342327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
3431c7c3c6aSMatthew Dillon 
34489c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
3454c36e917SKonstantin Belousov SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
3464c36e917SKonstantin Belousov     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
3474c36e917SKonstantin Belousov     "Maximum running async swap ops");
348d027ed2eSAlan Cox static int sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS);
349d027ed2eSAlan Cox SYSCTL_PROC(_vm, OID_AUTO, swap_fragmentation, CTLTYPE_STRING | CTLFLAG_RD |
350d027ed2eSAlan Cox     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_fragmentation, "A",
351d027ed2eSAlan Cox     "Swap Fragmentation Info");
35289c241d1SGleb Smirnoff 
3530cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
354327f4e83SMatthew Dillon 
3551c7c3c6aSMatthew Dillon /*
3561c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
3571c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
3581c7c3c6aSMatthew Dillon  */
3591c7c3c6aSMatthew Dillon 
3601c7c3c6aSMatthew Dillon #define NOBJLISTS		8
3611c7c3c6aSMatthew Dillon 
3621c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
363af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
3641c7c3c6aSMatthew Dillon 
3651c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
366756a5412SGleb Smirnoff static uma_zone_t swwbuf_zone;
367756a5412SGleb Smirnoff static uma_zone_t swrbuf_zone;
368f425ab8eSKonstantin Belousov static uma_zone_t swblk_zone;
369f425ab8eSKonstantin Belousov static uma_zone_t swpctrie_zone;
3701c7c3c6aSMatthew Dillon 
3711c7c3c6aSMatthew Dillon /*
3721c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
3731c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
3741c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
3751c7c3c6aSMatthew Dillon  */
376ff98689dSBruce Evans static vm_object_t
37711caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
3783364c323SKonstantin Belousov 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
37911caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
380b0cd2017SGleb Smirnoff static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
381b0cd2017SGleb Smirnoff     int *);
382b0cd2017SGleb Smirnoff static int	swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
383b0cd2017SGleb Smirnoff     int *, pgo_getpages_iodone_t, void *);
384751221fdSPoul-Henning Kamp static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
3855ea4972cSAlan Cox static boolean_t
3865ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
38711caded3SAlfred Perlstein static void	swap_pager_init(void);
38811caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
389b3fed13eSDavid Schultz static void	swap_pager_swapoff(struct swdevt *sp);
390fe7bcbafSKyle Evans static void	swap_pager_update_writecount(vm_object_t object,
391fe7bcbafSKyle Evans     vm_offset_t start, vm_offset_t end);
392fe7bcbafSKyle Evans static void	swap_pager_release_writecount(vm_object_t object,
393fe7bcbafSKyle Evans     vm_offset_t start, vm_offset_t end);
394f708ef1bSPoul-Henning Kamp 
395df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
396e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
397e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
398e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
399e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
40090effb23SGleb Smirnoff 	.pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)		*/
401e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
402e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
403e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
404fe7bcbafSKyle Evans 	.pgo_update_writecount = swap_pager_update_writecount,
405fe7bcbafSKyle Evans 	.pgo_release_writecount = swap_pager_release_writecount,
406df8bae1dSRodney W. Grimes };
407df8bae1dSRodney W. Grimes 
4081c7c3c6aSMatthew Dillon /*
4091c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
4101c7c3c6aSMatthew Dillon  * internal.
4111c7c3c6aSMatthew Dillon  */
412e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
413e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
41426f9a767SRodney W. Grimes 
41507c348eaSAlan Cox SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
41607c348eaSAlan Cox     "Maximum size of a swap block in pages");
417cee313c4SRobert Watson 
418a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
41911caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
420230869e0SAlan Cox static bool	swp_pager_swblk_empty(struct swblk *sb, int start, int limit);
421abdab7b6SDoug Moore static void	swp_pager_free_empty_swblk(vm_object_t, struct swblk *sb);
42288ad2d7bSKonstantin Belousov static int	swapongeom(struct vnode *);
42359efee01SPoul-Henning Kamp static int	swaponvp(struct thread *, struct vnode *, u_long);
42435918c55SChristian S.J. Peron static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
42524a1cce3SDavid Greenman 
4261c7c3c6aSMatthew Dillon /*
4271c7c3c6aSMatthew Dillon  * Swap bitmap functions
4281c7c3c6aSMatthew Dillon  */
429230869e0SAlan Cox static void	swp_pager_freeswapspace(daddr_t blk, daddr_t npages);
43048e98a2aSDoug Moore static daddr_t	swp_pager_getswapspace(int *npages, int limit);
4311c7c3c6aSMatthew Dillon 
4321c7c3c6aSMatthew Dillon /*
4331c7c3c6aSMatthew Dillon  * Metadata functions
4341c7c3c6aSMatthew Dillon  */
43578f1deefSAlan Cox static daddr_t swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
4362e56b64fSKonstantin Belousov static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
437467057fcSDoug Moore static void swp_pager_meta_transfer(vm_object_t src, vm_object_t dst,
438467057fcSDoug Moore     vm_pindex_t pindex, vm_pindex_t count);
43911caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
4408ecbf14bSDoug Moore static daddr_t swp_pager_meta_lookup(vm_object_t, vm_pindex_t);
4411c7c3c6aSMatthew Dillon 
44278f1deefSAlan Cox static void
44378f1deefSAlan Cox swp_pager_init_freerange(daddr_t *start, daddr_t *num)
44478f1deefSAlan Cox {
44578f1deefSAlan Cox 
44678f1deefSAlan Cox 	*start = SWAPBLK_NONE;
44778f1deefSAlan Cox 	*num = 0;
44878f1deefSAlan Cox }
44978f1deefSAlan Cox 
45078f1deefSAlan Cox static void
45178f1deefSAlan Cox swp_pager_update_freerange(daddr_t *start, daddr_t *num, daddr_t addr)
45278f1deefSAlan Cox {
45378f1deefSAlan Cox 
45478f1deefSAlan Cox 	if (*start + *num == addr) {
45578f1deefSAlan Cox 		(*num)++;
45678f1deefSAlan Cox 	} else {
45778f1deefSAlan Cox 		swp_pager_freeswapspace(*start, *num);
45878f1deefSAlan Cox 		*start = addr;
45978f1deefSAlan Cox 		*num = 1;
46078f1deefSAlan Cox 	}
46178f1deefSAlan Cox }
46278f1deefSAlan Cox 
463f425ab8eSKonstantin Belousov static void *
464f425ab8eSKonstantin Belousov swblk_trie_alloc(struct pctrie *ptree)
465f425ab8eSKonstantin Belousov {
466f425ab8eSKonstantin Belousov 
467f425ab8eSKonstantin Belousov 	return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ?
468f425ab8eSKonstantin Belousov 	    M_USE_RESERVE : 0)));
469f425ab8eSKonstantin Belousov }
470f425ab8eSKonstantin Belousov 
471f425ab8eSKonstantin Belousov static void
472f425ab8eSKonstantin Belousov swblk_trie_free(struct pctrie *ptree, void *node)
473f425ab8eSKonstantin Belousov {
474f425ab8eSKonstantin Belousov 
475f425ab8eSKonstantin Belousov 	uma_zfree(swpctrie_zone, node);
476f425ab8eSKonstantin Belousov }
477f425ab8eSKonstantin Belousov 
478f425ab8eSKonstantin Belousov PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free);
479f425ab8eSKonstantin Belousov 
4801c7c3c6aSMatthew Dillon /*
4811c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
4821c7c3c6aSMatthew Dillon  *
48320d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
48420d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
48520d3034fSMatthew Dillon  *
48620d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
4871c7c3c6aSMatthew Dillon  *
4881c7c3c6aSMatthew Dillon  *	No restrictions on call
4891c7c3c6aSMatthew Dillon  *	This routine may not block.
4901c7c3c6aSMatthew Dillon  */
491a5edd34aSPoul-Henning Kamp static void
4922f249180SPoul-Henning Kamp swp_sizecheck(void)
4930d94caffSDavid Greenman {
49423955314SAlfred Perlstein 
4958f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
49620d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
4971af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
49820d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
4992b0d37a4SMatthew Dillon 		}
50020d3034fSMatthew Dillon 	} else {
50126f9a767SRodney W. Grimes 		swap_pager_full = 0;
5028f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
50320d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
50426f9a767SRodney W. Grimes 	}
5051c7c3c6aSMatthew Dillon }
5061c7c3c6aSMatthew Dillon 
5071c7c3c6aSMatthew Dillon /*
5081c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
5091c7c3c6aSMatthew Dillon  *
5101c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
5111c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
5121c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
5131c7c3c6aSMatthew Dillon  */
514f5a12711SPoul-Henning Kamp static void
5152f249180SPoul-Henning Kamp swap_pager_init(void)
516df8bae1dSRodney W. Grimes {
5171c7c3c6aSMatthew Dillon 	/*
5181c7c3c6aSMatthew Dillon 	 * Initialize object lists
5191c7c3c6aSMatthew Dillon 	 */
5201c7c3c6aSMatthew Dillon 	int i;
5211c7c3c6aSMatthew Dillon 
5221c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
5231c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
52420da9c2eSPoul-Henning Kamp 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
52515719273SKonstantin Belousov 	sx_init(&sw_alloc_sx, "swspsx");
52604533e1eSKonstantin Belousov 	sx_init(&swdev_syscall_lock, "swsysc");
5271c7c3c6aSMatthew Dillon }
52826f9a767SRodney W. Grimes 
529*a8081778SJeff Roberson static void
530*a8081778SJeff Roberson swap_pager_counters(void)
531*a8081778SJeff Roberson {
532*a8081778SJeff Roberson 
533*a8081778SJeff Roberson 	swap_free_deferred = counter_u64_alloc(M_WAITOK);
534*a8081778SJeff Roberson 	swap_free_completed = counter_u64_alloc(M_WAITOK);
535*a8081778SJeff Roberson }
536*a8081778SJeff Roberson SYSINIT(swap_counters, SI_SUB_CPU, SI_ORDER_ANY, swap_pager_counters, NULL);
537*a8081778SJeff Roberson 
538df8bae1dSRodney W. Grimes /*
5391c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
5401c7c3c6aSMatthew Dillon  *
5411c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
5421c7c3c6aSMatthew Dillon  *	its main loop.
543df8bae1dSRodney W. Grimes  */
54424a1cce3SDavid Greenman void
5452f249180SPoul-Henning Kamp swap_pager_swap_init(void)
546df8bae1dSRodney W. Grimes {
54761203277SDag-Erling Smørgrav 	unsigned long n, n2;
5480d94caffSDavid Greenman 
54926f9a767SRodney W. Grimes 	/*
5501c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
5511c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
5521c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
5531c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
5541c7c3c6aSMatthew Dillon 	 *
555327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
5567b9bcad9SDoug Moore 	 * array, which has MAXPHYS / PAGE_SIZE entries, and our locally
5577b9bcad9SDoug Moore 	 * defined MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
5581c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
559327f4e83SMatthew Dillon 	 *
560327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
561327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
562327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
563327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
564327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
565327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
566327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
567327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
568327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
569327f4e83SMatthew Dillon 	 * So it all works out pretty well.
57026f9a767SRodney W. Grimes 	 */
5710cab71bcSDoug Moore 	nsw_cluster_max = min(MAXPHYS / PAGE_SIZE, MAX_PAGEOUT_CLUSTER);
572327f4e83SMatthew Dillon 
573327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
574327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
575756a5412SGleb Smirnoff 	mtx_init(&swbuf_mtx, "async swbuf mutex", NULL, MTX_DEF);
576756a5412SGleb Smirnoff 
577756a5412SGleb Smirnoff 	swwbuf_zone = pbuf_zsecond_create("swwbuf", nswbuf / 4);
578756a5412SGleb Smirnoff 	swrbuf_zone = pbuf_zsecond_create("swrbuf", nswbuf / 2);
57924a1cce3SDavid Greenman 
5801c7c3c6aSMatthew Dillon 	/*
581a8233027SKonstantin Belousov 	 * Initialize our zone, taking the user's requested size or
582a8233027SKonstantin Belousov 	 * estimating the number we need based on the number of pages
583a8233027SKonstantin Belousov 	 * in the system.
5841c7c3c6aSMatthew Dillon 	 */
585a8233027SKonstantin Belousov 	n = maxswzone != 0 ? maxswzone / sizeof(struct swblk) :
586a8233027SKonstantin Belousov 	    vm_cnt.v_page_count / 2;
587f425ab8eSKonstantin Belousov 	swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL,
588f5fbe90dSAlan Cox 	    pctrie_zone_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM);
589f425ab8eSKonstantin Belousov 	if (swpctrie_zone == NULL)
590f425ab8eSKonstantin Belousov 		panic("failed to create swap pctrie zone.");
591f425ab8eSKonstantin Belousov 	swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL,
592f5fbe90dSAlan Cox 	    NULL, NULL, _Alignof(struct swblk) - 1, UMA_ZONE_VM);
593f425ab8eSKonstantin Belousov 	if (swblk_zone == NULL)
594f425ab8eSKonstantin Belousov 		panic("failed to create swap blk zone.");
59522a5e6b9SDag-Erling Smørgrav 	n2 = n;
5968355f576SJeff Roberson 	do {
597f425ab8eSKonstantin Belousov 		if (uma_zone_reserve_kva(swblk_zone, n))
59861ce6eeeSAlfred Perlstein 			break;
59961ce6eeeSAlfred Perlstein 		/*
60061ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
60161ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
60261ce6eeeSAlfred Perlstein 		 */
60361ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
60461ce6eeeSAlfred Perlstein 	} while (n > 0);
60553faf5a7SKonstantin Belousov 
60653faf5a7SKonstantin Belousov 	/*
60753faf5a7SKonstantin Belousov 	 * Often uma_zone_reserve_kva() cannot reserve exactly the
60853faf5a7SKonstantin Belousov 	 * requested size.  Account for the difference when
60953faf5a7SKonstantin Belousov 	 * calculating swap_maxpages.
61053faf5a7SKonstantin Belousov 	 */
61153faf5a7SKonstantin Belousov 	n = uma_zone_get_max(swblk_zone);
61253faf5a7SKonstantin Belousov 
6131fffcd75SKonstantin Belousov 	if (n < n2)
614a8233027SKonstantin Belousov 		printf("Swap blk zone entries changed from %lu to %lu.\n",
615f425ab8eSKonstantin Belousov 		    n2, n);
616303fa05aSKonstantin Belousov 	/* absolute maximum we can handle assuming 100% efficiency */
61761203277SDag-Erling Smørgrav 	swap_maxpages = n * SWAP_META_PAGES;
618f425ab8eSKonstantin Belousov 	swzone = n * sizeof(struct swblk);
619f425ab8eSKonstantin Belousov 	if (!uma_zone_reserve_kva(swpctrie_zone, n))
620f425ab8eSKonstantin Belousov 		printf("Cannot reserve swap pctrie zone, "
621f425ab8eSKonstantin Belousov 		    "reduce kern.maxswzone.\n");
62224a1cce3SDavid Greenman }
62324a1cce3SDavid Greenman 
624eb4d6a1bSKonstantin Belousov static vm_object_t
625eb4d6a1bSKonstantin Belousov swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size,
626eb4d6a1bSKonstantin Belousov     vm_ooffset_t offset)
627eb4d6a1bSKonstantin Belousov {
628eb4d6a1bSKonstantin Belousov 	vm_object_t object;
629eb4d6a1bSKonstantin Belousov 
630eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
631eb4d6a1bSKonstantin Belousov 		if (!swap_reserve_by_cred(size, cred))
632eb4d6a1bSKonstantin Belousov 			return (NULL);
633eb4d6a1bSKonstantin Belousov 		crhold(cred);
634eb4d6a1bSKonstantin Belousov 	}
635f425ab8eSKonstantin Belousov 
636f425ab8eSKonstantin Belousov 	/*
637f425ab8eSKonstantin Belousov 	 * The un_pager.swp.swp_blks trie is initialized by
638f425ab8eSKonstantin Belousov 	 * vm_object_allocate() to ensure the correct order of
639f425ab8eSKonstantin Belousov 	 * visibility to other threads.
640f425ab8eSKonstantin Belousov 	 */
641eb4d6a1bSKonstantin Belousov 	object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset +
642eb4d6a1bSKonstantin Belousov 	    PAGE_MASK + size));
643f425ab8eSKonstantin Belousov 
644fe7bcbafSKyle Evans 	object->un_pager.swp.writemappings = 0;
645eb4d6a1bSKonstantin Belousov 	object->handle = handle;
646eb4d6a1bSKonstantin Belousov 	if (cred != NULL) {
647eb4d6a1bSKonstantin Belousov 		object->cred = cred;
648eb4d6a1bSKonstantin Belousov 		object->charge = size;
649eb4d6a1bSKonstantin Belousov 	}
650eb4d6a1bSKonstantin Belousov 	return (object);
651eb4d6a1bSKonstantin Belousov }
652eb4d6a1bSKonstantin Belousov 
65324a1cce3SDavid Greenman /*
6541c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
6551c7c3c6aSMatthew Dillon  *			its metadata structures.
6561c7c3c6aSMatthew Dillon  *
6571c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
658eb4d6a1bSKonstantin Belousov  *	OBJT_SWAP object.
6591c7c3c6aSMatthew Dillon  *
660eb4d6a1bSKonstantin Belousov  *	This routine must ensure that no live duplicate is created for
661eb4d6a1bSKonstantin Belousov  *	the named object request, which is protected against by
662eb4d6a1bSKonstantin Belousov  *	holding the sw_alloc_sx lock in case handle != NULL.
66324a1cce3SDavid Greenman  */
664f5a12711SPoul-Henning Kamp static vm_object_t
6656cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
6663364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
66724a1cce3SDavid Greenman {
66824a1cce3SDavid Greenman 	vm_object_t object;
6692f7af3dbSAlan Cox 
670eb4d6a1bSKonstantin Belousov 	if (handle != NULL) {
6711c7c3c6aSMatthew Dillon 		/*
6721c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
6731c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
6741c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
6751c7c3c6aSMatthew Dillon 		 * of the handle.
6761c7c3c6aSMatthew Dillon 		 */
6770cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
6781c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
679b5e8f167SAlan Cox 		if (object == NULL) {
680eb4d6a1bSKonstantin Belousov 			object = swap_pager_alloc_init(handle, cred, size,
681eb4d6a1bSKonstantin Belousov 			    offset);
682eb4d6a1bSKonstantin Belousov 			if (object != NULL) {
683eb4d6a1bSKonstantin Belousov 				TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
684eb4d6a1bSKonstantin Belousov 				    object, pager_object_list);
6853364c323SKonstantin Belousov 			}
68624a1cce3SDavid Greenman 		}
6870cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
68824a1cce3SDavid Greenman 	} else {
689eb4d6a1bSKonstantin Belousov 		object = swap_pager_alloc_init(handle, cred, size, offset);
69024a1cce3SDavid Greenman 	}
69124a1cce3SDavid Greenman 	return (object);
692df8bae1dSRodney W. Grimes }
693df8bae1dSRodney W. Grimes 
69426f9a767SRodney W. Grimes /*
6951c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
6961c7c3c6aSMatthew Dillon  *
6971c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
6981c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
6991c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
7001c7c3c6aSMatthew Dillon  *	about to be destroyed.
7011c7c3c6aSMatthew Dillon  *
70215523cf7SKonstantin Belousov  *	The object must be locked.
70326f9a767SRodney W. Grimes  */
704df8bae1dSRodney W. Grimes static void
7052f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object)
70626f9a767SRodney W. Grimes {
7074dcc5c2dSMatthew Dillon 
708eb4d6a1bSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
709eb4d6a1bSKonstantin Belousov 	KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
710eb4d6a1bSKonstantin Belousov 
71126f9a767SRodney W. Grimes 	/*
7121c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
7131c7c3c6aSMatthew Dillon 	 * pageout completion.
71426f9a767SRodney W. Grimes 	 */
71567388836SKonstantin Belousov 	if ((object->flags & OBJ_ANON) == 0 && object->handle != NULL) {
716eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
717eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
718eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(object->handle), object,
719eb4d6a1bSKonstantin Belousov 		    pager_object_list);
720eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
721eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(object);
722bd228075SAlan Cox 	}
7231c7c3c6aSMatthew Dillon 
7241c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
7251c7c3c6aSMatthew Dillon 
7261c7c3c6aSMatthew Dillon 	/*
7271c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
7281c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
7291c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
7301c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
7311c7c3c6aSMatthew Dillon 	 */
7321c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
733e735691bSJohn Baldwin 	object->handle = NULL;
734e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
7351c7c3c6aSMatthew Dillon }
7361c7c3c6aSMatthew Dillon 
7371c7c3c6aSMatthew Dillon /************************************************************************
7381c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
7391c7c3c6aSMatthew Dillon  ************************************************************************/
7401c7c3c6aSMatthew Dillon 
7411c7c3c6aSMatthew Dillon /*
7421c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
7431c7c3c6aSMatthew Dillon  *
74448e98a2aSDoug Moore  *	Allocate swap for up to the requested number of pages, and at
74548e98a2aSDoug Moore  *	least a minimum number of pages.  The starting swap block number
74648e98a2aSDoug Moore  *	(a page index) is returned or SWAPBLK_NONE if the allocation
74748e98a2aSDoug Moore  *	failed.
7481c7c3c6aSMatthew Dillon  *
7491c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
7501c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
7511c7c3c6aSMatthew Dillon  *
75215523cf7SKonstantin Belousov  *	This routine may not sleep.
7538f60c087SPoul-Henning Kamp  *
7548f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
7551c7c3c6aSMatthew Dillon  */
756a5edd34aSPoul-Henning Kamp static daddr_t
75748e98a2aSDoug Moore swp_pager_getswapspace(int *io_npages, int limit)
7581c7c3c6aSMatthew Dillon {
7591c7c3c6aSMatthew Dillon 	daddr_t blk;
7608f60c087SPoul-Henning Kamp 	struct swdevt *sp;
76187ae0686SDoug Moore 	int mpages, npages;
7621c7c3c6aSMatthew Dillon 
7638f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
76431c82722SDoug Moore 	mpages = *io_npages;
76531c82722SDoug Moore 	npages = imin(BLIST_MAX_ALLOC, mpages);
76620da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7678f60c087SPoul-Henning Kamp 	sp = swdevhd;
76848e98a2aSDoug Moore 	while (!TAILQ_EMPTY(&swtailq)) {
7698f60c087SPoul-Henning Kamp 		if (sp == NULL)
7708f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
77148e98a2aSDoug Moore 		if ((sp->sw_flags & SW_CLOSING) == 0)
77287ae0686SDoug Moore 			blk = blist_alloc(sp->sw_blist, &npages, mpages);
77348e98a2aSDoug Moore 		if (blk != SWAPBLK_NONE)
77448e98a2aSDoug Moore 			break;
77548e98a2aSDoug Moore 		sp = TAILQ_NEXT(sp, sw_list);
77648e98a2aSDoug Moore 		if (swdevhd == sp) {
77748e98a2aSDoug Moore 			if (npages <= limit)
77848e98a2aSDoug Moore 				break;
77987ae0686SDoug Moore 			mpages = npages - 1;
78048e98a2aSDoug Moore 			npages >>= 1;
78148e98a2aSDoug Moore 		}
78248e98a2aSDoug Moore 	}
7838f60c087SPoul-Henning Kamp 	if (blk != SWAPBLK_NONE) {
78448e98a2aSDoug Moore 		*io_npages = npages;
7858f60c087SPoul-Henning Kamp 		blk += sp->sw_first;
7868f60c087SPoul-Henning Kamp 		sp->sw_used += npages;
787d05bc129SAlan Cox 		swap_pager_avail -= npages;
7888f60c087SPoul-Henning Kamp 		swp_sizecheck();
7898f60c087SPoul-Henning Kamp 		swdevhd = TAILQ_NEXT(sp, sw_list);
79048e98a2aSDoug Moore 	} else {
7912b0d37a4SMatthew Dillon 		if (swap_pager_full != 2) {
79248e98a2aSDoug Moore 			printf("swp_pager_getswapspace(%d): failed\n",
79348e98a2aSDoug Moore 			    *io_npages);
7942b0d37a4SMatthew Dillon 			swap_pager_full = 2;
79520d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
7962b0d37a4SMatthew Dillon 		}
7978f60c087SPoul-Henning Kamp 		swdevhd = NULL;
79848e98a2aSDoug Moore 	}
799d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
8001c7c3c6aSMatthew Dillon 	return (blk);
80126f9a767SRodney W. Grimes }
80226f9a767SRodney W. Grimes 
803541a1175SAlan Cox static bool
804b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp)
8058f60c087SPoul-Henning Kamp {
8068f60c087SPoul-Henning Kamp 
807b3fed13eSDavid Schultz 	return (blk >= sp->sw_first && blk < sp->sw_end);
8088f60c087SPoul-Henning Kamp }
8098f60c087SPoul-Henning Kamp 
8104b03903aSPoul-Henning Kamp static void
8114b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp)
8124b03903aSPoul-Henning Kamp {
8134b03903aSPoul-Henning Kamp 	struct swdevt *sp;
8144b03903aSPoul-Henning Kamp 
81520da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
8164b03903aSPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
817541a1175SAlan Cox 		if (swp_pager_isondev(bp->b_blkno, sp)) {
81820da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
8192cc718a1SKonstantin Belousov 			if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
8202cc718a1SKonstantin Belousov 			    unmapped_buf_allowed) {
8212cc718a1SKonstantin Belousov 				bp->b_data = unmapped_buf;
8222cc718a1SKonstantin Belousov 				bp->b_offset = 0;
8232cc718a1SKonstantin Belousov 			} else {
8242cc718a1SKonstantin Belousov 				pmap_qenter((vm_offset_t)bp->b_data,
8252cc718a1SKonstantin Belousov 				    &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
8262cc718a1SKonstantin Belousov 			}
8274b03903aSPoul-Henning Kamp 			sp->sw_strategy(bp, sp);
8284b03903aSPoul-Henning Kamp 			return;
8294b03903aSPoul-Henning Kamp 		}
8304b03903aSPoul-Henning Kamp 	}
83120da9c2eSPoul-Henning Kamp 	panic("Swapdev not found");
8324b03903aSPoul-Henning Kamp }
8334b03903aSPoul-Henning Kamp 
8348f60c087SPoul-Henning Kamp 
83526f9a767SRodney W. Grimes /*
8361c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
8371c7c3c6aSMatthew Dillon  *
8381c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
8391c7c3c6aSMatthew Dillon  *
84015523cf7SKonstantin Belousov  *	This routine may not sleep.
84126f9a767SRodney W. Grimes  */
842a5edd34aSPoul-Henning Kamp static void
843230869e0SAlan Cox swp_pager_freeswapspace(daddr_t blk, daddr_t npages)
8440d94caffSDavid Greenman {
8458f60c087SPoul-Henning Kamp 	struct swdevt *sp;
84692da00bbSMatthew Dillon 
847230869e0SAlan Cox 	if (npages == 0)
848230869e0SAlan Cox 		return;
8497645e885SAlan Cox 	mtx_lock(&sw_dev_mtx);
8507645e885SAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
851541a1175SAlan Cox 		if (swp_pager_isondev(blk, sp)) {
85292da00bbSMatthew Dillon 			sp->sw_used -= npages;
85392da00bbSMatthew Dillon 			/*
8547645e885SAlan Cox 			 * If we are attempting to stop swapping on
8557645e885SAlan Cox 			 * this device, we don't want to mark any
8567645e885SAlan Cox 			 * blocks free lest they be reused.
85792da00bbSMatthew Dillon 			 */
8587645e885SAlan Cox 			if ((sp->sw_flags & SW_CLOSING) == 0) {
8597645e885SAlan Cox 				blist_free(sp->sw_blist, blk - sp->sw_first,
8607645e885SAlan Cox 				    npages);
8618f60c087SPoul-Henning Kamp 				swap_pager_avail += npages;
8621c7c3c6aSMatthew Dillon 				swp_sizecheck();
86326f9a767SRodney W. Grimes 			}
8647645e885SAlan Cox 			mtx_unlock(&sw_dev_mtx);
8657645e885SAlan Cox 			return;
8667645e885SAlan Cox 		}
8677645e885SAlan Cox 	}
8687645e885SAlan Cox 	panic("Swapdev not found");
8697645e885SAlan Cox }
8701c7c3c6aSMatthew Dillon 
87126f9a767SRodney W. Grimes /*
872d027ed2eSAlan Cox  * SYSCTL_SWAP_FRAGMENTATION() -	produce raw swap space stats
873d027ed2eSAlan Cox  */
874d027ed2eSAlan Cox static int
875d027ed2eSAlan Cox sysctl_swap_fragmentation(SYSCTL_HANDLER_ARGS)
876d027ed2eSAlan Cox {
877d027ed2eSAlan Cox 	struct sbuf sbuf;
878d027ed2eSAlan Cox 	struct swdevt *sp;
879d027ed2eSAlan Cox 	const char *devname;
880d027ed2eSAlan Cox 	int error;
881d027ed2eSAlan Cox 
882d027ed2eSAlan Cox 	error = sysctl_wire_old_buffer(req, 0);
883d027ed2eSAlan Cox 	if (error != 0)
884d027ed2eSAlan Cox 		return (error);
885d027ed2eSAlan Cox 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
886d027ed2eSAlan Cox 	mtx_lock(&sw_dev_mtx);
887d027ed2eSAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
888d027ed2eSAlan Cox 		if (vn_isdisk(sp->sw_vp, NULL))
889d027ed2eSAlan Cox 			devname = devtoname(sp->sw_vp->v_rdev);
890d027ed2eSAlan Cox 		else
891d027ed2eSAlan Cox 			devname = "[file]";
892d027ed2eSAlan Cox 		sbuf_printf(&sbuf, "\nFree space on device %s:\n", devname);
893d027ed2eSAlan Cox 		blist_stats(sp->sw_blist, &sbuf);
894d027ed2eSAlan Cox 	}
895d027ed2eSAlan Cox 	mtx_unlock(&sw_dev_mtx);
896d027ed2eSAlan Cox 	error = sbuf_finish(&sbuf);
897d027ed2eSAlan Cox 	sbuf_delete(&sbuf);
898d027ed2eSAlan Cox 	return (error);
899d027ed2eSAlan Cox }
900d027ed2eSAlan Cox 
901d027ed2eSAlan Cox /*
9021c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
9031c7c3c6aSMatthew Dillon  *				range within an object.
9041c7c3c6aSMatthew Dillon  *
9051c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
9061c7c3c6aSMatthew Dillon  *
9071c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
9081c7c3c6aSMatthew Dillon  *
9091c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
9101c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
9111c7c3c6aSMatthew Dillon  *	we should be ok.
912c25673ffSAttilio Rao  *
913c25673ffSAttilio Rao  *	The object must be locked.
91426f9a767SRodney W. Grimes  */
91526f9a767SRodney W. Grimes void
9162f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
91726f9a767SRodney W. Grimes {
91823955314SAlfred Perlstein 
9191c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
9204dcc5c2dSMatthew Dillon }
9214dcc5c2dSMatthew Dillon 
9224dcc5c2dSMatthew Dillon /*
9234dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
9244dcc5c2dSMatthew Dillon  *
9254dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
92656ce850bSKonstantin Belousov  *	swap blocks are not zeroed.  Any previous swap assignment is destroyed.
9274dcc5c2dSMatthew Dillon  *
9284dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
9294dcc5c2dSMatthew Dillon  */
9304dcc5c2dSMatthew Dillon int
9314dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
9324dcc5c2dSMatthew Dillon {
93348e98a2aSDoug Moore 	daddr_t addr, blk, n_free, s_free;
93448e98a2aSDoug Moore 	int i, j, n;
9354dcc5c2dSMatthew Dillon 
93678f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
93789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
93848e98a2aSDoug Moore 	for (i = 0; i < size; i += n) {
93931c82722SDoug Moore 		n = size - i;
94048e98a2aSDoug Moore 		blk = swp_pager_getswapspace(&n, 1);
94148e98a2aSDoug Moore 		if (blk == SWAPBLK_NONE) {
94248e98a2aSDoug Moore 			swp_pager_meta_free(object, start, i);
94389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
9444dcc5c2dSMatthew Dillon 			return (-1);
9454dcc5c2dSMatthew Dillon 		}
94648e98a2aSDoug Moore 		for (j = 0; j < n; ++j) {
94748e98a2aSDoug Moore 			addr = swp_pager_meta_build(object,
94848e98a2aSDoug Moore 			    start + i + j, blk + j);
94978f1deefSAlan Cox 			if (addr != SWAPBLK_NONE)
95048e98a2aSDoug Moore 				swp_pager_update_freerange(&s_free, &n_free,
95148e98a2aSDoug Moore 				    addr);
95248e98a2aSDoug Moore 		}
9534dcc5c2dSMatthew Dillon 	}
95478f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
95589f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
9564dcc5c2dSMatthew Dillon 	return (0);
95726f9a767SRodney W. Grimes }
95826f9a767SRodney W. Grimes 
959467057fcSDoug Moore static bool
960467057fcSDoug Moore swp_pager_xfer_source(vm_object_t srcobject, vm_object_t dstobject,
961467057fcSDoug Moore     vm_pindex_t pindex, daddr_t addr)
962467057fcSDoug Moore {
963467057fcSDoug Moore 	daddr_t dstaddr;
964467057fcSDoug Moore 
9658ecbf14bSDoug Moore 	KASSERT(srcobject->type == OBJT_SWAP,
9668ecbf14bSDoug Moore 	    ("%s: Srcobject not swappable", __func__));
9678ecbf14bSDoug Moore 	if (dstobject->type == OBJT_SWAP &&
9688ecbf14bSDoug Moore 	    swp_pager_meta_lookup(dstobject, pindex) != SWAPBLK_NONE) {
969467057fcSDoug Moore 		/* Caller should destroy the source block. */
970467057fcSDoug Moore 		return (false);
971467057fcSDoug Moore 	}
972467057fcSDoug Moore 
973467057fcSDoug Moore 	/*
974467057fcSDoug Moore 	 * Destination has no swapblk and is not resident, transfer source.
975467057fcSDoug Moore 	 * swp_pager_meta_build() can sleep.
976467057fcSDoug Moore 	 */
977467057fcSDoug Moore 	vm_object_pip_add(srcobject, 1);
978467057fcSDoug Moore 	VM_OBJECT_WUNLOCK(srcobject);
979467057fcSDoug Moore 	vm_object_pip_add(dstobject, 1);
980467057fcSDoug Moore 	dstaddr = swp_pager_meta_build(dstobject, pindex, addr);
981467057fcSDoug Moore 	KASSERT(dstaddr == SWAPBLK_NONE,
982467057fcSDoug Moore 	    ("Unexpected destination swapblk"));
983467057fcSDoug Moore 	vm_object_pip_wakeup(dstobject);
984467057fcSDoug Moore 	VM_OBJECT_WLOCK(srcobject);
985467057fcSDoug Moore 	vm_object_pip_wakeup(srcobject);
986467057fcSDoug Moore 	return (true);
987467057fcSDoug Moore }
988467057fcSDoug Moore 
9890a47b48bSJohn Dyson /*
9901c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
9911c7c3c6aSMatthew Dillon  *			and destroy the source.
9921c7c3c6aSMatthew Dillon  *
9931c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
9941c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
9951c7c3c6aSMatthew Dillon  *	we keep the destination's.
9961c7c3c6aSMatthew Dillon  *
99715523cf7SKonstantin Belousov  *	This routine is allowed to sleep.  It may sleep allocating metadata
9981c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
9991c7c3c6aSMatthew Dillon  *	progress on the source.
10001c7c3c6aSMatthew Dillon  *
10011c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
10021c7c3c6aSMatthew Dillon  *
10031c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
10041c7c3c6aSMatthew Dillon  *
100515523cf7SKonstantin Belousov  *	The source and destination objects must be locked.
100615523cf7SKonstantin Belousov  *	Both object locks may temporarily be released.
100726f9a767SRodney W. Grimes  */
100826f9a767SRodney W. Grimes void
10092f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
10102f249180SPoul-Henning Kamp     vm_pindex_t offset, int destroysource)
101126f9a767SRodney W. Grimes {
10124dcc5c2dSMatthew Dillon 
101389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(srcobject);
101489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(dstobject);
10150cddd8f0SMatthew Dillon 
101626f9a767SRodney W. Grimes 	/*
10171c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
10181c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
101926f9a767SRodney W. Grimes 	 */
102067388836SKonstantin Belousov 	if (destroysource && (srcobject->flags & OBJ_ANON) == 0 &&
102167388836SKonstantin Belousov 	    srcobject->handle != NULL) {
1022eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(srcobject, 1);
1023eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(srcobject);
1024eb4d6a1bSKonstantin Belousov 		vm_object_pip_add(dstobject, 1);
1025eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WUNLOCK(dstobject);
1026eb4d6a1bSKonstantin Belousov 		sx_xlock(&sw_alloc_sx);
1027eb4d6a1bSKonstantin Belousov 		TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
1028eb4d6a1bSKonstantin Belousov 		    pager_object_list);
1029eb4d6a1bSKonstantin Belousov 		sx_xunlock(&sw_alloc_sx);
1030eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(dstobject);
1031eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(dstobject);
1032eb4d6a1bSKonstantin Belousov 		VM_OBJECT_WLOCK(srcobject);
1033eb4d6a1bSKonstantin Belousov 		vm_object_pip_wakeup(srcobject);
1034bd228075SAlan Cox 	}
103526f9a767SRodney W. Grimes 
10361c7c3c6aSMatthew Dillon 	/*
10374abca9bbSAlan Cox 	 * Transfer source to destination.
10381c7c3c6aSMatthew Dillon 	 */
1039467057fcSDoug Moore 	swp_pager_meta_transfer(srcobject, dstobject, offset, dstobject->size);
104026f9a767SRodney W. Grimes 
104126f9a767SRodney W. Grimes 	/*
10421c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
10431c7c3c6aSMatthew Dillon 	 *
1044763df3ecSPedro F. Giffuni 	 * We have to revert the type to OBJT_DEFAULT so we do not accidentally
10451c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
104626f9a767SRodney W. Grimes 	 */
1047c0877f10SJohn Dyson 	if (destroysource) {
10481c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
10491c7c3c6aSMatthew Dillon 		/*
10501c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
10511c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
1052956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
10531c7c3c6aSMatthew Dillon 		 * queues.
10541c7c3c6aSMatthew Dillon 		 */
10551c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
1056c0877f10SJohn Dyson 	}
105726f9a767SRodney W. Grimes }
105826f9a767SRodney W. Grimes 
1059df8bae1dSRodney W. Grimes /*
10601c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
10611c7c3c6aSMatthew Dillon  *				the requested page.
10621c7c3c6aSMatthew Dillon  *
10631c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
10641c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
10651c7c3c6aSMatthew Dillon  *
10661c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
1067915d1b71SMark Johnston  *	store exists before and after the requested page.
1068df8bae1dSRodney W. Grimes  */
10695ea4972cSAlan Cox static boolean_t
1070915d1b71SMark Johnston swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
1071915d1b71SMark Johnston     int *after)
107226f9a767SRodney W. Grimes {
1073915d1b71SMark Johnston 	daddr_t blk, blk0;
1074915d1b71SMark Johnston 	int i;
107526f9a767SRodney W. Grimes 
1076c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
10778ecbf14bSDoug Moore 	KASSERT(object->type == OBJT_SWAP,
10788ecbf14bSDoug Moore 	    ("%s: object not swappable", __func__));
1079915d1b71SMark Johnston 
10801c7c3c6aSMatthew Dillon 	/*
10811c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
10821c7c3c6aSMatthew Dillon 	 */
10838ecbf14bSDoug Moore 	blk0 = swp_pager_meta_lookup(object, pindex);
10844dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
10851c7c3c6aSMatthew Dillon 		if (before)
108624a1cce3SDavid Greenman 			*before = 0;
10871c7c3c6aSMatthew Dillon 		if (after)
108824a1cce3SDavid Greenman 			*after = 0;
108926f9a767SRodney W. Grimes 		return (FALSE);
109026f9a767SRodney W. Grimes 	}
109126f9a767SRodney W. Grimes 
109226f9a767SRodney W. Grimes 	/*
10931c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
1094e47ed70bSJohn Dyson 	 */
10951c7c3c6aSMatthew Dillon 	if (before != NULL) {
1096915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
10971c7c3c6aSMatthew Dillon 			if (i > pindex)
10981c7c3c6aSMatthew Dillon 				break;
10998ecbf14bSDoug Moore 			blk = swp_pager_meta_lookup(object, pindex - i);
11001c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
11011c7c3c6aSMatthew Dillon 				break;
1102ffc82b0aSJohn Dyson 		}
1103915d1b71SMark Johnston 		*before = i - 1;
110426f9a767SRodney W. Grimes 	}
110526f9a767SRodney W. Grimes 
110626f9a767SRodney W. Grimes 	/*
11071c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
110826f9a767SRodney W. Grimes 	 */
11091c7c3c6aSMatthew Dillon 	if (after != NULL) {
1110915d1b71SMark Johnston 		for (i = 1; i < SWB_NPAGES; i++) {
11118ecbf14bSDoug Moore 			blk = swp_pager_meta_lookup(object, pindex + i);
11121c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
11131c7c3c6aSMatthew Dillon 				break;
111426f9a767SRodney W. Grimes 		}
1115915d1b71SMark Johnston 		*after = i - 1;
11161c7c3c6aSMatthew Dillon 	}
11171c7c3c6aSMatthew Dillon 	return (TRUE);
11181c7c3c6aSMatthew Dillon }
11191c7c3c6aSMatthew Dillon 
11201c7c3c6aSMatthew Dillon /*
11211c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
11221c7c3c6aSMatthew Dillon  *
11231c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
11241c7c3c6aSMatthew Dillon  *	not, from the page.
11251c7c3c6aSMatthew Dillon  *
11261c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
11271c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
11281c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
11291c7c3c6aSMatthew Dillon  *
11301c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
11311c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
11321c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
11331c7c3c6aSMatthew Dillon  *	depends on it.
11341c7c3c6aSMatthew Dillon  *
113515523cf7SKonstantin Belousov  *	This routine may not sleep.
1136c25673ffSAttilio Rao  *
1137*a8081778SJeff Roberson  *	The object containing the page may be locked.
11381c7c3c6aSMatthew Dillon  */
11391c7c3c6aSMatthew Dillon static void
11402f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m)
11411c7c3c6aSMatthew Dillon {
11428ecbf14bSDoug Moore 	struct swblk *sb;
1143*a8081778SJeff Roberson 	vm_object_t obj;
11442f249180SPoul-Henning Kamp 
1145*a8081778SJeff Roberson 	/*
1146*a8081778SJeff Roberson 	 * Handle enqueing deferred frees first.  If we do not have the
1147*a8081778SJeff Roberson 	 * object lock we wait for the page daemon to clear the space.
1148*a8081778SJeff Roberson 	 */
1149*a8081778SJeff Roberson 	obj = m->object;
1150*a8081778SJeff Roberson 	if (!VM_OBJECT_WOWNED(obj)) {
1151*a8081778SJeff Roberson 		VM_PAGE_OBJECT_BUSY_ASSERT(m);
1152*a8081778SJeff Roberson 		/*
1153*a8081778SJeff Roberson 		 * The caller is responsible for synchronization but we
1154*a8081778SJeff Roberson 		 * will harmlessly handle races.  This is typically provided
1155*a8081778SJeff Roberson 		 * by only calling unswapped() when a page transitions from
1156*a8081778SJeff Roberson 		 * clean to dirty.
1157*a8081778SJeff Roberson 		 */
1158*a8081778SJeff Roberson 		if ((m->a.flags & (PGA_SWAP_SPACE | PGA_SWAP_FREE)) ==
1159*a8081778SJeff Roberson 		    PGA_SWAP_SPACE) {
1160*a8081778SJeff Roberson 			vm_page_aflag_set(m, PGA_SWAP_FREE);
1161*a8081778SJeff Roberson 			counter_u64_add(swap_free_deferred, 1);
1162*a8081778SJeff Roberson 		}
1163*a8081778SJeff Roberson 		return;
1164*a8081778SJeff Roberson 	}
1165*a8081778SJeff Roberson 	if ((m->a.flags & PGA_SWAP_FREE) != 0)
1166*a8081778SJeff Roberson 		counter_u64_add(swap_free_completed, 1);
1167*a8081778SJeff Roberson 	vm_page_aflag_clear(m, PGA_SWAP_FREE | PGA_SWAP_SPACE);
11688ecbf14bSDoug Moore 
11698ecbf14bSDoug Moore 	/*
11708ecbf14bSDoug Moore 	 * The meta data only exists if the object is OBJT_SWAP
11718ecbf14bSDoug Moore 	 * and even then might not be allocated yet.
11728ecbf14bSDoug Moore 	 */
11738ecbf14bSDoug Moore 	KASSERT(m->object->type == OBJT_SWAP,
11748ecbf14bSDoug Moore 	    ("Free object not swappable"));
11758ecbf14bSDoug Moore 
11768ecbf14bSDoug Moore 	sb = SWAP_PCTRIE_LOOKUP(&m->object->un_pager.swp.swp_blks,
11778ecbf14bSDoug Moore 	    rounddown(m->pindex, SWAP_META_PAGES));
11788ecbf14bSDoug Moore 	if (sb == NULL)
11798ecbf14bSDoug Moore 		return;
11808ecbf14bSDoug Moore 	if (sb->d[m->pindex % SWAP_META_PAGES] == SWAPBLK_NONE)
11818ecbf14bSDoug Moore 		return;
11828ecbf14bSDoug Moore 	swp_pager_freeswapspace(sb->d[m->pindex % SWAP_META_PAGES], 1);
11838ecbf14bSDoug Moore 	sb->d[m->pindex % SWAP_META_PAGES] = SWAPBLK_NONE;
11848ecbf14bSDoug Moore 	swp_pager_free_empty_swblk(m->object, sb);
11851c7c3c6aSMatthew Dillon }
11861c7c3c6aSMatthew Dillon 
11871c7c3c6aSMatthew Dillon /*
1188915d1b71SMark Johnston  * swap_pager_getpages() - bring pages in from swap
11891c7c3c6aSMatthew Dillon  *
11903f060b60SMark Johnston  *	Attempt to page in the pages in array "ma" of length "count".  The
11913f060b60SMark Johnston  *	caller may optionally specify that additional pages preceding and
11923f060b60SMark Johnston  *	succeeding the specified range be paged in.  The number of such pages
11933f060b60SMark Johnston  *	is returned in the "rbehind" and "rahead" parameters, and they will
11943f060b60SMark Johnston  *	be in the inactive queue upon return.
11951c7c3c6aSMatthew Dillon  *
11963f060b60SMark Johnston  *	The pages in "ma" must be busied and will remain busied upon return.
11971c7c3c6aSMatthew Dillon  */
1198f708ef1bSPoul-Henning Kamp static int
11993f060b60SMark Johnston swap_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
1200b0cd2017SGleb Smirnoff     int *rahead)
1201df8bae1dSRodney W. Grimes {
12021c7c3c6aSMatthew Dillon 	struct buf *bp;
1203b7b8a096SKonstantin Belousov 	vm_page_t bm, mpred, msucc, p;
1204915d1b71SMark Johnston 	vm_pindex_t pindex;
12051c7c3c6aSMatthew Dillon 	daddr_t blk;
1206b7b8a096SKonstantin Belousov 	int i, maxahead, maxbehind, reqcount;
12070d94caffSDavid Greenman 
1208915d1b71SMark Johnston 	reqcount = count;
12091c7c3c6aSMatthew Dillon 
1210b7b8a096SKonstantin Belousov 	/*
1211b7b8a096SKonstantin Belousov 	 * Determine the final number of read-behind pages and
1212b7b8a096SKonstantin Belousov 	 * allocate them BEFORE releasing the object lock.  Otherwise,
1213b7b8a096SKonstantin Belousov 	 * there can be a problematic race with vm_object_split().
1214b7b8a096SKonstantin Belousov 	 * Specifically, vm_object_split() might first transfer pages
1215b7b8a096SKonstantin Belousov 	 * that precede ma[0] in the current object to a new object,
1216b7b8a096SKonstantin Belousov 	 * and then this function incorrectly recreates those pages as
1217b7b8a096SKonstantin Belousov 	 * read-behind pages in the current object.
1218b7b8a096SKonstantin Belousov 	 */
12198ecbf14bSDoug Moore 	KASSERT(object->type == OBJT_SWAP,
12208ecbf14bSDoug Moore 	    ("%s: object not swappable", __func__));
1221b7b8a096SKonstantin Belousov 	if (!swap_pager_haspage(object, ma[0]->pindex, &maxbehind, &maxahead))
1222915d1b71SMark Johnston 		return (VM_PAGER_FAIL);
1223915d1b71SMark Johnston 
1224915d1b71SMark Johnston 	/*
1225915d1b71SMark Johnston 	 * Clip the readahead and readbehind ranges to exclude resident pages.
1226915d1b71SMark Johnston 	 */
1227915d1b71SMark Johnston 	if (rahead != NULL) {
1228dd9cb6daSMark Johnston 		KASSERT(reqcount - 1 <= maxahead,
1229915d1b71SMark Johnston 		    ("page count %d extends beyond swap block", reqcount));
1230dd9cb6daSMark Johnston 		*rahead = imin(*rahead, maxahead - (reqcount - 1));
12313f060b60SMark Johnston 		pindex = ma[reqcount - 1]->pindex;
12323f060b60SMark Johnston 		msucc = TAILQ_NEXT(ma[reqcount - 1], listq);
1233915d1b71SMark Johnston 		if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1234915d1b71SMark Johnston 			*rahead = msucc->pindex - pindex - 1;
1235915d1b71SMark Johnston 	}
1236915d1b71SMark Johnston 	if (rbehind != NULL) {
1237dd9cb6daSMark Johnston 		*rbehind = imin(*rbehind, maxbehind);
12383f060b60SMark Johnston 		pindex = ma[0]->pindex;
12393f060b60SMark Johnston 		mpred = TAILQ_PREV(ma[0], pglist, listq);
1240915d1b71SMark Johnston 		if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1241915d1b71SMark Johnston 			*rbehind = pindex - mpred->pindex - 1;
1242915d1b71SMark Johnston 	}
1243915d1b71SMark Johnston 
1244b7b8a096SKonstantin Belousov 	bm = ma[0];
1245b7b8a096SKonstantin Belousov 	for (i = 0; i < count; i++)
1246b7b8a096SKonstantin Belousov 		ma[i]->oflags |= VPO_SWAPINPROG;
1247b7b8a096SKonstantin Belousov 
1248915d1b71SMark Johnston 	/*
1249915d1b71SMark Johnston 	 * Allocate readahead and readbehind pages.
1250915d1b71SMark Johnston 	 */
1251b7b8a096SKonstantin Belousov 	if (rbehind != NULL) {
1252b7b8a096SKonstantin Belousov 		for (i = 1; i <= *rbehind; i++) {
12533f060b60SMark Johnston 			p = vm_page_alloc(object, ma[0]->pindex - i,
12547667839aSAlan Cox 			    VM_ALLOC_NORMAL);
1255b7b8a096SKonstantin Belousov 			if (p == NULL)
1256915d1b71SMark Johnston 				break;
1257b7b8a096SKonstantin Belousov 			p->oflags |= VPO_SWAPINPROG;
1258b7b8a096SKonstantin Belousov 			bm = p;
1259915d1b71SMark Johnston 		}
1260b7b8a096SKonstantin Belousov 		*rbehind = i - 1;
1261915d1b71SMark Johnston 	}
1262915d1b71SMark Johnston 	if (rahead != NULL) {
1263915d1b71SMark Johnston 		for (i = 0; i < *rahead; i++) {
1264915d1b71SMark Johnston 			p = vm_page_alloc(object,
12653f060b60SMark Johnston 			    ma[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1266915d1b71SMark Johnston 			if (p == NULL)
1267915d1b71SMark Johnston 				break;
1268b7b8a096SKonstantin Belousov 			p->oflags |= VPO_SWAPINPROG;
1269915d1b71SMark Johnston 		}
1270915d1b71SMark Johnston 		*rahead = i;
1271915d1b71SMark Johnston 	}
1272915d1b71SMark Johnston 	if (rbehind != NULL)
1273915d1b71SMark Johnston 		count += *rbehind;
1274915d1b71SMark Johnston 	if (rahead != NULL)
1275915d1b71SMark Johnston 		count += *rahead;
1276915d1b71SMark Johnston 
1277915d1b71SMark Johnston 	vm_object_pip_add(object, count);
1278915d1b71SMark Johnston 
1279b7b8a096SKonstantin Belousov 	pindex = bm->pindex;
12808ecbf14bSDoug Moore 	blk = swp_pager_meta_lookup(object, pindex);
1281915d1b71SMark Johnston 	KASSERT(blk != SWAPBLK_NONE,
1282915d1b71SMark Johnston 	    ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1283915d1b71SMark Johnston 
1284915d1b71SMark Johnston 	VM_OBJECT_WUNLOCK(object);
1285756a5412SGleb Smirnoff 	bp = uma_zalloc(swrbuf_zone, M_WAITOK);
1286b7b8a096SKonstantin Belousov 	/* Pages cannot leave the object while busy. */
1287b7b8a096SKonstantin Belousov 	for (i = 0, p = bm; i < count; i++, p = TAILQ_NEXT(p, listq)) {
1288b7b8a096SKonstantin Belousov 		MPASS(p->pindex == bm->pindex + i);
1289b7b8a096SKonstantin Belousov 		bp->b_pages[i] = p;
1290b7b8a096SKonstantin Belousov 	}
1291915d1b71SMark Johnston 
1292915d1b71SMark Johnston 	bp->b_flags |= B_PAGING;
129321144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
12941c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1295fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1296fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
1297b0cd2017SGleb Smirnoff 	bp->b_blkno = blk;
1298b0cd2017SGleb Smirnoff 	bp->b_bcount = PAGE_SIZE * count;
1299b0cd2017SGleb Smirnoff 	bp->b_bufsize = PAGE_SIZE * count;
1300b0cd2017SGleb Smirnoff 	bp->b_npages = count;
1301915d1b71SMark Johnston 	bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1302915d1b71SMark Johnston 	bp->b_pgafter = rahead != NULL ? *rahead : 0;
130326f9a767SRodney W. Grimes 
130483c9dea1SGleb Smirnoff 	VM_CNT_INC(v_swapin);
130583c9dea1SGleb Smirnoff 	VM_CNT_ADD(v_swappgsin, count);
13061c7c3c6aSMatthew Dillon 
13071c7c3c6aSMatthew Dillon 	/*
13081c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
13091c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
13101c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
13111c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
13121c7c3c6aSMatthew Dillon 	 *
13133f060b60SMark Johnston 	 * The other pages in our ma[] array are also released on completion,
13141c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
13151c7c3c6aSMatthew Dillon 	 *
1316c37a77eeSPoul-Henning Kamp 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
13171c7c3c6aSMatthew Dillon 	 */
1318b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
13194b03903aSPoul-Henning Kamp 	swp_pager_strategy(bp);
132026f9a767SRodney W. Grimes 
132126f9a767SRodney W. Grimes 	/*
1322915d1b71SMark Johnston 	 * Wait for the pages we want to complete.  VPO_SWAPINPROG is always
13231c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1324915d1b71SMark Johnston 	 * is set in the metadata for each page in the request.
132526f9a767SRodney W. Grimes 	 */
132689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
13273f060b60SMark Johnston 	while ((ma[0]->oflags & VPO_SWAPINPROG) != 0) {
13283f060b60SMark Johnston 		ma[0]->oflags |= VPO_SWAPSLEEP;
132983c9dea1SGleb Smirnoff 		VM_CNT_INC(v_intrans);
1330cf27e0d1SJeff Roberson 		if (VM_OBJECT_SLEEP(object, &object->handle, PSWP,
1331c7aebda8SAttilio Rao 		    "swread", hz * 20)) {
13329bd86a98SBruce M Simpson 			printf(
1333c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1334c5690651SPoul-Henning Kamp 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
13351c7c3c6aSMatthew Dillon 		}
13361b119d9dSDavid Greenman 	}
133726f9a767SRodney W. Grimes 
133826f9a767SRodney W. Grimes 	/*
1339b0cd2017SGleb Smirnoff 	 * If we had an unrecoverable read error pages will not be valid.
134026f9a767SRodney W. Grimes 	 */
1341915d1b71SMark Johnston 	for (i = 0; i < reqcount; i++)
13423f060b60SMark Johnston 		if (ma[i]->valid != VM_PAGE_BITS_ALL)
13431c7c3c6aSMatthew Dillon 			return (VM_PAGER_ERROR);
1344b0cd2017SGleb Smirnoff 
13451c7c3c6aSMatthew Dillon 	return (VM_PAGER_OK);
13461c7c3c6aSMatthew Dillon 
13471c7c3c6aSMatthew Dillon 	/*
13481c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
13491c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
13501c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
13511c7c3c6aSMatthew Dillon 	 * to all-zero's later.
13521c7c3c6aSMatthew Dillon 	 */
1353df8bae1dSRodney W. Grimes }
1354df8bae1dSRodney W. Grimes 
13551c7c3c6aSMatthew Dillon /*
135690effb23SGleb Smirnoff  * 	swap_pager_getpages_async():
135790effb23SGleb Smirnoff  *
135890effb23SGleb Smirnoff  *	Right now this is emulation of asynchronous operation on top of
135990effb23SGleb Smirnoff  *	swap_pager_getpages().
136090effb23SGleb Smirnoff  */
136190effb23SGleb Smirnoff static int
13623f060b60SMark Johnston swap_pager_getpages_async(vm_object_t object, vm_page_t *ma, int count,
1363b0cd2017SGleb Smirnoff     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
136490effb23SGleb Smirnoff {
136590effb23SGleb Smirnoff 	int r, error;
136690effb23SGleb Smirnoff 
13673f060b60SMark Johnston 	r = swap_pager_getpages(object, ma, count, rbehind, rahead);
136890effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
136990effb23SGleb Smirnoff 	switch (r) {
137090effb23SGleb Smirnoff 	case VM_PAGER_OK:
137190effb23SGleb Smirnoff 		error = 0;
137290effb23SGleb Smirnoff 		break;
137390effb23SGleb Smirnoff 	case VM_PAGER_ERROR:
137490effb23SGleb Smirnoff 		error = EIO;
137590effb23SGleb Smirnoff 		break;
137690effb23SGleb Smirnoff 	case VM_PAGER_FAIL:
137790effb23SGleb Smirnoff 		error = EINVAL;
137890effb23SGleb Smirnoff 		break;
137990effb23SGleb Smirnoff 	default:
1380d9328101SGleb Smirnoff 		panic("unhandled swap_pager_getpages() error %d", r);
138190effb23SGleb Smirnoff 	}
13823f060b60SMark Johnston 	(iodone)(arg, ma, count, error);
138390effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
138490effb23SGleb Smirnoff 
138590effb23SGleb Smirnoff 	return (r);
138690effb23SGleb Smirnoff }
138790effb23SGleb Smirnoff 
138890effb23SGleb Smirnoff /*
13891c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
13901c7c3c6aSMatthew Dillon  *
13911c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
13921c7c3c6aSMatthew Dillon  *
13931c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
13941c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
13951c7c3c6aSMatthew Dillon  *
1396ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
13971c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
13981c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
13991c7c3c6aSMatthew Dillon  *	which needs work.
14001c7c3c6aSMatthew Dillon  *
14011c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
14021c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
14031c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
14041c7c3c6aSMatthew Dillon  *	completion.
14051c7c3c6aSMatthew Dillon  *
14061c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
140723612f0dSDoug Moore  *	those whose rtvals[] entry is not set to VM_PAGER_PEND on return.
14081c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
14091c7c3c6aSMatthew Dillon  */
1410d635a37fSWarner Losh static void
14113f060b60SMark Johnston swap_pager_putpages(vm_object_t object, vm_page_t *ma, int count,
1412e065e87cSKonstantin Belousov     int flags, int *rtvals)
1413df8bae1dSRodney W. Grimes {
141423612f0dSDoug Moore 	struct buf *bp;
141523612f0dSDoug Moore 	daddr_t addr, blk, n_free, s_free;
141623612f0dSDoug Moore 	vm_page_t mreq;
141723612f0dSDoug Moore 	int i, j, n;
141823612f0dSDoug Moore 	bool async;
1419df8bae1dSRodney W. Grimes 
142023612f0dSDoug Moore 	KASSERT(count == 0 || ma[0]->object == object,
142123612f0dSDoug Moore 	    ("%s: object mismatch %p/%p",
142223612f0dSDoug Moore 	    __func__, object, ma[0]->object));
1423ee3dc7d7SAlan Cox 
14241c7c3c6aSMatthew Dillon 	/*
14251c7c3c6aSMatthew Dillon 	 * Step 1
14261c7c3c6aSMatthew Dillon 	 *
142723612f0dSDoug Moore 	 * Turn object into OBJT_SWAP.  Force sync if not a pageout process.
14281c7c3c6aSMatthew Dillon 	 */
142978f1deefSAlan Cox 	if (object->type != OBJT_SWAP) {
143078f1deefSAlan Cox 		addr = swp_pager_meta_build(object, 0, SWAPBLK_NONE);
143178f1deefSAlan Cox 		KASSERT(addr == SWAPBLK_NONE,
143278f1deefSAlan Cox 		    ("unexpected object swap block"));
143378f1deefSAlan Cox 	}
143489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
143523612f0dSDoug Moore 	async = curproc == pageproc && (flags & VM_PAGER_PUT_SYNC) == 0;
143623612f0dSDoug Moore 	swp_pager_init_freerange(&s_free, &n_free);
143726f9a767SRodney W. Grimes 
14381c7c3c6aSMatthew Dillon 	/*
14391c7c3c6aSMatthew Dillon 	 * Step 2
14401c7c3c6aSMatthew Dillon 	 *
14411c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
14421c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
14431c7c3c6aSMatthew Dillon 	 * successfully.
14441c7c3c6aSMatthew Dillon 	 */
14451c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
144631c82722SDoug Moore 		/* Maximum I/O size is limited by maximum swap block size. */
144731c82722SDoug Moore 		n = min(count - i, nsw_cluster_max);
14481c7c3c6aSMatthew Dillon 
144948e98a2aSDoug Moore 		/* Get a block of swap of size up to size n. */
145048e98a2aSDoug Moore 		blk = swp_pager_getswapspace(&n, 4);
14511c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
14524dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
14531c7c3c6aSMatthew Dillon 				rtvals[i + j] = VM_PAGER_FAIL;
14541c7c3c6aSMatthew Dillon 			continue;
145526f9a767SRodney W. Grimes 		}
145626f9a767SRodney W. Grimes 
145726f9a767SRodney W. Grimes 		/*
145823612f0dSDoug Moore 		 * All I/O parameters have been satisfied.  Build the I/O
14591c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
146026f9a767SRodney W. Grimes 		 */
146123612f0dSDoug Moore 		if (async) {
1462756a5412SGleb Smirnoff 			mtx_lock(&swbuf_mtx);
1463756a5412SGleb Smirnoff 			while (nsw_wcount_async == 0)
1464756a5412SGleb Smirnoff 				msleep(&nsw_wcount_async, &swbuf_mtx, PVM,
1465756a5412SGleb Smirnoff 				    "swbufa", 0);
1466756a5412SGleb Smirnoff 			nsw_wcount_async--;
1467756a5412SGleb Smirnoff 			mtx_unlock(&swbuf_mtx);
1468327f4e83SMatthew Dillon 		}
1469756a5412SGleb Smirnoff 		bp = uma_zalloc(swwbuf_zone, M_WAITOK);
147023612f0dSDoug Moore 		if (async)
1471756a5412SGleb Smirnoff 			bp->b_flags = B_ASYNC;
14725e04322aSPoul-Henning Kamp 		bp->b_flags |= B_PAGING;
1473912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
147426f9a767SRodney W. Grimes 
1475fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1476fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
14771c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
14781c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
14791c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1480e47ed70bSJohn Dyson 
148189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
14821c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
148323612f0dSDoug Moore 			mreq = ma[i + j];
1484*a8081778SJeff Roberson 			vm_page_aflag_clear(mreq, PGA_SWAP_FREE);
148578f1deefSAlan Cox 			addr = swp_pager_meta_build(mreq->object, mreq->pindex,
148678f1deefSAlan Cox 			    blk + j);
148778f1deefSAlan Cox 			if (addr != SWAPBLK_NONE)
148878f1deefSAlan Cox 				swp_pager_update_freerange(&s_free, &n_free,
148978f1deefSAlan Cox 				    addr);
149087b0ab69SAlan Cox 			MPASS(mreq->dirty == VM_PAGE_BITS_ALL);
14915786be7cSAlan Cox 			mreq->oflags |= VPO_SWAPINPROG;
14921c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
14931c7c3c6aSMatthew Dillon 		}
149489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
14951c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1496a5296b05SJulian Elischer 		/*
1497a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1498a5296b05SJulian Elischer 		 */
1499a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1500a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
15011c7c3c6aSMatthew Dillon 
150283c9dea1SGleb Smirnoff 		VM_CNT_INC(v_swapout);
150383c9dea1SGleb Smirnoff 		VM_CNT_ADD(v_swappgsout, bp->b_npages);
150426f9a767SRodney W. Grimes 
150526f9a767SRodney W. Grimes 		/*
150677923df2SAlan Cox 		 * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
150777923df2SAlan Cox 		 * can call the async completion routine at the end of a
150877923df2SAlan Cox 		 * synchronous I/O operation.  Otherwise, our caller would
150977923df2SAlan Cox 		 * perform duplicate unbusy and wakeup operations on the page
151077923df2SAlan Cox 		 * and object, respectively.
151177923df2SAlan Cox 		 */
151277923df2SAlan Cox 		for (j = 0; j < n; j++)
151377923df2SAlan Cox 			rtvals[i + j] = VM_PAGER_PEND;
151477923df2SAlan Cox 
151577923df2SAlan Cox 		/*
15161c7c3c6aSMatthew Dillon 		 * asynchronous
15171c7c3c6aSMatthew Dillon 		 *
151823612f0dSDoug Moore 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy.
151926f9a767SRodney W. Grimes 		 */
152023612f0dSDoug Moore 		if (async) {
15211c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
152267812eacSKirk McKusick 			BUF_KERNPROC(bp);
15234b03903aSPoul-Henning Kamp 			swp_pager_strategy(bp);
15241c7c3c6aSMatthew Dillon 			continue;
152526f9a767SRodney W. Grimes 		}
1526e47ed70bSJohn Dyson 
152726f9a767SRodney W. Grimes 		/*
15281c7c3c6aSMatthew Dillon 		 * synchronous
15291c7c3c6aSMatthew Dillon 		 *
153023612f0dSDoug Moore 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy.
15311c7c3c6aSMatthew Dillon 		 */
15322c840b1fSAlan Cox 		bp->b_iodone = bdone;
15334b03903aSPoul-Henning Kamp 		swp_pager_strategy(bp);
15341c7c3c6aSMatthew Dillon 
15351c7c3c6aSMatthew Dillon 		/*
153677923df2SAlan Cox 		 * Wait for the sync I/O to complete.
153726f9a767SRodney W. Grimes 		 */
15382c840b1fSAlan Cox 		bwait(bp, PVM, "swwrt");
153977923df2SAlan Cox 
15401c7c3c6aSMatthew Dillon 		/*
15411c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
15421c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
15431c7c3c6aSMatthew Dillon 		 */
15441c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
15451c7c3c6aSMatthew Dillon 	}
154678f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
154723612f0dSDoug Moore 	VM_OBJECT_WLOCK(object);
15481c7c3c6aSMatthew Dillon }
15491c7c3c6aSMatthew Dillon 
15501c7c3c6aSMatthew Dillon /*
15511c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
15521c7c3c6aSMatthew Dillon  *
15531c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
15541c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
15551c7c3c6aSMatthew Dillon  *
155615523cf7SKonstantin Belousov  *	This routine may not sleep.
15571c7c3c6aSMatthew Dillon  */
15581c7c3c6aSMatthew Dillon static void
15592f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp)
15601c7c3c6aSMatthew Dillon {
15611c7c3c6aSMatthew Dillon 	int i;
15621c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
15631c7c3c6aSMatthew Dillon 
15641c7c3c6aSMatthew Dillon 	/*
15650b208315SEdward Tomasz Napierala 	 * Report error - unless we ran out of memory, in which case
15660b208315SEdward Tomasz Napierala 	 * we've already logged it in swapgeom_strategy().
15671c7c3c6aSMatthew Dillon 	 */
15680b208315SEdward Tomasz Napierala 	if (bp->b_ioflags & BIO_ERROR && bp->b_error != ENOMEM) {
15691c7c3c6aSMatthew Dillon 		printf(
15701c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
15711c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
157221144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
15731c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
15741c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
15751c7c3c6aSMatthew Dillon 		    bp->b_error
15761c7c3c6aSMatthew Dillon 		);
15771c7c3c6aSMatthew Dillon 	}
15781c7c3c6aSMatthew Dillon 
15791c7c3c6aSMatthew Dillon 	/*
158026f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
158126f9a767SRodney W. Grimes 	 */
1582fade8dd7SJeff Roberson 	if (buf_mapped(bp))
15831c7c3c6aSMatthew Dillon 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1584fade8dd7SJeff Roberson 	else
1585fade8dd7SJeff Roberson 		bp->b_data = bp->b_kvabase;
158626f9a767SRodney W. Grimes 
158733a609ecSAlan Cox 	if (bp->b_npages) {
158833a609ecSAlan Cox 		object = bp->b_pages[0]->object;
158989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
159033a609ecSAlan Cox 	}
15912965a453SKip Macy 
159226f9a767SRodney W. Grimes 	/*
15931c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
15941c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
15951c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
15961c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
15971c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
15981c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
159926f9a767SRodney W. Grimes 	 */
16001c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
16011c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1602e47ed70bSJohn Dyson 
16035786be7cSAlan Cox 		m->oflags &= ~VPO_SWAPINPROG;
1604c7aebda8SAttilio Rao 		if (m->oflags & VPO_SWAPSLEEP) {
1605c7aebda8SAttilio Rao 			m->oflags &= ~VPO_SWAPSLEEP;
1606cf27e0d1SJeff Roberson 			wakeup(&object->handle);
1607c7aebda8SAttilio Rao 		}
1608e47ed70bSJohn Dyson 
1609*a8081778SJeff Roberson 		/* We always have space after I/O, successful or not. */
1610*a8081778SJeff Roberson 		vm_page_aflag_set(m, PGA_SWAP_SPACE);
1611*a8081778SJeff Roberson 
1612c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1613ffc82b0aSJohn Dyson 			/*
16141c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
16151c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
16161c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
16171c7c3c6aSMatthew Dillon 			 * interrupt.
1618ffc82b0aSJohn Dyson 			 */
161921144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
16201c7c3c6aSMatthew Dillon 				/*
16211c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1622956f3135SPhilippe Charnier 				 * be overridden by the original caller of
16231c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
16241c7c3c6aSMatthew Dillon 				 */
16250012f373SJeff Roberson 				vm_page_invalid(m);
16261c7c3c6aSMatthew Dillon 			} else {
16271c7c3c6aSMatthew Dillon 				/*
16281c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
16291c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
16301c7c3c6aSMatthew Dillon 				 * then finish the I/O.
16311c7c3c6aSMatthew Dillon 				 */
163241e5a226SAlan Cox 				MPASS(m->dirty == VM_PAGE_BITS_ALL);
1633*a8081778SJeff Roberson 				/* PQ_UNSWAPPABLE? */
16343c4a2440SAlan Cox 				vm_page_lock(m);
16351c7c3c6aSMatthew Dillon 				vm_page_activate(m);
16363c4a2440SAlan Cox 				vm_page_unlock(m);
1637c7aebda8SAttilio Rao 				vm_page_sunbusy(m);
16381c7c3c6aSMatthew Dillon 			}
163921144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
16401c7c3c6aSMatthew Dillon 			/*
16411c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1642956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
16431c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
16441c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
16451c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
16461c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
16471c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
16481c7c3c6aSMatthew Dillon 			 */
1649016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1650016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is mapped", m));
1651016a3c93SAlan Cox 			KASSERT(m->dirty == 0,
1652016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is dirty", m));
1653915d1b71SMark Johnston 
16540012f373SJeff Roberson 			vm_page_valid(m);
1655915d1b71SMark Johnston 			if (i < bp->b_pgbefore ||
1656915d1b71SMark Johnston 			    i >= bp->b_npages - bp->b_pgafter)
1657915d1b71SMark Johnston 				vm_page_readahead_finish(m);
16581c7c3c6aSMatthew Dillon 		} else {
16591c7c3c6aSMatthew Dillon 			/*
1660016a3c93SAlan Cox 			 * For write success, clear the dirty
16611c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
16621c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
1663ebcddc72SAlan Cox 			 * A page is only written to swap after a period of
1664ebcddc72SAlan Cox 			 * inactivity.  Therefore, we do not expect it to be
1665ebcddc72SAlan Cox 			 * reused.
16661c7c3c6aSMatthew Dillon 			 */
16676031c68dSAlan Cox 			KASSERT(!pmap_page_is_write_mapped(m),
1668016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is not write"
1669016a3c93SAlan Cox 			    " protected", m));
1670c52e7044SAlan Cox 			vm_page_undirty(m);
16713c4a2440SAlan Cox 			vm_page_lock(m);
1672ebcddc72SAlan Cox 			vm_page_deactivate_noreuse(m);
16732965a453SKip Macy 			vm_page_unlock(m);
1674ebcddc72SAlan Cox 			vm_page_sunbusy(m);
16753c4a2440SAlan Cox 		}
16763c4a2440SAlan Cox 	}
167726f9a767SRodney W. Grimes 
16781c7c3c6aSMatthew Dillon 	/*
16791c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
16801c7c3c6aSMatthew Dillon 	 * pip refs on the object.
16811c7c3c6aSMatthew Dillon 	 */
16820d420ad3SAlan Cox 	if (object != NULL) {
16831c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
168489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
16850d420ad3SAlan Cox 	}
168626f9a767SRodney W. Grimes 
16871c7c3c6aSMatthew Dillon 	/*
1688100650deSOlivier Houchard 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1689100650deSOlivier Houchard 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1690100650deSOlivier Houchard 	 * trigger a KASSERT in relpbuf().
1691100650deSOlivier Houchard 	 */
1692100650deSOlivier Houchard 	if (bp->b_vp) {
1693100650deSOlivier Houchard 		    bp->b_vp = NULL;
1694100650deSOlivier Houchard 		    bp->b_bufobj = NULL;
1695100650deSOlivier Houchard 	}
1696100650deSOlivier Houchard 	/*
16971c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
16981c7c3c6aSMatthew Dillon 	 */
1699756a5412SGleb Smirnoff 	if (bp->b_flags & B_ASYNC) {
1700756a5412SGleb Smirnoff 		mtx_lock(&swbuf_mtx);
1701756a5412SGleb Smirnoff 		if (++nsw_wcount_async == 1)
1702756a5412SGleb Smirnoff 			wakeup(&nsw_wcount_async);
1703756a5412SGleb Smirnoff 		mtx_unlock(&swbuf_mtx);
1704756a5412SGleb Smirnoff 	}
1705756a5412SGleb Smirnoff 	uma_zfree((bp->b_iocmd == BIO_READ) ? swrbuf_zone : swwbuf_zone, bp);
170626f9a767SRodney W. Grimes }
17071c7c3c6aSMatthew Dillon 
1708b1fd102eSMark Johnston int
1709b1fd102eSMark Johnston swap_pager_nswapdev(void)
1710b1fd102eSMark Johnston {
1711b1fd102eSMark Johnston 
1712b1fd102eSMark Johnston 	return (nswapdev);
1713b1fd102eSMark Johnston }
1714b1fd102eSMark Johnston 
17157c022327SDoug Moore static void
17167c022327SDoug Moore swp_pager_force_dirty(vm_page_t m)
17177c022327SDoug Moore {
17187c022327SDoug Moore 
17197c022327SDoug Moore 	vm_page_dirty(m);
1720e8bcf696SMark Johnston #ifdef INVARIANTS
1721e8bcf696SMark Johnston 	vm_page_lock(m);
17225cff1f4dSMark Johnston 	if (!vm_page_wired(m) && m->a.queue == PQ_NONE)
1723e8bcf696SMark Johnston 		panic("page %p is neither wired nor queued", m);
1724e8bcf696SMark Johnston 	vm_page_unlock(m);
1725e8bcf696SMark Johnston #endif
17267c022327SDoug Moore 	vm_page_xunbusy(m);
172756948d17SDoug Moore 	swap_pager_unswapped(m);
17287c022327SDoug Moore }
17297c022327SDoug Moore 
17307c022327SDoug Moore static void
17317c022327SDoug Moore swp_pager_force_launder(vm_page_t m)
17327c022327SDoug Moore {
17337c022327SDoug Moore 
17347c022327SDoug Moore 	vm_page_dirty(m);
17357c022327SDoug Moore 	vm_page_lock(m);
17367c022327SDoug Moore 	vm_page_launder(m);
17377c022327SDoug Moore 	vm_page_unlock(m);
17387c022327SDoug Moore 	vm_page_xunbusy(m);
173956948d17SDoug Moore 	swap_pager_unswapped(m);
17407c022327SDoug Moore }
17417c022327SDoug Moore 
174292da00bbSMatthew Dillon /*
174356948d17SDoug Moore  * SWP_PAGER_FORCE_PAGEIN() - force swap blocks to be paged in
174492da00bbSMatthew Dillon  *
174556948d17SDoug Moore  *	This routine dissociates pages starting at the given index within an
174656948d17SDoug Moore  *	object from their backing store, paging them in if they do not reside
174756948d17SDoug Moore  *	in memory.  Pages that are paged in are marked dirty and placed in the
174856948d17SDoug Moore  *	laundry queue.  Pages are marked dirty because they no longer have
174956948d17SDoug Moore  *	backing store.  They are placed in the laundry queue because they have
175056948d17SDoug Moore  *	not been accessed recently.  Otherwise, they would already reside in
175156948d17SDoug Moore  *	memory.
175292da00bbSMatthew Dillon  */
17537c022327SDoug Moore static void
175456948d17SDoug Moore swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex, int npages)
175592da00bbSMatthew Dillon {
175656948d17SDoug Moore 	vm_page_t ma[npages];
175756948d17SDoug Moore 	int i, j;
175892da00bbSMatthew Dillon 
175956948d17SDoug Moore 	KASSERT(npages > 0, ("%s: No pages", __func__));
176056948d17SDoug Moore 	KASSERT(npages <= MAXPHYS / PAGE_SIZE,
176156948d17SDoug Moore 	    ("%s: Too many pages: %d", __func__, npages));
17628ecbf14bSDoug Moore 	KASSERT(object->type == OBJT_SWAP,
17638ecbf14bSDoug Moore 	    ("%s: Object not swappable", __func__));
176456948d17SDoug Moore 	vm_object_pip_add(object, npages);
176556948d17SDoug Moore 	vm_page_grab_pages(object, pindex, VM_ALLOC_NORMAL, ma, npages);
176656948d17SDoug Moore 	for (i = j = 0;; i++) {
176756948d17SDoug Moore 		/* Count nonresident pages, to page-in all at once. */
176856948d17SDoug Moore 		if (i < npages && ma[i]->valid != VM_PAGE_BITS_ALL)
176956948d17SDoug Moore 			continue;
177056948d17SDoug Moore 		if (j < i) {
177156948d17SDoug Moore 			/* Page-in nonresident pages. Mark for laundering. */
177256948d17SDoug Moore 			if (swap_pager_getpages(object, &ma[j], i - j, NULL,
177356948d17SDoug Moore 			    NULL) != VM_PAGER_OK)
177456948d17SDoug Moore 				panic("%s: read from swap failed", __func__);
177556948d17SDoug Moore 			do {
177656948d17SDoug Moore 				swp_pager_force_launder(ma[j]);
177756948d17SDoug Moore 			} while (++j < i);
177892da00bbSMatthew Dillon 		}
177956948d17SDoug Moore 		if (i == npages)
178056948d17SDoug Moore 			break;
178156948d17SDoug Moore 		/* Mark dirty a resident page. */
178256948d17SDoug Moore 		swp_pager_force_dirty(ma[j++]);
178356948d17SDoug Moore 	}
178456948d17SDoug Moore 	vm_object_pip_wakeupn(object, npages);
178592da00bbSMatthew Dillon }
178692da00bbSMatthew Dillon 
178792da00bbSMatthew Dillon /*
17887c022327SDoug Moore  *	swap_pager_swapoff_object:
17897c022327SDoug Moore  *
17907c022327SDoug Moore  *	Page in all of the pages that have been paged out for an object
179156948d17SDoug Moore  *	to a swap device.
17927c022327SDoug Moore  */
17937c022327SDoug Moore static void
17947c022327SDoug Moore swap_pager_swapoff_object(struct swdevt *sp, vm_object_t object)
17957c022327SDoug Moore {
17967c022327SDoug Moore 	struct swblk *sb;
179756948d17SDoug Moore 	vm_pindex_t pi, s_pindex;
179856948d17SDoug Moore 	daddr_t blk, n_blks, s_blk;
17997c022327SDoug Moore 	int i;
18007c022327SDoug Moore 
18018ecbf14bSDoug Moore 	KASSERT(object->type == OBJT_SWAP,
18028ecbf14bSDoug Moore 	    ("%s: Object not swappable", __func__));
180356948d17SDoug Moore 	n_blks = 0;
18047c022327SDoug Moore 	for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
18057c022327SDoug Moore 	    &object->un_pager.swp.swp_blks, pi)) != NULL; ) {
18067c022327SDoug Moore 		for (i = 0; i < SWAP_META_PAGES; i++) {
180756948d17SDoug Moore 			blk = sb->d[i];
180856948d17SDoug Moore 			if (!swp_pager_isondev(blk, sp))
180956948d17SDoug Moore 				blk = SWAPBLK_NONE;
181056948d17SDoug Moore 
181156948d17SDoug Moore 			/*
181256948d17SDoug Moore 			 * If there are no blocks/pages accumulated, start a new
181356948d17SDoug Moore 			 * accumulation here.
181456948d17SDoug Moore 			 */
181556948d17SDoug Moore 			if (n_blks == 0) {
181656948d17SDoug Moore 				if (blk != SWAPBLK_NONE) {
181756948d17SDoug Moore 					s_blk = blk;
181856948d17SDoug Moore 					s_pindex = sb->p + i;
181956948d17SDoug Moore 					n_blks = 1;
182056948d17SDoug Moore 				}
18217c022327SDoug Moore 				continue;
18227c022327SDoug Moore 			}
182356948d17SDoug Moore 
182456948d17SDoug Moore 			/*
182556948d17SDoug Moore 			 * If the accumulation can be extended without breaking
182656948d17SDoug Moore 			 * the sequence of consecutive blocks and pages that
182756948d17SDoug Moore 			 * swp_pager_force_pagein() depends on, do so.
182856948d17SDoug Moore 			 */
182956948d17SDoug Moore 			if (n_blks < MAXPHYS / PAGE_SIZE &&
183056948d17SDoug Moore 			    s_blk + n_blks == blk &&
183156948d17SDoug Moore 			    s_pindex + n_blks == sb->p + i) {
183256948d17SDoug Moore 				++n_blks;
183356948d17SDoug Moore 				continue;
18347c022327SDoug Moore 			}
183556948d17SDoug Moore 
183656948d17SDoug Moore 			/*
183756948d17SDoug Moore 			 * The sequence of consecutive blocks and pages cannot
183856948d17SDoug Moore 			 * be extended, so page them all in here.  Then,
183956948d17SDoug Moore 			 * because doing so involves releasing and reacquiring
184056948d17SDoug Moore 			 * a lock that protects the swap block pctrie, do not
184156948d17SDoug Moore 			 * rely on the current swap block.  Break this loop and
184256948d17SDoug Moore 			 * re-fetch the same pindex from the pctrie again.
184356948d17SDoug Moore 			 */
184456948d17SDoug Moore 			swp_pager_force_pagein(object, s_pindex, n_blks);
184556948d17SDoug Moore 			n_blks = 0;
184656948d17SDoug Moore 			break;
184756948d17SDoug Moore 		}
184856948d17SDoug Moore 		if (i == SWAP_META_PAGES)
184956948d17SDoug Moore 			pi = sb->p + SWAP_META_PAGES;
185056948d17SDoug Moore 	}
185156948d17SDoug Moore 	if (n_blks > 0)
185256948d17SDoug Moore 		swp_pager_force_pagein(object, s_pindex, n_blks);
18537c022327SDoug Moore }
18547c022327SDoug Moore 
18557c022327SDoug Moore /*
185692da00bbSMatthew Dillon  *	swap_pager_swapoff:
185792da00bbSMatthew Dillon  *
185892da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
185992da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
186092da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
186192da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
186292da00bbSMatthew Dillon  *
186392da00bbSMatthew Dillon  *	This routine may block.
186492da00bbSMatthew Dillon  */
1865e9c0cc15SPoul-Henning Kamp static void
1866b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp)
186792da00bbSMatthew Dillon {
1868f425ab8eSKonstantin Belousov 	vm_object_t object;
18697c022327SDoug Moore 	int retries;
187092da00bbSMatthew Dillon 
187104533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
187292da00bbSMatthew Dillon 
18738bc61209SDavid Schultz 	retries = 0;
187492da00bbSMatthew Dillon full_rescan:
1875f425ab8eSKonstantin Belousov 	mtx_lock(&vm_object_list_mtx);
1876f425ab8eSKonstantin Belousov 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
1877f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
187898150664SKonstantin Belousov 			continue;
1879f425ab8eSKonstantin Belousov 		mtx_unlock(&vm_object_list_mtx);
188098150664SKonstantin Belousov 		/* Depends on type-stability. */
188198150664SKonstantin Belousov 		VM_OBJECT_WLOCK(object);
1882f425ab8eSKonstantin Belousov 
1883f425ab8eSKonstantin Belousov 		/*
1884f425ab8eSKonstantin Belousov 		 * Dead objects are eventually terminated on their own.
1885f425ab8eSKonstantin Belousov 		 */
1886f425ab8eSKonstantin Belousov 		if ((object->flags & OBJ_DEAD) != 0)
1887f425ab8eSKonstantin Belousov 			goto next_obj;
1888f425ab8eSKonstantin Belousov 
1889f425ab8eSKonstantin Belousov 		/*
1890f425ab8eSKonstantin Belousov 		 * Sync with fences placed after pctrie
1891f425ab8eSKonstantin Belousov 		 * initialization.  We must not access pctrie below
1892f425ab8eSKonstantin Belousov 		 * unless we checked that our object is swap and not
1893f425ab8eSKonstantin Belousov 		 * dead.
1894f425ab8eSKonstantin Belousov 		 */
1895f425ab8eSKonstantin Belousov 		atomic_thread_fence_acq();
1896f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
1897f425ab8eSKonstantin Belousov 			goto next_obj;
1898f425ab8eSKonstantin Belousov 
18997c022327SDoug Moore 		swap_pager_swapoff_object(sp, object);
1900f425ab8eSKonstantin Belousov next_obj:
1901f425ab8eSKonstantin Belousov 		VM_OBJECT_WUNLOCK(object);
1902f425ab8eSKonstantin Belousov 		mtx_lock(&vm_object_list_mtx);
190392da00bbSMatthew Dillon 	}
1904f425ab8eSKonstantin Belousov 	mtx_unlock(&vm_object_list_mtx);
1905f425ab8eSKonstantin Belousov 
19068bc61209SDavid Schultz 	if (sp->sw_used) {
190792da00bbSMatthew Dillon 		/*
19088bc61209SDavid Schultz 		 * Objects may be locked or paging to the device being
19098bc61209SDavid Schultz 		 * removed, so we will miss their pages and need to
19108bc61209SDavid Schultz 		 * make another pass.  We have marked this device as
19118bc61209SDavid Schultz 		 * SW_CLOSING, so the activity should finish soon.
191292da00bbSMatthew Dillon 		 */
19138bc61209SDavid Schultz 		retries++;
19148bc61209SDavid Schultz 		if (retries > 100) {
19158bc61209SDavid Schultz 			panic("swapoff: failed to locate %d swap blocks",
19168bc61209SDavid Schultz 			    sp->sw_used);
19178bc61209SDavid Schultz 		}
19184d70511aSJohn Baldwin 		pause("swpoff", hz / 20);
191992da00bbSMatthew Dillon 		goto full_rescan;
192092da00bbSMatthew Dillon 	}
1921b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapoff, sp);
192292da00bbSMatthew Dillon }
192392da00bbSMatthew Dillon 
19241c7c3c6aSMatthew Dillon /************************************************************************
19251c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
19261c7c3c6aSMatthew Dillon  ************************************************************************
19271c7c3c6aSMatthew Dillon  *
19281c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
1929cec9f109SDavid E. O'Brien  *	OBJT_SWAP object.
19301c7c3c6aSMatthew Dillon  *
19314dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
19324dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
19334dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
19341c7c3c6aSMatthew Dillon  */
19351c7c3c6aSMatthew Dillon 
19361c7c3c6aSMatthew Dillon /*
1937230869e0SAlan Cox  * SWP_PAGER_SWBLK_EMPTY() - is a range of blocks free?
1938230869e0SAlan Cox  */
1939230869e0SAlan Cox static bool
1940230869e0SAlan Cox swp_pager_swblk_empty(struct swblk *sb, int start, int limit)
1941230869e0SAlan Cox {
1942230869e0SAlan Cox 	int i;
1943230869e0SAlan Cox 
1944230869e0SAlan Cox 	MPASS(0 <= start && start <= limit && limit <= SWAP_META_PAGES);
1945230869e0SAlan Cox 	for (i = start; i < limit; i++) {
1946230869e0SAlan Cox 		if (sb->d[i] != SWAPBLK_NONE)
1947230869e0SAlan Cox 			return (false);
1948230869e0SAlan Cox 	}
1949230869e0SAlan Cox 	return (true);
1950230869e0SAlan Cox }
1951230869e0SAlan Cox 
1952230869e0SAlan Cox /*
1953abdab7b6SDoug Moore  * SWP_PAGER_FREE_EMPTY_SWBLK() - frees if a block is free
1954abdab7b6SDoug Moore  *
1955abdab7b6SDoug Moore  *  Nothing is done if the block is still in use.
1956abdab7b6SDoug Moore  */
1957abdab7b6SDoug Moore static void
1958abdab7b6SDoug Moore swp_pager_free_empty_swblk(vm_object_t object, struct swblk *sb)
1959abdab7b6SDoug Moore {
1960abdab7b6SDoug Moore 
1961abdab7b6SDoug Moore 	if (swp_pager_swblk_empty(sb, 0, SWAP_META_PAGES)) {
1962abdab7b6SDoug Moore 		SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
1963abdab7b6SDoug Moore 		uma_zfree(swblk_zone, sb);
1964abdab7b6SDoug Moore 	}
1965abdab7b6SDoug Moore }
1966abdab7b6SDoug Moore 
1967abdab7b6SDoug Moore /*
19681c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
19691c7c3c6aSMatthew Dillon  *
19701c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
19711c7c3c6aSMatthew Dillon  *	object.
19721c7c3c6aSMatthew Dillon  *
19731c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
19741c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
197578f1deefSAlan Cox  *	assigned swapblk is returned.
19761c7c3c6aSMatthew Dillon  */
197778f1deefSAlan Cox static daddr_t
19782f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
19792f249180SPoul-Henning Kamp {
1980f425ab8eSKonstantin Belousov 	static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted;
1981eed99cb8SKonstantin Belousov 	struct swblk *sb, *sb1;
1982f425ab8eSKonstantin Belousov 	vm_pindex_t modpi, rdpi;
198378f1deefSAlan Cox 	daddr_t prev_swapblk;
1984f425ab8eSKonstantin Belousov 	int error, i;
19851c7c3c6aSMatthew Dillon 
198689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
1987f425ab8eSKonstantin Belousov 
19881c7c3c6aSMatthew Dillon 	/*
19891c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
19901c7c3c6aSMatthew Dillon 	 */
19911c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
1992f425ab8eSKonstantin Belousov 		pctrie_init(&object->un_pager.swp.swp_blks);
1993f425ab8eSKonstantin Belousov 
1994f425ab8eSKonstantin Belousov 		/*
1995f425ab8eSKonstantin Belousov 		 * Ensure that swap_pager_swapoff()'s iteration over
1996f425ab8eSKonstantin Belousov 		 * object_list does not see a garbage pctrie.
1997f425ab8eSKonstantin Belousov 		 */
1998f425ab8eSKonstantin Belousov 		atomic_thread_fence_rel();
1999f425ab8eSKonstantin Belousov 
20001c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
2001fe7bcbafSKyle Evans 		object->un_pager.swp.writemappings = 0;
200267388836SKonstantin Belousov 		KASSERT((object->flags & OBJ_ANON) != 0 ||
200367388836SKonstantin Belousov 		    object->handle == NULL,
200467388836SKonstantin Belousov 		    ("default pager %p with handle %p",
200567388836SKonstantin Belousov 		    object, object->handle));
2006bd228075SAlan Cox 	}
20071c7c3c6aSMatthew Dillon 
2008f425ab8eSKonstantin Belousov 	rdpi = rounddown(pindex, SWAP_META_PAGES);
2009f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi);
2010f425ab8eSKonstantin Belousov 	if (sb == NULL) {
20111c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
201278f1deefSAlan Cox 			return (SWAPBLK_NONE);
2013f425ab8eSKonstantin Belousov 		for (;;) {
2014f425ab8eSKonstantin Belousov 			sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc ==
2015f425ab8eSKonstantin Belousov 			    pageproc ? M_USE_RESERVE : 0));
2016f425ab8eSKonstantin Belousov 			if (sb != NULL) {
2017f425ab8eSKonstantin Belousov 				sb->p = rdpi;
2018f425ab8eSKonstantin Belousov 				for (i = 0; i < SWAP_META_PAGES; i++)
2019f425ab8eSKonstantin Belousov 					sb->d[i] = SWAPBLK_NONE;
2020f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swblk_zone_exhausted,
2021f425ab8eSKonstantin Belousov 				    1, 0))
2022f425ab8eSKonstantin Belousov 					printf("swblk zone ok\n");
2023f425ab8eSKonstantin Belousov 				break;
2024f425ab8eSKonstantin Belousov 			}
202589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
2026f425ab8eSKonstantin Belousov 			if (uma_zone_exhausted(swblk_zone)) {
2027f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swblk_zone_exhausted,
2028f425ab8eSKonstantin Belousov 				    0, 1))
2029f425ab8eSKonstantin Belousov 					printf("swap blk zone exhausted, "
20303ff863f1SDag-Erling Smørgrav 					    "increase kern.maxswzone\n");
20312025d69bSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
2032f425ab8eSKonstantin Belousov 				pause("swzonxb", 10);
20332025d69bSKonstantin Belousov 			} else
20348d6fbbb8SJeff Roberson 				uma_zwait(swblk_zone);
203589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
2036eed99cb8SKonstantin Belousov 			sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2037eed99cb8SKonstantin Belousov 			    rdpi);
2038eed99cb8SKonstantin Belousov 			if (sb != NULL)
2039eed99cb8SKonstantin Belousov 				/*
2040eed99cb8SKonstantin Belousov 				 * Somebody swapped out a nearby page,
2041eed99cb8SKonstantin Belousov 				 * allocating swblk at the rdpi index,
2042eed99cb8SKonstantin Belousov 				 * while we dropped the object lock.
2043eed99cb8SKonstantin Belousov 				 */
2044eed99cb8SKonstantin Belousov 				goto allocated;
20454dcc5c2dSMatthew Dillon 		}
2046f425ab8eSKonstantin Belousov 		for (;;) {
2047f425ab8eSKonstantin Belousov 			error = SWAP_PCTRIE_INSERT(
2048f425ab8eSKonstantin Belousov 			    &object->un_pager.swp.swp_blks, sb);
2049f425ab8eSKonstantin Belousov 			if (error == 0) {
2050f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
2051f425ab8eSKonstantin Belousov 				    1, 0))
2052f425ab8eSKonstantin Belousov 					printf("swpctrie zone ok\n");
2053f425ab8eSKonstantin Belousov 				break;
20541c7c3c6aSMatthew Dillon 			}
2055f425ab8eSKonstantin Belousov 			VM_OBJECT_WUNLOCK(object);
2056f425ab8eSKonstantin Belousov 			if (uma_zone_exhausted(swpctrie_zone)) {
2057f425ab8eSKonstantin Belousov 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
2058f425ab8eSKonstantin Belousov 				    0, 1))
2059f425ab8eSKonstantin Belousov 					printf("swap pctrie zone exhausted, "
2060f425ab8eSKonstantin Belousov 					    "increase kern.maxswzone\n");
2061f425ab8eSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
2062f425ab8eSKonstantin Belousov 				pause("swzonxp", 10);
2063f425ab8eSKonstantin Belousov 			} else
20648d6fbbb8SJeff Roberson 				uma_zwait(swpctrie_zone);
2065f425ab8eSKonstantin Belousov 			VM_OBJECT_WLOCK(object);
2066eed99cb8SKonstantin Belousov 			sb1 = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2067eed99cb8SKonstantin Belousov 			    rdpi);
2068eed99cb8SKonstantin Belousov 			if (sb1 != NULL) {
2069eed99cb8SKonstantin Belousov 				uma_zfree(swblk_zone, sb);
2070eed99cb8SKonstantin Belousov 				sb = sb1;
2071eed99cb8SKonstantin Belousov 				goto allocated;
20721c7c3c6aSMatthew Dillon 			}
2073f425ab8eSKonstantin Belousov 		}
2074eed99cb8SKonstantin Belousov 	}
2075eed99cb8SKonstantin Belousov allocated:
2076f425ab8eSKonstantin Belousov 	MPASS(sb->p == rdpi);
20771c7c3c6aSMatthew Dillon 
2078f425ab8eSKonstantin Belousov 	modpi = pindex % SWAP_META_PAGES;
207978f1deefSAlan Cox 	/* Return prior contents of metadata. */
208078f1deefSAlan Cox 	prev_swapblk = sb->d[modpi];
2081f425ab8eSKonstantin Belousov 	/* Enter block into metadata. */
2082f425ab8eSKonstantin Belousov 	sb->d[modpi] = swapblk;
208385d88d87SKonstantin Belousov 
208485d88d87SKonstantin Belousov 	/*
208585d88d87SKonstantin Belousov 	 * Free the swblk if we end up with the empty page run.
208685d88d87SKonstantin Belousov 	 */
2087abdab7b6SDoug Moore 	if (swapblk == SWAPBLK_NONE)
2088abdab7b6SDoug Moore 	    swp_pager_free_empty_swblk(object, sb);
208978f1deefSAlan Cox 	return (prev_swapblk);
209085d88d87SKonstantin Belousov }
20911c7c3c6aSMatthew Dillon 
20921c7c3c6aSMatthew Dillon /*
2093467057fcSDoug Moore  * SWP_PAGER_META_TRANSFER() - free a range of blocks in the srcobject's swap
2094467057fcSDoug Moore  * metadata, or transfer it into dstobject.
2095467057fcSDoug Moore  *
2096467057fcSDoug Moore  *	This routine will free swap metadata structures as they are cleaned
2097467057fcSDoug Moore  *	out.
2098467057fcSDoug Moore  */
2099467057fcSDoug Moore static void
2100467057fcSDoug Moore swp_pager_meta_transfer(vm_object_t srcobject, vm_object_t dstobject,
2101467057fcSDoug Moore     vm_pindex_t pindex, vm_pindex_t count)
2102467057fcSDoug Moore {
2103467057fcSDoug Moore 	struct swblk *sb;
2104467057fcSDoug Moore 	daddr_t n_free, s_free;
2105467057fcSDoug Moore 	vm_pindex_t offset, last;
2106467057fcSDoug Moore 	int i, limit, start;
2107467057fcSDoug Moore 
2108467057fcSDoug Moore 	VM_OBJECT_ASSERT_WLOCKED(srcobject);
2109467057fcSDoug Moore 	if (srcobject->type != OBJT_SWAP || count == 0)
2110467057fcSDoug Moore 		return;
2111467057fcSDoug Moore 
2112467057fcSDoug Moore 	swp_pager_init_freerange(&s_free, &n_free);
2113467057fcSDoug Moore 	offset = pindex;
2114467057fcSDoug Moore 	last = pindex + count;
2115467057fcSDoug Moore 	for (;;) {
2116467057fcSDoug Moore 		sb = SWAP_PCTRIE_LOOKUP_GE(&srcobject->un_pager.swp.swp_blks,
2117467057fcSDoug Moore 		    rounddown(pindex, SWAP_META_PAGES));
2118467057fcSDoug Moore 		if (sb == NULL || sb->p >= last)
2119467057fcSDoug Moore 			break;
2120467057fcSDoug Moore 		start = pindex > sb->p ? pindex - sb->p : 0;
2121467057fcSDoug Moore 		limit = last - sb->p < SWAP_META_PAGES ? last - sb->p :
2122467057fcSDoug Moore 		    SWAP_META_PAGES;
2123467057fcSDoug Moore 		for (i = start; i < limit; i++) {
2124467057fcSDoug Moore 			if (sb->d[i] == SWAPBLK_NONE)
2125467057fcSDoug Moore 				continue;
2126467057fcSDoug Moore 			if (dstobject == NULL ||
2127467057fcSDoug Moore 			    !swp_pager_xfer_source(srcobject, dstobject,
2128467057fcSDoug Moore 			    sb->p + i - offset, sb->d[i])) {
2129467057fcSDoug Moore 				swp_pager_update_freerange(&s_free, &n_free,
2130467057fcSDoug Moore 				    sb->d[i]);
2131467057fcSDoug Moore 			}
2132467057fcSDoug Moore 			sb->d[i] = SWAPBLK_NONE;
2133467057fcSDoug Moore 		}
2134467057fcSDoug Moore 		pindex = sb->p + SWAP_META_PAGES;
2135467057fcSDoug Moore 		if (swp_pager_swblk_empty(sb, 0, start) &&
2136467057fcSDoug Moore 		    swp_pager_swblk_empty(sb, limit, SWAP_META_PAGES)) {
2137467057fcSDoug Moore 			SWAP_PCTRIE_REMOVE(&srcobject->un_pager.swp.swp_blks,
2138467057fcSDoug Moore 			    sb->p);
2139467057fcSDoug Moore 			uma_zfree(swblk_zone, sb);
2140467057fcSDoug Moore 		}
2141467057fcSDoug Moore 	}
2142467057fcSDoug Moore 	swp_pager_freeswapspace(s_free, n_free);
2143467057fcSDoug Moore }
2144467057fcSDoug Moore 
2145467057fcSDoug Moore /*
21461c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
21471c7c3c6aSMatthew Dillon  *
21481c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
21491c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
21501c7c3c6aSMatthew Dillon  *
21511c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
21521c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
21531c7c3c6aSMatthew Dillon  *	with resident pages.
21541c7c3c6aSMatthew Dillon  */
21551c7c3c6aSMatthew Dillon static void
2156f425ab8eSKonstantin Belousov swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count)
21571c7c3c6aSMatthew Dillon {
2158467057fcSDoug Moore 	swp_pager_meta_transfer(object, NULL, pindex, count);
21591c7c3c6aSMatthew Dillon }
21601c7c3c6aSMatthew Dillon 
21611c7c3c6aSMatthew Dillon /*
21621c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
21631c7c3c6aSMatthew Dillon  *
21641c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
21651c7c3c6aSMatthew Dillon  *	an object.
21661c7c3c6aSMatthew Dillon  */
21671c7c3c6aSMatthew Dillon static void
21681c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
21691c7c3c6aSMatthew Dillon {
2170f425ab8eSKonstantin Belousov 	struct swblk *sb;
217178f1deefSAlan Cox 	daddr_t n_free, s_free;
2172f425ab8eSKonstantin Belousov 	vm_pindex_t pindex;
217371057cd2SKonstantin Belousov 	int i;
21741c7c3c6aSMatthew Dillon 
217589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
21761c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
21771c7c3c6aSMatthew Dillon 		return;
21781c7c3c6aSMatthew Dillon 
217978f1deefSAlan Cox 	swp_pager_init_freerange(&s_free, &n_free);
2180f425ab8eSKonstantin Belousov 	for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
2181f425ab8eSKonstantin Belousov 	    &object->un_pager.swp.swp_blks, pindex)) != NULL;) {
2182f425ab8eSKonstantin Belousov 		pindex = sb->p + SWAP_META_PAGES;
2183f425ab8eSKonstantin Belousov 		for (i = 0; i < SWAP_META_PAGES; i++) {
2184230869e0SAlan Cox 			if (sb->d[i] == SWAPBLK_NONE)
2185230869e0SAlan Cox 				continue;
218678f1deefSAlan Cox 			swp_pager_update_freerange(&s_free, &n_free, sb->d[i]);
21871c7c3c6aSMatthew Dillon 		}
2188f425ab8eSKonstantin Belousov 		SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
2189f425ab8eSKonstantin Belousov 		uma_zfree(swblk_zone, sb);
21901c7c3c6aSMatthew Dillon 	}
219178f1deefSAlan Cox 	swp_pager_freeswapspace(s_free, n_free);
21921c7c3c6aSMatthew Dillon }
21931c7c3c6aSMatthew Dillon 
21941c7c3c6aSMatthew Dillon /*
21954abca9bbSAlan Cox  * SWP_PAGER_METACTL() -  misc control of swap meta data.
21961c7c3c6aSMatthew Dillon  *
21974abca9bbSAlan Cox  *	This routine is capable of looking up, or removing swapblk
21984abca9bbSAlan Cox  *	assignments in the swap meta data.  It returns the swapblk being
21994abca9bbSAlan Cox  *	looked-up, popped, or SWAPBLK_NONE if the block was invalid.
22001c7c3c6aSMatthew Dillon  *
22011c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
22021c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
22031c7c3c6aSMatthew Dillon  *	busy page.
22041c7c3c6aSMatthew Dillon  */
22051c7c3c6aSMatthew Dillon static daddr_t
22068ecbf14bSDoug Moore swp_pager_meta_lookup(vm_object_t object, vm_pindex_t pindex)
22072f249180SPoul-Henning Kamp {
2208f425ab8eSKonstantin Belousov 	struct swblk *sb;
22094dcc5c2dSMatthew Dillon 
2210c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
2211ee620ea4SAlan Cox 
22121c7c3c6aSMatthew Dillon 	/*
2213ee620ea4SAlan Cox 	 * The meta data only exists if the object is OBJT_SWAP
22141c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
22151c7c3c6aSMatthew Dillon 	 */
22168ecbf14bSDoug Moore 	KASSERT(object->type == OBJT_SWAP,
22178ecbf14bSDoug Moore 	    ("Lookup object not swappable"));
22181c7c3c6aSMatthew Dillon 
2219f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
2220f425ab8eSKonstantin Belousov 	    rounddown(pindex, SWAP_META_PAGES));
2221f425ab8eSKonstantin Belousov 	if (sb == NULL)
2222f425ab8eSKonstantin Belousov 		return (SWAPBLK_NONE);
22238ecbf14bSDoug Moore 	return (sb->d[pindex % SWAP_META_PAGES]);
22241c7c3c6aSMatthew Dillon }
22251c7c3c6aSMatthew Dillon 
2226e9c0cc15SPoul-Henning Kamp /*
222777d6fd97SKonstantin Belousov  * Returns the least page index which is greater than or equal to the
222877d6fd97SKonstantin Belousov  * parameter pindex and for which there is a swap block allocated.
222977d6fd97SKonstantin Belousov  * Returns object's size if the object's type is not swap or if there
223077d6fd97SKonstantin Belousov  * are no allocated swap blocks for the object after the requested
223177d6fd97SKonstantin Belousov  * pindex.
223277d6fd97SKonstantin Belousov  */
223377d6fd97SKonstantin Belousov vm_pindex_t
223477d6fd97SKonstantin Belousov swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
223577d6fd97SKonstantin Belousov {
2236f425ab8eSKonstantin Belousov 	struct swblk *sb;
2237f425ab8eSKonstantin Belousov 	int i;
223877d6fd97SKonstantin Belousov 
223977d6fd97SKonstantin Belousov 	VM_OBJECT_ASSERT_LOCKED(object);
2240f425ab8eSKonstantin Belousov 	if (object->type != OBJT_SWAP)
224177d6fd97SKonstantin Belousov 		return (object->size);
224277d6fd97SKonstantin Belousov 
2243f425ab8eSKonstantin Belousov 	sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2244f425ab8eSKonstantin Belousov 	    rounddown(pindex, SWAP_META_PAGES));
2245f425ab8eSKonstantin Belousov 	if (sb == NULL)
2246f425ab8eSKonstantin Belousov 		return (object->size);
2247f425ab8eSKonstantin Belousov 	if (sb->p < pindex) {
2248f425ab8eSKonstantin Belousov 		for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) {
2249f425ab8eSKonstantin Belousov 			if (sb->d[i] != SWAPBLK_NONE)
2250f425ab8eSKonstantin Belousov 				return (sb->p + i);
225177d6fd97SKonstantin Belousov 		}
2252f425ab8eSKonstantin Belousov 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
2253f425ab8eSKonstantin Belousov 		    roundup(pindex, SWAP_META_PAGES));
2254f425ab8eSKonstantin Belousov 		if (sb == NULL)
2255f425ab8eSKonstantin Belousov 			return (object->size);
225677d6fd97SKonstantin Belousov 	}
2257f425ab8eSKonstantin Belousov 	for (i = 0; i < SWAP_META_PAGES; i++) {
2258f425ab8eSKonstantin Belousov 		if (sb->d[i] != SWAPBLK_NONE)
2259f425ab8eSKonstantin Belousov 			return (sb->p + i);
226077d6fd97SKonstantin Belousov 	}
2261f425ab8eSKonstantin Belousov 
2262f425ab8eSKonstantin Belousov 	/*
2263f425ab8eSKonstantin Belousov 	 * We get here if a swblk is present in the trie but it
2264f425ab8eSKonstantin Belousov 	 * doesn't map any blocks.
2265f425ab8eSKonstantin Belousov 	 */
2266f425ab8eSKonstantin Belousov 	MPASS(0);
2267f425ab8eSKonstantin Belousov 	return (object->size);
226877d6fd97SKonstantin Belousov }
226977d6fd97SKonstantin Belousov 
227077d6fd97SKonstantin Belousov /*
2271e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2272e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2273e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2274e9c0cc15SPoul-Henning Kamp  */
2275e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2276e9c0cc15SPoul-Henning Kamp struct swapon_args {
2277e9c0cc15SPoul-Henning Kamp 	char *name;
2278e9c0cc15SPoul-Henning Kamp };
2279e9c0cc15SPoul-Henning Kamp #endif
2280e9c0cc15SPoul-Henning Kamp 
2281e9c0cc15SPoul-Henning Kamp /*
2282e9c0cc15SPoul-Henning Kamp  * MPSAFE
2283e9c0cc15SPoul-Henning Kamp  */
2284e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2285e9c0cc15SPoul-Henning Kamp int
22868451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap)
2287e9c0cc15SPoul-Henning Kamp {
2288e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2289e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2290e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2291e9c0cc15SPoul-Henning Kamp 	int error;
2292e9c0cc15SPoul-Henning Kamp 
2293acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPON);
2294e9c0cc15SPoul-Henning Kamp 	if (error)
2295acd3428bSRobert Watson 		return (error);
2296e9c0cc15SPoul-Henning Kamp 
229704533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2298e9c0cc15SPoul-Henning Kamp 
2299e9c0cc15SPoul-Henning Kamp 	/*
2300e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2301e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2302e9c0cc15SPoul-Henning Kamp 	 */
2303f425ab8eSKonstantin Belousov 	if (swblk_zone == NULL) {
2304e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2305e9c0cc15SPoul-Henning Kamp 		goto done;
2306e9c0cc15SPoul-Henning Kamp 	}
2307e9c0cc15SPoul-Henning Kamp 
2308d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2309d9135e72SRobert Watson 	    uap->name, td);
2310e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2311e9c0cc15SPoul-Henning Kamp 	if (error)
2312e9c0cc15SPoul-Henning Kamp 		goto done;
2313e9c0cc15SPoul-Henning Kamp 
2314e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2315e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2316e9c0cc15SPoul-Henning Kamp 
231720da9c2eSPoul-Henning Kamp 	if (vn_isdisk(vp, &error)) {
231888ad2d7bSKonstantin Belousov 		error = swapongeom(vp);
231920da9c2eSPoul-Henning Kamp 	} else if (vp->v_type == VREG &&
2320e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
23210359a12eSAttilio Rao 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2322e9c0cc15SPoul-Henning Kamp 		/*
2323e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2324e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2325e9c0cc15SPoul-Henning Kamp 		 */
232659efee01SPoul-Henning Kamp 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2327e9c0cc15SPoul-Henning Kamp 	}
2328e9c0cc15SPoul-Henning Kamp 
2329e9c0cc15SPoul-Henning Kamp 	if (error)
2330e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2331e9c0cc15SPoul-Henning Kamp done:
233204533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2333e9c0cc15SPoul-Henning Kamp 	return (error);
2334e9c0cc15SPoul-Henning Kamp }
2335e9c0cc15SPoul-Henning Kamp 
23363ff863f1SDag-Erling Smørgrav /*
23373ff863f1SDag-Erling Smørgrav  * Check that the total amount of swap currently configured does not
23383ff863f1SDag-Erling Smørgrav  * exceed half the theoretical maximum.  If it does, print a warning
233935872e79SKonstantin Belousov  * message.
23403ff863f1SDag-Erling Smørgrav  */
234135872e79SKonstantin Belousov static void
234235872e79SKonstantin Belousov swapon_check_swzone(void)
23433ff863f1SDag-Erling Smørgrav {
23443ff863f1SDag-Erling Smørgrav 
23453ff863f1SDag-Erling Smørgrav 	/* recommend using no more than half that amount */
2346303fa05aSKonstantin Belousov 	if (swap_total > swap_maxpages / 2) {
23473ff863f1SDag-Erling Smørgrav 		printf("warning: total configured swap (%lu pages) "
23483ff863f1SDag-Erling Smørgrav 		    "exceeds maximum recommended amount (%lu pages).\n",
2349303fa05aSKonstantin Belousov 		    swap_total, swap_maxpages / 2);
23503ff863f1SDag-Erling Smørgrav 		printf("warning: increase kern.maxswzone "
23513ff863f1SDag-Erling Smørgrav 		    "or reduce amount of swap.\n");
23523ff863f1SDag-Erling Smørgrav 	}
23533ff863f1SDag-Erling Smørgrav }
23543ff863f1SDag-Erling Smørgrav 
235559efee01SPoul-Henning Kamp static void
23562cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks,
23572cc718a1SKonstantin Belousov     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2358e9c0cc15SPoul-Henning Kamp {
23592d9974c1SAlan Cox 	struct swdevt *sp, *tsp;
2360e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
23618f60c087SPoul-Henning Kamp 	u_long mblocks;
2362e9c0cc15SPoul-Henning Kamp 
2363e9c0cc15SPoul-Henning Kamp 	/*
2364e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2365e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2366e9c0cc15SPoul-Henning Kamp 	 *
2367e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2368e9c0cc15SPoul-Henning Kamp 	 */
2369e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2370e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2371e9c0cc15SPoul-Henning Kamp 
23726e903bd0SKonstantin Belousov 	/*
23736e903bd0SKonstantin Belousov 	 * If we go beyond this, we get overflows in the radix
23746e903bd0SKonstantin Belousov 	 * tree bitmap code.
23756e903bd0SKonstantin Belousov 	 */
23766e903bd0SKonstantin Belousov 	mblocks = 0x40000000 / BLIST_META_RADIX;
23776e903bd0SKonstantin Belousov 	if (nblks > mblocks) {
23786e903bd0SKonstantin Belousov 		printf(
23796e903bd0SKonstantin Belousov     "WARNING: reducing swap size to maximum of %luMB per unit\n",
23806e903bd0SKonstantin Belousov 		    mblocks / 1024 / 1024 * PAGE_SIZE);
23816e903bd0SKonstantin Belousov 		nblks = mblocks;
23826e903bd0SKonstantin Belousov 	}
23836e903bd0SKonstantin Belousov 
23848f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2385dee34ca4SPoul-Henning Kamp 	sp->sw_vp = vp;
2386dee34ca4SPoul-Henning Kamp 	sp->sw_id = id;
2387f3732fd1SPoul-Henning Kamp 	sp->sw_dev = dev;
2388e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2389e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
239059efee01SPoul-Henning Kamp 	sp->sw_strategy = strategy;
2391dee34ca4SPoul-Henning Kamp 	sp->sw_close = close;
23922cc718a1SKonstantin Belousov 	sp->sw_flags = flags;
2393e9c0cc15SPoul-Henning Kamp 
2394c8c7ad92SKip Macy 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2395e9c0cc15SPoul-Henning Kamp 	/*
2396504f5e29SDoug Moore 	 * Do not free the first blocks in order to avoid overwriting
23978f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2398e9c0cc15SPoul-Henning Kamp 	 */
2399504f5e29SDoug Moore 	blist_free(sp->sw_blist, howmany(BBSIZE, PAGE_SIZE),
2400504f5e29SDoug Moore 	    nblks - howmany(BBSIZE, PAGE_SIZE));
2401e9c0cc15SPoul-Henning Kamp 
24022d9974c1SAlan Cox 	dvbase = 0;
240320da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
24042d9974c1SAlan Cox 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
24052d9974c1SAlan Cox 		if (tsp->sw_end >= dvbase) {
24062d9974c1SAlan Cox 			/*
24072d9974c1SAlan Cox 			 * We put one uncovered page between the devices
24082d9974c1SAlan Cox 			 * in order to definitively prevent any cross-device
24092d9974c1SAlan Cox 			 * I/O requests
24102d9974c1SAlan Cox 			 */
24112d9974c1SAlan Cox 			dvbase = tsp->sw_end + 1;
24122d9974c1SAlan Cox 		}
24132d9974c1SAlan Cox 	}
24142d9974c1SAlan Cox 	sp->sw_first = dvbase;
24152d9974c1SAlan Cox 	sp->sw_end = dvbase + nblks;
24168f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
24178f60c087SPoul-Henning Kamp 	nswapdev++;
2418504f5e29SDoug Moore 	swap_pager_avail += nblks - howmany(BBSIZE, PAGE_SIZE);
2419e8bb589dSMatt Macy 	swap_total += nblks;
242035872e79SKonstantin Belousov 	swapon_check_swzone();
2421d05bc129SAlan Cox 	swp_sizecheck();
2422d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2423b1fd102eSMark Johnston 	EVENTHANDLER_INVOKE(swapon, sp);
242459efee01SPoul-Henning Kamp }
2425e9c0cc15SPoul-Henning Kamp 
2426e9c0cc15SPoul-Henning Kamp /*
2427e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2428e9c0cc15SPoul-Henning Kamp  *
2429e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2430dee34ca4SPoul-Henning Kamp  *
2431dee34ca4SPoul-Henning Kamp  * XXX: Badly designed system call: it should use a device index
2432dee34ca4SPoul-Henning Kamp  * rather than filename as specification.  We keep sw_vp around
2433dee34ca4SPoul-Henning Kamp  * only to make this work.
2434e9c0cc15SPoul-Henning Kamp  */
2435e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2436e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2437e9c0cc15SPoul-Henning Kamp 	char *name;
2438e9c0cc15SPoul-Henning Kamp };
2439e9c0cc15SPoul-Henning Kamp #endif
2440e9c0cc15SPoul-Henning Kamp 
2441e9c0cc15SPoul-Henning Kamp /*
2442e9c0cc15SPoul-Henning Kamp  * MPSAFE
2443e9c0cc15SPoul-Henning Kamp  */
2444e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2445e9c0cc15SPoul-Henning Kamp int
24468451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap)
2447e9c0cc15SPoul-Henning Kamp {
2448e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2449e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2450e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
24518f60c087SPoul-Henning Kamp 	int error;
2452e9c0cc15SPoul-Henning Kamp 
2453acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPOFF);
2454e9c0cc15SPoul-Henning Kamp 	if (error)
24550909f38aSPawel Jakub Dawidek 		return (error);
2456e9c0cc15SPoul-Henning Kamp 
245704533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
2458e9c0cc15SPoul-Henning Kamp 
2459d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2460d9135e72SRobert Watson 	    td);
2461e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2462e9c0cc15SPoul-Henning Kamp 	if (error)
2463e9c0cc15SPoul-Henning Kamp 		goto done;
2464e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2465e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2466e9c0cc15SPoul-Henning Kamp 
246720da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
24688f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2469dee34ca4SPoul-Henning Kamp 		if (sp->sw_vp == vp)
24700909f38aSPawel Jakub Dawidek 			break;
2471e9c0cc15SPoul-Henning Kamp 	}
247220da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
24730909f38aSPawel Jakub Dawidek 	if (sp == NULL) {
2474e9c0cc15SPoul-Henning Kamp 		error = EINVAL;
2475e9c0cc15SPoul-Henning Kamp 		goto done;
24760909f38aSPawel Jakub Dawidek 	}
247735918c55SChristian S.J. Peron 	error = swapoff_one(sp, td->td_ucred);
24780909f38aSPawel Jakub Dawidek done:
247904533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
24800909f38aSPawel Jakub Dawidek 	return (error);
24810909f38aSPawel Jakub Dawidek }
24820909f38aSPawel Jakub Dawidek 
24830909f38aSPawel Jakub Dawidek static int
248435918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred)
24850909f38aSPawel Jakub Dawidek {
248603bdd65fSAlan Cox 	u_long nblks;
2487e9c0cc15SPoul-Henning Kamp #ifdef MAC
24880909f38aSPawel Jakub Dawidek 	int error;
2489e9c0cc15SPoul-Henning Kamp #endif
2490e9c0cc15SPoul-Henning Kamp 
249104533e1eSKonstantin Belousov 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
24920909f38aSPawel Jakub Dawidek #ifdef MAC
2493cb05b60aSAttilio Rao 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
249435918c55SChristian S.J. Peron 	error = mac_system_check_swapoff(cred, sp->sw_vp);
249522db15c0SAttilio Rao 	(void) VOP_UNLOCK(sp->sw_vp, 0);
24960909f38aSPawel Jakub Dawidek 	if (error != 0)
24970909f38aSPawel Jakub Dawidek 		return (error);
24980909f38aSPawel Jakub Dawidek #endif
2499e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2500e9c0cc15SPoul-Henning Kamp 
2501e9c0cc15SPoul-Henning Kamp 	/*
2502e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2503e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2504e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2505e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2506e9c0cc15SPoul-Henning Kamp 	 */
2507e2068d0bSJeff Roberson 	if (vm_free_count() + swap_pager_avail < nblks + nswap_lowat)
25080909f38aSPawel Jakub Dawidek 		return (ENOMEM);
2509e9c0cc15SPoul-Henning Kamp 
2510e9c0cc15SPoul-Henning Kamp 	/*
2511e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2512e9c0cc15SPoul-Henning Kamp 	 */
25132928cef7SAlan Cox 	mtx_lock(&sw_dev_mtx);
2514e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
251503bdd65fSAlan Cox 	swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks);
2516e8bb589dSMatt Macy 	swap_total -= nblks;
25172928cef7SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2518e9c0cc15SPoul-Henning Kamp 
2519e9c0cc15SPoul-Henning Kamp 	/*
2520e9c0cc15SPoul-Henning Kamp 	 * Page in the contents of the device and close it.
2521e9c0cc15SPoul-Henning Kamp 	 */
2522b3fed13eSDavid Schultz 	swap_pager_swapoff(sp);
2523e9c0cc15SPoul-Henning Kamp 
252435918c55SChristian S.J. Peron 	sp->sw_close(curthread, sp);
252520da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
25269e3e3fe5SWarner Losh 	sp->sw_id = NULL;
25278f60c087SPoul-Henning Kamp 	TAILQ_REMOVE(&swtailq, sp, sw_list);
25280676a140SAlan Cox 	nswapdev--;
25297dea2c2eSAlan Cox 	if (nswapdev == 0) {
25307dea2c2eSAlan Cox 		swap_pager_full = 2;
25317dea2c2eSAlan Cox 		swap_pager_almost_full = 1;
25327dea2c2eSAlan Cox 	}
25338f60c087SPoul-Henning Kamp 	if (swdevhd == sp)
25348f60c087SPoul-Henning Kamp 		swdevhd = NULL;
2535d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
25368f60c087SPoul-Henning Kamp 	blist_destroy(sp->sw_blist);
25378f60c087SPoul-Henning Kamp 	free(sp, M_VMPGDATA);
25380909f38aSPawel Jakub Dawidek 	return (0);
25390909f38aSPawel Jakub Dawidek }
2540e9c0cc15SPoul-Henning Kamp 
25410909f38aSPawel Jakub Dawidek void
25420909f38aSPawel Jakub Dawidek swapoff_all(void)
25430909f38aSPawel Jakub Dawidek {
25440909f38aSPawel Jakub Dawidek 	struct swdevt *sp, *spt;
25450909f38aSPawel Jakub Dawidek 	const char *devname;
25460909f38aSPawel Jakub Dawidek 	int error;
25470909f38aSPawel Jakub Dawidek 
254804533e1eSKonstantin Belousov 	sx_xlock(&swdev_syscall_lock);
25490909f38aSPawel Jakub Dawidek 
25500909f38aSPawel Jakub Dawidek 	mtx_lock(&sw_dev_mtx);
25510909f38aSPawel Jakub Dawidek 	TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
25520909f38aSPawel Jakub Dawidek 		mtx_unlock(&sw_dev_mtx);
25530909f38aSPawel Jakub Dawidek 		if (vn_isdisk(sp->sw_vp, NULL))
25547870adb6SEd Schouten 			devname = devtoname(sp->sw_vp->v_rdev);
25550909f38aSPawel Jakub Dawidek 		else
25560909f38aSPawel Jakub Dawidek 			devname = "[file]";
255735918c55SChristian S.J. Peron 		error = swapoff_one(sp, thread0.td_ucred);
25580909f38aSPawel Jakub Dawidek 		if (error != 0) {
25590909f38aSPawel Jakub Dawidek 			printf("Cannot remove swap device %s (error=%d), "
25600909f38aSPawel Jakub Dawidek 			    "skipping.\n", devname, error);
25610909f38aSPawel Jakub Dawidek 		} else if (bootverbose) {
25620909f38aSPawel Jakub Dawidek 			printf("Swap device %s removed.\n", devname);
25630909f38aSPawel Jakub Dawidek 		}
25640909f38aSPawel Jakub Dawidek 		mtx_lock(&sw_dev_mtx);
25650909f38aSPawel Jakub Dawidek 	}
25660909f38aSPawel Jakub Dawidek 	mtx_unlock(&sw_dev_mtx);
25670909f38aSPawel Jakub Dawidek 
256804533e1eSKonstantin Belousov 	sx_xunlock(&swdev_syscall_lock);
2569e9c0cc15SPoul-Henning Kamp }
2570e9c0cc15SPoul-Henning Kamp 
2571567104a1SPoul-Henning Kamp void
2572567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used)
2573567104a1SPoul-Henning Kamp {
2574567104a1SPoul-Henning Kamp 	struct swdevt *sp;
2575567104a1SPoul-Henning Kamp 
2576567104a1SPoul-Henning Kamp 	*total = 0;
2577567104a1SPoul-Henning Kamp 	*used = 0;
257820da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
25798f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2580567104a1SPoul-Henning Kamp 		*total += sp->sw_nblks;
2581567104a1SPoul-Henning Kamp 		*used += sp->sw_used;
2582567104a1SPoul-Henning Kamp 	}
258320da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2584567104a1SPoul-Henning Kamp }
2585567104a1SPoul-Henning Kamp 
2586dda4f960SKonstantin Belousov int
2587dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2588dda4f960SKonstantin Belousov {
2589dda4f960SKonstantin Belousov 	struct swdevt *sp;
25907870adb6SEd Schouten 	const char *tmp_devname;
2591dda4f960SKonstantin Belousov 	int error, n;
2592dda4f960SKonstantin Belousov 
2593dda4f960SKonstantin Belousov 	n = 0;
2594dda4f960SKonstantin Belousov 	error = ENOENT;
2595dda4f960SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2596dda4f960SKonstantin Belousov 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2597dda4f960SKonstantin Belousov 		if (n != name) {
2598dda4f960SKonstantin Belousov 			n++;
2599dda4f960SKonstantin Belousov 			continue;
2600dda4f960SKonstantin Belousov 		}
2601dda4f960SKonstantin Belousov 		xs->xsw_version = XSWDEV_VERSION;
2602dda4f960SKonstantin Belousov 		xs->xsw_dev = sp->sw_dev;
2603dda4f960SKonstantin Belousov 		xs->xsw_flags = sp->sw_flags;
2604dda4f960SKonstantin Belousov 		xs->xsw_nblks = sp->sw_nblks;
2605dda4f960SKonstantin Belousov 		xs->xsw_used = sp->sw_used;
2606dda4f960SKonstantin Belousov 		if (devname != NULL) {
2607dda4f960SKonstantin Belousov 			if (vn_isdisk(sp->sw_vp, NULL))
26087870adb6SEd Schouten 				tmp_devname = devtoname(sp->sw_vp->v_rdev);
2609dda4f960SKonstantin Belousov 			else
2610dda4f960SKonstantin Belousov 				tmp_devname = "[file]";
2611dda4f960SKonstantin Belousov 			strncpy(devname, tmp_devname, len);
2612dda4f960SKonstantin Belousov 		}
2613dda4f960SKonstantin Belousov 		error = 0;
2614dda4f960SKonstantin Belousov 		break;
2615dda4f960SKonstantin Belousov 	}
2616dda4f960SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2617dda4f960SKonstantin Belousov 	return (error);
2618dda4f960SKonstantin Belousov }
2619dda4f960SKonstantin Belousov 
262069921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
262169921123SKonstantin Belousov #define XSWDEV_VERSION_11	1
262269921123SKonstantin Belousov struct xswdev11 {
262369921123SKonstantin Belousov 	u_int	xsw_version;
262469921123SKonstantin Belousov 	uint32_t xsw_dev;
262569921123SKonstantin Belousov 	int	xsw_flags;
262669921123SKonstantin Belousov 	int	xsw_nblks;
262769921123SKonstantin Belousov 	int     xsw_used;
262869921123SKonstantin Belousov };
262969921123SKonstantin Belousov #endif
263069921123SKonstantin Belousov 
2631f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2632f6d281e8SKonstantin Belousov struct xswdev32 {
2633f6d281e8SKonstantin Belousov 	u_int	xsw_version;
2634f6d281e8SKonstantin Belousov 	u_int	xsw_dev1, xsw_dev2;
2635f6d281e8SKonstantin Belousov 	int	xsw_flags;
2636f6d281e8SKonstantin Belousov 	int	xsw_nblks;
2637f6d281e8SKonstantin Belousov 	int     xsw_used;
2638f6d281e8SKonstantin Belousov };
2639f6d281e8SKonstantin Belousov #endif
2640f6d281e8SKonstantin Belousov 
2641e9c0cc15SPoul-Henning Kamp static int
2642e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2643e9c0cc15SPoul-Henning Kamp {
2644e9c0cc15SPoul-Henning Kamp 	struct xswdev xs;
2645f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2646f6d281e8SKonstantin Belousov 	struct xswdev32 xs32;
2647f6d281e8SKonstantin Belousov #endif
264869921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
264969921123SKonstantin Belousov 	struct xswdev11 xs11;
265069921123SKonstantin Belousov #endif
2651dda4f960SKonstantin Belousov 	int error;
2652e9c0cc15SPoul-Henning Kamp 
2653e9c0cc15SPoul-Henning Kamp 	if (arg2 != 1)			/* name length */
2654e9c0cc15SPoul-Henning Kamp 		return (EINVAL);
2655dda4f960SKonstantin Belousov 	error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2656dda4f960SKonstantin Belousov 	if (error != 0)
2657dda4f960SKonstantin Belousov 		return (error);
2658f6d281e8SKonstantin Belousov #if defined(__amd64__) && defined(COMPAT_FREEBSD32)
2659f6d281e8SKonstantin Belousov 	if (req->oldlen == sizeof(xs32)) {
2660f6d281e8SKonstantin Belousov 		xs32.xsw_version = XSWDEV_VERSION;
2661f6d281e8SKonstantin Belousov 		xs32.xsw_dev1 = xs.xsw_dev;
2662f6d281e8SKonstantin Belousov 		xs32.xsw_dev2 = xs.xsw_dev >> 32;
2663f6d281e8SKonstantin Belousov 		xs32.xsw_flags = xs.xsw_flags;
2664f6d281e8SKonstantin Belousov 		xs32.xsw_nblks = xs.xsw_nblks;
2665f6d281e8SKonstantin Belousov 		xs32.xsw_used = xs.xsw_used;
2666f6d281e8SKonstantin Belousov 		error = SYSCTL_OUT(req, &xs32, sizeof(xs32));
2667f6d281e8SKonstantin Belousov 		return (error);
2668f6d281e8SKonstantin Belousov 	}
2669f6d281e8SKonstantin Belousov #endif
267069921123SKonstantin Belousov #if defined(COMPAT_FREEBSD11)
267169921123SKonstantin Belousov 	if (req->oldlen == sizeof(xs11)) {
267269921123SKonstantin Belousov 		xs11.xsw_version = XSWDEV_VERSION_11;
267369921123SKonstantin Belousov 		xs11.xsw_dev = xs.xsw_dev; /* truncation */
267469921123SKonstantin Belousov 		xs11.xsw_flags = xs.xsw_flags;
267569921123SKonstantin Belousov 		xs11.xsw_nblks = xs.xsw_nblks;
267669921123SKonstantin Belousov 		xs11.xsw_used = xs.xsw_used;
267769921123SKonstantin Belousov 		error = SYSCTL_OUT(req, &xs11, sizeof(xs11));
2678f6d281e8SKonstantin Belousov 		return (error);
2679f6d281e8SKonstantin Belousov 	}
268069921123SKonstantin Belousov #endif
2681e9c0cc15SPoul-Henning Kamp 	error = SYSCTL_OUT(req, &xs, sizeof(xs));
2682e9c0cc15SPoul-Henning Kamp 	return (error);
2683e9c0cc15SPoul-Henning Kamp }
2684e9c0cc15SPoul-Henning Kamp 
26858f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2686e9c0cc15SPoul-Henning Kamp     "Number of swap devices");
26874c36e917SKonstantin Belousov SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE,
26884c36e917SKonstantin Belousov     sysctl_vm_swap_info,
2689e9c0cc15SPoul-Henning Kamp     "Swap statistics by device");
2690ec38b344SPoul-Henning Kamp 
2691ec38b344SPoul-Henning Kamp /*
2692f425ab8eSKonstantin Belousov  * Count the approximate swap usage in pages for a vmspace.  The
2693f425ab8eSKonstantin Belousov  * shadowed or not yet copied on write swap blocks are not accounted.
2694ec38b344SPoul-Henning Kamp  * The map must be locked.
2695ec38b344SPoul-Henning Kamp  */
26962860553aSRebecca Cran long
2697ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2698ec38b344SPoul-Henning Kamp {
269965d8409cSRebecca Cran 	vm_map_t map;
2700ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
270165d8409cSRebecca Cran 	vm_object_t object;
2702f425ab8eSKonstantin Belousov 	struct swblk *sb;
2703f425ab8eSKonstantin Belousov 	vm_pindex_t e, pi;
2704f425ab8eSKonstantin Belousov 	long count;
2705f425ab8eSKonstantin Belousov 	int i;
270665d8409cSRebecca Cran 
270765d8409cSRebecca Cran 	map = &vmspace->vm_map;
270865d8409cSRebecca Cran 	count = 0;
2709ec38b344SPoul-Henning Kamp 
27102288078cSDoug Moore 	VM_MAP_ENTRY_FOREACH(cur, map) {
2711f425ab8eSKonstantin Belousov 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2712f425ab8eSKonstantin Belousov 			continue;
2713f425ab8eSKonstantin Belousov 		object = cur->object.vm_object;
2714f425ab8eSKonstantin Belousov 		if (object == NULL || object->type != OBJT_SWAP)
2715f425ab8eSKonstantin Belousov 			continue;
2716f425ab8eSKonstantin Belousov 		VM_OBJECT_RLOCK(object);
2717f425ab8eSKonstantin Belousov 		if (object->type != OBJT_SWAP)
2718f425ab8eSKonstantin Belousov 			goto unlock;
2719f425ab8eSKonstantin Belousov 		pi = OFF_TO_IDX(cur->offset);
2720f425ab8eSKonstantin Belousov 		e = pi + OFF_TO_IDX(cur->end - cur->start);
2721f425ab8eSKonstantin Belousov 		for (;; pi = sb->p + SWAP_META_PAGES) {
2722f425ab8eSKonstantin Belousov 			sb = SWAP_PCTRIE_LOOKUP_GE(
2723f425ab8eSKonstantin Belousov 			    &object->un_pager.swp.swp_blks, pi);
2724f425ab8eSKonstantin Belousov 			if (sb == NULL || sb->p >= e)
2725f425ab8eSKonstantin Belousov 				break;
2726f425ab8eSKonstantin Belousov 			for (i = 0; i < SWAP_META_PAGES; i++) {
2727f425ab8eSKonstantin Belousov 				if (sb->p + i < e &&
2728f425ab8eSKonstantin Belousov 				    sb->d[i] != SWAPBLK_NONE)
2729f425ab8eSKonstantin Belousov 					count++;
2730ec38b344SPoul-Henning Kamp 			}
2731ec38b344SPoul-Henning Kamp 		}
2732f425ab8eSKonstantin Belousov unlock:
2733f425ab8eSKonstantin Belousov 		VM_OBJECT_RUNLOCK(object);
2734ec38b344SPoul-Henning Kamp 	}
2735ec38b344SPoul-Henning Kamp 	return (count);
2736ec38b344SPoul-Henning Kamp }
2737dee34ca4SPoul-Henning Kamp 
2738dee34ca4SPoul-Henning Kamp /*
2739dee34ca4SPoul-Henning Kamp  * GEOM backend
2740dee34ca4SPoul-Henning Kamp  *
2741dee34ca4SPoul-Henning Kamp  * Swapping onto disk devices.
2742dee34ca4SPoul-Henning Kamp  *
2743dee34ca4SPoul-Henning Kamp  */
2744dee34ca4SPoul-Henning Kamp 
27455721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan;
27465721c9c7SPoul-Henning Kamp 
2747dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = {
2748dee34ca4SPoul-Henning Kamp 	.name = "SWAP",
27495721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
27505721c9c7SPoul-Henning Kamp 	.orphan = swapgeom_orphan,
2751dee34ca4SPoul-Henning Kamp };
2752dee34ca4SPoul-Henning Kamp 
2753dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class);
2754dee34ca4SPoul-Henning Kamp 
2755dee34ca4SPoul-Henning Kamp 
2756dee34ca4SPoul-Henning Kamp static void
27573398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags)
27583398491bSAlexander Motin {
27593398491bSAlexander Motin 	struct g_consumer *cp;
27603398491bSAlexander Motin 
27613398491bSAlexander Motin 	cp = arg;
27623398491bSAlexander Motin 	g_access(cp, -1, -1, 0);
27633398491bSAlexander Motin 	g_detach(cp);
27643398491bSAlexander Motin 	g_destroy_consumer(cp);
27653398491bSAlexander Motin }
27663398491bSAlexander Motin 
27679e3e3fe5SWarner Losh /*
27689e3e3fe5SWarner Losh  * Add a reference to the g_consumer for an inflight transaction.
27699e3e3fe5SWarner Losh  */
27709e3e3fe5SWarner Losh static void
27719e3e3fe5SWarner Losh swapgeom_acquire(struct g_consumer *cp)
27729e3e3fe5SWarner Losh {
27739e3e3fe5SWarner Losh 
27749e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
27759e3e3fe5SWarner Losh 	cp->index++;
27769e3e3fe5SWarner Losh }
27779e3e3fe5SWarner Losh 
27789e3e3fe5SWarner Losh /*
27790c657d22SKonstantin Belousov  * Remove a reference from the g_consumer.  Post a close event if all
27800c657d22SKonstantin Belousov  * references go away, since the function might be called from the
27810c657d22SKonstantin Belousov  * biodone context.
27829e3e3fe5SWarner Losh  */
27839e3e3fe5SWarner Losh static void
27849e3e3fe5SWarner Losh swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
27859e3e3fe5SWarner Losh {
27869e3e3fe5SWarner Losh 
27879e3e3fe5SWarner Losh 	mtx_assert(&sw_dev_mtx, MA_OWNED);
27889e3e3fe5SWarner Losh 	cp->index--;
27899e3e3fe5SWarner Losh 	if (cp->index == 0) {
27909e3e3fe5SWarner Losh 		if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
27919e3e3fe5SWarner Losh 			sp->sw_id = NULL;
27929e3e3fe5SWarner Losh 	}
27939e3e3fe5SWarner Losh }
27949e3e3fe5SWarner Losh 
27953398491bSAlexander Motin static void
2796dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2)
2797dee34ca4SPoul-Henning Kamp {
27983398491bSAlexander Motin 	struct swdevt *sp;
2799dee34ca4SPoul-Henning Kamp 	struct buf *bp;
28003398491bSAlexander Motin 	struct g_consumer *cp;
2801dee34ca4SPoul-Henning Kamp 
2802dee34ca4SPoul-Henning Kamp 	bp = bp2->bio_caller2;
28033398491bSAlexander Motin 	cp = bp2->bio_from;
2804c5d3d25eSPoul-Henning Kamp 	bp->b_ioflags = bp2->bio_flags;
2805dee34ca4SPoul-Henning Kamp 	if (bp2->bio_error)
2806dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2807c5d3d25eSPoul-Henning Kamp 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2808c5d3d25eSPoul-Henning Kamp 	bp->b_error = bp2->bio_error;
2809756a5412SGleb Smirnoff 	bp->b_caller1 = NULL;
2810dee34ca4SPoul-Henning Kamp 	bufdone(bp);
28113398491bSAlexander Motin 	sp = bp2->bio_caller1;
28129e3e3fe5SWarner Losh 	mtx_lock(&sw_dev_mtx);
28139e3e3fe5SWarner Losh 	swapgeom_release(cp, sp);
28143398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
2815dee34ca4SPoul-Henning Kamp 	g_destroy_bio(bp2);
2816dee34ca4SPoul-Henning Kamp }
2817dee34ca4SPoul-Henning Kamp 
2818dee34ca4SPoul-Henning Kamp static void
2819dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2820dee34ca4SPoul-Henning Kamp {
2821dee34ca4SPoul-Henning Kamp 	struct bio *bio;
2822dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2823dee34ca4SPoul-Henning Kamp 
28243398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
2825dee34ca4SPoul-Henning Kamp 	cp = sp->sw_id;
2826dee34ca4SPoul-Henning Kamp 	if (cp == NULL) {
28273398491bSAlexander Motin 		mtx_unlock(&sw_dev_mtx);
2828dee34ca4SPoul-Henning Kamp 		bp->b_error = ENXIO;
2829dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2830dee34ca4SPoul-Henning Kamp 		bufdone(bp);
2831dee34ca4SPoul-Henning Kamp 		return;
2832dee34ca4SPoul-Henning Kamp 	}
28339e3e3fe5SWarner Losh 	swapgeom_acquire(cp);
28343398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
283511041003SKonstantin Belousov 	if (bp->b_iocmd == BIO_WRITE)
283611041003SKonstantin Belousov 		bio = g_new_bio();
283711041003SKonstantin Belousov 	else
28384f8205e5SPoul-Henning Kamp 		bio = g_alloc_bio();
28394f8205e5SPoul-Henning Kamp 	if (bio == NULL) {
28409e3e3fe5SWarner Losh 		mtx_lock(&sw_dev_mtx);
28419e3e3fe5SWarner Losh 		swapgeom_release(cp, sp);
28429e3e3fe5SWarner Losh 		mtx_unlock(&sw_dev_mtx);
28433e5b6861SPoul-Henning Kamp 		bp->b_error = ENOMEM;
28443e5b6861SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
28450b208315SEdward Tomasz Napierala 		printf("swap_pager: cannot allocate bio\n");
28463e5b6861SPoul-Henning Kamp 		bufdone(bp);
28473e5b6861SPoul-Henning Kamp 		return;
28483e5b6861SPoul-Henning Kamp 	}
284911041003SKonstantin Belousov 
2850756a5412SGleb Smirnoff 	bp->b_caller1 = bio;
28513398491bSAlexander Motin 	bio->bio_caller1 = sp;
2852dee34ca4SPoul-Henning Kamp 	bio->bio_caller2 = bp;
2853c5d3d25eSPoul-Henning Kamp 	bio->bio_cmd = bp->b_iocmd;
2854dee34ca4SPoul-Henning Kamp 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2855dee34ca4SPoul-Henning Kamp 	bio->bio_length = bp->b_bcount;
2856dee34ca4SPoul-Henning Kamp 	bio->bio_done = swapgeom_done;
2857fade8dd7SJeff Roberson 	if (!buf_mapped(bp)) {
28582cc718a1SKonstantin Belousov 		bio->bio_ma = bp->b_pages;
28592cc718a1SKonstantin Belousov 		bio->bio_data = unmapped_buf;
28602cc718a1SKonstantin Belousov 		bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
28612cc718a1SKonstantin Belousov 		bio->bio_ma_n = bp->b_npages;
28622cc718a1SKonstantin Belousov 		bio->bio_flags |= BIO_UNMAPPED;
28632cc718a1SKonstantin Belousov 	} else {
28642cc718a1SKonstantin Belousov 		bio->bio_data = bp->b_data;
28652cc718a1SKonstantin Belousov 		bio->bio_ma = NULL;
28662cc718a1SKonstantin Belousov 	}
2867dee34ca4SPoul-Henning Kamp 	g_io_request(bio, cp);
2868dee34ca4SPoul-Henning Kamp 	return;
2869dee34ca4SPoul-Henning Kamp }
2870dee34ca4SPoul-Henning Kamp 
2871dee34ca4SPoul-Henning Kamp static void
2872dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp)
2873dee34ca4SPoul-Henning Kamp {
2874dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
28753398491bSAlexander Motin 	int destroy;
2876dee34ca4SPoul-Henning Kamp 
2877dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
28783398491bSAlexander Motin 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
28793398491bSAlexander Motin 		if (sp->sw_id == cp) {
28808f12d83aSAlexander Motin 			sp->sw_flags |= SW_CLOSING;
28813398491bSAlexander Motin 			break;
2882dee34ca4SPoul-Henning Kamp 		}
28833398491bSAlexander Motin 	}
28849e3e3fe5SWarner Losh 	/*
28859e3e3fe5SWarner Losh 	 * Drop reference we were created with. Do directly since we're in a
28869e3e3fe5SWarner Losh 	 * special context where we don't have to queue the call to
28879e3e3fe5SWarner Losh 	 * swapgeom_close_ev().
28889e3e3fe5SWarner Losh 	 */
28899e3e3fe5SWarner Losh 	cp->index--;
28903398491bSAlexander Motin 	destroy = ((sp != NULL) && (cp->index == 0));
28913398491bSAlexander Motin 	if (destroy)
28923398491bSAlexander Motin 		sp->sw_id = NULL;
28933398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
28943398491bSAlexander Motin 	if (destroy)
28953398491bSAlexander Motin 		swapgeom_close_ev(cp, 0);
2896dee34ca4SPoul-Henning Kamp }
2897dee34ca4SPoul-Henning Kamp 
2898dee34ca4SPoul-Henning Kamp static void
2899dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw)
2900dee34ca4SPoul-Henning Kamp {
29013398491bSAlexander Motin 	struct g_consumer *cp;
2902dee34ca4SPoul-Henning Kamp 
29033398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
29043398491bSAlexander Motin 	cp = sw->sw_id;
29053398491bSAlexander Motin 	sw->sw_id = NULL;
29063398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
29070c657d22SKonstantin Belousov 
29080c657d22SKonstantin Belousov 	/*
29090c657d22SKonstantin Belousov 	 * swapgeom_close() may be called from the biodone context,
29100c657d22SKonstantin Belousov 	 * where we cannot perform topology changes.  Delegate the
29110c657d22SKonstantin Belousov 	 * work to the events thread.
29120c657d22SKonstantin Belousov 	 */
29133398491bSAlexander Motin 	if (cp != NULL)
29143398491bSAlexander Motin 		g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2915dee34ca4SPoul-Henning Kamp }
2916dee34ca4SPoul-Henning Kamp 
291788ad2d7bSKonstantin Belousov static int
291888ad2d7bSKonstantin Belousov swapongeom_locked(struct cdev *dev, struct vnode *vp)
2919dee34ca4SPoul-Henning Kamp {
2920dee34ca4SPoul-Henning Kamp 	struct g_provider *pp;
2921dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2922dee34ca4SPoul-Henning Kamp 	static struct g_geom *gp;
2923dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2924dee34ca4SPoul-Henning Kamp 	u_long nblks;
2925dee34ca4SPoul-Henning Kamp 	int error;
2926dee34ca4SPoul-Henning Kamp 
292788ad2d7bSKonstantin Belousov 	pp = g_dev_getprovider(dev);
292888ad2d7bSKonstantin Belousov 	if (pp == NULL)
292988ad2d7bSKonstantin Belousov 		return (ENODEV);
2930dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2931dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2932dee34ca4SPoul-Henning Kamp 		cp = sp->sw_id;
2933dee34ca4SPoul-Henning Kamp 		if (cp != NULL && cp->provider == pp) {
2934dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
293588ad2d7bSKonstantin Belousov 			return (EBUSY);
2936dee34ca4SPoul-Henning Kamp 		}
2937dee34ca4SPoul-Henning Kamp 	}
2938dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
29395721c9c7SPoul-Henning Kamp 	if (gp == NULL)
294002c62349SJaakko Heinonen 		gp = g_new_geomf(&g_swap_class, "swap");
2941dee34ca4SPoul-Henning Kamp 	cp = g_new_consumer(gp);
29429e3e3fe5SWarner Losh 	cp->index = 1;	/* Number of active I/Os, plus one for being active. */
29439e3e3fe5SWarner Losh 	cp->flags |=  G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2944dee34ca4SPoul-Henning Kamp 	g_attach(cp, pp);
2945afeb65e6SPoul-Henning Kamp 	/*
2946afeb65e6SPoul-Henning Kamp 	 * XXX: Every time you think you can improve the margin for
2947afeb65e6SPoul-Henning Kamp 	 * footshooting, somebody depends on the ability to do so:
2948afeb65e6SPoul-Henning Kamp 	 * savecore(8) wants to write to our swapdev so we cannot
2949afeb65e6SPoul-Henning Kamp 	 * set an exclusive count :-(
2950afeb65e6SPoul-Henning Kamp 	 */
2951d2bae332SPoul-Henning Kamp 	error = g_access(cp, 1, 1, 0);
295288ad2d7bSKonstantin Belousov 	if (error != 0) {
2953dee34ca4SPoul-Henning Kamp 		g_detach(cp);
2954dee34ca4SPoul-Henning Kamp 		g_destroy_consumer(cp);
295588ad2d7bSKonstantin Belousov 		return (error);
2956dee34ca4SPoul-Henning Kamp 	}
2957dee34ca4SPoul-Henning Kamp 	nblks = pp->mediasize / DEV_BSIZE;
295888ad2d7bSKonstantin Belousov 	swaponsomething(vp, cp, nblks, swapgeom_strategy,
295988ad2d7bSKonstantin Belousov 	    swapgeom_close, dev2udev(dev),
29602cc718a1SKonstantin Belousov 	    (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
296188ad2d7bSKonstantin Belousov 	return (0);
2962dee34ca4SPoul-Henning Kamp }
2963dee34ca4SPoul-Henning Kamp 
2964dee34ca4SPoul-Henning Kamp static int
296588ad2d7bSKonstantin Belousov swapongeom(struct vnode *vp)
2966dee34ca4SPoul-Henning Kamp {
2967dee34ca4SPoul-Henning Kamp 	int error;
2968dee34ca4SPoul-Henning Kamp 
2969cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2970abd80ddbSMateusz Guzik 	if (vp->v_type != VCHR || VN_IS_DOOMED(vp)) {
297188ad2d7bSKonstantin Belousov 		error = ENOENT;
297288ad2d7bSKonstantin Belousov 	} else {
297388ad2d7bSKonstantin Belousov 		g_topology_lock();
297488ad2d7bSKonstantin Belousov 		error = swapongeom_locked(vp->v_rdev, vp);
297588ad2d7bSKonstantin Belousov 		g_topology_unlock();
297688ad2d7bSKonstantin Belousov 	}
297722db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
2978dee34ca4SPoul-Henning Kamp 	return (error);
2979dee34ca4SPoul-Henning Kamp }
2980dee34ca4SPoul-Henning Kamp 
2981dee34ca4SPoul-Henning Kamp /*
2982dee34ca4SPoul-Henning Kamp  * VNODE backend
2983dee34ca4SPoul-Henning Kamp  *
2984dee34ca4SPoul-Henning Kamp  * This is used mainly for network filesystem (read: probably only tested
2985dee34ca4SPoul-Henning Kamp  * with NFS) swapfiles.
2986dee34ca4SPoul-Henning Kamp  *
2987dee34ca4SPoul-Henning Kamp  */
2988dee34ca4SPoul-Henning Kamp 
2989dee34ca4SPoul-Henning Kamp static void
2990dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp)
2991dee34ca4SPoul-Henning Kamp {
2992494eb176SPoul-Henning Kamp 	struct vnode *vp2;
2993dee34ca4SPoul-Henning Kamp 
2994dee34ca4SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2995dee34ca4SPoul-Henning Kamp 
2996dee34ca4SPoul-Henning Kamp 	vp2 = sp->sw_id;
2997dee34ca4SPoul-Henning Kamp 	vhold(vp2);
2998dee34ca4SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
29993cfc7651SOlivier Houchard 		if (bp->b_bufobj)
3000494eb176SPoul-Henning Kamp 			bufobj_wdrop(bp->b_bufobj);
3001a76d8f4eSPoul-Henning Kamp 		bufobj_wref(&vp2->v_bufobj);
3002dee34ca4SPoul-Henning Kamp 	}
30033cfc7651SOlivier Houchard 	if (bp->b_bufobj != &vp2->v_bufobj)
30043cfc7651SOlivier Houchard 		bp->b_bufobj = &vp2->v_bufobj;
3005dee34ca4SPoul-Henning Kamp 	bp->b_vp = vp2;
30062c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
3007b792bebeSPoul-Henning Kamp 	bstrategy(bp);
3008dee34ca4SPoul-Henning Kamp 	return;
3009dee34ca4SPoul-Henning Kamp }
3010dee34ca4SPoul-Henning Kamp 
3011dee34ca4SPoul-Henning Kamp static void
3012dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp)
3013dee34ca4SPoul-Henning Kamp {
3014dee34ca4SPoul-Henning Kamp 
3015dee34ca4SPoul-Henning Kamp 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
3016dee34ca4SPoul-Henning Kamp 	vrele(sp->sw_vp);
3017dee34ca4SPoul-Henning Kamp }
3018dee34ca4SPoul-Henning Kamp 
3019dee34ca4SPoul-Henning Kamp 
3020dee34ca4SPoul-Henning Kamp static int
3021dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
3022dee34ca4SPoul-Henning Kamp {
3023dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
3024dee34ca4SPoul-Henning Kamp 	int error;
3025dee34ca4SPoul-Henning Kamp 
3026dee34ca4SPoul-Henning Kamp 	if (nblks == 0)
3027dee34ca4SPoul-Henning Kamp 		return (ENXIO);
3028dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
3029dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
3030dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == vp) {
3031dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
3032dee34ca4SPoul-Henning Kamp 			return (EBUSY);
3033dee34ca4SPoul-Henning Kamp 		}
3034dee34ca4SPoul-Henning Kamp 	}
3035dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
3036dee34ca4SPoul-Henning Kamp 
3037cb05b60aSAttilio Rao 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3038dee34ca4SPoul-Henning Kamp #ifdef MAC
303930d239bcSRobert Watson 	error = mac_system_check_swapon(td->td_ucred, vp);
3040dee34ca4SPoul-Henning Kamp 	if (error == 0)
3041dee34ca4SPoul-Henning Kamp #endif
30429e223287SKonstantin Belousov 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
304322db15c0SAttilio Rao 	(void) VOP_UNLOCK(vp, 0);
3044dee34ca4SPoul-Henning Kamp 	if (error)
3045dee34ca4SPoul-Henning Kamp 		return (error);
3046dee34ca4SPoul-Henning Kamp 
3047dee34ca4SPoul-Henning Kamp 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
30482cc718a1SKonstantin Belousov 	    NODEV, 0);
3049dee34ca4SPoul-Henning Kamp 	return (0);
3050dee34ca4SPoul-Henning Kamp }
305189c241d1SGleb Smirnoff 
305289c241d1SGleb Smirnoff static int
305389c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
305489c241d1SGleb Smirnoff {
305589c241d1SGleb Smirnoff 	int error, new, n;
305689c241d1SGleb Smirnoff 
305789c241d1SGleb Smirnoff 	new = nsw_wcount_async_max;
305889c241d1SGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
305989c241d1SGleb Smirnoff 	if (error != 0 || req->newptr == NULL)
306089c241d1SGleb Smirnoff 		return (error);
306189c241d1SGleb Smirnoff 
306289c241d1SGleb Smirnoff 	if (new > nswbuf / 2 || new < 1)
306389c241d1SGleb Smirnoff 		return (EINVAL);
306489c241d1SGleb Smirnoff 
3065756a5412SGleb Smirnoff 	mtx_lock(&swbuf_mtx);
306689c241d1SGleb Smirnoff 	while (nsw_wcount_async_max != new) {
306789c241d1SGleb Smirnoff 		/*
306889c241d1SGleb Smirnoff 		 * Adjust difference.  If the current async count is too low,
306989c241d1SGleb Smirnoff 		 * we will need to sqeeze our update slowly in.  Sleep with a
307089c241d1SGleb Smirnoff 		 * higher priority than getpbuf() to finish faster.
307189c241d1SGleb Smirnoff 		 */
307289c241d1SGleb Smirnoff 		n = new - nsw_wcount_async_max;
307389c241d1SGleb Smirnoff 		if (nsw_wcount_async + n >= 0) {
307489c241d1SGleb Smirnoff 			nsw_wcount_async += n;
307589c241d1SGleb Smirnoff 			nsw_wcount_async_max += n;
307689c241d1SGleb Smirnoff 			wakeup(&nsw_wcount_async);
307789c241d1SGleb Smirnoff 		} else {
307889c241d1SGleb Smirnoff 			nsw_wcount_async_max -= nsw_wcount_async;
307989c241d1SGleb Smirnoff 			nsw_wcount_async = 0;
3080756a5412SGleb Smirnoff 			msleep(&nsw_wcount_async, &swbuf_mtx, PSWP,
308189c241d1SGleb Smirnoff 			    "swpsysctl", 0);
308289c241d1SGleb Smirnoff 		}
308389c241d1SGleb Smirnoff 	}
3084756a5412SGleb Smirnoff 	mtx_unlock(&swbuf_mtx);
308589c241d1SGleb Smirnoff 
308689c241d1SGleb Smirnoff 	return (0);
308789c241d1SGleb Smirnoff }
3088fe7bcbafSKyle Evans 
3089fe7bcbafSKyle Evans static void
3090fe7bcbafSKyle Evans swap_pager_update_writecount(vm_object_t object, vm_offset_t start,
3091fe7bcbafSKyle Evans     vm_offset_t end)
3092fe7bcbafSKyle Evans {
3093fe7bcbafSKyle Evans 
3094fe7bcbafSKyle Evans 	VM_OBJECT_WLOCK(object);
309563967687SJeff Roberson 	KASSERT((object->flags & OBJ_ANON) == 0,
3096fe7bcbafSKyle Evans 	    ("Splittable object with writecount"));
3097fe7bcbafSKyle Evans 	object->un_pager.swp.writemappings += (vm_ooffset_t)end - start;
3098fe7bcbafSKyle Evans 	VM_OBJECT_WUNLOCK(object);
3099fe7bcbafSKyle Evans }
3100fe7bcbafSKyle Evans 
3101fe7bcbafSKyle Evans static void
3102fe7bcbafSKyle Evans swap_pager_release_writecount(vm_object_t object, vm_offset_t start,
3103fe7bcbafSKyle Evans     vm_offset_t end)
3104fe7bcbafSKyle Evans {
3105fe7bcbafSKyle Evans 
3106fe7bcbafSKyle Evans 	VM_OBJECT_WLOCK(object);
310763967687SJeff Roberson 	KASSERT((object->flags & OBJ_ANON) == 0,
3108fe7bcbafSKyle Evans 	    ("Splittable object with writecount"));
3109fe7bcbafSKyle Evans 	object->un_pager.swp.writemappings -= (vm_ooffset_t)end - start;
3110fe7bcbafSKyle Evans 	VM_OBJECT_WUNLOCK(object);
3111fe7bcbafSKyle Evans }
3112