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