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