xref: /freebsd/sys/vm/swap_pager.c (revision 2965a4531505c497a6bbc6f2974499a520c75394)
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>
893364c323SKonstantin Belousov #include <sys/resource.h>
903364c323SKonstantin Belousov #include <sys/resourcevar.h>
91327f4e83SMatthew Dillon #include <sys/sysctl.h>
92e9c0cc15SPoul-Henning Kamp #include <sys/sysproto.h>
931c7c3c6aSMatthew Dillon #include <sys/blist.h>
941c7c3c6aSMatthew Dillon #include <sys/lock.h>
950cddd8f0SMatthew Dillon #include <sys/sx.h>
96936524aaSMatthew Dillon #include <sys/vmmeter.h>
97df8bae1dSRodney W. Grimes 
98aed55708SRobert Watson #include <security/mac/mac_framework.h>
99aed55708SRobert Watson 
100df8bae1dSRodney W. Grimes #include <vm/vm.h>
10121cd6e62SSeigo Tanimura #include <vm/pmap.h>
10221cd6e62SSeigo Tanimura #include <vm/vm_map.h>
10321cd6e62SSeigo Tanimura #include <vm/vm_kern.h>
104efeaf95aSDavid Greenman #include <vm/vm_object.h>
105df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
106efeaf95aSDavid Greenman #include <vm/vm_pager.h>
107df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
108e9c0cc15SPoul-Henning Kamp #include <vm/vm_param.h>
109df8bae1dSRodney W. Grimes #include <vm/swap_pager.h>
110efeaf95aSDavid Greenman #include <vm/vm_extern.h>
111670d17b5SJeff Roberson #include <vm/uma.h>
112df8bae1dSRodney W. Grimes 
113dee34ca4SPoul-Henning Kamp #include <geom/geom.h>
114dee34ca4SPoul-Henning Kamp 
115ec38b344SPoul-Henning Kamp /*
116ec38b344SPoul-Henning Kamp  * SWB_NPAGES must be a power of 2.  It may be set to 1, 2, 4, 8, or 16
117ec38b344SPoul-Henning Kamp  * pages per allocation.  We recommend you stick with the default of 8.
118ec38b344SPoul-Henning Kamp  * The 16-page limit is due to the radix code (kern/subr_blist.c).
119ec38b344SPoul-Henning Kamp  */
120ec38b344SPoul-Henning Kamp #ifndef MAX_PAGEOUT_CLUSTER
121ec38b344SPoul-Henning Kamp #define MAX_PAGEOUT_CLUSTER 16
122ec38b344SPoul-Henning Kamp #endif
123ec38b344SPoul-Henning Kamp 
124ec38b344SPoul-Henning Kamp #if !defined(SWB_NPAGES)
125ec38b344SPoul-Henning Kamp #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
126ec38b344SPoul-Henning Kamp #endif
127ec38b344SPoul-Henning Kamp 
128ec38b344SPoul-Henning Kamp /*
129ec38b344SPoul-Henning Kamp  * Piecemeal swap metadata structure.  Swap is stored in a radix tree.
130ec38b344SPoul-Henning Kamp  *
131ec38b344SPoul-Henning Kamp  * If SWB_NPAGES is 8 and sizeof(char *) == sizeof(daddr_t), our radix
132ec38b344SPoul-Henning Kamp  * is basically 8.  Assuming PAGE_SIZE == 4096, one tree level represents
133ec38b344SPoul-Henning Kamp  * 32K worth of data, two levels represent 256K, three levels represent
134ec38b344SPoul-Henning Kamp  * 2 MBytes.   This is acceptable.
135ec38b344SPoul-Henning Kamp  *
136ec38b344SPoul-Henning Kamp  * Overall memory utilization is about the same as the old swap structure.
137ec38b344SPoul-Henning Kamp  */
138ec38b344SPoul-Henning Kamp #define SWCORRECT(n) (sizeof(void *) * (n) / sizeof(daddr_t))
139ec38b344SPoul-Henning Kamp #define SWAP_META_PAGES		(SWB_NPAGES * 2)
140ec38b344SPoul-Henning Kamp #define SWAP_META_MASK		(SWAP_META_PAGES - 1)
141ec38b344SPoul-Henning Kamp 
142e9c0cc15SPoul-Henning Kamp struct swblock {
143e9c0cc15SPoul-Henning Kamp 	struct swblock	*swb_hnext;
144e9c0cc15SPoul-Henning Kamp 	vm_object_t	swb_object;
145e9c0cc15SPoul-Henning Kamp 	vm_pindex_t	swb_index;
146e9c0cc15SPoul-Henning Kamp 	int		swb_count;
147e9c0cc15SPoul-Henning Kamp 	daddr_t		swb_pages[SWAP_META_PAGES];
148e9c0cc15SPoul-Henning Kamp };
149e9c0cc15SPoul-Henning Kamp 
15020da9c2eSPoul-Henning Kamp static struct mtx sw_dev_mtx;
1518f60c087SPoul-Henning Kamp static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
1528f60c087SPoul-Henning Kamp static struct swdevt *swdevhd;	/* Allocate from here next */
1538f60c087SPoul-Henning Kamp static int nswapdev;		/* Number of swap devices */
1548f60c087SPoul-Henning Kamp int swap_pager_avail;
155e9c0cc15SPoul-Henning Kamp static int swdev_syscall_active = 0; /* serialize swap(on|off) */
156e9c0cc15SPoul-Henning Kamp 
1573364c323SKonstantin Belousov static vm_ooffset_t swap_total;
1588a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0,
1598a9c731fSIvan Voras     "Total amount of available swap storage.");
1603364c323SKonstantin Belousov static vm_ooffset_t swap_reserved;
1618a9c731fSIvan Voras SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0,
1628a9c731fSIvan Voras     "Amount of swap storage needed to back all allocated anonymous memory.");
1633364c323SKonstantin Belousov static int overcommit = 0;
1648a9c731fSIvan Voras SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0,
1658a9c731fSIvan Voras     "Configure virtual memory overcommit behavior. See tuning(7) "
1668a9c731fSIvan Voras     "for details.");
1673364c323SKonstantin Belousov 
1683364c323SKonstantin Belousov /* bits from overcommit */
1693364c323SKonstantin Belousov #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
1703364c323SKonstantin Belousov #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
1713364c323SKonstantin Belousov #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
1723364c323SKonstantin Belousov 
1733364c323SKonstantin Belousov int
1743364c323SKonstantin Belousov swap_reserve(vm_ooffset_t incr)
1753364c323SKonstantin Belousov {
1763364c323SKonstantin Belousov 
1773364c323SKonstantin Belousov 	return (swap_reserve_by_uid(incr, curthread->td_ucred->cr_ruidinfo));
1783364c323SKonstantin Belousov }
1793364c323SKonstantin Belousov 
1803364c323SKonstantin Belousov int
1813364c323SKonstantin Belousov swap_reserve_by_uid(vm_ooffset_t incr, struct uidinfo *uip)
1823364c323SKonstantin Belousov {
1835c0e1c11SKonstantin Belousov 	vm_ooffset_t r, s;
1843364c323SKonstantin Belousov 	int res, error;
1853364c323SKonstantin Belousov 	static int curfail;
1863364c323SKonstantin Belousov 	static struct timeval lastfail;
1873364c323SKonstantin Belousov 
1883364c323SKonstantin Belousov 	if (incr & PAGE_MASK)
1893364c323SKonstantin Belousov 		panic("swap_reserve: & PAGE_MASK");
1903364c323SKonstantin Belousov 
1913364c323SKonstantin Belousov 	res = 0;
1923364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
1933364c323SKonstantin Belousov 	r = swap_reserved + incr;
1943364c323SKonstantin Belousov 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
1953364c323SKonstantin Belousov 		s = cnt.v_page_count - cnt.v_free_reserved - cnt.v_wire_count;
1963364c323SKonstantin Belousov 		s *= PAGE_SIZE;
1973364c323SKonstantin Belousov 	} else
1983364c323SKonstantin Belousov 		s = 0;
1993364c323SKonstantin Belousov 	s += swap_total;
2003364c323SKonstantin Belousov 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
2013364c323SKonstantin Belousov 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
2023364c323SKonstantin Belousov 		res = 1;
2033364c323SKonstantin Belousov 		swap_reserved = r;
2043364c323SKonstantin Belousov 	}
2053364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2063364c323SKonstantin Belousov 
2073364c323SKonstantin Belousov 	if (res) {
2083364c323SKonstantin Belousov 		PROC_LOCK(curproc);
2093364c323SKonstantin Belousov 		UIDINFO_VMSIZE_LOCK(uip);
2105c0e1c11SKonstantin Belousov 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
2115c0e1c11SKonstantin Belousov 		    uip->ui_vmsize + incr > lim_cur(curproc, RLIMIT_SWAP) &&
2125c0e1c11SKonstantin Belousov 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT))
2133364c323SKonstantin Belousov 			res = 0;
2143364c323SKonstantin Belousov 		else
2153364c323SKonstantin Belousov 			uip->ui_vmsize += incr;
2163364c323SKonstantin Belousov 		UIDINFO_VMSIZE_UNLOCK(uip);
2173364c323SKonstantin Belousov 		PROC_UNLOCK(curproc);
2183364c323SKonstantin Belousov 		if (!res) {
2193364c323SKonstantin Belousov 			mtx_lock(&sw_dev_mtx);
2203364c323SKonstantin Belousov 			swap_reserved -= incr;
2213364c323SKonstantin Belousov 			mtx_unlock(&sw_dev_mtx);
2223364c323SKonstantin Belousov 		}
2233364c323SKonstantin Belousov 	}
2243364c323SKonstantin Belousov 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
2253364c323SKonstantin Belousov 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
2263364c323SKonstantin Belousov 		    curproc->p_pid, uip->ui_uid, incr);
2273364c323SKonstantin Belousov 	}
2283364c323SKonstantin Belousov 
2293364c323SKonstantin Belousov 	return (res);
2303364c323SKonstantin Belousov }
2313364c323SKonstantin Belousov 
2323364c323SKonstantin Belousov void
2333364c323SKonstantin Belousov swap_reserve_force(vm_ooffset_t incr)
2343364c323SKonstantin Belousov {
2353364c323SKonstantin Belousov 	struct uidinfo *uip;
2363364c323SKonstantin Belousov 
2373364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2383364c323SKonstantin Belousov 	swap_reserved += incr;
2393364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2403364c323SKonstantin Belousov 
2413364c323SKonstantin Belousov 	uip = curthread->td_ucred->cr_ruidinfo;
2423364c323SKonstantin Belousov 	PROC_LOCK(curproc);
2433364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
2443364c323SKonstantin Belousov 	uip->ui_vmsize += incr;
2453364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
2463364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2473364c323SKonstantin Belousov }
2483364c323SKonstantin Belousov 
2493364c323SKonstantin Belousov void
2503364c323SKonstantin Belousov swap_release(vm_ooffset_t decr)
2513364c323SKonstantin Belousov {
2523364c323SKonstantin Belousov 	struct uidinfo *uip;
2533364c323SKonstantin Belousov 
2543364c323SKonstantin Belousov 	PROC_LOCK(curproc);
2553364c323SKonstantin Belousov 	uip = curthread->td_ucred->cr_ruidinfo;
2563364c323SKonstantin Belousov 	swap_release_by_uid(decr, uip);
2573364c323SKonstantin Belousov 	PROC_UNLOCK(curproc);
2583364c323SKonstantin Belousov }
2593364c323SKonstantin Belousov 
2603364c323SKonstantin Belousov void
2613364c323SKonstantin Belousov swap_release_by_uid(vm_ooffset_t decr, struct uidinfo *uip)
2623364c323SKonstantin Belousov {
2633364c323SKonstantin Belousov 
2643364c323SKonstantin Belousov 	if (decr & PAGE_MASK)
2653364c323SKonstantin Belousov 		panic("swap_release: & PAGE_MASK");
2663364c323SKonstantin Belousov 
2673364c323SKonstantin Belousov 	mtx_lock(&sw_dev_mtx);
2683364c323SKonstantin Belousov 	if (swap_reserved < decr)
2693364c323SKonstantin Belousov 		panic("swap_reserved < decr");
2703364c323SKonstantin Belousov 	swap_reserved -= decr;
2713364c323SKonstantin Belousov 	mtx_unlock(&sw_dev_mtx);
2723364c323SKonstantin Belousov 
2733364c323SKonstantin Belousov 	UIDINFO_VMSIZE_LOCK(uip);
2743364c323SKonstantin Belousov 	if (uip->ui_vmsize < decr)
2753364c323SKonstantin Belousov 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
2763364c323SKonstantin Belousov 	uip->ui_vmsize -= decr;
2773364c323SKonstantin Belousov 	UIDINFO_VMSIZE_UNLOCK(uip);
2783364c323SKonstantin Belousov }
2793364c323SKonstantin Belousov 
2804b03903aSPoul-Henning Kamp static void swapdev_strategy(struct buf *, struct swdevt *sw);
281e9c0cc15SPoul-Henning Kamp 
2821c7c3c6aSMatthew Dillon #define SWM_FREE	0x02	/* free, period			*/
2831c7c3c6aSMatthew Dillon #define SWM_POP		0x04	/* pop out			*/
28426f9a767SRodney W. Grimes 
2857dea2c2eSAlan Cox int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
2867dea2c2eSAlan Cox static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
2871c7c3c6aSMatthew Dillon static int nsw_rcount;		/* free read buffers			*/
288327f4e83SMatthew Dillon static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
289327f4e83SMatthew Dillon static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
290327f4e83SMatthew Dillon static int nsw_wcount_async_max;/* assigned maximum			*/
291327f4e83SMatthew Dillon static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
2921c7c3c6aSMatthew Dillon 
2931c7c3c6aSMatthew Dillon static struct swblock **swhash;
2941c7c3c6aSMatthew Dillon static int swhash_mask;
2957827d9b0SAlan Cox static struct mtx swhash_mtx;
2967827d9b0SAlan Cox 
297327f4e83SMatthew Dillon static int swap_async_max = 4;	/* maximum in-progress async I/O's	*/
2980cddd8f0SMatthew Dillon static struct sx sw_alloc_sx;
299327f4e83SMatthew Dillon 
30024e7ab7cSPoul-Henning Kamp 
301327f4e83SMatthew Dillon SYSCTL_INT(_vm, OID_AUTO, swap_async_max,
302327f4e83SMatthew Dillon         CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops");
3031c7c3c6aSMatthew Dillon 
3041c7c3c6aSMatthew Dillon /*
3051c7c3c6aSMatthew Dillon  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
3061c7c3c6aSMatthew Dillon  * of searching a named list by hashing it just a little.
3071c7c3c6aSMatthew Dillon  */
3081c7c3c6aSMatthew Dillon 
3091c7c3c6aSMatthew Dillon #define NOBJLISTS		8
3101c7c3c6aSMatthew Dillon 
3111c7c3c6aSMatthew Dillon #define NOBJLIST(handle)	\
312af647ddeSBruce Evans 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
3131c7c3c6aSMatthew Dillon 
314a9fa2c05SAlfred Perlstein static struct mtx sw_alloc_mtx;	/* protect list manipulation */
3151c7c3c6aSMatthew Dillon static struct pagerlst	swap_pager_object_list[NOBJLISTS];
316e9c0cc15SPoul-Henning Kamp static uma_zone_t	swap_zone;
3175285558aSAlan Cox static struct vm_object	swap_zone_obj;
3181c7c3c6aSMatthew Dillon 
3191c7c3c6aSMatthew Dillon /*
3201c7c3c6aSMatthew Dillon  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
3211c7c3c6aSMatthew Dillon  * calls hooked from other parts of the VM system and do not appear here.
3221c7c3c6aSMatthew Dillon  * (see vm/swap_pager.h).
3231c7c3c6aSMatthew Dillon  */
324ff98689dSBruce Evans static vm_object_t
32511caded3SAlfred Perlstein 		swap_pager_alloc(void *handle, vm_ooffset_t size,
3263364c323SKonstantin Belousov 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
32711caded3SAlfred Perlstein static void	swap_pager_dealloc(vm_object_t object);
32811caded3SAlfred Perlstein static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int);
329751221fdSPoul-Henning Kamp static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
3305ea4972cSAlan Cox static boolean_t
3315ea4972cSAlan Cox 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
33211caded3SAlfred Perlstein static void	swap_pager_init(void);
33311caded3SAlfred Perlstein static void	swap_pager_unswapped(vm_page_t);
334b3fed13eSDavid Schultz static void	swap_pager_swapoff(struct swdevt *sp);
335f708ef1bSPoul-Henning Kamp 
336df8bae1dSRodney W. Grimes struct pagerops swappagerops = {
337e04e4bacSPoul-Henning Kamp 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
338e04e4bacSPoul-Henning Kamp 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
339e04e4bacSPoul-Henning Kamp 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
340e04e4bacSPoul-Henning Kamp 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
341e04e4bacSPoul-Henning Kamp 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
342e04e4bacSPoul-Henning Kamp 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
343e04e4bacSPoul-Henning Kamp 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
344df8bae1dSRodney W. Grimes };
345df8bae1dSRodney W. Grimes 
3461c7c3c6aSMatthew Dillon /*
3471c7c3c6aSMatthew Dillon  * dmmax is in page-sized chunks with the new swap system.  It was
34864bcb9c8SMatthew Dillon  * dev-bsized chunks in the old.  dmmax is always a power of 2.
3491c7c3c6aSMatthew Dillon  *
3501c7c3c6aSMatthew Dillon  * swap_*() routines are externally accessible.  swp_*() routines are
3511c7c3c6aSMatthew Dillon  * internal.
3521c7c3c6aSMatthew Dillon  */
353751221fdSPoul-Henning Kamp static int dmmax;
354e9c0cc15SPoul-Henning Kamp static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
355e9c0cc15SPoul-Henning Kamp static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
35626f9a767SRodney W. Grimes 
357cee313c4SRobert Watson SYSCTL_INT(_vm, OID_AUTO, dmmax,
358cee313c4SRobert Watson 	CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block");
359cee313c4SRobert Watson 
360a5edd34aSPoul-Henning Kamp static void	swp_sizecheck(void);
36111caded3SAlfred Perlstein static void	swp_pager_async_iodone(struct buf *bp);
362dee34ca4SPoul-Henning Kamp static int	swapongeom(struct thread *, struct vnode *);
36359efee01SPoul-Henning Kamp static int	swaponvp(struct thread *, struct vnode *, u_long);
36435918c55SChristian S.J. Peron static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
36524a1cce3SDavid Greenman 
3661c7c3c6aSMatthew Dillon /*
3671c7c3c6aSMatthew Dillon  * Swap bitmap functions
3681c7c3c6aSMatthew Dillon  */
369a5edd34aSPoul-Henning Kamp static void	swp_pager_freeswapspace(daddr_t blk, int npages);
370a5edd34aSPoul-Henning Kamp static daddr_t	swp_pager_getswapspace(int npages);
3711c7c3c6aSMatthew Dillon 
3721c7c3c6aSMatthew Dillon /*
3731c7c3c6aSMatthew Dillon  * Metadata functions
3741c7c3c6aSMatthew Dillon  */
375a5edd34aSPoul-Henning Kamp static struct swblock **swp_pager_hash(vm_object_t object, vm_pindex_t index);
37611caded3SAlfred Perlstein static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
37711caded3SAlfred Perlstein static void swp_pager_meta_free(vm_object_t, vm_pindex_t, daddr_t);
37811caded3SAlfred Perlstein static void swp_pager_meta_free_all(vm_object_t);
37911caded3SAlfred Perlstein static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
3801c7c3c6aSMatthew Dillon 
381e86a87e9SKonstantin Belousov static void
382e86a87e9SKonstantin Belousov swp_pager_free_nrpage(vm_page_t m)
383e86a87e9SKonstantin Belousov {
384e86a87e9SKonstantin Belousov 
385e86a87e9SKonstantin Belousov 	if (m->wire_count == 0)
386e86a87e9SKonstantin Belousov 		vm_page_free(m);
387e86a87e9SKonstantin Belousov }
388e86a87e9SKonstantin Belousov 
3891c7c3c6aSMatthew Dillon /*
3901c7c3c6aSMatthew Dillon  * SWP_SIZECHECK() -	update swap_pager_full indication
3911c7c3c6aSMatthew Dillon  *
39220d3034fSMatthew Dillon  *	update the swap_pager_almost_full indication and warn when we are
39320d3034fSMatthew Dillon  *	about to run out of swap space, using lowat/hiwat hysteresis.
39420d3034fSMatthew Dillon  *
39520d3034fSMatthew Dillon  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
3961c7c3c6aSMatthew Dillon  *
3971c7c3c6aSMatthew Dillon  *	No restrictions on call
3981c7c3c6aSMatthew Dillon  *	This routine may not block.
3991c7c3c6aSMatthew Dillon  *	This routine must be called at splvm()
4001c7c3c6aSMatthew Dillon  */
401a5edd34aSPoul-Henning Kamp static void
4022f249180SPoul-Henning Kamp swp_sizecheck(void)
4030d94caffSDavid Greenman {
40423955314SAlfred Perlstein 
4058f60c087SPoul-Henning Kamp 	if (swap_pager_avail < nswap_lowat) {
40620d3034fSMatthew Dillon 		if (swap_pager_almost_full == 0) {
4071af87c92SDavid Greenman 			printf("swap_pager: out of swap space\n");
40820d3034fSMatthew Dillon 			swap_pager_almost_full = 1;
4092b0d37a4SMatthew Dillon 		}
41020d3034fSMatthew Dillon 	} else {
41126f9a767SRodney W. Grimes 		swap_pager_full = 0;
4128f60c087SPoul-Henning Kamp 		if (swap_pager_avail > nswap_hiwat)
41320d3034fSMatthew Dillon 			swap_pager_almost_full = 0;
41426f9a767SRodney W. Grimes 	}
4151c7c3c6aSMatthew Dillon }
4161c7c3c6aSMatthew Dillon 
4171c7c3c6aSMatthew Dillon /*
418da5fd145SPeter Wemm  * SWP_PAGER_HASH() -	hash swap meta data
419da5fd145SPeter Wemm  *
420a5edd34aSPoul-Henning Kamp  *	This is an helper function which hashes the swapblk given
421da5fd145SPeter Wemm  *	the object and page index.  It returns a pointer to a pointer
422da5fd145SPeter Wemm  *	to the object, or a pointer to a NULL pointer if it could not
423da5fd145SPeter Wemm  *	find a swapblk.
424da5fd145SPeter Wemm  *
425da5fd145SPeter Wemm  *	This routine must be called at splvm().
426da5fd145SPeter Wemm  */
427a5edd34aSPoul-Henning Kamp static struct swblock **
428da5fd145SPeter Wemm swp_pager_hash(vm_object_t object, vm_pindex_t index)
429da5fd145SPeter Wemm {
430da5fd145SPeter Wemm 	struct swblock **pswap;
431da5fd145SPeter Wemm 	struct swblock *swap;
432da5fd145SPeter Wemm 
433da5fd145SPeter Wemm 	index &= ~(vm_pindex_t)SWAP_META_MASK;
434da5fd145SPeter Wemm 	pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask];
435da5fd145SPeter Wemm 	while ((swap = *pswap) != NULL) {
436da5fd145SPeter Wemm 		if (swap->swb_object == object &&
437da5fd145SPeter Wemm 		    swap->swb_index == index
438da5fd145SPeter Wemm 		) {
439da5fd145SPeter Wemm 			break;
440da5fd145SPeter Wemm 		}
441da5fd145SPeter Wemm 		pswap = &swap->swb_hnext;
442da5fd145SPeter Wemm 	}
443da5fd145SPeter Wemm 	return (pswap);
444da5fd145SPeter Wemm }
445da5fd145SPeter Wemm 
446da5fd145SPeter Wemm /*
4471c7c3c6aSMatthew Dillon  * SWAP_PAGER_INIT() -	initialize the swap pager!
4481c7c3c6aSMatthew Dillon  *
4491c7c3c6aSMatthew Dillon  *	Expected to be started from system init.  NOTE:  This code is run
4501c7c3c6aSMatthew Dillon  *	before much else so be careful what you depend on.  Most of the VM
4511c7c3c6aSMatthew Dillon  *	system has yet to be initialized at this point.
4521c7c3c6aSMatthew Dillon  */
453f5a12711SPoul-Henning Kamp static void
4542f249180SPoul-Henning Kamp swap_pager_init(void)
455df8bae1dSRodney W. Grimes {
4561c7c3c6aSMatthew Dillon 	/*
4571c7c3c6aSMatthew Dillon 	 * Initialize object lists
4581c7c3c6aSMatthew Dillon 	 */
4591c7c3c6aSMatthew Dillon 	int i;
4601c7c3c6aSMatthew Dillon 
4611c7c3c6aSMatthew Dillon 	for (i = 0; i < NOBJLISTS; ++i)
4621c7c3c6aSMatthew Dillon 		TAILQ_INIT(&swap_pager_object_list[i]);
4636008862bSJohn Baldwin 	mtx_init(&sw_alloc_mtx, "swap_pager list", NULL, MTX_DEF);
46420da9c2eSPoul-Henning Kamp 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
465df8bae1dSRodney W. Grimes 
466df8bae1dSRodney W. Grimes 	/*
4671c7c3c6aSMatthew Dillon 	 * Device Stripe, in PAGE_SIZE'd blocks
468df8bae1dSRodney W. Grimes 	 */
4691c7c3c6aSMatthew Dillon 	dmmax = SWB_NPAGES * 2;
4701c7c3c6aSMatthew Dillon }
47126f9a767SRodney W. Grimes 
472df8bae1dSRodney W. Grimes /*
4731c7c3c6aSMatthew Dillon  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
4741c7c3c6aSMatthew Dillon  *
4751c7c3c6aSMatthew Dillon  *	Expected to be started from pageout process once, prior to entering
4761c7c3c6aSMatthew Dillon  *	its main loop.
477df8bae1dSRodney W. Grimes  */
47824a1cce3SDavid Greenman void
4792f249180SPoul-Henning Kamp swap_pager_swap_init(void)
480df8bae1dSRodney W. Grimes {
48121cd6e62SSeigo Tanimura 	int n, n2;
4820d94caffSDavid Greenman 
48326f9a767SRodney W. Grimes 	/*
4841c7c3c6aSMatthew Dillon 	 * Number of in-transit swap bp operations.  Don't
4851c7c3c6aSMatthew Dillon 	 * exhaust the pbufs completely.  Make sure we
4861c7c3c6aSMatthew Dillon 	 * initialize workable values (0 will work for hysteresis
4871c7c3c6aSMatthew Dillon 	 * but it isn't very efficient).
4881c7c3c6aSMatthew Dillon 	 *
489327f4e83SMatthew Dillon 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
4901c7c3c6aSMatthew Dillon 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
4911c7c3c6aSMatthew Dillon 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
4921c7c3c6aSMatthew Dillon 	 * constrained by the swap device interleave stripe size.
493327f4e83SMatthew Dillon 	 *
494327f4e83SMatthew Dillon 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
495327f4e83SMatthew Dillon 	 * designed to prevent other I/O from having high latencies due to
496327f4e83SMatthew Dillon 	 * our pageout I/O.  The value 4 works well for one or two active swap
497327f4e83SMatthew Dillon 	 * devices but is probably a little low if you have more.  Even so,
498327f4e83SMatthew Dillon 	 * a higher value would probably generate only a limited improvement
499327f4e83SMatthew Dillon 	 * with three or four active swap devices since the system does not
500327f4e83SMatthew Dillon 	 * typically have to pageout at extreme bandwidths.   We will want
501327f4e83SMatthew Dillon 	 * at least 2 per swap devices, and 4 is a pretty good value if you
502327f4e83SMatthew Dillon 	 * have one NFS swap device due to the command/ack latency over NFS.
503327f4e83SMatthew Dillon 	 * So it all works out pretty well.
50426f9a767SRodney W. Grimes 	 */
505ad3cce20SMatthew Dillon 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
506327f4e83SMatthew Dillon 
5076d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
5081c7c3c6aSMatthew Dillon 	nsw_rcount = (nswbuf + 1) / 2;
509327f4e83SMatthew Dillon 	nsw_wcount_sync = (nswbuf + 3) / 4;
510327f4e83SMatthew Dillon 	nsw_wcount_async = 4;
511327f4e83SMatthew Dillon 	nsw_wcount_async_max = nsw_wcount_async;
5126d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
51324a1cce3SDavid Greenman 
5141c7c3c6aSMatthew Dillon 	/*
5151c7c3c6aSMatthew Dillon 	 * Initialize our zone.  Right now I'm just guessing on the number
5161c7c3c6aSMatthew Dillon 	 * we need based on the number of pages in the system.  Each swblock
5172f9e4e80SMatthew Dillon 	 * can hold 16 pages, so this is probably overkill.  This reservation
518ec61f55dSMatthew Dillon 	 * is typically limited to around 32MB by default.
5191c7c3c6aSMatthew Dillon 	 */
5202feb50bfSAttilio Rao 	n = cnt.v_page_count / 2;
5212f9e4e80SMatthew Dillon 	if (maxswzone && n > maxswzone / sizeof(struct swblock))
5222f9e4e80SMatthew Dillon 		n = maxswzone / sizeof(struct swblock);
52321cd6e62SSeigo Tanimura 	n2 = n;
524670d17b5SJeff Roberson 	swap_zone = uma_zcreate("SWAPMETA", sizeof(struct swblock), NULL, NULL,
5257827d9b0SAlan Cox 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE | UMA_ZONE_VM);
526010b1ca1SDavid Schultz 	if (swap_zone == NULL)
527010b1ca1SDavid Schultz 		panic("failed to create swap_zone.");
5288355f576SJeff Roberson 	do {
5295285558aSAlan Cox 		if (uma_zone_set_obj(swap_zone, &swap_zone_obj, n))
53061ce6eeeSAlfred Perlstein 			break;
53161ce6eeeSAlfred Perlstein 		/*
53261ce6eeeSAlfred Perlstein 		 * if the allocation failed, try a zone two thirds the
53361ce6eeeSAlfred Perlstein 		 * size of the previous attempt.
53461ce6eeeSAlfred Perlstein 		 */
53561ce6eeeSAlfred Perlstein 		n -= ((n + 2) / 3);
53661ce6eeeSAlfred Perlstein 	} while (n > 0);
53721cd6e62SSeigo Tanimura 	if (n2 != n)
53861ce6eeeSAlfred Perlstein 		printf("Swap zone entries reduced from %d to %d.\n", n2, n);
53921cd6e62SSeigo Tanimura 	n2 = n;
54024a1cce3SDavid Greenman 
5411c7c3c6aSMatthew Dillon 	/*
5421c7c3c6aSMatthew Dillon 	 * Initialize our meta-data hash table.  The swapper does not need to
5431c7c3c6aSMatthew Dillon 	 * be quite as efficient as the VM system, so we do not use an
5441c7c3c6aSMatthew Dillon 	 * oversized hash table.
5451c7c3c6aSMatthew Dillon 	 *
5461c7c3c6aSMatthew Dillon 	 * 	n: 		size of hash table, must be power of 2
5471c7c3c6aSMatthew Dillon 	 *	swhash_mask:	hash table index mask
5481c7c3c6aSMatthew Dillon 	 */
54961ce6eeeSAlfred Perlstein 	for (n = 1; n < n2 / 8; n *= 2)
5501c7c3c6aSMatthew Dillon 		;
551a163d034SWarner Losh 	swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO);
5521c7c3c6aSMatthew Dillon 	swhash_mask = n - 1;
5537827d9b0SAlan Cox 	mtx_init(&swhash_mtx, "swap_pager swhash", NULL, MTX_DEF);
55424a1cce3SDavid Greenman }
55524a1cce3SDavid Greenman 
55624a1cce3SDavid Greenman /*
5571c7c3c6aSMatthew Dillon  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
5581c7c3c6aSMatthew Dillon  *			its metadata structures.
5591c7c3c6aSMatthew Dillon  *
5601c7c3c6aSMatthew Dillon  *	This routine is called from the mmap and fork code to create a new
5611c7c3c6aSMatthew Dillon  *	OBJT_SWAP object.  We do this by creating an OBJT_DEFAULT object
5621c7c3c6aSMatthew Dillon  *	and then converting it with swp_pager_meta_build().
5631c7c3c6aSMatthew Dillon  *
5641c7c3c6aSMatthew Dillon  *	This routine may block in vm_object_allocate() and create a named
5651c7c3c6aSMatthew Dillon  *	object lookup race, so we must interlock.   We must also run at
5661c7c3c6aSMatthew Dillon  *	splvm() for the object lookup to handle races with interrupts, but
5671c7c3c6aSMatthew Dillon  *	we do not have to maintain splvm() in between the lookup and the
5681c7c3c6aSMatthew Dillon  *	add because (I believe) it is not possible to attempt to create
5691c7c3c6aSMatthew Dillon  *	a new swap object w/handle when a default object with that handle
5701c7c3c6aSMatthew Dillon  *	already exists.
57124c46d03SAlan Cox  *
57224c46d03SAlan Cox  * MPSAFE
57324a1cce3SDavid Greenman  */
574f5a12711SPoul-Henning Kamp static vm_object_t
5756cde7a16SDavid Greenman swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
5763364c323SKonstantin Belousov     vm_ooffset_t offset, struct ucred *cred)
57724a1cce3SDavid Greenman {
57824a1cce3SDavid Greenman 	vm_object_t object;
5792f7af3dbSAlan Cox 	vm_pindex_t pindex;
5803364c323SKonstantin Belousov 	struct uidinfo *uip;
5812f7af3dbSAlan Cox 
5827a8af8eeSKonstantin Belousov 	uip = NULL;
5832f7af3dbSAlan Cox 	pindex = OFF_TO_IDX(offset + PAGE_MASK + size);
58424a1cce3SDavid Greenman 	if (handle) {
585e793b779SAlan Cox 		mtx_lock(&Giant);
5861c7c3c6aSMatthew Dillon 		/*
5871c7c3c6aSMatthew Dillon 		 * Reference existing named region or allocate new one.  There
5881c7c3c6aSMatthew Dillon 		 * should not be a race here against swp_pager_meta_build()
5891c7c3c6aSMatthew Dillon 		 * as called from vm_page_remove() in regards to the lookup
5901c7c3c6aSMatthew Dillon 		 * of the handle.
5911c7c3c6aSMatthew Dillon 		 */
5920cddd8f0SMatthew Dillon 		sx_xlock(&sw_alloc_sx);
5931c7c3c6aSMatthew Dillon 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
594b5e8f167SAlan Cox 		if (object == NULL) {
5953364c323SKonstantin Belousov 			if (cred != NULL) {
5963364c323SKonstantin Belousov 				uip = cred->cr_ruidinfo;
5973364c323SKonstantin Belousov 				if (!swap_reserve_by_uid(size, uip)) {
5983364c323SKonstantin Belousov 					sx_xunlock(&sw_alloc_sx);
5993364c323SKonstantin Belousov 					mtx_unlock(&Giant);
6003364c323SKonstantin Belousov 					return (NULL);
6013364c323SKonstantin Belousov 				}
6023364c323SKonstantin Belousov 				uihold(uip);
6033364c323SKonstantin Belousov 			}
6042f7af3dbSAlan Cox 			object = vm_object_allocate(OBJT_DEFAULT, pindex);
605ee3dc7d7SAlan Cox 			VM_OBJECT_LOCK(object);
6063364c323SKonstantin Belousov 			object->handle = handle;
6073364c323SKonstantin Belousov 			if (cred != NULL) {
6083364c323SKonstantin Belousov 				object->uip = uip;
6093364c323SKonstantin Belousov 				object->charge = size;
6103364c323SKonstantin Belousov 			}
6114dcc5c2dSMatthew Dillon 			swp_pager_meta_build(object, 0, SWAPBLK_NONE);
612ee3dc7d7SAlan Cox 			VM_OBJECT_UNLOCK(object);
61324a1cce3SDavid Greenman 		}
6140cddd8f0SMatthew Dillon 		sx_xunlock(&sw_alloc_sx);
615e793b779SAlan Cox 		mtx_unlock(&Giant);
61624a1cce3SDavid Greenman 	} else {
6173364c323SKonstantin Belousov 		if (cred != NULL) {
6183364c323SKonstantin Belousov 			uip = cred->cr_ruidinfo;
6193364c323SKonstantin Belousov 			if (!swap_reserve_by_uid(size, uip))
6203364c323SKonstantin Belousov 				return (NULL);
6213364c323SKonstantin Belousov 			uihold(uip);
6223364c323SKonstantin Belousov 		}
6232f7af3dbSAlan Cox 		object = vm_object_allocate(OBJT_DEFAULT, pindex);
624ee3dc7d7SAlan Cox 		VM_OBJECT_LOCK(object);
6253364c323SKonstantin Belousov 		if (cred != NULL) {
6263364c323SKonstantin Belousov 			object->uip = uip;
6273364c323SKonstantin Belousov 			object->charge = size;
6283364c323SKonstantin Belousov 		}
6294dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
630ee3dc7d7SAlan Cox 		VM_OBJECT_UNLOCK(object);
63124a1cce3SDavid Greenman 	}
63224a1cce3SDavid Greenman 	return (object);
633df8bae1dSRodney W. Grimes }
634df8bae1dSRodney W. Grimes 
63526f9a767SRodney W. Grimes /*
6361c7c3c6aSMatthew Dillon  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
6371c7c3c6aSMatthew Dillon  *
6381c7c3c6aSMatthew Dillon  *	The swap backing for the object is destroyed.  The code is
6391c7c3c6aSMatthew Dillon  *	designed such that we can reinstantiate it later, but this
6401c7c3c6aSMatthew Dillon  *	routine is typically called only when the entire object is
6411c7c3c6aSMatthew Dillon  *	about to be destroyed.
6421c7c3c6aSMatthew Dillon  *
6431c7c3c6aSMatthew Dillon  *	This routine may block, but no longer does.
6441c7c3c6aSMatthew Dillon  *
6451c7c3c6aSMatthew Dillon  *	The object must be locked or unreferenceable.
64626f9a767SRodney W. Grimes  */
647df8bae1dSRodney W. Grimes static void
6482f249180SPoul-Henning Kamp swap_pager_dealloc(vm_object_t object)
64926f9a767SRodney W. Grimes {
6504dcc5c2dSMatthew Dillon 
65126f9a767SRodney W. Grimes 	/*
6521c7c3c6aSMatthew Dillon 	 * Remove from list right away so lookups will fail if we block for
6531c7c3c6aSMatthew Dillon 	 * pageout completion.
65426f9a767SRodney W. Grimes 	 */
655bd228075SAlan Cox 	if (object->handle != NULL) {
656a9fa2c05SAlfred Perlstein 		mtx_lock(&sw_alloc_mtx);
6571c7c3c6aSMatthew Dillon 		TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list);
658a9fa2c05SAlfred Perlstein 		mtx_unlock(&sw_alloc_mtx);
659bd228075SAlan Cox 	}
6601c7c3c6aSMatthew Dillon 
661658ad5ffSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
6621c7c3c6aSMatthew Dillon 	vm_object_pip_wait(object, "swpdea");
6631c7c3c6aSMatthew Dillon 
6641c7c3c6aSMatthew Dillon 	/*
6651c7c3c6aSMatthew Dillon 	 * Free all remaining metadata.  We only bother to free it from
6661c7c3c6aSMatthew Dillon 	 * the swap meta data.  We do not attempt to free swapblk's still
6671c7c3c6aSMatthew Dillon 	 * associated with vm_page_t's for this object.  We do not care
6681c7c3c6aSMatthew Dillon 	 * if paging is still in progress on some objects.
6691c7c3c6aSMatthew Dillon 	 */
6701c7c3c6aSMatthew Dillon 	swp_pager_meta_free_all(object);
6711c7c3c6aSMatthew Dillon }
6721c7c3c6aSMatthew Dillon 
6731c7c3c6aSMatthew Dillon /************************************************************************
6741c7c3c6aSMatthew Dillon  *			SWAP PAGER BITMAP ROUTINES			*
6751c7c3c6aSMatthew Dillon  ************************************************************************/
6761c7c3c6aSMatthew Dillon 
6771c7c3c6aSMatthew Dillon /*
6781c7c3c6aSMatthew Dillon  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
6791c7c3c6aSMatthew Dillon  *
6801c7c3c6aSMatthew Dillon  *	Allocate swap for the requested number of pages.  The starting
6811c7c3c6aSMatthew Dillon  *	swap block number (a page index) is returned or SWAPBLK_NONE
6821c7c3c6aSMatthew Dillon  *	if the allocation failed.
6831c7c3c6aSMatthew Dillon  *
6841c7c3c6aSMatthew Dillon  *	Also has the side effect of advising that somebody made a mistake
6851c7c3c6aSMatthew Dillon  *	when they configured swap and didn't configure enough.
6861c7c3c6aSMatthew Dillon  *
6871c7c3c6aSMatthew Dillon  *	Must be called at splvm() to avoid races with bitmap frees from
6881c7c3c6aSMatthew Dillon  *	vm_page_remove() aka swap_pager_page_removed().
6891c7c3c6aSMatthew Dillon  *
6901c7c3c6aSMatthew Dillon  *	This routine may not block
6911c7c3c6aSMatthew Dillon  *	This routine must be called at splvm().
6928f60c087SPoul-Henning Kamp  *
6938f60c087SPoul-Henning Kamp  *	We allocate in round-robin fashion from the configured devices.
6941c7c3c6aSMatthew Dillon  */
695a5edd34aSPoul-Henning Kamp static daddr_t
6962f249180SPoul-Henning Kamp swp_pager_getswapspace(int npages)
6971c7c3c6aSMatthew Dillon {
6981c7c3c6aSMatthew Dillon 	daddr_t blk;
6998f60c087SPoul-Henning Kamp 	struct swdevt *sp;
7008f60c087SPoul-Henning Kamp 	int i;
7011c7c3c6aSMatthew Dillon 
7028f60c087SPoul-Henning Kamp 	blk = SWAPBLK_NONE;
70320da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7048f60c087SPoul-Henning Kamp 	sp = swdevhd;
7058f60c087SPoul-Henning Kamp 	for (i = 0; i < nswapdev; i++) {
7068f60c087SPoul-Henning Kamp 		if (sp == NULL)
7078f60c087SPoul-Henning Kamp 			sp = TAILQ_FIRST(&swtailq);
7088f60c087SPoul-Henning Kamp 		if (!(sp->sw_flags & SW_CLOSING)) {
7098f60c087SPoul-Henning Kamp 			blk = blist_alloc(sp->sw_blist, npages);
7108f60c087SPoul-Henning Kamp 			if (blk != SWAPBLK_NONE) {
7118f60c087SPoul-Henning Kamp 				blk += sp->sw_first;
7128f60c087SPoul-Henning Kamp 				sp->sw_used += npages;
713d05bc129SAlan Cox 				swap_pager_avail -= npages;
7148f60c087SPoul-Henning Kamp 				swp_sizecheck();
7158f60c087SPoul-Henning Kamp 				swdevhd = TAILQ_NEXT(sp, sw_list);
716d05bc129SAlan Cox 				goto done;
7178f60c087SPoul-Henning Kamp 			}
7188f60c087SPoul-Henning Kamp 		}
7198f60c087SPoul-Henning Kamp 		sp = TAILQ_NEXT(sp, sw_list);
7208f60c087SPoul-Henning Kamp 	}
7212b0d37a4SMatthew Dillon 	if (swap_pager_full != 2) {
72220da9c2eSPoul-Henning Kamp 		printf("swap_pager_getswapspace(%d): failed\n", npages);
7232b0d37a4SMatthew Dillon 		swap_pager_full = 2;
72420d3034fSMatthew Dillon 		swap_pager_almost_full = 1;
7252b0d37a4SMatthew Dillon 	}
7268f60c087SPoul-Henning Kamp 	swdevhd = NULL;
727d05bc129SAlan Cox done:
728d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
7291c7c3c6aSMatthew Dillon 	return (blk);
73026f9a767SRodney W. Grimes }
73126f9a767SRodney W. Grimes 
732b3fed13eSDavid Schultz static int
733b3fed13eSDavid Schultz swp_pager_isondev(daddr_t blk, struct swdevt *sp)
7348f60c087SPoul-Henning Kamp {
7358f60c087SPoul-Henning Kamp 
736b3fed13eSDavid Schultz 	return (blk >= sp->sw_first && blk < sp->sw_end);
7378f60c087SPoul-Henning Kamp }
7388f60c087SPoul-Henning Kamp 
7394b03903aSPoul-Henning Kamp static void
7404b03903aSPoul-Henning Kamp swp_pager_strategy(struct buf *bp)
7414b03903aSPoul-Henning Kamp {
7424b03903aSPoul-Henning Kamp 	struct swdevt *sp;
7434b03903aSPoul-Henning Kamp 
74420da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
7454b03903aSPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7464b03903aSPoul-Henning Kamp 		if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) {
74720da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
7484b03903aSPoul-Henning Kamp 			sp->sw_strategy(bp, sp);
7494b03903aSPoul-Henning Kamp 			return;
7504b03903aSPoul-Henning Kamp 		}
7514b03903aSPoul-Henning Kamp 	}
75220da9c2eSPoul-Henning Kamp 	panic("Swapdev not found");
7534b03903aSPoul-Henning Kamp }
7544b03903aSPoul-Henning Kamp 
7558f60c087SPoul-Henning Kamp 
75626f9a767SRodney W. Grimes /*
7571c7c3c6aSMatthew Dillon  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
7581c7c3c6aSMatthew Dillon  *
7591c7c3c6aSMatthew Dillon  *	This routine returns the specified swap blocks back to the bitmap.
7601c7c3c6aSMatthew Dillon  *
7611c7c3c6aSMatthew Dillon  *	Note:  This routine may not block (it could in the old swap code),
7621c7c3c6aSMatthew Dillon  *	and through the use of the new blist routines it does not block.
7631c7c3c6aSMatthew Dillon  *
7641c7c3c6aSMatthew Dillon  *	We must be called at splvm() to avoid races with bitmap frees from
7651c7c3c6aSMatthew Dillon  *	vm_page_remove() aka swap_pager_page_removed().
7661c7c3c6aSMatthew Dillon  *
7671c7c3c6aSMatthew Dillon  *	This routine may not block
7681c7c3c6aSMatthew Dillon  *	This routine must be called at splvm().
76926f9a767SRodney W. Grimes  */
770a5edd34aSPoul-Henning Kamp static void
7718f60c087SPoul-Henning Kamp swp_pager_freeswapspace(daddr_t blk, int npages)
7720d94caffSDavid Greenman {
7738f60c087SPoul-Henning Kamp 	struct swdevt *sp;
77492da00bbSMatthew Dillon 
7757645e885SAlan Cox 	mtx_lock(&sw_dev_mtx);
7767645e885SAlan Cox 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
7777645e885SAlan Cox 		if (blk >= sp->sw_first && blk < sp->sw_end) {
77892da00bbSMatthew Dillon 			sp->sw_used -= npages;
77992da00bbSMatthew Dillon 			/*
7807645e885SAlan Cox 			 * If we are attempting to stop swapping on
7817645e885SAlan Cox 			 * this device, we don't want to mark any
7827645e885SAlan Cox 			 * blocks free lest they be reused.
78392da00bbSMatthew Dillon 			 */
7847645e885SAlan Cox 			if ((sp->sw_flags & SW_CLOSING) == 0) {
7857645e885SAlan Cox 				blist_free(sp->sw_blist, blk - sp->sw_first,
7867645e885SAlan Cox 				    npages);
7878f60c087SPoul-Henning Kamp 				swap_pager_avail += npages;
7881c7c3c6aSMatthew Dillon 				swp_sizecheck();
78926f9a767SRodney W. Grimes 			}
7907645e885SAlan Cox 			mtx_unlock(&sw_dev_mtx);
7917645e885SAlan Cox 			return;
7927645e885SAlan Cox 		}
7937645e885SAlan Cox 	}
7947645e885SAlan Cox 	panic("Swapdev not found");
7957645e885SAlan Cox }
7961c7c3c6aSMatthew Dillon 
79726f9a767SRodney W. Grimes /*
7981c7c3c6aSMatthew Dillon  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
7991c7c3c6aSMatthew Dillon  *				range within an object.
8001c7c3c6aSMatthew Dillon  *
8011c7c3c6aSMatthew Dillon  *	This is a globally accessible routine.
8021c7c3c6aSMatthew Dillon  *
8031c7c3c6aSMatthew Dillon  *	This routine removes swapblk assignments from swap metadata.
8041c7c3c6aSMatthew Dillon  *
8051c7c3c6aSMatthew Dillon  *	The external callers of this routine typically have already destroyed
8061c7c3c6aSMatthew Dillon  *	or renamed vm_page_t's associated with this range in the object so
8071c7c3c6aSMatthew Dillon  *	we should be ok.
8084dcc5c2dSMatthew Dillon  *
8094dcc5c2dSMatthew Dillon  *	This routine may be called at any spl.  We up our spl to splvm temporarily
8104dcc5c2dSMatthew Dillon  *	in order to perform the metadata removal.
81126f9a767SRodney W. Grimes  */
81226f9a767SRodney W. Grimes void
8132f249180SPoul-Henning Kamp swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
81426f9a767SRodney W. Grimes {
81523955314SAlfred Perlstein 
81619ba4c8eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
8171c7c3c6aSMatthew Dillon 	swp_pager_meta_free(object, start, size);
8184dcc5c2dSMatthew Dillon }
8194dcc5c2dSMatthew Dillon 
8204dcc5c2dSMatthew Dillon /*
8214dcc5c2dSMatthew Dillon  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
8224dcc5c2dSMatthew Dillon  *
8234dcc5c2dSMatthew Dillon  *	Assigns swap blocks to the specified range within the object.  The
8244dcc5c2dSMatthew Dillon  *	swap blocks are not zerod.  Any previous swap assignment is destroyed.
8254dcc5c2dSMatthew Dillon  *
8264dcc5c2dSMatthew Dillon  *	Returns 0 on success, -1 on failure.
8274dcc5c2dSMatthew Dillon  */
8284dcc5c2dSMatthew Dillon int
8294dcc5c2dSMatthew Dillon swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
8304dcc5c2dSMatthew Dillon {
8314dcc5c2dSMatthew Dillon 	int n = 0;
8324dcc5c2dSMatthew Dillon 	daddr_t blk = SWAPBLK_NONE;
8334dcc5c2dSMatthew Dillon 	vm_pindex_t beg = start;	/* save start index */
8344dcc5c2dSMatthew Dillon 
835ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK(object);
8364dcc5c2dSMatthew Dillon 	while (size) {
8374dcc5c2dSMatthew Dillon 		if (n == 0) {
8384dcc5c2dSMatthew Dillon 			n = BLIST_MAX_ALLOC;
8394dcc5c2dSMatthew Dillon 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
8404dcc5c2dSMatthew Dillon 				n >>= 1;
8414dcc5c2dSMatthew Dillon 				if (n == 0) {
8424dcc5c2dSMatthew Dillon 					swp_pager_meta_free(object, beg, start - beg);
843ee3dc7d7SAlan Cox 					VM_OBJECT_UNLOCK(object);
8444dcc5c2dSMatthew Dillon 					return (-1);
8454dcc5c2dSMatthew Dillon 				}
8464dcc5c2dSMatthew Dillon 			}
8474dcc5c2dSMatthew Dillon 		}
8484dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, start, blk);
8494dcc5c2dSMatthew Dillon 		--size;
8504dcc5c2dSMatthew Dillon 		++start;
8514dcc5c2dSMatthew Dillon 		++blk;
8524dcc5c2dSMatthew Dillon 		--n;
8534dcc5c2dSMatthew Dillon 	}
8544dcc5c2dSMatthew Dillon 	swp_pager_meta_free(object, start, n);
855ee3dc7d7SAlan Cox 	VM_OBJECT_UNLOCK(object);
8564dcc5c2dSMatthew Dillon 	return (0);
85726f9a767SRodney W. Grimes }
85826f9a767SRodney W. Grimes 
8590a47b48bSJohn Dyson /*
8601c7c3c6aSMatthew Dillon  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
8611c7c3c6aSMatthew Dillon  *			and destroy the source.
8621c7c3c6aSMatthew Dillon  *
8631c7c3c6aSMatthew Dillon  *	Copy any valid swapblks from the source to the destination.  In
8641c7c3c6aSMatthew Dillon  *	cases where both the source and destination have a valid swapblk,
8651c7c3c6aSMatthew Dillon  *	we keep the destination's.
8661c7c3c6aSMatthew Dillon  *
8671c7c3c6aSMatthew Dillon  *	This routine is allowed to block.  It may block allocating metadata
8681c7c3c6aSMatthew Dillon  *	indirectly through swp_pager_meta_build() or if paging is still in
8691c7c3c6aSMatthew Dillon  *	progress on the source.
8701c7c3c6aSMatthew Dillon  *
8714dcc5c2dSMatthew Dillon  *	This routine can be called at any spl
8724dcc5c2dSMatthew Dillon  *
8731c7c3c6aSMatthew Dillon  *	XXX vm_page_collapse() kinda expects us not to block because we
8741c7c3c6aSMatthew Dillon  *	supposedly do not need to allocate memory, but for the moment we
8751c7c3c6aSMatthew Dillon  *	*may* have to get a little memory from the zone allocator, but
8761c7c3c6aSMatthew Dillon  *	it is taken from the interrupt memory.  We should be ok.
8771c7c3c6aSMatthew Dillon  *
8781c7c3c6aSMatthew Dillon  *	The source object contains no vm_page_t's (which is just as well)
8791c7c3c6aSMatthew Dillon  *
8801c7c3c6aSMatthew Dillon  *	The source object is of type OBJT_SWAP.
8811c7c3c6aSMatthew Dillon  *
8824dcc5c2dSMatthew Dillon  *	The source and destination objects must be locked or
8834dcc5c2dSMatthew Dillon  *	inaccessible (XXX are they ?)
88426f9a767SRodney W. Grimes  */
88526f9a767SRodney W. Grimes void
8862f249180SPoul-Henning Kamp swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
8872f249180SPoul-Henning Kamp     vm_pindex_t offset, int destroysource)
88826f9a767SRodney W. Grimes {
889a316d390SJohn Dyson 	vm_pindex_t i;
8904dcc5c2dSMatthew Dillon 
891c7c8dd7eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(srcobject, MA_OWNED);
892c7c8dd7eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(dstobject, MA_OWNED);
8930cddd8f0SMatthew Dillon 
89426f9a767SRodney W. Grimes 	/*
8951c7c3c6aSMatthew Dillon 	 * If destroysource is set, we remove the source object from the
8961c7c3c6aSMatthew Dillon 	 * swap_pager internal queue now.
89726f9a767SRodney W. Grimes 	 */
898cbd8ec09SJohn Dyson 	if (destroysource) {
899bd228075SAlan Cox 		if (srcobject->handle != NULL) {
900a9fa2c05SAlfred Perlstein 			mtx_lock(&sw_alloc_mtx);
9011c7c3c6aSMatthew Dillon 			TAILQ_REMOVE(
9021c7c3c6aSMatthew Dillon 			    NOBJLIST(srcobject->handle),
9031c7c3c6aSMatthew Dillon 			    srcobject,
9041c7c3c6aSMatthew Dillon 			    pager_object_list
9051c7c3c6aSMatthew Dillon 			);
906a9fa2c05SAlfred Perlstein 			mtx_unlock(&sw_alloc_mtx);
907cbd8ec09SJohn Dyson 		}
908bd228075SAlan Cox 	}
90926f9a767SRodney W. Grimes 
9101c7c3c6aSMatthew Dillon 	/*
9111c7c3c6aSMatthew Dillon 	 * transfer source to destination.
9121c7c3c6aSMatthew Dillon 	 */
9131c7c3c6aSMatthew Dillon 	for (i = 0; i < dstobject->size; ++i) {
9141c7c3c6aSMatthew Dillon 		daddr_t dstaddr;
9151c7c3c6aSMatthew Dillon 
9161c7c3c6aSMatthew Dillon 		/*
9171c7c3c6aSMatthew Dillon 		 * Locate (without changing) the swapblk on the destination,
9181c7c3c6aSMatthew Dillon 		 * unless it is invalid in which case free it silently, or
9191c7c3c6aSMatthew Dillon 		 * if the destination is a resident page, in which case the
9201c7c3c6aSMatthew Dillon 		 * source is thrown away.
9211c7c3c6aSMatthew Dillon 		 */
9221c7c3c6aSMatthew Dillon 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
9231c7c3c6aSMatthew Dillon 
9241c7c3c6aSMatthew Dillon 		if (dstaddr == SWAPBLK_NONE) {
9251c7c3c6aSMatthew Dillon 			/*
9261c7c3c6aSMatthew Dillon 			 * Destination has no swapblk and is not resident,
9271c7c3c6aSMatthew Dillon 			 * copy source.
9281c7c3c6aSMatthew Dillon 			 */
9291c7c3c6aSMatthew Dillon 			daddr_t srcaddr;
9301c7c3c6aSMatthew Dillon 
9311c7c3c6aSMatthew Dillon 			srcaddr = swp_pager_meta_ctl(
9321c7c3c6aSMatthew Dillon 			    srcobject,
9331c7c3c6aSMatthew Dillon 			    i + offset,
9341c7c3c6aSMatthew Dillon 			    SWM_POP
9351c7c3c6aSMatthew Dillon 			);
9361c7c3c6aSMatthew Dillon 
937ee3dc7d7SAlan Cox 			if (srcaddr != SWAPBLK_NONE) {
938c7c8dd7eSAlan Cox 				/*
939c7c8dd7eSAlan Cox 				 * swp_pager_meta_build() can sleep.
940c7c8dd7eSAlan Cox 				 */
941c7c8dd7eSAlan Cox 				vm_object_pip_add(srcobject, 1);
942c7c8dd7eSAlan Cox 				VM_OBJECT_UNLOCK(srcobject);
943c7c8dd7eSAlan Cox 				vm_object_pip_add(dstobject, 1);
9444dcc5c2dSMatthew Dillon 				swp_pager_meta_build(dstobject, i, srcaddr);
945c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(dstobject);
946c7c8dd7eSAlan Cox 				VM_OBJECT_LOCK(srcobject);
947c7c8dd7eSAlan Cox 				vm_object_pip_wakeup(srcobject);
948ee3dc7d7SAlan Cox 			}
9491c7c3c6aSMatthew Dillon 		} else {
9501c7c3c6aSMatthew Dillon 			/*
9511c7c3c6aSMatthew Dillon 			 * Destination has valid swapblk or it is represented
9521c7c3c6aSMatthew Dillon 			 * by a resident page.  We destroy the sourceblock.
9531c7c3c6aSMatthew Dillon 			 */
9541c7c3c6aSMatthew Dillon 
9551c7c3c6aSMatthew Dillon 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
9561c7c3c6aSMatthew Dillon 		}
95726f9a767SRodney W. Grimes 	}
95826f9a767SRodney W. Grimes 
95926f9a767SRodney W. Grimes 	/*
9601c7c3c6aSMatthew Dillon 	 * Free left over swap blocks in source.
9611c7c3c6aSMatthew Dillon 	 *
9621c7c3c6aSMatthew Dillon 	 * We have to revert the type to OBJT_DEFAULT so we do not accidently
9631c7c3c6aSMatthew Dillon 	 * double-remove the object from the swap queues.
96426f9a767SRodney W. Grimes 	 */
965c0877f10SJohn Dyson 	if (destroysource) {
9661c7c3c6aSMatthew Dillon 		swp_pager_meta_free_all(srcobject);
9671c7c3c6aSMatthew Dillon 		/*
9681c7c3c6aSMatthew Dillon 		 * Reverting the type is not necessary, the caller is going
9691c7c3c6aSMatthew Dillon 		 * to destroy srcobject directly, but I'm doing it here
970956f3135SPhilippe Charnier 		 * for consistency since we've removed the object from its
9711c7c3c6aSMatthew Dillon 		 * queues.
9721c7c3c6aSMatthew Dillon 		 */
9731c7c3c6aSMatthew Dillon 		srcobject->type = OBJT_DEFAULT;
974c0877f10SJohn Dyson 	}
97526f9a767SRodney W. Grimes }
97626f9a767SRodney W. Grimes 
977df8bae1dSRodney W. Grimes /*
9781c7c3c6aSMatthew Dillon  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
9791c7c3c6aSMatthew Dillon  *				the requested page.
9801c7c3c6aSMatthew Dillon  *
9811c7c3c6aSMatthew Dillon  *	We determine whether good backing store exists for the requested
9821c7c3c6aSMatthew Dillon  *	page and return TRUE if it does, FALSE if it doesn't.
9831c7c3c6aSMatthew Dillon  *
9841c7c3c6aSMatthew Dillon  *	If TRUE, we also try to determine how much valid, contiguous backing
9851c7c3c6aSMatthew Dillon  *	store exists before and after the requested page within a reasonable
9861c7c3c6aSMatthew Dillon  *	distance.  We do not try to restrict it to the swap device stripe
9871c7c3c6aSMatthew Dillon  *	(that is handled in getpages/putpages).  It probably isn't worth
9881c7c3c6aSMatthew Dillon  *	doing here.
989df8bae1dSRodney W. Grimes  */
9905ea4972cSAlan Cox static boolean_t
9912f249180SPoul-Henning Kamp swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after)
99226f9a767SRodney W. Grimes {
9931c7c3c6aSMatthew Dillon 	daddr_t blk0;
99426f9a767SRodney W. Grimes 
995ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
9961c7c3c6aSMatthew Dillon 	/*
9971c7c3c6aSMatthew Dillon 	 * do we have good backing store at the requested index ?
9981c7c3c6aSMatthew Dillon 	 */
9991c7c3c6aSMatthew Dillon 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
10001c7c3c6aSMatthew Dillon 
10014dcc5c2dSMatthew Dillon 	if (blk0 == SWAPBLK_NONE) {
10021c7c3c6aSMatthew Dillon 		if (before)
100324a1cce3SDavid Greenman 			*before = 0;
10041c7c3c6aSMatthew Dillon 		if (after)
100524a1cce3SDavid Greenman 			*after = 0;
100626f9a767SRodney W. Grimes 		return (FALSE);
100726f9a767SRodney W. Grimes 	}
100826f9a767SRodney W. Grimes 
100926f9a767SRodney W. Grimes 	/*
10101c7c3c6aSMatthew Dillon 	 * find backwards-looking contiguous good backing store
1011e47ed70bSJohn Dyson 	 */
10121c7c3c6aSMatthew Dillon 	if (before != NULL) {
101326f9a767SRodney W. Grimes 		int i;
10140d94caffSDavid Greenman 
10151c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
10161c7c3c6aSMatthew Dillon 			daddr_t blk;
10171c7c3c6aSMatthew Dillon 
10181c7c3c6aSMatthew Dillon 			if (i > pindex)
10191c7c3c6aSMatthew Dillon 				break;
10201c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
10211c7c3c6aSMatthew Dillon 			if (blk != blk0 - i)
10221c7c3c6aSMatthew Dillon 				break;
1023ffc82b0aSJohn Dyson 		}
10241c7c3c6aSMatthew Dillon 		*before = (i - 1);
102526f9a767SRodney W. Grimes 	}
102626f9a767SRodney W. Grimes 
102726f9a767SRodney W. Grimes 	/*
10281c7c3c6aSMatthew Dillon 	 * find forward-looking contiguous good backing store
102926f9a767SRodney W. Grimes 	 */
10301c7c3c6aSMatthew Dillon 	if (after != NULL) {
10311c7c3c6aSMatthew Dillon 		int i;
10321c7c3c6aSMatthew Dillon 
10331c7c3c6aSMatthew Dillon 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
10341c7c3c6aSMatthew Dillon 			daddr_t blk;
10351c7c3c6aSMatthew Dillon 
10361c7c3c6aSMatthew Dillon 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
10371c7c3c6aSMatthew Dillon 			if (blk != blk0 + i)
10381c7c3c6aSMatthew Dillon 				break;
103926f9a767SRodney W. Grimes 		}
10401c7c3c6aSMatthew Dillon 		*after = (i - 1);
10411c7c3c6aSMatthew Dillon 	}
10421c7c3c6aSMatthew Dillon 	return (TRUE);
10431c7c3c6aSMatthew Dillon }
10441c7c3c6aSMatthew Dillon 
10451c7c3c6aSMatthew Dillon /*
10461c7c3c6aSMatthew Dillon  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
10471c7c3c6aSMatthew Dillon  *
10481c7c3c6aSMatthew Dillon  *	This removes any associated swap backing store, whether valid or
10491c7c3c6aSMatthew Dillon  *	not, from the page.
10501c7c3c6aSMatthew Dillon  *
10511c7c3c6aSMatthew Dillon  *	This routine is typically called when a page is made dirty, at
10521c7c3c6aSMatthew Dillon  *	which point any associated swap can be freed.  MADV_FREE also
10531c7c3c6aSMatthew Dillon  *	calls us in a special-case situation
10541c7c3c6aSMatthew Dillon  *
10551c7c3c6aSMatthew Dillon  *	NOTE!!!  If the page is clean and the swap was valid, the caller
10561c7c3c6aSMatthew Dillon  *	should make the page dirty before calling this routine.  This routine
10571c7c3c6aSMatthew Dillon  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
10581c7c3c6aSMatthew Dillon  *	depends on it.
10591c7c3c6aSMatthew Dillon  *
10601c7c3c6aSMatthew Dillon  *	This routine may not block
10614dcc5c2dSMatthew Dillon  *	This routine must be called at splvm()
10621c7c3c6aSMatthew Dillon  */
10631c7c3c6aSMatthew Dillon static void
10642f249180SPoul-Henning Kamp swap_pager_unswapped(vm_page_t m)
10651c7c3c6aSMatthew Dillon {
10662f249180SPoul-Henning Kamp 
1067ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
10681c7c3c6aSMatthew Dillon 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
10691c7c3c6aSMatthew Dillon }
10701c7c3c6aSMatthew Dillon 
10711c7c3c6aSMatthew Dillon /*
10721c7c3c6aSMatthew Dillon  * SWAP_PAGER_GETPAGES() - bring pages in from swap
10731c7c3c6aSMatthew Dillon  *
10741c7c3c6aSMatthew Dillon  *	Attempt to retrieve (m, count) pages from backing store, but make
10751c7c3c6aSMatthew Dillon  *	sure we retrieve at least m[reqpage].  We try to load in as large
10761c7c3c6aSMatthew Dillon  *	a chunk surrounding m[reqpage] as is contiguous in swap and which
10771c7c3c6aSMatthew Dillon  *	belongs to the same object.
10781c7c3c6aSMatthew Dillon  *
10791c7c3c6aSMatthew Dillon  *	The code is designed for asynchronous operation and
10801c7c3c6aSMatthew Dillon  *	immediate-notification of 'reqpage' but tends not to be
10811c7c3c6aSMatthew Dillon  *	used that way.  Please do not optimize-out this algorithmic
10821c7c3c6aSMatthew Dillon  *	feature, I intend to improve on it in the future.
10831c7c3c6aSMatthew Dillon  *
10841c7c3c6aSMatthew Dillon  *	The parent has a single vm_object_pip_add() reference prior to
10851c7c3c6aSMatthew Dillon  *	calling us and we should return with the same.
10861c7c3c6aSMatthew Dillon  *
10871c7c3c6aSMatthew Dillon  *	The parent has BUSY'd the pages.  We should return with 'm'
10881c7c3c6aSMatthew Dillon  *	left busy, but the others adjusted.
10891c7c3c6aSMatthew Dillon  */
1090f708ef1bSPoul-Henning Kamp static int
10912f249180SPoul-Henning Kamp swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
1092df8bae1dSRodney W. Grimes {
10931c7c3c6aSMatthew Dillon 	struct buf *bp;
10941c7c3c6aSMatthew Dillon 	vm_page_t mreq;
109526f9a767SRodney W. Grimes 	int i;
109626f9a767SRodney W. Grimes 	int j;
10971c7c3c6aSMatthew Dillon 	daddr_t blk;
10980d94caffSDavid Greenman 
10991c7c3c6aSMatthew Dillon 	mreq = m[reqpage];
11001c7c3c6aSMatthew Dillon 
1101e04e4bacSPoul-Henning Kamp 	KASSERT(mreq->object == object,
1102e04e4bacSPoul-Henning Kamp 	    ("swap_pager_getpages: object mismatch %p/%p",
1103e04e4bacSPoul-Henning Kamp 	    object, mreq->object));
1104e04e4bacSPoul-Henning Kamp 
11051c7c3c6aSMatthew Dillon 	/*
11061c7c3c6aSMatthew Dillon 	 * Calculate range to retrieve.  The pages have already been assigned
1107751221fdSPoul-Henning Kamp 	 * their swapblks.  We require a *contiguous* range but we know it to
1108751221fdSPoul-Henning Kamp 	 * not span devices.   If we do not supply it, bad things
11094dcc5c2dSMatthew Dillon 	 * happen.  Note that blk, iblk & jblk can be SWAPBLK_NONE, but the
11104dcc5c2dSMatthew Dillon 	 * loops are set up such that the case(s) are handled implicitly.
11114dcc5c2dSMatthew Dillon 	 *
11120b6ace47SAlan Cox 	 * The swp_*() calls must be made with the object locked.
11131c7c3c6aSMatthew Dillon 	 */
11141c7c3c6aSMatthew Dillon 	blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0);
11151c7c3c6aSMatthew Dillon 
11161c7c3c6aSMatthew Dillon 	for (i = reqpage - 1; i >= 0; --i) {
11171c7c3c6aSMatthew Dillon 		daddr_t iblk;
11181c7c3c6aSMatthew Dillon 
11191c7c3c6aSMatthew Dillon 		iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0);
11201c7c3c6aSMatthew Dillon 		if (blk != iblk + (reqpage - i))
112126f9a767SRodney W. Grimes 			break;
112226f9a767SRodney W. Grimes 	}
11231c7c3c6aSMatthew Dillon 	++i;
11241c7c3c6aSMatthew Dillon 
11251c7c3c6aSMatthew Dillon 	for (j = reqpage + 1; j < count; ++j) {
11261c7c3c6aSMatthew Dillon 		daddr_t jblk;
11271c7c3c6aSMatthew Dillon 
11281c7c3c6aSMatthew Dillon 		jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0);
11291c7c3c6aSMatthew Dillon 		if (blk != jblk - (j - reqpage))
11301c7c3c6aSMatthew Dillon 			break;
11311c7c3c6aSMatthew Dillon 	}
11321c7c3c6aSMatthew Dillon 
11331c7c3c6aSMatthew Dillon 	/*
11341c7c3c6aSMatthew Dillon 	 * free pages outside our collection range.   Note: we never free
11351c7c3c6aSMatthew Dillon 	 * mreq, it must remain busy throughout.
11361c7c3c6aSMatthew Dillon 	 */
1137071a1710SAlan Cox 	if (0 < i || j < count) {
11381c7c3c6aSMatthew Dillon 		int k;
11391c7c3c6aSMatthew Dillon 
1140*2965a453SKip Macy 
1141*2965a453SKip Macy 		for (k = 0; k < i; ++k) {
1142*2965a453SKip Macy 			vm_page_lock(m[k]);
1143071a1710SAlan Cox 			vm_page_lock_queues();
1144e86a87e9SKonstantin Belousov 			swp_pager_free_nrpage(m[k]);
1145ab9abe5dSAlan Cox 			vm_page_unlock_queues();
1146*2965a453SKip Macy 			vm_page_unlock(m[k]);
1147*2965a453SKip Macy 		}
1148*2965a453SKip Macy 		for (k = j; k < count; ++k) {
1149*2965a453SKip Macy 			vm_page_lock(m[k]);
1150*2965a453SKip Macy 			vm_page_lock_queues();
1151*2965a453SKip Macy 			swp_pager_free_nrpage(m[k]);
1152*2965a453SKip Macy 			vm_page_unlock_queues();
1153*2965a453SKip Macy 			vm_page_unlock(m[k]);
1154*2965a453SKip Macy 		}
1155071a1710SAlan Cox 	}
11561c7c3c6aSMatthew Dillon 
11571c7c3c6aSMatthew Dillon 	/*
11584dcc5c2dSMatthew Dillon 	 * Return VM_PAGER_FAIL if we have nothing to do.  Return mreq
11594dcc5c2dSMatthew Dillon 	 * still busy, but the others unbusied.
11601c7c3c6aSMatthew Dillon 	 */
11614dcc5c2dSMatthew Dillon 	if (blk == SWAPBLK_NONE)
116226f9a767SRodney W. Grimes 		return (VM_PAGER_FAIL);
1163df8bae1dSRodney W. Grimes 
116416f62314SDavid Greenman 	/*
11658630c117SAlan Cox 	 * Getpbuf() can sleep.
11668630c117SAlan Cox 	 */
11678630c117SAlan Cox 	VM_OBJECT_UNLOCK(object);
11688630c117SAlan Cox 	/*
116916f62314SDavid Greenman 	 * Get a swap buffer header to perform the IO
117016f62314SDavid Greenman 	 */
11711c7c3c6aSMatthew Dillon 	bp = getpbuf(&nsw_rcount);
11725e04322aSPoul-Henning Kamp 	bp->b_flags |= B_PAGING;
117326f9a767SRodney W. Grimes 
117416f62314SDavid Greenman 	/*
117516f62314SDavid Greenman 	 * map our page(s) into kva for input
117616f62314SDavid Greenman 	 */
1177d68d828bSAlan Cox 	pmap_qenter((vm_offset_t)bp->b_data, m + i, j - i);
11781c7c3c6aSMatthew Dillon 
117921144e3bSPoul-Henning Kamp 	bp->b_iocmd = BIO_READ;
11801c7c3c6aSMatthew Dillon 	bp->b_iodone = swp_pager_async_iodone;
1181fdcc1cc0SJohn Baldwin 	bp->b_rcred = crhold(thread0.td_ucred);
1182fdcc1cc0SJohn Baldwin 	bp->b_wcred = crhold(thread0.td_ucred);
11831c7c3c6aSMatthew Dillon 	bp->b_blkno = blk - (reqpage - i);
11841c7c3c6aSMatthew Dillon 	bp->b_bcount = PAGE_SIZE * (j - i);
11851c7c3c6aSMatthew Dillon 	bp->b_bufsize = PAGE_SIZE * (j - i);
11861c7c3c6aSMatthew Dillon 	bp->b_pager.pg_reqpage = reqpage - i;
11871c7c3c6aSMatthew Dillon 
11888630c117SAlan Cox 	VM_OBJECT_LOCK(object);
11891c7c3c6aSMatthew Dillon 	{
11901c7c3c6aSMatthew Dillon 		int k;
11911c7c3c6aSMatthew Dillon 
11921c7c3c6aSMatthew Dillon 		for (k = i; k < j; ++k) {
11931c7c3c6aSMatthew Dillon 			bp->b_pages[k - i] = m[k];
11945786be7cSAlan Cox 			m[k]->oflags |= VPO_SWAPINPROG;
11951c7c3c6aSMatthew Dillon 		}
11961c7c3c6aSMatthew Dillon 	}
11971c7c3c6aSMatthew Dillon 	bp->b_npages = j - i;
119826f9a767SRodney W. Grimes 
1199b4b70819SAttilio Rao 	PCPU_INC(cnt.v_swapin);
1200b4b70819SAttilio Rao 	PCPU_ADD(cnt.v_swappgsin, bp->b_npages);
12011c7c3c6aSMatthew Dillon 
1202df8bae1dSRodney W. Grimes 	/*
12031c7c3c6aSMatthew Dillon 	 * We still hold the lock on mreq, and our automatic completion routine
12041c7c3c6aSMatthew Dillon 	 * does not remove it.
1205df8bae1dSRodney W. Grimes 	 */
1206071a1710SAlan Cox 	vm_object_pip_add(object, bp->b_npages);
1207071a1710SAlan Cox 	VM_OBJECT_UNLOCK(object);
12081c7c3c6aSMatthew Dillon 
12091c7c3c6aSMatthew Dillon 	/*
12101c7c3c6aSMatthew Dillon 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
12111c7c3c6aSMatthew Dillon 	 * this point because we automatically release it on completion.
12121c7c3c6aSMatthew Dillon 	 * Instead, we look at the one page we are interested in which we
12131c7c3c6aSMatthew Dillon 	 * still hold a lock on even through the I/O completion.
12141c7c3c6aSMatthew Dillon 	 *
12151c7c3c6aSMatthew Dillon 	 * The other pages in our m[] array are also released on completion,
12161c7c3c6aSMatthew Dillon 	 * so we cannot assume they are valid anymore either.
12171c7c3c6aSMatthew Dillon 	 *
1218c37a77eeSPoul-Henning Kamp 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
12191c7c3c6aSMatthew Dillon 	 */
1220b890cb2cSPeter Wemm 	BUF_KERNPROC(bp);
12214b03903aSPoul-Henning Kamp 	swp_pager_strategy(bp);
122226f9a767SRodney W. Grimes 
122326f9a767SRodney W. Grimes 	/*
12245786be7cSAlan Cox 	 * wait for the page we want to complete.  VPO_SWAPINPROG is always
12251c7c3c6aSMatthew Dillon 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
12261c7c3c6aSMatthew Dillon 	 * is set in the meta-data.
122726f9a767SRodney W. Grimes 	 */
122891449ce9SAlan Cox 	VM_OBJECT_LOCK(object);
12295786be7cSAlan Cox 	while ((mreq->oflags & VPO_SWAPINPROG) != 0) {
12305786be7cSAlan Cox 		mreq->oflags |= VPO_WANTED;
1231b4b70819SAttilio Rao 		PCPU_INC(cnt.v_intrans);
123291449ce9SAlan Cox 		if (msleep(mreq, VM_OBJECT_MTX(object), PSWP, "swread", hz*20)) {
12339bd86a98SBruce M Simpson 			printf(
1234c5690651SPoul-Henning Kamp "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1235c5690651SPoul-Henning Kamp 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
12361c7c3c6aSMatthew Dillon 		}
12371b119d9dSDavid Greenman 	}
123826f9a767SRodney W. Grimes 
123926f9a767SRodney W. Grimes 	/*
1240a1287949SEivind Eklund 	 * mreq is left busied after completion, but all the other pages
12411c7c3c6aSMatthew Dillon 	 * are freed.  If we had an unrecoverable read error the page will
12421c7c3c6aSMatthew Dillon 	 * not be valid.
124326f9a767SRodney W. Grimes 	 */
12441c7c3c6aSMatthew Dillon 	if (mreq->valid != VM_PAGE_BITS_ALL) {
12451c7c3c6aSMatthew Dillon 		return (VM_PAGER_ERROR);
124626f9a767SRodney W. Grimes 	} else {
12471c7c3c6aSMatthew Dillon 		return (VM_PAGER_OK);
124826f9a767SRodney W. Grimes 	}
12491c7c3c6aSMatthew Dillon 
12501c7c3c6aSMatthew Dillon 	/*
12511c7c3c6aSMatthew Dillon 	 * A final note: in a low swap situation, we cannot deallocate swap
12521c7c3c6aSMatthew Dillon 	 * and mark a page dirty here because the caller is likely to mark
12531c7c3c6aSMatthew Dillon 	 * the page clean when we return, causing the page to possibly revert
12541c7c3c6aSMatthew Dillon 	 * to all-zero's later.
12551c7c3c6aSMatthew Dillon 	 */
1256df8bae1dSRodney W. Grimes }
1257df8bae1dSRodney W. Grimes 
12581c7c3c6aSMatthew Dillon /*
12591c7c3c6aSMatthew Dillon  *	swap_pager_putpages:
12601c7c3c6aSMatthew Dillon  *
12611c7c3c6aSMatthew Dillon  *	Assign swap (if necessary) and initiate I/O on the specified pages.
12621c7c3c6aSMatthew Dillon  *
12631c7c3c6aSMatthew Dillon  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
12641c7c3c6aSMatthew Dillon  *	are automatically converted to SWAP objects.
12651c7c3c6aSMatthew Dillon  *
1266ea3aecf5SPeter Wemm  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
12671c7c3c6aSMatthew Dillon  *	vm_page reservation system coupled with properly written VFS devices
12681c7c3c6aSMatthew Dillon  *	should ensure that no low-memory deadlock occurs.  This is an area
12691c7c3c6aSMatthew Dillon  *	which needs work.
12701c7c3c6aSMatthew Dillon  *
12711c7c3c6aSMatthew Dillon  *	The parent has N vm_object_pip_add() references prior to
12721c7c3c6aSMatthew Dillon  *	calling us and will remove references for rtvals[] that are
12731c7c3c6aSMatthew Dillon  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
12741c7c3c6aSMatthew Dillon  *	completion.
12751c7c3c6aSMatthew Dillon  *
12761c7c3c6aSMatthew Dillon  *	The parent has soft-busy'd the pages it passes us and will unbusy
12771c7c3c6aSMatthew Dillon  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
12781c7c3c6aSMatthew Dillon  *	We need to unbusy the rest on I/O completion.
12791c7c3c6aSMatthew Dillon  */
1280e4542174SMatthew Dillon void
12812f249180SPoul-Henning Kamp swap_pager_putpages(vm_object_t object, vm_page_t *m, int count,
12822f249180SPoul-Henning Kamp     boolean_t sync, int *rtvals)
1283df8bae1dSRodney W. Grimes {
12841c7c3c6aSMatthew Dillon 	int i;
12851c7c3c6aSMatthew Dillon 	int n = 0;
1286df8bae1dSRodney W. Grimes 
12871c7c3c6aSMatthew Dillon 	if (count && m[0]->object != object) {
12887036145bSMaxim Konovalov 		panic("swap_pager_putpages: object mismatch %p/%p",
12891c7c3c6aSMatthew Dillon 		    object,
12901c7c3c6aSMatthew Dillon 		    m[0]->object
12911c7c3c6aSMatthew Dillon 		);
12921c7c3c6aSMatthew Dillon 	}
1293ee3dc7d7SAlan Cox 
12941c7c3c6aSMatthew Dillon 	/*
12951c7c3c6aSMatthew Dillon 	 * Step 1
12961c7c3c6aSMatthew Dillon 	 *
12971c7c3c6aSMatthew Dillon 	 * Turn object into OBJT_SWAP
12981c7c3c6aSMatthew Dillon 	 * check for bogus sysops
12991c7c3c6aSMatthew Dillon 	 * force sync if not pageout process
13001c7c3c6aSMatthew Dillon 	 */
13014dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
13024dcc5c2dSMatthew Dillon 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
1303ee3dc7d7SAlan Cox 	VM_OBJECT_UNLOCK(object);
1304e47ed70bSJohn Dyson 
1305e47ed70bSJohn Dyson 	if (curproc != pageproc)
1306e47ed70bSJohn Dyson 		sync = TRUE;
130726f9a767SRodney W. Grimes 
13081c7c3c6aSMatthew Dillon 	/*
13091c7c3c6aSMatthew Dillon 	 * Step 2
13101c7c3c6aSMatthew Dillon 	 *
1311ad3cce20SMatthew Dillon 	 * Update nsw parameters from swap_async_max sysctl values.
1312ad3cce20SMatthew Dillon 	 * Do not let the sysop crash the machine with bogus numbers.
1313327f4e83SMatthew Dillon 	 */
13146d541bf1SJohn Baldwin 	mtx_lock(&pbuf_mtx);
1315327f4e83SMatthew Dillon 	if (swap_async_max != nsw_wcount_async_max) {
1316327f4e83SMatthew Dillon 		int n;
1317327f4e83SMatthew Dillon 
1318327f4e83SMatthew Dillon 		/*
1319327f4e83SMatthew Dillon 		 * limit range
1320327f4e83SMatthew Dillon 		 */
1321327f4e83SMatthew Dillon 		if ((n = swap_async_max) > nswbuf / 2)
1322327f4e83SMatthew Dillon 			n = nswbuf / 2;
1323327f4e83SMatthew Dillon 		if (n < 1)
1324327f4e83SMatthew Dillon 			n = 1;
1325327f4e83SMatthew Dillon 		swap_async_max = n;
1326327f4e83SMatthew Dillon 
1327327f4e83SMatthew Dillon 		/*
1328327f4e83SMatthew Dillon 		 * Adjust difference ( if possible ).  If the current async
1329327f4e83SMatthew Dillon 		 * count is too low, we may not be able to make the adjustment
1330327f4e83SMatthew Dillon 		 * at this time.
1331327f4e83SMatthew Dillon 		 */
1332327f4e83SMatthew Dillon 		n -= nsw_wcount_async_max;
1333327f4e83SMatthew Dillon 		if (nsw_wcount_async + n >= 0) {
1334327f4e83SMatthew Dillon 			nsw_wcount_async += n;
1335327f4e83SMatthew Dillon 			nsw_wcount_async_max += n;
1336327f4e83SMatthew Dillon 			wakeup(&nsw_wcount_async);
1337327f4e83SMatthew Dillon 		}
1338327f4e83SMatthew Dillon 	}
13396d541bf1SJohn Baldwin 	mtx_unlock(&pbuf_mtx);
1340327f4e83SMatthew Dillon 
1341327f4e83SMatthew Dillon 	/*
1342327f4e83SMatthew Dillon 	 * Step 3
1343327f4e83SMatthew Dillon 	 *
13441c7c3c6aSMatthew Dillon 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
13451c7c3c6aSMatthew Dillon 	 * The page is left dirty until the pageout operation completes
13461c7c3c6aSMatthew Dillon 	 * successfully.
13471c7c3c6aSMatthew Dillon 	 */
13481c7c3c6aSMatthew Dillon 	for (i = 0; i < count; i += n) {
13491c7c3c6aSMatthew Dillon 		int j;
13501c7c3c6aSMatthew Dillon 		struct buf *bp;
1351a316d390SJohn Dyson 		daddr_t blk;
135226f9a767SRodney W. Grimes 
1353df8bae1dSRodney W. Grimes 		/*
13541c7c3c6aSMatthew Dillon 		 * Maximum I/O size is limited by a number of factors.
1355df8bae1dSRodney W. Grimes 		 */
13561c7c3c6aSMatthew Dillon 		n = min(BLIST_MAX_ALLOC, count - i);
1357327f4e83SMatthew Dillon 		n = min(n, nsw_cluster_max);
13581c7c3c6aSMatthew Dillon 
135926f9a767SRodney W. Grimes 		/*
13601c7c3c6aSMatthew Dillon 		 * Get biggest block of swap we can.  If we fail, fall
13611c7c3c6aSMatthew Dillon 		 * back and try to allocate a smaller block.  Don't go
13621c7c3c6aSMatthew Dillon 		 * overboard trying to allocate space if it would overly
13631c7c3c6aSMatthew Dillon 		 * fragment swap.
136426f9a767SRodney W. Grimes 		 */
13651c7c3c6aSMatthew Dillon 		while (
13661c7c3c6aSMatthew Dillon 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
13671c7c3c6aSMatthew Dillon 		    n > 4
13681c7c3c6aSMatthew Dillon 		) {
13691c7c3c6aSMatthew Dillon 			n >>= 1;
137026f9a767SRodney W. Grimes 		}
13711c7c3c6aSMatthew Dillon 		if (blk == SWAPBLK_NONE) {
13724dcc5c2dSMatthew Dillon 			for (j = 0; j < n; ++j)
13731c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_FAIL;
13741c7c3c6aSMatthew Dillon 			continue;
137526f9a767SRodney W. Grimes 		}
137626f9a767SRodney W. Grimes 
137726f9a767SRodney W. Grimes 		/*
13781c7c3c6aSMatthew Dillon 		 * All I/O parameters have been satisfied, build the I/O
13791c7c3c6aSMatthew Dillon 		 * request and assign the swap space.
138026f9a767SRodney W. Grimes 		 */
1381327f4e83SMatthew Dillon 		if (sync == TRUE) {
1382327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_sync);
1383327f4e83SMatthew Dillon 		} else {
1384327f4e83SMatthew Dillon 			bp = getpbuf(&nsw_wcount_async);
138521144e3bSPoul-Henning Kamp 			bp->b_flags = B_ASYNC;
1386327f4e83SMatthew Dillon 		}
13875e04322aSPoul-Henning Kamp 		bp->b_flags |= B_PAGING;
1388912e4ae9SPoul-Henning Kamp 		bp->b_iocmd = BIO_WRITE;
138926f9a767SRodney W. Grimes 
13901c7c3c6aSMatthew Dillon 		pmap_qenter((vm_offset_t)bp->b_data, &m[i], n);
13911c7c3c6aSMatthew Dillon 
1392fdcc1cc0SJohn Baldwin 		bp->b_rcred = crhold(thread0.td_ucred);
1393fdcc1cc0SJohn Baldwin 		bp->b_wcred = crhold(thread0.td_ucred);
13941c7c3c6aSMatthew Dillon 		bp->b_bcount = PAGE_SIZE * n;
13951c7c3c6aSMatthew Dillon 		bp->b_bufsize = PAGE_SIZE * n;
13961c7c3c6aSMatthew Dillon 		bp->b_blkno = blk;
1397e47ed70bSJohn Dyson 
1398ee3dc7d7SAlan Cox 		VM_OBJECT_LOCK(object);
13991c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j) {
14001c7c3c6aSMatthew Dillon 			vm_page_t mreq = m[i+j];
14011c7c3c6aSMatthew Dillon 
14021c7c3c6aSMatthew Dillon 			swp_pager_meta_build(
14031c7c3c6aSMatthew Dillon 			    mreq->object,
14041c7c3c6aSMatthew Dillon 			    mreq->pindex,
14054dcc5c2dSMatthew Dillon 			    blk + j
14061c7c3c6aSMatthew Dillon 			);
14077dbf82dcSMatthew Dillon 			vm_page_dirty(mreq);
14081c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_OK;
14091c7c3c6aSMatthew Dillon 
14105786be7cSAlan Cox 			mreq->oflags |= VPO_SWAPINPROG;
14111c7c3c6aSMatthew Dillon 			bp->b_pages[j] = mreq;
14121c7c3c6aSMatthew Dillon 		}
1413ee3dc7d7SAlan Cox 		VM_OBJECT_UNLOCK(object);
14141c7c3c6aSMatthew Dillon 		bp->b_npages = n;
1415a5296b05SJulian Elischer 		/*
1416a5296b05SJulian Elischer 		 * Must set dirty range for NFS to work.
1417a5296b05SJulian Elischer 		 */
1418a5296b05SJulian Elischer 		bp->b_dirtyoff = 0;
1419a5296b05SJulian Elischer 		bp->b_dirtyend = bp->b_bcount;
14201c7c3c6aSMatthew Dillon 
1421b4b70819SAttilio Rao 		PCPU_INC(cnt.v_swapout);
1422b4b70819SAttilio Rao 		PCPU_ADD(cnt.v_swappgsout, bp->b_npages);
142326f9a767SRodney W. Grimes 
142426f9a767SRodney W. Grimes 		/*
14251c7c3c6aSMatthew Dillon 		 * asynchronous
14261c7c3c6aSMatthew Dillon 		 *
1427c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
142826f9a767SRodney W. Grimes 		 */
14291c7c3c6aSMatthew Dillon 		if (sync == FALSE) {
14301c7c3c6aSMatthew Dillon 			bp->b_iodone = swp_pager_async_iodone;
143167812eacSKirk McKusick 			BUF_KERNPROC(bp);
14324b03903aSPoul-Henning Kamp 			swp_pager_strategy(bp);
14331c7c3c6aSMatthew Dillon 
14341c7c3c6aSMatthew Dillon 			for (j = 0; j < n; ++j)
14351c7c3c6aSMatthew Dillon 				rtvals[i+j] = VM_PAGER_PEND;
143623955314SAlfred Perlstein 			/* restart outter loop */
14371c7c3c6aSMatthew Dillon 			continue;
143826f9a767SRodney W. Grimes 		}
1439e47ed70bSJohn Dyson 
144026f9a767SRodney W. Grimes 		/*
14411c7c3c6aSMatthew Dillon 		 * synchronous
14421c7c3c6aSMatthew Dillon 		 *
1443c37a77eeSPoul-Henning Kamp 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
14441c7c3c6aSMatthew Dillon 		 */
14452c840b1fSAlan Cox 		bp->b_iodone = bdone;
14464b03903aSPoul-Henning Kamp 		swp_pager_strategy(bp);
14471c7c3c6aSMatthew Dillon 
14481c7c3c6aSMatthew Dillon 		/*
14491c7c3c6aSMatthew Dillon 		 * Wait for the sync I/O to complete, then update rtvals.
14501c7c3c6aSMatthew Dillon 		 * We just set the rtvals[] to VM_PAGER_PEND so we can call
14511c7c3c6aSMatthew Dillon 		 * our async completion routine at the end, thus avoiding a
14521c7c3c6aSMatthew Dillon 		 * double-free.
145326f9a767SRodney W. Grimes 		 */
14542c840b1fSAlan Cox 		bwait(bp, PVM, "swwrt");
14551c7c3c6aSMatthew Dillon 		for (j = 0; j < n; ++j)
14561c7c3c6aSMatthew Dillon 			rtvals[i+j] = VM_PAGER_PEND;
14571c7c3c6aSMatthew Dillon 		/*
14581c7c3c6aSMatthew Dillon 		 * Now that we are through with the bp, we can call the
14591c7c3c6aSMatthew Dillon 		 * normal async completion, which frees everything up.
14601c7c3c6aSMatthew Dillon 		 */
14611c7c3c6aSMatthew Dillon 		swp_pager_async_iodone(bp);
14621c7c3c6aSMatthew Dillon 	}
14632e3b314dSAlan Cox 	VM_OBJECT_LOCK(object);
14641c7c3c6aSMatthew Dillon }
14651c7c3c6aSMatthew Dillon 
14661c7c3c6aSMatthew Dillon /*
14671c7c3c6aSMatthew Dillon  *	swp_pager_async_iodone:
14681c7c3c6aSMatthew Dillon  *
14691c7c3c6aSMatthew Dillon  *	Completion routine for asynchronous reads and writes from/to swap.
14701c7c3c6aSMatthew Dillon  *	Also called manually by synchronous code to finish up a bp.
14711c7c3c6aSMatthew Dillon  *
14721c7c3c6aSMatthew Dillon  *	For READ operations, the pages are PG_BUSY'd.  For WRITE operations,
14731c7c3c6aSMatthew Dillon  *	the pages are vm_page_t->busy'd.  For READ operations, we PG_BUSY
14741c7c3c6aSMatthew Dillon  *	unbusy all pages except the 'main' request page.  For WRITE
14751c7c3c6aSMatthew Dillon  *	operations, we vm_page_t->busy'd unbusy all pages ( we can do this
14761c7c3c6aSMatthew Dillon  *	because we marked them all VM_PAGER_PEND on return from putpages ).
14771c7c3c6aSMatthew Dillon  *
14781c7c3c6aSMatthew Dillon  *	This routine may not block.
14791c7c3c6aSMatthew Dillon  */
14801c7c3c6aSMatthew Dillon static void
14812f249180SPoul-Henning Kamp swp_pager_async_iodone(struct buf *bp)
14821c7c3c6aSMatthew Dillon {
14831c7c3c6aSMatthew Dillon 	int i;
14841c7c3c6aSMatthew Dillon 	vm_object_t object = NULL;
14851c7c3c6aSMatthew Dillon 
14861c7c3c6aSMatthew Dillon 	/*
14871c7c3c6aSMatthew Dillon 	 * report error
14881c7c3c6aSMatthew Dillon 	 */
1489c244d2deSPoul-Henning Kamp 	if (bp->b_ioflags & BIO_ERROR) {
14901c7c3c6aSMatthew Dillon 		printf(
14911c7c3c6aSMatthew Dillon 		    "swap_pager: I/O error - %s failed; blkno %ld,"
14921c7c3c6aSMatthew Dillon 			"size %ld, error %d\n",
149321144e3bSPoul-Henning Kamp 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
14941c7c3c6aSMatthew Dillon 		    (long)bp->b_blkno,
14951c7c3c6aSMatthew Dillon 		    (long)bp->b_bcount,
14961c7c3c6aSMatthew Dillon 		    bp->b_error
14971c7c3c6aSMatthew Dillon 		);
14981c7c3c6aSMatthew Dillon 	}
14991c7c3c6aSMatthew Dillon 
15001c7c3c6aSMatthew Dillon 	/*
150126f9a767SRodney W. Grimes 	 * remove the mapping for kernel virtual
150226f9a767SRodney W. Grimes 	 */
15031c7c3c6aSMatthew Dillon 	pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
150426f9a767SRodney W. Grimes 
150533a609ecSAlan Cox 	if (bp->b_npages) {
150633a609ecSAlan Cox 		object = bp->b_pages[0]->object;
150733a609ecSAlan Cox 		VM_OBJECT_LOCK(object);
150833a609ecSAlan Cox 	}
1509*2965a453SKip Macy 
151026f9a767SRodney W. Grimes 	/*
15111c7c3c6aSMatthew Dillon 	 * cleanup pages.  If an error occurs writing to swap, we are in
15121c7c3c6aSMatthew Dillon 	 * very serious trouble.  If it happens to be a disk error, though,
15131c7c3c6aSMatthew Dillon 	 * we may be able to recover by reassigning the swap later on.  So
15141c7c3c6aSMatthew Dillon 	 * in this case we remove the m->swapblk assignment for the page
15151c7c3c6aSMatthew Dillon 	 * but do not free it in the rlist.  The errornous block(s) are thus
15161c7c3c6aSMatthew Dillon 	 * never reallocated as swap.  Redirty the page and continue.
151726f9a767SRodney W. Grimes 	 */
15181c7c3c6aSMatthew Dillon 	for (i = 0; i < bp->b_npages; ++i) {
15191c7c3c6aSMatthew Dillon 		vm_page_t m = bp->b_pages[i];
1520e47ed70bSJohn Dyson 
1521*2965a453SKip Macy 		vm_page_lock(m);
1522*2965a453SKip Macy 		vm_page_lock_queues();
15235786be7cSAlan Cox 		m->oflags &= ~VPO_SWAPINPROG;
1524e47ed70bSJohn Dyson 
1525c244d2deSPoul-Henning Kamp 		if (bp->b_ioflags & BIO_ERROR) {
1526ffc82b0aSJohn Dyson 			/*
15271c7c3c6aSMatthew Dillon 			 * If an error occurs I'd love to throw the swapblk
15281c7c3c6aSMatthew Dillon 			 * away without freeing it back to swapspace, so it
15291c7c3c6aSMatthew Dillon 			 * can never be used again.  But I can't from an
15301c7c3c6aSMatthew Dillon 			 * interrupt.
1531ffc82b0aSJohn Dyson 			 */
153221144e3bSPoul-Henning Kamp 			if (bp->b_iocmd == BIO_READ) {
15331c7c3c6aSMatthew Dillon 				/*
15341c7c3c6aSMatthew Dillon 				 * When reading, reqpage needs to stay
15351c7c3c6aSMatthew Dillon 				 * locked for the parent, but all other
15361c7c3c6aSMatthew Dillon 				 * pages can be freed.  We still want to
15371c7c3c6aSMatthew Dillon 				 * wakeup the parent waiting on the page,
15381c7c3c6aSMatthew Dillon 				 * though.  ( also: pg_reqpage can be -1 and
15391c7c3c6aSMatthew Dillon 				 * not match anything ).
15401c7c3c6aSMatthew Dillon 				 *
15411c7c3c6aSMatthew Dillon 				 * We have to wake specifically requested pages
15425786be7cSAlan Cox 				 * up too because we cleared VPO_SWAPINPROG and
15431c7c3c6aSMatthew Dillon 				 * someone may be waiting for that.
15441c7c3c6aSMatthew Dillon 				 *
15451c7c3c6aSMatthew Dillon 				 * NOTE: for reads, m->dirty will probably
1546956f3135SPhilippe Charnier 				 * be overridden by the original caller of
15471c7c3c6aSMatthew Dillon 				 * getpages so don't play cute tricks here.
15481c7c3c6aSMatthew Dillon 				 */
15491c7c3c6aSMatthew Dillon 				m->valid = 0;
15501c7c3c6aSMatthew Dillon 				if (i != bp->b_pager.pg_reqpage)
1551e86a87e9SKonstantin Belousov 					swp_pager_free_nrpage(m);
15521c7c3c6aSMatthew Dillon 				else
15531c7c3c6aSMatthew Dillon 					vm_page_flash(m);
15541c7c3c6aSMatthew Dillon 				/*
15551c7c3c6aSMatthew Dillon 				 * If i == bp->b_pager.pg_reqpage, do not wake
15561c7c3c6aSMatthew Dillon 				 * the page up.  The caller needs to.
15571c7c3c6aSMatthew Dillon 				 */
15581c7c3c6aSMatthew Dillon 			} else {
15591c7c3c6aSMatthew Dillon 				/*
15601c7c3c6aSMatthew Dillon 				 * If a write error occurs, reactivate page
15611c7c3c6aSMatthew Dillon 				 * so it doesn't clog the inactive list,
15621c7c3c6aSMatthew Dillon 				 * then finish the I/O.
15631c7c3c6aSMatthew Dillon 				 */
15647dbf82dcSMatthew Dillon 				vm_page_dirty(m);
15651c7c3c6aSMatthew Dillon 				vm_page_activate(m);
15661c7c3c6aSMatthew Dillon 				vm_page_io_finish(m);
15671c7c3c6aSMatthew Dillon 			}
156821144e3bSPoul-Henning Kamp 		} else if (bp->b_iocmd == BIO_READ) {
15691c7c3c6aSMatthew Dillon 			/*
15701c7c3c6aSMatthew Dillon 			 * NOTE: for reads, m->dirty will probably be
1571956f3135SPhilippe Charnier 			 * overridden by the original caller of getpages so
15721c7c3c6aSMatthew Dillon 			 * we cannot set them in order to free the underlying
15731c7c3c6aSMatthew Dillon 			 * swap in a low-swap situation.  I don't think we'd
15741c7c3c6aSMatthew Dillon 			 * want to do that anyway, but it was an optimization
15751c7c3c6aSMatthew Dillon 			 * that existed in the old swapper for a time before
15761c7c3c6aSMatthew Dillon 			 * it got ripped out due to precisely this problem.
15771c7c3c6aSMatthew Dillon 			 *
15781c7c3c6aSMatthew Dillon 			 * If not the requested page then deactivate it.
15791c7c3c6aSMatthew Dillon 			 *
15801c7c3c6aSMatthew Dillon 			 * Note that the requested page, reqpage, is left
15811c7c3c6aSMatthew Dillon 			 * busied, but we still have to wake it up.  The
15821c7c3c6aSMatthew Dillon 			 * other pages are released (unbusied) by
1583a80982c1SAlan Cox 			 * vm_page_wakeup().
15841c7c3c6aSMatthew Dillon 			 */
1585016a3c93SAlan Cox 			KASSERT(!pmap_page_is_mapped(m),
1586016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is mapped", m));
15871c7c3c6aSMatthew Dillon 			m->valid = VM_PAGE_BITS_ALL;
1588016a3c93SAlan Cox 			KASSERT(m->dirty == 0,
1589016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is dirty", m));
15901c7c3c6aSMatthew Dillon 
15911c7c3c6aSMatthew Dillon 			/*
15921c7c3c6aSMatthew Dillon 			 * We have to wake specifically requested pages
15935786be7cSAlan Cox 			 * up too because we cleared VPO_SWAPINPROG and
15941c7c3c6aSMatthew Dillon 			 * could be waiting for it in getpages.  However,
15951c7c3c6aSMatthew Dillon 			 * be sure to not unbusy getpages specifically
15961c7c3c6aSMatthew Dillon 			 * requested page - getpages expects it to be
15971c7c3c6aSMatthew Dillon 			 * left busy.
15981c7c3c6aSMatthew Dillon 			 */
15991c7c3c6aSMatthew Dillon 			if (i != bp->b_pager.pg_reqpage) {
16001c7c3c6aSMatthew Dillon 				vm_page_deactivate(m);
16011c7c3c6aSMatthew Dillon 				vm_page_wakeup(m);
16021c7c3c6aSMatthew Dillon 			} else {
16031c7c3c6aSMatthew Dillon 				vm_page_flash(m);
16041c7c3c6aSMatthew Dillon 			}
16051c7c3c6aSMatthew Dillon 		} else {
16061c7c3c6aSMatthew Dillon 			/*
1607016a3c93SAlan Cox 			 * For write success, clear the dirty
16081c7c3c6aSMatthew Dillon 			 * status, then finish the I/O ( which decrements the
16091c7c3c6aSMatthew Dillon 			 * busy count and possibly wakes waiter's up ).
16101c7c3c6aSMatthew Dillon 			 */
1611016a3c93SAlan Cox 			KASSERT((m->flags & PG_WRITEABLE) == 0,
1612016a3c93SAlan Cox 			    ("swp_pager_async_iodone: page %p is not write"
1613016a3c93SAlan Cox 			    " protected", m));
1614c52e7044SAlan Cox 			vm_page_undirty(m);
16151c7c3c6aSMatthew Dillon 			vm_page_io_finish(m);
16162c840b1fSAlan Cox 			if (vm_page_count_severe())
16172c840b1fSAlan Cox 				vm_page_try_to_cache(m);
1618ffc82b0aSJohn Dyson 		}
161940eab1e9SAlan Cox 		vm_page_unlock_queues();
1620*2965a453SKip Macy 		vm_page_unlock(m);
1621*2965a453SKip Macy 	}
162226f9a767SRodney W. Grimes 
16231c7c3c6aSMatthew Dillon 	/*
16241c7c3c6aSMatthew Dillon 	 * adjust pip.  NOTE: the original parent may still have its own
16251c7c3c6aSMatthew Dillon 	 * pip refs on the object.
16261c7c3c6aSMatthew Dillon 	 */
16270d420ad3SAlan Cox 	if (object != NULL) {
16281c7c3c6aSMatthew Dillon 		vm_object_pip_wakeupn(object, bp->b_npages);
16290d420ad3SAlan Cox 		VM_OBJECT_UNLOCK(object);
16300d420ad3SAlan Cox 	}
163126f9a767SRodney W. Grimes 
16321c7c3c6aSMatthew Dillon 	/*
1633100650deSOlivier Houchard 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1634100650deSOlivier Houchard 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1635100650deSOlivier Houchard 	 * trigger a KASSERT in relpbuf().
1636100650deSOlivier Houchard 	 */
1637100650deSOlivier Houchard 	if (bp->b_vp) {
1638100650deSOlivier Houchard 		    bp->b_vp = NULL;
1639100650deSOlivier Houchard 		    bp->b_bufobj = NULL;
1640100650deSOlivier Houchard 	}
1641100650deSOlivier Houchard 	/*
16421c7c3c6aSMatthew Dillon 	 * release the physical I/O buffer
16431c7c3c6aSMatthew Dillon 	 */
1644327f4e83SMatthew Dillon 	relpbuf(
1645327f4e83SMatthew Dillon 	    bp,
164621144e3bSPoul-Henning Kamp 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1647327f4e83SMatthew Dillon 		((bp->b_flags & B_ASYNC) ?
1648327f4e83SMatthew Dillon 		    &nsw_wcount_async :
1649327f4e83SMatthew Dillon 		    &nsw_wcount_sync
1650327f4e83SMatthew Dillon 		)
1651327f4e83SMatthew Dillon 	    )
1652327f4e83SMatthew Dillon 	);
165326f9a767SRodney W. Grimes }
16541c7c3c6aSMatthew Dillon 
165592da00bbSMatthew Dillon /*
165692da00bbSMatthew Dillon  *	swap_pager_isswapped:
165792da00bbSMatthew Dillon  *
165892da00bbSMatthew Dillon  *	Return 1 if at least one page in the given object is paged
165992da00bbSMatthew Dillon  *	out to the given swap device.
166092da00bbSMatthew Dillon  *
166192da00bbSMatthew Dillon  *	This routine may not block.
166292da00bbSMatthew Dillon  */
16638f60c087SPoul-Henning Kamp int
16648f60c087SPoul-Henning Kamp swap_pager_isswapped(vm_object_t object, struct swdevt *sp)
16658f60c087SPoul-Henning Kamp {
166692da00bbSMatthew Dillon 	daddr_t index = 0;
166792da00bbSMatthew Dillon 	int bcount;
166892da00bbSMatthew Dillon 	int i;
166992da00bbSMatthew Dillon 
167017cd3642SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
1671f6bcadc4SDavid Schultz 	if (object->type != OBJT_SWAP)
1672f6bcadc4SDavid Schultz 		return (0);
1673f6bcadc4SDavid Schultz 
1674b3fed13eSDavid Schultz 	mtx_lock(&swhash_mtx);
167592da00bbSMatthew Dillon 	for (bcount = 0; bcount < object->un_pager.swp.swp_bcount; bcount++) {
167692da00bbSMatthew Dillon 		struct swblock *swap;
167792da00bbSMatthew Dillon 
167892da00bbSMatthew Dillon 		if ((swap = *swp_pager_hash(object, index)) != NULL) {
167992da00bbSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
1680b3fed13eSDavid Schultz 				if (swp_pager_isondev(swap->swb_pages[i], sp)) {
16817827d9b0SAlan Cox 					mtx_unlock(&swhash_mtx);
1682b3fed13eSDavid Schultz 					return (1);
168392da00bbSMatthew Dillon 				}
168492da00bbSMatthew Dillon 			}
16857827d9b0SAlan Cox 		}
168692da00bbSMatthew Dillon 		index += SWAP_META_PAGES;
168792da00bbSMatthew Dillon 		if (index > 0x20000000)
168892da00bbSMatthew Dillon 			panic("swap_pager_isswapped: failed to locate all swap meta blocks");
168992da00bbSMatthew Dillon 	}
1690b3fed13eSDavid Schultz 	mtx_unlock(&swhash_mtx);
1691b3fed13eSDavid Schultz 	return (0);
169292da00bbSMatthew Dillon }
169392da00bbSMatthew Dillon 
169492da00bbSMatthew Dillon /*
169592da00bbSMatthew Dillon  * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in
169692da00bbSMatthew Dillon  *
169792da00bbSMatthew Dillon  *	This routine dissociates the page at the given index within a
169892da00bbSMatthew Dillon  *	swap block from its backing store, paging it in if necessary.
169992da00bbSMatthew Dillon  *	If the page is paged in, it is placed in the inactive queue,
170092da00bbSMatthew Dillon  *	since it had its backing store ripped out from under it.
170192da00bbSMatthew Dillon  *	We also attempt to swap in all other pages in the swap block,
170292da00bbSMatthew Dillon  *	we only guarantee that the one at the specified index is
170392da00bbSMatthew Dillon  *	paged in.
170492da00bbSMatthew Dillon  *
170592da00bbSMatthew Dillon  *	XXX - The code to page the whole block in doesn't work, so we
170692da00bbSMatthew Dillon  *	      revert to the one-by-one behavior for now.  Sigh.
170792da00bbSMatthew Dillon  */
170862a59e8fSWarner Losh static inline void
1709b3fed13eSDavid Schultz swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex)
171092da00bbSMatthew Dillon {
171192da00bbSMatthew Dillon 	vm_page_t m;
171292da00bbSMatthew Dillon 
171392da00bbSMatthew Dillon 	vm_object_pip_add(object, 1);
1714b3fed13eSDavid Schultz 	m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
171592da00bbSMatthew Dillon 	if (m->valid == VM_PAGE_BITS_ALL) {
171692da00bbSMatthew Dillon 		vm_object_pip_subtract(object, 1);
1717*2965a453SKip Macy 		vm_page_lock(m);
171892da00bbSMatthew Dillon 		vm_page_lock_queues();
171992da00bbSMatthew Dillon 		vm_page_activate(m);
172092da00bbSMatthew Dillon 		vm_page_dirty(m);
172192da00bbSMatthew Dillon 		vm_page_unlock_queues();
1722*2965a453SKip Macy 		vm_page_unlock(m);
172366bdd5d6SAlan Cox 		vm_page_wakeup(m);
172492da00bbSMatthew Dillon 		vm_pager_page_unswapped(m);
172592da00bbSMatthew Dillon 		return;
172692da00bbSMatthew Dillon 	}
172792da00bbSMatthew Dillon 
1728b3fed13eSDavid Schultz 	if (swap_pager_getpages(object, &m, 1, 0) != VM_PAGER_OK)
172992da00bbSMatthew Dillon 		panic("swap_pager_force_pagein: read from swap failed");/*XXX*/
173092da00bbSMatthew Dillon 	vm_object_pip_subtract(object, 1);
1731*2965a453SKip Macy 	vm_page_lock(m);
173292da00bbSMatthew Dillon 	vm_page_lock_queues();
173392da00bbSMatthew Dillon 	vm_page_dirty(m);
173492da00bbSMatthew Dillon 	vm_page_dontneed(m);
173592da00bbSMatthew Dillon 	vm_page_unlock_queues();
1736*2965a453SKip Macy 	vm_page_unlock(m);
173766bdd5d6SAlan Cox 	vm_page_wakeup(m);
173892da00bbSMatthew Dillon 	vm_pager_page_unswapped(m);
173992da00bbSMatthew Dillon }
174092da00bbSMatthew Dillon 
174192da00bbSMatthew Dillon /*
174292da00bbSMatthew Dillon  *	swap_pager_swapoff:
174392da00bbSMatthew Dillon  *
174492da00bbSMatthew Dillon  *	Page in all of the pages that have been paged out to the
174592da00bbSMatthew Dillon  *	given device.  The corresponding blocks in the bitmap must be
174692da00bbSMatthew Dillon  *	marked as allocated and the device must be flagged SW_CLOSING.
174792da00bbSMatthew Dillon  *	There may be no processes swapped out to the device.
174892da00bbSMatthew Dillon  *
174992da00bbSMatthew Dillon  *	This routine may block.
175092da00bbSMatthew Dillon  */
1751e9c0cc15SPoul-Henning Kamp static void
1752b3fed13eSDavid Schultz swap_pager_swapoff(struct swdevt *sp)
175392da00bbSMatthew Dillon {
175492da00bbSMatthew Dillon 	struct swblock *swap;
17558bc61209SDavid Schultz 	int i, j, retries;
175692da00bbSMatthew Dillon 
175792da00bbSMatthew Dillon 	GIANT_REQUIRED;
175892da00bbSMatthew Dillon 
17598bc61209SDavid Schultz 	retries = 0;
176092da00bbSMatthew Dillon full_rescan:
1761b3fed13eSDavid Schultz 	mtx_lock(&swhash_mtx);
176292da00bbSMatthew Dillon 	for (i = 0; i <= swhash_mask; i++) { /* '<=' is correct here */
176392da00bbSMatthew Dillon restart:
1764b3fed13eSDavid Schultz 		for (swap = swhash[i]; swap != NULL; swap = swap->swb_hnext) {
1765b3fed13eSDavid Schultz 			vm_object_t object = swap->swb_object;
1766b3fed13eSDavid Schultz 			vm_pindex_t pindex = swap->swb_index;
176792da00bbSMatthew Dillon                         for (j = 0; j < SWAP_META_PAGES; ++j) {
1768b3fed13eSDavid Schultz                                 if (swp_pager_isondev(swap->swb_pages[j], sp)) {
1769b3fed13eSDavid Schultz 					/* avoid deadlock */
1770b3fed13eSDavid Schultz 					if (!VM_OBJECT_TRYLOCK(object)) {
177192da00bbSMatthew Dillon 						break;
1772b3fed13eSDavid Schultz 					} else {
1773b3fed13eSDavid Schultz 						mtx_unlock(&swhash_mtx);
1774b3fed13eSDavid Schultz 						swp_pager_force_pagein(object,
1775b3fed13eSDavid Schultz 						    pindex + j);
1776b3fed13eSDavid Schultz 						VM_OBJECT_UNLOCK(object);
1777b3fed13eSDavid Schultz 						mtx_lock(&swhash_mtx);
177892da00bbSMatthew Dillon 						goto restart;
177992da00bbSMatthew Dillon 					}
1780b3fed13eSDavid Schultz 				}
1781b3fed13eSDavid Schultz                         }
1782b3fed13eSDavid Schultz 		}
178392da00bbSMatthew Dillon 	}
1784d536c58fSAlan Cox 	mtx_unlock(&swhash_mtx);
17858bc61209SDavid Schultz 	if (sp->sw_used) {
178692da00bbSMatthew Dillon 		/*
17878bc61209SDavid Schultz 		 * Objects may be locked or paging to the device being
17888bc61209SDavid Schultz 		 * removed, so we will miss their pages and need to
17898bc61209SDavid Schultz 		 * make another pass.  We have marked this device as
17908bc61209SDavid Schultz 		 * SW_CLOSING, so the activity should finish soon.
179192da00bbSMatthew Dillon 		 */
17928bc61209SDavid Schultz 		retries++;
17938bc61209SDavid Schultz 		if (retries > 100) {
17948bc61209SDavid Schultz 			panic("swapoff: failed to locate %d swap blocks",
17958bc61209SDavid Schultz 			    sp->sw_used);
17968bc61209SDavid Schultz 		}
17974d70511aSJohn Baldwin 		pause("swpoff", hz / 20);
179892da00bbSMatthew Dillon 		goto full_rescan;
179992da00bbSMatthew Dillon 	}
180092da00bbSMatthew Dillon }
180192da00bbSMatthew Dillon 
18021c7c3c6aSMatthew Dillon /************************************************************************
18031c7c3c6aSMatthew Dillon  *				SWAP META DATA 				*
18041c7c3c6aSMatthew Dillon  ************************************************************************
18051c7c3c6aSMatthew Dillon  *
18061c7c3c6aSMatthew Dillon  *	These routines manipulate the swap metadata stored in the
18074dcc5c2dSMatthew Dillon  *	OBJT_SWAP object.  All swp_*() routines must be called at
18084dcc5c2dSMatthew Dillon  *	splvm() because swap can be freed up by the low level vm_page
18094dcc5c2dSMatthew Dillon  *	code which might be called from interrupts beyond what splbio() covers.
18101c7c3c6aSMatthew Dillon  *
18114dcc5c2dSMatthew Dillon  *	Swap metadata is implemented with a global hash and not directly
18124dcc5c2dSMatthew Dillon  *	linked into the object.  Instead the object simply contains
18134dcc5c2dSMatthew Dillon  *	appropriate tracking counters.
18141c7c3c6aSMatthew Dillon  */
18151c7c3c6aSMatthew Dillon 
18161c7c3c6aSMatthew Dillon /*
18171c7c3c6aSMatthew Dillon  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
18181c7c3c6aSMatthew Dillon  *
18191c7c3c6aSMatthew Dillon  *	We first convert the object to a swap object if it is a default
18201c7c3c6aSMatthew Dillon  *	object.
18211c7c3c6aSMatthew Dillon  *
18221c7c3c6aSMatthew Dillon  *	The specified swapblk is added to the object's swap metadata.  If
18231c7c3c6aSMatthew Dillon  *	the swapblk is not valid, it is freed instead.  Any previously
18241c7c3c6aSMatthew Dillon  *	assigned swapblk is freed.
18254dcc5c2dSMatthew Dillon  *
18264dcc5c2dSMatthew Dillon  *	This routine must be called at splvm(), except when used to convert
18274dcc5c2dSMatthew Dillon  *	an OBJT_DEFAULT object into an OBJT_SWAP object.
18281c7c3c6aSMatthew Dillon  */
18291c7c3c6aSMatthew Dillon static void
18302f249180SPoul-Henning Kamp swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
18312f249180SPoul-Henning Kamp {
18321c7c3c6aSMatthew Dillon 	struct swblock *swap;
18331c7c3c6aSMatthew Dillon 	struct swblock **pswap;
183423f09d50SIan Dowse 	int idx;
18351c7c3c6aSMatthew Dillon 
1836ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
18371c7c3c6aSMatthew Dillon 	/*
18381c7c3c6aSMatthew Dillon 	 * Convert default object to swap object if necessary
18391c7c3c6aSMatthew Dillon 	 */
18401c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP) {
18411c7c3c6aSMatthew Dillon 		object->type = OBJT_SWAP;
18421c7c3c6aSMatthew Dillon 		object->un_pager.swp.swp_bcount = 0;
18431c7c3c6aSMatthew Dillon 
18441c7c3c6aSMatthew Dillon 		if (object->handle != NULL) {
1845bd228075SAlan Cox 			mtx_lock(&sw_alloc_mtx);
18461c7c3c6aSMatthew Dillon 			TAILQ_INSERT_TAIL(
18471c7c3c6aSMatthew Dillon 			    NOBJLIST(object->handle),
18481c7c3c6aSMatthew Dillon 			    object,
18491c7c3c6aSMatthew Dillon 			    pager_object_list
18501c7c3c6aSMatthew Dillon 			);
1851a9fa2c05SAlfred Perlstein 			mtx_unlock(&sw_alloc_mtx);
18521c7c3c6aSMatthew Dillon 		}
1853bd228075SAlan Cox 	}
18541c7c3c6aSMatthew Dillon 
18551c7c3c6aSMatthew Dillon 	/*
18561c7c3c6aSMatthew Dillon 	 * Locate hash entry.  If not found create, but if we aren't adding
18574dcc5c2dSMatthew Dillon 	 * anything just return.  If we run out of space in the map we wait
18584dcc5c2dSMatthew Dillon 	 * and, since the hash table may have changed, retry.
18591c7c3c6aSMatthew Dillon 	 */
18604dcc5c2dSMatthew Dillon retry:
18617827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
186223f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
18631c7c3c6aSMatthew Dillon 
18641c7c3c6aSMatthew Dillon 	if ((swap = *pswap) == NULL) {
18651c7c3c6aSMatthew Dillon 		int i;
18661c7c3c6aSMatthew Dillon 
18671c7c3c6aSMatthew Dillon 		if (swapblk == SWAPBLK_NONE)
18687827d9b0SAlan Cox 			goto done;
18691c7c3c6aSMatthew Dillon 
1870670d17b5SJeff Roberson 		swap = *pswap = uma_zalloc(swap_zone, M_NOWAIT);
18714dcc5c2dSMatthew Dillon 		if (swap == NULL) {
18727827d9b0SAlan Cox 			mtx_unlock(&swhash_mtx);
1873ee3dc7d7SAlan Cox 			VM_OBJECT_UNLOCK(object);
18742025d69bSKonstantin Belousov 			if (uma_zone_exhausted(swap_zone)) {
18752025d69bSKonstantin Belousov 				printf("swap zone exhausted, increase kern.maxswzone\n");
18762025d69bSKonstantin Belousov 				vm_pageout_oom(VM_OOM_SWAPZ);
18772025d69bSKonstantin Belousov 				pause("swzonex", 10);
18782025d69bSKonstantin Belousov 			} else
18794dcc5c2dSMatthew Dillon 				VM_WAIT;
1880ee3dc7d7SAlan Cox 			VM_OBJECT_LOCK(object);
18814dcc5c2dSMatthew Dillon 			goto retry;
18824dcc5c2dSMatthew Dillon 		}
1883670d17b5SJeff Roberson 
18841c7c3c6aSMatthew Dillon 		swap->swb_hnext = NULL;
18851c7c3c6aSMatthew Dillon 		swap->swb_object = object;
188623f09d50SIan Dowse 		swap->swb_index = pindex & ~(vm_pindex_t)SWAP_META_MASK;
18871c7c3c6aSMatthew Dillon 		swap->swb_count = 0;
18881c7c3c6aSMatthew Dillon 
18891c7c3c6aSMatthew Dillon 		++object->un_pager.swp.swp_bcount;
18901c7c3c6aSMatthew Dillon 
18911c7c3c6aSMatthew Dillon 		for (i = 0; i < SWAP_META_PAGES; ++i)
18921c7c3c6aSMatthew Dillon 			swap->swb_pages[i] = SWAPBLK_NONE;
18931c7c3c6aSMatthew Dillon 	}
18941c7c3c6aSMatthew Dillon 
18951c7c3c6aSMatthew Dillon 	/*
18961c7c3c6aSMatthew Dillon 	 * Delete prior contents of metadata
18971c7c3c6aSMatthew Dillon 	 */
189823f09d50SIan Dowse 	idx = pindex & SWAP_META_MASK;
18991c7c3c6aSMatthew Dillon 
190023f09d50SIan Dowse 	if (swap->swb_pages[idx] != SWAPBLK_NONE) {
190123f09d50SIan Dowse 		swp_pager_freeswapspace(swap->swb_pages[idx], 1);
19021c7c3c6aSMatthew Dillon 		--swap->swb_count;
19031c7c3c6aSMatthew Dillon 	}
19041c7c3c6aSMatthew Dillon 
19051c7c3c6aSMatthew Dillon 	/*
19061c7c3c6aSMatthew Dillon 	 * Enter block into metadata
19071c7c3c6aSMatthew Dillon 	 */
190823f09d50SIan Dowse 	swap->swb_pages[idx] = swapblk;
19094dcc5c2dSMatthew Dillon 	if (swapblk != SWAPBLK_NONE)
19101c7c3c6aSMatthew Dillon 		++swap->swb_count;
19117827d9b0SAlan Cox done:
19127827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
19131c7c3c6aSMatthew Dillon }
19141c7c3c6aSMatthew Dillon 
19151c7c3c6aSMatthew Dillon /*
19161c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
19171c7c3c6aSMatthew Dillon  *
19181c7c3c6aSMatthew Dillon  *	The requested range of blocks is freed, with any associated swap
19191c7c3c6aSMatthew Dillon  *	returned to the swap bitmap.
19201c7c3c6aSMatthew Dillon  *
19211c7c3c6aSMatthew Dillon  *	This routine will free swap metadata structures as they are cleaned
19221c7c3c6aSMatthew Dillon  *	out.  This routine does *NOT* operate on swap metadata associated
19231c7c3c6aSMatthew Dillon  *	with resident pages.
19241c7c3c6aSMatthew Dillon  *
19251c7c3c6aSMatthew Dillon  *	This routine must be called at splvm()
19261c7c3c6aSMatthew Dillon  */
19271c7c3c6aSMatthew Dillon static void
19284dcc5c2dSMatthew Dillon swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count)
19291c7c3c6aSMatthew Dillon {
19302928cef7SAlan Cox 
1931ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
19321c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19331c7c3c6aSMatthew Dillon 		return;
19341c7c3c6aSMatthew Dillon 
19351c7c3c6aSMatthew Dillon 	while (count > 0) {
19361c7c3c6aSMatthew Dillon 		struct swblock **pswap;
19371c7c3c6aSMatthew Dillon 		struct swblock *swap;
19381c7c3c6aSMatthew Dillon 
19397827d9b0SAlan Cox 		mtx_lock(&swhash_mtx);
19401c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
19411c7c3c6aSMatthew Dillon 
19421c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
19431c7c3c6aSMatthew Dillon 			daddr_t v = swap->swb_pages[index & SWAP_META_MASK];
19441c7c3c6aSMatthew Dillon 
19451c7c3c6aSMatthew Dillon 			if (v != SWAPBLK_NONE) {
19461c7c3c6aSMatthew Dillon 				swp_pager_freeswapspace(v, 1);
19471c7c3c6aSMatthew Dillon 				swap->swb_pages[index & SWAP_META_MASK] =
19481c7c3c6aSMatthew Dillon 					SWAPBLK_NONE;
19491c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
19501c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
1951670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
19521c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
19531c7c3c6aSMatthew Dillon 				}
19541c7c3c6aSMatthew Dillon 			}
19551c7c3c6aSMatthew Dillon 			--count;
19561c7c3c6aSMatthew Dillon 			++index;
19571c7c3c6aSMatthew Dillon 		} else {
19584dcc5c2dSMatthew Dillon 			int n = SWAP_META_PAGES - (index & SWAP_META_MASK);
19591c7c3c6aSMatthew Dillon 			count -= n;
19601c7c3c6aSMatthew Dillon 			index += n;
19611c7c3c6aSMatthew Dillon 		}
19627827d9b0SAlan Cox 		mtx_unlock(&swhash_mtx);
19631c7c3c6aSMatthew Dillon 	}
19641c7c3c6aSMatthew Dillon }
19651c7c3c6aSMatthew Dillon 
19661c7c3c6aSMatthew Dillon /*
19671c7c3c6aSMatthew Dillon  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
19681c7c3c6aSMatthew Dillon  *
19691c7c3c6aSMatthew Dillon  *	This routine locates and destroys all swap metadata associated with
19701c7c3c6aSMatthew Dillon  *	an object.
19714dcc5c2dSMatthew Dillon  *
19724dcc5c2dSMatthew Dillon  *	This routine must be called at splvm()
19731c7c3c6aSMatthew Dillon  */
19741c7c3c6aSMatthew Dillon static void
19751c7c3c6aSMatthew Dillon swp_pager_meta_free_all(vm_object_t object)
19761c7c3c6aSMatthew Dillon {
19771c7c3c6aSMatthew Dillon 	daddr_t index = 0;
19781c7c3c6aSMatthew Dillon 
1979ee3dc7d7SAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
19801c7c3c6aSMatthew Dillon 	if (object->type != OBJT_SWAP)
19811c7c3c6aSMatthew Dillon 		return;
19821c7c3c6aSMatthew Dillon 
19831c7c3c6aSMatthew Dillon 	while (object->un_pager.swp.swp_bcount) {
19841c7c3c6aSMatthew Dillon 		struct swblock **pswap;
19851c7c3c6aSMatthew Dillon 		struct swblock *swap;
19861c7c3c6aSMatthew Dillon 
19877827d9b0SAlan Cox 		mtx_lock(&swhash_mtx);
19881c7c3c6aSMatthew Dillon 		pswap = swp_pager_hash(object, index);
19891c7c3c6aSMatthew Dillon 		if ((swap = *pswap) != NULL) {
19901c7c3c6aSMatthew Dillon 			int i;
19911c7c3c6aSMatthew Dillon 
19921c7c3c6aSMatthew Dillon 			for (i = 0; i < SWAP_META_PAGES; ++i) {
19931c7c3c6aSMatthew Dillon 				daddr_t v = swap->swb_pages[i];
19941c7c3c6aSMatthew Dillon 				if (v != SWAPBLK_NONE) {
19951c7c3c6aSMatthew Dillon 					--swap->swb_count;
19964dcc5c2dSMatthew Dillon 					swp_pager_freeswapspace(v, 1);
19971c7c3c6aSMatthew Dillon 				}
19981c7c3c6aSMatthew Dillon 			}
19991c7c3c6aSMatthew Dillon 			if (swap->swb_count != 0)
20001c7c3c6aSMatthew Dillon 				panic("swap_pager_meta_free_all: swb_count != 0");
20011c7c3c6aSMatthew Dillon 			*pswap = swap->swb_hnext;
2002670d17b5SJeff Roberson 			uma_zfree(swap_zone, swap);
20031c7c3c6aSMatthew Dillon 			--object->un_pager.swp.swp_bcount;
20041c7c3c6aSMatthew Dillon 		}
20057827d9b0SAlan Cox 		mtx_unlock(&swhash_mtx);
20061c7c3c6aSMatthew Dillon 		index += SWAP_META_PAGES;
20071c7c3c6aSMatthew Dillon 		if (index > 0x20000000)
20081c7c3c6aSMatthew Dillon 			panic("swp_pager_meta_free_all: failed to locate all swap meta blocks");
20091c7c3c6aSMatthew Dillon 	}
20101c7c3c6aSMatthew Dillon }
20111c7c3c6aSMatthew Dillon 
20121c7c3c6aSMatthew Dillon /*
20131c7c3c6aSMatthew Dillon  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
20141c7c3c6aSMatthew Dillon  *
20151c7c3c6aSMatthew Dillon  *	This routine is capable of looking up, popping, or freeing
20161c7c3c6aSMatthew Dillon  *	swapblk assignments in the swap meta data or in the vm_page_t.
20171c7c3c6aSMatthew Dillon  *	The routine typically returns the swapblk being looked-up, or popped,
20181c7c3c6aSMatthew Dillon  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
20191c7c3c6aSMatthew Dillon  *	was invalid.  This routine will automatically free any invalid
20201c7c3c6aSMatthew Dillon  *	meta-data swapblks.
20211c7c3c6aSMatthew Dillon  *
20221c7c3c6aSMatthew Dillon  *	It is not possible to store invalid swapblks in the swap meta data
20231c7c3c6aSMatthew Dillon  *	(other then a literal 'SWAPBLK_NONE'), so we don't bother checking.
20241c7c3c6aSMatthew Dillon  *
20251c7c3c6aSMatthew Dillon  *	When acting on a busy resident page and paging is in progress, we
20261c7c3c6aSMatthew Dillon  *	have to wait until paging is complete but otherwise can act on the
20271c7c3c6aSMatthew Dillon  *	busy page.
20281c7c3c6aSMatthew Dillon  *
20294dcc5c2dSMatthew Dillon  *	This routine must be called at splvm().
20301c7c3c6aSMatthew Dillon  *
20314dcc5c2dSMatthew Dillon  *	SWM_FREE	remove and free swap block from metadata
20321c7c3c6aSMatthew Dillon  *	SWM_POP		remove from meta data but do not free.. pop it out
20331c7c3c6aSMatthew Dillon  */
20341c7c3c6aSMatthew Dillon static daddr_t
20352f249180SPoul-Henning Kamp swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags)
20362f249180SPoul-Henning Kamp {
20374dcc5c2dSMatthew Dillon 	struct swblock **pswap;
20384dcc5c2dSMatthew Dillon 	struct swblock *swap;
20394dcc5c2dSMatthew Dillon 	daddr_t r1;
204023f09d50SIan Dowse 	int idx;
20414dcc5c2dSMatthew Dillon 
2042c7c8dd7eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
20431c7c3c6aSMatthew Dillon 	/*
20441c7c3c6aSMatthew Dillon 	 * The meta data only exists of the object is OBJT_SWAP
20451c7c3c6aSMatthew Dillon 	 * and even then might not be allocated yet.
20461c7c3c6aSMatthew Dillon 	 */
20474dcc5c2dSMatthew Dillon 	if (object->type != OBJT_SWAP)
20481c7c3c6aSMatthew Dillon 		return (SWAPBLK_NONE);
20491c7c3c6aSMatthew Dillon 
20504dcc5c2dSMatthew Dillon 	r1 = SWAPBLK_NONE;
20517827d9b0SAlan Cox 	mtx_lock(&swhash_mtx);
205223f09d50SIan Dowse 	pswap = swp_pager_hash(object, pindex);
20531c7c3c6aSMatthew Dillon 
20541c7c3c6aSMatthew Dillon 	if ((swap = *pswap) != NULL) {
205523f09d50SIan Dowse 		idx = pindex & SWAP_META_MASK;
205623f09d50SIan Dowse 		r1 = swap->swb_pages[idx];
20571c7c3c6aSMatthew Dillon 
20581c7c3c6aSMatthew Dillon 		if (r1 != SWAPBLK_NONE) {
20591c7c3c6aSMatthew Dillon 			if (flags & SWM_FREE) {
20604dcc5c2dSMatthew Dillon 				swp_pager_freeswapspace(r1, 1);
20611c7c3c6aSMatthew Dillon 				r1 = SWAPBLK_NONE;
20621c7c3c6aSMatthew Dillon 			}
20631c7c3c6aSMatthew Dillon 			if (flags & (SWM_FREE|SWM_POP)) {
206423f09d50SIan Dowse 				swap->swb_pages[idx] = SWAPBLK_NONE;
20651c7c3c6aSMatthew Dillon 				if (--swap->swb_count == 0) {
20661c7c3c6aSMatthew Dillon 					*pswap = swap->swb_hnext;
2067670d17b5SJeff Roberson 					uma_zfree(swap_zone, swap);
20681c7c3c6aSMatthew Dillon 					--object->un_pager.swp.swp_bcount;
20691c7c3c6aSMatthew Dillon 				}
20701c7c3c6aSMatthew Dillon 			}
20711c7c3c6aSMatthew Dillon 		}
20721c7c3c6aSMatthew Dillon 	}
20737827d9b0SAlan Cox 	mtx_unlock(&swhash_mtx);
20741c7c3c6aSMatthew Dillon 	return (r1);
20751c7c3c6aSMatthew Dillon }
20761c7c3c6aSMatthew Dillon 
2077e9c0cc15SPoul-Henning Kamp /*
2078e9c0cc15SPoul-Henning Kamp  * System call swapon(name) enables swapping on device name,
2079e9c0cc15SPoul-Henning Kamp  * which must be in the swdevsw.  Return EBUSY
2080e9c0cc15SPoul-Henning Kamp  * if already swapping on this device.
2081e9c0cc15SPoul-Henning Kamp  */
2082e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2083e9c0cc15SPoul-Henning Kamp struct swapon_args {
2084e9c0cc15SPoul-Henning Kamp 	char *name;
2085e9c0cc15SPoul-Henning Kamp };
2086e9c0cc15SPoul-Henning Kamp #endif
2087e9c0cc15SPoul-Henning Kamp 
2088e9c0cc15SPoul-Henning Kamp /*
2089e9c0cc15SPoul-Henning Kamp  * MPSAFE
2090e9c0cc15SPoul-Henning Kamp  */
2091e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2092e9c0cc15SPoul-Henning Kamp int
20932f249180SPoul-Henning Kamp swapon(struct thread *td, struct swapon_args *uap)
2094e9c0cc15SPoul-Henning Kamp {
2095e9c0cc15SPoul-Henning Kamp 	struct vattr attr;
2096e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2097e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2098e9c0cc15SPoul-Henning Kamp 	int error;
2099e9c0cc15SPoul-Henning Kamp 
2100acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPON);
2101e9c0cc15SPoul-Henning Kamp 	if (error)
2102acd3428bSRobert Watson 		return (error);
2103e9c0cc15SPoul-Henning Kamp 
2104acd3428bSRobert Watson 	mtx_lock(&Giant);
2105e9c0cc15SPoul-Henning Kamp 	while (swdev_syscall_active)
2106e9c0cc15SPoul-Henning Kamp 	    tsleep(&swdev_syscall_active, PUSER - 1, "swpon", 0);
2107e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 1;
2108e9c0cc15SPoul-Henning Kamp 
2109e9c0cc15SPoul-Henning Kamp 	/*
2110e9c0cc15SPoul-Henning Kamp 	 * Swap metadata may not fit in the KVM if we have physical
2111e9c0cc15SPoul-Henning Kamp 	 * memory of >1GB.
2112e9c0cc15SPoul-Henning Kamp 	 */
2113e9c0cc15SPoul-Henning Kamp 	if (swap_zone == NULL) {
2114e9c0cc15SPoul-Henning Kamp 		error = ENOMEM;
2115e9c0cc15SPoul-Henning Kamp 		goto done;
2116e9c0cc15SPoul-Henning Kamp 	}
2117e9c0cc15SPoul-Henning Kamp 
2118d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2119d9135e72SRobert Watson 	    uap->name, td);
2120e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2121e9c0cc15SPoul-Henning Kamp 	if (error)
2122e9c0cc15SPoul-Henning Kamp 		goto done;
2123e9c0cc15SPoul-Henning Kamp 
2124e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2125e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2126e9c0cc15SPoul-Henning Kamp 
212720da9c2eSPoul-Henning Kamp 	if (vn_isdisk(vp, &error)) {
2128dee34ca4SPoul-Henning Kamp 		error = swapongeom(td, vp);
212920da9c2eSPoul-Henning Kamp 	} else if (vp->v_type == VREG &&
2130e9c0cc15SPoul-Henning Kamp 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
21310359a12eSAttilio Rao 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2132e9c0cc15SPoul-Henning Kamp 		/*
2133e9c0cc15SPoul-Henning Kamp 		 * Allow direct swapping to NFS regular files in the same
2134e9c0cc15SPoul-Henning Kamp 		 * way that nfs_mountroot() sets up diskless swapping.
2135e9c0cc15SPoul-Henning Kamp 		 */
213659efee01SPoul-Henning Kamp 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2137e9c0cc15SPoul-Henning Kamp 	}
2138e9c0cc15SPoul-Henning Kamp 
2139e9c0cc15SPoul-Henning Kamp 	if (error)
2140e9c0cc15SPoul-Henning Kamp 		vrele(vp);
2141e9c0cc15SPoul-Henning Kamp done:
2142e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 0;
2143e9c0cc15SPoul-Henning Kamp 	wakeup_one(&swdev_syscall_active);
2144e9c0cc15SPoul-Henning Kamp 	mtx_unlock(&Giant);
2145e9c0cc15SPoul-Henning Kamp 	return (error);
2146e9c0cc15SPoul-Henning Kamp }
2147e9c0cc15SPoul-Henning Kamp 
214859efee01SPoul-Henning Kamp static void
2149f3732fd1SPoul-Henning Kamp swaponsomething(struct vnode *vp, void *id, u_long nblks, sw_strategy_t *strategy, sw_close_t *close, dev_t dev)
2150e9c0cc15SPoul-Henning Kamp {
21512d9974c1SAlan Cox 	struct swdevt *sp, *tsp;
2152e9c0cc15SPoul-Henning Kamp 	swblk_t dvbase;
21538f60c087SPoul-Henning Kamp 	u_long mblocks;
2154e9c0cc15SPoul-Henning Kamp 
2155e9c0cc15SPoul-Henning Kamp 	/*
2156e9c0cc15SPoul-Henning Kamp 	 * If we go beyond this, we get overflows in the radix
2157e9c0cc15SPoul-Henning Kamp 	 * tree bitmap code.
2158e9c0cc15SPoul-Henning Kamp 	 */
21598f60c087SPoul-Henning Kamp 	mblocks = 0x40000000 / BLIST_META_RADIX;
2160d3dd89abSPoul-Henning Kamp 	if (nblks > mblocks) {
216185fdafb9SPoul-Henning Kamp 		printf("WARNING: reducing size to maximum of %lu blocks per swap unit\n",
2162d3dd89abSPoul-Henning Kamp 			mblocks);
2163d3dd89abSPoul-Henning Kamp 		nblks = mblocks;
2164e9c0cc15SPoul-Henning Kamp 	}
2165e9c0cc15SPoul-Henning Kamp 	/*
2166e9c0cc15SPoul-Henning Kamp 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2167e9c0cc15SPoul-Henning Kamp 	 * First chop nblks off to page-align it, then convert.
2168e9c0cc15SPoul-Henning Kamp 	 *
2169e9c0cc15SPoul-Henning Kamp 	 * sw->sw_nblks is in page-sized chunks now too.
2170e9c0cc15SPoul-Henning Kamp 	 */
2171e9c0cc15SPoul-Henning Kamp 	nblks &= ~(ctodb(1) - 1);
2172e9c0cc15SPoul-Henning Kamp 	nblks = dbtoc(nblks);
2173e9c0cc15SPoul-Henning Kamp 
21748f60c087SPoul-Henning Kamp 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2175dee34ca4SPoul-Henning Kamp 	sp->sw_vp = vp;
2176dee34ca4SPoul-Henning Kamp 	sp->sw_id = id;
2177f3732fd1SPoul-Henning Kamp 	sp->sw_dev = dev;
21788d677ef9SPoul-Henning Kamp 	sp->sw_flags = 0;
2179e9c0cc15SPoul-Henning Kamp 	sp->sw_nblks = nblks;
2180e9c0cc15SPoul-Henning Kamp 	sp->sw_used = 0;
218159efee01SPoul-Henning Kamp 	sp->sw_strategy = strategy;
2182dee34ca4SPoul-Henning Kamp 	sp->sw_close = close;
2183e9c0cc15SPoul-Henning Kamp 
2184c8c7ad92SKip Macy 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2185e9c0cc15SPoul-Henning Kamp 	/*
2186ef3c5abdSPoul-Henning Kamp 	 * Do not free the first two block in order to avoid overwriting
21878f60c087SPoul-Henning Kamp 	 * any bsd label at the front of the partition
2188e9c0cc15SPoul-Henning Kamp 	 */
2189ef3c5abdSPoul-Henning Kamp 	blist_free(sp->sw_blist, 2, nblks - 2);
2190e9c0cc15SPoul-Henning Kamp 
21912d9974c1SAlan Cox 	dvbase = 0;
219220da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
21932d9974c1SAlan Cox 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
21942d9974c1SAlan Cox 		if (tsp->sw_end >= dvbase) {
21952d9974c1SAlan Cox 			/*
21962d9974c1SAlan Cox 			 * We put one uncovered page between the devices
21972d9974c1SAlan Cox 			 * in order to definitively prevent any cross-device
21982d9974c1SAlan Cox 			 * I/O requests
21992d9974c1SAlan Cox 			 */
22002d9974c1SAlan Cox 			dvbase = tsp->sw_end + 1;
22012d9974c1SAlan Cox 		}
22022d9974c1SAlan Cox 	}
22032d9974c1SAlan Cox 	sp->sw_first = dvbase;
22042d9974c1SAlan Cox 	sp->sw_end = dvbase + nblks;
22058f60c087SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
22068f60c087SPoul-Henning Kamp 	nswapdev++;
22078f60c087SPoul-Henning Kamp 	swap_pager_avail += nblks;
22083364c323SKonstantin Belousov 	swap_total += (vm_ooffset_t)nblks * PAGE_SIZE;
2209d05bc129SAlan Cox 	swp_sizecheck();
2210d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
221159efee01SPoul-Henning Kamp }
2212e9c0cc15SPoul-Henning Kamp 
2213e9c0cc15SPoul-Henning Kamp /*
2214e9c0cc15SPoul-Henning Kamp  * SYSCALL: swapoff(devname)
2215e9c0cc15SPoul-Henning Kamp  *
2216e9c0cc15SPoul-Henning Kamp  * Disable swapping on the given device.
2217dee34ca4SPoul-Henning Kamp  *
2218dee34ca4SPoul-Henning Kamp  * XXX: Badly designed system call: it should use a device index
2219dee34ca4SPoul-Henning Kamp  * rather than filename as specification.  We keep sw_vp around
2220dee34ca4SPoul-Henning Kamp  * only to make this work.
2221e9c0cc15SPoul-Henning Kamp  */
2222e9c0cc15SPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_
2223e9c0cc15SPoul-Henning Kamp struct swapoff_args {
2224e9c0cc15SPoul-Henning Kamp 	char *name;
2225e9c0cc15SPoul-Henning Kamp };
2226e9c0cc15SPoul-Henning Kamp #endif
2227e9c0cc15SPoul-Henning Kamp 
2228e9c0cc15SPoul-Henning Kamp /*
2229e9c0cc15SPoul-Henning Kamp  * MPSAFE
2230e9c0cc15SPoul-Henning Kamp  */
2231e9c0cc15SPoul-Henning Kamp /* ARGSUSED */
2232e9c0cc15SPoul-Henning Kamp int
22332f249180SPoul-Henning Kamp swapoff(struct thread *td, struct swapoff_args *uap)
2234e9c0cc15SPoul-Henning Kamp {
2235e9c0cc15SPoul-Henning Kamp 	struct vnode *vp;
2236e9c0cc15SPoul-Henning Kamp 	struct nameidata nd;
2237e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
22388f60c087SPoul-Henning Kamp 	int error;
2239e9c0cc15SPoul-Henning Kamp 
2240acd3428bSRobert Watson 	error = priv_check(td, PRIV_SWAPOFF);
2241e9c0cc15SPoul-Henning Kamp 	if (error)
22420909f38aSPawel Jakub Dawidek 		return (error);
2243e9c0cc15SPoul-Henning Kamp 
22440909f38aSPawel Jakub Dawidek 	mtx_lock(&Giant);
2245e9c0cc15SPoul-Henning Kamp 	while (swdev_syscall_active)
2246e9c0cc15SPoul-Henning Kamp 	    tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0);
2247e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 1;
2248e9c0cc15SPoul-Henning Kamp 
2249d9135e72SRobert Watson 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2250d9135e72SRobert Watson 	    td);
2251e9c0cc15SPoul-Henning Kamp 	error = namei(&nd);
2252e9c0cc15SPoul-Henning Kamp 	if (error)
2253e9c0cc15SPoul-Henning Kamp 		goto done;
2254e9c0cc15SPoul-Henning Kamp 	NDFREE(&nd, NDF_ONLY_PNBUF);
2255e9c0cc15SPoul-Henning Kamp 	vp = nd.ni_vp;
2256e9c0cc15SPoul-Henning Kamp 
225720da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
22588f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2259dee34ca4SPoul-Henning Kamp 		if (sp->sw_vp == vp)
22600909f38aSPawel Jakub Dawidek 			break;
2261e9c0cc15SPoul-Henning Kamp 	}
226220da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
22630909f38aSPawel Jakub Dawidek 	if (sp == NULL) {
2264e9c0cc15SPoul-Henning Kamp 		error = EINVAL;
2265e9c0cc15SPoul-Henning Kamp 		goto done;
22660909f38aSPawel Jakub Dawidek 	}
226735918c55SChristian S.J. Peron 	error = swapoff_one(sp, td->td_ucred);
22680909f38aSPawel Jakub Dawidek done:
22690909f38aSPawel Jakub Dawidek 	swdev_syscall_active = 0;
22700909f38aSPawel Jakub Dawidek 	wakeup_one(&swdev_syscall_active);
22710909f38aSPawel Jakub Dawidek 	mtx_unlock(&Giant);
22720909f38aSPawel Jakub Dawidek 	return (error);
22730909f38aSPawel Jakub Dawidek }
22740909f38aSPawel Jakub Dawidek 
22750909f38aSPawel Jakub Dawidek static int
227635918c55SChristian S.J. Peron swapoff_one(struct swdevt *sp, struct ucred *cred)
22770909f38aSPawel Jakub Dawidek {
22780909f38aSPawel Jakub Dawidek 	u_long nblks, dvbase;
2279e9c0cc15SPoul-Henning Kamp #ifdef MAC
22800909f38aSPawel Jakub Dawidek 	int error;
2281e9c0cc15SPoul-Henning Kamp #endif
2282e9c0cc15SPoul-Henning Kamp 
22830909f38aSPawel Jakub Dawidek 	mtx_assert(&Giant, MA_OWNED);
22840909f38aSPawel Jakub Dawidek #ifdef MAC
2285cb05b60aSAttilio Rao 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
228635918c55SChristian S.J. Peron 	error = mac_system_check_swapoff(cred, sp->sw_vp);
228722db15c0SAttilio Rao 	(void) VOP_UNLOCK(sp->sw_vp, 0);
22880909f38aSPawel Jakub Dawidek 	if (error != 0)
22890909f38aSPawel Jakub Dawidek 		return (error);
22900909f38aSPawel Jakub Dawidek #endif
2291e9c0cc15SPoul-Henning Kamp 	nblks = sp->sw_nblks;
2292e9c0cc15SPoul-Henning Kamp 
2293e9c0cc15SPoul-Henning Kamp 	/*
2294e9c0cc15SPoul-Henning Kamp 	 * We can turn off this swap device safely only if the
2295e9c0cc15SPoul-Henning Kamp 	 * available virtual memory in the system will fit the amount
2296e9c0cc15SPoul-Henning Kamp 	 * of data we will have to page back in, plus an epsilon so
2297e9c0cc15SPoul-Henning Kamp 	 * the system doesn't become critically low on swap space.
2298e9c0cc15SPoul-Henning Kamp 	 */
22992feb50bfSAttilio Rao 	if (cnt.v_free_count + cnt.v_cache_count + swap_pager_avail <
23002feb50bfSAttilio Rao 	    nblks + nswap_lowat) {
23010909f38aSPawel Jakub Dawidek 		return (ENOMEM);
2302e9c0cc15SPoul-Henning Kamp 	}
2303e9c0cc15SPoul-Henning Kamp 
2304e9c0cc15SPoul-Henning Kamp 	/*
2305e9c0cc15SPoul-Henning Kamp 	 * Prevent further allocations on this device.
2306e9c0cc15SPoul-Henning Kamp 	 */
23072928cef7SAlan Cox 	mtx_lock(&sw_dev_mtx);
2308e9c0cc15SPoul-Henning Kamp 	sp->sw_flags |= SW_CLOSING;
23098f60c087SPoul-Henning Kamp 	for (dvbase = 0; dvbase < sp->sw_end; dvbase += dmmax) {
23108f60c087SPoul-Henning Kamp 		swap_pager_avail -= blist_fill(sp->sw_blist,
23118f60c087SPoul-Henning Kamp 		     dvbase, dmmax);
2312e9c0cc15SPoul-Henning Kamp 	}
23133364c323SKonstantin Belousov 	swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE;
23142928cef7SAlan Cox 	mtx_unlock(&sw_dev_mtx);
2315e9c0cc15SPoul-Henning Kamp 
2316e9c0cc15SPoul-Henning Kamp 	/*
2317e9c0cc15SPoul-Henning Kamp 	 * Page in the contents of the device and close it.
2318e9c0cc15SPoul-Henning Kamp 	 */
2319b3fed13eSDavid Schultz 	swap_pager_swapoff(sp);
2320e9c0cc15SPoul-Henning Kamp 
232135918c55SChristian S.J. Peron 	sp->sw_close(curthread, sp);
232259efee01SPoul-Henning Kamp 	sp->sw_id = NULL;
232320da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23248f60c087SPoul-Henning Kamp 	TAILQ_REMOVE(&swtailq, sp, sw_list);
23250676a140SAlan Cox 	nswapdev--;
23267dea2c2eSAlan Cox 	if (nswapdev == 0) {
23277dea2c2eSAlan Cox 		swap_pager_full = 2;
23287dea2c2eSAlan Cox 		swap_pager_almost_full = 1;
23297dea2c2eSAlan Cox 	}
23308f60c087SPoul-Henning Kamp 	if (swdevhd == sp)
23318f60c087SPoul-Henning Kamp 		swdevhd = NULL;
2332d05bc129SAlan Cox 	mtx_unlock(&sw_dev_mtx);
23338f60c087SPoul-Henning Kamp 	blist_destroy(sp->sw_blist);
23348f60c087SPoul-Henning Kamp 	free(sp, M_VMPGDATA);
23350909f38aSPawel Jakub Dawidek 	return (0);
23360909f38aSPawel Jakub Dawidek }
2337e9c0cc15SPoul-Henning Kamp 
23380909f38aSPawel Jakub Dawidek void
23390909f38aSPawel Jakub Dawidek swapoff_all(void)
23400909f38aSPawel Jakub Dawidek {
23410909f38aSPawel Jakub Dawidek 	struct swdevt *sp, *spt;
23420909f38aSPawel Jakub Dawidek 	const char *devname;
23430909f38aSPawel Jakub Dawidek 	int error;
23440909f38aSPawel Jakub Dawidek 
23450909f38aSPawel Jakub Dawidek 	mtx_lock(&Giant);
23460909f38aSPawel Jakub Dawidek 	while (swdev_syscall_active)
23470909f38aSPawel Jakub Dawidek 		tsleep(&swdev_syscall_active, PUSER - 1, "swpoff", 0);
23480909f38aSPawel Jakub Dawidek 	swdev_syscall_active = 1;
23490909f38aSPawel Jakub Dawidek 
23500909f38aSPawel Jakub Dawidek 	mtx_lock(&sw_dev_mtx);
23510909f38aSPawel Jakub Dawidek 	TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
23520909f38aSPawel Jakub Dawidek 		mtx_unlock(&sw_dev_mtx);
23530909f38aSPawel Jakub Dawidek 		if (vn_isdisk(sp->sw_vp, NULL))
23540909f38aSPawel Jakub Dawidek 			devname = sp->sw_vp->v_rdev->si_name;
23550909f38aSPawel Jakub Dawidek 		else
23560909f38aSPawel Jakub Dawidek 			devname = "[file]";
235735918c55SChristian S.J. Peron 		error = swapoff_one(sp, thread0.td_ucred);
23580909f38aSPawel Jakub Dawidek 		if (error != 0) {
23590909f38aSPawel Jakub Dawidek 			printf("Cannot remove swap device %s (error=%d), "
23600909f38aSPawel Jakub Dawidek 			    "skipping.\n", devname, error);
23610909f38aSPawel Jakub Dawidek 		} else if (bootverbose) {
23620909f38aSPawel Jakub Dawidek 			printf("Swap device %s removed.\n", devname);
23630909f38aSPawel Jakub Dawidek 		}
23640909f38aSPawel Jakub Dawidek 		mtx_lock(&sw_dev_mtx);
23650909f38aSPawel Jakub Dawidek 	}
23660909f38aSPawel Jakub Dawidek 	mtx_unlock(&sw_dev_mtx);
23670909f38aSPawel Jakub Dawidek 
2368e9c0cc15SPoul-Henning Kamp 	swdev_syscall_active = 0;
2369e9c0cc15SPoul-Henning Kamp 	wakeup_one(&swdev_syscall_active);
2370e9c0cc15SPoul-Henning Kamp 	mtx_unlock(&Giant);
2371e9c0cc15SPoul-Henning Kamp }
2372e9c0cc15SPoul-Henning Kamp 
2373567104a1SPoul-Henning Kamp void
2374567104a1SPoul-Henning Kamp swap_pager_status(int *total, int *used)
2375567104a1SPoul-Henning Kamp {
2376567104a1SPoul-Henning Kamp 	struct swdevt *sp;
2377567104a1SPoul-Henning Kamp 
2378567104a1SPoul-Henning Kamp 	*total = 0;
2379567104a1SPoul-Henning Kamp 	*used = 0;
238020da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
23818f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2382567104a1SPoul-Henning Kamp 		*total += sp->sw_nblks;
2383567104a1SPoul-Henning Kamp 		*used += sp->sw_used;
2384567104a1SPoul-Henning Kamp 	}
238520da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2386567104a1SPoul-Henning Kamp }
2387567104a1SPoul-Henning Kamp 
2388e9c0cc15SPoul-Henning Kamp static int
2389e9c0cc15SPoul-Henning Kamp sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2390e9c0cc15SPoul-Henning Kamp {
2391e9c0cc15SPoul-Henning Kamp 	int	*name = (int *)arg1;
23928f60c087SPoul-Henning Kamp 	int	error, n;
2393e9c0cc15SPoul-Henning Kamp 	struct xswdev xs;
2394e9c0cc15SPoul-Henning Kamp 	struct swdevt *sp;
2395e9c0cc15SPoul-Henning Kamp 
2396e9c0cc15SPoul-Henning Kamp 	if (arg2 != 1) /* name length */
2397e9c0cc15SPoul-Henning Kamp 		return (EINVAL);
2398e9c0cc15SPoul-Henning Kamp 
23998f60c087SPoul-Henning Kamp 	n = 0;
240020da9c2eSPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
24018f60c087SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2402e9c0cc15SPoul-Henning Kamp 		if (n == *name) {
240320da9c2eSPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2404e9c0cc15SPoul-Henning Kamp 			xs.xsw_version = XSWDEV_VERSION;
2405f3732fd1SPoul-Henning Kamp 			xs.xsw_dev = sp->sw_dev;
2406e9c0cc15SPoul-Henning Kamp 			xs.xsw_flags = sp->sw_flags;
2407e9c0cc15SPoul-Henning Kamp 			xs.xsw_nblks = sp->sw_nblks;
2408e9c0cc15SPoul-Henning Kamp 			xs.xsw_used = sp->sw_used;
2409e9c0cc15SPoul-Henning Kamp 
2410e9c0cc15SPoul-Henning Kamp 			error = SYSCTL_OUT(req, &xs, sizeof(xs));
2411e9c0cc15SPoul-Henning Kamp 			return (error);
2412e9c0cc15SPoul-Henning Kamp 		}
2413e9c0cc15SPoul-Henning Kamp 		n++;
2414e9c0cc15SPoul-Henning Kamp 	}
241520da9c2eSPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2416e9c0cc15SPoul-Henning Kamp 	return (ENOENT);
2417e9c0cc15SPoul-Henning Kamp }
2418e9c0cc15SPoul-Henning Kamp 
24198f60c087SPoul-Henning Kamp SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2420e9c0cc15SPoul-Henning Kamp     "Number of swap devices");
2421e9c0cc15SPoul-Henning Kamp SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD, sysctl_vm_swap_info,
2422e9c0cc15SPoul-Henning Kamp     "Swap statistics by device");
2423ec38b344SPoul-Henning Kamp 
2424ec38b344SPoul-Henning Kamp /*
24258eb5a1cdSKonstantin Belousov  * vmspace_swap_count() - count the approximate swap usage in pages for a
2426ec38b344SPoul-Henning Kamp  *			  vmspace.
2427ec38b344SPoul-Henning Kamp  *
2428ec38b344SPoul-Henning Kamp  *	The map must be locked.
2429ec38b344SPoul-Henning Kamp  *
24308eb5a1cdSKonstantin Belousov  *	Swap usage is determined by taking the proportional swap used by
2431ec38b344SPoul-Henning Kamp  *	VM objects backing the VM map.  To make up for fractional losses,
2432ec38b344SPoul-Henning Kamp  *	if the VM object has any swap use at all the associated map entries
2433ec38b344SPoul-Henning Kamp  *	count for at least 1 swap page.
2434ec38b344SPoul-Henning Kamp  */
2435ec38b344SPoul-Henning Kamp int
2436ec38b344SPoul-Henning Kamp vmspace_swap_count(struct vmspace *vmspace)
2437ec38b344SPoul-Henning Kamp {
2438ec38b344SPoul-Henning Kamp 	vm_map_t map = &vmspace->vm_map;
2439ec38b344SPoul-Henning Kamp 	vm_map_entry_t cur;
2440ec38b344SPoul-Henning Kamp 	int count = 0;
2441ec38b344SPoul-Henning Kamp 
2442ec38b344SPoul-Henning Kamp 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2443ec38b344SPoul-Henning Kamp 		vm_object_t object;
2444ec38b344SPoul-Henning Kamp 
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) {
2450ec38b344SPoul-Henning Kamp 				int n = (cur->end - cur->start) / PAGE_SIZE;
2451ec38b344SPoul-Henning Kamp 
2452ec38b344SPoul-Henning Kamp 				count += object->un_pager.swp.swp_bcount *
2453ec38b344SPoul-Henning Kamp 				    SWAP_META_PAGES * n / object->size + 1;
2454ec38b344SPoul-Henning Kamp 			}
2455ec38b344SPoul-Henning Kamp 			VM_OBJECT_UNLOCK(object);
2456ec38b344SPoul-Henning Kamp 		}
2457ec38b344SPoul-Henning Kamp 	}
2458ec38b344SPoul-Henning Kamp 	return (count);
2459ec38b344SPoul-Henning Kamp }
2460dee34ca4SPoul-Henning Kamp 
2461dee34ca4SPoul-Henning Kamp /*
2462dee34ca4SPoul-Henning Kamp  * GEOM backend
2463dee34ca4SPoul-Henning Kamp  *
2464dee34ca4SPoul-Henning Kamp  * Swapping onto disk devices.
2465dee34ca4SPoul-Henning Kamp  *
2466dee34ca4SPoul-Henning Kamp  */
2467dee34ca4SPoul-Henning Kamp 
24685721c9c7SPoul-Henning Kamp static g_orphan_t swapgeom_orphan;
24695721c9c7SPoul-Henning Kamp 
2470dee34ca4SPoul-Henning Kamp static struct g_class g_swap_class = {
2471dee34ca4SPoul-Henning Kamp 	.name = "SWAP",
24725721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
24735721c9c7SPoul-Henning Kamp 	.orphan = swapgeom_orphan,
2474dee34ca4SPoul-Henning Kamp };
2475dee34ca4SPoul-Henning Kamp 
2476dee34ca4SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_swap_class, g_class);
2477dee34ca4SPoul-Henning Kamp 
2478dee34ca4SPoul-Henning Kamp 
2479dee34ca4SPoul-Henning Kamp static void
2480dee34ca4SPoul-Henning Kamp swapgeom_done(struct bio *bp2)
2481dee34ca4SPoul-Henning Kamp {
2482dee34ca4SPoul-Henning Kamp 	struct buf *bp;
2483dee34ca4SPoul-Henning Kamp 
2484dee34ca4SPoul-Henning Kamp 	bp = bp2->bio_caller2;
2485c5d3d25eSPoul-Henning Kamp 	bp->b_ioflags = bp2->bio_flags;
2486dee34ca4SPoul-Henning Kamp 	if (bp2->bio_error)
2487dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2488c5d3d25eSPoul-Henning Kamp 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2489c5d3d25eSPoul-Henning Kamp 	bp->b_error = bp2->bio_error;
2490dee34ca4SPoul-Henning Kamp 	bufdone(bp);
2491dee34ca4SPoul-Henning Kamp 	g_destroy_bio(bp2);
2492dee34ca4SPoul-Henning Kamp }
2493dee34ca4SPoul-Henning Kamp 
2494dee34ca4SPoul-Henning Kamp static void
2495dee34ca4SPoul-Henning Kamp swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2496dee34ca4SPoul-Henning Kamp {
2497dee34ca4SPoul-Henning Kamp 	struct bio *bio;
2498dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2499dee34ca4SPoul-Henning Kamp 
2500dee34ca4SPoul-Henning Kamp 	cp = sp->sw_id;
2501dee34ca4SPoul-Henning Kamp 	if (cp == NULL) {
2502dee34ca4SPoul-Henning Kamp 		bp->b_error = ENXIO;
2503dee34ca4SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
2504dee34ca4SPoul-Henning Kamp 		bufdone(bp);
2505dee34ca4SPoul-Henning Kamp 		return;
2506dee34ca4SPoul-Henning Kamp 	}
250711041003SKonstantin Belousov 	if (bp->b_iocmd == BIO_WRITE)
250811041003SKonstantin Belousov 		bio = g_new_bio();
250911041003SKonstantin Belousov 	else
25104f8205e5SPoul-Henning Kamp 		bio = g_alloc_bio();
25114f8205e5SPoul-Henning Kamp 	if (bio == NULL) {
25123e5b6861SPoul-Henning Kamp 		bp->b_error = ENOMEM;
25133e5b6861SPoul-Henning Kamp 		bp->b_ioflags |= BIO_ERROR;
25143e5b6861SPoul-Henning Kamp 		bufdone(bp);
25153e5b6861SPoul-Henning Kamp 		return;
25163e5b6861SPoul-Henning Kamp 	}
251711041003SKonstantin Belousov 
2518dee34ca4SPoul-Henning Kamp 	bio->bio_caller2 = bp;
2519c5d3d25eSPoul-Henning Kamp 	bio->bio_cmd = bp->b_iocmd;
2520c5d3d25eSPoul-Henning Kamp 	bio->bio_data = bp->b_data;
2521dee34ca4SPoul-Henning Kamp 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2522dee34ca4SPoul-Henning Kamp 	bio->bio_length = bp->b_bcount;
2523dee34ca4SPoul-Henning Kamp 	bio->bio_done = swapgeom_done;
2524dee34ca4SPoul-Henning Kamp 	g_io_request(bio, cp);
2525dee34ca4SPoul-Henning Kamp 	return;
2526dee34ca4SPoul-Henning Kamp }
2527dee34ca4SPoul-Henning Kamp 
2528dee34ca4SPoul-Henning Kamp static void
2529dee34ca4SPoul-Henning Kamp swapgeom_orphan(struct g_consumer *cp)
2530dee34ca4SPoul-Henning Kamp {
2531dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2532dee34ca4SPoul-Henning Kamp 
2533dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2534dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list)
2535dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == cp)
2536dee34ca4SPoul-Henning Kamp 			sp->sw_id = NULL;
2537dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2538dee34ca4SPoul-Henning Kamp }
2539dee34ca4SPoul-Henning Kamp 
2540dee34ca4SPoul-Henning Kamp static void
2541dee34ca4SPoul-Henning Kamp swapgeom_close_ev(void *arg, int flags)
2542dee34ca4SPoul-Henning Kamp {
2543dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2544dee34ca4SPoul-Henning Kamp 
2545dee34ca4SPoul-Henning Kamp 	cp = arg;
2546d2bae332SPoul-Henning Kamp 	g_access(cp, -1, -1, 0);
2547dee34ca4SPoul-Henning Kamp 	g_detach(cp);
2548dee34ca4SPoul-Henning Kamp 	g_destroy_consumer(cp);
2549dee34ca4SPoul-Henning Kamp }
2550dee34ca4SPoul-Henning Kamp 
2551dee34ca4SPoul-Henning Kamp static void
2552dee34ca4SPoul-Henning Kamp swapgeom_close(struct thread *td, struct swdevt *sw)
2553dee34ca4SPoul-Henning Kamp {
2554dee34ca4SPoul-Henning Kamp 
2555dee34ca4SPoul-Henning Kamp 	/* XXX: direct call when Giant untangled */
2556dee34ca4SPoul-Henning Kamp 	g_waitfor_event(swapgeom_close_ev, sw->sw_id, M_WAITOK, NULL);
2557dee34ca4SPoul-Henning Kamp }
2558dee34ca4SPoul-Henning Kamp 
2559dee34ca4SPoul-Henning Kamp 
2560dee34ca4SPoul-Henning Kamp struct swh0h0 {
256189c9c53dSPoul-Henning Kamp 	struct cdev *dev;
2562dee34ca4SPoul-Henning Kamp 	struct vnode *vp;
2563dee34ca4SPoul-Henning Kamp 	int	error;
2564dee34ca4SPoul-Henning Kamp };
2565dee34ca4SPoul-Henning Kamp 
2566dee34ca4SPoul-Henning Kamp static void
2567dee34ca4SPoul-Henning Kamp swapongeom_ev(void *arg, int flags)
2568dee34ca4SPoul-Henning Kamp {
2569dee34ca4SPoul-Henning Kamp 	struct swh0h0 *swh;
2570dee34ca4SPoul-Henning Kamp 	struct g_provider *pp;
2571dee34ca4SPoul-Henning Kamp 	struct g_consumer *cp;
2572dee34ca4SPoul-Henning Kamp 	static struct g_geom *gp;
2573dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2574dee34ca4SPoul-Henning Kamp 	u_long nblks;
2575dee34ca4SPoul-Henning Kamp 	int error;
2576dee34ca4SPoul-Henning Kamp 
2577dee34ca4SPoul-Henning Kamp 	swh = arg;
2578dee34ca4SPoul-Henning Kamp 	swh->error = 0;
2579dee34ca4SPoul-Henning Kamp 	pp = g_dev_getprovider(swh->dev);
2580dee34ca4SPoul-Henning Kamp 	if (pp == NULL) {
2581dee34ca4SPoul-Henning Kamp 		swh->error = ENODEV;
2582dee34ca4SPoul-Henning Kamp 		return;
2583dee34ca4SPoul-Henning Kamp 	}
2584dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2585dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2586dee34ca4SPoul-Henning Kamp 		cp = sp->sw_id;
2587dee34ca4SPoul-Henning Kamp 		if (cp != NULL && cp->provider == pp) {
2588dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2589dee34ca4SPoul-Henning Kamp 			swh->error = EBUSY;
2590dee34ca4SPoul-Henning Kamp 			return;
2591dee34ca4SPoul-Henning Kamp 		}
2592dee34ca4SPoul-Henning Kamp 	}
2593dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
25945721c9c7SPoul-Henning Kamp 	if (gp == NULL)
2595dee34ca4SPoul-Henning Kamp 		gp = g_new_geomf(&g_swap_class, "swap", NULL);
2596dee34ca4SPoul-Henning Kamp 	cp = g_new_consumer(gp);
2597dee34ca4SPoul-Henning Kamp 	g_attach(cp, pp);
2598afeb65e6SPoul-Henning Kamp 	/*
2599afeb65e6SPoul-Henning Kamp 	 * XXX: Everytime you think you can improve the margin for
2600afeb65e6SPoul-Henning Kamp 	 * footshooting, somebody depends on the ability to do so:
2601afeb65e6SPoul-Henning Kamp 	 * savecore(8) wants to write to our swapdev so we cannot
2602afeb65e6SPoul-Henning Kamp 	 * set an exclusive count :-(
2603afeb65e6SPoul-Henning Kamp 	 */
2604d2bae332SPoul-Henning Kamp 	error = g_access(cp, 1, 1, 0);
2605dee34ca4SPoul-Henning Kamp 	if (error) {
2606dee34ca4SPoul-Henning Kamp 		g_detach(cp);
2607dee34ca4SPoul-Henning Kamp 		g_destroy_consumer(cp);
2608dee34ca4SPoul-Henning Kamp 		swh->error = error;
2609dee34ca4SPoul-Henning Kamp 		return;
2610dee34ca4SPoul-Henning Kamp 	}
2611dee34ca4SPoul-Henning Kamp 	nblks = pp->mediasize / DEV_BSIZE;
2612dee34ca4SPoul-Henning Kamp 	swaponsomething(swh->vp, cp, nblks, swapgeom_strategy,
2613dee34ca4SPoul-Henning Kamp 	    swapgeom_close, dev2udev(swh->dev));
2614dee34ca4SPoul-Henning Kamp 	swh->error = 0;
2615dee34ca4SPoul-Henning Kamp 	return;
2616dee34ca4SPoul-Henning Kamp }
2617dee34ca4SPoul-Henning Kamp 
2618dee34ca4SPoul-Henning Kamp static int
2619dee34ca4SPoul-Henning Kamp swapongeom(struct thread *td, struct vnode *vp)
2620dee34ca4SPoul-Henning Kamp {
2621dee34ca4SPoul-Henning Kamp 	int error;
2622dee34ca4SPoul-Henning Kamp 	struct swh0h0 swh;
2623dee34ca4SPoul-Henning Kamp 
2624cb05b60aSAttilio Rao 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2625dee34ca4SPoul-Henning Kamp 
2626dee34ca4SPoul-Henning Kamp 	swh.dev = vp->v_rdev;
2627dee34ca4SPoul-Henning Kamp 	swh.vp = vp;
2628dee34ca4SPoul-Henning Kamp 	swh.error = 0;
2629dee34ca4SPoul-Henning Kamp 	/* XXX: direct call when Giant untangled */
2630dee34ca4SPoul-Henning Kamp 	error = g_waitfor_event(swapongeom_ev, &swh, M_WAITOK, NULL);
2631dee34ca4SPoul-Henning Kamp 	if (!error)
2632dee34ca4SPoul-Henning Kamp 		error = swh.error;
263322db15c0SAttilio Rao 	VOP_UNLOCK(vp, 0);
2634dee34ca4SPoul-Henning Kamp 	return (error);
2635dee34ca4SPoul-Henning Kamp }
2636dee34ca4SPoul-Henning Kamp 
2637dee34ca4SPoul-Henning Kamp /*
2638dee34ca4SPoul-Henning Kamp  * VNODE backend
2639dee34ca4SPoul-Henning Kamp  *
2640dee34ca4SPoul-Henning Kamp  * This is used mainly for network filesystem (read: probably only tested
2641dee34ca4SPoul-Henning Kamp  * with NFS) swapfiles.
2642dee34ca4SPoul-Henning Kamp  *
2643dee34ca4SPoul-Henning Kamp  */
2644dee34ca4SPoul-Henning Kamp 
2645dee34ca4SPoul-Henning Kamp static void
2646dee34ca4SPoul-Henning Kamp swapdev_strategy(struct buf *bp, struct swdevt *sp)
2647dee34ca4SPoul-Henning Kamp {
2648494eb176SPoul-Henning Kamp 	struct vnode *vp2;
2649dee34ca4SPoul-Henning Kamp 
2650dee34ca4SPoul-Henning Kamp 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2651dee34ca4SPoul-Henning Kamp 
2652dee34ca4SPoul-Henning Kamp 	vp2 = sp->sw_id;
2653dee34ca4SPoul-Henning Kamp 	vhold(vp2);
2654dee34ca4SPoul-Henning Kamp 	if (bp->b_iocmd == BIO_WRITE) {
26553cfc7651SOlivier Houchard 		if (bp->b_bufobj)
2656494eb176SPoul-Henning Kamp 			bufobj_wdrop(bp->b_bufobj);
2657a76d8f4eSPoul-Henning Kamp 		bufobj_wref(&vp2->v_bufobj);
2658dee34ca4SPoul-Henning Kamp 	}
26593cfc7651SOlivier Houchard 	if (bp->b_bufobj != &vp2->v_bufobj)
26603cfc7651SOlivier Houchard 		bp->b_bufobj = &vp2->v_bufobj;
2661dee34ca4SPoul-Henning Kamp 	bp->b_vp = vp2;
26622c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
2663b792bebeSPoul-Henning Kamp 	bstrategy(bp);
2664dee34ca4SPoul-Henning Kamp 	return;
2665dee34ca4SPoul-Henning Kamp }
2666dee34ca4SPoul-Henning Kamp 
2667dee34ca4SPoul-Henning Kamp static void
2668dee34ca4SPoul-Henning Kamp swapdev_close(struct thread *td, struct swdevt *sp)
2669dee34ca4SPoul-Henning Kamp {
2670dee34ca4SPoul-Henning Kamp 
2671dee34ca4SPoul-Henning Kamp 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2672dee34ca4SPoul-Henning Kamp 	vrele(sp->sw_vp);
2673dee34ca4SPoul-Henning Kamp }
2674dee34ca4SPoul-Henning Kamp 
2675dee34ca4SPoul-Henning Kamp 
2676dee34ca4SPoul-Henning Kamp static int
2677dee34ca4SPoul-Henning Kamp swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2678dee34ca4SPoul-Henning Kamp {
2679dee34ca4SPoul-Henning Kamp 	struct swdevt *sp;
2680dee34ca4SPoul-Henning Kamp 	int error;
2681dee34ca4SPoul-Henning Kamp 
2682dee34ca4SPoul-Henning Kamp 	if (nblks == 0)
2683dee34ca4SPoul-Henning Kamp 		return (ENXIO);
2684dee34ca4SPoul-Henning Kamp 	mtx_lock(&sw_dev_mtx);
2685dee34ca4SPoul-Henning Kamp 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2686dee34ca4SPoul-Henning Kamp 		if (sp->sw_id == vp) {
2687dee34ca4SPoul-Henning Kamp 			mtx_unlock(&sw_dev_mtx);
2688dee34ca4SPoul-Henning Kamp 			return (EBUSY);
2689dee34ca4SPoul-Henning Kamp 		}
2690dee34ca4SPoul-Henning Kamp 	}
2691dee34ca4SPoul-Henning Kamp 	mtx_unlock(&sw_dev_mtx);
2692dee34ca4SPoul-Henning Kamp 
2693cb05b60aSAttilio Rao 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2694dee34ca4SPoul-Henning Kamp #ifdef MAC
269530d239bcSRobert Watson 	error = mac_system_check_swapon(td->td_ucred, vp);
2696dee34ca4SPoul-Henning Kamp 	if (error == 0)
2697dee34ca4SPoul-Henning Kamp #endif
26989e223287SKonstantin Belousov 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
269922db15c0SAttilio Rao 	(void) VOP_UNLOCK(vp, 0);
2700dee34ca4SPoul-Henning Kamp 	if (error)
2701dee34ca4SPoul-Henning Kamp 		return (error);
2702dee34ca4SPoul-Henning Kamp 
2703dee34ca4SPoul-Henning Kamp 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
2704f3732fd1SPoul-Henning Kamp 	    NODEV);
2705dee34ca4SPoul-Henning Kamp 	return (0);
2706dee34ca4SPoul-Henning Kamp }
2707