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.18 2005/03/02 18:42:09 hmp 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>
67 #include <vm/vm_object.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_pager.h>
70 #include <vm/vm_map.h>
71 #include <vm/vnode_pager.h>
72 #include <vm/vm_extern.h>
74 static vm_offset_t
vnode_pager_addr (struct vnode
*vp
, vm_ooffset_t address
,
76 static void vnode_pager_iodone (struct buf
*bp
);
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
, vm_ooffset_t size
, vm_prot_t prot
,
108 * Pageout to vnode, no can do yet.
114 * XXX hack - This initialization should be put somewhere else.
116 if (vnode_pbuf_freecnt
< 0) {
117 vnode_pbuf_freecnt
= nswbuf
/ 2 + 1;
120 vp
= (struct vnode
*) handle
;
123 * Prevent race condition when allocating the object. This
124 * can happen with NFS vnodes since the nfsnode isn't locked.
126 while (vp
->v_flag
& VOLOCK
) {
127 vp
->v_flag
|= VOWANT
;
128 tsleep(vp
, 0, "vnpobj", 0);
130 vp
->v_flag
|= VOLOCK
;
133 * If the object is being terminated, wait for it to
136 while (((object
= vp
->v_object
) != NULL
) &&
137 (object
->flags
& OBJ_DEAD
)) {
138 tsleep(object
, 0, "vadead", 0);
141 if (vp
->v_usecount
== 0)
142 panic("vnode_pager_alloc: no vnode reference");
144 if (object
== NULL
) {
146 * And an object of the appropriate size
148 object
= vm_object_allocate(OBJT_VNODE
, OFF_TO_IDX(round_page(size
)));
151 object
->un_pager
.vnp
.vnp_size
= size
;
153 object
->handle
= handle
;
154 vp
->v_object
= object
;
161 vp
->v_flag
&= ~VOLOCK
;
162 if (vp
->v_flag
& VOWANT
) {
163 vp
->v_flag
&= ~VOWANT
;
170 vnode_pager_dealloc(vm_object_t object
)
172 struct vnode
*vp
= object
->handle
;
175 panic("vnode_pager_dealloc: pager already dealloced");
177 vm_object_pip_wait(object
, "vnpdea");
179 object
->handle
= NULL
;
180 object
->type
= OBJT_DEAD
;
182 vp
->v_flag
&= ~(VTEXT
| VOBJBUF
);
186 vnode_pager_haspage(vm_object_t object
, vm_pindex_t pindex
, int *before
,
189 struct vnode
*vp
= object
->handle
;
195 int pagesperblock
, blocksperpage
;
198 * If no vp or vp is doomed or marked transparent to VM, we do not
201 if ((vp
== NULL
) || (vp
->v_flag
& VRECLAIMED
))
205 * If filesystem no longer mounted or offset beyond end of file we do
208 if ((vp
->v_mount
== NULL
) ||
209 (IDX_TO_OFF(pindex
) >= object
->un_pager
.vnp
.vnp_size
))
212 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
213 pagesperblock
= bsize
/ PAGE_SIZE
;
215 if (pagesperblock
> 0) {
216 reqblock
= pindex
/ pagesperblock
;
218 blocksperpage
= (PAGE_SIZE
/ bsize
);
219 reqblock
= pindex
* blocksperpage
;
221 err
= VOP_BMAP(vp
, reqblock
, (struct vnode
**) 0, &bn
,
227 if (pagesperblock
> 0) {
228 poff
= pindex
- (reqblock
* pagesperblock
);
230 *before
*= pagesperblock
;
235 *after
*= pagesperblock
;
236 numafter
= pagesperblock
- (poff
+ 1);
237 if (IDX_TO_OFF(pindex
+ numafter
) > object
->un_pager
.vnp
.vnp_size
) {
238 numafter
= OFF_TO_IDX((object
->un_pager
.vnp
.vnp_size
- IDX_TO_OFF(pindex
)));
244 *before
/= blocksperpage
;
248 *after
/= blocksperpage
;
255 * Lets the VM system know about a change in size for a file.
256 * We adjust our own internal size and flush any cached pages in
257 * the associated object that are affected by the size change.
259 * Note: this routine may be invoked as a result of a pager put
260 * operation (possibly at object termination time), so we must be careful.
263 vnode_pager_setsize(struct vnode
*vp
, vm_ooffset_t nsize
)
265 vm_pindex_t nobjsize
;
266 vm_object_t object
= vp
->v_object
;
272 * Hasn't changed size
274 if (nsize
== object
->un_pager
.vnp
.vnp_size
)
277 nobjsize
= OFF_TO_IDX(nsize
+ PAGE_MASK
);
280 * File has shrunk. Toss any cached pages beyond the new EOF.
282 if (nsize
< object
->un_pager
.vnp
.vnp_size
) {
283 vm_freeze_copyopts(object
, OFF_TO_IDX(nsize
), object
->size
);
284 if (nobjsize
< object
->size
) {
285 vm_object_page_remove(object
, nobjsize
, object
->size
,
289 * This gets rid of garbage at the end of a page that is now
290 * only partially backed by the vnode. Since we are setting
291 * the entire page valid & clean after we are done we have
292 * to be sure that the portion of the page within the file
293 * bounds is already valid. If it isn't then making it
294 * valid would create a corrupt block.
296 if (nsize
& PAGE_MASK
) {
300 m
= vm_page_lookup(object
, OFF_TO_IDX(nsize
));
302 int base
= (int)nsize
& PAGE_MASK
;
303 int size
= PAGE_SIZE
- base
;
307 * Clear out partial-page garbage in case
308 * the page has been mapped.
310 sf
= sf_buf_alloc(m
, SFB_CPUPRIVATE
);
311 kva
= sf_buf_kva(sf
);
312 bzero((caddr_t
)kva
+ base
, size
);
316 * XXX work around SMP data integrity race
317 * by unmapping the page from user processes.
318 * The garbage we just cleared may be mapped
319 * to a user process running on another cpu
320 * and this code is not running through normal
321 * I/O channels which handle SMP issues for
322 * us, so unmap page to synchronize all cpus.
324 * XXX should vm_pager_unmap_page() have
327 vm_page_protect(m
, VM_PROT_NONE
);
330 * Clear out partial-page dirty bits. This
331 * has the side effect of setting the valid
332 * bits, but that is ok. There are a bunch
333 * of places in the VM system where we expected
334 * m->dirty == VM_PAGE_BITS_ALL. The file EOF
335 * case is one of them. If the page is still
336 * partially dirty, make it fully dirty.
338 * note that we do not clear out the valid
339 * bits. This would prevent bogus_page
340 * replacement from working properly.
342 vm_page_set_validclean(m
, base
, size
);
344 m
->dirty
= VM_PAGE_BITS_ALL
;
348 object
->un_pager
.vnp
.vnp_size
= nsize
;
349 object
->size
= nobjsize
;
353 vnode_pager_freepage(vm_page_t m
)
359 * calculate the linear (byte) disk address of specified virtual
363 vnode_pager_addr(struct vnode
*vp
, vm_ooffset_t address
, int *run
)
376 if (vp
->v_mount
== NULL
)
379 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
380 vblock
= address
/ bsize
;
381 voffset
= address
% bsize
;
383 err
= VOP_BMAP(vp
, vblock
, &rtvp
, &block
, run
, NULL
);
385 if (err
|| (block
== -1))
388 rtaddress
= block
+ voffset
/ DEV_BSIZE
;
391 *run
*= bsize
/PAGE_SIZE
;
392 *run
-= voffset
/PAGE_SIZE
;
400 * interrupt routine for I/O completion
403 vnode_pager_iodone(struct buf
*bp
)
405 bp
->b_flags
|= B_DONE
;
410 * small block file system vnode pager input
413 vnode_pager_input_smlfs(vm_object_t object
, vm_page_t m
)
417 struct vnode
*dp
, *vp
;
426 if (vp
->v_mount
== NULL
)
429 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
432 VOP_BMAP(vp
, 0, &dp
, 0, NULL
, NULL
);
434 sf
= sf_buf_alloc(m
, 0);
435 kva
= sf_buf_kva(sf
);
437 for (i
= 0; i
< PAGE_SIZE
/ bsize
; i
++) {
438 vm_ooffset_t address
;
440 if (vm_page_bits(i
* bsize
, bsize
) & m
->valid
)
443 address
= IDX_TO_OFF(m
->pindex
) + i
* bsize
;
444 if (address
>= object
->un_pager
.vnp
.vnp_size
) {
447 fileaddr
= vnode_pager_addr(vp
, address
, NULL
);
449 if (fileaddr
!= -1) {
450 bp
= getpbuf(&vnode_pbuf_freecnt
);
452 /* build a minimal buffer header */
453 bp
->b_flags
= B_READ
| B_CALL
;
454 bp
->b_iodone
= vnode_pager_iodone
;
455 bp
->b_data
= (caddr_t
) kva
+ i
* bsize
;
456 bp
->b_blkno
= fileaddr
;
458 bp
->b_bcount
= bsize
;
459 bp
->b_bufsize
= bsize
;
460 bp
->b_runningbufspace
= bp
->b_bufsize
;
461 runningbufspace
+= bp
->b_runningbufspace
;
464 VOP_STRATEGY(bp
->b_vp
, bp
);
466 /* we definitely need to be at splvm here */
469 while ((bp
->b_flags
& B_DONE
) == 0) {
470 tsleep(bp
, 0, "vnsrd", 0);
473 if ((bp
->b_flags
& B_ERROR
) != 0)
477 * free the buffer header back to the swap buffer pool
479 relpbuf(bp
, &vnode_pbuf_freecnt
);
483 vm_page_set_validclean(m
, (i
* bsize
) & PAGE_MASK
, bsize
);
485 vm_page_set_validclean(m
, (i
* bsize
) & PAGE_MASK
, bsize
);
486 bzero((caddr_t
) kva
+ i
* bsize
, bsize
);
490 pmap_clear_modify(m
);
491 vm_page_flag_clear(m
, PG_ZERO
);
493 return VM_PAGER_ERROR
;
501 * old style vnode pager output routine
504 vnode_pager_input_old(vm_object_t object
, vm_page_t m
)
516 * Return failure if beyond current EOF
518 if (IDX_TO_OFF(m
->pindex
) >= object
->un_pager
.vnp
.vnp_size
) {
522 if (IDX_TO_OFF(m
->pindex
) + size
> object
->un_pager
.vnp
.vnp_size
)
523 size
= object
->un_pager
.vnp
.vnp_size
- IDX_TO_OFF(m
->pindex
);
526 * Allocate a kernel virtual address and initialize so that
527 * we can use VOP_READ/WRITE routines.
529 sf
= sf_buf_alloc(m
, 0);
530 kva
= sf_buf_kva(sf
);
532 aiov
.iov_base
= (caddr_t
) kva
;
534 auio
.uio_iov
= &aiov
;
536 auio
.uio_offset
= IDX_TO_OFF(m
->pindex
);
537 auio
.uio_segflg
= UIO_SYSSPACE
;
538 auio
.uio_rw
= UIO_READ
;
539 auio
.uio_resid
= size
;
540 auio
.uio_td
= curthread
;
542 error
= VOP_READ(((struct vnode
*)object
->handle
),
543 &auio
, 0, proc0
.p_ucred
);
545 int count
= size
- auio
.uio_resid
;
549 else if (count
!= PAGE_SIZE
)
550 bzero((caddr_t
) kva
+ count
, PAGE_SIZE
- count
);
554 pmap_clear_modify(m
);
556 vm_page_flag_clear(m
, PG_ZERO
);
558 m
->valid
= VM_PAGE_BITS_ALL
;
559 return error
? VM_PAGER_ERROR
: VM_PAGER_OK
;
563 * generic vnode pager input routine
567 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
568 * implement their own VOP_GETPAGES, their VOP_GETPAGES should call to
569 * vnode_pager_generic_getpages() to implement the previous behaviour.
571 * All other FS's should use the bypass to get to the local media
572 * backing vp's VOP_GETPAGES.
575 vnode_pager_getpages(vm_object_t object
, vm_page_t
*m
, int count
, int reqpage
)
579 int bytes
= count
* PAGE_SIZE
;
583 * XXX temporary diagnostic message to help track stale FS code,
584 * Returning EOPNOTSUPP from here may make things unhappy.
586 rtval
= VOP_GETPAGES(vp
, m
, bytes
, reqpage
, 0);
587 if (rtval
== EOPNOTSUPP
) {
588 printf("vnode_pager: *** WARNING *** stale FS getpages\n");
589 rtval
= vnode_pager_generic_getpages( vp
, m
, bytes
, reqpage
);
596 * This is now called from local media FS's to operate against their
597 * own vnodes if they fail to implement VOP_GETPAGES.
600 vnode_pager_generic_getpages(struct vnode
*vp
, vm_page_t
*m
, int bytecount
,
605 off_t foff
, tfoff
, nextoff
;
606 int i
, size
, bsize
, first
, firstaddr
;
615 object
= vp
->v_object
;
616 count
= bytecount
/ PAGE_SIZE
;
618 if (vp
->v_mount
== NULL
)
621 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
623 /* get the UNDERLYING device for the file with VOP_BMAP() */
626 * originally, we did not check for an error return value -- assuming
627 * an fs always has a bmap entry point -- that assumption is wrong!!!
629 foff
= IDX_TO_OFF(m
[reqpage
]->pindex
);
632 * if we can't bmap, use old VOP code
634 if (VOP_BMAP(vp
, 0, &dp
, 0, NULL
, NULL
)) {
635 for (i
= 0; i
< count
; i
++) {
637 vnode_pager_freepage(m
[i
]);
640 mycpu
->gd_cnt
.v_vnodein
++;
641 mycpu
->gd_cnt
.v_vnodepgsin
++;
642 return vnode_pager_input_old(object
, m
[reqpage
]);
645 * if the blocksize is smaller than a page size, then use
646 * special small filesystem code. NFS sometimes has a small
647 * blocksize, but it can handle large reads itself.
649 } else if ((PAGE_SIZE
/ bsize
) > 1 &&
650 (vp
->v_mount
->mnt_stat
.f_type
!= nfs_mount_type
)) {
651 for (i
= 0; i
< count
; i
++) {
653 vnode_pager_freepage(m
[i
]);
656 mycpu
->gd_cnt
.v_vnodein
++;
657 mycpu
->gd_cnt
.v_vnodepgsin
++;
658 return vnode_pager_input_smlfs(object
, m
[reqpage
]);
662 * If we have a completely valid page available to us, we can
663 * clean up and return. Otherwise we have to re-read the
666 * Note that this does not work with NFS, so NFS has its own
667 * getpages routine. The problem is that NFS can have partially
668 * valid pages associated with the buffer cache due to the piecemeal
669 * write support. If we were to fall through and re-read the media
670 * as we do here, dirty data could be lost.
673 if (m
[reqpage
]->valid
== VM_PAGE_BITS_ALL
) {
674 for (i
= 0; i
< count
; i
++) {
676 vnode_pager_freepage(m
[i
]);
680 m
[reqpage
]->valid
= 0;
683 * here on direct device I/O
688 * calculate the run that includes the required page
690 for(first
= 0, i
= 0; i
< count
; i
= runend
) {
691 firstaddr
= vnode_pager_addr(vp
,
692 IDX_TO_OFF(m
[i
]->pindex
), &runpg
);
693 if (firstaddr
== -1) {
694 if (i
== reqpage
&& foff
< object
->un_pager
.vnp
.vnp_size
) {
695 /* XXX no %qd in kernel. */
696 panic("vnode_pager_getpages: unexpected missing page: firstaddr: %d, foff: 0x%lx%08lx, vnp_size: 0x%lx%08lx",
697 firstaddr
, (u_long
)(foff
>> 32),
698 (u_long
)(u_int32_t
)foff
,
700 (object
->un_pager
.vnp
.vnp_size
>> 32),
702 object
->un_pager
.vnp
.vnp_size
);
704 vnode_pager_freepage(m
[i
]);
710 if (runend
<= reqpage
) {
712 for (j
= i
; j
< runend
; j
++) {
713 vnode_pager_freepage(m
[j
]);
716 if (runpg
< (count
- first
)) {
717 for (i
= first
+ runpg
; i
< count
; i
++)
718 vnode_pager_freepage(m
[i
]);
719 count
= first
+ runpg
;
727 * the first and last page have been calculated now, move input pages
728 * to be zero based...
731 for (i
= first
; i
< count
; i
++) {
739 * calculate the file virtual address for the transfer
741 foff
= IDX_TO_OFF(m
[0]->pindex
);
744 * calculate the size of the transfer
746 size
= count
* PAGE_SIZE
;
747 if ((foff
+ size
) > object
->un_pager
.vnp
.vnp_size
)
748 size
= object
->un_pager
.vnp
.vnp_size
- foff
;
751 * round up physical size for real devices.
753 if (dp
->v_type
== VBLK
|| dp
->v_type
== VCHR
) {
754 int secmask
= dp
->v_rdev
->si_bsize_phys
- 1;
755 KASSERT(secmask
< PAGE_SIZE
, ("vnode_pager_generic_getpages: sector size %d too large\n", secmask
+ 1));
756 size
= (size
+ secmask
) & ~secmask
;
759 bp
= getpbuf(&vnode_pbuf_freecnt
);
760 kva
= (vm_offset_t
) bp
->b_data
;
763 * and map the pages to be read into the kva
765 pmap_qenter(kva
, m
, count
);
767 /* build a minimal buffer header */
768 bp
->b_flags
= B_READ
| B_CALL
;
769 bp
->b_iodone
= vnode_pager_iodone
;
770 /* B_PHYS is not set, but it is nice to fill this in */
771 bp
->b_blkno
= firstaddr
;
774 bp
->b_bufsize
= size
;
775 bp
->b_runningbufspace
= bp
->b_bufsize
;
776 runningbufspace
+= bp
->b_runningbufspace
;
778 mycpu
->gd_cnt
.v_vnodein
++;
779 mycpu
->gd_cnt
.v_vnodepgsin
+= count
;
782 VOP_STRATEGY(bp
->b_vp
, bp
);
785 /* we definitely need to be at splvm here */
787 while ((bp
->b_flags
& B_DONE
) == 0) {
788 tsleep(bp
, 0, "vnread", 0);
791 if ((bp
->b_flags
& B_ERROR
) != 0)
795 if (size
!= count
* PAGE_SIZE
)
796 bzero((caddr_t
) kva
+ size
, PAGE_SIZE
* count
- size
);
798 pmap_qremove(kva
, count
);
801 * free the buffer header back to the swap buffer pool
803 relpbuf(bp
, &vnode_pbuf_freecnt
);
805 for (i
= 0, tfoff
= foff
; i
< count
; i
++, tfoff
= nextoff
) {
808 nextoff
= tfoff
+ PAGE_SIZE
;
811 if (nextoff
<= object
->un_pager
.vnp
.vnp_size
) {
813 * Read filled up entire page.
815 mt
->valid
= VM_PAGE_BITS_ALL
;
816 vm_page_undirty(mt
); /* should be an assert? XXX */
817 pmap_clear_modify(mt
);
820 * Read did not fill up entire page. Since this
821 * is getpages, the page may be mapped, so we have
822 * to zero the invalid portions of the page even
823 * though we aren't setting them valid.
825 * Currently we do not set the entire page valid,
826 * we just try to clear the piece that we couldn't
829 vm_page_set_validclean(mt
, 0,
830 object
->un_pager
.vnp
.vnp_size
- tfoff
);
831 /* handled by vm_fault now */
832 /* vm_page_zero_invalid(mt, FALSE); */
835 vm_page_flag_clear(mt
, PG_ZERO
);
839 * whether or not to leave the page activated is up in
840 * the air, but we should put the page on a page queue
841 * somewhere. (it already is in the object). Result:
842 * It appears that empirical results show that
843 * deactivating pages is best.
847 * just in case someone was asking for this page we
848 * now tell them that it is ok to use
851 if (mt
->flags
& PG_WANTED
)
852 vm_page_activate(mt
);
854 vm_page_deactivate(mt
);
857 vnode_pager_freepage(mt
);
862 printf("vnode_pager_getpages: I/O read error\n");
864 return (error
? VM_PAGER_ERROR
: VM_PAGER_OK
);
868 * EOPNOTSUPP is no longer legal. For local media VFS's that do not
869 * implement their own VOP_PUTPAGES, their VOP_PUTPAGES should call to
870 * vnode_pager_generic_putpages() to implement the previous behaviour.
872 * All other FS's should use the bypass to get to the local media
873 * backing vp's VOP_PUTPAGES.
876 vnode_pager_putpages(vm_object_t object
, vm_page_t
*m
, int count
,
877 boolean_t sync
, int *rtvals
)
881 int bytes
= count
* PAGE_SIZE
;
884 * Force synchronous operation if we are extremely low on memory
885 * to prevent a low-memory deadlock. VOP operations often need to
886 * allocate more memory to initiate the I/O ( i.e. do a BMAP
887 * operation ). The swapper handles the case by limiting the amount
888 * of asynchronous I/O, but that sort of solution doesn't scale well
889 * for the vnode pager without a lot of work.
891 * Also, the backing vnode's iodone routine may not wake the pageout
892 * daemon up. This should be probably be addressed XXX.
895 if ((vmstats
.v_free_count
+ vmstats
.v_cache_count
) < vmstats
.v_pageout_free_min
)
899 * Call device-specific putpages function
903 rtval
= VOP_PUTPAGES(vp
, m
, bytes
, sync
, rtvals
, 0);
904 if (rtval
== EOPNOTSUPP
) {
905 printf("vnode_pager: *** WARNING *** stale FS putpages\n");
906 rtval
= vnode_pager_generic_putpages( vp
, m
, bytes
, sync
, rtvals
);
912 * This is now called from local media FS's to operate against their
913 * own vnodes if they fail to implement VOP_PUTPAGES.
915 * This is typically called indirectly via the pageout daemon and
916 * clustering has already typically occured, so in general we ask the
917 * underlying filesystem to write the data out asynchronously rather
921 vnode_pager_generic_putpages(struct vnode
*vp
, vm_page_t
*m
, int bytecount
,
922 int flags
, int *rtvals
)
929 vm_ooffset_t poffset
;
935 object
= vp
->v_object
;
936 count
= bytecount
/ PAGE_SIZE
;
938 for (i
= 0; i
< count
; i
++)
939 rtvals
[i
] = VM_PAGER_AGAIN
;
941 if ((int) m
[0]->pindex
< 0) {
942 printf("vnode_pager_putpages: attempt to write meta-data!!! -- 0x%lx(%x)\n",
943 (long)m
[0]->pindex
, m
[0]->dirty
);
944 rtvals
[0] = VM_PAGER_BAD
;
948 maxsize
= count
* PAGE_SIZE
;
951 poffset
= IDX_TO_OFF(m
[0]->pindex
);
954 * If the page-aligned write is larger then the actual file we
955 * have to invalidate pages occuring beyond the file EOF. However,
956 * there is an edge case where a file may not be page-aligned where
957 * the last page is partially invalid. In this case the filesystem
958 * may not properly clear the dirty bits for the entire page (which
959 * could be VM_PAGE_BITS_ALL due to the page having been mmap()d).
960 * With the page locked we are free to fix-up the dirty bits here.
962 * We do not under any circumstances truncate the valid bits, as
963 * this will screw up bogus page replacement.
965 if (maxsize
+ poffset
> object
->un_pager
.vnp
.vnp_size
) {
966 if (object
->un_pager
.vnp
.vnp_size
> poffset
) {
969 maxsize
= object
->un_pager
.vnp
.vnp_size
- poffset
;
970 ncount
= btoc(maxsize
);
971 if ((pgoff
= (int)maxsize
& PAGE_MASK
) != 0) {
972 vm_page_clear_dirty(m
[ncount
- 1], pgoff
,
979 if (ncount
< count
) {
980 for (i
= ncount
; i
< count
; i
++) {
981 rtvals
[i
] = VM_PAGER_BAD
;
987 * pageouts are already clustered, use IO_ASYNC to force a bawrite()
988 * rather then a bdwrite() to prevent paging I/O from saturating
989 * the buffer cache. Dummy-up the sequential heuristic to cause
990 * large ranges to cluster. If neither IO_SYNC or IO_ASYNC is set,
991 * the system decides how to cluster.
994 if (flags
& (VM_PAGER_PUT_SYNC
| VM_PAGER_PUT_INVAL
))
996 else if ((flags
& VM_PAGER_CLUSTER_OK
) == 0)
998 ioflags
|= (flags
& VM_PAGER_PUT_INVAL
) ? IO_INVAL
: 0;
999 ioflags
|= IO_SEQMAX
<< IO_SEQSHIFT
;
1001 aiov
.iov_base
= (caddr_t
) 0;
1002 aiov
.iov_len
= maxsize
;
1003 auio
.uio_iov
= &aiov
;
1004 auio
.uio_iovcnt
= 1;
1005 auio
.uio_offset
= poffset
;
1006 auio
.uio_segflg
= UIO_NOCOPY
;
1007 auio
.uio_rw
= UIO_WRITE
;
1008 auio
.uio_resid
= maxsize
;
1010 error
= VOP_WRITE(vp
, &auio
, ioflags
, proc0
.p_ucred
);
1011 mycpu
->gd_cnt
.v_vnodeout
++;
1012 mycpu
->gd_cnt
.v_vnodepgsout
+= ncount
;
1015 printf("vnode_pager_putpages: I/O error %d\n", error
);
1017 if (auio
.uio_resid
) {
1018 printf("vnode_pager_putpages: residual I/O %d at %lu\n",
1019 auio
.uio_resid
, (u_long
)m
[0]->pindex
);
1021 for (i
= 0; i
< ncount
; i
++) {
1022 rtvals
[i
] = VM_PAGER_OK
;
1028 vnode_pager_lock(vm_object_t object
)
1030 struct thread
*td
= curthread
; /* XXX */
1033 for (; object
!= NULL
; object
= object
->backing_object
) {
1034 if (object
->type
!= OBJT_VNODE
)
1036 if (object
->flags
& OBJ_DEAD
)
1040 struct vnode
*vp
= object
->handle
;
1041 error
= vget(vp
, LK_NOPAUSE
| LK_SHARED
|
1042 LK_RETRY
| LK_CANRECURSE
, td
);
1044 if (object
->handle
!= vp
) {
1050 if ((object
->flags
& OBJ_DEAD
) ||
1051 (object
->type
!= OBJT_VNODE
)) {
1054 printf("vnode_pager_lock: vp %p error %d lockstatus %d, retrying\n", vp
, error
, lockstatus(&vp
->v_lock
, td
));
1055 tsleep(object
->handle
, 0, "vnpgrl", hz
);