2 * Copyright (c) 1990 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
5 * Copyright (c) 1993, 1994 John S. Dyson
6 * Copyright (c) 1995, David Greenman
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
40 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91
44 * Page to/from files (vnodes).
49 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
50 * greatly re-simplify the vnode_pager.
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
56 #include <sys/param.h>
57 #include <sys/systm.h>
59 #include <sys/vnode.h>
60 #include <sys/mount.h>
63 #include <sys/vmmeter.h>
64 #include <sys/limits.h>
66 #include <sys/sf_buf.h>
68 #include <machine/atomic.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vm_map.h>
75 #include <vm/vnode_pager.h>
76 #include <vm/vm_extern.h>
78 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
79 daddr_t *rtaddress, int *run);
80 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
81 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
82 static void vnode_pager_dealloc(vm_object_t);
83 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
84 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
85 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
86 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t);
88 struct pagerops vnodepagerops = {
89 .pgo_alloc = vnode_pager_alloc,
90 .pgo_dealloc = vnode_pager_dealloc,
91 .pgo_getpages = vnode_pager_getpages,
92 .pgo_putpages = vnode_pager_putpages,
93 .pgo_haspage = vnode_pager_haspage,
96 int vnode_pbuf_freecnt;
98 /* Create the VM system backing object for this vnode */
100 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
103 vm_ooffset_t size = isize;
106 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
109 while ((object = vp->v_object) != NULL) {
110 VM_OBJECT_LOCK(object);
111 if (!(object->flags & OBJ_DEAD)) {
112 VM_OBJECT_UNLOCK(object);
116 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
117 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0);
118 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
122 if (vn_isdisk(vp, NULL)) {
123 size = IDX_TO_OFF(INT_MAX);
125 if (VOP_GETATTR(vp, &va, td->td_ucred, td) != 0)
131 object = vnode_pager_alloc(vp, size, 0, 0);
133 * Dereference the reference we just created. This assumes
134 * that the object is associated with the vp.
136 VM_OBJECT_LOCK(object);
138 VM_OBJECT_UNLOCK(object);
141 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
147 vnode_destroy_vobject(struct vnode *vp)
149 struct vm_object *obj;
154 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
156 if (obj->ref_count == 0) {
158 * vclean() may be called twice. The first time
159 * removes the primary reference to the object,
160 * the second time goes one further and is a
161 * special-case to terminate the object.
163 * don't double-terminate the object
165 if ((obj->flags & OBJ_DEAD) == 0)
166 vm_object_terminate(obj);
168 VM_OBJECT_UNLOCK(obj);
171 * Woe to the process that tries to page now :-).
173 vm_pager_deallocate(obj);
174 VM_OBJECT_UNLOCK(obj);
181 * Allocate (or lookup) pager for a vnode.
182 * Handle is a vnode pointer.
187 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
194 * Pageout to vnode, no can do yet.
199 vp = (struct vnode *) handle;
201 ASSERT_VOP_ELOCKED(vp, "vnode_pager_alloc");
204 * If the object is being terminated, wait for it to
207 while ((object = vp->v_object) != NULL) {
208 VM_OBJECT_LOCK(object);
209 if ((object->flags & OBJ_DEAD) == 0)
211 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
212 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
215 if (vp->v_usecount == 0)
216 panic("vnode_pager_alloc: no vnode reference");
218 if (object == NULL) {
220 * And an object of the appropriate size
222 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
224 object->un_pager.vnp.vnp_size = size;
226 object->handle = handle;
227 if (VFS_NEEDSGIANT(vp->v_mount))
228 vm_object_set_flag(object, OBJ_NEEDGIANT);
229 vp->v_object = object;
232 VM_OBJECT_UNLOCK(object);
239 * The object must be locked.
242 vnode_pager_dealloc(object)
245 struct vnode *vp = object->handle;
248 panic("vnode_pager_dealloc: pager already dealloced");
250 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
251 vm_object_pip_wait(object, "vnpdea");
253 object->handle = NULL;
254 object->type = OBJT_DEAD;
255 if (object->flags & OBJ_DISCONNECTWNT) {
256 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
259 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
261 vp->v_vflag &= ~VV_TEXT;
265 vnode_pager_haspage(object, pindex, before, after)
271 struct vnode *vp = object->handle;
277 int pagesperblock, blocksperpage;
280 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
282 * If no vp or vp is doomed or marked transparent to VM, we do not
285 if (vp == NULL || vp->v_iflag & VI_DOOMED)
288 * If the offset is beyond end of file we do
291 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
294 bsize = vp->v_mount->mnt_stat.f_iosize;
295 pagesperblock = bsize / PAGE_SIZE;
297 if (pagesperblock > 0) {
298 reqblock = pindex / pagesperblock;
300 blocksperpage = (PAGE_SIZE / bsize);
301 reqblock = pindex * blocksperpage;
303 VM_OBJECT_UNLOCK(object);
304 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
305 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
306 VFS_UNLOCK_GIANT(vfslocked);
307 VM_OBJECT_LOCK(object);
312 if (pagesperblock > 0) {
313 poff = pindex - (reqblock * pagesperblock);
315 *before *= pagesperblock;
320 *after *= pagesperblock;
321 numafter = pagesperblock - (poff + 1);
322 if (IDX_TO_OFF(pindex + numafter) >
323 object->un_pager.vnp.vnp_size) {
325 OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
332 *before /= blocksperpage;
336 *after /= blocksperpage;
343 * Lets the VM system know about a change in size for a file.
344 * We adjust our own internal size and flush any cached pages in
345 * the associated object that are affected by the size change.
347 * Note: this routine may be invoked as a result of a pager put
348 * operation (possibly at object termination time), so we must be careful.
351 vnode_pager_setsize(vp, nsize)
357 vm_pindex_t nobjsize;
359 if ((object = vp->v_object) == NULL)
361 VM_OBJECT_LOCK(object);
362 if (nsize == object->un_pager.vnp.vnp_size) {
364 * Hasn't changed size
366 VM_OBJECT_UNLOCK(object);
369 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
370 if (nsize < object->un_pager.vnp.vnp_size) {
372 * File has shrunk. Toss any cached pages beyond the new EOF.
374 if (nobjsize < object->size)
375 vm_object_page_remove(object, nobjsize, object->size,
378 * this gets rid of garbage at the end of a page that is now
379 * only partially backed by the vnode.
381 * XXX for some reason (I don't know yet), if we take a
382 * completely invalid page and mark it partially valid
383 * it can screw up NFS reads, so we don't allow the case.
385 if ((nsize & PAGE_MASK) &&
386 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
388 int base = (int)nsize & PAGE_MASK;
389 int size = PAGE_SIZE - base;
392 * Clear out partial-page garbage in case
393 * the page has been mapped.
395 pmap_zero_page_area(m, base, size);
398 * Clear out partial-page dirty bits. This
399 * has the side effect of setting the valid
400 * bits, but that is ok. There are a bunch
401 * of places in the VM system where we expected
402 * m->dirty == VM_PAGE_BITS_ALL. The file EOF
403 * case is one of them. If the page is still
404 * partially dirty, make it fully dirty.
406 * note that we do not clear out the valid
407 * bits. This would prevent bogus_page
408 * replacement from working properly.
410 vm_page_lock_queues();
411 vm_page_set_validclean(m, base, size);
413 m->dirty = VM_PAGE_BITS_ALL;
414 vm_page_unlock_queues();
415 } else if ((nsize & PAGE_MASK) &&
416 __predict_false(object->cache != NULL)) {
417 vm_page_cache_free(object, OFF_TO_IDX(nsize),
421 object->un_pager.vnp.vnp_size = nsize;
422 object->size = nobjsize;
423 VM_OBJECT_UNLOCK(object);
427 * calculate the linear (byte) disk address of specified virtual
431 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
442 if (vp->v_iflag & VI_DOOMED)
445 bsize = vp->v_mount->mnt_stat.f_iosize;
446 vblock = address / bsize;
447 voffset = address % bsize;
449 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
451 if (*rtaddress != -1)
452 *rtaddress += voffset / DEV_BSIZE;
455 *run *= bsize/PAGE_SIZE;
456 *run -= voffset/PAGE_SIZE;
464 * small block filesystem vnode pager input
467 vnode_pager_input_smlfs(object, m)
481 if (vp->v_iflag & VI_DOOMED)
484 bsize = vp->v_mount->mnt_stat.f_iosize;
486 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
488 sf = sf_buf_alloc(m, 0);
490 for (i = 0; i < PAGE_SIZE / bsize; i++) {
491 vm_ooffset_t address;
493 if (vm_page_bits(i * bsize, bsize) & m->valid)
496 address = IDX_TO_OFF(m->pindex) + i * bsize;
497 if (address >= object->un_pager.vnp.vnp_size) {
500 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
504 if (fileaddr != -1) {
505 bp = getpbuf(&vnode_pbuf_freecnt);
507 /* build a minimal buffer header */
508 bp->b_iocmd = BIO_READ;
509 bp->b_iodone = bdone;
510 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
511 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
512 bp->b_rcred = crhold(curthread->td_ucred);
513 bp->b_wcred = crhold(curthread->td_ucred);
514 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
515 bp->b_blkno = fileaddr;
517 bp->b_bcount = bsize;
518 bp->b_bufsize = bsize;
519 bp->b_runningbufspace = bp->b_bufsize;
520 atomic_add_int(&runningbufspace, bp->b_runningbufspace);
523 bp->b_iooffset = dbtob(bp->b_blkno);
526 bwait(bp, PVM, "vnsrd");
528 if ((bp->b_ioflags & BIO_ERROR) != 0)
532 * free the buffer header back to the swap buffer pool
535 relpbuf(bp, &vnode_pbuf_freecnt);
539 VM_OBJECT_LOCK(object);
540 vm_page_lock_queues();
541 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
542 vm_page_unlock_queues();
543 VM_OBJECT_UNLOCK(object);
545 VM_OBJECT_LOCK(object);
546 vm_page_lock_queues();
547 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
548 vm_page_unlock_queues();
549 VM_OBJECT_UNLOCK(object);
550 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
554 vm_page_lock_queues();
555 pmap_clear_modify(m);
556 vm_page_unlock_queues();
558 return VM_PAGER_ERROR;
566 * old style vnode pager input routine
569 vnode_pager_input_old(object, m)
580 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
584 * Return failure if beyond current EOF
586 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
590 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
591 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
593 VM_OBJECT_UNLOCK(object);
596 * Allocate a kernel virtual address and initialize so that
597 * we can use VOP_READ/WRITE routines.
599 sf = sf_buf_alloc(m, 0);
601 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
603 auio.uio_iov = &aiov;
605 auio.uio_offset = IDX_TO_OFF(m->pindex);
606 auio.uio_segflg = UIO_SYSSPACE;
607 auio.uio_rw = UIO_READ;
608 auio.uio_resid = size;
609 auio.uio_td = curthread;
611 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
613 int count = size - auio.uio_resid;
617 else if (count != PAGE_SIZE)
618 bzero((caddr_t)sf_buf_kva(sf) + count,
623 VM_OBJECT_LOCK(object);
625 vm_page_lock_queues();
626 pmap_clear_modify(m);
628 vm_page_unlock_queues();
630 m->valid = VM_PAGE_BITS_ALL;
631 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
635 * generic vnode pager input routine
639 * Local media VFS's that do not implement their own VOP_GETPAGES
640 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
641 * to implement the previous behaviour.
643 * All other FS's should use the bypass to get to the local media
644 * backing vp's VOP_GETPAGES.
647 vnode_pager_getpages(object, m, count, reqpage)
655 int bytes = count * PAGE_SIZE;
659 VM_OBJECT_UNLOCK(object);
660 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
661 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
662 KASSERT(rtval != EOPNOTSUPP,
663 ("vnode_pager: FS getpages not implemented\n"));
664 VFS_UNLOCK_GIANT(vfslocked);
665 VM_OBJECT_LOCK(object);
670 * This is now called from local media FS's to operate against their
671 * own vnodes if they fail to implement VOP_GETPAGES.
674 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
682 off_t foff, tfoff, nextoff;
683 int i, j, size, bsize, first;
684 daddr_t firstaddr, reqblock;
692 object = vp->v_object;
693 count = bytecount / PAGE_SIZE;
695 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
696 ("vnode_pager_generic_getpages does not support devices"));
697 if (vp->v_iflag & VI_DOOMED)
700 bsize = vp->v_mount->mnt_stat.f_iosize;
702 /* get the UNDERLYING device for the file with VOP_BMAP() */
705 * originally, we did not check for an error return value -- assuming
706 * an fs always has a bmap entry point -- that assumption is wrong!!!
708 foff = IDX_TO_OFF(m[reqpage]->pindex);
711 * if we can't bmap, use old VOP code
713 error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
714 if (error == EOPNOTSUPP) {
715 VM_OBJECT_LOCK(object);
716 vm_page_lock_queues();
717 for (i = 0; i < count; i++)
720 vm_page_unlock_queues();
721 PCPU_INC(cnt.v_vnodein);
722 PCPU_INC(cnt.v_vnodepgsin);
723 error = vnode_pager_input_old(object, m[reqpage]);
724 VM_OBJECT_UNLOCK(object);
726 } else if (error != 0) {
727 VM_OBJECT_LOCK(object);
728 vm_page_lock_queues();
729 for (i = 0; i < count; i++)
732 vm_page_unlock_queues();
733 VM_OBJECT_UNLOCK(object);
734 return (VM_PAGER_ERROR);
737 * if the blocksize is smaller than a page size, then use
738 * special small filesystem code. NFS sometimes has a small
739 * blocksize, but it can handle large reads itself.
741 } else if ((PAGE_SIZE / bsize) > 1 &&
742 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
743 VM_OBJECT_LOCK(object);
744 vm_page_lock_queues();
745 for (i = 0; i < count; i++)
748 vm_page_unlock_queues();
749 VM_OBJECT_UNLOCK(object);
750 PCPU_INC(cnt.v_vnodein);
751 PCPU_INC(cnt.v_vnodepgsin);
752 return vnode_pager_input_smlfs(object, m[reqpage]);
756 * If we have a completely valid page available to us, we can
757 * clean up and return. Otherwise we have to re-read the
760 VM_OBJECT_LOCK(object);
761 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
762 vm_page_lock_queues();
763 for (i = 0; i < count; i++)
766 vm_page_unlock_queues();
767 VM_OBJECT_UNLOCK(object);
769 } else if (reqblock == -1) {
770 pmap_zero_page(m[reqpage]);
771 vm_page_undirty(m[reqpage]);
772 m[reqpage]->valid = VM_PAGE_BITS_ALL;
773 vm_page_lock_queues();
774 for (i = 0; i < count; i++)
777 vm_page_unlock_queues();
778 VM_OBJECT_UNLOCK(object);
779 return (VM_PAGER_OK);
781 m[reqpage]->valid = 0;
782 VM_OBJECT_UNLOCK(object);
785 * here on direct device I/O
790 * calculate the run that includes the required page
792 for (first = 0, i = 0; i < count; i = runend) {
793 if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
795 VM_OBJECT_LOCK(object);
796 vm_page_lock_queues();
797 for (; i < count; i++)
800 vm_page_unlock_queues();
801 VM_OBJECT_UNLOCK(object);
802 return (VM_PAGER_ERROR);
804 if (firstaddr == -1) {
805 VM_OBJECT_LOCK(object);
806 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
807 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
808 (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
811 (object->un_pager.vnp.vnp_size >> 32),
812 (uintmax_t)object->un_pager.vnp.vnp_size);
814 vm_page_lock_queues();
816 vm_page_unlock_queues();
817 VM_OBJECT_UNLOCK(object);
823 if (runend <= reqpage) {
824 VM_OBJECT_LOCK(object);
825 vm_page_lock_queues();
826 for (j = i; j < runend; j++)
828 vm_page_unlock_queues();
829 VM_OBJECT_UNLOCK(object);
831 if (runpg < (count - first)) {
832 VM_OBJECT_LOCK(object);
833 vm_page_lock_queues();
834 for (i = first + runpg; i < count; i++)
836 vm_page_unlock_queues();
837 VM_OBJECT_UNLOCK(object);
838 count = first + runpg;
846 * the first and last page have been calculated now, move input pages
847 * to be zero based...
856 * calculate the file virtual address for the transfer
858 foff = IDX_TO_OFF(m[0]->pindex);
861 * calculate the size of the transfer
863 size = count * PAGE_SIZE;
864 KASSERT(count > 0, ("zero count"));
865 if ((foff + size) > object->un_pager.vnp.vnp_size)
866 size = object->un_pager.vnp.vnp_size - foff;
867 KASSERT(size > 0, ("zero size"));
870 * round up physical size for real devices.
873 int secmask = bo->bo_bsize - 1;
874 KASSERT(secmask < PAGE_SIZE && secmask > 0,
875 ("vnode_pager_generic_getpages: sector size %d too large",
877 size = (size + secmask) & ~secmask;
880 bp = getpbuf(&vnode_pbuf_freecnt);
881 kva = (vm_offset_t) bp->b_data;
884 * and map the pages to be read into the kva
886 pmap_qenter(kva, m, count);
888 /* build a minimal buffer header */
889 bp->b_iocmd = BIO_READ;
890 bp->b_iodone = bdone;
891 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
892 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
893 bp->b_rcred = crhold(curthread->td_ucred);
894 bp->b_wcred = crhold(curthread->td_ucred);
895 bp->b_blkno = firstaddr;
898 bp->b_bufsize = size;
899 bp->b_runningbufspace = bp->b_bufsize;
900 atomic_add_int(&runningbufspace, bp->b_runningbufspace);
902 PCPU_INC(cnt.v_vnodein);
903 PCPU_ADD(cnt.v_vnodepgsin, count);
906 bp->b_iooffset = dbtob(bp->b_blkno);
909 bwait(bp, PVM, "vnread");
911 if ((bp->b_ioflags & BIO_ERROR) != 0)
915 if (size != count * PAGE_SIZE)
916 bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
918 pmap_qremove(kva, count);
921 * free the buffer header back to the swap buffer pool
924 relpbuf(bp, &vnode_pbuf_freecnt);
926 VM_OBJECT_LOCK(object);
927 vm_page_lock_queues();
928 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
931 nextoff = tfoff + PAGE_SIZE;
934 if (nextoff <= object->un_pager.vnp.vnp_size) {
936 * Read filled up entire page.
938 mt->valid = VM_PAGE_BITS_ALL;
939 vm_page_undirty(mt); /* should be an assert? XXX */
940 pmap_clear_modify(mt);
943 * Read did not fill up entire page. Since this
944 * is getpages, the page may be mapped, so we have
945 * to zero the invalid portions of the page even
946 * though we aren't setting them valid.
948 * Currently we do not set the entire page valid,
949 * we just try to clear the piece that we couldn't
952 vm_page_set_validclean(mt, 0,
953 object->un_pager.vnp.vnp_size - tfoff);
954 /* handled by vm_fault now */
955 /* vm_page_zero_invalid(mt, FALSE); */
961 * whether or not to leave the page activated is up in
962 * the air, but we should put the page on a page queue
963 * somewhere. (it already is in the object). Result:
964 * It appears that empirical results show that
965 * deactivating pages is best.
969 * just in case someone was asking for this page we
970 * now tell them that it is ok to use
973 if (mt->oflags & VPO_WANTED)
974 vm_page_activate(mt);
976 vm_page_deactivate(mt);
983 vm_page_unlock_queues();
984 VM_OBJECT_UNLOCK(object);
986 printf("vnode_pager_getpages: I/O read error\n");
988 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
992 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
993 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
994 * vnode_pager_generic_putpages() to implement the previous behaviour.
996 * All other FS's should use the bypass to get to the local media
997 * backing vp's VOP_PUTPAGES.
1000 vnode_pager_putpages(object, m, count, sync, rtvals)
1010 int bytes = count * PAGE_SIZE;
1013 * Force synchronous operation if we are extremely low on memory
1014 * to prevent a low-memory deadlock. VOP operations often need to
1015 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1016 * operation ). The swapper handles the case by limiting the amount
1017 * of asynchronous I/O, but that sort of solution doesn't scale well
1018 * for the vnode pager without a lot of work.
1020 * Also, the backing vnode's iodone routine may not wake the pageout
1021 * daemon up. This should be probably be addressed XXX.
1024 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
1028 * Call device-specific putpages function
1030 vp = object->handle;
1031 VM_OBJECT_UNLOCK(object);
1032 if (vp->v_type != VREG)
1034 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
1035 KASSERT(rtval != EOPNOTSUPP,
1036 ("vnode_pager: stale FS putpages\n"));
1037 VM_OBJECT_LOCK(object);
1042 * This is now called from local media FS's to operate against their
1043 * own vnodes if they fail to implement VOP_PUTPAGES.
1045 * This is typically called indirectly via the pageout daemon and
1046 * clustering has already typically occured, so in general we ask the
1047 * underlying filesystem to write the data out asynchronously rather
1051 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
1062 int maxsize, ncount;
1063 vm_ooffset_t poffset;
1069 static struct timeval lastfail;
1072 object = vp->v_object;
1073 count = bytecount / PAGE_SIZE;
1075 for (i = 0; i < count; i++)
1076 rtvals[i] = VM_PAGER_AGAIN;
1078 if ((int64_t)m[0]->pindex < 0) {
1079 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1080 (long)m[0]->pindex, (u_long)m[0]->dirty);
1081 rtvals[0] = VM_PAGER_BAD;
1082 return VM_PAGER_BAD;
1085 maxsize = count * PAGE_SIZE;
1088 poffset = IDX_TO_OFF(m[0]->pindex);
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.
1099 * We do not under any circumstances truncate the valid bits, as
1100 * this will screw up bogus page replacement.
1102 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1103 if (object->un_pager.vnp.vnp_size > poffset) {
1106 maxsize = object->un_pager.vnp.vnp_size - poffset;
1107 ncount = btoc(maxsize);
1108 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1109 vm_page_lock_queues();
1110 vm_page_clear_dirty(m[ncount - 1], pgoff,
1112 vm_page_unlock_queues();
1118 if (ncount < count) {
1119 for (i = ncount; i < count; i++) {
1120 rtvals[i] = VM_PAGER_BAD;
1126 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
1127 * rather then a bdwrite() to prevent paging I/O from saturating
1128 * the buffer cache. Dummy-up the sequential heuristic to cause
1129 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
1130 * the system decides how to cluster.
1133 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1135 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1136 ioflags |= IO_ASYNC;
1137 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1138 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1140 aiov.iov_base = (caddr_t) 0;
1141 aiov.iov_len = maxsize;
1142 auio.uio_iov = &aiov;
1143 auio.uio_iovcnt = 1;
1144 auio.uio_offset = poffset;
1145 auio.uio_segflg = UIO_NOCOPY;
1146 auio.uio_rw = UIO_WRITE;
1147 auio.uio_resid = maxsize;
1148 auio.uio_td = (struct thread *) 0;
1149 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1150 PCPU_INC(cnt.v_vnodeout);
1151 PCPU_ADD(cnt.v_vnodepgsout, ncount);
1154 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1155 printf("vnode_pager_putpages: I/O error %d\n", error);
1157 if (auio.uio_resid) {
1158 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1159 printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1160 auio.uio_resid, (u_long)m[0]->pindex);
1162 for (i = 0; i < ncount; i++) {
1163 rtvals[i] = VM_PAGER_OK;
1169 vnode_pager_lock(vm_object_t first_object)
1172 vm_object_t backing_object, object;
1174 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
1175 for (object = first_object; object != NULL; object = backing_object) {
1176 if (object->type != OBJT_VNODE) {
1177 if ((backing_object = object->backing_object) != NULL)
1178 VM_OBJECT_LOCK(backing_object);
1179 if (object != first_object)
1180 VM_OBJECT_UNLOCK(object);
1184 if (object->flags & OBJ_DEAD) {
1185 if (object != first_object)
1186 VM_OBJECT_UNLOCK(object);
1189 vp = object->handle;
1191 VM_OBJECT_UNLOCK(object);
1192 if (first_object != object)
1193 VM_OBJECT_UNLOCK(first_object);
1194 VFS_ASSERT_GIANT(vp->v_mount);
1195 if (vget(vp, LK_CANRECURSE | LK_INTERLOCK |
1196 LK_RETRY | LK_SHARED, curthread)) {
1197 VM_OBJECT_LOCK(first_object);
1198 if (object != first_object)
1199 VM_OBJECT_LOCK(object);
1200 if (object->type != OBJT_VNODE) {
1201 if (object != first_object)
1202 VM_OBJECT_UNLOCK(object);
1205 printf("vnode_pager_lock: retrying\n");
1208 VM_OBJECT_LOCK(first_object);