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