]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_symfile.c
xnu-2050.18.24.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 67int
7ddcb079
A
68get_kernel_symfile(__unused proc_t p, __unused char const **symfile);
69int
2d21ac55 70get_kernel_symfile(__unused proc_t p, __unused char const **symfile)
1c79356b 71{
2d21ac55 72 return KERN_FAILURE;
0b4e3aa0 73}
91447636 74
3a60a9f5
A
75struct kern_direct_file_io_ref_t
76{
6d2010ae
A
77 vfs_context_t ctx;
78 struct vnode * vp;
79 dev_t device;
7ddcb079 80 uint32_t blksize;
316670eb
A
81 off_t filelength;
82 char pinned;
3a60a9f5
A
83};
84
85
6d2010ae 86static int file_ioctl(void * p1, void * p2, u_long theIoctl, caddr_t result)
3a60a9f5 87{
b0d623f7 88 dev_t device = *(dev_t*) p1;
3a60a9f5
A
89
90 return ((*bdevsw[major(device)].d_ioctl)
91 (device, theIoctl, result, S_IFBLK, p2));
92}
93
6d2010ae 94static int device_ioctl(void * p1, __unused void * p2, u_long theIoctl, caddr_t result)
3a60a9f5
A
95{
96 return (VNOP_IOCTL(p1, theIoctl, result, 0, p2));
97}
98
316670eb
A
99static int
100kern_ioctl_file_extents(struct kern_direct_file_io_ref_t * ref, u_long theIoctl, off_t offset, off_t end)
101{
102 int error;
103 int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
104 void * p1;
105 void * p2;
106 uint64_t fileblk;
107 size_t filechunk;
108 dk_extent_t extent;
109 dk_unmap_t unmap;
110 _dk_cs_pin_t pin;
111
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)
116 {
117 p1 = &ref->device;
118 p2 = kernproc;
119 do_ioctl = &file_ioctl;
120 }
121 else
122 {
123 /* Partition. */
124 p1 = ref->vp;
125 p2 = ref->ctx;
126 do_ioctl = &device_ioctl;
127 }
128 while (offset < end)
129 {
130 if (ref->vp->v_type == VREG)
131 {
132 daddr64_t blkno;
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);
137 if (error) break;
138 fileblk = blkno * ref->blksize;
139 }
140 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
141 {
142 fileblk = offset;
143 filechunk = ref->filelength;
144 }
145
146 if (DKIOCUNMAP == theIoctl)
147 {
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);
154 }
155 else if (_DKIOCCSPINEXTENT == theIoctl)
156 {
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))
162 {
163 printf("_DKIOCCSPINEXTENT(%d) 0x%qx, 0x%qx\n",
164 error, pin.cp_extent.offset, pin.cp_extent.length);
165 }
166 }
167 else error = EINVAL;
168
169 if (error) break;
170 offset += filechunk;
171 }
172 return (error);
173}
174
7ddcb079
A
175int
176kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len);
177
3a60a9f5
A
178struct kern_direct_file_io_ref_t *
179kern_open_file_for_direct_io(const char * name,
180 kern_get_file_extents_callback_t callback,
181 void * callback_ref,
6d2010ae
A
182 dev_t * partition_device_result,
183 dev_t * image_device_result,
3a60a9f5 184 uint64_t * partitionbase_result,
0b4c1975 185 uint64_t * maxiocount_result,
6d2010ae
A
186 uint32_t * oflags,
187 off_t offset,
188 caddr_t addr,
189 vm_size_t len)
3a60a9f5
A
190{
191 struct kern_direct_file_io_ref_t * ref;
192
2d21ac55 193 proc_t p;
3a60a9f5
A
194 struct vnode_attr va;
195 int error;
196 off_t f_offset;
6d2010ae
A
197 uint64_t fileblk;
198 size_t filechunk;
199 uint64_t physoffset;
3a60a9f5 200 dev_t device;
6d2010ae
A
201 dev_t target = 0;
202 int isssd = 0;
203 uint32_t flags = 0;
204 uint32_t blksize;
3a60a9f5 205 off_t maxiocount, count;
6d2010ae 206 boolean_t locked = FALSE;
3a60a9f5 207
6d2010ae
A
208 int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
209 void * p1 = NULL;
210 void * p2 = NULL;
3a60a9f5
A
211
212 error = EFAULT;
213
214 ref = (struct kern_direct_file_io_ref_t *) kalloc(sizeof(struct kern_direct_file_io_ref_t));
215 if (!ref)
216 {
217 error = EFAULT;
218 goto out;
219 }
220
316670eb 221 bzero(ref, sizeof(*ref));
6d2010ae 222 p = kernproc;
2d21ac55 223 ref->ctx = vfs_context_create(vfs_context_current());
3a60a9f5 224
2d21ac55 225 if ((error = vnode_open(name, (O_CREAT | FWRITE), (0), 0, &ref->vp, ref->ctx)))
3a60a9f5
A
226 goto out;
227
6d2010ae
A
228 if (addr && len)
229 {
230 if ((error = kern_write_file(ref, offset, addr, len)))
231 goto out;
232 }
233
3a60a9f5
A
234 VATTR_INIT(&va);
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);
239 error = EFAULT;
2d21ac55 240 if (vnode_getattr(ref->vp, &va, ref->ctx))
3a60a9f5
A
241 goto out;
242
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);
246
247 if (ref->vp->v_type == VREG)
248 {
249 /* Don't dump files with links. */
250 if (va.va_nlink != 1)
251 goto out;
252
253 device = va.va_fsid;
b0d623f7 254 p1 = &device;
3a60a9f5
A
255 p2 = p;
256 do_ioctl = &file_ioctl;
257 }
258 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
259 {
260 /* Partition. */
261 device = va.va_rdev;
262
263 p1 = ref->vp;
2d21ac55 264 p2 = ref->ctx;
3a60a9f5
A
265 do_ioctl = &device_ioctl;
266 }
267 else
268 {
269 /* Don't dump to non-regular files. */
270 error = EFAULT;
271 goto out;
272 }
6d2010ae
A
273 ref->device = device;
274
6d2010ae
A
275 // get block size
276
7ddcb079 277 error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &ref->blksize);
6d2010ae
A
278 if (error)
279 goto out;
280
281 if (ref->vp->v_type == VREG)
7ddcb079 282 ref->filelength = va.va_data_size;
6d2010ae
A
283 else
284 {
285 error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &fileblk);
286 if (error)
287 goto out;
7ddcb079 288 ref->filelength = fileblk * ref->blksize;
6d2010ae
A
289 }
290
316670eb
A
291 // pin logical extents
292
293 error = kern_ioctl_file_extents(ref, _DKIOCCSPINEXTENT, 0, ref->filelength);
294 if (error && (ENOTTY != error)) goto out;
295 ref->pinned = (error == 0);
296
297 // generate the block list
298
299 error = do_ioctl(p1, p2, DKIOCLOCKPHYSICALEXTENTS, NULL);
300 if (error)
301 goto out;
302 locked = TRUE;
303
6d2010ae 304 f_offset = 0;
7ddcb079 305 while (f_offset < ref->filelength)
6d2010ae
A
306 {
307 if (ref->vp->v_type == VREG)
308 {
309 filechunk = 1*1024*1024*1024;
310 daddr64_t blkno;
311
312 error = VNOP_BLOCKMAP(ref->vp, f_offset, filechunk, &blkno, &filechunk, NULL, 0, NULL);
313 if (error)
314 goto out;
315
7ddcb079 316 fileblk = blkno * ref->blksize;
6d2010ae
A
317 }
318 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
319 {
320 fileblk = f_offset;
7ddcb079 321 filechunk = f_offset ? 0 : ref->filelength;
6d2010ae
A
322 }
323
324 physoffset = 0;
325 while (physoffset < filechunk)
326 {
327 dk_physical_extent_t getphysreq;
328 bzero(&getphysreq, sizeof(getphysreq));
329
330 getphysreq.offset = fileblk + physoffset;
331 getphysreq.length = (filechunk - physoffset);
332 error = do_ioctl(p1, p2, DKIOCGETPHYSICALEXTENT, (caddr_t) &getphysreq);
333 if (error)
334 goto out;
335 if (!target)
336 {
337 target = getphysreq.dev;
338 }
339 else if (target != getphysreq.dev)
340 {
341 error = ENOTSUP;
342 goto out;
343 }
344 callback(callback_ref, getphysreq.offset, getphysreq.length);
345 physoffset += getphysreq.length;
346 }
347 f_offset += filechunk;
348 }
349 callback(callback_ref, 0ULL, 0ULL);
350
351 if (ref->vp->v_type == VREG)
352 p1 = &target;
3a60a9f5
A
353
354 // get partition base
355
356 error = do_ioctl(p1, p2, DKIOCGETBASE, (caddr_t) partitionbase_result);
357 if (error)
358 goto out;
359
360 // get block size & constraints
361
362 error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &blksize);
363 if (error)
364 goto out;
365
366 maxiocount = 1*1024*1024*1024;
367
368 error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t) &count);
369 if (error)
370 count = 0;
371 count *= blksize;
372 if (count && (count < maxiocount))
373 maxiocount = count;
374
375 error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t) &count);
376 if (error)
377 count = 0;
378 count *= blksize;
379 if (count && (count < maxiocount))
380 maxiocount = count;
381
382 error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTREAD, (caddr_t) &count);
383 if (error)
384 count = 0;
385 if (count && (count < maxiocount))
386 maxiocount = count;
387
388 error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t) &count);
389 if (error)
390 count = 0;
391 if (count && (count < maxiocount))
392 maxiocount = count;
393
394 error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTREAD, (caddr_t) &count);
395 if (error)
396 count = 0;
397 if (count && (count < maxiocount))
398 maxiocount = count;
399
400 error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTWRITE, (caddr_t) &count);
401 if (error)
402 count = 0;
403 if (count && (count < maxiocount))
404 maxiocount = count;
405
406 kprintf("max io 0x%qx bytes\n", maxiocount);
407 if (maxiocount_result)
408 *maxiocount_result = maxiocount;
409
6d2010ae
A
410 error = do_ioctl(p1, p2, DKIOCISSOLIDSTATE, (caddr_t)&isssd);
411 if (!error && isssd)
412 flags |= kIOHibernateOptionSSD;
3a60a9f5 413
6d2010ae
A
414 if (partition_device_result)
415 *partition_device_result = device;
416 if (image_device_result)
417 *image_device_result = target;
418 if (flags)
419 *oflags = flags;
3a60a9f5
A
420
421out:
422 kprintf("kern_open_file_for_direct_io(%d)\n", error);
423
6d2010ae
A
424 if (error && locked)
425 {
426 p1 = &device;
427 (void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL);
428 }
429
430 if (error && ref)
431 {
432 if (ref->vp)
433 {
2d21ac55
A
434 vnode_close(ref->vp, FWRITE, ref->ctx);
435 ref->vp = NULLVP;
436 }
2d21ac55
A
437 vfs_context_rele(ref->ctx);
438 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
439 ref = NULL;
3a60a9f5 440 }
3a60a9f5
A
441 return(ref);
442}
443
444int
445kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len)
446{
447 return (vn_rdwr(UIO_WRITE, ref->vp,
448 addr, len, offset,
b0d623f7 449 UIO_SYSSPACE, IO_SYNC|IO_NODELOCKED|IO_UNIT,
2d21ac55
A
450 vfs_context_ucred(ref->ctx), (int *) 0,
451 vfs_context_proc(ref->ctx)));
3a60a9f5
A
452}
453
7ddcb079 454
3a60a9f5 455void
6d2010ae 456kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
7ddcb079
A
457 off_t write_offset, caddr_t addr, vm_size_t write_length,
458 off_t discard_offset, off_t discard_end)
3a60a9f5 459{
6d2010ae 460 int error;
3a60a9f5
A
461 kprintf("kern_close_file_for_direct_io\n");
462
6d2010ae 463 if (!ref) return;
3a60a9f5 464
6d2010ae
A
465 if (ref->vp)
466 {
467 int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
468 void * p1;
469 void * p2;
470
471 if (ref->vp->v_type == VREG)
472 {
473 p1 = &ref->device;
474 p2 = kernproc;
475 do_ioctl = &file_ioctl;
476 }
477 else
478 {
479 /* Partition. */
480 p1 = ref->vp;
481 p2 = ref->ctx;
482 do_ioctl = &device_ioctl;
483 }
484 (void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL);
485
7ddcb079
A
486 if (addr && write_length)
487 {
488 (void) kern_write_file(ref, write_offset, addr, write_length);
489 }
316670eb 490 if (discard_offset && discard_end && !ref->pinned)
6d2010ae 491 {
316670eb 492 (void) kern_ioctl_file_extents(ref, DKIOCUNMAP, discard_offset, discard_end);
6d2010ae
A
493 }
494
495 error = vnode_close(ref->vp, FWRITE, ref->ctx);
496
497 ref->vp = NULLVP;
498 kprintf("vnode_close(%d)\n", error);
3a60a9f5 499 }
6d2010ae
A
500 vfs_context_rele(ref->ctx);
501 ref->ctx = NULL;
502 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
3a60a9f5 503}
2d21ac55 504