]> git.saurik.com Git - apple/xnu.git/blob - bsd/vm/dp_backing_file.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / bsd / vm / dp_backing_file.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/lock.h>
32 #include <sys/proc_internal.h>
33 #include <sys/kauth.h>
34 #include <sys/buf.h>
35 #include <sys/uio.h>
36 #include <sys/vnode_internal.h>
37 #include <sys/namei.h>
38 #include <sys/ubc_internal.h>
39 #include <sys/malloc.h>
40
41 #include <default_pager/default_pager_types.h>
42 #include <default_pager/default_pager_object.h>
43
44 #include <bsm/audit_kernel.h>
45 #include <bsm/audit_kevents.h>
46
47 #include <mach/mach_types.h>
48 #include <mach/host_priv.h>
49 #include <mach/mach_traps.h>
50 #include <mach/boolean.h>
51
52 #include <kern/kern_types.h>
53 #include <kern/host.h>
54 #include <kern/task.h>
55 #include <kern/zalloc.h>
56 #include <kern/kalloc.h>
57 #include <kern/assert.h>
58
59 #include <libkern/libkern.h>
60
61 #include <vm/vm_pageout.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vnode_pager.h>
65 #include <vm/vm_protos.h>
66
67 extern thread_t current_act(void);
68
69 /*
70 * temporary support for delayed instantiation
71 * of default_pager
72 */
73 int default_pager_init_flag = 0;
74
75 struct bs_map bs_port_table[MAX_BACKING_STORE] = {
76 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
77 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
78 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
79 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
80 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
81 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
82 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
83 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
84 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
85 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
86
87 /* ###################################################### */
88
89
90 /*
91 * Routine: macx_backing_store_recovery
92 * Function:
93 * Syscall interface to set a tasks privilege
94 * level so that it is not subject to
95 * macx_backing_store_suspend
96 */
97 int
98 macx_backing_store_recovery(
99 struct macx_backing_store_recovery_args *args)
100 {
101 int pid = args->pid;
102 int error;
103 struct proc *p = current_proc();
104 boolean_t funnel_state;
105
106 funnel_state = thread_funnel_set(kernel_flock, TRUE);
107 if ((error = suser(kauth_cred_get(), 0)))
108 goto backing_store_recovery_return;
109
110 /* for now restrict backing_store_recovery */
111 /* usage to only present task */
112 if(pid != proc_selfpid()) {
113 error = EINVAL;
114 goto backing_store_recovery_return;
115 }
116
117 task_backing_store_privileged(p->task);
118
119 backing_store_recovery_return:
120 (void) thread_funnel_set(kernel_flock, FALSE);
121 return(error);
122 }
123
124 /*
125 * Routine: macx_backing_store_suspend
126 * Function:
127 * Syscall interface to stop new demand for
128 * backing store when backing store is low
129 */
130
131 int
132 macx_backing_store_suspend(
133 struct macx_backing_store_suspend_args *args)
134 {
135 boolean_t suspend = args->suspend;
136 int error;
137 boolean_t funnel_state;
138
139 funnel_state = thread_funnel_set(kernel_flock, TRUE);
140 if ((error = suser(kauth_cred_get(), 0)))
141 goto backing_store_suspend_return;
142
143 vm_backing_store_disable(suspend);
144
145 backing_store_suspend_return:
146 (void) thread_funnel_set(kernel_flock, FALSE);
147 return(error);
148 }
149
150 /*
151 * Routine: macx_swapon
152 * Function:
153 * Syscall interface to add a file to backing store
154 */
155 int
156 macx_swapon(
157 struct macx_swapon_args *args)
158 {
159 int size = args->size;
160 vnode_t vp = (vnode_t)NULL;
161 struct nameidata nd, *ndp;
162 struct proc *p = current_proc();
163 register int error;
164 kern_return_t kr;
165 mach_port_t backing_store;
166 memory_object_default_t default_pager;
167 int i;
168 boolean_t funnel_state;
169 off_t file_size;
170 struct vfs_context context;
171
172 context.vc_proc = p;
173 context.vc_ucred = kauth_cred_get();
174
175 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPON);
176 AUDIT_ARG(value, args->priority);
177
178 funnel_state = thread_funnel_set(kernel_flock, TRUE);
179 ndp = &nd;
180
181 if ((error = suser(kauth_cred_get(), 0)))
182 goto swapon_bailout;
183
184 if(default_pager_init_flag == 0) {
185 start_def_pager(NULL);
186 default_pager_init_flag = 1;
187 }
188
189 /*
190 * Get a vnode for the paging area.
191 */
192 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
193 ((IS_64BIT_PROCESS(p)) ? UIO_USERSPACE64 : UIO_USERSPACE32),
194 CAST_USER_ADDR_T(args->filename), &context);
195
196 if ((error = namei(ndp)))
197 goto swapon_bailout;
198 nameidone(ndp);
199 vp = ndp->ni_vp;
200
201 if (vp->v_type != VREG) {
202 error = EINVAL;
203 goto swapon_bailout;
204 }
205 UBCINFOCHECK("macx_swapon", vp);
206
207 /* get file size */
208 if ((error = vnode_size(vp, &file_size, &context)) != 0)
209 goto swapon_bailout;
210
211 /* resize to desired size if it's too small */
212 if ((file_size < (off_t)size) && ((error = vnode_setsize(vp, (off_t)size, 0, &context)) != 0))
213 goto swapon_bailout;
214
215 /* add new backing store to list */
216 i = 0;
217 while(bs_port_table[i].vp != 0) {
218 if(i == MAX_BACKING_STORE)
219 break;
220 i++;
221 }
222 if(i == MAX_BACKING_STORE) {
223 error = ENOMEM;
224 goto swapon_bailout;
225 }
226
227 /* remember the vnode. This vnode has namei() reference */
228 bs_port_table[i].vp = vp;
229
230 /*
231 * Look to see if we are already paging to this file.
232 */
233 /* make certain the copy send of kernel call will work */
234 default_pager = MEMORY_OBJECT_DEFAULT_NULL;
235 kr = host_default_memory_manager(host_priv_self(), &default_pager, 0);
236 if(kr != KERN_SUCCESS) {
237 error = EAGAIN;
238 bs_port_table[i].vp = 0;
239 goto swapon_bailout;
240 }
241
242 kr = default_pager_backing_store_create(default_pager,
243 -1, /* default priority */
244 0, /* default cluster size */
245 &backing_store);
246 memory_object_default_deallocate(default_pager);
247
248 if(kr != KERN_SUCCESS) {
249 error = ENOMEM;
250 bs_port_table[i].vp = 0;
251 goto swapon_bailout;
252 }
253
254 /*
255 * NOTE: we are able to supply PAGE_SIZE here instead of
256 * an actual record size or block number because:
257 * a: we do not support offsets from the beginning of the
258 * file (allowing for non page size/record modulo offsets.
259 * b: because allow paging will be done modulo page size
260 */
261
262 kr = default_pager_add_file(backing_store, (vnode_ptr_t) vp,
263 PAGE_SIZE, (int)(file_size/PAGE_SIZE));
264 if(kr != KERN_SUCCESS) {
265 bs_port_table[i].vp = 0;
266 if(kr == KERN_INVALID_ARGUMENT)
267 error = EINVAL;
268 else
269 error = ENOMEM;
270 goto swapon_bailout;
271 }
272 bs_port_table[i].bs = (void *)backing_store;
273 error = 0;
274
275 /* Mark this vnode as being used for swapfile */
276 SET(vp->v_flag, VSWAP);
277
278 ubc_setthreadcred(vp, p, current_thread());
279
280 /*
281 * take a long term reference on the vnode to keep
282 * vnreclaim() away from this vnode.
283 */
284 vnode_ref(vp);
285
286 swapon_bailout:
287 if (vp) {
288 vnode_put(vp);
289 }
290 (void) thread_funnel_set(kernel_flock, FALSE);
291 AUDIT_MACH_SYSCALL_EXIT(error);
292 return(error);
293 }
294
295 /*
296 * Routine: macx_swapoff
297 * Function:
298 * Syscall interface to remove a file from backing store
299 */
300 int
301 macx_swapoff(
302 struct macx_swapoff_args *args)
303 {
304 __unused int flags = args->flags;
305 kern_return_t kr;
306 mach_port_t backing_store;
307
308 struct vnode *vp = 0;
309 struct nameidata nd, *ndp;
310 struct proc *p = current_proc();
311 int i;
312 int error;
313 boolean_t funnel_state;
314 struct vfs_context context;
315
316 context.vc_proc = p;
317 context.vc_ucred = kauth_cred_get();
318
319 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPOFF);
320
321 funnel_state = thread_funnel_set(kernel_flock, TRUE);
322 backing_store = NULL;
323 ndp = &nd;
324
325 if ((error = suser(kauth_cred_get(), 0)))
326 goto swapoff_bailout;
327
328 /*
329 * Get the vnode for the paging area.
330 */
331 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
332 ((IS_64BIT_PROCESS(p)) ? UIO_USERSPACE64 : UIO_USERSPACE32),
333 CAST_USER_ADDR_T(args->filename), &context);
334
335 if ((error = namei(ndp)))
336 goto swapoff_bailout;
337 nameidone(ndp);
338 vp = ndp->ni_vp;
339
340 if (vp->v_type != VREG) {
341 error = EINVAL;
342 goto swapoff_bailout;
343 }
344
345 for(i = 0; i < MAX_BACKING_STORE; i++) {
346 if(bs_port_table[i].vp == vp) {
347 break;
348 }
349 }
350 if (i == MAX_BACKING_STORE) {
351 error = EINVAL;
352 goto swapoff_bailout;
353 }
354 backing_store = (mach_port_t)bs_port_table[i].bs;
355
356 kr = default_pager_backing_store_delete(backing_store);
357 switch (kr) {
358 case KERN_SUCCESS:
359 error = 0;
360 bs_port_table[i].vp = 0;
361 /* This vnode is no longer used for swapfile */
362 CLR(vp->v_flag, VSWAP);
363
364 /* get rid of macx_swapon() "long term" reference */
365 vnode_rele(vp);
366
367 break;
368 case KERN_FAILURE:
369 error = EAGAIN;
370 break;
371 default:
372 error = EAGAIN;
373 break;
374 }
375
376 swapoff_bailout:
377 /* get rid of macx_swapoff() namei() reference */
378 if (vp)
379 vnode_put(vp);
380
381 (void) thread_funnel_set(kernel_flock, FALSE);
382 AUDIT_MACH_SYSCALL_EXIT(error);
383 return(error);
384 }
385
386 /*
387 * Routine: macx_swapinfo
388 * Function:
389 * Syscall interface to get general swap statistics
390 */
391 int
392 macx_swapinfo(
393 memory_object_size_t *total_p,
394 memory_object_size_t *avail_p,
395 vm_size_t *pagesize_p,
396 boolean_t *encrypted_p)
397 {
398 int error;
399 memory_object_default_t default_pager;
400 default_pager_info_64_t dpi64;
401 kern_return_t kr;
402
403 error = 0;
404
405 /*
406 * Get a handle on the default pager.
407 */
408 default_pager = MEMORY_OBJECT_DEFAULT_NULL;
409 kr = host_default_memory_manager(host_priv_self(), &default_pager, 0);
410 if (kr != KERN_SUCCESS) {
411 error = EAGAIN; /* XXX why EAGAIN ? */
412 goto done;
413 }
414 if (default_pager == MEMORY_OBJECT_DEFAULT_NULL) {
415 /*
416 * The default pager has not initialized yet,
417 * so it can't be using any swap space at all.
418 */
419 *total_p = 0;
420 *avail_p = 0;
421 *pagesize_p = 0;
422 *encrypted_p = FALSE;
423 goto done;
424 }
425
426 /*
427 * Get swap usage data from default pager.
428 */
429 kr = default_pager_info_64(default_pager, &dpi64);
430 if (kr != KERN_SUCCESS) {
431 error = ENOTSUP;
432 goto done;
433 }
434
435 /*
436 * Provide default pager info to caller.
437 */
438 *total_p = dpi64.dpi_total_space;
439 *avail_p = dpi64.dpi_free_space;
440 *pagesize_p = dpi64.dpi_page_size;
441 if (dpi64.dpi_flags & DPI_ENCRYPTED) {
442 *encrypted_p = TRUE;
443 } else {
444 *encrypted_p = FALSE;
445 }
446
447 done:
448 if (default_pager != MEMORY_OBJECT_DEFAULT_NULL) {
449 /* release our handle on default pager */
450 memory_object_default_deallocate(default_pager);
451 }
452 return error;
453 }