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,
87 vm_ooffset_t, struct ucred *cred);
89 struct pagerops vnodepagerops = {
90 .pgo_alloc = vnode_pager_alloc,
91 .pgo_dealloc = vnode_pager_dealloc,
92 .pgo_getpages = vnode_pager_getpages,
93 .pgo_putpages = vnode_pager_putpages,
94 .pgo_haspage = vnode_pager_haspage,
97 int vnode_pbuf_freecnt;
99 /* Create the VM system backing object for this vnode */
101 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
104 vm_ooffset_t size = isize;
107 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
110 while ((object = vp->v_object) != NULL) {
111 VM_OBJECT_LOCK(object);
112 if (!(object->flags & OBJ_DEAD)) {
113 VM_OBJECT_UNLOCK(object);
117 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
118 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0);
119 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
123 if (vn_isdisk(vp, NULL)) {
124 size = IDX_TO_OFF(INT_MAX);
126 if (VOP_GETATTR(vp, &va, td->td_ucred))
132 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
134 * Dereference the reference we just created. This assumes
135 * that the object is associated with the vp.
137 VM_OBJECT_LOCK(object);
139 VM_OBJECT_UNLOCK(object);
142 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
148 vnode_destroy_vobject(struct vnode *vp)
150 struct vm_object *obj;
155 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
157 if (obj->ref_count == 0) {
159 * vclean() may be called twice. The first time
160 * removes the primary reference to the object,
161 * the second time goes one further and is a
162 * special-case to terminate the object.
164 * don't double-terminate the object
166 if ((obj->flags & OBJ_DEAD) == 0)
167 vm_object_terminate(obj);
169 VM_OBJECT_UNLOCK(obj);
172 * Woe to the process that tries to page now :-).
174 vm_pager_deallocate(obj);
175 VM_OBJECT_UNLOCK(obj);
182 * Allocate (or lookup) pager for a vnode.
183 * Handle is a vnode pointer.
188 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
189 vm_ooffset_t offset, struct ucred *cred)
195 * Pageout to vnode, no can do yet.
200 vp = (struct vnode *) handle;
203 * 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 * Add 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;
228 if (vp->v_object != NULL) {
230 * Object has been created while we were sleeping
233 vm_object_destroy(object);
236 vp->v_object = object;
240 VM_OBJECT_UNLOCK(object);
247 * The object must be locked.
250 vnode_pager_dealloc(object)
258 panic("vnode_pager_dealloc: pager already dealloced");
260 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
261 vm_object_pip_wait(object, "vnpdea");
262 refs = object->ref_count;
264 object->handle = NULL;
265 object->type = OBJT_DEAD;
266 if (object->flags & OBJ_DISCONNECTWNT) {
267 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
270 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
272 vp->v_vflag &= ~VV_TEXT;
278 vnode_pager_haspage(object, pindex, before, after)
284 struct vnode *vp = object->handle;
290 int pagesperblock, blocksperpage;
293 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
295 * If no vp or vp is doomed or marked transparent to VM, we do not
298 if (vp == NULL || vp->v_iflag & VI_DOOMED)
301 * If the offset is beyond end of file we do
304 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
307 bsize = vp->v_mount->mnt_stat.f_iosize;
308 pagesperblock = bsize / PAGE_SIZE;
310 if (pagesperblock > 0) {
311 reqblock = pindex / pagesperblock;
313 blocksperpage = (PAGE_SIZE / bsize);
314 reqblock = pindex * blocksperpage;
316 VM_OBJECT_UNLOCK(object);
317 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
318 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
319 VFS_UNLOCK_GIANT(vfslocked);
320 VM_OBJECT_LOCK(object);
325 if (pagesperblock > 0) {
326 poff = pindex - (reqblock * pagesperblock);
328 *before *= pagesperblock;
333 *after *= pagesperblock;
334 numafter = pagesperblock - (poff + 1);
335 if (IDX_TO_OFF(pindex + numafter) >
336 object->un_pager.vnp.vnp_size) {
338 OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
345 *before /= blocksperpage;
349 *after /= blocksperpage;
356 * Lets the VM system know about a change in size for a file.
357 * We adjust our own internal size and flush any cached pages in
358 * the associated object that are affected by the size change.
360 * Note: this routine may be invoked as a result of a pager put
361 * operation (possibly at object termination time), so we must be careful.
364 vnode_pager_setsize(vp, nsize)
370 vm_pindex_t nobjsize;
372 if ((object = vp->v_object) == NULL)
374 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
375 VM_OBJECT_LOCK(object);
376 if (nsize == object->un_pager.vnp.vnp_size) {
378 * Hasn't changed size
380 VM_OBJECT_UNLOCK(object);
383 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
384 if (nsize < object->un_pager.vnp.vnp_size) {
386 * File has shrunk. Toss any cached pages beyond the new EOF.
388 if (nobjsize < object->size)
389 vm_object_page_remove(object, nobjsize, object->size,
392 * this gets rid of garbage at the end of a page that is now
393 * only partially backed by the vnode.
395 * XXX for some reason (I don't know yet), if we take a
396 * completely invalid page and mark it partially valid
397 * it can screw up NFS reads, so we don't allow the case.
399 if ((nsize & PAGE_MASK) &&
400 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
402 int base = (int)nsize & PAGE_MASK;
403 int size = PAGE_SIZE - base;
406 * Clear out partial-page garbage in case
407 * the page has been mapped.
409 pmap_zero_page_area(m, base, size);
412 * Update the valid bits to reflect the blocks that
413 * have been zeroed. Some of these valid bits may
414 * have already been set.
416 vm_page_set_valid(m, base, size);
419 * Round "base" to the next block boundary so that the
420 * dirty bit for a partially zeroed block is not
423 base = roundup2(base, DEV_BSIZE);
426 * Clear out partial-page dirty bits.
428 * note that we do not clear out the valid
429 * bits. This would prevent bogus_page
430 * replacement from working properly.
432 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
433 } else if ((nsize & PAGE_MASK) &&
434 __predict_false(object->cache != NULL)) {
435 vm_page_cache_free(object, OFF_TO_IDX(nsize),
439 object->un_pager.vnp.vnp_size = nsize;
440 object->size = nobjsize;
441 VM_OBJECT_UNLOCK(object);
445 * calculate the linear (byte) disk address of specified virtual
449 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
460 if (vp->v_iflag & VI_DOOMED)
463 bsize = vp->v_mount->mnt_stat.f_iosize;
464 vblock = address / bsize;
465 voffset = address % bsize;
467 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
469 if (*rtaddress != -1)
470 *rtaddress += voffset / DEV_BSIZE;
473 *run *= bsize/PAGE_SIZE;
474 *run -= voffset/PAGE_SIZE;
482 * small block filesystem vnode pager input
485 vnode_pager_input_smlfs(object, m)
499 if (vp->v_iflag & VI_DOOMED)
502 bsize = vp->v_mount->mnt_stat.f_iosize;
504 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
506 sf = sf_buf_alloc(m, 0);
508 for (i = 0; i < PAGE_SIZE / bsize; i++) {
509 vm_ooffset_t address;
511 bits = vm_page_bits(i * bsize, bsize);
515 address = IDX_TO_OFF(m->pindex) + i * bsize;
516 if (address >= object->un_pager.vnp.vnp_size) {
519 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
523 if (fileaddr != -1) {
524 bp = getpbuf(&vnode_pbuf_freecnt);
526 /* build a minimal buffer header */
527 bp->b_iocmd = BIO_READ;
528 bp->b_iodone = bdone;
529 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
530 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
531 bp->b_rcred = crhold(curthread->td_ucred);
532 bp->b_wcred = crhold(curthread->td_ucred);
533 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
534 bp->b_blkno = fileaddr;
536 bp->b_bcount = bsize;
537 bp->b_bufsize = bsize;
538 bp->b_runningbufspace = bp->b_bufsize;
539 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
542 bp->b_iooffset = dbtob(bp->b_blkno);
545 bwait(bp, PVM, "vnsrd");
547 if ((bp->b_ioflags & BIO_ERROR) != 0)
551 * free the buffer header back to the swap buffer pool
554 relpbuf(bp, &vnode_pbuf_freecnt);
558 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
559 KASSERT((m->dirty & bits) == 0,
560 ("vnode_pager_input_smlfs: page %p is dirty", m));
561 VM_OBJECT_LOCK(object);
563 VM_OBJECT_UNLOCK(object);
567 return VM_PAGER_ERROR;
573 * old style vnode pager input routine
576 vnode_pager_input_old(object, m)
587 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
591 * Return failure if beyond current EOF
593 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
597 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
598 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
600 VM_OBJECT_UNLOCK(object);
603 * Allocate a kernel virtual address and initialize so that
604 * we can use VOP_READ/WRITE routines.
606 sf = sf_buf_alloc(m, 0);
608 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
610 auio.uio_iov = &aiov;
612 auio.uio_offset = IDX_TO_OFF(m->pindex);
613 auio.uio_segflg = UIO_SYSSPACE;
614 auio.uio_rw = UIO_READ;
615 auio.uio_resid = size;
616 auio.uio_td = curthread;
618 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
620 int count = size - auio.uio_resid;
624 else if (count != PAGE_SIZE)
625 bzero((caddr_t)sf_buf_kva(sf) + count,
630 VM_OBJECT_LOCK(object);
632 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
634 m->valid = VM_PAGE_BITS_ALL;
635 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
639 * generic vnode pager input routine
643 * Local media VFS's that do not implement their own VOP_GETPAGES
644 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
645 * to implement the previous behaviour.
647 * All other FS's should use the bypass to get to the local media
648 * backing vp's VOP_GETPAGES.
651 vnode_pager_getpages(object, m, count, reqpage)
659 int bytes = count * PAGE_SIZE;
663 VM_OBJECT_UNLOCK(object);
664 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
665 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
666 KASSERT(rtval != EOPNOTSUPP,
667 ("vnode_pager: FS getpages not implemented\n"));
668 VFS_UNLOCK_GIANT(vfslocked);
669 VM_OBJECT_LOCK(object);
674 * This is now called from local media FS's to operate against their
675 * own vnodes if they fail to implement VOP_GETPAGES.
678 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
686 off_t foff, tfoff, nextoff;
687 int i, j, size, bsize, first;
688 daddr_t firstaddr, reqblock;
696 object = vp->v_object;
697 count = bytecount / PAGE_SIZE;
699 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
700 ("vnode_pager_generic_getpages does not support devices"));
701 if (vp->v_iflag & VI_DOOMED)
704 bsize = vp->v_mount->mnt_stat.f_iosize;
706 /* get the UNDERLYING device for the file with VOP_BMAP() */
709 * originally, we did not check for an error return value -- assuming
710 * an fs always has a bmap entry point -- that assumption is wrong!!!
712 foff = IDX_TO_OFF(m[reqpage]->pindex);
715 * if we can't bmap, use old VOP code
717 error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
718 if (error == EOPNOTSUPP) {
719 VM_OBJECT_LOCK(object);
721 for (i = 0; i < count; i++)
725 vm_page_unlock(m[i]);
727 PCPU_INC(cnt.v_vnodein);
728 PCPU_INC(cnt.v_vnodepgsin);
729 error = vnode_pager_input_old(object, m[reqpage]);
730 VM_OBJECT_UNLOCK(object);
732 } else if (error != 0) {
733 VM_OBJECT_LOCK(object);
734 for (i = 0; i < count; i++)
738 vm_page_unlock(m[i]);
740 VM_OBJECT_UNLOCK(object);
741 return (VM_PAGER_ERROR);
744 * if the blocksize is smaller than a page size, then use
745 * special small filesystem code. NFS sometimes has a small
746 * blocksize, but it can handle large reads itself.
748 } else if ((PAGE_SIZE / bsize) > 1 &&
749 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
750 VM_OBJECT_LOCK(object);
751 for (i = 0; i < count; i++)
755 vm_page_unlock(m[i]);
757 VM_OBJECT_UNLOCK(object);
758 PCPU_INC(cnt.v_vnodein);
759 PCPU_INC(cnt.v_vnodepgsin);
760 return vnode_pager_input_smlfs(object, m[reqpage]);
764 * If we have a completely valid page available to us, we can
765 * clean up and return. Otherwise we have to re-read the
768 VM_OBJECT_LOCK(object);
769 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
770 for (i = 0; i < count; i++)
774 vm_page_unlock(m[i]);
776 VM_OBJECT_UNLOCK(object);
778 } else if (reqblock == -1) {
779 pmap_zero_page(m[reqpage]);
780 KASSERT(m[reqpage]->dirty == 0,
781 ("vnode_pager_generic_getpages: page %p is dirty", m));
782 m[reqpage]->valid = VM_PAGE_BITS_ALL;
783 for (i = 0; i < count; i++)
787 vm_page_unlock(m[i]);
789 VM_OBJECT_UNLOCK(object);
790 return (VM_PAGER_OK);
792 m[reqpage]->valid = 0;
793 VM_OBJECT_UNLOCK(object);
796 * here on direct device I/O
801 * calculate the run that includes the required page
803 for (first = 0, i = 0; i < count; i = runend) {
804 if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
806 VM_OBJECT_LOCK(object);
807 for (; i < count; i++)
811 vm_page_unlock(m[i]);
813 VM_OBJECT_UNLOCK(object);
814 return (VM_PAGER_ERROR);
816 if (firstaddr == -1) {
817 VM_OBJECT_LOCK(object);
818 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
819 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
820 (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
823 (object->un_pager.vnp.vnp_size >> 32),
824 (uintmax_t)object->un_pager.vnp.vnp_size);
828 vm_page_unlock(m[i]);
829 VM_OBJECT_UNLOCK(object);
835 if (runend <= reqpage) {
836 VM_OBJECT_LOCK(object);
837 for (j = i; j < runend; j++) {
840 vm_page_unlock(m[j]);
842 VM_OBJECT_UNLOCK(object);
844 if (runpg < (count - first)) {
845 VM_OBJECT_LOCK(object);
846 for (i = first + runpg; i < count; i++) {
849 vm_page_unlock(m[i]);
851 VM_OBJECT_UNLOCK(object);
852 count = first + runpg;
860 * the first and last page have been calculated now, move input pages
861 * to be zero based...
870 * calculate the file virtual address for the transfer
872 foff = IDX_TO_OFF(m[0]->pindex);
875 * calculate the size of the transfer
877 size = count * PAGE_SIZE;
878 KASSERT(count > 0, ("zero count"));
879 if ((foff + size) > object->un_pager.vnp.vnp_size)
880 size = object->un_pager.vnp.vnp_size - foff;
881 KASSERT(size > 0, ("zero size"));
884 * round up physical size for real devices.
887 int secmask = bo->bo_bsize - 1;
888 KASSERT(secmask < PAGE_SIZE && secmask > 0,
889 ("vnode_pager_generic_getpages: sector size %d too large",
891 size = (size + secmask) & ~secmask;
894 bp = getpbuf(&vnode_pbuf_freecnt);
895 kva = (vm_offset_t) bp->b_data;
898 * and map the pages to be read into the kva
900 pmap_qenter(kva, m, count);
902 /* build a minimal buffer header */
903 bp->b_iocmd = BIO_READ;
904 bp->b_iodone = bdone;
905 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
906 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
907 bp->b_rcred = crhold(curthread->td_ucred);
908 bp->b_wcred = crhold(curthread->td_ucred);
909 bp->b_blkno = firstaddr;
912 bp->b_bufsize = size;
913 bp->b_runningbufspace = bp->b_bufsize;
914 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
916 PCPU_INC(cnt.v_vnodein);
917 PCPU_ADD(cnt.v_vnodepgsin, count);
920 bp->b_iooffset = dbtob(bp->b_blkno);
923 bwait(bp, PVM, "vnread");
925 if ((bp->b_ioflags & BIO_ERROR) != 0)
929 if (size != count * PAGE_SIZE)
930 bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
932 pmap_qremove(kva, count);
935 * free the buffer header back to the swap buffer pool
938 relpbuf(bp, &vnode_pbuf_freecnt);
940 VM_OBJECT_LOCK(object);
941 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
944 nextoff = tfoff + PAGE_SIZE;
947 if (nextoff <= object->un_pager.vnp.vnp_size) {
949 * Read filled up entire page.
951 mt->valid = VM_PAGE_BITS_ALL;
952 KASSERT(mt->dirty == 0,
953 ("vnode_pager_generic_getpages: page %p is dirty",
955 KASSERT(!pmap_page_is_mapped(mt),
956 ("vnode_pager_generic_getpages: page %p is mapped",
960 * Read did not fill up entire page.
962 * Currently we do not set the entire page valid,
963 * we just try to clear the piece that we couldn't
966 vm_page_set_valid(mt, 0,
967 object->un_pager.vnp.vnp_size - tfoff);
968 KASSERT((mt->dirty & vm_page_bits(0,
969 object->un_pager.vnp.vnp_size - tfoff)) == 0,
970 ("vnode_pager_generic_getpages: page %p is dirty",
977 * whether or not to leave the page activated is up in
978 * the air, but we should put the page on a page queue
979 * somewhere. (it already is in the object). Result:
980 * It appears that empirical results show that
981 * deactivating pages is best.
985 * just in case someone was asking for this page we
986 * now tell them that it is ok to use
989 if (mt->oflags & VPO_WANTED) {
991 vm_page_activate(mt);
995 vm_page_deactivate(mt);
1006 VM_OBJECT_UNLOCK(object);
1008 printf("vnode_pager_getpages: I/O read error\n");
1010 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
1014 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1015 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1016 * vnode_pager_generic_putpages() to implement the previous behaviour.
1018 * All other FS's should use the bypass to get to the local media
1019 * backing vp's VOP_PUTPAGES.
1022 vnode_pager_putpages(object, m, count, sync, rtvals)
1031 int bytes = count * PAGE_SIZE;
1034 * Force synchronous operation if we are extremely low on memory
1035 * to prevent a low-memory deadlock. VOP operations often need to
1036 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1037 * operation ). The swapper handles the case by limiting the amount
1038 * of asynchronous I/O, but that sort of solution doesn't scale well
1039 * for the vnode pager without a lot of work.
1041 * Also, the backing vnode's iodone routine may not wake the pageout
1042 * daemon up. This should be probably be addressed XXX.
1045 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
1049 * Call device-specific putpages function
1051 vp = object->handle;
1052 VM_OBJECT_UNLOCK(object);
1053 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
1054 KASSERT(rtval != EOPNOTSUPP,
1055 ("vnode_pager: stale FS putpages\n"));
1056 VM_OBJECT_LOCK(object);
1061 * This is now called from local media FS's to operate against their
1062 * own vnodes if they fail to implement VOP_PUTPAGES.
1064 * This is typically called indirectly via the pageout daemon and
1065 * clustering has already typically occured, so in general we ask the
1066 * underlying filesystem to write the data out asynchronously rather
1070 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1071 int flags, int *rtvals)
1078 int maxsize, ncount;
1079 vm_ooffset_t poffset;
1085 static struct timeval lastfail;
1088 object = vp->v_object;
1089 count = bytecount / PAGE_SIZE;
1091 for (i = 0; i < count; i++)
1092 rtvals[i] = VM_PAGER_ERROR;
1094 if ((int64_t)ma[0]->pindex < 0) {
1095 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1096 (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1097 rtvals[0] = VM_PAGER_BAD;
1098 return VM_PAGER_BAD;
1101 maxsize = count * PAGE_SIZE;
1104 poffset = IDX_TO_OFF(ma[0]->pindex);
1107 * If the page-aligned write is larger then the actual file we
1108 * have to invalidate pages occuring beyond the file EOF. However,
1109 * there is an edge case where a file may not be page-aligned where
1110 * the last page is partially invalid. In this case the filesystem
1111 * may not properly clear the dirty bits for the entire page (which
1112 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1113 * With the page locked we are free to fix-up the dirty bits here.
1115 * We do not under any circumstances truncate the valid bits, as
1116 * this will screw up bogus page replacement.
1118 VM_OBJECT_LOCK(object);
1119 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1120 if (object->un_pager.vnp.vnp_size > poffset) {
1123 maxsize = object->un_pager.vnp.vnp_size - poffset;
1124 ncount = btoc(maxsize);
1125 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1127 * If the object is locked and the following
1128 * conditions hold, then the page's dirty
1129 * field cannot be concurrently changed by a
1133 KASSERT(m->busy > 0,
1134 ("vnode_pager_generic_putpages: page %p is not busy", m));
1135 KASSERT((m->aflags & PGA_WRITEABLE) == 0,
1136 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1137 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1144 if (ncount < count) {
1145 for (i = ncount; i < count; i++) {
1146 rtvals[i] = VM_PAGER_BAD;
1150 VM_OBJECT_UNLOCK(object);
1153 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
1154 * rather then a bdwrite() to prevent paging I/O from saturating
1155 * the buffer cache. Dummy-up the sequential heuristic to cause
1156 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
1157 * the system decides how to cluster.
1160 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1162 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1163 ioflags |= IO_ASYNC;
1164 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1165 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1167 aiov.iov_base = (caddr_t) 0;
1168 aiov.iov_len = maxsize;
1169 auio.uio_iov = &aiov;
1170 auio.uio_iovcnt = 1;
1171 auio.uio_offset = poffset;
1172 auio.uio_segflg = UIO_NOCOPY;
1173 auio.uio_rw = UIO_WRITE;
1174 auio.uio_resid = maxsize;
1175 auio.uio_td = (struct thread *) 0;
1176 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1177 PCPU_INC(cnt.v_vnodeout);
1178 PCPU_ADD(cnt.v_vnodepgsout, ncount);
1181 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1182 printf("vnode_pager_putpages: I/O error %d\n", error);
1184 if (auio.uio_resid) {
1185 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1186 printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1187 auio.uio_resid, (u_long)ma[0]->pindex);
1189 for (i = 0; i < ncount; i++) {
1190 rtvals[i] = VM_PAGER_OK;
1196 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1203 obj = ma[0]->object;
1204 VM_OBJECT_LOCK(obj);
1205 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1206 if (pos < trunc_page(written)) {
1207 rtvals[i] = VM_PAGER_OK;
1208 vm_page_undirty(ma[i]);
1210 /* Partially written page. */
1211 rtvals[i] = VM_PAGER_AGAIN;
1212 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1215 VM_OBJECT_UNLOCK(obj);