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 #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 <mach/mach_types.h>
35 #include <vm/vm_map.h>
36 #include <vm/vm_kern.h>
37 #include <kern/host.h>
38 #include <kern/parallel.h>
39 #include <kern/zalloc.h>
40 #include <kern/kalloc.h>
41 #include <libkern/libkern.h>
42 #include <sys/malloc.h>
44 #include <vm/vnode_pager.h>
47 * temporary support for delayed instantiation
50 int default_pager_init_flag
= 0;
52 struct bs_map bs_port_table
[MAX_BACKING_STORE
] = {
53 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
54 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
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}};
64 /* ###################################################### */
67 #include <kern/assert.h>
70 * Routine: macx_swapon
72 * Syscall interface to add a file to backing store
82 struct nameidata nd
, *ndp
;
83 struct proc
*p
= current_proc();
87 mach_port_t backing_store
;
88 memory_object_default_t default_pager
;
90 boolean_t funnel_state
;
94 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
97 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
102 if(default_pager_init_flag
== 0) {
103 start_def_pager(NULL
);
104 default_pager_init_flag
= 1;
108 * Get a vnode for the paging area.
110 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_USERSPACE
,
113 if ((error
= namei(ndp
)))
117 if (vp
->v_type
!= VREG
) {
119 VOP_UNLOCK(vp
, 0, p
);
122 UBCINFOCHECK("macx_swapon", vp
);
124 if (error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
)) {
125 VOP_UNLOCK(vp
, 0, p
);
129 if (vattr
.va_size
< (u_quad_t
)size
) {
131 vattr
.va_size
= (u_quad_t
)size
;
132 error
= VOP_SETATTR(vp
, &vattr
, p
->p_ucred
, p
);
134 VOP_UNLOCK(vp
, 0, p
);
139 /* add new backing store to list */
141 while(bs_port_table
[i
].vp
!= 0) {
142 if(i
== MAX_BACKING_STORE
)
146 if(i
== MAX_BACKING_STORE
) {
148 VOP_UNLOCK(vp
, 0, p
);
152 /* remember the vnode. This vnode has namei() reference */
153 bs_port_table
[i
].vp
= vp
;
156 * Look to see if we are already paging to this file.
158 /* make certain the copy send of kernel call will work */
159 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
160 kr
= host_default_memory_manager(host_priv_self(), &default_pager
, 0);
161 if(kr
!= KERN_SUCCESS
) {
163 VOP_UNLOCK(vp
, 0, p
);
164 bs_port_table
[i
].vp
= 0;
168 kr
= default_pager_backing_store_create(default_pager
,
169 -1, /* default priority */
170 0, /* default cluster size */
172 memory_object_default_deallocate(default_pager
);
174 if(kr
!= KERN_SUCCESS
) {
176 VOP_UNLOCK(vp
, 0, p
);
177 bs_port_table
[i
].vp
= 0;
182 * NOTE: we are able to supply PAGE_SIZE here instead of
183 * an actual record size or block number because:
184 * a: we do not support offsets from the beginning of the
185 * file (allowing for non page size/record modulo offsets.
186 * b: because allow paging will be done modulo page size
189 VOP_UNLOCK(vp
, 0, p
);
190 kr
= default_pager_add_file(backing_store
, vp
, PAGE_SIZE
,
191 ((int)vattr
.va_size
)/PAGE_SIZE
);
192 if(kr
!= KERN_SUCCESS
) {
193 bs_port_table
[i
].vp
= 0;
194 if(kr
== KERN_INVALID_ARGUMENT
)
200 bs_port_table
[i
].bs
= (void *)backing_store
;
203 panic("macx_swapon: hold");
205 /* Mark this vnode as being used for swapfile */
206 SET(vp
->v_flag
, VSWAP
);
209 * take an extra reference on the vnode to keep
210 * vnreclaim() away from this vnode.
214 /* Hold on to the namei reference to the paging file vnode */
222 (void) thread_funnel_set(kernel_flock
, FALSE
);
227 * Routine: macx_swapoff
229 * Syscall interface to remove a file from backing store
237 mach_port_t backing_store
;
239 struct vnode
*vp
= 0;
240 struct nameidata nd
, *ndp
;
241 struct proc
*p
= current_proc();
244 boolean_t funnel_state
;
246 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
247 backing_store
= NULL
;
250 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)))
251 goto swapoff_bailout
;
256 * Get the vnode for the paging area.
258 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_USERSPACE
,
261 if ((error
= namei(ndp
)))
262 goto swapoff_bailout
;
265 if (vp
->v_type
!= VREG
) {
267 VOP_UNLOCK(vp
, 0, p
);
268 goto swapoff_bailout
;
271 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
272 if(bs_port_table
[i
].vp
== vp
) {
277 if (i
== MAX_BACKING_STORE
) {
279 VOP_UNLOCK(vp
, 0, p
);
280 goto swapoff_bailout
;
282 backing_store
= (mach_port_t
)bs_port_table
[i
].bs
;
284 VOP_UNLOCK(vp
, 0, p
);
285 kr
= default_pager_backing_store_delete(backing_store
);
289 bs_port_table
[i
].vp
= 0;
291 /* This vnode is no longer used for swapfile */
292 CLR(vp
->v_flag
, VSWAP
);
294 /* get rid of macx_swapon() namei() reference */
297 /* get rid of macx_swapon() "extra" reference */
309 /* get rid of macx_swapoff() namei() reference */
314 (void) thread_funnel_set(kernel_flock
, FALSE
);