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