]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_resource.c
xnu-6153.11.26.tar.gz
[apple/xnu.git] / bsd / kern / kern_resource.c
index 780159263d051645e27aea413e9f7592123608a2..7978cff02ed0b314e8c672a2c3e6c56915cf540c 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
- * 
+ *
  * 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
  * 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,
@@ -22,7 +22,7 @@
  * 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_OSREFERENCE_LICENSE_HEADER_END@
  */
 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
@@ -99,9 +99,9 @@
 #include <kern/policy_internal.h>
 
 #include <kern/task.h>
-#include <kern/clock.h>                /* for absolutetime_to_microtime() */
-#include <netinet/in.h>                /* for TRAFFIC_MGT_SO_* */
-#include <sys/socketvar.h>     /* for struct socket */
+#include <kern/clock.h>         /* for absolutetime_to_microtime() */
+#include <netinet/in.h>         /* for TRAFFIC_MGT_SO_* */
+#include <sys/socketvar.h>      /* for struct socket */
 #if NECP
 #include <net/necp.h>
 #endif /* NECP */
 #include <security/mac_framework.h>
 #endif
 
-int    donice(struct proc *curp, struct proc *chgp, int n);
-int    dosetrlimit(struct proc *p, u_int which, struct rlimit *limp);
-int    uthread_get_background_state(uthread_t);
+int     donice(struct proc *curp, struct proc *chgp, int n);
+int     dosetrlimit(struct proc *p, u_int which, struct rlimit *limp);
+int     uthread_get_background_state(uthread_t);
 static void do_background_socket(struct proc *p, thread_t thread);
 static int do_background_thread(thread_t thread, int priority);
 static int do_background_proc(struct proc *curp, struct proc *targetp, int priority);
@@ -133,13 +133,13 @@ int fill_task_rusage(task_t task, rusage_info_current *ri);
 void fill_task_billed_usage(task_t task, rusage_info_current *ri);
 int fill_task_io_rusage(task_t task, rusage_info_current *ri);
 int fill_task_qos_rusage(task_t task, rusage_info_current *ri);
-uint64_t get_task_logical_writes(task_t task);
+uint64_t get_task_logical_writes(task_t task, boolean_t external);
 void fill_task_monotonic_rusage(task_t task, rusage_info_current *ri);
 
 int proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie);
 
-rlim_t maxdmap = MAXDSIZ;      /* XXX */ 
-rlim_t maxsmap = MAXSSIZ - PAGE_MAX_SIZE;      /* XXX */ 
+rlim_t maxdmap = MAXDSIZ;       /* XXX */
+rlim_t maxsmap = MAXSSIZ - PAGE_MAX_SIZE;       /* XXX */
 
 /*
  * Limits on the number of open files per process, and the number
@@ -147,21 +147,21 @@ rlim_t maxsmap = MAXSSIZ - PAGE_MAX_SIZE; /* XXX */
  *
  * Note: would be in kern/subr_param.c in FreeBSD.
  */
-__private_extern__ int maxfilesperproc = OPEN_MAX;             /* per-proc open files limit */
+__private_extern__ int maxfilesperproc = OPEN_MAX;              /* per-proc open files limit */
 
 SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW | CTLFLAG_LOCKED,
-               &maxprocperuid, 0, "Maximum processes allowed per userid" );
+    &maxprocperuid, 0, "Maximum processes allowed per userid" );
 
 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW | CTLFLAG_LOCKED,
-               &maxfilesperproc, 0, "Maximum files allowed open per process" );
+    &maxfilesperproc, 0, "Maximum files allowed open per process" );
 
 /* Args and fn for proc_iteration callback used in setpriority */
 struct puser_nice_args {
        proc_t curp;
-       int     prio;
-       id_t    who;
-       int *   foundp;
-       int *   errorp;
+       int     prio;
+       id_t    who;
+       int *   foundp;
+       int *   errorp;
 };
 static int puser_donice_callback(proc_t p, void * arg);
 
@@ -169,9 +169,9 @@ static int puser_donice_callback(proc_t p, void * arg);
 /* Args and fn for proc_iteration callback used in setpriority */
 struct ppgrp_nice_args {
        proc_t curp;
-       int     prio;
-       int *   foundp;
-       int *   errorp;
+       int     prio;
+       int *   foundp;
+       int *   errorp;
 };
 static int ppgrp_donice_callback(proc_t p, void * arg);
 
@@ -188,22 +188,22 @@ getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
        int error = 0;
 
        /* would also test (uap->who < 0), but id_t is unsigned */
-       if (uap->who > 0x7fffffff)
-               return (EINVAL);
+       if (uap->who > 0x7fffffff) {
+               return EINVAL;
+       }
 
        switch (uap->which) {
-
        case PRIO_PROCESS:
                if (uap->who == 0) {
                        p = curp;
                        low = p->p_nice;
                } else {
                        p = proc_find(uap->who);
-                       if (p == 0)
+                       if (p == 0) {
                                break;
+                       }
                        low = p->p_nice;
                        proc_rele(p);
-
                }
                break;
 
@@ -213,14 +213,15 @@ getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
                if (uap->who == 0) {
                        /* returns the pgrp to ref */
                        pg = proc_pgrp(curp);
-                } else if ((pg = pgfind(uap->who)) == PGRP_NULL) {
+               } else if ((pg = pgfind(uap->who)) == PGRP_NULL) {
                        break;
                }
                /* No need for iteration as it is a simple scan */
                pgrp_lock(pg);
                PGMEMBERS_FOREACH(pg, p) {
-                       if (p->p_nice < low)
+                       if (p->p_nice < low) {
                                low = p->p_nice;
+                       }
                }
                pgrp_unlock(pg);
                pg_rele(pg);
@@ -228,16 +229,18 @@ getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
        }
 
        case PRIO_USER:
-               if (uap->who == 0)
+               if (uap->who == 0) {
                        uap->who = kauth_cred_getuid(kauth_cred_get());
+               }
 
                proc_list_lock();
 
                for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
                        my_cred = kauth_cred_proc_ref(p);
                        if (kauth_cred_getuid(my_cred) == uap->who &&
-                           p->p_nice < low)
+                           p->p_nice < low) {
                                low = p->p_nice;
+                       }
                        kauth_cred_unref(&my_cred);
                }
 
@@ -247,8 +250,9 @@ getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
 
        case PRIO_DARWIN_THREAD:
                /* we currently only support the current thread */
-               if (uap->who != 0)
-                       return (EINVAL);
+               if (uap->who != 0) {
+                       return EINVAL;
+               }
 
                low = proc_get_thread_policy(current_thread(), TASK_POLICY_INTERNAL, TASK_POLICY_DARWIN_BG);
 
@@ -259,17 +263,20 @@ getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
                        p = curp;
                } else {
                        p = proc_find(uap->who);
-                       if (p == PROC_NULL)
+                       if (p == PROC_NULL) {
                                break;
+                       }
                        refheld = 1;
                }
 
                error = get_background_proc(curp, p, &low);
 
-               if (refheld)
+               if (refheld) {
                        proc_rele(p);
-               if (error)
-                       return (error);
+               }
+               if (error) {
+                       return error;
+               }
                break;
 
        case PRIO_DARWIN_ROLE:
@@ -277,26 +284,30 @@ getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
                        p = curp;
                } else {
                        p = proc_find(uap->who);
-                       if (p == PROC_NULL)
+                       if (p == PROC_NULL) {
                                break;
+                       }
                        refheld = 1;
                }
 
                error = proc_get_darwin_role(curp, p, &low);
 
-               if (refheld)
+               if (refheld) {
                        proc_rele(p);
-               if (error)
-                       return (error);
+               }
+               if (error) {
+                       return error;
+               }
                break;
 
        default:
-               return (EINVAL);
+               return EINVAL;
+       }
+       if (low == PRIO_MAX + 1) {
+               return ESRCH;
        }
-       if (low == PRIO_MAX + 1)
-               return (ESRCH);
        *retval = low;
-       return (0);
+       return 0;
 }
 
 /* call back function used for proc iteration in PRIO_USER */
@@ -310,16 +321,17 @@ puser_donice_callback(proc_t p, void * arg)
        my_cred = kauth_cred_proc_ref(p);
        if (kauth_cred_getuid(my_cred) == pun->who) {
                error = donice(pun->curp, p, pun->prio);
-               if (pun->errorp != NULL)
+               if (pun->errorp != NULL) {
                        *pun->errorp = error;
+               }
                if (pun->foundp != NULL) {
                        n = *pun->foundp;
-                       *pun->foundp = n+1;
+                       *pun->foundp = n + 1;
                }
        }
        kauth_cred_unref(&my_cred);
 
-       return(PROC_RETURNED);
+       return PROC_RETURNED;
 }
 
 /* call back function used for proc iteration in PRIO_PGRP */
@@ -331,14 +343,15 @@ ppgrp_donice_callback(proc_t p, void * arg)
        int n;
 
        error = donice(pun->curp, p, pun->prio);
