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
;
87 static IOPolledFilePollers
* copyPollers(IOService
* media
);
90 OSDefineMetaClassAndStructors(IOPolledFilePollers
, OSObject
)
92 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
95 IOPolledFilePollers::copyPollers(IOService
* media
)
97 IOPolledFilePollers
* vars
;
101 IORegistryEntry
* next
;
102 IORegistryEntry
* child
;
104 if ((obj
= media
->copyProperty(kIOPolledInterfaceStackKey
)))
106 return (OSDynamicCast(IOPolledFilePollers
, obj
));
111 vars
= OSTypeAlloc(IOPolledFilePollers
);
114 vars
->pollers
= OSArray::withCapacity(4);
117 err
= kIOReturnNoMemory
;
121 next
= vars
->media
= media
;
124 IOPolledInterface
* poller
;
127 obj
= next
->getProperty(kIOPolledInterfaceSupportKey
);
128 if (kOSBooleanFalse
== obj
)
130 vars
->pollers
->flushCollection();
133 else if ((poller
= OSDynamicCast(IOPolledInterface
, obj
)))
134 vars
->pollers
->setObject(poller
);
136 if ((service
= OSDynamicCast(IOService
, next
))
137 && service
->getDeviceMemory()
138 && !vars
->pollers
->getCount()) break;
142 while ((next
= child
->getParentEntry(gIOServicePlane
))
143 && child
->isParent(next
, gIOServicePlane
, true));
145 if (!vars
->pollers
->getCount())
147 err
= kIOReturnUnsupported
;
153 media
->setProperty(kIOPolledInterfaceStackKey
, vars
);
158 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
161 IOPolledFilePollersIODone(IOPolledFilePollers
* vars
, bool abortable
);
163 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
166 IOPolledFilePollersProbe(IOPolledFilePollers
* vars
)
168 IOReturn err
= kIOReturnError
;
170 IOPolledInterface
* poller
;
172 for (idx
= vars
->pollers
->getCount() - 1; idx
>= 0; idx
--)
174 poller
= (IOPolledInterface
*) vars
->pollers
->getObject(idx
);
175 err
= poller
->probe(vars
->media
);
178 HIBLOG("IOPolledInterface::probe[%d] 0x%x\n", idx
, err
);
186 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
189 IOPolledFilePollersOpen(IOPolledFileIOVars
* filevars
, uint32_t state
, bool abortable
)
192 IOPolledFilePollers
* vars
= filevars
->pollers
;
193 IOBufferMemoryDescriptor
* ioBuffer
;
194 IOPolledInterface
* poller
;
196 IOReturn err
= kIOReturnError
;
199 vars
->abortable
= abortable
;
202 if (kIOPolledAfterSleepState
== state
)
207 (void) IOPolledFilePollersIODone(vars
, false);
209 if ((kIOPolledPreflightState
== state
) || (kIOPolledPreflightCoreDumpState
== state
))
211 ioBuffer
= vars
->ioBuffer
;
214 vars
->ioBuffer
= ioBuffer
= IOBufferMemoryDescriptor::withOptions(kIODirectionInOut
,
215 2 * kDefaultIOSize
, page_size
);
216 if (!ioBuffer
) return (kIOReturnNoMemory
);
220 for (idx
= vars
->pollers
->getCount() - 1; idx
>= 0; idx
--)
222 poller
= (IOPolledInterface
*) vars
->pollers
->getObject(idx
);
223 err
= poller
->open(state
, ioBuffer
);
224 if (kIOReturnSuccess
!= err
)
226 HIBLOG("IOPolledInterface::open[%d] 0x%x\n", idx
, err
);
230 if ((kIOReturnSuccess
== err
) && (kIOPolledPreflightState
== state
))
235 next
->setProperty(kIOPolledInterfaceActiveKey
, kOSBooleanTrue
);
236 next
= next
->getProvider();
243 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
246 IOPolledFilePollersClose(IOPolledFileIOVars
* filevars
, uint32_t state
)
248 IOPolledFilePollers
* vars
= filevars
->pollers
;
249 IOPolledInterface
* poller
;
250 IORegistryEntry
* next
;
254 (void) IOPolledFilePollersIODone(vars
, false);
256 if ((kIOPolledPostflightState
== state
) || (kIOPolledPostflightCoreDumpState
== state
))
261 for (idx
= 0, err
= kIOReturnSuccess
;
262 (poller
= (IOPolledInterface
*) vars
->pollers
->getObject(idx
));
265 err
= poller
->close(state
);
266 if (err
) HIBLOG("IOPolledInterface::close[%d] 0x%x\n", idx
, err
);
269 if (kIOPolledPostflightState
== state
)
274 next
->removeProperty(kIOPolledInterfaceActiveKey
);
275 next
= next
->getParentEntry(gIOServicePlane
);
279 if ((kIOPolledPostflightState
== state
) || (kIOPolledPostflightCoreDumpState
== state
)) do
281 if (vars
->openCount
) break;
284 vars
->ioBuffer
->release();
292 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
295 IOPolledFileGetIOBuffer(IOPolledFileIOVars
* vars
)
297 return (vars
->pollers
->ioBuffer
);
300 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
303 IOPolledIOComplete(void * target
,
306 UInt64 actualByteCount
)
308 IOPolledFilePollers
* vars
= (IOPolledFilePollers
*) parameter
;
310 vars
->ioStatus
= status
;
313 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
316 IOStartPolledIO(IOPolledFilePollers
* vars
,
317 uint32_t operation
, uint32_t bufferOffset
,
318 uint64_t deviceOffset
, uint64_t length
)
321 IOPolledInterface
* poller
;
322 IOPolledCompletion completion
;
324 err
= vars
->ioStatus
;
325 if (kIOReturnSuccess
!= err
) return (err
);
327 completion
.target
= 0;
328 completion
.action
= &IOPolledIOComplete
;
329 completion
.parameter
= vars
;
333 poller
= (IOPolledInterface
*) vars
->pollers
->getObject(0);
334 err
= poller
->startIO(operation
, bufferOffset
, deviceOffset
, length
, completion
);
336 HIBLOG("IOPolledInterface::startIO[%d] 0x%x\n", 0, err
);
341 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
344 IOPolledFilePollersIODone(IOPolledFilePollers
* vars
, bool abortable
)
346 IOReturn err
= kIOReturnSuccess
;
348 IOPolledInterface
* poller
;
349 AbsoluteTime deadline
;
351 if (!vars
->io
) return (kIOReturnSuccess
);
353 abortable
&= vars
->abortable
;
355 clock_interval_to_deadline(2000, kMillisecondScale
, &deadline
);
357 while (-1 == vars
->ioStatus
)
360 (poller
= (IOPolledInterface
*) vars
->pollers
->getObject(idx
));
364 newErr
= poller
->checkForWork();
365 if ((newErr
== kIOReturnAborted
) && !abortable
)
366 newErr
= kIOReturnSuccess
;
367 if (kIOReturnSuccess
== err
)
370 if ((false) && (kIOReturnSuccess
== err
) && (mach_absolute_time() > AbsoluteTime_to_scalar(&deadline
)))
372 HIBLOG("IOPolledInterface::forced timeout\n");
373 vars
->ioStatus
= kIOReturnTimeout
;
379 if ((kIOReturnSuccess
== err
) && abortable
&& hibernate_should_abort())
381 err
= kIOReturnAborted
;
382 HIBLOG("IOPolledInterface::checkForWork sw abort\n");
388 HIBLOG("IOPolledInterface::checkForWork[%d] 0x%x\n", idx
, err
);
392 err
= vars
->ioStatus
;
393 if (kIOReturnSuccess
!= err
) HIBLOG("IOPolledInterface::ioStatus 0x%x\n", err
);
399 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
400 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
402 struct _OpenFileContext
409 file_extent_callback(void * ref
, uint64_t start
, uint64_t length
)
411 _OpenFileContext
* ctx
= (_OpenFileContext
*) ref
;
412 IOPolledFileExtent extent
;
414 extent
.start
= start
;
415 extent
.length
= length
;
416 ctx
->extents
->appendBytes(&extent
, sizeof(extent
));
420 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
423 IOCopyMediaForDev(dev_t device
)
425 OSDictionary
* matching
;
428 IOService
* result
= 0;
430 matching
= IOService::serviceMatching("IOMedia");
435 num
= OSNumber::withNumber(major(device
), 32);
438 matching
->setObject(kIOBSDMajorKey
, num
);
440 num
= OSNumber::withNumber(minor(device
), 32);
443 matching
->setObject(kIOBSDMinorKey
, num
);
447 iter
= IOService::getMatchingServices(matching
);
450 result
= (IOService
*) iter
->getNextObject();
462 IOGetVolumeCryptKey(dev_t block_dev
, OSString
** pKeyUUID
,
463 uint8_t * volumeCryptKey
, size_t keySize
)
467 OSString
* keyUUID
= 0;
468 OSString
* keyStoreUUID
= 0;
469 uuid_t volumeKeyUUID
;
470 aks_volume_key_t vek
;
472 static IOService
* sKeyStore
;
474 part
= IOCopyMediaForDev(block_dev
);
475 if (!part
) return (kIOReturnNotFound
);
477 err
= part
->callPlatformFunction(PLATFORM_FUNCTION_GET_MEDIA_ENCRYPTION_KEY_UUID
, false,
478 (void *) &keyUUID
, (void *) &keyStoreUUID
, NULL
, NULL
);
479 if ((kIOReturnSuccess
== err
) && keyUUID
&& keyStoreUUID
)
481 // IOLog("got volume key %s\n", keyStoreUUID->getCStringNoCopy());
484 sKeyStore
= (IOService
*) IORegistryEntry::fromPath(AKS_SERVICE_PATH
, gIOServicePlane
);
486 err
= uuid_parse(keyStoreUUID
->getCStringNoCopy(), volumeKeyUUID
);
488 err
= kIOReturnNoResources
;
489 if (kIOReturnSuccess
== err
)
490 err
= sKeyStore
->callPlatformFunction(gAKSGetKey
, true, volumeKeyUUID
, &vek
, NULL
, NULL
);
491 if (kIOReturnSuccess
!= err
)
492 IOLog("volume key err 0x%x\n", err
);
495 if (vek
.key
.keybytecount
< keySize
) keySize
= vek
.key
.keybytecount
;
496 bcopy(&vek
.key
.keybytes
[0], volumeCryptKey
, keySize
);
498 bzero(&vek
, sizeof(vek
));
502 if (pKeyUUID
) *pKeyUUID
= keyUUID
;
507 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
510 IOPolledFileOpen(const char * filename
,
511 uint64_t setFileSize
, uint64_t fsFreeSize
,
512 void * write_file_addr
, size_t write_file_len
,
513 IOPolledFileIOVars
** fileVars
,
515 uint8_t * volumeCryptKey
, size_t keySize
)
517 IOReturn err
= kIOReturnSuccess
;
518 IOPolledFileIOVars
* vars
;
519 _OpenFileContext ctx
;
520 OSData
* extentsData
;
522 IOService
* part
= 0;
525 AbsoluteTime startTime
, endTime
;
528 vars
= IONew(IOPolledFileIOVars
, 1);
529 if (!vars
) return (kIOReturnNoMemory
);
530 bzero(vars
, sizeof(*vars
));
531 vars
->allocated
= true;
535 extentsData
= OSData::withCapacity(32);
536 ctx
.extents
= extentsData
;
538 clock_get_uptime(&startTime
);
540 vars
->fileRef
= kern_open_file_for_direct_io(filename
,
541 (write_file_addr
!= NULL
) || (0 != setFileSize
),
542 &file_extent_callback
, &ctx
,
546 0, write_file_addr
, write_file_len
,
554 uint32_t msDelay
= (131071 & random());
555 HIBLOG("sleep %d\n", msDelay
);
558 clock_get_uptime(&endTime
);
559 SUB_ABSOLUTETIME(&endTime
, &startTime
);
560 absolutetime_to_nanoseconds(endTime
, &nsec
);
562 if (!vars
->fileRef
) err
= kIOReturnNoSpace
;
564 HIBLOG("kern_open_file_for_direct_io took %qd ms\n", nsec
/ 1000000ULL);
565 if (kIOReturnSuccess
!= err
) break;
567 HIBLOG("Opened file %s, size %qd, extents %ld, maxio %qx ssd %d\n", filename
, ctx
.size
,
568 (extentsData
->getLength() / sizeof(IOPolledFileExtent
)) - 1,
569 vars
->maxiobytes
, kIOPolledFileSSD
& vars
->flags
);
570 assert(!vars
->block0
);
571 if (extentsData
->getLength() < sizeof(IOPolledFileExtent
))
573 err
= kIOReturnNoSpace
;
577 vars
->fileSize
= ctx
.size
;
578 vars
->extentMap
= (IOPolledFileExtent
*) extentsData
->getBytesNoCopy();
580 part
= IOCopyMediaForDev(image_dev
);
583 err
= kIOReturnNotFound
;
587 if (!(vars
->pollers
= IOPolledFilePollers::copyPollers(part
))) break;
589 if ((num
= OSDynamicCast(OSNumber
, part
->getProperty(kIOMediaPreferredBlockSizeKey
))))
590 vars
->blockSize
= num
->unsigned32BitValue();
591 if (vars
->blockSize
< 4096) vars
->blockSize
= 4096;
593 HIBLOG("polled file major %d, minor %d, blocksize %ld, pollers %d\n",
594 major(image_dev
), minor(image_dev
), (long)vars
->blockSize
,
595 vars
->pollers
->pollers
->getCount());
597 OSString
* keyUUID
= NULL
;
600 err
= IOGetVolumeCryptKey(block_dev
, &keyUUID
, volumeCryptKey
, keySize
);
604 vars
->fileExtents
= extentsData
;
610 #if defined(__i386__) || defined(__x86_64__)
611 char str2
[24 + sizeof(uuid_string_t
) + 2];
614 snprintf(str2
, sizeof(str2
), "%qx:%s",
615 vars
->extentMap
[0].start
, keyUUID
->getCStringNoCopy());
617 snprintf(str2
, sizeof(str2
), "%qx", vars
->extentMap
[0].start
);
619 err
= IOService::getPlatform()->callPlatformFunction(
620 gIOCreateEFIDevicePathSymbol
, false,
621 (void *) part
, (void *) str2
,
622 (void *) (uintptr_t) true, (void *) &data
);
625 err
= kIOReturnSuccess
;
627 if (kIOReturnSuccess
!= err
)
629 HIBLOG("error 0x%x getting path\n", err
);
637 if (kIOReturnSuccess
!= err
)
639 HIBLOG("error 0x%x opening polled file\n", err
);
640 IOPolledFileClose(&vars
, 0, 0, 0, 0, 0);
643 if (part
) part
->release();
648 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
651 IOPolledFileClose(IOPolledFileIOVars
** pVars
,
652 off_t write_offset
, void * addr
, size_t write_length
,
653 off_t discard_offset
, off_t discard_end
)
655 IOPolledFileIOVars
* vars
;
658 if (!vars
) return(kIOReturnSuccess
);
662 kern_close_file_for_direct_io(vars
->fileRef
, write_offset
, addr
, write_length
,
663 discard_offset
, discard_end
);
664 vars
->fileRef
= NULL
;
666 if (vars
->fileExtents
)
668 vars
->fileExtents
->release();
669 vars
->fileExtents
= 0;
673 vars
->pollers
->release();
677 if (vars
->allocated
) IODelete(vars
, IOPolledFileIOVars
, 1);
678 else bzero(vars
, sizeof(IOPolledFileIOVars
));
681 return (kIOReturnSuccess
);
684 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
687 IOPolledFilePollersSetup(IOPolledFileIOVars
* vars
,
692 err
= kIOReturnSuccess
;
695 if (!vars
->pollers
->openCount
)
697 err
= IOPolledFilePollersProbe(vars
->pollers
);
698 if (kIOReturnSuccess
!= err
) break;
700 err
= IOPolledFilePollersOpen(vars
, openState
, false);
701 if (kIOReturnSuccess
!= err
) break;
702 if ((kIOPolledPreflightState
== openState
) || (kIOPolledPreflightCoreDumpState
== openState
))
704 vars
->pollers
->openCount
++;
706 vars
->pollers
->io
= false;
707 vars
->buffer
= (uint8_t *) vars
->pollers
->ioBuffer
->getBytesNoCopy();
708 vars
->bufferHalf
= 0;
709 vars
->bufferOffset
= 0;
710 vars
->bufferSize
= (vars
->pollers
->ioBuffer
->getLength() >> 1);
712 if (vars
->maxiobytes
< vars
->bufferSize
) vars
->bufferSize
= vars
->maxiobytes
;
716 if (kIOReturnSuccess
!= err
) HIBLOG("IOPolledFilePollersSetup(%d) error 0x%x\n", openState
, err
);
722 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
725 IOPolledFileSeek(IOPolledFileIOVars
* vars
, uint64_t position
)
727 IOPolledFileExtent
* extentMap
;
729 extentMap
= vars
->extentMap
;
731 vars
->position
= position
;
733 while (position
>= extentMap
->length
)
735 position
-= extentMap
->length
;
739 vars
->currentExtent
= extentMap
;
740 vars
->extentRemaining
= extentMap
->length
- position
;
741 vars
->extentPosition
= vars
->position
- position
;
743 if (vars
->bufferSize
<= vars
->extentRemaining
)
744 vars
->bufferLimit
= vars
->bufferSize
;
746 vars
->bufferLimit
= vars
->extentRemaining
;
748 return (kIOReturnSuccess
);
751 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
754 IOPolledFileWrite(IOPolledFileIOVars
* vars
,
755 const uint8_t * bytes
, IOByteCount size
,
756 IOPolledFileCryptVars
* cryptvars
)
758 IOReturn err
= kIOReturnSuccess
;
766 // seek to end of block & flush
767 size
= vars
->position
& (vars
->blockSize
- 1);
769 size
= vars
->blockSize
- size
;
771 // use some garbage for the fill
772 bytes
= vars
->buffer
+ vars
->bufferOffset
;
775 copy
= vars
->bufferLimit
- vars
->bufferOffset
;
783 bcopy(bytes
, vars
->buffer
+ vars
->bufferHalf
+ vars
->bufferOffset
, copy
);
787 bzero(vars
->buffer
+ vars
->bufferHalf
+ vars
->bufferOffset
, copy
);
790 vars
->bufferOffset
+= copy
;
791 vars
->position
+= copy
;
793 if (flush
&& vars
->bufferOffset
)
795 uint64_t offset
= (vars
->position
- vars
->bufferOffset
796 - vars
->extentPosition
+ vars
->currentExtent
->start
);
797 uint32_t length
= (vars
->bufferOffset
);
800 if (cryptvars
&& vars
->encryptStart
801 && (vars
->position
> vars
->encryptStart
)
802 && ((vars
->position
- length
) < vars
->encryptEnd
))
804 AbsoluteTime startTime
, endTime
;
806 uint64_t encryptLen
, encryptStart
;
807 encryptLen
= vars
->position
- vars
->encryptStart
;
808 if (encryptLen
> length
)
810 encryptStart
= length
- encryptLen
;
811 if (vars
->position
> vars
->encryptEnd
)
812 encryptLen
-= (vars
->position
- vars
->encryptEnd
);
814 clock_get_uptime(&startTime
);
816 // encrypt the buffer
817 aes_encrypt_cbc(vars
->buffer
+ vars
->bufferHalf
+ encryptStart
,
818 &cryptvars
->aes_iv
[0],
819 encryptLen
/ AES_BLOCK_SIZE
,
820 vars
->buffer
+ vars
->bufferHalf
+ encryptStart
,
821 &cryptvars
->ctx
.encrypt
);
823 clock_get_uptime(&endTime
);
824 ADD_ABSOLUTETIME(&vars
->cryptTime
, &endTime
);
825 SUB_ABSOLUTETIME(&vars
->cryptTime
, &startTime
);
826 vars
->cryptBytes
+= encryptLen
;
828 // save initial vector for following encrypts
829 bcopy(vars
->buffer
+ vars
->bufferHalf
+ encryptStart
+ encryptLen
- AES_BLOCK_SIZE
,
830 &cryptvars
->aes_iv
[0],
835 err
= IOPolledFilePollersIODone(vars
->pollers
, true);
836 if (kIOReturnSuccess
!= err
)
839 if (vars
->position
& (vars
->blockSize
- 1)) HIBLOG("misaligned file pos %qx\n", vars
->position
);
840 //if (length != vars->bufferSize) HIBLOG("short write of %qx ends@ %qx\n", length, offset + length);
842 err
= IOStartPolledIO(vars
->pollers
, kIOPolledWrite
, vars
->bufferHalf
, offset
, length
);
843 if (kIOReturnSuccess
!= err
)
845 vars
->pollers
->io
= true;
847 vars
->extentRemaining
-= vars
->bufferOffset
;
848 if (!vars
->extentRemaining
)
850 vars
->currentExtent
++;
851 vars
->extentRemaining
= vars
->currentExtent
->length
;
852 vars
->extentPosition
= vars
->position
;
855 vars
->bufferHalf
= vars
->bufferHalf
? 0 : vars
->bufferSize
;
856 vars
->bufferOffset
= 0;
857 if (vars
->bufferSize
<= vars
->extentRemaining
)
858 vars
->bufferLimit
= vars
->bufferSize
;
860 vars
->bufferLimit
= vars
->extentRemaining
;
862 if (!vars
->extentRemaining
)
864 err
= kIOReturnOverrun
;
876 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
879 IOPolledFileRead(IOPolledFileIOVars
* vars
,
880 uint8_t * bytes
, IOByteCount size
,
881 IOPolledFileCryptVars
* cryptvars
)
883 IOReturn err
= kIOReturnSuccess
;
886 // bytesWritten += size;
890 copy
= vars
->bufferLimit
- vars
->bufferOffset
;
896 bcopy(vars
->buffer
+ vars
->bufferHalf
+ vars
->bufferOffset
, bytes
, copy
);
900 vars
->bufferOffset
+= copy
;
901 // vars->position += copy;
903 if ((vars
->bufferOffset
== vars
->bufferLimit
) && (vars
->position
< vars
->readEnd
))
905 if (!vars
->pollers
->io
) cryptvars
= 0;
906 err
= IOPolledFilePollersIODone(vars
->pollers
, true);
907 if (kIOReturnSuccess
!= err
)
910 if (vars
->position
& (vars
->blockSize
- 1)) HIBLOG("misaligned file pos %qx\n", vars
->position
);
912 vars
->position
+= vars
->lastRead
;
913 vars
->extentRemaining
-= vars
->lastRead
;
914 vars
->bufferLimit
= vars
->lastRead
;
916 if (!vars
->extentRemaining
)
918 vars
->currentExtent
++;
919 vars
->extentRemaining
= vars
->currentExtent
->length
;
920 vars
->extentPosition
= vars
->position
;
921 if (!vars
->extentRemaining
)
923 err
= kIOReturnOverrun
;
929 uint64_t lastReadLength
= vars
->lastRead
;
930 uint64_t offset
= (vars
->position
931 - vars
->extentPosition
+ vars
->currentExtent
->start
);
932 if (vars
->extentRemaining
<= vars
->bufferSize
)
933 length
= vars
->extentRemaining
;
935 length
= vars
->bufferSize
;
936 if ((length
+ vars
->position
) > vars
->readEnd
)
937 length
= vars
->readEnd
- vars
->position
;
939 vars
->lastRead
= length
;
942 //if (length != vars->bufferSize) HIBLOG("short read of %qx ends@ %qx\n", length, offset + length);
943 err
= IOStartPolledIO(vars
->pollers
, kIOPolledRead
, vars
->bufferHalf
, offset
, length
);
944 if (kIOReturnSuccess
!= err
)
946 vars
->pollers
->io
= true;
949 vars
->bufferHalf
= vars
->bufferHalf
? 0 : vars
->bufferSize
;
950 vars
->bufferOffset
= 0;
955 uint8_t thisVector
[AES_BLOCK_SIZE
];
956 AbsoluteTime startTime
, endTime
;
958 // save initial vector for following decrypts
959 bcopy(&cryptvars
->aes_iv
[0], &thisVector
[0], AES_BLOCK_SIZE
);
960 bcopy(vars
->buffer
+ vars
->bufferHalf
+ lastReadLength
- AES_BLOCK_SIZE
,
961 &cryptvars
->aes_iv
[0], AES_BLOCK_SIZE
);
963 // decrypt the buffer
964 clock_get_uptime(&startTime
);
966 aes_decrypt_cbc(vars
->buffer
+ vars
->bufferHalf
,
968 lastReadLength
/ AES_BLOCK_SIZE
,
969 vars
->buffer
+ vars
->bufferHalf
,
970 &cryptvars
->ctx
.decrypt
);
972 clock_get_uptime(&endTime
);
973 ADD_ABSOLUTETIME(&vars
->cryptTime
, &endTime
);
974 SUB_ABSOLUTETIME(&vars
->cryptTime
, &startTime
);
975 vars
->cryptBytes
+= lastReadLength
;
985 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */