2 * Copyright (c) 2000-2004 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 #include <mach/boolean.h>
24 #include <sys/param.h>
25 #include <sys/systm.h>
30 #include <sys/vnode.h>
31 #include <sys/namei.h>
34 #include <bsm/audit_kernel.h>
35 #include <bsm/audit_kevents.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 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPON
);
156 AUDIT_ARG(value
, priority
);
158 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
161 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
164 if(default_pager_init_flag
== 0) {
165 start_def_pager(NULL
);
166 default_pager_init_flag
= 1;
170 * Get a vnode for the paging area.
172 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
, UIO_USERSPACE
,
175 if ((error
= namei(ndp
)))
179 if (vp
->v_type
!= VREG
) {
181 VOP_UNLOCK(vp
, 0, p
);
184 UBCINFOCHECK("macx_swapon", vp
);
186 if (error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
)) {
187 VOP_UNLOCK(vp
, 0, p
);
191 if (vattr
.va_size
< (u_quad_t
)size
) {
193 vattr
.va_size
= (u_quad_t
)size
;
194 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
, p
);
196 VOP_UNLOCK(vp
, 0, p
);
201 /* add new backing store to list */
203 while(bs_port_table
[i
].vp
!= 0) {
204 if(i
== MAX_BACKING_STORE
)
208 if(i
== MAX_BACKING_STORE
) {
210 VOP_UNLOCK(vp
, 0, p
);
214 /* remember the vnode. This vnode has namei() reference */
215 bs_port_table
[i
].vp
= vp
;
218 * Look to see if we are already paging to this file.
220 /* make certain the copy send of kernel call will work */
221 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
222 kr
= host_default_memory_manager(host_priv_self(), &default_pager
, 0);
223 if(kr
!= KERN_SUCCESS
) {
225 VOP_UNLOCK(vp
, 0, p
);
226 bs_port_table
[i
].vp
= 0;
230 kr
= default_pager_backing_store_create(default_pager
,
231 -1, /* default priority */
232 0, /* default cluster size */
234 memory_object_default_deallocate(default_pager
);
236 if(kr
!= KERN_SUCCESS
) {
238 VOP_UNLOCK(vp
, 0, p
);
239 bs_port_table
[i
].vp
= 0;
244 * NOTE: we are able to supply PAGE_SIZE here instead of
245 * an actual record size or block number because:
246 * a: we do not support offsets from the beginning of the
247 * file (allowing for non page size/record modulo offsets.
248 * b: because allow paging will be done modulo page size
251 VOP_UNLOCK(vp
, 0, p
);
252 kr
= default_pager_add_file(backing_store
, vp
, PAGE_SIZE
,
253 ((int)vattr
.va_size
)/PAGE_SIZE
);
254 if(kr
!= KERN_SUCCESS
) {
255 bs_port_table
[i
].vp
= 0;
256 if(kr
== KERN_INVALID_ARGUMENT
)
262 bs_port_table
[i
].bs
= (void *)backing_store
;
265 panic("macx_swapon: hold");
267 /* Mark this vnode as being used for swapfile */
268 SET(vp
->v_flag
, VSWAP
);
273 * take an extra reference on the vnode to keep
274 * vnreclaim() away from this vnode.
278 /* Hold on to the namei reference to the paging file vnode */
285 (void) thread_funnel_set(kernel_flock
, FALSE
);
286 AUDIT_MACH_SYSCALL_EXIT(error
);
291 * Routine: macx_swapoff
293 * Syscall interface to remove a file from backing store
301 mach_port_t backing_store
;
303 struct vnode
*vp
= 0;
304 struct nameidata nd
, *ndp
;
305 struct proc
*p
= current_proc();
308 boolean_t funnel_state
;
310 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPOFF
);
311 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
312 backing_store
= NULL
;
315 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
316 goto swapoff_bailout
;
319 * Get the vnode for the paging area.
321 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
, UIO_USERSPACE
,
324 if ((error
= namei(ndp
)))
325 goto swapoff_bailout
;
328 if (vp
->v_type
!= VREG
) {
330 VOP_UNLOCK(vp
, 0, p
);
331 goto swapoff_bailout
;
334 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
335 if(bs_port_table
[i
].vp
== vp
) {
340 if (i
== MAX_BACKING_STORE
) {
342 VOP_UNLOCK(vp
, 0, p
);
343 goto swapoff_bailout
;
345 backing_store
= (mach_port_t
)bs_port_table
[i
].bs
;
347 VOP_UNLOCK(vp
, 0, p
);
348 kr
= default_pager_backing_store_delete(backing_store
);
352 bs_port_table
[i
].vp
= 0;
354 /* This vnode is no longer used for swapfile */
355 CLR(vp
->v_flag
, VSWAP
);
357 /* get rid of macx_swapon() namei() reference */
360 /* get rid of macx_swapon() "extra" reference */
372 /* get rid of macx_swapoff() namei() reference */
376 (void) thread_funnel_set(kernel_flock
, FALSE
);
377 AUDIT_MACH_SYSCALL_EXIT(error
);