]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
43866e37 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
43866e37 A |
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 | |
13 | * file. | |
14 | * | |
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 | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
43866e37 A |
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. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | ||
26 | #include <mach/boolean.h> | |
27 | #include <sys/param.h> | |
28 | #include <sys/systm.h> | |
29 | #include <sys/lock.h> | |
30 | #include <sys/proc.h> | |
31 | #include <sys/buf.h> | |
32 | #include <sys/uio.h> | |
33 | #include <sys/vnode.h> | |
34 | #include <sys/namei.h> | |
35 | #include <sys/ubc.h> | |
36 | ||
37 | #include <mach/mach_types.h> | |
38 | #include <vm/vm_map.h> | |
39 | #include <vm/vm_kern.h> | |
40 | #include <kern/host.h> | |
1c79356b A |
41 | #include <kern/zalloc.h> |
42 | #include <kern/kalloc.h> | |
43 | #include <libkern/libkern.h> | |
44 | #include <sys/malloc.h> | |
45 | ||
46 | #include <vm/vnode_pager.h> | |
47 | ||
48 | /* | |
49 | * temporary support for delayed instantiation | |
50 | * of default_pager | |
51 | */ | |
52 | int default_pager_init_flag = 0; | |
53 | ||
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}}; | |
65 | ||
66 | /* ###################################################### */ | |
67 | ||
68 | ||
69 | #include <kern/assert.h> | |
70 | ||
71 | /* | |
72 | * Routine: macx_swapon | |
73 | * Function: | |
74 | * Syscall interface to add a file to backing store | |
75 | */ | |
76 | int | |
77 | macx_swapon( | |
78 | char *filename, | |
79 | int flags, | |
80 | long size, | |
81 | long priority) | |
82 | { | |
83 | struct vnode *vp = 0; | |
84 | struct nameidata nd, *ndp; | |
85 | struct proc *p = current_proc(); | |
86 | pager_file_t pf; | |
87 | register int error; | |
88 | kern_return_t kr; | |
89 | mach_port_t backing_store; | |
0b4e3aa0 | 90 | memory_object_default_t default_pager; |
1c79356b A |
91 | int i; |
92 | boolean_t funnel_state; | |
93 | ||
94 | struct vattr vattr; | |
95 | ||
96 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
97 | ndp = &nd; | |
98 | ||
99 | if ((error = suser(p->p_ucred, &p->p_acflag))) | |
100 | goto swapon_bailout; | |
101 | ||
1c79356b A |
102 | if(default_pager_init_flag == 0) { |
103 | start_def_pager(NULL); | |
104 | default_pager_init_flag = 1; | |
105 | } | |
106 | ||
107 | /* | |
108 | * Get a vnode for the paging area. | |
109 | */ | |
110 | NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, | |
111 | filename, p); | |
112 | ||
113 | if ((error = namei(ndp))) | |
114 | goto swapon_bailout; | |
115 | vp = ndp->ni_vp; | |
116 | ||
117 | if (vp->v_type != VREG) { | |
118 | error = EINVAL; | |
119 | VOP_UNLOCK(vp, 0, p); | |
120 | goto swapon_bailout; | |
121 | } | |
122 | UBCINFOCHECK("macx_swapon", vp); | |
123 | ||
124 | if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) { | |
125 | VOP_UNLOCK(vp, 0, p); | |
126 | goto swapon_bailout; | |
127 | } | |
128 | ||
129 | if (vattr.va_size < (u_quad_t)size) { | |
130 | vattr_null(&vattr); | |
131 | vattr.va_size = (u_quad_t)size; | |
132 | error = VOP_SETATTR(vp, &vattr, p->p_ucred, p); | |
133 | if (error) { | |
134 | VOP_UNLOCK(vp, 0, p); | |
135 | goto swapon_bailout; | |
136 | } | |
137 | } | |
138 | ||
139 | /* add new backing store to list */ | |
140 | i = 0; | |
141 | while(bs_port_table[i].vp != 0) { | |
142 | if(i == MAX_BACKING_STORE) | |
143 | break; | |
144 | i++; | |
145 | } | |
146 | if(i == MAX_BACKING_STORE) { | |
147 | error = ENOMEM; | |
148 | VOP_UNLOCK(vp, 0, p); | |
149 | goto swapon_bailout; | |
150 | } | |
151 | ||
152 | /* remember the vnode. This vnode has namei() reference */ | |
153 | bs_port_table[i].vp = vp; | |
154 | ||
155 | /* | |
156 | * Look to see if we are already paging to this file. | |
157 | */ | |
158 | /* make certain the copy send of kernel call will work */ | |
0b4e3aa0 A |
159 | default_pager = MEMORY_OBJECT_DEFAULT_NULL; |
160 | kr = host_default_memory_manager(host_priv_self(), &default_pager, 0); | |
1c79356b A |
161 | if(kr != KERN_SUCCESS) { |
162 | error = EAGAIN; | |
163 | VOP_UNLOCK(vp, 0, p); | |
164 | bs_port_table[i].vp = 0; | |
165 | goto swapon_bailout; | |
166 | } | |
167 | ||
0b4e3aa0 | 168 | kr = default_pager_backing_store_create(default_pager, |
1c79356b A |
169 | -1, /* default priority */ |
170 | 0, /* default cluster size */ | |
171 | &backing_store); | |
0b4e3aa0 A |
172 | memory_object_default_deallocate(default_pager); |
173 | ||
1c79356b A |
174 | if(kr != KERN_SUCCESS) { |
175 | error = ENOMEM; | |
176 | VOP_UNLOCK(vp, 0, p); | |
177 | bs_port_table[i].vp = 0; | |
178 | goto swapon_bailout; | |
179 | } | |
180 | ||
181 | /* | |
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 | |
187 | */ | |
188 | ||
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) | |
195 | error = EINVAL; | |
196 | else | |
197 | error = ENOMEM; | |
198 | goto swapon_bailout; | |
199 | } | |
200 | bs_port_table[i].bs = (void *)backing_store; | |
201 | error = 0; | |
202 | if (!ubc_hold(vp)) | |
203 | panic("macx_swapon: hold"); | |
204 | ||
205 | /* Mark this vnode as being used for swapfile */ | |
206 | SET(vp->v_flag, VSWAP); | |
207 | ||
208 | /* | |
209 | * take an extra reference on the vnode to keep | |
210 | * vnreclaim() away from this vnode. | |
211 | */ | |
212 | VREF(vp); | |
213 | ||
214 | /* Hold on to the namei reference to the paging file vnode */ | |
215 | vp = 0; | |
216 | ||
217 | swapon_bailout: | |
218 | if (vp) { | |
219 | vrele(vp); | |
220 | } | |
1c79356b A |
221 | (void) thread_funnel_set(kernel_flock, FALSE); |
222 | return(error); | |
223 | } | |
224 | ||
225 | /* | |
226 | * Routine: macx_swapoff | |
227 | * Function: | |
228 | * Syscall interface to remove a file from backing store | |
229 | */ | |
230 | int | |
231 | macx_swapoff( | |
232 | char *filename, | |
233 | int flags) | |
234 | { | |
235 | kern_return_t kr; | |
236 | mach_port_t backing_store; | |
237 | ||
238 | struct vnode *vp = 0; | |
239 | struct nameidata nd, *ndp; | |
240 | struct proc *p = current_proc(); | |
241 | int i; | |
242 | int error; | |
243 | boolean_t funnel_state; | |
244 | ||
245 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
246 | backing_store = NULL; | |
247 | ndp = &nd; | |
248 | ||
249 | if ((error = suser(p->p_ucred, &p->p_acflag))) | |
250 | goto swapoff_bailout; | |
251 | ||
1c79356b A |
252 | /* |
253 | * Get the vnode for the paging area. | |
254 | */ | |
255 | NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, | |
256 | filename, p); | |
257 | ||
258 | if ((error = namei(ndp))) | |
259 | goto swapoff_bailout; | |
260 | vp = ndp->ni_vp; | |
261 | ||
262 | if (vp->v_type != VREG) { | |
263 | error = EINVAL; | |
264 | VOP_UNLOCK(vp, 0, p); | |
265 | goto swapoff_bailout; | |
266 | } | |
267 | ||
268 | for(i = 0; i < MAX_BACKING_STORE; i++) { | |
269 | if(bs_port_table[i].vp == vp) { | |
270 | backing_store; | |
271 | break; | |
272 | } | |
273 | } | |
274 | if (i == MAX_BACKING_STORE) { | |
275 | error = EINVAL; | |
276 | VOP_UNLOCK(vp, 0, p); | |
277 | goto swapoff_bailout; | |
278 | } | |
279 | backing_store = (mach_port_t)bs_port_table[i].bs; | |
280 | ||
281 | VOP_UNLOCK(vp, 0, p); | |
282 | kr = default_pager_backing_store_delete(backing_store); | |
283 | switch (kr) { | |
284 | case KERN_SUCCESS: | |
285 | error = 0; | |
286 | bs_port_table[i].vp = 0; | |
287 | ubc_rele(vp); | |
288 | /* This vnode is no longer used for swapfile */ | |
289 | CLR(vp->v_flag, VSWAP); | |
290 | ||
291 | /* get rid of macx_swapon() namei() reference */ | |
292 | vrele(vp); | |
293 | ||
294 | /* get rid of macx_swapon() "extra" reference */ | |
295 | vrele(vp); | |
296 | break; | |
297 | case KERN_FAILURE: | |
298 | error = EAGAIN; | |
299 | break; | |
300 | default: | |
301 | error = EAGAIN; | |
302 | break; | |
303 | } | |
304 | ||
305 | swapoff_bailout: | |
306 | /* get rid of macx_swapoff() namei() reference */ | |
307 | if (vp) | |
308 | vrele(vp); | |
309 | ||
1c79356b A |
310 | (void) thread_funnel_set(kernel_flock, FALSE); |
311 | return(error); | |
312 | } |