2 * Copyright (c) 1990 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
5 * Copyright (c) 1993, 1994 John S. Dyson
6 * Copyright (c) 1995, David Greenman
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * from: @(#)vnode_pager.c 7.5 (Berkeley) 4/20/91
44 * Page to/from files (vnodes).
49 * Implement VOP_GETPAGES/PUTPAGES interface for filesystems. Will
50 * greatly re-simplify the vnode_pager.
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
58 #include <sys/param.h>
59 #include <sys/systm.h>
61 #include <sys/vnode.h>
62 #include <sys/mount.h>
65 #include <sys/vmmeter.h>
66 #include <sys/limits.h>
68 #include <sys/rwlock.h>
69 #include <sys/sf_buf.h>
71 #include <machine/atomic.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_map.h>
79 #include <vm/vnode_pager.h>
80 #include <vm/vm_extern.h>
82 static int vnode_pager_addr(struct vnode *vp, vm_ooffset_t address,
83 daddr_t *rtaddress, int *run);
84 static int vnode_pager_input_smlfs(vm_object_t object, vm_page_t m);
85 static int vnode_pager_input_old(vm_object_t object, vm_page_t m);
86 static void vnode_pager_dealloc(vm_object_t);
87 static int vnode_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
88 static int vnode_pager_getpages_async(vm_object_t, vm_page_t *, int, int *,
89 int *, vop_getpages_iodone_t, void *);
90 static void vnode_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
91 static boolean_t vnode_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
92 static vm_object_t vnode_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
93 vm_ooffset_t, struct ucred *cred);
94 static int vnode_pager_generic_getpages_done(struct buf *);
95 static void vnode_pager_generic_getpages_done_async(struct buf *);
97 struct pagerops vnodepagerops = {
98 .pgo_alloc = vnode_pager_alloc,
99 .pgo_dealloc = vnode_pager_dealloc,
100 .pgo_getpages = vnode_pager_getpages,
101 .pgo_getpages_async = vnode_pager_getpages_async,
102 .pgo_putpages = vnode_pager_putpages,
103 .pgo_haspage = vnode_pager_haspage,
106 int vnode_pbuf_freecnt;
107 int vnode_async_pbuf_freecnt;
109 /* Create the VM system backing object for this vnode */
111 vnode_create_vobject(struct vnode *vp, off_t isize, struct thread *td)
114 vm_ooffset_t size = isize;
117 if (!vn_isdisk(vp, NULL) && vn_canvmio(vp) == FALSE)
120 while ((object = vp->v_object) != NULL) {
121 VM_OBJECT_WLOCK(object);
122 if (!(object->flags & OBJ_DEAD)) {
123 VM_OBJECT_WUNLOCK(object);
127 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
128 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vodead", 0);
129 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
133 if (vn_isdisk(vp, NULL)) {
134 size = IDX_TO_OFF(INT_MAX);
136 if (VOP_GETATTR(vp, &va, td->td_ucred))
142 object = vnode_pager_alloc(vp, size, 0, 0, td->td_ucred);
144 * Dereference the reference we just created. This assumes
145 * that the object is associated with the vp.
147 VM_OBJECT_WLOCK(object);
149 VM_OBJECT_WUNLOCK(object);
152 KASSERT(vp->v_object != NULL, ("vnode_create_vobject: NULL object"));
158 vnode_destroy_vobject(struct vnode *vp)
160 struct vm_object *obj;
165 ASSERT_VOP_ELOCKED(vp, "vnode_destroy_vobject");
166 VM_OBJECT_WLOCK(obj);
167 umtx_shm_object_terminated(obj);
168 if (obj->ref_count == 0) {
170 * don't double-terminate the object
172 if ((obj->flags & OBJ_DEAD) == 0) {
173 vm_object_terminate(obj);
176 * Waiters were already handled during object
177 * termination. The exclusive vnode lock hopefully
178 * prevented new waiters from referencing the dying
181 KASSERT((obj->flags & OBJ_DISCONNECTWNT) == 0,
182 ("OBJ_DISCONNECTWNT set obj %p flags %x",
185 VM_OBJECT_WUNLOCK(obj);
189 * Woe to the process that tries to page now :-).
191 vm_pager_deallocate(obj);
192 VM_OBJECT_WUNLOCK(obj);
194 KASSERT(vp->v_object == NULL, ("vp %p obj %p", vp, vp->v_object));
199 * Allocate (or lookup) pager for a vnode.
200 * Handle is a vnode pointer.
205 vnode_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
206 vm_ooffset_t offset, struct ucred *cred)
212 * Pageout to vnode, no can do yet.
217 vp = (struct vnode *) handle;
220 * If the object is being terminated, wait for it to
224 while ((object = vp->v_object) != NULL) {
225 VM_OBJECT_WLOCK(object);
226 if ((object->flags & OBJ_DEAD) == 0)
228 vm_object_set_flag(object, OBJ_DISCONNECTWNT);
229 VM_OBJECT_SLEEP(object, object, PDROP | PVM, "vadead", 0);
232 KASSERT(vp->v_usecount != 0, ("vnode_pager_alloc: no vnode reference"));
234 if (object == NULL) {
236 * Add an object of the appropriate size
238 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
240 object->un_pager.vnp.vnp_size = size;
241 object->un_pager.vnp.writemappings = 0;
243 object->handle = handle;
245 if (vp->v_object != NULL) {
247 * Object has been created while we were sleeping
250 VM_OBJECT_WLOCK(object);
251 KASSERT(object->ref_count == 1,
252 ("leaked ref %p %d", object, object->ref_count));
253 object->type = OBJT_DEAD;
254 object->ref_count = 0;
255 VM_OBJECT_WUNLOCK(object);
256 vm_object_destroy(object);
259 vp->v_object = object;
263 #if VM_NRESERVLEVEL > 0
264 vm_object_color(object, 0);
266 VM_OBJECT_WUNLOCK(object);
273 * The object must be locked.
276 vnode_pager_dealloc(vm_object_t object)
283 panic("vnode_pager_dealloc: pager already dealloced");
285 VM_OBJECT_ASSERT_WLOCKED(object);
286 vm_object_pip_wait(object, "vnpdea");
287 refs = object->ref_count;
289 object->handle = NULL;
290 object->type = OBJT_DEAD;
291 if (object->flags & OBJ_DISCONNECTWNT) {
292 vm_object_clear_flag(object, OBJ_DISCONNECTWNT);
295 ASSERT_VOP_ELOCKED(vp, "vnode_pager_dealloc");
296 if (object->un_pager.vnp.writemappings > 0) {
297 object->un_pager.vnp.writemappings = 0;
298 VOP_ADD_WRITECOUNT(vp, -1);
299 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
300 __func__, vp, vp->v_writecount);
304 VM_OBJECT_WUNLOCK(object);
307 VM_OBJECT_WLOCK(object);
311 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
314 struct vnode *vp = object->handle;
320 int pagesperblock, blocksperpage;
322 VM_OBJECT_ASSERT_WLOCKED(object);
324 * If no vp or vp is doomed or marked transparent to VM, we do not
327 if (vp == NULL || vp->v_iflag & VI_DOOMED)
330 * If the offset is beyond end of file we do
333 if (IDX_TO_OFF(pindex) >= object->un_pager.vnp.vnp_size)
336 bsize = vp->v_mount->mnt_stat.f_iosize;
337 pagesperblock = bsize / PAGE_SIZE;
339 if (pagesperblock > 0) {
340 reqblock = pindex / pagesperblock;
342 blocksperpage = (PAGE_SIZE / bsize);
343 reqblock = pindex * blocksperpage;
345 VM_OBJECT_WUNLOCK(object);
346 err = VOP_BMAP(vp, reqblock, NULL, &bn, after, before);
347 VM_OBJECT_WLOCK(object);
352 if (pagesperblock > 0) {
353 poff = pindex - (reqblock * pagesperblock);
355 *before *= pagesperblock;
360 * The BMAP vop can report a partial block in the
361 * 'after', but must not report blocks after EOF.
362 * Assert the latter, and truncate 'after' in case
365 KASSERT((reqblock + *after) * pagesperblock <
366 roundup2(object->size, pagesperblock),
367 ("%s: reqblock %jd after %d size %ju", __func__,
368 (intmax_t )reqblock, *after,
369 (uintmax_t )object->size));
370 *after *= pagesperblock;
371 *after += pagesperblock - (poff + 1);
372 if (pindex + *after >= object->size)
373 *after = object->size - 1 - pindex;
377 *before /= blocksperpage;
381 *after /= blocksperpage;
388 * Lets the VM system know about a change in size for a file.
389 * We adjust our own internal size and flush any cached pages in
390 * the associated object that are affected by the size change.
392 * Note: this routine may be invoked as a result of a pager put
393 * operation (possibly at object termination time), so we must be careful.
396 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
400 vm_pindex_t nobjsize;
402 if ((object = vp->v_object) == NULL)
404 /* ASSERT_VOP_ELOCKED(vp, "vnode_pager_setsize and not locked vnode"); */
405 VM_OBJECT_WLOCK(object);
406 if (object->type == OBJT_DEAD) {
407 VM_OBJECT_WUNLOCK(object);
410 KASSERT(object->type == OBJT_VNODE,
411 ("not vnode-backed object %p", object));
412 if (nsize == object->un_pager.vnp.vnp_size) {
414 * Hasn't changed size
416 VM_OBJECT_WUNLOCK(object);
419 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
420 if (nsize < object->un_pager.vnp.vnp_size) {
422 * File has shrunk. Toss any cached pages beyond the new EOF.
424 if (nobjsize < object->size)
425 vm_object_page_remove(object, nobjsize, object->size,
428 * this gets rid of garbage at the end of a page that is now
429 * only partially backed by the vnode.
431 * XXX for some reason (I don't know yet), if we take a
432 * completely invalid page and mark it partially valid
433 * it can screw up NFS reads, so we don't allow the case.
435 if ((nsize & PAGE_MASK) &&
436 (m = vm_page_lookup(object, OFF_TO_IDX(nsize))) != NULL &&
438 int base = (int)nsize & PAGE_MASK;
439 int size = PAGE_SIZE - base;
442 * Clear out partial-page garbage in case
443 * the page has been mapped.
445 pmap_zero_page_area(m, base, size);
448 * Update the valid bits to reflect the blocks that
449 * have been zeroed. Some of these valid bits may
450 * have already been set.
452 vm_page_set_valid_range(m, base, size);
455 * Round "base" to the next block boundary so that the
456 * dirty bit for a partially zeroed block is not
459 base = roundup2(base, DEV_BSIZE);
462 * Clear out partial-page dirty bits.
464 * note that we do not clear out the valid
465 * bits. This would prevent bogus_page
466 * replacement from working properly.
468 vm_page_clear_dirty(m, base, PAGE_SIZE - base);
471 object->un_pager.vnp.vnp_size = nsize;
472 object->size = nobjsize;
473 VM_OBJECT_WUNLOCK(object);
477 * calculate the linear (byte) disk address of specified virtual
481 vnode_pager_addr(struct vnode *vp, vm_ooffset_t address, daddr_t *rtaddress,
492 if (vp->v_iflag & VI_DOOMED)
495 bsize = vp->v_mount->mnt_stat.f_iosize;
496 vblock = address / bsize;
497 voffset = address % bsize;
499 err = VOP_BMAP(vp, vblock, NULL, rtaddress, run, NULL);
501 if (*rtaddress != -1)
502 *rtaddress += voffset / DEV_BSIZE;
505 *run *= bsize/PAGE_SIZE;
506 *run -= voffset/PAGE_SIZE;
514 * small block filesystem vnode pager input
517 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
530 if (vp->v_iflag & VI_DOOMED)
533 bsize = vp->v_mount->mnt_stat.f_iosize;
535 VOP_BMAP(vp, 0, &bo, 0, NULL, NULL);
537 sf = sf_buf_alloc(m, 0);
539 for (i = 0; i < PAGE_SIZE / bsize; i++) {
540 vm_ooffset_t address;
542 bits = vm_page_bits(i * bsize, bsize);
546 address = IDX_TO_OFF(m->pindex) + i * bsize;
547 if (address >= object->un_pager.vnp.vnp_size) {
550 error = vnode_pager_addr(vp, address, &fileaddr, NULL);
554 if (fileaddr != -1) {
555 bp = getpbuf(&vnode_pbuf_freecnt);
557 /* build a minimal buffer header */
558 bp->b_iocmd = BIO_READ;
559 bp->b_iodone = bdone;
560 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
561 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
562 bp->b_rcred = crhold(curthread->td_ucred);
563 bp->b_wcred = crhold(curthread->td_ucred);
564 bp->b_data = (caddr_t)sf_buf_kva(sf) + i * bsize;
565 bp->b_blkno = fileaddr;
568 bp->b_bcount = bsize;
569 bp->b_bufsize = bsize;
570 bp->b_runningbufspace = bp->b_bufsize;
571 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
574 bp->b_iooffset = dbtob(bp->b_blkno);
577 bwait(bp, PVM, "vnsrd");
579 if ((bp->b_ioflags & BIO_ERROR) != 0)
583 * free the buffer header back to the swap buffer pool
587 relpbuf(bp, &vnode_pbuf_freecnt);
591 bzero((caddr_t)sf_buf_kva(sf) + i * bsize, bsize);
592 KASSERT((m->dirty & bits) == 0,
593 ("vnode_pager_input_smlfs: page %p is dirty", m));
594 VM_OBJECT_WLOCK(object);
596 VM_OBJECT_WUNLOCK(object);
600 return VM_PAGER_ERROR;
606 * old style vnode pager input routine
609 vnode_pager_input_old(vm_object_t object, vm_page_t m)
618 VM_OBJECT_ASSERT_WLOCKED(object);
622 * Return failure if beyond current EOF
624 if (IDX_TO_OFF(m->pindex) >= object->un_pager.vnp.vnp_size) {
628 if (IDX_TO_OFF(m->pindex) + size > object->un_pager.vnp.vnp_size)
629 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(m->pindex);
631 VM_OBJECT_WUNLOCK(object);
634 * Allocate a kernel virtual address and initialize so that
635 * we can use VOP_READ/WRITE routines.
637 sf = sf_buf_alloc(m, 0);
639 aiov.iov_base = (caddr_t)sf_buf_kva(sf);
641 auio.uio_iov = &aiov;
643 auio.uio_offset = IDX_TO_OFF(m->pindex);
644 auio.uio_segflg = UIO_SYSSPACE;
645 auio.uio_rw = UIO_READ;
646 auio.uio_resid = size;
647 auio.uio_td = curthread;
649 error = VOP_READ(vp, &auio, 0, curthread->td_ucred);
651 int count = size - auio.uio_resid;
655 else if (count != PAGE_SIZE)
656 bzero((caddr_t)sf_buf_kva(sf) + count,
661 VM_OBJECT_WLOCK(object);
663 KASSERT(m->dirty == 0, ("vnode_pager_input_old: page %p is dirty", m));
665 m->valid = VM_PAGE_BITS_ALL;
666 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
670 * generic vnode pager input routine
674 * Local media VFS's that do not implement their own VOP_GETPAGES
675 * should have their VOP_GETPAGES call to vnode_pager_generic_getpages()
676 * to implement the previous behaviour.
678 * All other FS's should use the bypass to get to the local media
679 * backing vp's VOP_GETPAGES.
682 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
689 VM_OBJECT_WUNLOCK(object);
690 rtval = VOP_GETPAGES(vp, m, count, rbehind, rahead);
691 KASSERT(rtval != EOPNOTSUPP,
692 ("vnode_pager: FS getpages not implemented\n"));
693 VM_OBJECT_WLOCK(object);
698 vnode_pager_getpages_async(vm_object_t object, vm_page_t *m, int count,
699 int *rbehind, int *rahead, vop_getpages_iodone_t iodone, void *arg)
705 VM_OBJECT_WUNLOCK(object);
706 rtval = VOP_GETPAGES_ASYNC(vp, m, count, rbehind, rahead, iodone, arg);
707 KASSERT(rtval != EOPNOTSUPP,
708 ("vnode_pager: FS getpages_async not implemented\n"));
709 VM_OBJECT_WLOCK(object);
714 * The implementation of VOP_GETPAGES() and VOP_GETPAGES_ASYNC() for
715 * local filesystems, where partially valid pages can only occur at
719 vnode_pager_local_getpages(struct vop_getpages_args *ap)
722 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
723 ap->a_rbehind, ap->a_rahead, NULL, NULL));
727 vnode_pager_local_getpages_async(struct vop_getpages_async_args *ap)
730 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
731 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg));
735 * This is now called from local media FS's to operate against their
736 * own vnodes if they fail to implement VOP_GETPAGES.
739 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int count,
740 int *a_rbehind, int *a_rahead, vop_getpages_iodone_t iodone, void *arg)
749 int bsize, pagesperblock, *freecnt;
750 int error, before, after, rbehind, rahead, poff, i;
751 int bytecount, secmask;
753 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
754 ("%s does not support devices", __func__));
756 if (vp->v_iflag & VI_DOOMED)
757 return (VM_PAGER_BAD);
759 object = vp->v_object;
760 foff = IDX_TO_OFF(m[0]->pindex);
761 bsize = vp->v_mount->mnt_stat.f_iosize;
762 pagesperblock = bsize / PAGE_SIZE;
764 KASSERT(foff < object->un_pager.vnp.vnp_size,
765 ("%s: page %p offset beyond vp %p size", __func__, m[0], vp));
766 KASSERT(count <= sizeof(bp->b_pages),
767 ("%s: requested %d pages", __func__, count));
770 * The last page has valid blocks. Invalid part can only
771 * exist at the end of file, and the page is made fully valid
772 * by zeroing in vm_pager_get_pages().
774 if (m[count - 1]->valid != 0 && --count == 0) {
776 iodone(arg, m, 1, 0);
777 return (VM_PAGER_OK);
781 * Synchronous and asynchronous paging operations use different
782 * free pbuf counters. This is done to avoid asynchronous requests
783 * to consume all pbufs.
784 * Allocate the pbuf at the very beginning of the function, so that
785 * if we are low on certain kind of pbufs don't even proceed to BMAP,
788 freecnt = iodone != NULL ?
789 &vnode_async_pbuf_freecnt : &vnode_pbuf_freecnt;
790 bp = getpbuf(freecnt);
793 * Get the underlying device blocks for the file with VOP_BMAP().
794 * If the file system doesn't support VOP_BMAP, use old way of
795 * getting pages via VOP_READ.
797 error = VOP_BMAP(vp, foff / bsize, &bo, &bp->b_blkno, &after, &before);
798 if (error == EOPNOTSUPP) {
799 relpbuf(bp, freecnt);
800 VM_OBJECT_WLOCK(object);
801 for (i = 0; i < count; i++) {
802 VM_CNT_INC(v_vnodein);
803 VM_CNT_INC(v_vnodepgsin);
804 error = vnode_pager_input_old(object, m[i]);
808 VM_OBJECT_WUNLOCK(object);
810 } else if (error != 0) {
811 relpbuf(bp, freecnt);
812 return (VM_PAGER_ERROR);
816 * If the file system supports BMAP, but blocksize is smaller
817 * than a page size, then use special small filesystem code.
819 if (pagesperblock == 0) {
820 relpbuf(bp, freecnt);
821 for (i = 0; i < count; i++) {
822 VM_CNT_INC(v_vnodein);
823 VM_CNT_INC(v_vnodepgsin);
824 error = vnode_pager_input_smlfs(object, m[i]);
832 * A sparse file can be encountered only for a single page request,
833 * which may not be preceded by call to vm_pager_haspage().
835 if (bp->b_blkno == -1) {
837 ("%s: array[%d] request to a sparse file %p", __func__,
839 relpbuf(bp, freecnt);
840 pmap_zero_page(m[0]);
841 KASSERT(m[0]->dirty == 0, ("%s: page %p is dirty",
843 VM_OBJECT_WLOCK(object);
844 m[0]->valid = VM_PAGE_BITS_ALL;
845 VM_OBJECT_WUNLOCK(object);
846 return (VM_PAGER_OK);
850 blkno0 = bp->b_blkno;
852 bp->b_blkno += (foff % bsize) / DEV_BSIZE;
854 /* Recalculate blocks available after/before to pages. */
855 poff = (foff % bsize) / PAGE_SIZE;
856 before *= pagesperblock;
858 after *= pagesperblock;
859 after += pagesperblock - (poff + 1);
860 if (m[0]->pindex + after >= object->size)
861 after = object->size - 1 - m[0]->pindex;
862 KASSERT(count <= after + 1, ("%s: %d pages asked, can do only %d",
863 __func__, count, after + 1));
866 /* Trim requested rbehind/rahead to possible values. */
867 rbehind = a_rbehind ? *a_rbehind : 0;
868 rahead = a_rahead ? *a_rahead : 0;
869 rbehind = min(rbehind, before);
870 rbehind = min(rbehind, m[0]->pindex);
871 rahead = min(rahead, after);
872 rahead = min(rahead, object->size - m[count - 1]->pindex);
874 * Check that total amount of pages fit into buf. Trim rbehind and
875 * rahead evenly if not.
877 if (rbehind + rahead + count > nitems(bp->b_pages)) {
880 trim = rbehind + rahead + count - nitems(bp->b_pages) + 1;
881 sum = rbehind + rahead;
882 if (rbehind == before) {
883 /* Roundup rbehind trim to block size. */
884 rbehind -= roundup(trim * rbehind / sum, pagesperblock);
888 rbehind -= trim * rbehind / sum;
889 rahead -= trim * rahead / sum;
891 KASSERT(rbehind + rahead + count <= nitems(bp->b_pages),
892 ("%s: behind %d ahead %d count %d", __func__,
893 rbehind, rahead, count));
896 * Fill in the bp->b_pages[] array with requested and optional
897 * read behind or read ahead pages. Read behind pages are looked
898 * up in a backward direction, down to a first cached page. Same
899 * for read ahead pages, but there is no need to shift the array
900 * in case of encountering a cached page.
902 i = bp->b_npages = 0;
904 vm_pindex_t startpindex, tpindex;
907 VM_OBJECT_WLOCK(object);
908 startpindex = m[0]->pindex - rbehind;
909 if ((p = TAILQ_PREV(m[0], pglist, listq)) != NULL &&
910 p->pindex >= startpindex)
911 startpindex = p->pindex + 1;
913 /* tpindex is unsigned; beware of numeric underflow. */
914 for (tpindex = m[0]->pindex - 1;
915 tpindex >= startpindex && tpindex < m[0]->pindex;
917 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
919 /* Shift the array. */
920 for (int j = 0; j < i; j++)
921 bp->b_pages[j] = bp->b_pages[j +
922 tpindex + 1 - startpindex];
925 bp->b_pages[tpindex - startpindex] = p;
930 bp->b_blkno -= IDX_TO_OFF(i) / DEV_BSIZE;
934 /* Requested pages. */
935 for (int j = 0; j < count; j++, i++)
936 bp->b_pages[i] = m[j];
937 bp->b_npages += count;
940 vm_pindex_t endpindex, tpindex;
943 if (!VM_OBJECT_WOWNED(object))
944 VM_OBJECT_WLOCK(object);
945 endpindex = m[count - 1]->pindex + rahead + 1;
946 if ((p = TAILQ_NEXT(m[count - 1], listq)) != NULL &&
947 p->pindex < endpindex)
948 endpindex = p->pindex;
949 if (endpindex > object->size)
950 endpindex = object->size;
952 for (tpindex = m[count - 1]->pindex + 1;
953 tpindex < endpindex; i++, tpindex++) {
954 p = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
960 bp->b_pgafter = i - bp->b_npages;
965 if (VM_OBJECT_WOWNED(object))
966 VM_OBJECT_WUNLOCK(object);
968 /* Report back actual behind/ahead read. */
970 *a_rbehind = bp->b_pgbefore;
972 *a_rahead = bp->b_pgafter;
975 KASSERT(bp->b_npages <= nitems(bp->b_pages),
976 ("%s: buf %p overflowed", __func__, bp));
977 for (int j = 1, prev = 0; j < bp->b_npages; j++) {
978 if (bp->b_pages[j] == bogus_page)
980 KASSERT(bp->b_pages[j]->pindex - bp->b_pages[prev]->pindex ==
981 j - prev, ("%s: pages array not consecutive, bp %p",
988 * Recalculate first offset and bytecount with regards to read behind.
989 * Truncate bytecount to vnode real size and round up physical size
992 foff = IDX_TO_OFF(bp->b_pages[0]->pindex);
993 bytecount = bp->b_npages << PAGE_SHIFT;
994 if ((foff + bytecount) > object->un_pager.vnp.vnp_size)
995 bytecount = object->un_pager.vnp.vnp_size - foff;
996 secmask = bo->bo_bsize - 1;
997 KASSERT(secmask < PAGE_SIZE && secmask > 0,
998 ("%s: sector size %d too large", __func__, secmask + 1));
999 bytecount = (bytecount + secmask) & ~secmask;
1002 * And map the pages to be read into the kva, if the filesystem
1003 * requires mapped buffers.
1005 if ((vp->v_mount->mnt_kern_flag & MNTK_UNMAPPED_BUFS) != 0 &&
1006 unmapped_buf_allowed) {
1007 bp->b_data = unmapped_buf;
1010 bp->b_data = bp->b_kvabase;
1011 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1014 /* Build a minimal buffer header. */
1015 bp->b_iocmd = BIO_READ;
1016 KASSERT(bp->b_rcred == NOCRED, ("leaking read ucred"));
1017 KASSERT(bp->b_wcred == NOCRED, ("leaking write ucred"));
1018 bp->b_rcred = crhold(curthread->td_ucred);
1019 bp->b_wcred = crhold(curthread->td_ucred);
1022 bp->b_bcount = bp->b_bufsize = bp->b_runningbufspace = bytecount;
1023 bp->b_iooffset = dbtob(bp->b_blkno);
1024 KASSERT(IDX_TO_OFF(m[0]->pindex - bp->b_pages[0]->pindex) ==
1025 (blkno0 - bp->b_blkno) * DEV_BSIZE +
1026 IDX_TO_OFF(m[0]->pindex) % bsize,
1027 ("wrong offsets bsize %d m[0] %ju b_pages[0] %ju "
1028 "blkno0 %ju b_blkno %ju", bsize,
1029 (uintmax_t)m[0]->pindex, (uintmax_t)bp->b_pages[0]->pindex,
1030 (uintmax_t)blkno0, (uintmax_t)bp->b_blkno));
1032 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
1033 VM_CNT_INC(v_vnodein);
1034 VM_CNT_ADD(v_vnodepgsin, bp->b_npages);
1036 if (iodone != NULL) { /* async */
1037 bp->b_pgiodone = iodone;
1038 bp->b_caller1 = arg;
1039 bp->b_iodone = vnode_pager_generic_getpages_done_async;
1040 bp->b_flags |= B_ASYNC;
1043 return (VM_PAGER_OK);
1045 bp->b_iodone = bdone;
1047 bwait(bp, PVM, "vnread");
1048 error = vnode_pager_generic_getpages_done(bp);
1049 for (i = 0; i < bp->b_npages; i++)
1050 bp->b_pages[i] = NULL;
1053 relpbuf(bp, &vnode_pbuf_freecnt);
1054 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
1059 vnode_pager_generic_getpages_done_async(struct buf *bp)
1063 error = vnode_pager_generic_getpages_done(bp);
1064 /* Run the iodone upon the requested range. */
1065 bp->b_pgiodone(bp->b_caller1, bp->b_pages + bp->b_pgbefore,
1066 bp->b_npages - bp->b_pgbefore - bp->b_pgafter, error);
1067 for (int i = 0; i < bp->b_npages; i++)
1068 bp->b_pages[i] = NULL;
1071 relpbuf(bp, &vnode_async_pbuf_freecnt);
1075 vnode_pager_generic_getpages_done(struct buf *bp)
1078 off_t tfoff, nextoff;
1081 error = (bp->b_ioflags & BIO_ERROR) != 0 ? EIO : 0;
1082 object = bp->b_vp->v_object;
1084 if (error == 0 && bp->b_bcount != bp->b_npages * PAGE_SIZE) {
1085 if (!buf_mapped(bp)) {
1086 bp->b_data = bp->b_kvabase;
1087 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages,
1090 bzero(bp->b_data + bp->b_bcount,
1091 PAGE_SIZE * bp->b_npages - bp->b_bcount);
1093 if (buf_mapped(bp)) {
1094 pmap_qremove((vm_offset_t)bp->b_data, bp->b_npages);
1095 bp->b_data = unmapped_buf;
1098 VM_OBJECT_WLOCK(object);
1099 for (i = 0, tfoff = IDX_TO_OFF(bp->b_pages[0]->pindex);
1100 i < bp->b_npages; i++, tfoff = nextoff) {
1103 nextoff = tfoff + PAGE_SIZE;
1104 mt = bp->b_pages[i];
1106 if (nextoff <= object->un_pager.vnp.vnp_size) {
1108 * Read filled up entire page.
1110 mt->valid = VM_PAGE_BITS_ALL;
1111 KASSERT(mt->dirty == 0,
1112 ("%s: page %p is dirty", __func__, mt));
1113 KASSERT(!pmap_page_is_mapped(mt),
1114 ("%s: page %p is mapped", __func__, mt));
1117 * Read did not fill up entire page.
1119 * Currently we do not set the entire page valid,
1120 * we just try to clear the piece that we couldn't
1123 vm_page_set_valid_range(mt, 0,
1124 object->un_pager.vnp.vnp_size - tfoff);
1125 KASSERT((mt->dirty & vm_page_bits(0,
1126 object->un_pager.vnp.vnp_size - tfoff)) == 0,
1127 ("%s: page %p is dirty", __func__, mt));
1130 if (i < bp->b_pgbefore || i >= bp->b_npages - bp->b_pgafter)
1131 vm_page_readahead_finish(mt);
1133 VM_OBJECT_WUNLOCK(object);
1135 printf("%s: I/O read error %d\n", __func__, error);
1141 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
1142 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
1143 * vnode_pager_generic_putpages() to implement the previous behaviour.
1145 * All other FS's should use the bypass to get to the local media
1146 * backing vp's VOP_PUTPAGES.
1149 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
1150 int flags, int *rtvals)
1154 int bytes = count * PAGE_SIZE;
1157 * Force synchronous operation if we are extremely low on memory
1158 * to prevent a low-memory deadlock. VOP operations often need to
1159 * allocate more memory to initiate the I/O ( i.e. do a BMAP
1160 * operation ). The swapper handles the case by limiting the amount
1161 * of asynchronous I/O, but that sort of solution doesn't scale well
1162 * for the vnode pager without a lot of work.
1164 * Also, the backing vnode's iodone routine may not wake the pageout
1165 * daemon up. This should be probably be addressed XXX.
1168 if (vm_cnt.v_free_count < vm_cnt.v_pageout_free_min)
1169 flags |= VM_PAGER_PUT_SYNC;
1172 * Call device-specific putpages function
1174 vp = object->handle;
1175 VM_OBJECT_WUNLOCK(object);
1176 rtval = VOP_PUTPAGES(vp, m, bytes, flags, rtvals);
1177 KASSERT(rtval != EOPNOTSUPP,
1178 ("vnode_pager: stale FS putpages\n"));
1179 VM_OBJECT_WLOCK(object);
1183 vn_off2bidx(vm_ooffset_t offset)
1186 return ((offset & PAGE_MASK) / DEV_BSIZE);
1190 vn_dirty_blk(vm_page_t m, vm_ooffset_t offset)
1193 KASSERT(IDX_TO_OFF(m->pindex) <= offset &&
1194 offset < IDX_TO_OFF(m->pindex + 1),
1195 ("page %p pidx %ju offset %ju", m, (uintmax_t)m->pindex,
1196 (uintmax_t)offset));
1197 return ((m->dirty & ((vm_page_bits_t)1 << vn_off2bidx(offset))) != 0);
1201 * This is now called from local media FS's to operate against their
1202 * own vnodes if they fail to implement VOP_PUTPAGES.
1204 * This is typically called indirectly via the pageout daemon and
1205 * clustering has already typically occurred, so in general we ask the
1206 * underlying filesystem to write the data out asynchronously rather
1210 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *ma, int bytecount,
1211 int flags, int *rtvals)
1215 vm_ooffset_t maxblksz, next_offset, poffset, prev_offset;
1218 off_t prev_resid, wrsz;
1219 int count, error, i, maxsize, ncount, pgoff, ppscheck;
1221 static struct timeval lastfail;
1224 object = vp->v_object;
1225 count = bytecount / PAGE_SIZE;
1227 for (i = 0; i < count; i++)
1228 rtvals[i] = VM_PAGER_ERROR;
1230 if ((int64_t)ma[0]->pindex < 0) {
1231 printf("vnode_pager_generic_putpages: "
1232 "attempt to write meta-data 0x%jx(%lx)\n",
1233 (uintmax_t)ma[0]->pindex, (u_long)ma[0]->dirty);
1234 rtvals[0] = VM_PAGER_BAD;
1235 return (VM_PAGER_BAD);
1238 maxsize = count * PAGE_SIZE;
1241 poffset = IDX_TO_OFF(ma[0]->pindex);
1244 * If the page-aligned write is larger then the actual file we
1245 * have to invalidate pages occurring beyond the file EOF. However,
1246 * there is an edge case where a file may not be page-aligned where
1247 * the last page is partially invalid. In this case the filesystem
1248 * may not properly clear the dirty bits for the entire page (which
1249 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
1250 * With the page locked we are free to fix-up the dirty bits here.
1252 * We do not under any circumstances truncate the valid bits, as
1253 * this will screw up bogus page replacement.
1255 VM_OBJECT_RLOCK(object);
1256 if (maxsize + poffset > object->un_pager.vnp.vnp_size) {
1257 if (!VM_OBJECT_TRYUPGRADE(object)) {
1258 VM_OBJECT_RUNLOCK(object);
1259 VM_OBJECT_WLOCK(object);
1260 if (maxsize + poffset <= object->un_pager.vnp.vnp_size)
1263 if (object->un_pager.vnp.vnp_size > poffset) {
1264 maxsize = object->un_pager.vnp.vnp_size - poffset;
1265 ncount = btoc(maxsize);
1266 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
1268 * If the object is locked and the following
1269 * conditions hold, then the page's dirty
1270 * field cannot be concurrently changed by a
1274 vm_page_assert_sbusied(m);
1275 KASSERT(!pmap_page_is_write_mapped(m),
1276 ("vnode_pager_generic_putpages: page %p is not read-only", m));
1277 MPASS(m->dirty != 0);
1278 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
1285 for (i = ncount; i < count; i++)
1286 rtvals[i] = VM_PAGER_BAD;
1288 VM_OBJECT_LOCK_DOWNGRADE(object);
1291 auio.uio_iov = &aiov;
1292 auio.uio_segflg = UIO_NOCOPY;
1293 auio.uio_rw = UIO_WRITE;
1295 maxblksz = roundup2(poffset + maxsize, DEV_BSIZE);
1297 for (prev_offset = poffset; prev_offset < maxblksz;) {
1298 /* Skip clean blocks. */
1299 for (in_hole = true; in_hole && prev_offset < maxblksz;) {
1300 m = ma[OFF_TO_IDX(prev_offset - poffset)];
1301 for (i = vn_off2bidx(prev_offset);
1302 i < sizeof(vm_page_bits_t) * NBBY &&
1303 prev_offset < maxblksz; i++) {
1304 if (vn_dirty_blk(m, prev_offset)) {
1308 prev_offset += DEV_BSIZE;
1314 /* Find longest run of dirty blocks. */
1315 for (next_offset = prev_offset; next_offset < maxblksz;) {
1316 m = ma[OFF_TO_IDX(next_offset - poffset)];
1317 for (i = vn_off2bidx(next_offset);
1318 i < sizeof(vm_page_bits_t) * NBBY &&
1319 next_offset < maxblksz; i++) {
1320 if (!vn_dirty_blk(m, next_offset))
1322 next_offset += DEV_BSIZE;
1326 if (next_offset > poffset + maxsize)
1327 next_offset = poffset + maxsize;
1330 * Getting here requires finding a dirty block in the
1331 * 'skip clean blocks' loop.
1333 MPASS(prev_offset < next_offset);
1335 VM_OBJECT_RUNLOCK(object);
1336 aiov.iov_base = NULL;
1337 auio.uio_iovcnt = 1;
1338 auio.uio_offset = prev_offset;
1339 prev_resid = auio.uio_resid = aiov.iov_len = next_offset -
1341 error = VOP_WRITE(vp, &auio,
1342 vnode_pager_putpages_ioflags(flags), curthread->td_ucred);
1344 wrsz = prev_resid - auio.uio_resid;
1346 if (ppsratecheck(&lastfail, &curfail, 1) != 0) {
1347 vn_printf(vp, "vnode_pager_putpages: "
1348 "zero-length write at %ju resid %zd\n",
1349 auio.uio_offset, auio.uio_resid);
1351 VM_OBJECT_RLOCK(object);
1355 /* Adjust the starting offset for next iteration. */
1356 prev_offset += wrsz;
1357 MPASS(auio.uio_offset == prev_offset);
1360 if (error != 0 && (ppscheck = ppsratecheck(&lastfail,
1362 vn_printf(vp, "vnode_pager_putpages: I/O error %d\n",
1364 if (auio.uio_resid != 0 && (ppscheck != 0 ||
1365 ppsratecheck(&lastfail, &curfail, 1) != 0))
1366 vn_printf(vp, "vnode_pager_putpages: residual I/O %zd "
1367 "at %ju\n", auio.uio_resid,
1368 (uintmax_t)ma[0]->pindex);
1369 VM_OBJECT_RLOCK(object);
1370 if (error != 0 || auio.uio_resid != 0)
1374 /* Mark completely processed pages. */
1375 for (i = 0; i < OFF_TO_IDX(prev_offset - poffset); i++)
1376 rtvals[i] = VM_PAGER_OK;
1377 /* Mark partial EOF page. */
1378 if (prev_offset == poffset + maxsize && (prev_offset & PAGE_MASK) != 0)
1379 rtvals[i++] = VM_PAGER_OK;
1380 /* Unwritten pages in range, free bonus if the page is clean. */
1381 for (; i < ncount; i++)
1382 rtvals[i] = ma[i]->dirty == 0 ? VM_PAGER_OK : VM_PAGER_ERROR;
1383 VM_OBJECT_RUNLOCK(object);
1384 VM_CNT_ADD(v_vnodepgsout, i);
1385 VM_CNT_INC(v_vnodeout);
1390 vnode_pager_putpages_ioflags(int pager_flags)
1395 * Pageouts are already clustered, use IO_ASYNC to force a
1396 * bawrite() rather then a bdwrite() to prevent paging I/O
1397 * from saturating the buffer cache. Dummy-up the sequential
1398 * heuristic to cause large ranges to cluster. If neither
1399 * IO_SYNC or IO_ASYNC is set, the system decides how to
1403 if ((pager_flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0)
1405 else if ((pager_flags & VM_PAGER_CLUSTER_OK) == 0)
1406 ioflags |= IO_ASYNC;
1407 ioflags |= (pager_flags & VM_PAGER_PUT_INVAL) != 0 ? IO_INVAL: 0;
1408 ioflags |= (pager_flags & VM_PAGER_PUT_NOREUSE) != 0 ? IO_NOREUSE : 0;
1409 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
1414 * vnode_pager_undirty_pages().
1416 * A helper to mark pages as clean after pageout that was possibly
1417 * done with a short write. The lpos argument specifies the page run
1418 * length in bytes, and the written argument specifies how many bytes
1419 * were actually written. eof is the offset past the last valid byte
1420 * in the vnode using the absolute file position of the first byte in
1421 * the run as the base from which it is computed.
1424 vnode_pager_undirty_pages(vm_page_t *ma, int *rtvals, int written, off_t eof,
1428 int i, pos, pos_devb;
1430 if (written == 0 && eof >= lpos)
1432 obj = ma[0]->object;
1433 VM_OBJECT_WLOCK(obj);
1434 for (i = 0, pos = 0; pos < written; i++, pos += PAGE_SIZE) {
1435 if (pos < trunc_page(written)) {
1436 rtvals[i] = VM_PAGER_OK;
1437 vm_page_undirty(ma[i]);
1439 /* Partially written page. */
1440 rtvals[i] = VM_PAGER_AGAIN;
1441 vm_page_clear_dirty(ma[i], 0, written & PAGE_MASK);
1444 if (eof >= lpos) /* avoid truncation */
1446 for (pos = eof, i = OFF_TO_IDX(trunc_page(pos)); pos < lpos; i++) {
1447 if (pos != trunc_page(pos)) {
1449 * The page contains the last valid byte in
1450 * the vnode, mark the rest of the page as
1451 * clean, potentially making the whole page
1454 pos_devb = roundup2(pos & PAGE_MASK, DEV_BSIZE);
1455 vm_page_clear_dirty(ma[i], pos_devb, PAGE_SIZE -
1459 * If the page was cleaned, report the pageout
1460 * on it as successful. msync() no longer
1461 * needs to write out the page, endlessly
1462 * creating write requests and dirty buffers.
1464 if (ma[i]->dirty == 0)
1465 rtvals[i] = VM_PAGER_OK;
1467 pos = round_page(pos);
1469 /* vm_pageout_flush() clears dirty */
1470 rtvals[i] = VM_PAGER_BAD;
1475 VM_OBJECT_WUNLOCK(obj);
1479 vnode_pager_update_writecount(vm_object_t object, vm_offset_t start,
1483 vm_ooffset_t old_wm;
1485 VM_OBJECT_WLOCK(object);
1486 if (object->type != OBJT_VNODE) {
1487 VM_OBJECT_WUNLOCK(object);
1490 old_wm = object->un_pager.vnp.writemappings;
1491 object->un_pager.vnp.writemappings += (vm_ooffset_t)end - start;
1492 vp = object->handle;
1493 if (old_wm == 0 && object->un_pager.vnp.writemappings != 0) {
1494 ASSERT_VOP_ELOCKED(vp, "v_writecount inc");
1495 VOP_ADD_WRITECOUNT(vp, 1);
1496 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1497 __func__, vp, vp->v_writecount);
1498 } else if (old_wm != 0 && object->un_pager.vnp.writemappings == 0) {
1499 ASSERT_VOP_ELOCKED(vp, "v_writecount dec");
1500 VOP_ADD_WRITECOUNT(vp, -1);
1501 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1502 __func__, vp, vp->v_writecount);
1504 VM_OBJECT_WUNLOCK(object);
1508 vnode_pager_release_writecount(vm_object_t object, vm_offset_t start,
1515 VM_OBJECT_WLOCK(object);
1518 * First, recheck the object type to account for the race when
1519 * the vnode is reclaimed.
1521 if (object->type != OBJT_VNODE) {
1522 VM_OBJECT_WUNLOCK(object);
1527 * Optimize for the case when writemappings is not going to
1531 if (object->un_pager.vnp.writemappings != inc) {
1532 object->un_pager.vnp.writemappings -= inc;
1533 VM_OBJECT_WUNLOCK(object);
1537 vp = object->handle;
1539 VM_OBJECT_WUNLOCK(object);
1541 vn_start_write(vp, &mp, V_WAIT);
1542 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1545 * Decrement the object's writemappings, by swapping the start
1546 * and end arguments for vnode_pager_update_writecount(). If
1547 * there was not a race with vnode reclaimation, then the
1548 * vnode's v_writecount is decremented.
1550 vnode_pager_update_writecount(object, end, start);
1554 vn_finished_write(mp);