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