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/systm.h>
62 #include <sys/sysctl.h>
64 #include <sys/vnode.h>
65 #include <sys/mount.h>
68 #include <sys/vmmeter.h>
69 #include <sys/limits.h>
71 #include <sys/rwlock.h>
72 #include <sys/sf_buf.h>
73 #include <sys/domainset.h>
75 #include <machine/atomic.h>
78 #include <vm/vm_param.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_pager.h>
82 #include <vm/vm_map.h>
83 #include <vm/vnode_pager.h>
84 #include <vm/vm_extern.h>
86 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
87 daddr_t *rtaddress, int *run);
88 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
89 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
90 static void vnode_pager_dealloc(vm_object_t);
91 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
92 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
93 int *, vop_getpages_iodone_t, void *);
94 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
95 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
96 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
97 vm_ooffset_t, struct ucred *cred);
98 static int vnode_pager_generic_getpages_done(struct buf *);
99 static void vnode_pager_generic_getpages_done_async(struct buf *);
101 struct pagerops vnodepagerops = {
102 .pgo_alloc = vnode_pager_alloc,
103 .pgo_dealloc = vnode_pager_dealloc,
104 .pgo_getpages = vnode_pager_getpages,
105 .pgo_getpages_async = vnode_pager_getpages_async,
106 .pgo_putpages = vnode_pager_putpages,
107 .pgo_haspage = vnode_pager_haspage,
110 int vnode_pbuf_freecnt;
111 int vnode_async_pbuf_freecnt;
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");
119 /* Create the VM system backing object for this vnode */
121 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
124 vm_ooffset_t size = isize;
127 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
130 while ((object = vp->v_object) != NULL) {
131 VM_OBJECT_WLOCK(object);
132 if (!(object->flags & OBJ_DEAD)) {
133 VM_OBJECT_WUNLOCK(object);
137 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
138 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
139 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
143 if (vn_isdisk(vp, NULL)) {
144 size = IDX_TO_OFF(INT_MAX);
146 if (VOP_GETATTR(vp, &va, td->td_ucred))
152 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
154 * Dereference the reference we just created. This assumes
155 * that the object is associated with the vp.
157 VM_OBJECT_WLOCK(object);
159 VM_OBJECT_WUNLOCK(object);
162 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
168 vnode_destroy_vobject(struct vnode *vp)
170 struct vm_object *obj;
175 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
176 VM_OBJECT_WLOCK(obj);
177 umtx_shm_object_terminated(obj);
178 if (obj->ref_count == 0) {
180 * don't double-terminate the object
182 if ((obj->flags & OBJ_DEAD) == 0) {
183 vm_object_terminate(obj);
186 * Waiters were already handled during object
187 * termination. The exclusive vnode lock hopefully
188 * prevented new waiters from referencing the dying
191 KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0,
192 ("OBJ_DISCONNECTWNT set obj %p flags %x",
195 VM_OBJECT_WUNLOCK(obj);
199 * Woe to the process that tries to page now :-).
201 vm_pager_deallocate(obj);
202 VM_OBJECT_WUNLOCK(obj);
204 KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
209 * Allocate (or lookup) pager for a vnode.
210 * Handle is a vnode pointer.
215 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
216 vm_ooffset_t offset, struct ucred *cred)
222 * Pageout to vnode, no can do yet.
227 vp = (struct vnode *) handle;
230 * If the object is being terminated, wait for it to
234 while ((object = vp->v_object) != NULL) {
235 VM_OBJECT_WLOCK(object);
236 if ((object->flags & OBJ_DEAD) == 0)
238 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
239 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
242 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
244 if (object == NULL) {
246 * Add an object of the appropriate size
248 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
250 object->un_pager.vnp.vnp_size = size;
251 object->un_pager.vnp.writemappings = 0;
252 object->domain.dr_policy = vnode_domainset;
254 object->handle = handle;
256 if (vp->v_object != NULL) {
258 * Object has been created while we were sleeping
261 VM_OBJECT_WLOCK(object);
262 KASSERT(object->ref_count == 1,
263 ("leaked ref %p %d", object, object->ref_count));
264 object->type = OBJT_DEAD;
265 object->ref_count = 0;
266 VM_OBJECT_WUNLOCK(object);
267 vm_object_destroy(object);
270 vp->v_object = object;
274 #if VM_NRESERVLEVEL > 0
275 vm_object_color(object, 0);
277 VM_OBJECT_WUNLOCK(object);
284 * The object must be locked.
287 vnode_pager_dealloc(vm_object_t object)
294 panic("vnode_pager_dealloc: pager already dealloced");
296 VM_OBJECT_ASSERT_WLOCKED(object);
297 vm_object_pip_wait(object, "vnpdea");
298 refs = object->ref_count;
300 object->handle = NULL;
301 object->type = OBJT_DEAD;
302 if (object->flags & OBJ_DISCONNECTWNT) {
303 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
306 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
307 if (object->un_pager.vnp.writemappings > 0) {
308 object->un_pager.vnp.writemappings = 0;
309 VOP_ADD_WRITECOUNT(vp, -1);
310 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
311 __func__, vp, vp->v_writecount);
315 VM_OBJECT_WUNLOCK(object);
318 VM_OBJECT_WLOCK(object);
322 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
325 struct vnode *vp = object->handle;
331 int pagesperblock, blocksperpage;
333 VM_OBJECT_ASSERT_WLOCKED(object);
335 * If no vp or vp is doomed or marked transparent to VM, we do not
338 if (vp == NULL || vp->v_iflag & VI_DOOMED)
341 * If the offset is beyond end of file we do
344 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
347 bsize = vp->v_mount->mnt_stat.f_iosize;
348 pagesperblock = bsize / PAGE_SIZE;
350 if (pagesperblock > 0) {
351 reqblock = pindex / pagesperblock;
353 blocksperpage = (PAGE_SIZE / bsize);
354 reqblock = pindex * blocksperpage;
356 VM_OBJECT_WUNLOCK(object);
357 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
358 VM_OBJECT_WLOCK(object);
363 if (pagesperblock > 0) {
364 poff = pindex - (reqblock * pagesperblock);
366 *before *= pagesperblock;
371 * The BMAP vop can report a partial block in the
372 * 'after', but must not report blocks after EOF.
373 * Assert the latter, and truncate 'after' in case
376 KASSERT((reqblock + *after) * pagesperblock <
377 roundup2(object->size, pagesperblock),
378 ("%s: reqblock %jd after %d size %ju", __func__,
379 (intmax_t )reqblock, *after,
380 (uintmax_t )object->size));
381 *after *= pagesperblock;
382 *after += pagesperblock - (poff + 1);
383 if (pindex + *after >= object->size)
384 *after = object->size - 1 - pindex;
388 *before /= blocksperpage;
392 *after /= blocksperpage;
399 * Lets the VM system know about a change in size for a file.
400 * We adjust our own internal size and flush any cached pages in
401 * the associated object that are affected by the size change.
403 * Note: this routine may be invoked as a result of a pager put
404 * operation (possibly at object termination time), so we must be careful.
407 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
411 vm_pindex_t nobjsize;
413 if ((object = vp->v_object) == NULL)
415 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
416 VM_OBJECT_WLOCK(object);
417 if (object->type == OBJT_DEAD) {
418 VM_OBJECT_WUNLOCK(object);
421 KASSERT(object->type == OBJT_VNODE,
422 ("not vnode-backed object %p", object));
423 if (nsize == object->un_pager.vnp.vnp_size) {
425 * Hasn't changed size
427 VM_OBJECT_WUNLOCK(object);
430 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
431 if (nsize < object->un_pager.vnp.vnp_size) {
433 * File has shrunk. Toss any cached pages beyond the new EOF.
435 if (nobjsize < object->size)
436 vm_object_page_remove(object, nobjsize, object->size,
439 * this gets rid of garbage at the end of a page that is now
440 * only partially backed by the vnode.
442 * XXX for some reason (I don't know yet), if we take a
443 * completely invalid page and mark it partially valid
444 * it can screw up NFS reads, so we don't allow the case.
446 if ((nsize & PAGE_MASK) &&
447 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
449 int base = (int)nsize & PAGE_MASK;
450 int size = PAGE_SIZE - base;
453 * Clear out partial-page garbage in case
454 * the page has been mapped.
456 pmap_zero_page_area(m, base, size);
459 * Update the valid bits to reflect the blocks that
460 * have been zeroed. Some of these valid bits may
461 * have already been set.
463 vm_page_set_valid_range(m, base, size);
466 * Round "base" to the next block boundary so that the
467 * dirty bit for a partially zeroed block is not
470 base = roundup2(base, DEV_BSIZE);
473 * Clear out partial-page dirty bits.
475 * note that we do not clear out the valid
476 * bits. This would prevent bogus_page
477 * replacement from working properly.
479 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
482 object->un_pager.vnp.vnp_size = nsize;
483 object->size = nobjsize;
484 VM_OBJECT_WUNLOCK(object);
488 * calculate the linear (byte) disk address of specified virtual
492 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
503 if (vp->v_iflag & VI_DOOMED)
506 bsize = vp->v_mount->mnt_stat.f_iosize;
507 vblock = address / bsize;
508 voffset = address % bsize;
510 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
512 if (*rtaddress != -1)
513 *rtaddress += voffset / DEV_BSIZE;
516 *run *= bsize/PAGE_SIZE;
517 *run -= voffset/PAGE_SIZE;
525 * small block filesystem vnode pager input
528 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
541 if (vp->v_iflag & VI_DOOMED)
544 bsize = vp->v_mount->mnt_stat.f_iosize;
546 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
548 sf = sf_buf_alloc(m, 0);
550 for (i = 0; i < PAGE_SIZE / bsize; i++) {
551 vm_ooffset_t address;
553 bits = vm_page_bits(i * bsize, bsize);
557 address = IDX_TO_OFF(m->pindex) + i * bsize;
558 if (address >= object->un_pager.vnp.vnp_size) {
561 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
565 if (fileaddr != -1) {
566 bp = getpbuf(&vnode_pbuf_freecnt);
568 /* build a minimal buffer header */
569 bp->b_iocmd = BIO_READ;
570 bp->b_iodone = bdone;
571 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
572 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
573 bp->b_rcred = crhold(curthread->td_ucred);
574 bp->b_wcred = crhold(curthread->td_ucred);
575 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
576 bp->b_blkno = fileaddr;
579 bp->b_bcount = bsize;
580 bp->b_bufsize = bsize;
581 bp->b_runningbufspace = bp->b_bufsize;
582 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
585 bp->b_iooffset = dbtob(bp->b_blkno);
588 bwait(bp, PVM, "vnsrd");
590 if ((bp->b_ioflags & BIO_ERROR) != 0)
594 * free the buffer header back to the swap buffer pool
598 relpbuf(bp, &vnode_pbuf_freecnt);
602 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
603 KASSERT((m->dirty & bits) == 0,
604 ("vnode_pager_input_smlfs: page %p is dirty", m));
605 VM_OBJECT_WLOCK(object);
607 VM_OBJECT_WUNLOCK(object);
611 return VM_PAGER_ERROR;
617 * old style vnode pager input routine
620 vnode_pager_input_old(vm_object_t object, vm_page_t m)
629 VM_OBJECT_ASSERT_WLOCKED(object);
633 * Return failure if beyond current EOF
635 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
639 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
640 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
642 VM_OBJECT_WUNLOCK(object);
645 * Allocate a kernel virtual address and initialize so that
646 * we can use VOP_READ/WRITE routines.
648 sf = sf_buf_alloc(m, 0);
650 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
652 auio.uio_iov = &aiov;
654 auio.uio_offset = IDX_TO_OFF(m->pindex);
655 auio.uio_segflg = UIO_SYSSPACE;
656 auio.uio_rw = UIO_READ;
657 auio.uio_resid = size;
658 auio.uio_td = curthread;
660 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
662 int count = size - auio.uio_resid;
666 else if (count != PAGE_SIZE)
667 bzero((caddr_t)sf_buf_kva(sf) + count,
672 VM_OBJECT_WLOCK(object);
674 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
676 m->valid = VM_PAGE_BITS_ALL;
677 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
681 * generic vnode pager input routine
685 * Local media VFS's that do not implement their own VOP_GETPAGES
686 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
687 * to implement the previous behaviour.
689 * All other FS's should use the bypass to get to the local media
690 * backing vp's VOP_GETPAGES.
693 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
700 VM_OBJECT_WUNLOCK(object);
701 rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
702 KASSERT(rtval != EOPNOTSUPP,
703 ("vnode_pager: FS getpages not implemented\n"));
704 VM_OBJECT_WLOCK(object);
709 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
710 int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
716 VM_OBJECT_WUNLOCK(object);
717 rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
718 KASSERT(rtval != EOPNOTSUPP,
719 ("vnode_pager: FS getpages_async not implemented\n"));
720 VM_OBJECT_WLOCK(object);
725 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
726 * local filesystems, where partially valid pages can only occur at
730 vnode_pager_local_getpages(struct vop_getpages_args *ap)
733 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
734 ap->a_rbehind, ap->a_rahead, NULL, NULL));
738 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
741 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
742 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
746 * This is now called from local media FS's to operate against their
747 * own vnodes if they fail to implement VOP_GETPAGES.
750 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
751 int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
760 int bsize, pagesperblock, *freecnt;
761 int error, before, after, rbehind, rahead, poff, i;
762 int bytecount, secmask;
764 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
765 ("%s does not support devices", __func__));
767 if (vp->v_iflag & VI_DOOMED)
768 return (VM_PAGER_BAD);
770 object = vp->v_object;
771 foff = IDX_TO_OFF(m[0]->pindex);
772 bsize = vp->v_mount->mnt_stat.f_iosize;
773 pagesperblock = bsize / PAGE_SIZE;
775 KASSERT(foff < object->un_pager.vnp.vnp_size,
776 ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
777 KASSERT(count <= sizeof(bp->b_pages),
778 ("%s: requested %d pages", __func__, count));
781 * The last page has valid blocks. Invalid part can only
782 * exist at the end of file, and the page is made fully valid
783 * by zeroing in vm_pager_get_pages().
785 if (m[count - 1]->valid != 0 && --count == 0) {
787 iodone(arg, m, 1, 0);
788 return (VM_PAGER_OK);
792 * Synchronous and asynchronous paging operations use different
793 * free pbuf counters. This is done to avoid asynchronous requests
794 * to consume all pbufs.
795 * Allocate the pbuf at the very beginning of the function, so that
796 * if we are low on certain kind of pbufs don't even proceed to BMAP,
799 freecnt = iodone != NULL ?
800 &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
801 bp = getpbuf(freecnt);
804 * Get the underlying device blocks for the file with VOP_BMAP().
805 * If the file system doesn't support VOP_BMAP, use old way of
806 * getting pages via VOP_READ.
808 error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
809 if (error == EOPNOTSUPP) {
810 relpbuf(bp, freecnt);
811 VM_OBJECT_WLOCK(object);
812 for (i = 0; i < count; i++) {
813 VM_CNT_INC(v_vnodein);
814 VM_CNT_INC(v_vnodepgsin);
815 error = vnode_pager_input_old(object, m[i]);
819 VM_OBJECT_WUNLOCK(object);
821 } else if (error != 0) {
822 relpbuf(bp, freecnt);
823 return (VM_PAGER_ERROR);
827 * If the file system supports BMAP, but blocksize is smaller
828 * than a page size, then use special small filesystem code.
830 if (pagesperblock == 0) {
831 relpbuf(bp, freecnt);
832 for (i = 0; i < count; i++) {
833 VM_CNT_INC(v_vnodein);
834 VM_CNT_INC(v_vnodepgsin);
835 error = vnode_pager_input_smlfs(object, m[i]);
843 * A sparse file can be encountered only for a single page request,
844 * which may not be preceded by call to vm_pager_haspage().
846 if (bp->b_blkno == -1) {
848 ("%s: array[%d] request to a sparse file %p", __func__,
850 relpbuf(bp, freecnt);
851 pmap_zero_page(m[0]);
852 KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
854 VM_OBJECT_WLOCK(object);
855 m[0]->valid = VM_PAGE_BITS_ALL;
856 VM_OBJECT_WUNLOCK(object);
857 return (VM_PAGER_OK);
861 blkno0 = bp->b_blkno;
863 bp->b_blkno += (foff % bsize) / DEV_BSIZE;
865 /* Recalculate blocks available after/before to pages. */
866 poff = (foff % bsize) / PAGE_SIZE;
867 before *= pagesperblock;
869 after *= pagesperblock;
870 after += pagesperblock - (poff + 1);
871 if (m[0]->pindex + after >= object->size)
872 after = object->size - 1 - m[0]->pindex;
873 KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
874 __func__, count, after + 1));
877 /* Trim requested rbehind/rahead to possible values. */
878 rbehind = a_rbehind ? *a_rbehind : 0;
879 rahead = a_rahead ? *a_rahead : 0;
880 rbehind = min(rbehind, before);
881 rbehind = min(rbehind, m[0]->pindex);
882 rahead = min(rahead, after);
883 rahead = min(rahead, object->size - m[count - 1]->pindex);
885 * Check that total amount of pages fit into buf. Trim rbehind and
886 * rahead evenly if not.
888 if (rbehind + rahead + count > nitems(bp->b_pages)) {
891 trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
892 sum = rbehind + rahead;
893 if (rbehind == before) {
894 /* Roundup rbehind trim to block size. */
895 rbehind -= roundup(trim * rbehind / sum, pagesperblock);
899 rbehind -= trim * rbehind / sum;
900 rahead -= trim * rahead / sum;
902 KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
903 ("%s: behind %d ahead %d count %d", __func__,
904 rbehind, rahead, count));
907 * Fill in the bp->b_pages[] array with requested and optional
908 * read behind or read ahead pages. Read behind pages are looked
909 * up in a backward direction, down to a first cached page. Same
910 * for read ahead pages, but there is no need to shift the array
911 * in case of encountering a cached page.
913 i = bp->b_npages = 0;
915 vm_pindex_t startpindex, tpindex;
918 VM_OBJECT_WLOCK(object);
919 startpindex = m[0]->pindex - rbehind;
920 if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
921 p->pindex >= startpindex)
922 startpindex = p->pindex + 1;
924 /* tpindex is unsigned; beware of numeric underflow. */
925 for (tpindex = m[0]->pindex - 1;
926 tpindex >= startpindex && tpindex < m[0]->pindex;
928 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
930 /* Shift the array. */
931 for (int j = 0; j < i; j++)
932 bp->b_pages[j] = bp->b_pages[j +
933 tpindex + 1 - startpindex];
936 bp->b_pages[tpindex - startpindex] = p;
941 bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
945 /* Requested pages. */
946 for (int j = 0; j < count; j++, i++)
947 bp->b_pages[i] = m[j];
948 bp->b_npages += count;
951 vm_pindex_t endpindex, tpindex;
954 if (!VM_OBJECT_WOWNED(object))
955 VM_OBJECT_WLOCK(object);
956 endpindex = m[count - 1]->pindex + rahead + 1;
957 if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
958 p->pindex < endpindex)
959 endpindex = p->pindex;
960 if (endpindex > object->size)
961 endpindex = object->size;
963 for (tpindex = m[count - 1]->pindex + 1;
964 tpindex < endpindex; i++, tpindex++) {
965 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
971 bp->b_pgafter = i - bp->b_npages;
976 if (VM_OBJECT_WOWNED(object))
977 VM_OBJECT_WUNLOCK(object);
979 /* Report back actual behind/ahead read. */
981 *a_rbehind = bp->b_pgbefore;
983 *a_rahead = bp->b_pgafter;
986 KASSERT(bp->b_npages <= nitems(bp->b_pages),
987 ("%s: buf %p overflowed", __func__, bp));
988 for (int j = 1, prev = 0; j < bp->b_npages; j++) {
989 if (bp->b_pages[j] == bogus_page)
991 KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
992 j - prev, ("%s: pages array not consecutive, bp %p",
999 * Recalculate first offset and bytecount with regards to read behind.
1000 * Truncate bytecount to vnode real size and round up physical size
1003 foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1004 bytecount = bp->b_npages << PAGE_SHIFT;
1005 if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
1006 bytecount = object->un_pager.vnp.vnp_size - foff;
1007 secmask = bo->bo_bsize - 1;
1008 KASSERT(secmask < PAGE_SIZE && secmask > 0,
1009 ("%s: sector size %d too large", __func__, secmask + 1));
1010 bytecount = (bytecount + secmask) & ~secmask;
1013 * And map the pages to be read into the kva, if the filesystem
1014 * requires mapped buffers.
1016 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
1017 unmapped_buf_allowed) {
1018 bp->b_data = unmapped_buf;
1021 bp->b_data = bp->b_kvabase;
1022 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1025 /* Build a minimal buffer header. */
1026 bp->b_iocmd = BIO_READ;
1027 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1028 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1029 bp->b_rcred = crhold(curthread->td_ucred);
1030 bp->b_wcred = crhold(curthread->td_ucred);
1033 bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
1034 bp->b_iooffset = dbtob(bp->b_blkno);
1035 KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1036 (blkno0 - bp->b_blkno) * DEV_BSIZE +
1037 IDX_TO_OFF(m[0]->pindex) % bsize,
1038 ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1039 "blkno0 %ju b_blkno %ju", bsize,
1040 (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1041 (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
1043 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
1044 VM_CNT_INC(v_vnodein);
1045 VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1047 if (iodone != NULL) { /* async */
1048 bp->b_pgiodone = iodone;
1049 bp->b_caller1 = arg;
1050 bp->b_iodone = vnode_pager_generic_getpages_done_async;
1051 bp->b_flags |= B_ASYNC;
1054 return (VM_PAGER_OK);
1056 bp->b_iodone = bdone;
1058 bwait(bp, PVM, "vnread");
1059 error = vnode_pager_generic_getpages_done(bp);
1060 for (i = 0; i < bp->b_npages; i++)
1061 bp->b_pages[i] = NULL;
1064 relpbuf(bp, &vnode_pbuf_freecnt);
1065 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1070 vnode_pager_generic_getpages_done_async(struct buf *bp)
1074 error = vnode_pager_generic_getpages_done(bp);
1075 /* Run the iodone upon the requested range. */
1076 bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1077 bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
1078 for (int i = 0; i < bp->b_npages; i++)
1079 bp->b_pages[i] = NULL;
1082 relpbuf(bp, &vnode_async_pbuf_freecnt);
1086 vnode_pager_generic_getpages_done(struct buf *bp)
1089 off_t tfoff, nextoff;
1092 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1093 object = bp->b_vp->v_object;
1095 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1096 if (!buf_mapped(bp)) {
1097 bp->b_data = bp->b_kvabase;
1098 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
1101 bzero(bp->b_data + bp->b_bcount,
1102 PAGE_SIZE * bp->b_npages - bp->b_bcount);
1104 if (buf_mapped(bp)) {
1105 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1106 bp->b_data = unmapped_buf;
1109 VM_OBJECT_WLOCK(object);
1110 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1111 i < bp->b_npages; i++, tfoff = nextoff) {
1114 nextoff = tfoff + PAGE_SIZE;
1115 mt = bp->b_pages[i];
1117 if (nextoff <= object->un_pager.vnp.vnp_size) {
1119 * Read filled up entire page.
1121 mt->valid = VM_PAGE_BITS_ALL;
1122 KASSERT(mt->dirty == 0,
1123 ("%s: page %p is dirty", __func__, mt));
1124 KASSERT(!pmap_page_is_mapped(mt),
1125 ("%s: page %p is mapped", __func__, mt));
1128 * Read did not fill up entire page.
1130 * Currently we do not set the entire page valid,
1131 * we just try to clear the piece that we couldn't
1134 vm_page_set_valid_range(mt, 0,
1135 object->un_pager.vnp.vnp_size - tfoff);
1136 KASSERT((mt->dirty & vm_page_bits(0,
1137 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1138 ("%s: page %p is dirty", __func__, mt));
1141 if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1142 vm_page_readahead_finish(mt);
1144 VM_OBJECT_WUNLOCK(object);
1146 printf("%s: I/O read error %d\n", __func__, error);
1152 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1153 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1154 * vnode_pager_generic_putpages() to implement the previous behaviour.
1156 * All other FS's should use the bypass to get to the local media
1157 * backing vp's VOP_PUTPAGES.
1160 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1161 int flags, int *rtvals)
1165 int bytes = count * PAGE_SIZE;
1168 * Force synchronous operation if we are extremely low on memory
1169 * to prevent a low-memory deadlock. VOP operations often need to
1170 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1171 * operation ). The swapper handles the case by limiting the amount
1172 * of asynchronous I/O, but that sort of solution doesn't scale well
1173 * for the vnode pager without a lot of work.
1175 * Also, the backing vnode's iodone routine may not wake the pageout
1176 * daemon up. This should be probably be addressed XXX.
1179 if (vm_page_count_min())
1180 flags |= VM_PAGER_PUT_SYNC;
1183 * Call device-specific putpages function
1185 vp = object->handle;
1186 VM_OBJECT_WUNLOCK(object);
1187 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1188 KASSERT(rtval != EOPNOTSUPP,
1189 ("vnode_pager: stale FS putpages\n"));
1190 VM_OBJECT_WLOCK(object);
1194 vn_off2bidx(vm_ooffset_t offset)
1197 return ((offset & PAGE_MASK) / DEV_BSIZE);
1201 vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
1204 KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
1205 offset < IDX_TO_OFF(m->pindex + 1),
1206 ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
1207 (uintmax_t)offset));
1208 return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
1212 * This is now called from local media FS's to operate against their
1213 * own vnodes if they fail to implement VOP_PUTPAGES.
1215 * This is typically called indirectly via the pageout daemon and
1216 * clustering has already typically occurred, so in general we ask the
1217 * underlying filesystem to write the data out asynchronously rather
1221 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1222 int flags, int *rtvals)
1226 vm_ooffset_t maxblksz, next_offset, poffset, prev_offset;
1229 off_t prev_resid, wrsz;
1230 int count, error, i, maxsize, ncount, pgoff, ppscheck;
1232 static struct timeval lastfail;
1235 object = vp->v_object;
1236 count = bytecount / PAGE_SIZE;
1238 for (i = 0; i < count; i++)
1239 rtvals[i] = VM_PAGER_ERROR;
1241 if ((int64_t)ma[0]->pindex < 0) {
1242 printf("vnode_pager_generic_putpages: "
1243 "attempt to write meta-data 0x%jx(%lx)\n",
1244 (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1245 rtvals[0] = VM_PAGER_BAD;
1246 return (VM_PAGER_BAD);
1249 maxsize = count * PAGE_SIZE;
1252 poffset = IDX_TO_OFF(ma[0]->pindex);
1255 * If the page-aligned write is larger then the actual file we
1256 * have to invalidate pages occurring beyond the file EOF. However,
1257 * there is an edge case where a file may not be page-aligned where
1258 * the last page is partially invalid. In this case the filesystem
1259 * may not properly clear the dirty bits for the entire page (which
1260 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1261 * With the page locked we are free to fix-up the dirty bits here.
1263 * We do not under any circumstances truncate the valid bits, as
1264 * this will screw up bogus page replacement.
1266 VM_OBJECT_RLOCK(object);
1267 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1268 if (!VM_OBJECT_TRYUPGRADE(object)) {
1269 VM_OBJECT_RUNLOCK(object);
1270 VM_OBJECT_WLOCK(object);
1271 if (maxsize + poffset <= object->un_pager.vnp.vnp_size)
1274 if (object->un_pager.vnp.vnp_size > poffset) {
1275 maxsize = object->un_pager.vnp.vnp_size - poffset;
1276 ncount = btoc(maxsize);
1277 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1278 pgoff = roundup2(pgoff, DEV_BSIZE);
1281 * If the object is locked and the following
1282 * conditions hold, then the page's dirty
1283 * field cannot be concurrently changed by a
1287 vm_page_assert_sbusied(m);
1288 KASSERT(!pmap_page_is_write_mapped(m),
1289 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1290 MPASS(m->dirty != 0);
1291 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1298 for (i = ncount; i < count; i++)
1299 rtvals[i] = VM_PAGER_BAD;
1301 VM_OBJECT_LOCK_DOWNGRADE(object);
1304 auio.uio_iov = &aiov;
1305 auio.uio_segflg = UIO_NOCOPY;
1306 auio.uio_rw = UIO_WRITE;
1308 maxblksz = roundup2(poffset + maxsize, DEV_BSIZE);
1310 for (prev_offset = poffset; prev_offset < maxblksz;) {
1311 /* Skip clean blocks. */
1312 for (in_hole = true; in_hole && prev_offset < maxblksz;) {
1313 m = ma[OFF_TO_IDX(prev_offset - poffset)];
1314 for (i = vn_off2bidx(prev_offset);
1315 i < sizeof(vm_page_bits_t) * NBBY &&
1316 prev_offset < maxblksz; i++) {
1317 if (vn_dirty_blk(m, prev_offset)) {
1321 prev_offset += DEV_BSIZE;
1327 /* Find longest run of dirty blocks. */
1328 for (next_offset = prev_offset; next_offset < maxblksz;) {
1329 m = ma[OFF_TO_IDX(next_offset - poffset)];
1330 for (i = vn_off2bidx(next_offset);
1331 i < sizeof(vm_page_bits_t) * NBBY &&
1332 next_offset < maxblksz; i++) {
1333 if (!vn_dirty_blk(m, next_offset))
1335 next_offset += DEV_BSIZE;
1339 if (next_offset > poffset + maxsize)
1340 next_offset = poffset + maxsize;
1343 * Getting here requires finding a dirty block in the
1344 * 'skip clean blocks' loop.
1346 MPASS(prev_offset < next_offset);
1348 VM_OBJECT_RUNLOCK(object);
1349 aiov.iov_base = NULL;
1350 auio.uio_iovcnt = 1;
1351 auio.uio_offset = prev_offset;
1352 prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
1354 error = VOP_WRITE(vp, &auio,
1355 vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
1357 wrsz = prev_resid - auio.uio_resid;
1359 if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
1360 vn_printf(vp, "vnode_pager_putpages: "
1361 "zero-length write at %ju resid %zd\n",
1362 auio.uio_offset, auio.uio_resid);
1364 VM_OBJECT_RLOCK(object);
1368 /* Adjust the starting offset for next iteration. */
1369 prev_offset += wrsz;
1370 MPASS(auio.uio_offset == prev_offset);
1373 if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
1375 vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
1377 if (auio.uio_resid != 0 && (ppscheck != 0 ||
1378 ppsratecheck(&lastfail, &curfail, 1) != 0))
1379 vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
1380 "at %ju\n", auio.uio_resid,
1381 (uintmax_t)ma[0]->pindex);
1382 VM_OBJECT_RLOCK(object);
1383 if (error != 0 || auio.uio_resid != 0)
1387 /* Mark completely processed pages. */
1388 for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
1389 rtvals[i] = VM_PAGER_OK;
1390 /* Mark partial EOF page. */
1391 if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
1392 rtvals[i++] = VM_PAGER_OK;
1393 /* Unwritten pages in range, free bonus if the page is clean. */
1394 for (; i < ncount; i++)
1395 rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
1396 VM_OBJECT_RUNLOCK(object);
1397 VM_CNT_ADD(v_vnodepgsout, i);
1398 VM_CNT_INC(v_vnodeout);
1403 vnode_pager_putpages_ioflags(int pager_flags)
1408 * Pageouts are already clustered, use IO_ASYNC to force a
1409 * bawrite() rather then a bdwrite() to prevent paging I/O
1410 * from saturating the buffer cache. Dummy-up the sequential
1411 * heuristic to cause large ranges to cluster. If neither
1412 * IO_SYNC or IO_ASYNC is set, the system decides how to
1416 if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0)
1418 else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0)
1419 ioflags |= IO_ASYNC;
1420 ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0;
1421 ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0;
1422 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1427 * vnode_pager_undirty_pages().
1429 * A helper to mark pages as clean after pageout that was possibly
1430 * done with a short write. The lpos argument specifies the page run
1431 * length in bytes, and the written argument specifies how many bytes
1432 * were actually written. eof is the offset past the last valid byte
1433 * in the vnode using the absolute file position of the first byte in
1434 * the run as the base from which it is computed.
1437 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof,
1441 int i, pos, pos_devb;
1443 if (written == 0 && eof >= lpos)
1445 obj = ma[0]->object;
1446 VM_OBJECT_WLOCK(obj);
1447 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1448 if (pos < trunc_page(written)) {
1449 rtvals[i] = VM_PAGER_OK;
1450 vm_page_undirty(ma[i]);
1452 /* Partially written page. */
1453 rtvals[i] = VM_PAGER_AGAIN;
1454 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1457 if (eof >= lpos) /* avoid truncation */
1459 for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1460 if (pos != trunc_page(pos)) {
1462 * The page contains the last valid byte in
1463 * the vnode, mark the rest of the page as
1464 * clean, potentially making the whole page
1467 pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1468 vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1472 * If the page was cleaned, report the pageout
1473 * on it as successful. msync() no longer
1474 * needs to write out the page, endlessly
1475 * creating write requests and dirty buffers.
1477 if (ma[i]->dirty == 0)
1478 rtvals[i] = VM_PAGER_OK;
1480 pos = round_page(pos);
1482 /* vm_pageout_flush() clears dirty */
1483 rtvals[i] = VM_PAGER_BAD;
1488 VM_OBJECT_WUNLOCK(obj);
1492 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1496 vm_ooffset_t old_wm;
1498 VM_OBJECT_WLOCK(object);
1499 if (object->type != OBJT_VNODE) {
1500 VM_OBJECT_WUNLOCK(object);
1503 old_wm = object->un_pager.vnp.writemappings;
1504 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1505 vp = object->handle;
1506 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1507 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1508 VOP_ADD_WRITECOUNT(vp, 1);
1509 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1510 __func__, vp, vp->v_writecount);
1511 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1512 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1513 VOP_ADD_WRITECOUNT(vp, -1);
1514 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1515 __func__, vp, vp->v_writecount);
1517 VM_OBJECT_WUNLOCK(object);
1521 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1528 VM_OBJECT_WLOCK(object);
1531 * First, recheck the object type to account for the race when
1532 * the vnode is reclaimed.
1534 if (object->type != OBJT_VNODE) {
1535 VM_OBJECT_WUNLOCK(object);
1540 * Optimize for the case when writemappings is not going to
1544 if (object->un_pager.vnp.writemappings != inc) {
1545 object->un_pager.vnp.writemappings -= inc;
1546 VM_OBJECT_WUNLOCK(object);
1550 vp = object->handle;
1552 VM_OBJECT_WUNLOCK(object);
1554 vn_start_write(vp, &mp, V_WAIT);
1555 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1558 * Decrement the object's writemappings, by swapping the start
1559 * and end arguments for vnode_pager_update_writecount(). If
1560 * there was not a race with vnode reclaimation, then the
1561 * vnode's v_writecount is decremented.
1563 vnode_pager_update_writecount(object, end, start);
1567 vn_finished_write(mp);