-       if (pun->errorp != NULL)
+       if (pun->errorp != NULL) {
                *pun->errorp = error;
-       if (pun->foundp!= NULL) {
+       }
+       if (pun->foundp != NULL) {
                n = *pun->foundp;
-               *pun->foundp = n+1;
+               *pun->foundp = n + 1;
        }
 
-       return(PROC_RETURNED);
+       return PROC_RETURNED;
 }
 
 /*
@@ -361,40 +374,43 @@ setpriority(struct proc *curp, struct setpriority_args *uap, int32_t *retval)
        AUDIT_ARG(value32, uap->prio);
 
        /* would also test (uap->who < 0), but id_t is unsigned */
-       if (uap->who > 0x7fffffff)
-               return (EINVAL);
+       if (uap->who > 0x7fffffff) {
+               return EINVAL;
+       }
 
        switch (uap->which) {
-
        case PRIO_PROCESS:
-               if (uap->who == 0)
+               if (uap->who == 0) {
                        p = curp;
-               else {
+               else {
                        p = proc_find(uap->who);
-                       if (p == 0)
+                       if (p == 0) {
                                break;
+                       }
                        refheld = 1;
                }
                error = donice(curp, p, uap->prio);
                found++;
-               if (refheld != 0)
+               if (refheld != 0) {
                        proc_rele(p);
+               }
                break;
 
        case PRIO_PGRP: {
                struct pgrp *pg = PGRP_NULL;
                struct ppgrp_nice_args ppgrp;
-                
+
                if (uap->who == 0) {
                        pg = proc_pgrp(curp);
-                } else if ((pg = pgfind(uap->who)) == PGRP_NULL)
+               } else if ((pg = pgfind(uap->who)) == PGRP_NULL) {
                        break;
+               }
 
                ppgrp.curp = curp;
                ppgrp.prio = uap->prio;
                ppgrp.foundp = &found;
                ppgrp.errorp = &error;
-               
+
                /* PGRP_DROPREF drops the reference on process group */
                pgrp_iterate(pg, PGRP_DROPREF, ppgrp_donice_callback, (void *)&ppgrp, NULL, NULL);
 
@@ -404,8 +420,9 @@ setpriority(struct proc *curp, struct setpriority_args *uap, int32_t *retval)
        case PRIO_USER: {
                struct puser_nice_args punice;
 
-               if (uap->who == 0)
+               if (uap->who == 0) {
                        uap->who = kauth_cred_getuid(kauth_cred_get());
+               }
 
                punice.curp = curp;
                punice.prio = uap->prio;
@@ -420,8 +437,9 @@ setpriority(struct proc *curp, struct setpriority_args *uap, int32_t *retval)
 
        case PRIO_DARWIN_THREAD: {
                /* we currently only support the current thread */
-               if (uap->who != 0)
-                       return (EINVAL);
+               if (uap->who != 0) {
+                       return EINVAL;
+               }
 
                error = do_background_thread(current_thread(), uap->prio);
                found++;
@@ -429,30 +447,34 @@ setpriority(struct proc *curp, struct setpriority_args *uap, int32_t *retval)
        }
 
        case PRIO_DARWIN_PROCESS: {
-               if (uap->who == 0)
+               if (uap->who == 0) {
                        p = curp;
-               else {
+               else {
                        p = proc_find(uap->who);
-                       if (p == 0)
+                       if (p == 0) {
                                break;
+                       }
                        refheld = 1;
                }
 
                error = do_background_proc(curp, p, uap->prio);
 
                found++;
-               if (refheld != 0)
+               if (refheld != 0) {
                        proc_rele(p);
+               }
                break;
        }
 
        case PRIO_DARWIN_GPU: {
-               if (uap->who == 0)
-                       return (EINVAL);
+               if (uap->who == 0) {
+                       return EINVAL;
+               }
 
                p = proc_find(uap->who);
-               if (p == PROC_NULL)
+               if (p == PROC_NULL) {
                        break;
+               }
 
                error = set_gpudeny_proc(curp, p, uap->prio);
 
@@ -466,29 +488,32 @@ setpriority(struct proc *curp, struct setpriority_args *uap, int32_t *retval)
                        p = curp;
                } else {
                        p = proc_find(uap->who);
-                       if (p == PROC_NULL)
+                       if (p == PROC_NULL) {
                                break;
+                       }
                        refheld = 1;
                }
 
                error = proc_set_darwin_role(curp, p, uap->prio);
 
                found++;
-               if (refheld != 0)
+               if (refheld != 0) {
                        proc_rele(p);
+               }
                break;
        }
 
        default:
-               return (EINVAL);
+               return EINVAL;
+       }
+       if (found == 0) {
+               return ESRCH;
        }
-       if (found == 0)
-               return (ESRCH);
        if (error == EIDRM) {
                *retval = -2;
                error = 0;
        }
-       return (error);
+       return error;
 }
 
 
@@ -514,18 +539,21 @@ donice(struct proc *curp, struct proc *chgp, int n)
                error = EPERM;
                goto out;
        }
-       if (n > PRIO_MAX)
+       if (n > PRIO_MAX) {
                n = PRIO_MAX;
-       if (n < PRIO_MIN)
+       }
+       if (n < PRIO_MIN) {
                n = PRIO_MIN;
+       }
        if (n < chgp->p_nice && suser(ucred, &curp->p_acflag)) {
                error = EACCES;
                goto out;
        }
 #if CONFIG_MACF
        error = mac_proc_check_sched(curp, chgp);
-       if (error) 
+       if (error) {
                goto out;
+       }
 #endif
        proc_lock(chgp);
        chgp->p_nice = n;
@@ -534,7 +562,7 @@ donice(struct proc *curp, struct proc *chgp, int n)
 out:
        kauth_cred_unref(&ucred);
        kauth_cred_unref(&my_cred);
-       return (error);
+       return error;
 }
 
 static int
@@ -550,8 +578,8 @@ set_gpudeny_proc(struct proc *curp, struct proc *targetp, int priority)
        /* TODO: Entitlement instead of uid check */
 
        if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
-           kauth_cred_getuid(ucred)   != kauth_cred_getuid(target_cred) &&
-           kauth_cred_getruid(ucred)  != kauth_cred_getuid(target_cred)) {
+           kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
+           kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
                error = EPERM;
                goto out;
        }
@@ -563,26 +591,26 @@ set_gpudeny_proc(struct proc *curp, struct proc *targetp, int priority)
 
 #if CONFIG_MACF
        error = mac_proc_check_sched(curp, targetp);
-       if (error)
+       if (error) {
                goto out;
+       }
 #endif
 
        switch (priority) {
-               case PRIO_DARWIN_GPU_DENY:
-                       task_set_gpu_denied(proc_task(targetp), TRUE);
-                       break;
-               case PRIO_DARWIN_GPU_ALLOW:
-                       task_set_gpu_denied(proc_task(targetp), FALSE);
-                       break;
-               default:
-                       error = EINVAL;
-                       goto out;
+       case PRIO_DARWIN_GPU_DENY:
+               task_set_gpu_denied(proc_task(targetp), TRUE);
+               break;
+       case PRIO_DARWIN_GPU_ALLOW:
+               task_set_gpu_denied(proc_task(targetp), FALSE);
+               break;
+       default:
+               error = EINVAL;
+               goto out;
        }
 
 out:
        kauth_cred_unref(&target_cred);
-       return (error);
-
+       return error;
 }
 
 static int
@@ -597,7 +625,7 @@ proc_set_darwin_role(proc_t curp, proc_t targetp, int priority)
        target_cred = kauth_cred_proc_ref(targetp);
 
        if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
-           kauth_cred_getuid(ucred)  != kauth_cred_getuid(target_cred) &&
+           kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
            kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
                if (priv_check_cred(ucred, PRIV_SETPRIORITY_DARWIN_ROLE, 0) != 0) {
                        error = EPERM;
@@ -607,8 +635,9 @@ proc_set_darwin_role(proc_t curp, proc_t targetp, int priority)
 
        if (curp != targetp) {
 #if CONFIG_MACF
-               if ((error = mac_proc_check_sched(curp, targetp)))
+               if ((error = mac_proc_check_sched(curp, targetp))) {
                        goto out;
+               }
 #endif
        }
 
@@ -620,15 +649,16 @@ proc_set_darwin_role(proc_t curp, proc_t targetp, int priority)
 
        integer_t role = 0;
 
-       if ((error = proc_darwin_role_to_task_role(priority, &role)))
+       if ((error = proc_darwin_role_to_task_role(priority, &role))) {
                goto out;
+       }
 
        proc_set_task_policy(proc_task(targetp), TASK_POLICY_ATTRIBUTE,
-                            TASK_POLICY_ROLE, role);
+           TASK_POLICY_ROLE, role);
 
 out:
        kauth_cred_unref(&target_cred);
-       return (error);
+       return error;
 }
 
 static int
@@ -643,7 +673,7 @@ proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority)
        target_cred = kauth_cred_proc_ref(targetp);
 
        if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
-           kauth_cred_getuid(ucred)  != kauth_cred_getuid(target_cred) &&
+           kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
            kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
                error = EPERM;
                goto out;
