xref: /xnu-8019.80.24/bsd/kern/kern_symfile.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
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 	void * p1 = NULL;
247 	void * p2 = NULL;
248 
249 	error = EFAULT;
250 
251 	ref = kalloc_type(struct kern_direct_file_io_ref_t,
252 	    Z_WAITOK | Z_ZERO | Z_NOFAIL);
253 
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 	if (ref->blksize == 4096) {
383 		minoffset = HIBERNATE_MIN_PHYSICAL_LBA_4096 * ref->blksize;
384 	} else {
385 		minoffset = HIBERNATE_MIN_PHYSICAL_LBA_512 * ref->blksize;
386 	}
387 
388 	if (ref->vp->v_type != VREG) {
389 		error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &fileblk);
390 		if (error) {
391 			goto out;
392 		}
393 		ref->filelength = fileblk * ref->blksize;
394 	}
395 
396 	// pin logical extents, CS version
397 
398 	error = kern_ioctl_file_extents(ref, _DKIOCCSPINEXTENT, 0, ref->filelength);
399 	if (error && (ENOTTY != error)) {
400 		goto out;
401 	}
402 	ref->pinned = (error == 0);
403 
404 	// pin logical extents, apfs version
405 
406 	error = VNOP_IOCTL(ref->vp, FSCTL_FREEZE_EXTENTS, NULL, 0, ref->ctx);
407 	if (error && (ENOTTY != error)) {
408 		goto out;
409 	}
410 	ref->frozen = (error == 0);
411 
412 	// generate the block list
413 
414 	error = do_ioctl(p1, p2, DKIOCLOCKPHYSICALEXTENTS, NULL);
415 	if (error) {
416 		goto out;
417 	}
418 	locked = TRUE;
419 
420 	f_offset = 0;
421 	for (; f_offset < ref->filelength; f_offset += filechunk) {
422 		if (ref->vp->v_type == VREG) {
423 			filechunk = 1 * 1024 * 1024 * 1024;
424 			daddr64_t blkno;
425 
426 			error = VNOP_BLOCKMAP(ref->vp, f_offset, filechunk, &blkno,
427 			    &filechunk, NULL, VNODE_WRITE | VNODE_BLOCKMAP_NO_TRACK, NULL);
428 			if (error) {
429 				goto out;
430 			}
431 			if (-1LL == blkno) {
432 				continue;
433 			}
434 			fileblk = blkno * ref->blksize;
435 		} else if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR)) {
436 			fileblk = f_offset;
437 			filechunk = f_offset ? 0 : (unsigned long)ref->filelength;
438 		}
439 
440 		physoffset = 0;
441 		while (physoffset < filechunk) {
442 			dk_physical_extent_t getphysreq;
443 			bzero(&getphysreq, sizeof(getphysreq));
444 
445 			getphysreq.offset = fileblk + physoffset;
446 			getphysreq.length = (filechunk - physoffset);
447 			error = do_ioctl(p1, p2, DKIOCGETPHYSICALEXTENT, (caddr_t) &getphysreq);
448 			if (error) {
449 				goto out;
450 			}
451 			if (!target) {
452 				target = getphysreq.dev;
453 			} else if (target != getphysreq.dev) {
454 				error = ENOTSUP;
455 				goto out;
456 			}
457 
458 			assert(getphysreq.offset >= minoffset);
459 
460 #if HIBFRAGMENT
461 			uint64_t rev;
462 			for (rev = 4096; rev <= getphysreq.length; rev += 4096) {
463 				callback(callback_ref, getphysreq.offset + getphysreq.length - rev, 4096);
464 			}
465 #else
466 			callback(callback_ref, getphysreq.offset, getphysreq.length);
467 #endif
468 			physoffset += getphysreq.length;
469 		}
470 	}
471 	if (ref->wbcranged) {
472 		uint32_t idx;
473 		for (idx = 0; idx < wbc_range.count; idx++) {
474 			assert(wbc_range.extents[idx].offset >= minoffset);
475 			callback(callback_ref, wbc_range.extents[idx].offset, wbc_range.extents[idx].length);
476 		}
477 	}
478 	callback(callback_ref, 0ULL, 0ULL);
479 
480 	if (ref->vp->v_type == VREG) {
481 		p1 = &target;
482 	} else {
483 		p1 = &target;
484 		p2 = p;
485 		do_ioctl = &file_ioctl;
486 	}
487 
488 	// get partition base
489 
490 	if (partitionbase_result) {
491 		error = do_ioctl(p1, p2, DKIOCGETBASE, (caddr_t) partitionbase_result);
492 		if (error) {
493 			goto out;
494 		}
495 	}
496 
497 	// get block size & constraints
498 
499 	error = do_ioctl(p1, p2, DKIOCGETBLOCKSIZE, (caddr_t) &blksize);
500 	if (error) {
501 		goto out;
502 	}
503 
504 	maxiocount = 1 * 1024 * 1024 * 1024;
505 
506 	error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t) &count);
507 	if (error) {
508 		count = 0;
509 	}
510 	count *= blksize;
511 	if (count && (count < maxiocount)) {
512 		maxiocount = count;
513 	}
514 
515 	error = do_ioctl(p1, p2, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t) &count);
516 	if (error) {
517 		count = 0;
518 	}
519 	count *= blksize;
520 	if (count && (count < maxiocount)) {
521 		maxiocount = count;
522 	}
523 
524 	error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTREAD, (caddr_t) &count);
525 	if (error) {
526 		count = 0;
527 	}
528 	if (count && (count < maxiocount)) {
529 		maxiocount = count;
530 	}
531 
532 	error = do_ioctl(p1, p2, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t) &count);
533 	if (error) {
534 		count = 0;
535 	}
536 	if (count && (count < maxiocount)) {
537 		maxiocount = count;
538 	}
539 
540 	error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTREAD, (caddr_t) &count);
541 	if (!error) {
542 		error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTCOUNTREAD, (caddr_t) &segcount);
543 	}
544 	if (error) {
545 		count = segcount = 0;
546 	}
547 	count *= segcount;
548 	if (count && (count < maxiocount)) {
549 		maxiocount = count;
550 	}
551 
552 	error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTBYTECOUNTWRITE, (caddr_t) &count);
553 	if (!error) {
554 		error = do_ioctl(p1, p2, DKIOCGETMAXSEGMENTCOUNTWRITE, (caddr_t) &segcount);
555 	}
556 	if (error) {
557 		count = segcount = 0;
558 	}
559 	count *= segcount;
560 	if (count && (count < maxiocount)) {
561 		maxiocount = count;
562 	}
563 
564 	kprintf("max io 0x%qx bytes\n", maxiocount);
565 	if (maxiocount_result) {
566 		*maxiocount_result = maxiocount;
567 	}
568 
569 	error = do_ioctl(p1, p2, DKIOCISSOLIDSTATE, (caddr_t)&isssd);
570 	if (!error && isssd) {
571 		flags |= kIOPolledFileSSD;
572 	}
573 
574 	if (partition_device_result) {
575 		*partition_device_result = device;
576 	}
577 	if (image_device_result) {
578 		*image_device_result = target;
579 	}
580 	if (oflags) {
581 		*oflags = flags;
582 	}
583 
584 	if ((ref->vp->v_type == VBLK) || (ref->vp->v_type == VCHR)) {
585 		vnode_close(ref->vp, FWRITE, ref->ctx);
586 		ref->vp = NULLVP;
587 		ref->ctx = NULL;
588 	}
589 
590 out:
591 	printf("kern_open_file_for_direct_io(%p, %d)\n", ref, error);
592 
593 
594 	if (error && locked) {
595 		p1 = &device;
596 		(void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL);
597 	}
598 
599 	if (error && ref) {
600 		if (ref->vp) {
601 			(void) kern_ioctl_file_extents(ref, _DKIOCCSUNPINEXTENT, 0, (ref->pinned && ref->cf) ? ref->filelength : 0);
602 
603 			if (ref->frozen) {
604 				(void) VNOP_IOCTL(ref->vp, FSCTL_THAW_EXTENTS, NULL, 0, ref->ctx);
605 			}
606 			if (ref->wbcranged) {
607 				(void) do_ioctl(p1, p2, DKIOCAPFSRELEASEWBCRANGE, (caddr_t) NULL);
608 			}
609 			vnode_close(ref->vp, FWRITE, ref->ctx);
610 			ref->vp = NULLVP;
611 		}
612 		ref->ctx = NULL;
613 		kfree_type(struct kern_direct_file_io_ref_t, ref);
614 		ref = NULL;
615 	}
616 
617 	return ref;
618 }
619 
620 int
kern_write_file(struct kern_direct_file_io_ref_t * ref,off_t offset,void * addr,size_t len,int ioflag)621 kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag)
622 {
623 	assert(len <= INT32_MAX);
624 	return vn_rdwr(UIO_WRITE, ref->vp,
625 	           addr, (int)len, offset,
626 	           UIO_SYSSPACE, ioflag | IO_SYNC | IO_NODELOCKED | IO_UNIT,
627 	           vfs_context_ucred(ref->ctx), (int *) 0,
628 	           vfs_context_proc(ref->ctx));
629 }
630 
631 int
kern_read_file(struct kern_direct_file_io_ref_t * ref,off_t offset,void * addr,size_t len,int ioflag)632 kern_read_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag)
633 {
634 	assert(len <= INT32_MAX);
635 	return vn_rdwr(UIO_READ, ref->vp,
636 	           addr, (int)len, offset,
637 	           UIO_SYSSPACE, ioflag | IO_SYNC | IO_NODELOCKED | IO_UNIT,
638 	           vfs_context_ucred(ref->ctx), (int *) 0,
639 	           vfs_context_proc(ref->ctx));
640 }
641 
642 
643 struct mount *
kern_file_mount(struct kern_direct_file_io_ref_t * ref)644 kern_file_mount(struct kern_direct_file_io_ref_t * ref)
645 {
646 	return ref->vp->v_mount;
647 }
648 
649 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)650 kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
651     off_t write_offset, void * addr, size_t write_length,
652     off_t discard_offset, off_t discard_end)
653 {
654 	int error;
655 	printf("kern_close_file_for_direct_io(%p)\n", ref);
656 
657 	if (!ref) {
658 		return;
659 	}
660 
661 	if (ref->vp) {
662 		int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
663 		void * p1;
664 		void * p2;
665 
666 		discard_offset = ((discard_offset + ref->blksize - 1) & ~(((off_t) ref->blksize) - 1));
667 		discard_end    = ((discard_end)                       & ~(((off_t) ref->blksize) - 1));
668 
669 		if (ref->vp->v_type == VREG) {
670 			p1 = &ref->device;
671 			p2 = kernproc;
672 			do_ioctl = &file_ioctl;
673 		} else {
674 			/* Partition. */
675 			p1 = ref->vp;
676 			p2 = ref->ctx;
677 			do_ioctl = &device_ioctl;
678 		}
679 		(void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL);
680 
681 		//XXX If unmapping extents then don't also need to unpin; except ...
682 		//XXX if file unaligned (HFS 4k / Fusion 128k) then pin is superset and
683 		//XXX unmap is subset, so save extra walk over file extents (and the risk
684 		//XXX that CF drain starts) vs leaving partial units pinned to SSD
685 		//XXX (until whatever was sharing also unmaps).  Err on cleaning up fully.
686 		boolean_t will_unmap = (!ref->pinned || ref->cf) && (discard_end > discard_offset);
687 		boolean_t will_unpin = (ref->pinned && ref->cf /* && !will_unmap */);
688 
689 		(void) kern_ioctl_file_extents(ref, _DKIOCCSUNPINEXTENT, 0, (will_unpin) ? ref->filelength : 0);
690 
691 		if (will_unmap) {
692 			(void) kern_ioctl_file_extents(ref, DKIOCUNMAP, discard_offset, (ref->cf) ? ref->filelength : discard_end);
693 		}
694 
695 		if (ref->frozen) {
696 			(void) VNOP_IOCTL(ref->vp, FSCTL_THAW_EXTENTS, NULL, 0, ref->ctx);
697 		}
698 		if (ref->wbcranged) {
699 			(void) do_ioctl(p1, p2, DKIOCAPFSRELEASEWBCRANGE, (caddr_t) NULL);
700 		}
701 
702 		if (addr && write_length) {
703 			(void) kern_write_file(ref, write_offset, addr, write_length, IO_SKIP_ENCRYPTION);
704 		}
705 
706 		error = vnode_close(ref->vp, FWRITE, ref->ctx);
707 
708 		ref->vp = NULLVP;
709 		kprintf("vnode_close(%d)\n", error);
710 
711 	}
712 
713 	ref->ctx = NULL;
714 
715 	kfree_type(struct kern_direct_file_io_ref_t, ref);
716 }
717