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>
71 #include <sys/limits.h>
73 #include <sys/rwlock.h>
74 #include <sys/sf_buf.h>
75 #include <sys/domainset.h>
77 #include <machine/atomic.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_object.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pager.h>
84 #include <vm/vm_map.h>
85 #include <vm/vnode_pager.h>
86 #include <vm/vm_extern.h>
89 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
90 daddr_t *rtaddress, int *run);
91 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
92 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
93 static void vnode_pager_dealloc(vm_object_t);
94 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
95 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
96 int *, vop_getpages_iodone_t, void *);
97 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
98 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
99 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
100 vm_ooffset_t, struct ucred *cred);
101 static int vnode_pager_generic_getpages_done(struct buf *);
102 static void vnode_pager_generic_getpages_done_async(struct buf *);
104 struct pagerops vnodepagerops = {
105 .pgo_alloc = vnode_pager_alloc,
106 .pgo_dealloc = vnode_pager_dealloc,
107 .pgo_getpages = vnode_pager_getpages,
108 .pgo_getpages_async = vnode_pager_getpages_async,
109 .pgo_putpages = vnode_pager_putpages,
110 .pgo_haspage = vnode_pager_haspage,
113 static struct domainset *vnode_domainset = NULL;
115 SYSCTL_PROC(_debug, OID_AUTO, vnode_domainset, CTLTYPE_STRING | CTLFLAG_RW,
116 &vnode_domainset, 0, sysctl_handle_domainset, "A",
117 "Default vnode NUMA policy");
120 SYSCTL_INT(_vm, OID_AUTO, vnode_pbufs, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
121 &nvnpbufs, 0, "number of physical buffers allocated for vnode pager");
123 static uma_zone_t vnode_pbuf_zone;
126 vnode_pager_init(void *dummy)
130 nvnpbufs = nswbuf * 2;
132 nvnpbufs = nswbuf / 2;
134 TUNABLE_INT_FETCH("vm.vnode_pbufs", &nvnpbufs);
135 vnode_pbuf_zone = pbuf_zsecond_create("vnpbuf", nvnpbufs);
137 SYSINIT(vnode_pager, SI_SUB_CPU, SI_ORDER_ANY, vnode_pager_init, NULL);
139 /* Create the VM system backing object for this vnode */
141 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
144 vm_ooffset_t size = isize;
147 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
150 while ((object = vp->v_object) != NULL) {
151 VM_OBJECT_WLOCK(object);
152 if (!(object->flags & OBJ_DEAD)) {
153 VM_OBJECT_WUNLOCK(object);
157 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
158 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
159 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
163 if (vn_isdisk(vp, NULL)) {
164 size = IDX_TO_OFF(INT_MAX);
166 if (VOP_GETATTR(vp, &va, td->td_ucred))
172 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
174 * Dereference the reference we just created. This assumes
175 * that the object is associated with the vp.
177 VM_OBJECT_WLOCK(object);
179 VM_OBJECT_WUNLOCK(object);
182 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
188 vnode_destroy_vobject(struct vnode *vp)
190 struct vm_object *obj;
195 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
196 VM_OBJECT_WLOCK(obj);
197 umtx_shm_object_terminated(obj);
198 if (obj->ref_count == 0) {
200 * don't double-terminate the object
202 if ((obj->flags & OBJ_DEAD) == 0) {
203 vm_object_terminate(obj);
206 * Waiters were already handled during object
207 * termination. The exclusive vnode lock hopefully
208 * prevented new waiters from referencing the dying
211 KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0,
212 ("OBJ_DISCONNECTWNT set obj %p flags %x",
215 VM_OBJECT_WUNLOCK(obj);
219 * Woe to the process that tries to page now :-).
221 vm_pager_deallocate(obj);
222 VM_OBJECT_WUNLOCK(obj);
224 KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
229 * Allocate (or lookup) pager for a vnode.
230 * Handle is a vnode pointer.
235 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
236 vm_ooffset_t offset, struct ucred *cred)
242 * Pageout to vnode, no can do yet.
247 vp = (struct vnode *) handle;
250 * If the object is being terminated, wait for it to
254 while ((object = vp->v_object) != NULL) {
255 VM_OBJECT_WLOCK(object);
256 if ((object->flags & OBJ_DEAD) == 0)
258 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
259 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
262 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
264 if (object == NULL) {
266 * Add an object of the appropriate size
268 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
270 object->un_pager.vnp.vnp_size = size;
271 object->un_pager.vnp.writemappings = 0;
272 object->domain.dr_policy = vnode_domainset;
274 object->handle = handle;
276 if (vp->v_object != NULL) {
278 * Object has been created while we were sleeping
281 VM_OBJECT_WLOCK(object);
282 KASSERT(object->ref_count == 1,
283 ("leaked ref %p %d", object, object->ref_count));
284 object->type = OBJT_DEAD;
285 object->ref_count = 0;
286 VM_OBJECT_WUNLOCK(object);
287 vm_object_destroy(object);
290 vp->v_object = object;
294 #if VM_NRESERVLEVEL > 0
295 vm_object_color(object, 0);
297 VM_OBJECT_WUNLOCK(object);
304 * The object must be locked.
307 vnode_pager_dealloc(vm_object_t object)
314 panic("vnode_pager_dealloc: pager already dealloced");
316 VM_OBJECT_ASSERT_WLOCKED(object);
317 vm_object_pip_wait(object, "vnpdea");
318 refs = object->ref_count;
320 object->handle = NULL;
321 object->type = OBJT_DEAD;
322 if (object->flags & OBJ_DISCONNECTWNT) {
323 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
326 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
327 if (object->un_pager.vnp.writemappings > 0) {
328 object->un_pager.vnp.writemappings = 0;
329 VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
330 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
331 __func__, vp, vp->v_writecount);
337 * vm_map_entry_set_vnode_text() cannot reach this vnode by
338 * following object->handle. Clear all text references now.
339 * This also clears the transient references from
340 * kern_execve(), which is fine because dead_vnodeops uses nop
341 * for VOP_UNSET_TEXT().
343 if (vp->v_writecount < 0)
344 vp->v_writecount = 0;
346 VM_OBJECT_WUNLOCK(object);
349 VM_OBJECT_WLOCK(object);
353 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
356 struct vnode *vp = object->handle;
362 int pagesperblock, blocksperpage;
364 VM_OBJECT_ASSERT_WLOCKED(object);
366 * If no vp or vp is doomed or marked transparent to VM, we do not
369 if (vp == NULL || vp->v_iflag & VI_DOOMED)
372 * If the offset is beyond end of file we do
375 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
378 bsize = vp->v_mount->mnt_stat.f_iosize;
379 pagesperblock = bsize / PAGE_SIZE;
381 if (pagesperblock > 0) {
382 reqblock = pindex / pagesperblock;
384 blocksperpage = (PAGE_SIZE / bsize);
385 reqblock = pindex * blocksperpage;
387 VM_OBJECT_WUNLOCK(object);
388 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
389 VM_OBJECT_WLOCK(object);
394 if (pagesperblock > 0) {
395 poff = pindex - (reqblock * pagesperblock);
397 *before *= pagesperblock;
402 * The BMAP vop can report a partial block in the
403 * 'after', but must not report blocks after EOF.
404 * Assert the latter, and truncate 'after' in case
407 KASSERT((reqblock + *after) * pagesperblock <
408 roundup2(object->size, pagesperblock),
409 ("%s: reqblock %jd after %d size %ju", __func__,
410 (intmax_t )reqblock, *after,
411 (uintmax_t )object->size));
412 *after *= pagesperblock;
413 *after += pagesperblock - (poff + 1);
414 if (pindex + *after >= object->size)
415 *after = object->size - 1 - pindex;
419 *before /= blocksperpage;
423 *after /= blocksperpage;
430 * Lets the VM system know about a change in size for a file.
431 * We adjust our own internal size and flush any cached pages in
432 * the associated object that are affected by the size change.
434 * Note: this routine may be invoked as a result of a pager put
435 * operation (possibly at object termination time), so we must be careful.
438 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
442 vm_pindex_t nobjsize;
444 if ((object = vp->v_object) == NULL)
446 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
447 VM_OBJECT_WLOCK(object);
448 if (object->type == OBJT_DEAD) {
449 VM_OBJECT_WUNLOCK(object);
452 KASSERT(object->type == OBJT_VNODE,
453 ("not vnode-backed object %p", object));
454 if (nsize == object->un_pager.vnp.vnp_size) {
456 * Hasn't changed size
458 VM_OBJECT_WUNLOCK(object);
461 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
462 if (nsize < object->un_pager.vnp.vnp_size) {
464 * File has shrunk. Toss any cached pages beyond the new EOF.
466 if (nobjsize < object->size)
467 vm_object_page_remove(object, nobjsize, object->size,
470 * this gets rid of garbage at the end of a page that is now
471 * only partially backed by the vnode.
473 * XXX for some reason (I don't know yet), if we take a
474 * completely invalid page and mark it partially valid
475 * it can screw up NFS reads, so we don't allow the case.
477 if ((nsize & PAGE_MASK) &&
478 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
480 int base = (int)nsize & PAGE_MASK;
481 int size = PAGE_SIZE - base;
484 * Clear out partial-page garbage in case
485 * the page has been mapped.
487 pmap_zero_page_area(m, base, size);
490 * Update the valid bits to reflect the blocks that
491 * have been zeroed. Some of these valid bits may
492 * have already been set.
494 vm_page_set_valid_range(m, base, size);
497 * Round "base" to the next block boundary so that the
498 * dirty bit for a partially zeroed block is not
501 base = roundup2(base, DEV_BSIZE);
504 * Clear out partial-page dirty bits.
506 * note that we do not clear out the valid
507 * bits. This would prevent bogus_page
508 * replacement from working properly.
510 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
513 object->un_pager.vnp.vnp_size = nsize;
514 object->size = nobjsize;
515 VM_OBJECT_WUNLOCK(object);
519 * calculate the linear (byte) disk address of specified virtual
523 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
534 if (vp->v_iflag & VI_DOOMED)
537 bsize = vp->v_mount->mnt_stat.f_iosize;
538 vblock = address / bsize;
539 voffset = address % bsize;
541 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
543 if (*rtaddress != -1)
544 *rtaddress += voffset / DEV_BSIZE;
547 *run *= bsize / PAGE_SIZE;
548 *run -= voffset / PAGE_SIZE;
556 * small block filesystem vnode pager input
559 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
572 if (vp->v_iflag & VI_DOOMED)
575 bsize = vp->v_mount->mnt_stat.f_iosize;
577 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
579 sf = sf_buf_alloc(m, 0);
581 for (i = 0; i < PAGE_SIZE / bsize; i++) {
582 vm_ooffset_t address;
584 bits = vm_page_bits(i * bsize, bsize);
588 address = IDX_TO_OFF(m->pindex) + i * bsize;
589 if (address >= object->un_pager.vnp.vnp_size) {
592 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
596 if (fileaddr != -1) {
597 bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
599 /* build a minimal buffer header */
600 bp->b_iocmd = BIO_READ;
601 bp->b_iodone = bdone;
602 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
603 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
604 bp->b_rcred = crhold(curthread->td_ucred);
605 bp->b_wcred = crhold(curthread->td_ucred);
606 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
607 bp->b_blkno = fileaddr;
610 bp->b_bcount = bsize;
611 bp->b_bufsize = bsize;
612 bp->b_runningbufspace = bp->b_bufsize;
613 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
616 bp->b_iooffset = dbtob(bp->b_blkno);
619 bwait(bp, PVM, "vnsrd");
621 if ((bp->b_ioflags & BIO_ERROR) != 0)
625 * free the buffer header back to the swap buffer pool
629 uma_zfree(vnode_pbuf_zone, bp);
633 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
634 KASSERT((m->dirty & bits) == 0,
635 ("vnode_pager_input_smlfs: page %p is dirty", m));
636 VM_OBJECT_WLOCK(object);
638 VM_OBJECT_WUNLOCK(object);
642 return VM_PAGER_ERROR;
648 * old style vnode pager input routine
651 vnode_pager_input_old(vm_object_t object, vm_page_t m)
660 VM_OBJECT_ASSERT_WLOCKED(object);
664 * Return failure if beyond current EOF
666 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
670 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
671 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
673 VM_OBJECT_WUNLOCK(object);
676 * Allocate a kernel virtual address and initialize so that
677 * we can use VOP_READ/WRITE routines.
679 sf = sf_buf_alloc(m, 0);
681 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
683 auio.uio_iov = &aiov;
685 auio.uio_offset = IDX_TO_OFF(m->pindex);
686 auio.uio_segflg = UIO_SYSSPACE;
687 auio.uio_rw = UIO_READ;
688 auio.uio_resid = size;
689 auio.uio_td = curthread;
691 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
693 int count = size - auio.uio_resid;
697 else if (count != PAGE_SIZE)
698 bzero((caddr_t)sf_buf_kva(sf) + count,
703 VM_OBJECT_WLOCK(object);
705 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
707 m->valid = VM_PAGE_BITS_ALL;
708 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
712 * generic vnode pager input routine
716 * Local media VFS's that do not implement their own VOP_GETPAGES
717 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
718 * to implement the previous behaviour.
720 * All other FS's should use the bypass to get to the local media
721 * backing vp's VOP_GETPAGES.
724 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
731 VM_OBJECT_WUNLOCK(object);
732 rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
733 KASSERT(rtval != EOPNOTSUPP,
734 ("vnode_pager: FS getpages not implemented\n"));
735 VM_OBJECT_WLOCK(object);
740 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
741 int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
747 VM_OBJECT_WUNLOCK(object);
748 rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
749 KASSERT(rtval != EOPNOTSUPP,
750 ("vnode_pager: FS getpages_async not implemented\n"));
751 VM_OBJECT_WLOCK(object);
756 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
757 * local filesystems, where partially valid pages can only occur at
761 vnode_pager_local_getpages(struct vop_getpages_args *ap)
764 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
765 ap->a_rbehind, ap->a_rahead, NULL, NULL));
769 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
772 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
773 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
777 * This is now called from local media FS's to operate against their
778 * own vnodes if they fail to implement VOP_GETPAGES.
781 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
782 int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
791 int bsize, pagesperblock;
792 int error, before, after, rbehind, rahead, poff, i;
793 int bytecount, secmask;
795 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
796 ("%s does not support devices", __func__));
798 if (vp->v_iflag & VI_DOOMED)
799 return (VM_PAGER_BAD);
801 object = vp->v_object;
802 foff = IDX_TO_OFF(m[0]->pindex);
803 bsize = vp->v_mount->mnt_stat.f_iosize;
804 pagesperblock = bsize / PAGE_SIZE;
806 KASSERT(foff < object->un_pager.vnp.vnp_size,
807 ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
808 KASSERT(count <= nitems(bp->b_pages),
809 ("%s: requested %d pages", __func__, count));
812 * The last page has valid blocks. Invalid part can only
813 * exist at the end of file, and the page is made fully valid
814 * by zeroing in vm_pager_get_pages().
816 if (m[count - 1]->valid != 0 && --count == 0) {
818 iodone(arg, m, 1, 0);
819 return (VM_PAGER_OK);
822 bp = uma_zalloc(vnode_pbuf_zone, M_WAITOK);
825 * Get the underlying device blocks for the file with VOP_BMAP().
826 * If the file system doesn't support VOP_BMAP, use old way of
827 * getting pages via VOP_READ.
829 error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
830 if (error == EOPNOTSUPP) {
831 uma_zfree(vnode_pbuf_zone, bp);
832 VM_OBJECT_WLOCK(object);
833 for (i = 0; i < count; i++) {
834 VM_CNT_INC(v_vnodein);
835 VM_CNT_INC(v_vnodepgsin);
836 error = vnode_pager_input_old(object, m[i]);
840 VM_OBJECT_WUNLOCK(object);
842 } else if (error != 0) {
843 uma_zfree(vnode_pbuf_zone, bp);
844 return (VM_PAGER_ERROR);
848 * If the file system supports BMAP, but blocksize is smaller
849 * than a page size, then use special small filesystem code.
851 if (pagesperblock == 0) {
852 uma_zfree(vnode_pbuf_zone, bp);
853 for (i = 0; i < count; i++) {
854 VM_CNT_INC(v_vnodein);
855 VM_CNT_INC(v_vnodepgsin);
856 error = vnode_pager_input_smlfs(object, m[i]);
864 * A sparse file can be encountered only for a single page request,
865 * which may not be preceded by call to vm_pager_haspage().
867 if (bp->b_blkno == -1) {
869 ("%s: array[%d] request to a sparse file %p", __func__,
871 uma_zfree(vnode_pbuf_zone, bp);
872 pmap_zero_page(m[0]);
873 KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
875 VM_OBJECT_WLOCK(object);
876 m[0]->valid = VM_PAGE_BITS_ALL;
877 VM_OBJECT_WUNLOCK(object);
878 return (VM_PAGER_OK);
882 blkno0 = bp->b_blkno;
884 bp->b_blkno += (foff % bsize) / DEV_BSIZE;
886 /* Recalculate blocks available after/before to pages. */
887 poff = (foff % bsize) / PAGE_SIZE;
888 before *= pagesperblock;
890 after *= pagesperblock;
891 after += pagesperblock - (poff + 1);
892 if (m[0]->pindex + after >= object->size)
893 after = object->size - 1 - m[0]->pindex;
894 KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
895 __func__, count, after + 1));
898 /* Trim requested rbehind/rahead to possible values. */
899 rbehind = a_rbehind ? *a_rbehind : 0;
900 rahead = a_rahead ? *a_rahead : 0;
901 rbehind = min(rbehind, before);
902 rbehind = min(rbehind, m[0]->pindex);
903 rahead = min(rahead, after);
904 rahead = min(rahead, object->size - m[count - 1]->pindex);
906 * Check that total amount of pages fit into buf. Trim rbehind and
907 * rahead evenly if not.
909 if (rbehind + rahead + count > nitems(bp->b_pages)) {
912 trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
913 sum = rbehind + rahead;
914 if (rbehind == before) {
915 /* Roundup rbehind trim to block size. */
916 rbehind -= roundup(trim * rbehind / sum, pagesperblock);
920 rbehind -= trim * rbehind / sum;
921 rahead -= trim * rahead / sum;
923 KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
924 ("%s: behind %d ahead %d count %d", __func__,
925 rbehind, rahead, count));
928 * Fill in the bp->b_pages[] array with requested and optional
929 * read behind or read ahead pages. Read behind pages are looked
930 * up in a backward direction, down to a first cached page. Same
931 * for read ahead pages, but there is no need to shift the array
932 * in case of encountering a cached page.
934 i = bp->b_npages = 0;
936 vm_pindex_t startpindex, tpindex;
939 VM_OBJECT_WLOCK(object);
940 startpindex = m[0]->pindex - rbehind;
941 if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
942 p->pindex >= startpindex)
943 startpindex = p->pindex + 1;
945 /* tpindex is unsigned; beware of numeric underflow. */
946 for (tpindex = m[0]->pindex - 1;
947 tpindex >= startpindex && tpindex < m[0]->pindex;
949 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
951 /* Shift the array. */
952 for (int j = 0; j < i; j++)
953 bp->b_pages[j] = bp->b_pages[j +
954 tpindex + 1 - startpindex];
957 bp->b_pages[tpindex - startpindex] = p;
962 bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
966 /* Requested pages. */
967 for (int j = 0; j < count; j++, i++)
968 bp->b_pages[i] = m[j];
969 bp->b_npages += count;
972 vm_pindex_t endpindex, tpindex;
975 if (!VM_OBJECT_WOWNED(object))
976 VM_OBJECT_WLOCK(object);
977 endpindex = m[count - 1]->pindex + rahead + 1;
978 if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
979 p->pindex < endpindex)
980 endpindex = p->pindex;
981 if (endpindex > object->size)
982 endpindex = object->size;
984 for (tpindex = m[count - 1]->pindex + 1;
985 tpindex < endpindex; i++, tpindex++) {
986 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
992 bp->b_pgafter = i - bp->b_npages;
997 if (VM_OBJECT_WOWNED(object))
998 VM_OBJECT_WUNLOCK(object);
1000 /* Report back actual behind/ahead read. */
1002 *a_rbehind = bp->b_pgbefore;
1004 *a_rahead = bp->b_pgafter;
1007 KASSERT(bp->b_npages <= nitems(bp->b_pages),
1008 ("%s: buf %p overflowed", __func__, bp));
1009 for (int j = 1, prev = 0; j < bp->b_npages; j++) {
1010 if (bp->b_pages[j] == bogus_page)
1012 KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
1013 j - prev, ("%s: pages array not consecutive, bp %p",
1020 * Recalculate first offset and bytecount with regards to read behind.
1021 * Truncate bytecount to vnode real size and round up physical size
1024 foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1025 bytecount = bp->b_npages << PAGE_SHIFT;
1026 if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
1027 bytecount = object->un_pager.vnp.vnp_size - foff;
1028 secmask = bo->bo_bsize - 1;
1029 KASSERT(secmask < PAGE_SIZE && secmask > 0,
1030 ("%s: sector size %d too large", __func__, secmask + 1));
1031 bytecount = (bytecount + secmask) & ~secmask;
1034 * And map the pages to be read into the kva, if the filesystem
1035 * requires mapped buffers.
1037 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
1038 unmapped_buf_allowed) {
1039 bp->b_data = unmapped_buf;
1042 bp->b_data = bp->b_kvabase;
1043 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1046 /* Build a minimal buffer header. */
1047 bp->b_iocmd = BIO_READ;
1048 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1049 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1050 bp->b_rcred = crhold(curthread->td_ucred);
1051 bp->b_wcred = crhold(curthread->td_ucred);
1054 bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
1055 bp->b_iooffset = dbtob(bp->b_blkno);
1056 KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1057 (blkno0 - bp->b_blkno) * DEV_BSIZE +
1058 IDX_TO_OFF(m[0]->pindex) % bsize,
1059 ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1060 "blkno0 %ju b_blkno %ju", bsize,
1061 (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1062 (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
1064 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
1065 VM_CNT_INC(v_vnodein);
1066 VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1068 if (iodone != NULL) { /* async */
1069 bp->b_pgiodone = iodone;
1070 bp->b_caller1 = arg;
1071 bp->b_iodone = vnode_pager_generic_getpages_done_async;
1072 bp->b_flags |= B_ASYNC;
1075 return (VM_PAGER_OK);
1077 bp->b_iodone = bdone;
1079 bwait(bp, PVM, "vnread");
1080 error = vnode_pager_generic_getpages_done(bp);
1081 for (i = 0; i < bp->b_npages; i++)
1082 bp->b_pages[i] = NULL;
1085 uma_zfree(vnode_pbuf_zone, bp);
1086 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1091 vnode_pager_generic_getpages_done_async(struct buf *bp)
1095 error = vnode_pager_generic_getpages_done(bp);
1096 /* Run the iodone upon the requested range. */
1097 bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1098 bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
1099 for (int i = 0; i < bp->b_npages; i++)
1100 bp->b_pages[i] = NULL;
1103 uma_zfree(vnode_pbuf_zone, bp);
1107 vnode_pager_generic_getpages_done(struct buf *bp)
1110 off_t tfoff, nextoff;
1113 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1114 object = bp->b_vp->v_object;
1116 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1117 if (!buf_mapped(bp)) {
1118 bp->b_data = bp->b_kvabase;
1119 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
1122 bzero(bp->b_data + bp->b_bcount,
1123 PAGE_SIZE * bp->b_npages - bp->b_bcount);
1125 if (buf_mapped(bp)) {
1126 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1127 bp->b_data = unmapped_buf;
1130 VM_OBJECT_WLOCK(object);
1131 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1132 i < bp->b_npages; i++, tfoff = nextoff) {
1135 nextoff = tfoff + PAGE_SIZE;
1136 mt = bp->b_pages[i];
1138 if (nextoff <= object->un_pager.vnp.vnp_size) {
1140 * Read filled up entire page.
1142 mt->valid = VM_PAGE_BITS_ALL;
1143 KASSERT(mt->dirty == 0,
1144 ("%s: page %p is dirty", __func__, mt));
1145 KASSERT(!pmap_page_is_mapped(mt),
1146 ("%s: page %p is mapped", __func__, mt));
1149 * Read did not fill up entire page.
1151 * Currently we do not set the entire page valid,
1152 * we just try to clear the piece that we couldn't
1155 vm_page_set_valid_range(mt, 0,
1156 object->un_pager.vnp.vnp_size - tfoff);
1157 KASSERT((mt->dirty & vm_page_bits(0,
1158 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1159 ("%s: page %p is dirty", __func__, mt));
1162 if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1163 vm_page_readahead_finish(mt);
1165 VM_OBJECT_WUNLOCK(object);
1167 printf("%s: I/O read error %d\n", __func__, error);
1173 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1174 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1175 * vnode_pager_generic_putpages() to implement the previous behaviour.
1177 * All other FS's should use the bypass to get to the local media
1178 * backing vp's VOP_PUTPAGES.
1181 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1182 int flags, int *rtvals)
1186 int bytes = count * PAGE_SIZE;
1189 * Force synchronous operation if we are extremely low on memory
1190 * to prevent a low-memory deadlock. VOP operations often need to
1191 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1192 * operation ). The swapper handles the case by limiting the amount
1193 * of asynchronous I/O, but that sort of solution doesn't scale well
1194 * for the vnode pager without a lot of work.
1196 * Also, the backing vnode's iodone routine may not wake the pageout
1197 * daemon up. This should be probably be addressed XXX.
1200 if (vm_page_count_min())
1201 flags |= VM_PAGER_PUT_SYNC;
1204 * Call device-specific putpages function
1206 vp = object->handle;
1207 VM_OBJECT_WUNLOCK(object);
1208 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1209 KASSERT(rtval != EOPNOTSUPP,
1210 ("vnode_pager: stale FS putpages\n"));
1211 VM_OBJECT_WLOCK(object);
1215 vn_off2bidx(vm_ooffset_t offset)
1218 return ((offset & PAGE_MASK) / DEV_BSIZE);
1222 vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
1225 KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
1226 offset < IDX_TO_OFF(m->pindex + 1),
1227 ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
1228 (uintmax_t)offset));
1229 return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
1233 * This is now called from local media FS's to operate against their
1234 * own vnodes if they fail to implement VOP_PUTPAGES.
1236 * This is typically called indirectly via the pageout daemon and
1237 * clustering has already typically occurred, so in general we ask the
1238 * underlying filesystem to write the data out asynchronously rather
1242 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1243 int flags, int *rtvals)
1247 vm_ooffset_t maxblksz, next_offset, poffset, prev_offset;
1250 off_t prev_resid, wrsz;
1251 int count, error, i, maxsize, ncount, pgoff, ppscheck;
1253 static struct timeval lastfail;
1256 object = vp->v_object;
1257 count = bytecount / PAGE_SIZE;
1259 for (i = 0; i < count; i++)
1260 rtvals[i] = VM_PAGER_ERROR;
1262 if ((int64_t)ma[0]->pindex < 0) {
1263 printf("vnode_pager_generic_putpages: "
1264 "attempt to write meta-data 0x%jx(%lx)\n",
1265 (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1266 rtvals[0] = VM_PAGER_BAD;
1267 return (VM_PAGER_BAD);
1270 maxsize = count * PAGE_SIZE;
1273 poffset = IDX_TO_OFF(ma[0]->pindex);
1276 * If the page-aligned write is larger then the actual file we
1277 * have to invalidate pages occurring beyond the file EOF. However,
1278 * there is an edge case where a file may not be page-aligned where
1279 * the last page is partially invalid. In this case the filesystem
1280 * may not properly clear the dirty bits for the entire page (which
1281 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1282 * With the page locked we are free to fix-up the dirty bits here.
1284 * We do not under any circumstances truncate the valid bits, as
1285 * this will screw up bogus page replacement.
1287 VM_OBJECT_RLOCK(object);
1288 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1289 if (!VM_OBJECT_TRYUPGRADE(object)) {
1290 VM_OBJECT_RUNLOCK(object);
1291 VM_OBJECT_WLOCK(object);
1292 if (maxsize + poffset <= object->un_pager.vnp.vnp_size)
1295 if (object->un_pager.vnp.vnp_size > poffset) {
1296 maxsize = object->un_pager.vnp.vnp_size - poffset;
1297 ncount = btoc(maxsize);
1298 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1299 pgoff = roundup2(pgoff, DEV_BSIZE);
1302 * If the object is locked and the following
1303 * conditions hold, then the page's dirty
1304 * field cannot be concurrently changed by a
1308 vm_page_assert_sbusied(m);
1309 KASSERT(!pmap_page_is_write_mapped(m),
1310 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1311 MPASS(m->dirty != 0);
1312 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1319 for (i = ncount; i < count; i++)
1320 rtvals[i] = VM_PAGER_BAD;
1322 VM_OBJECT_LOCK_DOWNGRADE(object);
1325 auio.uio_iov = &aiov;
1326 auio.uio_segflg = UIO_NOCOPY;
1327 auio.uio_rw = UIO_WRITE;
1329 maxblksz = roundup2(poffset + maxsize, DEV_BSIZE);
1331 for (prev_offset = poffset; prev_offset < maxblksz;) {
1332 /* Skip clean blocks. */
1333 for (in_hole = true; in_hole && prev_offset < maxblksz;) {
1334 m = ma[OFF_TO_IDX(prev_offset - poffset)];
1335 for (i = vn_off2bidx(prev_offset);
1336 i < sizeof(vm_page_bits_t) * NBBY &&
1337 prev_offset < maxblksz; i++) {
1338 if (vn_dirty_blk(m, prev_offset)) {
1342 prev_offset += DEV_BSIZE;
1348 /* Find longest run of dirty blocks. */
1349 for (next_offset = prev_offset; next_offset < maxblksz;) {
1350 m = ma[OFF_TO_IDX(next_offset - poffset)];
1351 for (i = vn_off2bidx(next_offset);
1352 i < sizeof(vm_page_bits_t) * NBBY &&
1353 next_offset < maxblksz; i++) {
1354 if (!vn_dirty_blk(m, next_offset))
1356 next_offset += DEV_BSIZE;
1360 if (next_offset > poffset + maxsize)
1361 next_offset = poffset + maxsize;
1364 * Getting here requires finding a dirty block in the
1365 * 'skip clean blocks' loop.
1367 MPASS(prev_offset < next_offset);
1369 VM_OBJECT_RUNLOCK(object);
1370 aiov.iov_base = NULL;
1371 auio.uio_iovcnt = 1;
1372 auio.uio_offset = prev_offset;
1373 prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
1375 error = VOP_WRITE(vp, &auio,
1376 vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
1378 wrsz = prev_resid - auio.uio_resid;
1380 if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
1381 vn_printf(vp, "vnode_pager_putpages: "
1382 "zero-length write at %ju resid %zd\n",
1383 auio.uio_offset, auio.uio_resid);
1385 VM_OBJECT_RLOCK(object);
1389 /* Adjust the starting offset for next iteration. */
1390 prev_offset += wrsz;
1391 MPASS(auio.uio_offset == prev_offset);
1394 if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
1396 vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
1398 if (auio.uio_resid != 0 && (ppscheck != 0 ||
1399 ppsratecheck(&lastfail, &curfail, 1) != 0))
1400 vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
1401 "at %ju\n", auio.uio_resid,
1402 (uintmax_t)ma[0]->pindex);
1403 VM_OBJECT_RLOCK(object);
1404 if (error != 0 || auio.uio_resid != 0)
1408 /* Mark completely processed pages. */
1409 for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
1410 rtvals[i] = VM_PAGER_OK;
1411 /* Mark partial EOF page. */
1412 if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
1413 rtvals[i++] = VM_PAGER_OK;
1414 /* Unwritten pages in range, free bonus if the page is clean. */
1415 for (; i < ncount; i++)
1416 rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
1417 VM_OBJECT_RUNLOCK(object);
1418 VM_CNT_ADD(v_vnodepgsout, i);
1419 VM_CNT_INC(v_vnodeout);
1424 vnode_pager_putpages_ioflags(int pager_flags)
1429 * Pageouts are already clustered, use IO_ASYNC to force a
1430 * bawrite() rather then a bdwrite() to prevent paging I/O
1431 * from saturating the buffer cache. Dummy-up the sequential
1432 * heuristic to cause large ranges to cluster. If neither
1433 * IO_SYNC or IO_ASYNC is set, the system decides how to
1437 if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0)
1439 else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0)
1440 ioflags |= IO_ASYNC;
1441 ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0;
1442 ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0;
1443 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1448 * vnode_pager_undirty_pages().
1450 * A helper to mark pages as clean after pageout that was possibly
1451 * done with a short write. The lpos argument specifies the page run
1452 * length in bytes, and the written argument specifies how many bytes
1453 * were actually written. eof is the offset past the last valid byte
1454 * in the vnode using the absolute file position of the first byte in
1455 * the run as the base from which it is computed.
1458 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof,
1462 int i, pos, pos_devb;
1464 if (written == 0 && eof >= lpos)
1466 obj = ma[0]->object;
1467 VM_OBJECT_WLOCK(obj);
1468 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1469 if (pos < trunc_page(written)) {
1470 rtvals[i] = VM_PAGER_OK;
1471 vm_page_undirty(ma[i]);
1473 /* Partially written page. */
1474 rtvals[i] = VM_PAGER_AGAIN;
1475 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1478 if (eof >= lpos) /* avoid truncation */
1480 for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1481 if (pos != trunc_page(pos)) {
1483 * The page contains the last valid byte in
1484 * the vnode, mark the rest of the page as
1485 * clean, potentially making the whole page
1488 pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1489 vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1493 * If the page was cleaned, report the pageout
1494 * on it as successful. msync() no longer
1495 * needs to write out the page, endlessly
1496 * creating write requests and dirty buffers.
1498 if (ma[i]->dirty == 0)
1499 rtvals[i] = VM_PAGER_OK;
1501 pos = round_page(pos);
1503 /* vm_pageout_flush() clears dirty */
1504 rtvals[i] = VM_PAGER_BAD;
1509 VM_OBJECT_WUNLOCK(obj);
1513 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1517 vm_ooffset_t old_wm;
1519 VM_OBJECT_WLOCK(object);
1520 if (object->type != OBJT_VNODE) {
1521 VM_OBJECT_WUNLOCK(object);
1524 old_wm = object->un_pager.vnp.writemappings;
1525 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1526 vp = object->handle;
1527 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1528 ASSERT_VOP_LOCKED(vp, "v_writecount inc");
1529 VOP_ADD_WRITECOUNT_CHECKED(vp, 1);
1530 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1531 __func__, vp, vp->v_writecount);
1532 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1533 ASSERT_VOP_LOCKED(vp, "v_writecount dec");
1534 VOP_ADD_WRITECOUNT_CHECKED(vp, -1);
1535 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1536 __func__, vp, vp->v_writecount);
1538 VM_OBJECT_WUNLOCK(object);
1542 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1549 VM_OBJECT_WLOCK(object);
1552 * First, recheck the object type to account for the race when
1553 * the vnode is reclaimed.
1555 if (object->type != OBJT_VNODE) {
1556 VM_OBJECT_WUNLOCK(object);
1561 * Optimize for the case when writemappings is not going to
1565 if (object->un_pager.vnp.writemappings != inc) {
1566 object->un_pager.vnp.writemappings -= inc;
1567 VM_OBJECT_WUNLOCK(object);
1571 vp = object->handle;
1573 VM_OBJECT_WUNLOCK(object);
1575 vn_start_write(vp, &mp, V_WAIT);
1576 vn_lock(vp, LK_SHARED | LK_RETRY);
1579 * Decrement the object's writemappings, by swapping the start
1580 * and end arguments for vnode_pager_update_writecount(). If
1581 * there was not a race with vnode reclaimation, then the
1582 * vnode's v_writecount is decremented.
1584 vnode_pager_update_writecount(object, end, start);
1588 vn_finished_write(mp);