@@ -651,8 +681,9 @@ proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority)
 
        if (curp != targetp) {
 #if CONFIG_MACF
-               if ((error = mac_proc_check_sched(curp, targetp)))
+               if ((error = mac_proc_check_sched(curp, targetp))) {
                        goto out;
+               }
 #endif
        }
 
@@ -662,7 +693,7 @@ proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority)
 
 out:
        kauth_cred_unref(&target_cred);
-       return (error);
+       return error;
 }
 
 
@@ -689,7 +720,7 @@ get_background_proc(struct proc *curp, struct proc *targetp, int *priority)
 
 out:
        kauth_cred_unref(&target_cred);
-       return (error);
+       return error;
 }
 
 static int
@@ -708,55 +739,60 @@ do_background_proc(struct proc *curp, struct proc *targetp, int priority)
        target_cred = kauth_cred_proc_ref(targetp);
 
        if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
-               kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
-               kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred))
-       {
+           kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
+           kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
                error = EPERM;
                goto out;
        }
 
 #if CONFIG_MACF
        error = mac_proc_check_sched(curp, targetp);
-       if (error) 
+       if (error) {
                goto out;
+       }
 #endif
 
        external = (curp == targetp) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
 
        switch (priority) {
-               case PRIO_DARWIN_BG:
-                       enable = TASK_POLICY_ENABLE;
-                       break;
-               case PRIO_DARWIN_NONUI:
-                       /* ignored for compatibility */
-                       goto out;
-               default:
-                       /* TODO: EINVAL if priority != 0 */
-                       enable = TASK_POLICY_DISABLE;
-                       break;
+       case PRIO_DARWIN_BG:
+               enable = TASK_POLICY_ENABLE;
+               break;
+       case PRIO_DARWIN_NONUI:
+               /* ignored for compatibility */
+               goto out;
+       default:
+               /* TODO: EINVAL if priority != 0 */
+               enable = TASK_POLICY_DISABLE;
+               break;
        }
 
        proc_set_task_policy(proc_task(targetp), external, TASK_POLICY_DARWIN_BG, enable);
 
 out:
        kauth_cred_unref(&target_cred);
-       return (error);
+       return error;
 }
 
