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
)
73 struct kern_direct_file_io_ref_t
81 static int file_ioctl(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
)
83 dev_t device
= *(dev_t
*) p1
;
85 return ((*bdevsw
[major(device
)].d_ioctl
)
86 (device
, theIoctl
, result
, S_IFBLK
, p2
));
89 static int device_ioctl(void * p1
, __unused
void * p2
, u_long theIoctl
, caddr_t result
)
91 return (VNOP_IOCTL(p1
, theIoctl
, result
, 0, p2
));
94 struct kern_direct_file_io_ref_t
*
95 kern_open_file_for_direct_io(const char * name
,
96 kern_get_file_extents_callback_t callback
,
98 dev_t
* partition_device_result
,
99 dev_t
* image_device_result
,
100 uint64_t * partitionbase_result
,
101 uint64_t * maxiocount_result
,
107 struct kern_direct_file_io_ref_t
* ref
;
110 struct vnode_attr va
;
122 off_t maxiocount
, count
;
123 boolean_t locked
= FALSE
;
125 int (*do_ioctl
)(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
);
131 ref
= (struct kern_direct_file_io_ref_t
*) kalloc(sizeof(struct kern_direct_file_io_ref_t
));
140 ref
->ctx
= vfs_context_create(vfs_context_current());
142 if ((error
= vnode_open(name
, (O_CREAT
| FWRITE
), (0), 0, &ref
->vp
, ref
->ctx
)))
147 if ((error
= kern_write_file(ref
, offset
, addr
, len
)))
152 VATTR_WANTED(&va
, va_rdev
);
153 VATTR_WANTED(&va
, va_fsid
);
154 VATTR_WANTED(&va
, va_data_size
);
155 VATTR_WANTED(&va
, va_nlink
);
157 if (vnode_getattr(ref
->vp
, &va
, ref
->ctx
))
160 kprintf("vp va_rdev major %d minor %d\n", major(va
.va_rdev
), minor(va
.va_rdev
));
161 kprintf("vp va_fsid major %d minor %d\n", major(va
.va_fsid
), minor(va
.va_fsid
));
162 kprintf("vp size %qd\n", va
.va_data_size
);
164 if (ref
->vp
->v_type
== VREG
)
166 /* Don't dump files with links. */
167 if (va
.va_nlink
!= 1)
173 do_ioctl
= &file_ioctl
;
175 else if ((ref
->vp
->v_type
== VBLK
) || (ref
->vp
->v_type
== VCHR
))
182 do_ioctl
= &device_ioctl
;
186 /* Don't dump to non-regular files. */
190 ref
->device
= device
;
192 // generate the block list
194 error
= do_ioctl(p1
, p2
, DKIOCLOCKPHYSICALEXTENTS
, NULL
);
201 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKSIZE
, (caddr_t
) &blksize
);
205 if (ref
->vp
->v_type
== VREG
)
206 filelength
= va
.va_data_size
;
209 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKCOUNT
, (caddr_t
) &fileblk
);
212 filelength
= fileblk
* blksize
;
216 while (f_offset
< filelength
)
218 if (ref
->vp
->v_type
== VREG
)
220 filechunk
= 1*1024*1024*1024;
223 error
= VNOP_BLOCKMAP(ref
->vp
, f_offset
, filechunk
, &blkno
, &filechunk
, NULL
, 0, NULL
);
227 fileblk
= blkno
* blksize
;
229 else if ((ref
->vp
->v_type
== VBLK
) || (ref
->vp
->v_type
== VCHR
))
232 filechunk
= f_offset
? 0 : filelength
;
236 while (physoffset
< filechunk
)
238 dk_physical_extent_t getphysreq
;
239 bzero(&getphysreq
, sizeof(getphysreq
));
241 getphysreq
.offset
= fileblk
+ physoffset
;
242 getphysreq
.length
= (filechunk
- physoffset
);
243 error
= do_ioctl(p1
, p2
, DKIOCGETPHYSICALEXTENT
, (caddr_t
) &getphysreq
);
248 target
= getphysreq
.dev
;
250 else if (target
!= getphysreq
.dev
)
255 callback(callback_ref
, getphysreq
.offset
, getphysreq
.length
);
256 physoffset
+= getphysreq
.length
;
258 f_offset
+= filechunk
;
260 callback(callback_ref
, 0ULL, 0ULL);
262 if (ref
->vp
->v_type
== VREG
)
265 // get partition base
267 error
= do_ioctl(p1
, p2
, DKIOCGETBASE
, (caddr_t
) partitionbase_result
);
271 // get block size & constraints
273 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKSIZE
, (caddr_t
) &blksize
);
277 maxiocount
= 1*1024*1024*1024;
279 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBLOCKCOUNTREAD
, (caddr_t
) &count
);
283 if (count
&& (count
< maxiocount
))
286 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBLOCKCOUNTWRITE
, (caddr_t
) &count
);
290 if (count
&& (count
< maxiocount
))
293 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBYTECOUNTREAD
, (caddr_t
) &count
);
296 if (count
&& (count
< maxiocount
))
299 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBYTECOUNTWRITE
, (caddr_t
) &count
);
302 if (count
&& (count
< maxiocount
))
305 error
= do_ioctl(p1
, p2
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
, (caddr_t
) &count
);
308 if (count
&& (count
< maxiocount
))
311 error
= do_ioctl(p1
, p2
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
, (caddr_t
) &count
);
314 if (count
&& (count
< maxiocount
))
317 kprintf("max io 0x%qx bytes\n", maxiocount
);
318 if (maxiocount_result
)
319 *maxiocount_result
= maxiocount
;
321 error
= do_ioctl(p1
, p2
, DKIOCISSOLIDSTATE
, (caddr_t
)&isssd
);
323 flags
|= kIOHibernateOptionSSD
;
325 if (partition_device_result
)
326 *partition_device_result
= device
;
327 if (image_device_result
)
328 *image_device_result
= target
;
333 kprintf("kern_open_file_for_direct_io(%d)\n", error
);
338 (void) do_ioctl(p1
, p2
, DKIOCUNLOCKPHYSICALEXTENTS
, NULL
);
345 vnode_close(ref
->vp
, FWRITE
, ref
->ctx
);
348 vfs_context_rele(ref
->ctx
);
349 kfree(ref
, sizeof(struct kern_direct_file_io_ref_t
));
356 kern_write_file(struct kern_direct_file_io_ref_t
* ref
, off_t offset
, caddr_t addr
, vm_size_t len
)
358 return (vn_rdwr(UIO_WRITE
, ref
->vp
,
360 UIO_SYSSPACE
, IO_SYNC
|IO_NODELOCKED
|IO_UNIT
,
361 vfs_context_ucred(ref
->ctx
), (int *) 0,
362 vfs_context_proc(ref
->ctx
)));
366 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t
* ref
,
367 off_t offset
, caddr_t addr
, vm_size_t len
)
370 kprintf("kern_close_file_for_direct_io\n");
376 int (*do_ioctl
)(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
);
380 if (ref
->vp
->v_type
== VREG
)
384 do_ioctl
= &file_ioctl
;
391 do_ioctl
= &device_ioctl
;
393 (void) do_ioctl(p1
, p2
, DKIOCUNLOCKPHYSICALEXTENTS
, NULL
);
397 (void) kern_write_file(ref
, offset
, addr
, len
);
400 error
= vnode_close(ref
->vp
, FWRITE
, ref
->ctx
);
403 kprintf("vnode_close(%d)\n", error
);
405 vfs_context_rele(ref
->ctx
);
407 kfree(ref
, sizeof(struct kern_direct_file_io_ref_t
));