]>
Commit | Line | Data |
---|---|---|
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> | |
3e170ce0 | 54 | #include <sys/content_protection.h> |
1c79356b A |
55 | |
56 | #include <mach-o/loader.h> | |
57 | #include <mach-o/nlist.h> | |
58 | ||
91447636 | 59 | #include <kern/kalloc.h> |
1c79356b | 60 | #include <vm/vm_kern.h> |
91447636 | 61 | #include <pexpert/pexpert.h> |
3e170ce0 | 62 | #include <IOKit/IOPolledInterface.h> |
1c79356b | 63 | |
2d21ac55 A |
64 | /* This function is called from kern_sysctl in the current process context; |
65 | * it is exported with the System6.0.exports, but this appears to be a legacy | |
66 | * export, as there are no internal consumers. | |
1c79356b | 67 | */ |
2d21ac55 | 68 | int |
7ddcb079 A |
69 | get_kernel_symfile(__unused proc_t p, __unused char const **symfile); |
70 | int | |
2d21ac55 | 71 | get_kernel_symfile(__unused proc_t p, __unused char const **symfile) |
1c79356b | 72 | { |
2d21ac55 | 73 | return KERN_FAILURE; |
0b4e3aa0 | 74 | } |
91447636 | 75 | |
3a60a9f5 A |
76 | struct kern_direct_file_io_ref_t |
77 | { | |
6d2010ae A |
78 | vfs_context_t ctx; |
79 | struct vnode * vp; | |
80 | dev_t device; | |
7ddcb079 | 81 | uint32_t blksize; |
316670eb | 82 | off_t filelength; |
3e170ce0 | 83 | char cf; |
316670eb | 84 | char pinned; |
3a60a9f5 A |
85 | }; |
86 | ||
87 | ||
6d2010ae | 88 | static int file_ioctl(void * p1, void * p2, u_long theIoctl, caddr_t result) |
3a60a9f5 | 89 | { |
b0d623f7 | 90 | dev_t device = *(dev_t*) p1; |
3a60a9f5 A |
91 | |
92 | return ((*bdevsw[major(device)].d_ioctl) | |
93 | (device, theIoctl, result, S_IFBLK, p2)); | |
94 | } | |
95 | ||
6d2010ae | 96 | static int device_ioctl(void * p1, __unused void * p2, u_long theIoctl, caddr_t result) |
3a60a9f5 A |
97 | { |
98 | return (VNOP_IOCTL(p1, theIoctl, result, 0, p2)); | |
99 | } | |
100 | ||
316670eb A |
101 | static int |
102 | kern_ioctl_file_extents(struct kern_direct_file_io_ref_t * ref, u_long theIoctl, off_t offset, off_t end) | |
103 | { | |
3e170ce0 | 104 | int error = 0; |
316670eb A |
105 | int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result); |
106 | void * p1; | |
107 | void * p2; | |
108 | uint64_t fileblk; | |
109 | size_t filechunk; | |
110 | dk_extent_t extent; | |
111 | dk_unmap_t unmap; | |
112 | _dk_cs_pin_t pin; | |
113 | ||
114 | bzero(&extent, sizeof(dk_extent_t)); | |
115 | bzero(&unmap, sizeof(dk_unmap_t)); | |
116 | bzero(&pin, sizeof(pin)); | |
117 | if (ref->vp->v_type == VREG) | |
118 | { | |
119 | p1 = &ref->device; | |
120 | p2 = kernproc; | |
121 | do_ioctl = &file_ioctl; | |
122 | } | |
123 | else | |
124 | { | |
125 | /* Partition. */ | |
126 | p1 = ref->vp; | |
127 | p2 = ref->ctx; | |
128 | do_ioctl = &device_ioctl; | |
129 | } | |
3e170ce0 A |
130 | |
131 | if (_DKIOCCSPINEXTENT == theIoctl) { | |
132 | /* Tell CS the image size, so it knows whether to place the subsequent pins SSD/HDD */ | |
133 | pin.cp_extent.length = end; | |
134 | pin.cp_flags = _DKIOCCSHIBERNATEIMGSIZE; | |
135 | (void) do_ioctl(p1, p2, _DKIOCCSPINEXTENT, (caddr_t)&pin); | |
136 | } else if (_DKIOCCSUNPINEXTENT == theIoctl) { | |
137 | /* Tell CS hibernation is done, so it can stop blocking overlapping writes */ | |
138 | pin.cp_flags = _DKIOCCSPINDISCARDBLACKLIST; | |
139 | (void) do_ioctl(p1, p2, _DKIOCCSUNPINEXTENT, (caddr_t)&pin); | |
140 | } | |
141 | ||
316670eb A |
142 | while (offset < end) |
143 | { | |
144 | if (ref->vp->v_type == VREG) | |
145 | { | |
146 | daddr64_t blkno; | |
147 | filechunk = 1*1024*1024*1024; | |
148 | if (filechunk > (size_t)(end - offset)) | |
149 | filechunk = (size_t)(end - offset); | |
fe8ab488 A |
150 | error = VNOP_BLOCKMAP(ref->vp, offset, filechunk, &blkno, |
151 | &filechunk, NULL, VNODE_WRITE, NULL); | |
316670eb A |
152 | if (error) break; |
153 | fileblk = blkno * ref->blksize; | |
154 | } | |
155 | else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR)) | |
156 | { | |
157 | fileblk = offset; | |
158 | filechunk = ref->filelength; | |
159 | } | |
160 | ||
161 | if (DKIOCUNMAP == theIoctl) | |
162 | { | |
163 | extent.offset = fileblk; | |
164 | extent.length = filechunk; | |
165 | unmap.extents = &extent; | |
166 | unmap.extentsCount = 1; | |
167 | error = do_ioctl(p1, p2, theIoctl, (caddr_t)&unmap); | |
168 | // printf("DKIOCUNMAP(%d) 0x%qx, 0x%qx\n", error, extent.offset, extent.length); | |
169 | } | |
170 | else if (_DKIOCCSPINEXTENT == theIoctl) | |
171 | { | |
172 | pin.cp_extent.offset = fileblk; | |
173 | pin.cp_extent.length = filechunk; | |
fe8ab488 | 174 | pin.cp_flags = _DKIOCCSPINFORHIBERNATION; |
316670eb A |
175 | error = do_ioctl(p1, p2, theIoctl, (caddr_t)&pin); |
176 | if (error && (ENOTTY != error)) | |
177 | { | |
3e170ce0 A |
178 | printf("_DKIOCCSPINEXTENT(%d) 0x%qx, 0x%qx\n", error, pin.cp_extent.offset, pin.cp_extent.length); |
179 | } | |
180 | } | |
181 | else if (_DKIOCCSUNPINEXTENT == theIoctl) | |
182 | { | |
183 | pin.cp_extent.offset = fileblk; | |
184 | pin.cp_extent.length = filechunk; | |
185 | pin.cp_flags = _DKIOCCSPINFORHIBERNATION; | |
186 | error = do_ioctl(p1, p2, theIoctl, (caddr_t)&pin); | |
187 | if (error && (ENOTTY != error)) | |
188 | { | |
189 | printf("_DKIOCCSUNPINEXTENT(%d) 0x%qx, 0x%qx\n", error, pin.cp_extent.offset, pin.cp_extent.length); | |
316670eb A |
190 | } |
191 | } | |
192 | else error = EINVAL; | |
193 | ||
3e170ce0 | 194 | if (error) break; |
316670eb A |
195 | offset += filechunk; |
196 | } | |
197 | return (error); | |
198 | } | |
199 | ||
3e170ce0 | 200 | extern uint32_t freespace_mb(vnode_t vp); |
7ddcb079 | 201 | |
3a60a9f5 A |
202 | struct kern_direct_file_io_ref_t * |
203 | kern_open_file_for_direct_io(const char * name, | |
3e170ce0 | 204 | boolean_t create_file, |
3a60a9f5 A |
205 | kern_get_file_extents_callback_t callback, |
206 | void * callback_ref, | |
db609669 | 207 | off_t set_file_size, |
3e170ce0 | 208 | off_t fs_free_size, |
db609669 | 209 | off_t write_file_offset, |
3e170ce0 A |
210 | void * write_file_addr, |
211 | size_t write_file_len, | |
6d2010ae A |
212 | dev_t * partition_device_result, |
213 | dev_t * image_device_result, | |
3a60a9f5 | 214 | uint64_t * partitionbase_result, |
0b4c1975 | 215 | uint64_t * maxiocount_result, |
db609669 | 216 | uint32_t * oflags) |
3a60a9f5 A |
217 | { |
218 | struct kern_direct_file_io_ref_t * ref; | |
219 | ||
3e170ce0 A |
220 | proc_t p; |
221 | struct vnode_attr va; | |
222 | int error; | |
223 | off_t f_offset; | |
224 | uint64_t fileblk; | |
225 | size_t filechunk; | |
226 | uint64_t physoffset; | |
227 | dev_t device; | |
228 | dev_t target = 0; | |
229 | int isssd = 0; | |
230 | uint32_t flags = 0; | |
231 | uint32_t blksize; | |
232 | off_t maxiocount, count, segcount; | |
233 | boolean_t locked = FALSE; | |
234 | int fmode, cmode; | |
235 | struct nameidata nd; | |
236 | u_int32_t ndflags; | |
237 | off_t mpFree; | |
3a60a9f5 | 238 | |
6d2010ae A |
239 | int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result); |
240 | void * p1 = NULL; | |
241 | void * p2 = NULL; | |
3a60a9f5 A |
242 | |
243 | error = EFAULT; | |
244 | ||
245 | ref = (struct kern_direct_file_io_ref_t *) kalloc(sizeof(struct kern_direct_file_io_ref_t)); | |
246 | if (!ref) | |
247 | { | |
248 | error = EFAULT; | |
249 | goto out; | |
250 | } | |
251 | ||
316670eb | 252 | bzero(ref, sizeof(*ref)); |
6d2010ae | 253 | p = kernproc; |
3e170ce0 | 254 | ref->ctx = vfs_context_create(vfs_context_kernel()); |
3a60a9f5 | 255 | |
3e170ce0 A |
256 | fmode = (create_file) ? (O_CREAT | FWRITE) : FWRITE; |
257 | cmode = S_IRUSR | S_IWUSR; | |
258 | ndflags = NOFOLLOW; | |
259 | NDINIT(&nd, LOOKUP, OP_OPEN, ndflags, UIO_SYSSPACE, CAST_USER_ADDR_T(name), ref->ctx); | |
260 | VATTR_INIT(&va); | |
261 | VATTR_SET(&va, va_mode, cmode); | |
262 | VATTR_SET(&va, va_dataprotect_flags, VA_DP_RAWENCRYPTED); | |
263 | VATTR_SET(&va, va_dataprotect_class, PROTECTION_CLASS_D); | |
264 | if ((error = vn_open_auth(&nd, &fmode, &va))) goto out; | |
3a60a9f5 | 265 | |
3e170ce0 | 266 | ref->vp = nd.ni_vp; |
39236c6e A |
267 | if (ref->vp->v_type == VREG) |
268 | { | |
269 | vnode_lock_spin(ref->vp); | |
270 | SET(ref->vp->v_flag, VSWAP); | |
271 | vnode_unlock(ref->vp); | |
272 | } | |
273 | ||
db609669 | 274 | if (write_file_addr && write_file_len) |
6d2010ae | 275 | { |
3e170ce0 | 276 | if ((error = kern_write_file(ref, write_file_offset, write_file_addr, write_file_len, 0))) goto out; |
6d2010ae A |
277 | } |
278 | ||
3a60a9f5 A |
279 | VATTR_INIT(&va); |
280 | VATTR_WANTED(&va, va_rdev); | |
281 | VATTR_WANTED(&va, va_fsid); | |
282 | VATTR_WANTED(&va, va_data_size); | |
39236c6e | 283 | VATTR_WANTED(&va, va_data_alloc); |
3a60a9f5 A |
284 | VATTR_WANTED(&va, va_nlink); |
285 | error = EFAULT; | |
3e170ce0 | 286 | if (vnode_getattr(ref->vp, &va, ref->ctx)) goto out; |
3a60a9f5 | 287 | |
3e170ce0 A |
288 | mpFree = freespace_mb(ref->vp); |
289 | mpFree <<= 20; | |
290 | kprintf("kern_direct_file(%s): vp size %qd, alloc %qd, mp free %qd, keep free %qd\n", | |
291 | name, va.va_data_size, va.va_data_alloc, mpFree, fs_free_size); | |
3a60a9f5 A |
292 | |
293 | if (ref->vp->v_type == VREG) | |
294 | { | |
3e170ce0 A |
295 | /* Don't dump files with links. */ |
296 | if (va.va_nlink != 1) goto out; | |
3a60a9f5 A |
297 | |
298 | device = va.va_fsid; | |
db609669 A |
299 | ref->filelength = va.va_data_size; |
300 | ||
b0d623f7 | 301 | p1 = &device; |
3a60a9f5 A |
302 | p2 = p; |
303 | do_ioctl = &file_ioctl; | |
db609669 | 304 | |
3e170ce0 A |
305 | if (set_file_size) |
306 | { | |
307 | if (fs_free_size) | |
308 | { | |
309 | mpFree += va.va_data_alloc; | |
310 | if ((mpFree < set_file_size) || ((mpFree - set_file_size) < fs_free_size)) | |
311 | { | |
312 | error = ENOSPC; | |
313 | goto out; | |
fe8ab488 | 314 | } |
3e170ce0 A |
315 | } |
316 | error = vnode_setsize(ref->vp, set_file_size, IO_NOZEROFILL | IO_NOAUTH, ref->ctx); | |
317 | if (error) goto out; | |
318 | ref->filelength = set_file_size; | |
319 | } | |
3a60a9f5 A |
320 | } |
321 | else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR)) | |
322 | { | |
323 | /* Partition. */ | |
324 | device = va.va_rdev; | |
325 | ||
326 | p1 = ref->vp; | |
2d21ac55 | 327 | p2 = ref->ctx; |
3a60a9f5 A |
328 | do_ioctl = &device_ioctl; |
329 | } | |
330 | else | |
331 | { | |
332 | /* Don't dump to non-regular files. */ | |
3e170ce0 | 333 | error = EFAULT; |
3a60a9f5 A |
334 | goto out; |
335 | } | |
6d2010ae A |
336 | ref->device = device; |
337 | ||
3e170ce0 A |
338 | // probe for CF |
339 | dk_corestorage_info_t cs_info; | |
340 | memset(&cs_info, 0, sizeof(dk_corestorage_info_t)); | |
341 | error = do_ioctl(p1, p2, DKIOCCORESTORAGE, (caddr_t)&cs_info); | |
342 | ref->cf = (error == 0) && (cs_info.flags & DK_CORESTORAGE_ENABLE_HOTFILES); | |
343 | ||
6d2010ae A |
344 | // get block size |
345 | ||
7ddcb079 | 346 | error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &ref->blksize); |
6d2010ae A |
347 | if (error) |
348 | goto out; | |
349 | ||
db609669 | 350 | if (ref->vp->v_type != VREG) |
6d2010ae A |
351 | { |
352 | error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &fileblk); | |
3e170ce0 | 353 | if (error) goto out; |
7ddcb079 | 354 | ref->filelength = fileblk * ref->blksize; |
6d2010ae A |
355 | } |
356 | ||
316670eb A |
357 | // pin logical extents |
358 | ||
359 | error = kern_ioctl_file_extents(ref, _DKIOCCSPINEXTENT, 0, ref->filelength); | |
360 | if (error && (ENOTTY != error)) goto out; | |
361 | ref->pinned = (error == 0); | |
362 | ||
363 | // generate the block list | |
364 | ||
365 | error = do_ioctl(p1, p2, DKIOCLOCKPHYSICALEXTENTS, NULL); | |
3e170ce0 | 366 | if (error) goto out; |
316670eb A |
367 | locked = TRUE; |
368 | ||
6d2010ae | 369 | f_offset = 0; |
7ddcb079 | 370 | while (f_offset < ref->filelength) |
6d2010ae A |
371 | { |
372 | if (ref->vp->v_type == VREG) | |
373 | { | |
374 | filechunk = 1*1024*1024*1024; | |
375 | daddr64_t blkno; | |
376 | ||
fe8ab488 A |
377 | error = VNOP_BLOCKMAP(ref->vp, f_offset, filechunk, &blkno, |
378 | &filechunk, NULL, VNODE_WRITE, NULL); | |
3e170ce0 | 379 | if (error) goto out; |
6d2010ae | 380 | |
7ddcb079 | 381 | fileblk = blkno * ref->blksize; |
6d2010ae A |
382 | } |
383 | else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR)) | |
384 | { | |
385 | fileblk = f_offset; | |
7ddcb079 | 386 | filechunk = f_offset ? 0 : ref->filelength; |
6d2010ae A |
387 | } |
388 | ||
389 | physoffset = 0; | |
390 | while (physoffset < filechunk) | |
391 | { | |
392 | dk_physical_extent_t getphysreq; | |
393 | bzero(&getphysreq, sizeof(getphysreq)); | |
394 | ||
395 | getphysreq.offset = fileblk + physoffset; | |
396 | getphysreq.length = (filechunk - physoffset); | |
397 | error = do_ioctl(p1, p2, DKIOCGETPHYSICALEXTENT, (caddr_t) &getphysreq); | |
3e170ce0 | 398 | if (error) goto out; |
6d2010ae A |
399 | if (!target) |
400 | { | |
401 | target = getphysreq.dev; | |
402 | } | |
403 | else if (target != getphysreq.dev) | |
404 | { | |
405 | error = ENOTSUP; | |
406 | goto out; | |
407 | } | |
39236c6e A |
408 | #if HIBFRAGMENT |
409 | uint64_t rev; | |
410 | for (rev = 4096; rev <= getphysreq.length; rev += 4096) | |
411 | { | |
412 | callback(callback_ref, getphysreq.offset + getphysreq.length - rev, 4096); | |
413 | } | |
414 | #else | |
6d2010ae | 415 | callback(callback_ref, getphysreq.offset, getphysreq.length); |
39236c6e | 416 | #endif |
6d2010ae A |
417 | physoffset += getphysreq.length; |
418 | } | |
419 | f_offset += filechunk; | |
420 | } | |
421 | callback(callback_ref, 0ULL, 0ULL); | |
422 | ||
3e170ce0 A |
423 | if (ref->vp->v_type == VREG) p1 = ⌖ |
424 | else | |
425 | { | |
426 | p1 = ⌖ | |
427 | p2 = p; | |
428 | do_ioctl = &file_ioctl; | |
429 | } | |
3a60a9f5 A |
430 | |
431 | // get partition base | |
432 | ||
fe8ab488 A |
433 | if (partitionbase_result) |
434 | { | |
435 | error = do_ioctl(p1, p2, DKIOCGETBASE, (caddr_t) partitionbase_result); | |
436 | if (error) | |
437 | goto out; | |
438 | } | |
3a60a9f5 A |
439 | |
440 | // get block size & constraints | |
441 | ||
442 | error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &blksize); | |
443 | if (error) | |
444 | goto out; | |
445 | ||
446 | maxiocount = 1*1024*1024*1024; | |
447 | ||
448 | error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t) &count); | |
449 | if (error) | |
450 | count = 0; | |
451 | count *= blksize; | |
452 | if (count && (count < maxiocount)) | |
453 | maxiocount = count; | |
454 | ||
455 | error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t) &count); | |
456 | if (error) | |
457 | count = 0; | |
458 | count *= blksize; | |
459 | if (count && (count < maxiocount)) | |
460 | maxiocount = count; | |
461 | ||
462 | error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTREAD, (caddr_t) &count); | |
463 | if (error) | |
464 | count = 0; | |
465 | if (count && (count < maxiocount)) | |
466 | maxiocount = count; | |
467 | ||
468 | error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t) &count); | |
469 | if (error) | |
470 | count = 0; | |
471 | if (count && (count < maxiocount)) | |
472 | maxiocount = count; | |
473 | ||
474 | error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTREAD, (caddr_t) &count); | |
bd504ef0 A |
475 | if (!error) |
476 | error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTCOUNTREAD, (caddr_t) &segcount); | |
3a60a9f5 | 477 | if (error) |
bd504ef0 A |
478 | count = segcount = 0; |
479 | count *= segcount; | |
3a60a9f5 A |
480 | if (count && (count < maxiocount)) |
481 | maxiocount = count; | |
482 | ||
483 | error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTWRITE, (caddr_t) &count); | |
bd504ef0 A |
484 | if (!error) |
485 | error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTCOUNTWRITE, (caddr_t) &segcount); | |
3a60a9f5 | 486 | if (error) |
bd504ef0 A |
487 | count = segcount = 0; |
488 | count *= segcount; | |
3a60a9f5 A |
489 | if (count && (count < maxiocount)) |
490 | maxiocount = count; | |
491 | ||
492 | kprintf("max io 0x%qx bytes\n", maxiocount); | |
493 | if (maxiocount_result) | |
494 | *maxiocount_result = maxiocount; | |
495 | ||
6d2010ae A |
496 | error = do_ioctl(p1, p2, DKIOCISSOLIDSTATE, (caddr_t)&isssd); |
497 | if (!error && isssd) | |
3e170ce0 | 498 | flags |= kIOPolledFileSSD; |
3a60a9f5 | 499 | |
6d2010ae A |
500 | if (partition_device_result) |
501 | *partition_device_result = device; | |
502 | if (image_device_result) | |
503 | *image_device_result = target; | |
fe8ab488 | 504 | if (oflags) |
6d2010ae | 505 | *oflags = flags; |
3a60a9f5 | 506 | |
3e170ce0 A |
507 | if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR)) |
508 | { | |
509 | vnode_close(ref->vp, FWRITE, ref->ctx); | |
510 | ref->vp = NULLVP; | |
511 | vfs_context_rele(ref->ctx); | |
512 | ref->ctx = NULL; | |
513 | } | |
514 | ||
3a60a9f5 | 515 | out: |
3e170ce0 | 516 | printf("kern_open_file_for_direct_io(%d)\n", error); |
3a60a9f5 | 517 | |
6d2010ae A |
518 | if (error && locked) |
519 | { | |
520 | p1 = &device; | |
521 | (void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL); | |
522 | } | |
523 | ||
524 | if (error && ref) | |
525 | { | |
526 | if (ref->vp) | |
527 | { | |
3e170ce0 | 528 | (void) kern_ioctl_file_extents(ref, _DKIOCCSUNPINEXTENT, 0, (ref->pinned && ref->cf) ? ref->filelength : 0); |
2d21ac55 A |
529 | vnode_close(ref->vp, FWRITE, ref->ctx); |
530 | ref->vp = NULLVP; | |
531 | } | |
2d21ac55 A |
532 | vfs_context_rele(ref->ctx); |
533 | kfree(ref, sizeof(struct kern_direct_file_io_ref_t)); | |
534 | ref = NULL; | |
3a60a9f5 | 535 | } |
db609669 | 536 | |
3a60a9f5 A |
537 | return(ref); |
538 | } | |
539 | ||
540 | int | |
3e170ce0 | 541 | kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag) |
3a60a9f5 A |
542 | { |
543 | return (vn_rdwr(UIO_WRITE, ref->vp, | |
544 | addr, len, offset, | |
fe8ab488 | 545 | UIO_SYSSPACE, ioflag|IO_SYNC|IO_NODELOCKED|IO_UNIT, |
2d21ac55 A |
546 | vfs_context_ucred(ref->ctx), (int *) 0, |
547 | vfs_context_proc(ref->ctx))); | |
3a60a9f5 A |
548 | } |
549 | ||
3e170ce0 A |
550 | int |
551 | kern_read_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag) | |
552 | { | |
553 | return (vn_rdwr(UIO_READ, ref->vp, | |
554 | addr, len, offset, | |
555 | UIO_SYSSPACE, ioflag|IO_SYNC|IO_NODELOCKED|IO_UNIT, | |
556 | vfs_context_ucred(ref->ctx), (int *) 0, | |
557 | vfs_context_proc(ref->ctx))); | |
558 | } | |
559 | ||
560 | ||
561 | struct mount * | |
562 | kern_file_mount(struct kern_direct_file_io_ref_t * ref) | |
563 | { | |
564 | return (ref->vp->v_mount); | |
565 | } | |
7ddcb079 | 566 | |
3a60a9f5 | 567 | void |
6d2010ae | 568 | kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref, |
3e170ce0 | 569 | off_t write_offset, void * addr, size_t write_length, |
7ddcb079 | 570 | off_t discard_offset, off_t discard_end) |
3a60a9f5 | 571 | { |
6d2010ae | 572 | int error; |
3a60a9f5 A |
573 | kprintf("kern_close_file_for_direct_io\n"); |
574 | ||
6d2010ae | 575 | if (!ref) return; |
3a60a9f5 | 576 | |
6d2010ae A |
577 | if (ref->vp) |
578 | { | |
579 | int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result); | |
580 | void * p1; | |
581 | void * p2; | |
582 | ||
583 | if (ref->vp->v_type == VREG) | |
584 | { | |
585 | p1 = &ref->device; | |
586 | p2 = kernproc; | |
587 | do_ioctl = &file_ioctl; | |
588 | } | |
589 | else | |
590 | { | |
591 | /* Partition. */ | |
592 | p1 = ref->vp; | |
593 | p2 = ref->ctx; | |
594 | do_ioctl = &device_ioctl; | |
595 | } | |
596 | (void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL); | |
fe8ab488 | 597 | |
3e170ce0 A |
598 | //XXX If unmapping extents then don't also need to unpin; except ... |
599 | //XXX if file unaligned (HFS 4k / Fusion 128k) then pin is superset and | |
600 | //XXX unmap is subset, so save extra walk over file extents (and the risk | |
601 | //XXX that CF drain starts) vs leaving partial units pinned to SSD | |
602 | //XXX (until whatever was sharing also unmaps). Err on cleaning up fully. | |
603 | boolean_t will_unmap = (!ref->pinned || ref->cf) && (discard_end > discard_offset); | |
604 | boolean_t will_unpin = (ref->pinned && ref->cf /* && !will_unmap */); | |
fe8ab488 | 605 | |
3e170ce0 A |
606 | (void) kern_ioctl_file_extents(ref, _DKIOCCSUNPINEXTENT, 0, (will_unpin) ? ref->filelength : 0); |
607 | ||
608 | if (will_unmap) | |
6d2010ae | 609 | { |
3e170ce0 | 610 | (void) kern_ioctl_file_extents(ref, DKIOCUNMAP, discard_offset, (ref->cf) ? ref->filelength : discard_end); |
6d2010ae | 611 | } |
3e170ce0 | 612 | |
39236c6e A |
613 | if (addr && write_length) |
614 | { | |
fe8ab488 | 615 | (void) kern_write_file(ref, write_offset, addr, write_length, 0); |
39236c6e | 616 | } |
6d2010ae A |
617 | |
618 | error = vnode_close(ref->vp, FWRITE, ref->ctx); | |
619 | ||
620 | ref->vp = NULLVP; | |
621 | kprintf("vnode_close(%d)\n", error); | |
3a60a9f5 | 622 | } |
3e170ce0 A |
623 | if (ref->ctx) |
624 | { | |
625 | vfs_context_rele(ref->ctx); | |
626 | ref->ctx = NULL; | |
627 | } | |
6d2010ae | 628 | kfree(ref, sizeof(struct kern_direct_file_io_ref_t)); |
3a60a9f5 | 629 | } |