2 * Copyright (c) 2006-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 #include <IOKit/IOLib.h>
33 #include <IOKit/IOBSD.h>
34 #include <IOKit/IOService.h>
35 #include <IOKit/IOPlatformExpert.h>
36 #include <IOKit/IOPolledInterface.h>
37 #include <IOKit/IOHibernatePrivate.h>
38 #include <IOKit/IOBufferMemoryDescriptor.h>
39 #include <IOKit/AppleKeyStoreInterface.h>
40 #include "IOKitKernelInternal.h"
43 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45 OSDefineMetaClassAndAbstractStructors(IOPolledInterface
, OSObject
);
47 OSMetaClassDefineReservedUnused(IOPolledInterface
, 0);
48 OSMetaClassDefineReservedUnused(IOPolledInterface
, 1);
49 OSMetaClassDefineReservedUnused(IOPolledInterface
, 2);
50 OSMetaClassDefineReservedUnused(IOPolledInterface
, 3);
51 OSMetaClassDefineReservedUnused(IOPolledInterface
, 4);
52 OSMetaClassDefineReservedUnused(IOPolledInterface
, 5);
53 OSMetaClassDefineReservedUnused(IOPolledInterface
, 6);
54 OSMetaClassDefineReservedUnused(IOPolledInterface
, 7);
55 OSMetaClassDefineReservedUnused(IOPolledInterface
, 8);
56 OSMetaClassDefineReservedUnused(IOPolledInterface
, 9);
57 OSMetaClassDefineReservedUnused(IOPolledInterface
, 10);
58 OSMetaClassDefineReservedUnused(IOPolledInterface
, 11);
59 OSMetaClassDefineReservedUnused(IOPolledInterface
, 12);
60 OSMetaClassDefineReservedUnused(IOPolledInterface
, 13);
61 OSMetaClassDefineReservedUnused(IOPolledInterface
, 14);
62 OSMetaClassDefineReservedUnused(IOPolledInterface
, 15);
64 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
66 #ifndef kIOMediaPreferredBlockSizeKey
67 #define kIOMediaPreferredBlockSizeKey "Preferred Block Size"
70 enum { kDefaultIOSize
= 128*1024 };
72 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
74 class IOPolledFilePollers
: public OSObject
76 OSDeclareDefaultStructors(IOPolledFilePollers
)
81 IOBufferMemoryDescriptor
* ioBuffer
;
88 static IOPolledFilePollers
* copyPollers(IOService
* media
);
91 OSDefineMetaClassAndStructors(IOPolledFilePollers
, OSObject
)
93 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
96 IOPolledFilePollers::copyPollers(IOService
* media
)
98 IOPolledFilePollers
* vars
;
102 IORegistryEntry
* next
;
103 IORegistryEntry
* child
;
105 if ((obj
= media
->copyProperty(kIOPolledInterfaceStackKey
)))
107 return (OSDynamicCast(IOPolledFilePollers
, obj
));
112 vars
= OSTypeAlloc(IOPolledFilePollers
);
115 vars
->pollers
= OSArray::withCapacity(4);
118 err
= kIOReturnNoMemory
;
122 next
= vars
->media
= media
;
125 IOPolledInterface
* poller
;
128 obj
= next
->getProperty(kIOPolledInterfaceSupportKey
);
129 if (kOSBooleanFalse
== obj
)
131 vars
->pollers
->flushCollection();
134 else if ((poller
= OSDynamicCast(IOPolledInterface
, obj
)))
135 vars
->pollers
->setObject(poller
);
137 if ((service
= OSDynamicCast(IOService
, next
))
138 && service
->getDeviceMemory()
139 && !vars
->pollers
->getCount()) break;
143 while ((next
= child
->getParentEntry(gIOServicePlane
))
144 && child
->isParent(next
, gIOServicePlane
, true));
146 if (!vars
->pollers
->getCount())
148 err
= kIOReturnUnsupported
;
154 media
->setProperty(kIOPolledInterfaceStackKey
, vars
);
159 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
162 IOPolledFilePollersIODone(IOPolledFilePollers
* vars
, bool abortable
);
164 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
167 IOPolledFilePollersProbe(IOPolledFilePollers
* vars
)
169 IOReturn err
= kIOReturnError
;
171 IOPolledInterface
* poller
;
173 for (idx
= vars
->pollers
->getCount() - 1; idx
>= 0; idx
--)
175 poller
= (IOPolledInterface
*) vars
->pollers
->getObject(idx
);
176 err
= poller
->probe(vars
->media
);
179 HIBLOG("IOPolledInterface::probe[%d] 0x%x\n", idx
, err
);
187 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
190 IOPolledFilePollersOpen(IOPolledFileIOVars
* filevars
, uint32_t state
, bool abortable
)
193 IOPolledFilePollers
* vars
= filevars
->pollers
;
194 IOBufferMemoryDescriptor
* ioBuffer
;
195 IOPolledInterface
* poller
;
197 IOReturn err
= kIOReturnError
;
200 vars
->abortable
= abortable
;
203 if (kIOPolledAfterSleepState
== state
)
208 (void) IOPolledFilePollersIODone(vars
, false);
210 if ((kIOPolledPreflightState
== state
) || (kIOPolledPreflightCoreDumpState
== state
))
212 ioBuffer
= vars
->ioBuffer
;
215 vars
->ioBuffer
= ioBuffer
= IOBufferMemoryDescriptor::withOptions(kIODirectionInOut
,
216 2 * kDefaultIOSize
, page_size
);
217 if (!ioBuffer
) return (kIOReturnNoMemory
);
221 for (idx
= vars
->pollers
->getCount() - 1; idx
>= 0; idx
--)
223 poller
= (IOPolledInterface
*) vars
->pollers
->getObject(idx
);
224 err
= poller
->open(state
, ioBuffer
);
225 if ((kIOReturnSuccess
!= err
) && (kIOPolledPreflightCoreDumpState
== state
))
227 err
= poller
->open(kIOPolledPreflightState
, ioBuffer
);
229 if (kIOReturnSuccess
!= err
)
231 HIBLOG("IOPolledInterface::open[%d] 0x%x\n", idx
, err
);
235 if (kIOReturnSuccess
== err
)
240 next
->setProperty(kIOPolledInterfaceActiveKey
, kOSBooleanTrue
);
241 next
= next
->getProvider();
248 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
251 IOPolledFilePollersClose(IOPolledFileIOVars
* filevars
, uint32_t state
)
253 IOPolledFilePollers
* vars
= filevars
->pollers
;
254 IOPolledInterface
* poller
;
255 IORegistryEntry
* next
;
259 (void) IOPolledFilePollersIODone(vars
, false);
261 if (kIOPolledPostflightState
== state
)
267 IOPolledFilePollersOpen(filevars
, vars
->openState
, vars
->abortable
);
268 return (kIOReturnSuccess
);
272 for (idx
= 0, err
= kIOReturnSuccess
;
273 (poller
= (IOPolledInterface
*) vars
->pollers
->getObject(idx
));
276 err
= poller
->close(state
);
277 if (err
) HIBLOG("IOPolledInterface::close[%d] 0x%x\n", idx
, err
);
280 if (kIOPolledPostflightState
== state
)
285 next
->removeProperty(kIOPolledInterfaceActiveKey
);
286 next
= next
->getParentEntry(gIOServicePlane
);
291 vars
->ioBuffer
->release();
297 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
300 IOPolledFileGetIOBuffer(IOPolledFileIOVars
* vars
)
302 return (vars
->pollers
->ioBuffer
);
305 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
308 IOPolledIOComplete(void * target
,
311 UInt64 actualByteCount
)
313 IOPolledFilePollers
* vars
= (IOPolledFilePollers
*) parameter
;
315 vars
->ioStatus
= status
;
318 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
321 IOStartPolledIO(IOPolledFilePollers
* vars
,
322 uint32_t operation
, uint32_t bufferOffset
,
323 uint64_t deviceOffset
, uint64_t length
)
326 IOPolledInterface
* poller
;
327 IOPolledCompletion completion
;
329 err
= vars
->ioStatus
;
330 if (kIOReturnSuccess
!= err
) return (err
);
332 completion
.target
= 0;
333 completion
.action
= &IOPolledIOComplete
;
334 completion
.parameter
= vars
;
338 poller
= (IOPolledInterface
*) vars
->pollers
->getObject(0);
339 err
= poller
->startIO(operation
, bufferOffset
, deviceOffset
, length
, completion
);
341 HIBLOG("IOPolledInterface::startIO[%d] 0x%x\n", 0, err
);
346 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
349 IOPolledFilePollersIODone(IOPolledFilePollers
* vars
, bool abortable
)
351 IOReturn err
= kIOReturnSuccess
;
353 IOPolledInterface
* poller
;
354 AbsoluteTime deadline
;
356 if (!vars
->io
) return (kIOReturnSuccess
);
358 abortable
&= vars
->abortable
;
360 clock_interval_to_deadline(2000, kMillisecondScale
, &deadline
);
362 while (-1 == vars
->ioStatus
)
365 (poller
= (IOPolledInterface
*) vars
->pollers
->getObject(idx
));
369 newErr
= poller
->checkForWork();
370 if ((newErr
== kIOReturnAborted
) && !abortable
)
371 newErr
= kIOReturnSuccess
;
372 if (kIOReturnSuccess
== err
)
375 if ((false) && (kIOReturnSuccess
== err
) && (mach_absolute_time() > AbsoluteTime_to_scalar(&deadline
)))
377 HIBLOG("IOPolledInterface::forced timeout\n");
378 vars
->ioStatus
= kIOReturnTimeout
;
384 if ((kIOReturnSuccess
== err
) && abortable
&& hibernate_should_abort())
386 err
= kIOReturnAborted
;
387 HIBLOG("IOPolledInterface::checkForWork sw abort\n");
393 HIBLOG("IOPolledInterface::checkForWork[%d] 0x%x\n", idx
, err
);
397 err
= vars
->ioStatus
;
398 if (kIOReturnSuccess
!= err
) HIBLOG("IOPolledInterface::ioStatus 0x%x\n", err
);
404 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
405 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
407 struct _OpenFileContext
414 file_extent_callback(void * ref
, uint64_t start
, uint64_t length
)
416 _OpenFileContext
* ctx
= (_OpenFileContext
*) ref
;
417 IOPolledFileExtent extent
;
419 extent
.start
= start
;
420 extent
.length
= length
;
421 ctx
->extents
->appendBytes(&extent
, sizeof(extent
));
425 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
428 IOCopyMediaForDev(dev_t device
)
430 OSDictionary
* matching
;
433 IOService
* result
= 0;
435 matching
= IOService::serviceMatching("IOMedia");
440 num
= OSNumber::withNumber(major(device
), 32);
443 matching
->setObject(kIOBSDMajorKey
, num
);
445 num
= OSNumber::withNumber(minor(device
), 32);
448 matching
->setObject(kIOBSDMinorKey
, num
);
452 iter
= IOService::getMatchingServices(matching
);
455 result
= (IOService
*) iter
->getNextObject();
467 IOGetVolumeCryptKey(dev_t block_dev
, OSString
** pKeyUUID
,
468 uint8_t * volumeCryptKey
, size_t keySize
)
472 OSString
* keyUUID
= 0;
473 OSString
* keyStoreUUID
= 0;
474 uuid_t volumeKeyUUID
;
475 aks_volume_key_t vek
;
477 static IOService
* sKeyStore
;
479 part
= IOCopyMediaForDev(block_dev
);
480 if (!part
) return (kIOReturnNotFound
);
482 err
= part
->callPlatformFunction(PLATFORM_FUNCTION_GET_MEDIA_ENCRYPTION_KEY_UUID
, false,
483 (void *) &keyUUID
, (void *) &keyStoreUUID
, NULL
, NULL
);
484 if ((kIOReturnSuccess
== err
) && keyUUID
&& keyStoreUUID
)
486 // IOLog("got volume key %s\n", keyStoreUUID->getCStringNoCopy());
489 sKeyStore
= (IOService
*) IORegistryEntry::fromPath(AKS_SERVICE_PATH
, gIOServicePlane
);
491 err
= uuid_parse(keyStoreUUID
->getCStringNoCopy(), volumeKeyUUID
);
493 err
= kIOReturnNoResources
;
494 if (kIOReturnSuccess
== err
)
495 err
= sKeyStore
->callPlatformFunction(gAKSGetKey
, true, volumeKeyUUID
, &vek
, NULL
, NULL
);
496 if (kIOReturnSuccess
!= err
)
497 IOLog("volume key err 0x%x\n", err
);
500 if (vek
.key
.keybytecount
< keySize
) keySize
= vek
.key
.keybytecount
;
501 bcopy(&vek
.key
.keybytes
[0], volumeCryptKey
, keySize
);
503 bzero(&vek
, sizeof(vek
));
507 if (pKeyUUID
) *pKeyUUID
= keyUUID
;
512 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
515 IOPolledFileOpen(const char * filename
,
516 uint64_t setFileSize
, uint64_t fsFreeSize
,
517 void * write_file_addr
, size_t write_file_len
,
518 IOPolledFileIOVars
** fileVars
,
520 uint8_t * volumeCryptKey
, size_t keySize
)
522 IOReturn err
= kIOReturnSuccess
;
523 IOPolledFileIOVars
* vars
;
524 _OpenFileContext ctx
;
525 OSData
* extentsData
;
527 IOService
* part
= 0;
530 AbsoluteTime startTime
, endTime
;
533 vars
= IONew(IOPolledFileIOVars
, 1);
534 if (!vars
) return (kIOReturnNoMemory
);
535 bzero(vars
, sizeof(*vars
));
536 vars
->allocated
= true;
540 extentsData
= OSData::withCapacity(32);
541 ctx
.extents
= extentsData
;
543 clock_get_uptime(&startTime
);
545 vars
->fileRef
= kern_open_file_for_direct_io(filename
,
546 (write_file_addr
!= NULL
) || (0 != setFileSize
),
547 &file_extent_callback
, &ctx
,
551 0, write_file_addr
, write_file_len
,
559 uint32_t msDelay
= (131071 & random());
560 HIBLOG("sleep %d\n", msDelay
);
563 clock_get_uptime(&endTime
);
564 SUB_ABSOLUTETIME(&endTime
, &startTime
);
565 absolutetime_to_nanoseconds(endTime
, &nsec
);
567 if (!vars
->fileRef
) err
= kIOReturnNoSpace
;
569 HIBLOG("kern_open_file_for_direct_io took %qd ms\n", nsec
/ 1000000ULL);
570 if (kIOReturnSuccess
!= err
) break;
572 HIBLOG("Opened file %s, size %qd, extents %ld, maxio %qx ssd %d\n", filename
, ctx
.size
,
573 (extentsData
->getLength() / sizeof(IOPolledFileExtent
)) - 1,
574 vars
->maxiobytes
, kIOPolledFileSSD
& vars
->flags
);
575 assert(!vars
->block0
);
576 if (extentsData
->getLength() < sizeof(IOPolledFileExtent
))
578 err
= kIOReturnNoSpace
;
582 vars
->fileSize
= ctx
.size
;
583 vars
->extentMap
= (IOPolledFileExtent
*) extentsData
->getBytesNoCopy();
585 part
= IOCopyMediaForDev(image_dev
);
588 err
= kIOReturnNotFound
;
592 if (!(vars
->pollers
= IOPolledFilePollers::copyPollers(part
))) break;
594 if ((num
= OSDynamicCast(OSNumber
, part
->getProperty(kIOMediaPreferredBlockSizeKey
))))
595 vars
->blockSize
= num
->unsigned32BitValue();
596 if (vars
->blockSize
< 4096) vars
->blockSize
= 4096;
598 HIBLOG("polled file major %d, minor %d, blocksize %ld, pollers %d\n",
599 major(image_dev
), minor(image_dev
), (long)vars
->blockSize
,
600 vars
->pollers
->pollers
->getCount());
602 OSString
* keyUUID
= NULL
;
605 err
= IOGetVolumeCryptKey(block_dev
, &keyUUID
, volumeCryptKey
, keySize
);
609 vars
->fileExtents
= extentsData
;
615 #if defined(__i386__) || defined(__x86_64__)
616 char str2
[24 + sizeof(uuid_string_t
) + 2];
619 snprintf(str2
, sizeof(str2
), "%qx:%s",
620 vars
->extentMap
[0].start
, keyUUID
->getCStringNoCopy());
622 snprintf(str2
, sizeof(str2
), "%qx", vars
->extentMap
[0].start
);
624 err
= IOService::getPlatform()->callPlatformFunction(
625 gIOCreateEFIDevicePathSymbol
, false,
626 (void *) part
, (void *) str2
,
627 (void *) (uintptr_t) true, (void *) &data
);
630 err
= kIOReturnSuccess
;
632 if (kIOReturnSuccess
!= err
)
634 HIBLOG("error 0x%x getting path\n", err
);
642 if (kIOReturnSuccess
!= err
)
644 HIBLOG("error 0x%x opening polled file\n", err
);
645 IOPolledFileClose(&vars
, 0, 0, 0, 0, 0);
648 if (part
) part
->release();
653 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
656 IOPolledFileClose(IOPolledFileIOVars
** pVars
,
657 off_t write_offset
, void * addr
, size_t write_length
,
658 off_t discard_offset
, off_t discard_end
)
660 IOPolledFileIOVars
* vars
;
663 if (!vars
) return(kIOReturnSuccess
);
667 kern_close_file_for_direct_io(vars
->fileRef
, write_offset
, addr
, write_length
,
668 discard_offset
, discard_end
);
669 vars
->fileRef
= NULL
;
671 if (vars
->fileExtents
)
673 vars
->fileExtents
->release();
674 vars
->fileExtents
= 0;
678 vars
->pollers
->release();
682 if (vars
->allocated
) IODelete(vars
, IOPolledFileIOVars
, 1);
683 else bzero(vars
, sizeof(IOPolledFileIOVars
));
686 return (kIOReturnSuccess
);
689 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
692 IOPolledFilePollersSetup(IOPolledFileIOVars
* vars
,
697 err
= kIOReturnSuccess
;
700 if (!vars
->pollers
->openCount
)
702 err
= IOPolledFilePollersProbe(vars
->pollers
);
703 if (kIOReturnSuccess
!= err
) break;
704 err
= IOPolledFilePollersOpen(vars
, openState
, false);
705 if (kIOReturnSuccess
!= err
) break;
706 vars
->pollers
->openState
= openState
;
708 vars
->pollers
->openCount
++;
709 vars
->pollers
->io
= false;
710 vars
->buffer
= (uint8_t *) vars
->pollers
->ioBuffer
->getBytesNoCopy();
711 vars
->bufferHalf
= 0;
712 vars
->bufferOffset
= 0;
713 vars
->bufferSize
= (vars
->pollers
->ioBuffer
->getLength() >> 1);
715 if (vars
->maxiobytes
< vars
->bufferSize
) vars
->bufferSize
= vars
->maxiobytes
;
719 if (kIOReturnSuccess
!= err
) HIBLOG("IOPolledFilePollersSetup(%d) error 0x%x\n", openState
, err
);
725 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
728 IOPolledFileSeek(IOPolledFileIOVars
* vars
, uint64_t position
)
730 IOPolledFileExtent
* extentMap
;
732 extentMap
= vars
->extentMap
;
734 vars
->position
= position
;
736 while (position
>= extentMap
->length
)
738 position
-= extentMap
->length
;
742 vars
->currentExtent
= extentMap
;
743 vars
->extentRemaining
= extentMap
->length
- position
;
744 vars
->extentPosition
= vars
->position
- position
;
746 if (vars
->bufferSize
<= vars
->extentRemaining
)
747 vars
->bufferLimit
= vars
->bufferSize
;
749 vars
->bufferLimit
= vars
->extentRemaining
;
751 return (kIOReturnSuccess
);
754 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
757 IOPolledFileWrite(IOPolledFileIOVars
* vars
,
758 const uint8_t * bytes
, IOByteCount size
,
759 IOPolledFileCryptVars
* cryptvars
)
761 IOReturn err
= kIOReturnSuccess
;
769 // seek to end of block & flush
770 size
= vars
->position
& (vars
->blockSize
- 1);
772 size
= vars
->blockSize
- size
;
774 // use some garbage for the fill
775 bytes
= vars
->buffer
+ vars
->bufferOffset
;
778 copy
= vars
->bufferLimit
- vars
->bufferOffset
;
786 bcopy(bytes
, vars
->buffer
+ vars
->bufferHalf
+ vars
->bufferOffset
, copy
);
790 bzero(vars
->buffer
+ vars
->bufferHalf
+ vars
->bufferOffset
, copy
);
793 vars
->bufferOffset
+= copy
;
794 vars
->position
+= copy
;
796 if (flush
&& vars
->bufferOffset
)
798 uint64_t offset
= (vars
->position
- vars
->bufferOffset
799 - vars
->extentPosition
+ vars
->currentExtent
->start
);
800 uint32_t length
= (vars
->bufferOffset
);
803 if (cryptvars
&& vars
->encryptStart
804 && (vars
->position
> vars
->encryptStart
)
805 && ((vars
->position
- length
) < vars
->encryptEnd
))
807 AbsoluteTime startTime
, endTime
;
809 uint64_t encryptLen
, encryptStart
;
810 encryptLen
= vars
->position
- vars
->encryptStart
;
811 if (encryptLen
> length
)
813 encryptStart
= length
- encryptLen
;
814 if (vars
->position
> vars
->encryptEnd
)
815 encryptLen
-= (vars
->position
- vars
->encryptEnd
);
817 clock_get_uptime(&startTime
);
819 // encrypt the buffer
820 aes_encrypt_cbc(vars
->buffer
+ vars
->bufferHalf
+ encryptStart
,
821 &cryptvars
->aes_iv
[0],
822 encryptLen
/ AES_BLOCK_SIZE
,
823 vars
->buffer
+ vars
->bufferHalf
+ encryptStart
,
824 &cryptvars
->ctx
.encrypt
);
826 clock_get_uptime(&endTime
);
827 ADD_ABSOLUTETIME(&vars
->cryptTime
, &endTime
);
828 SUB_ABSOLUTETIME(&vars
->cryptTime
, &startTime
);
829 vars
->cryptBytes
+= encryptLen
;
831 // save initial vector for following encrypts
832 bcopy(vars
->buffer
+ vars
->bufferHalf
+ encryptStart
+ encryptLen
- AES_BLOCK_SIZE
,
833 &cryptvars
->aes_iv
[0],
838 err
= IOPolledFilePollersIODone(vars
->pollers
, true);
839 if (kIOReturnSuccess
!= err
)
842 if (vars
->position
& (vars
->blockSize
- 1)) HIBLOG("misaligned file pos %qx\n", vars
->position
);
843 //if (length != vars->bufferSize) HIBLOG("short write of %qx ends@ %qx\n", length, offset + length);
845 err
= IOStartPolledIO(vars
->pollers
, kIOPolledWrite
, vars
->bufferHalf
, offset
, length
);
846 if (kIOReturnSuccess
!= err
)
848 vars
->pollers
->io
= true;
850 vars
->extentRemaining
-= vars
->bufferOffset
;
851 if (!vars
->extentRemaining
)
853 vars
->currentExtent
++;
854 vars
->extentRemaining
= vars
->currentExtent
->length
;
855 vars
->extentPosition
= vars
->position
;
858 vars
->bufferHalf
= vars
->bufferHalf
? 0 : vars
->bufferSize
;
859 vars
->bufferOffset
= 0;
860 if (vars
->bufferSize
<= vars
->extentRemaining
)
861 vars
->bufferLimit
= vars
->bufferSize
;
863 vars
->bufferLimit
= vars
->extentRemaining
;
865 if (!vars
->extentRemaining
)
867 err
= kIOReturnOverrun
;
879 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
882 IOPolledFileRead(IOPolledFileIOVars
* vars
,
883 uint8_t * bytes
, IOByteCount size
,
884 IOPolledFileCryptVars
* cryptvars
)
886 IOReturn err
= kIOReturnSuccess
;
889 // bytesWritten += size;
893 copy
= vars
->bufferLimit
- vars
->bufferOffset
;
899 bcopy(vars
->buffer
+ vars
->bufferHalf
+ vars
->bufferOffset
, bytes
, copy
);
903 vars
->bufferOffset
+= copy
;
904 // vars->position += copy;
906 if ((vars
->bufferOffset
== vars
->bufferLimit
) && (vars
->position
< vars
->readEnd
))
908 if (!vars
->pollers
->io
) cryptvars
= 0;
909 err
= IOPolledFilePollersIODone(vars
->pollers
, true);
910 if (kIOReturnSuccess
!= err
)
913 if (vars
->position
& (vars
->blockSize
- 1)) HIBLOG("misaligned file pos %qx\n", vars
->position
);
915 vars
->position
+= vars
->lastRead
;
916 vars
->extentRemaining
-= vars
->lastRead
;
917 vars
->bufferLimit
= vars
->lastRead
;
919 if (!vars
->extentRemaining
)
921 vars
->currentExtent
++;
922 vars
->extentRemaining
= vars
->currentExtent
->length
;
923 vars
->extentPosition
= vars
->position
;
924 if (!vars
->extentRemaining
)
926 err
= kIOReturnOverrun
;
932 uint64_t lastReadLength
= vars
->lastRead
;
933 uint64_t offset
= (vars
->position
934 - vars
->extentPosition
+ vars
->currentExtent
->start
);
935 if (vars
->extentRemaining
<= vars
->bufferSize
)
936 length
= vars
->extentRemaining
;
938 length
= vars
->bufferSize
;
939 if ((length
+ vars
->position
) > vars
->readEnd
)
940 length
= vars
->readEnd
- vars
->position
;
942 vars
->lastRead
= length
;
945 //if (length != vars->bufferSize) HIBLOG("short read of %qx ends@ %qx\n", length, offset + length);
946 err
= IOStartPolledIO(vars
->pollers
, kIOPolledRead
, vars
->bufferHalf
, offset
, length
);
947 if (kIOReturnSuccess
!= err
)
949 vars
->pollers
->io
= true;
952 vars
->bufferHalf
= vars
->bufferHalf
? 0 : vars
->bufferSize
;
953 vars
->bufferOffset
= 0;
958 uint8_t thisVector
[AES_BLOCK_SIZE
];
959 AbsoluteTime startTime
, endTime
;
961 // save initial vector for following decrypts
962 bcopy(&cryptvars
->aes_iv
[0], &thisVector
[0], AES_BLOCK_SIZE
);
963 bcopy(vars
->buffer
+ vars
->bufferHalf
+ lastReadLength
- AES_BLOCK_SIZE
,
964 &cryptvars
->aes_iv
[0], AES_BLOCK_SIZE
);
966 // decrypt the buffer
967 clock_get_uptime(&startTime
);
969 aes_decrypt_cbc(vars
->buffer
+ vars
->bufferHalf
,
971 lastReadLength
/ AES_BLOCK_SIZE
,
972 vars
->buffer
+ vars
->bufferHalf
,
973 &cryptvars
->ctx
.decrypt
);
975 clock_get_uptime(&endTime
);
976 ADD_ABSOLUTETIME(&vars
->cryptTime
, &endTime
);
977 SUB_ABSOLUTETIME(&vars
->cryptTime
, &startTime
);
978 vars
->cryptBytes
+= lastReadLength
;
988 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */