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