+vfs_context_proc(vfs_context_t ctx)
+{
+ proc_t proc = NULL;
+
+ if (ctx != NULL && ctx->vc_thread != NULL) {
+ proc = (proc_t)get_bsdthreadtask_info(ctx->vc_thread);
+ }
+ if (proc != NULL && (proc->p_fd == NULL || (proc->p_lflag & P_LVFORK))) {
+ proc = NULL;
+ }
+
+ return proc == NULL ? current_proc() : proc;
+}
+
+/*
+ * vfs_context_get_special_port
+ *
+ * Description: Return the requested special port from the task associated
+ * with the given context.
+ *
+ * Parameters: vfs_context_t The context to use
+ * int Index of special port
+ * ipc_port_t * Pointer to returned port
+ *
+ * Returns: kern_return_t see task_get_special_port()
+ */
+kern_return_t
+vfs_context_get_special_port(vfs_context_t ctx, int which, ipc_port_t *portp)
+{
+ task_t task = NULL;
+
+ if (ctx != NULL && ctx->vc_thread != NULL) {
+ task = get_threadtask(ctx->vc_thread);
+ }
+
+ return task_get_special_port(task, which, portp);
+}
+
+/*
+ * vfs_context_set_special_port
+ *
+ * Description: Set the requested special port in the task associated
+ * with the given context.
+ *
+ * Parameters: vfs_context_t The context to use
+ * int Index of special port
+ * ipc_port_t New special port
+ *
+ * Returns: kern_return_t see task_set_special_port_internal()
+ */
+kern_return_t
+vfs_context_set_special_port(vfs_context_t ctx, int which, ipc_port_t port)
+{
+ task_t task = NULL;
+
+ if (ctx != NULL && ctx->vc_thread != NULL) {
+ task = get_threadtask(ctx->vc_thread);
+ }
+
+ return task_set_special_port_internal(task, which, port);
+}
+
+/*
+ * vfs_context_thread
+ *
+ * Description: Return the Mach thread associated with a vfs_context_t
+ *
+ * Parameters: vfs_context_t The context to use
+ *
+ * Returns: thread_t The thread for this context, or
+ * NULL, if there is not one.
+ *
+ * Notes: NULL thread_t's are legal, but discouraged. They occur only
+ * as a result of a static vfs_context_t declaration in a function
+ * and will result in this function returning NULL.
+ *
+ * This is intentional; this function should NOT return the
+ * current_thread() in this case.
+ */
+thread_t
+vfs_context_thread(vfs_context_t ctx)
+{
+ return ctx->vc_thread;
+}
+
+
+/*
+ * vfs_context_cwd
+ *
+ * Description: Returns a reference on the vnode for the current working
+ * directory for the supplied context
+ *
+ * Parameters: vfs_context_t The context to use
+ *
+ * Returns: vnode_t The current working directory
+ * for this context
+ *
+ * Notes: The function first attempts to obtain the current directory
+ * from the thread, and if it is not present there, falls back
+ * to obtaining it from the process instead. If it can't be
+ * obtained from either place, we return NULLVP.
+ */
+vnode_t
+vfs_context_cwd(vfs_context_t ctx)