/*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
*
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License"). You may not use this file except in compliance with the
- * License. Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * 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.
*
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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 OR NON-INFRINGEMENT. Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * 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_OSREFERENCE_LICENSE_HEADER_END@
*/
/* Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
*
* File: bsd/kern/kern_symfile.c
*
- * This file contains creates a dummy symbol file for mach_kernel based on
- * the symbol table information passed by the SecondaryLoader/PlatformExpert.
- * This allows us to correctly link other executables (drivers, etc) against the
- * the kernel in cases where the kernel image on the root device does not match
- * the live kernel. This can occur during net-booting where the actual kernel
- * image is obtained from the network via tftp rather than the root
- * device.
- *
- * If a symbol table is available, then the file /mach.sym will be created
- * containing a Mach Header and a LC_SYMTAB load command followed by the
- * the symbol table data for mach_kernel.
- *
* HISTORY
- *
- * .
*/
#include <mach/vm_param.h>
#include <sys/signalvar.h>
#include <sys/resourcevar.h>
#include <sys/namei.h>
-#include <sys/vnode.h>
-#include <sys/proc.h>
+#include <sys/vnode_internal.h>
+#include <sys/proc_internal.h>
+#include <sys/kauth.h>
#include <sys/timeb.h>
#include <sys/times.h>
-#include <sys/buf.h>
#include <sys/acct.h>
-#include <sys/file.h>
+#include <sys/file_internal.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/stat.h>
+#include <sys/disk.h>
+#include <sys/conf.h>
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
+#include <kern/kalloc.h>
#include <vm/vm_kern.h>
+#include <pexpert/pexpert.h>
+#include <IOKit/IOHibernatePrivate.h>
-extern unsigned char rootdevice[];
-extern vm_size_t page_size;
+/* This function is called from kern_sysctl in the current process context;
+ * it is exported with the System6.0.exports, but this appears to be a legacy
+ * export, as there are no internal consumers.
+ */
+int
+get_kernel_symfile(__unused proc_t p, __unused char const **symfile);
+int
+get_kernel_symfile(__unused proc_t p, __unused char const **symfile)
+{
+ return KERN_FAILURE;
+}
+struct kern_direct_file_io_ref_t
+{
+ vfs_context_t ctx;
+ struct vnode * vp;
+ dev_t device;
+ uint32_t blksize;
+ off_t filelength;
+ char pinned;
+};
-int kernel_symfile_opened = 0;
-int error_code = 0;
-extern int IODTGetLoaderInfo( char *key, void **infoAddr, int *infoSize );
-extern void IODTFreeLoaderInfo( char *key, void *infoAddr, int infoSize );
+static int file_ioctl(void * p1, void * p2, u_long theIoctl, caddr_t result)
+{
+ dev_t device = *(dev_t*) p1;
-struct segment_command *findSegmentByName( struct mach_header *mh, const char *section_name );
+ return ((*bdevsw[major(device)].d_ioctl)
+ (device, theIoctl, result, S_IFBLK, p2));
+}
-/*
- *
- */
-int get_kernel_symfile( struct proc *p, char **symfile )
+static int device_ioctl(void * p1, __unused void * p2, u_long theIoctl, caddr_t result)
+{
+ return (VNOP_IOCTL(p1, theIoctl, result, 0, p2));
+}
+
+static int
+kern_ioctl_file_extents(struct kern_direct_file_io_ref_t * ref, u_long theIoctl, off_t offset, off_t end)
{
- if ( kernel_symfile_opened == 0 )
+ int error;
+ int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
+ void * p1;
+ void * p2;
+ uint64_t fileblk;
+ size_t filechunk;
+ dk_extent_t extent;
+ dk_unmap_t unmap;
+ _dk_cs_pin_t pin;
+
+ bzero(&extent, sizeof(dk_extent_t));
+ bzero(&unmap, sizeof(dk_unmap_t));
+ bzero(&pin, sizeof(pin));
+ if (ref->vp->v_type == VREG)
+ {
+ p1 = &ref->device;
+ p2 = kernproc;
+ do_ioctl = &file_ioctl;
+ }
+ else
{
- kernel_symfile_opened = 1;
- error_code = output_kernel_symbols( p );
+ /* Partition. */
+ p1 = ref->vp;
+ p2 = ref->ctx;
+ do_ioctl = &device_ioctl;
}
- if ( error_code == 0 ) *symfile = "\\mach.sym";
+ while (offset < end)
+ {
+ if (ref->vp->v_type == VREG)
+ {
+ daddr64_t blkno;
+ filechunk = 1*1024*1024*1024;
+ if (filechunk > (size_t)(end - offset))
+ filechunk = (size_t)(end - offset);
+ error = VNOP_BLOCKMAP(ref->vp, offset, filechunk, &blkno, &filechunk, NULL, 0, NULL);
+ if (error) break;
+ fileblk = blkno * ref->blksize;
+ }
+ else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
+ {
+ fileblk = offset;
+ filechunk = ref->filelength;
+ }
- return error_code;
+ if (DKIOCUNMAP == theIoctl)
+ {
+ extent.offset = fileblk;
+ extent.length = filechunk;
+ unmap.extents = &extent;
+ unmap.extentsCount = 1;
+ error = do_ioctl(p1, p2, theIoctl, (caddr_t)&unmap);
+// printf("DKIOCUNMAP(%d) 0x%qx, 0x%qx\n", error, extent.offset, extent.length);
+ }
+ else if (_DKIOCCSPINEXTENT == theIoctl)
+ {
+ pin.cp_extent.offset = fileblk;
+ pin.cp_extent.length = filechunk;
+ pin.cp_flags = _DKIOCSPINDISCARDDATA;
+ error = do_ioctl(p1, p2, theIoctl, (caddr_t)&pin);
+ if (error && (ENOTTY != error))
+ {
+ printf("_DKIOCCSPINEXTENT(%d) 0x%qx, 0x%qx\n",
+ error, pin.cp_extent.offset, pin.cp_extent.length);
+ }
+ }
+ else error = EINVAL;
+
+ if (error) break;
+ offset += filechunk;
+ }
+ return (error);
}
-/*
- *
- */
-int output_kernel_symbols( register struct proc *p )
+int
+kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len);
+
+struct kern_direct_file_io_ref_t *
+kern_open_file_for_direct_io(const char * name,
+ kern_get_file_extents_callback_t callback,
+ void * callback_ref,
+ off_t set_file_size,
+ off_t write_file_offset,
+ caddr_t write_file_addr,
+ vm_size_t write_file_len,
+ dev_t * partition_device_result,
+ dev_t * image_device_result,
+ uint64_t * partitionbase_result,
+ uint64_t * maxiocount_result,
+ uint32_t * oflags)
{
- register struct vnode *vp;
- register struct pcred *pcred = p->p_cred;
- register struct ucred *cred = pcred->pc_ucred;
- struct nameidata nd;
- struct vattr vattr;
- struct mach_header *orig_mh, *mh;
- struct load_command *lc;
- struct segment_command *orig_ds, *orig_ts, *sg;
- struct section *se;
- struct symtab_command *sc, *sc0;
- struct nlist *nl;
- vm_size_t orig_mhsize, sc0_size;
- vm_offset_t header;
- vm_size_t header_size;
- int error, error1;
- int i, j;
- int symfoffset, symsize;
- int rc_mh, rc_sc;
-
- error = EFAULT;
-
- vp = NULL;
- header = NULL;
- orig_mh = NULL;
- sc0 = NULL;
-
- rc_mh = IODTGetLoaderInfo( "Kernel-__HEADER", (void **)&orig_mh, &orig_mhsize );
- rc_sc = IODTGetLoaderInfo( "Kernel-__SYMTAB", (void **)&sc0, &sc0_size );
-
- if ( rc_mh != 0 || orig_mh == 0 || orig_mhsize < sizeof(struct mach_header) ) goto out;
- if ( rc_sc != 0 || sc0 == 0 || sc0_size < sizeof(struct symtab_command) ) goto out;
+ struct kern_direct_file_io_ref_t * ref;
+
+ proc_t p;
+ struct vnode_attr va;
+ int error;
+ off_t f_offset;
+ uint64_t fileblk;
+ size_t filechunk;
+ uint64_t physoffset;
+ dev_t device;
+ dev_t target = 0;
+ int isssd = 0;
+ uint32_t flags = 0;
+ uint32_t blksize;
+ off_t maxiocount, count, segcount;
+ boolean_t locked = FALSE;
+
+ int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
+ void * p1 = NULL;
+ void * p2 = NULL;
+
+ error = EFAULT;
+
+ ref = (struct kern_direct_file_io_ref_t *) kalloc(sizeof(struct kern_direct_file_io_ref_t));
+ if (!ref)
+ {
+ error = EFAULT;
+ goto out;
+ }
- if ( pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid ) goto out;
+ bzero(ref, sizeof(*ref));
+ p = kernproc;
+ ref->ctx = vfs_context_create(vfs_context_current());
- if ( rootdevice[0] == 'e' && rootdevice[1] == 'n' ) goto out;
+ if ((error = vnode_open(name, (O_CREAT | FWRITE), (0), 0, &ref->vp, ref->ctx)))
+ goto out;
- NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "mach.sym", p);
- if( (error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IRGRP | S_IROTH )) != 0 ) goto out;
+ if (ref->vp->v_type == VREG)
+ {
+ vnode_lock_spin(ref->vp);
+ SET(ref->vp->v_flag, VSWAP);
+ vnode_unlock(ref->vp);
+ }
- vp = nd.ni_vp;
-
- /* Don't dump to non-regular files or files with links. */
- error = EFAULT;
- if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) goto out;
+ if (write_file_addr && write_file_len)
+ {
+ if ((error = kern_write_file(ref, write_file_offset, write_file_addr, write_file_len)))
+ goto out;
+ }
- VATTR_NULL(&vattr);
- vattr.va_size = 0;
- VOP_LEASE(vp, p, cred, LEASE_WRITE);
- VOP_SETATTR(vp, &vattr, cred, p);
- p->p_acflag |= ACORE;
+ VATTR_INIT(&va);
+ VATTR_WANTED(&va, va_rdev);
+ VATTR_WANTED(&va, va_fsid);
+ VATTR_WANTED(&va, va_data_size);
+ VATTR_WANTED(&va, va_data_alloc);
+ VATTR_WANTED(&va, va_nlink);
+ error = EFAULT;
+ if (vnode_getattr(ref->vp, &va, ref->ctx))
+ goto out;
+
+ kprintf("vp va_rdev major %d minor %d\n", major(va.va_rdev), minor(va.va_rdev));
+ kprintf("vp va_fsid major %d minor %d\n", major(va.va_fsid), minor(va.va_fsid));
+ kprintf("vp size %qd alloc %qd\n", va.va_data_size, va.va_data_alloc);
+
+ if (ref->vp->v_type == VREG)
+ {
+ /* Don't dump files with links. */
+ if (va.va_nlink != 1)
+ goto out;
- orig_ts = findSegmentByName(orig_mh, "__TEXT");
- orig_ds = findSegmentByName(orig_mh, "__DATA");
+ device = va.va_fsid;
+ ref->filelength = va.va_data_size;
- if ( orig_ts == NULL || orig_ds == NULL ) goto out;
+ p1 = &device;
+ p2 = p;
+ do_ioctl = &file_ioctl;
- header_size = sizeof(struct mach_header)
- + orig_ts->cmdsize
- + orig_ds->cmdsize
- + sizeof(struct symtab_command);
+ if (set_file_size && (set_file_size != (off_t) va.va_data_alloc))
+ {
+ u_int32_t alloc_flags = PREALLOCATE | ALLOCATEFROMPEOF | ALLOCATEALL;
- (void) kmem_alloc_wired( kernel_map,
- (vm_offset_t *)&header,
- (vm_size_t)header_size);
+ vnode_lock_spin(ref->vp);
+ CLR(ref->vp->v_flag, VSWAP);
+ vnode_unlock(ref->vp);
- if ( header == NULL ) goto out;
+ if (set_file_size < (off_t) va.va_data_alloc)
+ {
+ struct vnode_attr setva;
+ VATTR_INIT(&setva);
+ VATTR_SET(&setva, va_data_size, set_file_size);
+ error = vnode_setattr(ref->vp, &setva, ref->ctx);
+ }
+ else
+ {
+ off_t bytesallocated = set_file_size - va.va_data_alloc;
+ error = VNOP_ALLOCATE(ref->vp, bytesallocated, alloc_flags,
+ &bytesallocated, 0 /*fst_offset*/,
+ ref->ctx);
+ HIBLOG("VNOP_ALLOCATE(%d) %qd\n", error, bytesallocated);
+ }
+ // F_SETSIZE:
+ (void) vnode_setsize(ref->vp, set_file_size, IO_NOZEROFILL, ref->ctx);
+ ref->filelength = set_file_size;
+
+ vnode_lock_spin(ref->vp);
+ SET(ref->vp->v_flag, VSWAP);
+ vnode_unlock(ref->vp);
+ }
+ }
+ else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
+ {
+ /* Partition. */
+ device = va.va_rdev;
- bzero( (void *)header, header_size );
+ p1 = ref->vp;
+ p2 = ref->ctx;
+ do_ioctl = &device_ioctl;
+ }
+ else
+ {
+ /* Don't dump to non-regular files. */
+ error = EFAULT;
+ goto out;
+ }
+ ref->device = device;
- /*
- * Set up Mach-O header.
- */
- mh = (struct mach_header *) header;
- mh->magic = orig_mh->magic;
- mh->cputype = orig_mh->cputype;
- mh->cpusubtype = orig_mh->cpusubtype;
- mh->filetype = orig_mh->filetype;
- mh->ncmds = 3;
- mh->sizeofcmds = header_size - sizeof(struct mach_header);
+ // get block size
- /*
- * Copy __DATA and __TEXT segment commands from mach_kernel so loadable drivers
- * get correct section alignment hints.
- */
- sg = (struct segment_command *)(mh+1);
- bcopy( orig_ts, sg, orig_ts->cmdsize );
+ error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &ref->blksize);
+ if (error)
+ goto out;
- sg = (struct segment_command *)((int)sg + sg->cmdsize);
- bcopy( orig_ds, sg, orig_ds->cmdsize );
+ if (ref->vp->v_type != VREG)
+ {
+ error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &fileblk);
+ if (error)
+ goto out;
+ ref->filelength = fileblk * ref->blksize;
+ }
- sg = (struct segment_command *)(mh+1);
-
- for ( i = 0; i < 2; i++ )
- {
- sg->vmaddr = 0;
- sg->vmsize = 0x1000;
- sg->fileoff = 0;
- sg->filesize = 0;
- sg->maxprot = 0;
- sg->initprot = 0;
- sg->flags = 0;
-
- se = (struct section *)(sg+1);
- for ( j = 0; j < sg->nsects; j++, se++ )
- {
- se->addr = 0;
- se->size = 0;
- se->offset = 0;
- se->nreloc = 0;
- }
+ // pin logical extents
- sg = (struct segment_command *)((int)sg + sg->cmdsize);
- }
+ error = kern_ioctl_file_extents(ref, _DKIOCCSPINEXTENT, 0, ref->filelength);
+ if (error && (ENOTTY != error)) goto out;
+ ref->pinned = (error == 0);
- symfoffset = round_page(header_size);
+ // generate the block list
- /*
- * Set up LC_SYMTAB command
- */
- sc = (struct symtab_command *)sg;
- sc->cmd = LC_SYMTAB;
- sc->cmdsize = sizeof(struct symtab_command);
- sc->symoff = symfoffset;
- sc->nsyms = sc0->nsyms;
- sc->strsize = sc0->strsize;
- sc->stroff = symfoffset + sc->nsyms * sizeof(struct nlist);
+ error = do_ioctl(p1, p2, DKIOCLOCKPHYSICALEXTENTS, NULL);
+ if (error)
+ goto out;
+ locked = TRUE;
- symsize = sc->nsyms * sizeof(struct nlist) + sc->strsize;
+ f_offset = 0;
+ while (f_offset < ref->filelength)
+ {
+ if (ref->vp->v_type == VREG)
+ {
+ filechunk = 1*1024*1024*1024;
+ daddr64_t blkno;
+
+ error = VNOP_BLOCKMAP(ref->vp, f_offset, filechunk, &blkno, &filechunk, NULL, 0, NULL);
+ if (error)
+ goto out;
- nl = (struct nlist *)(sc0+1);
- for (i = 0; i < sc->nsyms; i++, nl++ )
+ fileblk = blkno * ref->blksize;
+ }
+ else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
{
- if ( (nl->n_type & N_TYPE) == N_SECT )
+ fileblk = f_offset;
+ filechunk = f_offset ? 0 : ref->filelength;
+ }
+
+ physoffset = 0;
+ while (physoffset < filechunk)
+ {
+ dk_physical_extent_t getphysreq;
+ bzero(&getphysreq, sizeof(getphysreq));
+
+ getphysreq.offset = fileblk + physoffset;
+ getphysreq.length = (filechunk - physoffset);
+ error = do_ioctl(p1, p2, DKIOCGETPHYSICALEXTENT, (caddr_t) &getphysreq);
+ if (error)
+ goto out;
+ if (!target)
+ {
+ target = getphysreq.dev;
+ }
+ else if (target != getphysreq.dev)
{
- nl->n_sect = NO_SECT;
- nl->n_type = (nl->n_type & ~N_TYPE) | N_ABS;
+ error = ENOTSUP;
+ goto out;
}
+#if HIBFRAGMENT
+ uint64_t rev;
+ for (rev = 4096; rev <= getphysreq.length; rev += 4096)
+ {
+ callback(callback_ref, getphysreq.offset + getphysreq.length - rev, 4096);
+ }
+#else
+ callback(callback_ref, getphysreq.offset, getphysreq.length);
+#endif
+ physoffset += getphysreq.length;
}
-
- /*
- * Write out the load commands at the beginning of the
- * file.
- */
- error = vn_rdwr(UIO_WRITE, vp, (caddr_t)mh, header_size, (off_t)0,
- UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
- if ( error != 0 ) goto out;
-
- /*
- * Write out kernel symbols
- */
- error = vn_rdwr(UIO_WRITE, vp, (caddr_t)(sc0+1), symsize, symfoffset,
- UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
- if ( error != 0 ) goto out;
+ f_offset += filechunk;
+ }
+ callback(callback_ref, 0ULL, 0ULL);
+
+ if (ref->vp->v_type == VREG)
+ p1 = ⌖
+
+ // get partition base
+
+ error = do_ioctl(p1, p2, DKIOCGETBASE, (caddr_t) partitionbase_result);
+ if (error)
+ goto out;
+
+ // get block size & constraints
+
+ error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &blksize);
+ if (error)
+ goto out;
+
+ maxiocount = 1*1024*1024*1024;
+
+ error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t) &count);
+ if (error)
+ count = 0;
+ count *= blksize;
+ if (count && (count < maxiocount))
+ maxiocount = count;
+
+ error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t) &count);
+ if (error)
+ count = 0;
+ count *= blksize;
+ if (count && (count < maxiocount))
+ maxiocount = count;
+
+ error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTREAD, (caddr_t) &count);
+ if (error)
+ count = 0;
+ if (count && (count < maxiocount))
+ maxiocount = count;
+
+ error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t) &count);
+ if (error)
+ count = 0;
+ if (count && (count < maxiocount))
+ maxiocount = count;
+
+ error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTREAD, (caddr_t) &count);
+ if (!error)
+ error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTCOUNTREAD, (caddr_t) &segcount);
+ if (error)
+ count = segcount = 0;
+ count *= segcount;
+ if (count && (count < maxiocount))
+ maxiocount = count;
+
+ error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTWRITE, (caddr_t) &count);
+ if (!error)
+ error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTCOUNTWRITE, (caddr_t) &segcount);
+ if (error)
+ count = segcount = 0;
+ count *= segcount;
+ if (count && (count < maxiocount))
+ maxiocount = count;
+
+ kprintf("max io 0x%qx bytes\n", maxiocount);
+ if (maxiocount_result)
+ *maxiocount_result = maxiocount;
+
+ error = do_ioctl(p1, p2, DKIOCISSOLIDSTATE, (caddr_t)&isssd);
+ if (!error && isssd)
+ flags |= kIOHibernateOptionSSD;
+
+ if (partition_device_result)
+ *partition_device_result = device;
+ if (image_device_result)
+ *image_device_result = target;
+ if (flags)
+ *oflags = flags;
out:
- if ( header != 0 ) kmem_free(kernel_map, header, header_size);
- if ( orig_mh != 0 ) IODTFreeLoaderInfo( "Kernel-__HEADER", (void *)orig_mh, round_page(orig_mhsize) );
- if ( sc0 != 0 ) IODTFreeLoaderInfo( "Kernel-__SYMTAB", (void *)sc0, round_page(sc0_size) );
-
- if ( vp != 0 )
- {
- VOP_UNLOCK(vp, 0, p);
- error1 = vn_close(vp, FWRITE, cred, p);
- if (error == 0) error = error1;
- }
+ kprintf("kern_open_file_for_direct_io(%d)\n", error);
+
+ if (error && locked)
+ {
+ p1 = &device;
+ (void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL);
+ }
- return(error);
+ if (error && ref)
+ {
+ if (ref->vp)
+ {
+ vnode_close(ref->vp, FWRITE, ref->ctx);
+ ref->vp = NULLVP;
+ }
+ vfs_context_rele(ref->ctx);
+ kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
+ ref = NULL;
+ }
+
+ return(ref);
}
-/*
- *
- */
-struct segment_command *findSegmentByName( struct mach_header *mh, const char *section_name )
+int
+kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len)
{
- struct segment_command *sg;
- int i;
-
- sg = (struct segment_command *)(mh+1);
+ return (vn_rdwr(UIO_WRITE, ref->vp,
+ addr, len, offset,
+ UIO_SYSSPACE, IO_SYNC|IO_NODELOCKED|IO_UNIT,
+ vfs_context_ucred(ref->ctx), (int *) 0,
+ vfs_context_proc(ref->ctx)));
+}
+
+
+void
+kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
+ off_t write_offset, caddr_t addr, vm_size_t write_length,
+ off_t discard_offset, off_t discard_end)
+{
+ int error;
+ kprintf("kern_close_file_for_direct_io\n");
+
+ if (!ref) return;
- for ( i=0; i < mh->ncmds; i++ )
+ if (ref->vp)
{
- if ( (sg->cmd == LC_SEGMENT) && (strcmp(sg->segname, section_name) == 0) )
+ int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
+ void * p1;
+ void * p2;
+
+ if (ref->vp->v_type == VREG)
+ {
+ p1 = &ref->device;
+ p2 = kernproc;
+ do_ioctl = &file_ioctl;
+ }
+ else
{
- return sg;
+ /* Partition. */
+ p1 = ref->vp;
+ p2 = ref->ctx;
+ do_ioctl = &device_ioctl;
}
+ (void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL);
- sg = (struct segment_command *)((int)sg + sg->cmdsize);
- }
+ if (discard_offset && discard_end && !ref->pinned)
+ {
+ (void) kern_ioctl_file_extents(ref, DKIOCUNMAP, discard_offset, discard_end);
+ }
+ if (addr && write_length)
+ {
+ (void) kern_write_file(ref, write_offset, addr, write_length);
+ }
+
+ error = vnode_close(ref->vp, FWRITE, ref->ctx);
- return NULL;
-}
-
-
-
-
+ ref->vp = NULLVP;
+ kprintf("vnode_close(%d)\n", error);
+ }
+ vfs_context_rele(ref->ctx);
+ ref->ctx = NULL;
+ kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
+}