]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
3 | * | |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 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. | |
14 | * | |
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 | |
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 | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
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 | * | |
91447636 A |
32 | * This file contains creates a dummy symbol file for mach_kernel |
33 | * based on the symbol table information passed by the | |
34 | * SecondaryLoader/PlatformExpert. This allows us to correctly | |
35 | * link other executables (drivers, etc) against the the kernel in | |
36 | * cases where the kernel image on the root device does not match | |
37 | * the live kernel. This can occur during net-booting where the | |
38 | * actual kernel image is obtained from the network via tftp rather | |
39 | * than the root device. | |
1c79356b | 40 | * |
91447636 A |
41 | * If a symbol table is available, then the file /mach.sym will be |
42 | * created containing a Mach Header and a LC_SYMTAB load command | |
43 | * followed by the the symbol table data for mach_kernel. | |
44 | * | |
45 | * NOTE: This file supports only 32 bit kernels at the present time; | |
46 | * adding support for 64 bit kernels is possible, but is not | |
47 | * necessary at the present time. | |
1c79356b A |
48 | * |
49 | * HISTORY | |
50 | * | |
51 | * . | |
52 | */ | |
53 | ||
54 | #include <mach/vm_param.h> | |
55 | ||
56 | #include <sys/param.h> | |
57 | #include <sys/systm.h> | |
58 | #include <sys/signalvar.h> | |
59 | #include <sys/resourcevar.h> | |
60 | #include <sys/namei.h> | |
91447636 A |
61 | #include <sys/vnode_internal.h> |
62 | #include <sys/proc_internal.h> | |
63 | #include <sys/kauth.h> | |
1c79356b A |
64 | #include <sys/timeb.h> |
65 | #include <sys/times.h> | |
1c79356b | 66 | #include <sys/acct.h> |
91447636 | 67 | #include <sys/file_internal.h> |
1c79356b A |
68 | #include <sys/uio.h> |
69 | #include <sys/kernel.h> | |
70 | #include <sys/stat.h> | |
91447636 A |
71 | #include <sys/disk.h> |
72 | #include <sys/conf.h> | |
1c79356b A |
73 | |
74 | #include <mach-o/loader.h> | |
75 | #include <mach-o/nlist.h> | |
76 | ||
91447636 | 77 | #include <kern/kalloc.h> |
1c79356b | 78 | #include <vm/vm_kern.h> |
91447636 | 79 | #include <pexpert/pexpert.h> |
3a60a9f5 | 80 | #include <IOKit/IOHibernatePrivate.h> |
1c79356b A |
81 | |
82 | extern unsigned char rootdevice[]; | |
0b4e3aa0 | 83 | extern struct mach_header _mh_execute_header; |
1c79356b | 84 | |
0b4e3aa0 A |
85 | static int kernel_symfile_opened = 0; |
86 | static int error_code = 0; | |
1c79356b | 87 | |
0b4e3aa0 A |
88 | extern int IODTGetLoaderInfo(char *key, void **infoAddr, int *infoSize); |
89 | extern void IODTFreeLoaderInfo(char *key, void *infoAddr, int infoSize); | |
1c79356b A |
90 | |
91 | /* | |
91447636 | 92 | * Can only operate against currently running 32 bit mach_kernel |
1c79356b | 93 | */ |
91447636 A |
94 | static int |
95 | output_kernel_symbols(struct proc *p) | |
1c79356b | 96 | { |
0b4e3aa0 | 97 | struct vnode *vp; |
21362eb3 | 98 | kauth_cred_t cred = p->p_ucred; /* XXX */ |
91447636 A |
99 | struct vnode_attr va; |
100 | struct vfs_context context; | |
0b4e3aa0 A |
101 | struct load_command *cmd; |
102 | struct mach_header *orig_mh, *mh; | |
103 | struct segment_command *orig_ds, *orig_ts, *orig_le, *sg; | |
104 | struct section *se, *const_text; | |
105 | struct symtab_command *st, *orig_st; | |
106 | struct nlist *sym; | |
107 | vm_size_t orig_mhsize, orig_st_size; | |
108 | vm_offset_t header; | |
91447636 | 109 | vm_size_t header_size = 0; /* out: protected by header */ |
0b4e3aa0 | 110 | int error, error1; |
91447636 | 111 | unsigned int i, j; |
0b4e3aa0 A |
112 | caddr_t addr; |
113 | vm_offset_t offset; | |
114 | int rc_mh, rc_sc; | |
115 | ||
116 | error = EFAULT; | |
117 | ||
118 | vp = NULL; | |
119 | header = NULL; | |
120 | orig_mh = NULL; | |
121 | orig_st = NULL; | |
122 | ||
123 | // Dispose of unnecessary gumf, the booter doesn't need to load these | |
124 | rc_mh = IODTGetLoaderInfo("Kernel-__HEADER", | |
125 | (void **)&orig_mh, &orig_mhsize); | |
55e303ae | 126 | if (rc_mh == 0 && orig_mh) |
0b4e3aa0 | 127 | IODTFreeLoaderInfo("Kernel-__HEADER", |
55e303ae | 128 | (void *)orig_mh, round_page_32(orig_mhsize)); |
0b4e3aa0 A |
129 | |
130 | rc_sc = IODTGetLoaderInfo("Kernel-__SYMTAB", | |
131 | (void **) &orig_st, &orig_st_size); | |
55e303ae | 132 | if (rc_sc == 0 && orig_st) |
0b4e3aa0 | 133 | IODTFreeLoaderInfo("Kernel-__SYMTAB", |
55e303ae | 134 | (void *)orig_st, round_page_32(orig_st_size)); |
0b4e3aa0 | 135 | |
91447636 | 136 | if (cred->cr_svuid != cred->cr_ruid || cred->cr_svgid != cred->cr_rgid) |
0b4e3aa0 A |
137 | goto out; |
138 | ||
139 | // Check to see if the root is 'e' or 'n', is this a test for network? | |
140 | if (rootdevice[0] == 'e' && rootdevice[1] == 'n') | |
141 | goto out; | |
142 | ||
21362eb3 A |
143 | context.vc_proc = p; |
144 | context.vc_ucred = cred; | |
145 | ||
91447636 A |
146 | if ((error = vnode_open("mach.sym", (O_CREAT | FWRITE), (S_IRUSR | S_IRGRP | S_IROTH), 0, &vp, &context))) |
147 | goto out; | |
0b4e3aa0 | 148 | |
0b4e3aa0 A |
149 | /* Don't dump to non-regular files or files with links. */ |
150 | error = EFAULT; | |
91447636 A |
151 | VATTR_INIT(&va); |
152 | VATTR_WANTED(&va, va_nlink); | |
153 | if ((vp->v_type != VREG) || vnode_getattr(vp, &va, &context) || (va.va_nlink != 1)) | |
0b4e3aa0 A |
154 | goto out; |
155 | ||
91447636 A |
156 | VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_getsize */ |
157 | VATTR_SET(&va, va_data_size, 0); | |
158 | vnode_setattr(vp, &va, &context); | |
0b4e3aa0 A |
159 | p->p_acflag |= ACORE; |
160 | ||
161 | // If the file type is MH_EXECUTE then this must be a kernel | |
162 | // as all Kernel extensions must be of type MH_OBJECT | |
163 | orig_ds = orig_ts = orig_le = NULL; | |
164 | orig_st = NULL; | |
165 | orig_mh = &_mh_execute_header; | |
166 | cmd = (struct load_command *) &orig_mh[1]; | |
167 | for (i = 0; i < orig_mh->ncmds; i++) { | |
168 | if (cmd->cmd == LC_SEGMENT) { | |
91447636 | 169 | struct segment_command *orig_sg = (struct segment_command *) cmd; |
0b4e3aa0 | 170 | |
91447636 A |
171 | if (!strcmp(SEG_TEXT, orig_sg->segname)) |
172 | orig_ts = orig_sg; | |
173 | else if (!strcmp(SEG_DATA, orig_sg->segname)) | |
174 | orig_ds = orig_sg; | |
175 | else if (!strcmp(SEG_LINKEDIT, orig_sg->segname)) | |
176 | orig_le = orig_sg; | |
0b4e3aa0 A |
177 | } |
178 | else if (cmd->cmd == LC_SYMTAB) | |
179 | orig_st = (struct symtab_command *) cmd; | |
180 | ||
181 | cmd = (struct load_command *) ((caddr_t) cmd + cmd->cmdsize); | |
1c79356b | 182 | } |
1c79356b | 183 | |
0b4e3aa0 A |
184 | if (!orig_ts || !orig_ds || !orig_le || !orig_st) |
185 | goto out; | |
1c79356b | 186 | |
0b4e3aa0 A |
187 | const_text = NULL; |
188 | se = (struct section *) &orig_ts[1]; | |
189 | for (i = 0; i < orig_ts->nsects; i++, se++) { | |
190 | if (!strcmp("__const", se->sectname)) { | |
191 | const_text = se; | |
192 | break; | |
193 | } | |
194 | } | |
195 | if (!const_text) | |
196 | goto out; | |
197 | ||
198 | header_size = sizeof(struct mach_header) | |
199 | + orig_ts->cmdsize | |
200 | + orig_ds->cmdsize | |
201 | + sizeof(struct symtab_command); | |
202 | ||
91447636 | 203 | (void) kmem_alloc(kernel_map, |
0b4e3aa0 A |
204 | (vm_offset_t *) &header, |
205 | (vm_size_t) header_size); | |
206 | if (header) | |
207 | bzero((void *) header, header_size); | |
208 | else | |
209 | goto out; | |
210 | ||
211 | /* | |
212 | * Set up Mach-O header. | |
213 | */ | |
214 | mh = (struct mach_header *) header; | |
215 | mh->magic = orig_mh->magic; | |
216 | mh->cputype = orig_mh->cputype; | |
217 | mh->cpusubtype = orig_mh->cpusubtype; | |
218 | mh->filetype = orig_mh->filetype; | |
219 | mh->ncmds = 3; | |
220 | mh->sizeofcmds = header_size - sizeof(struct mach_header); | |
221 | mh->flags = orig_mh->flags; | |
222 | ||
223 | // Initialise the current file offset and addr | |
91447636 | 224 | offset = round_page(header_size); |
0b4e3aa0 A |
225 | addr = (caddr_t) const_text->addr; // Load address of __TEXT,__const |
226 | ||
227 | /* | |
228 | * Construct a TEXT segment load command | |
229 | * the only part of the TEXT segment we keep is the __TEXT,__const | |
230 | * which contains the kernel vtables. | |
231 | */ | |
232 | sg = (struct segment_command *) &mh[1]; | |
233 | bcopy(orig_ts, sg, orig_ts->cmdsize); | |
234 | sg->vmaddr = (unsigned long) addr; | |
235 | sg->vmsize = const_text->size; | |
236 | sg->fileoff = 0; | |
91447636 | 237 | sg->filesize = const_text->size + round_page(header_size); |
0b4e3aa0 A |
238 | sg->maxprot = 0; |
239 | sg->initprot = 0; | |
240 | sg->flags = 0; | |
241 | se = (struct section *)(sg+1); | |
242 | for ( j = 0; j < sg->nsects; j++, se++ ) { | |
243 | se->addr = (unsigned long) addr; | |
244 | se->size = 0; | |
245 | se->offset = offset; | |
246 | se->nreloc = 0; | |
247 | if (!strcmp("__const", se->sectname)) { | |
248 | se->size = const_text->size; | |
249 | addr += const_text->size; | |
250 | offset += const_text->size; | |
251 | const_text = se; | |
252 | } | |
253 | } | |
91447636 | 254 | offset = round_page(offset); |
0b4e3aa0 A |
255 | |
256 | // Now copy of the __DATA segment load command, the image need | |
257 | // not be stored to disk nobody needs it, yet! | |
258 | sg = (struct segment_command *)((int)sg + sg->cmdsize); | |
259 | bcopy(orig_ds, sg, orig_ds->cmdsize); | |
260 | ||
261 | sg->vmaddr = (unsigned long) addr; | |
262 | sg->vmsize = 0x1000; // One page for some reason? | |
263 | sg->fileoff = offset; | |
264 | sg->filesize = 0; | |
265 | sg->maxprot = 0; | |
266 | sg->initprot = 0; | |
267 | sg->flags = 0; | |
268 | se = (struct section *)(sg+1); | |
269 | for ( j = 0; j < sg->nsects; j++, se++ ) { | |
270 | se->addr = (unsigned long) addr; | |
271 | se->size = 0; | |
272 | se->offset = offset; | |
273 | se->nreloc = 0; | |
274 | } | |
91447636 | 275 | offset = round_page(offset); |
0b4e3aa0 A |
276 | |
277 | ||
278 | /* | |
279 | * Set up LC_SYMTAB command | |
280 | */ | |
281 | st = (struct symtab_command *)((int)sg + sg->cmdsize); | |
282 | st->cmd = LC_SYMTAB; | |
283 | st->cmdsize = sizeof(struct symtab_command); | |
284 | st->symoff = offset; | |
285 | st->nsyms = orig_st->nsyms; | |
286 | st->strsize = orig_st->strsize; | |
287 | st->stroff = offset + st->nsyms * sizeof(struct nlist); | |
288 | ||
289 | /* | |
290 | * Convert the symbol table in place from section references | |
291 | * to absolute references. | |
292 | */ | |
293 | sym = (struct nlist *) orig_le->vmaddr; | |
294 | for (i = 0; i < st->nsyms; i++, sym++ ) { | |
295 | if ( (sym->n_type & N_TYPE) == N_SECT) { | |
296 | sym->n_sect = NO_SECT; | |
297 | sym->n_type = (sym->n_type & ~N_TYPE) | N_ABS; | |
298 | } | |
299 | } | |
300 | ||
301 | /* | |
302 | * Write out the load commands at the beginning of the file. | |
303 | */ | |
304 | error = vn_rdwr(UIO_WRITE, vp, (caddr_t) mh, header_size, (off_t) 0, | |
91447636 | 305 | UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); |
0b4e3aa0 A |
306 | if (error) |
307 | goto out; | |
308 | ||
309 | /* | |
310 | * Write out the __TEXT,__const data segment. | |
311 | */ | |
312 | error = vn_rdwr(UIO_WRITE, vp, (caddr_t) const_text->addr, | |
313 | const_text->size, const_text->offset, | |
91447636 | 314 | UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); |
0b4e3aa0 A |
315 | if (error) |
316 | goto out; | |
317 | ||
318 | /* | |
319 | * Write out kernel symbols | |
320 | */ | |
321 | offset = st->nsyms * sizeof(struct nlist) + st->strsize; // symtab size | |
322 | error = vn_rdwr(UIO_WRITE, vp, | |
323 | (caddr_t) orig_le->vmaddr, offset, st->symoff, | |
91447636 | 324 | UIO_SYSSPACE32, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p); |
1c79356b | 325 | out: |
0b4e3aa0 A |
326 | if (header) |
327 | kmem_free(kernel_map, header, header_size); | |
328 | ||
329 | if (vp) { | |
91447636 | 330 | error1 = vnode_close(vp, FWRITE, &context); |
0b4e3aa0 A |
331 | if (!error) error = error1; |
332 | } | |
1c79356b | 333 | |
0b4e3aa0 A |
334 | return(error); |
335 | } | |
1c79356b A |
336 | /* |
337 | * | |
338 | */ | |
0b4e3aa0 | 339 | int get_kernel_symfile(struct proc *p, char **symfile) |
1c79356b | 340 | { |
0b4e3aa0 A |
341 | if (!kernel_symfile_opened) { |
342 | kernel_symfile_opened = 1; | |
343 | error_code = output_kernel_symbols(p); | |
1c79356b | 344 | } |
0b4e3aa0 A |
345 | if (!error_code) |
346 | *symfile = "\\mach.sym"; | |
1c79356b | 347 | |
0b4e3aa0 A |
348 | return error_code; |
349 | } | |
91447636 | 350 | |
3a60a9f5 A |
351 | struct kern_direct_file_io_ref_t |
352 | { | |
353 | struct vfs_context context; | |
354 | struct vnode *vp; | |
355 | }; | |
356 | ||
357 | ||
358 | static int file_ioctl(void * p1, void * p2, int theIoctl, caddr_t result) | |
359 | { | |
360 | dev_t device = (dev_t) p1; | |
361 | ||
362 | return ((*bdevsw[major(device)].d_ioctl) | |
363 | (device, theIoctl, result, S_IFBLK, p2)); | |
364 | } | |
365 | ||
366 | static int device_ioctl(void * p1, __unused void * p2, int theIoctl, caddr_t result) | |
367 | { | |
368 | return (VNOP_IOCTL(p1, theIoctl, result, 0, p2)); | |
369 | } | |
370 | ||
371 | struct kern_direct_file_io_ref_t * | |
372 | kern_open_file_for_direct_io(const char * name, | |
373 | kern_get_file_extents_callback_t callback, | |
374 | void * callback_ref, | |
375 | dev_t * device_result, | |
376 | uint64_t * partitionbase_result, | |
377 | uint64_t * maxiocount_result) | |
378 | { | |
379 | struct kern_direct_file_io_ref_t * ref; | |
380 | ||
381 | struct proc *p; | |
21362eb3 | 382 | struct ucred *cred; |
3a60a9f5 A |
383 | struct vnode_attr va; |
384 | int error; | |
385 | off_t f_offset; | |
386 | uint32_t blksize; | |
387 | uint64_t size; | |
388 | dev_t device; | |
389 | off_t maxiocount, count; | |
390 | ||
391 | int (*do_ioctl)(void * p1, void * p2, int theIoctl, caddr_t result); | |
392 | void * p1; | |
393 | void * p2; | |
394 | ||
395 | error = EFAULT; | |
396 | ||
397 | ref = (struct kern_direct_file_io_ref_t *) kalloc(sizeof(struct kern_direct_file_io_ref_t)); | |
398 | if (!ref) | |
399 | { | |
400 | error = EFAULT; | |
401 | goto out; | |
402 | } | |
403 | ||
404 | ref->vp = NULL; | |
405 | p = current_proc(); // kernproc; | |
21362eb3 | 406 | cred = p->p_ucred; |
3a60a9f5 | 407 | ref->context.vc_proc = p; |
21362eb3 | 408 | ref->context.vc_ucred = cred; |
3a60a9f5 A |
409 | |
410 | if ((error = vnode_open(name, (O_CREAT | FWRITE), (0), 0, &ref->vp, &ref->context))) | |
411 | goto out; | |
412 | ||
413 | VATTR_INIT(&va); | |
414 | VATTR_WANTED(&va, va_rdev); | |
415 | VATTR_WANTED(&va, va_fsid); | |
416 | VATTR_WANTED(&va, va_data_size); | |
417 | VATTR_WANTED(&va, va_nlink); | |
418 | error = EFAULT; | |
419 | if (vnode_getattr(ref->vp, &va, &ref->context)) | |
420 | goto out; | |
421 | ||
422 | kprintf("vp va_rdev major %d minor %d\n", major(va.va_rdev), minor(va.va_rdev)); | |
423 | kprintf("vp va_fsid major %d minor %d\n", major(va.va_fsid), minor(va.va_fsid)); | |
424 | kprintf("vp size %qd\n", va.va_data_size); | |
425 | ||
426 | if (ref->vp->v_type == VREG) | |
427 | { | |
428 | /* Don't dump files with links. */ | |
429 | if (va.va_nlink != 1) | |
430 | goto out; | |
431 | ||
432 | device = va.va_fsid; | |
433 | p1 = (void *) device; | |
434 | p2 = p; | |
435 | do_ioctl = &file_ioctl; | |
436 | } | |
437 | else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR)) | |
438 | { | |
439 | /* Partition. */ | |
440 | device = va.va_rdev; | |
441 | ||
442 | p1 = ref->vp; | |
443 | p2 = &ref->context; | |
444 | do_ioctl = &device_ioctl; | |
445 | } | |
446 | else | |
447 | { | |
448 | /* Don't dump to non-regular files. */ | |
449 | error = EFAULT; | |
450 | goto out; | |
451 | } | |
452 | ||
453 | // get partition base | |
454 | ||
455 | error = do_ioctl(p1, p2, DKIOCGETBASE, (caddr_t) partitionbase_result); | |
456 | if (error) | |
457 | goto out; | |
458 | ||
459 | // get block size & constraints | |
460 | ||
461 | error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &blksize); | |
462 | if (error) | |
463 | goto out; | |
464 | ||
465 | maxiocount = 1*1024*1024*1024; | |
466 | ||
467 | error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t) &count); | |
468 | if (error) | |
469 | count = 0; | |
470 | count *= blksize; | |
471 | if (count && (count < maxiocount)) | |
472 | maxiocount = count; | |
473 | ||
474 | error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t) &count); | |
475 | if (error) | |
476 | count = 0; | |
477 | count *= blksize; | |
478 | if (count && (count < maxiocount)) | |
479 | maxiocount = count; | |
480 | ||
481 | error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTREAD, (caddr_t) &count); | |
482 | if (error) | |
483 | count = 0; | |
484 | if (count && (count < maxiocount)) | |
485 | maxiocount = count; | |
486 | ||
487 | error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t) &count); | |
488 | if (error) | |
489 | count = 0; | |
490 | if (count && (count < maxiocount)) | |
491 | maxiocount = count; | |
492 | ||
493 | error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTREAD, (caddr_t) &count); | |
494 | if (error) | |
495 | count = 0; | |
496 | if (count && (count < maxiocount)) | |
497 | maxiocount = count; | |
498 | ||
499 | error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTWRITE, (caddr_t) &count); | |
500 | if (error) | |
501 | count = 0; | |
502 | if (count && (count < maxiocount)) | |
503 | maxiocount = count; | |
504 | ||
505 | kprintf("max io 0x%qx bytes\n", maxiocount); | |
506 | if (maxiocount_result) | |
507 | *maxiocount_result = maxiocount; | |
508 | ||
509 | // generate the block list | |
510 | ||
511 | error = 0; | |
512 | if (ref->vp->v_type == VREG) | |
513 | { | |
514 | f_offset = 0; | |
515 | while(f_offset < (off_t) va.va_data_size) | |
516 | { | |
517 | size_t io_size = 1*1024*1024*1024; | |
518 | daddr64_t blkno; | |
519 | ||
520 | error = VNOP_BLOCKMAP(ref->vp, f_offset, io_size, &blkno, (size_t *)&io_size, NULL, 0, NULL); | |
521 | if (error) | |
522 | goto out; | |
523 | callback(callback_ref, ((uint64_t) blkno) * blksize, (uint64_t) io_size); | |
524 | f_offset += io_size; | |
525 | } | |
526 | callback(callback_ref, 0ULL, 0ULL); | |
527 | } | |
528 | else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR)) | |
529 | { | |
530 | error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &size); | |
531 | if (error) | |
532 | goto out; | |
533 | size *= blksize; | |
534 | callback(callback_ref, 0ULL, size); | |
535 | callback(callback_ref, size, 0ULL); | |
536 | } | |
537 | ||
538 | if (device_result) | |
539 | *device_result = device; | |
540 | ||
541 | out: | |
542 | kprintf("kern_open_file_for_direct_io(%d)\n", error); | |
543 | ||
544 | if (error && ref) { | |
21362eb3 A |
545 | if (ref->vp) { |
546 | vnode_close(ref->vp, FWRITE, &ref->context); | |
547 | ref->vp = NULLVP; | |
548 | } | |
549 | ||
550 | kfree(ref, sizeof(struct kern_direct_file_io_ref_t)); | |
551 | ref = NULL; | |
3a60a9f5 A |
552 | } |
553 | ||
554 | return(ref); | |
555 | } | |
556 | ||
557 | int | |
558 | kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len) | |
559 | { | |
560 | return (vn_rdwr(UIO_WRITE, ref->vp, | |
561 | addr, len, offset, | |
562 | UIO_SYSSPACE32, IO_SYNC|IO_NODELOCKED|IO_UNIT, | |
563 | ref->context.vc_ucred, (int *) 0, ref->context.vc_proc)); | |
564 | } | |
565 | ||
566 | void | |
567 | kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref) | |
568 | { | |
569 | kprintf("kern_close_file_for_direct_io\n"); | |
570 | ||
571 | if (ref) { | |
572 | int error; | |
573 | ||
574 | if (ref->vp) { | |
575 | error = vnode_close(ref->vp, FWRITE, &ref->context); | |
576 | kprintf("vnode_close(%d)\n", error); | |
8f6c56a5 | 577 | ref->vp = NULLVP; |
3a60a9f5 A |
578 | } |
579 | kfree(ref, sizeof(struct kern_direct_file_io_ref_t)); | |
580 | } | |
581 | } |