2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Mach Operating System
24 * Copyright (c) 1987 Carnegie-Mellon University
25 * All rights reserved. The CMU software License Agreement specifies
26 * the terms and conditions for use and redistribution.
31 * "Swap" pager that pages to/from vnodes. Also
32 * handles demand paging from files.
36 #include <mach/boolean.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
42 #include <sys/vnode.h>
43 #include <sys/namei.h>
44 #include <sys/mount.h>
48 #include <mach/mach_types.h>
49 #include <mach/memory_object_types.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_kern.h>
53 #include <kern/zalloc.h>
54 #include <kern/kalloc.h>
55 #include <libkern/libkern.h>
57 #include <vm/vnode_pager.h>
58 #include <vm/vm_pageout.h>
60 #include <kern/assert.h>
61 #include <sys/kdebug.h>
63 unsigned int vp_pagein
=0;
64 unsigned int vp_pgodirty
=0;
65 unsigned int vp_pgoclean
=0;
66 unsigned int dp_pgouts
=0; /* Default pager pageouts */
67 unsigned int dp_pgins
=0; /* Default pager pageins */
70 vnode_pager_get_filesize(struct vnode
*vp
)
73 return (vm_object_offset_t
) 0;
76 return (vm_object_offset_t
) ubc_getsize(vp
);
81 vnode_pageout(struct vnode
*vp
,
83 vm_offset_t upl_offset
,
84 vm_object_offset_t f_offset
,
89 int result
= PAGER_SUCCESS
;
90 struct proc
*p
= current_proc();
97 boolean_t funnel_state
;
101 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
106 result
= error
= PAGER_ERROR
;
109 UBCINFOCHECK("vnode_pageout", vp
);
111 if (UBCINVALID(vp
)) {
112 result
= error
= PAGER_ERROR
;
114 if (upl
&& !(flags
& UPL_NOCOMMIT
))
115 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
120 * This is a pageout from the Default pager,
121 * just go ahead and call VOP_PAGEOUT
125 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
128 if (error
= VOP_PAGEOUT(vp
, upl
, upl_offset
, (off_t
)f_offset
,
129 (size_t)size
, p
->p_ucred
, flags
))
130 result
= error
= PAGER_ERROR
;
132 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
137 ubc_create_upl(vp
, f_offset
, isize
, &vpupl
, &pl
, UPL_COPYOUT_FROM
);
139 if (vpupl
== (upl_t
) 0) {
140 result
= error
= PAGER_ABSENT
;
144 * if we get here, we've created the upl and
145 * are responsible for commiting/aborting it
146 * regardless of what the caller has passed in
148 flags
&= ~UPL_NOCOMMIT
;
150 if (ubc_getsize(vp
) == 0) {
151 for (offset
= 0; isize
; isize
-= PAGE_SIZE
,
152 offset
+= PAGE_SIZE
) {
153 blkno
= ubc_offtoblk(vp
, (off_t
)f_offset
);
154 f_offset
+= PAGE_SIZE
;
155 if ((bp
= incore(vp
, blkno
)) &&
156 ISSET(bp
->b_flags
, B_BUSY
)) {
157 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
158 UPL_ABORT_FREE_ON_EMPTY
);
159 result
= error
= PAGER_ERROR
;
163 SET(bp
->b_flags
, B_BUSY
| B_INVAL
);
166 ubc_upl_commit_range(vpupl
, offset
, PAGE_SIZE
,
167 UPL_COMMIT_FREE_ON_EMPTY
);
178 if ( !upl_valid_page(pl
, pg_index
)) {
179 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
180 UPL_ABORT_FREE_ON_EMPTY
);
187 if ( !upl_dirty_page(pl
, pg_index
)) {
189 * if the page is not dirty and reached here it is
190 * marked precious or it is due to invalidation in
191 * memory_object_lock request as part of truncation
192 * We also get here from vm_object_terminate()
193 * So all you need to do in these
194 * cases is to invalidate incore buffer if it is there
195 * Note we must not sleep here if B_BUSY - that is
196 * a lock inversion which causes deadlock.
198 blkno
= ubc_offtoblk(vp
, (off_t
)(f_offset
+ offset
));
201 if ((bp
= incore(vp
, blkno
)) &&
202 ISSET(bp
->b_flags
, B_BUSY
| B_NEEDCOMMIT
)) {
204 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
205 UPL_ABORT_FREE_ON_EMPTY
);
206 result
= error
= PAGER_ERROR
;
213 SET(bp
->b_flags
, B_BUSY
| B_INVAL
);
219 ubc_upl_commit_range(vpupl
, offset
, PAGE_SIZE
,
220 UPL_COMMIT_FREE_ON_EMPTY
);
230 xsize
= isize
- PAGE_SIZE
;
233 if ( !upl_valid_page(pl
, pg_index
+ num_of_pages
))
235 if ( !upl_dirty_page(pl
, pg_index
+ num_of_pages
))
240 xsize
= num_of_pages
* PAGE_SIZE
;
242 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
245 if (error
= VOP_PAGEOUT(vp
, vpupl
, (vm_offset_t
)offset
,
246 (off_t
)(f_offset
+ offset
), xsize
,
248 result
= error
= PAGER_ERROR
;
250 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
255 pg_index
+= num_of_pages
;
261 thread_funnel_set(kernel_flock
, funnel_state
);
271 vm_offset_t upl_offset
,
272 vm_object_offset_t f_offset
,
277 struct proc
*p
= current_proc();
279 int result
= PAGER_SUCCESS
;
287 int abort_needed
= 1;
288 boolean_t funnel_state
;
291 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
293 UBCINFOCHECK("vnode_pagein", vp
);
295 if (UBCINVALID(vp
)) {
296 result
= PAGER_ERROR
;
298 if (upl
&& !(flags
& UPL_NOCOMMIT
)) {
299 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
303 if (upl
== (upl_t
)NULL
) {
304 if (size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
305 result
= PAGER_ERROR
;
309 ubc_create_upl(vp
, f_offset
, size
, &upl
, &pl
, UPL_RET_ONLY_ABSENT
);
311 if (upl
== (upl_t
)NULL
) {
312 result
= PAGER_ABSENT
;
313 error
= PAGER_ABSENT
;
318 * if we get here, we've created the upl and
319 * are responsible for commiting/aborting it
320 * regardless of what the caller has passed in
322 flags
&= ~UPL_NOCOMMIT
;
326 pl
= ubc_upl_pageinfo(upl
);
330 pages_in_upl
= size
/ PAGE_SIZE
;
331 first_pg
= upl_offset
/ PAGE_SIZE
;
334 * before we start marching forward, we must make sure we end on
335 * a present page, otherwise we will be working with a freed
338 for (last_pg
= pages_in_upl
- 1; last_pg
>= first_pg
; last_pg
--) {
339 if (upl_page_present(pl
, last_pg
))
342 pages_in_upl
= last_pg
+ 1;
344 for (last_pg
= first_pg
; last_pg
< pages_in_upl
;) {
346 * scan the upl looking for the next
347 * page that is present.... if all of the
348 * pages are absent, we're done
350 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
351 if (upl_page_present(pl
, last_pg
))
354 if (last_pg
== pages_in_upl
)
358 * if we get here, we've sitting on a page
359 * that is present... we want to skip over
360 * any range of 'valid' pages... if this takes
361 * us to the end of the request, than we're done
363 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
364 if (!upl_valid_page(pl
, last_pg
) || !upl_page_present(pl
, last_pg
))
367 if (last_pg
> start_pg
) {
369 * we've found a range of valid pages
370 * if we've got COMMIT responsibility
371 * commit this range of pages back to the
374 xsize
= (last_pg
- start_pg
) * PAGE_SIZE
;
376 if (!(flags
& UPL_NOCOMMIT
))
377 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, xsize
, UPL_ABORT_FREE_ON_EMPTY
);
381 if (last_pg
== pages_in_upl
)
384 if (!upl_page_present(pl
, last_pg
))
386 * if we found a range of valid pages
387 * terminated by a non-present page
393 * scan from the found invalid page looking for a valid
394 * or non-present page before the end of the upl is reached, if we
395 * find one, then it will be the last page of the request to
398 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
399 if (upl_valid_page(pl
, last_pg
) || !upl_page_present(pl
, last_pg
))
402 if (last_pg
> start_pg
) {
405 xsize
= (last_pg
- start_pg
) * PAGE_SIZE
;
406 xoff
= start_pg
* PAGE_SIZE
;
408 if (error
= VOP_PAGEIN(vp
, upl
, (vm_offset_t
) xoff
,
409 (off_t
)f_offset
+ xoff
,
412 result
= PAGER_ERROR
;
419 if (!(flags
& UPL_NOCOMMIT
) && abort_needed
)
420 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
424 thread_funnel_set(kernel_flock
, funnel_state
);
430 vnode_pager_shutdown()
433 extern struct bs_map bs_port_table
[];
436 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
437 vp
= (struct vnode
*)(bs_port_table
[i
]).vp
;
439 (bs_port_table
[i
]).vp
= 0;
441 /* get rid of macx_swapon() namei() reference */
444 /* get rid of macx_swapon() "extra" reference */
452 upl_get_internal_page_list(upl_t upl
)
454 return(UPL_GET_INTERNAL_PAGE_LIST(upl
));