-static void 
+static void
 do_background_socket(struct proc *p, thread_t thread)
 {
 #if SOCKETS
        struct filedesc                     *fdp;
        struct fileproc                     *fp;
-       int                                 i, background;
+       int                                 i = 0;
+       int                                                                     background = false;
+#if NECP
+       int                                                                     update_necp = false;
+#endif /* NECP */
 
        proc_fdlock(p);
 
-       if (thread != THREAD_NULL)
+       if (thread != THREAD_NULL) {
                background = proc_get_effective_thread_policy(thread, TASK_POLICY_ALL_SOCKETS_BG);
-       else
+       } else {
                background = proc_get_effective_task_policy(proc_task(p), TASK_POLICY_ALL_SOCKETS_BG);
+       }
 
        if (background) {
                /*
@@ -779,23 +815,25 @@ do_background_socket(struct proc *p, thread_t thread)
                                }
 #if NECP
                                else if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_NETPOLICY) {
-                                       necp_set_client_as_background(p, fp, background);
+                                       if (necp_set_client_as_background(p, fp, background)) {
+                                               update_necp = true;
+                                       }
                                }
 #endif /* NECP */
                        }
                }
        } else {
                /* disable networking IO throttle.
-                * NOTE - It is a known limitation of the current design that we 
-                * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for 
-                * sockets created by other threads within this process.  
+                * NOTE - It is a known limitation of the current design that we
+                * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
+                * sockets created by other threads within this process.
                 */
                fdp = p->p_fd;
-               for ( i = 0; i < fdp->fd_nfiles; i++ ) {
+               for (i = 0; i < fdp->fd_nfiles; i++) {
                        struct socket       *sockp;
 
-                       fp = fdp->fd_ofiles[ i ];
-                       if (fp == NULL || (fdp->fd_ofileflags[ i ] & UF_RESERVED) != 0) {
+                       fp = fdp->fd_ofiles[i];
+                       if (fp == NULL || (fdp->fd_ofileflags[i] & UF_RESERVED) != 0) {
                                continue;
                        }
                        if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_SOCKET) {
@@ -809,13 +847,21 @@ do_background_socket(struct proc *p, thread_t thread)
                        }
 #if NECP
                        else if (FILEGLOB_DTYPE(fp->f_fglob) == DTYPE_NETPOLICY) {
-                               necp_set_client_as_background(p, fp, background);
+                               if (necp_set_client_as_background(p, fp, background)) {
+                                       update_necp = true;
+                               }
                        }
 #endif /* NECP */
                }
        }
 
        proc_fdunlock(p);
+
+#if NECP
+       if (update_necp) {
+               necp_update_all_clients();
+       }
+#endif /* NECP */
 #else
 #pragma unused(p, thread)
 #endif
@@ -841,12 +887,13 @@ do_background_thread(thread_t thread, int priority)
        ut = get_bsdthread_info(thread);
 
        /* Backgrounding is unsupported for threads in vfork */
-       if ((ut->uu_flag & UT_VFORK) != 0)
-               return(EPERM);
+       if ((ut->uu_flag & UT_VFORK) != 0) {
+               return EPERM;
+       }
 
        /* Backgrounding is unsupported for workq threads */
        if (thread_is_static_param(thread)) {
-               return(EPERM);
+               return EPERM;
        }
 
        /* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
@@ -878,10 +925,11 @@ setrlimit(struct proc *p, struct setrlimit_args *uap, __unused int32_t *retval)
        int error;
 
        if ((error = copyin(uap->rlp, (caddr_t)&alim,
-           sizeof (struct rlimit))))
-               return (error);
+           sizeof(struct rlimit)))) {
+               return error;
+       }
 
-       return (dosetrlimit(p, uap->which, &alim));
+       return dosetrlimit(p, uap->which, &alim);
 }
 
 /*
@@ -899,53 +947,54 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
 {
        struct rlimit *alimp;
        int error;
-       kern_return_t   kr;
+       kern_return_t   kr;
        int posix = (which & _RLIMIT_POSIX_FLAG) ? 1 : 0;
 
        /* Mask out POSIX flag, saved above */
        which &= ~_RLIMIT_POSIX_FLAG;
 
-       if (which >= RLIM_NLIMITS)
-               return (EINVAL);
+       if (which >= RLIM_NLIMITS) {
+               return EINVAL;
+       }
 
        alimp = &p->p_rlimit[which];
-       if (limp->rlim_cur > limp->rlim_max)
+       if (limp->rlim_cur > limp->rlim_max) {
                return EINVAL;
+       }
 
-       if (limp->rlim_cur > alimp->rlim_max || 
-           limp->rlim_max > alimp->rlim_max)
+       if (limp->rlim_cur > alimp->rlim_max ||
+           limp->rlim_max > alimp->rlim_max) {
                if ((error = suser(kauth_cred_get(), &p->p_acflag))) {
-                       return (error);
+                       return error;
+               }
        }
 
        proc_limitblock(p);
 
        if ((error = proc_limitreplace(p)) != 0) {
                proc_limitunblock(p);
-               return(error);
+               return error;
        }
 
        alimp = &p->p_rlimit[which];
-       
-       switch (which) {
 
+       switch (which) {
        case RLIMIT_CPU:
                if (limp->rlim_cur == RLIM_INFINITY) {
                        task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
                        timerclear(&p->p_rlim_cpu);
-               }
-               else {
-                       task_absolutetime_info_data_t   tinfo;
-                       mach_msg_type_number_t                  count;
-                       struct timeval                                  ttv, tv;
-                       clock_sec_t                                             tv_sec;
-                       clock_usec_t                                    tv_usec;
+               } else {
+                       task_absolutetime_info_data_t   tinfo;
+                       mach_msg_type_number_t                  count;
+                       struct timeval                                  ttv, tv;
+                       clock_sec_t                                             tv_sec;
+                       clock_usec_t                                    tv_usec;
 
                        count = TASK_ABSOLUTETIME_INFO_COUNT;
                        task_info(p->task, TASK_ABSOLUTETIME_INFO,
-                                                               (task_info_t)&tinfo, &count);
+                           (task_info_t)&tinfo, &count);
                        absolutetime_to_microtime(tinfo.total_user + tinfo.total_system,
-                                                                         &tv_sec, &tv_usec);
+                           &tv_sec, &tv_usec);
                        ttv.tv_sec = tv_sec;
                        ttv.tv_usec = tv_usec;
 
@@ -954,9 +1003,9 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
                        timersub(&tv, &ttv, &p->p_rlim_cpu);
 
                        timerclear(&tv);
-                       if (timercmp(&p->p_rlim_cpu, &tv, >))
+                       if (timercmp(&p->p_rlim_cpu, &tv, >)) {
                                task_vtimer_set(p->task, TASK_VTIMER_RLIM);
-                       else {
+                       else {
                                task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
 
                                timerclear(&p->p_rlim_cpu);
@@ -967,30 +1016,39 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
                break;
 
        case RLIMIT_DATA:
-               if (limp->rlim_cur > maxdmap)
+               if (limp->rlim_cur > maxdmap) {
                        limp->rlim_cur = maxdmap;
-               if (limp->rlim_max > maxdmap)
+               }
+               if (limp->rlim_max > maxdmap) {
                        limp->rlim_max = maxdmap;
+               }
                break;
 
        case RLIMIT_STACK:
+               if (p->p_lflag & P_LCUSTOM_STACK) {
+                       /* Process has a custom stack set - rlimit cannot be used to change it */
+                       error = EINVAL;
+                       goto out;
+               }
+
                /* Disallow illegal stack size instead of clipping */
                if (limp->rlim_cur > maxsmap ||
                    limp->rlim_max > maxsmap) {
                        if (posix) {
                                error = EINVAL;
                                goto out;
-                       }
-                       else {
-                               /* 
-                                * 4797860 - workaround poorly written installers by 
-                                * doing previous implementation (< 10.5) when caller 
+                       } else {
+                               /*
+                                * 4797860 - workaround poorly written installers by
+                                * doing previous implementation (< 10.5) when caller
                                 * is non-POSIX conforming.
                                 */
-                               if (limp->rlim_cur > maxsmap) 
+                               if (limp->rlim_cur > maxsmap) {
                                        limp->rlim_cur = maxsmap;
-                               if (limp->rlim_max > maxsmap) 
+                               }
+                               if (limp->rlim_max > maxsmap) {
                                        limp->rlim_max = maxsmap;
+                               }
                        }
                }
 
@@ -1002,15 +1060,15 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
                if (limp->rlim_cur > alimp->rlim_cur) {
                        user_addr_t addr;
                        user_size_t size;
-                       
-                               /* grow stack */
-                               size = round_page_64(limp->rlim_cur);
-                               size -= round_page_64(alimp->rlim_cur);
+
+                       /* grow stack */
+                       size = round_page_64(limp->rlim_cur);
+                       size -= round_page_64(alimp->rlim_cur);
 
                        addr = p->user_stack - round_page_64(limp->rlim_cur);
-                       kr = mach_vm_protect(current_map(), 
-                                            addr, size,
-                                            FALSE, VM_PROT_DEFAULT);
+                       kr = mach_vm_protect(current_map(),
+                           addr, size,
+                           FALSE, VM_PROT_DEFAULT);
                        if (kr != KERN_SUCCESS) {
                                error =  EINVAL;
                                goto out;
@@ -1020,7 +1078,7 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
                        user_size_t size;
                        user_addr_t cur_sp;
 
-                               /* shrink stack */
+                       /* shrink stack */
 
                        /*
                         * First check if new stack limit would agree
@@ -1028,13 +1086,13 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
                         * Get the current thread's stack pointer...
                         */
                        cur_sp = thread_adjuserstack(current_thread(),
-                                                    0);
+                           0);
                        if (cur_sp <= p->user_stack &&
                            cur_sp > (p->user_stack -
-                                     round_page_64(alimp->rlim_cur))) {
+                           round_page_64(alimp->rlim_cur))) {
                                /* stack pointer is in main stack */
                                if (cur_sp <= (p->user_stack -
-                                              round_page_64(limp->rlim_cur))) {
+                                   round_page_64(limp->rlim_cur))) {
                                        /*
                                         * New limit would cause
                                         * current usage to be invalid:
@@ -1048,15 +1106,15 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
                                error =  EINVAL;
                                goto out;
                        }
-                               
+
                        size = round_page_64(alimp->rlim_cur);
                        size -= round_page_64(limp->rlim_cur);
 
                        addr = p->user_stack - round_page_64(alimp->rlim_cur);
 
                        kr = mach_vm_protect(current_map(),
-                                            addr, size,
-                                            FALSE, VM_PROT_NONE);
+                           addr, size,
+                           FALSE, VM_PROT_NONE);
                        if (kr != KERN_SUCCESS) {
                                error =  EINVAL;
                                goto out;
@@ -1067,58 +1125,62 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
                break;
 
        case RLIMIT_NOFILE:
-               /* 
+               /*
                 * Only root can set the maxfiles limits, as it is
                 * systemwide resource.  If we are expecting POSIX behavior,
                 * instead of clamping the value, return EINVAL.  We do this
                 * because historically, people have been able to attempt to
                 * set RLIM_INFINITY to get "whatever the maximum is".
-               */
-               if ( kauth_cred_issuser(kauth_cred_get()) ) {
+                */
+               if (kauth_cred_issuser(kauth_cred_get())) {
                        if (limp->rlim_cur != alimp->rlim_cur &&
                            limp->rlim_cur > (rlim_t)maxfiles) {
-                               if (posix) {
+                               if (posix) {
                                        error =  EINVAL;
                                        goto out;
                                }
                                limp->rlim_cur = maxfiles;
                        }
                        if (limp->rlim_max != alimp->rlim_max &&
-                           limp->rlim_max > (rlim_t)maxfiles)
+                           limp->rlim_max > (rlim_t)maxfiles) {
                                limp->rlim_max = maxfiles;
-               }
-               else {
+                       }
+               else {
                        if (limp->rlim_cur != alimp->rlim_cur &&
                            limp->rlim_cur > (rlim_t)maxfilesperproc) {
-                               if (posix) {
+                               if (posix) {
                                        error =  EINVAL;
                                        goto out;
                                }
                                limp->rlim_cur = maxfilesperproc;
                        }
                        if (limp->rlim_max != alimp->rlim_max &&
-                           limp->rlim_max > (rlim_t)maxfilesperproc)
+                           limp->rlim_max > (rlim_t)maxfilesperproc) {
                                limp->rlim_max = maxfilesperproc;
+                       }
                }
                break;
 
        case RLIMIT_NPROC:
-               /* 
+               /*
                 * Only root can set to the maxproc limits, as it is
                 * systemwide resource; all others are limited to
                 * maxprocperuid (presumably less than maxproc).
                 */
-               if ( kauth_cred_issuser(kauth_cred_get()) ) {
-                       if (limp->rlim_cur > (rlim_t)maxproc)
+               if (kauth_cred_issuser(kauth_cred_get())) {
+                       if (limp->rlim_cur > (rlim_t)maxproc) {
                                limp->rlim_cur = maxproc;
-                       if (limp->rlim_max > (rlim_t)maxproc)
+                       }
+                       if (limp->rlim_max > (rlim_t)maxproc) {
                                limp->rlim_max = maxproc;
-               } 
-               else {
-                       if (limp->rlim_cur > (rlim_t)maxprocperuid)
+                       }
+               else {
+                       if (limp->rlim_cur > (rlim_t)maxprocperuid) {
                                limp->rlim_cur = maxprocperuid;
-                       if (limp->rlim_max > (rlim_t)maxprocperuid)
+                       }
+                       if (limp->rlim_max > (rlim_t)maxprocperuid) {
                                limp->rlim_max = maxprocperuid;
+                       }
                }
                break;
 
@@ -1129,7 +1191,6 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
 
                vm_map_set_user_wire_limit(current_map(), limp->rlim_cur);
                break;
-               
        } /* switch... */
        proc_lock(p);
        *alimp = *limp;
@@ -1137,7 +1198,7 @@ dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
        error = 0;
 out:
        proc_limitunblock(p);
-       return (error);
+       return error;
 }
 
 /* ARGSUSED */
@@ -1152,11 +1213,12 @@ getrlimit(struct proc *p, struct getrlimit_args *uap, __unused int32_t *retval)
         */
        uap->which &= ~_RLIMIT_POSIX_FLAG;
 
-       if (uap->which >= RLIM_NLIMITS)
-               return (EINVAL);
+       if (uap->which >= RLIM_NLIMITS) {
+               return EINVAL;
+       }
        proc_limitget(p, uap->which, &lim);
-       return (copyout((caddr_t)&lim,
-                       uap->rlp, sizeof (struct rlimit)));
+       return copyout((caddr_t)&lim,
+                  uap->rlp, sizeof(struct rlimit));
 }
 
 /*
@@ -1167,12 +1229,13 @@ getrlimit(struct proc *p, struct getrlimit_args *uap, __unused int32_t *retval)
 void
 calcru(struct proc *p, struct timeval *up, struct timeval *sp, struct timeval *ip)
 {
-       task_t                  task;
+       task_t                  task;
 
        timerclear(up);
        timerclear(sp);
-       if (ip != NULL)
+       if (ip != NULL) {
                timerclear(ip);
+       }
 
        task = p->task;
        if (task) {
@@ -1181,11 +1244,11 @@ calcru(struct proc *p, struct timeval *up, struct timeval *sp, struct timeval *i
                task_events_info_data_t teventsinfo;
                mach_msg_type_number_t task_info_count, task_ttimes_count;
                mach_msg_type_number_t task_events_count;
-               struct timeval ut,st;
+               struct timeval ut, st;
 
-               task_info_count = MACH_TASK_BASIC_INFO_COUNT;
+               task_info_count = MACH_TASK_BASIC_INFO_COUNT;
                task_info(task, MACH_TASK_BASIC_INFO,
-                         (task_info_t)&tinfo, &task_info_count);
+                   (task_info_t)&tinfo, &task_info_count);
                ut.tv_sec = tinfo.user_time.seconds;
                ut.tv_usec = tinfo.user_time.microseconds;
                st.tv_sec = tinfo.system_time.seconds;
@@ -1195,7 +1258,7 @@ calcru(struct proc *p, struct timeval *up, struct timeval *sp, struct timeval *i
 
                task_ttimes_count = TASK_THREAD_TIMES_INFO_COUNT;
                task_info(task, TASK_THREAD_TIMES_INFO,
-                         (task_info_t)&ttimesinfo, &task_ttimes_count);
+                   (task_info_t)&ttimesinfo, &task_ttimes_count);
 
                ut.tv_sec = ttimesinfo.user_time.seconds;
                ut.tv_usec = ttimesinfo.user_time.microseconds;
@@ -1206,19 +1269,20 @@ calcru(struct proc *p, struct timeval *up, struct timeval *sp, struct timeval *i
 
                task_events_count = TASK_EVENTS_INFO_COUNT;
                task_info(task, TASK_EVENTS_INFO,
-                         (task_info_t)&teventsinfo, &task_events_count);
+                   (task_info_t)&teventsinfo, &task_events_count);
 
                /*
                 * No need to lock "p":  this does not need to be
                 * completely consistent, right ?
                 */
                p->p_stats->p_ru.ru_minflt = (teventsinfo.faults -
-                                             teventsinfo.pageins);
+                   teventsinfo.pageins);
                p->p_stats->p_ru.ru_majflt = teventsinfo.pageins;
                p->p_stats->p_ru.ru_nivcsw = (teventsinfo.csw -
-                                             p->p_stats->p_ru.ru_nvcsw);
-               if (p->p_stats->p_ru.ru_nivcsw < 0)
+                   p->p_stats->p_ru.ru_nvcsw);
+               if (p->p_stats->p_ru.ru_nivcsw < 0) {
                        p->p_stats->p_ru.ru_nivcsw = 0;
+               }
 
                p->p_stats->p_ru.ru_maxrss = tinfo.resident_size_max;
        }
@@ -1232,10 +1296,10 @@ int
 getrusage(struct proc *p, struct getrusage_args *uap, __unused int32_t *retval)
 {
        struct rusage *rup, rubuf;
-       struct user64_rusage rubuf64;
-       struct user32_rusage rubuf32;
-       size_t retsize = sizeof(rubuf);                 /* default: 32 bits */
-       caddr_t retbuf = (caddr_t)&rubuf;               /* default: 32 bits */
+       struct user64_rusage rubuf64 = {};
+       struct user32_rusage rubuf32 = {};
+       size_t retsize = sizeof(rubuf);                 /* default: 32 bits */
+       caddr_t retbuf = (caddr_t)&rubuf;               /* default: 32 bits */
        struct timeval utime;
        struct timeval stime;
 
@@ -1261,7 +1325,7 @@ getrusage(struct proc *p, struct getrusage_args *uap, __unused int32_t *retval)
                break;
 
        default:
-               return (EINVAL);
+               return EINVAL;
        }
        if (IS_64BIT_PROCESS(p)) {
                retsize = sizeof(rubuf64);
@@ -1273,7 +1337,7 @@ getrusage(struct proc *p, struct getrusage_args *uap, __unused int32_t *retval)
                munge_user32_rusage(&rubuf, &rubuf32);
        }
 
-       return (copyout(retbuf, uap->rusage, retsize));
+       return copyout(retbuf, uap->rusage, retsize);
 }
 
 void
@@ -1284,16 +1348,18 @@ ruadd(struct rusage *ru, struct rusage *ru2)
 
        timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
        timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
-       if (ru->ru_maxrss < ru2->ru_maxrss)
+       if (ru->ru_maxrss < ru2->ru_maxrss) {
                ru->ru_maxrss = ru2->ru_maxrss;
+       }
        ip = &ru->ru_first; ip2 = &ru2->ru_first;
-       for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
+       for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) {
                *ip++ += *ip2++;
+       }
 }
 
 /*
  * Add the rusage stats of child in parent.
- * 
+ *
  * It adds rusage statistics of child process and statistics of all its
  * children to its parent.
  *
@@ -1303,17 +1369,17 @@ void
 update_rusage_info_child(struct rusage_info_child *ri, rusage_info_current *ri_current)
 {
        ri->ri_child_user_time += (ri_current->ri_user_time +
-                                       ri_current->ri_child_user_time);
+           ri_current->ri_child_user_time);
        ri->ri_child_system_time += (ri_current->ri_system_time +
-                                       ri_current->ri_child_system_time);
+           ri_current->ri_child_system_time);
        ri->ri_child_pkg_idle_wkups += (ri_current->ri_pkg_idle_wkups +
-                                       ri_current->ri_child_pkg_idle_wkups);
+           ri_current->ri_child_pkg_idle_wkups);
        ri->ri_child_interrupt_wkups += (ri_current->ri_interrupt_wkups +
-                                       ri_current->ri_child_interrupt_wkups);
+           ri_current->ri_child_interrupt_wkups);
        ri->ri_child_pageins += (ri_current->ri_pageins +
-                                       ri_current->ri_child_pageins);
+           ri_current->ri_child_pageins);
        ri->ri_child_elapsed_abstime += ((ri_current->ri_proc_exit_abstime -
-               ri_current->ri_proc_start_abstime) + ri_current->ri_child_elapsed_abstime);
+           ri_current->ri_proc_start_abstime) + ri_current->ri_child_elapsed_abstime);
 }
 
 void
@@ -1334,7 +1400,7 @@ proc_limitdrop(proc_t p, int exiting)
 
        proc_list_lock();
 
-       if (--p->p_limit->pl_refcnt == 0) { 
+       if (--p->p_limit->pl_refcnt == 0) {
                freelim = p->p_limit;
                p->p_limit = NULL;
        }
@@ -1344,10 +1410,12 @@ proc_limitdrop(proc_t p, int exiting)
        }
 
        proc_list_unlock();
-       if (freelim != NULL)
+       if (freelim != NULL) {
                FREE_ZONE(freelim, sizeof *p->p_limit, M_PLIMIT);
-       if (freeoldlim != NULL)
+       }
+       if (freeoldlim != NULL) {
                FREE_ZONE(freeoldlim, sizeof *p->p_olimit, M_PLIMIT);
+       }
 }
 
 
@@ -1371,7 +1439,6 @@ proc_limitblock(proc_t p)
        }
        p->p_lflag |= P_LLIMCHANGE;
        proc_unlock(p);
-
 }
 
 
@@ -1398,15 +1465,15 @@ proc_limitreplace(proc_t p)
 
        if (p->p_limit->pl_refcnt == 1) {
                proc_list_unlock();
-               return(0);
+               return 0;
        }
-               
+
        proc_list_unlock();
 
        MALLOC_ZONE(copy, struct plimit *,
-                       sizeof(struct plimit), M_PLIMIT, M_WAITOK);
+           sizeof(struct plimit), M_PLIMIT, M_WAITOK);
        if (copy == NULL) {
-               return(ENOMEM);
+               return ENOMEM;
        }
 
        proc_list_lock();
@@ -1418,9 +1485,20 @@ proc_limitreplace(proc_t p)
        p->p_limit = copy;
        proc_list_unlock();
 
-       return(0);
+       return 0;
 }
 
+static int
+iopolicysys_disk(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
+static int
+iopolicysys_vfs_hfs_case_sensitivity(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
+static int
+iopolicysys_vfs_atime_updates(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
+static int
+iopolicysys_vfs_materialize_dataless_files(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
+static int
+iopolicysys_vfs_statfs_no_data_volume(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
+
 /*
  * iopolicysys
  *
@@ -1433,239 +1511,523 @@ proc_limitreplace(proc_t p)
  *             EINVAL                          Invalid command or invalid policy arguments
  *
  */
-
-static int
-iopolicysys_disk(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
-static int
-iopolicysys_vfs(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
-
 int
 iopolicysys(struct proc *p, struct iopolicysys_args *uap, int32_t *retval)
 {
        int     error = 0;
        struct _iopol_param_t iop_param;
 
-       if ((error = copyin(uap->arg, &iop_param, sizeof(iop_param))) != 0)
+       if ((error = copyin(uap->arg, &iop_param, sizeof(iop_param))) != 0) {
                goto out;
+       }
 
        switch (iop_param.iop_iotype) {
-               case IOPOL_TYPE_DISK:
-                       error = iopolicysys_disk(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
-                       if (error == EIDRM) {
-                               *retval = -2;
-                               error = 0;
-                       }
-                       if (error)
-                               goto out;
-                       break;
-               case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY:
-                       error = iopolicysys_vfs(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
-                       if (error)
-                               goto out;
-                       break;
-               default:
-                       error = EINVAL;
+       case IOPOL_TYPE_DISK:
+               error = iopolicysys_disk(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
+               if (error == EIDRM) {
+                       *retval = -2;
+                       error = 0;
+               }
+               if (error) {
+                       goto out;
+               }
+               break;
+       case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY:
+               error = iopolicysys_vfs_hfs_case_sensitivity(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
+               if (error) {
+                       goto out;
+               }
+               break;
+       case IOPOL_TYPE_VFS_ATIME_UPDATES:
+               error = iopolicysys_vfs_atime_updates(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
+               if (error) {
+                       goto out;
+               }
+               break;
+       case IOPOL_TYPE_VFS_MATERIALIZE_DATALESS_FILES:
+               error = iopolicysys_vfs_materialize_dataless_files(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
+               if (error) {
+                       goto out;
+               }
+               break;
+       case IOPOL_TYPE_VFS_STATFS_NO_DATA_VOLUME:
+               error = iopolicysys_vfs_statfs_no_data_volume(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
+               if (error) {
                        goto out;
+               }
+       default:
+               error = EINVAL;
+               goto out;
        }
 
        /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
        if (uap->cmd == IOPOL_CMD_GET) {
                error = copyout((caddr_t)&iop_param, uap->arg, sizeof(iop_param));
-               if (error)
+               if (error) {
                        goto out;
+               }
        }
 
 out:
-       return (error);
+       return error;
 }
 
 static int
 iopolicysys_disk(struct proc *p __unused, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
 {
-       int                     error = 0;
-       thread_t        thread;
-       int                     policy_flavor;
+       int                     error = 0;
+       thread_t        thread;
+       int                     policy_flavor;
 
        /* Validate scope */
        switch (scope) {
-               case IOPOL_SCOPE_PROCESS:
-                       thread = THREAD_NULL;
-                       policy_flavor = TASK_POLICY_IOPOL;
-                       break;
+       case IOPOL_SCOPE_PROCESS:
+               thread = THREAD_NULL;
+               policy_flavor = TASK_POLICY_IOPOL;
+               break;
 
-               case IOPOL_SCOPE_THREAD:
-                       thread = current_thread();
-                       policy_flavor = TASK_POLICY_IOPOL;
-
-                       /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
-                       if (cmd == IOPOL_CMD_SET && thread_has_qos_policy(thread)) {
-                               switch (policy) {
-                                       case IOPOL_DEFAULT:
-                                       case IOPOL_PASSIVE:
-                                               break;
-                                       case IOPOL_UTILITY:
-                                       case IOPOL_THROTTLE:
-                                       case IOPOL_IMPORTANT:
-                                       case IOPOL_STANDARD:
-                                               if (!thread_is_static_param(thread)) {
-                                                       thread_remove_qos_policy(thread);
-                                                       /*
-                                                        * This is not an error case, this is to return a marker to user-space that
-                                                        * we stripped the thread of its QoS class.
-                                                        */
-                                                       error = EIDRM;
-                                                       break;
-                                               }
-                                               /* otherwise, fall through to the error case. */
-                                       default:
-                                               error = EINVAL;
-                                               goto out;
+       case IOPOL_SCOPE_THREAD:
+               thread = current_thread();
+               policy_flavor = TASK_POLICY_IOPOL;
+
+               /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
+               if (cmd == IOPOL_CMD_SET && thread_has_qos_policy(thread)) {
+                       switch (policy) {
+                       case IOPOL_DEFAULT:
+                       case IOPOL_PASSIVE:
+                               break;
+                       case IOPOL_UTILITY:
+                       case IOPOL_THROTTLE:
+                       case IOPOL_IMPORTANT:
+                       case IOPOL_STANDARD:
+                               if (!thread_is_static_param(thread)) {
+                                       thread_remove_qos_policy(thread);
+                                       /*
+                                        * This is not an error case, this is to return a marker to user-space that
+                                        * we stripped the thread of its QoS class.
+                                        */
+                                       error = EIDRM;
+                                       break;
                                }
+                       /* otherwise, fall through to the error case. */
+                       default:
+                               error = EINVAL;
+                               goto out;
                        }
-                       break;
+               }
+               break;
 
-               case IOPOL_SCOPE_DARWIN_BG:
+       case IOPOL_SCOPE_DARWIN_BG:
 #if CONFIG_EMBEDDED
-                       /* Embedded doesn't want this as BG is always IOPOL_THROTTLE */
-                       error = ENOTSUP;
-                       goto out;
+               /* Embedded doesn't want this as BG is always IOPOL_THROTTLE */
+               error = ENOTSUP;
+               goto out;
 #else /* CONFIG_EMBEDDED */
-                       thread = THREAD_NULL;
-                       policy_flavor = TASK_POLICY_DARWIN_BG_IOPOL;
-                       break;
+               thread = THREAD_NULL;
+               policy_flavor = TASK_POLICY_DARWIN_BG_IOPOL;
+               break;
 #endif /* CONFIG_EMBEDDED */
 
+       default:
+               error = EINVAL;
+               goto out;
+       }
+
+       /* Validate policy */
+       if (cmd == IOPOL_CMD_SET) {
+               switch (policy) {
+               case IOPOL_DEFAULT:
+                       if (scope == IOPOL_SCOPE_DARWIN_BG) {
+                               /* the current default BG throttle level is UTILITY */
+                               policy = IOPOL_UTILITY;
+                       } else {
+                               policy = IOPOL_IMPORTANT;
+                       }
+                       break;
+               case IOPOL_UTILITY:
+               /* fall-through */
+               case IOPOL_THROTTLE:
+                       /* These levels are OK */
+                       break;
+               case IOPOL_IMPORTANT:
+               /* fall-through */
+               case IOPOL_STANDARD:
+               /* fall-through */
+               case IOPOL_PASSIVE:
+                       if (scope == IOPOL_SCOPE_DARWIN_BG) {
+                               /* These levels are invalid for BG */
+                               error = EINVAL;
+                               goto out;
+                       } else {
+                               /* OK for other scopes */
+                       }
+                       break;
                default:
                        error = EINVAL;
                        goto out;
+               }
+       }
+
+       /* Perform command */
+       switch (cmd) {
+       case IOPOL_CMD_SET:
+               if (thread != THREAD_NULL) {
+                       proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor, policy);
+               } else {
+                       proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor, policy);
+               }
+               break;
+       case IOPOL_CMD_GET:
+               if (thread != THREAD_NULL) {
+                       policy = proc_get_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor);
+               } else {
+                       policy = proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor);
+               }
+               iop_param->iop_policy = policy;
+               break;
+       default:
+               error = EINVAL;         /* unknown command */
+               break;
+       }
+
+out:
+       return error;
+}
+
+static int
+iopolicysys_vfs_hfs_case_sensitivity(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
+{
+       int                     error = 0;
+
+       /* Validate scope */
+       switch (scope) {
+       case IOPOL_SCOPE_PROCESS:
+               /* Only process OK */
+               break;
+       default:
+               error = EINVAL;
+               goto out;
        }
 
        /* Validate policy */
        if (cmd == IOPOL_CMD_SET) {
                switch (policy) {
-                       case IOPOL_DEFAULT:
-                               if (scope == IOPOL_SCOPE_DARWIN_BG) {
-                                       /* the current default BG throttle level is UTILITY */
-                                       policy = IOPOL_UTILITY;
-                               } else {
-                                       policy = IOPOL_IMPORTANT;
-                               }
-                               break;
-                       case IOPOL_UTILITY:
-                               /* fall-through */
-                       case IOPOL_THROTTLE:
-                               /* These levels are OK */
-                               break;
-                       case IOPOL_IMPORTANT:
-                               /* fall-through */
-                       case IOPOL_STANDARD:
-                               /* fall-through */
-                       case IOPOL_PASSIVE:
-                               if (scope == IOPOL_SCOPE_DARWIN_BG) {
-                                       /* These levels are invalid for BG */
-                                       error = EINVAL;
-                                       goto out;
-                               } else {
-                                       /* OK for other scopes */
-                               }
-                               break;
-                       default:
-                               error = EINVAL;
-                               goto out;
+               case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
+               /* fall-through */
+               case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
+                       /* These policies are OK */
+                       break;
+               default:
+                       error = EINVAL;
+                       goto out;
                }
        }
 
        /* Perform command */
-       switch(cmd) {
-               case IOPOL_CMD_SET:
-                       if (thread != THREAD_NULL)
-                               proc_set_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor, policy);
-                       else
-                               proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor, policy);
+       switch (cmd) {
+       case IOPOL_CMD_SET:
+               if (0 == kauth_cred_issuser(kauth_cred_get())) {
+                       /* If it's a non-root process, it needs to have the entitlement to set the policy */
+                       boolean_t entitled = FALSE;
+                       entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
+                       if (!entitled) {
+                               error = EPERM;
+                               goto out;
+                       }
+               }
+
+               switch (policy) {
+               case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
+                       OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY), &p->p_vfs_iopolicy);
                        break;
-               case IOPOL_CMD_GET:
-                       if (thread != THREAD_NULL)
-                               policy = proc_get_thread_policy(thread, TASK_POLICY_INTERNAL, policy_flavor);
-                       else
-                               policy = proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL, policy_flavor);
-                       iop_param->iop_policy = policy;
+               case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
+                       OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY, &p->p_vfs_iopolicy);
                        break;
                default:
-                       error = EINVAL; /* unknown command */
+                       error = EINVAL;
+                       goto out;
+               }
+
+               break;
+       case IOPOL_CMD_GET:
+               iop_param->iop_policy = (p->p_vfs_iopolicy & P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY)
+                   ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
+                   : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT;
+               break;
+       default:
+               error = EINVAL;         /* unknown command */
+               break;
+       }
+
+out:
+       return error;
+}
+
+static inline int
+get_thread_atime_policy(struct uthread *ut)
+{
+       return (ut->uu_flag & UT_ATIME_UPDATE) ? IOPOL_ATIME_UPDATES_OFF : IOPOL_ATIME_UPDATES_DEFAULT;
+}
+
+static inline void
+set_thread_atime_policy(struct uthread *ut, int policy)
+{
+       if (policy == IOPOL_ATIME_UPDATES_OFF) {
+               ut->uu_flag |= UT_ATIME_UPDATE;
+       } else {
+               ut->uu_flag &= ~UT_ATIME_UPDATE;
+       }
+}
+
+static inline void
+set_task_atime_policy(struct proc *p, int policy)
+{
+       if (policy == IOPOL_ATIME_UPDATES_OFF) {
+               OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES, &p->p_vfs_iopolicy);
+       } else {
+               OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES), &p->p_vfs_iopolicy);
+       }
+}
+
+static inline int
+get_task_atime_policy(struct proc *p)
+{
+       return (p->p_vfs_iopolicy & P_VFS_IOPOLICY_ATIME_UPDATES) ? IOPOL_ATIME_UPDATES_OFF : IOPOL_ATIME_UPDATES_DEFAULT;
+}
+
+static int
+iopolicysys_vfs_atime_updates(struct proc *p __unused, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
+{
+       int                     error = 0;
+       thread_t                thread;
+
+       /* Validate scope */
+       switch (scope) {
+       case IOPOL_SCOPE_THREAD:
+               thread = current_thread();
+               break;
+       case IOPOL_SCOPE_PROCESS:
+               thread = THREAD_NULL;
+               break;
+       default:
+               error = EINVAL;
+               goto out;
+       }
+
+       /* Validate policy */
+       if (cmd == IOPOL_CMD_SET) {
+               switch (policy) {
+               case IOPOL_ATIME_UPDATES_DEFAULT:
+               case IOPOL_ATIME_UPDATES_OFF:
                        break;
+               default:
+                       error = EINVAL;
+                       goto out;
+               }
+       }
+
+       /* Perform command */
+       switch (cmd) {
+       case IOPOL_CMD_SET:
+               if (thread != THREAD_NULL) {
+                       set_thread_atime_policy(get_bsdthread_info(thread), policy);
+               } else {
+                       set_task_atime_policy(p, policy);
+               }
+               break;
+       case IOPOL_CMD_GET:
+               if (thread != THREAD_NULL) {
+                       policy = get_thread_atime_policy(get_bsdthread_info(thread));
+               } else {
+                       policy = get_task_atime_policy(p);
+               }
+               iop_param->iop_policy = policy;
+               break;
+       default:
+               error = EINVAL;         /* unknown command */
+               break;
        }
 
 out:
-       return (error);
+       return error;
+}
+
+static inline int
+get_thread_materialize_policy(struct uthread *ut)
+{
+       if (ut->uu_flag & UT_NSPACE_NODATALESSFAULTS) {
+               return IOPOL_MATERIALIZE_DATALESS_FILES_OFF;
+       } else if (ut->uu_flag & UT_NSPACE_FORCEDATALESSFAULTS) {
+               return IOPOL_MATERIALIZE_DATALESS_FILES_ON;
+       }
+       /* Default thread behavior is "inherit process behavior". */
+       return IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT;
+}
+
+static inline void
+set_thread_materialize_policy(struct uthread *ut, int policy)
+{
+       if (policy == IOPOL_MATERIALIZE_DATALESS_FILES_OFF) {
+               ut->uu_flag &= ~UT_NSPACE_FORCEDATALESSFAULTS;
+               ut->uu_flag |= UT_NSPACE_NODATALESSFAULTS;
+       } else if (policy == IOPOL_MATERIALIZE_DATALESS_FILES_ON) {
+               ut->uu_flag &= ~UT_NSPACE_NODATALESSFAULTS;
+               ut->uu_flag |= UT_NSPACE_FORCEDATALESSFAULTS;
+       } else {
+               ut->uu_flag &= ~(UT_NSPACE_NODATALESSFAULTS | UT_NSPACE_FORCEDATALESSFAULTS);
+       }
+}
+
+static inline void
+set_proc_materialize_policy(struct proc *p, int policy)
+{
+       if (policy == IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT) {
+               /*
+                * Caller has specified "use the default policy".
+                * The default policy is to NOT materialize dataless
+                * files.
+                */
+               policy = IOPOL_MATERIALIZE_DATALESS_FILES_OFF;
+       }
+       if (policy == IOPOL_MATERIALIZE_DATALESS_FILES_ON) {
+               OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES, &p->p_vfs_iopolicy);
+       } else {
+               OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES), &p->p_vfs_iopolicy);
+       }
 }
 
 static int
