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 #include <mach/boolean.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
33 #include <sys/vnode.h>
34 #include <sys/namei.h>
37 #include <mach/mach_types.h>
38 #include <vm/vm_map.h>
39 #include <vm/vm_kern.h>
40 #include <kern/host.h>
41 #include <kern/zalloc.h>
42 #include <kern/kalloc.h>
43 #include <libkern/libkern.h>
44 #include <sys/malloc.h>
46 #include <vm/vnode_pager.h>
49 * temporary support for delayed instantiation
52 int default_pager_init_flag
= 0;
54 struct bs_map bs_port_table
[MAX_BACKING_STORE
] = {
55 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
56 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
57 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
58 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
59 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
60 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
61 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
62 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
63 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
64 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
66 /* ###################################################### */
69 #include <kern/assert.h>
72 * Routine: macx_backing_store_recovery
74 * Syscall interface to set a tasks privilege
75 * level so that it is not subject to
76 * macx_backing_store_suspend
79 macx_backing_store_recovery(
83 struct proc
*p
= current_proc();
84 boolean_t funnel_state
;
86 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
87 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
88 goto backing_store_recovery_return
;
90 /* for now restrict backing_store_recovery */
91 /* usage to only present task */
94 goto backing_store_recovery_return
;
97 task_backing_store_privileged(p
->task
);
99 backing_store_recovery_return
:
100 (void) thread_funnel_set(kernel_flock
, FALSE
);
105 * Routine: macx_backing_store_suspend
107 * Syscall interface to stop new demand for
108 * backing store when backing store is low
112 macx_backing_store_suspend(
116 struct proc
*p
= current_proc();
117 boolean_t funnel_state
;
119 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
120 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
121 goto backing_store_suspend_return
;
123 vm_backing_store_disable(suspend
);
125 backing_store_suspend_return
:
126 (void) thread_funnel_set(kernel_flock
, FALSE
);
131 * Routine: macx_swapon
133 * Syscall interface to add a file to backing store
142 struct vnode
*vp
= 0;
143 struct nameidata nd
, *ndp
;
144 struct proc
*p
= current_proc();
148 mach_port_t backing_store
;
149 memory_object_default_t default_pager
;
151 boolean_t funnel_state
;
155 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
158 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
161 if(default_pager_init_flag
== 0) {
162 start_def_pager(NULL
);
163 default_pager_init_flag
= 1;
167 * Get a vnode for the paging area.
169 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_USERSPACE
,
172 if ((error
= namei(ndp
)))
176 if (vp
->v_type
!= VREG
) {
178 VOP_UNLOCK(vp
, 0, p
);
181 UBCINFOCHECK("macx_swapon", vp
);
183 if (error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
)) {
184 VOP_UNLOCK(vp
, 0, p
);
188 if (vattr
.va_size
< (u_quad_t
)size
) {
190 vattr
.va_size
= (u_quad_t
)size
;
191 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
, p
);
193 VOP_UNLOCK(vp
, 0, p
);
198 /* add new backing store to list */
200 while(bs_port_table
[i
].vp
!= 0) {
201 if(i
== MAX_BACKING_STORE
)
205 if(i
== MAX_BACKING_STORE
) {
207 VOP_UNLOCK(vp
, 0, p
);
211 /* remember the vnode. This vnode has namei() reference */
212 bs_port_table
[i
].vp
= vp
;
215 * Look to see if we are already paging to this file.
217 /* make certain the copy send of kernel call will work */
218 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
219 kr
= host_default_memory_manager(host_priv_self(), &default_pager
, 0);
220 if(kr
!= KERN_SUCCESS
) {
222 VOP_UNLOCK(vp
, 0, p
);
223 bs_port_table
[i
].vp
= 0;
227 kr
= default_pager_backing_store_create(default_pager
,
228 -1, /* default priority */
229 0, /* default cluster size */
231 memory_object_default_deallocate(default_pager
);
233 if(kr
!= KERN_SUCCESS
) {
235 VOP_UNLOCK(vp
, 0, p
);
236 bs_port_table
[i
].vp
= 0;
241 * NOTE: we are able to supply PAGE_SIZE here instead of
242 * an actual record size or block number because:
243 * a: we do not support offsets from the beginning of the
244 * file (allowing for non page size/record modulo offsets.
245 * b: because allow paging will be done modulo page size
248 VOP_UNLOCK(vp
, 0, p
);
249 kr
= default_pager_add_file(backing_store
, vp
, PAGE_SIZE
,
250 ((int)vattr
.va_size
)/PAGE_SIZE
);
251 if(kr
!= KERN_SUCCESS
) {
252 bs_port_table
[i
].vp
= 0;
253 if(kr
== KERN_INVALID_ARGUMENT
)
259 bs_port_table
[i
].bs
= (void *)backing_store
;
262 panic("macx_swapon: hold");
264 /* Mark this vnode as being used for swapfile */
265 SET(vp
->v_flag
, VSWAP
);
270 * take an extra reference on the vnode to keep
271 * vnreclaim() away from this vnode.
275 /* Hold on to the namei reference to the paging file vnode */
282 (void) thread_funnel_set(kernel_flock
, FALSE
);
287 * Routine: macx_swapoff
289 * Syscall interface to remove a file from backing store
297 mach_port_t backing_store
;
299 struct vnode
*vp
= 0;
300 struct nameidata nd
, *ndp
;
301 struct proc
*p
= current_proc();
304 boolean_t funnel_state
;
306 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
307 backing_store
= NULL
;
310 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
311 goto swapoff_bailout
;
314 * Get the vnode for the paging area.
316 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_USERSPACE
,
319 if ((error
= namei(ndp
)))
320 goto swapoff_bailout
;
323 if (vp
->v_type
!= VREG
) {
325 VOP_UNLOCK(vp
, 0, p
);
326 goto swapoff_bailout
;
329 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
330 if(bs_port_table
[i
].vp
== vp
) {
335 if (i
== MAX_BACKING_STORE
) {
337 VOP_UNLOCK(vp
, 0, p
);
338 goto swapoff_bailout
;
340 backing_store
= (mach_port_t
)bs_port_table
[i
].bs
;
342 VOP_UNLOCK(vp
, 0, p
);
343 kr
= default_pager_backing_store_delete(backing_store
);
347 bs_port_table
[i
].vp
= 0;
349 /* This vnode is no longer used for swapfile */
350 CLR(vp
->v_flag
, VSWAP
);
352 /* get rid of macx_swapon() namei() reference */
355 /* get rid of macx_swapon() "extra" reference */
367 /* get rid of macx_swapoff() namei() reference */
371 (void) thread_funnel_set(kernel_flock
, FALSE
);