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