-iopolicysys_vfs(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
+get_proc_materialize_policy(struct proc *p)
 {
-       int                     error = 0;
+       return (p->p_vfs_iopolicy & P_VFS_IOPOLICY_MATERIALIZE_DATALESS_FILES) ? IOPOL_MATERIALIZE_DATALESS_FILES_ON : IOPOL_MATERIALIZE_DATALESS_FILES_OFF;
+}
+
+static int
+iopolicysys_vfs_materialize_dataless_files(struct proc *p __unused, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
+{
+       int                     error = 0;
+       thread_t                thread;
 
        /* Validate scope */
        switch (scope) {
-               case IOPOL_SCOPE_PROCESS:
-                       /* Only process OK */
+       case IOPOL_SCOPE_THREAD:
+               thread = current_thread();
+               break;
+       case IOPOL_SCOPE_PROCESS:
+               thread = THREAD_NULL;
+               break;
+       default:
+               error = EINVAL;
+               goto out;
+       }
+
+       /* Validate policy */
+       if (cmd == IOPOL_CMD_SET) {
+               switch (policy) {
+               case IOPOL_MATERIALIZE_DATALESS_FILES_DEFAULT:
+               case IOPOL_MATERIALIZE_DATALESS_FILES_OFF:
+               case IOPOL_MATERIALIZE_DATALESS_FILES_ON:
                        break;
                default:
                        error = EINVAL;
                        goto out;
+               }
+       }
+
+       /* Perform command */
+       switch (cmd) {
+       case IOPOL_CMD_SET:
+               if (thread != THREAD_NULL) {
+                       set_thread_materialize_policy(get_bsdthread_info(thread), policy);
+               } else {
+                       set_proc_materialize_policy(p, policy);
+               }
+               break;
+       case IOPOL_CMD_GET:
+               if (thread != THREAD_NULL) {
+                       policy = get_thread_materialize_policy(get_bsdthread_info(thread));
+               } else {
+                       policy = get_proc_materialize_policy(p);
+               }
+               iop_param->iop_policy = policy;
+               break;
+       default:
+               error = EINVAL;         /* unknown command */
+               break;
+       }
+
+out:
+       return error;
+}
+
+static int
+iopolicysys_vfs_statfs_no_data_volume(struct proc *p __unused, int cmd,
+    int scope, int policy, struct _iopol_param_t *iop_param)
+{
+       int error = 0;
+
+       /* Validate scope */
+       switch (scope) {
+       case IOPOL_SCOPE_PROCESS:
+               /* Only process OK */
+               break;
+       default:
+               error = EINVAL;
+               goto out;
        }
 
        /* Validate policy */
        if (cmd == IOPOL_CMD_SET) {
                switch (policy) {
-                       case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
-                               /* fall-through */
-                       case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
-                               /* These policies are OK */
-                               break;
-                       default:
-                               error = EINVAL;
-                               goto out;
+               case IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT:
+               /* fall-through */
+               case IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME:
+                       /* These policies are OK */
+                       break;
+               default:
+                       error = EINVAL;
+                       goto out;
                }
        }
 
        /* Perform command */
-       switch(cmd) {
-               case IOPOL_CMD_SET:
-                       if (0 == kauth_cred_issuser(kauth_cred_get())) {
-                               /* If it's a non-root process, it needs to have the entitlement to set the policy */
-                               boolean_t entitled = FALSE;
-                               entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
-                               if (!entitled) {
-                                       error = EPERM;
-                                       goto out;
-                               }
+       switch (cmd) {
+       case IOPOL_CMD_SET:
+               if (0 == kauth_cred_issuser(kauth_cred_get())) {
+                       /* If it's a non-root process, it needs to have the entitlement to set the policy */
+                       boolean_t entitled = FALSE;
+                       entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
+                       if (!entitled) {
+                               error = EPERM;
+                               goto out;
                        }
+               }
 
-                       switch (policy) {
-                               case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
-                                       OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY), &p->p_vfs_iopolicy);
-                                       break;
-                               case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
-                                       OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY, &p->p_vfs_iopolicy);
-                                       break;
-                               default:
-                                       error = EINVAL;
-                                       goto out;
-                       }
-                       
+               switch (policy) {
+               case IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT:
+                       OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME), &p->p_vfs_iopolicy);
                        break;
-               case IOPOL_CMD_GET:
-                       iop_param->iop_policy = (p->p_vfs_iopolicy & P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY)
-                               ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
-                               : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT;
+               case IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME:
+                       OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME, &p->p_vfs_iopolicy);
                        break;
                default:
-                       error = EINVAL; /* unknown command */
-                       break;
+                       error = EINVAL;
+                       goto out;
+               }
+
+               break;
+       case IOPOL_CMD_GET:
+               iop_param->iop_policy = (p->p_vfs_iopolicy & P_VFS_IOPOLICY_STATFS_NO_DATA_VOLUME)
+                   ? IOPOL_VFS_STATFS_FORCE_NO_DATA_VOLUME
+                   : IOPOL_VFS_STATFS_NO_DATA_VOLUME_DEFAULT;
+               break;
+       default:
+               error = EINVAL;         /* unknown command */
+               break;
        }
 
 out:
