]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_symfile.c
xnu-1504.9.17.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{
b0d623f7 82 dev_t device = *(dev_t*) p1;
3a60a9f5
A
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,
0b4c1975
A
99 uint64_t * maxiocount_result,
100 boolean_t * solid_state)
3a60a9f5
A
101{
102 struct kern_direct_file_io_ref_t * ref;
103
2d21ac55 104 proc_t p;
3a60a9f5
A
105 struct vnode_attr va;
106 int error;
107 off_t f_offset;
108 uint32_t blksize;
109 uint64_t size;
110 dev_t device;
111 off_t maxiocount, count;
112
113 int (*do_ioctl)(void * p1, void * p2, int theIoctl, caddr_t result);
114 void * p1;
115 void * p2;
116
117 error = EFAULT;
118
119 ref = (struct kern_direct_file_io_ref_t *) kalloc(sizeof(struct kern_direct_file_io_ref_t));
120 if (!ref)
121 {
122 error = EFAULT;
123 goto out;
124 }
125
126 ref->vp = NULL;
127 p = current_proc(); // kernproc;
2d21ac55 128 ref->ctx = vfs_context_create(vfs_context_current());
3a60a9f5 129
2d21ac55 130 if ((error = vnode_open(name, (O_CREAT | FWRITE), (0), 0, &ref->vp, ref->ctx)))
3a60a9f5
A
131 goto out;
132
133 VATTR_INIT(&va);
134 VATTR_WANTED(&va, va_rdev);
135 VATTR_WANTED(&va, va_fsid);
136 VATTR_WANTED(&va, va_data_size);
137 VATTR_WANTED(&va, va_nlink);
138 error = EFAULT;
2d21ac55 139 if (vnode_getattr(ref->vp, &va, ref->ctx))
3a60a9f5
A
140 goto out;
141
142 kprintf("vp va_rdev major %d minor %d\n", major(va.va_rdev), minor(va.va_rdev));
143 kprintf("vp va_fsid major %d minor %d\n", major(va.va_fsid), minor(va.va_fsid));
144 kprintf("vp size %qd\n", va.va_data_size);
145
146 if (ref->vp->v_type == VREG)
147 {
148 /* Don't dump files with links. */
149 if (va.va_nlink != 1)
150 goto out;
151
152 device = va.va_fsid;
b0d623f7 153 p1 = &device;
3a60a9f5
A
154 p2 = p;
155 do_ioctl = &file_ioctl;
156 }
157 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
158 {
159 /* Partition. */
160 device = va.va_rdev;
161
162 p1 = ref->vp;
2d21ac55 163 p2 = ref->ctx;
3a60a9f5
A
164 do_ioctl = &device_ioctl;
165 }
166 else
167 {
168 /* Don't dump to non-regular files. */
169 error = EFAULT;
170 goto out;
171 }
172
173 // get partition base
174
175 error = do_ioctl(p1, p2, DKIOCGETBASE, (caddr_t) partitionbase_result);
176 if (error)
177 goto out;
178
179 // get block size & constraints
180
181 error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &blksize);
182 if (error)
183 goto out;
184
185 maxiocount = 1*1024*1024*1024;
186
187 error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t) &count);
188 if (error)
189 count = 0;
190 count *= blksize;
191 if (count && (count < maxiocount))
192 maxiocount = count;
193
194 error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t) &count);
195 if (error)
196 count = 0;
197 count *= blksize;
198 if (count && (count < maxiocount))
199 maxiocount = count;
200
201 error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTREAD, (caddr_t) &count);
202 if (error)
203 count = 0;
204 if (count && (count < maxiocount))
205 maxiocount = count;
206
207 error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t) &count);
208 if (error)
209 count = 0;
210 if (count && (count < maxiocount))
211 maxiocount = count;
212
213 error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTREAD, (caddr_t) &count);
214 if (error)
215 count = 0;
216 if (count && (count < maxiocount))
217 maxiocount = count;
218
219 error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTWRITE, (caddr_t) &count);
220 if (error)
221 count = 0;
222 if (count && (count < maxiocount))
223 maxiocount = count;
224
225 kprintf("max io 0x%qx bytes\n", maxiocount);
226 if (maxiocount_result)
227 *maxiocount_result = maxiocount;
228
0b4c1975
A
229 if (solid_state)
230 {
231 int isssd = 0;
232 error = do_ioctl(p1, p2, DKIOCISSOLIDSTATE, (caddr_t)&isssd);
233 if (error)
234 *solid_state = FALSE;
235 else
236 *solid_state = isssd;
237 }
238
3a60a9f5
A
239 // generate the block list
240
241 error = 0;
242 if (ref->vp->v_type == VREG)
243 {
244 f_offset = 0;
245 while(f_offset < (off_t) va.va_data_size)
246 {
247 size_t io_size = 1*1024*1024*1024;
248 daddr64_t blkno;
249
250 error = VNOP_BLOCKMAP(ref->vp, f_offset, io_size, &blkno, (size_t *)&io_size, NULL, 0, NULL);
251 if (error)
252 goto out;
253 callback(callback_ref, ((uint64_t) blkno) * blksize, (uint64_t) io_size);
254 f_offset += io_size;
255 }
256 callback(callback_ref, 0ULL, 0ULL);
257 }
258 else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR))
259 {
260 error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &size);
261 if (error)
262 goto out;
263 size *= blksize;
264 callback(callback_ref, 0ULL, size);
265 callback(callback_ref, size, 0ULL);
266 }
267
268 if (device_result)
269 *device_result = device;
270
271out:
272 kprintf("kern_open_file_for_direct_io(%d)\n", error);
273
274 if (error && ref) {
2d21ac55
A
275 if (ref->vp) {
276 vnode_close(ref->vp, FWRITE, ref->ctx);
277 ref->vp = NULLVP;
278 }
279
280 vfs_context_rele(ref->ctx);
281 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
282 ref = NULL;
3a60a9f5
A
283 }
284
285 return(ref);
286}
287
288int
289kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, caddr_t addr, vm_size_t len)
290{
291 return (vn_rdwr(UIO_WRITE, ref->vp,
292 addr, len, offset,
b0d623f7 293 UIO_SYSSPACE, IO_SYNC|IO_NODELOCKED|IO_UNIT,
2d21ac55
A
294 vfs_context_ucred(ref->ctx), (int *) 0,
295 vfs_context_proc(ref->ctx)));
3a60a9f5
A
296}
297
298void
299kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref)
300{
301 kprintf("kern_close_file_for_direct_io\n");
302
303 if (ref) {
304 int error;
305
306 if (ref->vp) {
2d21ac55
A
307 error = vnode_close(ref->vp, FWRITE, ref->ctx);
308 ref->vp = NULLVP;
3a60a9f5
A
309 kprintf("vnode_close(%d)\n", error);
310 }
2d21ac55
A
311 vfs_context_rele(ref->ctx);
312 ref->ctx = NULL;
3a60a9f5
A
313 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
314 }
315}
2d21ac55 316