vnode_pager.c revision 12914
1/*
2 * Copyright (c) 1990 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
4 * All rights reserved.
5 * Copyright (c) 1993, 1994 John S. Dyson
6 * Copyright (c) 1995, David Greenman
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 *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
41 *	$Id: vnode_pager.c,v 1.56 1995/12/14 09:55:14 phk Exp $
42 */
43
44/*
45 * Page to/from files (vnodes).
46 */
47
48/*
49 * TODO:
50 *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
51 *	greatly re-simplify the vnode_pager.
52 */
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/kernel.h>
57#include <sys/proc.h>
58#include <sys/malloc.h>
59#include <sys/vnode.h>
60#include <sys/uio.h>
61#include <sys/mount.h>
62#include <sys/buf.h>
63#include <sys/vmmeter.h>
64
65#include <vm/vm.h>
66#include <vm/vm_param.h>
67#include <vm/vm_prot.h>
68#include <vm/vm_object.h>
69#include <vm/vm_page.h>
70#include <vm/vm_pager.h>
71#include <vm/vnode_pager.h>
72#include <vm/vm_extern.h>
73
74static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
75					 int *run));
76static void vnode_pager_iodone __P((struct buf *bp));
77static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
78static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
79static void vnode_pager_dealloc __P((vm_object_t));
80static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
81static int vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
82static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
83
84struct pagerops vnodepagerops = {
85	NULL,
86	vnode_pager_alloc,
87	vnode_pager_dealloc,
88	vnode_pager_getpages,
89	vnode_pager_putpages,
90	vnode_pager_haspage,
91	NULL
92};
93
94static int vnode_pager_leaf_getpages __P((vm_object_t object, vm_page_t *m,
95					  int count, int reqpage));
96static int vnode_pager_leaf_putpages __P((vm_object_t object, vm_page_t *m,
97					  int count, boolean_t sync,
98					  int *rtvals));
99
100/*
101 * Allocate (or lookup) pager for a vnode.
102 * Handle is a vnode pointer.
103 */
104vm_object_t
105vnode_pager_alloc(handle, size, prot, offset)
106	void *handle;
107	vm_size_t size;
108	vm_prot_t prot;
109	vm_ooffset_t offset;
110{
111	vm_object_t object;
112	struct vnode *vp;
113
114	/*
115	 * Pageout to vnode, no can do yet.
116	 */
117	if (handle == NULL)
118		return (NULL);
119
120	vp = (struct vnode *) handle;
121
122	/*
123	 * Prevent race condition when allocating the object. This
124	 * can happen with NFS vnodes since the nfsnode isn't locked.
125	 */
126	while (vp->v_flag & VOLOCK) {
127		vp->v_flag |= VOWANT;
128		tsleep(vp, PVM, "vnpobj", 0);
129	}
130	vp->v_flag |= VOLOCK;
131
132	/*
133	 * If the object is being terminated, wait for it to
134	 * go away.
135	 */
136	while (((object = vp->v_object) != NULL) && (object->flags & OBJ_DEAD)) {
137		tsleep(object, PVM, "vadead", 0);
138	}
139
140	if (object == NULL) {
141		/*
142		 * And an object of the appropriate size
143		 */
144		object = vm_object_allocate(OBJT_VNODE, size);
145		object->flags = OBJ_CANPERSIST;
146
147		/*
148		 * Hold a reference to the vnode and initialize object data.
149		 */
150		VREF(vp);
151		object->un_pager.vnp.vnp_size = (vm_ooffset_t) size * PAGE_SIZE;
152
153		object->handle = handle;
154		vp->v_object = object;
155	} else {
156		/*
157		 * vm_object_reference() will remove the object from the cache if
158		 * found and gain a reference to the object.
159		 */
160		vm_object_reference(object);
161	}
162
163	if (vp->v_type == VREG)
164		vp->v_flag |= VVMIO;
165
166	vp->v_flag &= ~VOLOCK;
167	if (vp->v_flag & VOWANT) {
168		vp->v_flag &= ~VOWANT;
169		wakeup(vp);
170	}
171	return (object);
172}
173
174static void
175vnode_pager_dealloc(object)
176	vm_object_t object;
177{
178	register struct vnode *vp = object->handle;
179
180	if (vp == NULL)
181		panic("vnode_pager_dealloc: pager already dealloced");
182
183	if (object->paging_in_progress) {
184		int s = splbio();
185		while (object->paging_in_progress) {
186			object->flags |= OBJ_PIPWNT;
187			tsleep(object, PVM, "vnpdea", 0);
188		}
189		splx(s);
190	}
191
192	object->handle = NULL;
193
194	vp->v_object = NULL;
195	vp->v_flag &= ~(VTEXT | VVMIO);
196	vp->v_flag |= VAGE;
197	vrele(vp);
198}
199
200static boolean_t
201vnode_pager_haspage(object, pindex, before, after)
202	vm_object_t object;
203	vm_pindex_t pindex;
204	int *before;
205	int *after;
206{
207	struct vnode *vp = object->handle;
208	daddr_t bn;
209	int err;
210	daddr_t reqblock;
211	int poff;
212	int bsize;
213	int pagesperblock, blocksperpage;
214
215	/*
216	 * If filesystem no longer mounted or offset beyond end of file we do
217	 * not have the page.
218	 */
219	if ((vp->v_mount == NULL) ||
220		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
221		return FALSE;
222
223	bsize = vp->v_mount->mnt_stat.f_iosize;
224	pagesperblock = bsize / PAGE_SIZE;
225	blocksperpage = 0;
226	if (pagesperblock > 0) {
227		reqblock = pindex / pagesperblock;
228	} else {
229		blocksperpage = (PAGE_SIZE / bsize);
230		reqblock = pindex * blocksperpage;
231	}
232	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
233		after, before);
234	if (err)
235		return TRUE;
236	if ( bn == -1)
237		return FALSE;
238	if (pagesperblock > 0) {
239		poff = pindex - (reqblock * pagesperblock);
240		if (before) {
241			*before *= pagesperblock;
242			*before += poff;
243		}
244		if (after) {
245			int numafter;
246			*after *= pagesperblock;
247			numafter = pagesperblock - (poff + 1);
248			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
249				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
250			}
251			*after += numafter;
252		}
253	} else {
254		if (before) {
255			*before /= blocksperpage;
256		}
257
258		if (after) {
259			*after /= blocksperpage;
260		}
261	}
262	return TRUE;
263}
264
265/*
266 * Lets the VM system know about a change in size for a file.
267 * We adjust our own internal size and flush any cached pages in
268 * the associated object that are affected by the size change.
269 *
270 * Note: this routine may be invoked as a result of a pager put
271 * operation (possibly at object termination time), so we must be careful.
272 */
273void
274vnode_pager_setsize(vp, nsize)
275	struct vnode *vp;
276	vm_ooffset_t nsize;
277{
278	vm_object_t object = vp->v_object;
279
280	if (object == NULL)
281		return;
282
283	/*
284	 * Hasn't changed size
285	 */
286	if (nsize == object->un_pager.vnp.vnp_size)
287		return;
288
289	/*
290	 * File has shrunk. Toss any cached pages beyond the new EOF.
291	 */
292	if (nsize < object->un_pager.vnp.vnp_size) {
293		vm_ooffset_t nsizerounded;
294		nsizerounded = IDX_TO_OFF(OFF_TO_IDX(nsize + PAGE_SIZE - 1));
295		if (nsizerounded < object->un_pager.vnp.vnp_size) {
296			vm_object_page_remove(object,
297				OFF_TO_IDX(nsize + PAGE_SIZE - 1),
298				OFF_TO_IDX(object->un_pager.vnp.vnp_size),
299				FALSE);
300		}
301		/*
302		 * this gets rid of garbage at the end of a page that is now
303		 * only partially backed by the vnode...
304		 */
305		if (nsize & PAGE_MASK) {
306			vm_offset_t kva;
307			vm_page_t m;
308
309			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
310			if (m) {
311				kva = vm_pager_map_page(m);
312				bzero((caddr_t) kva + (nsize & PAGE_MASK),
313				    (int) (round_page(nsize) - nsize));
314				vm_pager_unmap_page(kva);
315			}
316		}
317	}
318	object->un_pager.vnp.vnp_size = nsize;
319	object->size = OFF_TO_IDX(nsize + PAGE_SIZE - 1);
320}
321
322void
323vnode_pager_umount(mp)
324	register struct mount *mp;
325{
326	struct vnode *vp, *nvp;
327
328loop:
329	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
330		/*
331		 * Vnode can be reclaimed by getnewvnode() while we
332		 * traverse the list.
333		 */
334		if (vp->v_mount != mp)
335			goto loop;
336
337		/*
338		 * Save the next pointer now since uncaching may terminate the
339		 * object and render vnode invalid
340		 */
341		nvp = vp->v_mntvnodes.le_next;
342
343		if (vp->v_object != NULL) {
344			VOP_LOCK(vp);
345			vnode_pager_uncache(vp);
346			VOP_UNLOCK(vp);
347		}
348	}
349}
350
351/*
352 * Remove vnode associated object from the object cache.
353 * This routine must be called with the vnode locked.
354 *
355 * XXX unlock the vnode.
356 * We must do this since uncaching the object may result in its
357 * destruction which may initiate paging activity which may necessitate
358 * re-locking the vnode.
359 */
360void
361vnode_pager_uncache(vp)
362	struct vnode *vp;
363{
364	vm_object_t object;
365
366	/*
367	 * Not a mapped vnode
368	 */
369	object = vp->v_object;
370	if (object == NULL)
371		return;
372
373	vm_object_reference(object);
374	VOP_UNLOCK(vp);
375	pager_cache(object, FALSE);
376	VOP_LOCK(vp);
377	return;
378}
379
380
381void
382vnode_pager_freepage(m)
383	vm_page_t m;
384{
385	PAGE_WAKEUP(m);
386	vm_page_free(m);
387}
388
389/*
390 * calculate the linear (byte) disk address of specified virtual
391 * file address
392 */
393static vm_offset_t
394vnode_pager_addr(vp, address, run)
395	struct vnode *vp;
396	vm_ooffset_t address;
397	int *run;
398{
399	int rtaddress;
400	int bsize;
401	daddr_t block;
402	struct vnode *rtvp;
403	int err;
404	daddr_t vblock;
405	int voffset;
406
407	if ((int) address < 0)
408		return -1;
409
410	if (vp->v_mount == NULL)
411		return -1;
412
413	bsize = vp->v_mount->mnt_stat.f_iosize;
414	vblock = address / bsize;
415	voffset = address % bsize;
416
417	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
418
419	if (err || (block == -1))
420		rtaddress = -1;
421	else {
422		rtaddress = block + voffset / DEV_BSIZE;
423		if( run) {
424			*run += 1;
425			*run *= bsize/PAGE_SIZE;
426			*run -= voffset/PAGE_SIZE;
427		}
428	}
429
430	return rtaddress;
431}
432
433/*
434 * interrupt routine for I/O completion
435 */
436static void
437vnode_pager_iodone(bp)
438	struct buf *bp;
439{
440	bp->b_flags |= B_DONE;
441	wakeup(bp);
442}
443
444/*
445 * small block file system vnode pager input
446 */
447static int
448vnode_pager_input_smlfs(object, m)
449	vm_object_t object;
450	vm_page_t m;
451{
452	int i;
453	int s;
454	struct vnode *dp, *vp;
455	struct buf *bp;
456	vm_offset_t kva;
457	int fileaddr;
458	vm_offset_t bsize;
459	int error = 0;
460
461	vp = object->handle;
462	if (vp->v_mount == NULL)
463		return VM_PAGER_BAD;
464
465	bsize = vp->v_mount->mnt_stat.f_iosize;
466
467
468	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
469
470	kva = vm_pager_map_page(m);
471
472	for (i = 0; i < PAGE_SIZE / bsize; i++) {
473
474		if ((vm_page_bits(IDX_TO_OFF(m->pindex) + i * bsize, bsize) & m->valid))
475			continue;
476
477		fileaddr = vnode_pager_addr(vp,
478			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
479		if (fileaddr != -1) {
480			bp = getpbuf();
481
482			/* build a minimal buffer header */
483			bp->b_flags = B_BUSY | B_READ | B_CALL;
484			bp->b_iodone = vnode_pager_iodone;
485			bp->b_proc = curproc;
486			bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
487			if (bp->b_rcred != NOCRED)
488				crhold(bp->b_rcred);
489			if (bp->b_wcred != NOCRED)
490				crhold(bp->b_wcred);
491			bp->b_un.b_addr = (caddr_t) kva + i * bsize;
492			bp->b_blkno = fileaddr;
493			pbgetvp(dp, bp);
494			bp->b_bcount = bsize;
495			bp->b_bufsize = bsize;
496
497			/* do the input */
498			VOP_STRATEGY(bp);
499
500			/* we definitely need to be at splbio here */
501
502			s = splbio();
503			while ((bp->b_flags & B_DONE) == 0) {
504				tsleep(bp, PVM, "vnsrd", 0);
505			}
506			splx(s);
507			if ((bp->b_flags & B_ERROR) != 0)
508				error = EIO;
509
510			/*
511			 * free the buffer header back to the swap buffer pool
512			 */
513			relpbuf(bp);
514			if (error)
515				break;
516
517			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
518		} else {
519			vm_page_set_validclean(m, (i * bsize) & (PAGE_SIZE-1), bsize);
520			bzero((caddr_t) kva + i * bsize, bsize);
521		}
522	}
523	vm_pager_unmap_page(kva);
524	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
525	m->flags &= ~PG_ZERO;
526	if (error) {
527		return VM_PAGER_ERROR;
528	}
529	return VM_PAGER_OK;
530
531}
532
533
534/*
535 * old style vnode pager output routine
536 */
537static int
538vnode_pager_input_old(object, m)
539	vm_object_t object;
540	vm_page_t m;
541{
542	struct uio auio;
543	struct iovec aiov;
544	int error;
545	int size;
546	vm_offset_t kva;
547
548	error = 0;
549
550	/*
551	 * Return failure if beyond current EOF
552	 */
553	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
554		return VM_PAGER_BAD;
555	} else {
556		size = PAGE_SIZE;
557		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
558			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
559
560		/*
561		 * Allocate a kernel virtual address and initialize so that
562		 * we can use VOP_READ/WRITE routines.
563		 */
564		kva = vm_pager_map_page(m);
565
566		aiov.iov_base = (caddr_t) kva;
567		aiov.iov_len = size;
568		auio.uio_iov = &aiov;
569		auio.uio_iovcnt = 1;
570		auio.uio_offset = IDX_TO_OFF(m->pindex);
571		auio.uio_segflg = UIO_SYSSPACE;
572		auio.uio_rw = UIO_READ;
573		auio.uio_resid = size;
574		auio.uio_procp = (struct proc *) 0;
575
576		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
577		if (!error) {
578			register int count = size - auio.uio_resid;
579
580			if (count == 0)
581				error = EINVAL;
582			else if (count != PAGE_SIZE)
583				bzero((caddr_t) kva + count, PAGE_SIZE - count);
584		}
585		vm_pager_unmap_page(kva);
586	}
587	pmap_clear_modify(VM_PAGE_TO_PHYS(m));
588	m->dirty = 0;
589	m->flags &= ~PG_ZERO;
590	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
591}
592
593/*
594 * generic vnode pager input routine
595 */
596
597static int
598vnode_pager_getpages(object, m, count, reqpage)
599	vm_object_t object;
600	vm_page_t *m;
601	int count;
602	int reqpage;
603{
604	int rtval;
605	struct vnode *vp;
606	vp = object->handle;
607	rtval = VOP_GETPAGES(vp, m, count*PAGE_SIZE, reqpage, 0);
608	if (rtval == EOPNOTSUPP)
609		return vnode_pager_leaf_getpages(object, m, count, reqpage);
610	else
611		return rtval;
612}
613
614static int
615vnode_pager_leaf_getpages(object, m, count, reqpage)
616	vm_object_t object;
617	vm_page_t *m;
618	int count;
619	int reqpage;
620{
621	vm_offset_t kva;
622	off_t foff;
623	int i, size, bsize, first, firstaddr;
624	struct vnode *dp, *vp;
625	int runpg;
626	int runend;
627	struct buf *bp;
628	int s;
629	int error = 0;
630
631	vp = object->handle;
632	if (vp->v_mount == NULL)
633		return VM_PAGER_BAD;
634
635	bsize = vp->v_mount->mnt_stat.f_iosize;
636
637	/* get the UNDERLYING device for the file with VOP_BMAP() */
638
639	/*
640	 * originally, we did not check for an error return value -- assuming
641	 * an fs always has a bmap entry point -- that assumption is wrong!!!
642	 */
643	foff = IDX_TO_OFF(m[reqpage]->pindex);
644
645	/*
646	 * if we can't bmap, use old VOP code
647	 */
648	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
649		for (i = 0; i < count; i++) {
650			if (i != reqpage) {
651				vnode_pager_freepage(m[i]);
652			}
653		}
654		cnt.v_vnodein++;
655		cnt.v_vnodepgsin++;
656		return vnode_pager_input_old(object, m[reqpage]);
657
658		/*
659		 * if the blocksize is smaller than a page size, then use
660		 * special small filesystem code.  NFS sometimes has a small
661		 * blocksize, but it can handle large reads itself.
662		 */
663	} else if ((PAGE_SIZE / bsize) > 1 &&
664	    (vp->v_mount->mnt_stat.f_type != MOUNT_NFS)) {
665
666		for (i = 0; i < count; i++) {
667			if (i != reqpage) {
668				vnode_pager_freepage(m[i]);
669			}
670		}
671		cnt.v_vnodein++;
672		cnt.v_vnodepgsin++;
673		return vnode_pager_input_smlfs(object, m[reqpage]);
674	}
675	/*
676	 * if ANY DEV_BSIZE blocks are valid on a large filesystem block
677	 * then, the entire page is valid --
678	 */
679	if (m[reqpage]->valid) {
680		m[reqpage]->valid = VM_PAGE_BITS_ALL;
681		for (i = 0; i < count; i++) {
682			if (i != reqpage)
683				vnode_pager_freepage(m[i]);
684		}
685		return VM_PAGER_OK;
686	}
687
688	/*
689	 * here on direct device I/O
690	 */
691
692	firstaddr = -1;
693	/*
694	 * calculate the run that includes the required page
695	 */
696	for(first = 0, i = 0; i < count; i = runend) {
697		firstaddr = vnode_pager_addr(vp,
698			IDX_TO_OFF(m[i]->pindex), &runpg);
699		if (firstaddr == -1) {
700			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
701				panic("vnode_pager_putpages: unexpected missing page: firstaddr: %d, foff: %ld, vnp_size: %d",
702			   	 firstaddr, foff, object->un_pager.vnp.vnp_size);
703			}
704			vnode_pager_freepage(m[i]);
705			runend = i + 1;
706			first = runend;
707			continue;
708		}
709		runend = i + runpg;
710		if (runend <= reqpage) {
711			int j;
712			for (j = i; j < runend; j++) {
713				vnode_pager_freepage(m[j]);
714			}
715		} else {
716			if (runpg < (count - first)) {
717				for (i = first + runpg; i < count; i++)
718					vnode_pager_freepage(m[i]);
719				count = first + runpg;
720			}
721			break;
722		}
723		first = runend;
724	}
725
726	/*
727	 * the first and last page have been calculated now, move input pages
728	 * to be zero based...
729	 */
730	if (first != 0) {
731		for (i = first; i < count; i++) {
732			m[i - first] = m[i];
733		}
734		count -= first;
735		reqpage -= first;
736	}
737
738	/*
739	 * calculate the file virtual address for the transfer
740	 */
741	foff = IDX_TO_OFF(m[0]->pindex);
742
743	/*
744	 * calculate the size of the transfer
745	 */
746	size = count * PAGE_SIZE;
747	if ((foff + size) > object->un_pager.vnp.vnp_size)
748		size = object->un_pager.vnp.vnp_size - foff;
749
750	/*
751	 * round up physical size for real devices
752	 */
753	if (dp->v_type == VBLK || dp->v_type == VCHR)
754		size = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
755
756	bp = getpbuf();
757	kva = (vm_offset_t) bp->b_data;
758
759	/*
760	 * and map the pages to be read into the kva
761	 */
762	pmap_qenter(kva, m, count);
763
764	/* build a minimal buffer header */
765	bp->b_flags = B_BUSY | B_READ | B_CALL;
766	bp->b_iodone = vnode_pager_iodone;
767	/* B_PHYS is not set, but it is nice to fill this in */
768	bp->b_proc = curproc;
769	bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
770	if (bp->b_rcred != NOCRED)
771		crhold(bp->b_rcred);
772	if (bp->b_wcred != NOCRED)
773		crhold(bp->b_wcred);
774	bp->b_blkno = firstaddr;
775	pbgetvp(dp, bp);
776	bp->b_bcount = size;
777	bp->b_bufsize = size;
778
779	cnt.v_vnodein++;
780	cnt.v_vnodepgsin += count;
781
782	/* do the input */
783	VOP_STRATEGY(bp);
784
785	s = splbio();
786	/* we definitely need to be at splbio here */
787
788	while ((bp->b_flags & B_DONE) == 0) {
789		tsleep(bp, PVM, "vnread", 0);
790	}
791	splx(s);
792	if ((bp->b_flags & B_ERROR) != 0)
793		error = EIO;
794
795	if (!error) {
796		if (size != count * PAGE_SIZE)
797			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
798	}
799	pmap_qremove(kva, count);
800
801	/*
802	 * free the buffer header back to the swap buffer pool
803	 */
804	relpbuf(bp);
805
806	for (i = 0; i < count; i++) {
807		pmap_clear_modify(VM_PAGE_TO_PHYS(m[i]));
808		m[i]->dirty = 0;
809		m[i]->valid = VM_PAGE_BITS_ALL;
810		m[i]->flags &= ~PG_ZERO;
811		if (i != reqpage) {
812
813			/*
814			 * whether or not to leave the page activated is up in
815			 * the air, but we should put the page on a page queue
816			 * somewhere. (it already is in the object). Result:
817			 * It appears that emperical results show that
818			 * deactivating pages is best.
819			 */
820
821			/*
822			 * just in case someone was asking for this page we
823			 * now tell them that it is ok to use
824			 */
825			if (!error) {
826				vm_page_deactivate(m[i]);
827				PAGE_WAKEUP(m[i]);
828			} else {
829				vnode_pager_freepage(m[i]);
830			}
831		}
832	}
833	if (error) {
834		printf("vnode_pager_getpages: I/O read error\n");
835	}
836	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
837}
838
839static int
840vnode_pager_putpages(object, m, count, sync, rtvals)
841	vm_object_t object;
842	vm_page_t *m;
843	int count;
844	boolean_t sync;
845	int *rtvals;
846{
847	int rtval;
848	struct vnode *vp;
849	vp = object->handle;
850	rtval = VOP_PUTPAGES(vp, m, count*PAGE_SIZE, sync, rtvals, 0);
851	if (rtval == EOPNOTSUPP)
852		return vnode_pager_leaf_putpages(object, m, count, sync, rtvals);
853	else
854		return rtval;
855}
856
857/*
858 * generic vnode pager output routine
859 */
860static int
861vnode_pager_leaf_putpages(object, m, count, sync, rtvals)
862	vm_object_t object;
863	vm_page_t *m;
864	int count;
865	boolean_t sync;
866	int *rtvals;
867{
868	int i;
869
870	struct vnode *vp;
871	int maxsize, ncount;
872	vm_ooffset_t poffset;
873	struct uio auio;
874	struct iovec aiov;
875	int error;
876
877	vp = object->handle;;
878	for (i = 0; i < count; i++)
879		rtvals[i] = VM_PAGER_AGAIN;
880
881	if ((int) m[0]->pindex < 0) {
882		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%x(%x)\n", m[0]->pindex, m[0]->dirty);
883		rtvals[0] = VM_PAGER_BAD;
884		return VM_PAGER_BAD;
885	}
886
887	maxsize = count * PAGE_SIZE;
888	ncount = count;
889
890	poffset = IDX_TO_OFF(m[0]->pindex);
891	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
892		if (object->un_pager.vnp.vnp_size > poffset)
893			maxsize = object->un_pager.vnp.vnp_size - poffset;
894		else
895			maxsize = 0;
896		ncount = (maxsize + PAGE_SIZE - 1) / PAGE_SIZE;
897		if (ncount < count) {
898			for (i = ncount; i < count; i++) {
899				rtvals[i] = VM_PAGER_BAD;
900			}
901#ifdef BOGUS
902			if (ncount == 0) {
903				printf("vnode_pager_putpages: write past end of file: %d, %lu\n",
904					poffset,
905					(unsigned long) object->un_pager.vnp.vnp_size);
906				return rtvals[0];
907			}
908#endif
909		}
910	}
911
912	for (i = 0; i < count; i++) {
913		m[i]->busy++;
914		m[i]->flags &= ~PG_BUSY;
915	}
916
917	aiov.iov_base = (caddr_t) 0;
918	aiov.iov_len = maxsize;
919	auio.uio_iov = &aiov;
920	auio.uio_iovcnt = 1;
921	auio.uio_offset = poffset;
922	auio.uio_segflg = UIO_NOCOPY;
923	auio.uio_rw = UIO_WRITE;
924	auio.uio_resid = maxsize;
925	auio.uio_procp = (struct proc *) 0;
926	error = VOP_WRITE(vp, &auio, IO_VMIO|(sync?IO_SYNC:0), curproc->p_ucred);
927	cnt.v_vnodeout++;
928	cnt.v_vnodepgsout += ncount;
929
930	if (error) {
931		printf("vnode_pager_putpages: I/O error %d\n", error);
932	}
933	if (auio.uio_resid) {
934		printf("vnode_pager_putpages: residual I/O %d at %ld\n",
935			auio.uio_resid, m[0]->pindex);
936	}
937	for (i = 0; i < count; i++) {
938		m[i]->busy--;
939		if (i < ncount) {
940			rtvals[i] = VM_PAGER_OK;
941		}
942		if ((m[i]->busy == 0) && (m[i]->flags & PG_WANTED))
943			wakeup(m[i]);
944	}
945	return rtvals[0];
946}
947
948struct vnode *
949vnode_pager_lock(object)
950	vm_object_t object;
951{
952	for (; object != NULL; object = object->backing_object) {
953		if (object->type != OBJT_VNODE)
954			continue;
955
956		VOP_LOCK(object->handle);
957		return object->handle;
958	}
959	return NULL;
960}
961