-       return (error);
+       return error;
 }
 
 /* BSD call back function for task_policy networking changes */
@@ -1693,28 +2055,31 @@ gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor)
 
        assert(p->p_stats != NULL);
        memset(ru, 0, sizeof(*ru));
-       switch(flavor) {
+       switch (flavor) {
        case RUSAGE_INFO_V4:
-               ru->ri_logical_writes = get_task_logical_writes(p->task);
+               ru->ri_logical_writes = get_task_logical_writes(p->task, FALSE);
                ru->ri_lifetime_max_phys_footprint = get_task_phys_footprint_lifetime_max(p->task);
+#if CONFIG_LEDGER_INTERVAL_MAX
+               ru->ri_interval_max_phys_footprint = get_task_phys_footprint_interval_max(p->task, FALSE);
+#endif
                fill_task_monotonic_rusage(p->task, ru);
-        /* fall through */
+       /* fall through */
 
        case RUSAGE_INFO_V3:
                fill_task_qos_rusage(p->task, ru);
                fill_task_billed_usage(p->task, ru);
-               /* fall through */
+       /* fall through */
 
        case RUSAGE_INFO_V2:
                fill_task_io_rusage(p->task, ru);
-               /* fall through */
+       /* fall through */
 
        case RUSAGE_INFO_V1:
                /*
                 * p->p_stats->ri_child statistics are protected under proc lock.
                 */
                proc_lock(p);
-               
+
                ri_child = &(p->p_stats->ri_child);
                ru->ri_child_user_time = ri_child->ri_child_user_time;
                ru->ri_child_system_time = ri_child->ri_child_system_time;
@@ -1724,10 +2089,10 @@ gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor)
                ru->ri_child_elapsed_abstime = ri_child->ri_child_elapsed_abstime;
 
                proc_unlock(p);
-               /* fall through */
+       /* fall through */
 
        case RUSAGE_INFO_V0:
-               proc_getexecutableuuid(p, (unsigned char *)&ru->ri_uuid, sizeof (ru->ri_uuid));
+               proc_getexecutableuuid(p, (unsigned char *)&ru->ri_uuid, sizeof(ru->ri_uuid));
                fill_task_rusage(p->task, ru);
                ru->ri_proc_start_abstime = p->p_stats->ps_start;
        }
