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