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