xref: /freebsd/sys/vm/swap_pager.c (revision 8657387683946d0c03e09fe77029edfe309eeb20)
1 /*-
2  * Copyright (c) 1998 Matthew Dillon,
3  * Copyright (c) 1994 John S. Dyson
4  * Copyright (c) 1990 University of Utah.
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *				New Swap System
41  *				Matthew Dillon
42  *
43  * Radix Bitmap 'blists'.
44  *
45  *	- The new swapper uses the new radix bitmap code.  This should scale
46  *	  to arbitrarily small or arbitrarily large swap spaces and an almost
47  *	  arbitrary degree of fragmentation.
48  *
49  * Features:
50  *
51  *	- on the fly reallocation of swap during putpages.  The new system
52  *	  does not try to keep previously allocated swap blocks for dirty
53  *	  pages.
54  *
55  *	- on the fly deallocation of swap
56  *
57  *	- No more garbage collection required.  Unnecessarily allocated swap
58  *	  blocks only exist for dirty vm_page_t's now and these are already
59  *	  cycled (in a high-load system) by the pager.  We also do on-the-fly
60  *	  removal of invalidated swap blocks when a page is destroyed
61  *	  or renamed.
62  *
63  * from: Utah $Hdr: swap_pager.c 1.4 91/04/30$
64  *
65  *	@(#)swap_pager.c	8.9 (Berkeley) 3/21/94
66  *	@(#)vm_swap.c	8.5 (Berkeley) 2/17/94
67  */
68 
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
71 
72 #include "opt_compat.h"
73 #include "opt_swap.h"
74 #include "opt_vm.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/conf.h>
79 #include <sys/kernel.h>
80 #include <sys/priv.h>
81 #include <sys/proc.h>
82 #include <sys/bio.h>
83 #include <sys/buf.h>
84 #include <sys/disk.h>
85 #include <sys/fcntl.h>
86 #include <sys/mount.h>
87 #include <sys/namei.h>
88 #include <sys/vnode.h>
89 #include <sys/malloc.h>
90 #include <sys/pctrie.h>
91 #include <sys/racct.h>
92 #include <sys/resource.h>
93 #include <sys/resourcevar.h>
94 #include <sys/rwlock.h>
95 #include <sys/sysctl.h>
96 #include <sys/sysproto.h>
97 #include <sys/blist.h>
98 #include <sys/lock.h>
99 #include <sys/sx.h>
100 #include <sys/vmmeter.h>
101 
102 #include <security/mac/mac_framework.h>
103 
104 #include <vm/vm.h>
105 #include <vm/pmap.h>
106 #include <vm/vm_map.h>
107 #include <vm/vm_kern.h>
108 #include <vm/vm_object.h>
109 #include <vm/vm_page.h>
110 #include <vm/vm_pager.h>
111 #include <vm/vm_pageout.h>
112 #include <vm/vm_param.h>
113 #include <vm/swap_pager.h>
114 #include <vm/vm_extern.h>
115 #include <vm/uma.h>
116 
117 #include <geom/geom.h>
118 
119 /*
120  * MAX_PAGEOUT_CLUSTER must be a power of 2 between 1 and 64.
121  * The 64-page limit is due to the radix code (kern/subr_blist.c).
122  */
123 #ifndef MAX_PAGEOUT_CLUSTER
124 #define	MAX_PAGEOUT_CLUSTER	32
125 #endif
126 
127 #if !defined(SWB_NPAGES)
128 #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
129 #endif
130 
131 #define	SWAP_META_PAGES		PCTRIE_COUNT
132 
133 /*
134  * A swblk structure maps each page index within a
135  * SWAP_META_PAGES-aligned and sized range to the address of an
136  * on-disk swap block (or SWAPBLK_NONE). The collection of these
137  * mappings for an entire vm object is implemented as a pc-trie.
138  */
139 struct swblk {
140 	vm_pindex_t	p;
141 	daddr_t		d[SWAP_META_PAGES];
142 };
143 
144 static MALLOC_DEFINE(M_VMPGDATA, "vm_pgdata", "swap pager private data");
145 static struct mtx sw_dev_mtx;
146 static TAILQ_HEAD(, swdevt) swtailq = TAILQ_HEAD_INITIALIZER(swtailq);
147 static struct swdevt *swdevhd;	/* Allocate from here next */
148 static int nswapdev;		/* Number of swap devices */
149 int swap_pager_avail;
150 static struct sx swdev_syscall_lock;	/* serialize swap(on|off) */
151 
152 static vm_ooffset_t swap_total;
153 SYSCTL_QUAD(_vm, OID_AUTO, swap_total, CTLFLAG_RD, &swap_total, 0,
154     "Total amount of available swap storage.");
155 static vm_ooffset_t swap_reserved;
156 SYSCTL_QUAD(_vm, OID_AUTO, swap_reserved, CTLFLAG_RD, &swap_reserved, 0,
157     "Amount of swap storage needed to back all allocated anonymous memory.");
158 static int overcommit = 0;
159 SYSCTL_INT(_vm, OID_AUTO, overcommit, CTLFLAG_RW, &overcommit, 0,
160     "Configure virtual memory overcommit behavior. See tuning(7) "
161     "for details.");
162 static unsigned long swzone;
163 SYSCTL_ULONG(_vm, OID_AUTO, swzone, CTLFLAG_RD, &swzone, 0,
164     "Actual size of swap metadata zone");
165 static unsigned long swap_maxpages;
166 SYSCTL_ULONG(_vm, OID_AUTO, swap_maxpages, CTLFLAG_RD, &swap_maxpages, 0,
167     "Maximum amount of swap supported");
168 
169 /* bits from overcommit */
170 #define	SWAP_RESERVE_FORCE_ON		(1 << 0)
171 #define	SWAP_RESERVE_RLIMIT_ON		(1 << 1)
172 #define	SWAP_RESERVE_ALLOW_NONWIRED	(1 << 2)
173 
174 int
175 swap_reserve(vm_ooffset_t incr)
176 {
177 
178 	return (swap_reserve_by_cred(incr, curthread->td_ucred));
179 }
180 
181 int
182 swap_reserve_by_cred(vm_ooffset_t incr, struct ucred *cred)
183 {
184 	vm_ooffset_t r, s;
185 	int res, error;
186 	static int curfail;
187 	static struct timeval lastfail;
188 	struct uidinfo *uip;
189 
190 	uip = cred->cr_ruidinfo;
191 
192 	if (incr & PAGE_MASK)
193 		panic("swap_reserve: & PAGE_MASK");
194 
195 #ifdef RACCT
196 	if (racct_enable) {
197 		PROC_LOCK(curproc);
198 		error = racct_add(curproc, RACCT_SWAP, incr);
199 		PROC_UNLOCK(curproc);
200 		if (error != 0)
201 			return (0);
202 	}
203 #endif
204 
205 	res = 0;
206 	mtx_lock(&sw_dev_mtx);
207 	r = swap_reserved + incr;
208 	if (overcommit & SWAP_RESERVE_ALLOW_NONWIRED) {
209 		s = vm_cnt.v_page_count - vm_cnt.v_free_reserved - vm_cnt.v_wire_count;
210 		s *= PAGE_SIZE;
211 	} else
212 		s = 0;
213 	s += swap_total;
214 	if ((overcommit & SWAP_RESERVE_FORCE_ON) == 0 || r <= s ||
215 	    (error = priv_check(curthread, PRIV_VM_SWAP_NOQUOTA)) == 0) {
216 		res = 1;
217 		swap_reserved = r;
218 	}
219 	mtx_unlock(&sw_dev_mtx);
220 
221 	if (res) {
222 		UIDINFO_VMSIZE_LOCK(uip);
223 		if ((overcommit & SWAP_RESERVE_RLIMIT_ON) != 0 &&
224 		    uip->ui_vmsize + incr > lim_cur(curthread, RLIMIT_SWAP) &&
225 		    priv_check(curthread, PRIV_VM_SWAP_NORLIMIT))
226 			res = 0;
227 		else
228 			uip->ui_vmsize += incr;
229 		UIDINFO_VMSIZE_UNLOCK(uip);
230 		if (!res) {
231 			mtx_lock(&sw_dev_mtx);
232 			swap_reserved -= incr;
233 			mtx_unlock(&sw_dev_mtx);
234 		}
235 	}
236 	if (!res && ppsratecheck(&lastfail, &curfail, 1)) {
237 		printf("uid %d, pid %d: swap reservation for %jd bytes failed\n",
238 		    uip->ui_uid, curproc->p_pid, incr);
239 	}
240 
241 #ifdef RACCT
242 	if (!res) {
243 		PROC_LOCK(curproc);
244 		racct_sub(curproc, RACCT_SWAP, incr);
245 		PROC_UNLOCK(curproc);
246 	}
247 #endif
248 
249 	return (res);
250 }
251 
252 void
253 swap_reserve_force(vm_ooffset_t incr)
254 {
255 	struct uidinfo *uip;
256 
257 	mtx_lock(&sw_dev_mtx);
258 	swap_reserved += incr;
259 	mtx_unlock(&sw_dev_mtx);
260 
261 #ifdef RACCT
262 	PROC_LOCK(curproc);
263 	racct_add_force(curproc, RACCT_SWAP, incr);
264 	PROC_UNLOCK(curproc);
265 #endif
266 
267 	uip = curthread->td_ucred->cr_ruidinfo;
268 	PROC_LOCK(curproc);
269 	UIDINFO_VMSIZE_LOCK(uip);
270 	uip->ui_vmsize += incr;
271 	UIDINFO_VMSIZE_UNLOCK(uip);
272 	PROC_UNLOCK(curproc);
273 }
274 
275 void
276 swap_release(vm_ooffset_t decr)
277 {
278 	struct ucred *cred;
279 
280 	PROC_LOCK(curproc);
281 	cred = curthread->td_ucred;
282 	swap_release_by_cred(decr, cred);
283 	PROC_UNLOCK(curproc);
284 }
285 
286 void
287 swap_release_by_cred(vm_ooffset_t decr, struct ucred *cred)
288 {
289  	struct uidinfo *uip;
290 
291 	uip = cred->cr_ruidinfo;
292 
293 	if (decr & PAGE_MASK)
294 		panic("swap_release: & PAGE_MASK");
295 
296 	mtx_lock(&sw_dev_mtx);
297 	if (swap_reserved < decr)
298 		panic("swap_reserved < decr");
299 	swap_reserved -= decr;
300 	mtx_unlock(&sw_dev_mtx);
301 
302 	UIDINFO_VMSIZE_LOCK(uip);
303 	if (uip->ui_vmsize < decr)
304 		printf("negative vmsize for uid = %d\n", uip->ui_uid);
305 	uip->ui_vmsize -= decr;
306 	UIDINFO_VMSIZE_UNLOCK(uip);
307 
308 	racct_sub_cred(cred, RACCT_SWAP, decr);
309 }
310 
311 #define SWM_FREE	0x02	/* free, period			*/
312 #define SWM_POP		0x04	/* pop out			*/
313 
314 int swap_pager_full = 2;	/* swap space exhaustion (task killing) */
315 static int swap_pager_almost_full = 1; /* swap space exhaustion (w/hysteresis)*/
316 static int nsw_rcount;		/* free read buffers			*/
317 static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
318 static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
319 static int nsw_wcount_async_max;/* assigned maximum			*/
320 static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
321 
322 static int sysctl_swap_async_max(SYSCTL_HANDLER_ARGS);
323 SYSCTL_PROC(_vm, OID_AUTO, swap_async_max, CTLTYPE_INT | CTLFLAG_RW |
324     CTLFLAG_MPSAFE, NULL, 0, sysctl_swap_async_max, "I",
325     "Maximum running async swap ops");
326 
327 static struct sx sw_alloc_sx;
328 
329 /*
330  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
331  * of searching a named list by hashing it just a little.
332  */
333 
334 #define NOBJLISTS		8
335 
336 #define NOBJLIST(handle)	\
337 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
338 
339 static struct pagerlst	swap_pager_object_list[NOBJLISTS];
340 static uma_zone_t swblk_zone;
341 static uma_zone_t swpctrie_zone;
342 
343 /*
344  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
345  * calls hooked from other parts of the VM system and do not appear here.
346  * (see vm/swap_pager.h).
347  */
348 static vm_object_t
349 		swap_pager_alloc(void *handle, vm_ooffset_t size,
350 		    vm_prot_t prot, vm_ooffset_t offset, struct ucred *);
351 static void	swap_pager_dealloc(vm_object_t object);
352 static int	swap_pager_getpages(vm_object_t, vm_page_t *, int, int *,
353     int *);
354 static int	swap_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
355     int *, pgo_getpages_iodone_t, void *);
356 static void	swap_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
357 static boolean_t
358 		swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, int *after);
359 static void	swap_pager_init(void);
360 static void	swap_pager_unswapped(vm_page_t);
361 static void	swap_pager_swapoff(struct swdevt *sp);
362 
363 struct pagerops swappagerops = {
364 	.pgo_init =	swap_pager_init,	/* early system initialization of pager	*/
365 	.pgo_alloc =	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
366 	.pgo_dealloc =	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
367 	.pgo_getpages =	swap_pager_getpages,	/* pagein				*/
368 	.pgo_getpages_async = swap_pager_getpages_async, /* pagein (async)		*/
369 	.pgo_putpages =	swap_pager_putpages,	/* pageout				*/
370 	.pgo_haspage =	swap_pager_haspage,	/* get backing store status for page	*/
371 	.pgo_pageunswapped = swap_pager_unswapped,	/* remove swap related to page		*/
372 };
373 
374 /*
375  * swap_*() routines are externally accessible.  swp_*() routines are
376  * internal.
377  */
378 static int nswap_lowat = 128;	/* in pages, swap_pager_almost_full warn */
379 static int nswap_hiwat = 512;	/* in pages, swap_pager_almost_full warn */
380 
381 SYSCTL_INT(_vm, OID_AUTO, dmmax, CTLFLAG_RD, &nsw_cluster_max, 0,
382     "Maximum size of a swap block in pages");
383 
384 static void	swp_sizecheck(void);
385 static void	swp_pager_async_iodone(struct buf *bp);
386 static int	swapongeom(struct vnode *);
387 static int	swaponvp(struct thread *, struct vnode *, u_long);
388 static int	swapoff_one(struct swdevt *sp, struct ucred *cred);
389 
390 /*
391  * Swap bitmap functions
392  */
393 static void	swp_pager_freeswapspace(daddr_t blk, int npages);
394 static daddr_t	swp_pager_getswapspace(int npages);
395 
396 /*
397  * Metadata functions
398  */
399 static void swp_pager_meta_build(vm_object_t, vm_pindex_t, daddr_t);
400 static void swp_pager_meta_free(vm_object_t, vm_pindex_t, vm_pindex_t);
401 static void swp_pager_meta_free_all(vm_object_t);
402 static daddr_t swp_pager_meta_ctl(vm_object_t, vm_pindex_t, int);
403 
404 static void *
405 swblk_trie_alloc(struct pctrie *ptree)
406 {
407 
408 	return (uma_zalloc(swpctrie_zone, M_NOWAIT | (curproc == pageproc ?
409 	    M_USE_RESERVE : 0)));
410 }
411 
412 static void
413 swblk_trie_free(struct pctrie *ptree, void *node)
414 {
415 
416 	uma_zfree(swpctrie_zone, node);
417 }
418 
419 PCTRIE_DEFINE(SWAP, swblk, p, swblk_trie_alloc, swblk_trie_free);
420 
421 /*
422  * SWP_SIZECHECK() -	update swap_pager_full indication
423  *
424  *	update the swap_pager_almost_full indication and warn when we are
425  *	about to run out of swap space, using lowat/hiwat hysteresis.
426  *
427  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
428  *
429  *	No restrictions on call
430  *	This routine may not block.
431  */
432 static void
433 swp_sizecheck(void)
434 {
435 
436 	if (swap_pager_avail < nswap_lowat) {
437 		if (swap_pager_almost_full == 0) {
438 			printf("swap_pager: out of swap space\n");
439 			swap_pager_almost_full = 1;
440 		}
441 	} else {
442 		swap_pager_full = 0;
443 		if (swap_pager_avail > nswap_hiwat)
444 			swap_pager_almost_full = 0;
445 	}
446 }
447 
448 /*
449  * SWAP_PAGER_INIT() -	initialize the swap pager!
450  *
451  *	Expected to be started from system init.  NOTE:  This code is run
452  *	before much else so be careful what you depend on.  Most of the VM
453  *	system has yet to be initialized at this point.
454  */
455 static void
456 swap_pager_init(void)
457 {
458 	/*
459 	 * Initialize object lists
460 	 */
461 	int i;
462 
463 	for (i = 0; i < NOBJLISTS; ++i)
464 		TAILQ_INIT(&swap_pager_object_list[i]);
465 	mtx_init(&sw_dev_mtx, "swapdev", NULL, MTX_DEF);
466 	sx_init(&sw_alloc_sx, "swspsx");
467 	sx_init(&swdev_syscall_lock, "swsysc");
468 }
469 
470 /*
471  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
472  *
473  *	Expected to be started from pageout process once, prior to entering
474  *	its main loop.
475  */
476 void
477 swap_pager_swap_init(void)
478 {
479 	unsigned long n, n2;
480 
481 	/*
482 	 * Number of in-transit swap bp operations.  Don't
483 	 * exhaust the pbufs completely.  Make sure we
484 	 * initialize workable values (0 will work for hysteresis
485 	 * but it isn't very efficient).
486 	 *
487 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
488 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
489 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
490 	 * constrained by the swap device interleave stripe size.
491 	 *
492 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
493 	 * designed to prevent other I/O from having high latencies due to
494 	 * our pageout I/O.  The value 4 works well for one or two active swap
495 	 * devices but is probably a little low if you have more.  Even so,
496 	 * a higher value would probably generate only a limited improvement
497 	 * with three or four active swap devices since the system does not
498 	 * typically have to pageout at extreme bandwidths.   We will want
499 	 * at least 2 per swap devices, and 4 is a pretty good value if you
500 	 * have one NFS swap device due to the command/ack latency over NFS.
501 	 * So it all works out pretty well.
502 	 */
503 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
504 
505 	mtx_lock(&pbuf_mtx);
506 	nsw_rcount = (nswbuf + 1) / 2;
507 	nsw_wcount_sync = (nswbuf + 3) / 4;
508 	nsw_wcount_async = 4;
509 	nsw_wcount_async_max = nsw_wcount_async;
510 	mtx_unlock(&pbuf_mtx);
511 
512 	/*
513 	 * Initialize our zone, guessing on the number we need based
514 	 * on the number of pages in the system.
515 	 */
516 	n = vm_cnt.v_page_count / 2;
517 	if (maxswzone && n > maxswzone / sizeof(struct swblk))
518 		n = maxswzone / sizeof(struct swblk);
519 	swpctrie_zone = uma_zcreate("swpctrie", pctrie_node_size(), NULL, NULL,
520 	    pctrie_zone_init, NULL, UMA_ALIGN_PTR,
521 	    UMA_ZONE_NOFREE | UMA_ZONE_VM);
522 	if (swpctrie_zone == NULL)
523 		panic("failed to create swap pctrie zone.");
524 	swblk_zone = uma_zcreate("swblk", sizeof(struct swblk), NULL, NULL,
525 	    NULL, NULL, _Alignof(struct swblk) - 1,
526 	    UMA_ZONE_NOFREE | UMA_ZONE_VM);
527 	if (swblk_zone == NULL)
528 		panic("failed to create swap blk zone.");
529 	n2 = n;
530 	do {
531 		if (uma_zone_reserve_kva(swblk_zone, n))
532 			break;
533 		/*
534 		 * if the allocation failed, try a zone two thirds the
535 		 * size of the previous attempt.
536 		 */
537 		n -= ((n + 2) / 3);
538 	} while (n > 0);
539 	if (n2 != n)
540 		printf("Swap blk zone entries reduced from %lu to %lu.\n",
541 		    n2, n);
542 	swap_maxpages = n * SWAP_META_PAGES;
543 	swzone = n * sizeof(struct swblk);
544 	if (!uma_zone_reserve_kva(swpctrie_zone, n))
545 		printf("Cannot reserve swap pctrie zone, "
546 		    "reduce kern.maxswzone.\n");
547 }
548 
549 static vm_object_t
550 swap_pager_alloc_init(void *handle, struct ucred *cred, vm_ooffset_t size,
551     vm_ooffset_t offset)
552 {
553 	vm_object_t object;
554 
555 	if (cred != NULL) {
556 		if (!swap_reserve_by_cred(size, cred))
557 			return (NULL);
558 		crhold(cred);
559 	}
560 
561 	/*
562 	 * The un_pager.swp.swp_blks trie is initialized by
563 	 * vm_object_allocate() to ensure the correct order of
564 	 * visibility to other threads.
565 	 */
566 	object = vm_object_allocate(OBJT_SWAP, OFF_TO_IDX(offset +
567 	    PAGE_MASK + size));
568 
569 	object->handle = handle;
570 	if (cred != NULL) {
571 		object->cred = cred;
572 		object->charge = size;
573 	}
574 	return (object);
575 }
576 
577 /*
578  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
579  *			its metadata structures.
580  *
581  *	This routine is called from the mmap and fork code to create a new
582  *	OBJT_SWAP object.
583  *
584  *	This routine must ensure that no live duplicate is created for
585  *	the named object request, which is protected against by
586  *	holding the sw_alloc_sx lock in case handle != NULL.
587  */
588 static vm_object_t
589 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
590     vm_ooffset_t offset, struct ucred *cred)
591 {
592 	vm_object_t object;
593 
594 	if (handle != NULL) {
595 		/*
596 		 * Reference existing named region or allocate new one.  There
597 		 * should not be a race here against swp_pager_meta_build()
598 		 * as called from vm_page_remove() in regards to the lookup
599 		 * of the handle.
600 		 */
601 		sx_xlock(&sw_alloc_sx);
602 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
603 		if (object == NULL) {
604 			object = swap_pager_alloc_init(handle, cred, size,
605 			    offset);
606 			if (object != NULL) {
607 				TAILQ_INSERT_TAIL(NOBJLIST(object->handle),
608 				    object, pager_object_list);
609 			}
610 		}
611 		sx_xunlock(&sw_alloc_sx);
612 	} else {
613 		object = swap_pager_alloc_init(handle, cred, size, offset);
614 	}
615 	return (object);
616 }
617 
618 /*
619  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
620  *
621  *	The swap backing for the object is destroyed.  The code is
622  *	designed such that we can reinstantiate it later, but this
623  *	routine is typically called only when the entire object is
624  *	about to be destroyed.
625  *
626  *	The object must be locked.
627  */
628 static void
629 swap_pager_dealloc(vm_object_t object)
630 {
631 
632 	VM_OBJECT_ASSERT_WLOCKED(object);
633 	KASSERT((object->flags & OBJ_DEAD) != 0, ("dealloc of reachable obj"));
634 
635 	/*
636 	 * Remove from list right away so lookups will fail if we block for
637 	 * pageout completion.
638 	 */
639 	if (object->handle != NULL) {
640 		VM_OBJECT_WUNLOCK(object);
641 		sx_xlock(&sw_alloc_sx);
642 		TAILQ_REMOVE(NOBJLIST(object->handle), object,
643 		    pager_object_list);
644 		sx_xunlock(&sw_alloc_sx);
645 		VM_OBJECT_WLOCK(object);
646 	}
647 
648 	vm_object_pip_wait(object, "swpdea");
649 
650 	/*
651 	 * Free all remaining metadata.  We only bother to free it from
652 	 * the swap meta data.  We do not attempt to free swapblk's still
653 	 * associated with vm_page_t's for this object.  We do not care
654 	 * if paging is still in progress on some objects.
655 	 */
656 	swp_pager_meta_free_all(object);
657 	object->handle = NULL;
658 	object->type = OBJT_DEAD;
659 }
660 
661 /************************************************************************
662  *			SWAP PAGER BITMAP ROUTINES			*
663  ************************************************************************/
664 
665 /*
666  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
667  *
668  *	Allocate swap for the requested number of pages.  The starting
669  *	swap block number (a page index) is returned or SWAPBLK_NONE
670  *	if the allocation failed.
671  *
672  *	Also has the side effect of advising that somebody made a mistake
673  *	when they configured swap and didn't configure enough.
674  *
675  *	This routine may not sleep.
676  *
677  *	We allocate in round-robin fashion from the configured devices.
678  */
679 static daddr_t
680 swp_pager_getswapspace(int npages)
681 {
682 	daddr_t blk;
683 	struct swdevt *sp;
684 	int i;
685 
686 	blk = SWAPBLK_NONE;
687 	mtx_lock(&sw_dev_mtx);
688 	sp = swdevhd;
689 	for (i = 0; i < nswapdev; i++) {
690 		if (sp == NULL)
691 			sp = TAILQ_FIRST(&swtailq);
692 		if (!(sp->sw_flags & SW_CLOSING)) {
693 			blk = blist_alloc(sp->sw_blist, npages);
694 			if (blk != SWAPBLK_NONE) {
695 				blk += sp->sw_first;
696 				sp->sw_used += npages;
697 				swap_pager_avail -= npages;
698 				swp_sizecheck();
699 				swdevhd = TAILQ_NEXT(sp, sw_list);
700 				goto done;
701 			}
702 		}
703 		sp = TAILQ_NEXT(sp, sw_list);
704 	}
705 	if (swap_pager_full != 2) {
706 		printf("swap_pager_getswapspace(%d): failed\n", npages);
707 		swap_pager_full = 2;
708 		swap_pager_almost_full = 1;
709 	}
710 	swdevhd = NULL;
711 done:
712 	mtx_unlock(&sw_dev_mtx);
713 	return (blk);
714 }
715 
716 static int
717 swp_pager_isondev(daddr_t blk, struct swdevt *sp)
718 {
719 
720 	return (blk >= sp->sw_first && blk < sp->sw_end);
721 }
722 
723 static void
724 swp_pager_strategy(struct buf *bp)
725 {
726 	struct swdevt *sp;
727 
728 	mtx_lock(&sw_dev_mtx);
729 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
730 		if (bp->b_blkno >= sp->sw_first && bp->b_blkno < sp->sw_end) {
731 			mtx_unlock(&sw_dev_mtx);
732 			if ((sp->sw_flags & SW_UNMAPPED) != 0 &&
733 			    unmapped_buf_allowed) {
734 				bp->b_data = unmapped_buf;
735 				bp->b_offset = 0;
736 			} else {
737 				pmap_qenter((vm_offset_t)bp->b_data,
738 				    &bp->b_pages[0], bp->b_bcount / PAGE_SIZE);
739 			}
740 			sp->sw_strategy(bp, sp);
741 			return;
742 		}
743 	}
744 	panic("Swapdev not found");
745 }
746 
747 
748 /*
749  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
750  *
751  *	This routine returns the specified swap blocks back to the bitmap.
752  *
753  *	This routine may not sleep.
754  */
755 static void
756 swp_pager_freeswapspace(daddr_t blk, int npages)
757 {
758 	struct swdevt *sp;
759 
760 	mtx_lock(&sw_dev_mtx);
761 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
762 		if (blk >= sp->sw_first && blk < sp->sw_end) {
763 			sp->sw_used -= npages;
764 			/*
765 			 * If we are attempting to stop swapping on
766 			 * this device, we don't want to mark any
767 			 * blocks free lest they be reused.
768 			 */
769 			if ((sp->sw_flags & SW_CLOSING) == 0) {
770 				blist_free(sp->sw_blist, blk - sp->sw_first,
771 				    npages);
772 				swap_pager_avail += npages;
773 				swp_sizecheck();
774 			}
775 			mtx_unlock(&sw_dev_mtx);
776 			return;
777 		}
778 	}
779 	panic("Swapdev not found");
780 }
781 
782 /*
783  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
784  *				range within an object.
785  *
786  *	This is a globally accessible routine.
787  *
788  *	This routine removes swapblk assignments from swap metadata.
789  *
790  *	The external callers of this routine typically have already destroyed
791  *	or renamed vm_page_t's associated with this range in the object so
792  *	we should be ok.
793  *
794  *	The object must be locked.
795  */
796 void
797 swap_pager_freespace(vm_object_t object, vm_pindex_t start, vm_size_t size)
798 {
799 
800 	swp_pager_meta_free(object, start, size);
801 }
802 
803 /*
804  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
805  *
806  *	Assigns swap blocks to the specified range within the object.  The
807  *	swap blocks are not zeroed.  Any previous swap assignment is destroyed.
808  *
809  *	Returns 0 on success, -1 on failure.
810  */
811 int
812 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
813 {
814 	int n = 0;
815 	daddr_t blk = SWAPBLK_NONE;
816 	vm_pindex_t beg = start;	/* save start index */
817 
818 	VM_OBJECT_WLOCK(object);
819 	while (size) {
820 		if (n == 0) {
821 			n = BLIST_MAX_ALLOC;
822 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
823 				n >>= 1;
824 				if (n == 0) {
825 					swp_pager_meta_free(object, beg, start - beg);
826 					VM_OBJECT_WUNLOCK(object);
827 					return (-1);
828 				}
829 			}
830 		}
831 		swp_pager_meta_build(object, start, blk);
832 		--size;
833 		++start;
834 		++blk;
835 		--n;
836 	}
837 	swp_pager_meta_free(object, start, n);
838 	VM_OBJECT_WUNLOCK(object);
839 	return (0);
840 }
841 
842 /*
843  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
844  *			and destroy the source.
845  *
846  *	Copy any valid swapblks from the source to the destination.  In
847  *	cases where both the source and destination have a valid swapblk,
848  *	we keep the destination's.
849  *
850  *	This routine is allowed to sleep.  It may sleep allocating metadata
851  *	indirectly through swp_pager_meta_build() or if paging is still in
852  *	progress on the source.
853  *
854  *	The source object contains no vm_page_t's (which is just as well)
855  *
856  *	The source object is of type OBJT_SWAP.
857  *
858  *	The source and destination objects must be locked.
859  *	Both object locks may temporarily be released.
860  */
861 void
862 swap_pager_copy(vm_object_t srcobject, vm_object_t dstobject,
863     vm_pindex_t offset, int destroysource)
864 {
865 	vm_pindex_t i;
866 
867 	VM_OBJECT_ASSERT_WLOCKED(srcobject);
868 	VM_OBJECT_ASSERT_WLOCKED(dstobject);
869 
870 	/*
871 	 * If destroysource is set, we remove the source object from the
872 	 * swap_pager internal queue now.
873 	 */
874 	if (destroysource && srcobject->handle != NULL) {
875 		vm_object_pip_add(srcobject, 1);
876 		VM_OBJECT_WUNLOCK(srcobject);
877 		vm_object_pip_add(dstobject, 1);
878 		VM_OBJECT_WUNLOCK(dstobject);
879 		sx_xlock(&sw_alloc_sx);
880 		TAILQ_REMOVE(NOBJLIST(srcobject->handle), srcobject,
881 		    pager_object_list);
882 		sx_xunlock(&sw_alloc_sx);
883 		VM_OBJECT_WLOCK(dstobject);
884 		vm_object_pip_wakeup(dstobject);
885 		VM_OBJECT_WLOCK(srcobject);
886 		vm_object_pip_wakeup(srcobject);
887 	}
888 
889 	/*
890 	 * transfer source to destination.
891 	 */
892 	for (i = 0; i < dstobject->size; ++i) {
893 		daddr_t dstaddr;
894 
895 		/*
896 		 * Locate (without changing) the swapblk on the destination,
897 		 * unless it is invalid in which case free it silently, or
898 		 * if the destination is a resident page, in which case the
899 		 * source is thrown away.
900 		 */
901 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
902 
903 		if (dstaddr == SWAPBLK_NONE) {
904 			/*
905 			 * Destination has no swapblk and is not resident,
906 			 * copy source.
907 			 */
908 			daddr_t srcaddr;
909 
910 			srcaddr = swp_pager_meta_ctl(
911 			    srcobject,
912 			    i + offset,
913 			    SWM_POP
914 			);
915 
916 			if (srcaddr != SWAPBLK_NONE) {
917 				/*
918 				 * swp_pager_meta_build() can sleep.
919 				 */
920 				vm_object_pip_add(srcobject, 1);
921 				VM_OBJECT_WUNLOCK(srcobject);
922 				vm_object_pip_add(dstobject, 1);
923 				swp_pager_meta_build(dstobject, i, srcaddr);
924 				vm_object_pip_wakeup(dstobject);
925 				VM_OBJECT_WLOCK(srcobject);
926 				vm_object_pip_wakeup(srcobject);
927 			}
928 		} else {
929 			/*
930 			 * Destination has valid swapblk or it is represented
931 			 * by a resident page.  We destroy the sourceblock.
932 			 */
933 
934 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
935 		}
936 	}
937 
938 	/*
939 	 * Free left over swap blocks in source.
940 	 *
941 	 * We have to revert the type to OBJT_DEFAULT so we do not accidentally
942 	 * double-remove the object from the swap queues.
943 	 */
944 	if (destroysource) {
945 		swp_pager_meta_free_all(srcobject);
946 		/*
947 		 * Reverting the type is not necessary, the caller is going
948 		 * to destroy srcobject directly, but I'm doing it here
949 		 * for consistency since we've removed the object from its
950 		 * queues.
951 		 */
952 		srcobject->type = OBJT_DEFAULT;
953 	}
954 }
955 
956 /*
957  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
958  *				the requested page.
959  *
960  *	We determine whether good backing store exists for the requested
961  *	page and return TRUE if it does, FALSE if it doesn't.
962  *
963  *	If TRUE, we also try to determine how much valid, contiguous backing
964  *	store exists before and after the requested page.
965  */
966 static boolean_t
967 swap_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
968     int *after)
969 {
970 	daddr_t blk, blk0;
971 	int i;
972 
973 	VM_OBJECT_ASSERT_LOCKED(object);
974 
975 	/*
976 	 * do we have good backing store at the requested index ?
977 	 */
978 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
979 	if (blk0 == SWAPBLK_NONE) {
980 		if (before)
981 			*before = 0;
982 		if (after)
983 			*after = 0;
984 		return (FALSE);
985 	}
986 
987 	/*
988 	 * find backwards-looking contiguous good backing store
989 	 */
990 	if (before != NULL) {
991 		for (i = 1; i < SWB_NPAGES; i++) {
992 			if (i > pindex)
993 				break;
994 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
995 			if (blk != blk0 - i)
996 				break;
997 		}
998 		*before = i - 1;
999 	}
1000 
1001 	/*
1002 	 * find forward-looking contiguous good backing store
1003 	 */
1004 	if (after != NULL) {
1005 		for (i = 1; i < SWB_NPAGES; i++) {
1006 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
1007 			if (blk != blk0 + i)
1008 				break;
1009 		}
1010 		*after = i - 1;
1011 	}
1012 	return (TRUE);
1013 }
1014 
1015 /*
1016  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
1017  *
1018  *	This removes any associated swap backing store, whether valid or
1019  *	not, from the page.
1020  *
1021  *	This routine is typically called when a page is made dirty, at
1022  *	which point any associated swap can be freed.  MADV_FREE also
1023  *	calls us in a special-case situation
1024  *
1025  *	NOTE!!!  If the page is clean and the swap was valid, the caller
1026  *	should make the page dirty before calling this routine.  This routine
1027  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
1028  *	depends on it.
1029  *
1030  *	This routine may not sleep.
1031  *
1032  *	The object containing the page must be locked.
1033  */
1034 static void
1035 swap_pager_unswapped(vm_page_t m)
1036 {
1037 
1038 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
1039 }
1040 
1041 /*
1042  * swap_pager_getpages() - bring pages in from swap
1043  *
1044  *	Attempt to page in the pages in array "m" of length "count".  The caller
1045  *	may optionally specify that additional pages preceding and succeeding
1046  *	the specified range be paged in.  The number of such pages is returned
1047  *	in the "rbehind" and "rahead" parameters, and they will be in the
1048  *	inactive queue upon return.
1049  *
1050  *	The pages in "m" must be busied and will remain busied upon return.
1051  */
1052 static int
1053 swap_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
1054     int *rahead)
1055 {
1056 	struct buf *bp;
1057 	vm_page_t mpred, msucc, p;
1058 	vm_pindex_t pindex;
1059 	daddr_t blk;
1060 	int i, j, maxahead, maxbehind, reqcount, shift;
1061 
1062 	reqcount = count;
1063 
1064 	VM_OBJECT_WUNLOCK(object);
1065 	bp = getpbuf(&nsw_rcount);
1066 	VM_OBJECT_WLOCK(object);
1067 
1068 	if (!swap_pager_haspage(object, m[0]->pindex, &maxbehind, &maxahead)) {
1069 		relpbuf(bp, &nsw_rcount);
1070 		return (VM_PAGER_FAIL);
1071 	}
1072 
1073 	/*
1074 	 * Clip the readahead and readbehind ranges to exclude resident pages.
1075 	 */
1076 	if (rahead != NULL) {
1077 		KASSERT(reqcount - 1 <= maxahead,
1078 		    ("page count %d extends beyond swap block", reqcount));
1079 		*rahead = imin(*rahead, maxahead - (reqcount - 1));
1080 		pindex = m[reqcount - 1]->pindex;
1081 		msucc = TAILQ_NEXT(m[reqcount - 1], listq);
1082 		if (msucc != NULL && msucc->pindex - pindex - 1 < *rahead)
1083 			*rahead = msucc->pindex - pindex - 1;
1084 	}
1085 	if (rbehind != NULL) {
1086 		*rbehind = imin(*rbehind, maxbehind);
1087 		pindex = m[0]->pindex;
1088 		mpred = TAILQ_PREV(m[0], pglist, listq);
1089 		if (mpred != NULL && pindex - mpred->pindex - 1 < *rbehind)
1090 			*rbehind = pindex - mpred->pindex - 1;
1091 	}
1092 
1093 	/*
1094 	 * Allocate readahead and readbehind pages.
1095 	 */
1096 	shift = rbehind != NULL ? *rbehind : 0;
1097 	if (shift != 0) {
1098 		for (i = 1; i <= shift; i++) {
1099 			p = vm_page_alloc(object, m[0]->pindex - i,
1100 			    VM_ALLOC_NORMAL);
1101 			if (p == NULL) {
1102 				/* Shift allocated pages to the left. */
1103 				for (j = 0; j < i - 1; j++)
1104 					bp->b_pages[j] =
1105 					    bp->b_pages[j + shift - i + 1];
1106 				break;
1107 			}
1108 			bp->b_pages[shift - i] = p;
1109 		}
1110 		shift = i - 1;
1111 		*rbehind = shift;
1112 	}
1113 	for (i = 0; i < reqcount; i++)
1114 		bp->b_pages[i + shift] = m[i];
1115 	if (rahead != NULL) {
1116 		for (i = 0; i < *rahead; i++) {
1117 			p = vm_page_alloc(object,
1118 			    m[reqcount - 1]->pindex + i + 1, VM_ALLOC_NORMAL);
1119 			if (p == NULL)
1120 				break;
1121 			bp->b_pages[shift + reqcount + i] = p;
1122 		}
1123 		*rahead = i;
1124 	}
1125 	if (rbehind != NULL)
1126 		count += *rbehind;
1127 	if (rahead != NULL)
1128 		count += *rahead;
1129 
1130 	vm_object_pip_add(object, count);
1131 
1132 	for (i = 0; i < count; i++)
1133 		bp->b_pages[i]->oflags |= VPO_SWAPINPROG;
1134 
1135 	pindex = bp->b_pages[0]->pindex;
1136 	blk = swp_pager_meta_ctl(object, pindex, 0);
1137 	KASSERT(blk != SWAPBLK_NONE,
1138 	    ("no swap blocking containing %p(%jx)", object, (uintmax_t)pindex));
1139 
1140 	VM_OBJECT_WUNLOCK(object);
1141 
1142 	bp->b_flags |= B_PAGING;
1143 	bp->b_iocmd = BIO_READ;
1144 	bp->b_iodone = swp_pager_async_iodone;
1145 	bp->b_rcred = crhold(thread0.td_ucred);
1146 	bp->b_wcred = crhold(thread0.td_ucred);
1147 	bp->b_blkno = blk;
1148 	bp->b_bcount = PAGE_SIZE * count;
1149 	bp->b_bufsize = PAGE_SIZE * count;
1150 	bp->b_npages = count;
1151 	bp->b_pgbefore = rbehind != NULL ? *rbehind : 0;
1152 	bp->b_pgafter = rahead != NULL ? *rahead : 0;
1153 
1154 	VM_CNT_INC(v_swapin);
1155 	VM_CNT_ADD(v_swappgsin, count);
1156 
1157 	/*
1158 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
1159 	 * this point because we automatically release it on completion.
1160 	 * Instead, we look at the one page we are interested in which we
1161 	 * still hold a lock on even through the I/O completion.
1162 	 *
1163 	 * The other pages in our m[] array are also released on completion,
1164 	 * so we cannot assume they are valid anymore either.
1165 	 *
1166 	 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
1167 	 */
1168 	BUF_KERNPROC(bp);
1169 	swp_pager_strategy(bp);
1170 
1171 	/*
1172 	 * Wait for the pages we want to complete.  VPO_SWAPINPROG is always
1173 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1174 	 * is set in the metadata for each page in the request.
1175 	 */
1176 	VM_OBJECT_WLOCK(object);
1177 	while ((m[0]->oflags & VPO_SWAPINPROG) != 0) {
1178 		m[0]->oflags |= VPO_SWAPSLEEP;
1179 		VM_CNT_INC(v_intrans);
1180 		if (VM_OBJECT_SLEEP(object, &object->paging_in_progress, PSWP,
1181 		    "swread", hz * 20)) {
1182 			printf(
1183 "swap_pager: indefinite wait buffer: bufobj: %p, blkno: %jd, size: %ld\n",
1184 			    bp->b_bufobj, (intmax_t)bp->b_blkno, bp->b_bcount);
1185 		}
1186 	}
1187 
1188 	/*
1189 	 * If we had an unrecoverable read error pages will not be valid.
1190 	 */
1191 	for (i = 0; i < reqcount; i++)
1192 		if (m[i]->valid != VM_PAGE_BITS_ALL)
1193 			return (VM_PAGER_ERROR);
1194 
1195 	return (VM_PAGER_OK);
1196 
1197 	/*
1198 	 * A final note: in a low swap situation, we cannot deallocate swap
1199 	 * and mark a page dirty here because the caller is likely to mark
1200 	 * the page clean when we return, causing the page to possibly revert
1201 	 * to all-zero's later.
1202 	 */
1203 }
1204 
1205 /*
1206  * 	swap_pager_getpages_async():
1207  *
1208  *	Right now this is emulation of asynchronous operation on top of
1209  *	swap_pager_getpages().
1210  */
1211 static int
1212 swap_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
1213     int *rbehind, int *rahead, pgo_getpages_iodone_t iodone, void *arg)
1214 {
1215 	int r, error;
1216 
1217 	r = swap_pager_getpages(object, m, count, rbehind, rahead);
1218 	VM_OBJECT_WUNLOCK(object);
1219 	switch (r) {
1220 	case VM_PAGER_OK:
1221 		error = 0;
1222 		break;
1223 	case VM_PAGER_ERROR:
1224 		error = EIO;
1225 		break;
1226 	case VM_PAGER_FAIL:
1227 		error = EINVAL;
1228 		break;
1229 	default:
1230 		panic("unhandled swap_pager_getpages() error %d", r);
1231 	}
1232 	(iodone)(arg, m, count, error);
1233 	VM_OBJECT_WLOCK(object);
1234 
1235 	return (r);
1236 }
1237 
1238 /*
1239  *	swap_pager_putpages:
1240  *
1241  *	Assign swap (if necessary) and initiate I/O on the specified pages.
1242  *
1243  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
1244  *	are automatically converted to SWAP objects.
1245  *
1246  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
1247  *	vm_page reservation system coupled with properly written VFS devices
1248  *	should ensure that no low-memory deadlock occurs.  This is an area
1249  *	which needs work.
1250  *
1251  *	The parent has N vm_object_pip_add() references prior to
1252  *	calling us and will remove references for rtvals[] that are
1253  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
1254  *	completion.
1255  *
1256  *	The parent has soft-busy'd the pages it passes us and will unbusy
1257  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
1258  *	We need to unbusy the rest on I/O completion.
1259  */
1260 static void
1261 swap_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1262     int flags, int *rtvals)
1263 {
1264 	int i, n;
1265 	boolean_t sync;
1266 
1267 	if (count && m[0]->object != object) {
1268 		panic("swap_pager_putpages: object mismatch %p/%p",
1269 		    object,
1270 		    m[0]->object
1271 		);
1272 	}
1273 
1274 	/*
1275 	 * Step 1
1276 	 *
1277 	 * Turn object into OBJT_SWAP
1278 	 * check for bogus sysops
1279 	 * force sync if not pageout process
1280 	 */
1281 	if (object->type != OBJT_SWAP)
1282 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
1283 	VM_OBJECT_WUNLOCK(object);
1284 
1285 	n = 0;
1286 	if (curproc != pageproc)
1287 		sync = TRUE;
1288 	else
1289 		sync = (flags & VM_PAGER_PUT_SYNC) != 0;
1290 
1291 	/*
1292 	 * Step 2
1293 	 *
1294 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
1295 	 * The page is left dirty until the pageout operation completes
1296 	 * successfully.
1297 	 */
1298 	for (i = 0; i < count; i += n) {
1299 		int j;
1300 		struct buf *bp;
1301 		daddr_t blk;
1302 
1303 		/*
1304 		 * Maximum I/O size is limited by a number of factors.
1305 		 */
1306 		n = min(BLIST_MAX_ALLOC, count - i);
1307 		n = min(n, nsw_cluster_max);
1308 
1309 		/*
1310 		 * Get biggest block of swap we can.  If we fail, fall
1311 		 * back and try to allocate a smaller block.  Don't go
1312 		 * overboard trying to allocate space if it would overly
1313 		 * fragment swap.
1314 		 */
1315 		while (
1316 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
1317 		    n > 4
1318 		) {
1319 			n >>= 1;
1320 		}
1321 		if (blk == SWAPBLK_NONE) {
1322 			for (j = 0; j < n; ++j)
1323 				rtvals[i+j] = VM_PAGER_FAIL;
1324 			continue;
1325 		}
1326 
1327 		/*
1328 		 * All I/O parameters have been satisfied, build the I/O
1329 		 * request and assign the swap space.
1330 		 */
1331 		if (sync == TRUE) {
1332 			bp = getpbuf(&nsw_wcount_sync);
1333 		} else {
1334 			bp = getpbuf(&nsw_wcount_async);
1335 			bp->b_flags = B_ASYNC;
1336 		}
1337 		bp->b_flags |= B_PAGING;
1338 		bp->b_iocmd = BIO_WRITE;
1339 
1340 		bp->b_rcred = crhold(thread0.td_ucred);
1341 		bp->b_wcred = crhold(thread0.td_ucred);
1342 		bp->b_bcount = PAGE_SIZE * n;
1343 		bp->b_bufsize = PAGE_SIZE * n;
1344 		bp->b_blkno = blk;
1345 
1346 		VM_OBJECT_WLOCK(object);
1347 		for (j = 0; j < n; ++j) {
1348 			vm_page_t mreq = m[i+j];
1349 
1350 			swp_pager_meta_build(
1351 			    mreq->object,
1352 			    mreq->pindex,
1353 			    blk + j
1354 			);
1355 			MPASS(mreq->dirty == VM_PAGE_BITS_ALL);
1356 			mreq->oflags |= VPO_SWAPINPROG;
1357 			bp->b_pages[j] = mreq;
1358 		}
1359 		VM_OBJECT_WUNLOCK(object);
1360 		bp->b_npages = n;
1361 		/*
1362 		 * Must set dirty range for NFS to work.
1363 		 */
1364 		bp->b_dirtyoff = 0;
1365 		bp->b_dirtyend = bp->b_bcount;
1366 
1367 		VM_CNT_INC(v_swapout);
1368 		VM_CNT_ADD(v_swappgsout, bp->b_npages);
1369 
1370 		/*
1371 		 * We unconditionally set rtvals[] to VM_PAGER_PEND so that we
1372 		 * can call the async completion routine at the end of a
1373 		 * synchronous I/O operation.  Otherwise, our caller would
1374 		 * perform duplicate unbusy and wakeup operations on the page
1375 		 * and object, respectively.
1376 		 */
1377 		for (j = 0; j < n; j++)
1378 			rtvals[i + j] = VM_PAGER_PEND;
1379 
1380 		/*
1381 		 * asynchronous
1382 		 *
1383 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
1384 		 */
1385 		if (sync == FALSE) {
1386 			bp->b_iodone = swp_pager_async_iodone;
1387 			BUF_KERNPROC(bp);
1388 			swp_pager_strategy(bp);
1389 			continue;
1390 		}
1391 
1392 		/*
1393 		 * synchronous
1394 		 *
1395 		 * NOTE: b_blkno is destroyed by the call to swapdev_strategy
1396 		 */
1397 		bp->b_iodone = bdone;
1398 		swp_pager_strategy(bp);
1399 
1400 		/*
1401 		 * Wait for the sync I/O to complete.
1402 		 */
1403 		bwait(bp, PVM, "swwrt");
1404 
1405 		/*
1406 		 * Now that we are through with the bp, we can call the
1407 		 * normal async completion, which frees everything up.
1408 		 */
1409 		swp_pager_async_iodone(bp);
1410 	}
1411 	VM_OBJECT_WLOCK(object);
1412 }
1413 
1414 /*
1415  *	swp_pager_async_iodone:
1416  *
1417  *	Completion routine for asynchronous reads and writes from/to swap.
1418  *	Also called manually by synchronous code to finish up a bp.
1419  *
1420  *	This routine may not sleep.
1421  */
1422 static void
1423 swp_pager_async_iodone(struct buf *bp)
1424 {
1425 	int i;
1426 	vm_object_t object = NULL;
1427 
1428 	/*
1429 	 * report error
1430 	 */
1431 	if (bp->b_ioflags & BIO_ERROR) {
1432 		printf(
1433 		    "swap_pager: I/O error - %s failed; blkno %ld,"
1434 			"size %ld, error %d\n",
1435 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
1436 		    (long)bp->b_blkno,
1437 		    (long)bp->b_bcount,
1438 		    bp->b_error
1439 		);
1440 	}
1441 
1442 	/*
1443 	 * remove the mapping for kernel virtual
1444 	 */
1445 	if (buf_mapped(bp))
1446 		pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1447 	else
1448 		bp->b_data = bp->b_kvabase;
1449 
1450 	if (bp->b_npages) {
1451 		object = bp->b_pages[0]->object;
1452 		VM_OBJECT_WLOCK(object);
1453 	}
1454 
1455 	/*
1456 	 * cleanup pages.  If an error occurs writing to swap, we are in
1457 	 * very serious trouble.  If it happens to be a disk error, though,
1458 	 * we may be able to recover by reassigning the swap later on.  So
1459 	 * in this case we remove the m->swapblk assignment for the page
1460 	 * but do not free it in the rlist.  The errornous block(s) are thus
1461 	 * never reallocated as swap.  Redirty the page and continue.
1462 	 */
1463 	for (i = 0; i < bp->b_npages; ++i) {
1464 		vm_page_t m = bp->b_pages[i];
1465 
1466 		m->oflags &= ~VPO_SWAPINPROG;
1467 		if (m->oflags & VPO_SWAPSLEEP) {
1468 			m->oflags &= ~VPO_SWAPSLEEP;
1469 			wakeup(&object->paging_in_progress);
1470 		}
1471 
1472 		if (bp->b_ioflags & BIO_ERROR) {
1473 			/*
1474 			 * If an error occurs I'd love to throw the swapblk
1475 			 * away without freeing it back to swapspace, so it
1476 			 * can never be used again.  But I can't from an
1477 			 * interrupt.
1478 			 */
1479 			if (bp->b_iocmd == BIO_READ) {
1480 				/*
1481 				 * NOTE: for reads, m->dirty will probably
1482 				 * be overridden by the original caller of
1483 				 * getpages so don't play cute tricks here.
1484 				 */
1485 				m->valid = 0;
1486 			} else {
1487 				/*
1488 				 * If a write error occurs, reactivate page
1489 				 * so it doesn't clog the inactive list,
1490 				 * then finish the I/O.
1491 				 */
1492 				vm_page_dirty(m);
1493 				vm_page_lock(m);
1494 				vm_page_activate(m);
1495 				vm_page_unlock(m);
1496 				vm_page_sunbusy(m);
1497 			}
1498 		} else if (bp->b_iocmd == BIO_READ) {
1499 			/*
1500 			 * NOTE: for reads, m->dirty will probably be
1501 			 * overridden by the original caller of getpages so
1502 			 * we cannot set them in order to free the underlying
1503 			 * swap in a low-swap situation.  I don't think we'd
1504 			 * want to do that anyway, but it was an optimization
1505 			 * that existed in the old swapper for a time before
1506 			 * it got ripped out due to precisely this problem.
1507 			 */
1508 			KASSERT(!pmap_page_is_mapped(m),
1509 			    ("swp_pager_async_iodone: page %p is mapped", m));
1510 			KASSERT(m->dirty == 0,
1511 			    ("swp_pager_async_iodone: page %p is dirty", m));
1512 
1513 			m->valid = VM_PAGE_BITS_ALL;
1514 			if (i < bp->b_pgbefore ||
1515 			    i >= bp->b_npages - bp->b_pgafter)
1516 				vm_page_readahead_finish(m);
1517 		} else {
1518 			/*
1519 			 * For write success, clear the dirty
1520 			 * status, then finish the I/O ( which decrements the
1521 			 * busy count and possibly wakes waiter's up ).
1522 			 * A page is only written to swap after a period of
1523 			 * inactivity.  Therefore, we do not expect it to be
1524 			 * reused.
1525 			 */
1526 			KASSERT(!pmap_page_is_write_mapped(m),
1527 			    ("swp_pager_async_iodone: page %p is not write"
1528 			    " protected", m));
1529 			vm_page_undirty(m);
1530 			vm_page_lock(m);
1531 			vm_page_deactivate_noreuse(m);
1532 			vm_page_unlock(m);
1533 			vm_page_sunbusy(m);
1534 		}
1535 	}
1536 
1537 	/*
1538 	 * adjust pip.  NOTE: the original parent may still have its own
1539 	 * pip refs on the object.
1540 	 */
1541 	if (object != NULL) {
1542 		vm_object_pip_wakeupn(object, bp->b_npages);
1543 		VM_OBJECT_WUNLOCK(object);
1544 	}
1545 
1546 	/*
1547 	 * swapdev_strategy() manually sets b_vp and b_bufobj before calling
1548 	 * bstrategy(). Set them back to NULL now we're done with it, or we'll
1549 	 * trigger a KASSERT in relpbuf().
1550 	 */
1551 	if (bp->b_vp) {
1552 		    bp->b_vp = NULL;
1553 		    bp->b_bufobj = NULL;
1554 	}
1555 	/*
1556 	 * release the physical I/O buffer
1557 	 */
1558 	relpbuf(
1559 	    bp,
1560 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1561 		((bp->b_flags & B_ASYNC) ?
1562 		    &nsw_wcount_async :
1563 		    &nsw_wcount_sync
1564 		)
1565 	    )
1566 	);
1567 }
1568 
1569 int
1570 swap_pager_nswapdev(void)
1571 {
1572 
1573 	return (nswapdev);
1574 }
1575 
1576 /*
1577  * SWP_PAGER_FORCE_PAGEIN() - force a swap block to be paged in
1578  *
1579  *	This routine dissociates the page at the given index within an object
1580  *	from its backing store, paging it in if it does not reside in memory.
1581  *	If the page is paged in, it is marked dirty and placed in the laundry
1582  *	queue.  The page is marked dirty because it no longer has backing
1583  *	store.  It is placed in the laundry queue because it has not been
1584  *	accessed recently.  Otherwise, it would already reside in memory.
1585  *
1586  *	We also attempt to swap in all other pages in the swap block.
1587  *	However, we only guarantee that the one at the specified index is
1588  *	paged in.
1589  *
1590  *	XXX - The code to page the whole block in doesn't work, so we
1591  *	      revert to the one-by-one behavior for now.  Sigh.
1592  */
1593 static inline void
1594 swp_pager_force_pagein(vm_object_t object, vm_pindex_t pindex)
1595 {
1596 	vm_page_t m;
1597 
1598 	vm_object_pip_add(object, 1);
1599 	m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL);
1600 	if (m->valid == VM_PAGE_BITS_ALL) {
1601 		vm_object_pip_wakeup(object);
1602 		vm_page_dirty(m);
1603 		vm_page_lock(m);
1604 		vm_page_activate(m);
1605 		vm_page_unlock(m);
1606 		vm_page_xunbusy(m);
1607 		vm_pager_page_unswapped(m);
1608 		return;
1609 	}
1610 
1611 	if (swap_pager_getpages(object, &m, 1, NULL, NULL) != VM_PAGER_OK)
1612 		panic("swap_pager_force_pagein: read from swap failed");/*XXX*/
1613 	vm_object_pip_wakeup(object);
1614 	vm_page_dirty(m);
1615 	vm_page_lock(m);
1616 	vm_page_launder(m);
1617 	vm_page_unlock(m);
1618 	vm_page_xunbusy(m);
1619 	vm_pager_page_unswapped(m);
1620 }
1621 
1622 /*
1623  *	swap_pager_swapoff:
1624  *
1625  *	Page in all of the pages that have been paged out to the
1626  *	given device.  The corresponding blocks in the bitmap must be
1627  *	marked as allocated and the device must be flagged SW_CLOSING.
1628  *	There may be no processes swapped out to the device.
1629  *
1630  *	This routine may block.
1631  */
1632 static void
1633 swap_pager_swapoff(struct swdevt *sp)
1634 {
1635 	struct swblk *sb;
1636 	vm_object_t object;
1637 	vm_pindex_t pi;
1638 	int i, retries;
1639 
1640 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
1641 
1642 	retries = 0;
1643 full_rescan:
1644 	mtx_lock(&vm_object_list_mtx);
1645 	TAILQ_FOREACH(object, &vm_object_list, object_list) {
1646 		if (object->type != OBJT_SWAP)
1647 			continue;
1648 		mtx_unlock(&vm_object_list_mtx);
1649 		/* Depends on type-stability. */
1650 		VM_OBJECT_WLOCK(object);
1651 
1652 		/*
1653 		 * Dead objects are eventually terminated on their own.
1654 		 */
1655 		if ((object->flags & OBJ_DEAD) != 0)
1656 			goto next_obj;
1657 
1658 		/*
1659 		 * Sync with fences placed after pctrie
1660 		 * initialization.  We must not access pctrie below
1661 		 * unless we checked that our object is swap and not
1662 		 * dead.
1663 		 */
1664 		atomic_thread_fence_acq();
1665 		if (object->type != OBJT_SWAP)
1666 			goto next_obj;
1667 
1668 		for (pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
1669 		    &object->un_pager.swp.swp_blks, pi)) != NULL; ) {
1670 			pi = sb->p + SWAP_META_PAGES;
1671 			for (i = 0; i < SWAP_META_PAGES; i++) {
1672 				if (sb->d[i] == SWAPBLK_NONE)
1673 					continue;
1674 				if (swp_pager_isondev(sb->d[i], sp))
1675 					swp_pager_force_pagein(object,
1676 					    sb->p + i);
1677 			}
1678 		}
1679 next_obj:
1680 		VM_OBJECT_WUNLOCK(object);
1681 		mtx_lock(&vm_object_list_mtx);
1682 	}
1683 	mtx_unlock(&vm_object_list_mtx);
1684 
1685 	if (sp->sw_used) {
1686 		/*
1687 		 * Objects may be locked or paging to the device being
1688 		 * removed, so we will miss their pages and need to
1689 		 * make another pass.  We have marked this device as
1690 		 * SW_CLOSING, so the activity should finish soon.
1691 		 */
1692 		retries++;
1693 		if (retries > 100) {
1694 			panic("swapoff: failed to locate %d swap blocks",
1695 			    sp->sw_used);
1696 		}
1697 		pause("swpoff", hz / 20);
1698 		goto full_rescan;
1699 	}
1700 	EVENTHANDLER_INVOKE(swapoff, sp);
1701 }
1702 
1703 /************************************************************************
1704  *				SWAP META DATA 				*
1705  ************************************************************************
1706  *
1707  *	These routines manipulate the swap metadata stored in the
1708  *	OBJT_SWAP object.
1709  *
1710  *	Swap metadata is implemented with a global hash and not directly
1711  *	linked into the object.  Instead the object simply contains
1712  *	appropriate tracking counters.
1713  */
1714 
1715 /*
1716  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
1717  *
1718  *	We first convert the object to a swap object if it is a default
1719  *	object.
1720  *
1721  *	The specified swapblk is added to the object's swap metadata.  If
1722  *	the swapblk is not valid, it is freed instead.  Any previously
1723  *	assigned swapblk is freed.
1724  */
1725 static void
1726 swp_pager_meta_build(vm_object_t object, vm_pindex_t pindex, daddr_t swapblk)
1727 {
1728 	static volatile int swblk_zone_exhausted, swpctrie_zone_exhausted;
1729 	struct swblk *sb;
1730 	vm_pindex_t modpi, rdpi;
1731 	int error, i;
1732 
1733 	VM_OBJECT_ASSERT_WLOCKED(object);
1734 
1735 	/*
1736 	 * Convert default object to swap object if necessary
1737 	 */
1738 	if (object->type != OBJT_SWAP) {
1739 		pctrie_init(&object->un_pager.swp.swp_blks);
1740 
1741 		/*
1742 		 * Ensure that swap_pager_swapoff()'s iteration over
1743 		 * object_list does not see a garbage pctrie.
1744 		 */
1745 		atomic_thread_fence_rel();
1746 
1747 		object->type = OBJT_SWAP;
1748 		KASSERT(object->handle == NULL, ("default pager with handle"));
1749 	}
1750 
1751 	rdpi = rounddown(pindex, SWAP_META_PAGES);
1752 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks, rdpi);
1753 	if (sb == NULL) {
1754 		if (swapblk == SWAPBLK_NONE)
1755 			return;
1756 		for (;;) {
1757 			sb = uma_zalloc(swblk_zone, M_NOWAIT | (curproc ==
1758 			    pageproc ? M_USE_RESERVE : 0));
1759 			if (sb != NULL) {
1760 				sb->p = rdpi;
1761 				for (i = 0; i < SWAP_META_PAGES; i++)
1762 					sb->d[i] = SWAPBLK_NONE;
1763 				if (atomic_cmpset_int(&swblk_zone_exhausted,
1764 				    1, 0))
1765 					printf("swblk zone ok\n");
1766 				break;
1767 			}
1768 			VM_OBJECT_WUNLOCK(object);
1769 			if (uma_zone_exhausted(swblk_zone)) {
1770 				if (atomic_cmpset_int(&swblk_zone_exhausted,
1771 				    0, 1))
1772 					printf("swap blk zone exhausted, "
1773 					    "increase kern.maxswzone\n");
1774 				vm_pageout_oom(VM_OOM_SWAPZ);
1775 				pause("swzonxb", 10);
1776 			} else
1777 				VM_WAIT;
1778 			VM_OBJECT_WLOCK(object);
1779 		}
1780 		for (;;) {
1781 			error = SWAP_PCTRIE_INSERT(
1782 			    &object->un_pager.swp.swp_blks, sb);
1783 			if (error == 0) {
1784 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1785 				    1, 0))
1786 					printf("swpctrie zone ok\n");
1787 				break;
1788 			}
1789 			VM_OBJECT_WUNLOCK(object);
1790 			if (uma_zone_exhausted(swpctrie_zone)) {
1791 				if (atomic_cmpset_int(&swpctrie_zone_exhausted,
1792 				    0, 1))
1793 					printf("swap pctrie zone exhausted, "
1794 					    "increase kern.maxswzone\n");
1795 				vm_pageout_oom(VM_OOM_SWAPZ);
1796 				pause("swzonxp", 10);
1797 			} else
1798 				VM_WAIT;
1799 			VM_OBJECT_WLOCK(object);
1800 		}
1801 	}
1802 	MPASS(sb->p == rdpi);
1803 
1804 	modpi = pindex % SWAP_META_PAGES;
1805 	/* Delete prior contents of metadata. */
1806 	if (sb->d[modpi] != SWAPBLK_NONE)
1807 		swp_pager_freeswapspace(sb->d[modpi], 1);
1808 	/* Enter block into metadata. */
1809 	sb->d[modpi] = swapblk;
1810 }
1811 
1812 /*
1813  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
1814  *
1815  *	The requested range of blocks is freed, with any associated swap
1816  *	returned to the swap bitmap.
1817  *
1818  *	This routine will free swap metadata structures as they are cleaned
1819  *	out.  This routine does *NOT* operate on swap metadata associated
1820  *	with resident pages.
1821  */
1822 static void
1823 swp_pager_meta_free(vm_object_t object, vm_pindex_t pindex, vm_pindex_t count)
1824 {
1825 	struct swblk *sb;
1826 	vm_pindex_t last;
1827 	int i;
1828 	bool empty;
1829 
1830 	VM_OBJECT_ASSERT_LOCKED(object);
1831 	if (object->type != OBJT_SWAP || count == 0)
1832 		return;
1833 
1834 	last = pindex + count - 1;
1835 	for (;;) {
1836 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
1837 		    rounddown(pindex, SWAP_META_PAGES));
1838 		if (sb == NULL || sb->p > last)
1839 			break;
1840 		empty = true;
1841 		for (i = 0; i < SWAP_META_PAGES; i++) {
1842 			if (sb->d[i] == SWAPBLK_NONE)
1843 				continue;
1844 			if (pindex <= sb->p + i && sb->p + i <= last) {
1845 				swp_pager_freeswapspace(sb->d[i], 1);
1846 				sb->d[i] = SWAPBLK_NONE;
1847 			} else
1848 				empty = false;
1849 		}
1850 		pindex = sb->p + SWAP_META_PAGES;
1851 		if (empty) {
1852 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
1853 			    sb->p);
1854 			uma_zfree(swblk_zone, sb);
1855 		}
1856 	}
1857 }
1858 
1859 /*
1860  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
1861  *
1862  *	This routine locates and destroys all swap metadata associated with
1863  *	an object.
1864  */
1865 static void
1866 swp_pager_meta_free_all(vm_object_t object)
1867 {
1868 	struct swblk *sb;
1869 	vm_pindex_t pindex;
1870 	int i;
1871 
1872 	VM_OBJECT_ASSERT_WLOCKED(object);
1873 	if (object->type != OBJT_SWAP)
1874 		return;
1875 
1876 	for (pindex = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
1877 	    &object->un_pager.swp.swp_blks, pindex)) != NULL;) {
1878 		pindex = sb->p + SWAP_META_PAGES;
1879 		for (i = 0; i < SWAP_META_PAGES; i++) {
1880 			if (sb->d[i] != SWAPBLK_NONE)
1881 				swp_pager_freeswapspace(sb->d[i], 1);
1882 		}
1883 		SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks, sb->p);
1884 		uma_zfree(swblk_zone, sb);
1885 	}
1886 }
1887 
1888 /*
1889  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
1890  *
1891  *	This routine is capable of looking up, popping, or freeing
1892  *	swapblk assignments in the swap meta data or in the vm_page_t.
1893  *	The routine typically returns the swapblk being looked-up, or popped,
1894  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
1895  *	was invalid.  This routine will automatically free any invalid
1896  *	meta-data swapblks.
1897  *
1898  *	When acting on a busy resident page and paging is in progress, we
1899  *	have to wait until paging is complete but otherwise can act on the
1900  *	busy page.
1901  *
1902  *	SWM_FREE	remove and free swap block from metadata
1903  *	SWM_POP		remove from meta data but do not free.. pop it out
1904  */
1905 static daddr_t
1906 swp_pager_meta_ctl(vm_object_t object, vm_pindex_t pindex, int flags)
1907 {
1908 	struct swblk *sb;
1909 	daddr_t r1;
1910 	int i;
1911 
1912 	VM_OBJECT_ASSERT_LOCKED(object);
1913 	/*
1914 	 * The meta data only exists of the object is OBJT_SWAP
1915 	 * and even then might not be allocated yet.
1916 	 */
1917 	if (object->type != OBJT_SWAP)
1918 		return (SWAPBLK_NONE);
1919 
1920 	sb = SWAP_PCTRIE_LOOKUP(&object->un_pager.swp.swp_blks,
1921 	    rounddown(pindex, SWAP_META_PAGES));
1922 	if (sb == NULL)
1923 		return (SWAPBLK_NONE);
1924 	r1 = sb->d[pindex % SWAP_META_PAGES];
1925 	if (r1 == SWAPBLK_NONE)
1926 		return (SWAPBLK_NONE);
1927 	if ((flags & (SWM_FREE | SWM_POP)) != 0) {
1928 		sb->d[pindex % SWAP_META_PAGES] = SWAPBLK_NONE;
1929 		for (i = 0; i < SWAP_META_PAGES; i++) {
1930 			if (sb->d[i] != SWAPBLK_NONE)
1931 				break;
1932 		}
1933 		if (i == SWAP_META_PAGES) {
1934 			SWAP_PCTRIE_REMOVE(&object->un_pager.swp.swp_blks,
1935 			    rounddown(pindex, SWAP_META_PAGES));
1936 			uma_zfree(swblk_zone, sb);
1937 		}
1938 	}
1939 	if ((flags & SWM_FREE) != 0) {
1940 		swp_pager_freeswapspace(r1, 1);
1941 		r1 = SWAPBLK_NONE;
1942 	}
1943 	return (r1);
1944 }
1945 
1946 /*
1947  * Returns the least page index which is greater than or equal to the
1948  * parameter pindex and for which there is a swap block allocated.
1949  * Returns object's size if the object's type is not swap or if there
1950  * are no allocated swap blocks for the object after the requested
1951  * pindex.
1952  */
1953 vm_pindex_t
1954 swap_pager_find_least(vm_object_t object, vm_pindex_t pindex)
1955 {
1956 	struct swblk *sb;
1957 	int i;
1958 
1959 	VM_OBJECT_ASSERT_LOCKED(object);
1960 	if (object->type != OBJT_SWAP)
1961 		return (object->size);
1962 
1963 	sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
1964 	    rounddown(pindex, SWAP_META_PAGES));
1965 	if (sb == NULL)
1966 		return (object->size);
1967 	if (sb->p < pindex) {
1968 		for (i = pindex % SWAP_META_PAGES; i < SWAP_META_PAGES; i++) {
1969 			if (sb->d[i] != SWAPBLK_NONE)
1970 				return (sb->p + i);
1971 		}
1972 		sb = SWAP_PCTRIE_LOOKUP_GE(&object->un_pager.swp.swp_blks,
1973 		    roundup(pindex, SWAP_META_PAGES));
1974 		if (sb == NULL)
1975 			return (object->size);
1976 	}
1977 	for (i = 0; i < SWAP_META_PAGES; i++) {
1978 		if (sb->d[i] != SWAPBLK_NONE)
1979 			return (sb->p + i);
1980 	}
1981 
1982 	/*
1983 	 * We get here if a swblk is present in the trie but it
1984 	 * doesn't map any blocks.
1985 	 */
1986 	MPASS(0);
1987 	return (object->size);
1988 }
1989 
1990 /*
1991  * System call swapon(name) enables swapping on device name,
1992  * which must be in the swdevsw.  Return EBUSY
1993  * if already swapping on this device.
1994  */
1995 #ifndef _SYS_SYSPROTO_H_
1996 struct swapon_args {
1997 	char *name;
1998 };
1999 #endif
2000 
2001 /*
2002  * MPSAFE
2003  */
2004 /* ARGSUSED */
2005 int
2006 sys_swapon(struct thread *td, struct swapon_args *uap)
2007 {
2008 	struct vattr attr;
2009 	struct vnode *vp;
2010 	struct nameidata nd;
2011 	int error;
2012 
2013 	error = priv_check(td, PRIV_SWAPON);
2014 	if (error)
2015 		return (error);
2016 
2017 	sx_xlock(&swdev_syscall_lock);
2018 
2019 	/*
2020 	 * Swap metadata may not fit in the KVM if we have physical
2021 	 * memory of >1GB.
2022 	 */
2023 	if (swblk_zone == NULL) {
2024 		error = ENOMEM;
2025 		goto done;
2026 	}
2027 
2028 	NDINIT(&nd, LOOKUP, ISOPEN | FOLLOW | AUDITVNODE1, UIO_USERSPACE,
2029 	    uap->name, td);
2030 	error = namei(&nd);
2031 	if (error)
2032 		goto done;
2033 
2034 	NDFREE(&nd, NDF_ONLY_PNBUF);
2035 	vp = nd.ni_vp;
2036 
2037 	if (vn_isdisk(vp, &error)) {
2038 		error = swapongeom(vp);
2039 	} else if (vp->v_type == VREG &&
2040 	    (vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
2041 	    (error = VOP_GETATTR(vp, &attr, td->td_ucred)) == 0) {
2042 		/*
2043 		 * Allow direct swapping to NFS regular files in the same
2044 		 * way that nfs_mountroot() sets up diskless swapping.
2045 		 */
2046 		error = swaponvp(td, vp, attr.va_size / DEV_BSIZE);
2047 	}
2048 
2049 	if (error)
2050 		vrele(vp);
2051 done:
2052 	sx_xunlock(&swdev_syscall_lock);
2053 	return (error);
2054 }
2055 
2056 /*
2057  * Check that the total amount of swap currently configured does not
2058  * exceed half the theoretical maximum.  If it does, print a warning
2059  * message and return -1; otherwise, return 0.
2060  */
2061 static int
2062 swapon_check_swzone(unsigned long npages)
2063 {
2064 	unsigned long maxpages;
2065 
2066 	/* absolute maximum we can handle assuming 100% efficiency */
2067 	maxpages = uma_zone_get_max(swblk_zone) * SWAP_META_PAGES;
2068 
2069 	/* recommend using no more than half that amount */
2070 	if (npages > maxpages / 2) {
2071 		printf("warning: total configured swap (%lu pages) "
2072 		    "exceeds maximum recommended amount (%lu pages).\n",
2073 		    npages, maxpages / 2);
2074 		printf("warning: increase kern.maxswzone "
2075 		    "or reduce amount of swap.\n");
2076 		return (-1);
2077 	}
2078 	return (0);
2079 }
2080 
2081 static void
2082 swaponsomething(struct vnode *vp, void *id, u_long nblks,
2083     sw_strategy_t *strategy, sw_close_t *close, dev_t dev, int flags)
2084 {
2085 	struct swdevt *sp, *tsp;
2086 	swblk_t dvbase;
2087 	u_long mblocks;
2088 
2089 	/*
2090 	 * nblks is in DEV_BSIZE'd chunks, convert to PAGE_SIZE'd chunks.
2091 	 * First chop nblks off to page-align it, then convert.
2092 	 *
2093 	 * sw->sw_nblks is in page-sized chunks now too.
2094 	 */
2095 	nblks &= ~(ctodb(1) - 1);
2096 	nblks = dbtoc(nblks);
2097 
2098 	/*
2099 	 * If we go beyond this, we get overflows in the radix
2100 	 * tree bitmap code.
2101 	 */
2102 	mblocks = 0x40000000 / BLIST_META_RADIX;
2103 	if (nblks > mblocks) {
2104 		printf(
2105     "WARNING: reducing swap size to maximum of %luMB per unit\n",
2106 		    mblocks / 1024 / 1024 * PAGE_SIZE);
2107 		nblks = mblocks;
2108 	}
2109 
2110 	sp = malloc(sizeof *sp, M_VMPGDATA, M_WAITOK | M_ZERO);
2111 	sp->sw_vp = vp;
2112 	sp->sw_id = id;
2113 	sp->sw_dev = dev;
2114 	sp->sw_flags = 0;
2115 	sp->sw_nblks = nblks;
2116 	sp->sw_used = 0;
2117 	sp->sw_strategy = strategy;
2118 	sp->sw_close = close;
2119 	sp->sw_flags = flags;
2120 
2121 	sp->sw_blist = blist_create(nblks, M_WAITOK);
2122 	/*
2123 	 * Do not free the first two block in order to avoid overwriting
2124 	 * any bsd label at the front of the partition
2125 	 */
2126 	blist_free(sp->sw_blist, 2, nblks - 2);
2127 
2128 	dvbase = 0;
2129 	mtx_lock(&sw_dev_mtx);
2130 	TAILQ_FOREACH(tsp, &swtailq, sw_list) {
2131 		if (tsp->sw_end >= dvbase) {
2132 			/*
2133 			 * We put one uncovered page between the devices
2134 			 * in order to definitively prevent any cross-device
2135 			 * I/O requests
2136 			 */
2137 			dvbase = tsp->sw_end + 1;
2138 		}
2139 	}
2140 	sp->sw_first = dvbase;
2141 	sp->sw_end = dvbase + nblks;
2142 	TAILQ_INSERT_TAIL(&swtailq, sp, sw_list);
2143 	nswapdev++;
2144 	swap_pager_avail += nblks - 2;
2145 	swap_total += (vm_ooffset_t)nblks * PAGE_SIZE;
2146 	swapon_check_swzone(swap_total / PAGE_SIZE);
2147 	swp_sizecheck();
2148 	mtx_unlock(&sw_dev_mtx);
2149 	EVENTHANDLER_INVOKE(swapon, sp);
2150 }
2151 
2152 /*
2153  * SYSCALL: swapoff(devname)
2154  *
2155  * Disable swapping on the given device.
2156  *
2157  * XXX: Badly designed system call: it should use a device index
2158  * rather than filename as specification.  We keep sw_vp around
2159  * only to make this work.
2160  */
2161 #ifndef _SYS_SYSPROTO_H_
2162 struct swapoff_args {
2163 	char *name;
2164 };
2165 #endif
2166 
2167 /*
2168  * MPSAFE
2169  */
2170 /* ARGSUSED */
2171 int
2172 sys_swapoff(struct thread *td, struct swapoff_args *uap)
2173 {
2174 	struct vnode *vp;
2175 	struct nameidata nd;
2176 	struct swdevt *sp;
2177 	int error;
2178 
2179 	error = priv_check(td, PRIV_SWAPOFF);
2180 	if (error)
2181 		return (error);
2182 
2183 	sx_xlock(&swdev_syscall_lock);
2184 
2185 	NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, UIO_USERSPACE, uap->name,
2186 	    td);
2187 	error = namei(&nd);
2188 	if (error)
2189 		goto done;
2190 	NDFREE(&nd, NDF_ONLY_PNBUF);
2191 	vp = nd.ni_vp;
2192 
2193 	mtx_lock(&sw_dev_mtx);
2194 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2195 		if (sp->sw_vp == vp)
2196 			break;
2197 	}
2198 	mtx_unlock(&sw_dev_mtx);
2199 	if (sp == NULL) {
2200 		error = EINVAL;
2201 		goto done;
2202 	}
2203 	error = swapoff_one(sp, td->td_ucred);
2204 done:
2205 	sx_xunlock(&swdev_syscall_lock);
2206 	return (error);
2207 }
2208 
2209 static int
2210 swapoff_one(struct swdevt *sp, struct ucred *cred)
2211 {
2212 	u_long nblks;
2213 #ifdef MAC
2214 	int error;
2215 #endif
2216 
2217 	sx_assert(&swdev_syscall_lock, SA_XLOCKED);
2218 #ifdef MAC
2219 	(void) vn_lock(sp->sw_vp, LK_EXCLUSIVE | LK_RETRY);
2220 	error = mac_system_check_swapoff(cred, sp->sw_vp);
2221 	(void) VOP_UNLOCK(sp->sw_vp, 0);
2222 	if (error != 0)
2223 		return (error);
2224 #endif
2225 	nblks = sp->sw_nblks;
2226 
2227 	/*
2228 	 * We can turn off this swap device safely only if the
2229 	 * available virtual memory in the system will fit the amount
2230 	 * of data we will have to page back in, plus an epsilon so
2231 	 * the system doesn't become critically low on swap space.
2232 	 */
2233 	if (vm_cnt.v_free_count + swap_pager_avail < nblks + nswap_lowat)
2234 		return (ENOMEM);
2235 
2236 	/*
2237 	 * Prevent further allocations on this device.
2238 	 */
2239 	mtx_lock(&sw_dev_mtx);
2240 	sp->sw_flags |= SW_CLOSING;
2241 	swap_pager_avail -= blist_fill(sp->sw_blist, 0, nblks);
2242 	swap_total -= (vm_ooffset_t)nblks * PAGE_SIZE;
2243 	mtx_unlock(&sw_dev_mtx);
2244 
2245 	/*
2246 	 * Page in the contents of the device and close it.
2247 	 */
2248 	swap_pager_swapoff(sp);
2249 
2250 	sp->sw_close(curthread, sp);
2251 	mtx_lock(&sw_dev_mtx);
2252 	sp->sw_id = NULL;
2253 	TAILQ_REMOVE(&swtailq, sp, sw_list);
2254 	nswapdev--;
2255 	if (nswapdev == 0) {
2256 		swap_pager_full = 2;
2257 		swap_pager_almost_full = 1;
2258 	}
2259 	if (swdevhd == sp)
2260 		swdevhd = NULL;
2261 	mtx_unlock(&sw_dev_mtx);
2262 	blist_destroy(sp->sw_blist);
2263 	free(sp, M_VMPGDATA);
2264 	return (0);
2265 }
2266 
2267 void
2268 swapoff_all(void)
2269 {
2270 	struct swdevt *sp, *spt;
2271 	const char *devname;
2272 	int error;
2273 
2274 	sx_xlock(&swdev_syscall_lock);
2275 
2276 	mtx_lock(&sw_dev_mtx);
2277 	TAILQ_FOREACH_SAFE(sp, &swtailq, sw_list, spt) {
2278 		mtx_unlock(&sw_dev_mtx);
2279 		if (vn_isdisk(sp->sw_vp, NULL))
2280 			devname = devtoname(sp->sw_vp->v_rdev);
2281 		else
2282 			devname = "[file]";
2283 		error = swapoff_one(sp, thread0.td_ucred);
2284 		if (error != 0) {
2285 			printf("Cannot remove swap device %s (error=%d), "
2286 			    "skipping.\n", devname, error);
2287 		} else if (bootverbose) {
2288 			printf("Swap device %s removed.\n", devname);
2289 		}
2290 		mtx_lock(&sw_dev_mtx);
2291 	}
2292 	mtx_unlock(&sw_dev_mtx);
2293 
2294 	sx_xunlock(&swdev_syscall_lock);
2295 }
2296 
2297 void
2298 swap_pager_status(int *total, int *used)
2299 {
2300 	struct swdevt *sp;
2301 
2302 	*total = 0;
2303 	*used = 0;
2304 	mtx_lock(&sw_dev_mtx);
2305 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2306 		*total += sp->sw_nblks;
2307 		*used += sp->sw_used;
2308 	}
2309 	mtx_unlock(&sw_dev_mtx);
2310 }
2311 
2312 int
2313 swap_dev_info(int name, struct xswdev *xs, char *devname, size_t len)
2314 {
2315 	struct swdevt *sp;
2316 	const char *tmp_devname;
2317 	int error, n;
2318 
2319 	n = 0;
2320 	error = ENOENT;
2321 	mtx_lock(&sw_dev_mtx);
2322 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2323 		if (n != name) {
2324 			n++;
2325 			continue;
2326 		}
2327 		xs->xsw_version = XSWDEV_VERSION;
2328 		xs->xsw_dev = sp->sw_dev;
2329 		xs->xsw_flags = sp->sw_flags;
2330 		xs->xsw_nblks = sp->sw_nblks;
2331 		xs->xsw_used = sp->sw_used;
2332 		if (devname != NULL) {
2333 			if (vn_isdisk(sp->sw_vp, NULL))
2334 				tmp_devname = devtoname(sp->sw_vp->v_rdev);
2335 			else
2336 				tmp_devname = "[file]";
2337 			strncpy(devname, tmp_devname, len);
2338 		}
2339 		error = 0;
2340 		break;
2341 	}
2342 	mtx_unlock(&sw_dev_mtx);
2343 	return (error);
2344 }
2345 
2346 #if defined(COMPAT_FREEBSD11)
2347 #define XSWDEV_VERSION_11	1
2348 struct xswdev11 {
2349 	u_int	xsw_version;
2350 	uint32_t xsw_dev;
2351 	int	xsw_flags;
2352 	int	xsw_nblks;
2353 	int     xsw_used;
2354 };
2355 #endif
2356 
2357 static int
2358 sysctl_vm_swap_info(SYSCTL_HANDLER_ARGS)
2359 {
2360 	struct xswdev xs;
2361 #if defined(COMPAT_FREEBSD11)
2362 	struct xswdev11 xs11;
2363 #endif
2364 	int error;
2365 
2366 	if (arg2 != 1)			/* name length */
2367 		return (EINVAL);
2368 	error = swap_dev_info(*(int *)arg1, &xs, NULL, 0);
2369 	if (error != 0)
2370 		return (error);
2371 #if defined(COMPAT_FREEBSD11)
2372 	if (req->oldlen == sizeof(xs11)) {
2373 		xs11.xsw_version = XSWDEV_VERSION_11;
2374 		xs11.xsw_dev = xs.xsw_dev; /* truncation */
2375 		xs11.xsw_flags = xs.xsw_flags;
2376 		xs11.xsw_nblks = xs.xsw_nblks;
2377 		xs11.xsw_used = xs.xsw_used;
2378 		error = SYSCTL_OUT(req, &xs11, sizeof(xs11));
2379 	} else
2380 #endif
2381 		error = SYSCTL_OUT(req, &xs, sizeof(xs));
2382 	return (error);
2383 }
2384 
2385 SYSCTL_INT(_vm, OID_AUTO, nswapdev, CTLFLAG_RD, &nswapdev, 0,
2386     "Number of swap devices");
2387 SYSCTL_NODE(_vm, OID_AUTO, swap_info, CTLFLAG_RD | CTLFLAG_MPSAFE,
2388     sysctl_vm_swap_info,
2389     "Swap statistics by device");
2390 
2391 /*
2392  * Count the approximate swap usage in pages for a vmspace.  The
2393  * shadowed or not yet copied on write swap blocks are not accounted.
2394  * The map must be locked.
2395  */
2396 long
2397 vmspace_swap_count(struct vmspace *vmspace)
2398 {
2399 	vm_map_t map;
2400 	vm_map_entry_t cur;
2401 	vm_object_t object;
2402 	struct swblk *sb;
2403 	vm_pindex_t e, pi;
2404 	long count;
2405 	int i;
2406 
2407 	map = &vmspace->vm_map;
2408 	count = 0;
2409 
2410 	for (cur = map->header.next; cur != &map->header; cur = cur->next) {
2411 		if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
2412 			continue;
2413 		object = cur->object.vm_object;
2414 		if (object == NULL || object->type != OBJT_SWAP)
2415 			continue;
2416 		VM_OBJECT_RLOCK(object);
2417 		if (object->type != OBJT_SWAP)
2418 			goto unlock;
2419 		pi = OFF_TO_IDX(cur->offset);
2420 		e = pi + OFF_TO_IDX(cur->end - cur->start);
2421 		for (;; pi = sb->p + SWAP_META_PAGES) {
2422 			sb = SWAP_PCTRIE_LOOKUP_GE(
2423 			    &object->un_pager.swp.swp_blks, pi);
2424 			if (sb == NULL || sb->p >= e)
2425 				break;
2426 			for (i = 0; i < SWAP_META_PAGES; i++) {
2427 				if (sb->p + i < e &&
2428 				    sb->d[i] != SWAPBLK_NONE)
2429 					count++;
2430 			}
2431 		}
2432 unlock:
2433 		VM_OBJECT_RUNLOCK(object);
2434 	}
2435 	return (count);
2436 }
2437 
2438 /*
2439  * GEOM backend
2440  *
2441  * Swapping onto disk devices.
2442  *
2443  */
2444 
2445 static g_orphan_t swapgeom_orphan;
2446 
2447 static struct g_class g_swap_class = {
2448 	.name = "SWAP",
2449 	.version = G_VERSION,
2450 	.orphan = swapgeom_orphan,
2451 };
2452 
2453 DECLARE_GEOM_CLASS(g_swap_class, g_class);
2454 
2455 
2456 static void
2457 swapgeom_close_ev(void *arg, int flags)
2458 {
2459 	struct g_consumer *cp;
2460 
2461 	cp = arg;
2462 	g_access(cp, -1, -1, 0);
2463 	g_detach(cp);
2464 	g_destroy_consumer(cp);
2465 }
2466 
2467 /*
2468  * Add a reference to the g_consumer for an inflight transaction.
2469  */
2470 static void
2471 swapgeom_acquire(struct g_consumer *cp)
2472 {
2473 
2474 	mtx_assert(&sw_dev_mtx, MA_OWNED);
2475 	cp->index++;
2476 }
2477 
2478 /*
2479  * Remove a reference from the g_consumer.  Post a close event if all
2480  * references go away, since the function might be called from the
2481  * biodone context.
2482  */
2483 static void
2484 swapgeom_release(struct g_consumer *cp, struct swdevt *sp)
2485 {
2486 
2487 	mtx_assert(&sw_dev_mtx, MA_OWNED);
2488 	cp->index--;
2489 	if (cp->index == 0) {
2490 		if (g_post_event(swapgeom_close_ev, cp, M_NOWAIT, NULL) == 0)
2491 			sp->sw_id = NULL;
2492 	}
2493 }
2494 
2495 static void
2496 swapgeom_done(struct bio *bp2)
2497 {
2498 	struct swdevt *sp;
2499 	struct buf *bp;
2500 	struct g_consumer *cp;
2501 
2502 	bp = bp2->bio_caller2;
2503 	cp = bp2->bio_from;
2504 	bp->b_ioflags = bp2->bio_flags;
2505 	if (bp2->bio_error)
2506 		bp->b_ioflags |= BIO_ERROR;
2507 	bp->b_resid = bp->b_bcount - bp2->bio_completed;
2508 	bp->b_error = bp2->bio_error;
2509 	bufdone(bp);
2510 	sp = bp2->bio_caller1;
2511 	mtx_lock(&sw_dev_mtx);
2512 	swapgeom_release(cp, sp);
2513 	mtx_unlock(&sw_dev_mtx);
2514 	g_destroy_bio(bp2);
2515 }
2516 
2517 static void
2518 swapgeom_strategy(struct buf *bp, struct swdevt *sp)
2519 {
2520 	struct bio *bio;
2521 	struct g_consumer *cp;
2522 
2523 	mtx_lock(&sw_dev_mtx);
2524 	cp = sp->sw_id;
2525 	if (cp == NULL) {
2526 		mtx_unlock(&sw_dev_mtx);
2527 		bp->b_error = ENXIO;
2528 		bp->b_ioflags |= BIO_ERROR;
2529 		bufdone(bp);
2530 		return;
2531 	}
2532 	swapgeom_acquire(cp);
2533 	mtx_unlock(&sw_dev_mtx);
2534 	if (bp->b_iocmd == BIO_WRITE)
2535 		bio = g_new_bio();
2536 	else
2537 		bio = g_alloc_bio();
2538 	if (bio == NULL) {
2539 		mtx_lock(&sw_dev_mtx);
2540 		swapgeom_release(cp, sp);
2541 		mtx_unlock(&sw_dev_mtx);
2542 		bp->b_error = ENOMEM;
2543 		bp->b_ioflags |= BIO_ERROR;
2544 		bufdone(bp);
2545 		return;
2546 	}
2547 
2548 	bio->bio_caller1 = sp;
2549 	bio->bio_caller2 = bp;
2550 	bio->bio_cmd = bp->b_iocmd;
2551 	bio->bio_offset = (bp->b_blkno - sp->sw_first) * PAGE_SIZE;
2552 	bio->bio_length = bp->b_bcount;
2553 	bio->bio_done = swapgeom_done;
2554 	if (!buf_mapped(bp)) {
2555 		bio->bio_ma = bp->b_pages;
2556 		bio->bio_data = unmapped_buf;
2557 		bio->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
2558 		bio->bio_ma_n = bp->b_npages;
2559 		bio->bio_flags |= BIO_UNMAPPED;
2560 	} else {
2561 		bio->bio_data = bp->b_data;
2562 		bio->bio_ma = NULL;
2563 	}
2564 	g_io_request(bio, cp);
2565 	return;
2566 }
2567 
2568 static void
2569 swapgeom_orphan(struct g_consumer *cp)
2570 {
2571 	struct swdevt *sp;
2572 	int destroy;
2573 
2574 	mtx_lock(&sw_dev_mtx);
2575 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2576 		if (sp->sw_id == cp) {
2577 			sp->sw_flags |= SW_CLOSING;
2578 			break;
2579 		}
2580 	}
2581 	/*
2582 	 * Drop reference we were created with. Do directly since we're in a
2583 	 * special context where we don't have to queue the call to
2584 	 * swapgeom_close_ev().
2585 	 */
2586 	cp->index--;
2587 	destroy = ((sp != NULL) && (cp->index == 0));
2588 	if (destroy)
2589 		sp->sw_id = NULL;
2590 	mtx_unlock(&sw_dev_mtx);
2591 	if (destroy)
2592 		swapgeom_close_ev(cp, 0);
2593 }
2594 
2595 static void
2596 swapgeom_close(struct thread *td, struct swdevt *sw)
2597 {
2598 	struct g_consumer *cp;
2599 
2600 	mtx_lock(&sw_dev_mtx);
2601 	cp = sw->sw_id;
2602 	sw->sw_id = NULL;
2603 	mtx_unlock(&sw_dev_mtx);
2604 
2605 	/*
2606 	 * swapgeom_close() may be called from the biodone context,
2607 	 * where we cannot perform topology changes.  Delegate the
2608 	 * work to the events thread.
2609 	 */
2610 	if (cp != NULL)
2611 		g_waitfor_event(swapgeom_close_ev, cp, M_WAITOK, NULL);
2612 }
2613 
2614 static int
2615 swapongeom_locked(struct cdev *dev, struct vnode *vp)
2616 {
2617 	struct g_provider *pp;
2618 	struct g_consumer *cp;
2619 	static struct g_geom *gp;
2620 	struct swdevt *sp;
2621 	u_long nblks;
2622 	int error;
2623 
2624 	pp = g_dev_getprovider(dev);
2625 	if (pp == NULL)
2626 		return (ENODEV);
2627 	mtx_lock(&sw_dev_mtx);
2628 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2629 		cp = sp->sw_id;
2630 		if (cp != NULL && cp->provider == pp) {
2631 			mtx_unlock(&sw_dev_mtx);
2632 			return (EBUSY);
2633 		}
2634 	}
2635 	mtx_unlock(&sw_dev_mtx);
2636 	if (gp == NULL)
2637 		gp = g_new_geomf(&g_swap_class, "swap");
2638 	cp = g_new_consumer(gp);
2639 	cp->index = 1;	/* Number of active I/Os, plus one for being active. */
2640 	cp->flags |=  G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2641 	g_attach(cp, pp);
2642 	/*
2643 	 * XXX: Every time you think you can improve the margin for
2644 	 * footshooting, somebody depends on the ability to do so:
2645 	 * savecore(8) wants to write to our swapdev so we cannot
2646 	 * set an exclusive count :-(
2647 	 */
2648 	error = g_access(cp, 1, 1, 0);
2649 	if (error != 0) {
2650 		g_detach(cp);
2651 		g_destroy_consumer(cp);
2652 		return (error);
2653 	}
2654 	nblks = pp->mediasize / DEV_BSIZE;
2655 	swaponsomething(vp, cp, nblks, swapgeom_strategy,
2656 	    swapgeom_close, dev2udev(dev),
2657 	    (pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ? SW_UNMAPPED : 0);
2658 	return (0);
2659 }
2660 
2661 static int
2662 swapongeom(struct vnode *vp)
2663 {
2664 	int error;
2665 
2666 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2667 	if (vp->v_type != VCHR || (vp->v_iflag & VI_DOOMED) != 0) {
2668 		error = ENOENT;
2669 	} else {
2670 		g_topology_lock();
2671 		error = swapongeom_locked(vp->v_rdev, vp);
2672 		g_topology_unlock();
2673 	}
2674 	VOP_UNLOCK(vp, 0);
2675 	return (error);
2676 }
2677 
2678 /*
2679  * VNODE backend
2680  *
2681  * This is used mainly for network filesystem (read: probably only tested
2682  * with NFS) swapfiles.
2683  *
2684  */
2685 
2686 static void
2687 swapdev_strategy(struct buf *bp, struct swdevt *sp)
2688 {
2689 	struct vnode *vp2;
2690 
2691 	bp->b_blkno = ctodb(bp->b_blkno - sp->sw_first);
2692 
2693 	vp2 = sp->sw_id;
2694 	vhold(vp2);
2695 	if (bp->b_iocmd == BIO_WRITE) {
2696 		if (bp->b_bufobj)
2697 			bufobj_wdrop(bp->b_bufobj);
2698 		bufobj_wref(&vp2->v_bufobj);
2699 	}
2700 	if (bp->b_bufobj != &vp2->v_bufobj)
2701 		bp->b_bufobj = &vp2->v_bufobj;
2702 	bp->b_vp = vp2;
2703 	bp->b_iooffset = dbtob(bp->b_blkno);
2704 	bstrategy(bp);
2705 	return;
2706 }
2707 
2708 static void
2709 swapdev_close(struct thread *td, struct swdevt *sp)
2710 {
2711 
2712 	VOP_CLOSE(sp->sw_vp, FREAD | FWRITE, td->td_ucred, td);
2713 	vrele(sp->sw_vp);
2714 }
2715 
2716 
2717 static int
2718 swaponvp(struct thread *td, struct vnode *vp, u_long nblks)
2719 {
2720 	struct swdevt *sp;
2721 	int error;
2722 
2723 	if (nblks == 0)
2724 		return (ENXIO);
2725 	mtx_lock(&sw_dev_mtx);
2726 	TAILQ_FOREACH(sp, &swtailq, sw_list) {
2727 		if (sp->sw_id == vp) {
2728 			mtx_unlock(&sw_dev_mtx);
2729 			return (EBUSY);
2730 		}
2731 	}
2732 	mtx_unlock(&sw_dev_mtx);
2733 
2734 	(void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2735 #ifdef MAC
2736 	error = mac_system_check_swapon(td->td_ucred, vp);
2737 	if (error == 0)
2738 #endif
2739 		error = VOP_OPEN(vp, FREAD | FWRITE, td->td_ucred, td, NULL);
2740 	(void) VOP_UNLOCK(vp, 0);
2741 	if (error)
2742 		return (error);
2743 
2744 	swaponsomething(vp, vp, nblks, swapdev_strategy, swapdev_close,
2745 	    NODEV, 0);
2746 	return (0);
2747 }
2748 
2749 static int
2750 sysctl_swap_async_max(SYSCTL_HANDLER_ARGS)
2751 {
2752 	int error, new, n;
2753 
2754 	new = nsw_wcount_async_max;
2755 	error = sysctl_handle_int(oidp, &new, 0, req);
2756 	if (error != 0 || req->newptr == NULL)
2757 		return (error);
2758 
2759 	if (new > nswbuf / 2 || new < 1)
2760 		return (EINVAL);
2761 
2762 	mtx_lock(&pbuf_mtx);
2763 	while (nsw_wcount_async_max != new) {
2764 		/*
2765 		 * Adjust difference.  If the current async count is too low,
2766 		 * we will need to sqeeze our update slowly in.  Sleep with a
2767 		 * higher priority than getpbuf() to finish faster.
2768 		 */
2769 		n = new - nsw_wcount_async_max;
2770 		if (nsw_wcount_async + n >= 0) {
2771 			nsw_wcount_async += n;
2772 			nsw_wcount_async_max += n;
2773 			wakeup(&nsw_wcount_async);
2774 		} else {
2775 			nsw_wcount_async_max -= nsw_wcount_async;
2776 			nsw_wcount_async = 0;
2777 			msleep(&nsw_wcount_async, &pbuf_mtx, PSWP,
2778 			    "swpsysctl", 0);
2779 		}
2780 	}
2781 	mtx_unlock(&pbuf_mtx);
2782 
2783 	return (0);
2784 }
2785