vnode_pager.c revision 100832
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1990 University of Utah.
31549Srgrimes * Copyright (c) 1991 The Regents of the University of California.
41549Srgrimes * All rights reserved.
59507Sdg * Copyright (c) 1993, 1994 John S. Dyson
69507Sdg * Copyright (c) 1995, David Greenman
71541Srgrimes *
81541Srgrimes * This code is derived from software contributed to Berkeley by
91541Srgrimes * the Systems Programming Group of the University of Utah Computer
101541Srgrimes * Science Department.
111541Srgrimes *
121541Srgrimes * Redistribution and use in source and binary forms, with or without
131541Srgrimes * modification, are permitted provided that the following conditions
141541Srgrimes * are met:
151541Srgrimes * 1. Redistributions of source code must retain the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer.
171541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
181541Srgrimes *    notice, this list of conditions and the following disclaimer in the
191541Srgrimes *    documentation and/or other materials provided with the distribution.
201541Srgrimes * 3. All advertising materials mentioning features or use of this software
2158705Scharnier *    must display the following acknowledgement:
221541Srgrimes *	This product includes software developed by the University of
231541Srgrimes *	California, Berkeley and its contributors.
241541Srgrimes * 4. Neither the name of the University nor the names of its contributors
251541Srgrimes *    may be used to endorse or promote products derived from this software
261541Srgrimes *    without specific prior written permission.
271541Srgrimes *
281541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
291541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
301541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
311541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
321541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
331541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
341541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
361541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
371541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381541Srgrimes * SUCH DAMAGE.
391541Srgrimes *
401549Srgrimes *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
4150477Speter * $FreeBSD: head/sys/vm/vnode_pager.c 100832 2002-07-28 20:13:48Z alc $
421541Srgrimes */
431541Srgrimes
441541Srgrimes/*
451541Srgrimes * Page to/from files (vnodes).
461541Srgrimes */
471541Srgrimes
481549Srgrimes/*
491549Srgrimes * TODO:
509507Sdg *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
517695Sdg *	greatly re-simplify the vnode_pager.
521549Srgrimes */
531549Srgrimes
541541Srgrimes#include <sys/param.h>
551541Srgrimes#include <sys/systm.h>
561541Srgrimes#include <sys/proc.h>
571541Srgrimes#include <sys/vnode.h>
581541Srgrimes#include <sys/mount.h>
5960041Sphk#include <sys/bio.h>
609507Sdg#include <sys/buf.h>
6112662Sdg#include <sys/vmmeter.h>
6251340Sdillon#include <sys/conf.h>
631541Srgrimes
641541Srgrimes#include <vm/vm.h>
6512662Sdg#include <vm/vm_object.h>
661541Srgrimes#include <vm/vm_page.h>
679507Sdg#include <vm/vm_pager.h>
6831853Sdyson#include <vm/vm_map.h>
691541Srgrimes#include <vm/vnode_pager.h>
7012662Sdg#include <vm/vm_extern.h>
711541Srgrimes
7292727Salfredstatic void vnode_pager_init(void);
7392727Salfredstatic vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
7492727Salfred					 int *run);
7592727Salfredstatic void vnode_pager_iodone(struct buf *bp);
7692727Salfredstatic int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
7792727Salfredstatic int vnode_pager_input_old(vm_object_t object, vm_page_t m);
7892727Salfredstatic void vnode_pager_dealloc(vm_object_t);
7992727Salfredstatic int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
8092727Salfredstatic void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
8192727Salfredstatic boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
8211943Sbde
831541Srgrimesstruct pagerops vnodepagerops = {
8479127Sjhb	vnode_pager_init,
851541Srgrimes	vnode_pager_alloc,
861541Srgrimes	vnode_pager_dealloc,
879507Sdg	vnode_pager_getpages,
889507Sdg	vnode_pager_putpages,
899507Sdg	vnode_pager_haspage,
909507Sdg	NULL
911541Srgrimes};
921541Srgrimes
9379127Sjhbint vnode_pbuf_freecnt;
9410556Sdyson
9579127Sjhbvoid
9679127Sjhbvnode_pager_init(void)
9779127Sjhb{
9842957Sdillon
9979127Sjhb	vnode_pbuf_freecnt = nswbuf / 2 + 1;
10079127Sjhb}
10179127Sjhb
1021541Srgrimes/*
1031541Srgrimes * Allocate (or lookup) pager for a vnode.
1041541Srgrimes * Handle is a vnode pointer.
10598604Salc *
10698604Salc * MPSAFE
1071541Srgrimes */
1089507Sdgvm_object_t
10940286Sdgvnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
11028751Sbde		  vm_ooffset_t offset)
1111541Srgrimes{
1129456Sdg	vm_object_t object;
1131541Srgrimes	struct vnode *vp;
1141541Srgrimes
1151541Srgrimes	/*
1161541Srgrimes	 * Pageout to vnode, no can do yet.
1171541Srgrimes	 */
1181541Srgrimes	if (handle == NULL)
1191827Sdg		return (NULL);
1201541Srgrimes
1219411Sdg	vp = (struct vnode *) handle;
1229411Sdg
12398604Salc	mtx_lock(&Giant);
1241541Srgrimes	/*
1259411Sdg	 * Prevent race condition when allocating the object. This
1269411Sdg	 * can happen with NFS vnodes since the nfsnode isn't locked.
1271541Srgrimes	 */
1289411Sdg	while (vp->v_flag & VOLOCK) {
1299411Sdg		vp->v_flag |= VOWANT;
1309411Sdg		tsleep(vp, PVM, "vnpobj", 0);
1319411Sdg	}
1329411Sdg	vp->v_flag |= VOLOCK;
1339411Sdg
1349411Sdg	/*
1359411Sdg	 * If the object is being terminated, wait for it to
1369411Sdg	 * go away.
1379411Sdg	 */
13813490Sdyson	while (((object = vp->v_object) != NULL) &&
13913490Sdyson		(object->flags & OBJ_DEAD)) {
14079224Sdillon		tsleep(object, PVM, "vadead", 0);
1419507Sdg	}
1425455Sdg
14332071Sdyson	if (vp->v_usecount == 0)
14432071Sdyson		panic("vnode_pager_alloc: no vnode reference");
14532071Sdyson
1469507Sdg	if (object == NULL) {
1471541Srgrimes		/*
1481541Srgrimes		 * And an object of the appropriate size
1491541Srgrimes		 */
15040286Sdg		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
1511827Sdg
15240286Sdg		object->un_pager.vnp.vnp_size = size;
1531549Srgrimes
1549507Sdg		object->handle = handle;
1559507Sdg		vp->v_object = object;
1561541Srgrimes	} else {
15732286Sdyson		object->ref_count++;
1581541Srgrimes	}
15998604Salc	vp->v_usecount++;
1609411Sdg	vp->v_flag &= ~VOLOCK;
1619411Sdg	if (vp->v_flag & VOWANT) {
1629411Sdg		vp->v_flag &= ~VOWANT;
1639411Sdg		wakeup(vp);
1649411Sdg	}
16598604Salc	mtx_unlock(&Giant);
1669507Sdg	return (object);
1671541Srgrimes}
1681541Srgrimes
16912820Sphkstatic void
1709507Sdgvnode_pager_dealloc(object)
1719507Sdg	vm_object_t object;
1721541Srgrimes{
17379242Sdillon	struct vnode *vp = object->handle;
1741541Srgrimes
17579224Sdillon	GIANT_REQUIRED;
1769507Sdg	if (vp == NULL)
1779507Sdg		panic("vnode_pager_dealloc: pager already dealloced");
1789507Sdg
17933817Sdyson	vm_object_pip_wait(object, "vnpdea");
1801541Srgrimes
1819507Sdg	object->handle = NULL;
18233109Sdyson	object->type = OBJT_DEAD;
1839507Sdg	vp->v_object = NULL;
18433109Sdyson	vp->v_flag &= ~(VTEXT | VOBJBUF);
1851549Srgrimes}
1861541Srgrimes
18712820Sphkstatic boolean_t
18812767Sdysonvnode_pager_haspage(object, pindex, before, after)
1899507Sdg	vm_object_t object;
19012767Sdyson	vm_pindex_t pindex;
1919507Sdg	int *before;
1929507Sdg	int *after;
1931541Srgrimes{
1949507Sdg	struct vnode *vp = object->handle;
19596572Sphk	daddr_t bn;
19612423Sphk	int err;
19710556Sdyson	daddr_t reqblock;
19811701Sdyson	int poff;
19911701Sdyson	int bsize;
20012914Sdyson	int pagesperblock, blocksperpage;
2011541Srgrimes
20279224Sdillon	GIANT_REQUIRED;
20351340Sdillon	/*
20451340Sdillon	 * If no vp or vp is doomed or marked transparent to VM, we do not
20551340Sdillon	 * have the page.
20651340Sdillon	 */
20732585Sdyson	if ((vp == NULL) || (vp->v_flag & VDOOMED))
20832585Sdyson		return FALSE;
20932585Sdyson
2101541Srgrimes	/*
2115455Sdg	 * If filesystem no longer mounted or offset beyond end of file we do
2125455Sdg	 * not have the page.
2131541Srgrimes	 */
21412767Sdyson	if ((vp->v_mount == NULL) ||
21581140Sjhb	    (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
2164797Sdg		return FALSE;
2171541Srgrimes
21811576Sdg	bsize = vp->v_mount->mnt_stat.f_iosize;
21910556Sdyson	pagesperblock = bsize / PAGE_SIZE;
22012914Sdyson	blocksperpage = 0;
22112914Sdyson	if (pagesperblock > 0) {
22212914Sdyson		reqblock = pindex / pagesperblock;
22312914Sdyson	} else {
22412914Sdyson		blocksperpage = (PAGE_SIZE / bsize);
22512914Sdyson		reqblock = pindex * blocksperpage;
22612914Sdyson	}
22710556Sdyson	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
22810556Sdyson		after, before);
2298876Srgrimes	if (err)
2309507Sdg		return TRUE;
23192029Seivind	if (bn == -1)
23210576Sdyson		return FALSE;
23312914Sdyson	if (pagesperblock > 0) {
23412914Sdyson		poff = pindex - (reqblock * pagesperblock);
23512914Sdyson		if (before) {
23612914Sdyson			*before *= pagesperblock;
23712914Sdyson			*before += poff;
23810669Sdyson		}
23912914Sdyson		if (after) {
24012914Sdyson			int numafter;
24112914Sdyson			*after *= pagesperblock;
24212914Sdyson			numafter = pagesperblock - (poff + 1);
24399211Srobert			if (IDX_TO_OFF(pindex + numafter) >
24499211Srobert			    object->un_pager.vnp.vnp_size) {
24599211Srobert				numafter =
24699211Srobert		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
24799211Srobert				    pindex;
24812914Sdyson			}
24912914Sdyson			*after += numafter;
25012914Sdyson		}
25112914Sdyson	} else {
25212914Sdyson		if (before) {
25312914Sdyson			*before /= blocksperpage;
25412914Sdyson		}
25512914Sdyson
25612914Sdyson		if (after) {
25712914Sdyson			*after /= blocksperpage;
25812914Sdyson		}
25910556Sdyson	}
26010576Sdyson	return TRUE;
2611541Srgrimes}
2621541Srgrimes
2631541Srgrimes/*
2641541Srgrimes * Lets the VM system know about a change in size for a file.
2659507Sdg * We adjust our own internal size and flush any cached pages in
2661541Srgrimes * the associated object that are affected by the size change.
2671541Srgrimes *
2681541Srgrimes * Note: this routine may be invoked as a result of a pager put
2691541Srgrimes * operation (possibly at object termination time), so we must be careful.
2701541Srgrimes */
2711541Srgrimesvoid
2721541Srgrimesvnode_pager_setsize(vp, nsize)
2731541Srgrimes	struct vnode *vp;
27412767Sdyson	vm_ooffset_t nsize;
2751541Srgrimes{
27638542Sluoqi	vm_pindex_t nobjsize;
2779507Sdg	vm_object_t object = vp->v_object;
2781541Srgrimes
27979224Sdillon	GIANT_REQUIRED;
28079224Sdillon
2819507Sdg	if (object == NULL)
2821541Srgrimes		return;
2831827Sdg
2841541Srgrimes	/*
2851541Srgrimes	 * Hasn't changed size
2861541Srgrimes	 */
2879507Sdg	if (nsize == object->un_pager.vnp.vnp_size)
2883374Sdg		return;
2891827Sdg
29038542Sluoqi	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
29138542Sluoqi
2921541Srgrimes	/*
2931827Sdg	 * File has shrunk. Toss any cached pages beyond the new EOF.
2941541Srgrimes	 */
2959507Sdg	if (nsize < object->un_pager.vnp.vnp_size) {
29696095Salc#ifdef ENABLE_VFS_IOOPT
29738542Sluoqi		vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
29896095Salc#endif
29938542Sluoqi		if (nobjsize < object->size) {
30038542Sluoqi			vm_object_page_remove(object, nobjsize, object->size,
30138542Sluoqi				FALSE);
3025455Sdg		}
3031827Sdg		/*
3041827Sdg		 * this gets rid of garbage at the end of a page that is now
30587834Sdillon		 * only partially backed by the vnode.
30687834Sdillon		 *
30787834Sdillon		 * XXX for some reason (I don't know yet), if we take a
30887834Sdillon		 * completely invalid page and mark it partially valid
30987834Sdillon		 * it can screw up NFS reads, so we don't allow the case.
3101827Sdg		 */
3111827Sdg		if (nsize & PAGE_MASK) {
3121827Sdg			vm_page_t m;
3131827Sdg
31412767Sdyson			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
31587834Sdillon			if (m && m->valid) {
31670374Sdillon				int base = (int)nsize & PAGE_MASK;
31770374Sdillon				int size = PAGE_SIZE - base;
31870374Sdillon
31970374Sdillon				/*
32070374Sdillon				 * Clear out partial-page garbage in case
32170374Sdillon				 * the page has been mapped.
32270374Sdillon				 */
32395598Speter				vm_page_zero_fill_area(m, base, size);
32470374Sdillon
32570374Sdillon				/*
32687834Sdillon				 * XXX work around SMP data integrity race
32787834Sdillon				 * by unmapping the page from user processes.
32887834Sdillon				 * The garbage we just cleared may be mapped
32987834Sdillon				 * to a user process running on another cpu
33087834Sdillon				 * and this code is not running through normal
33187834Sdillon				 * I/O channels which handle SMP issues for
33287834Sdillon				 * us, so unmap page to synchronize all cpus.
33387834Sdillon				 *
33487834Sdillon				 * XXX should vm_pager_unmap_page() have
33587834Sdillon				 * dealt with this?
33687834Sdillon				 */
33787834Sdillon				vm_page_protect(m, VM_PROT_NONE);
33887834Sdillon
33987834Sdillon				/*
34070374Sdillon				 * Clear out partial-page dirty bits.  This
34170374Sdillon				 * has the side effect of setting the valid
34270374Sdillon				 * bits, but that is ok.  There are a bunch
34370374Sdillon				 * of places in the VM system where we expected
34470374Sdillon				 * m->dirty == VM_PAGE_BITS_ALL.  The file EOF
34570374Sdillon				 * case is one of them.  If the page is still
34670374Sdillon				 * partially dirty, make it fully dirty.
34787834Sdillon				 *
34887834Sdillon				 * note that we do not clear out the valid
34987834Sdillon				 * bits.  This would prevent bogus_page
35087834Sdillon				 * replacement from working properly.
35170374Sdillon				 */
35270374Sdillon				vm_page_set_validclean(m, base, size);
35370374Sdillon				if (m->dirty != 0)
35470374Sdillon					m->dirty = VM_PAGE_BITS_ALL;
3551827Sdg			}
3561827Sdg		}
3571541Srgrimes	}
35812767Sdyson	object->un_pager.vnp.vnp_size = nsize;
35938542Sluoqi	object->size = nobjsize;
3601541Srgrimes}
3611541Srgrimes
3621549Srgrimes/*
3631549Srgrimes * calculate the linear (byte) disk address of specified virtual
3641549Srgrimes * file address
3651549Srgrimes */
36612820Sphkstatic vm_offset_t
3676151Sdgvnode_pager_addr(vp, address, run)
3681549Srgrimes	struct vnode *vp;
36912767Sdyson	vm_ooffset_t address;
3706151Sdg	int *run;
3711549Srgrimes{
3725455Sdg	int rtaddress;
3735455Sdg	int bsize;
37496572Sphk	daddr_t block;
3751549Srgrimes	struct vnode *rtvp;
3765455Sdg	int err;
37712767Sdyson	daddr_t vblock;
37812767Sdyson	int voffset;
3791549Srgrimes
38079224Sdillon	GIANT_REQUIRED;
3815455Sdg	if ((int) address < 0)
3825455Sdg		return -1;
3835455Sdg
38411701Sdyson	if (vp->v_mount == NULL)
38511701Sdyson		return -1;
38611701Sdyson
3871549Srgrimes	bsize = vp->v_mount->mnt_stat.f_iosize;
3881549Srgrimes	vblock = address / bsize;
3891549Srgrimes	voffset = address % bsize;
3901549Srgrimes
39110551Sdyson	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
3921549Srgrimes
3936151Sdg	if (err || (block == -1))
3941549Srgrimes		rtaddress = -1;
3956151Sdg	else {
3966626Sdg		rtaddress = block + voffset / DEV_BSIZE;
39792029Seivind		if (run) {
3986151Sdg			*run += 1;
3996151Sdg			*run *= bsize/PAGE_SIZE;
4006151Sdg			*run -= voffset/PAGE_SIZE;
4016151Sdg		}
4026151Sdg	}
4031549Srgrimes
4041549Srgrimes	return rtaddress;
4051549Srgrimes}
4061549Srgrimes
4071549Srgrimes/*
4081549Srgrimes * interrupt routine for I/O completion
4091549Srgrimes */
41012820Sphkstatic void
4111549Srgrimesvnode_pager_iodone(bp)
4121549Srgrimes	struct buf *bp;
4131549Srgrimes{
4141549Srgrimes	bp->b_flags |= B_DONE;
4159507Sdg	wakeup(bp);
4161549Srgrimes}
4171549Srgrimes
4181549Srgrimes/*
41996755Strhodes * small block filesystem vnode pager input
4201549Srgrimes */
42112820Sphkstatic int
4229507Sdgvnode_pager_input_smlfs(object, m)
4239507Sdg	vm_object_t object;
4241549Srgrimes	vm_page_t m;
4251549Srgrimes{
4265455Sdg	int i;
4275455Sdg	int s;
4281549Srgrimes	struct vnode *dp, *vp;
4291549Srgrimes	struct buf *bp;
4301549Srgrimes	vm_offset_t kva;
4315455Sdg	int fileaddr;
4321549Srgrimes	vm_offset_t bsize;
4335455Sdg	int error = 0;
4341549Srgrimes
43579224Sdillon	GIANT_REQUIRED;
43679224Sdillon
4379507Sdg	vp = object->handle;
43811701Sdyson	if (vp->v_mount == NULL)
43911701Sdyson		return VM_PAGER_BAD;
44011701Sdyson
4411549Srgrimes	bsize = vp->v_mount->mnt_stat.f_iosize;
4421549Srgrimes
44310551Sdyson	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
4441549Srgrimes
4451549Srgrimes	kva = vm_pager_map_page(m);
4461549Srgrimes
4471827Sdg	for (i = 0; i < PAGE_SIZE / bsize; i++) {
44886092Sdillon		vm_ooffset_t address;
4491827Sdg
45045561Sdt		if (vm_page_bits(i * bsize, bsize) & m->valid)
4515455Sdg			continue;
4521549Srgrimes
45386092Sdillon		address = IDX_TO_OFF(m->pindex) + i * bsize;
45486092Sdillon		if (address >= object->un_pager.vnp.vnp_size) {
45586092Sdillon			fileaddr = -1;
45686092Sdillon		} else {
45786092Sdillon			fileaddr = vnode_pager_addr(vp, address, NULL);
45886092Sdillon		}
4591827Sdg		if (fileaddr != -1) {
46042957Sdillon			bp = getpbuf(&vnode_pbuf_freecnt);
4611549Srgrimes
4621827Sdg			/* build a minimal buffer header */
46358345Sphk			bp->b_iocmd = BIO_READ;
4641549Srgrimes			bp->b_iodone = vnode_pager_iodone;
46584827Sjhb			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
46684827Sjhb			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
46791406Sjhb			bp->b_rcred = crhold(curthread->td_ucred);
46891406Sjhb			bp->b_wcred = crhold(curthread->td_ucred);
46931493Sphk			bp->b_data = (caddr_t) kva + i * bsize;
4706626Sdg			bp->b_blkno = fileaddr;
4715455Sdg			pbgetvp(dp, bp);
4721549Srgrimes			bp->b_bcount = bsize;
4731549Srgrimes			bp->b_bufsize = bsize;
47470374Sdillon			bp->b_runningbufspace = bp->b_bufsize;
47570374Sdillon			runningbufspace += bp->b_runningbufspace;
4761827Sdg
4771827Sdg			/* do the input */
47858349Sphk			BUF_STRATEGY(bp);
4791549Srgrimes
48033758Sdyson			/* we definitely need to be at splvm here */
4811549Srgrimes
48233758Sdyson			s = splvm();
4831549Srgrimes			while ((bp->b_flags & B_DONE) == 0) {
4849356Sdg				tsleep(bp, PVM, "vnsrd", 0);
4851549Srgrimes			}
4861549Srgrimes			splx(s);
48758934Sphk			if ((bp->b_ioflags & BIO_ERROR) != 0)
4881549Srgrimes				error = EIO;
4891549Srgrimes
4901827Sdg			/*
4911827Sdg			 * free the buffer header back to the swap buffer pool
4921827Sdg			 */
49342957Sdillon			relpbuf(bp, &vnode_pbuf_freecnt);
4941827Sdg			if (error)
4951549Srgrimes				break;
4965455Sdg
49715583Sphk			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
4981549Srgrimes		} else {
49915583Sphk			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
5001549Srgrimes			bzero((caddr_t) kva + i * bsize, bsize);
5011549Srgrimes		}
5021549Srgrimes	}
5031549Srgrimes	vm_pager_unmap_page(kva);
50460755Speter	pmap_clear_modify(m);
50538799Sdfr	vm_page_flag_clear(m, PG_ZERO);
5061827Sdg	if (error) {
5074207Sdg		return VM_PAGER_ERROR;
5081549Srgrimes	}
5091549Srgrimes	return VM_PAGER_OK;
5101549Srgrimes
5111549Srgrimes}
5121549Srgrimes
5131549Srgrimes
5141549Srgrimes/*
5151549Srgrimes * old style vnode pager output routine
5161549Srgrimes */
51712820Sphkstatic int
5189507Sdgvnode_pager_input_old(object, m)
5199507Sdg	vm_object_t object;
5201549Srgrimes	vm_page_t m;
5211549Srgrimes{
5221541Srgrimes	struct uio auio;
5231541Srgrimes	struct iovec aiov;
5245455Sdg	int error;
5255455Sdg	int size;
5261549Srgrimes	vm_offset_t kva;
52777398Sjhb	struct vnode *vp;
5281549Srgrimes
52979224Sdillon	GIANT_REQUIRED;
5301549Srgrimes	error = 0;
5311827Sdg
5321549Srgrimes	/*
5331549Srgrimes	 * Return failure if beyond current EOF
5341549Srgrimes	 */
53512767Sdyson	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
5361549Srgrimes		return VM_PAGER_BAD;
5371549Srgrimes	} else {
5381549Srgrimes		size = PAGE_SIZE;
53912767Sdyson		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
54012767Sdyson			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
5417178Sdg
5425455Sdg		/*
5435455Sdg		 * Allocate a kernel virtual address and initialize so that
5445455Sdg		 * we can use VOP_READ/WRITE routines.
5455455Sdg		 */
5461549Srgrimes		kva = vm_pager_map_page(m);
5477178Sdg
54877398Sjhb		vp = object->handle;
5491827Sdg		aiov.iov_base = (caddr_t) kva;
5501549Srgrimes		aiov.iov_len = size;
5511549Srgrimes		auio.uio_iov = &aiov;
5521549Srgrimes		auio.uio_iovcnt = 1;
55312767Sdyson		auio.uio_offset = IDX_TO_OFF(m->pindex);
5541549Srgrimes		auio.uio_segflg = UIO_SYSSPACE;
5551549Srgrimes		auio.uio_rw = UIO_READ;
5561549Srgrimes		auio.uio_resid = size;
55783366Sjulian		auio.uio_td = curthread;
5581549Srgrimes
55991406Sjhb		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
5601549Srgrimes		if (!error) {
56179242Sdillon			int count = size - auio.uio_resid;
5621549Srgrimes
5631549Srgrimes			if (count == 0)
5641549Srgrimes				error = EINVAL;
5651549Srgrimes			else if (count != PAGE_SIZE)
5661827Sdg				bzero((caddr_t) kva + count, PAGE_SIZE - count);
5671549Srgrimes		}
5681549Srgrimes		vm_pager_unmap_page(kva);
5691549Srgrimes	}
57060755Speter	pmap_clear_modify(m);
57149945Salc	vm_page_undirty(m);
57238799Sdfr	vm_page_flag_clear(m, PG_ZERO);
57339739Srvb	if (!error)
57439739Srvb		m->valid = VM_PAGE_BITS_ALL;
5754207Sdg	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
5761549Srgrimes}
5771549Srgrimes
5781549Srgrimes/*
5791549Srgrimes * generic vnode pager input routine
5801549Srgrimes */
58110556Sdyson
58233847Smsmith/*
58376827Salfred * Local media VFS's that do not implement their own VOP_GETPAGES
58499211Srobert * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
58599211Srobert * to implement the previous behaviour.
58633847Smsmith *
58733847Smsmith * All other FS's should use the bypass to get to the local media
58833847Smsmith * backing vp's VOP_GETPAGES.
58933847Smsmith */
59012820Sphkstatic int
5919507Sdgvnode_pager_getpages(object, m, count, reqpage)
5929507Sdg	vm_object_t object;
5931549Srgrimes	vm_page_t *m;
5949507Sdg	int count;
5959507Sdg	int reqpage;
5961549Srgrimes{
59710556Sdyson	int rtval;
59810556Sdyson	struct vnode *vp;
59934403Smsmith	int bytes = count * PAGE_SIZE;
60032286Sdyson
60179224Sdillon	GIANT_REQUIRED;
60210556Sdyson	vp = object->handle;
60334403Smsmith	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
60476827Salfred	KASSERT(rtval != EOPNOTSUPP,
60576827Salfred	    ("vnode_pager: FS getpages not implemented\n"));
60633847Smsmith	return rtval;
60710556Sdyson}
60810556Sdyson
60933847Smsmith/*
61033847Smsmith * This is now called from local media FS's to operate against their
61133847Smsmith * own vnodes if they fail to implement VOP_GETPAGES.
61233847Smsmith */
61333847Smsmithint
61433847Smsmithvnode_pager_generic_getpages(vp, m, bytecount, reqpage)
61533847Smsmith	struct vnode *vp;
61610556Sdyson	vm_page_t *m;
61733847Smsmith	int bytecount;
61810556Sdyson	int reqpage;
61910556Sdyson{
62033847Smsmith	vm_object_t object;
62112767Sdyson	vm_offset_t kva;
62234206Sdyson	off_t foff, tfoff, nextoff;
623100832Salc	int i, j, size, bsize, first, firstaddr;
62433847Smsmith	struct vnode *dp;
6256151Sdg	int runpg;
6266151Sdg	int runend;
6277178Sdg	struct buf *bp;
6285455Sdg	int s;
62933847Smsmith	int count;
6305455Sdg	int error = 0;
6311549Srgrimes
63279224Sdillon	GIANT_REQUIRED;
63333847Smsmith	object = vp->v_object;
63433847Smsmith	count = bytecount / PAGE_SIZE;
63533847Smsmith
63611701Sdyson	if (vp->v_mount == NULL)
63711701Sdyson		return VM_PAGER_BAD;
63811701Sdyson
6391549Srgrimes	bsize = vp->v_mount->mnt_stat.f_iosize;
6401549Srgrimes
6411549Srgrimes	/* get the UNDERLYING device for the file with VOP_BMAP() */
6421827Sdg
6431549Srgrimes	/*
6441827Sdg	 * originally, we did not check for an error return value -- assuming
6451827Sdg	 * an fs always has a bmap entry point -- that assumption is wrong!!!
6461549Srgrimes	 */
64712767Sdyson	foff = IDX_TO_OFF(m[reqpage]->pindex);
6481827Sdg
6491549Srgrimes	/*
6501887Sdg	 * if we can't bmap, use old VOP code
6511549Srgrimes	 */
65210551Sdyson	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
653100832Salc		vm_page_lock_queues();
654100832Salc		for (i = 0; i < count; i++)
655100832Salc			if (i != reqpage)
65675692Salfred				vm_page_free(m[i]);
657100832Salc		vm_page_unlock_queues();
6583612Sdg		cnt.v_vnodein++;
6593612Sdg		cnt.v_vnodepgsin++;
6609507Sdg		return vnode_pager_input_old(object, m[reqpage]);
6611549Srgrimes
6621827Sdg		/*
6631827Sdg		 * if the blocksize is smaller than a page size, then use
6641827Sdg		 * special small filesystem code.  NFS sometimes has a small
6651827Sdg		 * blocksize, but it can handle large reads itself.
6661827Sdg		 */
6671827Sdg	} else if ((PAGE_SIZE / bsize) > 1 &&
66838866Sbde	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
669100832Salc		vm_page_lock_queues();
670100832Salc		for (i = 0; i < count; i++)
671100832Salc			if (i != reqpage)
67275692Salfred				vm_page_free(m[i]);
673100832Salc		vm_page_unlock_queues();
6743612Sdg		cnt.v_vnodein++;
6753612Sdg		cnt.v_vnodepgsin++;
6769507Sdg		return vnode_pager_input_smlfs(object, m[reqpage]);
6771549Srgrimes	}
67845347Sjulian
6791549Srgrimes	/*
68045347Sjulian	 * If we have a completely valid page available to us, we can
68145347Sjulian	 * clean up and return.  Otherwise we have to re-read the
68245347Sjulian	 * media.
6831549Srgrimes	 */
68445347Sjulian	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
685100832Salc		vm_page_lock_queues();
686100832Salc		for (i = 0; i < count; i++)
6875455Sdg			if (i != reqpage)
68875692Salfred				vm_page_free(m[i]);
689100832Salc		vm_page_unlock_queues();
6905455Sdg		return VM_PAGER_OK;
6911549Srgrimes	}
69245347Sjulian	m[reqpage]->valid = 0;
6937178Sdg
6945455Sdg	/*
6955455Sdg	 * here on direct device I/O
6965455Sdg	 */
69792029Seivind	firstaddr = -1;
6981549Srgrimes
6991549Srgrimes	/*
7006151Sdg	 * calculate the run that includes the required page
7011549Srgrimes	 */
70292029Seivind	for (first = 0, i = 0; i < count; i = runend) {
70312767Sdyson		firstaddr = vnode_pager_addr(vp,
70412767Sdyson			IDX_TO_OFF(m[i]->pindex), &runpg);
7056151Sdg		if (firstaddr == -1) {
7069507Sdg			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
70737562Sbde				/* XXX no %qd in kernel. */
70837562Sbde				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx",
70937562Sbde			   	 firstaddr, (u_long)(foff >> 32),
71037562Sbde			   	 (u_long)(u_int32_t)foff,
71137562Sbde				 (u_long)(u_int32_t)
71237562Sbde				 (object->un_pager.vnp.vnp_size >> 32),
71337562Sbde				 (u_long)(u_int32_t)
71437562Sbde				 object->un_pager.vnp.vnp_size);
7156151Sdg			}
716100832Salc			vm_page_lock_queues();
71775692Salfred			vm_page_free(m[i]);
718100832Salc			vm_page_unlock_queues();
7196151Sdg			runend = i + 1;
7206151Sdg			first = runend;
7216151Sdg			continue;
7221549Srgrimes		}
7236151Sdg		runend = i + runpg;
7249507Sdg		if (runend <= reqpage) {
725100832Salc			vm_page_lock_queues();
726100832Salc			for (j = i; j < runend; j++)
72775692Salfred				vm_page_free(m[j]);
728100832Salc			vm_page_unlock_queues();
7291549Srgrimes		} else {
7309507Sdg			if (runpg < (count - first)) {
731100832Salc				vm_page_lock_queues();
7329507Sdg				for (i = first + runpg; i < count; i++)
73375692Salfred					vm_page_free(m[i]);
734100832Salc				vm_page_unlock_queues();
7356151Sdg				count = first + runpg;
7366151Sdg			}
7376151Sdg			break;
7381549Srgrimes		}
7396151Sdg		first = runend;
7401549Srgrimes	}
7411549Srgrimes
7421549Srgrimes	/*
7431827Sdg	 * the first and last page have been calculated now, move input pages
7441827Sdg	 * to be zero based...
7451549Srgrimes	 */
7461549Srgrimes	if (first != 0) {
7471549Srgrimes		for (i = first; i < count; i++) {
7481549Srgrimes			m[i - first] = m[i];
7491549Srgrimes		}
7501549Srgrimes		count -= first;
7511549Srgrimes		reqpage -= first;
7521549Srgrimes	}
7536151Sdg
7541549Srgrimes	/*
7551549Srgrimes	 * calculate the file virtual address for the transfer
7561549Srgrimes	 */
75712767Sdyson	foff = IDX_TO_OFF(m[0]->pindex);
7581827Sdg
7591549Srgrimes	/*
7601549Srgrimes	 * calculate the size of the transfer
7611549Srgrimes	 */
7621549Srgrimes	size = count * PAGE_SIZE;
7639507Sdg	if ((foff + size) > object->un_pager.vnp.vnp_size)
7649507Sdg		size = object->un_pager.vnp.vnp_size - foff;
7651549Srgrimes
7661549Srgrimes	/*
76751340Sdillon	 * round up physical size for real devices.
7681549Srgrimes	 */
76951340Sdillon	if (dp->v_type == VBLK || dp->v_type == VCHR) {
77051340Sdillon		int secmask = dp->v_rdev->si_bsize_phys - 1;
77151340Sdillon		KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
77251340Sdillon		size = (size + secmask) & ~secmask;
77351340Sdillon	}
7741549Srgrimes
77542957Sdillon	bp = getpbuf(&vnode_pbuf_freecnt);
7765455Sdg	kva = (vm_offset_t) bp->b_data;
7771887Sdg
7781549Srgrimes	/*
7791549Srgrimes	 * and map the pages to be read into the kva
7801549Srgrimes	 */
7811887Sdg	pmap_qenter(kva, m, count);
7821549Srgrimes
7831549Srgrimes	/* build a minimal buffer header */
78458345Sphk	bp->b_iocmd = BIO_READ;
7851549Srgrimes	bp->b_iodone = vnode_pager_iodone;
7861549Srgrimes	/* B_PHYS is not set, but it is nice to fill this in */
78784827Sjhb	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
78884827Sjhb	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
78991406Sjhb	bp->b_rcred = crhold(curthread->td_ucred);
79091406Sjhb	bp->b_wcred = crhold(curthread->td_ucred);
7916626Sdg	bp->b_blkno = firstaddr;
7925455Sdg	pbgetvp(dp, bp);
7931549Srgrimes	bp->b_bcount = size;
7941549Srgrimes	bp->b_bufsize = size;
79570374Sdillon	bp->b_runningbufspace = bp->b_bufsize;
79670374Sdillon	runningbufspace += bp->b_runningbufspace;
7971549Srgrimes
7983612Sdg	cnt.v_vnodein++;
7993612Sdg	cnt.v_vnodepgsin += count;
8003612Sdg
8011549Srgrimes	/* do the input */
80258349Sphk	BUF_STRATEGY(bp);
8033612Sdg
80433758Sdyson	s = splvm();
80533758Sdyson	/* we definitely need to be at splvm here */
8061549Srgrimes
8071549Srgrimes	while ((bp->b_flags & B_DONE) == 0) {
8089356Sdg		tsleep(bp, PVM, "vnread", 0);
8091549Srgrimes	}
8101549Srgrimes	splx(s);
81158934Sphk	if ((bp->b_ioflags & BIO_ERROR) != 0)
8121549Srgrimes		error = EIO;
8131549Srgrimes
8141549Srgrimes	if (!error) {
8151549Srgrimes		if (size != count * PAGE_SIZE)
8161827Sdg			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
8171549Srgrimes	}
8185455Sdg	pmap_qremove(kva, count);
8191549Srgrimes
8201549Srgrimes	/*
8211549Srgrimes	 * free the buffer header back to the swap buffer pool
8221549Srgrimes	 */
82342957Sdillon	relpbuf(bp, &vnode_pbuf_freecnt);
8241549Srgrimes
825100736Salc	vm_page_lock_queues();
82634206Sdyson	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
82734206Sdyson		vm_page_t mt;
82834206Sdyson
82934206Sdyson		nextoff = tfoff + PAGE_SIZE;
83034206Sdyson		mt = m[i];
83134206Sdyson
83247239Sdt		if (nextoff <= object->un_pager.vnp.vnp_size) {
83345347Sjulian			/*
83445347Sjulian			 * Read filled up entire page.
83545347Sjulian			 */
83634206Sdyson			mt->valid = VM_PAGE_BITS_ALL;
83749945Salc			vm_page_undirty(mt);	/* should be an assert? XXX */
83860755Speter			pmap_clear_modify(mt);
83934206Sdyson		} else {
84045347Sjulian			/*
84145347Sjulian			 * Read did not fill up entire page.  Since this
84245347Sjulian			 * is getpages, the page may be mapped, so we have
84345347Sjulian			 * to zero the invalid portions of the page even
84445347Sjulian			 * though we aren't setting them valid.
84545347Sjulian			 *
84645347Sjulian			 * Currently we do not set the entire page valid,
84745347Sjulian			 * we just try to clear the piece that we couldn't
84845347Sjulian			 * read.
84945347Sjulian			 */
85047239Sdt			vm_page_set_validclean(mt, 0,
85147239Sdt			    object->un_pager.vnp.vnp_size - tfoff);
85246349Salc			/* handled by vm_fault now */
85346349Salc			/* vm_page_zero_invalid(mt, FALSE); */
85434206Sdyson		}
85534206Sdyson
85638799Sdfr		vm_page_flag_clear(mt, PG_ZERO);
8571549Srgrimes		if (i != reqpage) {
8581827Sdg
8591549Srgrimes			/*
8601827Sdg			 * whether or not to leave the page activated is up in
8611827Sdg			 * the air, but we should put the page on a page queue
8621827Sdg			 * somewhere. (it already is in the object). Result:
86358634Scharnier			 * It appears that empirical results show that
8641827Sdg			 * deactivating pages is best.
8651549Srgrimes			 */
8661827Sdg
8671549Srgrimes			/*
8681827Sdg			 * just in case someone was asking for this page we
8691827Sdg			 * now tell them that it is ok to use
8701549Srgrimes			 */
8711549Srgrimes			if (!error) {
87234206Sdyson				if (mt->flags & PG_WANTED)
87334206Sdyson					vm_page_activate(mt);
87433109Sdyson				else
87534206Sdyson					vm_page_deactivate(mt);
87638799Sdfr				vm_page_wakeup(mt);
8771549Srgrimes			} else {
87875692Salfred				vm_page_free(mt);
8791549Srgrimes			}
8801549Srgrimes		}
8811549Srgrimes	}
882100736Salc	vm_page_unlock_queues();
8831549Srgrimes	if (error) {
8849507Sdg		printf("vnode_pager_getpages: I/O read error\n");
8851549Srgrimes	}
8864207Sdg	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
8871549Srgrimes}
8881549Srgrimes
88933847Smsmith/*
89033847Smsmith * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
89133847Smsmith * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
89233847Smsmith * vnode_pager_generic_putpages() to implement the previous behaviour.
89333847Smsmith *
89433847Smsmith * All other FS's should use the bypass to get to the local media
89533847Smsmith * backing vp's VOP_PUTPAGES.
89633847Smsmith */
89743129Sdillonstatic void
89810556Sdysonvnode_pager_putpages(object, m, count, sync, rtvals)
89910556Sdyson	vm_object_t object;
90010556Sdyson	vm_page_t *m;
90110556Sdyson	int count;
90210556Sdyson	boolean_t sync;
90310556Sdyson	int *rtvals;
90410556Sdyson{
90510556Sdyson	int rtval;
90610556Sdyson	struct vnode *vp;
90762976Smckusick	struct mount *mp;
90834403Smsmith	int bytes = count * PAGE_SIZE;
90918973Sdyson
91079224Sdillon	GIANT_REQUIRED;
91144321Salc	/*
91244321Salc	 * Force synchronous operation if we are extremely low on memory
91344321Salc	 * to prevent a low-memory deadlock.  VOP operations often need to
91444321Salc	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
91544321Salc	 * operation ).  The swapper handles the case by limiting the amount
91644321Salc	 * of asynchronous I/O, but that sort of solution doesn't scale well
91744321Salc	 * for the vnode pager without a lot of work.
91844321Salc	 *
91944321Salc	 * Also, the backing vnode's iodone routine may not wake the pageout
92044321Salc	 * daemon up.  This should be probably be addressed XXX.
92144321Salc	 */
92244321Salc
92344321Salc	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
92444321Salc		sync |= OBJPC_SYNC;
92544321Salc
92644321Salc	/*
92744321Salc	 * Call device-specific putpages function
92844321Salc	 */
92910556Sdyson	vp = object->handle;
93062976Smckusick	if (vp->v_type != VREG)
93162976Smckusick		mp = NULL;
93262976Smckusick	(void)vn_start_write(vp, &mp, V_WAIT);
93334403Smsmith	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
93476827Salfred	KASSERT(rtval != EOPNOTSUPP,
93576827Salfred	    ("vnode_pager: stale FS putpages\n"));
93662976Smckusick	vn_finished_write(mp);
93710556Sdyson}
93810556Sdyson
93933847Smsmith
9401549Srgrimes/*
94133847Smsmith * This is now called from local media FS's to operate against their
94245057Seivind * own vnodes if they fail to implement VOP_PUTPAGES.
94370374Sdillon *
94470374Sdillon * This is typically called indirectly via the pageout daemon and
94570374Sdillon * clustering has already typically occured, so in general we ask the
94670374Sdillon * underlying filesystem to write the data out asynchronously rather
94770374Sdillon * then delayed.
9481549Srgrimes */
94933847Smsmithint
95034206Sdysonvnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
95133847Smsmith	struct vnode *vp;
9521549Srgrimes	vm_page_t *m;
95333847Smsmith	int bytecount;
95434206Sdyson	int flags;
9555455Sdg	int *rtvals;
9561549Srgrimes{
9577695Sdg	int i;
95833847Smsmith	vm_object_t object;
95933847Smsmith	int count;
9601549Srgrimes
9617695Sdg	int maxsize, ncount;
96212767Sdyson	vm_ooffset_t poffset;
9637695Sdg	struct uio auio;
9647695Sdg	struct iovec aiov;
9657695Sdg	int error;
96634206Sdyson	int ioflags;
9671549Srgrimes
96879224Sdillon	GIANT_REQUIRED;
96933847Smsmith	object = vp->v_object;
97033847Smsmith	count = bytecount / PAGE_SIZE;
97133847Smsmith
9721827Sdg	for (i = 0; i < count; i++)
9731549Srgrimes		rtvals[i] = VM_PAGER_AGAIN;
9741549Srgrimes
97512767Sdyson	if ((int) m[0]->pindex < 0) {
97648409Speter		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n",
97748409Speter			(long)m[0]->pindex, m[0]->dirty);
9787695Sdg		rtvals[0] = VM_PAGER_BAD;
9797695Sdg		return VM_PAGER_BAD;
9805455Sdg	}
9817178Sdg
9827695Sdg	maxsize = count * PAGE_SIZE;
9837695Sdg	ncount = count;
9841549Srgrimes
98512767Sdyson	poffset = IDX_TO_OFF(m[0]->pindex);
98684854Sdillon
98784854Sdillon	/*
98884854Sdillon	 * If the page-aligned write is larger then the actual file we
98984854Sdillon	 * have to invalidate pages occuring beyond the file EOF.  However,
99084854Sdillon	 * there is an edge case where a file may not be page-aligned where
99184854Sdillon	 * the last page is partially invalid.  In this case the filesystem
99284854Sdillon	 * may not properly clear the dirty bits for the entire page (which
99384854Sdillon	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
99484854Sdillon	 * With the page locked we are free to fix-up the dirty bits here.
99587834Sdillon	 *
99687834Sdillon	 * We do not under any circumstances truncate the valid bits, as
99787834Sdillon	 * this will screw up bogus page replacement.
99884854Sdillon	 */
99912767Sdyson	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
100084854Sdillon		if (object->un_pager.vnp.vnp_size > poffset) {
100184854Sdillon			int pgoff;
100284854Sdillon
100312767Sdyson			maxsize = object->un_pager.vnp.vnp_size - poffset;
100484854Sdillon			ncount = btoc(maxsize);
100584854Sdillon			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
100684854Sdillon				vm_page_clear_dirty(m[ncount - 1], pgoff,
100784854Sdillon					PAGE_SIZE - pgoff);
100884854Sdillon			}
100984854Sdillon		} else {
10108585Sdg			maxsize = 0;
101184854Sdillon			ncount = 0;
101284854Sdillon		}
10138585Sdg		if (ncount < count) {
10148585Sdg			for (i = ncount; i < count; i++) {
10157695Sdg				rtvals[i] = VM_PAGER_BAD;
10161549Srgrimes			}
10171549Srgrimes		}
10181541Srgrimes	}
10197695Sdg
102070374Sdillon	/*
102170374Sdillon	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
102270374Sdillon	 * rather then a bdwrite() to prevent paging I/O from saturating
102370374Sdillon	 * the buffer cache.
102470374Sdillon	 */
102534206Sdyson	ioflags = IO_VMIO;
102670374Sdillon	ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: IO_ASYNC;
102734206Sdyson	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
10281827Sdg
10297695Sdg	aiov.iov_base = (caddr_t) 0;
10307695Sdg	aiov.iov_len = maxsize;
10317695Sdg	auio.uio_iov = &aiov;
10327695Sdg	auio.uio_iovcnt = 1;
103312767Sdyson	auio.uio_offset = poffset;
10347695Sdg	auio.uio_segflg = UIO_NOCOPY;
10357695Sdg	auio.uio_rw = UIO_WRITE;
10367695Sdg	auio.uio_resid = maxsize;
103783366Sjulian	auio.uio_td = (struct thread *) 0;
103891406Sjhb	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
10393612Sdg	cnt.v_vnodeout++;
10407695Sdg	cnt.v_vnodepgsout += ncount;
10413612Sdg
10428585Sdg	if (error) {
10439507Sdg		printf("vnode_pager_putpages: I/O error %d\n", error);
10447695Sdg	}
10458585Sdg	if (auio.uio_resid) {
104637555Sbde		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
104737555Sbde		    auio.uio_resid, (u_long)m[0]->pindex);
10487695Sdg	}
104933936Sdyson	for (i = 0; i < ncount; i++) {
105033936Sdyson		rtvals[i] = VM_PAGER_OK;
10517695Sdg	}
10527695Sdg	return rtvals[0];
10537695Sdg}
10541549Srgrimes
10557695Sdgstruct vnode *
10569507Sdgvnode_pager_lock(object)
10579507Sdg	vm_object_t object;
10589507Sdg{
105983366Sjulian	struct thread *td = curthread;	/* XXX */
106022521Sdyson
106179224Sdillon	GIANT_REQUIRED;
106279224Sdillon
10639507Sdg	for (; object != NULL; object = object->backing_object) {
10649507Sdg		if (object->type != OBJT_VNODE)
10657695Sdg			continue;
106677094Sjhb		if (object->flags & OBJ_DEAD) {
106732585Sdyson			return NULL;
106877094Sjhb		}
10691549Srgrimes
107077094Sjhb		/* XXX; If object->handle can change, we need to cache it. */
107132585Sdyson		while (vget(object->handle,
107283366Sjulian			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, td)){
107334611Sdyson			if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE))
107434611Sdyson				return NULL;
107532585Sdyson			printf("vnode_pager_lock: retrying\n");
107632585Sdyson		}
10779507Sdg		return object->handle;
10781549Srgrimes	}
10799507Sdg	return NULL;
10807695Sdg}
1081