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
86 static int file_ioctl(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
)
88 dev_t device
= *(dev_t
*) p1
;
90 return ((*bdevsw
[major(device
)].d_ioctl
)
91 (device
, theIoctl
, result
, S_IFBLK
, p2
));
94 static int device_ioctl(void * p1
, __unused
void * p2
, u_long theIoctl
, caddr_t result
)
96 return (VNOP_IOCTL(p1
, theIoctl
, result
, 0, p2
));
100 kern_ioctl_file_extents(struct kern_direct_file_io_ref_t
* ref
, u_long theIoctl
, off_t offset
, off_t end
)
103 int (*do_ioctl
)(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
);
112 bzero(&extent
, sizeof(dk_extent_t
));
113 bzero(&unmap
, sizeof(dk_unmap_t
));
114 bzero(&pin
, sizeof(pin
));
115 if (ref
->vp
->v_type
== VREG
)
119 do_ioctl
= &file_ioctl
;
126 do_ioctl
= &device_ioctl
;
130 if (ref
->vp
->v_type
== VREG
)
133 filechunk
= 1*1024*1024*1024;
134 if (filechunk
> (size_t)(end
- offset
))
135 filechunk
= (size_t)(end
- offset
);
136 error
= VNOP_BLOCKMAP(ref
->vp
, offset
, filechunk
, &blkno
, &filechunk
, NULL
, 0, NULL
);
138 fileblk
= blkno
* ref
->blksize
;
140 else if ((ref
->vp
->v_type
== VBLK
) || (ref
->vp
->v_type
== VCHR
))
143 filechunk
= ref
->filelength
;
146 if (DKIOCUNMAP
== theIoctl
)
148 extent
.offset
= fileblk
;
149 extent
.length
= filechunk
;
150 unmap
.extents
= &extent
;
151 unmap
.extentsCount
= 1;
152 error
= do_ioctl(p1
, p2
, theIoctl
, (caddr_t
)&unmap
);
153 // printf("DKIOCUNMAP(%d) 0x%qx, 0x%qx\n", error, extent.offset, extent.length);
155 else if (_DKIOCCSPINEXTENT
== theIoctl
)
157 pin
.cp_extent
.offset
= fileblk
;
158 pin
.cp_extent
.length
= filechunk
;
159 pin
.cp_flags
= _DKIOCSPINDISCARDDATA
;
160 error
= do_ioctl(p1
, p2
, theIoctl
, (caddr_t
)&pin
);
161 if (error
&& (ENOTTY
!= error
))
163 printf("_DKIOCCSPINEXTENT(%d) 0x%qx, 0x%qx\n",
164 error
, pin
.cp_extent
.offset
, pin
.cp_extent
.length
);
176 kern_write_file(struct kern_direct_file_io_ref_t
* ref
, off_t offset
, caddr_t addr
, vm_size_t len
);
178 struct kern_direct_file_io_ref_t
*
179 kern_open_file_for_direct_io(const char * name
,
180 kern_get_file_extents_callback_t callback
,
182 dev_t
* partition_device_result
,
183 dev_t
* image_device_result
,
184 uint64_t * partitionbase_result
,
185 uint64_t * maxiocount_result
,
191 struct kern_direct_file_io_ref_t
* ref
;
194 struct vnode_attr va
;
205 off_t maxiocount
, count
;
206 boolean_t locked
= FALSE
;
208 int (*do_ioctl
)(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
);
214 ref
= (struct kern_direct_file_io_ref_t
*) kalloc(sizeof(struct kern_direct_file_io_ref_t
));
221 bzero(ref
, sizeof(*ref
));
223 ref
->ctx
= vfs_context_create(vfs_context_current());
225 if ((error
= vnode_open(name
, (O_CREAT
| FWRITE
), (0), 0, &ref
->vp
, ref
->ctx
)))
230 if ((error
= kern_write_file(ref
, offset
, addr
, len
)))
235 VATTR_WANTED(&va
, va_rdev
);
236 VATTR_WANTED(&va
, va_fsid
);
237 VATTR_WANTED(&va
, va_data_size
);
238 VATTR_WANTED(&va
, va_nlink
);
240 if (vnode_getattr(ref
->vp
, &va
, ref
->ctx
))
243 kprintf("vp va_rdev major %d minor %d\n", major(va
.va_rdev
), minor(va
.va_rdev
));
244 kprintf("vp va_fsid major %d minor %d\n", major(va
.va_fsid
), minor(va
.va_fsid
));
245 kprintf("vp size %qd\n", va
.va_data_size
);
247 if (ref
->vp
->v_type
== VREG
)
249 /* Don't dump files with links. */
250 if (va
.va_nlink
!= 1)
256 do_ioctl
= &file_ioctl
;
258 else if ((ref
->vp
->v_type
== VBLK
) || (ref
->vp
->v_type
== VCHR
))
265 do_ioctl
= &device_ioctl
;
269 /* Don't dump to non-regular files. */
273 ref
->device
= device
;
277 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKSIZE
, (caddr_t
) &ref
->blksize
);
281 if (ref
->vp
->v_type
== VREG
)
282 ref
->filelength
= va
.va_data_size
;
285 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKCOUNT
, (caddr_t
) &fileblk
);
288 ref
->filelength
= fileblk
* ref
->blksize
;
291 // pin logical extents
293 error
= kern_ioctl_file_extents(ref
, _DKIOCCSPINEXTENT
, 0, ref
->filelength
);
294 if (error
&& (ENOTTY
!= error
)) goto out
;
295 ref
->pinned
= (error
== 0);
297 // generate the block list
299 error
= do_ioctl(p1
, p2
, DKIOCLOCKPHYSICALEXTENTS
, NULL
);
305 while (f_offset
< ref
->filelength
)
307 if (ref
->vp
->v_type
== VREG
)
309 filechunk
= 1*1024*1024*1024;
312 error
= VNOP_BLOCKMAP(ref
->vp
, f_offset
, filechunk
, &blkno
, &filechunk
, NULL
, 0, NULL
);
316 fileblk
= blkno
* ref
->blksize
;
318 else if ((ref
->vp
->v_type
== VBLK
) || (ref
->vp
->v_type
== VCHR
))
321 filechunk
= f_offset
? 0 : ref
->filelength
;
325 while (physoffset
< filechunk
)
327 dk_physical_extent_t getphysreq
;
328 bzero(&getphysreq
, sizeof(getphysreq
));
330 getphysreq
.offset
= fileblk
+ physoffset
;
331 getphysreq
.length
= (filechunk
- physoffset
);
332 error
= do_ioctl(p1
, p2
, DKIOCGETPHYSICALEXTENT
, (caddr_t
) &getphysreq
);
337 target
= getphysreq
.dev
;
339 else if (target
!= getphysreq
.dev
)
344 callback(callback_ref
, getphysreq
.offset
, getphysreq
.length
);
345 physoffset
+= getphysreq
.length
;
347 f_offset
+= filechunk
;
349 callback(callback_ref
, 0ULL, 0ULL);
351 if (ref
->vp
->v_type
== VREG
)
354 // get partition base
356 error
= do_ioctl(p1
, p2
, DKIOCGETBASE
, (caddr_t
) partitionbase_result
);
360 // get block size & constraints
362 error
= do_ioctl(p1
, p2
, DKIOCGETBLOCKSIZE
, (caddr_t
) &blksize
);
366 maxiocount
= 1*1024*1024*1024;
368 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBLOCKCOUNTREAD
, (caddr_t
) &count
);
372 if (count
&& (count
< maxiocount
))
375 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBLOCKCOUNTWRITE
, (caddr_t
) &count
);
379 if (count
&& (count
< maxiocount
))
382 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBYTECOUNTREAD
, (caddr_t
) &count
);
385 if (count
&& (count
< maxiocount
))
388 error
= do_ioctl(p1
, p2
, DKIOCGETMAXBYTECOUNTWRITE
, (caddr_t
) &count
);
391 if (count
&& (count
< maxiocount
))
394 error
= do_ioctl(p1
, p2
, DKIOCGETMAXSEGMENTBYTECOUNTREAD
, (caddr_t
) &count
);
397 if (count
&& (count
< maxiocount
))
400 error
= do_ioctl(p1
, p2
, DKIOCGETMAXSEGMENTBYTECOUNTWRITE
, (caddr_t
) &count
);
403 if (count
&& (count
< maxiocount
))
406 kprintf("max io 0x%qx bytes\n", maxiocount
);
407 if (maxiocount_result
)
408 *maxiocount_result
= maxiocount
;
410 error
= do_ioctl(p1
, p2
, DKIOCISSOLIDSTATE
, (caddr_t
)&isssd
);
412 flags
|= kIOHibernateOptionSSD
;
414 if (partition_device_result
)
415 *partition_device_result
= device
;
416 if (image_device_result
)
417 *image_device_result
= target
;
422 kprintf("kern_open_file_for_direct_io(%d)\n", error
);
427 (void) do_ioctl(p1
, p2
, DKIOCUNLOCKPHYSICALEXTENTS
, NULL
);
434 vnode_close(ref
->vp
, FWRITE
, ref
->ctx
);
437 vfs_context_rele(ref
->ctx
);
438 kfree(ref
, sizeof(struct kern_direct_file_io_ref_t
));
445 kern_write_file(struct kern_direct_file_io_ref_t
* ref
, off_t offset
, caddr_t addr
, vm_size_t len
)
447 return (vn_rdwr(UIO_WRITE
, ref
->vp
,
449 UIO_SYSSPACE
, IO_SYNC
|IO_NODELOCKED
|IO_UNIT
,
450 vfs_context_ucred(ref
->ctx
), (int *) 0,
451 vfs_context_proc(ref
->ctx
)));
456 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t
* ref
,
457 off_t write_offset
, caddr_t addr
, vm_size_t write_length
,
458 off_t discard_offset
, off_t discard_end
)
461 kprintf("kern_close_file_for_direct_io\n");
467 int (*do_ioctl
)(void * p1
, void * p2
, u_long theIoctl
, caddr_t result
);
471 if (ref
->vp
->v_type
== VREG
)
475 do_ioctl
= &file_ioctl
;
482 do_ioctl
= &device_ioctl
;
484 (void) do_ioctl(p1
, p2
, DKIOCUNLOCKPHYSICALEXTENTS
, NULL
);
486 if (addr
&& write_length
)
488 (void) kern_write_file(ref
, write_offset
, addr
, write_length
);
490 if (discard_offset
&& discard_end
&& !ref
->pinned
)
492 (void) kern_ioctl_file_extents(ref
, DKIOCUNMAP
, discard_offset
, discard_end
);
495 error
= vnode_close(ref
->vp
, FWRITE
, ref
->ctx
);
498 kprintf("vnode_close(%d)\n", error
);
500 vfs_context_rele(ref
->ctx
);
502 kfree(ref
, sizeof(struct kern_direct_file_io_ref_t
));