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