xref: /freebsd/sys/vm/swap_pager.c (revision 1a2cdef4962b47be5057809ce730a733b7f3c27c)
1 /*
2  * Copyright (c) 1998 Matthew Dillon,
3  * Copyright (c) 1994 John S. Dyson
4  * Copyright (c) 1990 University of Utah.
5  * Copyright (c) 1991, 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  *
67  * $FreeBSD$
68  */
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/conf.h>
73 #include <sys/kernel.h>
74 #include <sys/proc.h>
75 #include <sys/bio.h>
76 #include <sys/buf.h>
77 #include <sys/vnode.h>
78 #include <sys/malloc.h>
79 #include <sys/vmmeter.h>
80 #include <sys/sysctl.h>
81 #include <sys/blist.h>
82 #include <sys/lock.h>
83 #include <sys/vmmeter.h>
84 
85 #ifndef MAX_PAGEOUT_CLUSTER
86 #define MAX_PAGEOUT_CLUSTER 16
87 #endif
88 
89 #define SWB_NPAGES	MAX_PAGEOUT_CLUSTER
90 
91 #include "opt_swap.h"
92 #include <vm/vm.h>
93 #include <vm/pmap.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_kern.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pager.h>
99 #include <vm/vm_pageout.h>
100 #include <vm/vm_zone.h>
101 #include <vm/swap_pager.h>
102 #include <vm/vm_extern.h>
103 
104 #define SWM_FREE	0x02	/* free, period			*/
105 #define SWM_POP		0x04	/* pop out			*/
106 
107 /*
108  * vm_swap_size is in page-sized chunks now.  It was DEV_BSIZE'd chunks
109  * in the old system.
110  */
111 
112 extern int vm_swap_size;	/* number of free swap blocks, in pages */
113 
114 int swap_pager_full;		/* swap space exhaustion (task killing) */
115 static int swap_pager_almost_full; /* swap space exhaustion (w/ hysteresis)*/
116 static int nsw_rcount;		/* free read buffers			*/
117 static int nsw_wcount_sync;	/* limit write buffers / synchronous	*/
118 static int nsw_wcount_async;	/* limit write buffers / asynchronous	*/
119 static int nsw_wcount_async_max;/* assigned maximum			*/
120 static int nsw_cluster_max;	/* maximum VOP I/O allowed		*/
121 static int sw_alloc_interlock;	/* swap pager allocation interlock	*/
122 
123 struct blist *swapblist;
124 static struct swblock **swhash;
125 static int swhash_mask;
126 static int swap_async_max = 4;	/* maximum in-progress async I/O's	*/
127 
128 /* from vm_swap.c */
129 extern struct vnode *swapdev_vp;
130 extern struct swdevt *swdevt;
131 extern int nswdev;
132 
133 SYSCTL_INT(_vm, OID_AUTO, swap_async_max,
134         CTLFLAG_RW, &swap_async_max, 0, "Maximum running async swap ops");
135 
136 #define BLK2DEVIDX(blk) (nswdev > 1 ? blk / dmmax % nswdev : 0)
137 
138 /*
139  * "named" and "unnamed" anon region objects.  Try to reduce the overhead
140  * of searching a named list by hashing it just a little.
141  */
142 
143 #define NOBJLISTS		8
144 
145 #define NOBJLIST(handle)	\
146 	(&swap_pager_object_list[((int)(intptr_t)handle >> 4) & (NOBJLISTS-1)])
147 
148 static struct pagerlst	swap_pager_object_list[NOBJLISTS];
149 struct pagerlst		swap_pager_un_object_list;
150 vm_zone_t		swap_zone;
151 
152 /*
153  * pagerops for OBJT_SWAP - "swap pager".  Some ops are also global procedure
154  * calls hooked from other parts of the VM system and do not appear here.
155  * (see vm/swap_pager.h).
156  */
157 
158 static vm_object_t
159 		swap_pager_alloc __P((void *handle, vm_ooffset_t size,
160 				      vm_prot_t prot, vm_ooffset_t offset));
161 static void	swap_pager_dealloc __P((vm_object_t object));
162 static int	swap_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
163 static void	swap_pager_init __P((void));
164 static void	swap_pager_unswapped __P((vm_page_t));
165 static void	swap_pager_strategy __P((vm_object_t, struct bio *));
166 
167 struct pagerops swappagerops = {
168 	swap_pager_init,	/* early system initialization of pager	*/
169 	swap_pager_alloc,	/* allocate an OBJT_SWAP object		*/
170 	swap_pager_dealloc,	/* deallocate an OBJT_SWAP object	*/
171 	swap_pager_getpages,	/* pagein				*/
172 	swap_pager_putpages,	/* pageout				*/
173 	swap_pager_haspage,	/* get backing store status for page	*/
174 	swap_pager_unswapped,	/* remove swap related to page		*/
175 	swap_pager_strategy	/* pager strategy call			*/
176 };
177 
178 static struct buf *getchainbuf(struct bio *bp, struct vnode *vp, int flags);
179 static void flushchainbuf(struct buf *nbp);
180 static void waitchainbuf(struct bio *bp, int count, int done);
181 
182 /*
183  * dmmax is in page-sized chunks with the new swap system.  It was
184  * dev-bsized chunks in the old.  dmmax is always a power of 2.
185  *
186  * swap_*() routines are externally accessible.  swp_*() routines are
187  * internal.
188  */
189 
190 int dmmax;
191 static int dmmax_mask;
192 int nswap_lowat = 128;		/* in pages, swap_pager_almost_full warn */
193 int nswap_hiwat = 512;		/* in pages, swap_pager_almost_full warn */
194 
195 SYSCTL_INT(_vm, OID_AUTO, dmmax,
196 	CTLFLAG_RD, &dmmax, 0, "Maximum size of a swap block");
197 
198 static __inline void	swp_sizecheck __P((void));
199 static void	swp_pager_sync_iodone __P((struct buf *bp));
200 static void	swp_pager_async_iodone __P((struct buf *bp));
201 
202 /*
203  * Swap bitmap functions
204  */
205 
206 static __inline void	swp_pager_freeswapspace __P((daddr_t blk, int npages));
207 static __inline daddr_t	swp_pager_getswapspace __P((int npages));
208 
209 /*
210  * Metadata functions
211  */
212 
213 static void swp_pager_meta_build __P((vm_object_t, vm_pindex_t, daddr_t));
214 static void swp_pager_meta_free __P((vm_object_t, vm_pindex_t, daddr_t));
215 static void swp_pager_meta_free_all __P((vm_object_t));
216 static daddr_t swp_pager_meta_ctl __P((vm_object_t, vm_pindex_t, int));
217 
218 /*
219  * SWP_SIZECHECK() -	update swap_pager_full indication
220  *
221  *	update the swap_pager_almost_full indication and warn when we are
222  *	about to run out of swap space, using lowat/hiwat hysteresis.
223  *
224  *	Clear swap_pager_full ( task killing ) indication when lowat is met.
225  *
226  *	No restrictions on call
227  *	This routine may not block.
228  *	This routine must be called at splvm()
229  */
230 
231 static __inline void
232 swp_sizecheck()
233 {
234 	if (vm_swap_size < nswap_lowat) {
235 		if (swap_pager_almost_full == 0) {
236 			printf("swap_pager: out of swap space\n");
237 			swap_pager_almost_full = 1;
238 		}
239 	} else {
240 		swap_pager_full = 0;
241 		if (vm_swap_size > nswap_hiwat)
242 			swap_pager_almost_full = 0;
243 	}
244 }
245 
246 /*
247  * SWAP_PAGER_INIT() -	initialize the swap pager!
248  *
249  *	Expected to be started from system init.  NOTE:  This code is run
250  *	before much else so be careful what you depend on.  Most of the VM
251  *	system has yet to be initialized at this point.
252  */
253 
254 static void
255 swap_pager_init()
256 {
257 	/*
258 	 * Initialize object lists
259 	 */
260 	int i;
261 
262 	for (i = 0; i < NOBJLISTS; ++i)
263 		TAILQ_INIT(&swap_pager_object_list[i]);
264 	TAILQ_INIT(&swap_pager_un_object_list);
265 
266 	/*
267 	 * Device Stripe, in PAGE_SIZE'd blocks
268 	 */
269 
270 	dmmax = SWB_NPAGES * 2;
271 	dmmax_mask = ~(dmmax - 1);
272 }
273 
274 /*
275  * SWAP_PAGER_SWAP_INIT() - swap pager initialization from pageout process
276  *
277  *	Expected to be started from pageout process once, prior to entering
278  *	its main loop.
279  */
280 
281 void
282 swap_pager_swap_init()
283 {
284 	int n, n2;
285 
286 	/*
287 	 * Number of in-transit swap bp operations.  Don't
288 	 * exhaust the pbufs completely.  Make sure we
289 	 * initialize workable values (0 will work for hysteresis
290 	 * but it isn't very efficient).
291 	 *
292 	 * The nsw_cluster_max is constrained by the bp->b_pages[]
293 	 * array (MAXPHYS/PAGE_SIZE) and our locally defined
294 	 * MAX_PAGEOUT_CLUSTER.   Also be aware that swap ops are
295 	 * constrained by the swap device interleave stripe size.
296 	 *
297 	 * Currently we hardwire nsw_wcount_async to 4.  This limit is
298 	 * designed to prevent other I/O from having high latencies due to
299 	 * our pageout I/O.  The value 4 works well for one or two active swap
300 	 * devices but is probably a little low if you have more.  Even so,
301 	 * a higher value would probably generate only a limited improvement
302 	 * with three or four active swap devices since the system does not
303 	 * typically have to pageout at extreme bandwidths.   We will want
304 	 * at least 2 per swap devices, and 4 is a pretty good value if you
305 	 * have one NFS swap device due to the command/ack latency over NFS.
306 	 * So it all works out pretty well.
307 	 */
308 
309 	nsw_cluster_max = min((MAXPHYS/PAGE_SIZE), MAX_PAGEOUT_CLUSTER);
310 
311 	nsw_rcount = (nswbuf + 1) / 2;
312 	nsw_wcount_sync = (nswbuf + 3) / 4;
313 	nsw_wcount_async = 4;
314 	nsw_wcount_async_max = nsw_wcount_async;
315 
316 	/*
317 	 * Initialize our zone.  Right now I'm just guessing on the number
318 	 * we need based on the number of pages in the system.  Each swblock
319 	 * can hold 16 pages, so this is probably overkill.
320 	 */
321 
322 	n = min(cnt.v_page_count, (kernel_map->max_offset - kernel_map->min_offset) / PAGE_SIZE) * 2;
323 	n2 = n;
324 
325 	while (n > 0
326 	       && (swap_zone = zinit(
327 		       "SWAPMETA",
328 		       sizeof(struct swblock),
329 		       n,
330 		       ZONE_INTERRUPT,
331 		       1
332 		       )) == NULL)
333 		n >>= 1;
334 	if (swap_zone == NULL)
335 		printf("WARNING: failed to init swap_zone!\n");
336 	if (n2 != n)
337 		printf("Swap zone entries reduced to %d.\n", n);
338 	n2 = n;
339 
340 	/*
341 	 * Initialize our meta-data hash table.  The swapper does not need to
342 	 * be quite as efficient as the VM system, so we do not use an
343 	 * oversized hash table.
344 	 *
345 	 * 	n: 		size of hash table, must be power of 2
346 	 *	swhash_mask:	hash table index mask
347 	 */
348 
349 	for (n = 1; n < n2 ; n <<= 1)
350 		;
351 
352 	swhash = malloc(sizeof(struct swblock *) * n, M_VMPGDATA, M_WAITOK | M_ZERO);
353 
354 	swhash_mask = n - 1;
355 }
356 
357 /*
358  * SWAP_PAGER_ALLOC() -	allocate a new OBJT_SWAP VM object and instantiate
359  *			its metadata structures.
360  *
361  *	This routine is called from the mmap and fork code to create a new
362  *	OBJT_SWAP object.  We do this by creating an OBJT_DEFAULT object
363  *	and then converting it with swp_pager_meta_build().
364  *
365  *	This routine may block in vm_object_allocate() and create a named
366  *	object lookup race, so we must interlock.   We must also run at
367  *	splvm() for the object lookup to handle races with interrupts, but
368  *	we do not have to maintain splvm() in between the lookup and the
369  *	add because (I believe) it is not possible to attempt to create
370  *	a new swap object w/handle when a default object with that handle
371  *	already exists.
372  */
373 
374 static vm_object_t
375 swap_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
376 		 vm_ooffset_t offset)
377 {
378 	vm_object_t object;
379 
380 	if (handle) {
381 		/*
382 		 * Reference existing named region or allocate new one.  There
383 		 * should not be a race here against swp_pager_meta_build()
384 		 * as called from vm_page_remove() in regards to the lookup
385 		 * of the handle.
386 		 */
387 
388 		while (sw_alloc_interlock) {
389 			sw_alloc_interlock = -1;
390 			tsleep(&sw_alloc_interlock, PVM, "swpalc", 0);
391 		}
392 		sw_alloc_interlock = 1;
393 
394 		object = vm_pager_object_lookup(NOBJLIST(handle), handle);
395 
396 		if (object != NULL) {
397 			vm_object_reference(object);
398 		} else {
399 			object = vm_object_allocate(OBJT_DEFAULT,
400 				OFF_TO_IDX(offset + PAGE_MASK + size));
401 			object->handle = handle;
402 
403 			swp_pager_meta_build(object, 0, SWAPBLK_NONE);
404 		}
405 
406 		if (sw_alloc_interlock < 0)
407 			wakeup(&sw_alloc_interlock);
408 
409 		sw_alloc_interlock = 0;
410 	} else {
411 		object = vm_object_allocate(OBJT_DEFAULT,
412 			OFF_TO_IDX(offset + PAGE_MASK + size));
413 
414 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
415 	}
416 
417 	return (object);
418 }
419 
420 /*
421  * SWAP_PAGER_DEALLOC() -	remove swap metadata from object
422  *
423  *	The swap backing for the object is destroyed.  The code is
424  *	designed such that we can reinstantiate it later, but this
425  *	routine is typically called only when the entire object is
426  *	about to be destroyed.
427  *
428  *	This routine may block, but no longer does.
429  *
430  *	The object must be locked or unreferenceable.
431  */
432 
433 static void
434 swap_pager_dealloc(object)
435 	vm_object_t object;
436 {
437 	int s;
438 
439 	/*
440 	 * Remove from list right away so lookups will fail if we block for
441 	 * pageout completion.
442 	 */
443 
444 	if (object->handle == NULL) {
445 		TAILQ_REMOVE(&swap_pager_un_object_list, object, pager_object_list);
446 	} else {
447 		TAILQ_REMOVE(NOBJLIST(object->handle), object, pager_object_list);
448 	}
449 
450 	vm_object_pip_wait(object, "swpdea");
451 
452 	/*
453 	 * Free all remaining metadata.  We only bother to free it from
454 	 * the swap meta data.  We do not attempt to free swapblk's still
455 	 * associated with vm_page_t's for this object.  We do not care
456 	 * if paging is still in progress on some objects.
457 	 */
458 	s = splvm();
459 	swp_pager_meta_free_all(object);
460 	splx(s);
461 }
462 
463 /************************************************************************
464  *			SWAP PAGER BITMAP ROUTINES			*
465  ************************************************************************/
466 
467 /*
468  * SWP_PAGER_GETSWAPSPACE() -	allocate raw swap space
469  *
470  *	Allocate swap for the requested number of pages.  The starting
471  *	swap block number (a page index) is returned or SWAPBLK_NONE
472  *	if the allocation failed.
473  *
474  *	Also has the side effect of advising that somebody made a mistake
475  *	when they configured swap and didn't configure enough.
476  *
477  *	Must be called at splvm() to avoid races with bitmap frees from
478  *	vm_page_remove() aka swap_pager_page_removed().
479  *
480  *	This routine may not block
481  *	This routine must be called at splvm().
482  */
483 
484 static __inline daddr_t
485 swp_pager_getswapspace(npages)
486 	int npages;
487 {
488 	daddr_t blk;
489 
490 	if ((blk = blist_alloc(swapblist, npages)) == SWAPBLK_NONE) {
491 		if (swap_pager_full != 2) {
492 			printf("swap_pager_getswapspace: failed\n");
493 			swap_pager_full = 2;
494 			swap_pager_almost_full = 1;
495 		}
496 	} else {
497 		vm_swap_size -= npages;
498 		/* per-swap area stats */
499 		swdevt[BLK2DEVIDX(blk)].sw_used += npages;
500 		swp_sizecheck();
501 	}
502 	return(blk);
503 }
504 
505 /*
506  * SWP_PAGER_FREESWAPSPACE() -	free raw swap space
507  *
508  *	This routine returns the specified swap blocks back to the bitmap.
509  *
510  *	Note:  This routine may not block (it could in the old swap code),
511  *	and through the use of the new blist routines it does not block.
512  *
513  *	We must be called at splvm() to avoid races with bitmap frees from
514  *	vm_page_remove() aka swap_pager_page_removed().
515  *
516  *	This routine may not block
517  *	This routine must be called at splvm().
518  */
519 
520 static __inline void
521 swp_pager_freeswapspace(blk, npages)
522 	daddr_t blk;
523 	int npages;
524 {
525 	blist_free(swapblist, blk, npages);
526 	vm_swap_size += npages;
527 	/* per-swap area stats */
528 	swdevt[BLK2DEVIDX(blk)].sw_used -= npages;
529 	swp_sizecheck();
530 }
531 
532 /*
533  * SWAP_PAGER_FREESPACE() -	frees swap blocks associated with a page
534  *				range within an object.
535  *
536  *	This is a globally accessible routine.
537  *
538  *	This routine removes swapblk assignments from swap metadata.
539  *
540  *	The external callers of this routine typically have already destroyed
541  *	or renamed vm_page_t's associated with this range in the object so
542  *	we should be ok.
543  *
544  *	This routine may be called at any spl.  We up our spl to splvm temporarily
545  *	in order to perform the metadata removal.
546  */
547 
548 void
549 swap_pager_freespace(object, start, size)
550 	vm_object_t object;
551 	vm_pindex_t start;
552 	vm_size_t size;
553 {
554 	int s = splvm();
555 	swp_pager_meta_free(object, start, size);
556 	splx(s);
557 }
558 
559 /*
560  * SWAP_PAGER_RESERVE() - reserve swap blocks in object
561  *
562  *	Assigns swap blocks to the specified range within the object.  The
563  *	swap blocks are not zerod.  Any previous swap assignment is destroyed.
564  *
565  *	Returns 0 on success, -1 on failure.
566  */
567 
568 int
569 swap_pager_reserve(vm_object_t object, vm_pindex_t start, vm_size_t size)
570 {
571 	int s;
572 	int n = 0;
573 	daddr_t blk = SWAPBLK_NONE;
574 	vm_pindex_t beg = start;	/* save start index */
575 
576 	s = splvm();
577 	while (size) {
578 		if (n == 0) {
579 			n = BLIST_MAX_ALLOC;
580 			while ((blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE) {
581 				n >>= 1;
582 				if (n == 0) {
583 					swp_pager_meta_free(object, beg, start - beg);
584 					splx(s);
585 					return(-1);
586 				}
587 			}
588 		}
589 		swp_pager_meta_build(object, start, blk);
590 		--size;
591 		++start;
592 		++blk;
593 		--n;
594 	}
595 	swp_pager_meta_free(object, start, n);
596 	splx(s);
597 	return(0);
598 }
599 
600 /*
601  * SWAP_PAGER_COPY() -  copy blocks from source pager to destination pager
602  *			and destroy the source.
603  *
604  *	Copy any valid swapblks from the source to the destination.  In
605  *	cases where both the source and destination have a valid swapblk,
606  *	we keep the destination's.
607  *
608  *	This routine is allowed to block.  It may block allocating metadata
609  *	indirectly through swp_pager_meta_build() or if paging is still in
610  *	progress on the source.
611  *
612  *	This routine can be called at any spl
613  *
614  *	XXX vm_page_collapse() kinda expects us not to block because we
615  *	supposedly do not need to allocate memory, but for the moment we
616  *	*may* have to get a little memory from the zone allocator, but
617  *	it is taken from the interrupt memory.  We should be ok.
618  *
619  *	The source object contains no vm_page_t's (which is just as well)
620  *
621  *	The source object is of type OBJT_SWAP.
622  *
623  *	The source and destination objects must be locked or
624  *	inaccessible (XXX are they ?)
625  */
626 
627 void
628 swap_pager_copy(srcobject, dstobject, offset, destroysource)
629 	vm_object_t srcobject;
630 	vm_object_t dstobject;
631 	vm_pindex_t offset;
632 	int destroysource;
633 {
634 	vm_pindex_t i;
635 	int s;
636 
637 	s = splvm();
638 
639 	/*
640 	 * If destroysource is set, we remove the source object from the
641 	 * swap_pager internal queue now.
642 	 */
643 
644 	if (destroysource) {
645 		if (srcobject->handle == NULL) {
646 			TAILQ_REMOVE(
647 			    &swap_pager_un_object_list,
648 			    srcobject,
649 			    pager_object_list
650 			);
651 		} else {
652 			TAILQ_REMOVE(
653 			    NOBJLIST(srcobject->handle),
654 			    srcobject,
655 			    pager_object_list
656 			);
657 		}
658 	}
659 
660 	/*
661 	 * transfer source to destination.
662 	 */
663 
664 	for (i = 0; i < dstobject->size; ++i) {
665 		daddr_t dstaddr;
666 
667 		/*
668 		 * Locate (without changing) the swapblk on the destination,
669 		 * unless it is invalid in which case free it silently, or
670 		 * if the destination is a resident page, in which case the
671 		 * source is thrown away.
672 		 */
673 
674 		dstaddr = swp_pager_meta_ctl(dstobject, i, 0);
675 
676 		if (dstaddr == SWAPBLK_NONE) {
677 			/*
678 			 * Destination has no swapblk and is not resident,
679 			 * copy source.
680 			 */
681 			daddr_t srcaddr;
682 
683 			srcaddr = swp_pager_meta_ctl(
684 			    srcobject,
685 			    i + offset,
686 			    SWM_POP
687 			);
688 
689 			if (srcaddr != SWAPBLK_NONE)
690 				swp_pager_meta_build(dstobject, i, srcaddr);
691 		} else {
692 			/*
693 			 * Destination has valid swapblk or it is represented
694 			 * by a resident page.  We destroy the sourceblock.
695 			 */
696 
697 			swp_pager_meta_ctl(srcobject, i + offset, SWM_FREE);
698 		}
699 	}
700 
701 	/*
702 	 * Free left over swap blocks in source.
703 	 *
704 	 * We have to revert the type to OBJT_DEFAULT so we do not accidently
705 	 * double-remove the object from the swap queues.
706 	 */
707 
708 	if (destroysource) {
709 		swp_pager_meta_free_all(srcobject);
710 		/*
711 		 * Reverting the type is not necessary, the caller is going
712 		 * to destroy srcobject directly, but I'm doing it here
713 		 * for consistency since we've removed the object from its
714 		 * queues.
715 		 */
716 		srcobject->type = OBJT_DEFAULT;
717 	}
718 	splx(s);
719 }
720 
721 /*
722  * SWAP_PAGER_HASPAGE() -	determine if we have good backing store for
723  *				the requested page.
724  *
725  *	We determine whether good backing store exists for the requested
726  *	page and return TRUE if it does, FALSE if it doesn't.
727  *
728  *	If TRUE, we also try to determine how much valid, contiguous backing
729  *	store exists before and after the requested page within a reasonable
730  *	distance.  We do not try to restrict it to the swap device stripe
731  *	(that is handled in getpages/putpages).  It probably isn't worth
732  *	doing here.
733  */
734 
735 boolean_t
736 swap_pager_haspage(object, pindex, before, after)
737 	vm_object_t object;
738 	vm_pindex_t pindex;
739 	int *before;
740 	int *after;
741 {
742 	daddr_t blk0;
743 	int s;
744 
745 	/*
746 	 * do we have good backing store at the requested index ?
747 	 */
748 
749 	s = splvm();
750 	blk0 = swp_pager_meta_ctl(object, pindex, 0);
751 
752 	if (blk0 == SWAPBLK_NONE) {
753 		splx(s);
754 		if (before)
755 			*before = 0;
756 		if (after)
757 			*after = 0;
758 		return (FALSE);
759 	}
760 
761 	/*
762 	 * find backwards-looking contiguous good backing store
763 	 */
764 
765 	if (before != NULL) {
766 		int i;
767 
768 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
769 			daddr_t blk;
770 
771 			if (i > pindex)
772 				break;
773 			blk = swp_pager_meta_ctl(object, pindex - i, 0);
774 			if (blk != blk0 - i)
775 				break;
776 		}
777 		*before = (i - 1);
778 	}
779 
780 	/*
781 	 * find forward-looking contiguous good backing store
782 	 */
783 
784 	if (after != NULL) {
785 		int i;
786 
787 		for (i = 1; i < (SWB_NPAGES/2); ++i) {
788 			daddr_t blk;
789 
790 			blk = swp_pager_meta_ctl(object, pindex + i, 0);
791 			if (blk != blk0 + i)
792 				break;
793 		}
794 		*after = (i - 1);
795 	}
796 	splx(s);
797 	return (TRUE);
798 }
799 
800 /*
801  * SWAP_PAGER_PAGE_UNSWAPPED() - remove swap backing store related to page
802  *
803  *	This removes any associated swap backing store, whether valid or
804  *	not, from the page.
805  *
806  *	This routine is typically called when a page is made dirty, at
807  *	which point any associated swap can be freed.  MADV_FREE also
808  *	calls us in a special-case situation
809  *
810  *	NOTE!!!  If the page is clean and the swap was valid, the caller
811  *	should make the page dirty before calling this routine.  This routine
812  *	does NOT change the m->dirty status of the page.  Also: MADV_FREE
813  *	depends on it.
814  *
815  *	This routine may not block
816  *	This routine must be called at splvm()
817  */
818 
819 static void
820 swap_pager_unswapped(m)
821 	vm_page_t m;
822 {
823 	swp_pager_meta_ctl(m->object, m->pindex, SWM_FREE);
824 }
825 
826 /*
827  * SWAP_PAGER_STRATEGY() - read, write, free blocks
828  *
829  *	This implements the vm_pager_strategy() interface to swap and allows
830  *	other parts of the system to directly access swap as backing store
831  *	through vm_objects of type OBJT_SWAP.  This is intended to be a
832  *	cacheless interface ( i.e. caching occurs at higher levels ).
833  *	Therefore we do not maintain any resident pages.  All I/O goes
834  *	directly to and from the swap device.
835  *
836  *	Note that b_blkno is scaled for PAGE_SIZE
837  *
838  *	We currently attempt to run I/O synchronously or asynchronously as
839  *	the caller requests.  This isn't perfect because we loose error
840  *	sequencing when we run multiple ops in parallel to satisfy a request.
841  *	But this is swap, so we let it all hang out.
842  */
843 
844 static void
845 swap_pager_strategy(vm_object_t object, struct bio *bp)
846 {
847 	vm_pindex_t start;
848 	int count;
849 	int s;
850 	char *data;
851 	struct buf *nbp = NULL;
852 
853 	/* XXX: KASSERT instead ? */
854 	if (bp->bio_bcount & PAGE_MASK) {
855 		bp->bio_error = EINVAL;
856 		bp->bio_flags |= BIO_ERROR;
857 		biodone(bp);
858 		printf("swap_pager_strategy: bp %p blk %d size %d, not page bounded\n", bp, (int)bp->bio_pblkno, (int)bp->bio_bcount);
859 		return;
860 	}
861 
862 	/*
863 	 * Clear error indication, initialize page index, count, data pointer.
864 	 */
865 
866 	bp->bio_error = 0;
867 	bp->bio_flags &= ~BIO_ERROR;
868 	bp->bio_resid = bp->bio_bcount;
869 
870 	start = bp->bio_pblkno;
871 	count = howmany(bp->bio_bcount, PAGE_SIZE);
872 	data = bp->bio_data;
873 
874 	s = splvm();
875 
876 	/*
877 	 * Deal with BIO_DELETE
878 	 */
879 
880 	if (bp->bio_cmd == BIO_DELETE) {
881 		/*
882 		 * FREE PAGE(s) - destroy underlying swap that is no longer
883 		 *		  needed.
884 		 */
885 		swp_pager_meta_free(object, start, count);
886 		splx(s);
887 		bp->bio_resid = 0;
888 		biodone(bp);
889 		return;
890 	}
891 
892 	/*
893 	 * Execute read or write
894 	 */
895 
896 	while (count > 0) {
897 		daddr_t blk;
898 
899 		/*
900 		 * Obtain block.  If block not found and writing, allocate a
901 		 * new block and build it into the object.
902 		 */
903 
904 		blk = swp_pager_meta_ctl(object, start, 0);
905 		if ((blk == SWAPBLK_NONE) && (bp->bio_cmd == BIO_WRITE)) {
906 			blk = swp_pager_getswapspace(1);
907 			if (blk == SWAPBLK_NONE) {
908 				bp->bio_error = ENOMEM;
909 				bp->bio_flags |= BIO_ERROR;
910 				break;
911 			}
912 			swp_pager_meta_build(object, start, blk);
913 		}
914 
915 		/*
916 		 * Do we have to flush our current collection?  Yes if:
917 		 *
918 		 *	- no swap block at this index
919 		 *	- swap block is not contiguous
920 		 *	- we cross a physical disk boundry in the
921 		 *	  stripe.
922 		 */
923 
924 		if (
925 		    nbp && (nbp->b_blkno + btoc(nbp->b_bcount) != blk ||
926 		     ((nbp->b_blkno ^ blk) & dmmax_mask)
927 		    )
928 		) {
929 			splx(s);
930 			if (bp->bio_cmd == BIO_READ) {
931 				++cnt.v_swapin;
932 				cnt.v_swappgsin += btoc(nbp->b_bcount);
933 			} else {
934 				++cnt.v_swapout;
935 				cnt.v_swappgsout += btoc(nbp->b_bcount);
936 				nbp->b_dirtyend = nbp->b_bcount;
937 			}
938 			flushchainbuf(nbp);
939 			s = splvm();
940 			nbp = NULL;
941 		}
942 
943 		/*
944 		 * Add new swapblk to nbp, instantiating nbp if necessary.
945 		 * Zero-fill reads are able to take a shortcut.
946 		 */
947 
948 		if (blk == SWAPBLK_NONE) {
949 			/*
950 			 * We can only get here if we are reading.  Since
951 			 * we are at splvm() we can safely modify b_resid,
952 			 * even if chain ops are in progress.
953 			 */
954 			bzero(data, PAGE_SIZE);
955 			bp->bio_resid -= PAGE_SIZE;
956 		} else {
957 			if (nbp == NULL) {
958 				nbp = getchainbuf(bp, swapdev_vp, B_ASYNC);
959 				nbp->b_blkno = blk;
960 				nbp->b_bcount = 0;
961 				nbp->b_data = data;
962 			}
963 			nbp->b_bcount += PAGE_SIZE;
964 		}
965 		--count;
966 		++start;
967 		data += PAGE_SIZE;
968 	}
969 
970 	/*
971 	 *  Flush out last buffer
972 	 */
973 
974 	splx(s);
975 
976 	if (nbp) {
977 		if (nbp->b_iocmd == BIO_READ) {
978 			++cnt.v_swapin;
979 			cnt.v_swappgsin += btoc(nbp->b_bcount);
980 		} else {
981 			++cnt.v_swapout;
982 			cnt.v_swappgsout += btoc(nbp->b_bcount);
983 			nbp->b_dirtyend = nbp->b_bcount;
984 		}
985 		flushchainbuf(nbp);
986 		/* nbp = NULL; */
987 	}
988 
989 	/*
990 	 * Wait for completion.
991 	 */
992 
993 	waitchainbuf(bp, 0, 1);
994 }
995 
996 /*
997  * SWAP_PAGER_GETPAGES() - bring pages in from swap
998  *
999  *	Attempt to retrieve (m, count) pages from backing store, but make
1000  *	sure we retrieve at least m[reqpage].  We try to load in as large
1001  *	a chunk surrounding m[reqpage] as is contiguous in swap and which
1002  *	belongs to the same object.
1003  *
1004  *	The code is designed for asynchronous operation and
1005  *	immediate-notification of 'reqpage' but tends not to be
1006  *	used that way.  Please do not optimize-out this algorithmic
1007  *	feature, I intend to improve on it in the future.
1008  *
1009  *	The parent has a single vm_object_pip_add() reference prior to
1010  *	calling us and we should return with the same.
1011  *
1012  *	The parent has BUSY'd the pages.  We should return with 'm'
1013  *	left busy, but the others adjusted.
1014  */
1015 
1016 static int
1017 swap_pager_getpages(object, m, count, reqpage)
1018 	vm_object_t object;
1019 	vm_page_t *m;
1020 	int count, reqpage;
1021 {
1022 	struct buf *bp;
1023 	vm_page_t mreq;
1024 	int s;
1025 	int i;
1026 	int j;
1027 	daddr_t blk;
1028 	vm_offset_t kva;
1029 	vm_pindex_t lastpindex;
1030 
1031 	mreq = m[reqpage];
1032 
1033 	if (mreq->object != object) {
1034 		panic("swap_pager_getpages: object mismatch %p/%p",
1035 		    object,
1036 		    mreq->object
1037 		);
1038 	}
1039 	/*
1040 	 * Calculate range to retrieve.  The pages have already been assigned
1041 	 * their swapblks.  We require a *contiguous* range that falls entirely
1042 	 * within a single device stripe.   If we do not supply it, bad things
1043 	 * happen.  Note that blk, iblk & jblk can be SWAPBLK_NONE, but the
1044 	 * loops are set up such that the case(s) are handled implicitly.
1045 	 *
1046 	 * The swp_*() calls must be made at splvm().  vm_page_free() does
1047 	 * not need to be, but it will go a little faster if it is.
1048 	 */
1049 
1050 	s = splvm();
1051 	blk = swp_pager_meta_ctl(mreq->object, mreq->pindex, 0);
1052 
1053 	for (i = reqpage - 1; i >= 0; --i) {
1054 		daddr_t iblk;
1055 
1056 		iblk = swp_pager_meta_ctl(m[i]->object, m[i]->pindex, 0);
1057 		if (blk != iblk + (reqpage - i))
1058 			break;
1059 		if ((blk ^ iblk) & dmmax_mask)
1060 			break;
1061 	}
1062 	++i;
1063 
1064 	for (j = reqpage + 1; j < count; ++j) {
1065 		daddr_t jblk;
1066 
1067 		jblk = swp_pager_meta_ctl(m[j]->object, m[j]->pindex, 0);
1068 		if (blk != jblk - (j - reqpage))
1069 			break;
1070 		if ((blk ^ jblk) & dmmax_mask)
1071 			break;
1072 	}
1073 
1074 	/*
1075 	 * free pages outside our collection range.   Note: we never free
1076 	 * mreq, it must remain busy throughout.
1077 	 */
1078 
1079 	{
1080 		int k;
1081 
1082 		for (k = 0; k < i; ++k)
1083 			vm_page_free(m[k]);
1084 		for (k = j; k < count; ++k)
1085 			vm_page_free(m[k]);
1086 	}
1087 	splx(s);
1088 
1089 
1090 	/*
1091 	 * Return VM_PAGER_FAIL if we have nothing to do.  Return mreq
1092 	 * still busy, but the others unbusied.
1093 	 */
1094 
1095 	if (blk == SWAPBLK_NONE)
1096 		return(VM_PAGER_FAIL);
1097 
1098 	/*
1099 	 * Get a swap buffer header to perform the IO
1100 	 */
1101 
1102 	bp = getpbuf(&nsw_rcount);
1103 	kva = (vm_offset_t) bp->b_data;
1104 
1105 	/*
1106 	 * map our page(s) into kva for input
1107 	 *
1108 	 * NOTE: B_PAGING is set by pbgetvp()
1109 	 */
1110 
1111 	pmap_qenter(kva, m + i, j - i);
1112 
1113 	bp->b_iocmd = BIO_READ;
1114 	bp->b_iodone = swp_pager_async_iodone;
1115 	bp->b_rcred = bp->b_wcred = proc0.p_ucred;
1116 	bp->b_data = (caddr_t) kva;
1117 	crhold(bp->b_rcred);
1118 	crhold(bp->b_wcred);
1119 	bp->b_blkno = blk - (reqpage - i);
1120 	bp->b_bcount = PAGE_SIZE * (j - i);
1121 	bp->b_bufsize = PAGE_SIZE * (j - i);
1122 	bp->b_pager.pg_reqpage = reqpage - i;
1123 
1124 	{
1125 		int k;
1126 
1127 		for (k = i; k < j; ++k) {
1128 			bp->b_pages[k - i] = m[k];
1129 			vm_page_flag_set(m[k], PG_SWAPINPROG);
1130 		}
1131 	}
1132 	bp->b_npages = j - i;
1133 
1134 	pbgetvp(swapdev_vp, bp);
1135 
1136 	cnt.v_swapin++;
1137 	cnt.v_swappgsin += bp->b_npages;
1138 
1139 	/*
1140 	 * We still hold the lock on mreq, and our automatic completion routine
1141 	 * does not remove it.
1142 	 */
1143 
1144 	vm_object_pip_add(mreq->object, bp->b_npages);
1145 	lastpindex = m[j-1]->pindex;
1146 
1147 	/*
1148 	 * perform the I/O.  NOTE!!!  bp cannot be considered valid after
1149 	 * this point because we automatically release it on completion.
1150 	 * Instead, we look at the one page we are interested in which we
1151 	 * still hold a lock on even through the I/O completion.
1152 	 *
1153 	 * The other pages in our m[] array are also released on completion,
1154 	 * so we cannot assume they are valid anymore either.
1155 	 *
1156 	 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
1157 	 */
1158 
1159 	BUF_KERNPROC(bp);
1160 	BUF_STRATEGY(bp);
1161 
1162 	/*
1163 	 * wait for the page we want to complete.  PG_SWAPINPROG is always
1164 	 * cleared on completion.  If an I/O error occurs, SWAPBLK_NONE
1165 	 * is set in the meta-data.
1166 	 */
1167 
1168 	s = splvm();
1169 
1170 	while ((mreq->flags & PG_SWAPINPROG) != 0) {
1171 		vm_page_flag_set(mreq, PG_WANTED | PG_REFERENCED);
1172 		cnt.v_intrans++;
1173 		if (tsleep(mreq, PSWP, "swread", hz*20)) {
1174 			printf(
1175 			    "swap_pager: indefinite wait buffer: device:"
1176 				" %s, blkno: %ld, size: %ld\n",
1177 			    devtoname(bp->b_dev), (long)bp->b_blkno,
1178 			    bp->b_bcount
1179 			);
1180 		}
1181 	}
1182 
1183 	splx(s);
1184 
1185 	/*
1186 	 * mreq is left bussied after completion, but all the other pages
1187 	 * are freed.  If we had an unrecoverable read error the page will
1188 	 * not be valid.
1189 	 */
1190 
1191 	if (mreq->valid != VM_PAGE_BITS_ALL) {
1192 		return(VM_PAGER_ERROR);
1193 	} else {
1194 		return(VM_PAGER_OK);
1195 	}
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_putpages:
1207  *
1208  *	Assign swap (if necessary) and initiate I/O on the specified pages.
1209  *
1210  *	We support both OBJT_DEFAULT and OBJT_SWAP objects.  DEFAULT objects
1211  *	are automatically converted to SWAP objects.
1212  *
1213  *	In a low memory situation we may block in VOP_STRATEGY(), but the new
1214  *	vm_page reservation system coupled with properly written VFS devices
1215  *	should ensure that no low-memory deadlock occurs.  This is an area
1216  *	which needs work.
1217  *
1218  *	The parent has N vm_object_pip_add() references prior to
1219  *	calling us and will remove references for rtvals[] that are
1220  *	not set to VM_PAGER_PEND.  We need to remove the rest on I/O
1221  *	completion.
1222  *
1223  *	The parent has soft-busy'd the pages it passes us and will unbusy
1224  *	those whos rtvals[] entry is not set to VM_PAGER_PEND on return.
1225  *	We need to unbusy the rest on I/O completion.
1226  */
1227 
1228 void
1229 swap_pager_putpages(object, m, count, sync, rtvals)
1230 	vm_object_t object;
1231 	vm_page_t *m;
1232 	int count;
1233 	boolean_t sync;
1234 	int *rtvals;
1235 {
1236 	int i;
1237 	int n = 0;
1238 
1239 	if (count && m[0]->object != object) {
1240 		panic("swap_pager_getpages: object mismatch %p/%p",
1241 		    object,
1242 		    m[0]->object
1243 		);
1244 	}
1245 	/*
1246 	 * Step 1
1247 	 *
1248 	 * Turn object into OBJT_SWAP
1249 	 * check for bogus sysops
1250 	 * force sync if not pageout process
1251 	 */
1252 
1253 	if (object->type != OBJT_SWAP)
1254 		swp_pager_meta_build(object, 0, SWAPBLK_NONE);
1255 
1256 	if (curproc != pageproc)
1257 		sync = TRUE;
1258 
1259 	/*
1260 	 * Step 2
1261 	 *
1262 	 * Update nsw parameters from swap_async_max sysctl values.
1263 	 * Do not let the sysop crash the machine with bogus numbers.
1264 	 */
1265 
1266 	if (swap_async_max != nsw_wcount_async_max) {
1267 		int n;
1268 		int s;
1269 
1270 		/*
1271 		 * limit range
1272 		 */
1273 		if ((n = swap_async_max) > nswbuf / 2)
1274 			n = nswbuf / 2;
1275 		if (n < 1)
1276 			n = 1;
1277 		swap_async_max = n;
1278 
1279 		/*
1280 		 * Adjust difference ( if possible ).  If the current async
1281 		 * count is too low, we may not be able to make the adjustment
1282 		 * at this time.
1283 		 */
1284 		s = splvm();
1285 		mtx_lock(&pbuf_mtx);
1286 		n -= nsw_wcount_async_max;
1287 		if (nsw_wcount_async + n >= 0) {
1288 			nsw_wcount_async += n;
1289 			nsw_wcount_async_max += n;
1290 			wakeup(&nsw_wcount_async);
1291 		}
1292 		mtx_unlock(&pbuf_mtx);
1293 		splx(s);
1294 	}
1295 
1296 	/*
1297 	 * Step 3
1298 	 *
1299 	 * Assign swap blocks and issue I/O.  We reallocate swap on the fly.
1300 	 * The page is left dirty until the pageout operation completes
1301 	 * successfully.
1302 	 */
1303 
1304 	for (i = 0; i < count; i += n) {
1305 		int s;
1306 		int j;
1307 		struct buf *bp;
1308 		daddr_t blk;
1309 
1310 		/*
1311 		 * Maximum I/O size is limited by a number of factors.
1312 		 */
1313 
1314 		n = min(BLIST_MAX_ALLOC, count - i);
1315 		n = min(n, nsw_cluster_max);
1316 
1317 		s = splvm();
1318 
1319 		/*
1320 		 * Get biggest block of swap we can.  If we fail, fall
1321 		 * back and try to allocate a smaller block.  Don't go
1322 		 * overboard trying to allocate space if it would overly
1323 		 * fragment swap.
1324 		 */
1325 		while (
1326 		    (blk = swp_pager_getswapspace(n)) == SWAPBLK_NONE &&
1327 		    n > 4
1328 		) {
1329 			n >>= 1;
1330 		}
1331 		if (blk == SWAPBLK_NONE) {
1332 			for (j = 0; j < n; ++j)
1333 				rtvals[i+j] = VM_PAGER_FAIL;
1334 			splx(s);
1335 			continue;
1336 		}
1337 
1338 		/*
1339 		 * The I/O we are constructing cannot cross a physical
1340 		 * disk boundry in the swap stripe.  Note: we are still
1341 		 * at splvm().
1342 		 */
1343 		if ((blk ^ (blk + n)) & dmmax_mask) {
1344 			j = ((blk + dmmax) & dmmax_mask) - blk;
1345 			swp_pager_freeswapspace(blk + j, n - j);
1346 			n = j;
1347 		}
1348 
1349 		/*
1350 		 * All I/O parameters have been satisfied, build the I/O
1351 		 * request and assign the swap space.
1352 		 *
1353 		 * NOTE: B_PAGING is set by pbgetvp()
1354 		 */
1355 
1356 		if (sync == TRUE) {
1357 			bp = getpbuf(&nsw_wcount_sync);
1358 		} else {
1359 			bp = getpbuf(&nsw_wcount_async);
1360 			bp->b_flags = B_ASYNC;
1361 		}
1362 		bp->b_iocmd = BIO_WRITE;
1363 		bp->b_spc = NULL;	/* not used, but NULL-out anyway */
1364 
1365 		pmap_qenter((vm_offset_t)bp->b_data, &m[i], n);
1366 
1367 		bp->b_rcred = bp->b_wcred = proc0.p_ucred;
1368 		bp->b_bcount = PAGE_SIZE * n;
1369 		bp->b_bufsize = PAGE_SIZE * n;
1370 		bp->b_blkno = blk;
1371 
1372 		crhold(bp->b_rcred);
1373 		crhold(bp->b_wcred);
1374 
1375 		pbgetvp(swapdev_vp, bp);
1376 
1377 		for (j = 0; j < n; ++j) {
1378 			vm_page_t mreq = m[i+j];
1379 
1380 			swp_pager_meta_build(
1381 			    mreq->object,
1382 			    mreq->pindex,
1383 			    blk + j
1384 			);
1385 			vm_page_dirty(mreq);
1386 			rtvals[i+j] = VM_PAGER_OK;
1387 
1388 			vm_page_flag_set(mreq, PG_SWAPINPROG);
1389 			bp->b_pages[j] = mreq;
1390 		}
1391 		bp->b_npages = n;
1392 		/*
1393 		 * Must set dirty range for NFS to work.
1394 		 */
1395 		bp->b_dirtyoff = 0;
1396 		bp->b_dirtyend = bp->b_bcount;
1397 
1398 		cnt.v_swapout++;
1399 		cnt.v_swappgsout += bp->b_npages;
1400 		swapdev_vp->v_numoutput++;
1401 
1402 		splx(s);
1403 
1404 		/*
1405 		 * asynchronous
1406 		 *
1407 		 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
1408 		 */
1409 
1410 		if (sync == FALSE) {
1411 			bp->b_iodone = swp_pager_async_iodone;
1412 			BUF_KERNPROC(bp);
1413 			BUF_STRATEGY(bp);
1414 
1415 			for (j = 0; j < n; ++j)
1416 				rtvals[i+j] = VM_PAGER_PEND;
1417 			continue;
1418 		}
1419 
1420 		/*
1421 		 * synchronous
1422 		 *
1423 		 * NOTE: b_blkno is destroyed by the call to VOP_STRATEGY
1424 		 */
1425 
1426 		bp->b_iodone = swp_pager_sync_iodone;
1427 		BUF_STRATEGY(bp);
1428 
1429 		/*
1430 		 * Wait for the sync I/O to complete, then update rtvals.
1431 		 * We just set the rtvals[] to VM_PAGER_PEND so we can call
1432 		 * our async completion routine at the end, thus avoiding a
1433 		 * double-free.
1434 		 */
1435 		s = splbio();
1436 
1437 		while ((bp->b_flags & B_DONE) == 0) {
1438 			tsleep(bp, PVM, "swwrt", 0);
1439 		}
1440 
1441 		for (j = 0; j < n; ++j)
1442 			rtvals[i+j] = VM_PAGER_PEND;
1443 
1444 		/*
1445 		 * Now that we are through with the bp, we can call the
1446 		 * normal async completion, which frees everything up.
1447 		 */
1448 
1449 		swp_pager_async_iodone(bp);
1450 
1451 		splx(s);
1452 	}
1453 }
1454 
1455 /*
1456  *	swap_pager_sync_iodone:
1457  *
1458  *	Completion routine for synchronous reads and writes from/to swap.
1459  *	We just mark the bp is complete and wake up anyone waiting on it.
1460  *
1461  *	This routine may not block.  This routine is called at splbio() or better.
1462  */
1463 
1464 static void
1465 swp_pager_sync_iodone(bp)
1466 	struct buf *bp;
1467 {
1468 	bp->b_flags |= B_DONE;
1469 	bp->b_flags &= ~B_ASYNC;
1470 	wakeup(bp);
1471 }
1472 
1473 /*
1474  *	swp_pager_async_iodone:
1475  *
1476  *	Completion routine for asynchronous reads and writes from/to swap.
1477  *	Also called manually by synchronous code to finish up a bp.
1478  *
1479  *	For READ operations, the pages are PG_BUSY'd.  For WRITE operations,
1480  *	the pages are vm_page_t->busy'd.  For READ operations, we PG_BUSY
1481  *	unbusy all pages except the 'main' request page.  For WRITE
1482  *	operations, we vm_page_t->busy'd unbusy all pages ( we can do this
1483  *	because we marked them all VM_PAGER_PEND on return from putpages ).
1484  *
1485  *	This routine may not block.
1486  *	This routine is called at splbio() or better
1487  *
1488  *	We up ourselves to splvm() as required for various vm_page related
1489  *	calls.
1490  */
1491 
1492 static void
1493 swp_pager_async_iodone(bp)
1494 	register struct buf *bp;
1495 {
1496 	int s;
1497 	int i;
1498 	vm_object_t object = NULL;
1499 
1500 	bp->b_flags |= B_DONE;
1501 
1502 	/*
1503 	 * report error
1504 	 */
1505 
1506 	if (bp->b_ioflags & BIO_ERROR) {
1507 		printf(
1508 		    "swap_pager: I/O error - %s failed; blkno %ld,"
1509 			"size %ld, error %d\n",
1510 		    ((bp->b_iocmd == BIO_READ) ? "pagein" : "pageout"),
1511 		    (long)bp->b_blkno,
1512 		    (long)bp->b_bcount,
1513 		    bp->b_error
1514 		);
1515 	}
1516 
1517 	/*
1518 	 * set object, raise to splvm().
1519 	 */
1520 
1521 	if (bp->b_npages)
1522 		object = bp->b_pages[0]->object;
1523 	s = splvm();
1524 
1525 	/*
1526 	 * remove the mapping for kernel virtual
1527 	 */
1528 
1529 	pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1530 
1531 	/*
1532 	 * cleanup pages.  If an error occurs writing to swap, we are in
1533 	 * very serious trouble.  If it happens to be a disk error, though,
1534 	 * we may be able to recover by reassigning the swap later on.  So
1535 	 * in this case we remove the m->swapblk assignment for the page
1536 	 * but do not free it in the rlist.  The errornous block(s) are thus
1537 	 * never reallocated as swap.  Redirty the page and continue.
1538 	 */
1539 
1540 	for (i = 0; i < bp->b_npages; ++i) {
1541 		vm_page_t m = bp->b_pages[i];
1542 
1543 		vm_page_flag_clear(m, PG_SWAPINPROG);
1544 
1545 		if (bp->b_ioflags & BIO_ERROR) {
1546 			/*
1547 			 * If an error occurs I'd love to throw the swapblk
1548 			 * away without freeing it back to swapspace, so it
1549 			 * can never be used again.  But I can't from an
1550 			 * interrupt.
1551 			 */
1552 
1553 			if (bp->b_iocmd == BIO_READ) {
1554 				/*
1555 				 * When reading, reqpage needs to stay
1556 				 * locked for the parent, but all other
1557 				 * pages can be freed.  We still want to
1558 				 * wakeup the parent waiting on the page,
1559 				 * though.  ( also: pg_reqpage can be -1 and
1560 				 * not match anything ).
1561 				 *
1562 				 * We have to wake specifically requested pages
1563 				 * up too because we cleared PG_SWAPINPROG and
1564 				 * someone may be waiting for that.
1565 				 *
1566 				 * NOTE: for reads, m->dirty will probably
1567 				 * be overridden by the original caller of
1568 				 * getpages so don't play cute tricks here.
1569 				 *
1570 				 * XXX IT IS NOT LEGAL TO FREE THE PAGE HERE
1571 				 * AS THIS MESSES WITH object->memq, and it is
1572 				 * not legal to mess with object->memq from an
1573 				 * interrupt.
1574 				 */
1575 
1576 				m->valid = 0;
1577 				vm_page_flag_clear(m, PG_ZERO);
1578 
1579 				if (i != bp->b_pager.pg_reqpage)
1580 					vm_page_free(m);
1581 				else
1582 					vm_page_flash(m);
1583 				/*
1584 				 * If i == bp->b_pager.pg_reqpage, do not wake
1585 				 * the page up.  The caller needs to.
1586 				 */
1587 			} else {
1588 				/*
1589 				 * If a write error occurs, reactivate page
1590 				 * so it doesn't clog the inactive list,
1591 				 * then finish the I/O.
1592 				 */
1593 				vm_page_dirty(m);
1594 				vm_page_activate(m);
1595 				vm_page_io_finish(m);
1596 			}
1597 		} else if (bp->b_iocmd == BIO_READ) {
1598 			/*
1599 			 * For read success, clear dirty bits.  Nobody should
1600 			 * have this page mapped but don't take any chances,
1601 			 * make sure the pmap modify bits are also cleared.
1602 			 *
1603 			 * NOTE: for reads, m->dirty will probably be
1604 			 * overridden by the original caller of getpages so
1605 			 * we cannot set them in order to free the underlying
1606 			 * swap in a low-swap situation.  I don't think we'd
1607 			 * want to do that anyway, but it was an optimization
1608 			 * that existed in the old swapper for a time before
1609 			 * it got ripped out due to precisely this problem.
1610 			 *
1611 			 * clear PG_ZERO in page.
1612 			 *
1613 			 * If not the requested page then deactivate it.
1614 			 *
1615 			 * Note that the requested page, reqpage, is left
1616 			 * busied, but we still have to wake it up.  The
1617 			 * other pages are released (unbusied) by
1618 			 * vm_page_wakeup().  We do not set reqpage's
1619 			 * valid bits here, it is up to the caller.
1620 			 */
1621 
1622 			pmap_clear_modify(m);
1623 			m->valid = VM_PAGE_BITS_ALL;
1624 			vm_page_undirty(m);
1625 			vm_page_flag_clear(m, PG_ZERO);
1626 
1627 			/*
1628 			 * We have to wake specifically requested pages
1629 			 * up too because we cleared PG_SWAPINPROG and
1630 			 * could be waiting for it in getpages.  However,
1631 			 * be sure to not unbusy getpages specifically
1632 			 * requested page - getpages expects it to be
1633 			 * left busy.
1634 			 */
1635 			if (i != bp->b_pager.pg_reqpage) {
1636 				vm_page_deactivate(m);
1637 				vm_page_wakeup(m);
1638 			} else {
1639 				vm_page_flash(m);
1640 			}
1641 		} else {
1642 			/*
1643 			 * For write success, clear the modify and dirty
1644 			 * status, then finish the I/O ( which decrements the
1645 			 * busy count and possibly wakes waiter's up ).
1646 			 */
1647 			pmap_clear_modify(m);
1648 			vm_page_undirty(m);
1649 			vm_page_io_finish(m);
1650 			if (!vm_page_count_severe() || !vm_page_try_to_cache(m))
1651 				vm_page_protect(m, VM_PROT_READ);
1652 		}
1653 	}
1654 
1655 	/*
1656 	 * adjust pip.  NOTE: the original parent may still have its own
1657 	 * pip refs on the object.
1658 	 */
1659 
1660 	if (object)
1661 		vm_object_pip_wakeupn(object, bp->b_npages);
1662 
1663 	/*
1664 	 * release the physical I/O buffer
1665 	 */
1666 
1667 	relpbuf(
1668 	    bp,
1669 	    ((bp->b_iocmd == BIO_READ) ? &nsw_rcount :
1670 		((bp->b_flags & B_ASYNC) ?
1671 		    &nsw_wcount_async :
1672 		    &nsw_wcount_sync
1673 		)
1674 	    )
1675 	);
1676 	splx(s);
1677 }
1678 
1679 /************************************************************************
1680  *				SWAP META DATA 				*
1681  ************************************************************************
1682  *
1683  *	These routines manipulate the swap metadata stored in the
1684  *	OBJT_SWAP object.  All swp_*() routines must be called at
1685  *	splvm() because swap can be freed up by the low level vm_page
1686  *	code which might be called from interrupts beyond what splbio() covers.
1687  *
1688  *	Swap metadata is implemented with a global hash and not directly
1689  *	linked into the object.  Instead the object simply contains
1690  *	appropriate tracking counters.
1691  */
1692 
1693 /*
1694  * SWP_PAGER_HASH() -	hash swap meta data
1695  *
1696  *	This is an inline helper function which hashes the swapblk given
1697  *	the object and page index.  It returns a pointer to a pointer
1698  *	to the object, or a pointer to a NULL pointer if it could not
1699  *	find a swapblk.
1700  *
1701  *	This routine must be called at splvm().
1702  */
1703 
1704 static __inline struct swblock **
1705 swp_pager_hash(vm_object_t object, vm_pindex_t index)
1706 {
1707 	struct swblock **pswap;
1708 	struct swblock *swap;
1709 
1710 	index &= ~SWAP_META_MASK;
1711 	pswap = &swhash[(index ^ (int)(intptr_t)object) & swhash_mask];
1712 
1713 	while ((swap = *pswap) != NULL) {
1714 		if (swap->swb_object == object &&
1715 		    swap->swb_index == index
1716 		) {
1717 			break;
1718 		}
1719 		pswap = &swap->swb_hnext;
1720 	}
1721 	return(pswap);
1722 }
1723 
1724 /*
1725  * SWP_PAGER_META_BUILD() -	add swap block to swap meta data for object
1726  *
1727  *	We first convert the object to a swap object if it is a default
1728  *	object.
1729  *
1730  *	The specified swapblk is added to the object's swap metadata.  If
1731  *	the swapblk is not valid, it is freed instead.  Any previously
1732  *	assigned swapblk is freed.
1733  *
1734  *	This routine must be called at splvm(), except when used to convert
1735  *	an OBJT_DEFAULT object into an OBJT_SWAP object.
1736 
1737  */
1738 
1739 static void
1740 swp_pager_meta_build(
1741 	vm_object_t object,
1742 	vm_pindex_t index,
1743 	daddr_t swapblk
1744 ) {
1745 	struct swblock *swap;
1746 	struct swblock **pswap;
1747 
1748 	/*
1749 	 * Convert default object to swap object if necessary
1750 	 */
1751 
1752 	if (object->type != OBJT_SWAP) {
1753 		object->type = OBJT_SWAP;
1754 		object->un_pager.swp.swp_bcount = 0;
1755 
1756 		if (object->handle != NULL) {
1757 			TAILQ_INSERT_TAIL(
1758 			    NOBJLIST(object->handle),
1759 			    object,
1760 			    pager_object_list
1761 			);
1762 		} else {
1763 			TAILQ_INSERT_TAIL(
1764 			    &swap_pager_un_object_list,
1765 			    object,
1766 			    pager_object_list
1767 			);
1768 		}
1769 	}
1770 
1771 	/*
1772 	 * Locate hash entry.  If not found create, but if we aren't adding
1773 	 * anything just return.  If we run out of space in the map we wait
1774 	 * and, since the hash table may have changed, retry.
1775 	 */
1776 
1777 retry:
1778 	pswap = swp_pager_hash(object, index);
1779 
1780 	if ((swap = *pswap) == NULL) {
1781 		int i;
1782 
1783 		if (swapblk == SWAPBLK_NONE)
1784 			return;
1785 
1786 		swap = *pswap = zalloc(swap_zone);
1787 		if (swap == NULL) {
1788 			VM_WAIT;
1789 			goto retry;
1790 		}
1791 		swap->swb_hnext = NULL;
1792 		swap->swb_object = object;
1793 		swap->swb_index = index & ~SWAP_META_MASK;
1794 		swap->swb_count = 0;
1795 
1796 		++object->un_pager.swp.swp_bcount;
1797 
1798 		for (i = 0; i < SWAP_META_PAGES; ++i)
1799 			swap->swb_pages[i] = SWAPBLK_NONE;
1800 	}
1801 
1802 	/*
1803 	 * Delete prior contents of metadata
1804 	 */
1805 
1806 	index &= SWAP_META_MASK;
1807 
1808 	if (swap->swb_pages[index] != SWAPBLK_NONE) {
1809 		swp_pager_freeswapspace(swap->swb_pages[index], 1);
1810 		--swap->swb_count;
1811 	}
1812 
1813 	/*
1814 	 * Enter block into metadata
1815 	 */
1816 
1817 	swap->swb_pages[index] = swapblk;
1818 	if (swapblk != SWAPBLK_NONE)
1819 		++swap->swb_count;
1820 }
1821 
1822 /*
1823  * SWP_PAGER_META_FREE() - free a range of blocks in the object's swap metadata
1824  *
1825  *	The requested range of blocks is freed, with any associated swap
1826  *	returned to the swap bitmap.
1827  *
1828  *	This routine will free swap metadata structures as they are cleaned
1829  *	out.  This routine does *NOT* operate on swap metadata associated
1830  *	with resident pages.
1831  *
1832  *	This routine must be called at splvm()
1833  */
1834 
1835 static void
1836 swp_pager_meta_free(vm_object_t object, vm_pindex_t index, daddr_t count)
1837 {
1838 	if (object->type != OBJT_SWAP)
1839 		return;
1840 
1841 	while (count > 0) {
1842 		struct swblock **pswap;
1843 		struct swblock *swap;
1844 
1845 		pswap = swp_pager_hash(object, index);
1846 
1847 		if ((swap = *pswap) != NULL) {
1848 			daddr_t v = swap->swb_pages[index & SWAP_META_MASK];
1849 
1850 			if (v != SWAPBLK_NONE) {
1851 				swp_pager_freeswapspace(v, 1);
1852 				swap->swb_pages[index & SWAP_META_MASK] =
1853 					SWAPBLK_NONE;
1854 				if (--swap->swb_count == 0) {
1855 					*pswap = swap->swb_hnext;
1856 					zfree(swap_zone, swap);
1857 					--object->un_pager.swp.swp_bcount;
1858 				}
1859 			}
1860 			--count;
1861 			++index;
1862 		} else {
1863 			int n = SWAP_META_PAGES - (index & SWAP_META_MASK);
1864 			count -= n;
1865 			index += n;
1866 		}
1867 	}
1868 }
1869 
1870 /*
1871  * SWP_PAGER_META_FREE_ALL() - destroy all swap metadata associated with object
1872  *
1873  *	This routine locates and destroys all swap metadata associated with
1874  *	an object.
1875  *
1876  *	This routine must be called at splvm()
1877  */
1878 
1879 static void
1880 swp_pager_meta_free_all(vm_object_t object)
1881 {
1882 	daddr_t index = 0;
1883 
1884 	if (object->type != OBJT_SWAP)
1885 		return;
1886 
1887 	while (object->un_pager.swp.swp_bcount) {
1888 		struct swblock **pswap;
1889 		struct swblock *swap;
1890 
1891 		pswap = swp_pager_hash(object, index);
1892 		if ((swap = *pswap) != NULL) {
1893 			int i;
1894 
1895 			for (i = 0; i < SWAP_META_PAGES; ++i) {
1896 				daddr_t v = swap->swb_pages[i];
1897 				if (v != SWAPBLK_NONE) {
1898 					--swap->swb_count;
1899 					swp_pager_freeswapspace(v, 1);
1900 				}
1901 			}
1902 			if (swap->swb_count != 0)
1903 				panic("swap_pager_meta_free_all: swb_count != 0");
1904 			*pswap = swap->swb_hnext;
1905 			zfree(swap_zone, swap);
1906 			--object->un_pager.swp.swp_bcount;
1907 		}
1908 		index += SWAP_META_PAGES;
1909 		if (index > 0x20000000)
1910 			panic("swp_pager_meta_free_all: failed to locate all swap meta blocks");
1911 	}
1912 }
1913 
1914 /*
1915  * SWP_PAGER_METACTL() -  misc control of swap and vm_page_t meta data.
1916  *
1917  *	This routine is capable of looking up, popping, or freeing
1918  *	swapblk assignments in the swap meta data or in the vm_page_t.
1919  *	The routine typically returns the swapblk being looked-up, or popped,
1920  *	or SWAPBLK_NONE if the block was freed, or SWAPBLK_NONE if the block
1921  *	was invalid.  This routine will automatically free any invalid
1922  *	meta-data swapblks.
1923  *
1924  *	It is not possible to store invalid swapblks in the swap meta data
1925  *	(other then a literal 'SWAPBLK_NONE'), so we don't bother checking.
1926  *
1927  *	When acting on a busy resident page and paging is in progress, we
1928  *	have to wait until paging is complete but otherwise can act on the
1929  *	busy page.
1930  *
1931  *	This routine must be called at splvm().
1932  *
1933  *	SWM_FREE	remove and free swap block from metadata
1934  *	SWM_POP		remove from meta data but do not free.. pop it out
1935  */
1936 
1937 static daddr_t
1938 swp_pager_meta_ctl(
1939 	vm_object_t object,
1940 	vm_pindex_t index,
1941 	int flags
1942 ) {
1943 	struct swblock **pswap;
1944 	struct swblock *swap;
1945 	daddr_t r1;
1946 
1947 	/*
1948 	 * The meta data only exists of the object is OBJT_SWAP
1949 	 * and even then might not be allocated yet.
1950 	 */
1951 
1952 	if (object->type != OBJT_SWAP)
1953 		return(SWAPBLK_NONE);
1954 
1955 	r1 = SWAPBLK_NONE;
1956 	pswap = swp_pager_hash(object, index);
1957 
1958 	if ((swap = *pswap) != NULL) {
1959 		index &= SWAP_META_MASK;
1960 		r1 = swap->swb_pages[index];
1961 
1962 		if (r1 != SWAPBLK_NONE) {
1963 			if (flags & SWM_FREE) {
1964 				swp_pager_freeswapspace(r1, 1);
1965 				r1 = SWAPBLK_NONE;
1966 			}
1967 			if (flags & (SWM_FREE|SWM_POP)) {
1968 				swap->swb_pages[index] = SWAPBLK_NONE;
1969 				if (--swap->swb_count == 0) {
1970 					*pswap = swap->swb_hnext;
1971 					zfree(swap_zone, swap);
1972 					--object->un_pager.swp.swp_bcount;
1973 				}
1974 			}
1975 		}
1976 	}
1977 	return(r1);
1978 }
1979 
1980 /********************************************************
1981  *		CHAINING FUNCTIONS			*
1982  ********************************************************
1983  *
1984  *	These functions support recursion of I/O operations
1985  *	on bp's, typically by chaining one or more 'child' bp's
1986  *	to the parent.  Synchronous, asynchronous, and semi-synchronous
1987  *	chaining is possible.
1988  */
1989 
1990 /*
1991  *	vm_pager_chain_iodone:
1992  *
1993  *	io completion routine for child bp.  Currently we fudge a bit
1994  *	on dealing with b_resid.   Since users of these routines may issue
1995  *	multiple children simultaneously, sequencing of the error can be lost.
1996  */
1997 
1998 static void
1999 vm_pager_chain_iodone(struct buf *nbp)
2000 {
2001 	struct bio *bp;
2002 	u_int *count;
2003 
2004 	bp = nbp->b_caller1;
2005 	count = (u_int *)&(bp->bio_caller1);
2006 	if (bp != NULL) {
2007 		if (nbp->b_ioflags & BIO_ERROR) {
2008 			bp->bio_flags |= BIO_ERROR;
2009 			bp->bio_error = nbp->b_error;
2010 		} else if (nbp->b_resid != 0) {
2011 			bp->bio_flags |= BIO_ERROR;
2012 			bp->bio_error = EINVAL;
2013 		} else {
2014 			bp->bio_resid -= nbp->b_bcount;
2015 		}
2016 		nbp->b_caller1 = NULL;
2017 		--(*count);
2018 		if (bp->bio_flags & BIO_FLAG1) {
2019 			bp->bio_flags &= ~BIO_FLAG1;
2020 			wakeup(bp);
2021 		}
2022 	}
2023 	nbp->b_flags |= B_DONE;
2024 	nbp->b_flags &= ~B_ASYNC;
2025 	relpbuf(nbp, NULL);
2026 }
2027 
2028 /*
2029  *	getchainbuf:
2030  *
2031  *	Obtain a physical buffer and chain it to its parent buffer.  When
2032  *	I/O completes, the parent buffer will be B_SIGNAL'd.  Errors are
2033  *	automatically propagated to the parent
2034  */
2035 
2036 struct buf *
2037 getchainbuf(struct bio *bp, struct vnode *vp, int flags)
2038 {
2039 	struct buf *nbp = getpbuf(NULL);
2040 	u_int *count = (u_int *)&(bp->bio_caller1);
2041 
2042 	nbp->b_caller1 = bp;
2043 	++(*count);
2044 
2045 	if (*count > 4)
2046 		waitchainbuf(bp, 4, 0);
2047 
2048 	nbp->b_iocmd = bp->bio_cmd;
2049 	nbp->b_ioflags = bp->bio_flags & BIO_ORDERED;
2050 	nbp->b_flags = flags;
2051 	nbp->b_rcred = nbp->b_wcred = proc0.p_ucred;
2052 	nbp->b_iodone = vm_pager_chain_iodone;
2053 
2054 	crhold(nbp->b_rcred);
2055 	crhold(nbp->b_wcred);
2056 
2057 	if (vp)
2058 		pbgetvp(vp, nbp);
2059 	return(nbp);
2060 }
2061 
2062 void
2063 flushchainbuf(struct buf *nbp)
2064 {
2065 	if (nbp->b_bcount) {
2066 		nbp->b_bufsize = nbp->b_bcount;
2067 		if (nbp->b_iocmd == BIO_WRITE)
2068 			nbp->b_dirtyend = nbp->b_bcount;
2069 		BUF_KERNPROC(nbp);
2070 		BUF_STRATEGY(nbp);
2071 	} else {
2072 		bufdone(nbp);
2073 	}
2074 }
2075 
2076 void
2077 waitchainbuf(struct bio *bp, int limit, int done)
2078 {
2079  	int s;
2080 	u_int *count = (u_int *)&(bp->bio_caller1);
2081 
2082 	s = splbio();
2083 	while (*count > limit) {
2084 		bp->bio_flags |= BIO_FLAG1;
2085 		tsleep(bp, PRIBIO + 4, "bpchain", 0);
2086 	}
2087 	if (done) {
2088 		if (bp->bio_resid != 0 && !(bp->bio_flags & BIO_ERROR)) {
2089 			bp->bio_flags |= BIO_ERROR;
2090 			bp->bio_error = EINVAL;
2091 		}
2092 		biodone(bp);
2093 	}
2094 	splx(s);
2095 }
2096 
2097