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