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$");
58 #include <sys/param.h>
59 #include <sys/systm.h>
61 #include <sys/vnode.h>
62 #include <sys/mount.h>
65 #include <sys/vmmeter.h>
66 #include <sys/limits.h>
68 #include <sys/rwlock.h>
69 #include <sys/sf_buf.h>
71 #include <machine/atomic.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_map.h>
79 #include <vm/vnode_pager.h>
80 #include <vm/vm_extern.h>
82 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
83 daddr_t *rtaddress, int *run);
84 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
85 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
86 static void vnode_pager_dealloc(vm_object_t);
87 static int vnode_pager_local_getpages0(struct vnode *, vm_page_t *, int, int,
88 vop_getpages_iodone_t, void *);
89 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int);
90 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int,
91 vop_getpages_iodone_t, void *);
92 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
93 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
94 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
95 vm_ooffset_t, struct ucred *cred);
96 static int vnode_pager_generic_getpages_done(struct buf *);
97 static void vnode_pager_generic_getpages_done_async(struct buf *);
99 struct pagerops vnodepagerops = {
100 .pgo_alloc = vnode_pager_alloc,
101 .pgo_dealloc = vnode_pager_dealloc,
102 .pgo_getpages = vnode_pager_getpages,
103 .pgo_getpages_async = vnode_pager_getpages_async,
104 .pgo_putpages = vnode_pager_putpages,
105 .pgo_haspage = vnode_pager_haspage,
108 int vnode_pbuf_freecnt;
109 int vnode_async_pbuf_freecnt;
111 /* Create the VM system backing object for this vnode */
113 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
116 vm_ooffset_t size = isize;
119 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
122 while ((object = vp->v_object) != NULL) {
123 VM_OBJECT_WLOCK(object);
124 if (!(object->flags & OBJ_DEAD)) {
125 VM_OBJECT_WUNLOCK(object);
129 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
130 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
131 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
135 if (vn_isdisk(vp, NULL)) {
136 size = IDX_TO_OFF(INT_MAX);
138 if (VOP_GETATTR(vp, &va, td->td_ucred))
144 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
146 * Dereference the reference we just created. This assumes
147 * that the object is associated with the vp.
149 VM_OBJECT_WLOCK(object);
151 VM_OBJECT_WUNLOCK(object);
154 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
160 vnode_destroy_vobject(struct vnode *vp)
162 struct vm_object *obj;
167 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
168 VM_OBJECT_WLOCK(obj);
169 if (obj->ref_count == 0) {
171 * don't double-terminate the object
173 if ((obj->flags & OBJ_DEAD) == 0)
174 vm_object_terminate(obj);
176 VM_OBJECT_WUNLOCK(obj);
179 * Woe to the process that tries to page now :-).
181 vm_pager_deallocate(obj);
182 VM_OBJECT_WUNLOCK(obj);
189 * Allocate (or lookup) pager for a vnode.
190 * Handle is a vnode pointer.
195 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
196 vm_ooffset_t offset, struct ucred *cred)
202 * Pageout to vnode, no can do yet.
207 vp = (struct vnode *) handle;
210 * If the object is being terminated, wait for it to
214 while ((object = vp->v_object) != NULL) {
215 VM_OBJECT_WLOCK(object);
216 if ((object->flags & OBJ_DEAD) == 0)
218 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
219 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
222 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
224 if (object == NULL) {
226 * Add an object of the appropriate size
228 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
230 object->un_pager.vnp.vnp_size = size;
231 object->un_pager.vnp.writemappings = 0;
233 object->handle = handle;
235 if (vp->v_object != NULL) {
237 * Object has been created while we were sleeping
240 vm_object_destroy(object);
243 vp->v_object = object;
247 #if VM_NRESERVLEVEL > 0
248 vm_object_color(object, 0);
250 VM_OBJECT_WUNLOCK(object);
257 * The object must be locked.
260 vnode_pager_dealloc(vm_object_t object)
267 panic("vnode_pager_dealloc: pager already dealloced");
269 VM_OBJECT_ASSERT_WLOCKED(object);
270 vm_object_pip_wait(object, "vnpdea");
271 refs = object->ref_count;
273 object->handle = NULL;
274 object->type = OBJT_DEAD;
275 if (object->flags & OBJ_DISCONNECTWNT) {
276 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
279 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
280 if (object->un_pager.vnp.writemappings > 0) {
281 object->un_pager.vnp.writemappings = 0;
282 VOP_ADD_WRITECOUNT(vp, -1);
283 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
284 __func__, vp, vp->v_writecount);
288 VM_OBJECT_WUNLOCK(object);
291 VM_OBJECT_WLOCK(object);
295 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
298 struct vnode *vp = object->handle;
304 int pagesperblock, blocksperpage;
306 VM_OBJECT_ASSERT_WLOCKED(object);
308 * If no vp or vp is doomed or marked transparent to VM, we do not
311 if (vp == NULL || vp->v_iflag & VI_DOOMED)
314 * If the offset is beyond end of file we do
317 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
320 bsize = vp->v_mount->mnt_stat.f_iosize;
321 pagesperblock = bsize / PAGE_SIZE;
323 if (pagesperblock > 0) {
324 reqblock = pindex / pagesperblock;
326 blocksperpage = (PAGE_SIZE / bsize);
327 reqblock = pindex * blocksperpage;
329 VM_OBJECT_WUNLOCK(object);
330 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
331 VM_OBJECT_WLOCK(object);
336 if (pagesperblock > 0) {
337 poff = pindex - (reqblock * pagesperblock);
339 *before *= pagesperblock;
344 *after *= pagesperblock;
345 numafter = pagesperblock - (poff + 1);
346 if (IDX_TO_OFF(pindex + numafter) >
347 object->un_pager.vnp.vnp_size) {
349 OFF_TO_IDX(object->un_pager.vnp.vnp_size) -
356 *before /= blocksperpage;
360 *after /= blocksperpage;
367 * Lets the VM system know about a change in size for a file.
368 * We adjust our own internal size and flush any cached pages in
369 * the associated object that are affected by the size change.
371 * Note: this routine may be invoked as a result of a pager put
372 * operation (possibly at object termination time), so we must be careful.
375 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
379 vm_pindex_t nobjsize;
381 if ((object = vp->v_object) == NULL)
383 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
384 VM_OBJECT_WLOCK(object);
385 if (object->type == OBJT_DEAD) {
386 VM_OBJECT_WUNLOCK(object);
389 KASSERT(object->type == OBJT_VNODE,
390 ("not vnode-backed object %p", object));
391 if (nsize == object->un_pager.vnp.vnp_size) {
393 * Hasn't changed size
395 VM_OBJECT_WUNLOCK(object);
398 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
399 if (nsize < object->un_pager.vnp.vnp_size) {
401 * File has shrunk. Toss any cached pages beyond the new EOF.
403 if (nobjsize < object->size)
404 vm_object_page_remove(object, nobjsize, object->size,
407 * this gets rid of garbage at the end of a page that is now
408 * only partially backed by the vnode.
410 * XXX for some reason (I don't know yet), if we take a
411 * completely invalid page and mark it partially valid
412 * it can screw up NFS reads, so we don't allow the case.
414 if ((nsize & PAGE_MASK) &&
415 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
417 int base = (int)nsize & PAGE_MASK;
418 int size = PAGE_SIZE - base;
421 * Clear out partial-page garbage in case
422 * the page has been mapped.
424 pmap_zero_page_area(m, base, size);
427 * Update the valid bits to reflect the blocks that
428 * have been zeroed. Some of these valid bits may
429 * have already been set.
431 vm_page_set_valid_range(m, base, size);
434 * Round "base" to the next block boundary so that the
435 * dirty bit for a partially zeroed block is not
438 base = roundup2(base, DEV_BSIZE);
441 * Clear out partial-page dirty bits.
443 * note that we do not clear out the valid
444 * bits. This would prevent bogus_page
445 * replacement from working properly.
447 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
448 } else if ((nsize & PAGE_MASK) &&
449 vm_page_is_cached(object, OFF_TO_IDX(nsize))) {
450 vm_page_cache_free(object, OFF_TO_IDX(nsize),
454 object->un_pager.vnp.vnp_size = nsize;
455 object->size = nobjsize;
456 VM_OBJECT_WUNLOCK(object);
460 * calculate the linear (byte) disk address of specified virtual
464 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
475 if (vp->v_iflag & VI_DOOMED)
478 bsize = vp->v_mount->mnt_stat.f_iosize;
479 vblock = address / bsize;
480 voffset = address % bsize;
482 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
484 if (*rtaddress != -1)
485 *rtaddress += voffset / DEV_BSIZE;
488 *run *= bsize/PAGE_SIZE;
489 *run -= voffset/PAGE_SIZE;
497 * small block filesystem vnode pager input
500 vnode_pager_input_smlfs(vm_object_t object, vm_page_t 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(vm_object_t object, vm_page_t m)
601 VM_OBJECT_ASSERT_WLOCKED(object);
605 * Return failure if beyond current EOF
607 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
611 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
612 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
614 VM_OBJECT_WUNLOCK(object);
617 * Allocate a kernel virtual address and initialize so that
618 * we can use VOP_READ/WRITE routines.
620 sf = sf_buf_alloc(m, 0);
622 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
624 auio.uio_iov = &aiov;
626 auio.uio_offset = IDX_TO_OFF(m->pindex);
627 auio.uio_segflg = UIO_SYSSPACE;
628 auio.uio_rw = UIO_READ;
629 auio.uio_resid = size;
630 auio.uio_td = curthread;
632 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
634 int count = size - auio.uio_resid;
638 else if (count != PAGE_SIZE)
639 bzero((caddr_t)sf_buf_kva(sf) + count,
644 VM_OBJECT_WLOCK(object);
646 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
648 m->valid = VM_PAGE_BITS_ALL;
649 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
653 * generic vnode pager input routine
657 * Local media VFS's that do not implement their own VOP_GETPAGES
658 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
659 * to implement the previous behaviour.
661 * All other FS's should use the bypass to get to the local media
662 * backing vp's VOP_GETPAGES.
665 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
669 int bytes = count * PAGE_SIZE;
672 VM_OBJECT_WUNLOCK(object);
673 rtval = VOP_GETPAGES(vp, m, bytes, reqpage);
674 KASSERT(rtval != EOPNOTSUPP,
675 ("vnode_pager: FS getpages not implemented\n"));
676 VM_OBJECT_WLOCK(object);
681 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
682 int reqpage, vop_getpages_iodone_t iodone, void *arg)
688 VM_OBJECT_WUNLOCK(object);
689 rtval = VOP_GETPAGES_ASYNC(vp, m, count * PAGE_SIZE, reqpage,
691 KASSERT(rtval != EOPNOTSUPP,
692 ("vnode_pager: FS getpages_async not implemented\n"));
693 VM_OBJECT_WLOCK(object);
698 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
699 * local filesystems, where partially valid pages can only occur at
703 vnode_pager_local_getpages(struct vop_getpages_args *ap)
706 return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count,
707 ap->a_reqpage, NULL, NULL));
711 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
714 return (vnode_pager_local_getpages0(ap->a_vp, ap->a_m, ap->a_count,
715 ap->a_reqpage, ap->a_iodone, ap->a_arg));
719 vnode_pager_local_getpages0(struct vnode *vp, vm_page_t *m, int bytecount,
720 int reqpage, vop_getpages_iodone_t iodone, void *arg)
727 * Since the caller has busied the requested page, that page's valid
728 * field will not be changed by other threads.
730 vm_page_assert_xbusied(mreq);
733 * The requested page has valid blocks. Invalid part can only
734 * exist at the end of file, and the page is made fully valid
735 * by zeroing in vm_pager_get_pages(). Free non-requested
736 * pages, since no i/o is done to read its content.
738 if (mreq->valid != 0) {
739 vm_pager_free_nonreq(mreq->object, m, reqpage,
740 round_page(bytecount) / PAGE_SIZE, FALSE);
742 iodone(arg, m, reqpage, 0);
743 return (VM_PAGER_OK);
746 return (vnode_pager_generic_getpages(vp, m, bytecount, reqpage,
751 * This is now called from local media FS's to operate against their
752 * own vnodes if they fail to implement VOP_GETPAGES.
755 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount,
756 int reqpage, vop_getpages_iodone_t iodone, void *arg)
760 int i, j, size, bsize, first, *freecnt;
761 daddr_t firstaddr, reqblock;
769 object = vp->v_object;
770 count = bytecount / PAGE_SIZE;
772 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
773 ("vnode_pager_generic_getpages does not support devices"));
774 if (vp->v_iflag & VI_DOOMED)
777 bsize = vp->v_mount->mnt_stat.f_iosize;
778 foff = IDX_TO_OFF(m[reqpage]->pindex);
781 * Synchronous and asynchronous paging operations use different
782 * free pbuf counters. This is done to avoid asynchronous requests
783 * to consume all pbufs.
784 * Allocate the pbuf at the very beginning of the function, so that
785 * if we are low on certain kind of pbufs don't even proceed to BMAP,
788 freecnt = iodone != NULL ?
789 &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
790 bp = getpbuf(freecnt);
793 * Get the underlying device blocks for the file with VOP_BMAP().
794 * If the file system doesn't support VOP_BMAP, use old way of
795 * getting pages via VOP_READ.
797 error = VOP_BMAP(vp, foff / bsize, &bo, &reqblock, NULL, NULL);
798 if (error == EOPNOTSUPP) {
799 relpbuf(bp, freecnt);
800 VM_OBJECT_WLOCK(object);
801 for (i = 0; i < count; i++)
805 vm_page_unlock(m[i]);
807 PCPU_INC(cnt.v_vnodein);
808 PCPU_INC(cnt.v_vnodepgsin);
809 error = vnode_pager_input_old(object, m[reqpage]);
810 VM_OBJECT_WUNLOCK(object);
812 } else if (error != 0) {
813 relpbuf(bp, freecnt);
814 vm_pager_free_nonreq(object, m, reqpage, count, FALSE);
815 return (VM_PAGER_ERROR);
818 * if the blocksize is smaller than a page size, then use
819 * special small filesystem code. NFS sometimes has a small
820 * blocksize, but it can handle large reads itself.
822 } else if ((PAGE_SIZE / bsize) > 1 &&
823 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
824 relpbuf(bp, freecnt);
825 vm_pager_free_nonreq(object, m, reqpage, count, FALSE);
826 PCPU_INC(cnt.v_vnodein);
827 PCPU_INC(cnt.v_vnodepgsin);
828 return vnode_pager_input_smlfs(object, m[reqpage]);
832 * Since the caller has busied the requested page, that page's valid
833 * field will not be changed by other threads.
835 vm_page_assert_xbusied(m[reqpage]);
838 * If we have a completely valid page available to us, we can
839 * clean up and return. Otherwise we have to re-read the
842 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
843 relpbuf(bp, freecnt);
844 vm_pager_free_nonreq(object, m, reqpage, count, FALSE);
845 return (VM_PAGER_OK);
846 } else if (reqblock == -1) {
847 relpbuf(bp, freecnt);
848 pmap_zero_page(m[reqpage]);
849 KASSERT(m[reqpage]->dirty == 0,
850 ("vnode_pager_generic_getpages: page %p is dirty", m));
851 VM_OBJECT_WLOCK(object);
852 m[reqpage]->valid = VM_PAGE_BITS_ALL;
853 vm_pager_free_nonreq(object, m, reqpage, count, TRUE);
854 VM_OBJECT_WUNLOCK(object);
855 return (VM_PAGER_OK);
856 } else if (m[reqpage]->valid != 0) {
857 VM_OBJECT_WLOCK(object);
858 m[reqpage]->valid = 0;
859 VM_OBJECT_WUNLOCK(object);
863 * here on direct device I/O
868 * calculate the run that includes the required page
870 for (first = 0, i = 0; i < count; i = runend) {
871 if (vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex), &firstaddr,
873 relpbuf(bp, freecnt);
874 /* The requested page may be out of range. */
875 vm_pager_free_nonreq(object, m + i, reqpage - i,
877 return (VM_PAGER_ERROR);
879 if (firstaddr == -1) {
880 VM_OBJECT_WLOCK(object);
881 if (i == reqpage && foff < object->un_pager.vnp.vnp_size) {
882 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %jd, foff: 0x%jx%08jx, vnp_size: 0x%jx%08jx",
883 (intmax_t)firstaddr, (uintmax_t)(foff >> 32),
886 (object->un_pager.vnp.vnp_size >> 32),
887 (uintmax_t)object->un_pager.vnp.vnp_size);
891 vm_page_unlock(m[i]);
892 VM_OBJECT_WUNLOCK(object);
898 if (runend <= reqpage) {
899 VM_OBJECT_WLOCK(object);
900 for (j = i; j < runend; j++) {
903 vm_page_unlock(m[j]);
905 VM_OBJECT_WUNLOCK(object);
907 if (runpg < (count - first)) {
908 VM_OBJECT_WLOCK(object);
909 for (i = first + runpg; i < count; i++) {
912 vm_page_unlock(m[i]);
914 VM_OBJECT_WUNLOCK(object);
915 count = first + runpg;
923 * the first and last page have been calculated now, move input pages
924 * to be zero based...
933 * calculate the file virtual address for the transfer
935 foff = IDX_TO_OFF(m[0]->pindex);
938 * calculate the size of the transfer
940 size = count * PAGE_SIZE;
941 KASSERT(count > 0, ("zero count"));
942 if ((foff + size) > object->un_pager.vnp.vnp_size)
943 size = object->un_pager.vnp.vnp_size - foff;
944 KASSERT(size > 0, ("zero size"));
947 * round up physical size for real devices.
950 int secmask = bo->bo_bsize - 1;
951 KASSERT(secmask < PAGE_SIZE && secmask > 0,
952 ("vnode_pager_generic_getpages: sector size %d too large",
954 size = (size + secmask) & ~secmask;
957 bp->b_kvaalloc = bp->b_data;
960 * and map the pages to be read into the kva, if the filesystem
961 * requires mapped buffers.
963 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
964 unmapped_buf_allowed) {
965 bp->b_data = unmapped_buf;
966 bp->b_kvabase = unmapped_buf;
968 bp->b_flags |= B_UNMAPPED;
970 pmap_qenter((vm_offset_t)bp->b_kvaalloc, m, count);
972 /* build a minimal buffer header */
973 bp->b_iocmd = BIO_READ;
974 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
975 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
976 bp->b_rcred = crhold(curthread->td_ucred);
977 bp->b_wcred = crhold(curthread->td_ucred);
978 bp->b_blkno = firstaddr;
982 bp->b_bufsize = size;
983 bp->b_runningbufspace = bp->b_bufsize;
984 for (i = 0; i < count; i++)
985 bp->b_pages[i] = m[i];
986 bp->b_npages = count;
987 bp->b_pager.pg_reqpage = reqpage;
988 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
990 PCPU_INC(cnt.v_vnodein);
991 PCPU_ADD(cnt.v_vnodepgsin, count);
994 bp->b_iooffset = dbtob(bp->b_blkno);
996 if (iodone != NULL) { /* async */
997 bp->b_pager.pg_iodone = iodone;
999 bp->b_iodone = vnode_pager_generic_getpages_done_async;
1000 bp->b_flags |= B_ASYNC;
1005 bp->b_iodone = bdone;
1007 bwait(bp, PVM, "vnread");
1008 error = vnode_pager_generic_getpages_done(bp);
1009 for (i = 0; i < bp->b_npages; i++)
1010 bp->b_pages[i] = NULL;
1013 relpbuf(bp, &vnode_pbuf_freecnt);
1016 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1020 vnode_pager_generic_getpages_done_async(struct buf *bp)
1024 error = vnode_pager_generic_getpages_done(bp);
1025 bp->b_pager.pg_iodone(bp->b_caller1, bp->b_pages,
1026 bp->b_pager.pg_reqpage, error);
1027 for (int i = 0; i < bp->b_npages; i++)
1028 bp->b_pages[i] = NULL;
1031 relpbuf(bp, &vnode_async_pbuf_freecnt);
1035 vnode_pager_generic_getpages_done(struct buf *bp)
1038 off_t tfoff, nextoff;
1041 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1042 object = bp->b_vp->v_object;
1044 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1045 if ((bp->b_flags & B_UNMAPPED) != 0) {
1046 bp->b_flags &= ~B_UNMAPPED;
1047 pmap_qenter((vm_offset_t)bp->b_kvaalloc, bp->b_pages,
1050 bzero(bp->b_kvaalloc + bp->b_bcount,
1051 PAGE_SIZE * bp->b_npages - bp->b_bcount);
1053 if ((bp->b_flags & B_UNMAPPED) == 0)
1054 pmap_qremove((vm_offset_t)bp->b_kvaalloc, bp->b_npages);
1055 if ((bp->b_vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0) {
1056 bp->b_data = bp->b_kvaalloc;
1057 bp->b_kvabase = bp->b_kvaalloc;
1058 bp->b_flags &= ~B_UNMAPPED;
1061 VM_OBJECT_WLOCK(object);
1062 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1063 i < bp->b_npages; i++, tfoff = nextoff) {
1066 nextoff = tfoff + PAGE_SIZE;
1067 mt = bp->b_pages[i];
1069 if (nextoff <= object->un_pager.vnp.vnp_size) {
1071 * Read filled up entire page.
1073 mt->valid = VM_PAGE_BITS_ALL;
1074 KASSERT(mt->dirty == 0,
1075 ("%s: page %p is dirty", __func__, mt));
1076 KASSERT(!pmap_page_is_mapped(mt),
1077 ("%s: page %p is mapped", __func__, mt));
1080 * Read did not fill up entire page.
1082 * Currently we do not set the entire page valid,
1083 * we just try to clear the piece that we couldn't
1086 vm_page_set_valid_range(mt, 0,
1087 object->un_pager.vnp.vnp_size - tfoff);
1088 KASSERT((mt->dirty & vm_page_bits(0,
1089 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1090 ("%s: page %p is dirty", __func__, mt));
1093 if (i != bp->b_pager.pg_reqpage)
1094 vm_page_readahead_finish(mt);
1096 VM_OBJECT_WUNLOCK(object);
1098 printf("%s: I/O read error %d\n", __func__, error);
1104 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1105 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1106 * vnode_pager_generic_putpages() to implement the previous behaviour.
1108 * All other FS's should use the bypass to get to the local media
1109 * backing vp's VOP_PUTPAGES.
1112 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1113 int flags, int *rtvals)
1117 int bytes = count * PAGE_SIZE;
1120 * Force synchronous operation if we are extremely low on memory
1121 * to prevent a low-memory deadlock. VOP operations often need to
1122 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1123 * operation ). The swapper handles the case by limiting the amount
1124 * of asynchronous I/O, but that sort of solution doesn't scale well
1125 * for the vnode pager without a lot of work.
1127 * Also, the backing vnode's iodone routine may not wake the pageout
1128 * daemon up. This should be probably be addressed XXX.
1131 if (vm_cnt.v_free_count + vm_cnt.v_cache_count <
1132 vm_cnt.v_pageout_free_min)
1133 flags |= VM_PAGER_PUT_SYNC;
1136 * Call device-specific putpages function
1138 vp = object->handle;
1139 VM_OBJECT_WUNLOCK(object);
1140 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1141 KASSERT(rtval != EOPNOTSUPP,
1142 ("vnode_pager: stale FS putpages\n"));
1143 VM_OBJECT_WLOCK(object);
1148 * This is now called from local media FS's to operate against their
1149 * own vnodes if they fail to implement VOP_PUTPAGES.
1151 * This is typically called indirectly via the pageout daemon and
1152 * clustering has already typically occured, so in general we ask the
1153 * underlying filesystem to write the data out asynchronously rather
1157 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1158 int flags, int *rtvals)
1165 int maxsize, ncount;
1166 vm_ooffset_t poffset;
1172 static struct timeval lastfail;
1175 object = vp->v_object;
1176 count = bytecount / PAGE_SIZE;
1178 for (i = 0; i < count; i++)
1179 rtvals[i] = VM_PAGER_ERROR;
1181 if ((int64_t)ma[0]->pindex < 0) {
1182 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%lx)\n",
1183 (long)ma[0]->pindex, (u_long)ma[0]->dirty);
1184 rtvals[0] = VM_PAGER_BAD;
1185 return VM_PAGER_BAD;
1188 maxsize = count * PAGE_SIZE;
1191 poffset = IDX_TO_OFF(ma[0]->pindex);
1194 * If the page-aligned write is larger then the actual file we
1195 * have to invalidate pages occuring beyond the file EOF. However,
1196 * there is an edge case where a file may not be page-aligned where
1197 * the last page is partially invalid. In this case the filesystem
1198 * may not properly clear the dirty bits for the entire page (which
1199 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1200 * With the page locked we are free to fix-up the dirty bits here.
1202 * We do not under any circumstances truncate the valid bits, as
1203 * this will screw up bogus page replacement.
1205 VM_OBJECT_WLOCK(object);
1206 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1207 if (object->un_pager.vnp.vnp_size > poffset) {
1210 maxsize = object->un_pager.vnp.vnp_size - poffset;
1211 ncount = btoc(maxsize);
1212 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1214 * If the object is locked and the following
1215 * conditions hold, then the page's dirty
1216 * field cannot be concurrently changed by a
1220 vm_page_assert_sbusied(m);
1221 KASSERT(!pmap_page_is_write_mapped(m),
1222 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1223 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1230 if (ncount < count) {
1231 for (i = ncount; i < count; i++) {
1232 rtvals[i] = VM_PAGER_BAD;
1236 VM_OBJECT_WUNLOCK(object);
1239 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
1240 * rather then a bdwrite() to prevent paging I/O from saturating
1241 * the buffer cache. Dummy-up the sequential heuristic to cause
1242 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
1243 * the system decides how to cluster.
1246 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
1248 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
1249 ioflags |= IO_ASYNC;
1250 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
1251 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1253 aiov.iov_base = (caddr_t) 0;
1254 aiov.iov_len = maxsize;
1255 auio.uio_iov = &aiov;
1256 auio.uio_iovcnt = 1;
1257 auio.uio_offset = poffset;
1258 auio.uio_segflg = UIO_NOCOPY;
1259 auio.uio_rw = UIO_WRITE;
1260 auio.uio_resid = maxsize;
1261 auio.uio_td = (struct thread *) 0;
1262 error = VOP_WRITE(vp, &auio, ioflags, curthread->td_ucred);
1263 PCPU_INC(cnt.v_vnodeout);
1264 PCPU_ADD(cnt.v_vnodepgsout, ncount);
1267 if ((ppscheck = ppsratecheck(&lastfail, &curfail, 1)))
1268 printf("vnode_pager_putpages: I/O error %d\n", error);
1270 if (auio.uio_resid) {
1271 if (ppscheck || ppsratecheck(&lastfail, &curfail, 1))
1272 printf("vnode_pager_putpages: residual I/O %zd at %lu\n",
1273 auio.uio_resid, (u_long)ma[0]->pindex);
1275 for (i = 0; i < ncount; i++) {
1276 rtvals[i] = VM_PAGER_OK;
1282 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written)
1289 obj = ma[0]->object;
1290 VM_OBJECT_WLOCK(obj);
1291 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1292 if (pos < trunc_page(written)) {
1293 rtvals[i] = VM_PAGER_OK;
1294 vm_page_undirty(ma[i]);
1296 /* Partially written page. */
1297 rtvals[i] = VM_PAGER_AGAIN;
1298 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1301 VM_OBJECT_WUNLOCK(obj);
1305 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1309 vm_ooffset_t old_wm;
1311 VM_OBJECT_WLOCK(object);
1312 if (object->type != OBJT_VNODE) {
1313 VM_OBJECT_WUNLOCK(object);
1316 old_wm = object->un_pager.vnp.writemappings;
1317 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1318 vp = object->handle;
1319 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1320 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1321 VOP_ADD_WRITECOUNT(vp, 1);
1322 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1323 __func__, vp, vp->v_writecount);
1324 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1325 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1326 VOP_ADD_WRITECOUNT(vp, -1);
1327 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1328 __func__, vp, vp->v_writecount);
1330 VM_OBJECT_WUNLOCK(object);
1334 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1341 VM_OBJECT_WLOCK(object);
1344 * First, recheck the object type to account for the race when
1345 * the vnode is reclaimed.
1347 if (object->type != OBJT_VNODE) {
1348 VM_OBJECT_WUNLOCK(object);
1353 * Optimize for the case when writemappings is not going to
1357 if (object->un_pager.vnp.writemappings != inc) {
1358 object->un_pager.vnp.writemappings -= inc;
1359 VM_OBJECT_WUNLOCK(object);
1363 vp = object->handle;
1365 VM_OBJECT_WUNLOCK(object);
1367 vn_start_write(vp, &mp, V_WAIT);
1368 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1371 * Decrement the object's writemappings, by swapping the start
1372 * and end arguments for vnode_pager_update_writecount(). If
1373 * there was not a race with vnode reclaimation, then the
1374 * vnode's v_writecount is decremented.
1376 vnode_pager_update_writecount(object, end, start);
1380 vn_finished_write(mp);