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