xref: /freebsd/sys/vm/swap_pager.c (revision e735691b613c2ac4aed254ffefeef429c91c2e0d)
160727d8bSWarner Losh /*-
21c7c3c6aSMatthew Dillon  * Copyright (c) 1998 Matthew Dillon,
326f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
4df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
5e9c0cc15SPoul-Henning Kamp  * Copyright (c) 1982, 1986, 1989, 1993
6df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
215929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
22df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
23df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
24df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
25df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
26df8bae1dSRodney W. Grimes  *    without specific prior written permission.
27df8bae1dSRodney W. Grimes  *
28df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
39df8bae1dSRodney W. Grimes  *
401c7c3c6aSMatthew Dillon  *				New Swap System
411c7c3c6aSMatthew Dillon  *				Matthew Dillon
421c7c3c6aSMatthew Dillon  *
431c7c3c6aSMatthew Dillon  * Radix Bitmap 'blists'.
441c7c3c6aSMatthew Dillon  *
451c7c3c6aSMatthew Dillon  *	- The new swapper uses the new radix bitmap code.  This should scale
461c7c3c6aSMatthew Dillon  *	  to arbitrarily small or arbitrarily large swap spaces and an almost
471c7c3c6aSMatthew Dillon  *	  arbitrary degree of fragmentation.
481c7c3c6aSMatthew Dillon  *
491c7c3c6aSMatthew Dillon  * Features:
501c7c3c6aSMatthew Dillon  *
511c7c3c6aSMatthew Dillon  *	- on the fly reallocation of swap during putpages.  The new system
521c7c3c6aSMatthew Dillon  *	  does not try to keep previously allocated swap blocks for dirty
531c7c3c6aSMatthew Dillon  *	  pages.
541c7c3c6aSMatthew Dillon  *
551c7c3c6aSMatthew Dillon  *	- on the fly deallocation of swap
561c7c3c6aSMatthew Dillon  *
571c7c3c6aSMatthew Dillon  *	- No more garbage collection required.  Unnecessarily allocated swap
581c7c3c6aSMatthew Dillon  *	  blocks only exist for dirty vm_page_t's now and these are already
591c7c3c6aSMatthew Dillon  *	  cycled (in a high-load system) by the pager.  We also do on-the-fly
601c7c3c6aSMatthew Dillon  *	  removal of invalidated swap blocks when a page is destroyed
611c7c3c6aSMatthew Dillon  *	  or renamed.
621c7c3c6aSMatthew Dillon  *
63df8bae1dSRodney W. Grimes  * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
64df8bae1dSRodney W. Grimes  *
65df8bae1dSRodney W. Grimes  *	@(#)swap_pager.c	8.9 (Berkeley) 3/21/94
66e9c0cc15SPoul-Henning Kamp  *	@(#)vm_swap.c	8.5 (Berkeley) 2/17/94
67df8bae1dSRodney W. Grimes  */
68df8bae1dSRodney W. Grimes 
69874651b1SDavid E. O'Brien #include <sys/cdefs.h>
70874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
71874651b1SDavid E. O'Brien 
72e9c0cc15SPoul-Henning Kamp #include "opt_swap.h"
73e9c0cc15SPoul-Henning Kamp #include "opt_vm.h"
74e9c0cc15SPoul-Henning Kamp 
75df8bae1dSRodney W. Grimes #include <sys/param.h>
76df8bae1dSRodney W. Grimes #include <sys/systm.h>
77af647ddeSBruce Evans #include <sys/conf.h>
7864abb5a5SDavid Greenman #include <sys/kernel.h>
79acd3428bSRobert Watson #include <sys/priv.h>
80df8bae1dSRodney W. Grimes #include <sys/proc.h>
819626b608SPoul-Henning Kamp #include <sys/bio.h>
82df8bae1dSRodney W. Grimes #include <sys/buf.h>
83e9c0cc15SPoul-Henning Kamp #include <sys/disk.h>
84e9c0cc15SPoul-Henning Kamp #include <sys/fcntl.h>
85e9c0cc15SPoul-Henning Kamp #include <sys/mount.h>
86e9c0cc15SPoul-Henning Kamp #include <sys/namei.h>
87df8bae1dSRodney W. Grimes #include <sys/vnode.h>
88df8bae1dSRodney W. Grimes #include <sys/malloc.h>
891ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
903364c323SKonstantin Belousov #include <sys/resource.h>
913364c323SKonstantin Belousov #include <sys/resourcevar.h>
9289f6b863SAttilio Rao #include <sys/rwlock.h>
93327f4e83SMatthew Dillon #include <sys/sysctl.h>
94e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
951c7c3c6aSMatthew Dillon #include <sys/blist.h>
961c7c3c6aSMatthew Dillon #include <sys/lock.h>
970cddd8f0SMatthew Dillon #include <sys/sx.h>
98936524aaSMatthew Dillon #include <sys/vmmeter.h>
99df8bae1dSRodney W. Grimes 
100aed55708SRobert Watson #include <security/mac/mac_framework.h>
101aed55708SRobert Watson 
102df8bae1dSRodney W. Grimes #include <vm/vm.h>
10321cd6e62SSeigo Tanimura #include <vm/pmap.h>
10421cd6e62SSeigo Tanimura #include <vm/vm_map.h>
10521cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
106efeaf95aSDavid Greenman #include <vm/vm_object.h>
107df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
108efeaf95aSDavid Greenman #include <vm/vm_pager.h>
109df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
110e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
111df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
112efeaf95aSDavid Greenman #include <vm/vm_extern.h>
113670d17b5SJeff Roberson #include <vm/uma.h>
114df8bae1dSRodney W. Grimes 
115dee34ca4SPoul-Henning Kamp #include <geom/geom.h>
116dee34ca4SPoul-Henning Kamp 
117ec38b344SPoul-Henning Kamp /*
11815523cf7SKonstantin Belousov  * SWB_NPAGES must be a power of 2.  It may be set to 1, 2, 4, 8, 16
11915523cf7SKonstantin Belousov  * or 32 pages per allocation.
12015523cf7SKonstantin Belousov  * The 32-page limit is due to the radix code (kern/subr_blist.c).
121ec38b344SPoul-Henning Kamp  */
122ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
123ec38b344SPoul-Henning Kamp #define MAX_PAGEOUT_CLUSTER 16
124ec38b344SPoul-Henning Kamp #endif
125ec38b344SPoul-Henning Kamp 
126ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
127ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
128ec38b344SPoul-Henning Kamp #endif
129ec38b344SPoul-Henning Kamp 
130ec38b344SPoul-Henning Kamp /*
13115523cf7SKonstantin Belousov  * The swblock structure maps an object and a small, fixed-size range
13215523cf7SKonstantin Belousov  * of page indices to disk addresses within a swap area.
13315523cf7SKonstantin Belousov  * The collection of these mappings is implemented as a hash table.
13415523cf7SKonstantin Belousov  * Unused disk addresses within a swap area are allocated and managed
13515523cf7SKonstantin Belousov  * using a blist.
136ec38b344SPoul-Henning Kamp  */
137ec38b344SPoul-Henning Kamp #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t))
138ec38b344SPoul-Henning Kamp #define SWAP_META_PAGES		(SWB_NPAGES * 2)
139ec38b344SPoul-Henning Kamp #define SWAP_META_MASK		(SWAP_META_PAGES - 1)
140ec38b344SPoul-Henning Kamp 
141e9c0cc15SPoul-Henning Kamp struct swblock {
142e9c0cc15SPoul-Henning Kamp 	struct swblock	*swb_hnext;
143e9c0cc15SPoul-Henning Kamp 	vm_object_t	swb_object;
144e9c0cc15SPoul-Henning Kamp 	vm_pindex_t	swb_index;
145e9c0cc15SPoul-Henning Kamp 	int		swb_count;
146e9c0cc15SPoul-Henning Kamp 	daddr_t		swb_pages[SWAP_META_PAGES];
147e9c0cc15SPoul-Henning Kamp };
148e9c0cc15SPoul-Henning Kamp 
1492c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
15020da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx;
1518f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1528f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1538f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1548f60c087SPoul-Henning Kamp int swap_pager_avail;
155e9c0cc15SPoul-Henning Kamp static int swdev_syscall_active = 0; /* serialize swap(on|off) */
156e9c0cc15SPoul-Henning Kamp 
1573364c323SKonstantin Belousov static vm_ooffset_t swap_total;
1588a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0,
1598a9c731fSIvan Voras     "Total amount of available swap storage.");
1603364c323SKonstantin Belousov static vm_ooffset_t swap_reserved;
1618a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0,
1628a9c731fSIvan Voras     "Amount of swap storage needed to back all allocated anonymous memory.");
1633364c323SKonstantin Belousov static int overcommit = 0;
1648a9c731fSIvan Voras SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0,
1658a9c731fSIvan Voras     "Configure virtual memory overcommit behavior. See tuning(7) "
1668a9c731fSIvan Voras     "for details.");
16761203277SDag-Erling Smørgrav static unsigned long swzone;
16861203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
16961203277SDag-Erling Smørgrav     "Actual size of swap metadata zone");
17061203277SDag-Erling Smørgrav static unsigned long swap_maxpages;
17161203277SDag-Erling Smørgrav SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
17261203277SDag-Erling Smørgrav     "Maximum amount of swap supported");
1733364c323SKonstantin Belousov 
1743364c323SKonstantin Belousov /* bits from overcommit */
1753364c323SKonstantin Belousov #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
1763364c323SKonstantin Belousov #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
1773364c323SKonstantin Belousov #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
1783364c323SKonstantin Belousov 
1793364c323SKonstantin Belousov int
1803364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr)
1813364c323SKonstantin Belousov {
1823364c323SKonstantin Belousov 
183ef694c1aSEdward Tomasz Napierala 	return (swap_reserve_by_cred(incr, curthread->td_ucred));
1843364c323SKonstantin Belousov }
1853364c323SKonstantin Belousov 
1863364c323SKonstantin Belousov int
187ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
1883364c323SKonstantin Belousov {
1895c0e1c11SKonstantin Belousov 	vm_ooffset_t r, s;
1903364c323SKonstantin Belousov 	int res, error;
1913364c323SKonstantin Belousov 	static int curfail;
1923364c323SKonstantin Belousov 	static struct timeval lastfail;
193ef694c1aSEdward Tomasz Napierala 	struct uidinfo *uip;
194ef694c1aSEdward Tomasz Napierala 
195ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
1963364c323SKonstantin Belousov 
1973364c323SKonstantin Belousov 	if (incr & PAGE_MASK)
1983364c323SKonstantin Belousov 		panic("swap_reserve: & PAGE_MASK");
1993364c323SKonstantin Belousov 
200afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2014b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
2021ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2031ba5ad42SEdward Tomasz Napierala 		error = racct_add(curproc, RACCT_SWAP, incr);
2041ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2051ba5ad42SEdward Tomasz Napierala 		if (error != 0)
2061ba5ad42SEdward Tomasz Napierala 			return (0);
2074b5c9cf6SEdward Tomasz Napierala 	}
208afcc55f3SEdward Tomasz Napierala #endif
2091ba5ad42SEdward Tomasz Napierala 
2103364c323SKonstantin Belousov 	res = 0;
2113364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2123364c323SKonstantin Belousov 	r = swap_reserved + incr;
2133364c323SKonstantin Belousov 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
21444f1c916SBryan Drewery 		s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - vm_cnt.v_wire_count;
2153364c323SKonstantin Belousov 		s *= PAGE_SIZE;
2163364c323SKonstantin Belousov 	} else
2173364c323SKonstantin Belousov 		s = 0;
2183364c323SKonstantin Belousov 	s += swap_total;
2193364c323SKonstantin Belousov 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
2203364c323SKonstantin Belousov 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
2213364c323SKonstantin Belousov 		res = 1;
2223364c323SKonstantin Belousov 		swap_reserved = r;
2233364c323SKonstantin Belousov 	}
2243364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2253364c323SKonstantin Belousov 
2263364c323SKonstantin Belousov 	if (res) {
2273364c323SKonstantin Belousov 		PROC_LOCK(curproc);
2283364c323SKonstantin Belousov 		UIDINFO_VMSIZE_LOCK(uip);
2295c0e1c11SKonstantin Belousov 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
2305c0e1c11SKonstantin Belousov 		    uip->ui_vmsize + incr > lim_cur(curproc, RLIMIT_SWAP) &&
2315c0e1c11SKonstantin Belousov 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT))
2323364c323SKonstantin Belousov 			res = 0;
2333364c323SKonstantin Belousov 		else
2343364c323SKonstantin Belousov 			uip->ui_vmsize += incr;
2353364c323SKonstantin Belousov 		UIDINFO_VMSIZE_UNLOCK(uip);
2363364c323SKonstantin Belousov 		PROC_UNLOCK(curproc);
2373364c323SKonstantin Belousov 		if (!res) {
2383364c323SKonstantin Belousov 			mtx_lock(&sw_dev_mtx);
2393364c323SKonstantin Belousov 			swap_reserved -= incr;
2403364c323SKonstantin Belousov 			mtx_unlock(&sw_dev_mtx);
2413364c323SKonstantin Belousov 		}
2423364c323SKonstantin Belousov 	}
2433364c323SKonstantin Belousov 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
2443364c323SKonstantin Belousov 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
245134465d7SKonstantin Belousov 		    uip->ui_uid, curproc->p_pid, incr);
2463364c323SKonstantin Belousov 	}
2473364c323SKonstantin Belousov 
248afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2491ba5ad42SEdward Tomasz Napierala 	if (!res) {
2501ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2511ba5ad42SEdward Tomasz Napierala 		racct_sub(curproc, RACCT_SWAP, incr);
2521ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2531ba5ad42SEdward Tomasz Napierala 	}
254afcc55f3SEdward Tomasz Napierala #endif
2551ba5ad42SEdward Tomasz Napierala 
2563364c323SKonstantin Belousov 	return (res);
2573364c323SKonstantin Belousov }
2583364c323SKonstantin Belousov 
2593364c323SKonstantin Belousov void
2603364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr)
2613364c323SKonstantin Belousov {
2623364c323SKonstantin Belousov 	struct uidinfo *uip;
2633364c323SKonstantin Belousov 
2643364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2653364c323SKonstantin Belousov 	swap_reserved += incr;
2663364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2673364c323SKonstantin Belousov 
268afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2691ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(curproc);
2701ba5ad42SEdward Tomasz Napierala 	racct_add_force(curproc, RACCT_SWAP, incr);
2711ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(curproc);
272afcc55f3SEdward Tomasz Napierala #endif
2731ba5ad42SEdward Tomasz Napierala 
2743364c323SKonstantin Belousov 	uip = curthread->td_ucred->cr_ruidinfo;
2753364c323SKonstantin Belousov 	PROC_LOCK(curproc);
2763364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
2773364c323SKonstantin Belousov 	uip->ui_vmsize += incr;
2783364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
2793364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2803364c323SKonstantin Belousov }
2813364c323SKonstantin Belousov 
2823364c323SKonstantin Belousov void
2833364c323SKonstantin Belousov swap_release(vm_ooffset_t decr)
2843364c323SKonstantin Belousov {
285ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
2863364c323SKonstantin Belousov 
2873364c323SKonstantin Belousov 	PROC_LOCK(curproc);
288ef694c1aSEdward Tomasz Napierala 	cred = curthread->td_ucred;
289ef694c1aSEdward Tomasz Napierala 	swap_release_by_cred(decr, cred);
2903364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2913364c323SKonstantin Belousov }
2923364c323SKonstantin Belousov 
2933364c323SKonstantin Belousov void
294ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
2953364c323SKonstantin Belousov {
296ef694c1aSEdward Tomasz Napierala  	struct uidinfo *uip;
297ef694c1aSEdward Tomasz Napierala 
298ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
2993364c323SKonstantin Belousov 
3003364c323SKonstantin Belousov 	if (decr & PAGE_MASK)
3013364c323SKonstantin Belousov 		panic("swap_release: & PAGE_MASK");
3023364c323SKonstantin Belousov 
3033364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
3043364c323SKonstantin Belousov 	if (swap_reserved < decr)
3053364c323SKonstantin Belousov 		panic("swap_reserved < decr");
3063364c323SKonstantin Belousov 	swap_reserved -= decr;
3073364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
3083364c323SKonstantin Belousov 
3093364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
3103364c323SKonstantin Belousov 	if (uip->ui_vmsize < decr)
3113364c323SKonstantin Belousov 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
3123364c323SKonstantin Belousov 	uip->ui_vmsize -= decr;
3133364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
3141ba5ad42SEdward Tomasz Napierala 
3151ba5ad42SEdward Tomasz Napierala 	racct_sub_cred(cred, RACCT_SWAP, decr);
3163364c323SKonstantin Belousov }
3173364c323SKonstantin Belousov 
3184b03903aSPoul-Henning Kamp static void swapdev_strategy(struct buf *, struct swdevt *sw);
319e9c0cc15SPoul-Henning Kamp 
3201c7c3c6aSMatthew Dillon #define SWM_FREE	0x02	/* free, period			*/
3211c7c3c6aSMatthew Dillon #define SWM_POP		0x04	/* pop out			*/
32226f9a767SRodney W. Grimes 
3237dea2c2eSAlan Cox int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
3247dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
3251c7c3c6aSMatthew Dillon static int nsw_rcount;		/* free read buffers			*/
326327f4e83SMatthew Dillon static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
327327f4e83SMatthew Dillon static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
328327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
329327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
3301c7c3c6aSMatthew Dillon 
33189c241d1SGleb Smirnoff static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
33289c241d1SGleb Smirnoff SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW,
33389c241d1SGleb Smirnoff     NULL, 0, sysctl_swap_async_max, "I", "Maximum running async swap ops");
33489c241d1SGleb Smirnoff 
3351c7c3c6aSMatthew Dillon static struct swblock **swhash;
3361c7c3c6aSMatthew Dillon static int swhash_mask;
3377827d9b0SAlan Cox static struct mtx swhash_mtx;
3387827d9b0SAlan Cox 
3390cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
340327f4e83SMatthew Dillon 
3411c7c3c6aSMatthew Dillon /*
3421c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
3431c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
3441c7c3c6aSMatthew Dillon  */
3451c7c3c6aSMatthew Dillon 
3461c7c3c6aSMatthew Dillon #define NOBJLISTS		8
3471c7c3c6aSMatthew Dillon 
3481c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
349af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
3501c7c3c6aSMatthew Dillon 
351a9fa2c05SAlfred Perlstein static struct mtx sw_alloc_mtx;	/* protect list manipulation */
3521c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
353e9c0cc15SPoul-Henning Kamp static uma_zone_t	swap_zone;
3541c7c3c6aSMatthew Dillon 
3551c7c3c6aSMatthew Dillon /*
3561c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
3571c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
3581c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
3591c7c3c6aSMatthew Dillon  */
360ff98689dSBruce Evans static vm_object_t
36111caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
3623364c323SKonstantin Belousov 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
36311caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
36411caded3SAlfred Perlstein static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int);
36590effb23SGleb Smirnoff static int	swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int,
36690effb23SGleb Smirnoff     pgo_getpages_iodone_t, void *);
367751221fdSPoul-Henning Kamp static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
3685ea4972cSAlan Cox static boolean_t
3695ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
37011caded3SAlfred Perlstein static void	swap_pager_init(void);
37111caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
372b3fed13eSDavid Schultz static void	swap_pager_swapoff(struct swdevt *sp);
373f708ef1bSPoul-Henning Kamp 
374df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
375e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
376e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
377e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
378e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
37990effb23SGleb Smirnoff 	.pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)		*/
380e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
381e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
382e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
383df8bae1dSRodney W. Grimes };
384df8bae1dSRodney W. Grimes 
3851c7c3c6aSMatthew Dillon /*
3861c7c3c6aSMatthew Dillon  * dmmax is in page-sized chunks with the new swap system.  It was
38764bcb9c8SMatthew Dillon  * dev-bsized chunks in the old.  dmmax is always a power of 2.
3881c7c3c6aSMatthew Dillon  *
3891c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
3901c7c3c6aSMatthew Dillon  * internal.
3911c7c3c6aSMatthew Dillon  */
392751221fdSPoul-Henning Kamp static int dmmax;
393e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
394e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
39526f9a767SRodney W. Grimes 
396cee313c4SRobert Watson SYSCTL_INT(_vm, OID_AUTO, dmmax,
397cee313c4SRobert Watson 	CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block");
398cee313c4SRobert Watson 
399a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
40011caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
401dee34ca4SPoul-Henning Kamp static int	swapongeom(struct thread *, struct vnode *);
40259efee01SPoul-Henning Kamp static int	swaponvp(struct thread *, struct vnode *, u_long);
40335918c55SChristian S.J. Peron static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
40424a1cce3SDavid Greenman 
4051c7c3c6aSMatthew Dillon /*
4061c7c3c6aSMatthew Dillon  * Swap bitmap functions
4071c7c3c6aSMatthew Dillon  */
408a5edd34aSPoul-Henning Kamp static void	swp_pager_freeswapspace(daddr_t blk, int npages);
409a5edd34aSPoul-Henning Kamp static daddr_t	swp_pager_getswapspace(int npages);
4101c7c3c6aSMatthew Dillon 
4111c7c3c6aSMatthew Dillon /*
4121c7c3c6aSMatthew Dillon  * Metadata functions
4131c7c3c6aSMatthew Dillon  */
414a5edd34aSPoul-Henning Kamp static struct swblock **swp_pager_hash(vm_object_t object, vm_pindex_t index);
41511caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
41611caded3SAlfred Perlstein static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t);
41711caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
41811caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
4191c7c3c6aSMatthew Dillon 
420e86a87e9SKonstantin Belousov static void
421e86a87e9SKonstantin Belousov swp_pager_free_nrpage(vm_page_t m)
422e86a87e9SKonstantin Belousov {
423e86a87e9SKonstantin Belousov 
4243c4a2440SAlan Cox 	vm_page_lock(m);
425e86a87e9SKonstantin Belousov 	if (m->wire_count == 0)
426e86a87e9SKonstantin Belousov 		vm_page_free(m);
4273c4a2440SAlan Cox 	vm_page_unlock(m);
428e86a87e9SKonstantin Belousov }
429e86a87e9SKonstantin Belousov 
4301c7c3c6aSMatthew Dillon /*
4311c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
4321c7c3c6aSMatthew Dillon  *
43320d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
43420d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
43520d3034fSMatthew Dillon  *
43620d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
4371c7c3c6aSMatthew Dillon  *
4381c7c3c6aSMatthew Dillon  *	No restrictions on call
4391c7c3c6aSMatthew Dillon  *	This routine may not block.
4401c7c3c6aSMatthew Dillon  */
441a5edd34aSPoul-Henning Kamp static void
4422f249180SPoul-Henning Kamp swp_sizecheck(void)
4430d94caffSDavid Greenman {
44423955314SAlfred Perlstein 
4458f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
44620d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
4471af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
44820d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
4492b0d37a4SMatthew Dillon 		}
45020d3034fSMatthew Dillon 	} else {
45126f9a767SRodney W. Grimes 		swap_pager_full = 0;
4528f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
45320d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
45426f9a767SRodney W. Grimes 	}
4551c7c3c6aSMatthew Dillon }
4561c7c3c6aSMatthew Dillon 
4571c7c3c6aSMatthew Dillon /*
458da5fd145SPeter Wemm  * SWP_PAGER_HASH() -	hash swap meta data
459da5fd145SPeter Wemm  *
460a5edd34aSPoul-Henning Kamp  *	This is an helper function which hashes the swapblk given
461da5fd145SPeter Wemm  *	the object and page index.  It returns a pointer to a pointer
462da5fd145SPeter Wemm  *	to the object, or a pointer to a NULL pointer if it could not
463da5fd145SPeter Wemm  *	find a swapblk.
464da5fd145SPeter Wemm  */
465a5edd34aSPoul-Henning Kamp static struct swblock **
466da5fd145SPeter Wemm swp_pager_hash(vm_object_t object, vm_pindex_t index)
467da5fd145SPeter Wemm {
468da5fd145SPeter Wemm 	struct swblock **pswap;
469da5fd145SPeter Wemm 	struct swblock *swap;
470da5fd145SPeter Wemm 
471da5fd145SPeter Wemm 	index &= ~(vm_pindex_t)SWAP_META_MASK;
472da5fd145SPeter Wemm 	pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask];
473da5fd145SPeter Wemm 	while ((swap = *pswap) != NULL) {
474da5fd145SPeter Wemm 		if (swap->swb_object == object &&
475da5fd145SPeter Wemm 		    swap->swb_index == index
476da5fd145SPeter Wemm 		) {
477da5fd145SPeter Wemm 			break;
478da5fd145SPeter Wemm 		}
479da5fd145SPeter Wemm 		pswap = &swap->swb_hnext;
480da5fd145SPeter Wemm 	}
481da5fd145SPeter Wemm 	return (pswap);
482da5fd145SPeter Wemm }
483da5fd145SPeter Wemm 
484da5fd145SPeter Wemm /*
4851c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
4861c7c3c6aSMatthew Dillon  *
4871c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
4881c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
4891c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
4901c7c3c6aSMatthew Dillon  */
491f5a12711SPoul-Henning Kamp static void
4922f249180SPoul-Henning Kamp swap_pager_init(void)
493df8bae1dSRodney W. Grimes {
4941c7c3c6aSMatthew Dillon 	/*
4951c7c3c6aSMatthew Dillon 	 * Initialize object lists
4961c7c3c6aSMatthew Dillon 	 */
4971c7c3c6aSMatthew Dillon 	int i;
4981c7c3c6aSMatthew Dillon 
4991c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
5001c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
5016008862bSJohn Baldwin 	mtx_init(&sw_alloc_mtx, "swap_pager list", NULL, MTX_DEF);
50220da9c2eSPoul-Henning Kamp 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes 	/*
5051c7c3c6aSMatthew Dillon 	 * Device Stripe, in PAGE_SIZE'd blocks
506df8bae1dSRodney W. Grimes 	 */
5071c7c3c6aSMatthew Dillon 	dmmax = SWB_NPAGES * 2;
5081c7c3c6aSMatthew Dillon }
50926f9a767SRodney W. Grimes 
510df8bae1dSRodney W. Grimes /*
5111c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
5121c7c3c6aSMatthew Dillon  *
5131c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
5141c7c3c6aSMatthew Dillon  *	its main loop.
515df8bae1dSRodney W. Grimes  */
51624a1cce3SDavid Greenman void
5172f249180SPoul-Henning Kamp swap_pager_swap_init(void)
518df8bae1dSRodney W. Grimes {
51961203277SDag-Erling Smørgrav 	unsigned long n, n2;
5200d94caffSDavid Greenman 
52126f9a767SRodney W. Grimes 	/*
5221c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
5231c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
5241c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
5251c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
5261c7c3c6aSMatthew Dillon 	 *
527327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
5281c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
5291c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
5301c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
531327f4e83SMatthew Dillon 	 *
532327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
533327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
534327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
535327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
536327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
537327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
538327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
539327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
540327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
541327f4e83SMatthew Dillon 	 * So it all works out pretty well.
54226f9a767SRodney W. Grimes 	 */
543ad3cce20SMatthew Dillon 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
544327f4e83SMatthew Dillon 
5456d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
5461c7c3c6aSMatthew Dillon 	nsw_rcount = (nswbuf + 1) / 2;
547327f4e83SMatthew Dillon 	nsw_wcount_sync = (nswbuf + 3) / 4;
548327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
549327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
5506d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
55124a1cce3SDavid Greenman 
5521c7c3c6aSMatthew Dillon 	/*
5531c7c3c6aSMatthew Dillon 	 * Initialize our zone.  Right now I'm just guessing on the number
5541c7c3c6aSMatthew Dillon 	 * we need based on the number of pages in the system.  Each swblock
55561203277SDag-Erling Smørgrav 	 * can hold 32 pages, so this is probably overkill.  This reservation
556ec61f55dSMatthew Dillon 	 * is typically limited to around 32MB by default.
5571c7c3c6aSMatthew Dillon 	 */
55844f1c916SBryan Drewery 	n = vm_cnt.v_page_count / 2;
5592f9e4e80SMatthew Dillon 	if (maxswzone && n > maxswzone / sizeof(struct swblock))
5602f9e4e80SMatthew Dillon 		n = maxswzone / sizeof(struct swblock);
56122a5e6b9SDag-Erling Smørgrav 	n2 = n;
562670d17b5SJeff Roberson 	swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL,
5637827d9b0SAlan Cox 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
564010b1ca1SDavid Schultz 	if (swap_zone == NULL)
565010b1ca1SDavid Schultz 		panic("failed to create swap_zone.");
5668355f576SJeff Roberson 	do {
567a4915c21SAttilio Rao 		if (uma_zone_reserve_kva(swap_zone, n))
56861ce6eeeSAlfred Perlstein 			break;
56961ce6eeeSAlfred Perlstein 		/*
57061ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
57161ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
57261ce6eeeSAlfred Perlstein 		 */
57361ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
57461ce6eeeSAlfred Perlstein 	} while (n > 0);
57521cd6e62SSeigo Tanimura 	if (n2 != n)
57661203277SDag-Erling Smørgrav 		printf("Swap zone entries reduced from %lu to %lu.\n", n2, n);
57761203277SDag-Erling Smørgrav 	swap_maxpages = n * SWAP_META_PAGES;
57861203277SDag-Erling Smørgrav 	swzone = n * sizeof(struct swblock);
57922a5e6b9SDag-Erling Smørgrav 	n2 = n;
58024a1cce3SDavid Greenman 
5811c7c3c6aSMatthew Dillon 	/*
5821c7c3c6aSMatthew Dillon 	 * Initialize our meta-data hash table.  The swapper does not need to
5831c7c3c6aSMatthew Dillon 	 * be quite as efficient as the VM system, so we do not use an
5841c7c3c6aSMatthew Dillon 	 * oversized hash table.
5851c7c3c6aSMatthew Dillon 	 *
5861c7c3c6aSMatthew Dillon 	 * 	n: 		size of hash table, must be power of 2
5871c7c3c6aSMatthew Dillon 	 *	swhash_mask:	hash table index mask
5881c7c3c6aSMatthew Dillon 	 */
58961ce6eeeSAlfred Perlstein 	for (n = 1; n < n2 / 8; n *= 2)
5901c7c3c6aSMatthew Dillon 		;
591a163d034SWarner Losh 	swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO);
5921c7c3c6aSMatthew Dillon 	swhash_mask = n - 1;
5937827d9b0SAlan Cox 	mtx_init(&swhash_mtx, "swap_pager swhash", NULL, MTX_DEF);
59424a1cce3SDavid Greenman }
59524a1cce3SDavid Greenman 
59624a1cce3SDavid Greenman /*
5971c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
5981c7c3c6aSMatthew Dillon  *			its metadata structures.
5991c7c3c6aSMatthew Dillon  *
6001c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
6011c7c3c6aSMatthew Dillon  *	OBJT_SWAP object.  We do this by creating an OBJT_DEFAULT object
6021c7c3c6aSMatthew Dillon  *	and then converting it with swp_pager_meta_build().
6031c7c3c6aSMatthew Dillon  *
6041c7c3c6aSMatthew Dillon  *	This routine may block in vm_object_allocate() and create a named
605cec9f109SDavid E. O'Brien  *	object lookup race, so we must interlock.
60624c46d03SAlan Cox  *
60724c46d03SAlan Cox  * MPSAFE
60824a1cce3SDavid Greenman  */
609f5a12711SPoul-Henning Kamp static vm_object_t
6106cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
6113364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
61224a1cce3SDavid Greenman {
61324a1cce3SDavid Greenman 	vm_object_t object;
6142f7af3dbSAlan Cox 	vm_pindex_t pindex;
6152f7af3dbSAlan Cox 
6162f7af3dbSAlan Cox 	pindex = OFF_TO_IDX(offset + PAGE_MASK + size);
61724a1cce3SDavid Greenman 	if (handle) {
618e793b779SAlan Cox 		mtx_lock(&Giant);
6191c7c3c6aSMatthew Dillon 		/*
6201c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
6211c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
6221c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
6231c7c3c6aSMatthew Dillon 		 * of the handle.
6241c7c3c6aSMatthew Dillon 		 */
6250cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
6261c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
627b5e8f167SAlan Cox 		if (object == NULL) {
6283364c323SKonstantin Belousov 			if (cred != NULL) {
629ef694c1aSEdward Tomasz Napierala 				if (!swap_reserve_by_cred(size, cred)) {
6303364c323SKonstantin Belousov 					sx_xunlock(&sw_alloc_sx);
6313364c323SKonstantin Belousov 					mtx_unlock(&Giant);
6323364c323SKonstantin Belousov 					return (NULL);
6333364c323SKonstantin Belousov 				}
634ef694c1aSEdward Tomasz Napierala 				crhold(cred);
6353364c323SKonstantin Belousov 			}
6362f7af3dbSAlan Cox 			object = vm_object_allocate(OBJT_DEFAULT, pindex);
63789f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
6383364c323SKonstantin Belousov 			object->handle = handle;
6393364c323SKonstantin Belousov 			if (cred != NULL) {
640ef694c1aSEdward Tomasz Napierala 				object->cred = cred;
6413364c323SKonstantin Belousov 				object->charge = size;
6423364c323SKonstantin Belousov 			}
6434dcc5c2dSMatthew Dillon 			swp_pager_meta_build(object, 0, SWAPBLK_NONE);
64489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
64524a1cce3SDavid Greenman 		}
6460cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
647e793b779SAlan Cox 		mtx_unlock(&Giant);
64824a1cce3SDavid Greenman 	} else {
6493364c323SKonstantin Belousov 		if (cred != NULL) {
650ef694c1aSEdward Tomasz Napierala 			if (!swap_reserve_by_cred(size, cred))
6513364c323SKonstantin Belousov 				return (NULL);
652ef694c1aSEdward Tomasz Napierala 			crhold(cred);
6533364c323SKonstantin Belousov 		}
6542f7af3dbSAlan Cox 		object = vm_object_allocate(OBJT_DEFAULT, pindex);
65589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
6563364c323SKonstantin Belousov 		if (cred != NULL) {
657ef694c1aSEdward Tomasz Napierala 			object->cred = cred;
6583364c323SKonstantin Belousov 			object->charge = size;
6593364c323SKonstantin Belousov 		}
6604dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
66189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
66224a1cce3SDavid Greenman 	}
66324a1cce3SDavid Greenman 	return (object);
664df8bae1dSRodney W. Grimes }
665df8bae1dSRodney W. Grimes 
66626f9a767SRodney W. Grimes /*
6671c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
6681c7c3c6aSMatthew Dillon  *
6691c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
6701c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
6711c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
6721c7c3c6aSMatthew Dillon  *	about to be destroyed.
6731c7c3c6aSMatthew Dillon  *
67415523cf7SKonstantin Belousov  *	The object must be locked.
67526f9a767SRodney W. Grimes  */
676df8bae1dSRodney W. Grimes static void
6772f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object)
67826f9a767SRodney W. Grimes {
6794dcc5c2dSMatthew Dillon 
68026f9a767SRodney W. Grimes 	/*
6811c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
6821c7c3c6aSMatthew Dillon 	 * pageout completion.
68326f9a767SRodney W. Grimes 	 */
684bd228075SAlan Cox 	if (object->handle != NULL) {
685a9fa2c05SAlfred Perlstein 		mtx_lock(&sw_alloc_mtx);
6861c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list);
687a9fa2c05SAlfred Perlstein 		mtx_unlock(&sw_alloc_mtx);
688bd228075SAlan Cox 	}
6891c7c3c6aSMatthew Dillon 
69089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
6911c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
6921c7c3c6aSMatthew Dillon 
6931c7c3c6aSMatthew Dillon 	/*
6941c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
6951c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
6961c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
6971c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
6981c7c3c6aSMatthew Dillon 	 */
6991c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
700*e735691bSJohn Baldwin 	object->handle = NULL;
701*e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
7021c7c3c6aSMatthew Dillon }
7031c7c3c6aSMatthew Dillon 
7041c7c3c6aSMatthew Dillon /************************************************************************
7051c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
7061c7c3c6aSMatthew Dillon  ************************************************************************/
7071c7c3c6aSMatthew Dillon 
7081c7c3c6aSMatthew Dillon /*
7091c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
7101c7c3c6aSMatthew Dillon  *
7111c7c3c6aSMatthew Dillon  *	Allocate swap for the requested number of pages.  The starting
7121c7c3c6aSMatthew Dillon  *	swap block number (a page index) is returned or SWAPBLK_NONE
7131c7c3c6aSMatthew Dillon  *	if the allocation failed.
7141c7c3c6aSMatthew Dillon  *
7151c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
7161c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
7171c7c3c6aSMatthew Dillon  *
71815523cf7SKonstantin Belousov  *	This routine may not sleep.
7198f60c087SPoul-Henning Kamp  *
7208f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
7211c7c3c6aSMatthew Dillon  */
722a5edd34aSPoul-Henning Kamp static daddr_t
7232f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages)
7241c7c3c6aSMatthew Dillon {
7251c7c3c6aSMatthew Dillon 	daddr_t blk;
7268f60c087SPoul-Henning Kamp 	struct swdevt *sp;
7278f60c087SPoul-Henning Kamp 	int i;
7281c7c3c6aSMatthew Dillon 
7298f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
73020da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7318f60c087SPoul-Henning Kamp 	sp = swdevhd;
7328f60c087SPoul-Henning Kamp 	for (i = 0; i < nswapdev; i++) {
7338f60c087SPoul-Henning Kamp 		if (sp == NULL)
7348f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
7358f60c087SPoul-Henning Kamp 		if (!(sp->sw_flags & SW_CLOSING)) {
7368f60c087SPoul-Henning Kamp 			blk = blist_alloc(sp->sw_blist, npages);
7378f60c087SPoul-Henning Kamp 			if (blk != SWAPBLK_NONE) {
7388f60c087SPoul-Henning Kamp 				blk += sp->sw_first;
7398f60c087SPoul-Henning Kamp 				sp->sw_used += npages;
740d05bc129SAlan Cox 				swap_pager_avail -= npages;
7418f60c087SPoul-Henning Kamp 				swp_sizecheck();
7428f60c087SPoul-Henning Kamp 				swdevhd = TAILQ_NEXT(sp, sw_list);
743d05bc129SAlan Cox 				goto done;
7448f60c087SPoul-Henning Kamp 			}
7458f60c087SPoul-Henning Kamp 		}
7468f60c087SPoul-Henning Kamp 		sp = TAILQ_NEXT(sp, sw_list);
7478f60c087SPoul-Henning Kamp 	}
7482b0d37a4SMatthew Dillon 	if (swap_pager_full != 2) {
74920da9c2eSPoul-Henning Kamp 		printf("swap_pager_getswapspace(%d): failed\n", npages);
7502b0d37a4SMatthew Dillon 		swap_pager_full = 2;
75120d3034fSMatthew Dillon 		swap_pager_almost_full = 1;
7522b0d37a4SMatthew Dillon 	}
7538f60c087SPoul-Henning Kamp 	swdevhd = NULL;
754d05bc129SAlan Cox done:
755d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
7561c7c3c6aSMatthew Dillon 	return (blk);
75726f9a767SRodney W. Grimes }
75826f9a767SRodney W. Grimes 
759b3fed13eSDavid Schultz static int
760b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp)
7618f60c087SPoul-Henning Kamp {
7628f60c087SPoul-Henning Kamp 
763b3fed13eSDavid Schultz 	return (blk >= sp->sw_first && blk < sp->sw_end);
7648f60c087SPoul-Henning Kamp }
7658f60c087SPoul-Henning Kamp 
7664b03903aSPoul-Henning Kamp static void
7674b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp)
7684b03903aSPoul-Henning Kamp {
7694b03903aSPoul-Henning Kamp 	struct swdevt *sp;
7704b03903aSPoul-Henning Kamp 
77120da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7724b03903aSPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7734b03903aSPoul-Henning Kamp 		if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) {
77420da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
7752cc718a1SKonstantin Belousov 			if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
7762cc718a1SKonstantin Belousov 			    unmapped_buf_allowed) {
7772cc718a1SKonstantin Belousov 				bp->b_kvaalloc = bp->b_data;
7782cc718a1SKonstantin Belousov 				bp->b_data = unmapped_buf;
7792cc718a1SKonstantin Belousov 				bp->b_kvabase = unmapped_buf;
7802cc718a1SKonstantin Belousov 				bp->b_offset = 0;
7812cc718a1SKonstantin Belousov 				bp->b_flags |= B_UNMAPPED;
7822cc718a1SKonstantin Belousov 			} else {
7832cc718a1SKonstantin Belousov 				pmap_qenter((vm_offset_t)bp->b_data,
7842cc718a1SKonstantin Belousov 				    &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
7852cc718a1SKonstantin Belousov 			}
7864b03903aSPoul-Henning Kamp 			sp->sw_strategy(bp, sp);
7874b03903aSPoul-Henning Kamp 			return;
7884b03903aSPoul-Henning Kamp 		}
7894b03903aSPoul-Henning Kamp 	}
79020da9c2eSPoul-Henning Kamp 	panic("Swapdev not found");
7914b03903aSPoul-Henning Kamp }
7924b03903aSPoul-Henning Kamp 
7938f60c087SPoul-Henning Kamp 
79426f9a767SRodney W. Grimes /*
7951c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
7961c7c3c6aSMatthew Dillon  *
7971c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
7981c7c3c6aSMatthew Dillon  *
79915523cf7SKonstantin Belousov  *	This routine may not sleep.
80026f9a767SRodney W. Grimes  */
801a5edd34aSPoul-Henning Kamp static void
8028f60c087SPoul-Henning Kamp swp_pager_freeswapspace(daddr_t blk, int npages)
8030d94caffSDavid Greenman {
8048f60c087SPoul-Henning Kamp 	struct swdevt *sp;
80592da00bbSMatthew Dillon 
8067645e885SAlan Cox 	mtx_lock(&sw_dev_mtx);
8077645e885SAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
8087645e885SAlan Cox 		if (blk >= sp->sw_first && blk < sp->sw_end) {
80992da00bbSMatthew Dillon 			sp->sw_used -= npages;
81092da00bbSMatthew Dillon 			/*
8117645e885SAlan Cox 			 * If we are attempting to stop swapping on
8127645e885SAlan Cox 			 * this device, we don't want to mark any
8137645e885SAlan Cox 			 * blocks free lest they be reused.
81492da00bbSMatthew Dillon 			 */
8157645e885SAlan Cox 			if ((sp->sw_flags & SW_CLOSING) == 0) {
8167645e885SAlan Cox 				blist_free(sp->sw_blist, blk - sp->sw_first,
8177645e885SAlan Cox 				    npages);
8188f60c087SPoul-Henning Kamp 				swap_pager_avail += npages;
8191c7c3c6aSMatthew Dillon 				swp_sizecheck();
82026f9a767SRodney W. Grimes 			}
8217645e885SAlan Cox 			mtx_unlock(&sw_dev_mtx);
8227645e885SAlan Cox 			return;
8237645e885SAlan Cox 		}
8247645e885SAlan Cox 	}
8257645e885SAlan Cox 	panic("Swapdev not found");
8267645e885SAlan Cox }
8271c7c3c6aSMatthew Dillon 
82826f9a767SRodney W. Grimes /*
8291c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
8301c7c3c6aSMatthew Dillon  *				range within an object.
8311c7c3c6aSMatthew Dillon  *
8321c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
8331c7c3c6aSMatthew Dillon  *
8341c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
8351c7c3c6aSMatthew Dillon  *
8361c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
8371c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
8381c7c3c6aSMatthew Dillon  *	we should be ok.
839c25673ffSAttilio Rao  *
840c25673ffSAttilio Rao  *	The object must be locked.
84126f9a767SRodney W. Grimes  */
84226f9a767SRodney W. Grimes void
8432f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
84426f9a767SRodney W. Grimes {
84523955314SAlfred Perlstein 
8461c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
8474dcc5c2dSMatthew Dillon }
8484dcc5c2dSMatthew Dillon 
8494dcc5c2dSMatthew Dillon /*
8504dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
8514dcc5c2dSMatthew Dillon  *
8524dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
85356ce850bSKonstantin Belousov  *	swap blocks are not zeroed.  Any previous swap assignment is destroyed.
8544dcc5c2dSMatthew Dillon  *
8554dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
8564dcc5c2dSMatthew Dillon  */
8574dcc5c2dSMatthew Dillon int
8584dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
8594dcc5c2dSMatthew Dillon {
8604dcc5c2dSMatthew Dillon 	int n = 0;
8614dcc5c2dSMatthew Dillon 	daddr_t blk = SWAPBLK_NONE;
8624dcc5c2dSMatthew Dillon 	vm_pindex_t beg = start;	/* save start index */
8634dcc5c2dSMatthew Dillon 
86489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
8654dcc5c2dSMatthew Dillon 	while (size) {
8664dcc5c2dSMatthew Dillon 		if (n == 0) {
8674dcc5c2dSMatthew Dillon 			n = BLIST_MAX_ALLOC;
8684dcc5c2dSMatthew Dillon 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
8694dcc5c2dSMatthew Dillon 				n >>= 1;
8704dcc5c2dSMatthew Dillon 				if (n == 0) {
8714dcc5c2dSMatthew Dillon 					swp_pager_meta_free(object, beg, start - beg);
87289f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(object);
8734dcc5c2dSMatthew Dillon 					return (-1);
8744dcc5c2dSMatthew Dillon 				}
8754dcc5c2dSMatthew Dillon 			}
8764dcc5c2dSMatthew Dillon 		}
8774dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, start, blk);
8784dcc5c2dSMatthew Dillon 		--size;
8794dcc5c2dSMatthew Dillon 		++start;
8804dcc5c2dSMatthew Dillon 		++blk;
8814dcc5c2dSMatthew Dillon 		--n;
8824dcc5c2dSMatthew Dillon 	}
8834dcc5c2dSMatthew Dillon 	swp_pager_meta_free(object, start, n);
88489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
8854dcc5c2dSMatthew Dillon 	return (0);
88626f9a767SRodney W. Grimes }
88726f9a767SRodney W. Grimes 
8880a47b48bSJohn Dyson /*
8891c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
8901c7c3c6aSMatthew Dillon  *			and destroy the source.
8911c7c3c6aSMatthew Dillon  *
8921c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
8931c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
8941c7c3c6aSMatthew Dillon  *	we keep the destination's.
8951c7c3c6aSMatthew Dillon  *
89615523cf7SKonstantin Belousov  *	This routine is allowed to sleep.  It may sleep allocating metadata
8971c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
8981c7c3c6aSMatthew Dillon  *	progress on the source.
8991c7c3c6aSMatthew Dillon  *
9001c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
9011c7c3c6aSMatthew Dillon  *
9021c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
9031c7c3c6aSMatthew Dillon  *
90415523cf7SKonstantin Belousov  *	The source and destination objects must be locked.
90515523cf7SKonstantin Belousov  *	Both object locks may temporarily be released.
90626f9a767SRodney W. Grimes  */
90726f9a767SRodney W. Grimes void
9082f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
9092f249180SPoul-Henning Kamp     vm_pindex_t offset, int destroysource)
91026f9a767SRodney W. Grimes {
911a316d390SJohn Dyson 	vm_pindex_t i;
9124dcc5c2dSMatthew Dillon 
91389f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(srcobject);
91489f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(dstobject);
9150cddd8f0SMatthew Dillon 
91626f9a767SRodney W. Grimes 	/*
9171c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
9181c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
91926f9a767SRodney W. Grimes 	 */
920cbd8ec09SJohn Dyson 	if (destroysource) {
921bd228075SAlan Cox 		if (srcobject->handle != NULL) {
922a9fa2c05SAlfred Perlstein 			mtx_lock(&sw_alloc_mtx);
9231c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
9241c7c3c6aSMatthew Dillon 			    NOBJLIST(srcobject->handle),
9251c7c3c6aSMatthew Dillon 			    srcobject,
9261c7c3c6aSMatthew Dillon 			    pager_object_list
9271c7c3c6aSMatthew Dillon 			);
928a9fa2c05SAlfred Perlstein 			mtx_unlock(&sw_alloc_mtx);
929cbd8ec09SJohn Dyson 		}
930bd228075SAlan Cox 	}
93126f9a767SRodney W. Grimes 
9321c7c3c6aSMatthew Dillon 	/*
9331c7c3c6aSMatthew Dillon 	 * transfer source to destination.
9341c7c3c6aSMatthew Dillon 	 */
9351c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
9361c7c3c6aSMatthew Dillon 		daddr_t dstaddr;
9371c7c3c6aSMatthew Dillon 
9381c7c3c6aSMatthew Dillon 		/*
9391c7c3c6aSMatthew Dillon 		 * Locate (without changing) the swapblk on the destination,
9401c7c3c6aSMatthew Dillon 		 * unless it is invalid in which case free it silently, or
9411c7c3c6aSMatthew Dillon 		 * if the destination is a resident page, in which case the
9421c7c3c6aSMatthew Dillon 		 * source is thrown away.
9431c7c3c6aSMatthew Dillon 		 */
9441c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
9451c7c3c6aSMatthew Dillon 
9461c7c3c6aSMatthew Dillon 		if (dstaddr == SWAPBLK_NONE) {
9471c7c3c6aSMatthew Dillon 			/*
9481c7c3c6aSMatthew Dillon 			 * Destination has no swapblk and is not resident,
9491c7c3c6aSMatthew Dillon 			 * copy source.
9501c7c3c6aSMatthew Dillon 			 */
9511c7c3c6aSMatthew Dillon 			daddr_t srcaddr;
9521c7c3c6aSMatthew Dillon 
9531c7c3c6aSMatthew Dillon 			srcaddr = swp_pager_meta_ctl(
9541c7c3c6aSMatthew Dillon 			    srcobject,
9551c7c3c6aSMatthew Dillon 			    i + offset,
9561c7c3c6aSMatthew Dillon 			    SWM_POP
9571c7c3c6aSMatthew Dillon 			);
9581c7c3c6aSMatthew Dillon 
959ee3dc7d7SAlan Cox 			if (srcaddr != SWAPBLK_NONE) {
960c7c8dd7eSAlan Cox 				/*
961c7c8dd7eSAlan Cox 				 * swp_pager_meta_build() can sleep.
962c7c8dd7eSAlan Cox 				 */
963c7c8dd7eSAlan Cox 				vm_object_pip_add(srcobject, 1);
96489f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(srcobject);
965c7c8dd7eSAlan Cox 				vm_object_pip_add(dstobject, 1);
9664dcc5c2dSMatthew Dillon 				swp_pager_meta_build(dstobject, i, srcaddr);
967c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(dstobject);
96889f6b863SAttilio Rao 				VM_OBJECT_WLOCK(srcobject);
969c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(srcobject);
970ee3dc7d7SAlan Cox 			}
9711c7c3c6aSMatthew Dillon 		} else {
9721c7c3c6aSMatthew Dillon 			/*
9731c7c3c6aSMatthew Dillon 			 * Destination has valid swapblk or it is represented
9741c7c3c6aSMatthew Dillon 			 * by a resident page.  We destroy the sourceblock.
9751c7c3c6aSMatthew Dillon 			 */
9761c7c3c6aSMatthew Dillon 
9771c7c3c6aSMatthew Dillon 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
9781c7c3c6aSMatthew Dillon 		}
97926f9a767SRodney W. Grimes 	}
98026f9a767SRodney W. Grimes 
98126f9a767SRodney W. Grimes 	/*
9821c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
9831c7c3c6aSMatthew Dillon 	 *
9841c7c3c6aSMatthew Dillon 	 * We have to revert the type to OBJT_DEFAULT so we do not accidently
9851c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
98626f9a767SRodney W. Grimes 	 */
987c0877f10SJohn Dyson 	if (destroysource) {
9881c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
9891c7c3c6aSMatthew Dillon 		/*
9901c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
9911c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
992956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
9931c7c3c6aSMatthew Dillon 		 * queues.
9941c7c3c6aSMatthew Dillon 		 */
9951c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
996c0877f10SJohn Dyson 	}
99726f9a767SRodney W. Grimes }
99826f9a767SRodney W. Grimes 
999df8bae1dSRodney W. Grimes /*
10001c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
10011c7c3c6aSMatthew Dillon  *				the requested page.
10021c7c3c6aSMatthew Dillon  *
10031c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
10041c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
10051c7c3c6aSMatthew Dillon  *
10061c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
10071c7c3c6aSMatthew Dillon  *	store exists before and after the requested page within a reasonable
10081c7c3c6aSMatthew Dillon  *	distance.  We do not try to restrict it to the swap device stripe
10091c7c3c6aSMatthew Dillon  *	(that is handled in getpages/putpages).  It probably isn't worth
10101c7c3c6aSMatthew Dillon  *	doing here.
1011df8bae1dSRodney W. Grimes  */
10125ea4972cSAlan Cox static boolean_t
10132f249180SPoul-Henning Kamp swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after)
101426f9a767SRodney W. Grimes {
10151c7c3c6aSMatthew Dillon 	daddr_t blk0;
101626f9a767SRodney W. Grimes 
1017c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
10181c7c3c6aSMatthew Dillon 	/*
10191c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
10201c7c3c6aSMatthew Dillon 	 */
10211c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
10221c7c3c6aSMatthew Dillon 
10234dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
10241c7c3c6aSMatthew Dillon 		if (before)
102524a1cce3SDavid Greenman 			*before = 0;
10261c7c3c6aSMatthew Dillon 		if (after)
102724a1cce3SDavid Greenman 			*after = 0;
102826f9a767SRodney W. Grimes 		return (FALSE);
102926f9a767SRodney W. Grimes 	}
103026f9a767SRodney W. Grimes 
103126f9a767SRodney W. Grimes 	/*
10321c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
1033e47ed70bSJohn Dyson 	 */
10341c7c3c6aSMatthew Dillon 	if (before != NULL) {
103526f9a767SRodney W. Grimes 		int i;
10360d94caffSDavid Greenman 
10371c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
10381c7c3c6aSMatthew Dillon 			daddr_t blk;
10391c7c3c6aSMatthew Dillon 
10401c7c3c6aSMatthew Dillon 			if (i > pindex)
10411c7c3c6aSMatthew Dillon 				break;
10421c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
10431c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
10441c7c3c6aSMatthew Dillon 				break;
1045ffc82b0aSJohn Dyson 		}
10461c7c3c6aSMatthew Dillon 		*before = (i - 1);
104726f9a767SRodney W. Grimes 	}
104826f9a767SRodney W. Grimes 
104926f9a767SRodney W. Grimes 	/*
10501c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
105126f9a767SRodney W. Grimes 	 */
10521c7c3c6aSMatthew Dillon 	if (after != NULL) {
10531c7c3c6aSMatthew Dillon 		int i;
10541c7c3c6aSMatthew Dillon 
10551c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
10561c7c3c6aSMatthew Dillon 			daddr_t blk;
10571c7c3c6aSMatthew Dillon 
10581c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
10591c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
10601c7c3c6aSMatthew Dillon 				break;
106126f9a767SRodney W. Grimes 		}
10621c7c3c6aSMatthew Dillon 		*after = (i - 1);
10631c7c3c6aSMatthew Dillon 	}
10641c7c3c6aSMatthew Dillon 	return (TRUE);
10651c7c3c6aSMatthew Dillon }
10661c7c3c6aSMatthew Dillon 
10671c7c3c6aSMatthew Dillon /*
10681c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
10691c7c3c6aSMatthew Dillon  *
10701c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
10711c7c3c6aSMatthew Dillon  *	not, from the page.
10721c7c3c6aSMatthew Dillon  *
10731c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
10741c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
10751c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
10761c7c3c6aSMatthew Dillon  *
10771c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
10781c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
10791c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
10801c7c3c6aSMatthew Dillon  *	depends on it.
10811c7c3c6aSMatthew Dillon  *
108215523cf7SKonstantin Belousov  *	This routine may not sleep.
1083c25673ffSAttilio Rao  *
1084c25673ffSAttilio Rao  *	The object containing the page must be locked.
10851c7c3c6aSMatthew Dillon  */
10861c7c3c6aSMatthew Dillon static void
10872f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m)
10881c7c3c6aSMatthew Dillon {
10892f249180SPoul-Henning Kamp 
10901c7c3c6aSMatthew Dillon 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
10911c7c3c6aSMatthew Dillon }
10921c7c3c6aSMatthew Dillon 
10931c7c3c6aSMatthew Dillon /*
10941c7c3c6aSMatthew Dillon  * SWAP_PAGER_GETPAGES() - bring pages in from swap
10951c7c3c6aSMatthew Dillon  *
10961c7c3c6aSMatthew Dillon  *	Attempt to retrieve (m, count) pages from backing store, but make
10971c7c3c6aSMatthew Dillon  *	sure we retrieve at least m[reqpage].  We try to load in as large
10981c7c3c6aSMatthew Dillon  *	a chunk surrounding m[reqpage] as is contiguous in swap and which
10991c7c3c6aSMatthew Dillon  *	belongs to the same object.
11001c7c3c6aSMatthew Dillon  *
11011c7c3c6aSMatthew Dillon  *	The code is designed for asynchronous operation and
11021c7c3c6aSMatthew Dillon  *	immediate-notification of 'reqpage' but tends not to be
11031c7c3c6aSMatthew Dillon  *	used that way.  Please do not optimize-out this algorithmic
11041c7c3c6aSMatthew Dillon  *	feature, I intend to improve on it in the future.
11051c7c3c6aSMatthew Dillon  *
11061c7c3c6aSMatthew Dillon  *	The parent has a single vm_object_pip_add() reference prior to
11071c7c3c6aSMatthew Dillon  *	calling us and we should return with the same.
11081c7c3c6aSMatthew Dillon  *
11091c7c3c6aSMatthew Dillon  *	The parent has BUSY'd the pages.  We should return with 'm'
11101c7c3c6aSMatthew Dillon  *	left busy, but the others adjusted.
11111c7c3c6aSMatthew Dillon  */
1112f708ef1bSPoul-Henning Kamp static int
11132f249180SPoul-Henning Kamp swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
1114df8bae1dSRodney W. Grimes {
11151c7c3c6aSMatthew Dillon 	struct buf *bp;
11161c7c3c6aSMatthew Dillon 	vm_page_t mreq;
111726f9a767SRodney W. Grimes 	int i;
111826f9a767SRodney W. Grimes 	int j;
11191c7c3c6aSMatthew Dillon 	daddr_t blk;
11200d94caffSDavid Greenman 
11211c7c3c6aSMatthew Dillon 	mreq = m[reqpage];
11221c7c3c6aSMatthew Dillon 
1123e04e4bacSPoul-Henning Kamp 	KASSERT(mreq->object == object,
1124e04e4bacSPoul-Henning Kamp 	    ("swap_pager_getpages: object mismatch %p/%p",
1125e04e4bacSPoul-Henning Kamp 	    object, mreq->object));
1126e04e4bacSPoul-Henning Kamp 
11271c7c3c6aSMatthew Dillon 	/*
11281c7c3c6aSMatthew Dillon 	 * Calculate range to retrieve.  The pages have already been assigned
1129751221fdSPoul-Henning Kamp 	 * their swapblks.  We require a *contiguous* range but we know it to
1130751221fdSPoul-Henning Kamp 	 * not span devices.   If we do not supply it, bad things
11314dcc5c2dSMatthew Dillon 	 * happen.  Note that blk, iblk & jblk can be SWAPBLK_NONE, but the
11324dcc5c2dSMatthew Dillon 	 * loops are set up such that the case(s) are handled implicitly.
11334dcc5c2dSMatthew Dillon 	 *
11340b6ace47SAlan Cox 	 * The swp_*() calls must be made with the object locked.
11351c7c3c6aSMatthew Dillon 	 */
11361c7c3c6aSMatthew Dillon 	blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0);
11371c7c3c6aSMatthew Dillon 
11381c7c3c6aSMatthew Dillon 	for (i = reqpage - 1; i >= 0; --i) {
11391c7c3c6aSMatthew Dillon 		daddr_t iblk;
11401c7c3c6aSMatthew Dillon 
11411c7c3c6aSMatthew Dillon 		iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0);
11421c7c3c6aSMatthew Dillon 		if (blk != iblk + (reqpage - i))
114326f9a767SRodney W. Grimes 			break;
114426f9a767SRodney W. Grimes 	}
11451c7c3c6aSMatthew Dillon 	++i;
11461c7c3c6aSMatthew Dillon 
11471c7c3c6aSMatthew Dillon 	for (j = reqpage + 1; j < count; ++j) {
11481c7c3c6aSMatthew Dillon 		daddr_t jblk;
11491c7c3c6aSMatthew Dillon 
11501c7c3c6aSMatthew Dillon 		jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0);
11511c7c3c6aSMatthew Dillon 		if (blk != jblk - (j - reqpage))
11521c7c3c6aSMatthew Dillon 			break;
11531c7c3c6aSMatthew Dillon 	}
11541c7c3c6aSMatthew Dillon 
11551c7c3c6aSMatthew Dillon 	/*
11561c7c3c6aSMatthew Dillon 	 * free pages outside our collection range.   Note: we never free
11571c7c3c6aSMatthew Dillon 	 * mreq, it must remain busy throughout.
11581c7c3c6aSMatthew Dillon 	 */
1159071a1710SAlan Cox 	if (0 < i || j < count) {
11601c7c3c6aSMatthew Dillon 		int k;
11611c7c3c6aSMatthew Dillon 
11623c4a2440SAlan Cox 		for (k = 0; k < i; ++k)
1163e86a87e9SKonstantin Belousov 			swp_pager_free_nrpage(m[k]);
11643c4a2440SAlan Cox 		for (k = j; k < count; ++k)
11652965a453SKip Macy 			swp_pager_free_nrpage(m[k]);
1166071a1710SAlan Cox 	}
11671c7c3c6aSMatthew Dillon 
11681c7c3c6aSMatthew Dillon 	/*
11694dcc5c2dSMatthew Dillon 	 * Return VM_PAGER_FAIL if we have nothing to do.  Return mreq
11704dcc5c2dSMatthew Dillon 	 * still busy, but the others unbusied.
11711c7c3c6aSMatthew Dillon 	 */
11724dcc5c2dSMatthew Dillon 	if (blk == SWAPBLK_NONE)
117326f9a767SRodney W. Grimes 		return (VM_PAGER_FAIL);
1174df8bae1dSRodney W. Grimes 
117516f62314SDavid Greenman 	/*
11768630c117SAlan Cox 	 * Getpbuf() can sleep.
11778630c117SAlan Cox 	 */
117889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
11798630c117SAlan Cox 	/*
118016f62314SDavid Greenman 	 * Get a swap buffer header to perform the IO
118116f62314SDavid Greenman 	 */
11821c7c3c6aSMatthew Dillon 	bp = getpbuf(&nsw_rcount);
11835e04322aSPoul-Henning Kamp 	bp->b_flags |= B_PAGING;
118426f9a767SRodney W. Grimes 
118521144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
11861c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1187fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1188fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
11891c7c3c6aSMatthew Dillon 	bp->b_blkno = blk - (reqpage - i);
11901c7c3c6aSMatthew Dillon 	bp->b_bcount = PAGE_SIZE * (j - i);
11911c7c3c6aSMatthew Dillon 	bp->b_bufsize = PAGE_SIZE * (j - i);
11921c7c3c6aSMatthew Dillon 	bp->b_pager.pg_reqpage = reqpage - i;
11931c7c3c6aSMatthew Dillon 
119489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
11951c7c3c6aSMatthew Dillon 	{
11961c7c3c6aSMatthew Dillon 		int k;
11971c7c3c6aSMatthew Dillon 
11981c7c3c6aSMatthew Dillon 		for (k = i; k < j; ++k) {
11991c7c3c6aSMatthew Dillon 			bp->b_pages[k - i] = m[k];
12005786be7cSAlan Cox 			m[k]->oflags |= VPO_SWAPINPROG;
12011c7c3c6aSMatthew Dillon 		}
12021c7c3c6aSMatthew Dillon 	}
12031c7c3c6aSMatthew Dillon 	bp->b_npages = j - i;
120426f9a767SRodney W. Grimes 
1205b4b70819SAttilio Rao 	PCPU_INC(cnt.v_swapin);
1206b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_swappgsin, bp->b_npages);
12071c7c3c6aSMatthew Dillon 
1208df8bae1dSRodney W. Grimes 	/*
12091c7c3c6aSMatthew Dillon 	 * We still hold the lock on mreq, and our automatic completion routine
12101c7c3c6aSMatthew Dillon 	 * does not remove it.
1211df8bae1dSRodney W. Grimes 	 */
1212071a1710SAlan Cox 	vm_object_pip_add(object, bp->b_npages);
121389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
12141c7c3c6aSMatthew Dillon 
12151c7c3c6aSMatthew Dillon 	/*
12161c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
12171c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
12181c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
12191c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
12201c7c3c6aSMatthew Dillon 	 *
12211c7c3c6aSMatthew Dillon 	 * The other pages in our m[] array are also released on completion,
12221c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
12231c7c3c6aSMatthew Dillon 	 *
1224c37a77eeSPoul-Henning Kamp 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
12251c7c3c6aSMatthew Dillon 	 */
1226b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
12274b03903aSPoul-Henning Kamp 	swp_pager_strategy(bp);
122826f9a767SRodney W. Grimes 
122926f9a767SRodney W. Grimes 	/*
12305786be7cSAlan Cox 	 * wait for the page we want to complete.  VPO_SWAPINPROG is always
12311c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
12321c7c3c6aSMatthew Dillon 	 * is set in the meta-data.
123326f9a767SRodney W. Grimes 	 */
123489f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
12355786be7cSAlan Cox 	while ((mreq->oflags & VPO_SWAPINPROG) != 0) {
1236c7aebda8SAttilio Rao 		mreq->oflags |= VPO_SWAPSLEEP;
1237b4b70819SAttilio Rao 		PCPU_INC(cnt.v_intrans);
1238c7aebda8SAttilio Rao 		if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP,
1239c7aebda8SAttilio Rao 		    "swread", hz * 20)) {
12409bd86a98SBruce M Simpson 			printf(
1241c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1242c5690651SPoul-Henning Kamp 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
12431c7c3c6aSMatthew Dillon 		}
12441b119d9dSDavid Greenman 	}
124526f9a767SRodney W. Grimes 
124626f9a767SRodney W. Grimes 	/*
1247a1287949SEivind Eklund 	 * mreq is left busied after completion, but all the other pages
12481c7c3c6aSMatthew Dillon 	 * are freed.  If we had an unrecoverable read error the page will
12491c7c3c6aSMatthew Dillon 	 * not be valid.
125026f9a767SRodney W. Grimes 	 */
12511c7c3c6aSMatthew Dillon 	if (mreq->valid != VM_PAGE_BITS_ALL) {
12521c7c3c6aSMatthew Dillon 		return (VM_PAGER_ERROR);
125326f9a767SRodney W. Grimes 	} else {
12541c7c3c6aSMatthew Dillon 		return (VM_PAGER_OK);
125526f9a767SRodney W. Grimes 	}
12561c7c3c6aSMatthew Dillon 
12571c7c3c6aSMatthew Dillon 	/*
12581c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
12591c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
12601c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
12611c7c3c6aSMatthew Dillon 	 * to all-zero's later.
12621c7c3c6aSMatthew Dillon 	 */
1263df8bae1dSRodney W. Grimes }
1264df8bae1dSRodney W. Grimes 
12651c7c3c6aSMatthew Dillon /*
126690effb23SGleb Smirnoff  * 	swap_pager_getpages_async():
126790effb23SGleb Smirnoff  *
126890effb23SGleb Smirnoff  *	Right now this is emulation of asynchronous operation on top of
126990effb23SGleb Smirnoff  *	swap_pager_getpages().
127090effb23SGleb Smirnoff  */
127190effb23SGleb Smirnoff static int
127290effb23SGleb Smirnoff swap_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
127390effb23SGleb Smirnoff     int reqpage, pgo_getpages_iodone_t iodone, void *arg)
127490effb23SGleb Smirnoff {
127590effb23SGleb Smirnoff 	int r, error;
127690effb23SGleb Smirnoff 
127790effb23SGleb Smirnoff 	r = swap_pager_getpages(object, m, count, reqpage);
127890effb23SGleb Smirnoff 	VM_OBJECT_WUNLOCK(object);
127990effb23SGleb Smirnoff 	switch (r) {
128090effb23SGleb Smirnoff 	case VM_PAGER_OK:
128190effb23SGleb Smirnoff 		error = 0;
128290effb23SGleb Smirnoff 		break;
128390effb23SGleb Smirnoff 	case VM_PAGER_ERROR:
128490effb23SGleb Smirnoff 		error = EIO;
128590effb23SGleb Smirnoff 		break;
128690effb23SGleb Smirnoff 	case VM_PAGER_FAIL:
128790effb23SGleb Smirnoff 		error = EINVAL;
128890effb23SGleb Smirnoff 		break;
128990effb23SGleb Smirnoff 	default:
1290d9328101SGleb Smirnoff 		panic("unhandled swap_pager_getpages() error %d", r);
129190effb23SGleb Smirnoff 	}
129290effb23SGleb Smirnoff 	(iodone)(arg, m, count, error);
129390effb23SGleb Smirnoff 	VM_OBJECT_WLOCK(object);
129490effb23SGleb Smirnoff 
129590effb23SGleb Smirnoff 	return (r);
129690effb23SGleb Smirnoff }
129790effb23SGleb Smirnoff 
129890effb23SGleb Smirnoff /*
12991c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
13001c7c3c6aSMatthew Dillon  *
13011c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
13021c7c3c6aSMatthew Dillon  *
13031c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
13041c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
13051c7c3c6aSMatthew Dillon  *
1306ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
13071c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
13081c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
13091c7c3c6aSMatthew Dillon  *	which needs work.
13101c7c3c6aSMatthew Dillon  *
13111c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
13121c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
13131c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
13141c7c3c6aSMatthew Dillon  *	completion.
13151c7c3c6aSMatthew Dillon  *
13161c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
13171c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
13181c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
13191c7c3c6aSMatthew Dillon  */
1320e4542174SMatthew Dillon void
13212f249180SPoul-Henning Kamp swap_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1322e065e87cSKonstantin Belousov     int flags, int *rtvals)
1323df8bae1dSRodney W. Grimes {
1324e065e87cSKonstantin Belousov 	int i, n;
1325e065e87cSKonstantin Belousov 	boolean_t sync;
1326df8bae1dSRodney W. Grimes 
13271c7c3c6aSMatthew Dillon 	if (count && m[0]->object != object) {
13287036145bSMaxim Konovalov 		panic("swap_pager_putpages: object mismatch %p/%p",
13291c7c3c6aSMatthew Dillon 		    object,
13301c7c3c6aSMatthew Dillon 		    m[0]->object
13311c7c3c6aSMatthew Dillon 		);
13321c7c3c6aSMatthew Dillon 	}
1333ee3dc7d7SAlan Cox 
13341c7c3c6aSMatthew Dillon 	/*
13351c7c3c6aSMatthew Dillon 	 * Step 1
13361c7c3c6aSMatthew Dillon 	 *
13371c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
13381c7c3c6aSMatthew Dillon 	 * check for bogus sysops
13391c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
13401c7c3c6aSMatthew Dillon 	 */
13414dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
13424dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
134389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
1344e47ed70bSJohn Dyson 
1345e065e87cSKonstantin Belousov 	n = 0;
1346e47ed70bSJohn Dyson 	if (curproc != pageproc)
1347e47ed70bSJohn Dyson 		sync = TRUE;
1348e065e87cSKonstantin Belousov 	else
1349e065e87cSKonstantin Belousov 		sync = (flags & VM_PAGER_PUT_SYNC) != 0;
135026f9a767SRodney W. Grimes 
13511c7c3c6aSMatthew Dillon 	/*
13521c7c3c6aSMatthew Dillon 	 * Step 2
13531c7c3c6aSMatthew Dillon 	 *
13541c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
13551c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
13561c7c3c6aSMatthew Dillon 	 * successfully.
13571c7c3c6aSMatthew Dillon 	 */
13581c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
13591c7c3c6aSMatthew Dillon 		int j;
13601c7c3c6aSMatthew Dillon 		struct buf *bp;
1361a316d390SJohn Dyson 		daddr_t blk;
136226f9a767SRodney W. Grimes 
1363df8bae1dSRodney W. Grimes 		/*
13641c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1365df8bae1dSRodney W. Grimes 		 */
13661c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1367327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
13681c7c3c6aSMatthew Dillon 
136926f9a767SRodney W. Grimes 		/*
13701c7c3c6aSMatthew Dillon 		 * Get biggest block of swap we can.  If we fail, fall
13711c7c3c6aSMatthew Dillon 		 * back and try to allocate a smaller block.  Don't go
13721c7c3c6aSMatthew Dillon 		 * overboard trying to allocate space if it would overly
13731c7c3c6aSMatthew Dillon 		 * fragment swap.
137426f9a767SRodney W. Grimes 		 */
13751c7c3c6aSMatthew Dillon 		while (
13761c7c3c6aSMatthew Dillon 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
13771c7c3c6aSMatthew Dillon 		    n > 4
13781c7c3c6aSMatthew Dillon 		) {
13791c7c3c6aSMatthew Dillon 			n >>= 1;
138026f9a767SRodney W. Grimes 		}
13811c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
13824dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
13831c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
13841c7c3c6aSMatthew Dillon 			continue;
138526f9a767SRodney W. Grimes 		}
138626f9a767SRodney W. Grimes 
138726f9a767SRodney W. Grimes 		/*
13881c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
13891c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
139026f9a767SRodney W. Grimes 		 */
1391327f4e83SMatthew Dillon 		if (sync == TRUE) {
1392327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_sync);
1393327f4e83SMatthew Dillon 		} else {
1394327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_async);
139521144e3bSPoul-Henning Kamp 			bp->b_flags = B_ASYNC;
1396327f4e83SMatthew Dillon 		}
13975e04322aSPoul-Henning Kamp 		bp->b_flags |= B_PAGING;
1398912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
139926f9a767SRodney W. Grimes 
1400fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1401fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
14021c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
14031c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
14041c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1405e47ed70bSJohn Dyson 
140689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
14071c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
14081c7c3c6aSMatthew Dillon 			vm_page_t mreq = m[i+j];
14091c7c3c6aSMatthew Dillon 
14101c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
14111c7c3c6aSMatthew Dillon 			    mreq->object,
14121c7c3c6aSMatthew Dillon 			    mreq->pindex,
14134dcc5c2dSMatthew Dillon 			    blk + j
14141c7c3c6aSMatthew Dillon 			);
14157dbf82dcSMatthew Dillon 			vm_page_dirty(mreq);
14161c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_OK;
14171c7c3c6aSMatthew Dillon 
14185786be7cSAlan Cox 			mreq->oflags |= VPO_SWAPINPROG;
14191c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
14201c7c3c6aSMatthew Dillon 		}
142189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
14221c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1423a5296b05SJulian Elischer 		/*
1424a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1425a5296b05SJulian Elischer 		 */
1426a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1427a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
14281c7c3c6aSMatthew Dillon 
1429b4b70819SAttilio Rao 		PCPU_INC(cnt.v_swapout);
1430b4b70819SAttilio Rao 		PCPU_ADD(cnt.v_swappgsout, bp->b_npages);
143126f9a767SRodney W. Grimes 
143226f9a767SRodney W. Grimes 		/*
14331c7c3c6aSMatthew Dillon 		 * asynchronous
14341c7c3c6aSMatthew Dillon 		 *
1435c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
143626f9a767SRodney W. Grimes 		 */
14371c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
14381c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
143967812eacSKirk McKusick 			BUF_KERNPROC(bp);
14404b03903aSPoul-Henning Kamp 			swp_pager_strategy(bp);
14411c7c3c6aSMatthew Dillon 
14421c7c3c6aSMatthew Dillon 			for (j = 0; j < n; ++j)
14431c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_PEND;
144423955314SAlfred Perlstein 			/* restart outter loop */
14451c7c3c6aSMatthew Dillon 			continue;
144626f9a767SRodney W. Grimes 		}
1447e47ed70bSJohn Dyson 
144826f9a767SRodney W. Grimes 		/*
14491c7c3c6aSMatthew Dillon 		 * synchronous
14501c7c3c6aSMatthew Dillon 		 *
1451c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
14521c7c3c6aSMatthew Dillon 		 */
14532c840b1fSAlan Cox 		bp->b_iodone = bdone;
14544b03903aSPoul-Henning Kamp 		swp_pager_strategy(bp);
14551c7c3c6aSMatthew Dillon 
14561c7c3c6aSMatthew Dillon 		/*
14571c7c3c6aSMatthew Dillon 		 * Wait for the sync I/O to complete, then update rtvals.
14581c7c3c6aSMatthew Dillon 		 * We just set the rtvals[] to VM_PAGER_PEND so we can call
14591c7c3c6aSMatthew Dillon 		 * our async completion routine at the end, thus avoiding a
14601c7c3c6aSMatthew Dillon 		 * double-free.
146126f9a767SRodney W. Grimes 		 */
14622c840b1fSAlan Cox 		bwait(bp, PVM, "swwrt");
14631c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j)
14641c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_PEND;
14651c7c3c6aSMatthew Dillon 		/*
14661c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
14671c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
14681c7c3c6aSMatthew Dillon 		 */
14691c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
14701c7c3c6aSMatthew Dillon 	}
147189f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
14721c7c3c6aSMatthew Dillon }
14731c7c3c6aSMatthew Dillon 
14741c7c3c6aSMatthew Dillon /*
14751c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
14761c7c3c6aSMatthew Dillon  *
14771c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
14781c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
14791c7c3c6aSMatthew Dillon  *
148015523cf7SKonstantin Belousov  *	This routine may not sleep.
14811c7c3c6aSMatthew Dillon  */
14821c7c3c6aSMatthew Dillon static void
14832f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp)
14841c7c3c6aSMatthew Dillon {
14851c7c3c6aSMatthew Dillon 	int i;
14861c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
14871c7c3c6aSMatthew Dillon 
14881c7c3c6aSMatthew Dillon 	/*
14891c7c3c6aSMatthew Dillon 	 * report error
14901c7c3c6aSMatthew Dillon 	 */
1491c244d2deSPoul-Henning Kamp 	if (bp->b_ioflags & BIO_ERROR) {
14921c7c3c6aSMatthew Dillon 		printf(
14931c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
14941c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
149521144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
14961c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
14971c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
14981c7c3c6aSMatthew Dillon 		    bp->b_error
14991c7c3c6aSMatthew Dillon 		);
15001c7c3c6aSMatthew Dillon 	}
15011c7c3c6aSMatthew Dillon 
15021c7c3c6aSMatthew Dillon 	/*
150326f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
150426f9a767SRodney W. Grimes 	 */
15052cc718a1SKonstantin Belousov 	if ((bp->b_flags & B_UNMAPPED) != 0) {
15062cc718a1SKonstantin Belousov 		bp->b_data = bp->b_kvaalloc;
15072cc718a1SKonstantin Belousov 		bp->b_kvabase = bp->b_kvaalloc;
15082cc718a1SKonstantin Belousov 		bp->b_flags &= ~B_UNMAPPED;
15092cc718a1SKonstantin Belousov 	} else
15101c7c3c6aSMatthew Dillon 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
151126f9a767SRodney W. Grimes 
151233a609ecSAlan Cox 	if (bp->b_npages) {
151333a609ecSAlan Cox 		object = bp->b_pages[0]->object;
151489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
151533a609ecSAlan Cox 	}
15162965a453SKip Macy 
151726f9a767SRodney W. Grimes 	/*
15181c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
15191c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
15201c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
15211c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
15221c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
15231c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
152426f9a767SRodney W. Grimes 	 */
15251c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
15261c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1527e47ed70bSJohn Dyson 
15285786be7cSAlan Cox 		m->oflags &= ~VPO_SWAPINPROG;
1529c7aebda8SAttilio Rao 		if (m->oflags & VPO_SWAPSLEEP) {
1530c7aebda8SAttilio Rao 			m->oflags &= ~VPO_SWAPSLEEP;
1531c7aebda8SAttilio Rao 			wakeup(&object->paging_in_progress);
1532c7aebda8SAttilio Rao 		}
1533e47ed70bSJohn Dyson 
1534c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1535ffc82b0aSJohn Dyson 			/*
15361c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
15371c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
15381c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
15391c7c3c6aSMatthew Dillon 			 * interrupt.
1540ffc82b0aSJohn Dyson 			 */
154121144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
15421c7c3c6aSMatthew Dillon 				/*
15431c7c3c6aSMatthew Dillon 				 * When reading, reqpage needs to stay
15441c7c3c6aSMatthew Dillon 				 * locked for the parent, but all other
15451c7c3c6aSMatthew Dillon 				 * pages can be freed.  We still want to
15461c7c3c6aSMatthew Dillon 				 * wakeup the parent waiting on the page,
15471c7c3c6aSMatthew Dillon 				 * though.  ( also: pg_reqpage can be -1 and
15481c7c3c6aSMatthew Dillon 				 * not match anything ).
15491c7c3c6aSMatthew Dillon 				 *
15501c7c3c6aSMatthew Dillon 				 * We have to wake specifically requested pages
15515786be7cSAlan Cox 				 * up too because we cleared VPO_SWAPINPROG and
15521c7c3c6aSMatthew Dillon 				 * someone may be waiting for that.
15531c7c3c6aSMatthew Dillon 				 *
15541c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1555956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15561c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15571c7c3c6aSMatthew Dillon 				 */
15581c7c3c6aSMatthew Dillon 				m->valid = 0;
15591c7c3c6aSMatthew Dillon 				if (i != bp->b_pager.pg_reqpage)
1560e86a87e9SKonstantin Belousov 					swp_pager_free_nrpage(m);
1561c7aebda8SAttilio Rao 				else {
1562c7aebda8SAttilio Rao 					vm_page_lock(m);
15631c7c3c6aSMatthew Dillon 					vm_page_flash(m);
1564c7aebda8SAttilio Rao 					vm_page_unlock(m);
1565c7aebda8SAttilio Rao 				}
15661c7c3c6aSMatthew Dillon 				/*
15671c7c3c6aSMatthew Dillon 				 * If i == bp->b_pager.pg_reqpage, do not wake
15681c7c3c6aSMatthew Dillon 				 * the page up.  The caller needs to.
15691c7c3c6aSMatthew Dillon 				 */
15701c7c3c6aSMatthew Dillon 			} else {
15711c7c3c6aSMatthew Dillon 				/*
15721c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
15731c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
15741c7c3c6aSMatthew Dillon 				 * then finish the I/O.
15751c7c3c6aSMatthew Dillon 				 */
15767dbf82dcSMatthew Dillon 				vm_page_dirty(m);
15773c4a2440SAlan Cox 				vm_page_lock(m);
15781c7c3c6aSMatthew Dillon 				vm_page_activate(m);
15793c4a2440SAlan Cox 				vm_page_unlock(m);
1580c7aebda8SAttilio Rao 				vm_page_sunbusy(m);
15811c7c3c6aSMatthew Dillon 			}
158221144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
15831c7c3c6aSMatthew Dillon 			/*
15841c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1585956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
15861c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
15871c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
15881c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
15891c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
15901c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
15911c7c3c6aSMatthew Dillon 			 *
15921c7c3c6aSMatthew Dillon 			 * If not the requested page then deactivate it.
15931c7c3c6aSMatthew Dillon 			 *
15941c7c3c6aSMatthew Dillon 			 * Note that the requested page, reqpage, is left
15951c7c3c6aSMatthew Dillon 			 * busied, but we still have to wake it up.  The
15961c7c3c6aSMatthew Dillon 			 * other pages are released (unbusied) by
1597c7aebda8SAttilio Rao 			 * vm_page_xunbusy().
15981c7c3c6aSMatthew Dillon 			 */
1599016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1600016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is mapped", m));
16011c7c3c6aSMatthew Dillon 			m->valid = VM_PAGE_BITS_ALL;
1602016a3c93SAlan Cox 			KASSERT(m->dirty == 0,
1603016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is dirty", m));
16041c7c3c6aSMatthew Dillon 
16051c7c3c6aSMatthew Dillon 			/*
16061c7c3c6aSMatthew Dillon 			 * We have to wake specifically requested pages
16075786be7cSAlan Cox 			 * up too because we cleared VPO_SWAPINPROG and
16081c7c3c6aSMatthew Dillon 			 * could be waiting for it in getpages.  However,
16091c7c3c6aSMatthew Dillon 			 * be sure to not unbusy getpages specifically
16101c7c3c6aSMatthew Dillon 			 * requested page - getpages expects it to be
16111c7c3c6aSMatthew Dillon 			 * left busy.
16121c7c3c6aSMatthew Dillon 			 */
16131c7c3c6aSMatthew Dillon 			if (i != bp->b_pager.pg_reqpage) {
16143c4a2440SAlan Cox 				vm_page_lock(m);
16151c7c3c6aSMatthew Dillon 				vm_page_deactivate(m);
16163c4a2440SAlan Cox 				vm_page_unlock(m);
1617c7aebda8SAttilio Rao 				vm_page_xunbusy(m);
1618c7aebda8SAttilio Rao 			} else {
1619c7aebda8SAttilio Rao 				vm_page_lock(m);
16201c7c3c6aSMatthew Dillon 				vm_page_flash(m);
1621c7aebda8SAttilio Rao 				vm_page_unlock(m);
1622c7aebda8SAttilio Rao 			}
16231c7c3c6aSMatthew Dillon 		} else {
16241c7c3c6aSMatthew Dillon 			/*
1625016a3c93SAlan Cox 			 * For write success, clear the dirty
16261c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
16271c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
16281c7c3c6aSMatthew Dillon 			 */
16296031c68dSAlan Cox 			KASSERT(!pmap_page_is_write_mapped(m),
1630016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is not write"
1631016a3c93SAlan Cox 			    " protected", m));
1632c52e7044SAlan Cox 			vm_page_undirty(m);
1633c7aebda8SAttilio Rao 			vm_page_sunbusy(m);
16343c4a2440SAlan Cox 			if (vm_page_count_severe()) {
16353c4a2440SAlan Cox 				vm_page_lock(m);
16362c840b1fSAlan Cox 				vm_page_try_to_cache(m);
16372965a453SKip Macy 				vm_page_unlock(m);
16382965a453SKip Macy 			}
16393c4a2440SAlan Cox 		}
16403c4a2440SAlan Cox 	}
164126f9a767SRodney W. Grimes 
16421c7c3c6aSMatthew Dillon 	/*
16431c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
16441c7c3c6aSMatthew Dillon 	 * pip refs on the object.
16451c7c3c6aSMatthew Dillon 	 */
16460d420ad3SAlan Cox 	if (object != NULL) {
16471c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
164889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
16490d420ad3SAlan Cox 	}
165026f9a767SRodney W. Grimes 
16511c7c3c6aSMatthew Dillon 	/*
1652100650deSOlivier Houchard 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1653100650deSOlivier Houchard 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1654100650deSOlivier Houchard 	 * trigger a KASSERT in relpbuf().
1655100650deSOlivier Houchard 	 */
1656100650deSOlivier Houchard 	if (bp->b_vp) {
1657100650deSOlivier Houchard 		    bp->b_vp = NULL;
1658100650deSOlivier Houchard 		    bp->b_bufobj = NULL;
1659100650deSOlivier Houchard 	}
1660100650deSOlivier Houchard 	/*
16611c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
16621c7c3c6aSMatthew Dillon 	 */
1663327f4e83SMatthew Dillon 	relpbuf(
1664327f4e83SMatthew Dillon 	    bp,
166521144e3bSPoul-Henning Kamp 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1666327f4e83SMatthew Dillon 		((bp->b_flags & B_ASYNC) ?
1667327f4e83SMatthew Dillon 		    &nsw_wcount_async :
1668327f4e83SMatthew Dillon 		    &nsw_wcount_sync
1669327f4e83SMatthew Dillon 		)
1670327f4e83SMatthew Dillon 	    )
1671327f4e83SMatthew Dillon 	);
167226f9a767SRodney W. Grimes }
16731c7c3c6aSMatthew Dillon 
167492da00bbSMatthew Dillon /*
167592da00bbSMatthew Dillon  *	swap_pager_isswapped:
167692da00bbSMatthew Dillon  *
167792da00bbSMatthew Dillon  *	Return 1 if at least one page in the given object is paged
167892da00bbSMatthew Dillon  *	out to the given swap device.
167992da00bbSMatthew Dillon  *
168015523cf7SKonstantin Belousov  *	This routine may not sleep.
168192da00bbSMatthew Dillon  */
16828f60c087SPoul-Henning Kamp int
16838f60c087SPoul-Henning Kamp swap_pager_isswapped(vm_object_t object, struct swdevt *sp)
16848f60c087SPoul-Henning Kamp {
168592da00bbSMatthew Dillon 	daddr_t index = 0;
168692da00bbSMatthew Dillon 	int bcount;
168792da00bbSMatthew Dillon 	int i;
168892da00bbSMatthew Dillon 
168989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
1690f6bcadc4SDavid Schultz 	if (object->type != OBJT_SWAP)
1691f6bcadc4SDavid Schultz 		return (0);
1692f6bcadc4SDavid Schultz 
1693b3fed13eSDavid Schultz 	mtx_lock(&swhash_mtx);
169492da00bbSMatthew Dillon 	for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) {
169592da00bbSMatthew Dillon 		struct swblock *swap;
169692da00bbSMatthew Dillon 
169792da00bbSMatthew Dillon 		if ((swap = *swp_pager_hash(object, index)) != NULL) {
169892da00bbSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
1699b3fed13eSDavid Schultz 				if (swp_pager_isondev(swap->swb_pages[i], sp)) {
17007827d9b0SAlan Cox 					mtx_unlock(&swhash_mtx);
1701b3fed13eSDavid Schultz 					return (1);
170292da00bbSMatthew Dillon 				}
170392da00bbSMatthew Dillon 			}
17047827d9b0SAlan Cox 		}
170592da00bbSMatthew Dillon 		index += SWAP_META_PAGES;
170692da00bbSMatthew Dillon 	}
1707b3fed13eSDavid Schultz 	mtx_unlock(&swhash_mtx);
1708b3fed13eSDavid Schultz 	return (0);
170992da00bbSMatthew Dillon }
171092da00bbSMatthew Dillon 
171192da00bbSMatthew Dillon /*
171292da00bbSMatthew Dillon  * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in
171392da00bbSMatthew Dillon  *
171492da00bbSMatthew Dillon  *	This routine dissociates the page at the given index within a
171592da00bbSMatthew Dillon  *	swap block from its backing store, paging it in if necessary.
171692da00bbSMatthew Dillon  *	If the page is paged in, it is placed in the inactive queue,
171792da00bbSMatthew Dillon  *	since it had its backing store ripped out from under it.
171892da00bbSMatthew Dillon  *	We also attempt to swap in all other pages in the swap block,
171992da00bbSMatthew Dillon  *	we only guarantee that the one at the specified index is
172092da00bbSMatthew Dillon  *	paged in.
172192da00bbSMatthew Dillon  *
172292da00bbSMatthew Dillon  *	XXX - The code to page the whole block in doesn't work, so we
172392da00bbSMatthew Dillon  *	      revert to the one-by-one behavior for now.  Sigh.
172492da00bbSMatthew Dillon  */
172562a59e8fSWarner Losh static inline void
1726b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex)
172792da00bbSMatthew Dillon {
172892da00bbSMatthew Dillon 	vm_page_t m;
172992da00bbSMatthew Dillon 
173092da00bbSMatthew Dillon 	vm_object_pip_add(object, 1);
17315944de8eSKonstantin Belousov 	m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
173292da00bbSMatthew Dillon 	if (m->valid == VM_PAGE_BITS_ALL) {
17330d8243ccSAttilio Rao 		vm_object_pip_wakeup(object);
173492da00bbSMatthew Dillon 		vm_page_dirty(m);
1735d061cdd5SAlan Cox 		vm_page_lock(m);
1736d061cdd5SAlan Cox 		vm_page_activate(m);
17372965a453SKip Macy 		vm_page_unlock(m);
1738c7aebda8SAttilio Rao 		vm_page_xunbusy(m);
173992da00bbSMatthew Dillon 		vm_pager_page_unswapped(m);
174092da00bbSMatthew Dillon 		return;
174192da00bbSMatthew Dillon 	}
174292da00bbSMatthew Dillon 
1743b3fed13eSDavid Schultz 	if (swap_pager_getpages(object, &m, 1, 0) != VM_PAGER_OK)
174492da00bbSMatthew Dillon 		panic("swap_pager_force_pagein: read from swap failed");/*XXX*/
17450d8243ccSAttilio Rao 	vm_object_pip_wakeup(object);
174692da00bbSMatthew Dillon 	vm_page_dirty(m);
1747db1f085eSAlan Cox 	vm_page_lock(m);
1748db1f085eSAlan Cox 	vm_page_deactivate(m);
17492965a453SKip Macy 	vm_page_unlock(m);
1750c7aebda8SAttilio Rao 	vm_page_xunbusy(m);
175192da00bbSMatthew Dillon 	vm_pager_page_unswapped(m);
175292da00bbSMatthew Dillon }
175392da00bbSMatthew Dillon 
175492da00bbSMatthew Dillon /*
175592da00bbSMatthew Dillon  *	swap_pager_swapoff:
175692da00bbSMatthew Dillon  *
175792da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
175892da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
175992da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
176092da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
176192da00bbSMatthew Dillon  *
176292da00bbSMatthew Dillon  *	This routine may block.
176392da00bbSMatthew Dillon  */
1764e9c0cc15SPoul-Henning Kamp static void
1765b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp)
176692da00bbSMatthew Dillon {
176792da00bbSMatthew Dillon 	struct swblock *swap;
17688bc61209SDavid Schultz 	int i, j, retries;
176992da00bbSMatthew Dillon 
177092da00bbSMatthew Dillon 	GIANT_REQUIRED;
177192da00bbSMatthew Dillon 
17728bc61209SDavid Schultz 	retries = 0;
177392da00bbSMatthew Dillon full_rescan:
1774b3fed13eSDavid Schultz 	mtx_lock(&swhash_mtx);
177592da00bbSMatthew Dillon 	for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */
177692da00bbSMatthew Dillon restart:
1777b3fed13eSDavid Schultz 		for (swap = swhash[i]; swap != NULL; swap = swap->swb_hnext) {
1778b3fed13eSDavid Schultz 			vm_object_t object = swap->swb_object;
1779b3fed13eSDavid Schultz 			vm_pindex_t pindex = swap->swb_index;
178092da00bbSMatthew Dillon 			for (j = 0; j < SWAP_META_PAGES; ++j) {
1781b3fed13eSDavid Schultz 				if (swp_pager_isondev(swap->swb_pages[j], sp)) {
1782b3fed13eSDavid Schultz 					/* avoid deadlock */
178389f6b863SAttilio Rao 					if (!VM_OBJECT_TRYWLOCK(object)) {
178492da00bbSMatthew Dillon 						break;
1785b3fed13eSDavid Schultz 					} else {
1786b3fed13eSDavid Schultz 						mtx_unlock(&swhash_mtx);
1787b3fed13eSDavid Schultz 						swp_pager_force_pagein(object,
1788b3fed13eSDavid Schultz 						    pindex + j);
178989f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(object);
1790b3fed13eSDavid Schultz 						mtx_lock(&swhash_mtx);
179192da00bbSMatthew Dillon 						goto restart;
179292da00bbSMatthew Dillon 					}
1793b3fed13eSDavid Schultz 				}
1794b3fed13eSDavid Schultz 			}
1795b3fed13eSDavid Schultz 		}
179692da00bbSMatthew Dillon 	}
1797d536c58fSAlan Cox 	mtx_unlock(&swhash_mtx);
17988bc61209SDavid Schultz 	if (sp->sw_used) {
179992da00bbSMatthew Dillon 		/*
18008bc61209SDavid Schultz 		 * Objects may be locked or paging to the device being
18018bc61209SDavid Schultz 		 * removed, so we will miss their pages and need to
18028bc61209SDavid Schultz 		 * make another pass.  We have marked this device as
18038bc61209SDavid Schultz 		 * SW_CLOSING, so the activity should finish soon.
180492da00bbSMatthew Dillon 		 */
18058bc61209SDavid Schultz 		retries++;
18068bc61209SDavid Schultz 		if (retries > 100) {
18078bc61209SDavid Schultz 			panic("swapoff: failed to locate %d swap blocks",
18088bc61209SDavid Schultz 			    sp->sw_used);
18098bc61209SDavid Schultz 		}
18104d70511aSJohn Baldwin 		pause("swpoff", hz / 20);
181192da00bbSMatthew Dillon 		goto full_rescan;
181292da00bbSMatthew Dillon 	}
181392da00bbSMatthew Dillon }
181492da00bbSMatthew Dillon 
18151c7c3c6aSMatthew Dillon /************************************************************************
18161c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
18171c7c3c6aSMatthew Dillon  ************************************************************************
18181c7c3c6aSMatthew Dillon  *
18191c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
1820cec9f109SDavid E. O'Brien  *	OBJT_SWAP object.
18211c7c3c6aSMatthew Dillon  *
18224dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
18234dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
18244dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
18251c7c3c6aSMatthew Dillon  */
18261c7c3c6aSMatthew Dillon 
18271c7c3c6aSMatthew Dillon /*
18281c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
18291c7c3c6aSMatthew Dillon  *
18301c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
18311c7c3c6aSMatthew Dillon  *	object.
18321c7c3c6aSMatthew Dillon  *
18331c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
18341c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
18351c7c3c6aSMatthew Dillon  *	assigned swapblk is freed.
18361c7c3c6aSMatthew Dillon  */
18371c7c3c6aSMatthew Dillon static void
18382f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
18392f249180SPoul-Henning Kamp {
18403ff863f1SDag-Erling Smørgrav 	static volatile int exhausted;
18411c7c3c6aSMatthew Dillon 	struct swblock *swap;
18421c7c3c6aSMatthew Dillon 	struct swblock **pswap;
184323f09d50SIan Dowse 	int idx;
18441c7c3c6aSMatthew Dillon 
184589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
18461c7c3c6aSMatthew Dillon 	/*
18471c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
18481c7c3c6aSMatthew Dillon 	 */
18491c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
18501c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
18511c7c3c6aSMatthew Dillon 		object->un_pager.swp.swp_bcount = 0;
18521c7c3c6aSMatthew Dillon 
18531c7c3c6aSMatthew Dillon 		if (object->handle != NULL) {
1854bd228075SAlan Cox 			mtx_lock(&sw_alloc_mtx);
18551c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
18561c7c3c6aSMatthew Dillon 			    NOBJLIST(object->handle),
18571c7c3c6aSMatthew Dillon 			    object,
18581c7c3c6aSMatthew Dillon 			    pager_object_list
18591c7c3c6aSMatthew Dillon 			);
1860a9fa2c05SAlfred Perlstein 			mtx_unlock(&sw_alloc_mtx);
18611c7c3c6aSMatthew Dillon 		}
1862bd228075SAlan Cox 	}
18631c7c3c6aSMatthew Dillon 
18641c7c3c6aSMatthew Dillon 	/*
18651c7c3c6aSMatthew Dillon 	 * Locate hash entry.  If not found create, but if we aren't adding
18664dcc5c2dSMatthew Dillon 	 * anything just return.  If we run out of space in the map we wait
18674dcc5c2dSMatthew Dillon 	 * and, since the hash table may have changed, retry.
18681c7c3c6aSMatthew Dillon 	 */
18694dcc5c2dSMatthew Dillon retry:
18707827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
187123f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
18721c7c3c6aSMatthew Dillon 
18731c7c3c6aSMatthew Dillon 	if ((swap = *pswap) == NULL) {
18741c7c3c6aSMatthew Dillon 		int i;
18751c7c3c6aSMatthew Dillon 
18761c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
18777827d9b0SAlan Cox 			goto done;
18781c7c3c6aSMatthew Dillon 
18795a3c920fSKonstantin Belousov 		swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT |
18805a3c920fSKonstantin Belousov 		    (curproc == pageproc ? M_USE_RESERVE : 0));
18814dcc5c2dSMatthew Dillon 		if (swap == NULL) {
18827827d9b0SAlan Cox 			mtx_unlock(&swhash_mtx);
188389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
18842025d69bSKonstantin Belousov 			if (uma_zone_exhausted(swap_zone)) {
1885dc1b35b5SDag-Erling Smørgrav 				if (atomic_cmpset_int(&exhausted, 0, 1))
18863ff863f1SDag-Erling Smørgrav 					printf("swap zone exhausted, "
18873ff863f1SDag-Erling Smørgrav 					    "increase kern.maxswzone\n");
18882025d69bSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
18892025d69bSKonstantin Belousov 				pause("swzonex", 10);
18902025d69bSKonstantin Belousov 			} else
18914dcc5c2dSMatthew Dillon 				VM_WAIT;
189289f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
18934dcc5c2dSMatthew Dillon 			goto retry;
18944dcc5c2dSMatthew Dillon 		}
1895670d17b5SJeff Roberson 
1896dc1b35b5SDag-Erling Smørgrav 		if (atomic_cmpset_int(&exhausted, 1, 0))
18973ff863f1SDag-Erling Smørgrav 			printf("swap zone ok\n");
18983ff863f1SDag-Erling Smørgrav 
18991c7c3c6aSMatthew Dillon 		swap->swb_hnext = NULL;
19001c7c3c6aSMatthew Dillon 		swap->swb_object = object;
190123f09d50SIan Dowse 		swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK;
19021c7c3c6aSMatthew Dillon 		swap->swb_count = 0;
19031c7c3c6aSMatthew Dillon 
19041c7c3c6aSMatthew Dillon 		++object->un_pager.swp.swp_bcount;
19051c7c3c6aSMatthew Dillon 
19061c7c3c6aSMatthew Dillon 		for (i = 0; i < SWAP_META_PAGES; ++i)
19071c7c3c6aSMatthew Dillon 			swap->swb_pages[i] = SWAPBLK_NONE;
19081c7c3c6aSMatthew Dillon 	}
19091c7c3c6aSMatthew Dillon 
19101c7c3c6aSMatthew Dillon 	/*
19111c7c3c6aSMatthew Dillon 	 * Delete prior contents of metadata
19121c7c3c6aSMatthew Dillon 	 */
191323f09d50SIan Dowse 	idx = pindex & SWAP_META_MASK;
19141c7c3c6aSMatthew Dillon 
191523f09d50SIan Dowse 	if (swap->swb_pages[idx] != SWAPBLK_NONE) {
191623f09d50SIan Dowse 		swp_pager_freeswapspace(swap->swb_pages[idx], 1);
19171c7c3c6aSMatthew Dillon 		--swap->swb_count;
19181c7c3c6aSMatthew Dillon 	}
19191c7c3c6aSMatthew Dillon 
19201c7c3c6aSMatthew Dillon 	/*
19211c7c3c6aSMatthew Dillon 	 * Enter block into metadata
19221c7c3c6aSMatthew Dillon 	 */
192323f09d50SIan Dowse 	swap->swb_pages[idx] = swapblk;
19244dcc5c2dSMatthew Dillon 	if (swapblk != SWAPBLK_NONE)
19251c7c3c6aSMatthew Dillon 		++swap->swb_count;
19267827d9b0SAlan Cox done:
19277827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
19281c7c3c6aSMatthew Dillon }
19291c7c3c6aSMatthew Dillon 
19301c7c3c6aSMatthew Dillon /*
19311c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
19321c7c3c6aSMatthew Dillon  *
19331c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
19341c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
19351c7c3c6aSMatthew Dillon  *
19361c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
19371c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
19381c7c3c6aSMatthew Dillon  *	with resident pages.
19391c7c3c6aSMatthew Dillon  */
19401c7c3c6aSMatthew Dillon static void
19414dcc5c2dSMatthew Dillon swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count)
19421c7c3c6aSMatthew Dillon {
19432928cef7SAlan Cox 
1944c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
19451c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19461c7c3c6aSMatthew Dillon 		return;
19471c7c3c6aSMatthew Dillon 
19481c7c3c6aSMatthew Dillon 	while (count > 0) {
19491c7c3c6aSMatthew Dillon 		struct swblock **pswap;
19501c7c3c6aSMatthew Dillon 		struct swblock *swap;
19511c7c3c6aSMatthew Dillon 
19527827d9b0SAlan Cox 		mtx_lock(&swhash_mtx);
19531c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
19541c7c3c6aSMatthew Dillon 
19551c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
19561c7c3c6aSMatthew Dillon 			daddr_t v = swap->swb_pages[index & SWAP_META_MASK];
19571c7c3c6aSMatthew Dillon 
19581c7c3c6aSMatthew Dillon 			if (v != SWAPBLK_NONE) {
19591c7c3c6aSMatthew Dillon 				swp_pager_freeswapspace(v, 1);
19601c7c3c6aSMatthew Dillon 				swap->swb_pages[index & SWAP_META_MASK] =
19611c7c3c6aSMatthew Dillon 					SWAPBLK_NONE;
19621c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
19631c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
1964670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
19651c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
19661c7c3c6aSMatthew Dillon 				}
19671c7c3c6aSMatthew Dillon 			}
19681c7c3c6aSMatthew Dillon 			--count;
19691c7c3c6aSMatthew Dillon 			++index;
19701c7c3c6aSMatthew Dillon 		} else {
19714dcc5c2dSMatthew Dillon 			int n = SWAP_META_PAGES - (index & SWAP_META_MASK);
19721c7c3c6aSMatthew Dillon 			count -= n;
19731c7c3c6aSMatthew Dillon 			index += n;
19741c7c3c6aSMatthew Dillon 		}
19757827d9b0SAlan Cox 		mtx_unlock(&swhash_mtx);
19761c7c3c6aSMatthew Dillon 	}
19771c7c3c6aSMatthew Dillon }
19781c7c3c6aSMatthew Dillon 
19791c7c3c6aSMatthew Dillon /*
19801c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
19811c7c3c6aSMatthew Dillon  *
19821c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
19831c7c3c6aSMatthew Dillon  *	an object.
19841c7c3c6aSMatthew Dillon  */
19851c7c3c6aSMatthew Dillon static void
19861c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
19871c7c3c6aSMatthew Dillon {
19881c7c3c6aSMatthew Dillon 	daddr_t index = 0;
19891c7c3c6aSMatthew Dillon 
199089f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
19911c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19921c7c3c6aSMatthew Dillon 		return;
19931c7c3c6aSMatthew Dillon 
19941c7c3c6aSMatthew Dillon 	while (object->un_pager.swp.swp_bcount) {
19951c7c3c6aSMatthew Dillon 		struct swblock **pswap;
19961c7c3c6aSMatthew Dillon 		struct swblock *swap;
19971c7c3c6aSMatthew Dillon 
19987827d9b0SAlan Cox 		mtx_lock(&swhash_mtx);
19991c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
20001c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
20011c7c3c6aSMatthew Dillon 			int i;
20021c7c3c6aSMatthew Dillon 
20031c7c3c6aSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
20041c7c3c6aSMatthew Dillon 				daddr_t v = swap->swb_pages[i];
20051c7c3c6aSMatthew Dillon 				if (v != SWAPBLK_NONE) {
20061c7c3c6aSMatthew Dillon 					--swap->swb_count;
20074dcc5c2dSMatthew Dillon 					swp_pager_freeswapspace(v, 1);
20081c7c3c6aSMatthew Dillon 				}
20091c7c3c6aSMatthew Dillon 			}
20101c7c3c6aSMatthew Dillon 			if (swap->swb_count != 0)
20111c7c3c6aSMatthew Dillon 				panic("swap_pager_meta_free_all: swb_count != 0");
20121c7c3c6aSMatthew Dillon 			*pswap = swap->swb_hnext;
2013670d17b5SJeff Roberson 			uma_zfree(swap_zone, swap);
20141c7c3c6aSMatthew Dillon 			--object->un_pager.swp.swp_bcount;
20151c7c3c6aSMatthew Dillon 		}
20167827d9b0SAlan Cox 		mtx_unlock(&swhash_mtx);
20171c7c3c6aSMatthew Dillon 		index += SWAP_META_PAGES;
20181c7c3c6aSMatthew Dillon 	}
20191c7c3c6aSMatthew Dillon }
20201c7c3c6aSMatthew Dillon 
20211c7c3c6aSMatthew Dillon /*
20221c7c3c6aSMatthew Dillon  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
20231c7c3c6aSMatthew Dillon  *
20241c7c3c6aSMatthew Dillon  *	This routine is capable of looking up, popping, or freeing
20251c7c3c6aSMatthew Dillon  *	swapblk assignments in the swap meta data or in the vm_page_t.
20261c7c3c6aSMatthew Dillon  *	The routine typically returns the swapblk being looked-up, or popped,
20271c7c3c6aSMatthew Dillon  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
20281c7c3c6aSMatthew Dillon  *	was invalid.  This routine will automatically free any invalid
20291c7c3c6aSMatthew Dillon  *	meta-data swapblks.
20301c7c3c6aSMatthew Dillon  *
20311c7c3c6aSMatthew Dillon  *	It is not possible to store invalid swapblks in the swap meta data
20321c7c3c6aSMatthew Dillon  *	(other then a literal 'SWAPBLK_NONE'), so we don't bother checking.
20331c7c3c6aSMatthew Dillon  *
20341c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
20351c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
20361c7c3c6aSMatthew Dillon  *	busy page.
20371c7c3c6aSMatthew Dillon  *
20384dcc5c2dSMatthew Dillon  *	SWM_FREE	remove and free swap block from metadata
20391c7c3c6aSMatthew Dillon  *	SWM_POP		remove from meta data but do not free.. pop it out
20401c7c3c6aSMatthew Dillon  */
20411c7c3c6aSMatthew Dillon static daddr_t
20422f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags)
20432f249180SPoul-Henning Kamp {
20444dcc5c2dSMatthew Dillon 	struct swblock **pswap;
20454dcc5c2dSMatthew Dillon 	struct swblock *swap;
20464dcc5c2dSMatthew Dillon 	daddr_t r1;
204723f09d50SIan Dowse 	int idx;
20484dcc5c2dSMatthew Dillon 
2049c25673ffSAttilio Rao 	VM_OBJECT_ASSERT_LOCKED(object);
20501c7c3c6aSMatthew Dillon 	/*
20511c7c3c6aSMatthew Dillon 	 * The meta data only exists of the object is OBJT_SWAP
20521c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
20531c7c3c6aSMatthew Dillon 	 */
20544dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
20551c7c3c6aSMatthew Dillon 		return (SWAPBLK_NONE);
20561c7c3c6aSMatthew Dillon 
20574dcc5c2dSMatthew Dillon 	r1 = SWAPBLK_NONE;
20587827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
205923f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
20601c7c3c6aSMatthew Dillon 
20611c7c3c6aSMatthew Dillon 	if ((swap = *pswap) != NULL) {
206223f09d50SIan Dowse 		idx = pindex & SWAP_META_MASK;
206323f09d50SIan Dowse 		r1 = swap->swb_pages[idx];
20641c7c3c6aSMatthew Dillon 
20651c7c3c6aSMatthew Dillon 		if (r1 != SWAPBLK_NONE) {
20661c7c3c6aSMatthew Dillon 			if (flags & SWM_FREE) {
20674dcc5c2dSMatthew Dillon 				swp_pager_freeswapspace(r1, 1);
20681c7c3c6aSMatthew Dillon 				r1 = SWAPBLK_NONE;
20691c7c3c6aSMatthew Dillon 			}
20701c7c3c6aSMatthew Dillon 			if (flags & (SWM_FREE|SWM_POP)) {
207123f09d50SIan Dowse 				swap->swb_pages[idx] = SWAPBLK_NONE;
20721c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
20731c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
2074670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
20751c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
20761c7c3c6aSMatthew Dillon 				}
20771c7c3c6aSMatthew Dillon 			}
20781c7c3c6aSMatthew Dillon 		}
20791c7c3c6aSMatthew Dillon 	}
20807827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
20811c7c3c6aSMatthew Dillon 	return (r1);
20821c7c3c6aSMatthew Dillon }
20831c7c3c6aSMatthew Dillon 
2084e9c0cc15SPoul-Henning Kamp /*
2085e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2086e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2087e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2088e9c0cc15SPoul-Henning Kamp  */
2089e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2090e9c0cc15SPoul-Henning Kamp struct swapon_args {
2091e9c0cc15SPoul-Henning Kamp 	char *name;
2092e9c0cc15SPoul-Henning Kamp };
2093e9c0cc15SPoul-Henning Kamp #endif
2094e9c0cc15SPoul-Henning Kamp 
2095e9c0cc15SPoul-Henning Kamp /*
2096e9c0cc15SPoul-Henning Kamp  * MPSAFE
2097e9c0cc15SPoul-Henning Kamp  */
2098e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2099e9c0cc15SPoul-Henning Kamp int
21008451d0ddSKip Macy sys_swapon(struct thread *td, struct swapon_args *uap)
2101e9c0cc15SPoul-Henning Kamp {
2102e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2103e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2104e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2105e9c0cc15SPoul-Henning Kamp 	int error;
2106e9c0cc15SPoul-Henning Kamp 
2107acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPON);
2108e9c0cc15SPoul-Henning Kamp 	if (error)
2109acd3428bSRobert Watson 		return (error);
2110e9c0cc15SPoul-Henning Kamp 
2111acd3428bSRobert Watson 	mtx_lock(&Giant);
2112e9c0cc15SPoul-Henning Kamp 	while (swdev_syscall_active)
2113e9c0cc15SPoul-Henning Kamp 	    tsleep(&swdev_syscall_active, PUSER - 1, "swpon", 0);
2114e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 1;
2115e9c0cc15SPoul-Henning Kamp 
2116e9c0cc15SPoul-Henning Kamp 	/*
2117e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2118e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2119e9c0cc15SPoul-Henning Kamp 	 */
2120e9c0cc15SPoul-Henning Kamp 	if (swap_zone == NULL) {
2121e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2122e9c0cc15SPoul-Henning Kamp 		goto done;
2123e9c0cc15SPoul-Henning Kamp 	}
2124e9c0cc15SPoul-Henning Kamp 
2125d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2126d9135e72SRobert Watson 	    uap->name, td);
2127e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2128e9c0cc15SPoul-Henning Kamp 	if (error)
2129e9c0cc15SPoul-Henning Kamp 		goto done;
2130e9c0cc15SPoul-Henning Kamp 
2131e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2132e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2133e9c0cc15SPoul-Henning Kamp 
213420da9c2eSPoul-Henning Kamp 	if (vn_isdisk(vp, &error)) {
2135dee34ca4SPoul-Henning Kamp 		error = swapongeom(td, vp);
213620da9c2eSPoul-Henning Kamp 	} else if (vp->v_type == VREG &&
2137e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
21380359a12eSAttilio Rao 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2139e9c0cc15SPoul-Henning Kamp 		/*
2140e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2141e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2142e9c0cc15SPoul-Henning Kamp 		 */
214359efee01SPoul-Henning Kamp 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2144e9c0cc15SPoul-Henning Kamp 	}
2145e9c0cc15SPoul-Henning Kamp 
2146e9c0cc15SPoul-Henning Kamp 	if (error)
2147e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2148e9c0cc15SPoul-Henning Kamp done:
2149e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 0;
2150e9c0cc15SPoul-Henning Kamp 	wakeup_one(&swdev_syscall_active);
2151e9c0cc15SPoul-Henning Kamp 	mtx_unlock(&Giant);
2152e9c0cc15SPoul-Henning Kamp 	return (error);
2153e9c0cc15SPoul-Henning Kamp }
2154e9c0cc15SPoul-Henning Kamp 
21553ff863f1SDag-Erling Smørgrav /*
21563ff863f1SDag-Erling Smørgrav  * Check that the total amount of swap currently configured does not
21573ff863f1SDag-Erling Smørgrav  * exceed half the theoretical maximum.  If it does, print a warning
21583ff863f1SDag-Erling Smørgrav  * message and return -1; otherwise, return 0.
21593ff863f1SDag-Erling Smørgrav  */
21603ff863f1SDag-Erling Smørgrav static int
21613ff863f1SDag-Erling Smørgrav swapon_check_swzone(unsigned long npages)
21623ff863f1SDag-Erling Smørgrav {
21633ff863f1SDag-Erling Smørgrav 	unsigned long maxpages;
21643ff863f1SDag-Erling Smørgrav 
21653ff863f1SDag-Erling Smørgrav 	/* absolute maximum we can handle assuming 100% efficiency */
21663ff863f1SDag-Erling Smørgrav 	maxpages = uma_zone_get_max(swap_zone) * SWAP_META_PAGES;
21673ff863f1SDag-Erling Smørgrav 
21683ff863f1SDag-Erling Smørgrav 	/* recommend using no more than half that amount */
21693ff863f1SDag-Erling Smørgrav 	if (npages > maxpages / 2) {
21703ff863f1SDag-Erling Smørgrav 		printf("warning: total configured swap (%lu pages) "
21713ff863f1SDag-Erling Smørgrav 		    "exceeds maximum recommended amount (%lu pages).\n",
21729462305cSSergey Kandaurov 		    npages, maxpages / 2);
21733ff863f1SDag-Erling Smørgrav 		printf("warning: increase kern.maxswzone "
21743ff863f1SDag-Erling Smørgrav 		    "or reduce amount of swap.\n");
21753ff863f1SDag-Erling Smørgrav 		return (-1);
21763ff863f1SDag-Erling Smørgrav 	}
21773ff863f1SDag-Erling Smørgrav 	return (0);
21783ff863f1SDag-Erling Smørgrav }
21793ff863f1SDag-Erling Smørgrav 
218059efee01SPoul-Henning Kamp static void
21812cc718a1SKonstantin Belousov swaponsomething(struct vnode *vp, void *id, u_long nblks,
21822cc718a1SKonstantin Belousov     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2183e9c0cc15SPoul-Henning Kamp {
21842d9974c1SAlan Cox 	struct swdevt *sp, *tsp;
2185e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
21868f60c087SPoul-Henning Kamp 	u_long mblocks;
2187e9c0cc15SPoul-Henning Kamp 
2188e9c0cc15SPoul-Henning Kamp 	/*
2189e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2190e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2191e9c0cc15SPoul-Henning Kamp 	 *
2192e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2193e9c0cc15SPoul-Henning Kamp 	 */
2194e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2195e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2196e9c0cc15SPoul-Henning Kamp 
21976e903bd0SKonstantin Belousov 	/*
21986e903bd0SKonstantin Belousov 	 * If we go beyond this, we get overflows in the radix
21996e903bd0SKonstantin Belousov 	 * tree bitmap code.
22006e903bd0SKonstantin Belousov 	 */
22016e903bd0SKonstantin Belousov 	mblocks = 0x40000000 / BLIST_META_RADIX;
22026e903bd0SKonstantin Belousov 	if (nblks > mblocks) {
22036e903bd0SKonstantin Belousov 		printf(
22046e903bd0SKonstantin Belousov     "WARNING: reducing swap size to maximum of %luMB per unit\n",
22056e903bd0SKonstantin Belousov 		    mblocks / 1024 / 1024 * PAGE_SIZE);
22066e903bd0SKonstantin Belousov 		nblks = mblocks;
22076e903bd0SKonstantin Belousov 	}
22086e903bd0SKonstantin Belousov 
22098f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2210dee34ca4SPoul-Henning Kamp 	sp->sw_vp = vp;
2211dee34ca4SPoul-Henning Kamp 	sp->sw_id = id;
2212f3732fd1SPoul-Henning Kamp 	sp->sw_dev = dev;
22138d677ef9SPoul-Henning Kamp 	sp->sw_flags = 0;
2214e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2215e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
221659efee01SPoul-Henning Kamp 	sp->sw_strategy = strategy;
2217dee34ca4SPoul-Henning Kamp 	sp->sw_close = close;
22182cc718a1SKonstantin Belousov 	sp->sw_flags = flags;
2219e9c0cc15SPoul-Henning Kamp 
2220c8c7ad92SKip Macy 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2221e9c0cc15SPoul-Henning Kamp 	/*
2222ef3c5abdSPoul-Henning Kamp 	 * Do not free the first two block in order to avoid overwriting
22238f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2224e9c0cc15SPoul-Henning Kamp 	 */
2225ef3c5abdSPoul-Henning Kamp 	blist_free(sp->sw_blist, 2, nblks - 2);
2226e9c0cc15SPoul-Henning Kamp 
22272d9974c1SAlan Cox 	dvbase = 0;
222820da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
22292d9974c1SAlan Cox 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
22302d9974c1SAlan Cox 		if (tsp->sw_end >= dvbase) {
22312d9974c1SAlan Cox 			/*
22322d9974c1SAlan Cox 			 * We put one uncovered page between the devices
22332d9974c1SAlan Cox 			 * in order to definitively prevent any cross-device
22342d9974c1SAlan Cox 			 * I/O requests
22352d9974c1SAlan Cox 			 */
22362d9974c1SAlan Cox 			dvbase = tsp->sw_end + 1;
22372d9974c1SAlan Cox 		}
22382d9974c1SAlan Cox 	}
22392d9974c1SAlan Cox 	sp->sw_first = dvbase;
22402d9974c1SAlan Cox 	sp->sw_end = dvbase + nblks;
22418f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
22428f60c087SPoul-Henning Kamp 	nswapdev++;
22438f60c087SPoul-Henning Kamp 	swap_pager_avail += nblks;
22443364c323SKonstantin Belousov 	swap_total += (vm_ooffset_t)nblks * PAGE_SIZE;
22453ff863f1SDag-Erling Smørgrav 	swapon_check_swzone(swap_total / PAGE_SIZE);
2246d05bc129SAlan Cox 	swp_sizecheck();
2247d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
224859efee01SPoul-Henning Kamp }
2249e9c0cc15SPoul-Henning Kamp 
2250e9c0cc15SPoul-Henning Kamp /*
2251e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2252e9c0cc15SPoul-Henning Kamp  *
2253e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2254dee34ca4SPoul-Henning Kamp  *
2255dee34ca4SPoul-Henning Kamp  * XXX: Badly designed system call: it should use a device index
2256dee34ca4SPoul-Henning Kamp  * rather than filename as specification.  We keep sw_vp around
2257dee34ca4SPoul-Henning Kamp  * only to make this work.
2258e9c0cc15SPoul-Henning Kamp  */
2259e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2260e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2261e9c0cc15SPoul-Henning Kamp 	char *name;
2262e9c0cc15SPoul-Henning Kamp };
2263e9c0cc15SPoul-Henning Kamp #endif
2264e9c0cc15SPoul-Henning Kamp 
2265e9c0cc15SPoul-Henning Kamp /*
2266e9c0cc15SPoul-Henning Kamp  * MPSAFE
2267e9c0cc15SPoul-Henning Kamp  */
2268e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2269e9c0cc15SPoul-Henning Kamp int
22708451d0ddSKip Macy sys_swapoff(struct thread *td, struct swapoff_args *uap)
2271e9c0cc15SPoul-Henning Kamp {
2272e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2273e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2274e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
22758f60c087SPoul-Henning Kamp 	int error;
2276e9c0cc15SPoul-Henning Kamp 
2277acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPOFF);
2278e9c0cc15SPoul-Henning Kamp 	if (error)
22790909f38aSPawel Jakub Dawidek 		return (error);
2280e9c0cc15SPoul-Henning Kamp 
22810909f38aSPawel Jakub Dawidek 	mtx_lock(&Giant);
2282e9c0cc15SPoul-Henning Kamp 	while (swdev_syscall_active)
2283e9c0cc15SPoul-Henning Kamp 	    tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0);
2284e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 1;
2285e9c0cc15SPoul-Henning Kamp 
2286d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2287d9135e72SRobert Watson 	    td);
2288e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2289e9c0cc15SPoul-Henning Kamp 	if (error)
2290e9c0cc15SPoul-Henning Kamp 		goto done;
2291e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2292e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2293e9c0cc15SPoul-Henning Kamp 
229420da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
22958f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2296dee34ca4SPoul-Henning Kamp 		if (sp->sw_vp == vp)
22970909f38aSPawel Jakub Dawidek 			break;
2298e9c0cc15SPoul-Henning Kamp 	}
229920da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
23000909f38aSPawel Jakub Dawidek 	if (sp == NULL) {
2301e9c0cc15SPoul-Henning Kamp 		error = EINVAL;
2302e9c0cc15SPoul-Henning Kamp 		goto done;
23030909f38aSPawel Jakub Dawidek 	}
230435918c55SChristian S.J. Peron 	error = swapoff_one(sp, td->td_ucred);
23050909f38aSPawel Jakub Dawidek done:
23060909f38aSPawel Jakub Dawidek 	swdev_syscall_active = 0;
23070909f38aSPawel Jakub Dawidek 	wakeup_one(&swdev_syscall_active);
23080909f38aSPawel Jakub Dawidek 	mtx_unlock(&Giant);
23090909f38aSPawel Jakub Dawidek 	return (error);
23100909f38aSPawel Jakub Dawidek }
23110909f38aSPawel Jakub Dawidek 
23120909f38aSPawel Jakub Dawidek static int
231335918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred)
23140909f38aSPawel Jakub Dawidek {
23150909f38aSPawel Jakub Dawidek 	u_long nblks, dvbase;
2316e9c0cc15SPoul-Henning Kamp #ifdef MAC
23170909f38aSPawel Jakub Dawidek 	int error;
2318e9c0cc15SPoul-Henning Kamp #endif
2319e9c0cc15SPoul-Henning Kamp 
23200909f38aSPawel Jakub Dawidek 	mtx_assert(&Giant, MA_OWNED);
23210909f38aSPawel Jakub Dawidek #ifdef MAC
2322cb05b60aSAttilio Rao 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
232335918c55SChristian S.J. Peron 	error = mac_system_check_swapoff(cred, sp->sw_vp);
232422db15c0SAttilio Rao 	(void) VOP_UNLOCK(sp->sw_vp, 0);
23250909f38aSPawel Jakub Dawidek 	if (error != 0)
23260909f38aSPawel Jakub Dawidek 		return (error);
23270909f38aSPawel Jakub Dawidek #endif
2328e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2329e9c0cc15SPoul-Henning Kamp 
2330e9c0cc15SPoul-Henning Kamp 	/*
2331e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2332e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2333e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2334e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2335e9c0cc15SPoul-Henning Kamp 	 */
233644f1c916SBryan Drewery 	if (vm_cnt.v_free_count + vm_cnt.v_cache_count + swap_pager_avail <
23372feb50bfSAttilio Rao 	    nblks + nswap_lowat) {
23380909f38aSPawel Jakub Dawidek 		return (ENOMEM);
2339e9c0cc15SPoul-Henning Kamp 	}
2340e9c0cc15SPoul-Henning Kamp 
2341e9c0cc15SPoul-Henning Kamp 	/*
2342e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2343e9c0cc15SPoul-Henning Kamp 	 */
23442928cef7SAlan Cox 	mtx_lock(&sw_dev_mtx);
2345e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
23468f60c087SPoul-Henning Kamp 	for (dvbase = 0; dvbase < sp->sw_end; dvbase += dmmax) {
23478f60c087SPoul-Henning Kamp 		swap_pager_avail -= blist_fill(sp->sw_blist,
23488f60c087SPoul-Henning Kamp 		     dvbase, dmmax);
2349e9c0cc15SPoul-Henning Kamp 	}
23503364c323SKonstantin Belousov 	swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE;
23512928cef7SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2352e9c0cc15SPoul-Henning Kamp 
2353e9c0cc15SPoul-Henning Kamp 	/*
2354e9c0cc15SPoul-Henning Kamp 	 * Page in the contents of the device and close it.
2355e9c0cc15SPoul-Henning Kamp 	 */
2356b3fed13eSDavid Schultz 	swap_pager_swapoff(sp);
2357e9c0cc15SPoul-Henning Kamp 
235835918c55SChristian S.J. Peron 	sp->sw_close(curthread, sp);
235959efee01SPoul-Henning Kamp 	sp->sw_id = NULL;
236020da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23618f60c087SPoul-Henning Kamp 	TAILQ_REMOVE(&swtailq, sp, sw_list);
23620676a140SAlan Cox 	nswapdev--;
23637dea2c2eSAlan Cox 	if (nswapdev == 0) {
23647dea2c2eSAlan Cox 		swap_pager_full = 2;
23657dea2c2eSAlan Cox 		swap_pager_almost_full = 1;
23667dea2c2eSAlan Cox 	}
23678f60c087SPoul-Henning Kamp 	if (swdevhd == sp)
23688f60c087SPoul-Henning Kamp 		swdevhd = NULL;
2369d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
23708f60c087SPoul-Henning Kamp 	blist_destroy(sp->sw_blist);
23718f60c087SPoul-Henning Kamp 	free(sp, M_VMPGDATA);
23720909f38aSPawel Jakub Dawidek 	return (0);
23730909f38aSPawel Jakub Dawidek }
2374e9c0cc15SPoul-Henning Kamp 
23750909f38aSPawel Jakub Dawidek void
23760909f38aSPawel Jakub Dawidek swapoff_all(void)
23770909f38aSPawel Jakub Dawidek {
23780909f38aSPawel Jakub Dawidek 	struct swdevt *sp, *spt;
23790909f38aSPawel Jakub Dawidek 	const char *devname;
23800909f38aSPawel Jakub Dawidek 	int error;
23810909f38aSPawel Jakub Dawidek 
23820909f38aSPawel Jakub Dawidek 	mtx_lock(&Giant);
23830909f38aSPawel Jakub Dawidek 	while (swdev_syscall_active)
23840909f38aSPawel Jakub Dawidek 		tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0);
23850909f38aSPawel Jakub Dawidek 	swdev_syscall_active = 1;
23860909f38aSPawel Jakub Dawidek 
23870909f38aSPawel Jakub Dawidek 	mtx_lock(&sw_dev_mtx);
23880909f38aSPawel Jakub Dawidek 	TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
23890909f38aSPawel Jakub Dawidek 		mtx_unlock(&sw_dev_mtx);
23900909f38aSPawel Jakub Dawidek 		if (vn_isdisk(sp->sw_vp, NULL))
23917870adb6SEd Schouten 			devname = devtoname(sp->sw_vp->v_rdev);
23920909f38aSPawel Jakub Dawidek 		else
23930909f38aSPawel Jakub Dawidek 			devname = "[file]";
239435918c55SChristian S.J. Peron 		error = swapoff_one(sp, thread0.td_ucred);
23950909f38aSPawel Jakub Dawidek 		if (error != 0) {
23960909f38aSPawel Jakub Dawidek 			printf("Cannot remove swap device %s (error=%d), "
23970909f38aSPawel Jakub Dawidek 			    "skipping.\n", devname, error);
23980909f38aSPawel Jakub Dawidek 		} else if (bootverbose) {
23990909f38aSPawel Jakub Dawidek 			printf("Swap device %s removed.\n", devname);
24000909f38aSPawel Jakub Dawidek 		}
24010909f38aSPawel Jakub Dawidek 		mtx_lock(&sw_dev_mtx);
24020909f38aSPawel Jakub Dawidek 	}
24030909f38aSPawel Jakub Dawidek 	mtx_unlock(&sw_dev_mtx);
24040909f38aSPawel Jakub Dawidek 
2405e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 0;
2406e9c0cc15SPoul-Henning Kamp 	wakeup_one(&swdev_syscall_active);
2407e9c0cc15SPoul-Henning Kamp 	mtx_unlock(&Giant);
2408e9c0cc15SPoul-Henning Kamp }
2409e9c0cc15SPoul-Henning Kamp 
2410567104a1SPoul-Henning Kamp void
2411567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used)
2412567104a1SPoul-Henning Kamp {
2413567104a1SPoul-Henning Kamp 	struct swdevt *sp;
2414567104a1SPoul-Henning Kamp 
2415567104a1SPoul-Henning Kamp 	*total = 0;
2416567104a1SPoul-Henning Kamp 	*used = 0;
241720da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
24188f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2419567104a1SPoul-Henning Kamp 		*total += sp->sw_nblks;
2420567104a1SPoul-Henning Kamp 		*used += sp->sw_used;
2421567104a1SPoul-Henning Kamp 	}
242220da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2423567104a1SPoul-Henning Kamp }
2424567104a1SPoul-Henning Kamp 
2425dda4f960SKonstantin Belousov int
2426dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2427dda4f960SKonstantin Belousov {
2428dda4f960SKonstantin Belousov 	struct swdevt *sp;
24297870adb6SEd Schouten 	const char *tmp_devname;
2430dda4f960SKonstantin Belousov 	int error, n;
2431dda4f960SKonstantin Belousov 
2432dda4f960SKonstantin Belousov 	n = 0;
2433dda4f960SKonstantin Belousov 	error = ENOENT;
2434dda4f960SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2435dda4f960SKonstantin Belousov 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2436dda4f960SKonstantin Belousov 		if (n != name) {
2437dda4f960SKonstantin Belousov 			n++;
2438dda4f960SKonstantin Belousov 			continue;
2439dda4f960SKonstantin Belousov 		}
2440dda4f960SKonstantin Belousov 		xs->xsw_version = XSWDEV_VERSION;
2441dda4f960SKonstantin Belousov 		xs->xsw_dev = sp->sw_dev;
2442dda4f960SKonstantin Belousov 		xs->xsw_flags = sp->sw_flags;
2443dda4f960SKonstantin Belousov 		xs->xsw_nblks = sp->sw_nblks;
2444dda4f960SKonstantin Belousov 		xs->xsw_used = sp->sw_used;
2445dda4f960SKonstantin Belousov 		if (devname != NULL) {
2446dda4f960SKonstantin Belousov 			if (vn_isdisk(sp->sw_vp, NULL))
24477870adb6SEd Schouten 				tmp_devname = devtoname(sp->sw_vp->v_rdev);
2448dda4f960SKonstantin Belousov 			else
2449dda4f960SKonstantin Belousov 				tmp_devname = "[file]";
2450dda4f960SKonstantin Belousov 			strncpy(devname, tmp_devname, len);
2451dda4f960SKonstantin Belousov 		}
2452dda4f960SKonstantin Belousov 		error = 0;
2453dda4f960SKonstantin Belousov 		break;
2454dda4f960SKonstantin Belousov 	}
2455dda4f960SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2456dda4f960SKonstantin Belousov 	return (error);
2457dda4f960SKonstantin Belousov }
2458dda4f960SKonstantin Belousov 
2459e9c0cc15SPoul-Henning Kamp static int
2460e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2461e9c0cc15SPoul-Henning Kamp {
2462e9c0cc15SPoul-Henning Kamp 	struct xswdev xs;
2463dda4f960SKonstantin Belousov 	int error;
2464e9c0cc15SPoul-Henning Kamp 
2465e9c0cc15SPoul-Henning Kamp 	if (arg2 != 1)			/* name length */
2466e9c0cc15SPoul-Henning Kamp 		return (EINVAL);
2467dda4f960SKonstantin Belousov 	error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2468dda4f960SKonstantin Belousov 	if (error != 0)
2469dda4f960SKonstantin Belousov 		return (error);
2470e9c0cc15SPoul-Henning Kamp 	error = SYSCTL_OUT(req, &xs, sizeof(xs));
2471e9c0cc15SPoul-Henning Kamp 	return (error);
2472e9c0cc15SPoul-Henning Kamp }
2473e9c0cc15SPoul-Henning Kamp 
24748f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2475e9c0cc15SPoul-Henning Kamp     "Number of swap devices");
2476e9c0cc15SPoul-Henning Kamp SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info,
2477e9c0cc15SPoul-Henning Kamp     "Swap statistics by device");
2478ec38b344SPoul-Henning Kamp 
2479ec38b344SPoul-Henning Kamp /*
24808eb5a1cdSKonstantin Belousov  * vmspace_swap_count() - count the approximate swap usage in pages for a
2481ec38b344SPoul-Henning Kamp  *			  vmspace.
2482ec38b344SPoul-Henning Kamp  *
2483ec38b344SPoul-Henning Kamp  *	The map must be locked.
2484ec38b344SPoul-Henning Kamp  *
24858eb5a1cdSKonstantin Belousov  *	Swap usage is determined by taking the proportional swap used by
2486ec38b344SPoul-Henning Kamp  *	VM objects backing the VM map.  To make up for fractional losses,
2487ec38b344SPoul-Henning Kamp  *	if the VM object has any swap use at all the associated map entries
2488ec38b344SPoul-Henning Kamp  *	count for at least 1 swap page.
2489ec38b344SPoul-Henning Kamp  */
24902860553aSRebecca Cran long
2491ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2492ec38b344SPoul-Henning Kamp {
249365d8409cSRebecca Cran 	vm_map_t map;
2494ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
249565d8409cSRebecca Cran 	vm_object_t object;
24962860553aSRebecca Cran 	long count, n;
249765d8409cSRebecca Cran 
249865d8409cSRebecca Cran 	map = &vmspace->vm_map;
249965d8409cSRebecca Cran 	count = 0;
2500ec38b344SPoul-Henning Kamp 
2501ec38b344SPoul-Henning Kamp 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2502ec38b344SPoul-Henning Kamp 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2503ec38b344SPoul-Henning Kamp 		    (object = cur->object.vm_object) != NULL) {
250489f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
2505ec38b344SPoul-Henning Kamp 			if (object->type == OBJT_SWAP &&
2506ec38b344SPoul-Henning Kamp 			    object->un_pager.swp.swp_bcount != 0) {
250765d8409cSRebecca Cran 				n = (cur->end - cur->start) / PAGE_SIZE;
2508ec38b344SPoul-Henning Kamp 				count += object->un_pager.swp.swp_bcount *
2509ec38b344SPoul-Henning Kamp 				    SWAP_META_PAGES * n / object->size + 1;
2510ec38b344SPoul-Henning Kamp 			}
251189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
2512ec38b344SPoul-Henning Kamp 		}
2513ec38b344SPoul-Henning Kamp 	}
2514ec38b344SPoul-Henning Kamp 	return (count);
2515ec38b344SPoul-Henning Kamp }
2516dee34ca4SPoul-Henning Kamp 
2517dee34ca4SPoul-Henning Kamp /*
2518dee34ca4SPoul-Henning Kamp  * GEOM backend
2519dee34ca4SPoul-Henning Kamp  *
2520dee34ca4SPoul-Henning Kamp  * Swapping onto disk devices.
2521dee34ca4SPoul-Henning Kamp  *
2522dee34ca4SPoul-Henning Kamp  */
2523dee34ca4SPoul-Henning Kamp 
25245721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan;
25255721c9c7SPoul-Henning Kamp 
2526dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = {
2527dee34ca4SPoul-Henning Kamp 	.name = "SWAP",
25285721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
25295721c9c7SPoul-Henning Kamp 	.orphan = swapgeom_orphan,
2530dee34ca4SPoul-Henning Kamp };
2531dee34ca4SPoul-Henning Kamp 
2532dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class);
2533dee34ca4SPoul-Henning Kamp 
2534dee34ca4SPoul-Henning Kamp 
2535dee34ca4SPoul-Henning Kamp static void
25363398491bSAlexander Motin swapgeom_close_ev(void *arg, int flags)
25373398491bSAlexander Motin {
25383398491bSAlexander Motin 	struct g_consumer *cp;
25393398491bSAlexander Motin 
25403398491bSAlexander Motin 	cp = arg;
25413398491bSAlexander Motin 	g_access(cp, -1, -1, 0);
25423398491bSAlexander Motin 	g_detach(cp);
25433398491bSAlexander Motin 	g_destroy_consumer(cp);
25443398491bSAlexander Motin }
25453398491bSAlexander Motin 
25463398491bSAlexander Motin static void
2547dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2)
2548dee34ca4SPoul-Henning Kamp {
25493398491bSAlexander Motin 	struct swdevt *sp;
2550dee34ca4SPoul-Henning Kamp 	struct buf *bp;
25513398491bSAlexander Motin 	struct g_consumer *cp;
2552dee34ca4SPoul-Henning Kamp 
2553dee34ca4SPoul-Henning Kamp 	bp = bp2->bio_caller2;
25543398491bSAlexander Motin 	cp = bp2->bio_from;
2555c5d3d25eSPoul-Henning Kamp 	bp->b_ioflags = bp2->bio_flags;
2556dee34ca4SPoul-Henning Kamp 	if (bp2->bio_error)
2557dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2558c5d3d25eSPoul-Henning Kamp 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2559c5d3d25eSPoul-Henning Kamp 	bp->b_error = bp2->bio_error;
2560dee34ca4SPoul-Henning Kamp 	bufdone(bp);
25613398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
25620ada3afcSAlexander Motin 	if ((--cp->index) == 0 && cp->private) {
25630ada3afcSAlexander Motin 		if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0) {
25643398491bSAlexander Motin 			sp = bp2->bio_caller1;
25653398491bSAlexander Motin 			sp->sw_id = NULL;
25663398491bSAlexander Motin 		}
25670ada3afcSAlexander Motin 	}
25683398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
2569dee34ca4SPoul-Henning Kamp 	g_destroy_bio(bp2);
2570dee34ca4SPoul-Henning Kamp }
2571dee34ca4SPoul-Henning Kamp 
2572dee34ca4SPoul-Henning Kamp static void
2573dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2574dee34ca4SPoul-Henning Kamp {
2575dee34ca4SPoul-Henning Kamp 	struct bio *bio;
2576dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2577dee34ca4SPoul-Henning Kamp 
25783398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
2579dee34ca4SPoul-Henning Kamp 	cp = sp->sw_id;
2580dee34ca4SPoul-Henning Kamp 	if (cp == NULL) {
25813398491bSAlexander Motin 		mtx_unlock(&sw_dev_mtx);
2582dee34ca4SPoul-Henning Kamp 		bp->b_error = ENXIO;
2583dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2584dee34ca4SPoul-Henning Kamp 		bufdone(bp);
2585dee34ca4SPoul-Henning Kamp 		return;
2586dee34ca4SPoul-Henning Kamp 	}
25873398491bSAlexander Motin 	cp->index++;
25883398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
258911041003SKonstantin Belousov 	if (bp->b_iocmd == BIO_WRITE)
259011041003SKonstantin Belousov 		bio = g_new_bio();
259111041003SKonstantin Belousov 	else
25924f8205e5SPoul-Henning Kamp 		bio = g_alloc_bio();
25934f8205e5SPoul-Henning Kamp 	if (bio == NULL) {
25943e5b6861SPoul-Henning Kamp 		bp->b_error = ENOMEM;
25953e5b6861SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
25963e5b6861SPoul-Henning Kamp 		bufdone(bp);
25973e5b6861SPoul-Henning Kamp 		return;
25983e5b6861SPoul-Henning Kamp 	}
259911041003SKonstantin Belousov 
26003398491bSAlexander Motin 	bio->bio_caller1 = sp;
2601dee34ca4SPoul-Henning Kamp 	bio->bio_caller2 = bp;
2602c5d3d25eSPoul-Henning Kamp 	bio->bio_cmd = bp->b_iocmd;
2603dee34ca4SPoul-Henning Kamp 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2604dee34ca4SPoul-Henning Kamp 	bio->bio_length = bp->b_bcount;
2605dee34ca4SPoul-Henning Kamp 	bio->bio_done = swapgeom_done;
26062cc718a1SKonstantin Belousov 	if ((bp->b_flags & B_UNMAPPED) != 0) {
26072cc718a1SKonstantin Belousov 		bio->bio_ma = bp->b_pages;
26082cc718a1SKonstantin Belousov 		bio->bio_data = unmapped_buf;
26092cc718a1SKonstantin Belousov 		bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
26102cc718a1SKonstantin Belousov 		bio->bio_ma_n = bp->b_npages;
26112cc718a1SKonstantin Belousov 		bio->bio_flags |= BIO_UNMAPPED;
26122cc718a1SKonstantin Belousov 	} else {
26132cc718a1SKonstantin Belousov 		bio->bio_data = bp->b_data;
26142cc718a1SKonstantin Belousov 		bio->bio_ma = NULL;
26152cc718a1SKonstantin Belousov 	}
2616dee34ca4SPoul-Henning Kamp 	g_io_request(bio, cp);
2617dee34ca4SPoul-Henning Kamp 	return;
2618dee34ca4SPoul-Henning Kamp }
2619dee34ca4SPoul-Henning Kamp 
2620dee34ca4SPoul-Henning Kamp static void
2621dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp)
2622dee34ca4SPoul-Henning Kamp {
2623dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
26243398491bSAlexander Motin 	int destroy;
2625dee34ca4SPoul-Henning Kamp 
2626dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
26273398491bSAlexander Motin 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
26283398491bSAlexander Motin 		if (sp->sw_id == cp) {
26298f12d83aSAlexander Motin 			sp->sw_flags |= SW_CLOSING;
26303398491bSAlexander Motin 			break;
2631dee34ca4SPoul-Henning Kamp 		}
26323398491bSAlexander Motin 	}
26333398491bSAlexander Motin 	cp->private = (void *)(uintptr_t)1;
26343398491bSAlexander Motin 	destroy = ((sp != NULL) && (cp->index == 0));
26353398491bSAlexander Motin 	if (destroy)
26363398491bSAlexander Motin 		sp->sw_id = NULL;
26373398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
26383398491bSAlexander Motin 	if (destroy)
26393398491bSAlexander Motin 		swapgeom_close_ev(cp, 0);
2640dee34ca4SPoul-Henning Kamp }
2641dee34ca4SPoul-Henning Kamp 
2642dee34ca4SPoul-Henning Kamp static void
2643dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw)
2644dee34ca4SPoul-Henning Kamp {
26453398491bSAlexander Motin 	struct g_consumer *cp;
2646dee34ca4SPoul-Henning Kamp 
26473398491bSAlexander Motin 	mtx_lock(&sw_dev_mtx);
26483398491bSAlexander Motin 	cp = sw->sw_id;
26493398491bSAlexander Motin 	sw->sw_id = NULL;
26503398491bSAlexander Motin 	mtx_unlock(&sw_dev_mtx);
2651dee34ca4SPoul-Henning Kamp 	/* XXX: direct call when Giant untangled */
26523398491bSAlexander Motin 	if (cp != NULL)
26533398491bSAlexander Motin 		g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2654dee34ca4SPoul-Henning Kamp }
2655dee34ca4SPoul-Henning Kamp 
2656dee34ca4SPoul-Henning Kamp 
2657dee34ca4SPoul-Henning Kamp struct swh0h0 {
265889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
2659dee34ca4SPoul-Henning Kamp 	struct vnode *vp;
2660dee34ca4SPoul-Henning Kamp 	int	error;
2661dee34ca4SPoul-Henning Kamp };
2662dee34ca4SPoul-Henning Kamp 
2663dee34ca4SPoul-Henning Kamp static void
2664dee34ca4SPoul-Henning Kamp swapongeom_ev(void *arg, int flags)
2665dee34ca4SPoul-Henning Kamp {
2666dee34ca4SPoul-Henning Kamp 	struct swh0h0 *swh;
2667dee34ca4SPoul-Henning Kamp 	struct g_provider *pp;
2668dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2669dee34ca4SPoul-Henning Kamp 	static struct g_geom *gp;
2670dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2671dee34ca4SPoul-Henning Kamp 	u_long nblks;
2672dee34ca4SPoul-Henning Kamp 	int error;
2673dee34ca4SPoul-Henning Kamp 
2674dee34ca4SPoul-Henning Kamp 	swh = arg;
2675dee34ca4SPoul-Henning Kamp 	swh->error = 0;
2676dee34ca4SPoul-Henning Kamp 	pp = g_dev_getprovider(swh->dev);
2677dee34ca4SPoul-Henning Kamp 	if (pp == NULL) {
2678dee34ca4SPoul-Henning Kamp 		swh->error = ENODEV;
2679dee34ca4SPoul-Henning Kamp 		return;
2680dee34ca4SPoul-Henning Kamp 	}
2681dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2682dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2683dee34ca4SPoul-Henning Kamp 		cp = sp->sw_id;
2684dee34ca4SPoul-Henning Kamp 		if (cp != NULL && cp->provider == pp) {
2685dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2686dee34ca4SPoul-Henning Kamp 			swh->error = EBUSY;
2687dee34ca4SPoul-Henning Kamp 			return;
2688dee34ca4SPoul-Henning Kamp 		}
2689dee34ca4SPoul-Henning Kamp 	}
2690dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
26915721c9c7SPoul-Henning Kamp 	if (gp == NULL)
269202c62349SJaakko Heinonen 		gp = g_new_geomf(&g_swap_class, "swap");
2693dee34ca4SPoul-Henning Kamp 	cp = g_new_consumer(gp);
26943398491bSAlexander Motin 	cp->index = 0;		/* Number of active I/Os. */
26953398491bSAlexander Motin 	cp->private = NULL;	/* Orphanization flag */
2696dee34ca4SPoul-Henning Kamp 	g_attach(cp, pp);
2697afeb65e6SPoul-Henning Kamp 	/*
2698afeb65e6SPoul-Henning Kamp 	 * XXX: Everytime you think you can improve the margin for
2699afeb65e6SPoul-Henning Kamp 	 * footshooting, somebody depends on the ability to do so:
2700afeb65e6SPoul-Henning Kamp 	 * savecore(8) wants to write to our swapdev so we cannot
2701afeb65e6SPoul-Henning Kamp 	 * set an exclusive count :-(
2702afeb65e6SPoul-Henning Kamp 	 */
2703d2bae332SPoul-Henning Kamp 	error = g_access(cp, 1, 1, 0);
2704dee34ca4SPoul-Henning Kamp 	if (error) {
2705dee34ca4SPoul-Henning Kamp 		g_detach(cp);
2706dee34ca4SPoul-Henning Kamp 		g_destroy_consumer(cp);
2707dee34ca4SPoul-Henning Kamp 		swh->error = error;
2708dee34ca4SPoul-Henning Kamp 		return;
2709dee34ca4SPoul-Henning Kamp 	}
2710dee34ca4SPoul-Henning Kamp 	nblks = pp->mediasize / DEV_BSIZE;
2711dee34ca4SPoul-Henning Kamp 	swaponsomething(swh->vp, cp, nblks, swapgeom_strategy,
27122cc718a1SKonstantin Belousov 	    swapgeom_close, dev2udev(swh->dev),
27132cc718a1SKonstantin Belousov 	    (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
2714dee34ca4SPoul-Henning Kamp 	swh->error = 0;
2715dee34ca4SPoul-Henning Kamp }
2716dee34ca4SPoul-Henning Kamp 
2717dee34ca4SPoul-Henning Kamp static int
2718dee34ca4SPoul-Henning Kamp swapongeom(struct thread *td, struct vnode *vp)
2719dee34ca4SPoul-Henning Kamp {
2720dee34ca4SPoul-Henning Kamp 	int error;
2721dee34ca4SPoul-Henning Kamp 	struct swh0h0 swh;
2722dee34ca4SPoul-Henning Kamp 
2723cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2724dee34ca4SPoul-Henning Kamp 
2725dee34ca4SPoul-Henning Kamp 	swh.dev = vp->v_rdev;
2726dee34ca4SPoul-Henning Kamp 	swh.vp = vp;
2727dee34ca4SPoul-Henning Kamp 	swh.error = 0;
2728dee34ca4SPoul-Henning Kamp 	/* XXX: direct call when Giant untangled */
2729dee34ca4SPoul-Henning Kamp 	error = g_waitfor_event(swapongeom_ev, &swh, M_WAITOK, NULL);
2730dee34ca4SPoul-Henning Kamp 	if (!error)
2731dee34ca4SPoul-Henning Kamp 		error = swh.error;
273222db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
2733dee34ca4SPoul-Henning Kamp 	return (error);
2734dee34ca4SPoul-Henning Kamp }
2735dee34ca4SPoul-Henning Kamp 
2736dee34ca4SPoul-Henning Kamp /*
2737dee34ca4SPoul-Henning Kamp  * VNODE backend
2738dee34ca4SPoul-Henning Kamp  *
2739dee34ca4SPoul-Henning Kamp  * This is used mainly for network filesystem (read: probably only tested
2740dee34ca4SPoul-Henning Kamp  * with NFS) swapfiles.
2741dee34ca4SPoul-Henning Kamp  *
2742dee34ca4SPoul-Henning Kamp  */
2743dee34ca4SPoul-Henning Kamp 
2744dee34ca4SPoul-Henning Kamp static void
2745dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp)
2746dee34ca4SPoul-Henning Kamp {
2747494eb176SPoul-Henning Kamp 	struct vnode *vp2;
2748dee34ca4SPoul-Henning Kamp 
2749dee34ca4SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2750dee34ca4SPoul-Henning Kamp 
2751dee34ca4SPoul-Henning Kamp 	vp2 = sp->sw_id;
2752dee34ca4SPoul-Henning Kamp 	vhold(vp2);
2753dee34ca4SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
27543cfc7651SOlivier Houchard 		if (bp->b_bufobj)
2755494eb176SPoul-Henning Kamp 			bufobj_wdrop(bp->b_bufobj);
2756a76d8f4eSPoul-Henning Kamp 		bufobj_wref(&vp2->v_bufobj);
2757dee34ca4SPoul-Henning Kamp 	}
27583cfc7651SOlivier Houchard 	if (bp->b_bufobj != &vp2->v_bufobj)
27593cfc7651SOlivier Houchard 		bp->b_bufobj = &vp2->v_bufobj;
2760dee34ca4SPoul-Henning Kamp 	bp->b_vp = vp2;
27612c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
2762b792bebeSPoul-Henning Kamp 	bstrategy(bp);
2763dee34ca4SPoul-Henning Kamp 	return;
2764dee34ca4SPoul-Henning Kamp }
2765dee34ca4SPoul-Henning Kamp 
2766dee34ca4SPoul-Henning Kamp static void
2767dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp)
2768dee34ca4SPoul-Henning Kamp {
2769dee34ca4SPoul-Henning Kamp 
2770dee34ca4SPoul-Henning Kamp 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2771dee34ca4SPoul-Henning Kamp 	vrele(sp->sw_vp);
2772dee34ca4SPoul-Henning Kamp }
2773dee34ca4SPoul-Henning Kamp 
2774dee34ca4SPoul-Henning Kamp 
2775dee34ca4SPoul-Henning Kamp static int
2776dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2777dee34ca4SPoul-Henning Kamp {
2778dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2779dee34ca4SPoul-Henning Kamp 	int error;
2780dee34ca4SPoul-Henning Kamp 
2781dee34ca4SPoul-Henning Kamp 	if (nblks == 0)
2782dee34ca4SPoul-Henning Kamp 		return (ENXIO);
2783dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2784dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2785dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == vp) {
2786dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2787dee34ca4SPoul-Henning Kamp 			return (EBUSY);
2788dee34ca4SPoul-Henning Kamp 		}
2789dee34ca4SPoul-Henning Kamp 	}
2790dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2791dee34ca4SPoul-Henning Kamp 
2792cb05b60aSAttilio Rao 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2793dee34ca4SPoul-Henning Kamp #ifdef MAC
279430d239bcSRobert Watson 	error = mac_system_check_swapon(td->td_ucred, vp);
2795dee34ca4SPoul-Henning Kamp 	if (error == 0)
2796dee34ca4SPoul-Henning Kamp #endif
27979e223287SKonstantin Belousov 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
279822db15c0SAttilio Rao 	(void) VOP_UNLOCK(vp, 0);
2799dee34ca4SPoul-Henning Kamp 	if (error)
2800dee34ca4SPoul-Henning Kamp 		return (error);
2801dee34ca4SPoul-Henning Kamp 
2802dee34ca4SPoul-Henning Kamp 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
28032cc718a1SKonstantin Belousov 	    NODEV, 0);
2804dee34ca4SPoul-Henning Kamp 	return (0);
2805dee34ca4SPoul-Henning Kamp }
280689c241d1SGleb Smirnoff 
280789c241d1SGleb Smirnoff static int
280889c241d1SGleb Smirnoff sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
280989c241d1SGleb Smirnoff {
281089c241d1SGleb Smirnoff 	int error, new, n;
281189c241d1SGleb Smirnoff 
281289c241d1SGleb Smirnoff 	new = nsw_wcount_async_max;
281389c241d1SGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
281489c241d1SGleb Smirnoff 	if (error != 0 || req->newptr == NULL)
281589c241d1SGleb Smirnoff 		return (error);
281689c241d1SGleb Smirnoff 
281789c241d1SGleb Smirnoff 	if (new > nswbuf / 2 || new < 1)
281889c241d1SGleb Smirnoff 		return (EINVAL);
281989c241d1SGleb Smirnoff 
282089c241d1SGleb Smirnoff 	mtx_lock(&pbuf_mtx);
282189c241d1SGleb Smirnoff 	while (nsw_wcount_async_max != new) {
282289c241d1SGleb Smirnoff 		/*
282389c241d1SGleb Smirnoff 		 * Adjust difference.  If the current async count is too low,
282489c241d1SGleb Smirnoff 		 * we will need to sqeeze our update slowly in.  Sleep with a
282589c241d1SGleb Smirnoff 		 * higher priority than getpbuf() to finish faster.
282689c241d1SGleb Smirnoff 		 */
282789c241d1SGleb Smirnoff 		n = new - nsw_wcount_async_max;
282889c241d1SGleb Smirnoff 		if (nsw_wcount_async + n >= 0) {
282989c241d1SGleb Smirnoff 			nsw_wcount_async += n;
283089c241d1SGleb Smirnoff 			nsw_wcount_async_max += n;
283189c241d1SGleb Smirnoff 			wakeup(&nsw_wcount_async);
283289c241d1SGleb Smirnoff 		} else {
283389c241d1SGleb Smirnoff 			nsw_wcount_async_max -= nsw_wcount_async;
283489c241d1SGleb Smirnoff 			nsw_wcount_async = 0;
283589c241d1SGleb Smirnoff 			msleep(&nsw_wcount_async, &pbuf_mtx, PSWP,
283689c241d1SGleb Smirnoff 			    "swpsysctl", 0);
283789c241d1SGleb Smirnoff 		}
283889c241d1SGleb Smirnoff 	}
283989c241d1SGleb Smirnoff 	mtx_unlock(&pbuf_mtx);
284089c241d1SGleb Smirnoff 
284189c241d1SGleb Smirnoff 	return (0);
284289c241d1SGleb Smirnoff }
2843