2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1987 Carnegie-Mellon University
31 * All rights reserved. The CMU software License Agreement specifies
32 * the terms and conditions for use and redistribution.
37 * "Swap" pager that pages to/from vnodes. Also
38 * handles demand paging from files.
42 #include <mach/boolean.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/kauth.h>
50 #include <sys/vnode_internal.h>
51 #include <sys/namei.h>
52 #include <sys/mount_internal.h> /* needs internal due to fhandle_t */
53 #include <sys/ubc_internal.h>
56 #include <mach/mach_types.h>
57 #include <mach/memory_object_types.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_kern.h>
61 #include <kern/zalloc.h>
62 #include <kern/kalloc.h>
63 #include <libkern/libkern.h>
65 #include <vm/vnode_pager.h>
66 #include <vm/vm_pageout.h>
68 #include <kern/assert.h>
69 #include <sys/kdebug.h>
70 #include <machine/spl.h>
72 #include <nfs/rpcv2.h>
73 #include <nfs/nfsproto.h>
76 #include <vm/vm_protos.h>
78 unsigned int vp_pagein
=0;
79 unsigned int vp_pgodirty
=0;
80 unsigned int vp_pgoclean
=0;
81 unsigned int dp_pgouts
=0; /* Default pager pageouts */
82 unsigned int dp_pgins
=0; /* Default pager pageins */
85 vnode_pager_get_filesize(struct vnode
*vp
)
88 return (vm_object_offset_t
) ubc_getsize(vp
);
92 vnode_pager_get_pathname(
99 len
= (int) *length_p
;
100 error
= vn_getpath(vp
, pathname
, &len
);
104 *length_p
= (vm_size_t
) len
;
109 vnode_pager_get_filename(
113 *filename
= vp
->v_name
;
118 vnode_pageout(struct vnode
*vp
,
120 vm_offset_t upl_offset
,
121 vm_object_offset_t f_offset
,
126 struct proc
*p
= current_proc();
127 int result
= PAGER_SUCCESS
;
136 struct vfs_context context
;
139 context
.vc_ucred
= kauth_cred_get();
144 result
= PAGER_ERROR
;
148 UBCINFOCHECK("vnode_pageout", vp
);
150 if (UBCINVALID(vp
)) {
151 result
= PAGER_ERROR
;
154 if (upl
&& !(flags
& UPL_NOCOMMIT
))
155 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
158 if ( !(flags
& UPL_VNODE_PAGER
)) {
160 * This is a pageout from the default pager,
161 * just go ahead and call vnop_pageout since
162 * it has already sorted out the dirty ranges
166 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
169 if ( (error_ret
= VNOP_PAGEOUT(vp
, upl
, upl_offset
, (off_t
)f_offset
,
170 (size_t)size
, flags
, &context
)) )
171 result
= PAGER_ERROR
;
173 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
179 * we come here for pageouts to 'real' files and
180 * for msyncs... the upl may not contain any
181 * dirty pages.. it's our responsibility to sort
182 * through it and find the 'runs' of dirty pages
183 * to call VNOP_PAGEOUT on...
185 pl
= ubc_upl_pageinfo(upl
);
187 if (ubc_getsize(vp
) == 0) {
189 * if the file has been effectively deleted, then
190 * we need to go through the UPL and invalidate any
191 * buffer headers we might have that reference any
194 for (offset
= upl_offset
; isize
; isize
-= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
196 if (vp
->v_tag
== VT_NFS
)
197 /* check with nfs if page is OK to drop */
198 error
= nfs_buf_page_inval(vp
, (off_t
)f_offset
);
202 blkno
= ubc_offtoblk(vp
, (off_t
)f_offset
);
203 error
= buf_invalblkno(vp
, blkno
, 0);
206 if ( !(flags
& UPL_NOCOMMIT
))
207 ubc_upl_abort_range(upl
, offset
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
210 result
= PAGER_ERROR
;
212 } else if ( !(flags
& UPL_NOCOMMIT
)) {
213 ubc_upl_commit_range(upl
, offset
, PAGE_SIZE
, UPL_COMMIT_FREE_ON_EMPTY
);
215 f_offset
+= PAGE_SIZE
;
220 * Ignore any non-present pages at the end of the
221 * UPL so that we aren't looking at a upl that
222 * may already have been freed by the preceeding
223 * aborts/completions.
225 base_index
= upl_offset
/ PAGE_SIZE
;
227 for (pg_index
= (upl_offset
+ isize
) / PAGE_SIZE
; pg_index
> base_index
;) {
228 if (upl_page_present(pl
, --pg_index
))
230 if (pg_index
== base_index
) {
232 * no pages were returned, so release
233 * our hold on the upl and leave
235 if ( !(flags
& UPL_NOCOMMIT
))
236 ubc_upl_abort_range(upl
, upl_offset
, isize
, UPL_ABORT_FREE_ON_EMPTY
);
241 isize
= (pg_index
+ 1) * PAGE_SIZE
;
244 pg_index
= base_index
;
250 if ( !upl_page_present(pl
, pg_index
)) {
252 * we asked for RET_ONLY_DIRTY, so it's possible
253 * to get back empty slots in the UPL
254 * just skip over them
262 if ( !upl_dirty_page(pl
, pg_index
)) {
264 * if the page is not dirty and reached here it is
265 * marked precious or it is due to invalidation in
266 * memory_object_lock request as part of truncation
267 * We also get here from vm_object_terminate()
268 * So all you need to do in these
269 * cases is to invalidate incore buffer if it is there
270 * Note we must not sleep here if the buffer is busy - that is
271 * a lock inversion which causes deadlock.
276 if (vp
->v_tag
== VT_NFS
)
277 /* check with nfs if page is OK to drop */
278 error
= nfs_buf_page_inval(vp
, (off_t
)(f_offset
+ offset
));
282 blkno
= ubc_offtoblk(vp
, (off_t
)(f_offset
+ offset
));
283 error
= buf_invalblkno(vp
, blkno
, 0);
286 if ( !(flags
& UPL_NOCOMMIT
))
287 ubc_upl_abort_range(upl
, offset
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
290 result
= PAGER_ERROR
;
292 } else if ( !(flags
& UPL_NOCOMMIT
)) {
293 ubc_upl_commit_range(upl
, offset
, PAGE_SIZE
, UPL_COMMIT_FREE_ON_EMPTY
);
304 xsize
= isize
- PAGE_SIZE
;
307 if ( !upl_dirty_page(pl
, pg_index
+ num_of_pages
))
312 xsize
= num_of_pages
* PAGE_SIZE
;
314 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
315 xsize
, (int)(f_offset
+ offset
), 0, 0, 0);
317 if ( (error
= VNOP_PAGEOUT(vp
, upl
, (vm_offset_t
)offset
,
318 (off_t
)(f_offset
+ offset
), xsize
,
319 flags
, &context
)) ) {
322 result
= PAGER_ERROR
;
324 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
329 pg_index
+= num_of_pages
;
345 vm_offset_t upl_offset
,
346 vm_object_offset_t f_offset
,
351 struct proc
*p
= current_proc();
354 int result
= PAGER_SUCCESS
;
361 int abort_needed
= 1;
364 UBCINFOCHECK("vnode_pagein", vp
);
366 if (UBCINVALID(vp
)) {
367 result
= PAGER_ERROR
;
369 if (upl
&& !(flags
& UPL_NOCOMMIT
)) {
370 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
374 if (upl
== (upl_t
)NULL
) {
375 if (size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
376 result
= PAGER_ERROR
;
380 ubc_create_upl(vp
, f_offset
, size
, &upl
, &pl
, UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
);
382 if (upl
== (upl_t
)NULL
) {
383 result
= PAGER_ABSENT
;
384 error
= PAGER_ABSENT
;
389 * if we get here, we've created the upl and
390 * are responsible for commiting/aborting it
391 * regardless of what the caller has passed in
393 flags
&= ~UPL_NOCOMMIT
;
397 pl
= ubc_upl_pageinfo(upl
);
401 pages_in_upl
= size
/ PAGE_SIZE
;
402 first_pg
= upl_offset
/ PAGE_SIZE
;
405 * before we start marching forward, we must make sure we end on
406 * a present page, otherwise we will be working with a freed
409 for (last_pg
= pages_in_upl
- 1; last_pg
>= first_pg
; last_pg
--) {
410 if (upl_page_present(pl
, last_pg
))
413 pages_in_upl
= last_pg
+ 1;
415 for (last_pg
= first_pg
; last_pg
< pages_in_upl
;) {
417 * scan the upl looking for the next
418 * page that is present.... if all of the
419 * pages are absent, we're done
421 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
422 if (upl_page_present(pl
, last_pg
))
425 if (last_pg
== pages_in_upl
)
429 * if we get here, we've sitting on a page
430 * that is present... we want to skip over
431 * any range of 'valid' pages... if this takes
432 * us to the end of the request, than we're done
434 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
435 if (!upl_valid_page(pl
, last_pg
) || !upl_page_present(pl
, last_pg
))
438 if (last_pg
> start_pg
) {
440 * we've found a range of valid pages
441 * if we've got COMMIT responsibility
442 * commit this range of pages back to the
445 xsize
= (last_pg
- start_pg
) * PAGE_SIZE
;
447 if (!(flags
& UPL_NOCOMMIT
))
448 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, xsize
, UPL_ABORT_FREE_ON_EMPTY
);
452 if (last_pg
== pages_in_upl
)
455 if (!upl_page_present(pl
, last_pg
))
457 * if we found a range of valid pages
458 * terminated by a non-present page
464 * scan from the found invalid page looking for a valid
465 * or non-present page before the end of the upl is reached, if we
466 * find one, then it will be the last page of the request to
469 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
470 if (upl_valid_page(pl
, last_pg
) || !upl_page_present(pl
, last_pg
))
473 if (last_pg
> start_pg
) {
475 struct vfs_context context
;
478 context
.vc_ucred
= kauth_cred_get();
479 xsize
= (last_pg
- start_pg
) * PAGE_SIZE
;
480 xoff
= start_pg
* PAGE_SIZE
;
482 if ( (error
= VNOP_PAGEIN(vp
, upl
, (vm_offset_t
) xoff
,
483 (off_t
)f_offset
+ xoff
,
484 xsize
, flags
, &context
)) ) {
485 result
= PAGER_ERROR
;
492 if (!(flags
& UPL_NOCOMMIT
) && abort_needed
)
493 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
498 ut
= get_bsdthread_info(current_thread());
500 if (ut
->uu_lowpri_delay
) {
502 * task is marked as a low priority I/O type
503 * and the I/O we issued while in this system call
504 * collided with normal I/O operations... we'll
505 * delay in order to mitigate the impact of this
506 * task on the normal operation of the system
508 IOSleep(ut
->uu_lowpri_delay
);
509 ut
->uu_lowpri_delay
= 0;
515 vnode_pager_shutdown(void)
520 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
521 vp
= (vnode_t
)(bs_port_table
[i
]).vp
;
523 (bs_port_table
[i
]).vp
= 0;
525 /* get rid of macx_swapon() reference */
533 upl_get_internal_page_list(upl_t upl
)
535 return(UPL_GET_INTERNAL_PAGE_LIST(upl
));