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