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