/*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
*
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
*
- * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
- *
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the
+ * License may not be used to create, or enable the creation or
+ * redistribution of, unlawful or unlicensed copies of an Apple operating
+ * system, or to circumvent, violate, or enable the circumvention or
+ * violation of, any terms of an Apple operating system software license
+ * agreement.
+ *
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
* file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
* limitations under the License.
- *
- * @APPLE_LICENSE_HEADER_END@
+ *
+ * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
*/
-#include <mach/boolean.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/lock.h>
-#include <sys/proc.h>
+#include <sys/proc_internal.h>
+#include <sys/kauth.h>
#include <sys/buf.h>
#include <sys/uio.h>
-#include <sys/vnode.h>
+#include <sys/vnode_internal.h>
#include <sys/namei.h>
-#include <sys/ubc.h>
+#include <sys/ubc_internal.h>
+#include <sys/malloc.h>
+
+#include <default_pager/default_pager_types.h>
+#include <default_pager/default_pager_object.h>
+
+#include <bsm/audit_kernel.h>
+#include <bsm/audit_kevents.h>
#include <mach/mach_types.h>
-#include <vm/vm_map.h>
-#include <vm/vm_kern.h>
+#include <mach/host_priv.h>
+#include <mach/mach_traps.h>
+#include <mach/boolean.h>
+
+#include <kern/kern_types.h>
#include <kern/host.h>
+#include <kern/task.h>
#include <kern/zalloc.h>
#include <kern/kalloc.h>
+#include <kern/assert.h>
+
#include <libkern/libkern.h>
-#include <sys/malloc.h>
+#include <vm/vm_pageout.h>
+#include <vm/vm_map.h>
+#include <vm/vm_kern.h>
#include <vm/vnode_pager.h>
+#include <vm/vm_protos.h>
+
+extern thread_t current_act(void);
/*
* temporary support for delayed instantiation
/* ###################################################### */
-#include <kern/assert.h>
+/*
+ * Routine: macx_backing_store_recovery
+ * Function:
+ * Syscall interface to set a tasks privilege
+ * level so that it is not subject to
+ * macx_backing_store_suspend
+ */
+int
+macx_backing_store_recovery(
+ struct macx_backing_store_recovery_args *args)
+{
+ int pid = args->pid;
+ int error;
+ struct proc *p = current_proc();
+ boolean_t funnel_state;
+
+ funnel_state = thread_funnel_set(kernel_flock, TRUE);
+ if ((error = suser(kauth_cred_get(), 0)))
+ goto backing_store_recovery_return;
+
+ /* for now restrict backing_store_recovery */
+ /* usage to only present task */
+ if(pid != proc_selfpid()) {
+ error = EINVAL;
+ goto backing_store_recovery_return;
+ }
+
+ task_backing_store_privileged(p->task);
+
+backing_store_recovery_return:
+ (void) thread_funnel_set(kernel_flock, FALSE);
+ return(error);
+}
+
+/*
+ * Routine: macx_backing_store_suspend
+ * Function:
+ * Syscall interface to stop new demand for
+ * backing store when backing store is low
+ */
+
+int
+macx_backing_store_suspend(
+ struct macx_backing_store_suspend_args *args)
+{
+ boolean_t suspend = args->suspend;
+ int error;
+ boolean_t funnel_state;
+
+ funnel_state = thread_funnel_set(kernel_flock, TRUE);
+ if ((error = suser(kauth_cred_get(), 0)))
+ goto backing_store_suspend_return;
+
+ vm_backing_store_disable(suspend);
+
+backing_store_suspend_return:
+ (void) thread_funnel_set(kernel_flock, FALSE);
+ return(error);
+}
/*
* Routine: macx_swapon
*/
int
macx_swapon(
- char *filename,
- int flags,
- long size,
- long priority)
+ struct macx_swapon_args *args)
{
- struct vnode *vp = 0;
+ int size = args->size;
+ vnode_t vp = (vnode_t)NULL;
struct nameidata nd, *ndp;
struct proc *p = current_proc();
- pager_file_t pf;
register int error;
kern_return_t kr;
mach_port_t backing_store;
memory_object_default_t default_pager;
int i;
boolean_t funnel_state;
+ off_t file_size;
+ struct vfs_context context;
- struct vattr vattr;
+ context.vc_proc = p;
+ context.vc_ucred = kauth_cred_get();
+
+ AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPON);
+ AUDIT_ARG(value, args->priority);
funnel_state = thread_funnel_set(kernel_flock, TRUE);
ndp = &nd;
- if ((error = suser(p->p_ucred, &p->p_acflag)))
+ if ((error = suser(kauth_cred_get(), 0)))
goto swapon_bailout;
if(default_pager_init_flag == 0) {
/*
* Get a vnode for the paging area.
*/
- NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
- filename, p);
+ NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
+ ((IS_64BIT_PROCESS(p)) ? UIO_USERSPACE64 : UIO_USERSPACE32),
+ CAST_USER_ADDR_T(args->filename), &context);
if ((error = namei(ndp)))
goto swapon_bailout;
+ nameidone(ndp);
vp = ndp->ni_vp;
if (vp->v_type != VREG) {
error = EINVAL;
- VOP_UNLOCK(vp, 0, p);
goto swapon_bailout;
}
UBCINFOCHECK("macx_swapon", vp);
- if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) {
- VOP_UNLOCK(vp, 0, p);
+ /* get file size */
+ if ((error = vnode_size(vp, &file_size, &context)) != 0)
goto swapon_bailout;
- }
- if (vattr.va_size < (u_quad_t)size) {
- vattr_null(&vattr);
- vattr.va_size = (u_quad_t)size;
- error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
- if (error) {
- VOP_UNLOCK(vp, 0, p);
- goto swapon_bailout;
- }
- }
+ /* resize to desired size if it's too small */
+ if ((file_size < (off_t)size) && ((error = vnode_setsize(vp, (off_t)size, 0, &context)) != 0))
+ goto swapon_bailout;
/* add new backing store to list */
i = 0;
}
if(i == MAX_BACKING_STORE) {
error = ENOMEM;
- VOP_UNLOCK(vp, 0, p);
goto swapon_bailout;
}
kr = host_default_memory_manager(host_priv_self(), &default_pager, 0);
if(kr != KERN_SUCCESS) {
error = EAGAIN;
- VOP_UNLOCK(vp, 0, p);
bs_port_table[i].vp = 0;
goto swapon_bailout;
}
if(kr != KERN_SUCCESS) {
error = ENOMEM;
- VOP_UNLOCK(vp, 0, p);
bs_port_table[i].vp = 0;
goto swapon_bailout;
}
* b: because allow paging will be done modulo page size
*/
- VOP_UNLOCK(vp, 0, p);
- kr = default_pager_add_file(backing_store, vp, PAGE_SIZE,
- ((int)vattr.va_size)/PAGE_SIZE);
+ kr = default_pager_add_file(backing_store, (vnode_ptr_t) vp,
+ PAGE_SIZE, (int)(file_size/PAGE_SIZE));
if(kr != KERN_SUCCESS) {
bs_port_table[i].vp = 0;
if(kr == KERN_INVALID_ARGUMENT)
}
bs_port_table[i].bs = (void *)backing_store;
error = 0;
- if (!ubc_hold(vp))
- panic("macx_swapon: hold");
/* Mark this vnode as being used for swapfile */
SET(vp->v_flag, VSWAP);
+ ubc_setthreadcred(vp, p, current_thread());
+
/*
- * take an extra reference on the vnode to keep
+ * take a long term reference on the vnode to keep
* vnreclaim() away from this vnode.
*/
- VREF(vp);
-
- /* Hold on to the namei reference to the paging file vnode */
- vp = 0;
+ vnode_ref(vp);
swapon_bailout:
if (vp) {
- vrele(vp);
+ vnode_put(vp);
}
(void) thread_funnel_set(kernel_flock, FALSE);
+ AUDIT_MACH_SYSCALL_EXIT(error);
return(error);
}
*/
int
macx_swapoff(
- char *filename,
- int flags)
+ struct macx_swapoff_args *args)
{
+ __unused int flags = args->flags;
kern_return_t kr;
mach_port_t backing_store;
int i;
int error;
boolean_t funnel_state;
+ struct vfs_context context;
+
+ context.vc_proc = p;
+ context.vc_ucred = kauth_cred_get();
+
+ AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPOFF);
funnel_state = thread_funnel_set(kernel_flock, TRUE);
backing_store = NULL;
ndp = &nd;
- if ((error = suser(p->p_ucred, &p->p_acflag)))
+ if ((error = suser(kauth_cred_get(), 0)))
goto swapoff_bailout;
/*
* Get the vnode for the paging area.
*/
- NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
- filename, p);
+ NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
+ ((IS_64BIT_PROCESS(p)) ? UIO_USERSPACE64 : UIO_USERSPACE32),
+ CAST_USER_ADDR_T(args->filename), &context);
if ((error = namei(ndp)))
goto swapoff_bailout;
+ nameidone(ndp);
vp = ndp->ni_vp;
if (vp->v_type != VREG) {
error = EINVAL;
- VOP_UNLOCK(vp, 0, p);
goto swapoff_bailout;
}
for(i = 0; i < MAX_BACKING_STORE; i++) {
if(bs_port_table[i].vp == vp) {
- backing_store;
break;
}
}
if (i == MAX_BACKING_STORE) {
error = EINVAL;
- VOP_UNLOCK(vp, 0, p);
goto swapoff_bailout;
}
backing_store = (mach_port_t)bs_port_table[i].bs;
- VOP_UNLOCK(vp, 0, p);
kr = default_pager_backing_store_delete(backing_store);
switch (kr) {
case KERN_SUCCESS:
error = 0;
bs_port_table[i].vp = 0;
- ubc_rele(vp);
/* This vnode is no longer used for swapfile */
CLR(vp->v_flag, VSWAP);
- /* get rid of macx_swapon() namei() reference */
- vrele(vp);
+ /* get rid of macx_swapon() "long term" reference */
+ vnode_rele(vp);
- /* get rid of macx_swapon() "extra" reference */
- vrele(vp);
break;
case KERN_FAILURE:
error = EAGAIN;
swapoff_bailout:
/* get rid of macx_swapoff() namei() reference */
if (vp)
- vrele(vp);
+ vnode_put(vp);
(void) thread_funnel_set(kernel_flock, FALSE);
+ AUDIT_MACH_SYSCALL_EXIT(error);
return(error);
}
+
+/*
+ * Routine: macx_swapinfo
+ * Function:
+ * Syscall interface to get general swap statistics
+ */
+int
+macx_swapinfo(
+ memory_object_size_t *total_p,
+ memory_object_size_t *avail_p,
+ vm_size_t *pagesize_p,
+ boolean_t *encrypted_p)
+{
+ int error;
+ memory_object_default_t default_pager;
+ default_pager_info_64_t dpi64;
+ kern_return_t kr;
+
+ error = 0;
+
+ /*
+ * Get a handle on the default pager.
+ */
+ default_pager = MEMORY_OBJECT_DEFAULT_NULL;
+ kr = host_default_memory_manager(host_priv_self(), &default_pager, 0);
+ if (kr != KERN_SUCCESS) {
+ error = EAGAIN; /* XXX why EAGAIN ? */
+ goto done;
+ }
+ if (default_pager == MEMORY_OBJECT_DEFAULT_NULL) {
+ /*
+ * The default pager has not initialized yet,
+ * so it can't be using any swap space at all.
+ */
+ *total_p = 0;
+ *avail_p = 0;
+ *pagesize_p = 0;
+ *encrypted_p = FALSE;
+ goto done;
+ }
+
+ /*
+ * Get swap usage data from default pager.
+ */
+ kr = default_pager_info_64(default_pager, &dpi64);
+ if (kr != KERN_SUCCESS) {
+ error = ENOTSUP;
+ goto done;
+ }
+
+ /*
+ * Provide default pager info to caller.
+ */
+ *total_p = dpi64.dpi_total_space;
+ *avail_p = dpi64.dpi_free_space;
+ *pagesize_p = dpi64.dpi_page_size;
+ if (dpi64.dpi_flags & DPI_ENCRYPTED) {
+ *encrypted_p = TRUE;
+ } else {
+ *encrypted_p = FALSE;
+ }
+
+done:
+ if (default_pager != MEMORY_OBJECT_DEFAULT_NULL) {
+ /* release our handle on default pager */
+ memory_object_default_deallocate(default_pager);
+ }
+ return error;
+}