vnode_pager.c revision 77094
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 * $FreeBSD: head/sys/vm/vnode_pager.c 77094 2001-05-23 22:51:23Z jhb $
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/proc.h>
57#include <sys/vnode.h>
58#include <sys/mount.h>
59#include <sys/bio.h>
60#include <sys/buf.h>
61#include <sys/vmmeter.h>
62#include <sys/conf.h>
63
64#include <vm/vm.h>
65#include <vm/vm_object.h>
66#include <vm/vm_page.h>
67#include <vm/vm_pager.h>
68#include <vm/vm_map.h>
69#include <vm/vnode_pager.h>
70#include <vm/vm_extern.h>
71
72static vm_offset_t vnode_pager_addr __P((struct vnode *vp, vm_ooffset_t address,
73					 int *run));
74static void vnode_pager_iodone __P((struct buf *bp));
75static int vnode_pager_input_smlfs __P((vm_object_t object, vm_page_t m));
76static int vnode_pager_input_old __P((vm_object_t object, vm_page_t m));
77static void vnode_pager_dealloc __P((vm_object_t));
78static int vnode_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
79static void vnode_pager_putpages __P((vm_object_t, vm_page_t *, int, boolean_t, int *));
80static boolean_t vnode_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *));
81
82struct pagerops vnodepagerops = {
83	NULL,
84	vnode_pager_alloc,
85	vnode_pager_dealloc,
86	vnode_pager_getpages,
87	vnode_pager_putpages,
88	vnode_pager_haspage,
89	NULL
90};
91
92int vnode_pbuf_freecnt = -1;	/* start out unlimited */
93
94
95/*
96 * Allocate (or lookup) pager for a vnode.
97 * Handle is a vnode pointer.
98 */
99vm_object_t
100vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
101		  vm_ooffset_t offset)
102{
103	vm_object_t object;
104	struct vnode *vp;
105
106	mtx_assert(&Giant, MA_OWNED);
107	/*
108	 * Pageout to vnode, no can do yet.
109	 */
110	if (handle == NULL)
111		return (NULL);
112
113	/*
114	 * XXX hack - This initialization should be put somewhere else.
115	 */
116	if (vnode_pbuf_freecnt < 0) {
117	    vnode_pbuf_freecnt = nswbuf / 2 + 1;
118	}
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	mtx_unlock(&vm_mtx);
127	while (vp->v_flag & VOLOCK) {
128		vp->v_flag |= VOWANT;
129		tsleep(vp, PVM, "vnpobj", 0);
130	}
131	vp->v_flag |= VOLOCK;
132	mtx_lock(&vm_mtx);
133
134	/*
135	 * If the object is being terminated, wait for it to
136	 * go away.
137	 */
138	while (((object = vp->v_object) != NULL) &&
139		(object->flags & OBJ_DEAD)) {
140		msleep(object, &vm_mtx, PVM, "vadead", 0);
141	}
142
143	if (vp->v_usecount == 0)
144		panic("vnode_pager_alloc: no vnode reference");
145
146	if (object == NULL) {
147		/*
148		 * And an object of the appropriate size
149		 */
150		object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
151		object->flags = 0;
152
153		object->un_pager.vnp.vnp_size = size;
154
155		object->handle = handle;
156		vp->v_object = object;
157		vp->v_usecount++;
158	} else {
159		object->ref_count++;
160		vp->v_usecount++;
161	}
162
163	mtx_unlock(&vm_mtx);
164	vp->v_flag &= ~VOLOCK;
165	if (vp->v_flag & VOWANT) {
166		vp->v_flag &= ~VOWANT;
167		wakeup(vp);
168	}
169	mtx_lock(&vm_mtx);
170	return (object);
171}
172
173static void
174vnode_pager_dealloc(object)
175	vm_object_t object;
176{
177	register struct vnode *vp = object->handle;
178
179	mtx_assert(&Giant, MA_OWNED);
180	if (vp == NULL)
181		panic("vnode_pager_dealloc: pager already dealloced");
182
183	vm_object_pip_wait(object, "vnpdea");
184
185	object->handle = NULL;
186	object->type = OBJT_DEAD;
187	vp->v_object = NULL;
188	vp->v_flag &= ~(VTEXT | VOBJBUF);
189}
190
191static boolean_t
192vnode_pager_haspage(object, pindex, before, after)
193	vm_object_t object;
194	vm_pindex_t pindex;
195	int *before;
196	int *after;
197{
198	struct vnode *vp = object->handle;
199	daddr_t bn;
200	int err;
201	daddr_t reqblock;
202	int poff;
203	int bsize;
204	int pagesperblock, blocksperpage;
205
206	mtx_assert(&Giant, MA_OWNED);
207	/*
208	 * If no vp or vp is doomed or marked transparent to VM, we do not
209	 * have the page.
210	 */
211	if ((vp == NULL) || (vp->v_flag & VDOOMED))
212		return FALSE;
213
214	/*
215	 * If filesystem no longer mounted or offset beyond end of file we do
216	 * not have the page.
217	 */
218	if ((vp->v_mount == NULL) ||
219		(IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
220		return FALSE;
221
222	bsize = vp->v_mount->mnt_stat.f_iosize;
223	pagesperblock = bsize / PAGE_SIZE;
224	blocksperpage = 0;
225	if (pagesperblock > 0) {
226		reqblock = pindex / pagesperblock;
227	} else {
228		blocksperpage = (PAGE_SIZE / bsize);
229		reqblock = pindex * blocksperpage;
230	}
231	mtx_unlock(&vm_mtx);
232	err = VOP_BMAP(vp, reqblock, (struct vnode **) 0, &bn,
233		after, before);
234	mtx_lock(&vm_mtx);
235	if (err)
236		return TRUE;
237	if ( bn == -1)
238		return FALSE;
239	if (pagesperblock > 0) {
240		poff = pindex - (reqblock * pagesperblock);
241		if (before) {
242			*before *= pagesperblock;
243			*before += poff;
244		}
245		if (after) {
246			int numafter;
247			*after *= pagesperblock;
248			numafter = pagesperblock - (poff + 1);
249			if (IDX_TO_OFF(pindex + numafter) > object->un_pager.vnp.vnp_size) {
250				numafter = OFF_TO_IDX((object->un_pager.vnp.vnp_size - IDX_TO_OFF(pindex)));
251			}
252			*after += numafter;
253		}
254	} else {
255		if (before) {
256			*before /= blocksperpage;
257		}
258
259		if (after) {
260			*after /= blocksperpage;
261		}
262	}
263	return TRUE;
264}
265
266/*
267 * Lets the VM system know about a change in size for a file.
268 * We adjust our own internal size and flush any cached pages in
269 * the associated object that are affected by the size change.
270 *
271 * Note: this routine may be invoked as a result of a pager put
272 * operation (possibly at object termination time), so we must be careful.
273 */
274void
275vnode_pager_setsize(vp, nsize)
276	struct vnode *vp;
277	vm_ooffset_t nsize;
278{
279	vm_pindex_t nobjsize;
280	vm_object_t object = vp->v_object;
281
282	if (object == NULL)
283		return;
284
285	/*
286	 * Hasn't changed size
287	 */
288	if (nsize == object->un_pager.vnp.vnp_size)
289		return;
290
291	nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
292
293	/*
294	 * File has shrunk. Toss any cached pages beyond the new EOF.
295	 */
296	if (nsize < object->un_pager.vnp.vnp_size) {
297		int hadvmlock;
298
299		hadvmlock = mtx_owned(&vm_mtx);
300		if (!hadvmlock)
301			mtx_lock(&vm_mtx);
302		vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
303		if (nobjsize < object->size) {
304			vm_object_page_remove(object, nobjsize, object->size,
305				FALSE);
306		}
307		/*
308		 * this gets rid of garbage at the end of a page that is now
309		 * only partially backed by the vnode...
310		 */
311		if (nsize & PAGE_MASK) {
312			vm_offset_t kva;
313			vm_page_t m;
314
315			m = vm_page_lookup(object, OFF_TO_IDX(nsize));
316			if (m) {
317				int base = (int)nsize & PAGE_MASK;
318				int size = PAGE_SIZE - base;
319
320				/*
321				 * Clear out partial-page garbage in case
322				 * the page has been mapped.
323				 */
324				kva = vm_pager_map_page(m);
325				bzero((caddr_t)kva + base, size);
326				vm_pager_unmap_page(kva);
327
328				/*
329				 * Clear out partial-page dirty bits.  This
330				 * has the side effect of setting the valid
331				 * bits, but that is ok.  There are a bunch
332				 * of places in the VM system where we expected
333				 * m->dirty == VM_PAGE_BITS_ALL.  The file EOF
334				 * case is one of them.  If the page is still
335				 * partially dirty, make it fully dirty.
336				 */
337				vm_page_set_validclean(m, base, size);
338				if (m->dirty != 0)
339					m->dirty = VM_PAGE_BITS_ALL;
340			}
341		}
342		if (!hadvmlock)
343			mtx_unlock(&vm_mtx);
344	}
345	object->un_pager.vnp.vnp_size = nsize;
346	object->size = nobjsize;
347}
348
349/*
350 * calculate the linear (byte) disk address of specified virtual
351 * file address
352 */
353static vm_offset_t
354vnode_pager_addr(vp, address, run)
355	struct vnode *vp;
356	vm_ooffset_t address;
357	int *run;
358{
359	int rtaddress;
360	int bsize;
361	daddr_t block;
362	struct vnode *rtvp;
363	int err;
364	daddr_t vblock;
365	int voffset;
366
367	mtx_assert(&Giant, MA_OWNED);
368	if ((int) address < 0)
369		return -1;
370
371	if (vp->v_mount == NULL)
372		return -1;
373
374	bsize = vp->v_mount->mnt_stat.f_iosize;
375	vblock = address / bsize;
376	voffset = address % bsize;
377
378	err = VOP_BMAP(vp, vblock, &rtvp, &block, run, NULL);
379
380	if (err || (block == -1))
381		rtaddress = -1;
382	else {
383		rtaddress = block + voffset / DEV_BSIZE;
384		if( run) {
385			*run += 1;
386			*run *= bsize/PAGE_SIZE;
387			*run -= voffset/PAGE_SIZE;
388		}
389	}
390
391	return rtaddress;
392}
393
394/*
395 * interrupt routine for I/O completion
396 */
397static void
398vnode_pager_iodone(bp)
399	struct buf *bp;
400{
401	bp->b_flags |= B_DONE;
402	wakeup(bp);
403}
404
405/*
406 * small block file system vnode pager input
407 */
408static int
409vnode_pager_input_smlfs(object, m)
410	vm_object_t object;
411	vm_page_t m;
412{
413	int i;
414	int s;
415	struct vnode *dp, *vp;
416	struct buf *bp;
417	vm_offset_t kva;
418	int fileaddr;
419	vm_offset_t bsize;
420	int error = 0;
421
422	mtx_assert(&Giant, MA_OWNED);
423	vp = object->handle;
424	if (vp->v_mount == NULL)
425		return VM_PAGER_BAD;
426
427	bsize = vp->v_mount->mnt_stat.f_iosize;
428
429
430	VOP_BMAP(vp, 0, &dp, 0, NULL, NULL);
431
432	kva = vm_pager_map_page(m);
433
434	for (i = 0; i < PAGE_SIZE / bsize; i++) {
435
436		if (vm_page_bits(i * bsize, bsize) & m->valid)
437			continue;
438
439		fileaddr = vnode_pager_addr(vp,
440			IDX_TO_OFF(m->pindex) + i * bsize, (int *)0);
441		if (fileaddr != -1) {
442			bp = getpbuf(&vnode_pbuf_freecnt);
443
444			/* build a minimal buffer header */
445			bp->b_iocmd = BIO_READ;
446			bp->b_iodone = vnode_pager_iodone;
447			bp->b_rcred = bp->b_wcred = curproc->p_ucred;
448			if (bp->b_rcred != NOCRED)
449				crhold(bp->b_rcred);
450			if (bp->b_wcred != NOCRED)
451				crhold(bp->b_wcred);
452			bp->b_data = (caddr_t) kva + i * bsize;
453			bp->b_blkno = fileaddr;
454			pbgetvp(dp, bp);
455			bp->b_bcount = bsize;
456			bp->b_bufsize = bsize;
457			bp->b_runningbufspace = bp->b_bufsize;
458			runningbufspace += bp->b_runningbufspace;
459
460			/* do the input */
461			BUF_STRATEGY(bp);
462
463			/* we definitely need to be at splvm here */
464
465			s = splvm();
466			while ((bp->b_flags & B_DONE) == 0) {
467				tsleep(bp, PVM, "vnsrd", 0);
468			}
469			splx(s);
470			if ((bp->b_ioflags & BIO_ERROR) != 0)
471				error = EIO;
472
473			/*
474			 * free the buffer header back to the swap buffer pool
475			 */
476			relpbuf(bp, &vnode_pbuf_freecnt);
477			if (error)
478				break;
479
480			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
481		} else {
482			vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
483			bzero((caddr_t) kva + i * bsize, bsize);
484		}
485	}
486	vm_pager_unmap_page(kva);
487	pmap_clear_modify(m);
488	vm_page_flag_clear(m, PG_ZERO);
489	if (error) {
490		return VM_PAGER_ERROR;
491	}
492	return VM_PAGER_OK;
493
494}
495
496
497/*
498 * old style vnode pager output routine
499 */
500static int
501vnode_pager_input_old(object, m)
502	vm_object_t object;
503	vm_page_t m;
504{
505	struct uio auio;
506	struct iovec aiov;
507	int error;
508	int size;
509	vm_offset_t kva;
510
511	mtx_assert(&Giant, MA_OWNED);
512	error = 0;
513
514	/*
515	 * Return failure if beyond current EOF
516	 */
517	if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
518		return VM_PAGER_BAD;
519	} else {
520		size = PAGE_SIZE;
521		if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
522			size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
523
524		/*
525		 * Allocate a kernel virtual address and initialize so that
526		 * we can use VOP_READ/WRITE routines.
527		 */
528		kva = vm_pager_map_page(m);
529
530		aiov.iov_base = (caddr_t) kva;
531		aiov.iov_len = size;
532		auio.uio_iov = &aiov;
533		auio.uio_iovcnt = 1;
534		auio.uio_offset = IDX_TO_OFF(m->pindex);
535		auio.uio_segflg = UIO_SYSSPACE;
536		auio.uio_rw = UIO_READ;
537		auio.uio_resid = size;
538		auio.uio_procp = curproc;
539
540		error = VOP_READ(object->handle, &auio, 0, curproc->p_ucred);
541		if (!error) {
542			register int count = size - auio.uio_resid;
543
544			if (count == 0)
545				error = EINVAL;
546			else if (count != PAGE_SIZE)
547				bzero((caddr_t) kva + count, PAGE_SIZE - count);
548		}
549		vm_pager_unmap_page(kva);
550	}
551	pmap_clear_modify(m);
552	vm_page_undirty(m);
553	vm_page_flag_clear(m, PG_ZERO);
554	if (!error)
555		m->valid = VM_PAGE_BITS_ALL;
556	return error ? VM_PAGER_ERROR : VM_PAGER_OK;
557}
558
559/*
560 * generic vnode pager input routine
561 */
562
563/*
564 * Local media VFS's that do not implement their own VOP_GETPAGES
565 * should have their VOP_GETPAGES should call to
566 * vnode_pager_generic_getpages() to implement the previous behaviour.
567 *
568 * All other FS's should use the bypass to get to the local media
569 * backing vp's VOP_GETPAGES.
570 */
571static int
572vnode_pager_getpages(object, m, count, reqpage)
573	vm_object_t object;
574	vm_page_t *m;
575	int count;
576	int reqpage;
577{
578	int rtval;
579	struct vnode *vp;
580	int bytes = count * PAGE_SIZE;
581
582	mtx_assert(&Giant, MA_OWNED);
583	vp = object->handle;
584	rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
585	KASSERT(rtval != EOPNOTSUPP,
586	    ("vnode_pager: FS getpages not implemented\n"));
587	return rtval;
588}
589
590
591/*
592 * This is now called from local media FS's to operate against their
593 * own vnodes if they fail to implement VOP_GETPAGES.
594 */
595int
596vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
597	struct vnode *vp;
598	vm_page_t *m;
599	int bytecount;
600	int reqpage;
601{
602	vm_object_t object;
603	vm_offset_t kva;
604	off_t foff, tfoff, nextoff;
605	int i, size, bsize, first, firstaddr;
606	struct vnode *dp;
607	int runpg;
608	int runend;
609	struct buf *bp;
610	int s;
611	int count;
612	int error = 0;
613
614	mtx_assert(&Giant, MA_OWNED);
615	object = vp->v_object;
616	count = bytecount / PAGE_SIZE;
617
618	if (vp->v_mount == NULL)
619		return VM_PAGER_BAD;
620
621	bsize = vp->v_mount->mnt_stat.f_iosize;
622
623	/* get the UNDERLYING device for the file with VOP_BMAP() */
624
625	/*
626	 * originally, we did not check for an error return value -- assuming
627	 * an fs always has a bmap entry point -- that assumption is wrong!!!
628	 */
629	foff = IDX_TO_OFF(m[reqpage]->pindex);
630
631	/*
632	 * if we can't bmap, use old VOP code
633	 */
634	if (VOP_BMAP(vp, 0, &dp, 0, NULL, NULL)) {
635		for (i = 0; i < count; i++) {
636			if (i != reqpage) {
637				vm_page_free(m[i]);
638			}
639		}
640		cnt.v_vnodein++;
641		cnt.v_vnodepgsin++;
642		return vnode_pager_input_old(object, m[reqpage]);
643
644		/*
645		 * if the blocksize is smaller than a page size, then use
646		 * special small filesystem code.  NFS sometimes has a small
647		 * blocksize, but it can handle large reads itself.
648		 */
649	} else if ((PAGE_SIZE / bsize) > 1 &&
650	    (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
651		for (i = 0; i < count; i++) {
652			if (i != reqpage) {
653				vm_page_free(m[i]);
654			}
655		}
656		cnt.v_vnodein++;
657		cnt.v_vnodepgsin++;
658		return vnode_pager_input_smlfs(object, m[reqpage]);
659	}
660
661	/*
662	 * If we have a completely valid page available to us, we can
663	 * clean up and return.  Otherwise we have to re-read the
664	 * media.
665	 */
666
667	if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
668		for (i = 0; i < count; i++) {
669			if (i != reqpage)
670				vm_page_free(m[i]);
671		}
672		return VM_PAGER_OK;
673	}
674	m[reqpage]->valid = 0;
675
676	/*
677	 * here on direct device I/O
678	 */
679
680	firstaddr = -1;
681	/*
682	 * calculate the run that includes the required page
683	 */
684	for(first = 0, i = 0; i < count; i = runend) {
685		firstaddr = vnode_pager_addr(vp,
686			IDX_TO_OFF(m[i]->pindex), &runpg);
687		if (firstaddr == -1) {
688			if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
689				/* XXX no %qd in kernel. */
690				panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx",
691			   	 firstaddr, (u_long)(foff >> 32),
692			   	 (u_long)(u_int32_t)foff,
693				 (u_long)(u_int32_t)
694				 (object->un_pager.vnp.vnp_size >> 32),
695				 (u_long)(u_int32_t)
696				 object->un_pager.vnp.vnp_size);
697			}
698			vm_page_free(m[i]);
699			runend = i + 1;
700			first = runend;
701			continue;
702		}
703		runend = i + runpg;
704		if (runend <= reqpage) {
705			int j;
706			for (j = i; j < runend; j++) {
707				vm_page_free(m[j]);
708			}
709		} else {
710			if (runpg < (count - first)) {
711				for (i = first + runpg; i < count; i++)
712					vm_page_free(m[i]);
713				count = first + runpg;
714			}
715			break;
716		}
717		first = runend;
718	}
719
720	/*
721	 * the first and last page have been calculated now, move input pages
722	 * to be zero based...
723	 */
724	if (first != 0) {
725		for (i = first; i < count; i++) {
726			m[i - first] = m[i];
727		}
728		count -= first;
729		reqpage -= first;
730	}
731
732	/*
733	 * calculate the file virtual address for the transfer
734	 */
735	foff = IDX_TO_OFF(m[0]->pindex);
736
737	/*
738	 * calculate the size of the transfer
739	 */
740	size = count * PAGE_SIZE;
741	if ((foff + size) > object->un_pager.vnp.vnp_size)
742		size = object->un_pager.vnp.vnp_size - foff;
743
744	/*
745	 * round up physical size for real devices.
746	 */
747	if (dp->v_type == VBLK || dp->v_type == VCHR) {
748		int secmask = dp->v_rdev->si_bsize_phys - 1;
749		KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
750		size = (size + secmask) & ~secmask;
751	}
752
753	bp = getpbuf(&vnode_pbuf_freecnt);
754	kva = (vm_offset_t) bp->b_data;
755
756	/*
757	 * and map the pages to be read into the kva
758	 */
759	pmap_qenter(kva, m, count);
760
761	/* build a minimal buffer header */
762	bp->b_iocmd = BIO_READ;
763	bp->b_iodone = vnode_pager_iodone;
764	/* B_PHYS is not set, but it is nice to fill this in */
765	bp->b_rcred = bp->b_wcred = curproc->p_ucred;
766	if (bp->b_rcred != NOCRED)
767		crhold(bp->b_rcred);
768	if (bp->b_wcred != NOCRED)
769		crhold(bp->b_wcred);
770	bp->b_blkno = firstaddr;
771	pbgetvp(dp, bp);
772	bp->b_bcount = size;
773	bp->b_bufsize = size;
774	bp->b_runningbufspace = bp->b_bufsize;
775	runningbufspace += bp->b_runningbufspace;
776
777	cnt.v_vnodein++;
778	cnt.v_vnodepgsin += count;
779
780	/* do the input */
781	BUF_STRATEGY(bp);
782
783	s = splvm();
784	/* we definitely need to be at splvm here */
785
786	while ((bp->b_flags & B_DONE) == 0) {
787		tsleep(bp, PVM, "vnread", 0);
788	}
789	splx(s);
790	if ((bp->b_ioflags & BIO_ERROR) != 0)
791		error = EIO;
792
793	if (!error) {
794		if (size != count * PAGE_SIZE)
795			bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
796	}
797	pmap_qremove(kva, count);
798
799	/*
800	 * free the buffer header back to the swap buffer pool
801	 */
802	relpbuf(bp, &vnode_pbuf_freecnt);
803
804	for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
805		vm_page_t mt;
806
807		nextoff = tfoff + PAGE_SIZE;
808		mt = m[i];
809
810		if (nextoff <= object->un_pager.vnp.vnp_size) {
811			/*
812			 * Read filled up entire page.
813			 */
814			mt->valid = VM_PAGE_BITS_ALL;
815			vm_page_undirty(mt);	/* should be an assert? XXX */
816			pmap_clear_modify(mt);
817		} else {
818			/*
819			 * Read did not fill up entire page.  Since this
820			 * is getpages, the page may be mapped, so we have
821			 * to zero the invalid portions of the page even
822			 * though we aren't setting them valid.
823			 *
824			 * Currently we do not set the entire page valid,
825			 * we just try to clear the piece that we couldn't
826			 * read.
827			 */
828			vm_page_set_validclean(mt, 0,
829			    object->un_pager.vnp.vnp_size - tfoff);
830			/* handled by vm_fault now */
831			/* vm_page_zero_invalid(mt, FALSE); */
832		}
833
834		vm_page_flag_clear(mt, PG_ZERO);
835		if (i != reqpage) {
836
837			/*
838			 * whether or not to leave the page activated is up in
839			 * the air, but we should put the page on a page queue
840			 * somewhere. (it already is in the object). Result:
841			 * It appears that empirical results show that
842			 * deactivating pages is best.
843			 */
844
845			/*
846			 * just in case someone was asking for this page we
847			 * now tell them that it is ok to use
848			 */
849			if (!error) {
850				if (mt->flags & PG_WANTED)
851					vm_page_activate(mt);
852				else
853					vm_page_deactivate(mt);
854				vm_page_wakeup(mt);
855			} else {
856				vm_page_free(mt);
857			}
858		}
859	}
860	if (error) {
861		printf("vnode_pager_getpages: I/O read error\n");
862	}
863	return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
864}
865
866/*
867 * EOPNOTSUPP is no longer legal.  For local media VFS's that do not
868 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
869 * vnode_pager_generic_putpages() to implement the previous behaviour.
870 *
871 * All other FS's should use the bypass to get to the local media
872 * backing vp's VOP_PUTPAGES.
873 */
874static void
875vnode_pager_putpages(object, m, count, sync, rtvals)
876	vm_object_t object;
877	vm_page_t *m;
878	int count;
879	boolean_t sync;
880	int *rtvals;
881{
882	int rtval;
883	struct vnode *vp;
884	struct mount *mp;
885	int bytes = count * PAGE_SIZE;
886
887	mtx_assert(&Giant, MA_OWNED);
888	/*
889	 * Force synchronous operation if we are extremely low on memory
890	 * to prevent a low-memory deadlock.  VOP operations often need to
891	 * allocate more memory to initiate the I/O ( i.e. do a BMAP
892	 * operation ).  The swapper handles the case by limiting the amount
893	 * of asynchronous I/O, but that sort of solution doesn't scale well
894	 * for the vnode pager without a lot of work.
895	 *
896	 * Also, the backing vnode's iodone routine may not wake the pageout
897	 * daemon up.  This should be probably be addressed XXX.
898	 */
899
900	if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
901		sync |= OBJPC_SYNC;
902
903	/*
904	 * Call device-specific putpages function
905	 */
906
907	vp = object->handle;
908	mtx_unlock(&vm_mtx);
909	if (vp->v_type != VREG)
910		mp = NULL;
911	(void)vn_start_write(vp, &mp, V_WAIT);
912	mtx_lock(&vm_mtx);
913	rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
914	KASSERT(rtval != EOPNOTSUPP,
915	    ("vnode_pager: stale FS putpages\n"));
916	mtx_unlock(&vm_mtx);
917	vn_finished_write(mp);
918	mtx_lock(&vm_mtx);
919}
920
921
922/*
923 * This is now called from local media FS's to operate against their
924 * own vnodes if they fail to implement VOP_PUTPAGES.
925 *
926 * This is typically called indirectly via the pageout daemon and
927 * clustering has already typically occured, so in general we ask the
928 * underlying filesystem to write the data out asynchronously rather
929 * then delayed.
930 */
931int
932vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
933	struct vnode *vp;
934	vm_page_t *m;
935	int bytecount;
936	int flags;
937	int *rtvals;
938{
939	int i;
940	vm_object_t object;
941	int count;
942
943	int maxsize, ncount;
944	vm_ooffset_t poffset;
945	struct uio auio;
946	struct iovec aiov;
947	int error;
948	int ioflags;
949
950	mtx_assert(&Giant, MA_OWNED);
951	object = vp->v_object;
952	count = bytecount / PAGE_SIZE;
953
954	for (i = 0; i < count; i++)
955		rtvals[i] = VM_PAGER_AGAIN;
956
957	if ((int) m[0]->pindex < 0) {
958		printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n",
959			(long)m[0]->pindex, m[0]->dirty);
960		rtvals[0] = VM_PAGER_BAD;
961		return VM_PAGER_BAD;
962	}
963
964	maxsize = count * PAGE_SIZE;
965	ncount = count;
966
967	poffset = IDX_TO_OFF(m[0]->pindex);
968	if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
969		if (object->un_pager.vnp.vnp_size > poffset)
970			maxsize = object->un_pager.vnp.vnp_size - poffset;
971		else
972			maxsize = 0;
973		ncount = btoc(maxsize);
974		if (ncount < count) {
975			for (i = ncount; i < count; i++) {
976				rtvals[i] = VM_PAGER_BAD;
977			}
978		}
979	}
980
981	/*
982	 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
983	 * rather then a bdwrite() to prevent paging I/O from saturating
984	 * the buffer cache.
985	 */
986	ioflags = IO_VMIO;
987	ioflags |= (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) ? IO_SYNC: IO_ASYNC;
988	ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
989
990	aiov.iov_base = (caddr_t) 0;
991	aiov.iov_len = maxsize;
992	auio.uio_iov = &aiov;
993	auio.uio_iovcnt = 1;
994	auio.uio_offset = poffset;
995	auio.uio_segflg = UIO_NOCOPY;
996	auio.uio_rw = UIO_WRITE;
997	auio.uio_resid = maxsize;
998	auio.uio_procp = (struct proc *) 0;
999	error = VOP_WRITE(vp, &auio, ioflags, curproc->p_ucred);
1000	cnt.v_vnodeout++;
1001	cnt.v_vnodepgsout += ncount;
1002
1003	if (error) {
1004		printf("vnode_pager_putpages: I/O error %d\n", error);
1005	}
1006	if (auio.uio_resid) {
1007		printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1008		    auio.uio_resid, (u_long)m[0]->pindex);
1009	}
1010	for (i = 0; i < ncount; i++) {
1011		rtvals[i] = VM_PAGER_OK;
1012	}
1013	return rtvals[0];
1014}
1015
1016struct vnode *
1017vnode_pager_lock(object)
1018	vm_object_t object;
1019{
1020	struct proc *p = curproc;	/* XXX */
1021
1022	mtx_assert(&vm_mtx, MA_NOTOWNED);
1023	mtx_assert(&Giant, MA_OWNED);
1024	mtx_lock(&vm_mtx);
1025	for (; object != NULL; object = object->backing_object) {
1026		if (object->type != OBJT_VNODE)
1027			continue;
1028		if (object->flags & OBJ_DEAD) {
1029			mtx_unlock(&vm_mtx);
1030			return NULL;
1031		}
1032
1033		mtx_unlock(&vm_mtx);
1034		/* XXX; If object->handle can change, we need to cache it. */
1035		while (vget(object->handle,
1036			LK_NOPAUSE | LK_SHARED | LK_RETRY | LK_CANRECURSE, p)) {
1037			if ((object->flags & OBJ_DEAD) || (object->type != OBJT_VNODE))
1038				return NULL;
1039			printf("vnode_pager_lock: retrying\n");
1040		}
1041		return object->handle;
1042	}
1043	mtx_unlock(&vm_mtx);
1044	return NULL;
1045}
1046