2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1987 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
34 * "Swap" pager that pages to/from vnodes. Also
35 * handles demand paging from files.
39 #include <mach/boolean.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/namei.h>
47 #include <sys/mount.h>
51 #include <mach/mach_types.h>
52 #include <mach/memory_object_types.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_kern.h>
56 #include <kern/zalloc.h>
57 #include <kern/kalloc.h>
58 #include <libkern/libkern.h>
60 #include <vm/vnode_pager.h>
61 #include <vm/vm_pageout.h>
63 #include <kern/assert.h>
64 #include <sys/kdebug.h>
66 unsigned int vp_pagein
=0;
67 unsigned int vp_pgodirty
=0;
68 unsigned int vp_pgoclean
=0;
69 unsigned int dp_pgouts
=0; /* Default pager pageouts */
70 unsigned int dp_pgins
=0; /* Default pager pageins */
73 vnode_pager_get_filesize(struct vnode
*vp
)
76 return (vm_object_offset_t
) 0;
79 return (vm_object_offset_t
) ubc_getsize(vp
);
84 vnode_pageout(struct vnode
*vp
,
86 vm_offset_t upl_offset
,
87 vm_object_offset_t f_offset
,
92 int result
= PAGER_SUCCESS
;
93 struct proc
*p
= current_proc();
100 boolean_t funnel_state
;
104 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
109 result
= error
= PAGER_ERROR
;
112 UBCINFOCHECK("vnode_pageout", vp
);
114 if (UBCINVALID(vp
)) {
115 result
= error
= PAGER_ERROR
;
117 if (upl
&& !(flags
& UPL_NOCOMMIT
))
118 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
123 * This is a pageout from the Default pager,
124 * just go ahead and call VOP_PAGEOUT
128 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
131 if (error
= VOP_PAGEOUT(vp
, upl
, upl_offset
, (off_t
)f_offset
,
132 (size_t)size
, p
->p_ucred
, flags
))
133 result
= error
= PAGER_ERROR
;
135 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
140 ubc_create_upl(vp
, f_offset
, isize
, &vpupl
, &pl
, UPL_FOR_PAGEOUT
| UPL_COPYOUT_FROM
);
142 if (vpupl
== (upl_t
) 0) {
143 result
= error
= PAGER_ABSENT
;
147 * if we get here, we've created the upl and
148 * are responsible for commiting/aborting it
149 * regardless of what the caller has passed in
151 flags
&= ~UPL_NOCOMMIT
;
153 if (ubc_getsize(vp
) == 0) {
154 for (offset
= 0; isize
; isize
-= PAGE_SIZE
,
155 offset
+= PAGE_SIZE
) {
156 blkno
= ubc_offtoblk(vp
, (off_t
)f_offset
);
157 f_offset
+= PAGE_SIZE
;
158 if ((bp
= incore(vp
, blkno
)) &&
159 ISSET(bp
->b_flags
, B_BUSY
)) {
160 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
161 UPL_ABORT_FREE_ON_EMPTY
);
162 result
= error
= PAGER_ERROR
;
166 SET(bp
->b_flags
, B_BUSY
| B_INVAL
);
169 ubc_upl_commit_range(vpupl
, offset
, PAGE_SIZE
,
170 UPL_COMMIT_FREE_ON_EMPTY
);
181 if ( !upl_valid_page(pl
, pg_index
)) {
182 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
183 UPL_ABORT_FREE_ON_EMPTY
);
190 if ( !upl_dirty_page(pl
, pg_index
)) {
192 * if the page is not dirty and reached here it is
193 * marked precious or it is due to invalidation in
194 * memory_object_lock request as part of truncation
195 * We also get here from vm_object_terminate()
196 * So all you need to do in these
197 * cases is to invalidate incore buffer if it is there
198 * Note we must not sleep here if B_BUSY - that is
199 * a lock inversion which causes deadlock.
201 blkno
= ubc_offtoblk(vp
, (off_t
)(f_offset
+ offset
));
204 if ((bp
= incore(vp
, blkno
)) &&
205 ISSET(bp
->b_flags
, B_BUSY
| B_NEEDCOMMIT
)) {
207 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
208 UPL_ABORT_FREE_ON_EMPTY
);
209 result
= error
= PAGER_ERROR
;
216 SET(bp
->b_flags
, B_BUSY
| B_INVAL
);
222 ubc_upl_commit_range(vpupl
, offset
, PAGE_SIZE
,
223 UPL_COMMIT_FREE_ON_EMPTY
);
233 xsize
= isize
- PAGE_SIZE
;
236 if ( !upl_valid_page(pl
, pg_index
+ num_of_pages
))
238 if ( !upl_dirty_page(pl
, pg_index
+ num_of_pages
))
243 xsize
= num_of_pages
* PAGE_SIZE
;
245 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
248 if (error
= VOP_PAGEOUT(vp
, vpupl
, (vm_offset_t
)offset
,
249 (off_t
)(f_offset
+ offset
), xsize
,
251 result
= error
= PAGER_ERROR
;
253 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
258 pg_index
+= num_of_pages
;
264 thread_funnel_set(kernel_flock
, funnel_state
);
274 vm_offset_t upl_offset
,
275 vm_object_offset_t f_offset
,
280 struct proc
*p
= current_proc();
282 int result
= PAGER_SUCCESS
;
290 int abort_needed
= 1;
291 boolean_t funnel_state
;
294 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
296 UBCINFOCHECK("vnode_pagein", vp
);
298 if (UBCINVALID(vp
)) {
299 result
= PAGER_ERROR
;
301 if (upl
&& !(flags
& UPL_NOCOMMIT
)) {
302 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
306 if (upl
== (upl_t
)NULL
) {
307 if (size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
308 result
= PAGER_ERROR
;
312 ubc_create_upl(vp
, f_offset
, size
, &upl
, &pl
, UPL_RET_ONLY_ABSENT
);
314 if (upl
== (upl_t
)NULL
) {
315 result
= PAGER_ABSENT
;
316 error
= PAGER_ABSENT
;
321 * if we get here, we've created the upl and
322 * are responsible for commiting/aborting it
323 * regardless of what the caller has passed in
325 flags
&= ~UPL_NOCOMMIT
;
329 pl
= ubc_upl_pageinfo(upl
);
333 pages_in_upl
= size
/ PAGE_SIZE
;
334 first_pg
= upl_offset
/ PAGE_SIZE
;
337 * before we start marching forward, we must make sure we end on
338 * a present page, otherwise we will be working with a freed
341 for (last_pg
= pages_in_upl
- 1; last_pg
>= first_pg
; last_pg
--) {
342 if (upl_page_present(pl
, last_pg
))
345 pages_in_upl
= last_pg
+ 1;
347 for (last_pg
= first_pg
; last_pg
< pages_in_upl
;) {
349 * scan the upl looking for the next
350 * page that is present.... if all of the
351 * pages are absent, we're done
353 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
354 if (upl_page_present(pl
, last_pg
))
357 if (last_pg
== pages_in_upl
)
361 * if we get here, we've sitting on a page
362 * that is present... we want to skip over
363 * any range of 'valid' pages... if this takes
364 * us to the end of the request, than we're done
366 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
367 if (!upl_valid_page(pl
, last_pg
) || !upl_page_present(pl
, last_pg
))
370 if (last_pg
> start_pg
) {
372 * we've found a range of valid pages
373 * if we've got COMMIT responsibility
374 * commit this range of pages back to the
377 xsize
= (last_pg
- start_pg
) * PAGE_SIZE
;
379 if (!(flags
& UPL_NOCOMMIT
))
380 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, xsize
, UPL_ABORT_FREE_ON_EMPTY
);
384 if (last_pg
== pages_in_upl
)
387 if (!upl_page_present(pl
, last_pg
))
389 * if we found a range of valid pages
390 * terminated by a non-present page
396 * scan from the found invalid page looking for a valid
397 * or non-present page before the end of the upl is reached, if we
398 * find one, then it will be the last page of the request to
401 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
402 if (upl_valid_page(pl
, last_pg
) || !upl_page_present(pl
, last_pg
))
405 if (last_pg
> start_pg
) {
408 xsize
= (last_pg
- start_pg
) * PAGE_SIZE
;
409 xoff
= start_pg
* PAGE_SIZE
;
411 if (error
= VOP_PAGEIN(vp
, upl
, (vm_offset_t
) xoff
,
412 (off_t
)f_offset
+ xoff
,
415 result
= PAGER_ERROR
;
422 if (!(flags
& UPL_NOCOMMIT
) && abort_needed
)
423 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
427 thread_funnel_set(kernel_flock
, funnel_state
);
433 vnode_pager_shutdown()
436 extern struct bs_map bs_port_table
[];
439 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
440 vp
= (struct vnode
*)(bs_port_table
[i
]).vp
;
442 (bs_port_table
[i
]).vp
= 0;
444 /* get rid of macx_swapon() namei() reference */
447 /* get rid of macx_swapon() "extra" reference */
455 upl_get_internal_page_list(upl_t upl
)
457 return(UPL_GET_INTERNAL_PAGE_LIST(upl
));