2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
30 * File: bsd/kern/kern_symfile.c
35 #include <mach/vm_param.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/signalvar.h>
40 #include <sys/resourcevar.h>
41 #include <sys/namei.h>
42 #include <sys/vnode_internal.h>
43 #include <sys/proc_internal.h>
44 #include <sys/kauth.h>
45 #include <sys/timeb.h>
46 #include <sys/times.h>
48 #include <sys/file_internal.h>
50 #include <sys/kernel.h>
55 #include <mach-o/loader.h>
56 #include <mach-o/nlist.h>
58 #include <kern/kalloc.h>
59 #include <vm/vm_kern.h>
60 #include <pexpert/pexpert.h>
61 #include <IOKit/IOHibernatePrivate.h>
63 /* This function is called from kern_sysctl in the current process context;
64 * it is exported with the System6.0.exports, but this appears to be a legacy
65 * export, as there are no internal consumers.
68 get_kernel_symfile(__unused proc_t p
, __unused
char const **symfile
);
70 get_kernel_symfile(__unused proc_t p
, __unused
char const **symfile
)
75 struct kern_direct_file_io_ref_t
85 static int file_ioctl(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
)
87 dev_t device
= *(dev_t
*) p1
;
89 return ((*bdevsw
[major(device
)].d_ioctl
)
90 (device
, theIoctl
, result
, S_IFBLK
, p2
));
93 static int device_ioctl(void * p1
, __unused
void * p2
, u_long theIoctl
, caddr_t result
)
95 return (VNOP_IOCTL(p1
, theIoctl
, result
, 0, p2
));
99 kern_unmap_file(struct kern_direct_file_io_ref_t
* ref
, off_t f_offset
, off_t end
);
101 kern_write_file(struct kern_direct_file_io_ref_t
* ref
, off_t offset
, caddr_t addr
, vm_size_t len
);
103 struct kern_direct_file_io_ref_t
*
104 kern_open_file_for_direct_io(const char * name
,
105 kern_get_file_extents_callback_t callback
,
107 dev_t
* partition_device_result
,
108 dev_t
* image_device_result
,
109 uint64_t * partitionbase_result
,
110 uint64_t * maxiocount_result
,
116 struct kern_direct_file_io_ref_t
* ref
;
119 struct vnode_attr va
;
130 off_t maxiocount
, count
;
131 boolean_t locked
= FALSE
;
133 int (*do_ioctl
)(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
);
139 ref
= (struct kern_direct_file_io_ref_t
*) kalloc(sizeof(struct kern_direct_file_io_ref_t
));
148 ref
->ctx
= vfs_context_create(vfs_context_current());
150 if ((error
= vnode_open(name
, (O_CREAT
| FWRITE
), (0), 0, &ref
->vp
, ref
->ctx
)))
155 if ((error
= kern_write_file(ref
, offset
, addr
, len
)))
160 VATTR_WANTED(&va
, va_rdev
);
161 VATTR_WANTED(&va
, va_fsid
);
162 VATTR_WANTED(&va
, va_data_size
);
163 VATTR_WANTED(&va
, va_nlink
);
165 if (vnode_getattr(ref
->vp
, &va
, ref
->ctx
))
168 kprintf("vp va_rdev major %d minor %d\n", major(va
.va_rdev
), minor(va
.va_rdev
));
169 kprintf("vp va_fsid major %d minor %d\n", major(va
.va_fsid
), minor(va
.va_fsid
));
170 kprintf("vp size %qd\n", va
.va_data_size
);
172 if (ref
->vp
->v_type
== VREG
)
174 /* Don't dump files with links. */
175 if (va
.va_nlink
!= 1)
181 do_ioctl
= &file_ioctl
;
183 else if ((ref
->vp
->v_type
== VBLK
) || (ref
->vp
->v_type
== VCHR
))
190 do_ioctl
= &device_ioctl
;
194 /* Don't dump to non-regular files. */
198 ref
->device
= device
;
200 // generate the block list
202 error
= do_ioctl(p1
, p2
, DKIOCLOCKPHYSICALEXTENTS
, NULL
);
209 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKSIZE
, (caddr_t
) &ref
->blksize
);
213 if (ref
->vp
->v_type
== VREG
)
214 ref
->filelength
= va
.va_data_size
;
217 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKCOUNT
, (caddr_t
) &fileblk
);
220 ref
->filelength
= fileblk
* ref
->blksize
;
224 while (f_offset
< ref
->filelength
)
226 if (ref
->vp
->v_type
== VREG
)
228 filechunk
= 1*1024*1024*1024;
231 error
= VNOP_BLOCKMAP(ref
->vp
, f_offset
, filechunk
, &blkno
, &filechunk
, NULL
, 0, NULL
);
235 fileblk
= blkno
* ref
->blksize
;
237 else if ((ref
->vp
->v_type
== VBLK
) || (ref
->vp
->v_type
== VCHR
))
240 filechunk
= f_offset
? 0 : ref
->filelength
;
244 while (physoffset
< filechunk
)
246 dk_physical_extent_t getphysreq
;
247 bzero(&getphysreq
, sizeof(getphysreq
));
249 getphysreq
.offset
= fileblk
+ physoffset
;
250 getphysreq
.length
= (filechunk
- physoffset
);
251 error
= do_ioctl(p1
, p2
, DKIOCGETPHYSICALEXTENT
, (caddr_t
) &getphysreq
);
256 target
= getphysreq
.dev
;
258 else if (target
!= getphysreq
.dev
)
263 callback(callback_ref
, getphysreq
.offset
, getphysreq
.length
);
264 physoffset
+= getphysreq
.length
;
266 f_offset
+= filechunk
;
268 callback(callback_ref
, 0ULL, 0ULL);
270 if (ref
->vp
->v_type
== VREG
)
273 // get partition base
275 error
= do_ioctl(p1
, p2
, DKIOCGETBASE
, (caddr_t
) partitionbase_result
);
279 // get block size & constraints
281 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKSIZE
, (caddr_t
) &blksize
);
285 maxiocount
= 1*1024*1024*1024;
287 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBLOCKCOUNTREAD
, (caddr_t
) &count
);
291 if (count
&& (count
< maxiocount
))
294 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBLOCKCOUNTWRITE
, (caddr_t
) &count
);
298 if (count
&& (count
< maxiocount
))
301 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBYTECOUNTREAD
, (caddr_t
) &count
);
304 if (count
&& (count
< maxiocount
))
307 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBYTECOUNTWRITE
, (caddr_t
) &count
);
310 if (count
&& (count
< maxiocount
))
313 error
= do_ioctl(p1
, p2
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
, (caddr_t
) &count
);
316 if (count
&& (count
< maxiocount
))
319 error
= do_ioctl(p1
, p2
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
, (caddr_t
) &count
);
322 if (count
&& (count
< maxiocount
))
325 kprintf("max io 0x%qx bytes\n", maxiocount
);
326 if (maxiocount_result
)
327 *maxiocount_result
= maxiocount
;
329 error
= do_ioctl(p1
, p2
, DKIOCISSOLIDSTATE
, (caddr_t
)&isssd
);
331 flags
|= kIOHibernateOptionSSD
;
333 if (partition_device_result
)
334 *partition_device_result
= device
;
335 if (image_device_result
)
336 *image_device_result
= target
;
341 kprintf("kern_open_file_for_direct_io(%d)\n", error
);
346 (void) do_ioctl(p1
, p2
, DKIOCUNLOCKPHYSICALEXTENTS
, NULL
);
353 vnode_close(ref
->vp
, FWRITE
, ref
->ctx
);
356 vfs_context_rele(ref
->ctx
);
357 kfree(ref
, sizeof(struct kern_direct_file_io_ref_t
));
364 kern_write_file(struct kern_direct_file_io_ref_t
* ref
, off_t offset
, caddr_t addr
, vm_size_t len
)
366 return (vn_rdwr(UIO_WRITE
, ref
->vp
,
368 UIO_SYSSPACE
, IO_SYNC
|IO_NODELOCKED
|IO_UNIT
,
369 vfs_context_ucred(ref
->ctx
), (int *) 0,
370 vfs_context_proc(ref
->ctx
)));
374 kern_unmap_file(struct kern_direct_file_io_ref_t
* ref
, off_t offset
, off_t end
)
377 int (*do_ioctl
)(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
);
385 bzero(&extent
, sizeof(dk_extent_t
));
386 bzero(&unmap
, sizeof(dk_unmap_t
));
387 if (ref
->vp
->v_type
== VREG
)
391 do_ioctl
= &file_ioctl
;
398 do_ioctl
= &device_ioctl
;
402 if (ref
->vp
->v_type
== VREG
)
405 filechunk
= 1*1024*1024*1024;
406 if (filechunk
> (size_t)(end
- offset
))
407 filechunk
= (size_t)(end
- offset
);
408 error
= VNOP_BLOCKMAP(ref
->vp
, offset
, filechunk
, &blkno
, &filechunk
, NULL
, 0, NULL
);
410 fileblk
= blkno
* ref
->blksize
;
412 else if ((ref
->vp
->v_type
== VBLK
) || (ref
->vp
->v_type
== VCHR
))
415 filechunk
= ref
->filelength
;
417 extent
.offset
= fileblk
;
418 extent
.length
= filechunk
;
419 unmap
.extents
= &extent
;
420 unmap
.extentsCount
= 1;
421 error
= do_ioctl(p1
, p2
, DKIOCUNMAP
, (caddr_t
)&unmap
);
422 // kprintf("DKIOCUNMAP(%d) 0x%qx, 0x%qx\n", error, extent.offset, extent.length);
429 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t
* ref
,
430 off_t write_offset
, caddr_t addr
, vm_size_t write_length
,
431 off_t discard_offset
, off_t discard_end
)
434 kprintf("kern_close_file_for_direct_io\n");
440 int (*do_ioctl
)(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
);
444 if (ref
->vp
->v_type
== VREG
)
448 do_ioctl
= &file_ioctl
;
455 do_ioctl
= &device_ioctl
;
457 (void) do_ioctl(p1
, p2
, DKIOCUNLOCKPHYSICALEXTENTS
, NULL
);
459 if (addr
&& write_length
)
461 (void) kern_write_file(ref
, write_offset
, addr
, write_length
);
463 if (discard_offset
&& discard_end
)
465 (void) kern_unmap_file(ref
, discard_offset
, discard_end
);
468 error
= vnode_close(ref
->vp
, FWRITE
, ref
->ctx
);
471 kprintf("vnode_close(%d)\n", error
);
473 vfs_context_rele(ref
->ctx
);
475 kfree(ref
, sizeof(struct kern_direct_file_io_ref_t
));