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