vnode_pager.c revision 119045
150477Speter/*
21817Sdg * Copyright (c) 1990 University of Utah.
31817Sdg * Copyright (c) 1991 The Regents of the University of California.
41541Srgrimes * All rights reserved.
51541Srgrimes * Copyright (c) 1993, 1994 John S. Dyson
61541Srgrimes * Copyright (c) 1995, David Greenman
7160798Sjhb *
81541Srgrimes * This code is derived from software contributed to Berkeley by
9146806Srwatson * the Systems Programming Group of the University of Utah Computer
10146806Srwatson * Science Department.
11146806Srwatson *
12146806Srwatson * Redistribution and use in source and binary forms, with or without
13146806Srwatson * modification, are permitted provided that the following conditions
14194390Sjhb * are met:
15203660Sed * 1. Redistributions of source code must retain the above copyright
16194390Sjhb *    notice, this list of conditions and the following disclaimer.
17194390Sjhb * 2. Redistributions in binary form must reproduce the above copyright
1811294Sswallace *    notice, this list of conditions and the following disclaimer in the
1910905Sbde *    documentation and/or other materials provided with the distribution.
201541Srgrimes * 3. All advertising materials mentioning features or use of this software
2110905Sbde *    must display the following acknowledgement:
2210905Sbde *	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 *
2899855Salfred * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29194645Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30194833Sjhb * 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
3369449Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34194383Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35160797Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36181972Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37181972Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38183361Sjhb * SUCH DAMAGE.
39181972Sobrien *
40181972Sobrien *	from: @(#)vnode_pager.c	7.5 (Berkeley) 4/20/91
41181972Sobrien */
42181972Sobrien
43211838Skib/*
44104747Srwatson * Page to/from files (vnodes).
45104747Srwatson */
46123408Speter
47123408Speter/*
481541Srgrimes * TODO:
491541Srgrimes *	Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
5011294Sswallace *	greatly re-simplify the vnode_pager.
5111294Sswallace */
5211294Sswallace
5311294Sswallace#include <sys/cdefs.h>
541541Srgrimes__FBSDID("$FreeBSD: head/sys/vm/vnode_pager.c 119045 2003-08-17 18:54:23Z phk $");
551541Srgrimes
561541Srgrimes#include <sys/param.h>
571541Srgrimes#include <sys/systm.h>
581541Srgrimes#include <sys/proc.h>
591541Srgrimes#include <sys/vnode.h>
60160798Sjhb#include <sys/mount.h>
61160798Sjhb#include <sys/bio.h>
62146806Srwatson#include <sys/buf.h>
63160798Sjhb#include <sys/vmmeter.h>
64160798Sjhb#include <sys/conf.h>
65146806Srwatson
66160798Sjhb#include <vm/vm.h>
67146806Srwatson#include <vm/vm_object.h>
68160798Sjhb#include <vm/vm_page.h>
6912216Sbde#include <vm/vm_pager.h>
7012216Sbde#include <vm/vm_map.h>
7112216Sbde#include <vm/vnode_pager.h>
72160798Sjhb#include <vm/vm_extern.h>
73160798Sjhb
74146806Srwatsonstatic void vnode_pager_init(void);
75146806Srwatsonstatic vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
76162991Srwatson					 int *run);
77160798Sjhbstatic void vnode_pager_iodone(struct buf *bp);
78160798Sjhbstatic int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
79146806Srwatsonstatic int vnode_pager_input_old(vm_object_t object, vm_page_t m);
80160798Sjhbstatic void vnode_pager_dealloc(vm_object_t);
81160798Sjhbstatic int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
82160798Sjhbstatic void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
83160798Sjhbstatic boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
84160798Sjhb
85160798Sjhbstruct pagerops vnodepagerops = {
86146806Srwatson	.pgo_init =	vnode_pager_init,
87160798Sjhb	.pgo_alloc =	vnode_pager_alloc,
88146806Srwatson	.pgo_dealloc =	vnode_pager_dealloc,
89160798Sjhb	.pgo_getpages =	vnode_pager_getpages,
90146806Srwatson	.pgo_putpages =	vnode_pager_putpages,
91160798Sjhb	.pgo_haspage =	vnode_pager_haspage,
92160798Sjhb};
93146806Srwatson
9412216Sbdeint vnode_pbuf_freecnt;
95160798Sjhb
96160798Sjhbstatic void
97160798Sjhbvnode_pager_init(void)
98160798Sjhb{
99160798Sjhb
100146806Srwatson	vnode_pbuf_freecnt = nswbuf / 2 + 1;
101160798Sjhb}
102146806Srwatson
103160798Sjhb/*
104146806Srwatson * Allocate (or lookup) pager for a vnode.
105160798Sjhb * Handle is a vnode pointer.
106146806Srwatson *
107146806Srwatson * MPSAFE
108146806Srwatson */
109160798Sjhbvm_object_t
110146806Srwatsonvnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
111146806Srwatson		  vm_ooffset_t offset)
112160798Sjhb{
113146806Srwatson	vm_object_t object;
114146806Srwatson	struct vnode *vp;
115160798Sjhb
116146806Srwatson	/*
117146806Srwatson	 * Pageout to vnode, no can do yet.
118160798Sjhb	 */
119160798Sjhb	if (handle == NULL)
120160798Sjhb		return (NULL);
121160798Sjhb
122160798Sjhb	vp = (struct vnode *) handle;
123160798Sjhb
124160798Sjhb	ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
125160798Sjhb
126160798Sjhb	mtx_lock(&Giant);
127160798Sjhb	/*
128160798Sjhb	 * Prevent race condition when allocating the object. This
129160798Sjhb	 * can happen with NFS vnodes since the nfsnode isn't locked.
130146806Srwatson	 */
131160798Sjhb	VI_LOCK(vp);
132146806Srwatson	while (vp->v_iflag & VI_OLOCK) {
133160798Sjhb		vp->v_iflag |= VI_OWANT;
134146806Srwatson		msleep(vp, VI_MTX(vp), PVM, "vnpobj", 0);
135146806Srwatson	}
136160798Sjhb	vp->v_iflag |= VI_OLOCK;
137160798Sjhb	VI_UNLOCK(vp);
13821776Sbde
13921776Sbde	/*
14021776Sbde	 * If the object is being terminated, wait for it to
141160798Sjhb	 * go away.
142146806Srwatson	 */
143160798Sjhb	while ((object = vp->v_object) != NULL) {
144160798Sjhb		VM_OBJECT_LOCK(object);
145160798Sjhb		if ((object->flags & OBJ_DEAD) == 0)
146162373Srwatson			break;
147146806Srwatson		msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
148160798Sjhb	}
149146806Srwatson
150160798Sjhb	if (vp->v_usecount == 0)
151160798Sjhb		panic("vnode_pager_alloc: no vnode reference");
152160798Sjhb
153176215Sru	if (object == NULL) {
154176215Sru		/*
155160798Sjhb		 * And an object of the appropriate size
156146806Srwatson		 */
157160798Sjhb		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
158146806Srwatson
159160798Sjhb		object->un_pager.vnp.vnp_size = size;
160160798Sjhb
161160798Sjhb		object->handle = handle;
162146806Srwatson		vp->v_object = object;
163146806Srwatson	} else {
164162991Srwatson		object->ref_count++;
165146806Srwatson		VM_OBJECT_UNLOCK(object);
166160798Sjhb	}
167146806Srwatson	VI_LOCK(vp);
168160798Sjhb	vp->v_usecount++;
169146806Srwatson	vp->v_iflag &= ~VI_OLOCK;
170146806Srwatson	if (vp->v_iflag & VI_OWANT) {
171160798Sjhb		vp->v_iflag &= ~VI_OWANT;
172160798Sjhb		wakeup(vp);
173160798Sjhb	}
174146806Srwatson	VI_UNLOCK(vp);
175160798Sjhb	mtx_unlock(&Giant);
176146806Srwatson	return (object);
177160798Sjhb}
178160798Sjhb
179146806Srwatson/*
180160798Sjhb *	The object must be locked.
181146806Srwatson */
182146806Srwatsonstatic void
183146806Srwatsonvnode_pager_dealloc(object)
184160798Sjhb	vm_object_t object;
185146806Srwatson{
186160798Sjhb	struct vnode *vp = object->handle;
187146806Srwatson
188160798Sjhb	if (vp == NULL)
189146806Srwatson		panic("vnode_pager_dealloc: pager already dealloced");
190160798Sjhb
191160798Sjhb	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
192160798Sjhb	vm_object_pip_wait(object, "vnpdea");
193146806Srwatson
194160798Sjhb	object->handle = NULL;
195160798Sjhb	object->type = OBJT_DEAD;
196160798Sjhb	ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc");
197146806Srwatson	vp->v_object = NULL;
198160798Sjhb	vp->v_vflag &= ~(VV_TEXT | VV_OBJBUF);
199146806Srwatson}
200146806Srwatson
201160798Sjhbstatic boolean_t
202146806Srwatsonvnode_pager_haspage(object, pindex, before, after)
203146806Srwatson	vm_object_t object;
204160798Sjhb	vm_pindex_t pindex;
205160798Sjhb	int *before;
206146806Srwatson	int *after;
207160798Sjhb{
208123750Speter	struct vnode *vp = object->handle;
20912216Sbde	daddr_t bn;
210160798Sjhb	int err;
211146806Srwatson	daddr_t reqblock;
212146806Srwatson	int poff;
213160798Sjhb	int bsize;
214160798Sjhb	int pagesperblock, blocksperpage;
215146806Srwatson
216160798Sjhb	VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
217146806Srwatson	/*
218160798Sjhb	 * If no vp or vp is doomed or marked transparent to VM, we do not
219146806Srwatson	 * have the page.
220194390Sjhb	 */
221146806Srwatson	if (vp == NULL)
222160798Sjhb		return FALSE;
223160798Sjhb
224146806Srwatson	VI_LOCK(vp);
225160798Sjhb	if (vp->v_iflag & VI_DOOMED) {
226146806Srwatson		VI_UNLOCK(vp);
227160798Sjhb		return FALSE;
228146806Srwatson	}
229160798Sjhb	VI_UNLOCK(vp);
230146806Srwatson	/*
231160798Sjhb	 * If filesystem no longer mounted or offset beyond end of file we do
232146806Srwatson	 * not have the page.
233160798Sjhb	 */
234146806Srwatson	if ((vp->v_mount == NULL) ||
235160798Sjhb	    (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
236146806Srwatson		return FALSE;
237160798Sjhb
238160798Sjhb	bsize = vp->v_mount->mnt_stat.f_iosize;
239160798Sjhb	pagesperblock = bsize / PAGE_SIZE;
24021776Sbde	blocksperpage = 0;
24121776Sbde	if (pagesperblock > 0) {
242160798Sjhb		reqblock = pindex / pagesperblock;
243146806Srwatson	} else {
244160798Sjhb		blocksperpage = (PAGE_SIZE / bsize);
245146806Srwatson		reqblock = pindex * blocksperpage;
246160798Sjhb	}
247146806Srwatson	VM_OBJECT_UNLOCK(object);
248146806Srwatson	err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
249160798Sjhb	VM_OBJECT_LOCK(object);
250146806Srwatson	if (err)
251160798Sjhb		return TRUE;
252146806Srwatson	if (bn == -1)
253160798Sjhb		return FALSE;
254146806Srwatson	if (pagesperblock > 0) {
255146806Srwatson		poff = pindex - (reqblock * pagesperblock);
256160798Sjhb		if (before) {
257146806Srwatson			*before *= pagesperblock;
258160798Sjhb			*before += poff;
259146806Srwatson		}
260160798Sjhb		if (after) {
261146806Srwatson			int numafter;
262160798Sjhb			*after *= pagesperblock;
263160798Sjhb			numafter = pagesperblock - (poff + 1);
264194390Sjhb			if (IDX_TO_OFF(pindex + numafter) >
265146806Srwatson			    object->un_pager.vnp.vnp_size) {
266146806Srwatson				numafter =
267146806Srwatson		    		    OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
268160798Sjhb				    pindex;
269160798Sjhb			}
270160798Sjhb			*after += numafter;
271160798Sjhb		}
272160798Sjhb	} else {
273160798Sjhb		if (before) {
274160798Sjhb			*before /= blocksperpage;
275160798Sjhb		}
276146806Srwatson
277160798Sjhb		if (after) {
278160798Sjhb			*after /= blocksperpage;
279146806Srwatson		}
280160798Sjhb	}
281160798Sjhb	return TRUE;
282160798Sjhb}
283146806Srwatson
284146806Srwatson/*
285160798Sjhb * Lets the VM system know about a change in size for a file.
286146806Srwatson * We adjust our own internal size and flush any cached pages in
287160798Sjhb * the associated object that are affected by the size change.
288146806Srwatson *
289160798Sjhb * Note: this routine may be invoked as a result of a pager put
290160798Sjhb * operation (possibly at object termination time), so we must be careful.
291160798Sjhb */
292146806Srwatsonvoid
293160798Sjhbvnode_pager_setsize(vp, nsize)
294146806Srwatson	struct vnode *vp;
295160798Sjhb	vm_ooffset_t nsize;
296160798Sjhb{
297160798Sjhb	vm_object_t object;
298146806Srwatson	vm_page_t m;
299160798Sjhb	vm_pindex_t nobjsize;
300194390Sjhb
301146806Srwatson	if ((object = vp->v_object) == NULL)
302146806Srwatson		return;
3031541Srgrimes	VM_OBJECT_LOCK(object);
3041541Srgrimes	if (nsize == object->un_pager.vnp.vnp_size) {
3051541Srgrimes		/*
3061541Srgrimes		 * Hasn't changed size
3071541Srgrimes		 */
308146806Srwatson		VM_OBJECT_UNLOCK(object);
309146806Srwatson		return;
310146806Srwatson	}
311177633Sdfr	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
312177633Sdfr	if (nsize < object->un_pager.vnp.vnp_size) {
31330740Sphk		/*
314161325Sjhb		 * File has shrunk. Toss any cached pages beyond the new EOF.
315160798Sjhb		 */
316146806Srwatson		if (nobjsize < object->size)
317160798Sjhb			vm_object_page_remove(object, nobjsize, object->size,
318146806Srwatson			    FALSE);
319160798Sjhb		/*
320146806Srwatson		 * this gets rid of garbage at the end of a page that is now
321146806Srwatson		 * only partially backed by the vnode.
322160798Sjhb		 *
323146806Srwatson		 * XXX for some reason (I don't know yet), if we take a
324160798Sjhb		 * completely invalid page and mark it partially valid
325146806Srwatson		 * it can screw up NFS reads, so we don't allow the case.
326184789Sed		 */
327146806Srwatson		if ((nsize & PAGE_MASK) &&
328184789Sed		    (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL) {
329146806Srwatson			vm_page_lock_queues();
330184789Sed			if (m->valid) {
331161952Srwatson				int base = (int)nsize & PAGE_MASK;
332161952Srwatson				int size = PAGE_SIZE - base;
333146806Srwatson
334146806Srwatson				/*
335146806Srwatson				 * Clear out partial-page garbage in case
336160798Sjhb				 * the page has been mapped.
337146806Srwatson				 */
338123750Speter				pmap_zero_page_area(m, base, size);
339160798Sjhb
340146806Srwatson				/*
341123750Speter				 * XXX work around SMP data integrity race
342160798Sjhb				 * by unmapping the page from user processes.
343146806Srwatson				 * The garbage we just cleared may be mapped
344123750Speter				 * to a user process running on another cpu
345146806Srwatson				 * and this code is not running through normal
346171209Speter				 * I/O channels which handle SMP issues for
347146806Srwatson				 * us, so unmap page to synchronize all cpus.
348171209Speter				 *
349171209Speter				 * XXX should vm_pager_unmap_page() have
350146806Srwatson				 * dealt with this?
351178888Sjulian				 */
352161946Srwatson				pmap_remove_all(m);
353146806Srwatson
354146806Srwatson				/*
355146806Srwatson				 * Clear out partial-page dirty bits.  This
356146806Srwatson				 * has the side effect of setting the valid
3571541Srgrimes				 * bits, but that is ok.  There are a bunch
35849428Sjkh				 * of places in the VM system where we expected
359160798Sjhb				 * m->dirty == VM_PAGE_BITS_ALL.  The file EOF
360160798Sjhb				 * case is one of them.  If the page is still
361160798Sjhb				 * partially dirty, make it fully dirty.
362146806Srwatson				 *
363146806Srwatson				 * note that we do not clear out the valid
364146806Srwatson				 * bits.  This would prevent bogus_page
365146806Srwatson				 * replacement from working properly.
366160798Sjhb				 */
367160798Sjhb				vm_page_set_validclean(m, base, size);
368160798Sjhb				if (m->dirty != 0)
369160798Sjhb					m->dirty = VM_PAGE_BITS_ALL;
370160798Sjhb			}
371146806Srwatson			vm_page_unlock_queues();
372160798Sjhb		}
373146806Srwatson	}
374146806Srwatson	object->un_pager.vnp.vnp_size = nsize;
375160798Sjhb	object->size = nobjsize;
376146806Srwatson	VM_OBJECT_UNLOCK(object);
377146806Srwatson}
378160798Sjhb
379146806Srwatson/*
380171209Speter * calculate the linear (byte) disk address of specified virtual
381171209Speter * file address
382171209Speter */
383183361Sjhbstatic vm_offset_t
384146806Srwatsonvnode_pager_addr(vp, address, run)
385171209Speter	struct vnode *vp;
386171209Speter	vm_ooffset_t address;
387171209Speter	int *run;
388146806Srwatson{
389171209Speter	int rtaddress;
390146806Srwatson	int bsize;
391160798Sjhb	daddr_t block;
392146806Srwatson	int err;
393146806Srwatson	daddr_t vblock;
394160798Sjhb	int voffset;
395160798Sjhb
396160798Sjhb	GIANT_REQUIRED;
397160798Sjhb	if ((int) address < 0)
398160798Sjhb		return -1;
399146806Srwatson
400160798Sjhb	if (vp->v_mount == NULL)
401146806Srwatson		return -1;
4022124Sdg
4032124Sdg	bsize = vp->v_mount->mnt_stat.f_iosize;
4042124Sdg	vblock = address / bsize;
4052124Sdg	voffset = address % bsize;
406209579Skib
407209579Skib	err = VOP_BMAP(vp, vblock, NULL, &block, run, NULL);
408209579Skib
409209579Skib	if (err || (block == -1))
410209579Skib		rtaddress = -1;
411209579Skib	else {
412209579Skib		rtaddress = block + voffset / DEV_BSIZE;
413209579Skib		if (run) {
414209579Skib			*run += 1;
415209579Skib			*run *= bsize/PAGE_SIZE;
41612864Speter			*run -= voffset/PAGE_SIZE;
41712864Speter		}
41814215Speter	}
419194910Sjhb
420194910Sjhb	return rtaddress;
421160798Sjhb}
422146806Srwatson
423160798Sjhb/*
424146806Srwatson * interrupt routine for I/O completion
425146806Srwatson */
426194910Sjhbstatic void
427194910Sjhbvnode_pager_iodone(bp)
428160798Sjhb	struct buf *bp;
429160798Sjhb{
430146806Srwatson	bp->b_flags |= B_DONE;
431160798Sjhb	wakeup(bp);
432146806Srwatson}
433160798Sjhb
434146806Srwatson/*
435194910Sjhb * small block filesystem vnode pager input
436194910Sjhb */
437160798Sjhbstatic int
438160798Sjhbvnode_pager_input_smlfs(object, m)
439146806Srwatson	vm_object_t object;
44014219Speter	vm_page_t m;
441160798Sjhb{
442146806Srwatson	int i;
443161952Srwatson	int s;
444161952Srwatson	struct vnode *dp, *vp;
445146806Srwatson	struct buf *bp;
446160798Sjhb	vm_offset_t kva;
447146806Srwatson	int fileaddr;
448160798Sjhb	vm_offset_t bsize;
449156134Sdavidxu	int error = 0;
450160798Sjhb
451160798Sjhb	GIANT_REQUIRED;
452151576Sdavidxu
453151576Sdavidxu	vp = object->handle;
454160798Sjhb	if (vp->v_mount == NULL)
455151576Sdavidxu		return VM_PAGER_BAD;
456160798Sjhb
457160798Sjhb	bsize = vp->v_mount->mnt_stat.f_iosize;
458146806Srwatson
459146806Srwatson	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
460146806Srwatson
461146806Srwatson	kva = vm_pager_map_page(m);
462146806Srwatson
463146806Srwatson	for (i = 0; i < PAGE_SIZE / bsize; i++) {
464146806Srwatson		vm_ooffset_t address;
465146806Srwatson
466160798Sjhb		if (vm_page_bits(i * bsize, bsize) & m->valid)
467146806Srwatson			continue;
46814219Speter
469160798Sjhb		address = IDX_TO_OFF(m->pindex) + i * bsize;
470146806Srwatson		if (address >= object->un_pager.vnp.vnp_size) {
471160798Sjhb			fileaddr = -1;
472160798Sjhb		} else {
473146806Srwatson			fileaddr = vnode_pager_addr(vp, address, NULL);
474160798Sjhb		}
475160798Sjhb		if (fileaddr != -1) {
476160798Sjhb			bp = getpbuf(&vnode_pbuf_freecnt);
477160798Sjhb
478160798Sjhb			/* build a minimal buffer header */
479151867Sdavidxu			bp->b_iocmd = BIO_READ;
480151867Sdavidxu			bp->b_iodone = vnode_pager_iodone;
481152845Sdavidxu			KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
482152845Sdavidxu			KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
483152845Sdavidxu			bp->b_rcred = crhold(curthread->td_ucred);
484152845Sdavidxu			bp->b_wcred = crhold(curthread->td_ucred);
485152845Sdavidxu			bp->b_data = (caddr_t) kva + i * bsize;
486152845Sdavidxu			bp->b_blkno = fileaddr;
487146806Srwatson			pbgetvp(dp, bp);
488146806Srwatson			bp->b_bcount = bsize;
489146806Srwatson			bp->b_bufsize = bsize;
490146806Srwatson			bp->b_runningbufspace = bp->b_bufsize;
491146806Srwatson			runningbufspace += bp->b_runningbufspace;
492146806Srwatson
493146806Srwatson			/* do the input */
494146806Srwatson			VOP_SPECSTRATEGY(bp->b_vp, bp);
495160798Sjhb
496146806Srwatson			/* we definitely need to be at splvm here */
497146806Srwatson
498160798Sjhb			s = splvm();
499160798Sjhb			while ((bp->b_flags & B_DONE) == 0) {
500146806Srwatson				tsleep(bp, PVM, "vnsrd", 0);
501146806Srwatson			}
502160798Sjhb			splx(s);
503146806Srwatson			if ((bp->b_ioflags & BIO_ERROR) != 0)
504160798Sjhb				error = EIO;
505146806Srwatson
506160798Sjhb			/*
507160798Sjhb			 * free the buffer header back to the swap buffer pool
508160798Sjhb			 */
509146806Srwatson			relpbuf(bp, &vnode_pbuf_freecnt);
510146806Srwatson			if (error)
511146806Srwatson				break;
512146806Srwatson
513146806Srwatson			vm_page_lock_queues();
514146806Srwatson			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
515146806Srwatson			vm_page_unlock_queues();
516146806Srwatson		} else {
517147813Sjhb			vm_page_lock_queues();
518161952Srwatson			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
519147813Sjhb			vm_page_unlock_queues();
520161952Srwatson			bzero((caddr_t) kva + i * bsize, bsize);
521147813Sjhb		}
522146806Srwatson	}
523146806Srwatson	vm_pager_unmap_page(kva);
524146806Srwatson	vm_page_lock_queues();
525146806Srwatson	pmap_clear_modify(m);
526146806Srwatson	vm_page_flag_clear(m, PG_ZERO);
527146806Srwatson	vm_page_unlock_queues();
52851138Salfred	if (error) {
529160798Sjhb		return VM_PAGER_ERROR;
530146806Srwatson	}
531146806Srwatson	return VM_PAGER_OK;
532160798Sjhb
533146806Srwatson}
534160798Sjhb
535146806Srwatson
53625537Sdfr/*
537160798Sjhb * old style vnode pager output routine
538160798Sjhb */
539146806Srwatsonstatic int
540160798Sjhbvnode_pager_input_old(object, m)
541160798Sjhb	vm_object_t object;
542160798Sjhb	vm_page_t m;
543160798Sjhb{
544160798Sjhb	struct uio auio;
545160798Sjhb	struct iovec aiov;
546160798Sjhb	int error;
547146806Srwatson	int size;
548160798Sjhb	vm_offset_t kva;
549160798Sjhb	struct vnode *vp;
550160798Sjhb
551146806Srwatson	GIANT_REQUIRED;
552160798Sjhb	error = 0;
553146806Srwatson
554146806Srwatson	/*
555160798Sjhb	 * Return failure if beyond current EOF
556160798Sjhb	 */
557146806Srwatson	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
558146806Srwatson		return VM_PAGER_BAD;
559160798Sjhb	} else {
560146806Srwatson		size = PAGE_SIZE;
561160798Sjhb		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
562160798Sjhb			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
563160798Sjhb
564160798Sjhb		/*
565151867Sdavidxu		 * Allocate a kernel virtual address and initialize so that
566151867Sdavidxu		 * we can use VOP_READ/WRITE routines.
567160798Sjhb		 */
568146806Srwatson		kva = vm_pager_map_page(m);
569146806Srwatson
570160798Sjhb		vp = object->handle;
571160798Sjhb		aiov.iov_base = (caddr_t) kva;
572161952Srwatson		aiov.iov_len = size;
57334925Sdufault		auio.uio_iov = &aiov;
574160798Sjhb		auio.uio_iovcnt = 1;
575146806Srwatson		auio.uio_offset = IDX_TO_OFF(m->pindex);
576160798Sjhb		auio.uio_segflg = UIO_SYSSPACE;
577146806Srwatson		auio.uio_rw = UIO_READ;
57834925Sdufault		auio.uio_resid = size;
579160798Sjhb		auio.uio_td = curthread;
580146806Srwatson
581146806Srwatson		error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
582160798Sjhb		if (!error) {
58334925Sdufault			int count = size - auio.uio_resid;
584160798Sjhb
585160798Sjhb			if (count == 0)
586160798Sjhb				error = EINVAL;
587160798Sjhb			else if (count != PAGE_SIZE)
588146806Srwatson				bzero((caddr_t) kva + count, PAGE_SIZE - count);
589160798Sjhb		}
590160798Sjhb		vm_pager_unmap_page(kva);
591146806Srwatson	}
592146806Srwatson	vm_page_lock_queues();
593146806Srwatson	pmap_clear_modify(m);
594160798Sjhb	vm_page_undirty(m);
595146806Srwatson	vm_page_flag_clear(m, PG_ZERO);
596160798Sjhb	if (!error)
597211998Skib		m->valid = VM_PAGE_BITS_ALL;
598211998Skib	vm_page_unlock_queues();
599211998Skib	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
600160798Sjhb}
601146806Srwatson
602160798Sjhb/*
603160798Sjhb * generic vnode pager input routine
604146806Srwatson */
605146806Srwatson
606160798Sjhb/*
607160798Sjhb * Local media VFS's that do not implement their own VOP_GETPAGES
608146806Srwatson * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
609160798Sjhb * to implement the previous behaviour.
610146806Srwatson *
611146806Srwatson * All other FS's should use the bypass to get to the local media
612160798Sjhb * backing vp's VOP_GETPAGES.
613146806Srwatson */
614160798Sjhbstatic int
615146806Srwatsonvnode_pager_getpages(object, m, count, reqpage)
616160798Sjhb	vm_object_t object;
617146806Srwatson	vm_page_t *m;
618160798Sjhb	int count;
619146806Srwatson	int reqpage;
620160798Sjhb{
621146806Srwatson	int rtval;
622160798Sjhb	struct vnode *vp;
623146806Srwatson	int bytes = count * PAGE_SIZE;
624160798Sjhb
625146806Srwatson	vp = object->handle;
626160798Sjhb	VM_OBJECT_UNLOCK(object);
627146806Srwatson	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
628160798Sjhb	KASSERT(rtval != EOPNOTSUPP,
629146806Srwatson	    ("vnode_pager: FS getpages not implemented\n"));
630160798Sjhb	VM_OBJECT_LOCK(object);
631146806Srwatson	return rtval;
632146806Srwatson}
633160798Sjhb
634160111Swsalamon/*
635160111Swsalamon * This is now called from local media FS's to operate against their
636160111Swsalamon * own vnodes if they fail to implement VOP_GETPAGES.
637160798Sjhb */
638160111Swsalamonint
639160111Swsalamonvnode_pager_generic_getpages(vp, m, bytecount, reqpage)
640160111Swsalamon	struct vnode *vp;
641160798Sjhb	vm_page_t *m;
642146806Srwatson	int bytecount;
643146806Srwatson	int reqpage;
644160798Sjhb{
645146806Srwatson	vm_object_t object;
646146806Srwatson	vm_offset_t kva;
647160798Sjhb	off_t foff, tfoff, nextoff;
648146806Srwatson	int i, j, size, bsize, first, firstaddr;
649160798Sjhb	struct vnode *dp;
650146806Srwatson	int runpg;
651161952Srwatson	int runend;
652160798Sjhb	struct buf *bp;
653146806Srwatson	int s;
654146806Srwatson	int count;
655146806Srwatson	int error = 0;
656146806Srwatson
657146806Srwatson	GIANT_REQUIRED;
658146806Srwatson	object = vp->v_object;
659146806Srwatson	count = bytecount / PAGE_SIZE;
660146806Srwatson
661146806Srwatson	if (vp->v_mount == NULL)
662183361Sjhb		return VM_PAGER_BAD;
663160798Sjhb
664146806Srwatson	bsize = vp->v_mount->mnt_stat.f_iosize;
665146806Srwatson
666160798Sjhb	/* get the UNDERLYING device for the file with VOP_BMAP() */
667146806Srwatson
668146806Srwatson	/*
669160798Sjhb	 * originally, we did not check for an error return value -- assuming
670146806Srwatson	 * an fs always has a bmap entry point -- that assumption is wrong!!!
671146806Srwatson	 */
672160798Sjhb	foff = IDX_TO_OFF(m[reqpage]->pindex);
673194383Sjhb
674160798Sjhb	/*
675211998Skib	 * if we can't bmap, use old VOP code
676211998Skib	 */
677211998Skib	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
678160798Sjhb		VM_OBJECT_LOCK(object);
679146806Srwatson		vm_page_lock_queues();
680177091Sjeff		for (i = 0; i < count; i++)
681177091Sjeff			if (i != reqpage)
682177091Sjeff				vm_page_free(m[i]);
683177091Sjeff		vm_page_unlock_queues();
684177091Sjeff		VM_OBJECT_UNLOCK(object);
685160798Sjhb		cnt.v_vnodein++;
686160798Sjhb		cnt.v_vnodepgsin++;
687160798Sjhb		return vnode_pager_input_old(object, m[reqpage]);
688146806Srwatson
689160798Sjhb		/*
690146806Srwatson		 * if the blocksize is smaller than a page size, then use
691160798Sjhb		 * special small filesystem code.  NFS sometimes has a small
692146806Srwatson		 * blocksize, but it can handle large reads itself.
693160798Sjhb		 */
694146806Srwatson	} else if ((PAGE_SIZE / bsize) > 1 &&
695160798Sjhb	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
696146806Srwatson		VM_OBJECT_LOCK(object);
697160798Sjhb		vm_page_lock_queues();
698160798Sjhb		for (i = 0; i < count; i++)
699146806Srwatson			if (i != reqpage)
700160798Sjhb				vm_page_free(m[i]);
701146806Srwatson		vm_page_unlock_queues();
702146806Srwatson		VM_OBJECT_UNLOCK(object);
703160798Sjhb		cnt.v_vnodein++;
704146806Srwatson		cnt.v_vnodepgsin++;
705160798Sjhb		return vnode_pager_input_smlfs(object, m[reqpage]);
706146806Srwatson	}
707160798Sjhb
708146806Srwatson	/*
709160798Sjhb	 * If we have a completely valid page available to us, we can
710161952Srwatson	 * clean up and return.  Otherwise we have to re-read the
711146806Srwatson	 * media.
712146806Srwatson	 */
713160798Sjhb	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
714160798Sjhb		VM_OBJECT_LOCK(object);
715160798Sjhb		vm_page_lock_queues();
716160798Sjhb		for (i = 0; i < count; i++)
717160798Sjhb			if (i != reqpage)
718146806Srwatson				vm_page_free(m[i]);
719160798Sjhb		vm_page_unlock_queues();
720146806Srwatson		VM_OBJECT_UNLOCK(object);
721146806Srwatson		return VM_PAGER_OK;
722160798Sjhb	}
723160798Sjhb	m[reqpage]->valid = 0;
724160798Sjhb
725160798Sjhb	/*
726146806Srwatson	 * here on direct device I/O
727160798Sjhb	 */
728146806Srwatson	firstaddr = -1;
729160798Sjhb
730146806Srwatson	/*
731160798Sjhb	 * calculate the run that includes the required page
732160111Swsalamon	 */
733160111Swsalamon	for (first = 0, i = 0; i < count; i = runend) {
734160111Swsalamon		firstaddr = vnode_pager_addr(vp,
735160798Sjhb			IDX_TO_OFF(m[i]->pindex), &runpg);
736160111Swsalamon		if (firstaddr == -1) {
737160111Swsalamon			VM_OBJECT_LOCK(object);
738160111Swsalamon			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
739160798Sjhb				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
740160111Swsalamon				    firstaddr, (uintmax_t)(foff >> 32),
741146806Srwatson				    (uintmax_t)foff,
742160798Sjhb				    (uintmax_t)
743146806Srwatson				    (object->un_pager.vnp.vnp_size >> 32),
744160798Sjhb				    (uintmax_t)object->un_pager.vnp.vnp_size);
745146806Srwatson			}
746146806Srwatson			vm_page_lock_queues();
747160798Sjhb			vm_page_free(m[i]);
748146806Srwatson			vm_page_unlock_queues();
749146806Srwatson			VM_OBJECT_UNLOCK(object);
750146806Srwatson			runend = i + 1;
751146806Srwatson			first = runend;
752160798Sjhb			continue;
753160798Sjhb		}
754146806Srwatson		runend = i + runpg;
755160798Sjhb		if (runend <= reqpage) {
756146806Srwatson			VM_OBJECT_LOCK(object);
757160798Sjhb			vm_page_lock_queues();
758160798Sjhb			for (j = i; j < runend; j++)
759146806Srwatson				vm_page_free(m[j]);
760160798Sjhb			vm_page_unlock_queues();
761146806Srwatson			VM_OBJECT_UNLOCK(object);
762160798Sjhb		} else {
763146806Srwatson			if (runpg < (count - first)) {
764160798Sjhb				VM_OBJECT_LOCK(object);
765146806Srwatson				vm_page_lock_queues();
766160798Sjhb				for (i = first + runpg; i < count; i++)
767146806Srwatson					vm_page_free(m[i]);
768160798Sjhb				vm_page_unlock_queues();
769146806Srwatson				VM_OBJECT_UNLOCK(object);
770160798Sjhb				count = first + runpg;
771160798Sjhb			}
772160798Sjhb			break;
773160798Sjhb		}
774160798Sjhb		first = runend;
775160798Sjhb	}
776160798Sjhb
777146806Srwatson	/*
778146806Srwatson	 * the first and last page have been calculated now, move input pages
779160798Sjhb	 * to be zero based...
780146806Srwatson	 */
781146806Srwatson	if (first != 0) {
782160798Sjhb		for (i = first; i < count; i++) {
783146806Srwatson			m[i - first] = m[i];
784146806Srwatson		}
785177091Sjeff		count -= first;
786160798Sjhb		reqpage -= first;
787151445Sstefanf	}
788160798Sjhb
789146806Srwatson	/*
790160798Sjhb	 * calculate the file virtual address for the transfer
791161952Srwatson	 */
792160798Sjhb	foff = IDX_TO_OFF(m[0]->pindex);
793146806Srwatson
794160798Sjhb	/*
795146806Srwatson	 * calculate the size of the transfer
796160798Sjhb	 */
797160798Sjhb	size = count * PAGE_SIZE;
798160798Sjhb	if ((foff + size) > object->un_pager.vnp.vnp_size)
799160798Sjhb		size = object->un_pager.vnp.vnp_size - foff;
800160798Sjhb
801146806Srwatson	/*
802146806Srwatson	 * round up physical size for real devices.
803160798Sjhb	 */
804146806Srwatson	if (dp->v_type == VBLK || dp->v_type == VCHR) {
805146806Srwatson		int secmask = dp->v_rdev->si_bsize_phys - 1;
806160798Sjhb		KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
807161678Sdavidxu		size = (size + secmask) & ~secmask;
808163449Sdavidxu	}
809160798Sjhb
810146806Srwatson	bp = getpbuf(&vnode_pbuf_freecnt);
811160798Sjhb	kva = (vm_offset_t) bp->b_data;
812160798Sjhb
813152845Sdavidxu	/*
814160798Sjhb	 * and map the pages to be read into the kva
815152845Sdavidxu	 */
816152845Sdavidxu	pmap_qenter(kva, m, count);
817160798Sjhb
818152845Sdavidxu	/* build a minimal buffer header */
819152845Sdavidxu	bp->b_iocmd = BIO_READ;
820152845Sdavidxu	bp->b_iodone = vnode_pager_iodone;
821160798Sjhb	/* B_PHYS is not set, but it is nice to fill this in */
822152845Sdavidxu	KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
823152845Sdavidxu	KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
824152845Sdavidxu	bp->b_rcred = crhold(curthread->td_ucred);
825160798Sjhb	bp->b_wcred = crhold(curthread->td_ucred);
826152845Sdavidxu	bp->b_blkno = firstaddr;
827160798Sjhb	pbgetvp(dp, bp);
828160798Sjhb	bp->b_bcount = size;
829160798Sjhb	bp->b_bufsize = size;
830160798Sjhb	bp->b_runningbufspace = bp->b_bufsize;
831162497Sdavidxu	runningbufspace += bp->b_runningbufspace;
832162497Sdavidxu
833162497Sdavidxu	cnt.v_vnodein++;
834162497Sdavidxu	cnt.v_vnodepgsin += count;
835161367Speter
836161367Speter	/* do the input */
837163953Srrs	if (dp->v_type == VCHR)
838163953Srrs		VOP_SPECSTRATEGY(bp->b_vp, bp);
839163953Srrs	else
840163953Srrs		VOP_STRATEGY(bp->b_vp, bp);
841163953Srrs
842163953Srrs	s = splvm();
843163953Srrs	/* we definitely need to be at splvm here */
844163953Srrs
845163953Srrs	while ((bp->b_flags & B_DONE) == 0) {
846163953Srrs		tsleep(bp, PVM, "vnread", 0);
847171209Speter	}
848171209Speter	splx(s);
849171209Speter	if ((bp->b_ioflags & BIO_ERROR) != 0)
850171209Speter		error = EIO;
851171209Speter
852171209Speter	if (!error) {
853171209Speter		if (size != count * PAGE_SIZE)
854171209Speter			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
855171209Speter	}
856171209Speter	pmap_qremove(kva, count);
857171859Sdavidxu
858175517Srwatson	/*
859175164Sjhb	 * free the buffer header back to the swap buffer pool
860175517Srwatson	 */
861176730Sjeff	relpbuf(bp, &vnode_pbuf_freecnt);
862176730Sjeff
863176730Sjeff	VM_OBJECT_LOCK(object);
864176730Sjeff	vm_page_lock_queues();
865176730Sjeff	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
866176730Sjeff		vm_page_t mt;
867176730Sjeff
868177597Sru		nextoff = tfoff + PAGE_SIZE;
869177597Sru		mt = m[i];
870176730Sjeff
871177597Sru		if (nextoff <= object->un_pager.vnp.vnp_size) {
872177597Sru			/*
873177788Skib			 * Read filled up entire page.
874177788Skib			 */
875177788Skib			mt->valid = VM_PAGE_BITS_ALL;
876177788Skib			vm_page_undirty(mt);	/* should be an assert? XXX */
877177788Skib			pmap_clear_modify(mt);
878177788Skib		} else {
879177788Skib			/*
880177788Skib			 * Read did not fill up entire page.  Since this
881177788Skib			 * is getpages, the page may be mapped, so we have
882177788Skib			 * to zero the invalid portions of the page even
883177788Skib			 * though we aren't setting them valid.
884177788Skib			 *
885177788Skib			 * Currently we do not set the entire page valid,
886177788Skib			 * we just try to clear the piece that we couldn't
887177788Skib			 * read.
888177788Skib			 */
889177788Skib			vm_page_set_validclean(mt, 0,
890177788Skib			    object->un_pager.vnp.vnp_size - tfoff);
891177788Skib			/* handled by vm_fault now */
892177788Skib			/* vm_page_zero_invalid(mt, FALSE); */
893177788Skib		}
894177788Skib
895177788Skib		vm_page_flag_clear(mt, PG_ZERO);
896177788Skib		if (i != reqpage) {
897177788Skib
898177788Skib			/*
899177788Skib			 * whether or not to leave the page activated is up in
900177788Skib			 * the air, but we should put the page on a page queue
901182123Srwatson			 * somewhere. (it already is in the object). Result:
902184588Sdfr			 * It appears that empirical results show that
903184588Sdfr			 * deactivating pages is best.
904191673Sjamie			 */
905191673Sjamie
906191673Sjamie			/*
907191673Sjamie			 * just in case someone was asking for this page we
908191673Sjamie			 * now tell them that it is ok to use
909194262Sjhb			 */
910194910Sjhb			if (!error) {
911194910Sjhb				if (mt->flags & PG_WANTED)
912194910Sjhb					vm_page_activate(mt);
913194910Sjhb				else
914194910Sjhb					vm_page_deactivate(mt);
915194910Sjhb				vm_page_wakeup(mt);
916195458Strasz			} else {
917224066Sjonathan				vm_page_free(mt);
918224066Sjonathan			}
919224066Sjonathan		}
920219129Srwatson	}
921219129Srwatson	vm_page_unlock_queues();
922224987Sjonathan	VM_OBJECT_UNLOCK(object);
923224987Sjonathan	if (error) {
924224987Sjonathan		printf("vnode_pager_getpages: I/O read error\n");
925224987Sjonathan	}
926198508Skib	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
927198508Skib}
928198508Skib
929198508Skib/*
930219304Strasz * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
931219304Strasz * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
932219304Strasz * vnode_pager_generic_putpages() to implement the previous behaviour.
933220163Strasz *
934220163Strasz * All other FS's should use the bypass to get to the local media
935220163Strasz * backing vp's VOP_PUTPAGES.
936220163Strasz */
937220163Straszstatic void
938220163Straszvnode_pager_putpages(object, m, count, sync, rtvals)
939220163Strasz	vm_object_t object;
940220163Strasz	vm_page_t *m;
941220163Strasz	int count;
942220163Strasz	boolean_t sync;
943220163Strasz	int *rtvals;
944220163Strasz{
945220163Strasz	int rtval;
946220163Strasz	struct vnode *vp;
947220163Strasz	struct mount *mp;
948220791Smdf	int bytes = count * PAGE_SIZE;
949220791Smdf
950220791Smdf	GIANT_REQUIRED;
951105144Speter	/*
952123408Speter	 * Force synchronous operation if we are extremely low on memory
953	 * to prevent a low-memory deadlock.  VOP operations often need to
954	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
955	 * operation ).  The swapper handles the case by limiting the amount
956	 * of asynchronous I/O, but that sort of solution doesn't scale well
957	 * for the vnode pager without a lot of work.
958	 *
959	 * Also, the backing vnode's iodone routine may not wake the pageout
960	 * daemon up.  This should be probably be addressed XXX.
961	 */
962
963	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
964		sync |= OBJPC_SYNC;
965
966	/*
967	 * Call device-specific putpages function
968	 */
969	vp = object->handle;
970	if (vp->v_type != VREG)
971		mp = NULL;
972	(void)vn_start_write(vp, &mp, V_WAIT);
973	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
974	KASSERT(rtval != EOPNOTSUPP,
975	    ("vnode_pager: stale FS putpages\n"));
976	vn_finished_write(mp);
977}
978
979
980/*
981 * This is now called from local media FS's to operate against their
982 * own vnodes if they fail to implement VOP_PUTPAGES.
983 *
984 * This is typically called indirectly via the pageout daemon and
985 * clustering has already typically occured, so in general we ask the
986 * underlying filesystem to write the data out asynchronously rather
987 * then delayed.
988 */
989int
990vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
991	struct vnode *vp;
992	vm_page_t *m;
993	int bytecount;
994	int flags;
995	int *rtvals;
996{
997	int i;
998	vm_object_t object;
999	int count;
1000
1001	int maxsize, ncount;
1002	vm_ooffset_t poffset;
1003	struct uio auio;
1004	struct iovec aiov;
1005	int error;
1006	int ioflags;
1007
1008	GIANT_REQUIRED;
1009	object = vp->v_object;
1010	count = bytecount / PAGE_SIZE;
1011
1012	for (i = 0; i < count; i++)
1013		rtvals[i] = VM_PAGER_AGAIN;
1014
1015	if ((int) m[0]->pindex < 0) {
1016		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n",
1017			(long)m[0]->pindex, m[0]->dirty);
1018		rtvals[0] = VM_PAGER_BAD;
1019		return VM_PAGER_BAD;
1020	}
1021
1022	maxsize = count * PAGE_SIZE;
1023	ncount = count;
1024
1025	poffset = IDX_TO_OFF(m[0]->pindex);
1026
1027	/*
1028	 * If the page-aligned write is larger then the actual file we
1029	 * have to invalidate pages occuring beyond the file EOF.  However,
1030	 * there is an edge case where a file may not be page-aligned where
1031	 * the last page is partially invalid.  In this case the filesystem
1032	 * may not properly clear the dirty bits for the entire page (which
1033	 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1034	 * With the page locked we are free to fix-up the dirty bits here.
1035	 *
1036	 * We do not under any circumstances truncate the valid bits, as
1037	 * this will screw up bogus page replacement.
1038	 */
1039	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1040		if (object->un_pager.vnp.vnp_size > poffset) {
1041			int pgoff;
1042
1043			maxsize = object->un_pager.vnp.vnp_size - poffset;
1044			ncount = btoc(maxsize);
1045			if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1046				vm_page_clear_dirty(m[ncount - 1], pgoff,
1047					PAGE_SIZE - pgoff);
1048			}
1049		} else {
1050			maxsize = 0;
1051			ncount = 0;
1052		}
1053		if (ncount < count) {
1054			for (i = ncount; i < count; i++) {
1055				rtvals[i] = VM_PAGER_BAD;
1056			}
1057		}
1058	}
1059
1060	/*
1061	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
1062	 * rather then a bdwrite() to prevent paging I/O from saturating
1063	 * the buffer cache.  Dummy-up the sequential heuristic to cause
1064	 * large ranges to cluster.  If neither IO_SYNC or IO_ASYNC is set,
1065	 * the system decides how to cluster.
1066	 */
1067	ioflags = IO_VMIO;
1068	if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1069		ioflags |= IO_SYNC;
1070	else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1071		ioflags |= IO_ASYNC;
1072	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1073	ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1074
1075	aiov.iov_base = (caddr_t) 0;
1076	aiov.iov_len = maxsize;
1077	auio.uio_iov = &aiov;
1078	auio.uio_iovcnt = 1;
1079	auio.uio_offset = poffset;
1080	auio.uio_segflg = UIO_NOCOPY;
1081	auio.uio_rw = UIO_WRITE;
1082	auio.uio_resid = maxsize;
1083	auio.uio_td = (struct thread *) 0;
1084	error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1085	cnt.v_vnodeout++;
1086	cnt.v_vnodepgsout += ncount;
1087
1088	if (error) {
1089		printf("vnode_pager_putpages: I/O error %d\n", error);
1090	}
1091	if (auio.uio_resid) {
1092		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1093		    auio.uio_resid, (u_long)m[0]->pindex);
1094	}
1095	for (i = 0; i < ncount; i++) {
1096		rtvals[i] = VM_PAGER_OK;
1097	}
1098	return rtvals[0];
1099}
1100
1101struct vnode *
1102vnode_pager_lock(object)
1103	vm_object_t object;
1104{
1105	struct thread *td = curthread;	/* XXX */
1106
1107	GIANT_REQUIRED;
1108
1109	for (; object != NULL; object = object->backing_object) {
1110		if (object->type != OBJT_VNODE)
1111			continue;
1112		if (object->flags & OBJ_DEAD) {
1113			return NULL;
1114		}
1115
1116		/* XXX; If object->handle can change, we need to cache it. */
1117		while (vget(object->handle,
1118			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, td)){
1119			if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE))
1120				return NULL;
1121			printf("vnode_pager_lock: retrying\n");
1122		}
1123		return object->handle;
1124	}
1125	return NULL;
1126}
1127