2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1990 University of Utah.
5 * Copyright (c) 1991 The Regents of the University of California.
7 * Copyright (c) 1993, 1994 John S. Dyson
8 * Copyright (c) 1995, David Greenman
10 * This code is derived from software contributed to Berkeley by
11 * the Systems Programming Group of the University of Utah Computer
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91
46 * Page to/from files (vnodes).
51 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
52 * greatly re-simplify the vnode_pager.
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
60 #include <sys/param.h>
61 #include <sys/kernel.h>
62 #include <sys/systm.h>
63 #include <sys/sysctl.h>
65 #include <sys/vnode.h>
66 #include <sys/mount.h>
69 #include <sys/vmmeter.h>
70 #include <sys/limits.h>
72 #include <sys/rwlock.h>
73 #include <sys/sf_buf.h>
74 #include <sys/domainset.h>
76 #include <machine/atomic.h>
79 #include <vm/vm_param.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_pager.h>
83 #include <vm/vm_map.h>
84 #include <vm/vnode_pager.h>
85 #include <vm/vm_extern.h>
88 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
89 daddr_t *rtaddress, int *run);
90 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
91 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
92 static void vnode_pager_dealloc(vm_object_t);
93 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
94 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
95 int *, vop_getpages_iodone_t, void *);
96 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
97 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
98 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
99 vm_ooffset_t, struct ucred *cred);
100 static int vnode_pager_generic_getpages_done(struct buf *);
101 static void vnode_pager_generic_getpages_done_async(struct buf *);
103 struct pagerops vnodepagerops = {
104 .pgo_alloc = vnode_pager_alloc,
105 .pgo_dealloc = vnode_pager_dealloc,
106 .pgo_getpages = vnode_pager_getpages,
107 .pgo_getpages_async = vnode_pager_getpages_async,
108 .pgo_putpages = vnode_pager_putpages,
109 .pgo_haspage = vnode_pager_haspage,
112 static struct domainset *vnode_domainset = NULL;
114 SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset, CTLTYPE_STRING | CTLFLAG_RW,
115 &vnode_domainset, 0, sysctl_handle_domainset, "A",
116 "Default vnode NUMA policy");
119 SYSCTL_INT(_vm, OID_AUTO, vnode_pbufs, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
120 &nvnpbufs, 0, "number of physical buffers allocated for vnode pager");
122 static uma_zone_t vnode_pbuf_zone;
125 vnode_pager_init(void *dummy)
129 nvnpbufs = nswbuf * 2;
131 nvnpbufs = nswbuf / 2;
133 TUNABLE_INT_FETCH("vm.vnode_pbufs", &nvnpbufs);
134 vnode_pbuf_zone = pbuf_zsecond_create("vnpbuf", nvnpbufs);
136 SYSINIT(vnode_pager, SI_SUB_CPU, SI_ORDER_ANY, vnode_pager_init, NULL);
138 /* Create the VM system backing object for this vnode */
140 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
143 vm_ooffset_t size = isize;
146 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
149 while ((object = vp->v_object) != NULL) {
150 VM_OBJECT_WLOCK(object);
151 if (!(object->flags & OBJ_DEAD)) {
152 VM_OBJECT_WUNLOCK(object);
156 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
157 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
158 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
162 if (vn_isdisk(vp, NULL)) {
163 size = IDX_TO_OFF(INT_MAX);
165 if (VOP_GETATTR(vp, &va, td->td_ucred))
171 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
173 * Dereference the reference we just created. This assumes
174 * that the object is associated with the vp.
176 VM_OBJECT_WLOCK(object);
178 VM_OBJECT_WUNLOCK(object);
181 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
187 vnode_destroy_vobject(struct vnode *vp)
189 struct vm_object *obj;
194 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
195 VM_OBJECT_WLOCK(obj);
196 umtx_shm_object_terminated(obj);
197 if (obj->ref_count == 0) {
199 * don't double-terminate the object
201 if ((obj->flags & OBJ_DEAD) == 0) {
202 vm_object_terminate(obj);
205 * Waiters were already handled during object
206 * termination. The exclusive vnode lock hopefully
207 * prevented new waiters from referencing the dying
210 KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0,
211 ("OBJ_DISCONNECTWNT set obj %p flags %x",
214 VM_OBJECT_WUNLOCK(obj);
218 * Woe to the process that tries to page now :-).
220 vm_pager_deallocate(obj);
221 VM_OBJECT_WUNLOCK(obj);
223 KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
228 * Allocate (or lookup) pager for a vnode.
229 * Handle is a vnode pointer.
234 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
235 vm_ooffset_t offset, struct ucred *cred)
241 * Pageout to vnode, no can do yet.
246 vp = (struct vnode *) handle;
249 * If the object is being terminated, wait for it to
253 while ((object = vp->v_object) != NULL) {
254 VM_OBJECT_WLOCK(object);
255 if ((object->flags & OBJ_DEAD) == 0)
257 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
258 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
261 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
263 if (object == NULL) {
265 * Add an object of the appropriate size
267 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
269 object->un_pager.vnp.vnp_size = size;
270 object->un_pager.vnp.writemappings = 0;
271 object->domain.dr_policy = vnode_domainset;
273 object->handle = handle;
275 if (vp->v_object != NULL) {
277 * Object has been created while we were sleeping
280 VM_OBJECT_WLOCK(object);
281 KASSERT(object->ref_count == 1,
282 ("leaked ref %p %d", object, object->ref_count));
283 object->type = OBJT_DEAD;
284 object->ref_count = 0;
285 VM_OBJECT_WUNLOCK(object);
286 vm_object_destroy(object);
289 vp->v_object = object;
293 #if VM_NRESERVLEVEL > 0
294 vm_object_color(object, 0);
296 VM_OBJECT_WUNLOCK(object);
303 * The object must be locked.
306 vnode_pager_dealloc(vm_object_t object)
313 panic("vnode_pager_dealloc: pager already dealloced");
315 VM_OBJECT_ASSERT_WLOCKED(object);
316 vm_object_pip_wait(object, "vnpdea");
317 refs = object->ref_count;
319 object->handle = NULL;
320 object->type = OBJT_DEAD;
321 if (object->flags & OBJ_DISCONNECTWNT) {
322 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
325 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
326 if (object->un_pager.vnp.writemappings > 0) {
327 object->un_pager.vnp.writemappings = 0;
328 VOP_ADD_WRITECOUNT(vp, -1);
329 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
330 __func__, vp, vp->v_writecount);
334 VM_OBJECT_WUNLOCK(object);
337 VM_OBJECT_WLOCK(object);
341 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
344 struct vnode *vp = object->handle;
350 int pagesperblock, blocksperpage;
352 VM_OBJECT_ASSERT_WLOCKED(object);
354 * If no vp or vp is doomed or marked transparent to VM, we do not
357 if (vp == NULL || vp->v_iflag & VI_DOOMED)
360 * If the offset is beyond end of file we do
363 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
366 bsize = vp->v_mount->mnt_stat.f_iosize;
367 pagesperblock = bsize / PAGE_SIZE;
369 if (pagesperblock > 0) {
370 reqblock = pindex / pagesperblock;
372 blocksperpage = (PAGE_SIZE / bsize);
373 reqblock = pindex * blocksperpage;
375 VM_OBJECT_WUNLOCK(object);
376 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
377 VM_OBJECT_WLOCK(object);
382 if (pagesperblock > 0) {
383 poff = pindex - (reqblock * pagesperblock);
385 *before *= pagesperblock;
390 * The BMAP vop can report a partial block in the
391 * 'after', but must not report blocks after EOF.
392 * Assert the latter, and truncate 'after' in case
395 KASSERT((reqblock + *after) * pagesperblock <
396 roundup2(object->size, pagesperblock),
397 ("%s: reqblock %jd after %d size %ju", __func__,
398 (intmax_t )reqblock, *after,
399 (uintmax_t )object->size));
400 *after *= pagesperblock;
401 *after += pagesperblock - (poff + 1);
402 if (pindex + *after >= object->size)
403 *after = object->size - 1 - pindex;
407 *before /= blocksperpage;
411 *after /= blocksperpage;
418 * Lets the VM system know about a change in size for a file.
419 * We adjust our own internal size and flush any cached pages in
420 * the associated object that are affected by the size change.
422 * Note: this routine may be invoked as a result of a pager put
423 * operation (possibly at object termination time), so we must be careful.
426 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
430 vm_pindex_t nobjsize;
432 if ((object = vp->v_object) == NULL)
434 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
435 VM_OBJECT_WLOCK(object);
436 if (object->type == OBJT_DEAD) {
437 VM_OBJECT_WUNLOCK(object);
440 KASSERT(object->type == OBJT_VNODE,
441 ("not vnode-backed object %p", object));
442 if (nsize == object->un_pager.vnp.vnp_size) {
444 * Hasn't changed size
446 VM_OBJECT_WUNLOCK(object);
449 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
450 if (nsize < object->un_pager.vnp.vnp_size) {
452 * File has shrunk. Toss any cached pages beyond the new EOF.
454 if (nobjsize < object->size)
455 vm_object_page_remove(object, nobjsize, object->size,
458 * this gets rid of garbage at the end of a page that is now
459 * only partially backed by the vnode.
461 * XXX for some reason (I don't know yet), if we take a
462 * completely invalid page and mark it partially valid
463 * it can screw up NFS reads, so we don't allow the case.
465 if ((nsize & PAGE_MASK) &&
466 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
468 int base = (int)nsize & PAGE_MASK;
469 int size = PAGE_SIZE - base;
472 * Clear out partial-page garbage in case
473 * the page has been mapped.
475 pmap_zero_page_area(m, base, size);
478 * Update the valid bits to reflect the blocks that
479 * have been zeroed. Some of these valid bits may
480 * have already been set.
482 vm_page_set_valid_range(m, base, size);
485 * Round "base" to the next block boundary so that the
486 * dirty bit for a partially zeroed block is not
489 base = roundup2(base, DEV_BSIZE);
492 * Clear out partial-page dirty bits.
494 * note that we do not clear out the valid
495 * bits. This would prevent bogus_page
496 * replacement from working properly.
498 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
501 object->un_pager.vnp.vnp_size = nsize;
502 object->size = nobjsize;
503 VM_OBJECT_WUNLOCK(object);
507 * calculate the linear (byte) disk address of specified virtual
511 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
522 if (vp->v_iflag & VI_DOOMED)
525 bsize = vp->v_mount->mnt_stat.f_iosize;
526 vblock = address / bsize;
527 voffset = address % bsize;
529 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
531 if (*rtaddress != -1)
532 *rtaddress += voffset / DEV_BSIZE;
535 *run *= bsize/PAGE_SIZE;
536 *run -= voffset/PAGE_SIZE;
544 * small block filesystem vnode pager input
547 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
560 if (vp->v_iflag & VI_DOOMED)
563 bsize = vp->v_mount->mnt_stat.f_iosize;
565 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
567 sf = sf_buf_alloc(m, 0);
569 for (i = 0; i < PAGE_SIZE / bsize; i++) {
570 vm_ooffset_t address;
572 bits = vm_page_bits(i * bsize, bsize);
576 address = IDX_TO_OFF(m->pindex) + i * bsize;
577 if (address >= object->un_pager.vnp.vnp_size) {
580 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
584 if (fileaddr != -1) {
585 bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
587 /* build a minimal buffer header */
588 bp->b_iocmd = BIO_READ;
589 bp->b_iodone = bdone;
590 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
591 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
592 bp->b_rcred = crhold(curthread->td_ucred);
593 bp->b_wcred = crhold(curthread->td_ucred);
594 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
595 bp->b_blkno = fileaddr;
598 bp->b_bcount = bsize;
599 bp->b_bufsize = bsize;
600 bp->b_runningbufspace = bp->b_bufsize;
601 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
604 bp->b_iooffset = dbtob(bp->b_blkno);
607 bwait(bp, PVM, "vnsrd");
609 if ((bp->b_ioflags & BIO_ERROR) != 0)
613 * free the buffer header back to the swap buffer pool
617 uma_zfree(vnode_pbuf_zone, bp);
621 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
622 KASSERT((m->dirty & bits) == 0,
623 ("vnode_pager_input_smlfs: page %p is dirty", m));
624 VM_OBJECT_WLOCK(object);
626 VM_OBJECT_WUNLOCK(object);
630 return VM_PAGER_ERROR;
636 * old style vnode pager input routine
639 vnode_pager_input_old(vm_object_t object, vm_page_t m)
648 VM_OBJECT_ASSERT_WLOCKED(object);
652 * Return failure if beyond current EOF
654 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
658 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
659 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
661 VM_OBJECT_WUNLOCK(object);
664 * Allocate a kernel virtual address and initialize so that
665 * we can use VOP_READ/WRITE routines.
667 sf = sf_buf_alloc(m, 0);
669 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
671 auio.uio_iov = &aiov;
673 auio.uio_offset = IDX_TO_OFF(m->pindex);
674 auio.uio_segflg = UIO_SYSSPACE;
675 auio.uio_rw = UIO_READ;
676 auio.uio_resid = size;
677 auio.uio_td = curthread;
679 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
681 int count = size - auio.uio_resid;
685 else if (count != PAGE_SIZE)
686 bzero((caddr_t)sf_buf_kva(sf) + count,
691 VM_OBJECT_WLOCK(object);
693 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
695 m->valid = VM_PAGE_BITS_ALL;
696 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
700 * generic vnode pager input routine
704 * Local media VFS's that do not implement their own VOP_GETPAGES
705 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
706 * to implement the previous behaviour.
708 * All other FS's should use the bypass to get to the local media
709 * backing vp's VOP_GETPAGES.
712 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
719 VM_OBJECT_WUNLOCK(object);
720 rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
721 KASSERT(rtval != EOPNOTSUPP,
722 ("vnode_pager: FS getpages not implemented\n"));
723 VM_OBJECT_WLOCK(object);
728 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
729 int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
735 VM_OBJECT_WUNLOCK(object);
736 rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
737 KASSERT(rtval != EOPNOTSUPP,
738 ("vnode_pager: FS getpages_async not implemented\n"));
739 VM_OBJECT_WLOCK(object);
744 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
745 * local filesystems, where partially valid pages can only occur at
749 vnode_pager_local_getpages(struct vop_getpages_args *ap)
752 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
753 ap->a_rbehind, ap->a_rahead, NULL, NULL));
757 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
760 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
761 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
765 * This is now called from local media FS's to operate against their
766 * own vnodes if they fail to implement VOP_GETPAGES.
769 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
770 int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
779 int bsize, pagesperblock;
780 int error, before, after, rbehind, rahead, poff, i;
781 int bytecount, secmask;
783 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
784 ("%s does not support devices", __func__));
786 if (vp->v_iflag & VI_DOOMED)
787 return (VM_PAGER_BAD);
789 object = vp->v_object;
790 foff = IDX_TO_OFF(m[0]->pindex);
791 bsize = vp->v_mount->mnt_stat.f_iosize;
792 pagesperblock = bsize / PAGE_SIZE;
794 KASSERT(foff < object->un_pager.vnp.vnp_size,
795 ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
796 KASSERT(count <= sizeof(bp->b_pages),
797 ("%s: requested %d pages", __func__, count));
800 * The last page has valid blocks. Invalid part can only
801 * exist at the end of file, and the page is made fully valid
802 * by zeroing in vm_pager_get_pages().
804 if (m[count - 1]->valid != 0 && --count == 0) {
806 iodone(arg, m, 1, 0);
807 return (VM_PAGER_OK);
810 bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
813 * Get the underlying device blocks for the file with VOP_BMAP().
814 * If the file system doesn't support VOP_BMAP, use old way of
815 * getting pages via VOP_READ.
817 error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
818 if (error == EOPNOTSUPP) {
819 uma_zfree(vnode_pbuf_zone, bp);
820 VM_OBJECT_WLOCK(object);
821 for (i = 0; i < count; i++) {
822 VM_CNT_INC(v_vnodein);
823 VM_CNT_INC(v_vnodepgsin);
824 error = vnode_pager_input_old(object, m[i]);
828 VM_OBJECT_WUNLOCK(object);
830 } else if (error != 0) {
831 uma_zfree(vnode_pbuf_zone, bp);
832 return (VM_PAGER_ERROR);
836 * If the file system supports BMAP, but blocksize is smaller
837 * than a page size, then use special small filesystem code.
839 if (pagesperblock == 0) {
840 uma_zfree(vnode_pbuf_zone, bp);
841 for (i = 0; i < count; i++) {
842 VM_CNT_INC(v_vnodein);
843 VM_CNT_INC(v_vnodepgsin);
844 error = vnode_pager_input_smlfs(object, m[i]);
852 * A sparse file can be encountered only for a single page request,
853 * which may not be preceded by call to vm_pager_haspage().
855 if (bp->b_blkno == -1) {
857 ("%s: array[%d] request to a sparse file %p", __func__,
859 uma_zfree(vnode_pbuf_zone, bp);
860 pmap_zero_page(m[0]);
861 KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
863 VM_OBJECT_WLOCK(object);
864 m[0]->valid = VM_PAGE_BITS_ALL;
865 VM_OBJECT_WUNLOCK(object);
866 return (VM_PAGER_OK);
870 blkno0 = bp->b_blkno;
872 bp->b_blkno += (foff % bsize) / DEV_BSIZE;
874 /* Recalculate blocks available after/before to pages. */
875 poff = (foff % bsize) / PAGE_SIZE;
876 before *= pagesperblock;
878 after *= pagesperblock;
879 after += pagesperblock - (poff + 1);
880 if (m[0]->pindex + after >= object->size)
881 after = object->size - 1 - m[0]->pindex;
882 KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
883 __func__, count, after + 1));
886 /* Trim requested rbehind/rahead to possible values. */
887 rbehind = a_rbehind ? *a_rbehind : 0;
888 rahead = a_rahead ? *a_rahead : 0;
889 rbehind = min(rbehind, before);
890 rbehind = min(rbehind, m[0]->pindex);
891 rahead = min(rahead, after);
892 rahead = min(rahead, object->size - m[count - 1]->pindex);
894 * Check that total amount of pages fit into buf. Trim rbehind and
895 * rahead evenly if not.
897 if (rbehind + rahead + count > nitems(bp->b_pages)) {
900 trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
901 sum = rbehind + rahead;
902 if (rbehind == before) {
903 /* Roundup rbehind trim to block size. */
904 rbehind -= roundup(trim * rbehind / sum, pagesperblock);
908 rbehind -= trim * rbehind / sum;
909 rahead -= trim * rahead / sum;
911 KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
912 ("%s: behind %d ahead %d count %d", __func__,
913 rbehind, rahead, count));
916 * Fill in the bp->b_pages[] array with requested and optional
917 * read behind or read ahead pages. Read behind pages are looked
918 * up in a backward direction, down to a first cached page. Same
919 * for read ahead pages, but there is no need to shift the array
920 * in case of encountering a cached page.
922 i = bp->b_npages = 0;
924 vm_pindex_t startpindex, tpindex;
927 VM_OBJECT_WLOCK(object);
928 startpindex = m[0]->pindex - rbehind;
929 if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
930 p->pindex >= startpindex)
931 startpindex = p->pindex + 1;
933 /* tpindex is unsigned; beware of numeric underflow. */
934 for (tpindex = m[0]->pindex - 1;
935 tpindex >= startpindex && tpindex < m[0]->pindex;
937 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
939 /* Shift the array. */
940 for (int j = 0; j < i; j++)
941 bp->b_pages[j] = bp->b_pages[j +
942 tpindex + 1 - startpindex];
945 bp->b_pages[tpindex - startpindex] = p;
950 bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
954 /* Requested pages. */
955 for (int j = 0; j < count; j++, i++)
956 bp->b_pages[i] = m[j];
957 bp->b_npages += count;
960 vm_pindex_t endpindex, tpindex;
963 if (!VM_OBJECT_WOWNED(object))
964 VM_OBJECT_WLOCK(object);
965 endpindex = m[count - 1]->pindex + rahead + 1;
966 if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
967 p->pindex < endpindex)
968 endpindex = p->pindex;
969 if (endpindex > object->size)
970 endpindex = object->size;
972 for (tpindex = m[count - 1]->pindex + 1;
973 tpindex < endpindex; i++, tpindex++) {
974 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
980 bp->b_pgafter = i - bp->b_npages;
985 if (VM_OBJECT_WOWNED(object))
986 VM_OBJECT_WUNLOCK(object);
988 /* Report back actual behind/ahead read. */
990 *a_rbehind = bp->b_pgbefore;
992 *a_rahead = bp->b_pgafter;
995 KASSERT(bp->b_npages <= nitems(bp->b_pages),
996 ("%s: buf %p overflowed", __func__, bp));
997 for (int j = 1, prev = 0; j < bp->b_npages; j++) {
998 if (bp->b_pages[j] == bogus_page)
1000 KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
1001 j - prev, ("%s: pages array not consecutive, bp %p",
1008 * Recalculate first offset and bytecount with regards to read behind.
1009 * Truncate bytecount to vnode real size and round up physical size
1012 foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1013 bytecount = bp->b_npages << PAGE_SHIFT;
1014 if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
1015 bytecount = object->un_pager.vnp.vnp_size - foff;
1016 secmask = bo->bo_bsize - 1;
1017 KASSERT(secmask < PAGE_SIZE && secmask > 0,
1018 ("%s: sector size %d too large", __func__, secmask + 1));
1019 bytecount = (bytecount + secmask) & ~secmask;
1022 * And map the pages to be read into the kva, if the filesystem
1023 * requires mapped buffers.
1025 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
1026 unmapped_buf_allowed) {
1027 bp->b_data = unmapped_buf;
1030 bp->b_data = bp->b_kvabase;
1031 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1034 /* Build a minimal buffer header. */
1035 bp->b_iocmd = BIO_READ;
1036 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1037 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1038 bp->b_rcred = crhold(curthread->td_ucred);
1039 bp->b_wcred = crhold(curthread->td_ucred);
1042 bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
1043 bp->b_iooffset = dbtob(bp->b_blkno);
1044 KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1045 (blkno0 - bp->b_blkno) * DEV_BSIZE +
1046 IDX_TO_OFF(m[0]->pindex) % bsize,
1047 ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1048 "blkno0 %ju b_blkno %ju", bsize,
1049 (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1050 (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
1052 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
1053 VM_CNT_INC(v_vnodein);
1054 VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1056 if (iodone != NULL) { /* async */
1057 bp->b_pgiodone = iodone;
1058 bp->b_caller1 = arg;
1059 bp->b_iodone = vnode_pager_generic_getpages_done_async;
1060 bp->b_flags |= B_ASYNC;
1063 return (VM_PAGER_OK);
1065 bp->b_iodone = bdone;
1067 bwait(bp, PVM, "vnread");
1068 error = vnode_pager_generic_getpages_done(bp);
1069 for (i = 0; i < bp->b_npages; i++)
1070 bp->b_pages[i] = NULL;
1073 uma_zfree(vnode_pbuf_zone, bp);
1074 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1079 vnode_pager_generic_getpages_done_async(struct buf *bp)
1083 error = vnode_pager_generic_getpages_done(bp);
1084 /* Run the iodone upon the requested range. */
1085 bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1086 bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
1087 for (int i = 0; i < bp->b_npages; i++)
1088 bp->b_pages[i] = NULL;
1091 uma_zfree(vnode_pbuf_zone, bp);
1095 vnode_pager_generic_getpages_done(struct buf *bp)
1098 off_t tfoff, nextoff;
1101 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1102 object = bp->b_vp->v_object;
1104 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1105 if (!buf_mapped(bp)) {
1106 bp->b_data = bp->b_kvabase;
1107 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
1110 bzero(bp->b_data + bp->b_bcount,
1111 PAGE_SIZE * bp->b_npages - bp->b_bcount);
1113 if (buf_mapped(bp)) {
1114 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1115 bp->b_data = unmapped_buf;
1118 VM_OBJECT_WLOCK(object);
1119 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1120 i < bp->b_npages; i++, tfoff = nextoff) {
1123 nextoff = tfoff + PAGE_SIZE;
1124 mt = bp->b_pages[i];
1126 if (nextoff <= object->un_pager.vnp.vnp_size) {
1128 * Read filled up entire page.
1130 mt->valid = VM_PAGE_BITS_ALL;
1131 KASSERT(mt->dirty == 0,
1132 ("%s: page %p is dirty", __func__, mt));
1133 KASSERT(!pmap_page_is_mapped(mt),
1134 ("%s: page %p is mapped", __func__, mt));
1137 * Read did not fill up entire page.
1139 * Currently we do not set the entire page valid,
1140 * we just try to clear the piece that we couldn't
1143 vm_page_set_valid_range(mt, 0,
1144 object->un_pager.vnp.vnp_size - tfoff);
1145 KASSERT((mt->dirty & vm_page_bits(0,
1146 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1147 ("%s: page %p is dirty", __func__, mt));
1150 if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1151 vm_page_readahead_finish(mt);
1153 VM_OBJECT_WUNLOCK(object);
1155 printf("%s: I/O read error %d\n", __func__, error);
1161 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1162 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1163 * vnode_pager_generic_putpages() to implement the previous behaviour.
1165 * All other FS's should use the bypass to get to the local media
1166 * backing vp's VOP_PUTPAGES.
1169 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1170 int flags, int *rtvals)
1174 int bytes = count * PAGE_SIZE;
1177 * Force synchronous operation if we are extremely low on memory
1178 * to prevent a low-memory deadlock. VOP operations often need to
1179 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1180 * operation ). The swapper handles the case by limiting the amount
1181 * of asynchronous I/O, but that sort of solution doesn't scale well
1182 * for the vnode pager without a lot of work.
1184 * Also, the backing vnode's iodone routine may not wake the pageout
1185 * daemon up. This should be probably be addressed XXX.
1188 if (vm_page_count_min())
1189 flags |= VM_PAGER_PUT_SYNC;
1192 * Call device-specific putpages function
1194 vp = object->handle;
1195 VM_OBJECT_WUNLOCK(object);
1196 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1197 KASSERT(rtval != EOPNOTSUPP,
1198 ("vnode_pager: stale FS putpages\n"));
1199 VM_OBJECT_WLOCK(object);
1203 vn_off2bidx(vm_ooffset_t offset)
1206 return ((offset & PAGE_MASK) / DEV_BSIZE);
1210 vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
1213 KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
1214 offset < IDX_TO_OFF(m->pindex + 1),
1215 ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
1216 (uintmax_t)offset));
1217 return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
1221 * This is now called from local media FS's to operate against their
1222 * own vnodes if they fail to implement VOP_PUTPAGES.
1224 * This is typically called indirectly via the pageout daemon and
1225 * clustering has already typically occurred, so in general we ask the
1226 * underlying filesystem to write the data out asynchronously rather
1230 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1231 int flags, int *rtvals)
1235 vm_ooffset_t maxblksz, next_offset, poffset, prev_offset;
1238 off_t prev_resid, wrsz;
1239 int count, error, i, maxsize, ncount, pgoff, ppscheck;
1241 static struct timeval lastfail;
1244 object = vp->v_object;
1245 count = bytecount / PAGE_SIZE;
1247 for (i = 0; i < count; i++)
1248 rtvals[i] = VM_PAGER_ERROR;
1250 if ((int64_t)ma[0]->pindex < 0) {
1251 printf("vnode_pager_generic_putpages: "
1252 "attempt to write meta-data 0x%jx(%lx)\n",
1253 (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1254 rtvals[0] = VM_PAGER_BAD;
1255 return (VM_PAGER_BAD);
1258 maxsize = count * PAGE_SIZE;
1261 poffset = IDX_TO_OFF(ma[0]->pindex);
1264 * If the page-aligned write is larger then the actual file we
1265 * have to invalidate pages occurring beyond the file EOF. However,
1266 * there is an edge case where a file may not be page-aligned where
1267 * the last page is partially invalid. In this case the filesystem
1268 * may not properly clear the dirty bits for the entire page (which
1269 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1270 * With the page locked we are free to fix-up the dirty bits here.
1272 * We do not under any circumstances truncate the valid bits, as
1273 * this will screw up bogus page replacement.
1275 VM_OBJECT_RLOCK(object);
1276 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1277 if (!VM_OBJECT_TRYUPGRADE(object)) {
1278 VM_OBJECT_RUNLOCK(object);
1279 VM_OBJECT_WLOCK(object);
1280 if (maxsize + poffset <= object->un_pager.vnp.vnp_size)
1283 if (object->un_pager.vnp.vnp_size > poffset) {
1284 maxsize = object->un_pager.vnp.vnp_size - poffset;
1285 ncount = btoc(maxsize);
1286 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1287 pgoff = roundup2(pgoff, DEV_BSIZE);
1290 * If the object is locked and the following
1291 * conditions hold, then the page's dirty
1292 * field cannot be concurrently changed by a
1296 vm_page_assert_sbusied(m);
1297 KASSERT(!pmap_page_is_write_mapped(m),
1298 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1299 MPASS(m->dirty != 0);
1300 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1307 for (i = ncount; i < count; i++)
1308 rtvals[i] = VM_PAGER_BAD;
1310 VM_OBJECT_LOCK_DOWNGRADE(object);
1313 auio.uio_iov = &aiov;
1314 auio.uio_segflg = UIO_NOCOPY;
1315 auio.uio_rw = UIO_WRITE;
1317 maxblksz = roundup2(poffset + maxsize, DEV_BSIZE);
1319 for (prev_offset = poffset; prev_offset < maxblksz;) {
1320 /* Skip clean blocks. */
1321 for (in_hole = true; in_hole && prev_offset < maxblksz;) {
1322 m = ma[OFF_TO_IDX(prev_offset - poffset)];
1323 for (i = vn_off2bidx(prev_offset);
1324 i < sizeof(vm_page_bits_t) * NBBY &&
1325 prev_offset < maxblksz; i++) {
1326 if (vn_dirty_blk(m, prev_offset)) {
1330 prev_offset += DEV_BSIZE;
1336 /* Find longest run of dirty blocks. */
1337 for (next_offset = prev_offset; next_offset < maxblksz;) {
1338 m = ma[OFF_TO_IDX(next_offset - poffset)];
1339 for (i = vn_off2bidx(next_offset);
1340 i < sizeof(vm_page_bits_t) * NBBY &&
1341 next_offset < maxblksz; i++) {
1342 if (!vn_dirty_blk(m, next_offset))
1344 next_offset += DEV_BSIZE;
1348 if (next_offset > poffset + maxsize)
1349 next_offset = poffset + maxsize;
1352 * Getting here requires finding a dirty block in the
1353 * 'skip clean blocks' loop.
1355 MPASS(prev_offset < next_offset);
1357 VM_OBJECT_RUNLOCK(object);
1358 aiov.iov_base = NULL;
1359 auio.uio_iovcnt = 1;
1360 auio.uio_offset = prev_offset;
1361 prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
1363 error = VOP_WRITE(vp, &auio,
1364 vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
1366 wrsz = prev_resid - auio.uio_resid;
1368 if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
1369 vn_printf(vp, "vnode_pager_putpages: "
1370 "zero-length write at %ju resid %zd\n",
1371 auio.uio_offset, auio.uio_resid);
1373 VM_OBJECT_RLOCK(object);
1377 /* Adjust the starting offset for next iteration. */
1378 prev_offset += wrsz;
1379 MPASS(auio.uio_offset == prev_offset);
1382 if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
1384 vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
1386 if (auio.uio_resid != 0 && (ppscheck != 0 ||
1387 ppsratecheck(&lastfail, &curfail, 1) != 0))
1388 vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
1389 "at %ju\n", auio.uio_resid,
1390 (uintmax_t)ma[0]->pindex);
1391 VM_OBJECT_RLOCK(object);
1392 if (error != 0 || auio.uio_resid != 0)
1396 /* Mark completely processed pages. */
1397 for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
1398 rtvals[i] = VM_PAGER_OK;
1399 /* Mark partial EOF page. */
1400 if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
1401 rtvals[i++] = VM_PAGER_OK;
1402 /* Unwritten pages in range, free bonus if the page is clean. */
1403 for (; i < ncount; i++)
1404 rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
1405 VM_OBJECT_RUNLOCK(object);
1406 VM_CNT_ADD(v_vnodepgsout, i);
1407 VM_CNT_INC(v_vnodeout);
1412 vnode_pager_putpages_ioflags(int pager_flags)
1417 * Pageouts are already clustered, use IO_ASYNC to force a
1418 * bawrite() rather then a bdwrite() to prevent paging I/O
1419 * from saturating the buffer cache. Dummy-up the sequential
1420 * heuristic to cause large ranges to cluster. If neither
1421 * IO_SYNC or IO_ASYNC is set, the system decides how to
1425 if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0)
1427 else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0)
1428 ioflags |= IO_ASYNC;
1429 ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0;
1430 ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0;
1431 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1436 * vnode_pager_undirty_pages().
1438 * A helper to mark pages as clean after pageout that was possibly
1439 * done with a short write. The lpos argument specifies the page run
1440 * length in bytes, and the written argument specifies how many bytes
1441 * were actually written. eof is the offset past the last valid byte
1442 * in the vnode using the absolute file position of the first byte in
1443 * the run as the base from which it is computed.
1446 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof,
1450 int i, pos, pos_devb;
1452 if (written == 0 && eof >= lpos)
1454 obj = ma[0]->object;
1455 VM_OBJECT_WLOCK(obj);
1456 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1457 if (pos < trunc_page(written)) {
1458 rtvals[i] = VM_PAGER_OK;
1459 vm_page_undirty(ma[i]);
1461 /* Partially written page. */
1462 rtvals[i] = VM_PAGER_AGAIN;
1463 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1466 if (eof >= lpos) /* avoid truncation */
1468 for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1469 if (pos != trunc_page(pos)) {
1471 * The page contains the last valid byte in
1472 * the vnode, mark the rest of the page as
1473 * clean, potentially making the whole page
1476 pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1477 vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1481 * If the page was cleaned, report the pageout
1482 * on it as successful. msync() no longer
1483 * needs to write out the page, endlessly
1484 * creating write requests and dirty buffers.
1486 if (ma[i]->dirty == 0)
1487 rtvals[i] = VM_PAGER_OK;
1489 pos = round_page(pos);
1491 /* vm_pageout_flush() clears dirty */
1492 rtvals[i] = VM_PAGER_BAD;
1497 VM_OBJECT_WUNLOCK(obj);
1501 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1505 vm_ooffset_t old_wm;
1507 VM_OBJECT_WLOCK(object);
1508 if (object->type != OBJT_VNODE) {
1509 VM_OBJECT_WUNLOCK(object);
1512 old_wm = object->un_pager.vnp.writemappings;
1513 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1514 vp = object->handle;
1515 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1516 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1517 VOP_ADD_WRITECOUNT(vp, 1);
1518 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1519 __func__, vp, vp->v_writecount);
1520 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1521 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1522 VOP_ADD_WRITECOUNT(vp, -1);
1523 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1524 __func__, vp, vp->v_writecount);
1526 VM_OBJECT_WUNLOCK(object);
1530 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1537 VM_OBJECT_WLOCK(object);
1540 * First, recheck the object type to account for the race when
1541 * the vnode is reclaimed.
1543 if (object->type != OBJT_VNODE) {
1544 VM_OBJECT_WUNLOCK(object);
1549 * Optimize for the case when writemappings is not going to
1553 if (object->un_pager.vnp.writemappings != inc) {
1554 object->un_pager.vnp.writemappings -= inc;
1555 VM_OBJECT_WUNLOCK(object);
1559 vp = object->handle;
1561 VM_OBJECT_WUNLOCK(object);
1563 vn_start_write(vp, &mp, V_WAIT);
1564 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1567 * Decrement the object's writemappings, by swapping the start
1568 * and end arguments for vnode_pager_update_writecount(). If
1569 * there was not a race with vnode reclaimation, then the
1570 * vnode's v_writecount is decremented.
1572 vnode_pager_update_writecount(object, end, start);
1576 vn_finished_write(mp);