forked from apple-oss-distributions/BootCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbc_utils.cpp
More file actions
397 lines (332 loc) · 12.7 KB
/
bc_utils.cpp
File metadata and controls
397 lines (332 loc) · 12.7 KB
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
#include <sys/types.h>
#include <APFS/APFSConstants.h>
#include <IOKit/IOBSD.h>
#include <IOKit/storage/IOMedia.h>
#include <sys/systm.h>
#include "bc_utils.h"
#include "BootCache_private.h"
#include "BootCache_debug.h"
#include <sys/vnode.h>
#include <uuid/uuid.h>
#include <apfs/apfs_fsctl.h>
// Grabbed from CoreStorageUserLib.h. Not sure where kernel lib is, but since CS is going away, I don't really exect it'll change
#ifndef kCoreStorageLogicalClassName
#define kCoreStorageLogicalClassName "CoreStorageLogical"
#endif
#ifndef kCoreStorageIsCompositedKey
#define kCoreStorageIsCompositedKey "CoreStorage CPDK" // CFBoolean
#endif
// Returns 0 on successfully checking, non-0 on error
static int
apfs_container_has_encrypted_or_rolling_volumes(IORegistryEntry *container, bool * _Nullable has_encrypted_volumes_out, bool * _Nullable has_rolling_volumes_out)
{
IORegistryEntry *volume = NULL;
OSIterator *iterator = NULL;
if (has_encrypted_volumes_out) {
*has_encrypted_volumes_out = false;
}
if (has_rolling_volumes_out) {
*has_rolling_volumes_out = false;
}
iterator = container->getChildIterator(gIOServicePlane);
if (!iterator) {
// Assume true since we're only checking to avoid caching unsupported containers, so this is the safer answer when we don't know the truth
if (has_encrypted_volumes_out) {
*has_encrypted_volumes_out = true;
}
if (has_rolling_volumes_out) {
*has_rolling_volumes_out = true;
}
return ENOENT;
}
while ((volume = (IORegistryEntry *)iterator->getNextObject()) != NULL) {
if (volume->metaCast(APFS_VOLUME_OBJECT) == NULL) {
continue;
}
if (has_encrypted_volumes_out && *has_encrypted_volumes_out == false) {
OSBoolean *boolean = OSDynamicCast(OSBoolean, volume->getProperty(kAPFSEncryptedKey));
if (boolean == kOSBooleanTrue) {
*has_encrypted_volumes_out = true;
}
}
if (has_rolling_volumes_out && *has_rolling_volumes_out == false) {
OSBoolean *boolean = OSDynamicCast(OSBoolean, volume->getProperty(kAPFSEncryptionRolling));
if (boolean == kOSBooleanTrue) {
*has_rolling_volumes_out = true;
}
}
if ((!has_rolling_volumes_out || *has_rolling_volumes_out != false) &&
(!has_encrypted_volumes_out || *has_encrypted_volumes_out != false)) {
break;
}
}
iterator->release();
return 0;
}
void
bc_get_volume_info(dev_t volume_dev,
uint32_t * _Nullable fs_flags_out,
dev_t * _Nullable apfs_container_dev_out,
uuid_t _Nullable apfs_container_uuid_out,
bool * _Nullable apfs_container_has_encrypted_volumes_out,
bool * _Nullable apfs_container_has_rolling_volumes_out)
{
if (fs_flags_out) {
*fs_flags_out = 0;
}
if (apfs_container_dev_out) {
*apfs_container_dev_out = nulldev();
}
if (apfs_container_uuid_out) {
uuid_clear(apfs_container_uuid_out);
}
if (apfs_container_has_encrypted_volumes_out) {
*apfs_container_has_encrypted_volumes_out = true;
}
if (apfs_container_has_rolling_volumes_out) {
*apfs_container_has_rolling_volumes_out = true;
}
if (volume_dev == nulldev() || volume_dev == NODEV) {
return;
}
OSDictionary *target, *filter;
OSNumber *number;
IOService *serviceMatchingDev;
IORegistryEntry *volume, *container, *media, *scheme;
OSString* uuid_str;
if ((target = IOService::serviceMatching(kIOMediaClass)) != NULL) {
filter = OSDictionary::withCapacity(2);
number = OSNumber::withNumber(major(volume_dev), 32);
filter->setObject(kIOBSDMajorKey, number);
number->release();
number = OSNumber::withNumber(minor(volume_dev), 32);
filter->setObject(kIOBSDMinorKey, number);
number->release();
target->setObject(gIOPropertyMatchKey, filter);
filter->release();
if ((serviceMatchingDev = IOService::copyMatchingService(target)) != NULL) {
serviceMatchingDev->waitQuiet();
bool isApfsSnapshot = false;
if (serviceMatchingDev->metaCast(APFS_SNAPSHOT_OBJECT) != NULL) {
debug("Found APFS snapshot for device %#x", volume_dev);
isApfsSnapshot = true;
if ((volume = serviceMatchingDev->getParentEntry(gIOServicePlane)) != NULL && volume->metaCast(APFS_VOLUME_OBJECT) != NULL) {
// Good case
} else {
message("No volume for APFS snapshot for device %#x: found %s", volume_dev, volume ? volume->getName(gIOServicePlane) : "null volume");
volume = NULL;
}
} else {
// If not an apfs snapshot, assume it's a volume
volume = serviceMatchingDev;
}
if (volume) {
if (volume->metaCast(APFS_VOLUME_OBJECT) != NULL) {
if (fs_flags_out) {
*fs_flags_out |= BC_FS_APFS;
if (isApfsSnapshot) {
*fs_flags_out |= BC_FS_APFS_SNAPSHOT;
} else {
*fs_flags_out |= BC_FS_APFS_VOLUME;
}
OSBoolean *boolean = OSDynamicCast(OSBoolean, volume->getProperty(kAPFSEncryptedKey));
if (boolean == kOSBooleanTrue) {
*fs_flags_out |= BC_FS_APFS_ENCRYPTED;
}
boolean = OSDynamicCast(OSBoolean, volume->getProperty(kAPFSEncryptionRolling));
if (boolean == kOSBooleanTrue) {
*fs_flags_out |= BC_FS_APFS_ENCRYPTION_ROLLING;
}
}
if ((container = volume->getParentEntry(gIOServicePlane)) != NULL && container->metaCast(APFS_CONTAINER_OBJECT) != NULL) {
if ((media = container->getParentEntry(gIOServicePlane)) != NULL && media->metaCast(APFS_MEDIA_OBJECT) != NULL) {
if ((scheme = media->getParentEntry(gIOServicePlane)) != NULL && scheme->metaCast(APFS_SCHEME_OBJECT) != NULL) {
if (apfs_container_uuid_out) {
if ((uuid_str = OSDynamicCast(OSString, container->getProperty(kIOMediaUUIDKey))) != NULL) {
uuid_parse(uuid_str->getCStringNoCopy(), apfs_container_uuid_out);
}
}
if (apfs_container_dev_out) {
OSNumber *dev_major, *dev_minor;
if ((dev_major = OSDynamicCast(OSNumber, media->getProperty(kIOBSDMajorKey))) != NULL &&
(dev_minor = OSDynamicCast(OSNumber, media->getProperty(kIOBSDMinorKey))) != NULL) {
*apfs_container_dev_out = makedev(dev_major->unsigned32BitValue(), dev_minor->unsigned32BitValue());
}
}
if (apfs_container_has_encrypted_volumes_out || apfs_container_has_rolling_volumes_out) {
apfs_container_has_encrypted_or_rolling_volumes(container, apfs_container_has_encrypted_volumes_out, apfs_container_has_rolling_volumes_out);
}
if (fs_flags_out) {
OSBoolean *boolean = OSDynamicCast(OSBoolean, scheme->getProperty(kAPFSContainerIsCompositedKey));
if (boolean == kOSBooleanTrue) {
*fs_flags_out |= BC_FS_APFS_FUSION;
}
}
} else {
message("No scheme for APFS media for device %#x", volume_dev);
}
} else {
message("No media for APFS container for device %#x", volume_dev);
}
} else {
message("No container for APFS volume for device %#x", volume_dev);
}
} else if (volume->metaCast(kCoreStorageLogicalClassName) != NULL) {
if (fs_flags_out) {
*fs_flags_out |= BC_FS_CS;
OSBoolean *boolean = OSDynamicCast(OSBoolean, volume->getProperty(kCoreStorageIsCompositedKey));
if (boolean == kOSBooleanTrue) {
*fs_flags_out |= BC_FS_CS_FUSION;
}
}
} else {
// Non-apfs and non-CoreStorage
}
}
serviceMatchingDev->release();
}
target->release();
}
}
/* group_uuid_out will be filled with the UUID of this device's group uuid,
* or zero'ed out if the device has no grouping.
*/
void
bc_get_group_uuid_for_dev(dev_t dev, uuid_t _Nonnull group_uuid_out)
{
if (!group_uuid_out) {
return;
}
uuid_clear(group_uuid_out);
if (dev == nulldev() || dev == NODEV) {
return;
}
OSDictionary *target, *filter;
OSNumber *number;
IOService *matchingService;
OSString* uuid_str;
if ((target = IOService::serviceMatching(kIOMediaClass)) != NULL) {
filter = OSDictionary::withCapacity(2);
number = OSNumber::withNumber(major(dev), 32);
filter->setObject(kIOBSDMajorKey, number);
number->release();
number = OSNumber::withNumber(minor(dev), 32);
filter->setObject(kIOBSDMinorKey, number);
number->release();
target->setObject(gIOPropertyMatchKey, filter);
filter->release();
if ((matchingService = IOService::copyMatchingService(target)) != NULL) {
matchingService->waitQuiet();
if (matchingService->metaCast(APFS_MEDIA_OBJECT) != NULL) {
// For APFS containers, the bsd device will match the container's parent APFS_MEDIA_OBJECT, so we need to search the APFS_MEDIA_OBJECT's children for an APFS container
OSIterator* iterator = matchingService->getChildIterator(gIOServicePlane);
if (iterator) {
IORegistryEntry* container = NULL;
bool foundContainer = false;
while ((container = (IORegistryEntry *)iterator->getNextObject()) != NULL) {
if (container->metaCast(APFS_CONTAINER_OBJECT)) {
foundContainer = true;
debug("Found apfs container via media for device %#x", dev);
if ((uuid_str = OSDynamicCast(OSString, container->getProperty(kIOMediaUUIDKey))) != NULL) {
uuid_parse(uuid_str->getCStringNoCopy(), group_uuid_out);
}
break;
}
}
if (!foundContainer) {
debug("No apfs container for media for device %#x", dev);
}
iterator->release();
} else {
message("No child iterator for media for device %#x", dev);
}
} else if (matchingService->metaCast(APFS_SNAPSHOT_OBJECT) != NULL) {
IORegistryEntry *volume = NULL, *container = NULL;
if ((volume = matchingService->getParentEntry(gIOServicePlane)) != NULL && volume->metaCast(APFS_VOLUME_OBJECT) != NULL) {
if ((container = volume->getParentEntry(gIOServicePlane)) != NULL && container->metaCast(APFS_CONTAINER_OBJECT) != NULL) {
debug("Found apfs container via snapshot for device %#x", dev);
if ((uuid_str = OSDynamicCast(OSString, container->getProperty(kIOMediaUUIDKey))) != NULL) {
uuid_parse(uuid_str->getCStringNoCopy(), group_uuid_out);
}
} else {
message("No container for APFS volume");
}
}else {
message("No volume for APFS snapshot");
}
} else if (matchingService->metaCast(APFS_VOLUME_OBJECT) != NULL) {
IORegistryEntry* container = NULL;
if ((container = matchingService->getParentEntry(gIOServicePlane)) != NULL && container->metaCast(APFS_CONTAINER_OBJECT) != NULL) {
debug("Found apfs container via volume for device %#x", dev);
if ((uuid_str = OSDynamicCast(OSString, container->getProperty(kIOMediaUUIDKey))) != NULL) {
uuid_parse(uuid_str->getCStringNoCopy(), group_uuid_out);
}
} else {
message("No container for APFS volume");
}
} else if (matchingService->metaCast(APFS_CONTAINER_OBJECT) != NULL) {
debug("Found apfs container directly for device %#x", dev);
if ((uuid_str = OSDynamicCast(OSString, matchingService->getProperty(kIOMediaUUIDKey))) != NULL) {
uuid_parse(uuid_str->getCStringNoCopy(), group_uuid_out);
}
} else if (matchingService->metaCast(kCoreStorageLogicalClassName) != NULL) {
// No groupings for CoreStorage
debug("No grouping for cs device %#x", dev);
} else {
// Non-apfs and non-CoreStorage, no groupings
debug("No grouping for non-apfs, non-cs device %#x", dev);
}
matchingService->release();
}
target->release();
}
}
// Get device string to pass to vnode_lookup for APFS container device
void
lookup_dev_name(dev_t dev, char* name, int nmlen)
{
if (!name || nmlen == 0) {
return;
}
name[0] = '\0';
if (dev == nulldev() || dev == NODEV) {
return;
}
OSDictionary *target, *filter;
OSNumber *number;
IOService *service;
OSString* string;
if ((target = IOService::serviceMatching(kIOMediaClass)) != NULL) {
filter = OSDictionary::withCapacity(2);
number = OSNumber::withNumber(major(dev), 32);
filter->setObject(kIOBSDMajorKey, number);
number->release();
number = OSNumber::withNumber(minor(dev), 32);
filter->setObject(kIOBSDMinorKey, number);
number->release();
target->setObject(gIOPropertyMatchKey, filter);
filter->release();
if ((service = IOService::copyMatchingService(target)) != NULL) {
service->waitQuiet();
if (service->metaCast(kIOMediaClass) != NULL && (string = OSDynamicCast(OSString, service->getProperty(kIOBSDNameKey))) != NULL) {
if (name != NULL) {
snprintf(name, nmlen, "/dev/%s", string->getCStringNoCopy());
}
}
service->release();
}
target->release();
}
}
u_int64_t
apfs_get_inode(vnode_t vp, u_int64_t lblkno, u_int64_t size, vfs_context_t vfs_context)
{
apfs_bc_getinodenum_t ioctlargs;
ioctlargs.bc_lblkno = lblkno;
ioctlargs.bc_bcount = (uint32_t)size;
ioctlargs.bc_inum = 0;
if (VNOP_IOCTL(vp, APFSIOC_BC_GETINODENUM, (caddr_t)&ioctlargs, 0, vfs_context)) {
return 0;
}
return ioctlargs.bc_inum;;
}