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
41 * $FreeBSD: src/sys/vm/vnode_pager.c,v 1.116.2.7 2002/12/31 09:34:51 dillon Exp $
42 * $DragonFly: src/sys/vm/vnode_pager.c,v 1.23 2006/03/27 01:54:18 dillon Exp $
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/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/vnode.h>
60 #include <sys/mount.h>
62 #include <sys/vmmeter.h>
64 #include <sys/sfbuf.h>
65 #include <sys/thread2.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pager.h>
71 #include <vm/vm_map.h>
72 #include <vm/vnode_pager.h>
73 #include <vm/vm_extern.h>
75 static off_t vnode_pager_addr (struct vnode *vp, off_t loffset, int *run);
76 static void vnode_pager_iodone (struct bio *bio);
77 static int vnode_pager_input_smlfs (vm_object_t object, vm_page_t m);
78 static int vnode_pager_input_old (vm_object_t object, vm_page_t m);
79 static void vnode_pager_dealloc (vm_object_t);
80 static int vnode_pager_getpages (vm_object_t, vm_page_t *, int, int);
81 static void vnode_pager_putpages (vm_object_t, vm_page_t *, int, boolean_t, int *);
82 static boolean_t vnode_pager_haspage (vm_object_t, vm_pindex_t, int *, int *);
84 struct pagerops vnodepagerops = {
94 int vnode_pbuf_freecnt = -1; /* start out unlimited */
97 * Allocate (or lookup) pager for a vnode.
98 * Handle is a vnode pointer.
101 vnode_pager_alloc(void *handle, off_t size, vm_prot_t prot, off_t offset)
107 * Pageout to vnode, no can do yet.
113 * XXX hack - This initialization should be put somewhere else.
115 if (vnode_pbuf_freecnt < 0) {
116 vnode_pbuf_freecnt = nswbuf / 2 + 1;
119 vp = (struct vnode *) handle;
122 * Prevent race condition when allocating the object. This
123 * can happen with NFS vnodes since the nfsnode isn't locked.
125 while (vp->v_flag & VOLOCK) {
126 vp->v_flag |= VOWANT;
127 tsleep(vp, 0, "vnpobj", 0);
129 vp->v_flag |= VOLOCK;
132 * If the object is being terminated, wait for it to
135 while (((object = vp->v_object) != NULL) &&
136 (object->flags & OBJ_DEAD)) {
137 tsleep(object, 0, "vadead", 0);
140 if (vp->v_usecount == 0)
141 panic("vnode_pager_alloc: no vnode reference");
143 if (object == NULL) {
145 * And an object of the appropriate size
147 object = vm_object_allocate(OBJT_VNODE, OFF_TO_IDX(round_page(size)));
149 object->handle = handle;
150 vp->v_object = object;
151 vp->v_filesize = size;
154 if (vp->v_filesize != size)
155 printf("vnode_pager_alloc: Warning, filesize mismatch %lld/%lld\n", vp->v_filesize, size);
159 vp->v_flag &= ~VOLOCK;
160 if (vp->v_flag & VOWANT) {
161 vp->v_flag &= ~VOWANT;
168 vnode_pager_dealloc(vm_object_t object)
170 struct vnode *vp = object->handle;
173 panic("vnode_pager_dealloc: pager already dealloced");
175 vm_object_pip_wait(object, "vnpdea");
177 object->handle = NULL;
178 object->type = OBJT_DEAD;
180 vp->v_filesize = NOOFFSET;
181 vp->v_flag &= ~(VTEXT | VOBJBUF);
185 * Return whether the vnode pager has the requested page. Return the
186 * number of disk-contiguous pages before and after the requested page,
187 * not including the requested page.
190 vnode_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
193 struct vnode *vp = object->handle;
201 * If no vp or vp is doomed or marked transparent to VM, we do not
204 if ((vp == NULL) || (vp->v_flag & VRECLAIMED))
208 * If filesystem no longer mounted or offset beyond end of file we do
211 loffset = IDX_TO_OFF(pindex);
213 if (vp->v_mount == NULL || loffset >= vp->v_filesize)
216 bsize = vp->v_mount->mnt_stat.f_iosize;
217 voff = loffset % bsize;
219 error = VOP_BMAP(vp, loffset - voff, NULL, &doffset, after, before);
222 if (doffset == NOOFFSET)
226 *before = (*before + voff) >> PAGE_SHIFT;
230 if (loffset + *after > vp->v_filesize)
231 *after = vp->v_filesize - loffset;
232 *after >>= PAGE_SHIFT;
240 * Lets the VM system know about a change in size for a file.
241 * We adjust our own internal size and flush any cached pages in
242 * the associated object that are affected by the size change.
244 * NOTE: This routine may be invoked as a result of a pager put
245 * operation (possibly at object termination time), so we must be careful.
247 * NOTE: vp->v_filesize is initialized to NOOFFSET (-1), be sure that
248 * we do not blow up on the case. nsize will always be >= 0, however.
251 vnode_pager_setsize(struct vnode *vp, vm_ooffset_t nsize)
253 vm_pindex_t nobjsize;
254 vm_object_t object = vp->v_object;
260 * Hasn't changed size
262 if (nsize == vp->v_filesize)
265 nobjsize = OFF_TO_IDX(nsize + PAGE_MASK);
268 * File has shrunk. Toss any cached pages beyond the new EOF.
270 if (nsize < vp->v_filesize) {
271 vm_freeze_copyopts(object, OFF_TO_IDX(nsize), object->size);
272 if (nobjsize < object->size) {
273 vm_object_page_remove(object, nobjsize, object->size,
277 * This gets rid of garbage at the end of a page that is now
278 * only partially backed by the vnode. Since we are setting
279 * the entire page valid & clean after we are done we have
280 * to be sure that the portion of the page within the file
281 * bounds is already valid. If it isn't then making it
282 * valid would create a corrupt block.
284 if (nsize & PAGE_MASK) {
288 m = vm_page_lookup(object, OFF_TO_IDX(nsize));
290 int base = (int)nsize & PAGE_MASK;
291 int size = PAGE_SIZE - base;
295 * Clear out partial-page garbage in case
296 * the page has been mapped.
298 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
299 kva = sf_buf_kva(sf);
300 bzero((caddr_t)kva + base, size);
304 * XXX work around SMP data integrity race
305 * by unmapping the page from user processes.
306 * The garbage we just cleared may be mapped
307 * to a user process running on another cpu
308 * and this code is not running through normal
309 * I/O channels which handle SMP issues for
310 * us, so unmap page to synchronize all cpus.
312 * XXX should vm_pager_unmap_page() have
315 vm_page_protect(m, VM_PROT_NONE);
318 * Clear out partial-page dirty bits. This
319 * has the side effect of setting the valid
320 * bits, but that is ok. There are a bunch
321 * of places in the VM system where we expected
322 * m->dirty == VM_PAGE_BITS_ALL. The file EOF
323 * case is one of them. If the page is still
324 * partially dirty, make it fully dirty.
326 * note that we do not clear out the valid
327 * bits. This would prevent bogus_page
328 * replacement from working properly.
330 vm_page_set_validclean(m, base, size);
332 m->dirty = VM_PAGE_BITS_ALL;
336 vp->v_filesize = nsize;
337 object->size = nobjsize;
341 vnode_pager_freepage(vm_page_t m)
347 * calculate the disk byte address of specified logical byte offset. The
348 * logical offset will be block-aligned. Return the number of contiguous
349 * pages that may be read from the underlying block device in *run. If
350 * *run is non-NULL, it will be set to a value of at least 1.
353 vnode_pager_addr(struct vnode *vp, off_t loffset, int *run)
364 if (vp->v_mount == NULL)
368 * Align loffset to a block boundary for the BMAP, then adjust the
369 * returned disk address appropriately.
371 bsize = vp->v_mount->mnt_stat.f_iosize;
372 voff = loffset % bsize;
375 * Map the block, adjust the disk offset so it represents the
376 * passed loffset rather then the block containing loffset.
378 error = VOP_BMAP(vp, loffset - voff, &rtvp, &doffset, run, NULL);
379 if (error || doffset == NOOFFSET) {
385 * When calculating *run, which is the number of pages
386 * worth of data which can be read linearly from disk,
387 * the minimum return value is 1 page.
390 *run = (*run - voff) >> PAGE_SHIFT;
400 * interrupt routine for I/O completion
403 vnode_pager_iodone(struct bio *bio)
405 struct buf *bp = bio->bio_buf;
407 bp->b_flags |= B_DONE;
412 * small block file system vnode pager input
415 vnode_pager_input_smlfs(vm_object_t object, vm_page_t m)
418 struct vnode *dp, *vp;
427 if (vp->v_mount == NULL)
430 bsize = vp->v_mount->mnt_stat.f_iosize;
433 VOP_BMAP(vp, (off_t)0, &dp, NULL, NULL, NULL);
435 sf = sf_buf_alloc(m, 0);
436 kva = sf_buf_kva(sf);
438 for (i = 0; i < PAGE_SIZE / bsize; i++) {
441 if (vm_page_bits(i * bsize, bsize) & m->valid)
444 loffset = IDX_TO_OFF(m->pindex) + i * bsize;
445 if (loffset >= vp->v_filesize) {
448 doffset = vnode_pager_addr(vp, loffset, NULL);
450 if (doffset != NOOFFSET) {
451 bp = getpbuf(&vnode_pbuf_freecnt);
453 /* build a minimal buffer header */
454 bp->b_flags = B_READ;
455 bp->b_data = (caddr_t) kva + i * bsize;
456 bp->b_bio1.bio_done = vnode_pager_iodone;
457 bp->b_bio1.bio_offset = doffset;
459 bp->b_bcount = bsize;
460 bp->b_bufsize = bsize;
461 bp->b_runningbufspace = bp->b_bufsize;
462 runningbufspace += bp->b_runningbufspace;
465 vn_strategy(dp, &bp->b_bio1);
467 /* we definitely need to be at splvm here */
470 while ((bp->b_flags & B_DONE) == 0) {
471 tsleep(bp, 0, "vnsrd", 0);
474 if ((bp->b_flags & B_ERROR) != 0)
478 * free the buffer header back to the swap buffer pool
480 relpbuf(bp, &vnode_pbuf_freecnt);
484 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
486 vm_page_set_validclean(m, (i * bsize) & PAGE_MASK, bsize);
487 bzero((caddr_t) kva + i * bsize, bsize);
491 pmap_clear_modify(m);
492 vm_page_flag_clear(m, PG_ZERO);
494 return VM_PAGER_ERROR;
502 * old style vnode pager output routine
505 vnode_pager_input_old(vm_object_t object, vm_page_t m)
519 * Return failure if beyond current EOF
521 if (IDX_TO_OFF(m->pindex) >= vp->v_filesize) {
525 if (IDX_TO_OFF(m->pindex) + size > vp->v_filesize)
526 size = vp->v_filesize - IDX_TO_OFF(m->pindex);
529 * Allocate a kernel virtual address and initialize so that
530 * we can use VOP_READ/WRITE routines.
532 sf = sf_buf_alloc(m, 0);
533 kva = sf_buf_kva(sf);
535 aiov.iov_base = (caddr_t) kva;
537 auio.uio_iov = &aiov;
539 auio.uio_offset = IDX_TO_OFF(m->pindex);
540 auio.uio_segflg = UIO_SYSSPACE;
541 auio.uio_rw = UIO_READ;
542 auio.uio_resid = size;
543 auio.uio_td = curthread;
545 error = VOP_READ(((struct vnode *)object->handle),
546 &auio, 0, proc0.p_ucred);
548 int count = size - auio.uio_resid;
552 else if (count != PAGE_SIZE)
553 bzero((caddr_t) kva + count, PAGE_SIZE - count);
557 pmap_clear_modify(m);
559 vm_page_flag_clear(m, PG_ZERO);
561 m->valid = VM_PAGE_BITS_ALL;
562 return error ? VM_PAGER_ERROR : VM_PAGER_OK;
566 * generic vnode pager input routine
570 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
571 * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to
572 * vnode_pager_generic_getpages() to implement the previous behaviour.
574 * All other FS's should use the bypass to get to the local media
575 * backing vp's VOP_GETPAGES.
578 vnode_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
582 int bytes = count * PAGE_SIZE;
586 * XXX temporary diagnostic message to help track stale FS code,
587 * Returning EOPNOTSUPP from here may make things unhappy.
589 rtval = VOP_GETPAGES(vp, m, bytes, reqpage, 0);
590 if (rtval == EOPNOTSUPP) {
591 printf("vnode_pager: *** WARNING *** stale FS getpages\n");
592 rtval = vnode_pager_generic_getpages( vp, m, bytes, reqpage);
599 * This is now called from local media FS's to operate against their
600 * own vnodes if they fail to implement VOP_GETPAGES.
603 vnode_pager_generic_getpages(struct vnode *vp, vm_page_t *m, int bytecount,
608 off_t foff, tfoff, nextoff;
609 int i, size, bsize, first;
618 object = vp->v_object;
619 count = bytecount / PAGE_SIZE;
621 if (vp->v_mount == NULL)
624 bsize = vp->v_mount->mnt_stat.f_iosize;
626 /* get the UNDERLYING device for the file with VOP_BMAP() */
629 * originally, we did not check for an error return value -- assuming
630 * an fs always has a bmap entry point -- that assumption is wrong!!!
632 foff = IDX_TO_OFF(m[reqpage]->pindex);
635 * if we can't bmap, use old VOP code
637 if (VOP_BMAP(vp, (off_t)0, &dp, NULL, NULL, NULL)) {
638 for (i = 0; i < count; i++) {
640 vnode_pager_freepage(m[i]);
643 mycpu->gd_cnt.v_vnodein++;
644 mycpu->gd_cnt.v_vnodepgsin++;
645 return vnode_pager_input_old(object, m[reqpage]);
648 * if the blocksize is smaller than a page size, then use
649 * special small filesystem code. NFS sometimes has a small
650 * blocksize, but it can handle large reads itself.
652 } else if ((PAGE_SIZE / bsize) > 1 &&
653 (vp->v_mount->mnt_stat.f_type != nfs_mount_type)) {
654 for (i = 0; i < count; i++) {
656 vnode_pager_freepage(m[i]);
659 mycpu->gd_cnt.v_vnodein++;
660 mycpu->gd_cnt.v_vnodepgsin++;
661 return vnode_pager_input_smlfs(object, m[reqpage]);
665 * If we have a completely valid page available to us, we can
666 * clean up and return. Otherwise we have to re-read the
669 * Note that this does not work with NFS, so NFS has its own
670 * getpages routine. The problem is that NFS can have partially
671 * valid pages associated with the buffer cache due to the piecemeal
672 * write support. If we were to fall through and re-read the media
673 * as we do here, dirty data could be lost.
676 if (m[reqpage]->valid == VM_PAGE_BITS_ALL) {
677 for (i = 0; i < count; i++) {
679 vnode_pager_freepage(m[i]);
683 m[reqpage]->valid = 0;
686 * here on direct device I/O
691 * calculate the run that includes the required page
693 for(first = 0, i = 0; i < count; i = runend) {
694 firstaddr = vnode_pager_addr(vp, IDX_TO_OFF(m[i]->pindex),
696 if (firstaddr == -1) {
697 if (i == reqpage && foff < vp->v_filesize) {
698 /* XXX no %qd in kernel. */
699 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %012llx, foff: 0x%012llx, v_filesize: 0x%012llx",
700 firstaddr, foff, vp->v_filesize);
702 vnode_pager_freepage(m[i]);
708 if (runend <= reqpage) {
710 for (j = i; j < runend; j++) {
711 vnode_pager_freepage(m[j]);
714 if (runpg < (count - first)) {
715 for (i = first + runpg; i < count; i++)
716 vnode_pager_freepage(m[i]);
717 count = first + runpg;
725 * the first and last page have been calculated now, move input pages
726 * to be zero based...
729 for (i = first; i < count; i++) {
737 * calculate the file virtual address for the transfer
739 foff = IDX_TO_OFF(m[0]->pindex);
742 * calculate the size of the transfer
744 size = count * PAGE_SIZE;
745 if ((foff + size) > vp->v_filesize)
746 size = vp->v_filesize - foff;
749 * round up physical size for real devices.
751 if (dp->v_type == VBLK || dp->v_type == VCHR) {
752 int secmask = dp->v_rdev->si_bsize_phys - 1;
753 KASSERT(secmask < PAGE_SIZE, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask + 1));
754 size = (size + secmask) & ~secmask;
757 bp = getpbuf(&vnode_pbuf_freecnt);
758 kva = (vm_offset_t) bp->b_data;
761 * and map the pages to be read into the kva
763 pmap_qenter(kva, m, count);
765 /* build a minimal buffer header */
766 bp->b_flags = B_READ;
767 bp->b_bio1.bio_done = vnode_pager_iodone;
768 bp->b_bio1.bio_offset = firstaddr;
771 bp->b_bufsize = size;
772 bp->b_runningbufspace = bp->b_bufsize;
773 runningbufspace += bp->b_runningbufspace;
775 mycpu->gd_cnt.v_vnodein++;
776 mycpu->gd_cnt.v_vnodepgsin += count;
779 vn_strategy(dp, &bp->b_bio1);
782 /* we definitely need to be at splvm here */
784 while ((bp->b_flags & B_DONE) == 0) {
785 tsleep(bp, 0, "vnread", 0);
788 if ((bp->b_flags & B_ERROR) != 0)
792 if (size != count * PAGE_SIZE)
793 bzero((caddr_t) kva + size, PAGE_SIZE * count - size);
795 pmap_qremove(kva, count);
798 * free the buffer header back to the swap buffer pool
800 relpbuf(bp, &vnode_pbuf_freecnt);
802 for (i = 0, tfoff = foff; i < count; i++, tfoff = nextoff) {
805 nextoff = tfoff + PAGE_SIZE;
808 if (nextoff <= vp->v_filesize) {
810 * Read filled up entire page.
812 mt->valid = VM_PAGE_BITS_ALL;
813 vm_page_undirty(mt); /* should be an assert? XXX */
814 pmap_clear_modify(mt);
817 * Read did not fill up entire page. Since this
818 * is getpages, the page may be mapped, so we have
819 * to zero the invalid portions of the page even
820 * though we aren't setting them valid.
822 * Currently we do not set the entire page valid,
823 * we just try to clear the piece that we couldn't
826 vm_page_set_validclean(mt, 0, vp->v_filesize - tfoff);
827 /* handled by vm_fault now */
828 /* vm_page_zero_invalid(mt, FALSE); */
831 vm_page_flag_clear(mt, PG_ZERO);
835 * whether or not to leave the page activated is up in
836 * the air, but we should put the page on a page queue
837 * somewhere. (it already is in the object). Result:
838 * It appears that empirical results show that
839 * deactivating pages is best.
843 * just in case someone was asking for this page we
844 * now tell them that it is ok to use
847 if (mt->flags & PG_WANTED)
848 vm_page_activate(mt);
850 vm_page_deactivate(mt);
853 vnode_pager_freepage(mt);
858 printf("vnode_pager_getpages: I/O read error\n");
860 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
864 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
865 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
866 * vnode_pager_generic_putpages() to implement the previous behaviour.
868 * All other FS's should use the bypass to get to the local media
869 * backing vp's VOP_PUTPAGES.
872 vnode_pager_putpages(vm_object_t object, vm_page_t *m, int count,
873 boolean_t sync, int *rtvals)
877 int bytes = count * PAGE_SIZE;
880 * Force synchronous operation if we are extremely low on memory
881 * to prevent a low-memory deadlock. VOP operations often need to
882 * allocate more memory to initiate the I/O ( i.e. do a BMAP
883 * operation ). The swapper handles the case by limiting the amount
884 * of asynchronous I/O, but that sort of solution doesn't scale well
885 * for the vnode pager without a lot of work.
887 * Also, the backing vnode's iodone routine may not wake the pageout
888 * daemon up. This should be probably be addressed XXX.
891 if ((vmstats.v_free_count + vmstats.v_cache_count) < vmstats.v_pageout_free_min)
895 * Call device-specific putpages function
899 rtval = VOP_PUTPAGES(vp, m, bytes, sync, rtvals, 0);
900 if (rtval == EOPNOTSUPP) {
901 printf("vnode_pager: *** WARNING *** stale FS putpages\n");
902 rtval = vnode_pager_generic_putpages( vp, m, bytes, sync, rtvals);
908 * This is now called from local media FS's to operate against their
909 * own vnodes if they fail to implement VOP_PUTPAGES.
911 * This is typically called indirectly via the pageout daemon and
912 * clustering has already typically occured, so in general we ask the
913 * underlying filesystem to write the data out asynchronously rather
917 vnode_pager_generic_putpages(struct vnode *vp, vm_page_t *m, int bytecount,
918 int flags, int *rtvals)
925 vm_ooffset_t poffset;
931 object = vp->v_object;
932 count = bytecount / PAGE_SIZE;
934 for (i = 0; i < count; i++)
935 rtvals[i] = VM_PAGER_AGAIN;
937 if ((int) m[0]->pindex < 0) {
938 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n",
939 (long)m[0]->pindex, m[0]->dirty);
940 rtvals[0] = VM_PAGER_BAD;
944 maxsize = count * PAGE_SIZE;
947 poffset = IDX_TO_OFF(m[0]->pindex);
950 * If the page-aligned write is larger then the actual file we
951 * have to invalidate pages occuring beyond the file EOF. However,
952 * there is an edge case where a file may not be page-aligned where
953 * the last page is partially invalid. In this case the filesystem
954 * may not properly clear the dirty bits for the entire page (which
955 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
956 * With the page locked we are free to fix-up the dirty bits here.
958 * We do not under any circumstances truncate the valid bits, as
959 * this will screw up bogus page replacement.
961 if (maxsize + poffset > vp->v_filesize) {
962 if (vp->v_filesize > poffset) {
965 maxsize = vp->v_filesize - poffset;
966 ncount = btoc(maxsize);
967 if ((pgoff = (int)maxsize & PAGE_MASK) != 0) {
968 vm_page_clear_dirty(m[ncount - 1], pgoff,
975 if (ncount < count) {
976 for (i = ncount; i < count; i++) {
977 rtvals[i] = VM_PAGER_BAD;
983 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
984 * rather then a bdwrite() to prevent paging I/O from saturating
985 * the buffer cache. Dummy-up the sequential heuristic to cause
986 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
987 * the system decides how to cluster.
990 if (flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL))
992 else if ((flags & VM_PAGER_CLUSTER_OK) == 0)
994 ioflags |= (flags & VM_PAGER_PUT_INVAL) ? IO_INVAL: 0;
995 ioflags |= IO_SEQMAX << IO_SEQSHIFT;
997 aiov.iov_base = (caddr_t) 0;
998 aiov.iov_len = maxsize;
999 auio.uio_iov = &aiov;
1000 auio.uio_iovcnt = 1;
1001 auio.uio_offset = poffset;
1002 auio.uio_segflg = UIO_NOCOPY;
1003 auio.uio_rw = UIO_WRITE;
1004 auio.uio_resid = maxsize;
1006 error = VOP_WRITE(vp, &auio, ioflags, proc0.p_ucred);
1007 mycpu->gd_cnt.v_vnodeout++;
1008 mycpu->gd_cnt.v_vnodepgsout += ncount;
1011 printf("vnode_pager_putpages: I/O error %d\n", error);
1013 if (auio.uio_resid) {
1014 printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1015 auio.uio_resid, (u_long)m[0]->pindex);
1017 for (i = 0; i < ncount; i++) {
1018 rtvals[i] = VM_PAGER_OK;
1024 vnode_pager_lock(vm_object_t object)
1026 struct thread *td = curthread; /* XXX */
1029 for (; object != NULL; object = object->backing_object) {
1030 if (object->type != OBJT_VNODE)
1032 if (object->flags & OBJ_DEAD)
1036 struct vnode *vp = object->handle;
1037 error = vget(vp, LK_NOPAUSE | LK_SHARED |
1038 LK_RETRY | LK_CANRECURSE, td);
1040 if (object->handle != vp) {
1046 if ((object->flags & OBJ_DEAD) ||
1047 (object->type != OBJT_VNODE)) {
1050 printf("vnode_pager_lock: vp %p error %d lockstatus %d, retrying\n", vp, error, lockstatus(&vp->v_lock, td));
1051 tsleep(object->handle, 0, "vnpgrl", hz);