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