@@ -1736,7 +2101,7 @@ gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor)
 int
 proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie)
 {
-       rusage_info_current ri_current;
+       rusage_info_current ri_current = {};
 
        int error = 0;
        size_t size = 0;
@@ -1766,11 +2131,11 @@ proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie
                return EINVAL;
        }
 
-       if(size == 0) {
+       if (size == 0) {
                return EINVAL;
        }
 
-        /*
+       /*
         * If task is still alive, collect info from the live task itself.
         * Otherwise, look to the cached info in the zombie proc.
         */
@@ -1783,7 +2148,7 @@ proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie
                error = copyout(&p->p_ru->ri, buffer, size);
        }
 
-       return (error);
+       return error;
 }
 
 static int
@@ -1811,16 +2176,23 @@ mach_to_bsd_rv(int mach_rv)
  * uap->flavor available flavors:
  *
  *     RLIMIT_WAKEUPS_MONITOR
+ *     RLIMIT_CPU_USAGE_MONITOR
+ *     RLIMIT_THREAD_CPULIMITS
+ *     RLIMIT_FOOTPRINT_INTERVAL
  */
 int
 proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *uap, __unused int32_t *retval)
 {
-       proc_t  targetp;
-       int     error = 0;
-       struct  proc_rlimit_control_wakeupmon wakeupmon_args;
+       proc_t  targetp;
+       int     error = 0;
+       struct  proc_rlimit_control_wakeupmon wakeupmon_args;
        uint32_t cpumon_flags;
        uint32_t cpulimits_flags;
        kauth_cred_t my_cred, target_cred;
+#if CONFIG_LEDGER_INTERVAL_MAX
+       uint32_t footprint_interval_flags;
+       uint64_t interval_max_footprint;
+#endif /* CONFIG_LEDGER_INTERVAL_MAX */
 
        /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
        if (uap->pid == -1) {
@@ -1831,7 +2203,7 @@ proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *ua
 
        /* proc_self() can return NULL for an exiting process */
        if (targetp == PROC_NULL) {
-               return (ESRCH);
+               return ESRCH;
        }
 
        my_cred = kauth_cred_get();
@@ -1842,19 +2214,19 @@ proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *ua
            kauth_cred_getruid(my_cred) != kauth_cred_getuid(target_cred)) {
                proc_rele(targetp);
                kauth_cred_unref(&target_cred);
-               return (EACCES);
+               return EACCES;
        }
 
        switch (uap->flavor) {
        case RLIMIT_WAKEUPS_MONITOR:
-               if ((error = copyin(uap->arg, &wakeupmon_args, sizeof (wakeupmon_args))) != 0) {
+               if ((error = copyin(uap->arg, &wakeupmon_args, sizeof(wakeupmon_args))) != 0) {
                        break;
                }
                if ((error = mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp->task, &wakeupmon_args.wm_flags,
-                    &wakeupmon_args.wm_rate))) != 0) {
+                   &wakeupmon_args.wm_rate))) != 0) {
                        break;
                }
