/
IOStorage.cpp
467 lines (384 loc) · 14.9 KB
/
IOStorage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Copyright (c) 1998-2014 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <IOKit/assert.h>
#include <IOKit/IOLib.h>
#include <IOKit/storage/IOStorage.h>
#define super IOService
OSDefineMetaClassAndAbstractStructors(IOStorage, IOService)
#ifndef __LP64__
#define kIOStorageAttributesUnsupported ( ( IOStorage::ExpansionData * ) 1 )
IOStorageAttributes gIOStorageAttributesUnsupported = { kIOStorageOptionReserved };
extern "C" void _ZN9IOStorage4readEP9IOServiceyP18IOMemoryDescriptor19IOStorageCompletion( IOStorage *, IOService *, UInt64, IOMemoryDescriptor *, IOStorageCompletion );
extern "C" void _ZN9IOStorage5writeEP9IOServiceyP18IOMemoryDescriptor19IOStorageCompletion( IOStorage *, IOService *, UInt64, IOMemoryDescriptor *, IOStorageCompletion );
extern "C" void _ZN9IOStorage4readEP9IOServiceyP18IOMemoryDescriptorP19IOStorageAttributesP19IOStorageCompletion( IOStorage *, IOService *, UInt64, IOMemoryDescriptor *, IOStorageAttributes *, IOStorageCompletion * );
extern "C" void _ZN9IOStorage5writeEP9IOServiceyP18IOMemoryDescriptorP19IOStorageAttributesP19IOStorageCompletion( IOStorage *, IOService *, UInt64, IOMemoryDescriptor *, IOStorageAttributes *, IOStorageCompletion * );
#define storageAttributes( storage ) ( ( OSMemberFunctionCast( void *, storage, ( void ( IOStorage::* )( IOService *, UInt64, IOMemoryDescriptor *, IOStorageCompletion ) ) &IOStorage::read ) == _ZN9IOStorage4readEP9IOServiceyP18IOMemoryDescriptor19IOStorageCompletion ) && \
( OSMemberFunctionCast( void *, storage, ( void ( IOStorage::* )( IOService *, UInt64, IOMemoryDescriptor *, IOStorageCompletion ) ) &IOStorage::write ) == _ZN9IOStorage5writeEP9IOServiceyP18IOMemoryDescriptor19IOStorageCompletion ) && \
( OSMemberFunctionCast( void *, storage, ( void ( IOStorage::* )( IOService *, UInt64, IOMemoryDescriptor *, IOStorageAttributes *, IOStorageCompletion * ) ) &IOStorage::read ) != _ZN9IOStorage4readEP9IOServiceyP18IOMemoryDescriptorP19IOStorageAttributesP19IOStorageCompletion ) && \
( OSMemberFunctionCast( void *, storage, ( void ( IOStorage::* )( IOService *, UInt64, IOMemoryDescriptor *, IOStorageAttributes *, IOStorageCompletion * ) ) &IOStorage::write ) != _ZN9IOStorage5writeEP9IOServiceyP18IOMemoryDescriptorP19IOStorageAttributesP19IOStorageCompletion ) )
#endif /* !__LP64__ */
class IOStorageSyncerLock
{
protected:
IOLock * _lock;
public:
inline IOStorageSyncerLock( )
{
_lock = IOLockAlloc( );
}
inline ~IOStorageSyncerLock( )
{
if ( _lock ) IOLockFree( _lock );
}
inline void lock( )
{
IOLockLock( _lock );
}
inline void unlock( )
{
IOLockUnlock( _lock );
}
inline void sleep( void * event )
{
IOLockSleep( _lock, event, THREAD_UNINT );
}
inline void wakeup( void * event )
{
IOLockWakeup( _lock, event, false );
}
};
static IOStorageSyncerLock gIOStorageSyncerLock;
class IOStorageSyncer
{
protected:
IOReturn _status;
bool _wakeup;
public:
IOStorageSyncer( )
{
_wakeup = false;
}
IOReturn wait( )
{
gIOStorageSyncerLock.lock( );
while ( _wakeup == false )
{
gIOStorageSyncerLock.sleep( this );
}
gIOStorageSyncerLock.unlock( );
return _status;
}
void signal( IOReturn status )
{
_status = status;
gIOStorageSyncerLock.lock( );
_wakeup = true;
gIOStorageSyncerLock.wakeup( this );
gIOStorageSyncerLock.unlock( );
}
};
static void storageCompletion(void * target,
void * parameter,
IOReturn status,
UInt64 actualByteCount)
{
//
// Internal completion routine for synchronous versions of read and write.
//
if (parameter) *((UInt64 *)parameter) = actualByteCount;
((IOStorageSyncer *)target)->signal(status);
}
#ifndef __LP64__
bool IOStorage::init(OSDictionary * properties)
{
//
// Initialize this object's minimal state.
//
if ( super::init( properties ) == false )
{
return false;
}
if ( storageAttributes( this ) == false )
{
IOStorage::_expansionData = kIOStorageAttributesUnsupported;
}
if ( IOStorage::_expansionData )
{
OSDictionary * features;
features = OSDictionary::withCapacity( 1 );
if ( features )
{
setProperty( kIOStorageFeaturesKey, features );
features->release( );
}
}
return true;
}
void IOStorage::complete(IOStorageCompletion completion,
IOReturn status,
UInt64 actualByteCount)
{
//
// Invokes the specified completion action of the read/write request. If
// the completion action is unspecified, no action is taken. This method
// serves simply as a convenience to storage subclass developers.
//
complete( &completion, status, actualByteCount );
}
#endif /* !__LP64__ */
void IOStorage::complete(IOStorageCompletion * completion,
IOReturn status,
UInt64 actualByteCount)
{
//
// Invokes the specified completion action of the read/write request. If
// the completion action is unspecified, no action is taken. This method
// serves simply as a convenience to storage subclass developers.
//
if ( completion && completion->action )
{
( completion->action )( completion->target, completion->parameter, status, actualByteCount );
}
}
bool IOStorage::open(IOService * client,
IOOptionBits options,
IOStorageAccess access)
{
//
// Ask the storage object for permission to access its contents; the method
// is equivalent to IOService::open(), but with the correct parameter types.
//
return super::open(client, options, (void *) (uintptr_t) access);
}
IOReturn IOStorage::read(IOService * client,
UInt64 byteStart,
IOMemoryDescriptor * buffer,
IOStorageAttributes * attributes,
UInt64 * actualByteCount)
{
//
// Read data from the storage object at the specified byte offset into the
// specified buffer, synchronously. When the read completes, this method
// will return to the caller. The actual byte count field is optional.
//
IOStorageCompletion completion;
IOStorageSyncer syncer;
// Fill in the completion information for this request.
completion.target = &syncer;
completion.action = storageCompletion;
completion.parameter = actualByteCount;
// Issue the asynchronous read.
read(client, byteStart, buffer, attributes, &completion);
// Wait for the read to complete.
return syncer.wait();
}
IOReturn IOStorage::write(IOService * client,
UInt64 byteStart,
IOMemoryDescriptor * buffer,
IOStorageAttributes * attributes,
UInt64 * actualByteCount)
{
//
// Write data into the storage object at the specified byte offset from the
// specified buffer, synchronously. When the write completes, this method
// will return to the caller. The actual byte count field is optional.
//
IOStorageCompletion completion;
IOStorageSyncer syncer;
// Fill in the completion information for this request.
completion.target = &syncer;
completion.action = storageCompletion;
completion.parameter = actualByteCount;
// Issue the asynchronous write.
write(client, byteStart, buffer, attributes, &completion);
// Wait for the write to complete.
return syncer.wait();
}
#ifndef __LP64__
void IOStorage::read(IOService * client,
UInt64 byteStart,
IOMemoryDescriptor * buffer,
IOStorageCompletion completion)
{
//
// Read data from the storage object at the specified byte offset into the
// specified buffer, asynchronously. When the read completes, the caller
// will be notified via the specified completion action.
//
// The buffer will be retained for the duration of the read.
//
if ( IOStorage::_expansionData == kIOStorageAttributesUnsupported )
{
read( client, byteStart, buffer, &gIOStorageAttributesUnsupported, &completion );
}
else
{
read( client, byteStart, buffer, NULL, &completion );
}
}
void IOStorage::write(IOService * client,
UInt64 byteStart,
IOMemoryDescriptor * buffer,
IOStorageCompletion completion)
{
//
// Write data into the storage object at the specified byte offset from the
// specified buffer, asynchronously. When the write completes, the caller
// will be notified via the specified completion action.
//
// The buffer will be retained for the duration of the write.
//
if ( IOStorage::_expansionData == kIOStorageAttributesUnsupported )
{
write( client, byteStart, buffer, &gIOStorageAttributesUnsupported, &completion );
}
else
{
write( client, byteStart, buffer, NULL, &completion );
}
}
void IOStorage::read(IOService * client,
UInt64 byteStart,
IOMemoryDescriptor * buffer,
IOStorageAttributes * attributes,
IOStorageCompletion * completion)
{
//
// Read data from the storage object at the specified byte offset into the
// specified buffer, asynchronously. When the read completes, the caller
// will be notified via the specified completion action.
//
// The buffer will be retained for the duration of the read.
//
if ( attributes && attributes->options )
{
complete( completion, kIOReturnUnsupported );
}
else
{
read( client, byteStart, buffer, completion ? *completion : ( IOStorageCompletion ) { 0 } );
}
}
void IOStorage::write(IOService * client,
UInt64 byteStart,
IOMemoryDescriptor * buffer,
IOStorageAttributes * attributes,
IOStorageCompletion * completion)
{
//
// Write data into the storage object at the specified byte offset from the
// specified buffer, asynchronously. When the write completes, the caller
// will be notified via the specified completion action.
//
// The buffer will be retained for the duration of the write.
//
if ( attributes && attributes->options )
{
complete( completion, kIOReturnUnsupported );
}
else
{
write( client, byteStart, buffer, completion ? *completion : ( IOStorageCompletion ) { 0 } );
}
}
#endif /* !__LP64__ */
IOReturn IOStorage::discard(IOService * client,
UInt64 byteStart,
UInt64 byteCount)
{
//
// Delete unused data from the storage object at the specified byte offset,
// synchronously.
//
return kIOReturnUnsupported;
}
IOReturn IOStorage::unmap(IOService * client,
IOStorageExtent * extents,
UInt32 extentsCount,
UInt32 options)
{
//
// Delete unused data from the storage object at the specified byte offsets,
// synchronously.
//
return kIOReturnUnsupported;
}
bool IOStorage::lockPhysicalExtents(IOService * client)
{
//
// Lock the contents of the storage object against relocation temporarily,
// for the purpose of getting physical extents.
//
return false;
}
IOStorage * IOStorage::copyPhysicalExtent(IOService * client,
UInt64 * byteStart,
UInt64 * byteCount)
{
//
// Convert the specified byte offset into a physical byte offset, relative
// to a physical storage object. This call should only be made within the
// context of lockPhysicalExtents().
//
return NULL;
}
void IOStorage::unlockPhysicalExtents(IOService * client)
{
//
// Unlock the contents of the storage object for relocation again. This
// call must balance a successful call to lockPhysicalExtents().
//
return;
}
IOReturn IOStorage::setPriority(IOService * client,
IOStorageExtent * extents,
UInt32 extentsCount,
IOStoragePriority priority)
{
//
// Reprioritize read or write requests at the specified byte offsets.
//
return kIOReturnUnsupported;
}
OSMetaClassDefineReservedUsed(IOStorage, 0);
OSMetaClassDefineReservedUsed(IOStorage, 1);
OSMetaClassDefineReservedUsed(IOStorage, 2);
OSMetaClassDefineReservedUsed(IOStorage, 3);
OSMetaClassDefineReservedUsed(IOStorage, 4);
#ifdef __LP64__
OSMetaClassDefineReservedUnused(IOStorage, 5);
OSMetaClassDefineReservedUnused(IOStorage, 6);
OSMetaClassDefineReservedUnused(IOStorage, 7);
#else /* !__LP64__ */
OSMetaClassDefineReservedUsed(IOStorage, 5);
OSMetaClassDefineReservedUsed(IOStorage, 6);
OSMetaClassDefineReservedUsed(IOStorage, 7);
#endif /* !__LP64__ */
OSMetaClassDefineReservedUnused(IOStorage, 8);
OSMetaClassDefineReservedUnused(IOStorage, 9);
OSMetaClassDefineReservedUnused(IOStorage, 10);
OSMetaClassDefineReservedUnused(IOStorage, 11);
OSMetaClassDefineReservedUnused(IOStorage, 12);
OSMetaClassDefineReservedUnused(IOStorage, 13);
OSMetaClassDefineReservedUnused(IOStorage, 14);
OSMetaClassDefineReservedUnused(IOStorage, 15);