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>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_pager.h>
72 #include <vm/vm_map.h>
73 #include <vm/vnode_pager.h>
74 #include <vm/vm_extern.h>
76 static void vnode_pager_init(void);
77 static vm_offset_t vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
79 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
80 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
81 static void vnode_pager_dealloc(vm_object_t);
82 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
83 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
84 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
85 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t, vm_ooffset_t);
87 struct pagerops vnodepagerops = {
88 .pgo_init = vnode_pager_init,
89 .pgo_alloc = vnode_pager_alloc,
90 .pgo_dealloc = vnode_pager_dealloc,
91 .pgo_getpages = vnode_pager_getpages,
92 .pgo_putpages = vnode_pager_putpages,
93 .pgo_haspage = vnode_pager_haspage,
96 int vnode_pbuf_freecnt;
99 vnode_pager_init(void)
102 vnode_pbuf_freecnt = nswbuf / 2 + 1;
105 /* Create the VM system backing object for this vnode */
107 vnode_create_vobject(struct vnode *vp, size_t isize, struct thread *td)
110 vm_ooffset_t size = isize;
113 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
116 while ((object = vp->v_object) != NULL) {
117 VM_OBJECT_LOCK(object);
118 if (!(object->flags & OBJ_DEAD)) {
119 VM_OBJECT_UNLOCK(object);
122 VOP_UNLOCK(vp, 0, td);
123 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
124 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vodead", 0);
125 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
129 if (vn_isdisk(vp, NULL)) {
130 size = IDX_TO_OFF(INT_MAX);
132 if (VOP_GETATTR(vp, &va, td->td_ucred, td) != 0)
138 object = vnode_pager_alloc(vp, size, 0, 0);
140 * Dereference the reference we just created. This assumes
141 * that the object is associated with the vp.
143 VM_OBJECT_LOCK(object);
145 VM_OBJECT_UNLOCK(object);
148 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
154 vnode_destroy_vobject(struct vnode *vp)
156 struct vm_object *obj;
161 ASSERT_VOP_LOCKED(vp, "vnode_destroy_vobject");
163 if (obj->ref_count == 0) {
165 * vclean() may be called twice. The first time
166 * removes the primary reference to the object,
167 * the second time goes one further and is a
168 * special-case to terminate the object.
170 * don't double-terminate the object
172 if ((obj->flags & OBJ_DEAD) == 0)
173 vm_object_terminate(obj);
175 VM_OBJECT_UNLOCK(obj);
178 * Woe to the process that tries to page now :-).
180 vm_pager_deallocate(obj);
181 VM_OBJECT_UNLOCK(obj);
188 * Allocate (or lookup) pager for a vnode.
189 * Handle is a vnode pointer.
194 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
201 * Pageout to vnode, no can do yet.
206 vp = (struct vnode *) handle;
208 ASSERT_VOP_LOCKED(vp, "vnode_pager_alloc");
211 * If the object is being terminated, wait for it to
214 while ((object = vp->v_object) != NULL) {
215 VM_OBJECT_LOCK(object);
216 if ((object->flags & OBJ_DEAD) == 0)
218 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
219 msleep(object, VM_OBJECT_MTX(object), PDROP | PVM, "vadead", 0);
222 if (vp->v_usecount == 0)
223 panic("vnode_pager_alloc: no vnode reference");
225 if (object == NULL) {
227 * And an object of the appropriate size
229 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
231 object->un_pager.vnp.vnp_size = size;
233 object->handle = handle;
234 if (VFS_NEEDSGIANT(vp->v_mount))
235 vm_object_set_flag(object, OBJ_NEEDGIANT);
236 vp->v_object = object;
239 VM_OBJECT_UNLOCK(object);
246 * The object must be locked.
249 vnode_pager_dealloc(object)
252 struct vnode *vp = object->handle;
255 panic("vnode_pager_dealloc: pager already dealloced");
257 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
258 vm_object_pip_wait(object, "vnpdea");
260 object->handle = NULL;
261 object->type = OBJT_DEAD;
262 if (object->flags & OBJ_DISCONNECTWNT) {
263 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
266 ASSERT_VOP_LOCKED(vp, "vnode_pager_dealloc");
268 vp->v_vflag &= ~VV_TEXT;
272 vnode_pager_haspage(object, pindex, before, after)
278 struct vnode *vp = object->handle;
284 int pagesperblock, blocksperpage;
287 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
289 * If no vp or vp is doomed or marked transparent to VM, we do not
296 if (vp->v_iflag & VI_DOOMED) {
302 * If filesystem no longer mounted or offset beyond end of file we do
305 if ((vp->v_mount == NULL) ||
306 (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size))
309 bsize = vp->v_mount->mnt_stat.f_iosize;
310 pagesperblock = bsize / PAGE_SIZE;
312 if (pagesperblock > 0) {
313 reqblock = pindex / pagesperblock;
315 blocksperpage = (PAGE_SIZE / bsize);
316 reqblock = pindex * blocksperpage;
318 VM_OBJECT_UNLOCK(object);
319 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
320 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
321 VFS_UNLOCK_GIANT(vfslocked);
322 VM_OBJECT_LOCK(object);
327 if (pagesperblock > 0) {
328 poff = pindex - (reqblock * pagesperblock);
330 *before *= pagesperblock;
335 *after *= pagesperblock;
336 numafter = pagesperblock - (poff + 1);
337 if (IDX_TO_OFF(pindex + numafter) >
338 object->un_pager.vnp.vnp_size) {
340 OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
347 *before /= blocksperpage;
351 *after /= blocksperpage;
358 * Lets the VM system know about a change in size for a file.
359 * We adjust our own internal size and flush any cached pages in
360 * the associated object that are affected by the size change.
362 * Note: this routine may be invoked as a result of a pager put
363 * operation (possibly at object termination time), so we must be careful.
366 vnode_pager_setsize(vp, nsize)
372 vm_pindex_t nobjsize;
374 if ((object = vp->v_object) == NULL)
376 VM_OBJECT_LOCK(object);
377 if (nsize == object->un_pager.vnp.vnp_size) {
379 * Hasn't changed size
381 VM_OBJECT_UNLOCK(object);
384 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
385 if (nsize < object->un_pager.vnp.vnp_size) {
387 * File has shrunk. Toss any cached pages beyond the new EOF.
389 if (nobjsize < object->size)
390 vm_object_page_remove(object, nobjsize, object->size,
393 * this gets rid of garbage at the end of a page that is now
394 * only partially backed by the vnode.
396 * XXX for some reason (I don't know yet), if we take a
397 * completely invalid page and mark it partially valid
398 * it can screw up NFS reads, so we don't allow the case.
400 if ((nsize & PAGE_MASK) &&
401 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
403 int base = (int)nsize & PAGE_MASK;
404 int size = PAGE_SIZE - base;
407 * Clear out partial-page garbage in case
408 * the page has been mapped.
410 pmap_zero_page_area(m, base, size);
413 * XXX work around SMP data integrity race
414 * by unmapping the page from user processes.
415 * The garbage we just cleared may be mapped
416 * to a user process running on another cpu
417 * and this code is not running through normal
418 * I/O channels which handle SMP issues for
419 * us, so unmap page to synchronize all cpus.
421 * XXX should vm_pager_unmap_page() have
424 vm_page_lock_queues();
428 * Clear out partial-page dirty bits. This
429 * has the side effect of setting the valid
430 * bits, but that is ok. There are a bunch
431 * of places in the VM system where we expected
432 * m->dirty == VM_PAGE_BITS_ALL. The file EOF
433 * case is one of them. If the page is still
434 * partially dirty, make it fully dirty.
436 * note that we do not clear out the valid
437 * bits. This would prevent bogus_page
438 * replacement from working properly.
440 vm_page_set_validclean(m, base, size);
442 m->dirty = VM_PAGE_BITS_ALL;
443 vm_page_unlock_queues();
446 object->un_pager.vnp.vnp_size = nsize;
447 object->size = nobjsize;
448 VM_OBJECT_UNLOCK(object);
452 * calculate the linear (byte) disk address of specified virtual
456 vnode_pager_addr(vp, address, run)
458 vm_ooffset_t address;
471 if (vp->v_mount == NULL)
474 bsize = vp->v_mount->mnt_stat.f_iosize;
475 vblock = address / bsize;
476 voffset = address % bsize;
478 err = VOP_BMAP(vp, vblock, NULL, &block, run, NULL);
480 if (err || (block == -1))
483 rtaddress = block + voffset / DEV_BSIZE;
486 *run *= bsize/PAGE_SIZE;
487 *run -= voffset/PAGE_SIZE;
495 * small block filesystem vnode pager input
498 vnode_pager_input_smlfs(object, m)
512 if (vp->v_mount == NULL)
515 bsize = vp->v_mount->mnt_stat.f_iosize;
517 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
519 sf = sf_buf_alloc(m, 0);
521 for (i = 0; i < PAGE_SIZE / bsize; i++) {
522 vm_ooffset_t address;
524 if (vm_page_bits(i * bsize, bsize) & m->valid)
527 address = IDX_TO_OFF(m->pindex) + i * bsize;
528 if (address >= object->un_pager.vnp.vnp_size) {
531 fileaddr = vnode_pager_addr(vp, address, NULL);
533 if (fileaddr != -1) {
534 bp = getpbuf(&vnode_pbuf_freecnt);
536 /* build a minimal buffer header */
537 bp->b_iocmd = BIO_READ;
538 bp->b_iodone = bdone;
539 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
540 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
541 bp->b_rcred = crhold(curthread->td_ucred);
542 bp->b_wcred = crhold(curthread->td_ucred);
543 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
544 bp->b_blkno = fileaddr;
546 bp->b_bcount = bsize;
547 bp->b_bufsize = bsize;
548 bp->b_runningbufspace = bp->b_bufsize;
549 runningbufspace += bp->b_runningbufspace;
552 bp->b_iooffset = dbtob(bp->b_blkno);
555 /* we definitely need to be at splvm here */
557 bwait(bp, PVM, "vnsrd");
559 if ((bp->b_ioflags & BIO_ERROR) != 0)
563 * free the buffer header back to the swap buffer pool
566 relpbuf(bp, &vnode_pbuf_freecnt);
570 VM_OBJECT_LOCK(object);
571 vm_page_lock_queues();
572 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
573 vm_page_unlock_queues();
574 VM_OBJECT_UNLOCK(object);
576 VM_OBJECT_LOCK(object);
577 vm_page_lock_queues();
578 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
579 vm_page_unlock_queues();
580 VM_OBJECT_UNLOCK(object);
581 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
585 vm_page_lock_queues();
586 pmap_clear_modify(m);
587 vm_page_unlock_queues();
589 return VM_PAGER_ERROR;
597 * old style vnode pager input routine
600 vnode_pager_input_old(object, m)
611 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
615 * Return failure if beyond current EOF
617 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
621 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
622 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
624 VM_OBJECT_UNLOCK(object);
627 * Allocate a kernel virtual address and initialize so that
628 * we can use VOP_READ/WRITE routines.
630 sf = sf_buf_alloc(m, 0);
632 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
634 auio.uio_iov = &aiov;
636 auio.uio_offset = IDX_TO_OFF(m->pindex);
637 auio.uio_segflg = UIO_SYSSPACE;
638 auio.uio_rw = UIO_READ;
639 auio.uio_resid = size;
640 auio.uio_td = curthread;
642 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
644 int count = size - auio.uio_resid;
648 else if (count != PAGE_SIZE)
649 bzero((caddr_t)sf_buf_kva(sf) + count,
654 VM_OBJECT_LOCK(object);
656 vm_page_lock_queues();
657 pmap_clear_modify(m);
659 vm_page_unlock_queues();
661 m->valid = VM_PAGE_BITS_ALL;
662 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
666 * generic vnode pager input routine
670 * Local media VFS's that do not implement their own VOP_GETPAGES
671 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
672 * to implement the previous behaviour.
674 * All other FS's should use the bypass to get to the local media
675 * backing vp's VOP_GETPAGES.
678 vnode_pager_getpages(object, m, count, reqpage)
686 int bytes = count * PAGE_SIZE;
690 VM_OBJECT_UNLOCK(object);
691 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
692 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
693 KASSERT(rtval != EOPNOTSUPP,
694 ("vnode_pager: FS getpages not implemented\n"));
695 VFS_UNLOCK_GIANT(vfslocked);
696 VM_OBJECT_LOCK(object);
701 * This is now called from local media FS's to operate against their
702 * own vnodes if they fail to implement VOP_GETPAGES.
705 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
713 off_t foff, tfoff, nextoff;
714 int i, j, size, bsize, first, firstaddr;
722 object = vp->v_object;
723 count = bytecount / PAGE_SIZE;
725 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
726 ("vnode_pager_generic_getpages does not support devices"));
727 if (vp->v_mount == NULL)
730 bsize = vp->v_mount->mnt_stat.f_iosize;
732 /* get the UNDERLYING device for the file with VOP_BMAP() */
735 * originally, we did not check for an error return value -- assuming
736 * an fs always has a bmap entry point -- that assumption is wrong!!!
738 foff = IDX_TO_OFF(m[reqpage]->pindex);
741 * if we can't bmap, use old VOP code
743 if (VOP_BMAP(vp, 0, &bo, 0, NULL, NULL)) {
744 VM_OBJECT_LOCK(object);
745 vm_page_lock_queues();
746 for (i = 0; i < count; i++)
749 vm_page_unlock_queues();
752 error = vnode_pager_input_old(object, m[reqpage]);
753 VM_OBJECT_UNLOCK(object);
757 * if the blocksize is smaller than a page size, then use
758 * special small filesystem code. NFS sometimes has a small
759 * blocksize, but it can handle large reads itself.
761 } else if ((PAGE_SIZE / bsize) > 1 &&
762 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
763 VM_OBJECT_LOCK(object);
764 vm_page_lock_queues();
765 for (i = 0; i < count; i++)
768 vm_page_unlock_queues();
769 VM_OBJECT_UNLOCK(object);
772 return vnode_pager_input_smlfs(object, m[reqpage]);
776 * If we have a completely valid page available to us, we can
777 * clean up and return. Otherwise we have to re-read the
780 VM_OBJECT_LOCK(object);
781 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
782 vm_page_lock_queues();
783 for (i = 0; i < count; i++)
786 vm_page_unlock_queues();
787 VM_OBJECT_UNLOCK(object);
790 m[reqpage]->valid = 0;
791 VM_OBJECT_UNLOCK(object);
794 * here on direct device I/O
799 * calculate the run that includes the required page
801 for (first = 0, i = 0; i < count; i = runend) {
802 firstaddr = vnode_pager_addr(vp,
803 IDX_TO_OFF(m[i]->pindex), &runpg);
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: %d, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
808 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...
850 for (i = first; i < count; i++) {
858 * calculate the file virtual address for the transfer
860 foff = IDX_TO_OFF(m[0]->pindex);
863 * calculate the size of the transfer
865 size = count * PAGE_SIZE;
866 KASSERT(count > 0, ("zero count"));
867 if ((foff + size) > object->un_pager.vnp.vnp_size)
868 size = object->un_pager.vnp.vnp_size - foff;
869 KASSERT(size > 0, ("zero size"));
872 * round up physical size for real devices.
875 int secmask = bo->bo_bsize - 1;
876 KASSERT(secmask < PAGE_SIZE && secmask > 0,
877 ("vnode_pager_generic_getpages: sector size %d too large",
879 size = (size + secmask) & ~secmask;
882 bp = getpbuf(&vnode_pbuf_freecnt);
883 kva = (vm_offset_t) bp->b_data;
886 * and map the pages to be read into the kva
888 pmap_qenter(kva, m, count);
890 /* build a minimal buffer header */
891 bp->b_iocmd = BIO_READ;
892 bp->b_iodone = bdone;
893 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
894 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
895 bp->b_rcred = crhold(curthread->td_ucred);
896 bp->b_wcred = crhold(curthread->td_ucred);
897 bp->b_blkno = firstaddr;
900 bp->b_bufsize = size;
901 bp->b_runningbufspace = bp->b_bufsize;
902 runningbufspace += bp->b_runningbufspace;
905 cnt.v_vnodepgsin += count;
908 bp->b_iooffset = dbtob(bp->b_blkno);
911 bwait(bp, PVM, "vnread");
913 if ((bp->b_ioflags & BIO_ERROR) != 0)
917 if (size != count * PAGE_SIZE)
918 bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
920 pmap_qremove(kva, count);
923 * free the buffer header back to the swap buffer pool
926 relpbuf(bp, &vnode_pbuf_freecnt);
928 VM_OBJECT_LOCK(object);
929 vm_page_lock_queues();
930 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
933 nextoff = tfoff + PAGE_SIZE;
936 if (nextoff <= object->un_pager.vnp.vnp_size) {
938 * Read filled up entire page.
940 mt->valid = VM_PAGE_BITS_ALL;
941 vm_page_undirty(mt); /* should be an assert? XXX */
942 pmap_clear_modify(mt);
945 * Read did not fill up entire page. Since this
946 * is getpages, the page may be mapped, so we have
947 * to zero the invalid portions of the page even
948 * though we aren't setting them valid.
950 * Currently we do not set the entire page valid,
951 * we just try to clear the piece that we couldn't
954 vm_page_set_validclean(mt, 0,
955 object->un_pager.vnp.vnp_size - tfoff);
956 /* handled by vm_fault now */
957 /* vm_page_zero_invalid(mt, FALSE); */
963 * whether or not to leave the page activated is up in
964 * the air, but we should put the page on a page queue
965 * somewhere. (it already is in the object). Result:
966 * It appears that empirical results show that
967 * deactivating pages is best.
971 * just in case someone was asking for this page we
972 * now tell them that it is ok to use
975 if (mt->flags & PG_WANTED)
976 vm_page_activate(mt);
978 vm_page_deactivate(mt);
985 vm_page_unlock_queues();
986 VM_OBJECT_UNLOCK(object);
988 printf("vnode_pager_getpages: I/O read error\n");
990 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
994 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
995 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
996 * vnode_pager_generic_putpages() to implement the previous behaviour.
998 * All other FS's should use the bypass to get to the local media
999 * backing vp's VOP_PUTPAGES.
1002 vnode_pager_putpages(object, m, count, sync, rtvals)
1012 int bytes = count * PAGE_SIZE;
1015 * Force synchronous operation if we are extremely low on memory
1016 * to prevent a low-memory deadlock. VOP operations often need to
1017 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1018 * operation ). The swapper handles the case by limiting the amount
1019 * of asynchronous I/O, but that sort of solution doesn't scale well
1020 * for the vnode pager without a lot of work.
1022 * Also, the backing vnode's iodone routine may not wake the pageout
1023 * daemon up. This should be probably be addressed XXX.
1026 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
1030 * Call device-specific putpages function
1032 vp = object->handle;
1033 VM_OBJECT_UNLOCK(object);
1034 if (vp->v_type != VREG)
1036 (void)vn_start_write(vp, &mp, V_WAIT);
1037 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
1038 KASSERT(rtval != EOPNOTSUPP,
1039 ("vnode_pager: stale FS putpages\n"));
1040 vn_finished_write(mp);
1041 VM_OBJECT_LOCK(object);
1046 * This is now called from local media FS's to operate against their
1047 * own vnodes if they fail to implement VOP_PUTPAGES.
1049 * This is typically called indirectly via the pageout daemon and
1050 * clustering has already typically occured, so in general we ask the
1051 * underlying filesystem to write the data out asynchronously rather
1055 vnode_pager_generic_putpages(vp, m, bytecount, flags, rtvals)
1066 int maxsize, ncount;
1067 vm_ooffset_t poffset;
1073 object = vp->v_object;
1074 count = bytecount / PAGE_SIZE;
1076 for (i = 0; i < count; i++)
1077 rtvals[i] = VM_PAGER_AGAIN;
1079 if ((int64_t)m[0]->pindex < 0) {
1080 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1081 (long)m[0]->pindex, (u_long)m[0]->dirty);
1082 rtvals[0] = VM_PAGER_BAD;
1083 return VM_PAGER_BAD;
1086 maxsize = count * PAGE_SIZE;
1089 poffset = IDX_TO_OFF(m[0]->pindex);
1092 * If the page-aligned write is larger then the actual file we
1093 * have to invalidate pages occuring beyond the file EOF. However,
1094 * there is an edge case where a file may not be page-aligned where
1095 * the last page is partially invalid. In this case the filesystem
1096 * may not properly clear the dirty bits for the entire page (which
1097 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1098 * With the page locked we are free to fix-up the dirty bits here.
1100 * We do not under any circumstances truncate the valid bits, as
1101 * this will screw up bogus page replacement.
1103 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1104 if (object->un_pager.vnp.vnp_size > poffset) {
1107 maxsize = object->un_pager.vnp.vnp_size - poffset;
1108 ncount = btoc(maxsize);
1109 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1110 vm_page_lock_queues();
1111 vm_page_clear_dirty(m[ncount - 1], pgoff,
1113 vm_page_unlock_queues();
1119 if (ncount < count) {
1120 for (i = ncount; i < count; i++) {
1121 rtvals[i] = VM_PAGER_BAD;
1127 * pageouts are already clustered, use IO_ASYNC t o force a bawrite()
1128 * rather then a bdwrite() to prevent paging I/O from saturating
1129 * the buffer cache. Dummy-up the sequential heuristic to cause
1130 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
1131 * the system decides how to cluster.
1134 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1136 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1137 ioflags |= IO_ASYNC;
1138 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1139 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1141 aiov.iov_base = (caddr_t) 0;
1142 aiov.iov_len = maxsize;
1143 auio.uio_iov = &aiov;
1144 auio.uio_iovcnt = 1;
1145 auio.uio_offset = poffset;
1146 auio.uio_segflg = UIO_NOCOPY;
1147 auio.uio_rw = UIO_WRITE;
1148 auio.uio_resid = maxsize;
1149 auio.uio_td = (struct thread *) 0;
1150 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1152 cnt.v_vnodepgsout += ncount;
1155 printf("vnode_pager_putpages: I/O error %d\n", error);
1157 if (auio.uio_resid) {
1158 printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1159 auio.uio_resid, (u_long)m[0]->pindex);
1161 for (i = 0; i < ncount; i++) {
1162 rtvals[i] = VM_PAGER_OK;
1168 vnode_pager_lock(vm_object_t first_object)
1171 vm_object_t backing_object, object;
1173 VM_OBJECT_LOCK_ASSERT(first_object, MA_OWNED);
1174 for (object = first_object; object != NULL; object = backing_object) {
1175 if (object->type != OBJT_VNODE) {
1176 if ((backing_object = object->backing_object) != NULL)
1177 VM_OBJECT_LOCK(backing_object);
1178 if (object != first_object)
1179 VM_OBJECT_UNLOCK(object);
1183 if (object->flags & OBJ_DEAD) {
1184 if (object != first_object)
1185 VM_OBJECT_UNLOCK(object);
1188 vp = object->handle;
1190 VM_OBJECT_UNLOCK(object);
1191 if (first_object != object)
1192 VM_OBJECT_UNLOCK(first_object);
1193 VFS_ASSERT_GIANT(vp->v_mount);
1194 if (vget(vp, LK_CANRECURSE | LK_INTERLOCK |
1195 LK_RETRY | LK_SHARED, curthread)) {
1196 VM_OBJECT_LOCK(first_object);
1197 if (object != first_object)
1198 VM_OBJECT_LOCK(object);
1199 if (object->type != OBJT_VNODE) {
1200 if (object != first_object)
1201 VM_OBJECT_UNLOCK(object);
1204 printf("vnode_pager_lock: retrying\n");
1207 VM_OBJECT_LOCK(first_object);