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/parallel.h>
54 #include <kern/zalloc.h>
55 #include <kern/kalloc.h>
56 #include <libkern/libkern.h>
58 #include <vm/vnode_pager.h>
59 #include <vm/vm_pageout.h>
61 #include <kern/assert.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 panic("-ve count in vnode_pageout");
108 panic("vnode_pageout: size == 0\n");
110 UBCINFOCHECK("vnode_pageout", vp
);
112 if (UBCINVALID(vp
)) {
113 result
= PAGER_ERROR
;
115 if (upl
&& !(flags
& UPL_NOCOMMIT
))
116 ubc_upl_abort(upl
, 0);
121 * This is a pageout from the Default pager,
122 * just go ahead and call VOP_PAGEOUT
125 if (error
= VOP_PAGEOUT(vp
, upl
, upl_offset
, (off_t
)f_offset
,
126 (size_t)size
, p
->p_ucred
, flags
))
127 result
= error
= PAGER_ERROR
;
130 ubc_create_upl(vp
, f_offset
, isize
, &vpupl
, &pl
, UPL_COPYOUT_FROM
);
131 if (vpupl
== (upl_t
) 0)
134 if (ubc_getsize(vp
) == 0) {
135 for (offset
= 0; isize
; isize
-= PAGE_SIZE
,
136 offset
+= PAGE_SIZE
) {
137 blkno
= ubc_offtoblk(vp
, (off_t
)f_offset
);
138 f_offset
+= PAGE_SIZE
;
139 if ((bp
= incore(vp
, blkno
)) &&
140 ISSET(bp
->b_flags
, B_BUSY
)) {
141 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
142 UPL_ABORT_FREE_ON_EMPTY
);
143 result
= error
= PAGER_ERROR
;
147 SET(bp
->b_flags
, B_BUSY
| B_INVAL
);
150 ubc_upl_commit_range(vpupl
, offset
, PAGE_SIZE
,
151 UPL_COMMIT_FREE_ON_EMPTY
);
162 if ( !upl_valid_page(pl
, pg_index
)) {
163 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
164 UPL_ABORT_FREE_ON_EMPTY
);
171 if ( !upl_dirty_page(pl
, pg_index
)) {
173 * if the page is not dirty and reached here it is
174 * marked precious or it is due to invalidation in
175 * memory_object_lock request as part of truncation
176 * We also get here from vm_object_terminate()
177 * So all you need to do in these
178 * cases is to invalidate incore buffer if it is there
179 * Note we must not sleep here if B_BUSY - that is
180 * a lock inversion which causes deadlock.
182 blkno
= ubc_offtoblk(vp
, (off_t
)(f_offset
+ offset
));
185 if ((bp
= incore(vp
, blkno
)) &&
186 ISSET(bp
->b_flags
, B_BUSY
| B_NEEDCOMMIT
)) {
188 ubc_upl_abort_range(vpupl
, offset
, PAGE_SIZE
,
189 UPL_ABORT_FREE_ON_EMPTY
);
190 result
= error
= PAGER_ERROR
;
197 SET(bp
->b_flags
, B_BUSY
| B_INVAL
);
203 ubc_upl_commit_range(vpupl
, offset
, PAGE_SIZE
,
204 UPL_COMMIT_FREE_ON_EMPTY
);
214 xsize
= isize
- PAGE_SIZE
;
217 if ( !upl_valid_page(pl
, pg_index
+ num_of_pages
))
219 if ( !upl_dirty_page(pl
, pg_index
+ num_of_pages
))
224 xsize
= num_of_pages
* PAGE_SIZE
;
226 /* By defn callee will commit or abort upls */
227 if (error
= VOP_PAGEOUT(vp
, vpupl
, (vm_offset_t
)offset
,
228 (off_t
)(f_offset
+ offset
), xsize
,
229 p
->p_ucred
, flags
& ~UPL_NOCOMMIT
))
230 result
= error
= PAGER_ERROR
;
233 pg_index
+= num_of_pages
;
239 thread_funnel_set(kernel_flock
, funnel_state
);
249 vm_offset_t pl_offset
,
250 vm_object_offset_t f_offset
,
255 int result
= PAGER_SUCCESS
;
256 struct proc
*p
= current_proc();
259 boolean_t funnel_state
;
264 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
266 UBCINFOCHECK("vnode_pagein", vp
);
268 if (UBCINVALID(vp
)) {
269 result
= PAGER_ERROR
;
271 if (pl
&& !(flags
& UPL_NOCOMMIT
)) {
272 ubc_upl_abort(pl
, 0);
279 if (error
= VOP_PAGEIN(vp
, pl
, pl_offset
, (off_t
)f_offset
,
280 size
, p
->p_ucred
, flags
)) {
281 result
= PAGER_ERROR
;
287 if(size
> 4096 && vp
->v_tag
== VT_NFS
) {
289 size
= size
- xfer_size
;
294 ubc_create_upl(vp
, f_offset
+ local_offset
, xfer_size
,
295 &vpupl
, NULL
, UPL_FLAGS_NONE
);
296 if (vpupl
== (upl_t
) 0) {
297 result
= PAGER_ABSENT
;
298 error
= PAGER_ABSENT
;
304 /* By defn callee will commit or abort upls */
305 if (error
= VOP_PAGEIN(vp
, vpupl
, (vm_offset_t
) 0,
306 (off_t
)f_offset
+ local_offset
,
307 xfer_size
, p
->p_ucred
,
308 flags
& ~UPL_NOCOMMIT
)) {
309 result
= PAGER_ERROR
;
312 local_offset
+= PAGE_SIZE_64
;
318 thread_funnel_set(kernel_flock
, funnel_state
);
324 vnode_pager_shutdown()
327 extern struct bs_map bs_port_table
[];
330 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
331 vp
= (struct vnode
*)(bs_port_table
[i
]).vp
;
333 (bs_port_table
[i
]).vp
= 0;
335 /* get rid of macx_swapon() namei() reference */
338 /* get rid of macx_swapon() "extra" reference */
346 upl_get_internal_page_list(upl_t upl
)
348 return(UPL_GET_INTERNAL_PAGE_LIST(upl
));