]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_symfile.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / bsd / kern / kern_symfile.c
CommitLineData
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
cb323159
A
65#define HIBERNATE_MIN_PHYSICAL_LBA_512 (34)
66#define HIBERNATE_MIN_PHYSICAL_LBA_4096 (6)
67#define HIBERNATE_MIN_FILE_SIZE (1024*1024)
d9a64523 68
2d21ac55
A
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.
1c79356b 72 */
2d21ac55 73int
7ddcb079
A
74get_kernel_symfile(__unused proc_t p, __unused char const **symfile);
75int
2d21ac55 76get_kernel_symfile(__unused proc_t p, __unused char const **symfile)
1c79356b 77{
0a7de745 78 return KERN_FAILURE;
0b4e3aa0 79}
91447636 80
0a7de745
A
81struct 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;
3a60a9f5
A
91};
92
93
0a7de745
A
94static int
95file_ioctl(void * p1, void * p2, u_long theIoctl, caddr_t result)
3a60a9f5 96{
0a7de745 97 dev_t device = *(dev_t*) p1;
3a60a9f5 98
0a7de745
A
99 return (*bdevsw[major(device)].d_ioctl)
100 (device, theIoctl, result, S_IFBLK, p2);
3a60a9f5
A
101}
102
0a7de745
A
103static int
104device_ioctl(void * p1, __unused void * p2, u_long theIoctl, caddr_t result)
3a60a9f5 105{
0a7de745 106 return VNOP_IOCTL(p1, theIoctl, result, 0, p2);
3a60a9f5
A
107}
108
316670eb
A
109static int
110kern_ioctl_file_extents(struct kern_direct_file_io_ref_t * ref, u_long theIoctl, off_t offset, off_t end)
111{
0a7de745
A
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;
117 size_t filechunk;
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 = _DKIOCCSPINDISCARDBLACKLIST;
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 = 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;
316670eb
A
200}
201
3e170ce0 202extern uint32_t freespace_mb(vnode_t vp);
7ddcb079 203
3a60a9f5 204struct kern_direct_file_io_ref_t *
0a7de745
A
205kern_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)
3a60a9f5 219{
0a7de745
A
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;
228 size_t filechunk;
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, cmode;
238 struct nameidata nd;
239 u_int32_t ndflags;
240 off_t mpFree;
241
242 int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
243 void * p1 = NULL;
244 void * p2 = NULL;
245
3a60a9f5 246 error = EFAULT;
0a7de745
A
247
248 ref = (struct kern_direct_file_io_ref_t *) kalloc(sizeof(struct kern_direct_file_io_ref_t));
249 if (!ref) {
250 error = EFAULT;
251 goto out;
252 }
253
254 bzero(ref, sizeof(*ref));
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
cb323159
A
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 }
0a7de745
A
388
389 if (ref->vp->v_type != VREG) {
390 error = do_ioctl(p1, p2, DKIOCGETBLOCKCOUNT, (caddr_t) &fileblk);
391 if (error) {
392 goto out;
fe8ab488 393 }
0a7de745
A
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 : 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);
d9a64523 460
39236c6e 461#if HIBFRAGMENT
0a7de745
A
462 uint64_t rev;
463 for (rev = 4096; rev <= getphysreq.length; rev += 4096) {
464 callback(callback_ref, getphysreq.offset + getphysreq.length - rev, 4096);
465 }
39236c6e 466#else
0a7de745 467 callback(callback_ref, getphysreq.offset, getphysreq.length);
39236c6e 468#endif
0a7de745
A
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 = &target;
483 } else {
484 p1 = &target;
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 }
3e170ce0 590
3a60a9f5 591out:
0a7de745
A
592 printf("kern_open_file_for_direct_io(%p, %d)\n", ref, error);
593
594
595 if (error && locked) {
596 p1 = &device;
597 (void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL);
598 }
599
600 if (error && ref) {
601 if (ref->vp) {
602 (void) kern_ioctl_file_extents(ref, _DKIOCCSUNPINEXTENT, 0, (ref->pinned && ref->cf) ? ref->filelength : 0);
603
604 if (ref->frozen) {
605 (void) VNOP_IOCTL(ref->vp, FSCTL_THAW_EXTENTS, NULL, 0, ref->ctx);
606 }
607 if (ref->wbcranged) {
608 (void) do_ioctl(p1, p2, DKIOCAPFSRELEASEWBCRANGE, (caddr_t) NULL);
609 }
610 vnode_close(ref->vp, FWRITE, ref->ctx);
611 ref->vp = NULLVP;
612 }
613 ref->ctx = NULL;
614 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
615 ref = NULL;
616 }
617
618 return ref;
3a60a9f5
A
619}
620
621int
3e170ce0 622kern_write_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag)
3a60a9f5 623{
0a7de745
A
624 return vn_rdwr(UIO_WRITE, ref->vp,
625 addr, 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));
3a60a9f5
A
629}
630
3e170ce0
A
631int
632kern_read_file(struct kern_direct_file_io_ref_t * ref, off_t offset, void * addr, size_t len, int ioflag)
633{
0a7de745
A
634 return vn_rdwr(UIO_READ, ref->vp,
635 addr, len, offset,
636 UIO_SYSSPACE, ioflag | IO_SYNC | IO_NODELOCKED | IO_UNIT,
637 vfs_context_ucred(ref->ctx), (int *) 0,
638 vfs_context_proc(ref->ctx));
3e170ce0
A
639}
640
641
642struct mount *
643kern_file_mount(struct kern_direct_file_io_ref_t * ref)
644{
0a7de745 645 return ref->vp->v_mount;
3e170ce0 646}
7ddcb079 647
3a60a9f5 648void
6d2010ae 649kern_close_file_for_direct_io(struct kern_direct_file_io_ref_t * ref,
0a7de745
A
650 off_t write_offset, void * addr, size_t write_length,
651 off_t discard_offset, off_t discard_end)
3a60a9f5 652{
0a7de745
A
653 int error;
654 printf("kern_close_file_for_direct_io(%p)\n", ref);
655
656 if (!ref) {
657 return;
658 }
659
660 if (ref->vp) {
661 int (*do_ioctl)(void * p1, void * p2, u_long theIoctl, caddr_t result);
662 void * p1;
663 void * p2;
664
665 discard_offset = ((discard_offset + ref->blksize - 1) & ~(((off_t) ref->blksize) - 1));
666 discard_end = ((discard_end) & ~(((off_t) ref->blksize) - 1));
667
668 if (ref->vp->v_type == VREG) {
669 p1 = &ref->device;
670 p2 = kernproc;
671 do_ioctl = &file_ioctl;
672 } else {
673 /* Partition. */
674 p1 = ref->vp;
675 p2 = ref->ctx;
676 do_ioctl = &device_ioctl;
677 }
678 (void) do_ioctl(p1, p2, DKIOCUNLOCKPHYSICALEXTENTS, NULL);
fe8ab488 679
3e170ce0
A
680 //XXX If unmapping extents then don't also need to unpin; except ...
681 //XXX if file unaligned (HFS 4k / Fusion 128k) then pin is superset and
682 //XXX unmap is subset, so save extra walk over file extents (and the risk
683 //XXX that CF drain starts) vs leaving partial units pinned to SSD
684 //XXX (until whatever was sharing also unmaps). Err on cleaning up fully.
685 boolean_t will_unmap = (!ref->pinned || ref->cf) && (discard_end > discard_offset);
686 boolean_t will_unpin = (ref->pinned && ref->cf /* && !will_unmap */);
fe8ab488 687
3e170ce0
A
688 (void) kern_ioctl_file_extents(ref, _DKIOCCSUNPINEXTENT, 0, (will_unpin) ? ref->filelength : 0);
689
0a7de745
A
690 if (will_unmap) {
691 (void) kern_ioctl_file_extents(ref, DKIOCUNMAP, discard_offset, (ref->cf) ? ref->filelength : discard_end);
692 }
3e170ce0 693
0a7de745
A
694 if (ref->frozen) {
695 (void) VNOP_IOCTL(ref->vp, FSCTL_THAW_EXTENTS, NULL, 0, ref->ctx);
696 }
697 if (ref->wbcranged) {
698 (void) do_ioctl(p1, p2, DKIOCAPFSRELEASEWBCRANGE, (caddr_t) NULL);
699 }
d9a64523 700
0a7de745
A
701 if (addr && write_length) {
702 (void) kern_write_file(ref, write_offset, addr, write_length, IO_SKIP_ENCRYPTION);
703 }
6d2010ae 704
0a7de745 705 error = vnode_close(ref->vp, FWRITE, ref->ctx);
6d2010ae 706
0a7de745
A
707 ref->vp = NULLVP;
708 kprintf("vnode_close(%d)\n", error);
00867663 709
0a7de745 710 }
00867663 711
0a7de745 712 ref->ctx = NULL;
00867663 713
0a7de745 714 kfree(ref, sizeof(struct kern_direct_file_io_ref_t));
3a60a9f5 715}