]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_symfile.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / bsd / kern / kern_symfile.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
29 *
30 * File: bsd/kern/kern_symfile.c
31 *
1c79356b 32 * HISTORY
1c79356b
A
33 */
34
35#include <mach/vm_param.h>
36
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>
91447636
A
42#include <sys/vnode_internal.h>
43#include <sys/proc_internal.h>
44#include <sys/kauth.h>
1c79356b
A
45#include <sys/timeb.h>
46#include <sys/times.h>
1c79356b 47#include <sys/acct.h>
91447636 48#include <sys/file_internal.h>
1c79356b
A
49#include <sys/uio.h>
50#include <sys/kernel.h>
51#include <sys/stat.h>
91447636
A
52#include <sys/disk.h>
53#include <sys/conf.h>
1c79356b
A
54
55#include <mach-o/loader.h>
56#include <mach-o/nlist.h>
57
91447636 58#include <kern/kalloc.h>
1c79356b 59#include <vm/vm_kern.h>
91447636 60#include <pexpert/pexpert.h>
3a60a9f5 61#include <IOKit/IOHibernatePrivate.h>
1c79356b 62
2d21ac55
A
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.
1c79356b 66 */
2d21ac55
A
67int
68get_kernel_symfile(__unused proc_t p, __unused char const **symfile)
1c79356b 69{
2d21ac55 70 return KERN_FAILURE;
0b4e3aa0 71}
91447636 72
3a60a9f5
A
73struct kern_direct_file_io_ref_t
74{
2d21ac55 75 vfs_context_t ctx;
3a60a9f5
A
76 struct vnode *vp;
77};
78
79
80static int file_ioctl(void * p1, void * p2, int theIoctl, caddr_t result)
81{
82 dev_t device = (dev_t) p1;
83
84 return ((*bdevsw[major(device)].d_ioctl)
85 (device, theIoctl, result, S_IFBLK, p2));
86}
87
88static int device_ioctl(void * p1, __unused void * p2, int theIoctl, caddr_t result)
89{
90 return (VNOP_IOCTL(p1, theIoctl, result, 0, p2));
91}
92
93struct kern_direct_file_io_ref_t *
94kern_open_file_for_direct_io(const char * name,
95 kern_get_file_extents_callback_t callback,
96 void * callback_ref,
97 dev_t * device_result,
98 uint64_t * partitionbase_result,
99 uint64_t * maxiocount_result)
100{
101 struct kern_direct_file_io_ref_t * ref;
102
2d21ac55 103 proc_t p;
3a60a9f5
A
104 struct vnode_attr va;
105 int error;
106 off_t f_offset;
107 uint32_t blksize;
108 uint64_t size;
109 dev_t device;
110 off_t maxiocount, count;
111
112 int (*do_ioctl)(void * p1, void * p2, int theIoctl, caddr_t result);
113 void * p1;
114 void * p2;
115
116 error = EFAULT;
117
118 ref = (struct kern_direct_file_io_ref_t *) kalloc(sizeof(struct kern_direct_file_io_ref_t));
119 if (!ref)
120 {
121 error = EFAULT;
122 goto out;
123 }
124
125 ref->vp = NULL;
126 p = current_proc(); // kernproc;
2d21ac55 127 ref->ctx = vfs_context_create(vfs_context_current());
3a60a9f5 128
2d21ac55 129 if ((error = vnode_open(name, (O_CREAT | FWRITE), (0), 0, &ref->vp, ref->ctx)))
3a60a9f5
A
130 goto out;
131
132 VATTR_INIT(&va);
133 VATTR_WANTED(&va, va_rdev);
134 VATTR_WANTED(&va, va_fsid);
135 VATTR_WANTED(&va, va_data_size);
136 VATTR_WANTED(&va, va_nlink);
137 error = EFAULT;
2d21ac55 138 if (vnode_getattr(ref->vp, &va, ref->ctx))
3a60a9f5
A
139 goto out;
140
141 kprintf("vp va_rdev major %d minor %d\n", major(va.va_rdev), minor(va.va_rdev));
142 kprintf("vp va_fsid major %d minor %d\n", major(va.va_fsid), minor(va.va_fsid));
143 kprintf("vp size %qd\n", va.va_data_size);
144
145 if (ref->vp->v_type == VREG)
146 {
147 /* Don't dump files with links. */
148 if (va.va_nlink != 1)
149 goto out;
150
151 device = va.va_fsid;
152 p1 = (void *) device;
153 p2 = p;
154 do_ioctl = &file_ioctl;
155 }
156 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
157 {
158 /* Partition. */
159 device = va.va_rdev;
160
161 p1 = ref->vp;
2d21ac55 162 p2 = ref->ctx;
3a60a9f5
A
163 do_ioctl = &device_ioctl;
164 }
165 else
166 {
167 /* Don't dump to non-regular files. */
168 error = EFAULT;
169 goto out;
170 }
171
172 // get partition base
173
174 error = do_ioctl(p1, p2, DKIOCGETBASE, (caddr_t) partitionbase_result);
175 if (error)
176 goto out;
177
178 // get block size & constraints
179
180 error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &blksize);
181 if (error)
182 goto out;
183
184 maxiocount = 1*1024*1024*1024;
185
186 error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t) &count);
187 if (error)
188 count = 0;
189 count *= blksize;
190 if (count && (count < maxiocount))
191 maxiocount = count;
192
193 error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t) &count);
194 if (error)
195 count = 0;
196 count *= blksize;
197 if (count && (count < maxiocount))
198 maxiocount = count;
199
200 error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTREAD, (caddr_t) &count);
201 if (error)
202 count = 0;
203 if (count && (count < maxiocount))
204 maxiocount = count;
205
206 error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t) &count);
207 if (error)
208 count = 0;
209 if (count && (count < maxiocount))
210 maxiocount = count;
211
212 error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTREAD, (caddr_t) &count);
213 if (error)
214 count = 0;
215 if (count && (count < maxiocount))
216 maxiocount = count;
217
218 error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTWRITE, (caddr_t) &count);
219 if (error)
220 count = 0;
221 if (count && (count < maxiocount))
222 maxiocount = count;
223
224 kprintf("max io 0x%qx bytes\n", maxiocount);
225 if (maxiocount_result)
226 *maxiocount_result = maxiocount;
227
228 // generate the block list
229
230 error = 0;
231 if (ref->vp->v_type == VREG)
232 {
233 f_offset = 0;
234 while(f_offset < (off_t) va.va_data_size)
235 {
236 size_t io_size = 1*1024*1024*1024;
237 daddr64_t blkno;
238
239 error = VNOP_BLOCKMAP(ref->vp, f_offset, io_size, &blkno, (size_t *)&io_size, NULL, 0, NULL);
240 if (error)
241 goto out;
242 callback(callback_ref, ((uint64_t) blkno) * blksize, (uint64_t) io_size);
243 f_offset += io_size;
244 }
245 callback(callback_ref, 0ULL, 0ULL);
246 }
247 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
248 {
249 error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &size);
250 if (error)
251 goto out;
252 size *= blksize;
253 callback(callback_ref, 0ULL, size);
254 callback(callback_ref, size, 0ULL);
255 }
256
257 if (device_result)
258 *device_result = device;
259
260out:
261 kprintf("kern_open_file_for_direct_io(%d)\n", error);
262
263 if (error && ref) {
2d21ac55
A
264 if (ref->vp) {
265 vnode_close(ref->vp, FWRITE, ref->ctx);
266 ref->vp = NULLVP;
267 }
268
269 vfs_context_rele(ref->ctx);
270 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
271 ref = NULL;
3a60a9f5
A
272 }
273
274 return(ref);
275}
276
277int
278kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len)
279{
280 return (vn_rdwr(UIO_WRITE, ref->vp,
281 addr, len, offset,
282 UIO_SYSSPACE32, IO_SYNC|IO_NODELOCKED|IO_UNIT,
2d21ac55
A
283 vfs_context_ucred(ref->ctx), (int *) 0,
284 vfs_context_proc(ref->ctx)));
3a60a9f5
A
285}
286
287void
288kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref)
289{
290 kprintf("kern_close_file_for_direct_io\n");
291
292 if (ref) {
293 int error;
294
295 if (ref->vp) {
2d21ac55
A
296 error = vnode_close(ref->vp, FWRITE, ref->ctx);
297 ref->vp = NULLVP;
3a60a9f5
A
298 kprintf("vnode_close(%d)\n", error);
299 }
2d21ac55
A
300 vfs_context_rele(ref->ctx);
301 ref->ctx = NULL;
3a60a9f5
A
302 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
303 }
304}
2d21ac55 305