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/rwlock.h>
67 #include <sys/sf_buf.h>
69 #include <machine/atomic.h>
72 #include <vm/vm_param.h>
73 #include <vm/vm_object.h>
74 #include <vm/vm_page.h>
75 #include <vm/vm_pager.h>
76 #include <vm/vm_map.h>
77 #include <vm/vnode_pager.h>
78 #include <vm/vm_extern.h>
80 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
81 daddr_t *rtaddress, int *run);
82 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
83 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
84 static void vnode_pager_dealloc(vm_object_t);
85 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
86 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, boolean_t, int *);
87 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
88 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
89 vm_ooffset_t, struct ucred *cred);
91 struct pagerops vnodepagerops = {
92 .pgo_alloc = vnode_pager_alloc,
93 .pgo_dealloc = vnode_pager_dealloc,
94 .pgo_getpages = vnode_pager_getpages,
95 .pgo_putpages = vnode_pager_putpages,
96 .pgo_haspage = vnode_pager_haspage,
99 int vnode_pbuf_freecnt;
101 /* Create the VM system backing object for this vnode */
103 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
106 vm_ooffset_t size = isize;
109 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
112 while ((object = vp->v_object) != NULL) {
113 VM_OBJECT_WLOCK(object);
114 if (!(object->flags & OBJ_DEAD)) {
115 VM_OBJECT_WUNLOCK(object);
119 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
120 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
121 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
125 if (vn_isdisk(vp, NULL)) {
126 size = IDX_TO_OFF(INT_MAX);
128 if (VOP_GETATTR(vp, &va, td->td_ucred))
134 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
136 * Dereference the reference we just created. This assumes
137 * that the object is associated with the vp.
139 VM_OBJECT_WLOCK(object);
141 VM_OBJECT_WUNLOCK(object);
144 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
150 vnode_destroy_vobject(struct vnode *vp)
152 struct vm_object *obj;
157 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
158 VM_OBJECT_WLOCK(obj);
159 if (obj->ref_count == 0) {
161 * vclean() may be called twice. The first time
162 * removes the primary reference to the object,
163 * the second time goes one further and is a
164 * special-case to terminate the object.
166 * don't double-terminate the object
168 if ((obj->flags & OBJ_DEAD) == 0)
169 vm_object_terminate(obj);
171 VM_OBJECT_WUNLOCK(obj);
174 * Woe to the process that tries to page now :-).
176 vm_pager_deallocate(obj);
177 VM_OBJECT_WUNLOCK(obj);
184 * Allocate (or lookup) pager for a vnode.
185 * Handle is a vnode pointer.
190 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
191 vm_ooffset_t offset, struct ucred *cred)
197 * Pageout to vnode, no can do yet.
202 vp = (struct vnode *) handle;
205 * If the object is being terminated, wait for it to
209 while ((object = vp->v_object) != NULL) {
210 VM_OBJECT_WLOCK(object);
211 if ((object->flags & OBJ_DEAD) == 0)
213 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
214 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
217 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
219 if (object == NULL) {
221 * Add an object of the appropriate size
223 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
225 object->un_pager.vnp.vnp_size = size;
226 object->un_pager.vnp.writemappings = 0;
228 object->handle = handle;
230 if (vp->v_object != NULL) {
232 * Object has been created while we were sleeping
235 vm_object_destroy(object);
238 vp->v_object = object;
242 VM_OBJECT_WUNLOCK(object);
249 * The object must be locked.
252 vnode_pager_dealloc(object)
260 panic("vnode_pager_dealloc: pager already dealloced");
262 VM_OBJECT_ASSERT_WLOCKED(object);
263 vm_object_pip_wait(object, "vnpdea");
264 refs = object->ref_count;
266 object->handle = NULL;
267 object->type = OBJT_DEAD;
268 if (object->flags & OBJ_DISCONNECTWNT) {
269 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
272 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
273 if (object->un_pager.vnp.writemappings > 0) {
274 object->un_pager.vnp.writemappings = 0;
275 VOP_ADD_WRITECOUNT(vp, -1);
276 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
277 __func__, vp, vp->v_writecount);
281 VM_OBJECT_WUNLOCK(object);
284 VM_OBJECT_WLOCK(object);
288 vnode_pager_haspage(object, pindex, before, after)
294 struct vnode *vp = object->handle;
300 int pagesperblock, blocksperpage;
302 VM_OBJECT_ASSERT_WLOCKED(object);
304 * If no vp or vp is doomed or marked transparent to VM, we do not
307 if (vp == NULL || vp->v_iflag & VI_DOOMED)
310 * If the offset is beyond end of file we do
313 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
316 bsize = vp->v_mount->mnt_stat.f_iosize;
317 pagesperblock = bsize / PAGE_SIZE;
319 if (pagesperblock > 0) {
320 reqblock = pindex / pagesperblock;
322 blocksperpage = (PAGE_SIZE / bsize);
323 reqblock = pindex * blocksperpage;
325 VM_OBJECT_WUNLOCK(object);
326 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
327 VM_OBJECT_WLOCK(object);
332 if (pagesperblock > 0) {
333 poff = pindex - (reqblock * pagesperblock);
335 *before *= pagesperblock;
340 *after *= pagesperblock;
341 numafter = pagesperblock - (poff + 1);
342 if (IDX_TO_OFF(pindex + numafter) >
343 object->un_pager.vnp.vnp_size) {
345 OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
352 *before /= blocksperpage;
356 *after /= blocksperpage;
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.
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.
371 vnode_pager_setsize(vp, nsize)
377 vm_pindex_t nobjsize;
379 if ((object = vp->v_object) == NULL)
381 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
382 VM_OBJECT_WLOCK(object);
383 if (object->type == OBJT_DEAD) {
384 VM_OBJECT_WUNLOCK(object);
387 KASSERT(object->type == OBJT_VNODE,
388 ("not vnode-backed object %p", object));
389 if (nsize == object->un_pager.vnp.vnp_size) {
391 * Hasn't changed size
393 VM_OBJECT_WUNLOCK(object);
396 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
397 if (nsize < object->un_pager.vnp.vnp_size) {
399 * File has shrunk. Toss any cached pages beyond the new EOF.
401 if (nobjsize < object->size)
402 vm_object_page_remove(object, nobjsize, object->size,
405 * this gets rid of garbage at the end of a page that is now
406 * only partially backed by the vnode.
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.
412 if ((nsize & PAGE_MASK) &&
413 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
415 int base = (int)nsize & PAGE_MASK;
416 int size = PAGE_SIZE - base;
419 * Clear out partial-page garbage in case
420 * the page has been mapped.
422 pmap_zero_page_area(m, base, size);
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.
429 vm_page_set_valid_range(m, base, size);
432 * Round "base" to the next block boundary so that the
433 * dirty bit for a partially zeroed block is not
436 base = roundup2(base, DEV_BSIZE);
439 * Clear out partial-page dirty bits.
441 * note that we do not clear out the valid
442 * bits. This would prevent bogus_page
443 * replacement from working properly.
445 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
446 } else if ((nsize & PAGE_MASK) &&
447 vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
448 vm_page_cache_free(object, OFF_TO_IDX(nsize),
452 object->un_pager.vnp.vnp_size = nsize;
453 object->size = nobjsize;
454 VM_OBJECT_WUNLOCK(object);
458 * calculate the linear (byte) disk address of specified virtual
462 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
473 if (vp->v_iflag & VI_DOOMED)
476 bsize = vp->v_mount->mnt_stat.f_iosize;
477 vblock = address / bsize;
478 voffset = address % bsize;
480 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
482 if (*rtaddress != -1)
483 *rtaddress += 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)
513 if (vp->v_iflag & VI_DOOMED)
516 bsize = vp->v_mount->mnt_stat.f_iosize;
518 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
520 sf = sf_buf_alloc(m, 0);
522 for (i = 0; i < PAGE_SIZE / bsize; i++) {
523 vm_ooffset_t address;
525 bits = vm_page_bits(i * bsize, bsize);
529 address = IDX_TO_OFF(m->pindex) + i * bsize;
530 if (address >= object->un_pager.vnp.vnp_size) {
533 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
537 if (fileaddr != -1) {
538 bp = getpbuf(&vnode_pbuf_freecnt);
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;
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);
557 bp->b_iooffset = dbtob(bp->b_blkno);
560 bwait(bp, PVM, "vnsrd");
562 if ((bp->b_ioflags & BIO_ERROR) != 0)
566 * free the buffer header back to the swap buffer pool
570 relpbuf(bp, &vnode_pbuf_freecnt);
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_WLOCK(object);
579 VM_OBJECT_WUNLOCK(object);
583 return VM_PAGER_ERROR;
589 * old style vnode pager input routine
592 vnode_pager_input_old(object, m)
603 VM_OBJECT_ASSERT_WLOCKED(object);
607 * Return failure if beyond current EOF
609 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_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);
616 VM_OBJECT_WUNLOCK(object);
619 * Allocate a kernel virtual address and initialize so that
620 * we can use VOP_READ/WRITE routines.
622 sf = sf_buf_alloc(m, 0);
624 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
626 auio.uio_iov = &aiov;
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;
634 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
636 int count = size - auio.uio_resid;
640 else if (count != PAGE_SIZE)
641 bzero((caddr_t)sf_buf_kva(sf) + count,
646 VM_OBJECT_WLOCK(object);
648 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
650 m->valid = VM_PAGE_BITS_ALL;
651 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
655 * generic vnode pager input routine
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.
663 * All other FS's should use the bypass to get to the local media
664 * backing vp's VOP_GETPAGES.
667 vnode_pager_getpages(object, m, count, reqpage)
675 int bytes = count * PAGE_SIZE;
678 VM_OBJECT_WUNLOCK(object);
679 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
680 KASSERT(rtval != EOPNOTSUPP,
681 ("vnode_pager: FS getpages not implemented\n"));
682 VM_OBJECT_WLOCK(object);
687 * This is now called from local media FS's to operate against their
688 * own vnodes if they fail to implement VOP_GETPAGES.
691 vnode_pager_generic_getpages(vp, m, bytecount, reqpage)
699 off_t foff, tfoff, nextoff;
700 int i, j, size, bsize, first;
701 daddr_t firstaddr, reqblock;
710 object = vp->v_object;
711 count = bytecount / PAGE_SIZE;
713 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
714 ("vnode_pager_generic_getpages does not support devices"));
715 if (vp->v_iflag & VI_DOOMED)
718 bsize = vp->v_mount->mnt_stat.f_iosize;
720 /* get the UNDERLYING device for the file with VOP_BMAP() */
723 * originally, we did not check for an error return value -- assuming
724 * an fs always has a bmap entry point -- that assumption is wrong!!!
726 foff = IDX_TO_OFF(m[reqpage]->pindex);
729 * if we can't bmap, use old VOP code
731 error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
732 if (error == EOPNOTSUPP) {
733 VM_OBJECT_WLOCK(object);
735 for (i = 0; i < count; i++)
739 vm_page_unlock(m[i]);
741 PCPU_INC(cnt.v_vnodein);
742 PCPU_INC(cnt.v_vnodepgsin);
743 error = vnode_pager_input_old(object, m[reqpage]);
744 VM_OBJECT_WUNLOCK(object);
746 } else if (error != 0) {
747 VM_OBJECT_WLOCK(object);
748 for (i = 0; i < count; i++)
752 vm_page_unlock(m[i]);
754 VM_OBJECT_WUNLOCK(object);
755 return (VM_PAGER_ERROR);
758 * if the blocksize is smaller than a page size, then use
759 * special small filesystem code. NFS sometimes has a small
760 * blocksize, but it can handle large reads itself.
762 } else if ((PAGE_SIZE / bsize) > 1 &&
763 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
764 VM_OBJECT_WLOCK(object);
765 for (i = 0; i < count; i++)
769 vm_page_unlock(m[i]);
771 VM_OBJECT_WUNLOCK(object);
772 PCPU_INC(cnt.v_vnodein);
773 PCPU_INC(cnt.v_vnodepgsin);
774 return vnode_pager_input_smlfs(object, m[reqpage]);
778 * If we have a completely valid page available to us, we can
779 * clean up and return. Otherwise we have to re-read the
782 VM_OBJECT_WLOCK(object);
783 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
784 for (i = 0; i < count; i++)
788 vm_page_unlock(m[i]);
790 VM_OBJECT_WUNLOCK(object);
792 } else if (reqblock == -1) {
793 pmap_zero_page(m[reqpage]);
794 KASSERT(m[reqpage]->dirty == 0,
795 ("vnode_pager_generic_getpages: page %p is dirty", m));
796 m[reqpage]->valid = VM_PAGE_BITS_ALL;
797 for (i = 0; i < count; i++)
801 vm_page_unlock(m[i]);
803 VM_OBJECT_WUNLOCK(object);
804 return (VM_PAGER_OK);
806 m[reqpage]->valid = 0;
807 VM_OBJECT_WUNLOCK(object);
810 * here on direct device I/O
815 * calculate the run that includes the required page
817 for (first = 0, i = 0; i < count; i = runend) {
818 if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
820 VM_OBJECT_WLOCK(object);
821 for (; i < count; i++)
825 vm_page_unlock(m[i]);
827 VM_OBJECT_WUNLOCK(object);
828 return (VM_PAGER_ERROR);
830 if (firstaddr == -1) {
831 VM_OBJECT_WLOCK(object);
832 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
833 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
834 (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
837 (object->un_pager.vnp.vnp_size >> 32),
838 (uintmax_t)object->un_pager.vnp.vnp_size);
842 vm_page_unlock(m[i]);
843 VM_OBJECT_WUNLOCK(object);
849 if (runend <= reqpage) {
850 VM_OBJECT_WLOCK(object);
851 for (j = i; j < runend; j++) {
854 vm_page_unlock(m[j]);
856 VM_OBJECT_WUNLOCK(object);
858 if (runpg < (count - first)) {
859 VM_OBJECT_WLOCK(object);
860 for (i = first + runpg; i < count; i++) {
863 vm_page_unlock(m[i]);
865 VM_OBJECT_WUNLOCK(object);
866 count = first + runpg;
874 * the first and last page have been calculated now, move input pages
875 * to be zero based...
884 * calculate the file virtual address for the transfer
886 foff = IDX_TO_OFF(m[0]->pindex);
889 * calculate the size of the transfer
891 size = count * PAGE_SIZE;
892 KASSERT(count > 0, ("zero count"));
893 if ((foff + size) > object->un_pager.vnp.vnp_size)
894 size = object->un_pager.vnp.vnp_size - foff;
895 KASSERT(size > 0, ("zero size"));
898 * round up physical size for real devices.
901 int secmask = bo->bo_bsize - 1;
902 KASSERT(secmask < PAGE_SIZE && secmask > 0,
903 ("vnode_pager_generic_getpages: sector size %d too large",
905 size = (size + secmask) & ~secmask;
908 bp = getpbuf(&vnode_pbuf_freecnt);
909 kva = (vm_offset_t)bp->b_data;
912 * and map the pages to be read into the kva, if the filesystem
913 * requires mapped buffers.
916 if (mp != NULL && (mp->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
917 unmapped_buf_allowed) {
918 bp->b_data = unmapped_buf;
919 bp->b_kvabase = unmapped_buf;
921 bp->b_flags |= B_UNMAPPED;
922 bp->b_npages = count;
923 for (i = 0; i < count; i++)
924 bp->b_pages[i] = m[i];
926 pmap_qenter(kva, m, count);
928 /* build a minimal buffer header */
929 bp->b_iocmd = BIO_READ;
930 bp->b_iodone = bdone;
931 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
932 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
933 bp->b_rcred = crhold(curthread->td_ucred);
934 bp->b_wcred = crhold(curthread->td_ucred);
935 bp->b_blkno = firstaddr;
939 bp->b_bufsize = size;
940 bp->b_runningbufspace = bp->b_bufsize;
941 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
943 PCPU_INC(cnt.v_vnodein);
944 PCPU_ADD(cnt.v_vnodepgsin, count);
947 bp->b_iooffset = dbtob(bp->b_blkno);
950 bwait(bp, PVM, "vnread");
952 if ((bp->b_ioflags & BIO_ERROR) != 0)
955 if (error == 0 && size != count * PAGE_SIZE) {
956 if ((bp->b_flags & B_UNMAPPED) != 0) {
957 bp->b_flags &= ~B_UNMAPPED;
958 pmap_qenter(kva, m, count);
960 bzero((caddr_t)kva + size, PAGE_SIZE * count - size);
962 if ((bp->b_flags & B_UNMAPPED) == 0)
963 pmap_qremove(kva, count);
964 if (mp != NULL && (mp->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0) {
965 bp->b_data = (caddr_t)kva;
966 bp->b_kvabase = (caddr_t)kva;
967 bp->b_flags &= ~B_UNMAPPED;
968 for (i = 0; i < count; i++)
969 bp->b_pages[i] = NULL;
973 * free the buffer header back to the swap buffer pool
977 relpbuf(bp, &vnode_pbuf_freecnt);
979 VM_OBJECT_WLOCK(object);
980 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
983 nextoff = tfoff + PAGE_SIZE;
986 if (nextoff <= object->un_pager.vnp.vnp_size) {
988 * Read filled up entire page.
990 mt->valid = VM_PAGE_BITS_ALL;
991 KASSERT(mt->dirty == 0,
992 ("vnode_pager_generic_getpages: page %p is dirty",
994 KASSERT(!pmap_page_is_mapped(mt),
995 ("vnode_pager_generic_getpages: page %p is mapped",
999 * Read did not fill up entire page.
1001 * Currently we do not set the entire page valid,
1002 * we just try to clear the piece that we couldn't
1005 vm_page_set_valid_range(mt, 0,
1006 object->un_pager.vnp.vnp_size - tfoff);
1007 KASSERT((mt->dirty & vm_page_bits(0,
1008 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1009 ("vnode_pager_generic_getpages: page %p is dirty",
1014 vm_page_readahead_finish(mt);
1016 VM_OBJECT_WUNLOCK(object);
1018 printf("vnode_pager_getpages: I/O read error\n");
1020 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
1024 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1025 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1026 * vnode_pager_generic_putpages() to implement the previous behaviour.
1028 * All other FS's should use the bypass to get to the local media
1029 * backing vp's VOP_PUTPAGES.
1032 vnode_pager_putpages(object, m, count, sync, rtvals)
1041 int bytes = count * PAGE_SIZE;
1044 * Force synchronous operation if we are extremely low on memory
1045 * to prevent a low-memory deadlock. VOP operations often need to
1046 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1047 * operation ). The swapper handles the case by limiting the amount
1048 * of asynchronous I/O, but that sort of solution doesn't scale well
1049 * for the vnode pager without a lot of work.
1051 * Also, the backing vnode's iodone routine may not wake the pageout
1052 * daemon up. This should be probably be addressed XXX.
1055 if ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_pageout_free_min)
1059 * Call device-specific putpages function
1061 vp = object->handle;
1062 VM_OBJECT_WUNLOCK(object);
1063 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
1064 KASSERT(rtval != EOPNOTSUPP,
1065 ("vnode_pager: stale FS putpages\n"));
1066 VM_OBJECT_WLOCK(object);
1071 * This is now called from local media FS's to operate against their
1072 * own vnodes if they fail to implement VOP_PUTPAGES.
1074 * This is typically called indirectly via the pageout daemon and
1075 * clustering has already typically occured, so in general we ask the
1076 * underlying filesystem to write the data out asynchronously rather
1080 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1081 int flags, int *rtvals)
1088 int maxsize, ncount;
1089 vm_ooffset_t poffset;
1095 static struct timeval lastfail;
1098 object = vp->v_object;
1099 count = bytecount / PAGE_SIZE;
1101 for (i = 0; i < count; i++)
1102 rtvals[i] = VM_PAGER_ERROR;
1104 if ((int64_t)ma[0]->pindex < 0) {
1105 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1106 (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1107 rtvals[0] = VM_PAGER_BAD;
1108 return VM_PAGER_BAD;
1111 maxsize = count * PAGE_SIZE;
1114 poffset = IDX_TO_OFF(ma[0]->pindex);
1117 * If the page-aligned write is larger then the actual file we
1118 * have to invalidate pages occuring beyond the file EOF. However,
1119 * there is an edge case where a file may not be page-aligned where
1120 * the last page is partially invalid. In this case the filesystem
1121 * may not properly clear the dirty bits for the entire page (which
1122 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1123 * With the page locked we are free to fix-up the dirty bits here.
1125 * We do not under any circumstances truncate the valid bits, as
1126 * this will screw up bogus page replacement.
1128 VM_OBJECT_WLOCK(object);
1129 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1130 if (object->un_pager.vnp.vnp_size > poffset) {
1133 maxsize = object->un_pager.vnp.vnp_size - poffset;
1134 ncount = btoc(maxsize);
1135 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1137 * If the object is locked and the following
1138 * conditions hold, then the page's dirty
1139 * field cannot be concurrently changed by a
1143 KASSERT(m->busy > 0,
1144 ("vnode_pager_generic_putpages: page %p is not busy", m));
1145 KASSERT(!pmap_page_is_write_mapped(m),
1146 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1147 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1154 if (ncount < count) {
1155 for (i = ncount; i < count; i++) {
1156 rtvals[i] = VM_PAGER_BAD;
1160 VM_OBJECT_WUNLOCK(object);
1163 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
1164 * rather then a bdwrite() to prevent paging I/O from saturating
1165 * the buffer cache. Dummy-up the sequential heuristic to cause
1166 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
1167 * the system decides how to cluster.
1170 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1172 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1173 ioflags |= IO_ASYNC;
1174 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1175 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1177 aiov.iov_base = (caddr_t) 0;
1178 aiov.iov_len = maxsize;
1179 auio.uio_iov = &aiov;
1180 auio.uio_iovcnt = 1;
1181 auio.uio_offset = poffset;
1182 auio.uio_segflg = UIO_NOCOPY;
1183 auio.uio_rw = UIO_WRITE;
1184 auio.uio_resid = maxsize;
1185 auio.uio_td = (struct thread *) 0;
1186 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1187 PCPU_INC(cnt.v_vnodeout);
1188 PCPU_ADD(cnt.v_vnodepgsout, ncount);
1191 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1192 printf("vnode_pager_putpages: I/O error %d\n", error);
1194 if (auio.uio_resid) {
1195 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1196 printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1197 auio.uio_resid, (u_long)ma[0]->pindex);
1199 for (i = 0; i < ncount; i++) {
1200 rtvals[i] = VM_PAGER_OK;
1206 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1213 obj = ma[0]->object;
1214 VM_OBJECT_WLOCK(obj);
1215 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1216 if (pos < trunc_page(written)) {
1217 rtvals[i] = VM_PAGER_OK;
1218 vm_page_undirty(ma[i]);
1220 /* Partially written page. */
1221 rtvals[i] = VM_PAGER_AGAIN;
1222 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1225 VM_OBJECT_WUNLOCK(obj);
1229 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1233 vm_ooffset_t old_wm;
1235 VM_OBJECT_WLOCK(object);
1236 if (object->type != OBJT_VNODE) {
1237 VM_OBJECT_WUNLOCK(object);
1240 old_wm = object->un_pager.vnp.writemappings;
1241 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1242 vp = object->handle;
1243 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1244 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1245 VOP_ADD_WRITECOUNT(vp, 1);
1246 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1247 __func__, vp, vp->v_writecount);
1248 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1249 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1250 VOP_ADD_WRITECOUNT(vp, -1);
1251 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1252 __func__, vp, vp->v_writecount);
1254 VM_OBJECT_WUNLOCK(object);
1258 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1265 VM_OBJECT_WLOCK(object);
1268 * First, recheck the object type to account for the race when
1269 * the vnode is reclaimed.
1271 if (object->type != OBJT_VNODE) {
1272 VM_OBJECT_WUNLOCK(object);
1277 * Optimize for the case when writemappings is not going to
1281 if (object->un_pager.vnp.writemappings != inc) {
1282 object->un_pager.vnp.writemappings -= inc;
1283 VM_OBJECT_WUNLOCK(object);
1287 vp = object->handle;
1289 VM_OBJECT_WUNLOCK(object);
1291 vn_start_write(vp, &mp, V_WAIT);
1292 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1295 * Decrement the object's writemappings, by swapping the start
1296 * and end arguments for vnode_pager_update_writecount(). If
1297 * there was not a race with vnode reclaimation, then the
1298 * vnode's v_writecount is decremented.
1300 vnode_pager_update_writecount(object, end, start);
1304 vn_finished_write(mp);