xref: /freebsd/sys/vm/swap_pager.c (revision dda4f960873c2234cf5d4f63b8acafbd17201b3d)
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>
92327f4e83SMatthew Dillon #include <sys/sysctl.h>
93e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
941c7c3c6aSMatthew Dillon #include <sys/blist.h>
951c7c3c6aSMatthew Dillon #include <sys/lock.h>
960cddd8f0SMatthew Dillon #include <sys/sx.h>
97936524aaSMatthew Dillon #include <sys/vmmeter.h>
98df8bae1dSRodney W. Grimes 
99aed55708SRobert Watson #include <security/mac/mac_framework.h>
100aed55708SRobert Watson 
101df8bae1dSRodney W. Grimes #include <vm/vm.h>
10221cd6e62SSeigo Tanimura #include <vm/pmap.h>
10321cd6e62SSeigo Tanimura #include <vm/vm_map.h>
10421cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
105efeaf95aSDavid Greenman #include <vm/vm_object.h>
106df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
107efeaf95aSDavid Greenman #include <vm/vm_pager.h>
108df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
109e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
110df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
111efeaf95aSDavid Greenman #include <vm/vm_extern.h>
112670d17b5SJeff Roberson #include <vm/uma.h>
113df8bae1dSRodney W. Grimes 
114dee34ca4SPoul-Henning Kamp #include <geom/geom.h>
115dee34ca4SPoul-Henning Kamp 
116ec38b344SPoul-Henning Kamp /*
117ec38b344SPoul-Henning Kamp  * SWB_NPAGES must be a power of 2.  It may be set to 1, 2, 4, 8, or 16
118ec38b344SPoul-Henning Kamp  * pages per allocation.  We recommend you stick with the default of 8.
119ec38b344SPoul-Henning Kamp  * The 16-page limit is due to the radix code (kern/subr_blist.c).
120ec38b344SPoul-Henning Kamp  */
121ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
122ec38b344SPoul-Henning Kamp #define MAX_PAGEOUT_CLUSTER 16
123ec38b344SPoul-Henning Kamp #endif
124ec38b344SPoul-Henning Kamp 
125ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
126ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
127ec38b344SPoul-Henning Kamp #endif
128ec38b344SPoul-Henning Kamp 
129ec38b344SPoul-Henning Kamp /*
130ec38b344SPoul-Henning Kamp  * Piecemeal swap metadata structure.  Swap is stored in a radix tree.
131ec38b344SPoul-Henning Kamp  *
132ec38b344SPoul-Henning Kamp  * If SWB_NPAGES is 8 and sizeof(char *) == sizeof(daddr_t), our radix
133ec38b344SPoul-Henning Kamp  * is basically 8.  Assuming PAGE_SIZE == 4096, one tree level represents
134ec38b344SPoul-Henning Kamp  * 32K worth of data, two levels represent 256K, three levels represent
135ec38b344SPoul-Henning Kamp  * 2 MBytes.   This is acceptable.
136ec38b344SPoul-Henning Kamp  *
137ec38b344SPoul-Henning Kamp  * Overall memory utilization is about the same as the old swap structure.
138ec38b344SPoul-Henning Kamp  */
139ec38b344SPoul-Henning Kamp #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t))
140ec38b344SPoul-Henning Kamp #define SWAP_META_PAGES		(SWB_NPAGES * 2)
141ec38b344SPoul-Henning Kamp #define SWAP_META_MASK		(SWAP_META_PAGES - 1)
142ec38b344SPoul-Henning Kamp 
143e9c0cc15SPoul-Henning Kamp struct swblock {
144e9c0cc15SPoul-Henning Kamp 	struct swblock	*swb_hnext;
145e9c0cc15SPoul-Henning Kamp 	vm_object_t	swb_object;
146e9c0cc15SPoul-Henning Kamp 	vm_pindex_t	swb_index;
147e9c0cc15SPoul-Henning Kamp 	int		swb_count;
148e9c0cc15SPoul-Henning Kamp 	daddr_t		swb_pages[SWAP_META_PAGES];
149e9c0cc15SPoul-Henning Kamp };
150e9c0cc15SPoul-Henning Kamp 
1512c4992dbSAlan Cox static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
15220da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx;
1538f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1548f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1558f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1568f60c087SPoul-Henning Kamp int swap_pager_avail;
157e9c0cc15SPoul-Henning Kamp static int swdev_syscall_active = 0; /* serialize swap(on|off) */
158e9c0cc15SPoul-Henning Kamp 
1593364c323SKonstantin Belousov static vm_ooffset_t swap_total;
1608a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0,
1618a9c731fSIvan Voras     "Total amount of available swap storage.");
1623364c323SKonstantin Belousov static vm_ooffset_t swap_reserved;
1638a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0,
1648a9c731fSIvan Voras     "Amount of swap storage needed to back all allocated anonymous memory.");
1653364c323SKonstantin Belousov static int overcommit = 0;
1668a9c731fSIvan Voras SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0,
1678a9c731fSIvan Voras     "Configure virtual memory overcommit behavior. See tuning(7) "
1688a9c731fSIvan Voras     "for details.");
1693364c323SKonstantin Belousov 
1703364c323SKonstantin Belousov /* bits from overcommit */
1713364c323SKonstantin Belousov #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
1723364c323SKonstantin Belousov #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
1733364c323SKonstantin Belousov #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
1743364c323SKonstantin Belousov 
1753364c323SKonstantin Belousov int
1763364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr)
1773364c323SKonstantin Belousov {
1783364c323SKonstantin Belousov 
179ef694c1aSEdward Tomasz Napierala 	return (swap_reserve_by_cred(incr, curthread->td_ucred));
1803364c323SKonstantin Belousov }
1813364c323SKonstantin Belousov 
1823364c323SKonstantin Belousov int
183ef694c1aSEdward Tomasz Napierala swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
1843364c323SKonstantin Belousov {
1855c0e1c11SKonstantin Belousov 	vm_ooffset_t r, s;
1863364c323SKonstantin Belousov 	int res, error;
1873364c323SKonstantin Belousov 	static int curfail;
1883364c323SKonstantin Belousov 	static struct timeval lastfail;
189ef694c1aSEdward Tomasz Napierala 	struct uidinfo *uip;
190ef694c1aSEdward Tomasz Napierala 
191ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
1923364c323SKonstantin Belousov 
1933364c323SKonstantin Belousov 	if (incr & PAGE_MASK)
1943364c323SKonstantin Belousov 		panic("swap_reserve: & PAGE_MASK");
1953364c323SKonstantin Belousov 
196afcc55f3SEdward Tomasz Napierala #ifdef RACCT
1971ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(curproc);
1981ba5ad42SEdward Tomasz Napierala 	error = racct_add(curproc, RACCT_SWAP, incr);
1991ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(curproc);
2001ba5ad42SEdward Tomasz Napierala 	if (error != 0)
2011ba5ad42SEdward Tomasz Napierala 		return (0);
202afcc55f3SEdward Tomasz Napierala #endif
2031ba5ad42SEdward Tomasz Napierala 
2043364c323SKonstantin Belousov 	res = 0;
2053364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2063364c323SKonstantin Belousov 	r = swap_reserved + incr;
2073364c323SKonstantin Belousov 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
2083364c323SKonstantin Belousov 		s = cnt.v_page_count - cnt.v_free_reserved - cnt.v_wire_count;
2093364c323SKonstantin Belousov 		s *= PAGE_SIZE;
2103364c323SKonstantin Belousov 	} else
2113364c323SKonstantin Belousov 		s = 0;
2123364c323SKonstantin Belousov 	s += swap_total;
2133364c323SKonstantin Belousov 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
2143364c323SKonstantin Belousov 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
2153364c323SKonstantin Belousov 		res = 1;
2163364c323SKonstantin Belousov 		swap_reserved = r;
2173364c323SKonstantin Belousov 	}
2183364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2193364c323SKonstantin Belousov 
2203364c323SKonstantin Belousov 	if (res) {
2213364c323SKonstantin Belousov 		PROC_LOCK(curproc);
2223364c323SKonstantin Belousov 		UIDINFO_VMSIZE_LOCK(uip);
2235c0e1c11SKonstantin Belousov 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
2245c0e1c11SKonstantin Belousov 		    uip->ui_vmsize + incr > lim_cur(curproc, RLIMIT_SWAP) &&
2255c0e1c11SKonstantin Belousov 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT))
2263364c323SKonstantin Belousov 			res = 0;
2273364c323SKonstantin Belousov 		else
2283364c323SKonstantin Belousov 			uip->ui_vmsize += incr;
2293364c323SKonstantin Belousov 		UIDINFO_VMSIZE_UNLOCK(uip);
2303364c323SKonstantin Belousov 		PROC_UNLOCK(curproc);
2313364c323SKonstantin Belousov 		if (!res) {
2323364c323SKonstantin Belousov 			mtx_lock(&sw_dev_mtx);
2333364c323SKonstantin Belousov 			swap_reserved -= incr;
2343364c323SKonstantin Belousov 			mtx_unlock(&sw_dev_mtx);
2353364c323SKonstantin Belousov 		}
2363364c323SKonstantin Belousov 	}
2373364c323SKonstantin Belousov 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
2383364c323SKonstantin Belousov 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
2393364c323SKonstantin Belousov 		    curproc->p_pid, uip->ui_uid, incr);
2403364c323SKonstantin Belousov 	}
2413364c323SKonstantin Belousov 
242afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2431ba5ad42SEdward Tomasz Napierala 	if (!res) {
2441ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(curproc);
2451ba5ad42SEdward Tomasz Napierala 		racct_sub(curproc, RACCT_SWAP, incr);
2461ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(curproc);
2471ba5ad42SEdward Tomasz Napierala 	}
248afcc55f3SEdward Tomasz Napierala #endif
2491ba5ad42SEdward Tomasz Napierala 
2503364c323SKonstantin Belousov 	return (res);
2513364c323SKonstantin Belousov }
2523364c323SKonstantin Belousov 
2533364c323SKonstantin Belousov void
2543364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr)
2553364c323SKonstantin Belousov {
2563364c323SKonstantin Belousov 	struct uidinfo *uip;
2573364c323SKonstantin Belousov 
2583364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2593364c323SKonstantin Belousov 	swap_reserved += incr;
2603364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2613364c323SKonstantin Belousov 
262afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2631ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(curproc);
2641ba5ad42SEdward Tomasz Napierala 	racct_add_force(curproc, RACCT_SWAP, incr);
2651ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(curproc);
266afcc55f3SEdward Tomasz Napierala #endif
2671ba5ad42SEdward Tomasz Napierala 
2683364c323SKonstantin Belousov 	uip = curthread->td_ucred->cr_ruidinfo;
2693364c323SKonstantin Belousov 	PROC_LOCK(curproc);
2703364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
2713364c323SKonstantin Belousov 	uip->ui_vmsize += incr;
2723364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
2733364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2743364c323SKonstantin Belousov }
2753364c323SKonstantin Belousov 
2763364c323SKonstantin Belousov void
2773364c323SKonstantin Belousov swap_release(vm_ooffset_t decr)
2783364c323SKonstantin Belousov {
279ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
2803364c323SKonstantin Belousov 
2813364c323SKonstantin Belousov 	PROC_LOCK(curproc);
282ef694c1aSEdward Tomasz Napierala 	cred = curthread->td_ucred;
283ef694c1aSEdward Tomasz Napierala 	swap_release_by_cred(decr, cred);
2843364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2853364c323SKonstantin Belousov }
2863364c323SKonstantin Belousov 
2873364c323SKonstantin Belousov void
288ef694c1aSEdward Tomasz Napierala swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
2893364c323SKonstantin Belousov {
290ef694c1aSEdward Tomasz Napierala  	struct uidinfo *uip;
291ef694c1aSEdward Tomasz Napierala 
292ef694c1aSEdward Tomasz Napierala 	uip = cred->cr_ruidinfo;
2933364c323SKonstantin Belousov 
2943364c323SKonstantin Belousov 	if (decr & PAGE_MASK)
2953364c323SKonstantin Belousov 		panic("swap_release: & PAGE_MASK");
2963364c323SKonstantin Belousov 
2973364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2983364c323SKonstantin Belousov 	if (swap_reserved < decr)
2993364c323SKonstantin Belousov 		panic("swap_reserved < decr");
3003364c323SKonstantin Belousov 	swap_reserved -= decr;
3013364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
3023364c323SKonstantin Belousov 
3033364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
3043364c323SKonstantin Belousov 	if (uip->ui_vmsize < decr)
3053364c323SKonstantin Belousov 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
3063364c323SKonstantin Belousov 	uip->ui_vmsize -= decr;
3073364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
3081ba5ad42SEdward Tomasz Napierala 
3091ba5ad42SEdward Tomasz Napierala 	racct_sub_cred(cred, RACCT_SWAP, decr);
3103364c323SKonstantin Belousov }
3113364c323SKonstantin Belousov 
3124b03903aSPoul-Henning Kamp static void swapdev_strategy(struct buf *, struct swdevt *sw);
313e9c0cc15SPoul-Henning Kamp 
3141c7c3c6aSMatthew Dillon #define SWM_FREE	0x02	/* free, period			*/
3151c7c3c6aSMatthew Dillon #define SWM_POP		0x04	/* pop out			*/
31626f9a767SRodney W. Grimes 
3177dea2c2eSAlan Cox int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
3187dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
3191c7c3c6aSMatthew Dillon static int nsw_rcount;		/* free read buffers			*/
320327f4e83SMatthew Dillon static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
321327f4e83SMatthew Dillon static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
322327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
323327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
3241c7c3c6aSMatthew Dillon 
3251c7c3c6aSMatthew Dillon static struct swblock **swhash;
3261c7c3c6aSMatthew Dillon static int swhash_mask;
3277827d9b0SAlan Cox static struct mtx swhash_mtx;
3287827d9b0SAlan Cox 
329327f4e83SMatthew Dillon static int swap_async_max = 4;	/* maximum in-progress async I/O's	*/
3300cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
331327f4e83SMatthew Dillon 
33224e7ab7cSPoul-Henning Kamp 
333327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_async_max,
334327f4e83SMatthew Dillon         CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops");
3351c7c3c6aSMatthew Dillon 
3361c7c3c6aSMatthew Dillon /*
3371c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
3381c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
3391c7c3c6aSMatthew Dillon  */
3401c7c3c6aSMatthew Dillon 
3411c7c3c6aSMatthew Dillon #define NOBJLISTS		8
3421c7c3c6aSMatthew Dillon 
3431c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
344af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
3451c7c3c6aSMatthew Dillon 
346a9fa2c05SAlfred Perlstein static struct mtx sw_alloc_mtx;	/* protect list manipulation */
3471c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
348e9c0cc15SPoul-Henning Kamp static uma_zone_t	swap_zone;
3495285558aSAlan Cox static struct vm_object	swap_zone_obj;
3501c7c3c6aSMatthew Dillon 
3511c7c3c6aSMatthew Dillon /*
3521c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
3531c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
3541c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
3551c7c3c6aSMatthew Dillon  */
356ff98689dSBruce Evans static vm_object_t
35711caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
3583364c323SKonstantin Belousov 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
35911caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
36011caded3SAlfred Perlstein static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int);
361751221fdSPoul-Henning Kamp static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
3625ea4972cSAlan Cox static boolean_t
3635ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
36411caded3SAlfred Perlstein static void	swap_pager_init(void);
36511caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
366b3fed13eSDavid Schultz static void	swap_pager_swapoff(struct swdevt *sp);
367f708ef1bSPoul-Henning Kamp 
368df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
369e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
370e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
371e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
372e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
373e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
374e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
375e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
376df8bae1dSRodney W. Grimes };
377df8bae1dSRodney W. Grimes 
3781c7c3c6aSMatthew Dillon /*
3791c7c3c6aSMatthew Dillon  * dmmax is in page-sized chunks with the new swap system.  It was
38064bcb9c8SMatthew Dillon  * dev-bsized chunks in the old.  dmmax is always a power of 2.
3811c7c3c6aSMatthew Dillon  *
3821c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
3831c7c3c6aSMatthew Dillon  * internal.
3841c7c3c6aSMatthew Dillon  */
385751221fdSPoul-Henning Kamp static int dmmax;
386e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
387e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
38826f9a767SRodney W. Grimes 
389cee313c4SRobert Watson SYSCTL_INT(_vm, OID_AUTO, dmmax,
390cee313c4SRobert Watson 	CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block");
391cee313c4SRobert Watson 
392a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
39311caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
394dee34ca4SPoul-Henning Kamp static int	swapongeom(struct thread *, struct vnode *);
39559efee01SPoul-Henning Kamp static int	swaponvp(struct thread *, struct vnode *, u_long);
39635918c55SChristian S.J. Peron static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
39724a1cce3SDavid Greenman 
3981c7c3c6aSMatthew Dillon /*
3991c7c3c6aSMatthew Dillon  * Swap bitmap functions
4001c7c3c6aSMatthew Dillon  */
401a5edd34aSPoul-Henning Kamp static void	swp_pager_freeswapspace(daddr_t blk, int npages);
402a5edd34aSPoul-Henning Kamp static daddr_t	swp_pager_getswapspace(int npages);
4031c7c3c6aSMatthew Dillon 
4041c7c3c6aSMatthew Dillon /*
4051c7c3c6aSMatthew Dillon  * Metadata functions
4061c7c3c6aSMatthew Dillon  */
407a5edd34aSPoul-Henning Kamp static struct swblock **swp_pager_hash(vm_object_t object, vm_pindex_t index);
40811caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
40911caded3SAlfred Perlstein static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t);
41011caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
41111caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
4121c7c3c6aSMatthew Dillon 
413e86a87e9SKonstantin Belousov static void
414e86a87e9SKonstantin Belousov swp_pager_free_nrpage(vm_page_t m)
415e86a87e9SKonstantin Belousov {
416e86a87e9SKonstantin Belousov 
4173c4a2440SAlan Cox 	vm_page_lock(m);
418e86a87e9SKonstantin Belousov 	if (m->wire_count == 0)
419e86a87e9SKonstantin Belousov 		vm_page_free(m);
4203c4a2440SAlan Cox 	vm_page_unlock(m);
421e86a87e9SKonstantin Belousov }
422e86a87e9SKonstantin Belousov 
4231c7c3c6aSMatthew Dillon /*
4241c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
4251c7c3c6aSMatthew Dillon  *
42620d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
42720d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
42820d3034fSMatthew Dillon  *
42920d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
4301c7c3c6aSMatthew Dillon  *
4311c7c3c6aSMatthew Dillon  *	No restrictions on call
4321c7c3c6aSMatthew Dillon  *	This routine may not block.
4331c7c3c6aSMatthew Dillon  */
434a5edd34aSPoul-Henning Kamp static void
4352f249180SPoul-Henning Kamp swp_sizecheck(void)
4360d94caffSDavid Greenman {
43723955314SAlfred Perlstein 
4388f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
43920d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
4401af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
44120d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
4422b0d37a4SMatthew Dillon 		}
44320d3034fSMatthew Dillon 	} else {
44426f9a767SRodney W. Grimes 		swap_pager_full = 0;
4458f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
44620d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
44726f9a767SRodney W. Grimes 	}
4481c7c3c6aSMatthew Dillon }
4491c7c3c6aSMatthew Dillon 
4501c7c3c6aSMatthew Dillon /*
451da5fd145SPeter Wemm  * SWP_PAGER_HASH() -	hash swap meta data
452da5fd145SPeter Wemm  *
453a5edd34aSPoul-Henning Kamp  *	This is an helper function which hashes the swapblk given
454da5fd145SPeter Wemm  *	the object and page index.  It returns a pointer to a pointer
455da5fd145SPeter Wemm  *	to the object, or a pointer to a NULL pointer if it could not
456da5fd145SPeter Wemm  *	find a swapblk.
457da5fd145SPeter Wemm  */
458a5edd34aSPoul-Henning Kamp static struct swblock **
459da5fd145SPeter Wemm swp_pager_hash(vm_object_t object, vm_pindex_t index)
460da5fd145SPeter Wemm {
461da5fd145SPeter Wemm 	struct swblock **pswap;
462da5fd145SPeter Wemm 	struct swblock *swap;
463da5fd145SPeter Wemm 
464da5fd145SPeter Wemm 	index &= ~(vm_pindex_t)SWAP_META_MASK;
465da5fd145SPeter Wemm 	pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask];
466da5fd145SPeter Wemm 	while ((swap = *pswap) != NULL) {
467da5fd145SPeter Wemm 		if (swap->swb_object == object &&
468da5fd145SPeter Wemm 		    swap->swb_index == index
469da5fd145SPeter Wemm 		) {
470da5fd145SPeter Wemm 			break;
471da5fd145SPeter Wemm 		}
472da5fd145SPeter Wemm 		pswap = &swap->swb_hnext;
473da5fd145SPeter Wemm 	}
474da5fd145SPeter Wemm 	return (pswap);
475da5fd145SPeter Wemm }
476da5fd145SPeter Wemm 
477da5fd145SPeter Wemm /*
4781c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
4791c7c3c6aSMatthew Dillon  *
4801c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
4811c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
4821c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
4831c7c3c6aSMatthew Dillon  */
484f5a12711SPoul-Henning Kamp static void
4852f249180SPoul-Henning Kamp swap_pager_init(void)
486df8bae1dSRodney W. Grimes {
4871c7c3c6aSMatthew Dillon 	/*
4881c7c3c6aSMatthew Dillon 	 * Initialize object lists
4891c7c3c6aSMatthew Dillon 	 */
4901c7c3c6aSMatthew Dillon 	int i;
4911c7c3c6aSMatthew Dillon 
4921c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
4931c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
4946008862bSJohn Baldwin 	mtx_init(&sw_alloc_mtx, "swap_pager list", NULL, MTX_DEF);
49520da9c2eSPoul-Henning Kamp 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
496df8bae1dSRodney W. Grimes 
497df8bae1dSRodney W. Grimes 	/*
4981c7c3c6aSMatthew Dillon 	 * Device Stripe, in PAGE_SIZE'd blocks
499df8bae1dSRodney W. Grimes 	 */
5001c7c3c6aSMatthew Dillon 	dmmax = SWB_NPAGES * 2;
5011c7c3c6aSMatthew Dillon }
50226f9a767SRodney W. Grimes 
503df8bae1dSRodney W. Grimes /*
5041c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
5051c7c3c6aSMatthew Dillon  *
5061c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
5071c7c3c6aSMatthew Dillon  *	its main loop.
508df8bae1dSRodney W. Grimes  */
50924a1cce3SDavid Greenman void
5102f249180SPoul-Henning Kamp swap_pager_swap_init(void)
511df8bae1dSRodney W. Grimes {
51221cd6e62SSeigo Tanimura 	int n, n2;
5130d94caffSDavid Greenman 
51426f9a767SRodney W. Grimes 	/*
5151c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
5161c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
5171c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
5181c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
5191c7c3c6aSMatthew Dillon 	 *
520327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
5211c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
5221c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
5231c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
524327f4e83SMatthew Dillon 	 *
525327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
526327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
527327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
528327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
529327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
530327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
531327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
532327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
533327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
534327f4e83SMatthew Dillon 	 * So it all works out pretty well.
53526f9a767SRodney W. Grimes 	 */
536ad3cce20SMatthew Dillon 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
537327f4e83SMatthew Dillon 
5386d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
5391c7c3c6aSMatthew Dillon 	nsw_rcount = (nswbuf + 1) / 2;
540327f4e83SMatthew Dillon 	nsw_wcount_sync = (nswbuf + 3) / 4;
541327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
542327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
5436d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
54424a1cce3SDavid Greenman 
5451c7c3c6aSMatthew Dillon 	/*
5461c7c3c6aSMatthew Dillon 	 * Initialize our zone.  Right now I'm just guessing on the number
5471c7c3c6aSMatthew Dillon 	 * we need based on the number of pages in the system.  Each swblock
5482f9e4e80SMatthew Dillon 	 * can hold 16 pages, so this is probably overkill.  This reservation
549ec61f55dSMatthew Dillon 	 * is typically limited to around 32MB by default.
5501c7c3c6aSMatthew Dillon 	 */
5512feb50bfSAttilio Rao 	n = cnt.v_page_count / 2;
5522f9e4e80SMatthew Dillon 	if (maxswzone && n > maxswzone / sizeof(struct swblock))
5532f9e4e80SMatthew Dillon 		n = maxswzone / sizeof(struct swblock);
55421cd6e62SSeigo Tanimura 	n2 = n;
555670d17b5SJeff Roberson 	swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL,
5567827d9b0SAlan Cox 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
557010b1ca1SDavid Schultz 	if (swap_zone == NULL)
558010b1ca1SDavid Schultz 		panic("failed to create swap_zone.");
5598355f576SJeff Roberson 	do {
5605285558aSAlan Cox 		if (uma_zone_set_obj(swap_zone, &swap_zone_obj, n))
56161ce6eeeSAlfred Perlstein 			break;
56261ce6eeeSAlfred Perlstein 		/*
56361ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
56461ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
56561ce6eeeSAlfred Perlstein 		 */
56661ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
56761ce6eeeSAlfred Perlstein 	} while (n > 0);
56821cd6e62SSeigo Tanimura 	if (n2 != n)
56961ce6eeeSAlfred Perlstein 		printf("Swap zone entries reduced from %d to %d.\n", n2, n);
57021cd6e62SSeigo Tanimura 	n2 = n;
57124a1cce3SDavid Greenman 
5721c7c3c6aSMatthew Dillon 	/*
5731c7c3c6aSMatthew Dillon 	 * Initialize our meta-data hash table.  The swapper does not need to
5741c7c3c6aSMatthew Dillon 	 * be quite as efficient as the VM system, so we do not use an
5751c7c3c6aSMatthew Dillon 	 * oversized hash table.
5761c7c3c6aSMatthew Dillon 	 *
5771c7c3c6aSMatthew Dillon 	 * 	n: 		size of hash table, must be power of 2
5781c7c3c6aSMatthew Dillon 	 *	swhash_mask:	hash table index mask
5791c7c3c6aSMatthew Dillon 	 */
58061ce6eeeSAlfred Perlstein 	for (n = 1; n < n2 / 8; n *= 2)
5811c7c3c6aSMatthew Dillon 		;
582a163d034SWarner Losh 	swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO);
5831c7c3c6aSMatthew Dillon 	swhash_mask = n - 1;
5847827d9b0SAlan Cox 	mtx_init(&swhash_mtx, "swap_pager swhash", NULL, MTX_DEF);
58524a1cce3SDavid Greenman }
58624a1cce3SDavid Greenman 
58724a1cce3SDavid Greenman /*
5881c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
5891c7c3c6aSMatthew Dillon  *			its metadata structures.
5901c7c3c6aSMatthew Dillon  *
5911c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
5921c7c3c6aSMatthew Dillon  *	OBJT_SWAP object.  We do this by creating an OBJT_DEFAULT object
5931c7c3c6aSMatthew Dillon  *	and then converting it with swp_pager_meta_build().
5941c7c3c6aSMatthew Dillon  *
5951c7c3c6aSMatthew Dillon  *	This routine may block in vm_object_allocate() and create a named
596cec9f109SDavid E. O'Brien  *	object lookup race, so we must interlock.
59724c46d03SAlan Cox  *
59824c46d03SAlan Cox  * MPSAFE
59924a1cce3SDavid Greenman  */
600f5a12711SPoul-Henning Kamp static vm_object_t
6016cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
6023364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
60324a1cce3SDavid Greenman {
60424a1cce3SDavid Greenman 	vm_object_t object;
6052f7af3dbSAlan Cox 	vm_pindex_t pindex;
6062f7af3dbSAlan Cox 
6072f7af3dbSAlan Cox 	pindex = OFF_TO_IDX(offset + PAGE_MASK + size);
60824a1cce3SDavid Greenman 	if (handle) {
609e793b779SAlan Cox 		mtx_lock(&Giant);
6101c7c3c6aSMatthew Dillon 		/*
6111c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
6121c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
6131c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
6141c7c3c6aSMatthew Dillon 		 * of the handle.
6151c7c3c6aSMatthew Dillon 		 */
6160cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
6171c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
618b5e8f167SAlan Cox 		if (object == NULL) {
6193364c323SKonstantin Belousov 			if (cred != NULL) {
620ef694c1aSEdward Tomasz Napierala 				if (!swap_reserve_by_cred(size, cred)) {
6213364c323SKonstantin Belousov 					sx_xunlock(&sw_alloc_sx);
6223364c323SKonstantin Belousov 					mtx_unlock(&Giant);
6233364c323SKonstantin Belousov 					return (NULL);
6243364c323SKonstantin Belousov 				}
625ef694c1aSEdward Tomasz Napierala 				crhold(cred);
6263364c323SKonstantin Belousov 			}
6272f7af3dbSAlan Cox 			object = vm_object_allocate(OBJT_DEFAULT, pindex);
628ee3dc7d7SAlan Cox 			VM_OBJECT_LOCK(object);
6293364c323SKonstantin Belousov 			object->handle = handle;
6303364c323SKonstantin Belousov 			if (cred != NULL) {
631ef694c1aSEdward Tomasz Napierala 				object->cred = cred;
6323364c323SKonstantin Belousov 				object->charge = size;
6333364c323SKonstantin Belousov 			}
6344dcc5c2dSMatthew Dillon 			swp_pager_meta_build(object, 0, SWAPBLK_NONE);
635ee3dc7d7SAlan Cox 			VM_OBJECT_UNLOCK(object);
63624a1cce3SDavid Greenman 		}
6370cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
638e793b779SAlan Cox 		mtx_unlock(&Giant);
63924a1cce3SDavid Greenman 	} else {
6403364c323SKonstantin Belousov 		if (cred != NULL) {
641ef694c1aSEdward Tomasz Napierala 			if (!swap_reserve_by_cred(size, cred))
6423364c323SKonstantin Belousov 				return (NULL);
643ef694c1aSEdward Tomasz Napierala 			crhold(cred);
6443364c323SKonstantin Belousov 		}
6452f7af3dbSAlan Cox 		object = vm_object_allocate(OBJT_DEFAULT, pindex);
646ee3dc7d7SAlan Cox 		VM_OBJECT_LOCK(object);
6473364c323SKonstantin Belousov 		if (cred != NULL) {
648ef694c1aSEdward Tomasz Napierala 			object->cred = cred;
6493364c323SKonstantin Belousov 			object->charge = size;
6503364c323SKonstantin Belousov 		}
6514dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
652ee3dc7d7SAlan Cox 		VM_OBJECT_UNLOCK(object);
65324a1cce3SDavid Greenman 	}
65424a1cce3SDavid Greenman 	return (object);
655df8bae1dSRodney W. Grimes }
656df8bae1dSRodney W. Grimes 
65726f9a767SRodney W. Grimes /*
6581c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
6591c7c3c6aSMatthew Dillon  *
6601c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
6611c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
6621c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
6631c7c3c6aSMatthew Dillon  *	about to be destroyed.
6641c7c3c6aSMatthew Dillon  *
6651c7c3c6aSMatthew Dillon  *	This routine may block, but no longer does.
6661c7c3c6aSMatthew Dillon  *
6671c7c3c6aSMatthew Dillon  *	The object must be locked or unreferenceable.
66826f9a767SRodney W. Grimes  */
669df8bae1dSRodney W. Grimes static void
6702f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object)
67126f9a767SRodney W. Grimes {
6724dcc5c2dSMatthew Dillon 
67326f9a767SRodney W. Grimes 	/*
6741c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
6751c7c3c6aSMatthew Dillon 	 * pageout completion.
67626f9a767SRodney W. Grimes 	 */
677bd228075SAlan Cox 	if (object->handle != NULL) {
678a9fa2c05SAlfred Perlstein 		mtx_lock(&sw_alloc_mtx);
6791c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list);
680a9fa2c05SAlfred Perlstein 		mtx_unlock(&sw_alloc_mtx);
681bd228075SAlan Cox 	}
6821c7c3c6aSMatthew Dillon 
683658ad5ffSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
6841c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
6851c7c3c6aSMatthew Dillon 
6861c7c3c6aSMatthew Dillon 	/*
6871c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
6881c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
6891c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
6901c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
6911c7c3c6aSMatthew Dillon 	 */
6921c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
6931c7c3c6aSMatthew Dillon }
6941c7c3c6aSMatthew Dillon 
6951c7c3c6aSMatthew Dillon /************************************************************************
6961c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
6971c7c3c6aSMatthew Dillon  ************************************************************************/
6981c7c3c6aSMatthew Dillon 
6991c7c3c6aSMatthew Dillon /*
7001c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
7011c7c3c6aSMatthew Dillon  *
7021c7c3c6aSMatthew Dillon  *	Allocate swap for the requested number of pages.  The starting
7031c7c3c6aSMatthew Dillon  *	swap block number (a page index) is returned or SWAPBLK_NONE
7041c7c3c6aSMatthew Dillon  *	if the allocation failed.
7051c7c3c6aSMatthew Dillon  *
7061c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
7071c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
7081c7c3c6aSMatthew Dillon  *
7091c7c3c6aSMatthew Dillon  *	This routine may not block
7108f60c087SPoul-Henning Kamp  *
7118f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
7121c7c3c6aSMatthew Dillon  */
713a5edd34aSPoul-Henning Kamp static daddr_t
7142f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages)
7151c7c3c6aSMatthew Dillon {
7161c7c3c6aSMatthew Dillon 	daddr_t blk;
7178f60c087SPoul-Henning Kamp 	struct swdevt *sp;
7188f60c087SPoul-Henning Kamp 	int i;
7191c7c3c6aSMatthew Dillon 
7208f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
72120da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7228f60c087SPoul-Henning Kamp 	sp = swdevhd;
7238f60c087SPoul-Henning Kamp 	for (i = 0; i < nswapdev; i++) {
7248f60c087SPoul-Henning Kamp 		if (sp == NULL)
7258f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
7268f60c087SPoul-Henning Kamp 		if (!(sp->sw_flags & SW_CLOSING)) {
7278f60c087SPoul-Henning Kamp 			blk = blist_alloc(sp->sw_blist, npages);
7288f60c087SPoul-Henning Kamp 			if (blk != SWAPBLK_NONE) {
7298f60c087SPoul-Henning Kamp 				blk += sp->sw_first;
7308f60c087SPoul-Henning Kamp 				sp->sw_used += npages;
731d05bc129SAlan Cox 				swap_pager_avail -= npages;
7328f60c087SPoul-Henning Kamp 				swp_sizecheck();
7338f60c087SPoul-Henning Kamp 				swdevhd = TAILQ_NEXT(sp, sw_list);
734d05bc129SAlan Cox 				goto done;
7358f60c087SPoul-Henning Kamp 			}
7368f60c087SPoul-Henning Kamp 		}
7378f60c087SPoul-Henning Kamp 		sp = TAILQ_NEXT(sp, sw_list);
7388f60c087SPoul-Henning Kamp 	}
7392b0d37a4SMatthew Dillon 	if (swap_pager_full != 2) {
74020da9c2eSPoul-Henning Kamp 		printf("swap_pager_getswapspace(%d): failed\n", npages);
7412b0d37a4SMatthew Dillon 		swap_pager_full = 2;
74220d3034fSMatthew Dillon 		swap_pager_almost_full = 1;
7432b0d37a4SMatthew Dillon 	}
7448f60c087SPoul-Henning Kamp 	swdevhd = NULL;
745d05bc129SAlan Cox done:
746d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
7471c7c3c6aSMatthew Dillon 	return (blk);
74826f9a767SRodney W. Grimes }
74926f9a767SRodney W. Grimes 
750b3fed13eSDavid Schultz static int
751b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp)
7528f60c087SPoul-Henning Kamp {
7538f60c087SPoul-Henning Kamp 
754b3fed13eSDavid Schultz 	return (blk >= sp->sw_first && blk < sp->sw_end);
7558f60c087SPoul-Henning Kamp }
7568f60c087SPoul-Henning Kamp 
7574b03903aSPoul-Henning Kamp static void
7584b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp)
7594b03903aSPoul-Henning Kamp {
7604b03903aSPoul-Henning Kamp 	struct swdevt *sp;
7614b03903aSPoul-Henning Kamp 
76220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7634b03903aSPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7644b03903aSPoul-Henning Kamp 		if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) {
76520da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
7664b03903aSPoul-Henning Kamp 			sp->sw_strategy(bp, sp);
7674b03903aSPoul-Henning Kamp 			return;
7684b03903aSPoul-Henning Kamp 		}
7694b03903aSPoul-Henning Kamp 	}
77020da9c2eSPoul-Henning Kamp 	panic("Swapdev not found");
7714b03903aSPoul-Henning Kamp }
7724b03903aSPoul-Henning Kamp 
7738f60c087SPoul-Henning Kamp 
77426f9a767SRodney W. Grimes /*
7751c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
7761c7c3c6aSMatthew Dillon  *
7771c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
7781c7c3c6aSMatthew Dillon  *
7791c7c3c6aSMatthew Dillon  *	Note:  This routine may not block (it could in the old swap code),
7801c7c3c6aSMatthew Dillon  *	and through the use of the new blist routines it does not block.
7811c7c3c6aSMatthew Dillon  *
7821c7c3c6aSMatthew Dillon  *	This routine may not block
78326f9a767SRodney W. Grimes  */
784a5edd34aSPoul-Henning Kamp static void
7858f60c087SPoul-Henning Kamp swp_pager_freeswapspace(daddr_t blk, int npages)
7860d94caffSDavid Greenman {
7878f60c087SPoul-Henning Kamp 	struct swdevt *sp;
78892da00bbSMatthew Dillon 
7897645e885SAlan Cox 	mtx_lock(&sw_dev_mtx);
7907645e885SAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7917645e885SAlan Cox 		if (blk >= sp->sw_first && blk < sp->sw_end) {
79292da00bbSMatthew Dillon 			sp->sw_used -= npages;
79392da00bbSMatthew Dillon 			/*
7947645e885SAlan Cox 			 * If we are attempting to stop swapping on
7957645e885SAlan Cox 			 * this device, we don't want to mark any
7967645e885SAlan Cox 			 * blocks free lest they be reused.
79792da00bbSMatthew Dillon 			 */
7987645e885SAlan Cox 			if ((sp->sw_flags & SW_CLOSING) == 0) {
7997645e885SAlan Cox 				blist_free(sp->sw_blist, blk - sp->sw_first,
8007645e885SAlan Cox 				    npages);
8018f60c087SPoul-Henning Kamp 				swap_pager_avail += npages;
8021c7c3c6aSMatthew Dillon 				swp_sizecheck();
80326f9a767SRodney W. Grimes 			}
8047645e885SAlan Cox 			mtx_unlock(&sw_dev_mtx);
8057645e885SAlan Cox 			return;
8067645e885SAlan Cox 		}
8077645e885SAlan Cox 	}
8087645e885SAlan Cox 	panic("Swapdev not found");
8097645e885SAlan Cox }
8101c7c3c6aSMatthew Dillon 
81126f9a767SRodney W. Grimes /*
8121c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
8131c7c3c6aSMatthew Dillon  *				range within an object.
8141c7c3c6aSMatthew Dillon  *
8151c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
8161c7c3c6aSMatthew Dillon  *
8171c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
8181c7c3c6aSMatthew Dillon  *
8191c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
8201c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
8211c7c3c6aSMatthew Dillon  *	we should be ok.
82226f9a767SRodney W. Grimes  */
82326f9a767SRodney W. Grimes void
8242f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
82526f9a767SRodney W. Grimes {
82623955314SAlfred Perlstein 
82719ba4c8eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
8281c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
8294dcc5c2dSMatthew Dillon }
8304dcc5c2dSMatthew Dillon 
8314dcc5c2dSMatthew Dillon /*
8324dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
8334dcc5c2dSMatthew Dillon  *
8344dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
8354dcc5c2dSMatthew Dillon  *	swap blocks are not zerod.  Any previous swap assignment is destroyed.
8364dcc5c2dSMatthew Dillon  *
8374dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
8384dcc5c2dSMatthew Dillon  */
8394dcc5c2dSMatthew Dillon int
8404dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
8414dcc5c2dSMatthew Dillon {
8424dcc5c2dSMatthew Dillon 	int n = 0;
8434dcc5c2dSMatthew Dillon 	daddr_t blk = SWAPBLK_NONE;
8444dcc5c2dSMatthew Dillon 	vm_pindex_t beg = start;	/* save start index */
8454dcc5c2dSMatthew Dillon 
846ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK(object);
8474dcc5c2dSMatthew Dillon 	while (size) {
8484dcc5c2dSMatthew Dillon 		if (n == 0) {
8494dcc5c2dSMatthew Dillon 			n = BLIST_MAX_ALLOC;
8504dcc5c2dSMatthew Dillon 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
8514dcc5c2dSMatthew Dillon 				n >>= 1;
8524dcc5c2dSMatthew Dillon 				if (n == 0) {
8534dcc5c2dSMatthew Dillon 					swp_pager_meta_free(object, beg, start - beg);
854ee3dc7d7SAlan Cox 					VM_OBJECT_UNLOCK(object);
8554dcc5c2dSMatthew Dillon 					return (-1);
8564dcc5c2dSMatthew Dillon 				}
8574dcc5c2dSMatthew Dillon 			}
8584dcc5c2dSMatthew Dillon 		}
8594dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, start, blk);
8604dcc5c2dSMatthew Dillon 		--size;
8614dcc5c2dSMatthew Dillon 		++start;
8624dcc5c2dSMatthew Dillon 		++blk;
8634dcc5c2dSMatthew Dillon 		--n;
8644dcc5c2dSMatthew Dillon 	}
8654dcc5c2dSMatthew Dillon 	swp_pager_meta_free(object, start, n);
866ee3dc7d7SAlan Cox 	VM_OBJECT_UNLOCK(object);
8674dcc5c2dSMatthew Dillon 	return (0);
86826f9a767SRodney W. Grimes }
86926f9a767SRodney W. Grimes 
8700a47b48bSJohn Dyson /*
8711c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
8721c7c3c6aSMatthew Dillon  *			and destroy the source.
8731c7c3c6aSMatthew Dillon  *
8741c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
8751c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
8761c7c3c6aSMatthew Dillon  *	we keep the destination's.
8771c7c3c6aSMatthew Dillon  *
8781c7c3c6aSMatthew Dillon  *	This routine is allowed to block.  It may block allocating metadata
8791c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
8801c7c3c6aSMatthew Dillon  *	progress on the source.
8811c7c3c6aSMatthew Dillon  *
8821c7c3c6aSMatthew Dillon  *	XXX vm_page_collapse() kinda expects us not to block because we
8831c7c3c6aSMatthew Dillon  *	supposedly do not need to allocate memory, but for the moment we
8841c7c3c6aSMatthew Dillon  *	*may* have to get a little memory from the zone allocator, but
8851c7c3c6aSMatthew Dillon  *	it is taken from the interrupt memory.  We should be ok.
8861c7c3c6aSMatthew Dillon  *
8871c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
8881c7c3c6aSMatthew Dillon  *
8891c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
8901c7c3c6aSMatthew Dillon  *
8914dcc5c2dSMatthew Dillon  *	The source and destination objects must be locked or
8924dcc5c2dSMatthew Dillon  *	inaccessible (XXX are they ?)
89326f9a767SRodney W. Grimes  */
89426f9a767SRodney W. Grimes void
8952f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
8962f249180SPoul-Henning Kamp     vm_pindex_t offset, int destroysource)
89726f9a767SRodney W. Grimes {
898a316d390SJohn Dyson 	vm_pindex_t i;
8994dcc5c2dSMatthew Dillon 
900c7c8dd7eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(srcobject, MA_OWNED);
901c7c8dd7eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(dstobject, MA_OWNED);
9020cddd8f0SMatthew Dillon 
90326f9a767SRodney W. Grimes 	/*
9041c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
9051c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
90626f9a767SRodney W. Grimes 	 */
907cbd8ec09SJohn Dyson 	if (destroysource) {
908bd228075SAlan Cox 		if (srcobject->handle != NULL) {
909a9fa2c05SAlfred Perlstein 			mtx_lock(&sw_alloc_mtx);
9101c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
9111c7c3c6aSMatthew Dillon 			    NOBJLIST(srcobject->handle),
9121c7c3c6aSMatthew Dillon 			    srcobject,
9131c7c3c6aSMatthew Dillon 			    pager_object_list
9141c7c3c6aSMatthew Dillon 			);
915a9fa2c05SAlfred Perlstein 			mtx_unlock(&sw_alloc_mtx);
916cbd8ec09SJohn Dyson 		}
917bd228075SAlan Cox 	}
91826f9a767SRodney W. Grimes 
9191c7c3c6aSMatthew Dillon 	/*
9201c7c3c6aSMatthew Dillon 	 * transfer source to destination.
9211c7c3c6aSMatthew Dillon 	 */
9221c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
9231c7c3c6aSMatthew Dillon 		daddr_t dstaddr;
9241c7c3c6aSMatthew Dillon 
9251c7c3c6aSMatthew Dillon 		/*
9261c7c3c6aSMatthew Dillon 		 * Locate (without changing) the swapblk on the destination,
9271c7c3c6aSMatthew Dillon 		 * unless it is invalid in which case free it silently, or
9281c7c3c6aSMatthew Dillon 		 * if the destination is a resident page, in which case the
9291c7c3c6aSMatthew Dillon 		 * source is thrown away.
9301c7c3c6aSMatthew Dillon 		 */
9311c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
9321c7c3c6aSMatthew Dillon 
9331c7c3c6aSMatthew Dillon 		if (dstaddr == SWAPBLK_NONE) {
9341c7c3c6aSMatthew Dillon 			/*
9351c7c3c6aSMatthew Dillon 			 * Destination has no swapblk and is not resident,
9361c7c3c6aSMatthew Dillon 			 * copy source.
9371c7c3c6aSMatthew Dillon 			 */
9381c7c3c6aSMatthew Dillon 			daddr_t srcaddr;
9391c7c3c6aSMatthew Dillon 
9401c7c3c6aSMatthew Dillon 			srcaddr = swp_pager_meta_ctl(
9411c7c3c6aSMatthew Dillon 			    srcobject,
9421c7c3c6aSMatthew Dillon 			    i + offset,
9431c7c3c6aSMatthew Dillon 			    SWM_POP
9441c7c3c6aSMatthew Dillon 			);
9451c7c3c6aSMatthew Dillon 
946ee3dc7d7SAlan Cox 			if (srcaddr != SWAPBLK_NONE) {
947c7c8dd7eSAlan Cox 				/*
948c7c8dd7eSAlan Cox 				 * swp_pager_meta_build() can sleep.
949c7c8dd7eSAlan Cox 				 */
950c7c8dd7eSAlan Cox 				vm_object_pip_add(srcobject, 1);
951c7c8dd7eSAlan Cox 				VM_OBJECT_UNLOCK(srcobject);
952c7c8dd7eSAlan Cox 				vm_object_pip_add(dstobject, 1);
9534dcc5c2dSMatthew Dillon 				swp_pager_meta_build(dstobject, i, srcaddr);
954c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(dstobject);
955c7c8dd7eSAlan Cox 				VM_OBJECT_LOCK(srcobject);
956c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(srcobject);
957ee3dc7d7SAlan Cox 			}
9581c7c3c6aSMatthew Dillon 		} else {
9591c7c3c6aSMatthew Dillon 			/*
9601c7c3c6aSMatthew Dillon 			 * Destination has valid swapblk or it is represented
9611c7c3c6aSMatthew Dillon 			 * by a resident page.  We destroy the sourceblock.
9621c7c3c6aSMatthew Dillon 			 */
9631c7c3c6aSMatthew Dillon 
9641c7c3c6aSMatthew Dillon 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
9651c7c3c6aSMatthew Dillon 		}
96626f9a767SRodney W. Grimes 	}
96726f9a767SRodney W. Grimes 
96826f9a767SRodney W. Grimes 	/*
9691c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
9701c7c3c6aSMatthew Dillon 	 *
9711c7c3c6aSMatthew Dillon 	 * We have to revert the type to OBJT_DEFAULT so we do not accidently
9721c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
97326f9a767SRodney W. Grimes 	 */
974c0877f10SJohn Dyson 	if (destroysource) {
9751c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
9761c7c3c6aSMatthew Dillon 		/*
9771c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
9781c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
979956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
9801c7c3c6aSMatthew Dillon 		 * queues.
9811c7c3c6aSMatthew Dillon 		 */
9821c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
983c0877f10SJohn Dyson 	}
98426f9a767SRodney W. Grimes }
98526f9a767SRodney W. Grimes 
986df8bae1dSRodney W. Grimes /*
9871c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
9881c7c3c6aSMatthew Dillon  *				the requested page.
9891c7c3c6aSMatthew Dillon  *
9901c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
9911c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
9921c7c3c6aSMatthew Dillon  *
9931c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
9941c7c3c6aSMatthew Dillon  *	store exists before and after the requested page within a reasonable
9951c7c3c6aSMatthew Dillon  *	distance.  We do not try to restrict it to the swap device stripe
9961c7c3c6aSMatthew Dillon  *	(that is handled in getpages/putpages).  It probably isn't worth
9971c7c3c6aSMatthew Dillon  *	doing here.
998df8bae1dSRodney W. Grimes  */
9995ea4972cSAlan Cox static boolean_t
10002f249180SPoul-Henning Kamp swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after)
100126f9a767SRodney W. Grimes {
10021c7c3c6aSMatthew Dillon 	daddr_t blk0;
100326f9a767SRodney W. Grimes 
1004ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
10051c7c3c6aSMatthew Dillon 	/*
10061c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
10071c7c3c6aSMatthew Dillon 	 */
10081c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
10091c7c3c6aSMatthew Dillon 
10104dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
10111c7c3c6aSMatthew Dillon 		if (before)
101224a1cce3SDavid Greenman 			*before = 0;
10131c7c3c6aSMatthew Dillon 		if (after)
101424a1cce3SDavid Greenman 			*after = 0;
101526f9a767SRodney W. Grimes 		return (FALSE);
101626f9a767SRodney W. Grimes 	}
101726f9a767SRodney W. Grimes 
101826f9a767SRodney W. Grimes 	/*
10191c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
1020e47ed70bSJohn Dyson 	 */
10211c7c3c6aSMatthew Dillon 	if (before != NULL) {
102226f9a767SRodney W. Grimes 		int i;
10230d94caffSDavid Greenman 
10241c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
10251c7c3c6aSMatthew Dillon 			daddr_t blk;
10261c7c3c6aSMatthew Dillon 
10271c7c3c6aSMatthew Dillon 			if (i > pindex)
10281c7c3c6aSMatthew Dillon 				break;
10291c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
10301c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
10311c7c3c6aSMatthew Dillon 				break;
1032ffc82b0aSJohn Dyson 		}
10331c7c3c6aSMatthew Dillon 		*before = (i - 1);
103426f9a767SRodney W. Grimes 	}
103526f9a767SRodney W. Grimes 
103626f9a767SRodney W. Grimes 	/*
10371c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
103826f9a767SRodney W. Grimes 	 */
10391c7c3c6aSMatthew Dillon 	if (after != NULL) {
10401c7c3c6aSMatthew Dillon 		int i;
10411c7c3c6aSMatthew Dillon 
10421c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
10431c7c3c6aSMatthew Dillon 			daddr_t blk;
10441c7c3c6aSMatthew Dillon 
10451c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
10461c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
10471c7c3c6aSMatthew Dillon 				break;
104826f9a767SRodney W. Grimes 		}
10491c7c3c6aSMatthew Dillon 		*after = (i - 1);
10501c7c3c6aSMatthew Dillon 	}
10511c7c3c6aSMatthew Dillon 	return (TRUE);
10521c7c3c6aSMatthew Dillon }
10531c7c3c6aSMatthew Dillon 
10541c7c3c6aSMatthew Dillon /*
10551c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
10561c7c3c6aSMatthew Dillon  *
10571c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
10581c7c3c6aSMatthew Dillon  *	not, from the page.
10591c7c3c6aSMatthew Dillon  *
10601c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
10611c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
10621c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
10631c7c3c6aSMatthew Dillon  *
10641c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
10651c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
10661c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
10671c7c3c6aSMatthew Dillon  *	depends on it.
10681c7c3c6aSMatthew Dillon  *
10691c7c3c6aSMatthew Dillon  *	This routine may not block
10701c7c3c6aSMatthew Dillon  */
10711c7c3c6aSMatthew Dillon static void
10722f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m)
10731c7c3c6aSMatthew Dillon {
10742f249180SPoul-Henning Kamp 
1075ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
10761c7c3c6aSMatthew Dillon 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
10771c7c3c6aSMatthew Dillon }
10781c7c3c6aSMatthew Dillon 
10791c7c3c6aSMatthew Dillon /*
10801c7c3c6aSMatthew Dillon  * SWAP_PAGER_GETPAGES() - bring pages in from swap
10811c7c3c6aSMatthew Dillon  *
10821c7c3c6aSMatthew Dillon  *	Attempt to retrieve (m, count) pages from backing store, but make
10831c7c3c6aSMatthew Dillon  *	sure we retrieve at least m[reqpage].  We try to load in as large
10841c7c3c6aSMatthew Dillon  *	a chunk surrounding m[reqpage] as is contiguous in swap and which
10851c7c3c6aSMatthew Dillon  *	belongs to the same object.
10861c7c3c6aSMatthew Dillon  *
10871c7c3c6aSMatthew Dillon  *	The code is designed for asynchronous operation and
10881c7c3c6aSMatthew Dillon  *	immediate-notification of 'reqpage' but tends not to be
10891c7c3c6aSMatthew Dillon  *	used that way.  Please do not optimize-out this algorithmic
10901c7c3c6aSMatthew Dillon  *	feature, I intend to improve on it in the future.
10911c7c3c6aSMatthew Dillon  *
10921c7c3c6aSMatthew Dillon  *	The parent has a single vm_object_pip_add() reference prior to
10931c7c3c6aSMatthew Dillon  *	calling us and we should return with the same.
10941c7c3c6aSMatthew Dillon  *
10951c7c3c6aSMatthew Dillon  *	The parent has BUSY'd the pages.  We should return with 'm'
10961c7c3c6aSMatthew Dillon  *	left busy, but the others adjusted.
10971c7c3c6aSMatthew Dillon  */
1098f708ef1bSPoul-Henning Kamp static int
10992f249180SPoul-Henning Kamp swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
1100df8bae1dSRodney W. Grimes {
11011c7c3c6aSMatthew Dillon 	struct buf *bp;
11021c7c3c6aSMatthew Dillon 	vm_page_t mreq;
110326f9a767SRodney W. Grimes 	int i;
110426f9a767SRodney W. Grimes 	int j;
11051c7c3c6aSMatthew Dillon 	daddr_t blk;
11060d94caffSDavid Greenman 
11071c7c3c6aSMatthew Dillon 	mreq = m[reqpage];
11081c7c3c6aSMatthew Dillon 
1109e04e4bacSPoul-Henning Kamp 	KASSERT(mreq->object == object,
1110e04e4bacSPoul-Henning Kamp 	    ("swap_pager_getpages: object mismatch %p/%p",
1111e04e4bacSPoul-Henning Kamp 	    object, mreq->object));
1112e04e4bacSPoul-Henning Kamp 
11131c7c3c6aSMatthew Dillon 	/*
11141c7c3c6aSMatthew Dillon 	 * Calculate range to retrieve.  The pages have already been assigned
1115751221fdSPoul-Henning Kamp 	 * their swapblks.  We require a *contiguous* range but we know it to
1116751221fdSPoul-Henning Kamp 	 * not span devices.   If we do not supply it, bad things
11174dcc5c2dSMatthew Dillon 	 * happen.  Note that blk, iblk & jblk can be SWAPBLK_NONE, but the
11184dcc5c2dSMatthew Dillon 	 * loops are set up such that the case(s) are handled implicitly.
11194dcc5c2dSMatthew Dillon 	 *
11200b6ace47SAlan Cox 	 * The swp_*() calls must be made with the object locked.
11211c7c3c6aSMatthew Dillon 	 */
11221c7c3c6aSMatthew Dillon 	blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0);
11231c7c3c6aSMatthew Dillon 
11241c7c3c6aSMatthew Dillon 	for (i = reqpage - 1; i >= 0; --i) {
11251c7c3c6aSMatthew Dillon 		daddr_t iblk;
11261c7c3c6aSMatthew Dillon 
11271c7c3c6aSMatthew Dillon 		iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0);
11281c7c3c6aSMatthew Dillon 		if (blk != iblk + (reqpage - i))
112926f9a767SRodney W. Grimes 			break;
113026f9a767SRodney W. Grimes 	}
11311c7c3c6aSMatthew Dillon 	++i;
11321c7c3c6aSMatthew Dillon 
11331c7c3c6aSMatthew Dillon 	for (j = reqpage + 1; j < count; ++j) {
11341c7c3c6aSMatthew Dillon 		daddr_t jblk;
11351c7c3c6aSMatthew Dillon 
11361c7c3c6aSMatthew Dillon 		jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0);
11371c7c3c6aSMatthew Dillon 		if (blk != jblk - (j - reqpage))
11381c7c3c6aSMatthew Dillon 			break;
11391c7c3c6aSMatthew Dillon 	}
11401c7c3c6aSMatthew Dillon 
11411c7c3c6aSMatthew Dillon 	/*
11421c7c3c6aSMatthew Dillon 	 * free pages outside our collection range.   Note: we never free
11431c7c3c6aSMatthew Dillon 	 * mreq, it must remain busy throughout.
11441c7c3c6aSMatthew Dillon 	 */
1145071a1710SAlan Cox 	if (0 < i || j < count) {
11461c7c3c6aSMatthew Dillon 		int k;
11471c7c3c6aSMatthew Dillon 
11483c4a2440SAlan Cox 		for (k = 0; k < i; ++k)
1149e86a87e9SKonstantin Belousov 			swp_pager_free_nrpage(m[k]);
11503c4a2440SAlan Cox 		for (k = j; k < count; ++k)
11512965a453SKip Macy 			swp_pager_free_nrpage(m[k]);
1152071a1710SAlan Cox 	}
11531c7c3c6aSMatthew Dillon 
11541c7c3c6aSMatthew Dillon 	/*
11554dcc5c2dSMatthew Dillon 	 * Return VM_PAGER_FAIL if we have nothing to do.  Return mreq
11564dcc5c2dSMatthew Dillon 	 * still busy, but the others unbusied.
11571c7c3c6aSMatthew Dillon 	 */
11584dcc5c2dSMatthew Dillon 	if (blk == SWAPBLK_NONE)
115926f9a767SRodney W. Grimes 		return (VM_PAGER_FAIL);
1160df8bae1dSRodney W. Grimes 
116116f62314SDavid Greenman 	/*
11628630c117SAlan Cox 	 * Getpbuf() can sleep.
11638630c117SAlan Cox 	 */
11648630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
11658630c117SAlan Cox 	/*
116616f62314SDavid Greenman 	 * Get a swap buffer header to perform the IO
116716f62314SDavid Greenman 	 */
11681c7c3c6aSMatthew Dillon 	bp = getpbuf(&nsw_rcount);
11695e04322aSPoul-Henning Kamp 	bp->b_flags |= B_PAGING;
117026f9a767SRodney W. Grimes 
117116f62314SDavid Greenman 	/*
117216f62314SDavid Greenman 	 * map our page(s) into kva for input
117316f62314SDavid Greenman 	 */
1174d68d828bSAlan Cox 	pmap_qenter((vm_offset_t)bp->b_data, m + i, j - i);
11751c7c3c6aSMatthew Dillon 
117621144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
11771c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1178fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1179fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
11801c7c3c6aSMatthew Dillon 	bp->b_blkno = blk - (reqpage - i);
11811c7c3c6aSMatthew Dillon 	bp->b_bcount = PAGE_SIZE * (j - i);
11821c7c3c6aSMatthew Dillon 	bp->b_bufsize = PAGE_SIZE * (j - i);
11831c7c3c6aSMatthew Dillon 	bp->b_pager.pg_reqpage = reqpage - i;
11841c7c3c6aSMatthew Dillon 
11858630c117SAlan Cox 	VM_OBJECT_LOCK(object);
11861c7c3c6aSMatthew Dillon 	{
11871c7c3c6aSMatthew Dillon 		int k;
11881c7c3c6aSMatthew Dillon 
11891c7c3c6aSMatthew Dillon 		for (k = i; k < j; ++k) {
11901c7c3c6aSMatthew Dillon 			bp->b_pages[k - i] = m[k];
11915786be7cSAlan Cox 			m[k]->oflags |= VPO_SWAPINPROG;
11921c7c3c6aSMatthew Dillon 		}
11931c7c3c6aSMatthew Dillon 	}
11941c7c3c6aSMatthew Dillon 	bp->b_npages = j - i;
119526f9a767SRodney W. Grimes 
1196b4b70819SAttilio Rao 	PCPU_INC(cnt.v_swapin);
1197b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_swappgsin, bp->b_npages);
11981c7c3c6aSMatthew Dillon 
1199df8bae1dSRodney W. Grimes 	/*
12001c7c3c6aSMatthew Dillon 	 * We still hold the lock on mreq, and our automatic completion routine
12011c7c3c6aSMatthew Dillon 	 * does not remove it.
1202df8bae1dSRodney W. Grimes 	 */
1203071a1710SAlan Cox 	vm_object_pip_add(object, bp->b_npages);
1204071a1710SAlan Cox 	VM_OBJECT_UNLOCK(object);
12051c7c3c6aSMatthew Dillon 
12061c7c3c6aSMatthew Dillon 	/*
12071c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
12081c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
12091c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
12101c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
12111c7c3c6aSMatthew Dillon 	 *
12121c7c3c6aSMatthew Dillon 	 * The other pages in our m[] array are also released on completion,
12131c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
12141c7c3c6aSMatthew Dillon 	 *
1215c37a77eeSPoul-Henning Kamp 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
12161c7c3c6aSMatthew Dillon 	 */
1217b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
12184b03903aSPoul-Henning Kamp 	swp_pager_strategy(bp);
121926f9a767SRodney W. Grimes 
122026f9a767SRodney W. Grimes 	/*
12215786be7cSAlan Cox 	 * wait for the page we want to complete.  VPO_SWAPINPROG is always
12221c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
12231c7c3c6aSMatthew Dillon 	 * is set in the meta-data.
122426f9a767SRodney W. Grimes 	 */
122591449ce9SAlan Cox 	VM_OBJECT_LOCK(object);
12265786be7cSAlan Cox 	while ((mreq->oflags & VPO_SWAPINPROG) != 0) {
12275786be7cSAlan Cox 		mreq->oflags |= VPO_WANTED;
1228b4b70819SAttilio Rao 		PCPU_INC(cnt.v_intrans);
122991449ce9SAlan Cox 		if (msleep(mreq, VM_OBJECT_MTX(object), PSWP, "swread", hz*20)) {
12309bd86a98SBruce M Simpson 			printf(
1231c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1232c5690651SPoul-Henning Kamp 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
12331c7c3c6aSMatthew Dillon 		}
12341b119d9dSDavid Greenman 	}
123526f9a767SRodney W. Grimes 
123626f9a767SRodney W. Grimes 	/*
1237a1287949SEivind Eklund 	 * mreq is left busied after completion, but all the other pages
12381c7c3c6aSMatthew Dillon 	 * are freed.  If we had an unrecoverable read error the page will
12391c7c3c6aSMatthew Dillon 	 * not be valid.
124026f9a767SRodney W. Grimes 	 */
12411c7c3c6aSMatthew Dillon 	if (mreq->valid != VM_PAGE_BITS_ALL) {
12421c7c3c6aSMatthew Dillon 		return (VM_PAGER_ERROR);
124326f9a767SRodney W. Grimes 	} else {
12441c7c3c6aSMatthew Dillon 		return (VM_PAGER_OK);
124526f9a767SRodney W. Grimes 	}
12461c7c3c6aSMatthew Dillon 
12471c7c3c6aSMatthew Dillon 	/*
12481c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
12491c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
12501c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
12511c7c3c6aSMatthew Dillon 	 * to all-zero's later.
12521c7c3c6aSMatthew Dillon 	 */
1253df8bae1dSRodney W. Grimes }
1254df8bae1dSRodney W. Grimes 
12551c7c3c6aSMatthew Dillon /*
12561c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
12571c7c3c6aSMatthew Dillon  *
12581c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
12591c7c3c6aSMatthew Dillon  *
12601c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
12611c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
12621c7c3c6aSMatthew Dillon  *
1263ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
12641c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
12651c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
12661c7c3c6aSMatthew Dillon  *	which needs work.
12671c7c3c6aSMatthew Dillon  *
12681c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
12691c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
12701c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
12711c7c3c6aSMatthew Dillon  *	completion.
12721c7c3c6aSMatthew Dillon  *
12731c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
12741c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
12751c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
12761c7c3c6aSMatthew Dillon  */
1277e4542174SMatthew Dillon void
12782f249180SPoul-Henning Kamp swap_pager_putpages(vm_object_t object, vm_page_t *m, int count,
12792f249180SPoul-Henning Kamp     boolean_t sync, int *rtvals)
1280df8bae1dSRodney W. Grimes {
12811c7c3c6aSMatthew Dillon 	int i;
12821c7c3c6aSMatthew Dillon 	int n = 0;
1283df8bae1dSRodney W. Grimes 
12841c7c3c6aSMatthew Dillon 	if (count && m[0]->object != object) {
12857036145bSMaxim Konovalov 		panic("swap_pager_putpages: object mismatch %p/%p",
12861c7c3c6aSMatthew Dillon 		    object,
12871c7c3c6aSMatthew Dillon 		    m[0]->object
12881c7c3c6aSMatthew Dillon 		);
12891c7c3c6aSMatthew Dillon 	}
1290ee3dc7d7SAlan Cox 
12911c7c3c6aSMatthew Dillon 	/*
12921c7c3c6aSMatthew Dillon 	 * Step 1
12931c7c3c6aSMatthew Dillon 	 *
12941c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
12951c7c3c6aSMatthew Dillon 	 * check for bogus sysops
12961c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
12971c7c3c6aSMatthew Dillon 	 */
12984dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
12994dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
1300ee3dc7d7SAlan Cox 	VM_OBJECT_UNLOCK(object);
1301e47ed70bSJohn Dyson 
1302e47ed70bSJohn Dyson 	if (curproc != pageproc)
1303e47ed70bSJohn Dyson 		sync = TRUE;
130426f9a767SRodney W. Grimes 
13051c7c3c6aSMatthew Dillon 	/*
13061c7c3c6aSMatthew Dillon 	 * Step 2
13071c7c3c6aSMatthew Dillon 	 *
1308ad3cce20SMatthew Dillon 	 * Update nsw parameters from swap_async_max sysctl values.
1309ad3cce20SMatthew Dillon 	 * Do not let the sysop crash the machine with bogus numbers.
1310327f4e83SMatthew Dillon 	 */
13116d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
1312327f4e83SMatthew Dillon 	if (swap_async_max != nsw_wcount_async_max) {
1313327f4e83SMatthew Dillon 		int n;
1314327f4e83SMatthew Dillon 
1315327f4e83SMatthew Dillon 		/*
1316327f4e83SMatthew Dillon 		 * limit range
1317327f4e83SMatthew Dillon 		 */
1318327f4e83SMatthew Dillon 		if ((n = swap_async_max) > nswbuf / 2)
1319327f4e83SMatthew Dillon 			n = nswbuf / 2;
1320327f4e83SMatthew Dillon 		if (n < 1)
1321327f4e83SMatthew Dillon 			n = 1;
1322327f4e83SMatthew Dillon 		swap_async_max = n;
1323327f4e83SMatthew Dillon 
1324327f4e83SMatthew Dillon 		/*
1325327f4e83SMatthew Dillon 		 * Adjust difference ( if possible ).  If the current async
1326327f4e83SMatthew Dillon 		 * count is too low, we may not be able to make the adjustment
1327327f4e83SMatthew Dillon 		 * at this time.
1328327f4e83SMatthew Dillon 		 */
1329327f4e83SMatthew Dillon 		n -= nsw_wcount_async_max;
1330327f4e83SMatthew Dillon 		if (nsw_wcount_async + n >= 0) {
1331327f4e83SMatthew Dillon 			nsw_wcount_async += n;
1332327f4e83SMatthew Dillon 			nsw_wcount_async_max += n;
1333327f4e83SMatthew Dillon 			wakeup(&nsw_wcount_async);
1334327f4e83SMatthew Dillon 		}
1335327f4e83SMatthew Dillon 	}
13366d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
1337327f4e83SMatthew Dillon 
1338327f4e83SMatthew Dillon 	/*
1339327f4e83SMatthew Dillon 	 * Step 3
1340327f4e83SMatthew Dillon 	 *
13411c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
13421c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
13431c7c3c6aSMatthew Dillon 	 * successfully.
13441c7c3c6aSMatthew Dillon 	 */
13451c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
13461c7c3c6aSMatthew Dillon 		int j;
13471c7c3c6aSMatthew Dillon 		struct buf *bp;
1348a316d390SJohn Dyson 		daddr_t blk;
134926f9a767SRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 		/*
13511c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1352df8bae1dSRodney W. Grimes 		 */
13531c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1354327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
13551c7c3c6aSMatthew Dillon 
135626f9a767SRodney W. Grimes 		/*
13571c7c3c6aSMatthew Dillon 		 * Get biggest block of swap we can.  If we fail, fall
13581c7c3c6aSMatthew Dillon 		 * back and try to allocate a smaller block.  Don't go
13591c7c3c6aSMatthew Dillon 		 * overboard trying to allocate space if it would overly
13601c7c3c6aSMatthew Dillon 		 * fragment swap.
136126f9a767SRodney W. Grimes 		 */
13621c7c3c6aSMatthew Dillon 		while (
13631c7c3c6aSMatthew Dillon 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
13641c7c3c6aSMatthew Dillon 		    n > 4
13651c7c3c6aSMatthew Dillon 		) {
13661c7c3c6aSMatthew Dillon 			n >>= 1;
136726f9a767SRodney W. Grimes 		}
13681c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
13694dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
13701c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
13711c7c3c6aSMatthew Dillon 			continue;
137226f9a767SRodney W. Grimes 		}
137326f9a767SRodney W. Grimes 
137426f9a767SRodney W. Grimes 		/*
13751c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
13761c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
137726f9a767SRodney W. Grimes 		 */
1378327f4e83SMatthew Dillon 		if (sync == TRUE) {
1379327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_sync);
1380327f4e83SMatthew Dillon 		} else {
1381327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_async);
138221144e3bSPoul-Henning Kamp 			bp->b_flags = B_ASYNC;
1383327f4e83SMatthew Dillon 		}
13845e04322aSPoul-Henning Kamp 		bp->b_flags |= B_PAGING;
1385912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
138626f9a767SRodney W. Grimes 
13871c7c3c6aSMatthew Dillon 		pmap_qenter((vm_offset_t)bp->b_data, &m[i], n);
13881c7c3c6aSMatthew Dillon 
1389fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1390fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
13911c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
13921c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
13931c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1394e47ed70bSJohn Dyson 
1395ee3dc7d7SAlan Cox 		VM_OBJECT_LOCK(object);
13961c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
13971c7c3c6aSMatthew Dillon 			vm_page_t mreq = m[i+j];
13981c7c3c6aSMatthew Dillon 
13991c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
14001c7c3c6aSMatthew Dillon 			    mreq->object,
14011c7c3c6aSMatthew Dillon 			    mreq->pindex,
14024dcc5c2dSMatthew Dillon 			    blk + j
14031c7c3c6aSMatthew Dillon 			);
14047dbf82dcSMatthew Dillon 			vm_page_dirty(mreq);
14051c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_OK;
14061c7c3c6aSMatthew Dillon 
14075786be7cSAlan Cox 			mreq->oflags |= VPO_SWAPINPROG;
14081c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
14091c7c3c6aSMatthew Dillon 		}
1410ee3dc7d7SAlan Cox 		VM_OBJECT_UNLOCK(object);
14111c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1412a5296b05SJulian Elischer 		/*
1413a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1414a5296b05SJulian Elischer 		 */
1415a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1416a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
14171c7c3c6aSMatthew Dillon 
1418b4b70819SAttilio Rao 		PCPU_INC(cnt.v_swapout);
1419b4b70819SAttilio Rao 		PCPU_ADD(cnt.v_swappgsout, bp->b_npages);
142026f9a767SRodney W. Grimes 
142126f9a767SRodney W. Grimes 		/*
14221c7c3c6aSMatthew Dillon 		 * asynchronous
14231c7c3c6aSMatthew Dillon 		 *
1424c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
142526f9a767SRodney W. Grimes 		 */
14261c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
14271c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
142867812eacSKirk McKusick 			BUF_KERNPROC(bp);
14294b03903aSPoul-Henning Kamp 			swp_pager_strategy(bp);
14301c7c3c6aSMatthew Dillon 
14311c7c3c6aSMatthew Dillon 			for (j = 0; j < n; ++j)
14321c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_PEND;
143323955314SAlfred Perlstein 			/* restart outter loop */
14341c7c3c6aSMatthew Dillon 			continue;
143526f9a767SRodney W. Grimes 		}
1436e47ed70bSJohn Dyson 
143726f9a767SRodney W. Grimes 		/*
14381c7c3c6aSMatthew Dillon 		 * synchronous
14391c7c3c6aSMatthew Dillon 		 *
1440c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
14411c7c3c6aSMatthew Dillon 		 */
14422c840b1fSAlan Cox 		bp->b_iodone = bdone;
14434b03903aSPoul-Henning Kamp 		swp_pager_strategy(bp);
14441c7c3c6aSMatthew Dillon 
14451c7c3c6aSMatthew Dillon 		/*
14461c7c3c6aSMatthew Dillon 		 * Wait for the sync I/O to complete, then update rtvals.
14471c7c3c6aSMatthew Dillon 		 * We just set the rtvals[] to VM_PAGER_PEND so we can call
14481c7c3c6aSMatthew Dillon 		 * our async completion routine at the end, thus avoiding a
14491c7c3c6aSMatthew Dillon 		 * double-free.
145026f9a767SRodney W. Grimes 		 */
14512c840b1fSAlan Cox 		bwait(bp, PVM, "swwrt");
14521c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j)
14531c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_PEND;
14541c7c3c6aSMatthew Dillon 		/*
14551c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
14561c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
14571c7c3c6aSMatthew Dillon 		 */
14581c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
14591c7c3c6aSMatthew Dillon 	}
14602e3b314dSAlan Cox 	VM_OBJECT_LOCK(object);
14611c7c3c6aSMatthew Dillon }
14621c7c3c6aSMatthew Dillon 
14631c7c3c6aSMatthew Dillon /*
14641c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
14651c7c3c6aSMatthew Dillon  *
14661c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
14671c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
14681c7c3c6aSMatthew Dillon  *
146955144670SAndriy Gapon  *	For READ operations, the pages are VPO_BUSY'd.  For WRITE operations,
147055144670SAndriy Gapon  *	the pages are vm_page_t->busy'd.  For READ operations, we VPO_BUSY
14711c7c3c6aSMatthew Dillon  *	unbusy all pages except the 'main' request page.  For WRITE
14721c7c3c6aSMatthew Dillon  *	operations, we vm_page_t->busy'd unbusy all pages ( we can do this
14731c7c3c6aSMatthew Dillon  *	because we marked them all VM_PAGER_PEND on return from putpages ).
14741c7c3c6aSMatthew Dillon  *
14751c7c3c6aSMatthew Dillon  *	This routine may not block.
14761c7c3c6aSMatthew Dillon  */
14771c7c3c6aSMatthew Dillon static void
14782f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp)
14791c7c3c6aSMatthew Dillon {
14801c7c3c6aSMatthew Dillon 	int i;
14811c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
14821c7c3c6aSMatthew Dillon 
14831c7c3c6aSMatthew Dillon 	/*
14841c7c3c6aSMatthew Dillon 	 * report error
14851c7c3c6aSMatthew Dillon 	 */
1486c244d2deSPoul-Henning Kamp 	if (bp->b_ioflags & BIO_ERROR) {
14871c7c3c6aSMatthew Dillon 		printf(
14881c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
14891c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
149021144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
14911c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
14921c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
14931c7c3c6aSMatthew Dillon 		    bp->b_error
14941c7c3c6aSMatthew Dillon 		);
14951c7c3c6aSMatthew Dillon 	}
14961c7c3c6aSMatthew Dillon 
14971c7c3c6aSMatthew Dillon 	/*
149826f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
149926f9a767SRodney W. Grimes 	 */
15001c7c3c6aSMatthew Dillon 	pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
150126f9a767SRodney W. Grimes 
150233a609ecSAlan Cox 	if (bp->b_npages) {
150333a609ecSAlan Cox 		object = bp->b_pages[0]->object;
150433a609ecSAlan Cox 		VM_OBJECT_LOCK(object);
150533a609ecSAlan Cox 	}
15062965a453SKip Macy 
150726f9a767SRodney W. Grimes 	/*
15081c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
15091c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
15101c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
15111c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
15121c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
15131c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
151426f9a767SRodney W. Grimes 	 */
15151c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
15161c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1517e47ed70bSJohn Dyson 
15185786be7cSAlan Cox 		m->oflags &= ~VPO_SWAPINPROG;
1519e47ed70bSJohn Dyson 
1520c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1521ffc82b0aSJohn Dyson 			/*
15221c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
15231c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
15241c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
15251c7c3c6aSMatthew Dillon 			 * interrupt.
1526ffc82b0aSJohn Dyson 			 */
152721144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
15281c7c3c6aSMatthew Dillon 				/*
15291c7c3c6aSMatthew Dillon 				 * When reading, reqpage needs to stay
15301c7c3c6aSMatthew Dillon 				 * locked for the parent, but all other
15311c7c3c6aSMatthew Dillon 				 * pages can be freed.  We still want to
15321c7c3c6aSMatthew Dillon 				 * wakeup the parent waiting on the page,
15331c7c3c6aSMatthew Dillon 				 * though.  ( also: pg_reqpage can be -1 and
15341c7c3c6aSMatthew Dillon 				 * not match anything ).
15351c7c3c6aSMatthew Dillon 				 *
15361c7c3c6aSMatthew Dillon 				 * We have to wake specifically requested pages
15375786be7cSAlan Cox 				 * up too because we cleared VPO_SWAPINPROG and
15381c7c3c6aSMatthew Dillon 				 * someone may be waiting for that.
15391c7c3c6aSMatthew Dillon 				 *
15401c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1541956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15421c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15431c7c3c6aSMatthew Dillon 				 */
15441c7c3c6aSMatthew Dillon 				m->valid = 0;
15451c7c3c6aSMatthew Dillon 				if (i != bp->b_pager.pg_reqpage)
1546e86a87e9SKonstantin Belousov 					swp_pager_free_nrpage(m);
15471c7c3c6aSMatthew Dillon 				else
15481c7c3c6aSMatthew Dillon 					vm_page_flash(m);
15491c7c3c6aSMatthew Dillon 				/*
15501c7c3c6aSMatthew Dillon 				 * If i == bp->b_pager.pg_reqpage, do not wake
15511c7c3c6aSMatthew Dillon 				 * the page up.  The caller needs to.
15521c7c3c6aSMatthew Dillon 				 */
15531c7c3c6aSMatthew Dillon 			} else {
15541c7c3c6aSMatthew Dillon 				/*
15551c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
15561c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
15571c7c3c6aSMatthew Dillon 				 * then finish the I/O.
15581c7c3c6aSMatthew Dillon 				 */
15597dbf82dcSMatthew Dillon 				vm_page_dirty(m);
15603c4a2440SAlan Cox 				vm_page_lock(m);
15611c7c3c6aSMatthew Dillon 				vm_page_activate(m);
15623c4a2440SAlan Cox 				vm_page_unlock(m);
15631c7c3c6aSMatthew Dillon 				vm_page_io_finish(m);
15641c7c3c6aSMatthew Dillon 			}
156521144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
15661c7c3c6aSMatthew Dillon 			/*
15671c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1568956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
15691c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
15701c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
15711c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
15721c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
15731c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
15741c7c3c6aSMatthew Dillon 			 *
15751c7c3c6aSMatthew Dillon 			 * If not the requested page then deactivate it.
15761c7c3c6aSMatthew Dillon 			 *
15771c7c3c6aSMatthew Dillon 			 * Note that the requested page, reqpage, is left
15781c7c3c6aSMatthew Dillon 			 * busied, but we still have to wake it up.  The
15791c7c3c6aSMatthew Dillon 			 * other pages are released (unbusied) by
1580a80982c1SAlan Cox 			 * vm_page_wakeup().
15811c7c3c6aSMatthew Dillon 			 */
1582016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1583016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is mapped", m));
15841c7c3c6aSMatthew Dillon 			m->valid = VM_PAGE_BITS_ALL;
1585016a3c93SAlan Cox 			KASSERT(m->dirty == 0,
1586016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is dirty", m));
15871c7c3c6aSMatthew Dillon 
15881c7c3c6aSMatthew Dillon 			/*
15891c7c3c6aSMatthew Dillon 			 * We have to wake specifically requested pages
15905786be7cSAlan Cox 			 * up too because we cleared VPO_SWAPINPROG and
15911c7c3c6aSMatthew Dillon 			 * could be waiting for it in getpages.  However,
15921c7c3c6aSMatthew Dillon 			 * be sure to not unbusy getpages specifically
15931c7c3c6aSMatthew Dillon 			 * requested page - getpages expects it to be
15941c7c3c6aSMatthew Dillon 			 * left busy.
15951c7c3c6aSMatthew Dillon 			 */
15961c7c3c6aSMatthew Dillon 			if (i != bp->b_pager.pg_reqpage) {
15973c4a2440SAlan Cox 				vm_page_lock(m);
15981c7c3c6aSMatthew Dillon 				vm_page_deactivate(m);
15993c4a2440SAlan Cox 				vm_page_unlock(m);
16001c7c3c6aSMatthew Dillon 				vm_page_wakeup(m);
16013c4a2440SAlan Cox 			} else
16021c7c3c6aSMatthew Dillon 				vm_page_flash(m);
16031c7c3c6aSMatthew Dillon 		} else {
16041c7c3c6aSMatthew Dillon 			/*
1605016a3c93SAlan Cox 			 * For write success, clear the dirty
16061c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
16071c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
16081c7c3c6aSMatthew Dillon 			 */
1609016a3c93SAlan Cox 			KASSERT((m->flags & PG_WRITEABLE) == 0,
1610016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is not write"
1611016a3c93SAlan Cox 			    " protected", m));
1612c52e7044SAlan Cox 			vm_page_undirty(m);
16131c7c3c6aSMatthew Dillon 			vm_page_io_finish(m);
16143c4a2440SAlan Cox 			if (vm_page_count_severe()) {
16153c4a2440SAlan Cox 				vm_page_lock(m);
16162c840b1fSAlan Cox 				vm_page_try_to_cache(m);
16172965a453SKip Macy 				vm_page_unlock(m);
16182965a453SKip Macy 			}
16193c4a2440SAlan Cox 		}
16203c4a2440SAlan Cox 	}
162126f9a767SRodney W. Grimes 
16221c7c3c6aSMatthew Dillon 	/*
16231c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
16241c7c3c6aSMatthew Dillon 	 * pip refs on the object.
16251c7c3c6aSMatthew Dillon 	 */
16260d420ad3SAlan Cox 	if (object != NULL) {
16271c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
16280d420ad3SAlan Cox 		VM_OBJECT_UNLOCK(object);
16290d420ad3SAlan Cox 	}
163026f9a767SRodney W. Grimes 
16311c7c3c6aSMatthew Dillon 	/*
1632100650deSOlivier Houchard 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1633100650deSOlivier Houchard 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1634100650deSOlivier Houchard 	 * trigger a KASSERT in relpbuf().
1635100650deSOlivier Houchard 	 */
1636100650deSOlivier Houchard 	if (bp->b_vp) {
1637100650deSOlivier Houchard 		    bp->b_vp = NULL;
1638100650deSOlivier Houchard 		    bp->b_bufobj = NULL;
1639100650deSOlivier Houchard 	}
1640100650deSOlivier Houchard 	/*
16411c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
16421c7c3c6aSMatthew Dillon 	 */
1643327f4e83SMatthew Dillon 	relpbuf(
1644327f4e83SMatthew Dillon 	    bp,
164521144e3bSPoul-Henning Kamp 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1646327f4e83SMatthew Dillon 		((bp->b_flags & B_ASYNC) ?
1647327f4e83SMatthew Dillon 		    &nsw_wcount_async :
1648327f4e83SMatthew Dillon 		    &nsw_wcount_sync
1649327f4e83SMatthew Dillon 		)
1650327f4e83SMatthew Dillon 	    )
1651327f4e83SMatthew Dillon 	);
165226f9a767SRodney W. Grimes }
16531c7c3c6aSMatthew Dillon 
165492da00bbSMatthew Dillon /*
165592da00bbSMatthew Dillon  *	swap_pager_isswapped:
165692da00bbSMatthew Dillon  *
165792da00bbSMatthew Dillon  *	Return 1 if at least one page in the given object is paged
165892da00bbSMatthew Dillon  *	out to the given swap device.
165992da00bbSMatthew Dillon  *
166092da00bbSMatthew Dillon  *	This routine may not block.
166192da00bbSMatthew Dillon  */
16628f60c087SPoul-Henning Kamp int
16638f60c087SPoul-Henning Kamp swap_pager_isswapped(vm_object_t object, struct swdevt *sp)
16648f60c087SPoul-Henning Kamp {
166592da00bbSMatthew Dillon 	daddr_t index = 0;
166692da00bbSMatthew Dillon 	int bcount;
166792da00bbSMatthew Dillon 	int i;
166892da00bbSMatthew Dillon 
166917cd3642SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1670f6bcadc4SDavid Schultz 	if (object->type != OBJT_SWAP)
1671f6bcadc4SDavid Schultz 		return (0);
1672f6bcadc4SDavid Schultz 
1673b3fed13eSDavid Schultz 	mtx_lock(&swhash_mtx);
167492da00bbSMatthew Dillon 	for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) {
167592da00bbSMatthew Dillon 		struct swblock *swap;
167692da00bbSMatthew Dillon 
167792da00bbSMatthew Dillon 		if ((swap = *swp_pager_hash(object, index)) != NULL) {
167892da00bbSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
1679b3fed13eSDavid Schultz 				if (swp_pager_isondev(swap->swb_pages[i], sp)) {
16807827d9b0SAlan Cox 					mtx_unlock(&swhash_mtx);
1681b3fed13eSDavid Schultz 					return (1);
168292da00bbSMatthew Dillon 				}
168392da00bbSMatthew Dillon 			}
16847827d9b0SAlan Cox 		}
168592da00bbSMatthew Dillon 		index += SWAP_META_PAGES;
168692da00bbSMatthew Dillon 	}
1687b3fed13eSDavid Schultz 	mtx_unlock(&swhash_mtx);
1688b3fed13eSDavid Schultz 	return (0);
168992da00bbSMatthew Dillon }
169092da00bbSMatthew Dillon 
169192da00bbSMatthew Dillon /*
169292da00bbSMatthew Dillon  * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in
169392da00bbSMatthew Dillon  *
169492da00bbSMatthew Dillon  *	This routine dissociates the page at the given index within a
169592da00bbSMatthew Dillon  *	swap block from its backing store, paging it in if necessary.
169692da00bbSMatthew Dillon  *	If the page is paged in, it is placed in the inactive queue,
169792da00bbSMatthew Dillon  *	since it had its backing store ripped out from under it.
169892da00bbSMatthew Dillon  *	We also attempt to swap in all other pages in the swap block,
169992da00bbSMatthew Dillon  *	we only guarantee that the one at the specified index is
170092da00bbSMatthew Dillon  *	paged in.
170192da00bbSMatthew Dillon  *
170292da00bbSMatthew Dillon  *	XXX - The code to page the whole block in doesn't work, so we
170392da00bbSMatthew Dillon  *	      revert to the one-by-one behavior for now.  Sigh.
170492da00bbSMatthew Dillon  */
170562a59e8fSWarner Losh static inline void
1706b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex)
170792da00bbSMatthew Dillon {
170892da00bbSMatthew Dillon 	vm_page_t m;
170992da00bbSMatthew Dillon 
171092da00bbSMatthew Dillon 	vm_object_pip_add(object, 1);
1711b3fed13eSDavid Schultz 	m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
171292da00bbSMatthew Dillon 	if (m->valid == VM_PAGE_BITS_ALL) {
171392da00bbSMatthew Dillon 		vm_object_pip_subtract(object, 1);
171492da00bbSMatthew Dillon 		vm_page_dirty(m);
1715d061cdd5SAlan Cox 		vm_page_lock(m);
1716d061cdd5SAlan Cox 		vm_page_activate(m);
17172965a453SKip Macy 		vm_page_unlock(m);
171866bdd5d6SAlan Cox 		vm_page_wakeup(m);
171992da00bbSMatthew Dillon 		vm_pager_page_unswapped(m);
172092da00bbSMatthew Dillon 		return;
172192da00bbSMatthew Dillon 	}
172292da00bbSMatthew Dillon 
1723b3fed13eSDavid Schultz 	if (swap_pager_getpages(object, &m, 1, 0) != VM_PAGER_OK)
172492da00bbSMatthew Dillon 		panic("swap_pager_force_pagein: read from swap failed");/*XXX*/
172592da00bbSMatthew Dillon 	vm_object_pip_subtract(object, 1);
172692da00bbSMatthew Dillon 	vm_page_dirty(m);
1727db1f085eSAlan Cox 	vm_page_lock(m);
1728db1f085eSAlan Cox 	vm_page_deactivate(m);
17292965a453SKip Macy 	vm_page_unlock(m);
173066bdd5d6SAlan Cox 	vm_page_wakeup(m);
173192da00bbSMatthew Dillon 	vm_pager_page_unswapped(m);
173292da00bbSMatthew Dillon }
173392da00bbSMatthew Dillon 
173492da00bbSMatthew Dillon /*
173592da00bbSMatthew Dillon  *	swap_pager_swapoff:
173692da00bbSMatthew Dillon  *
173792da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
173892da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
173992da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
174092da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
174192da00bbSMatthew Dillon  *
174292da00bbSMatthew Dillon  *	This routine may block.
174392da00bbSMatthew Dillon  */
1744e9c0cc15SPoul-Henning Kamp static void
1745b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp)
174692da00bbSMatthew Dillon {
174792da00bbSMatthew Dillon 	struct swblock *swap;
17488bc61209SDavid Schultz 	int i, j, retries;
174992da00bbSMatthew Dillon 
175092da00bbSMatthew Dillon 	GIANT_REQUIRED;
175192da00bbSMatthew Dillon 
17528bc61209SDavid Schultz 	retries = 0;
175392da00bbSMatthew Dillon full_rescan:
1754b3fed13eSDavid Schultz 	mtx_lock(&swhash_mtx);
175592da00bbSMatthew Dillon 	for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */
175692da00bbSMatthew Dillon restart:
1757b3fed13eSDavid Schultz 		for (swap = swhash[i]; swap != NULL; swap = swap->swb_hnext) {
1758b3fed13eSDavid Schultz 			vm_object_t object = swap->swb_object;
1759b3fed13eSDavid Schultz 			vm_pindex_t pindex = swap->swb_index;
176092da00bbSMatthew Dillon                         for (j = 0; j < SWAP_META_PAGES; ++j) {
1761b3fed13eSDavid Schultz                                 if (swp_pager_isondev(swap->swb_pages[j], sp)) {
1762b3fed13eSDavid Schultz 					/* avoid deadlock */
1763b3fed13eSDavid Schultz 					if (!VM_OBJECT_TRYLOCK(object)) {
176492da00bbSMatthew Dillon 						break;
1765b3fed13eSDavid Schultz 					} else {
1766b3fed13eSDavid Schultz 						mtx_unlock(&swhash_mtx);
1767b3fed13eSDavid Schultz 						swp_pager_force_pagein(object,
1768b3fed13eSDavid Schultz 						    pindex + j);
1769b3fed13eSDavid Schultz 						VM_OBJECT_UNLOCK(object);
1770b3fed13eSDavid Schultz 						mtx_lock(&swhash_mtx);
177192da00bbSMatthew Dillon 						goto restart;
177292da00bbSMatthew Dillon 					}
1773b3fed13eSDavid Schultz 				}
1774b3fed13eSDavid Schultz                         }
1775b3fed13eSDavid Schultz 		}
177692da00bbSMatthew Dillon 	}
1777d536c58fSAlan Cox 	mtx_unlock(&swhash_mtx);
17788bc61209SDavid Schultz 	if (sp->sw_used) {
177992da00bbSMatthew Dillon 		/*
17808bc61209SDavid Schultz 		 * Objects may be locked or paging to the device being
17818bc61209SDavid Schultz 		 * removed, so we will miss their pages and need to
17828bc61209SDavid Schultz 		 * make another pass.  We have marked this device as
17838bc61209SDavid Schultz 		 * SW_CLOSING, so the activity should finish soon.
178492da00bbSMatthew Dillon 		 */
17858bc61209SDavid Schultz 		retries++;
17868bc61209SDavid Schultz 		if (retries > 100) {
17878bc61209SDavid Schultz 			panic("swapoff: failed to locate %d swap blocks",
17888bc61209SDavid Schultz 			    sp->sw_used);
17898bc61209SDavid Schultz 		}
17904d70511aSJohn Baldwin 		pause("swpoff", hz / 20);
179192da00bbSMatthew Dillon 		goto full_rescan;
179292da00bbSMatthew Dillon 	}
179392da00bbSMatthew Dillon }
179492da00bbSMatthew Dillon 
17951c7c3c6aSMatthew Dillon /************************************************************************
17961c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
17971c7c3c6aSMatthew Dillon  ************************************************************************
17981c7c3c6aSMatthew Dillon  *
17991c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
1800cec9f109SDavid E. O'Brien  *	OBJT_SWAP object.
18011c7c3c6aSMatthew Dillon  *
18024dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
18034dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
18044dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
18051c7c3c6aSMatthew Dillon  */
18061c7c3c6aSMatthew Dillon 
18071c7c3c6aSMatthew Dillon /*
18081c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
18091c7c3c6aSMatthew Dillon  *
18101c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
18111c7c3c6aSMatthew Dillon  *	object.
18121c7c3c6aSMatthew Dillon  *
18131c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
18141c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
18151c7c3c6aSMatthew Dillon  *	assigned swapblk is freed.
18161c7c3c6aSMatthew Dillon  */
18171c7c3c6aSMatthew Dillon static void
18182f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
18192f249180SPoul-Henning Kamp {
18201c7c3c6aSMatthew Dillon 	struct swblock *swap;
18211c7c3c6aSMatthew Dillon 	struct swblock **pswap;
182223f09d50SIan Dowse 	int idx;
18231c7c3c6aSMatthew Dillon 
1824ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
18251c7c3c6aSMatthew Dillon 	/*
18261c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
18271c7c3c6aSMatthew Dillon 	 */
18281c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
18291c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
18301c7c3c6aSMatthew Dillon 		object->un_pager.swp.swp_bcount = 0;
18311c7c3c6aSMatthew Dillon 
18321c7c3c6aSMatthew Dillon 		if (object->handle != NULL) {
1833bd228075SAlan Cox 			mtx_lock(&sw_alloc_mtx);
18341c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
18351c7c3c6aSMatthew Dillon 			    NOBJLIST(object->handle),
18361c7c3c6aSMatthew Dillon 			    object,
18371c7c3c6aSMatthew Dillon 			    pager_object_list
18381c7c3c6aSMatthew Dillon 			);
1839a9fa2c05SAlfred Perlstein 			mtx_unlock(&sw_alloc_mtx);
18401c7c3c6aSMatthew Dillon 		}
1841bd228075SAlan Cox 	}
18421c7c3c6aSMatthew Dillon 
18431c7c3c6aSMatthew Dillon 	/*
18441c7c3c6aSMatthew Dillon 	 * Locate hash entry.  If not found create, but if we aren't adding
18454dcc5c2dSMatthew Dillon 	 * anything just return.  If we run out of space in the map we wait
18464dcc5c2dSMatthew Dillon 	 * and, since the hash table may have changed, retry.
18471c7c3c6aSMatthew Dillon 	 */
18484dcc5c2dSMatthew Dillon retry:
18497827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
185023f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
18511c7c3c6aSMatthew Dillon 
18521c7c3c6aSMatthew Dillon 	if ((swap = *pswap) == NULL) {
18531c7c3c6aSMatthew Dillon 		int i;
18541c7c3c6aSMatthew Dillon 
18551c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
18567827d9b0SAlan Cox 			goto done;
18571c7c3c6aSMatthew Dillon 
1858670d17b5SJeff Roberson 		swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT);
18594dcc5c2dSMatthew Dillon 		if (swap == NULL) {
18607827d9b0SAlan Cox 			mtx_unlock(&swhash_mtx);
1861ee3dc7d7SAlan Cox 			VM_OBJECT_UNLOCK(object);
18622025d69bSKonstantin Belousov 			if (uma_zone_exhausted(swap_zone)) {
18632025d69bSKonstantin Belousov 				printf("swap zone exhausted, increase kern.maxswzone\n");
18642025d69bSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
18652025d69bSKonstantin Belousov 				pause("swzonex", 10);
18662025d69bSKonstantin Belousov 			} else
18674dcc5c2dSMatthew Dillon 				VM_WAIT;
1868ee3dc7d7SAlan Cox 			VM_OBJECT_LOCK(object);
18694dcc5c2dSMatthew Dillon 			goto retry;
18704dcc5c2dSMatthew Dillon 		}
1871670d17b5SJeff Roberson 
18721c7c3c6aSMatthew Dillon 		swap->swb_hnext = NULL;
18731c7c3c6aSMatthew Dillon 		swap->swb_object = object;
187423f09d50SIan Dowse 		swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK;
18751c7c3c6aSMatthew Dillon 		swap->swb_count = 0;
18761c7c3c6aSMatthew Dillon 
18771c7c3c6aSMatthew Dillon 		++object->un_pager.swp.swp_bcount;
18781c7c3c6aSMatthew Dillon 
18791c7c3c6aSMatthew Dillon 		for (i = 0; i < SWAP_META_PAGES; ++i)
18801c7c3c6aSMatthew Dillon 			swap->swb_pages[i] = SWAPBLK_NONE;
18811c7c3c6aSMatthew Dillon 	}
18821c7c3c6aSMatthew Dillon 
18831c7c3c6aSMatthew Dillon 	/*
18841c7c3c6aSMatthew Dillon 	 * Delete prior contents of metadata
18851c7c3c6aSMatthew Dillon 	 */
188623f09d50SIan Dowse 	idx = pindex & SWAP_META_MASK;
18871c7c3c6aSMatthew Dillon 
188823f09d50SIan Dowse 	if (swap->swb_pages[idx] != SWAPBLK_NONE) {
188923f09d50SIan Dowse 		swp_pager_freeswapspace(swap->swb_pages[idx], 1);
18901c7c3c6aSMatthew Dillon 		--swap->swb_count;
18911c7c3c6aSMatthew Dillon 	}
18921c7c3c6aSMatthew Dillon 
18931c7c3c6aSMatthew Dillon 	/*
18941c7c3c6aSMatthew Dillon 	 * Enter block into metadata
18951c7c3c6aSMatthew Dillon 	 */
189623f09d50SIan Dowse 	swap->swb_pages[idx] = swapblk;
18974dcc5c2dSMatthew Dillon 	if (swapblk != SWAPBLK_NONE)
18981c7c3c6aSMatthew Dillon 		++swap->swb_count;
18997827d9b0SAlan Cox done:
19007827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
19011c7c3c6aSMatthew Dillon }
19021c7c3c6aSMatthew Dillon 
19031c7c3c6aSMatthew Dillon /*
19041c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
19051c7c3c6aSMatthew Dillon  *
19061c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
19071c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
19081c7c3c6aSMatthew Dillon  *
19091c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
19101c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
19111c7c3c6aSMatthew Dillon  *	with resident pages.
19121c7c3c6aSMatthew Dillon  */
19131c7c3c6aSMatthew Dillon static void
19144dcc5c2dSMatthew Dillon swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count)
19151c7c3c6aSMatthew Dillon {
19162928cef7SAlan Cox 
1917ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
19181c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19191c7c3c6aSMatthew Dillon 		return;
19201c7c3c6aSMatthew Dillon 
19211c7c3c6aSMatthew Dillon 	while (count > 0) {
19221c7c3c6aSMatthew Dillon 		struct swblock **pswap;
19231c7c3c6aSMatthew Dillon 		struct swblock *swap;
19241c7c3c6aSMatthew Dillon 
19257827d9b0SAlan Cox 		mtx_lock(&swhash_mtx);
19261c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
19271c7c3c6aSMatthew Dillon 
19281c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
19291c7c3c6aSMatthew Dillon 			daddr_t v = swap->swb_pages[index & SWAP_META_MASK];
19301c7c3c6aSMatthew Dillon 
19311c7c3c6aSMatthew Dillon 			if (v != SWAPBLK_NONE) {
19321c7c3c6aSMatthew Dillon 				swp_pager_freeswapspace(v, 1);
19331c7c3c6aSMatthew Dillon 				swap->swb_pages[index & SWAP_META_MASK] =
19341c7c3c6aSMatthew Dillon 					SWAPBLK_NONE;
19351c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
19361c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
1937670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
19381c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
19391c7c3c6aSMatthew Dillon 				}
19401c7c3c6aSMatthew Dillon 			}
19411c7c3c6aSMatthew Dillon 			--count;
19421c7c3c6aSMatthew Dillon 			++index;
19431c7c3c6aSMatthew Dillon 		} else {
19444dcc5c2dSMatthew Dillon 			int n = SWAP_META_PAGES - (index & SWAP_META_MASK);
19451c7c3c6aSMatthew Dillon 			count -= n;
19461c7c3c6aSMatthew Dillon 			index += n;
19471c7c3c6aSMatthew Dillon 		}
19487827d9b0SAlan Cox 		mtx_unlock(&swhash_mtx);
19491c7c3c6aSMatthew Dillon 	}
19501c7c3c6aSMatthew Dillon }
19511c7c3c6aSMatthew Dillon 
19521c7c3c6aSMatthew Dillon /*
19531c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
19541c7c3c6aSMatthew Dillon  *
19551c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
19561c7c3c6aSMatthew Dillon  *	an object.
19571c7c3c6aSMatthew Dillon  */
19581c7c3c6aSMatthew Dillon static void
19591c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
19601c7c3c6aSMatthew Dillon {
19611c7c3c6aSMatthew Dillon 	daddr_t index = 0;
19621c7c3c6aSMatthew Dillon 
1963ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
19641c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19651c7c3c6aSMatthew Dillon 		return;
19661c7c3c6aSMatthew Dillon 
19671c7c3c6aSMatthew Dillon 	while (object->un_pager.swp.swp_bcount) {
19681c7c3c6aSMatthew Dillon 		struct swblock **pswap;
19691c7c3c6aSMatthew Dillon 		struct swblock *swap;
19701c7c3c6aSMatthew Dillon 
19717827d9b0SAlan Cox 		mtx_lock(&swhash_mtx);
19721c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
19731c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
19741c7c3c6aSMatthew Dillon 			int i;
19751c7c3c6aSMatthew Dillon 
19761c7c3c6aSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
19771c7c3c6aSMatthew Dillon 				daddr_t v = swap->swb_pages[i];
19781c7c3c6aSMatthew Dillon 				if (v != SWAPBLK_NONE) {
19791c7c3c6aSMatthew Dillon 					--swap->swb_count;
19804dcc5c2dSMatthew Dillon 					swp_pager_freeswapspace(v, 1);
19811c7c3c6aSMatthew Dillon 				}
19821c7c3c6aSMatthew Dillon 			}
19831c7c3c6aSMatthew Dillon 			if (swap->swb_count != 0)
19841c7c3c6aSMatthew Dillon 				panic("swap_pager_meta_free_all: swb_count != 0");
19851c7c3c6aSMatthew Dillon 			*pswap = swap->swb_hnext;
1986670d17b5SJeff Roberson 			uma_zfree(swap_zone, swap);
19871c7c3c6aSMatthew Dillon 			--object->un_pager.swp.swp_bcount;
19881c7c3c6aSMatthew Dillon 		}
19897827d9b0SAlan Cox 		mtx_unlock(&swhash_mtx);
19901c7c3c6aSMatthew Dillon 		index += SWAP_META_PAGES;
19911c7c3c6aSMatthew Dillon 	}
19921c7c3c6aSMatthew Dillon }
19931c7c3c6aSMatthew Dillon 
19941c7c3c6aSMatthew Dillon /*
19951c7c3c6aSMatthew Dillon  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
19961c7c3c6aSMatthew Dillon  *
19971c7c3c6aSMatthew Dillon  *	This routine is capable of looking up, popping, or freeing
19981c7c3c6aSMatthew Dillon  *	swapblk assignments in the swap meta data or in the vm_page_t.
19991c7c3c6aSMatthew Dillon  *	The routine typically returns the swapblk being looked-up, or popped,
20001c7c3c6aSMatthew Dillon  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
20011c7c3c6aSMatthew Dillon  *	was invalid.  This routine will automatically free any invalid
20021c7c3c6aSMatthew Dillon  *	meta-data swapblks.
20031c7c3c6aSMatthew Dillon  *
20041c7c3c6aSMatthew Dillon  *	It is not possible to store invalid swapblks in the swap meta data
20051c7c3c6aSMatthew Dillon  *	(other then a literal 'SWAPBLK_NONE'), so we don't bother checking.
20061c7c3c6aSMatthew Dillon  *
20071c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
20081c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
20091c7c3c6aSMatthew Dillon  *	busy page.
20101c7c3c6aSMatthew Dillon  *
20114dcc5c2dSMatthew Dillon  *	SWM_FREE	remove and free swap block from metadata
20121c7c3c6aSMatthew Dillon  *	SWM_POP		remove from meta data but do not free.. pop it out
20131c7c3c6aSMatthew Dillon  */
20141c7c3c6aSMatthew Dillon static daddr_t
20152f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags)
20162f249180SPoul-Henning Kamp {
20174dcc5c2dSMatthew Dillon 	struct swblock **pswap;
20184dcc5c2dSMatthew Dillon 	struct swblock *swap;
20194dcc5c2dSMatthew Dillon 	daddr_t r1;
202023f09d50SIan Dowse 	int idx;
20214dcc5c2dSMatthew Dillon 
2022c7c8dd7eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
20231c7c3c6aSMatthew Dillon 	/*
20241c7c3c6aSMatthew Dillon 	 * The meta data only exists of the object is OBJT_SWAP
20251c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
20261c7c3c6aSMatthew Dillon 	 */
20274dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
20281c7c3c6aSMatthew Dillon 		return (SWAPBLK_NONE);
20291c7c3c6aSMatthew Dillon 
20304dcc5c2dSMatthew Dillon 	r1 = SWAPBLK_NONE;
20317827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
203223f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
20331c7c3c6aSMatthew Dillon 
20341c7c3c6aSMatthew Dillon 	if ((swap = *pswap) != NULL) {
203523f09d50SIan Dowse 		idx = pindex & SWAP_META_MASK;
203623f09d50SIan Dowse 		r1 = swap->swb_pages[idx];
20371c7c3c6aSMatthew Dillon 
20381c7c3c6aSMatthew Dillon 		if (r1 != SWAPBLK_NONE) {
20391c7c3c6aSMatthew Dillon 			if (flags & SWM_FREE) {
20404dcc5c2dSMatthew Dillon 				swp_pager_freeswapspace(r1, 1);
20411c7c3c6aSMatthew Dillon 				r1 = SWAPBLK_NONE;
20421c7c3c6aSMatthew Dillon 			}
20431c7c3c6aSMatthew Dillon 			if (flags & (SWM_FREE|SWM_POP)) {
204423f09d50SIan Dowse 				swap->swb_pages[idx] = SWAPBLK_NONE;
20451c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
20461c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
2047670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
20481c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
20491c7c3c6aSMatthew Dillon 				}
20501c7c3c6aSMatthew Dillon 			}
20511c7c3c6aSMatthew Dillon 		}
20521c7c3c6aSMatthew Dillon 	}
20537827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
20541c7c3c6aSMatthew Dillon 	return (r1);
20551c7c3c6aSMatthew Dillon }
20561c7c3c6aSMatthew Dillon 
2057e9c0cc15SPoul-Henning Kamp /*
2058e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2059e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2060e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2061e9c0cc15SPoul-Henning Kamp  */
2062e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2063e9c0cc15SPoul-Henning Kamp struct swapon_args {
2064e9c0cc15SPoul-Henning Kamp 	char *name;
2065e9c0cc15SPoul-Henning Kamp };
2066e9c0cc15SPoul-Henning Kamp #endif
2067e9c0cc15SPoul-Henning Kamp 
2068e9c0cc15SPoul-Henning Kamp /*
2069e9c0cc15SPoul-Henning Kamp  * MPSAFE
2070e9c0cc15SPoul-Henning Kamp  */
2071e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2072e9c0cc15SPoul-Henning Kamp int
20732f249180SPoul-Henning Kamp swapon(struct thread *td, struct swapon_args *uap)
2074e9c0cc15SPoul-Henning Kamp {
2075e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2076e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2077e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2078e9c0cc15SPoul-Henning Kamp 	int error;
2079e9c0cc15SPoul-Henning Kamp 
2080acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPON);
2081e9c0cc15SPoul-Henning Kamp 	if (error)
2082acd3428bSRobert Watson 		return (error);
2083e9c0cc15SPoul-Henning Kamp 
2084acd3428bSRobert Watson 	mtx_lock(&Giant);
2085e9c0cc15SPoul-Henning Kamp 	while (swdev_syscall_active)
2086e9c0cc15SPoul-Henning Kamp 	    tsleep(&swdev_syscall_active, PUSER - 1, "swpon", 0);
2087e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 1;
2088e9c0cc15SPoul-Henning Kamp 
2089e9c0cc15SPoul-Henning Kamp 	/*
2090e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2091e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2092e9c0cc15SPoul-Henning Kamp 	 */
2093e9c0cc15SPoul-Henning Kamp 	if (swap_zone == NULL) {
2094e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2095e9c0cc15SPoul-Henning Kamp 		goto done;
2096e9c0cc15SPoul-Henning Kamp 	}
2097e9c0cc15SPoul-Henning Kamp 
2098d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2099d9135e72SRobert Watson 	    uap->name, td);
2100e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2101e9c0cc15SPoul-Henning Kamp 	if (error)
2102e9c0cc15SPoul-Henning Kamp 		goto done;
2103e9c0cc15SPoul-Henning Kamp 
2104e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2105e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2106e9c0cc15SPoul-Henning Kamp 
210720da9c2eSPoul-Henning Kamp 	if (vn_isdisk(vp, &error)) {
2108dee34ca4SPoul-Henning Kamp 		error = swapongeom(td, vp);
210920da9c2eSPoul-Henning Kamp 	} else if (vp->v_type == VREG &&
2110e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
21110359a12eSAttilio Rao 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2112e9c0cc15SPoul-Henning Kamp 		/*
2113e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2114e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2115e9c0cc15SPoul-Henning Kamp 		 */
211659efee01SPoul-Henning Kamp 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2117e9c0cc15SPoul-Henning Kamp 	}
2118e9c0cc15SPoul-Henning Kamp 
2119e9c0cc15SPoul-Henning Kamp 	if (error)
2120e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2121e9c0cc15SPoul-Henning Kamp done:
2122e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 0;
2123e9c0cc15SPoul-Henning Kamp 	wakeup_one(&swdev_syscall_active);
2124e9c0cc15SPoul-Henning Kamp 	mtx_unlock(&Giant);
2125e9c0cc15SPoul-Henning Kamp 	return (error);
2126e9c0cc15SPoul-Henning Kamp }
2127e9c0cc15SPoul-Henning Kamp 
212859efee01SPoul-Henning Kamp static void
2129f3732fd1SPoul-Henning Kamp swaponsomething(struct vnode *vp, void *id, u_long nblks, sw_strategy_t *strategy, sw_close_t *close, dev_t dev)
2130e9c0cc15SPoul-Henning Kamp {
21312d9974c1SAlan Cox 	struct swdevt *sp, *tsp;
2132e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
21338f60c087SPoul-Henning Kamp 	u_long mblocks;
2134e9c0cc15SPoul-Henning Kamp 
2135e9c0cc15SPoul-Henning Kamp 	/*
2136e9c0cc15SPoul-Henning Kamp 	 * If we go beyond this, we get overflows in the radix
2137e9c0cc15SPoul-Henning Kamp 	 * tree bitmap code.
2138e9c0cc15SPoul-Henning Kamp 	 */
21398f60c087SPoul-Henning Kamp 	mblocks = 0x40000000 / BLIST_META_RADIX;
2140d3dd89abSPoul-Henning Kamp 	if (nblks > mblocks) {
214185fdafb9SPoul-Henning Kamp 		printf("WARNING: reducing size to maximum of %lu blocks per swap unit\n",
2142d3dd89abSPoul-Henning Kamp 			mblocks);
2143d3dd89abSPoul-Henning Kamp 		nblks = mblocks;
2144e9c0cc15SPoul-Henning Kamp 	}
2145e9c0cc15SPoul-Henning Kamp 	/*
2146e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2147e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2148e9c0cc15SPoul-Henning Kamp 	 *
2149e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2150e9c0cc15SPoul-Henning Kamp 	 */
2151e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2152e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2153e9c0cc15SPoul-Henning Kamp 
21548f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2155dee34ca4SPoul-Henning Kamp 	sp->sw_vp = vp;
2156dee34ca4SPoul-Henning Kamp 	sp->sw_id = id;
2157f3732fd1SPoul-Henning Kamp 	sp->sw_dev = dev;
21588d677ef9SPoul-Henning Kamp 	sp->sw_flags = 0;
2159e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2160e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
216159efee01SPoul-Henning Kamp 	sp->sw_strategy = strategy;
2162dee34ca4SPoul-Henning Kamp 	sp->sw_close = close;
2163e9c0cc15SPoul-Henning Kamp 
2164c8c7ad92SKip Macy 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2165e9c0cc15SPoul-Henning Kamp 	/*
2166ef3c5abdSPoul-Henning Kamp 	 * Do not free the first two block in order to avoid overwriting
21678f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2168e9c0cc15SPoul-Henning Kamp 	 */
2169ef3c5abdSPoul-Henning Kamp 	blist_free(sp->sw_blist, 2, nblks - 2);
2170e9c0cc15SPoul-Henning Kamp 
21712d9974c1SAlan Cox 	dvbase = 0;
217220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
21732d9974c1SAlan Cox 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
21742d9974c1SAlan Cox 		if (tsp->sw_end >= dvbase) {
21752d9974c1SAlan Cox 			/*
21762d9974c1SAlan Cox 			 * We put one uncovered page between the devices
21772d9974c1SAlan Cox 			 * in order to definitively prevent any cross-device
21782d9974c1SAlan Cox 			 * I/O requests
21792d9974c1SAlan Cox 			 */
21802d9974c1SAlan Cox 			dvbase = tsp->sw_end + 1;
21812d9974c1SAlan Cox 		}
21822d9974c1SAlan Cox 	}
21832d9974c1SAlan Cox 	sp->sw_first = dvbase;
21842d9974c1SAlan Cox 	sp->sw_end = dvbase + nblks;
21858f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
21868f60c087SPoul-Henning Kamp 	nswapdev++;
21878f60c087SPoul-Henning Kamp 	swap_pager_avail += nblks;
21883364c323SKonstantin Belousov 	swap_total += (vm_ooffset_t)nblks * PAGE_SIZE;
2189d05bc129SAlan Cox 	swp_sizecheck();
2190d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
219159efee01SPoul-Henning Kamp }
2192e9c0cc15SPoul-Henning Kamp 
2193e9c0cc15SPoul-Henning Kamp /*
2194e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2195e9c0cc15SPoul-Henning Kamp  *
2196e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2197dee34ca4SPoul-Henning Kamp  *
2198dee34ca4SPoul-Henning Kamp  * XXX: Badly designed system call: it should use a device index
2199dee34ca4SPoul-Henning Kamp  * rather than filename as specification.  We keep sw_vp around
2200dee34ca4SPoul-Henning Kamp  * only to make this work.
2201e9c0cc15SPoul-Henning Kamp  */
2202e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2203e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2204e9c0cc15SPoul-Henning Kamp 	char *name;
2205e9c0cc15SPoul-Henning Kamp };
2206e9c0cc15SPoul-Henning Kamp #endif
2207e9c0cc15SPoul-Henning Kamp 
2208e9c0cc15SPoul-Henning Kamp /*
2209e9c0cc15SPoul-Henning Kamp  * MPSAFE
2210e9c0cc15SPoul-Henning Kamp  */
2211e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2212e9c0cc15SPoul-Henning Kamp int
22132f249180SPoul-Henning Kamp swapoff(struct thread *td, struct swapoff_args *uap)
2214e9c0cc15SPoul-Henning Kamp {
2215e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2216e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2217e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
22188f60c087SPoul-Henning Kamp 	int error;
2219e9c0cc15SPoul-Henning Kamp 
2220acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPOFF);
2221e9c0cc15SPoul-Henning Kamp 	if (error)
22220909f38aSPawel Jakub Dawidek 		return (error);
2223e9c0cc15SPoul-Henning Kamp 
22240909f38aSPawel Jakub Dawidek 	mtx_lock(&Giant);
2225e9c0cc15SPoul-Henning Kamp 	while (swdev_syscall_active)
2226e9c0cc15SPoul-Henning Kamp 	    tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0);
2227e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 1;
2228e9c0cc15SPoul-Henning Kamp 
2229d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2230d9135e72SRobert Watson 	    td);
2231e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2232e9c0cc15SPoul-Henning Kamp 	if (error)
2233e9c0cc15SPoul-Henning Kamp 		goto done;
2234e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2235e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2236e9c0cc15SPoul-Henning Kamp 
223720da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
22388f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2239dee34ca4SPoul-Henning Kamp 		if (sp->sw_vp == vp)
22400909f38aSPawel Jakub Dawidek 			break;
2241e9c0cc15SPoul-Henning Kamp 	}
224220da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
22430909f38aSPawel Jakub Dawidek 	if (sp == NULL) {
2244e9c0cc15SPoul-Henning Kamp 		error = EINVAL;
2245e9c0cc15SPoul-Henning Kamp 		goto done;
22460909f38aSPawel Jakub Dawidek 	}
224735918c55SChristian S.J. Peron 	error = swapoff_one(sp, td->td_ucred);
22480909f38aSPawel Jakub Dawidek done:
22490909f38aSPawel Jakub Dawidek 	swdev_syscall_active = 0;
22500909f38aSPawel Jakub Dawidek 	wakeup_one(&swdev_syscall_active);
22510909f38aSPawel Jakub Dawidek 	mtx_unlock(&Giant);
22520909f38aSPawel Jakub Dawidek 	return (error);
22530909f38aSPawel Jakub Dawidek }
22540909f38aSPawel Jakub Dawidek 
22550909f38aSPawel Jakub Dawidek static int
225635918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred)
22570909f38aSPawel Jakub Dawidek {
22580909f38aSPawel Jakub Dawidek 	u_long nblks, dvbase;
2259e9c0cc15SPoul-Henning Kamp #ifdef MAC
22600909f38aSPawel Jakub Dawidek 	int error;
2261e9c0cc15SPoul-Henning Kamp #endif
2262e9c0cc15SPoul-Henning Kamp 
22630909f38aSPawel Jakub Dawidek 	mtx_assert(&Giant, MA_OWNED);
22640909f38aSPawel Jakub Dawidek #ifdef MAC
2265cb05b60aSAttilio Rao 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
226635918c55SChristian S.J. Peron 	error = mac_system_check_swapoff(cred, sp->sw_vp);
226722db15c0SAttilio Rao 	(void) VOP_UNLOCK(sp->sw_vp, 0);
22680909f38aSPawel Jakub Dawidek 	if (error != 0)
22690909f38aSPawel Jakub Dawidek 		return (error);
22700909f38aSPawel Jakub Dawidek #endif
2271e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2272e9c0cc15SPoul-Henning Kamp 
2273e9c0cc15SPoul-Henning Kamp 	/*
2274e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2275e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2276e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2277e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2278e9c0cc15SPoul-Henning Kamp 	 */
22792feb50bfSAttilio Rao 	if (cnt.v_free_count + cnt.v_cache_count + swap_pager_avail <
22802feb50bfSAttilio Rao 	    nblks + nswap_lowat) {
22810909f38aSPawel Jakub Dawidek 		return (ENOMEM);
2282e9c0cc15SPoul-Henning Kamp 	}
2283e9c0cc15SPoul-Henning Kamp 
2284e9c0cc15SPoul-Henning Kamp 	/*
2285e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2286e9c0cc15SPoul-Henning Kamp 	 */
22872928cef7SAlan Cox 	mtx_lock(&sw_dev_mtx);
2288e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
22898f60c087SPoul-Henning Kamp 	for (dvbase = 0; dvbase < sp->sw_end; dvbase += dmmax) {
22908f60c087SPoul-Henning Kamp 		swap_pager_avail -= blist_fill(sp->sw_blist,
22918f60c087SPoul-Henning Kamp 		     dvbase, dmmax);
2292e9c0cc15SPoul-Henning Kamp 	}
22933364c323SKonstantin Belousov 	swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE;
22942928cef7SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2295e9c0cc15SPoul-Henning Kamp 
2296e9c0cc15SPoul-Henning Kamp 	/*
2297e9c0cc15SPoul-Henning Kamp 	 * Page in the contents of the device and close it.
2298e9c0cc15SPoul-Henning Kamp 	 */
2299b3fed13eSDavid Schultz 	swap_pager_swapoff(sp);
2300e9c0cc15SPoul-Henning Kamp 
230135918c55SChristian S.J. Peron 	sp->sw_close(curthread, sp);
230259efee01SPoul-Henning Kamp 	sp->sw_id = NULL;
230320da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23048f60c087SPoul-Henning Kamp 	TAILQ_REMOVE(&swtailq, sp, sw_list);
23050676a140SAlan Cox 	nswapdev--;
23067dea2c2eSAlan Cox 	if (nswapdev == 0) {
23077dea2c2eSAlan Cox 		swap_pager_full = 2;
23087dea2c2eSAlan Cox 		swap_pager_almost_full = 1;
23097dea2c2eSAlan Cox 	}
23108f60c087SPoul-Henning Kamp 	if (swdevhd == sp)
23118f60c087SPoul-Henning Kamp 		swdevhd = NULL;
2312d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
23138f60c087SPoul-Henning Kamp 	blist_destroy(sp->sw_blist);
23148f60c087SPoul-Henning Kamp 	free(sp, M_VMPGDATA);
23150909f38aSPawel Jakub Dawidek 	return (0);
23160909f38aSPawel Jakub Dawidek }
2317e9c0cc15SPoul-Henning Kamp 
23180909f38aSPawel Jakub Dawidek void
23190909f38aSPawel Jakub Dawidek swapoff_all(void)
23200909f38aSPawel Jakub Dawidek {
23210909f38aSPawel Jakub Dawidek 	struct swdevt *sp, *spt;
23220909f38aSPawel Jakub Dawidek 	const char *devname;
23230909f38aSPawel Jakub Dawidek 	int error;
23240909f38aSPawel Jakub Dawidek 
23250909f38aSPawel Jakub Dawidek 	mtx_lock(&Giant);
23260909f38aSPawel Jakub Dawidek 	while (swdev_syscall_active)
23270909f38aSPawel Jakub Dawidek 		tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0);
23280909f38aSPawel Jakub Dawidek 	swdev_syscall_active = 1;
23290909f38aSPawel Jakub Dawidek 
23300909f38aSPawel Jakub Dawidek 	mtx_lock(&sw_dev_mtx);
23310909f38aSPawel Jakub Dawidek 	TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
23320909f38aSPawel Jakub Dawidek 		mtx_unlock(&sw_dev_mtx);
23330909f38aSPawel Jakub Dawidek 		if (vn_isdisk(sp->sw_vp, NULL))
23340909f38aSPawel Jakub Dawidek 			devname = sp->sw_vp->v_rdev->si_name;
23350909f38aSPawel Jakub Dawidek 		else
23360909f38aSPawel Jakub Dawidek 			devname = "[file]";
233735918c55SChristian S.J. Peron 		error = swapoff_one(sp, thread0.td_ucred);
23380909f38aSPawel Jakub Dawidek 		if (error != 0) {
23390909f38aSPawel Jakub Dawidek 			printf("Cannot remove swap device %s (error=%d), "
23400909f38aSPawel Jakub Dawidek 			    "skipping.\n", devname, error);
23410909f38aSPawel Jakub Dawidek 		} else if (bootverbose) {
23420909f38aSPawel Jakub Dawidek 			printf("Swap device %s removed.\n", devname);
23430909f38aSPawel Jakub Dawidek 		}
23440909f38aSPawel Jakub Dawidek 		mtx_lock(&sw_dev_mtx);
23450909f38aSPawel Jakub Dawidek 	}
23460909f38aSPawel Jakub Dawidek 	mtx_unlock(&sw_dev_mtx);
23470909f38aSPawel Jakub Dawidek 
2348e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 0;
2349e9c0cc15SPoul-Henning Kamp 	wakeup_one(&swdev_syscall_active);
2350e9c0cc15SPoul-Henning Kamp 	mtx_unlock(&Giant);
2351e9c0cc15SPoul-Henning Kamp }
2352e9c0cc15SPoul-Henning Kamp 
2353567104a1SPoul-Henning Kamp void
2354567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used)
2355567104a1SPoul-Henning Kamp {
2356567104a1SPoul-Henning Kamp 	struct swdevt *sp;
2357567104a1SPoul-Henning Kamp 
2358567104a1SPoul-Henning Kamp 	*total = 0;
2359567104a1SPoul-Henning Kamp 	*used = 0;
236020da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23618f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2362567104a1SPoul-Henning Kamp 		*total += sp->sw_nblks;
2363567104a1SPoul-Henning Kamp 		*used += sp->sw_used;
2364567104a1SPoul-Henning Kamp 	}
236520da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2366567104a1SPoul-Henning Kamp }
2367567104a1SPoul-Henning Kamp 
2368*dda4f960SKonstantin Belousov int
2369*dda4f960SKonstantin Belousov swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2370*dda4f960SKonstantin Belousov {
2371*dda4f960SKonstantin Belousov 	struct swdevt *sp;
2372*dda4f960SKonstantin Belousov 	char *tmp_devname;
2373*dda4f960SKonstantin Belousov 	int error, n;
2374*dda4f960SKonstantin Belousov 
2375*dda4f960SKonstantin Belousov 	n = 0;
2376*dda4f960SKonstantin Belousov 	error = ENOENT;
2377*dda4f960SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2378*dda4f960SKonstantin Belousov 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2379*dda4f960SKonstantin Belousov 		if (n != name) {
2380*dda4f960SKonstantin Belousov 			n++;
2381*dda4f960SKonstantin Belousov 			continue;
2382*dda4f960SKonstantin Belousov 		}
2383*dda4f960SKonstantin Belousov 		xs->xsw_version = XSWDEV_VERSION;
2384*dda4f960SKonstantin Belousov 		xs->xsw_dev = sp->sw_dev;
2385*dda4f960SKonstantin Belousov 		xs->xsw_flags = sp->sw_flags;
2386*dda4f960SKonstantin Belousov 		xs->xsw_nblks = sp->sw_nblks;
2387*dda4f960SKonstantin Belousov 		xs->xsw_used = sp->sw_used;
2388*dda4f960SKonstantin Belousov 		if (devname != NULL) {
2389*dda4f960SKonstantin Belousov 			if (vn_isdisk(sp->sw_vp, NULL))
2390*dda4f960SKonstantin Belousov 				tmp_devname = sp->sw_vp->v_rdev->si_name;
2391*dda4f960SKonstantin Belousov 			else
2392*dda4f960SKonstantin Belousov 				tmp_devname = "[file]";
2393*dda4f960SKonstantin Belousov 			strncpy(devname, tmp_devname, len);
2394*dda4f960SKonstantin Belousov 		}
2395*dda4f960SKonstantin Belousov 		error = 0;
2396*dda4f960SKonstantin Belousov 		break;
2397*dda4f960SKonstantin Belousov 	}
2398*dda4f960SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2399*dda4f960SKonstantin Belousov 	return (error);
2400*dda4f960SKonstantin Belousov }
2401*dda4f960SKonstantin Belousov 
2402e9c0cc15SPoul-Henning Kamp static int
2403e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2404e9c0cc15SPoul-Henning Kamp {
2405e9c0cc15SPoul-Henning Kamp 	struct xswdev xs;
2406*dda4f960SKonstantin Belousov 	int error;
2407e9c0cc15SPoul-Henning Kamp 
2408e9c0cc15SPoul-Henning Kamp 	if (arg2 != 1)			/* name length */
2409e9c0cc15SPoul-Henning Kamp 		return (EINVAL);
2410*dda4f960SKonstantin Belousov 	error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2411*dda4f960SKonstantin Belousov 	if (error != 0)
2412*dda4f960SKonstantin Belousov 		return (error);
2413e9c0cc15SPoul-Henning Kamp 	error = SYSCTL_OUT(req, &xs, sizeof(xs));
2414e9c0cc15SPoul-Henning Kamp 	return (error);
2415e9c0cc15SPoul-Henning Kamp }
2416e9c0cc15SPoul-Henning Kamp 
24178f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2418e9c0cc15SPoul-Henning Kamp     "Number of swap devices");
2419e9c0cc15SPoul-Henning Kamp SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info,
2420e9c0cc15SPoul-Henning Kamp     "Swap statistics by device");
2421ec38b344SPoul-Henning Kamp 
2422ec38b344SPoul-Henning Kamp /*
24238eb5a1cdSKonstantin Belousov  * vmspace_swap_count() - count the approximate swap usage in pages for a
2424ec38b344SPoul-Henning Kamp  *			  vmspace.
2425ec38b344SPoul-Henning Kamp  *
2426ec38b344SPoul-Henning Kamp  *	The map must be locked.
2427ec38b344SPoul-Henning Kamp  *
24288eb5a1cdSKonstantin Belousov  *	Swap usage is determined by taking the proportional swap used by
2429ec38b344SPoul-Henning Kamp  *	VM objects backing the VM map.  To make up for fractional losses,
2430ec38b344SPoul-Henning Kamp  *	if the VM object has any swap use at all the associated map entries
2431ec38b344SPoul-Henning Kamp  *	count for at least 1 swap page.
2432ec38b344SPoul-Henning Kamp  */
24332860553aSRebecca Cran long
2434ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2435ec38b344SPoul-Henning Kamp {
243665d8409cSRebecca Cran 	vm_map_t map;
2437ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
243865d8409cSRebecca Cran 	vm_object_t object;
24392860553aSRebecca Cran 	long count, n;
244065d8409cSRebecca Cran 
244165d8409cSRebecca Cran 	map = &vmspace->vm_map;
244265d8409cSRebecca Cran 	count = 0;
2443ec38b344SPoul-Henning Kamp 
2444ec38b344SPoul-Henning Kamp 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2445ec38b344SPoul-Henning Kamp 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2446ec38b344SPoul-Henning Kamp 		    (object = cur->object.vm_object) != NULL) {
2447ec38b344SPoul-Henning Kamp 			VM_OBJECT_LOCK(object);
2448ec38b344SPoul-Henning Kamp 			if (object->type == OBJT_SWAP &&
2449ec38b344SPoul-Henning Kamp 			    object->un_pager.swp.swp_bcount != 0) {
245065d8409cSRebecca Cran 				n = (cur->end - cur->start) / PAGE_SIZE;
2451ec38b344SPoul-Henning Kamp 				count += object->un_pager.swp.swp_bcount *
2452ec38b344SPoul-Henning Kamp 				    SWAP_META_PAGES * n / object->size + 1;
2453ec38b344SPoul-Henning Kamp 			}
2454ec38b344SPoul-Henning Kamp 			VM_OBJECT_UNLOCK(object);
2455ec38b344SPoul-Henning Kamp 		}
2456ec38b344SPoul-Henning Kamp 	}
2457ec38b344SPoul-Henning Kamp 	return (count);
2458ec38b344SPoul-Henning Kamp }
2459dee34ca4SPoul-Henning Kamp 
2460dee34ca4SPoul-Henning Kamp /*
2461dee34ca4SPoul-Henning Kamp  * GEOM backend
2462dee34ca4SPoul-Henning Kamp  *
2463dee34ca4SPoul-Henning Kamp  * Swapping onto disk devices.
2464dee34ca4SPoul-Henning Kamp  *
2465dee34ca4SPoul-Henning Kamp  */
2466dee34ca4SPoul-Henning Kamp 
24675721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan;
24685721c9c7SPoul-Henning Kamp 
2469dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = {
2470dee34ca4SPoul-Henning Kamp 	.name = "SWAP",
24715721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
24725721c9c7SPoul-Henning Kamp 	.orphan = swapgeom_orphan,
2473dee34ca4SPoul-Henning Kamp };
2474dee34ca4SPoul-Henning Kamp 
2475dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class);
2476dee34ca4SPoul-Henning Kamp 
2477dee34ca4SPoul-Henning Kamp 
2478dee34ca4SPoul-Henning Kamp static void
2479dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2)
2480dee34ca4SPoul-Henning Kamp {
2481dee34ca4SPoul-Henning Kamp 	struct buf *bp;
2482dee34ca4SPoul-Henning Kamp 
2483dee34ca4SPoul-Henning Kamp 	bp = bp2->bio_caller2;
2484c5d3d25eSPoul-Henning Kamp 	bp->b_ioflags = bp2->bio_flags;
2485dee34ca4SPoul-Henning Kamp 	if (bp2->bio_error)
2486dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2487c5d3d25eSPoul-Henning Kamp 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2488c5d3d25eSPoul-Henning Kamp 	bp->b_error = bp2->bio_error;
2489dee34ca4SPoul-Henning Kamp 	bufdone(bp);
2490dee34ca4SPoul-Henning Kamp 	g_destroy_bio(bp2);
2491dee34ca4SPoul-Henning Kamp }
2492dee34ca4SPoul-Henning Kamp 
2493dee34ca4SPoul-Henning Kamp static void
2494dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2495dee34ca4SPoul-Henning Kamp {
2496dee34ca4SPoul-Henning Kamp 	struct bio *bio;
2497dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2498dee34ca4SPoul-Henning Kamp 
2499dee34ca4SPoul-Henning Kamp 	cp = sp->sw_id;
2500dee34ca4SPoul-Henning Kamp 	if (cp == NULL) {
2501dee34ca4SPoul-Henning Kamp 		bp->b_error = ENXIO;
2502dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2503dee34ca4SPoul-Henning Kamp 		bufdone(bp);
2504dee34ca4SPoul-Henning Kamp 		return;
2505dee34ca4SPoul-Henning Kamp 	}
250611041003SKonstantin Belousov 	if (bp->b_iocmd == BIO_WRITE)
250711041003SKonstantin Belousov 		bio = g_new_bio();
250811041003SKonstantin Belousov 	else
25094f8205e5SPoul-Henning Kamp 		bio = g_alloc_bio();
25104f8205e5SPoul-Henning Kamp 	if (bio == NULL) {
25113e5b6861SPoul-Henning Kamp 		bp->b_error = ENOMEM;
25123e5b6861SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
25133e5b6861SPoul-Henning Kamp 		bufdone(bp);
25143e5b6861SPoul-Henning Kamp 		return;
25153e5b6861SPoul-Henning Kamp 	}
251611041003SKonstantin Belousov 
2517dee34ca4SPoul-Henning Kamp 	bio->bio_caller2 = bp;
2518c5d3d25eSPoul-Henning Kamp 	bio->bio_cmd = bp->b_iocmd;
2519c5d3d25eSPoul-Henning Kamp 	bio->bio_data = bp->b_data;
2520dee34ca4SPoul-Henning Kamp 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2521dee34ca4SPoul-Henning Kamp 	bio->bio_length = bp->b_bcount;
2522dee34ca4SPoul-Henning Kamp 	bio->bio_done = swapgeom_done;
2523dee34ca4SPoul-Henning Kamp 	g_io_request(bio, cp);
2524dee34ca4SPoul-Henning Kamp 	return;
2525dee34ca4SPoul-Henning Kamp }
2526dee34ca4SPoul-Henning Kamp 
2527dee34ca4SPoul-Henning Kamp static void
2528dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp)
2529dee34ca4SPoul-Henning Kamp {
2530dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2531dee34ca4SPoul-Henning Kamp 
2532dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2533dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list)
2534dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == cp)
2535dee34ca4SPoul-Henning Kamp 			sp->sw_id = NULL;
2536dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2537dee34ca4SPoul-Henning Kamp }
2538dee34ca4SPoul-Henning Kamp 
2539dee34ca4SPoul-Henning Kamp static void
2540dee34ca4SPoul-Henning Kamp swapgeom_close_ev(void *arg, int flags)
2541dee34ca4SPoul-Henning Kamp {
2542dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2543dee34ca4SPoul-Henning Kamp 
2544dee34ca4SPoul-Henning Kamp 	cp = arg;
2545d2bae332SPoul-Henning Kamp 	g_access(cp, -1, -1, 0);
2546dee34ca4SPoul-Henning Kamp 	g_detach(cp);
2547dee34ca4SPoul-Henning Kamp 	g_destroy_consumer(cp);
2548dee34ca4SPoul-Henning Kamp }
2549dee34ca4SPoul-Henning Kamp 
2550dee34ca4SPoul-Henning Kamp static void
2551dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw)
2552dee34ca4SPoul-Henning Kamp {
2553dee34ca4SPoul-Henning Kamp 
2554dee34ca4SPoul-Henning Kamp 	/* XXX: direct call when Giant untangled */
2555dee34ca4SPoul-Henning Kamp 	g_waitfor_event(swapgeom_close_ev, sw->sw_id, M_WAITOK, NULL);
2556dee34ca4SPoul-Henning Kamp }
2557dee34ca4SPoul-Henning Kamp 
2558dee34ca4SPoul-Henning Kamp 
2559dee34ca4SPoul-Henning Kamp struct swh0h0 {
256089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
2561dee34ca4SPoul-Henning Kamp 	struct vnode *vp;
2562dee34ca4SPoul-Henning Kamp 	int	error;
2563dee34ca4SPoul-Henning Kamp };
2564dee34ca4SPoul-Henning Kamp 
2565dee34ca4SPoul-Henning Kamp static void
2566dee34ca4SPoul-Henning Kamp swapongeom_ev(void *arg, int flags)
2567dee34ca4SPoul-Henning Kamp {
2568dee34ca4SPoul-Henning Kamp 	struct swh0h0 *swh;
2569dee34ca4SPoul-Henning Kamp 	struct g_provider *pp;
2570dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2571dee34ca4SPoul-Henning Kamp 	static struct g_geom *gp;
2572dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2573dee34ca4SPoul-Henning Kamp 	u_long nblks;
2574dee34ca4SPoul-Henning Kamp 	int error;
2575dee34ca4SPoul-Henning Kamp 
2576dee34ca4SPoul-Henning Kamp 	swh = arg;
2577dee34ca4SPoul-Henning Kamp 	swh->error = 0;
2578dee34ca4SPoul-Henning Kamp 	pp = g_dev_getprovider(swh->dev);
2579dee34ca4SPoul-Henning Kamp 	if (pp == NULL) {
2580dee34ca4SPoul-Henning Kamp 		swh->error = ENODEV;
2581dee34ca4SPoul-Henning Kamp 		return;
2582dee34ca4SPoul-Henning Kamp 	}
2583dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2584dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2585dee34ca4SPoul-Henning Kamp 		cp = sp->sw_id;
2586dee34ca4SPoul-Henning Kamp 		if (cp != NULL && cp->provider == pp) {
2587dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2588dee34ca4SPoul-Henning Kamp 			swh->error = EBUSY;
2589dee34ca4SPoul-Henning Kamp 			return;
2590dee34ca4SPoul-Henning Kamp 		}
2591dee34ca4SPoul-Henning Kamp 	}
2592dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
25935721c9c7SPoul-Henning Kamp 	if (gp == NULL)
2594dee34ca4SPoul-Henning Kamp 		gp = g_new_geomf(&g_swap_class, "swap", NULL);
2595dee34ca4SPoul-Henning Kamp 	cp = g_new_consumer(gp);
2596dee34ca4SPoul-Henning Kamp 	g_attach(cp, pp);
2597afeb65e6SPoul-Henning Kamp 	/*
2598afeb65e6SPoul-Henning Kamp 	 * XXX: Everytime you think you can improve the margin for
2599afeb65e6SPoul-Henning Kamp 	 * footshooting, somebody depends on the ability to do so:
2600afeb65e6SPoul-Henning Kamp 	 * savecore(8) wants to write to our swapdev so we cannot
2601afeb65e6SPoul-Henning Kamp 	 * set an exclusive count :-(
2602afeb65e6SPoul-Henning Kamp 	 */
2603d2bae332SPoul-Henning Kamp 	error = g_access(cp, 1, 1, 0);
2604dee34ca4SPoul-Henning Kamp 	if (error) {
2605dee34ca4SPoul-Henning Kamp 		g_detach(cp);
2606dee34ca4SPoul-Henning Kamp 		g_destroy_consumer(cp);
2607dee34ca4SPoul-Henning Kamp 		swh->error = error;
2608dee34ca4SPoul-Henning Kamp 		return;
2609dee34ca4SPoul-Henning Kamp 	}
2610dee34ca4SPoul-Henning Kamp 	nblks = pp->mediasize / DEV_BSIZE;
2611dee34ca4SPoul-Henning Kamp 	swaponsomething(swh->vp, cp, nblks, swapgeom_strategy,
2612dee34ca4SPoul-Henning Kamp 	    swapgeom_close, dev2udev(swh->dev));
2613dee34ca4SPoul-Henning Kamp 	swh->error = 0;
2614dee34ca4SPoul-Henning Kamp 	return;
2615dee34ca4SPoul-Henning Kamp }
2616dee34ca4SPoul-Henning Kamp 
2617dee34ca4SPoul-Henning Kamp static int
2618dee34ca4SPoul-Henning Kamp swapongeom(struct thread *td, struct vnode *vp)
2619dee34ca4SPoul-Henning Kamp {
2620dee34ca4SPoul-Henning Kamp 	int error;
2621dee34ca4SPoul-Henning Kamp 	struct swh0h0 swh;
2622dee34ca4SPoul-Henning Kamp 
2623cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2624dee34ca4SPoul-Henning Kamp 
2625dee34ca4SPoul-Henning Kamp 	swh.dev = vp->v_rdev;
2626dee34ca4SPoul-Henning Kamp 	swh.vp = vp;
2627dee34ca4SPoul-Henning Kamp 	swh.error = 0;
2628dee34ca4SPoul-Henning Kamp 	/* XXX: direct call when Giant untangled */
2629dee34ca4SPoul-Henning Kamp 	error = g_waitfor_event(swapongeom_ev, &swh, M_WAITOK, NULL);
2630dee34ca4SPoul-Henning Kamp 	if (!error)
2631dee34ca4SPoul-Henning Kamp 		error = swh.error;
263222db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
2633dee34ca4SPoul-Henning Kamp 	return (error);
2634dee34ca4SPoul-Henning Kamp }
2635dee34ca4SPoul-Henning Kamp 
2636dee34ca4SPoul-Henning Kamp /*
2637dee34ca4SPoul-Henning Kamp  * VNODE backend
2638dee34ca4SPoul-Henning Kamp  *
2639dee34ca4SPoul-Henning Kamp  * This is used mainly for network filesystem (read: probably only tested
2640dee34ca4SPoul-Henning Kamp  * with NFS) swapfiles.
2641dee34ca4SPoul-Henning Kamp  *
2642dee34ca4SPoul-Henning Kamp  */
2643dee34ca4SPoul-Henning Kamp 
2644dee34ca4SPoul-Henning Kamp static void
2645dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp)
2646dee34ca4SPoul-Henning Kamp {
2647494eb176SPoul-Henning Kamp 	struct vnode *vp2;
2648dee34ca4SPoul-Henning Kamp 
2649dee34ca4SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2650dee34ca4SPoul-Henning Kamp 
2651dee34ca4SPoul-Henning Kamp 	vp2 = sp->sw_id;
2652dee34ca4SPoul-Henning Kamp 	vhold(vp2);
2653dee34ca4SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
26543cfc7651SOlivier Houchard 		if (bp->b_bufobj)
2655494eb176SPoul-Henning Kamp 			bufobj_wdrop(bp->b_bufobj);
2656a76d8f4eSPoul-Henning Kamp 		bufobj_wref(&vp2->v_bufobj);
2657dee34ca4SPoul-Henning Kamp 	}
26583cfc7651SOlivier Houchard 	if (bp->b_bufobj != &vp2->v_bufobj)
26593cfc7651SOlivier Houchard 		bp->b_bufobj = &vp2->v_bufobj;
2660dee34ca4SPoul-Henning Kamp 	bp->b_vp = vp2;
26612c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
2662b792bebeSPoul-Henning Kamp 	bstrategy(bp);
2663dee34ca4SPoul-Henning Kamp 	return;
2664dee34ca4SPoul-Henning Kamp }
2665dee34ca4SPoul-Henning Kamp 
2666dee34ca4SPoul-Henning Kamp static void
2667dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp)
2668dee34ca4SPoul-Henning Kamp {
2669dee34ca4SPoul-Henning Kamp 
2670dee34ca4SPoul-Henning Kamp 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2671dee34ca4SPoul-Henning Kamp 	vrele(sp->sw_vp);
2672dee34ca4SPoul-Henning Kamp }
2673dee34ca4SPoul-Henning Kamp 
2674dee34ca4SPoul-Henning Kamp 
2675dee34ca4SPoul-Henning Kamp static int
2676dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2677dee34ca4SPoul-Henning Kamp {
2678dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2679dee34ca4SPoul-Henning Kamp 	int error;
2680dee34ca4SPoul-Henning Kamp 
2681dee34ca4SPoul-Henning Kamp 	if (nblks == 0)
2682dee34ca4SPoul-Henning Kamp 		return (ENXIO);
2683dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2684dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2685dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == vp) {
2686dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2687dee34ca4SPoul-Henning Kamp 			return (EBUSY);
2688dee34ca4SPoul-Henning Kamp 		}
2689dee34ca4SPoul-Henning Kamp 	}
2690dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2691dee34ca4SPoul-Henning Kamp 
2692cb05b60aSAttilio Rao 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2693dee34ca4SPoul-Henning Kamp #ifdef MAC
269430d239bcSRobert Watson 	error = mac_system_check_swapon(td->td_ucred, vp);
2695dee34ca4SPoul-Henning Kamp 	if (error == 0)
2696dee34ca4SPoul-Henning Kamp #endif
26979e223287SKonstantin Belousov 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
269822db15c0SAttilio Rao 	(void) VOP_UNLOCK(vp, 0);
2699dee34ca4SPoul-Henning Kamp 	if (error)
2700dee34ca4SPoul-Henning Kamp 		return (error);
2701dee34ca4SPoul-Henning Kamp 
2702dee34ca4SPoul-Henning Kamp 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
2703f3732fd1SPoul-Henning Kamp 	    NODEV);
2704dee34ca4SPoul-Henning Kamp 	return (0);
2705dee34ca4SPoul-Henning Kamp }
2706