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_local_getpages0(struct vnode *, vm_page_t *, int, int,
86 vop_getpages_iodone_t, void *);
87 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
88 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int,
89 vop_getpages_iodone_t, void *);
90 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
91 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
92 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
93 vm_ooffset_t, struct ucred *cred);
94 static int vnode_pager_generic_getpages_done(struct buf *);
95 static void vnode_pager_generic_getpages_done_async(struct buf *);
97 struct pagerops vnodepagerops = {
98 .pgo_alloc = vnode_pager_alloc,
99 .pgo_dealloc = vnode_pager_dealloc,
100 .pgo_getpages = vnode_pager_getpages,
101 .pgo_getpages_async = vnode_pager_getpages_async,
102 .pgo_putpages = vnode_pager_putpages,
103 .pgo_haspage = vnode_pager_haspage,
106 int vnode_pbuf_freecnt;
108 /* Create the VM system backing object for this vnode */
110 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
113 vm_ooffset_t size = isize;
116 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
119 while ((object = vp->v_object) != NULL) {
120 VM_OBJECT_WLOCK(object);
121 if (!(object->flags & OBJ_DEAD)) {
122 VM_OBJECT_WUNLOCK(object);
126 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
127 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
128 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
132 if (vn_isdisk(vp, NULL)) {
133 size = IDX_TO_OFF(INT_MAX);
135 if (VOP_GETATTR(vp, &va, td->td_ucred))
141 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
143 * Dereference the reference we just created. This assumes
144 * that the object is associated with the vp.
146 VM_OBJECT_WLOCK(object);
148 VM_OBJECT_WUNLOCK(object);
151 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
157 vnode_destroy_vobject(struct vnode *vp)
159 struct vm_object *obj;
164 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
165 VM_OBJECT_WLOCK(obj);
166 if (obj->ref_count == 0) {
168 * don't double-terminate the object
170 if ((obj->flags & OBJ_DEAD) == 0)
171 vm_object_terminate(obj);
173 VM_OBJECT_WUNLOCK(obj);
176 * Woe to the process that tries to page now :-).
178 vm_pager_deallocate(obj);
179 VM_OBJECT_WUNLOCK(obj);
186 * Allocate (or lookup) pager for a vnode.
187 * Handle is a vnode pointer.
192 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
193 vm_ooffset_t offset, struct ucred *cred)
199 * Pageout to vnode, no can do yet.
204 vp = (struct vnode *) handle;
207 * If the object is being terminated, wait for it to
211 while ((object = vp->v_object) != NULL) {
212 VM_OBJECT_WLOCK(object);
213 if ((object->flags & OBJ_DEAD) == 0)
215 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
216 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
219 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
221 if (object == NULL) {
223 * Add an object of the appropriate size
225 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
227 object->un_pager.vnp.vnp_size = size;
228 object->un_pager.vnp.writemappings = 0;
230 object->handle = handle;
232 if (vp->v_object != NULL) {
234 * Object has been created while we were sleeping
237 vm_object_destroy(object);
240 vp->v_object = object;
244 VM_OBJECT_WUNLOCK(object);
251 * The object must be locked.
254 vnode_pager_dealloc(vm_object_t object)
261 panic("vnode_pager_dealloc: pager already dealloced");
263 VM_OBJECT_ASSERT_WLOCKED(object);
264 vm_object_pip_wait(object, "vnpdea");
265 refs = object->ref_count;
267 object->handle = NULL;
268 object->type = OBJT_DEAD;
269 if (object->flags & OBJ_DISCONNECTWNT) {
270 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
273 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
274 if (object->un_pager.vnp.writemappings > 0) {
275 object->un_pager.vnp.writemappings = 0;
276 VOP_ADD_WRITECOUNT(vp, -1);
277 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
278 __func__, vp, vp->v_writecount);
282 VM_OBJECT_WUNLOCK(object);
285 VM_OBJECT_WLOCK(object);
289 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
292 struct vnode *vp = object->handle;
298 int pagesperblock, blocksperpage;
300 VM_OBJECT_ASSERT_WLOCKED(object);
302 * If no vp or vp is doomed or marked transparent to VM, we do not
305 if (vp == NULL || vp->v_iflag & VI_DOOMED)
308 * If the offset is beyond end of file we do
311 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
314 bsize = vp->v_mount->mnt_stat.f_iosize;
315 pagesperblock = bsize / PAGE_SIZE;
317 if (pagesperblock > 0) {
318 reqblock = pindex / pagesperblock;
320 blocksperpage = (PAGE_SIZE / bsize);
321 reqblock = pindex * blocksperpage;
323 VM_OBJECT_WUNLOCK(object);
324 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
325 VM_OBJECT_WLOCK(object);
330 if (pagesperblock > 0) {
331 poff = pindex - (reqblock * pagesperblock);
333 *before *= pagesperblock;
338 *after *= pagesperblock;
339 numafter = pagesperblock - (poff + 1);
340 if (IDX_TO_OFF(pindex + numafter) >
341 object->un_pager.vnp.vnp_size) {
343 OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
350 *before /= blocksperpage;
354 *after /= blocksperpage;
361 * Lets the VM system know about a change in size for a file.
362 * We adjust our own internal size and flush any cached pages in
363 * the associated object that are affected by the size change.
365 * Note: this routine may be invoked as a result of a pager put
366 * operation (possibly at object termination time), so we must be careful.
369 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
373 vm_pindex_t nobjsize;
375 if ((object = vp->v_object) == NULL)
377 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
378 VM_OBJECT_WLOCK(object);
379 if (object->type == OBJT_DEAD) {
380 VM_OBJECT_WUNLOCK(object);
383 KASSERT(object->type == OBJT_VNODE,
384 ("not vnode-backed object %p", object));
385 if (nsize == object->un_pager.vnp.vnp_size) {
387 * Hasn't changed size
389 VM_OBJECT_WUNLOCK(object);
392 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
393 if (nsize < object->un_pager.vnp.vnp_size) {
395 * File has shrunk. Toss any cached pages beyond the new EOF.
397 if (nobjsize < object->size)
398 vm_object_page_remove(object, nobjsize, object->size,
401 * this gets rid of garbage at the end of a page that is now
402 * only partially backed by the vnode.
404 * XXX for some reason (I don't know yet), if we take a
405 * completely invalid page and mark it partially valid
406 * it can screw up NFS reads, so we don't allow the case.
408 if ((nsize & PAGE_MASK) &&
409 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
411 int base = (int)nsize & PAGE_MASK;
412 int size = PAGE_SIZE - base;
415 * Clear out partial-page garbage in case
416 * the page has been mapped.
418 pmap_zero_page_area(m, base, size);
421 * Update the valid bits to reflect the blocks that
422 * have been zeroed. Some of these valid bits may
423 * have already been set.
425 vm_page_set_valid_range(m, base, size);
428 * Round "base" to the next block boundary so that the
429 * dirty bit for a partially zeroed block is not
432 base = roundup2(base, DEV_BSIZE);
435 * Clear out partial-page dirty bits.
437 * note that we do not clear out the valid
438 * bits. This would prevent bogus_page
439 * replacement from working properly.
441 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
442 } else if ((nsize & PAGE_MASK) &&
443 vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
444 vm_page_cache_free(object, OFF_TO_IDX(nsize),
448 object->un_pager.vnp.vnp_size = nsize;
449 object->size = nobjsize;
450 VM_OBJECT_WUNLOCK(object);
454 * calculate the linear (byte) disk address of specified virtual
458 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
469 if (vp->v_iflag & VI_DOOMED)
472 bsize = vp->v_mount->mnt_stat.f_iosize;
473 vblock = address / bsize;
474 voffset = address % bsize;
476 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
478 if (*rtaddress != -1)
479 *rtaddress += voffset / DEV_BSIZE;
482 *run *= bsize/PAGE_SIZE;
483 *run -= voffset/PAGE_SIZE;
491 * small block filesystem vnode pager input
494 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
507 if (vp->v_iflag & VI_DOOMED)
510 bsize = vp->v_mount->mnt_stat.f_iosize;
512 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
514 sf = sf_buf_alloc(m, 0);
516 for (i = 0; i < PAGE_SIZE / bsize; i++) {
517 vm_ooffset_t address;
519 bits = vm_page_bits(i * bsize, bsize);
523 address = IDX_TO_OFF(m->pindex) + i * bsize;
524 if (address >= object->un_pager.vnp.vnp_size) {
527 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
531 if (fileaddr != -1) {
532 bp = getpbuf(&vnode_pbuf_freecnt);
534 /* build a minimal buffer header */
535 bp->b_iocmd = BIO_READ;
536 bp->b_iodone = bdone;
537 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
538 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
539 bp->b_rcred = crhold(curthread->td_ucred);
540 bp->b_wcred = crhold(curthread->td_ucred);
541 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
542 bp->b_blkno = fileaddr;
545 bp->b_bcount = bsize;
546 bp->b_bufsize = bsize;
547 bp->b_runningbufspace = bp->b_bufsize;
548 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
551 bp->b_iooffset = dbtob(bp->b_blkno);
554 bwait(bp, PVM, "vnsrd");
556 if ((bp->b_ioflags & BIO_ERROR) != 0)
560 * free the buffer header back to the swap buffer pool
564 relpbuf(bp, &vnode_pbuf_freecnt);
568 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
569 KASSERT((m->dirty & bits) == 0,
570 ("vnode_pager_input_smlfs: page %p is dirty", m));
571 VM_OBJECT_WLOCK(object);
573 VM_OBJECT_WUNLOCK(object);
577 return VM_PAGER_ERROR;
583 * old style vnode pager input routine
586 vnode_pager_input_old(vm_object_t object, vm_page_t m)
595 VM_OBJECT_ASSERT_WLOCKED(object);
599 * Return failure if beyond current EOF
601 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
605 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
606 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
608 VM_OBJECT_WUNLOCK(object);
611 * Allocate a kernel virtual address and initialize so that
612 * we can use VOP_READ/WRITE routines.
614 sf = sf_buf_alloc(m, 0);
616 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
618 auio.uio_iov = &aiov;
620 auio.uio_offset = IDX_TO_OFF(m->pindex);
621 auio.uio_segflg = UIO_SYSSPACE;
622 auio.uio_rw = UIO_READ;
623 auio.uio_resid = size;
624 auio.uio_td = curthread;
626 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
628 int count = size - auio.uio_resid;
632 else if (count != PAGE_SIZE)
633 bzero((caddr_t)sf_buf_kva(sf) + count,
638 VM_OBJECT_WLOCK(object);
640 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
642 m->valid = VM_PAGE_BITS_ALL;
643 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
647 * generic vnode pager input routine
651 * Local media VFS's that do not implement their own VOP_GETPAGES
652 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
653 * to implement the previous behaviour.
655 * All other FS's should use the bypass to get to the local media
656 * backing vp's VOP_GETPAGES.
659 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
663 int bytes = count * PAGE_SIZE;
666 VM_OBJECT_WUNLOCK(object);
667 rtval = VOP_GETPAGES(vp, m, bytes, reqpage);
668 KASSERT(rtval != EOPNOTSUPP,
669 ("vnode_pager: FS getpages not implemented\n"));
670 VM_OBJECT_WLOCK(object);
675 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
676 int reqpage, vop_getpages_iodone_t iodone, void *arg)
682 VM_OBJECT_WUNLOCK(object);
683 rtval = VOP_GETPAGES_ASYNC(vp, m, count * PAGE_SIZE, reqpage, 0,
685 KASSERT(rtval != EOPNOTSUPP,
686 ("vnode_pager: FS getpages_async not implemented\n"));
687 VM_OBJECT_WLOCK(object);
692 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
693 * local filesystems, where partially valid pages can only occur at
697 vnode_pager_local_getpages(struct vop_getpages_args *ap)
700 return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count,
701 ap->a_reqpage, NULL, NULL));
705 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
708 return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count,
709 ap->a_reqpage, ap->a_iodone, ap->a_arg));
713 vnode_pager_local_getpages0(struct vnode *vp, vm_page_t *m, int bytecount,
714 int reqpage, vop_getpages_iodone_t iodone, void *arg)
721 * Since the caller has busied the requested page, that page's valid
722 * field will not be changed by other threads.
724 vm_page_assert_xbusied(mreq);
727 * The requested page has valid blocks. Invalid part can only
728 * exist at the end of file, and the page is made fully valid
729 * by zeroing in vm_pager_getpages(). Free non-requested
730 * pages, since no i/o is done to read its content.
732 if (mreq->valid != 0) {
733 vm_pager_free_nonreq(mreq->object, m, reqpage,
734 round_page(bytecount) / PAGE_SIZE);
736 iodone(arg, m, reqpage, 0);
737 return (VM_PAGER_OK);
740 return (vnode_pager_generic_getpages(vp, m, bytecount, reqpage,
745 * This is now called from local media FS's to operate against their
746 * own vnodes if they fail to implement VOP_GETPAGES.
749 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount,
750 int reqpage, vop_getpages_iodone_t iodone, void *arg)
754 int i, j, size, bsize, first;
755 daddr_t firstaddr, reqblock;
763 object = vp->v_object;
764 count = bytecount / PAGE_SIZE;
766 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
767 ("vnode_pager_generic_getpages does not support devices"));
768 if (vp->v_iflag & VI_DOOMED)
771 bsize = vp->v_mount->mnt_stat.f_iosize;
772 foff = IDX_TO_OFF(m[reqpage]->pindex);
775 * Get the underlying device blocks for the file with VOP_BMAP().
776 * If the file system doesn't support VOP_BMAP, use old way of
777 * getting pages via VOP_READ.
779 error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
780 if (error == EOPNOTSUPP) {
781 VM_OBJECT_WLOCK(object);
783 for (i = 0; i < count; i++)
787 vm_page_unlock(m[i]);
789 PCPU_INC(cnt.v_vnodein);
790 PCPU_INC(cnt.v_vnodepgsin);
791 error = vnode_pager_input_old(object, m[reqpage]);
792 VM_OBJECT_WUNLOCK(object);
794 } else if (error != 0) {
795 vm_pager_free_nonreq(object, m, reqpage, count);
796 return (VM_PAGER_ERROR);
799 * if the blocksize is smaller than a page size, then use
800 * special small filesystem code. NFS sometimes has a small
801 * blocksize, but it can handle large reads itself.
803 } else if ((PAGE_SIZE / bsize) > 1 &&
804 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
805 vm_pager_free_nonreq(object, m, reqpage, count);
806 PCPU_INC(cnt.v_vnodein);
807 PCPU_INC(cnt.v_vnodepgsin);
808 return vnode_pager_input_smlfs(object, m[reqpage]);
812 * Since the caller has busied the requested page, that page's valid
813 * field will not be changed by other threads.
815 vm_page_assert_xbusied(m[reqpage]);
818 * If we have a completely valid page available to us, we can
819 * clean up and return. Otherwise we have to re-read the
822 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
823 vm_pager_free_nonreq(object, m, reqpage, count);
824 return (VM_PAGER_OK);
825 } else if (reqblock == -1) {
826 pmap_zero_page(m[reqpage]);
827 KASSERT(m[reqpage]->dirty == 0,
828 ("vnode_pager_generic_getpages: page %p is dirty", m));
829 VM_OBJECT_WLOCK(object);
830 m[reqpage]->valid = VM_PAGE_BITS_ALL;
831 for (i = 0; i < count; i++)
835 vm_page_unlock(m[i]);
837 VM_OBJECT_WUNLOCK(object);
838 return (VM_PAGER_OK);
839 } else if (m[reqpage]->valid != 0) {
840 VM_OBJECT_WLOCK(object);
841 m[reqpage]->valid = 0;
842 VM_OBJECT_WUNLOCK(object);
846 * here on direct device I/O
851 * calculate the run that includes the required page
853 for (first = 0, i = 0; i < count; i = runend) {
854 if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
856 VM_OBJECT_WLOCK(object);
857 for (; i < count; i++)
861 vm_page_unlock(m[i]);
863 VM_OBJECT_WUNLOCK(object);
864 return (VM_PAGER_ERROR);
866 if (firstaddr == -1) {
867 VM_OBJECT_WLOCK(object);
868 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
869 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
870 (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
873 (object->un_pager.vnp.vnp_size >> 32),
874 (uintmax_t)object->un_pager.vnp.vnp_size);
878 vm_page_unlock(m[i]);
879 VM_OBJECT_WUNLOCK(object);
885 if (runend <= reqpage) {
886 VM_OBJECT_WLOCK(object);
887 for (j = i; j < runend; j++) {
890 vm_page_unlock(m[j]);
892 VM_OBJECT_WUNLOCK(object);
894 if (runpg < (count - first)) {
895 VM_OBJECT_WLOCK(object);
896 for (i = first + runpg; i < count; i++) {
899 vm_page_unlock(m[i]);
901 VM_OBJECT_WUNLOCK(object);
902 count = first + runpg;
910 * the first and last page have been calculated now, move input pages
911 * to be zero based...
920 * calculate the file virtual address for the transfer
922 foff = IDX_TO_OFF(m[0]->pindex);
925 * calculate the size of the transfer
927 size = count * PAGE_SIZE;
928 KASSERT(count > 0, ("zero count"));
929 if ((foff + size) > object->un_pager.vnp.vnp_size)
930 size = object->un_pager.vnp.vnp_size - foff;
931 KASSERT(size > 0, ("zero size"));
934 * round up physical size for real devices.
937 int secmask = bo->bo_bsize - 1;
938 KASSERT(secmask < PAGE_SIZE && secmask > 0,
939 ("vnode_pager_generic_getpages: sector size %d too large",
941 size = (size + secmask) & ~secmask;
944 bp = getpbuf(&vnode_pbuf_freecnt);
945 bp->b_kvaalloc = bp->b_data;
948 * and map the pages to be read into the kva, if the filesystem
949 * requires mapped buffers.
951 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
952 unmapped_buf_allowed) {
953 bp->b_data = unmapped_buf;
954 bp->b_kvabase = unmapped_buf;
956 bp->b_flags |= B_UNMAPPED;
958 pmap_qenter((vm_offset_t)bp->b_kvaalloc, m, count);
960 /* build a minimal buffer header */
961 bp->b_iocmd = BIO_READ;
962 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
963 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
964 bp->b_rcred = crhold(curthread->td_ucred);
965 bp->b_wcred = crhold(curthread->td_ucred);
966 bp->b_blkno = firstaddr;
970 bp->b_bufsize = size;
971 bp->b_runningbufspace = bp->b_bufsize;
972 for (i = 0; i < count; i++)
973 bp->b_pages[i] = m[i];
974 bp->b_npages = count;
975 bp->b_pager.pg_reqpage = reqpage;
976 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
978 PCPU_INC(cnt.v_vnodein);
979 PCPU_ADD(cnt.v_vnodepgsin, count);
982 bp->b_iooffset = dbtob(bp->b_blkno);
984 if (iodone != NULL) { /* async */
985 bp->b_pager.pg_iodone = iodone;
987 bp->b_iodone = vnode_pager_generic_getpages_done_async;
988 bp->b_flags |= B_ASYNC;
993 bp->b_iodone = bdone;
995 bwait(bp, PVM, "vnread");
996 error = vnode_pager_generic_getpages_done(bp);
997 for (i = 0; i < bp->b_npages; i++)
998 bp->b_pages[i] = NULL;
1001 relpbuf(bp, &vnode_pbuf_freecnt);
1004 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1008 vnode_pager_generic_getpages_done_async(struct buf *bp)
1012 error = vnode_pager_generic_getpages_done(bp);
1013 bp->b_pager.pg_iodone(bp->b_caller1, bp->b_pages,
1014 bp->b_pager.pg_reqpage, error);
1015 for (int i = 0; i < bp->b_npages; i++)
1016 bp->b_pages[i] = NULL;
1019 relpbuf(bp, &vnode_pbuf_freecnt);
1023 vnode_pager_generic_getpages_done(struct buf *bp)
1026 off_t tfoff, nextoff;
1029 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1030 object = bp->b_vp->v_object;
1032 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1033 if ((bp->b_flags & B_UNMAPPED) != 0) {
1034 bp->b_flags &= ~B_UNMAPPED;
1035 pmap_qenter((vm_offset_t)bp->b_kvaalloc, bp->b_pages,
1038 bzero(bp->b_kvaalloc + bp->b_bcount,
1039 PAGE_SIZE * bp->b_npages - bp->b_bcount);
1041 if ((bp->b_flags & B_UNMAPPED) == 0)
1042 pmap_qremove((vm_offset_t)bp->b_kvaalloc, bp->b_npages);
1043 if ((bp->b_vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0) {
1044 bp->b_data = bp->b_kvaalloc;
1045 bp->b_kvabase = bp->b_kvaalloc;
1046 bp->b_flags &= ~B_UNMAPPED;
1049 VM_OBJECT_WLOCK(object);
1050 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1051 i < bp->b_npages; i++, tfoff = nextoff) {
1054 nextoff = tfoff + PAGE_SIZE;
1055 mt = bp->b_pages[i];
1057 if (nextoff <= object->un_pager.vnp.vnp_size) {
1059 * Read filled up entire page.
1061 mt->valid = VM_PAGE_BITS_ALL;
1062 KASSERT(mt->dirty == 0,
1063 ("%s: page %p is dirty", __func__, mt));
1064 KASSERT(!pmap_page_is_mapped(mt),
1065 ("%s: page %p is mapped", __func__, mt));
1068 * Read did not fill up entire page.
1070 * Currently we do not set the entire page valid,
1071 * we just try to clear the piece that we couldn't
1074 vm_page_set_valid_range(mt, 0,
1075 object->un_pager.vnp.vnp_size - tfoff);
1076 KASSERT((mt->dirty & vm_page_bits(0,
1077 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1078 ("%s: page %p is dirty", __func__, mt));
1081 if (i != bp->b_pager.pg_reqpage)
1082 vm_page_readahead_finish(mt);
1084 VM_OBJECT_WUNLOCK(object);
1086 printf("%s: I/O read error %d\n", __func__, error);
1092 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1093 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1094 * vnode_pager_generic_putpages() to implement the previous behaviour.
1096 * All other FS's should use the bypass to get to the local media
1097 * backing vp's VOP_PUTPAGES.
1100 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1101 int flags, int *rtvals)
1105 int bytes = count * PAGE_SIZE;
1108 * Force synchronous operation if we are extremely low on memory
1109 * to prevent a low-memory deadlock. VOP operations often need to
1110 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1111 * operation ). The swapper handles the case by limiting the amount
1112 * of asynchronous I/O, but that sort of solution doesn't scale well
1113 * for the vnode pager without a lot of work.
1115 * Also, the backing vnode's iodone routine may not wake the pageout
1116 * daemon up. This should be probably be addressed XXX.
1119 if (vm_cnt.v_free_count + vm_cnt.v_cache_count <
1120 vm_cnt.v_pageout_free_min)
1121 flags |= VM_PAGER_PUT_SYNC;
1124 * Call device-specific putpages function
1126 vp = object->handle;
1127 VM_OBJECT_WUNLOCK(object);
1128 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1129 KASSERT(rtval != EOPNOTSUPP,
1130 ("vnode_pager: stale FS putpages\n"));
1131 VM_OBJECT_WLOCK(object);
1136 * This is now called from local media FS's to operate against their
1137 * own vnodes if they fail to implement VOP_PUTPAGES.
1139 * This is typically called indirectly via the pageout daemon and
1140 * clustering has already typically occured, so in general we ask the
1141 * underlying filesystem to write the data out asynchronously rather
1145 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1146 int flags, int *rtvals)
1153 int maxsize, ncount;
1154 vm_ooffset_t poffset;
1160 static struct timeval lastfail;
1163 object = vp->v_object;
1164 count = bytecount / PAGE_SIZE;
1166 for (i = 0; i < count; i++)
1167 rtvals[i] = VM_PAGER_ERROR;
1169 if ((int64_t)ma[0]->pindex < 0) {
1170 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1171 (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1172 rtvals[0] = VM_PAGER_BAD;
1173 return VM_PAGER_BAD;
1176 maxsize = count * PAGE_SIZE;
1179 poffset = IDX_TO_OFF(ma[0]->pindex);
1182 * If the page-aligned write is larger then the actual file we
1183 * have to invalidate pages occuring beyond the file EOF. However,
1184 * there is an edge case where a file may not be page-aligned where
1185 * the last page is partially invalid. In this case the filesystem
1186 * may not properly clear the dirty bits for the entire page (which
1187 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1188 * With the page locked we are free to fix-up the dirty bits here.
1190 * We do not under any circumstances truncate the valid bits, as
1191 * this will screw up bogus page replacement.
1193 VM_OBJECT_WLOCK(object);
1194 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1195 if (object->un_pager.vnp.vnp_size > poffset) {
1198 maxsize = object->un_pager.vnp.vnp_size - poffset;
1199 ncount = btoc(maxsize);
1200 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1202 * If the object is locked and the following
1203 * conditions hold, then the page's dirty
1204 * field cannot be concurrently changed by a
1208 vm_page_assert_sbusied(m);
1209 KASSERT(!pmap_page_is_write_mapped(m),
1210 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1211 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1218 if (ncount < count) {
1219 for (i = ncount; i < count; i++) {
1220 rtvals[i] = VM_PAGER_BAD;
1224 VM_OBJECT_WUNLOCK(object);
1227 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
1228 * rather then a bdwrite() to prevent paging I/O from saturating
1229 * the buffer cache. Dummy-up the sequential heuristic to cause
1230 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
1231 * the system decides how to cluster.
1234 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1236 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1237 ioflags |= IO_ASYNC;
1238 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1239 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1241 aiov.iov_base = (caddr_t) 0;
1242 aiov.iov_len = maxsize;
1243 auio.uio_iov = &aiov;
1244 auio.uio_iovcnt = 1;
1245 auio.uio_offset = poffset;
1246 auio.uio_segflg = UIO_NOCOPY;
1247 auio.uio_rw = UIO_WRITE;
1248 auio.uio_resid = maxsize;
1249 auio.uio_td = (struct thread *) 0;
1250 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1251 PCPU_INC(cnt.v_vnodeout);
1252 PCPU_ADD(cnt.v_vnodepgsout, ncount);
1255 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1256 printf("vnode_pager_putpages: I/O error %d\n", error);
1258 if (auio.uio_resid) {
1259 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1260 printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1261 auio.uio_resid, (u_long)ma[0]->pindex);
1263 for (i = 0; i < ncount; i++) {
1264 rtvals[i] = VM_PAGER_OK;
1270 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1277 obj = ma[0]->object;
1278 VM_OBJECT_WLOCK(obj);
1279 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1280 if (pos < trunc_page(written)) {
1281 rtvals[i] = VM_PAGER_OK;
1282 vm_page_undirty(ma[i]);
1284 /* Partially written page. */
1285 rtvals[i] = VM_PAGER_AGAIN;
1286 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1289 VM_OBJECT_WUNLOCK(obj);
1293 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1297 vm_ooffset_t old_wm;
1299 VM_OBJECT_WLOCK(object);
1300 if (object->type != OBJT_VNODE) {
1301 VM_OBJECT_WUNLOCK(object);
1304 old_wm = object->un_pager.vnp.writemappings;
1305 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1306 vp = object->handle;
1307 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1308 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1309 VOP_ADD_WRITECOUNT(vp, 1);
1310 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1311 __func__, vp, vp->v_writecount);
1312 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1313 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1314 VOP_ADD_WRITECOUNT(vp, -1);
1315 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1316 __func__, vp, vp->v_writecount);
1318 VM_OBJECT_WUNLOCK(object);
1322 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1329 VM_OBJECT_WLOCK(object);
1332 * First, recheck the object type to account for the race when
1333 * the vnode is reclaimed.
1335 if (object->type != OBJT_VNODE) {
1336 VM_OBJECT_WUNLOCK(object);
1341 * Optimize for the case when writemappings is not going to
1345 if (object->un_pager.vnp.writemappings != inc) {
1346 object->un_pager.vnp.writemappings -= inc;
1347 VM_OBJECT_WUNLOCK(object);
1351 vp = object->handle;
1353 VM_OBJECT_WUNLOCK(object);
1355 vn_start_write(vp, &mp, V_WAIT);
1356 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1359 * Decrement the object's writemappings, by swapping the start
1360 * and end arguments for vnode_pager_update_writecount(). If
1361 * there was not a race with vnode reclaimation, then the
1362 * vnode's v_writecount is decremented.
1364 vnode_pager_update_writecount(object, end, start);
1368 vn_finished_write(mp);