-               error = copyout(&wakeupmon_args, uap->arg, sizeof (wakeupmon_args));
+               error = copyout(&wakeupmon_args, uap->arg, sizeof(wakeupmon_args));
                break;
        case RLIMIT_CPU_USAGE_MONITOR:
                cpumon_flags = uap->arg; // XXX temporarily stashing flags in argp (12592127)
@@ -1872,8 +2244,8 @@ proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *ua
                uint32_t ms_refill = 0;
                uint64_t ns_refill;
 
-               percent = (uint8_t)(cpulimits_flags & 0xffU);                                   /* low 8 bits for percent */
-               ms_refill = (cpulimits_flags >> 8) & 0xffffff;          /* next 24 bits represent ms refill value */
+               percent = (uint8_t)(cpulimits_flags & 0xffU);                                   /* low 8 bits for percent */
+               ms_refill = (cpulimits_flags >> 8) & 0xffffff;          /* next 24 bits represent ms refill value */
                if (percent >= 100) {
                        error = EINVAL;
                        break;
@@ -1883,6 +2255,20 @@ proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *ua
 
                error = mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK, percent, ns_refill));
                break;
+
+#if CONFIG_LEDGER_INTERVAL_MAX
+       case RLIMIT_FOOTPRINT_INTERVAL:
+               footprint_interval_flags = uap->arg; // XXX temporarily stashing flags in argp (12592127)
+               /*
+                * There is currently only one option for this flavor.
+                */
+               if ((footprint_interval_flags & FOOTPRINT_INTERVAL_RESET) == 0) {
+                       error = EINVAL;
+                       break;
+               }
+               interval_max_footprint = get_task_phys_footprint_interval_max(targetp->task, TRUE);
+               break;
+#endif /* CONFIG_LEDGER_INTERVAL_MAX */
        default:
                error = EINVAL;
                break;
@@ -1894,24 +2280,26 @@ proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *ua
        /*
         * Return value from this function becomes errno to userland caller.
         */
-       return (error);
+       return error;
 }
 
 /*
  * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
  */
-int thread_selfusage(struct proc *p __unused, struct thread_selfusage_args *uap __unused, uint64_t *retval)
+int
+thread_selfusage(struct proc *p __unused, struct thread_selfusage_args *uap __unused, uint64_t *retval)
 {
        uint64_t runtime;
 
        runtime = thread_get_runtime_self();
        *retval = runtime;
 
-       return (0);
+       return 0;
 }
 
 #if !MONOTONIC
-int thread_selfcounts(__unused struct proc *p, __unused struct thread_selfcounts_args *uap, __unused int *ret_out)
+int
+thread_selfcounts(__unused struct proc *p, __unused struct thread_selfcounts_args *uap, __unused int *ret_out)
 {
        return ENOTSUP;
 }