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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
|
/* $Id: DrvVD.cpp $ */
/** @file
* DrvVD - Generic VBox disk media driver.
*/
/*
* Copyright (C) 2006-2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_VD
#include <VBox/VBoxHDD.h>
#include <VBox/pdmdrv.h>
#include <VBox/pdmasynccompletion.h>
#include <iprt/alloc.h>
#include <iprt/assert.h>
#include <iprt/uuid.h>
#include <iprt/file.h>
#include <iprt/string.h>
#include <iprt/cache.h>
#include <iprt/tcp.h>
#include <iprt/semaphore.h>
#ifdef VBOX_WITH_INIP
/* All lwip header files are not C++ safe. So hack around this. */
RT_C_DECLS_BEGIN
#include <lwip/inet.h>
#include <lwip/tcp.h>
#include <lwip/sockets.h>
RT_C_DECLS_END
#endif /* VBOX_WITH_INIP */
#include "Builtins.h"
#ifdef VBOX_WITH_INIP
/* Small hack to get at lwIP initialized status */
extern bool DevINIPConfigured(void);
#endif /* VBOX_WITH_INIP */
/*******************************************************************************
* Defined types, constants and macros *
*******************************************************************************/
/** Converts a pointer to VDIDISK::IMedia to a PVBOXDISK. */
#define PDMIMEDIA_2_VBOXDISK(pInterface) \
( (PVBOXDISK)((uintptr_t)pInterface - RT_OFFSETOF(VBOXDISK, IMedia)) )
/** Converts a pointer to PDMDRVINS::IBase to a PPDMDRVINS. */
#define PDMIBASE_2_DRVINS(pInterface) \
( (PPDMDRVINS)((uintptr_t)pInterface - RT_OFFSETOF(PDMDRVINS, IBase)) )
/** Converts a pointer to PDMDRVINS::IBase to a PVBOXDISK. */
#define PDMIBASE_2_VBOXDISK(pInterface) \
( PDMINS_2_DATA(PDMIBASE_2_DRVINS(pInterface), PVBOXDISK) )
/** Converts a pointer to VBOXDISK::IMediaAsync to a PVBOXDISK. */
#define PDMIMEDIAASYNC_2_VBOXDISK(pInterface) \
( (PVBOXDISK)((uintptr_t)pInterface - RT_OFFSETOF(VBOXDISK, IMediaAsync)) )
/**
* VBox disk container, image information, private part.
*/
typedef struct VBOXIMAGE
{
/** Pointer to next image. */
struct VBOXIMAGE *pNext;
/** Pointer to list of VD interfaces. Per-image. */
PVDINTERFACE pVDIfsImage;
/** Common structure for the configuration information interface. */
VDINTERFACE VDIConfig;
} VBOXIMAGE, *PVBOXIMAGE;
/**
* Storage backend data.
*/
typedef struct DRVVDSTORAGEBACKEND
{
/** PDM async completion end point. */
PPDMASYNCCOMPLETIONENDPOINT pEndpoint;
/** The template. */
PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
/** Event semaphore for synchronous operations. */
RTSEMEVENT EventSem;
/** Flag whether a synchronous operation is currently pending. */
volatile bool fSyncIoPending;
/** Callback routine */
PFNVDCOMPLETED pfnCompleted;
} DRVVDSTORAGEBACKEND, *PDRVVDSTORAGEBACKEND;
/**
* VBox disk container media main structure, private part.
*/
typedef struct VBOXDISK
{
/** The VBox disk container. */
PVBOXHDD pDisk;
/** The media interface. */
PDMIMEDIA IMedia;
/** Pointer to the driver instance. */
PPDMDRVINS pDrvIns;
/** Flag whether suspend has changed image open mode to read only. */
bool fTempReadOnly;
/** Flag whether to use the runtime (true) or startup error facility. */
bool fErrorUseRuntime;
/** Pointer to list of VD interfaces. Per-disk. */
PVDINTERFACE pVDIfsDisk;
/** Common structure for the supported error interface. */
VDINTERFACE VDIError;
/** Callback table for error interface. */
VDINTERFACEERROR VDIErrorCallbacks;
/** Common structure for the supported TCP network stack interface. */
VDINTERFACE VDITcpNet;
/** Callback table for TCP network stack interface. */
VDINTERFACETCPNET VDITcpNetCallbacks;
/** Common structure for the supported async I/O interface. */
VDINTERFACE VDIAsyncIO;
/** Callback table for async I/O interface. */
VDINTERFACEASYNCIO VDIAsyncIOCallbacks;
/** Callback table for the configuration information interface. */
VDINTERFACECONFIG VDIConfigCallbacks;
/** Flag whether opened disk suppports async I/O operations. */
bool fAsyncIOSupported;
/** The async media interface. */
PDMIMEDIAASYNC IMediaAsync;
/** The async media port interface above. */
PPDMIMEDIAASYNCPORT pDrvMediaAsyncPort;
/** Pointer to the list of data we need to keep per image. */
PVBOXIMAGE pImages;
} VBOXDISK, *PVBOXDISK;
/*******************************************************************************
* Error reporting callback *
*******************************************************************************/
static void drvvdErrorCallback(void *pvUser, int rc, RT_SRC_POS_DECL,
const char *pszFormat, va_list va)
{
PPDMDRVINS pDrvIns = (PPDMDRVINS)pvUser;
PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
if (pThis->fErrorUseRuntime)
/* We must not pass VMSETRTERR_FLAGS_FATAL as it could lead to a
* deadlock: We are probably executed in a thread context != EMT
* and the EM thread would wait until every thread is suspended
* but we would wait for the EM thread ... */
pDrvIns->pDrvHlp->pfnVMSetRuntimeErrorV(pDrvIns, /* fFlags=*/ 0, "DrvVD", pszFormat, va);
else
pDrvIns->pDrvHlp->pfnVMSetErrorV(pDrvIns, rc, RT_SRC_POS_ARGS, pszFormat, va);
}
/**
* Internal: allocate new image descriptor and put it in the list
*/
static PVBOXIMAGE drvvdNewImage(PVBOXDISK pThis)
{
AssertPtr(pThis);
PVBOXIMAGE pImage = (PVBOXIMAGE)RTMemAllocZ(sizeof(VBOXIMAGE));
if (pImage)
{
pImage->pVDIfsImage = NULL;
PVBOXIMAGE *pp = &pThis->pImages;
while (*pp != NULL)
pp = &(*pp)->pNext;
*pp = pImage;
pImage->pNext = NULL;
}
return pImage;
}
/**
* Internal: free the list of images descriptors.
*/
static void drvvdFreeImages(PVBOXDISK pThis)
{
while (pThis->pImages != NULL)
{
PVBOXIMAGE p = pThis->pImages;
pThis->pImages = pThis->pImages->pNext;
RTMemFree(p);
}
}
/*******************************************************************************
* VD Async I/O interface implementation *
*******************************************************************************/
#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
static DECLCALLBACK(void) drvvdAsyncTaskCompleted(PPDMDRVINS pDrvIns, void *pvTemplateUser, void *pvUser)
{
PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pvTemplateUser;
int rc = VINF_VD_ASYNC_IO_FINISHED;
void *pvCallerUser = NULL;
if (pStorageBackend->fSyncIoPending)
{
pStorageBackend->fSyncIoPending;
RTSemEventSignal(pStorageBackend->EventSem);
}
else if (pStorageBackend->pfnCompleted)
rc = pStorageBackend->pfnCompleted(pvUser, &pvCallerUser);
else
pvCallerUser = pvUser;
if (rc == VINF_VD_ASYNC_IO_FINISHED)
{
rc = pThis->pDrvMediaAsyncPort->pfnTransferCompleteNotify(pThis->pDrvMediaAsyncPort, pvCallerUser);
AssertRC(rc);
}
else
AssertMsg(rc == VERR_VD_ASYNC_IO_IN_PROGRESS, ("Invalid return code from disk backend rc=%Rrc\n", rc));
}
static DECLCALLBACK(int) drvvdAsyncIOOpen(void *pvUser, const char *pszLocation, bool fReadonly,
PFNVDCOMPLETED pfnCompleted, void **ppStorage)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)RTMemAllocZ(sizeof(DRVVDSTORAGEBACKEND));
int rc = VINF_SUCCESS;
if (pStorageBackend)
{
pStorageBackend->fSyncIoPending = false;
pStorageBackend->pfnCompleted = pfnCompleted;
int rc = RTSemEventCreate(&pStorageBackend->EventSem);
if (RT_SUCCESS(rc))
{
rc = PDMDrvHlpPDMAsyncCompletionTemplateCreate(pDrvVD->pDrvIns, &pStorageBackend->pTemplate,
drvvdAsyncTaskCompleted, pStorageBackend, "AsyncTaskCompleted");
if (RT_SUCCESS(rc))
{
rc = PDMR3AsyncCompletionEpCreateForFile(&pStorageBackend->pEndpoint, pszLocation,
fReadonly
? PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_CACHING
: PDMACEP_FILE_FLAGS_CACHING,
pStorageBackend->pTemplate);
if (RT_SUCCESS(rc))
{
*ppStorage = pStorageBackend;
return VINF_SUCCESS;
}
PDMR3AsyncCompletionTemplateDestroy(pStorageBackend->pTemplate);
}
RTSemEventDestroy(pStorageBackend->EventSem);
}
RTMemFree(pStorageBackend);
}
else
rc = VERR_NO_MEMORY;
return rc;
}
static DECLCALLBACK(int) drvvdAsyncIOClose(void *pvUser, void *pStorage)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pStorage;
PDMR3AsyncCompletionEpClose(pStorageBackend->pEndpoint);
PDMR3AsyncCompletionTemplateDestroy(pStorageBackend->pTemplate);
RTSemEventDestroy(pStorageBackend->EventSem);
RTMemFree(pStorageBackend);
return VINF_SUCCESS;;
}
static DECLCALLBACK(int) drvvdAsyncIOGetSize(void *pvUser, void *pStorage, uint64_t *pcbSize)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pStorage;
return PDMR3AsyncCompletionEpGetSize(pStorageBackend->pEndpoint, pcbSize);
}
static DECLCALLBACK(int) drvvdAsyncIOReadSync(void *pvUser, void *pStorage, uint64_t uOffset,
size_t cbRead, void *pvBuf, size_t *pcbRead)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pStorage;
PDMDATASEG DataSeg;
PPDMASYNCCOMPLETIONTASK pTask;
Assert(!pStorageBackend->fSyncIoPending);
pStorageBackend->fSyncIoPending = true;
DataSeg.cbSeg = cbRead;
DataSeg.pvSeg = pvBuf;
int rc = PDMR3AsyncCompletionEpRead(pStorageBackend->pEndpoint, uOffset, &DataSeg, 1, cbRead, NULL, &pTask);
if (RT_FAILURE(rc))
return rc;
/* Wait */
rc = RTSemEventWait(pStorageBackend->EventSem, RT_INDEFINITE_WAIT);
AssertRC(rc);
if (pcbRead)
*pcbRead = cbRead;
return VINF_SUCCESS;
}
static DECLCALLBACK(int) drvvdAsyncIOWriteSync(void *pvUser, void *pStorage, uint64_t uOffset,
size_t cbWrite, const void *pvBuf, size_t *pcbWritten)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pStorage;
PDMDATASEG DataSeg;
PPDMASYNCCOMPLETIONTASK pTask;
Assert(!pStorageBackend->fSyncIoPending);
pStorageBackend->fSyncIoPending = true;
DataSeg.cbSeg = cbWrite;
DataSeg.pvSeg = (void *)pvBuf;
int rc = PDMR3AsyncCompletionEpWrite(pStorageBackend->pEndpoint, uOffset, &DataSeg, 1, cbWrite, NULL, &pTask);
if (RT_FAILURE(rc))
return rc;
/* Wait */
rc = RTSemEventWait(pStorageBackend->EventSem, RT_INDEFINITE_WAIT);
AssertRC(rc);
if (pcbWritten)
*pcbWritten = cbWrite;
return VINF_SUCCESS;
}
static DECLCALLBACK(int) drvvdAsyncIOFlushSync(void *pvUser, void *pStorage)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pStorage;
PPDMASYNCCOMPLETIONTASK pTask;
Assert(!pStorageBackend->fSyncIoPending);
pStorageBackend->fSyncIoPending = true;
int rc = PDMR3AsyncCompletionEpFlush(pStorageBackend->pEndpoint, NULL, &pTask);
if (RT_FAILURE(rc))
return rc;
/* Wait */
rc = RTSemEventWait(pStorageBackend->EventSem, RT_INDEFINITE_WAIT);
AssertRC(rc);
return VINF_SUCCESS;
}
static DECLCALLBACK(int) drvvdAsyncIOReadAsync(void *pvUser, void *pStorage, uint64_t uOffset,
PCPDMDATASEG paSegments, size_t cSegments,
size_t cbRead, void *pvCompletion,
void **ppTask)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pStorage;
return PDMR3AsyncCompletionEpRead(pStorageBackend->pEndpoint, uOffset, paSegments, cSegments, cbRead,
pvCompletion, (PPPDMASYNCCOMPLETIONTASK)ppTask);
}
static DECLCALLBACK(int) drvvdAsyncIOWriteAsync(void *pvUser, void *pStorage, uint64_t uOffset,
PCPDMDATASEG paSegments, size_t cSegments,
size_t cbWrite, void *pvCompletion,
void **ppTask)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pStorage;
return PDMR3AsyncCompletionEpWrite(pStorageBackend->pEndpoint, uOffset, paSegments, cSegments, cbWrite,
pvCompletion, (PPPDMASYNCCOMPLETIONTASK)ppTask);
}
static DECLCALLBACK(int) drvvdAsyncIOFlushAsync(void *pvUser, void *pStorage,
void *pvCompletion, void **ppTask)
{
PVBOXDISK pDrvVD = (PVBOXDISK)pvUser;
PDRVVDSTORAGEBACKEND pStorageBackend = (PDRVVDSTORAGEBACKEND)pStorage;
return PDMR3AsyncCompletionEpFlush(pStorageBackend->pEndpoint, pvCompletion,
(PPPDMASYNCCOMPLETIONTASK)ppTask);
}
#endif /* VBOX_WITH_PDM_ASYNC_COMPLETION */
/*******************************************************************************
* VD Configuration interface implementation *
*******************************************************************************/
static bool drvvdCfgAreKeysValid(void *pvUser, const char *pszzValid)
{
return CFGMR3AreValuesValid((PCFGMNODE)pvUser, pszzValid);
}
static int drvvdCfgQuerySize(void *pvUser, const char *pszName, size_t *pcb)
{
return CFGMR3QuerySize((PCFGMNODE)pvUser, pszName, pcb);
}
static int drvvdCfgQuery(void *pvUser, const char *pszName, char *pszString, size_t cchString)
{
return CFGMR3QueryString((PCFGMNODE)pvUser, pszName, pszString, cchString);
}
#ifdef VBOX_WITH_INIP
/*******************************************************************************
* VD TCP network stack interface implementation - INIP case *
*******************************************************************************/
/** @copydoc VDINTERFACETCPNET::pfnClientConnect */
static DECLCALLBACK(int) drvvdINIPClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock)
{
int rc = VINF_SUCCESS;
/* First check whether lwIP is set up in this VM instance. */
if (!DevINIPConfigured())
{
LogRelFunc(("no IP stack\n"));
return VERR_NET_HOST_UNREACHABLE;
}
/* Resolve hostname. As there is no standard resolver for lwIP yet,
* just accept numeric IP addresses for now. */
struct in_addr ip;
if (!lwip_inet_aton(pszAddress, &ip))
{
LogRelFunc(("cannot resolve IP %s\n", pszAddress));
return VERR_NET_HOST_UNREACHABLE;
}
/* Create socket and connect. */
RTSOCKET Sock = lwip_socket(PF_INET, SOCK_STREAM, 0);
if (Sock != -1)
{
struct sockaddr_in InAddr = {0};
InAddr.sin_family = AF_INET;
InAddr.sin_port = htons(uPort);
InAddr.sin_addr = ip;
if (!lwip_connect(Sock, (struct sockaddr *)&InAddr, sizeof(InAddr)))
{
*pSock = Sock;
return VINF_SUCCESS;
}
rc = VERR_NET_CONNECTION_REFUSED; /* @todo real solution needed */
lwip_close(Sock);
}
else
rc = VERR_NET_CONNECTION_REFUSED; /* @todo real solution needed */
return rc;
}
/** @copydoc VDINTERFACETCPNET::pfnClientClose */
static DECLCALLBACK(int) drvvdINIPClientClose(RTSOCKET Sock)
{
lwip_close(Sock);
return VINF_SUCCESS; /** @todo real solution needed */
}
/** @copydoc VDINTERFACETCPNET::pfnSelectOne */
static DECLCALLBACK(int) drvvdINIPSelectOne(RTSOCKET Sock, unsigned cMillies)
{
fd_set fdsetR;
FD_ZERO(&fdsetR);
FD_SET(Sock, &fdsetR);
fd_set fdsetE = fdsetR;
int rc;
if (cMillies == RT_INDEFINITE_WAIT)
rc = lwip_select(Sock + 1, &fdsetR, NULL, &fdsetE, NULL);
else
{
struct timeval timeout;
timeout.tv_sec = cMillies / 1000;
timeout.tv_usec = (cMillies % 1000) * 1000;
rc = lwip_select(Sock + 1, &fdsetR, NULL, &fdsetE, &timeout);
}
if (rc > 0)
return VINF_SUCCESS;
if (rc == 0)
return VERR_TIMEOUT;
return VERR_NET_CONNECTION_REFUSED; /** @todo real solution needed */
}
/** @copydoc VDINTERFACETCPNET::pfnRead */
static DECLCALLBACK(int) drvvdINIPRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
{
/* Do params checking */
if (!pvBuffer || !cbBuffer)
{
AssertMsgFailed(("Invalid params\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Read loop.
* If pcbRead is NULL we have to fill the entire buffer!
*/
size_t cbRead = 0;
size_t cbToRead = cbBuffer;
for (;;)
{
/** @todo this clipping here is just in case (the send function
* needed it, so I added it here, too). Didn't investigate if this
* really has issues. Better be safe than sorry. */
ssize_t cbBytesRead = lwip_recv(Sock, (char *)pvBuffer + cbRead,
RT_MIN(cbToRead, 32768), 0);
if (cbBytesRead < 0)
return VERR_NET_CONNECTION_REFUSED; /** @todo real solution */
if (cbBytesRead == 0 && errno)
return VERR_NET_CONNECTION_REFUSED; /** @todo real solution */
if (pcbRead)
{
/* return partial data */
*pcbRead = cbBytesRead;
break;
}
/* read more? */
cbRead += cbBytesRead;
if (cbRead == cbBuffer)
break;
/* next */
cbToRead = cbBuffer - cbRead;
}
return VINF_SUCCESS;
}
/** @copydoc VDINTERFACETCPNET::pfnWrite */
static DECLCALLBACK(int) drvvdINIPWrite(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
{
do
{
/** @todo lwip send only supports up to 65535 bytes in a single
* send (stupid limitation buried in the code), so make sure we
* don't get any wraparounds. This should be moved to DevINIP
* stack interface once that's implemented. */
ssize_t cbWritten = lwip_send(Sock, (void *)pvBuffer,
RT_MIN(cbBuffer, 32768), 0);
if (cbWritten < 0)
return VERR_NET_CONNECTION_REFUSED; /** @todo real solution needed */
AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%d cbBuffer=%d\n",
cbWritten, cbBuffer));
cbBuffer -= cbWritten;
pvBuffer = (const char *)pvBuffer + cbWritten;
} while (cbBuffer);
return VINF_SUCCESS;
}
/** @copydoc VDINTERFACETCPNET::pfnFlush */
static DECLCALLBACK(int) drvvdINIPFlush(RTSOCKET Sock)
{
int fFlag = 1;
lwip_setsockopt(Sock, IPPROTO_TCP, TCP_NODELAY,
(const char *)&fFlag, sizeof(fFlag));
fFlag = 0;
lwip_setsockopt(Sock, IPPROTO_TCP, TCP_NODELAY,
(const char *)&fFlag, sizeof(fFlag));
return VINF_SUCCESS;
}
#endif /* VBOX_WITH_INIP */
/*******************************************************************************
* Media interface methods *
*******************************************************************************/
/** @copydoc PDMIMEDIA::pfnRead */
static DECLCALLBACK(int) drvvdRead(PPDMIMEDIA pInterface,
uint64_t off, void *pvBuf, size_t cbRead)
{
LogFlow(("%s: off=%#llx pvBuf=%p cbRead=%d\n", __FUNCTION__,
off, pvBuf, cbRead));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
int rc = VDRead(pThis->pDisk, off, pvBuf, cbRead);
if (RT_SUCCESS(rc))
Log2(("%s: off=%#llx pvBuf=%p cbRead=%d %.*Rhxd\n", __FUNCTION__,
off, pvBuf, cbRead, cbRead, pvBuf));
LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/** @copydoc PDMIMEDIA::pfnWrite */
static DECLCALLBACK(int) drvvdWrite(PPDMIMEDIA pInterface,
uint64_t off, const void *pvBuf,
size_t cbWrite)
{
LogFlow(("%s: off=%#llx pvBuf=%p cbWrite=%d\n", __FUNCTION__,
off, pvBuf, cbWrite));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
Log2(("%s: off=%#llx pvBuf=%p cbWrite=%d %.*Rhxd\n", __FUNCTION__,
off, pvBuf, cbWrite, cbWrite, pvBuf));
int rc = VDWrite(pThis->pDisk, off, pvBuf, cbWrite);
LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/** @copydoc PDMIMEDIA::pfnFlush */
static DECLCALLBACK(int) drvvdFlush(PPDMIMEDIA pInterface)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
int rc = VDFlush(pThis->pDisk);
LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/** @copydoc PDMIMEDIA::pfnGetSize */
static DECLCALLBACK(uint64_t) drvvdGetSize(PPDMIMEDIA pInterface)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
uint64_t cb = VDGetSize(pThis->pDisk, VD_LAST_IMAGE);
LogFlow(("%s: returns %#llx (%llu)\n", __FUNCTION__, cb, cb));
return cb;
}
/** @copydoc PDMIMEDIA::pfnIsReadOnly */
static DECLCALLBACK(bool) drvvdIsReadOnly(PPDMIMEDIA pInterface)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
bool f = VDIsReadOnly(pThis->pDisk);
LogFlow(("%s: returns %d\n", __FUNCTION__, f));
return f;
}
/** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
static DECLCALLBACK(int) drvvdBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
PPDMMEDIAGEOMETRY pPCHSGeometry)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
int rc = VDGetPCHSGeometry(pThis->pDisk, VD_LAST_IMAGE, pPCHSGeometry);
if (RT_FAILURE(rc))
{
Log(("%s: geometry not available.\n", __FUNCTION__));
rc = VERR_PDM_GEOMETRY_NOT_SET;
}
LogFlow(("%s: returns %Rrc (CHS=%d/%d/%d)\n", __FUNCTION__,
rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
return rc;
}
/** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
static DECLCALLBACK(int) drvvdBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
PCPDMMEDIAGEOMETRY pPCHSGeometry)
{
LogFlow(("%s: CHS=%d/%d/%d\n", __FUNCTION__,
pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
int rc = VDSetPCHSGeometry(pThis->pDisk, VD_LAST_IMAGE, pPCHSGeometry);
if (rc == VERR_VD_GEOMETRY_NOT_SET)
rc = VERR_PDM_GEOMETRY_NOT_SET;
LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
static DECLCALLBACK(int) drvvdBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
PPDMMEDIAGEOMETRY pLCHSGeometry)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
int rc = VDGetLCHSGeometry(pThis->pDisk, VD_LAST_IMAGE, pLCHSGeometry);
if (RT_FAILURE(rc))
{
Log(("%s: geometry not available.\n", __FUNCTION__));
rc = VERR_PDM_GEOMETRY_NOT_SET;
}
LogFlow(("%s: returns %Rrc (CHS=%d/%d/%d)\n", __FUNCTION__,
rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
return rc;
}
/** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
static DECLCALLBACK(int) drvvdBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
PCPDMMEDIAGEOMETRY pLCHSGeometry)
{
LogFlow(("%s: CHS=%d/%d/%d\n", __FUNCTION__,
pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
int rc = VDSetLCHSGeometry(pThis->pDisk, VD_LAST_IMAGE, pLCHSGeometry);
if (rc == VERR_VD_GEOMETRY_NOT_SET)
rc = VERR_PDM_GEOMETRY_NOT_SET;
LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/** @copydoc PDMIMEDIA::pfnGetUuid */
static DECLCALLBACK(int) drvvdGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMIMEDIA_2_VBOXDISK(pInterface);
int rc = VDGetUuid(pThis->pDisk, 0, pUuid);
LogFlow(("%s: returns %Rrc ({%RTuuid})\n", __FUNCTION__, rc, pUuid));
return rc;
}
/*******************************************************************************
* Async Media interface methods *
*******************************************************************************/
static DECLCALLBACK(int) drvvdStartRead(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
PPDMDATASEG paSeg, unsigned cSeg,
size_t cbRead, void *pvUser)
{
LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbRead=%d\n pvUser=%#p", __FUNCTION__,
uOffset, paSeg, cSeg, cbRead, pvUser));
PVBOXDISK pThis = PDMIMEDIAASYNC_2_VBOXDISK(pInterface);
int rc = VDAsyncRead(pThis->pDisk, uOffset, cbRead, paSeg, cSeg, pvUser);
LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
static DECLCALLBACK(int) drvvdStartWrite(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
PPDMDATASEG paSeg, unsigned cSeg,
size_t cbWrite, void *pvUser)
{
LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbWrite=%d\n pvUser=%#p", __FUNCTION__,
uOffset, paSeg, cSeg, cbWrite, pvUser));
PVBOXDISK pThis = PDMIMEDIAASYNC_2_VBOXDISK(pInterface);
int rc = VDAsyncWrite(pThis->pDisk, uOffset, cbWrite, paSeg, cSeg, pvUser);
LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/*******************************************************************************
* Async transport port interface methods *
*******************************************************************************/
static DECLCALLBACK(int) drvvdTasksCompleteNotify(PPDMDRVINS pDrvIns, void *pvUser)
{
return VERR_NOT_IMPLEMENTED;
}
/*******************************************************************************
* Base interface methods *
*******************************************************************************/
/** @copydoc PDMIBASE::pfnQueryInterface */
static DECLCALLBACK(void *) drvvdQueryInterface(PPDMIBASE pInterface,
PDMINTERFACE enmInterface)
{
PPDMDRVINS pDrvIns = PDMIBASE_2_DRVINS(pInterface);
PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pDrvIns->IBase;
case PDMINTERFACE_MEDIA:
return &pThis->IMedia;
case PDMINTERFACE_MEDIA_ASYNC:
return pThis->fAsyncIOSupported ? &pThis->IMediaAsync : NULL;
default:
return NULL;
}
}
/*******************************************************************************
* Driver methods *
*******************************************************************************/
/**
* Construct a VBox disk media driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
* of the driver instance. It's also found in pDrvIns->pCfgHandle as it's expected
* to be used frequently in this function.
*/
static DECLCALLBACK(int) drvvdConstruct(PPDMDRVINS pDrvIns,
PCFGMNODE pCfgHandle)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
int rc = VINF_SUCCESS;
char *pszName = NULL; /**< The path of the disk image file. */
char *pszFormat = NULL; /**< The format backed to use for this image. */
bool fReadOnly; /**< True if the media is readonly. */
bool fHonorZeroWrites; /**< True if zero blocks should be written. */
/*
* Init the static parts.
*/
pDrvIns->IBase.pfnQueryInterface = drvvdQueryInterface;
pThis->pDrvIns = pDrvIns;
pThis->fTempReadOnly = false;
pThis->pDisk = NULL;
pThis->fAsyncIOSupported = false;
/* IMedia */
pThis->IMedia.pfnRead = drvvdRead;
pThis->IMedia.pfnWrite = drvvdWrite;
pThis->IMedia.pfnFlush = drvvdFlush;
pThis->IMedia.pfnGetSize = drvvdGetSize;
pThis->IMedia.pfnIsReadOnly = drvvdIsReadOnly;
pThis->IMedia.pfnBiosGetPCHSGeometry = drvvdBiosGetPCHSGeometry;
pThis->IMedia.pfnBiosSetPCHSGeometry = drvvdBiosSetPCHSGeometry;
pThis->IMedia.pfnBiosGetLCHSGeometry = drvvdBiosGetLCHSGeometry;
pThis->IMedia.pfnBiosSetLCHSGeometry = drvvdBiosSetLCHSGeometry;
pThis->IMedia.pfnGetUuid = drvvdGetUuid;
/* IMediaAsync */
pThis->IMediaAsync.pfnStartRead = drvvdStartRead;
pThis->IMediaAsync.pfnStartWrite = drvvdStartWrite;
/* Initialize supported VD interfaces. */
pThis->pVDIfsDisk = NULL;
pThis->VDIErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
pThis->VDIErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
pThis->VDIErrorCallbacks.pfnError = drvvdErrorCallback;
rc = VDInterfaceAdd(&pThis->VDIError, "DrvVD_VDIError", VDINTERFACETYPE_ERROR,
&pThis->VDIErrorCallbacks, pDrvIns, &pThis->pVDIfsDisk);
AssertRC(rc);
#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
pThis->VDIAsyncIOCallbacks.cbSize = sizeof(VDINTERFACEASYNCIO);
pThis->VDIAsyncIOCallbacks.enmInterface = VDINTERFACETYPE_ASYNCIO;
pThis->VDIAsyncIOCallbacks.pfnOpen = drvvdAsyncIOOpen;
pThis->VDIAsyncIOCallbacks.pfnClose = drvvdAsyncIOClose;
pThis->VDIAsyncIOCallbacks.pfnGetSize = drvvdAsyncIOGetSize;
pThis->VDIAsyncIOCallbacks.pfnReadSync = drvvdAsyncIOReadSync;
pThis->VDIAsyncIOCallbacks.pfnWriteSync = drvvdAsyncIOWriteSync;
pThis->VDIAsyncIOCallbacks.pfnFlushSync = drvvdAsyncIOFlushSync;
pThis->VDIAsyncIOCallbacks.pfnReadAsync = drvvdAsyncIOReadAsync;
pThis->VDIAsyncIOCallbacks.pfnWriteAsync = drvvdAsyncIOWriteAsync;
pThis->VDIAsyncIOCallbacks.pfnFlushAsync = drvvdAsyncIOFlushAsync;
rc = VDInterfaceAdd(&pThis->VDIAsyncIO, "DrvVD_AsyncIO", VDINTERFACETYPE_ASYNCIO,
&pThis->VDIAsyncIOCallbacks, pThis, &pThis->pVDIfsDisk);
AssertRC(rc);
#endif
/* This is just prepared here, the actual interface is per-image, so it's
* added later. No need to have separate callback tables. */
pThis->VDIConfigCallbacks.cbSize = sizeof(VDINTERFACECONFIG);
pThis->VDIConfigCallbacks.enmInterface = VDINTERFACETYPE_CONFIG;
pThis->VDIConfigCallbacks.pfnAreKeysValid = drvvdCfgAreKeysValid;
pThis->VDIConfigCallbacks.pfnQuerySize = drvvdCfgQuerySize;
pThis->VDIConfigCallbacks.pfnQuery = drvvdCfgQuery;
/* List of images is empty now. */
pThis->pImages = NULL;
/* Try to attach async media port interface above.*/
pThis->pDrvMediaAsyncPort = (PPDMIMEDIAASYNCPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_MEDIA_ASYNC_PORT);
/*
* Attach the async transport driver below if the device above us implements the
* async interface.
*/
if (pThis->pDrvMediaAsyncPort)
{
/* Try to attach the driver. */
PPDMIBASE pBase;
rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBase);
if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
{
/*
* Though the device supports async I/O there is no transport driver
* which processes async requests.
* Revert to non async I/O.
*/
rc = VINF_SUCCESS;
pThis->pDrvMediaAsyncPort = NULL;
pThis->fAsyncIOSupported = false;
}
else if (RT_FAILURE(rc))
{
AssertMsgFailed(("Failed to attach async transport driver below rc=%Rrc\n", rc));
}
else
{
/*
* The device supports async I/O and we successfully attached the transport driver.
* Indicate that async I/O is supported for now as we check if the image backend supports
* it later.
*/
pThis->fAsyncIOSupported = true;
/** @todo: Use PDM async completion manager */
}
}
/*
* Validate configuration and find all parent images.
* It's sort of up side down from the image dependency tree.
*/
bool fHostIP = false;
unsigned iLevel = 0;
PCFGMNODE pCurNode = pCfgHandle;
for (;;)
{
bool fValid;
if (pCurNode == pCfgHandle)
{
/* Toplevel configuration additionally contains the global image
* open flags. Some might be converted to per-image flags later. */
fValid = CFGMR3AreValuesValid(pCurNode,
"Format\0Path\0"
"ReadOnly\0HonorZeroWrites\0"
"HostIPStack\0");
}
else
{
/* All other image configurations only contain image name and
* the format information. */
fValid = CFGMR3AreValuesValid(pCurNode, "Format\0Path\0");
}
if (!fValid)
{
rc = PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
RT_SRC_POS, N_("DrvVD: Configuration error: keys incorrect at level %d"), iLevel);
break;
}
if (pCurNode == pCfgHandle)
{
rc = CFGMR3QueryBool(pCurNode, "HostIPStack", &fHostIP);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
fHostIP = true;
rc = VINF_SUCCESS;
}
else if (RT_FAILURE(rc))
{
rc = PDMDRV_SET_ERROR(pDrvIns, rc,
N_("DrvVD: Configuration error: Querying \"HostIPStack\" as boolean failed"));
break;
}
rc = CFGMR3QueryBool(pCurNode, "HonorZeroWrites", &fHonorZeroWrites);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
fHonorZeroWrites = false;
rc = VINF_SUCCESS;
}
else if (RT_FAILURE(rc))
{
rc = PDMDRV_SET_ERROR(pDrvIns, rc,
N_("DrvVD: Configuration error: Querying \"HonorZeroWrites\" as boolean failed"));
break;
}
rc = CFGMR3QueryBool(pCurNode, "ReadOnly", &fReadOnly);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
fReadOnly = false;
rc = VINF_SUCCESS;
}
else if (RT_FAILURE(rc))
{
rc = PDMDRV_SET_ERROR(pDrvIns, rc,
N_("DrvVD: Configuration error: Querying \"ReadOnly\" as boolean failed"));
break;
}
}
PCFGMNODE pParent = CFGMR3GetChild(pCurNode, "Parent");
if (!pParent)
break;
pCurNode = pParent;
iLevel++;
}
/*
* Open the images.
*/
if (RT_SUCCESS(rc))
{
/* First of all figure out what kind of TCP networking stack interface
* to use. This is done unconditionally, as backends which don't need
* it will just ignore it. */
if (fHostIP)
{
pThis->VDITcpNetCallbacks.cbSize = sizeof(VDINTERFACETCPNET);
pThis->VDITcpNetCallbacks.enmInterface = VDINTERFACETYPE_TCPNET;
pThis->VDITcpNetCallbacks.pfnClientConnect = RTTcpClientConnect;
pThis->VDITcpNetCallbacks.pfnClientClose = RTTcpClientClose;
pThis->VDITcpNetCallbacks.pfnSelectOne = RTTcpSelectOne;
pThis->VDITcpNetCallbacks.pfnRead = RTTcpRead;
pThis->VDITcpNetCallbacks.pfnWrite = RTTcpWrite;
pThis->VDITcpNetCallbacks.pfnFlush = RTTcpFlush;
}
else
{
#ifndef VBOX_WITH_INIP
rc = PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
RT_SRC_POS, N_("DrvVD: Configuration error: TCP over Internal Networking not compiled in"));
#else /* VBOX_WITH_INIP */
pThis->VDITcpNetCallbacks.cbSize = sizeof(VDINTERFACETCPNET);
pThis->VDITcpNetCallbacks.enmInterface = VDINTERFACETYPE_TCPNET;
pThis->VDITcpNetCallbacks.pfnClientConnect = drvvdINIPClientConnect;
pThis->VDITcpNetCallbacks.pfnClientClose = drvvdINIPClientClose;
pThis->VDITcpNetCallbacks.pfnSelectOne = drvvdINIPSelectOne;
pThis->VDITcpNetCallbacks.pfnRead = drvvdINIPRead;
pThis->VDITcpNetCallbacks.pfnWrite = drvvdINIPWrite;
pThis->VDITcpNetCallbacks.pfnFlush = drvvdINIPFlush;
#endif /* VBOX_WITH_INIP */
}
if (RT_SUCCESS(rc))
{
rc = VDInterfaceAdd(&pThis->VDITcpNet, "DrvVD_INIP",
VDINTERFACETYPE_TCPNET,
&pThis->VDITcpNetCallbacks, NULL,
&pThis->pVDIfsDisk);
}
if (RT_SUCCESS(rc))
{
rc = VDCreate(pThis->pVDIfsDisk, &pThis->pDisk);
/* Error message is already set correctly. */
}
}
while (pCurNode && RT_SUCCESS(rc))
{
/* Allocate per-image data. */
PVBOXIMAGE pImage = drvvdNewImage(pThis);
if (!pImage)
{
rc = VERR_NO_MEMORY;
break;
}
/*
* Read the image configuration.
*/
rc = CFGMR3QueryStringAlloc(pCurNode, "Path", &pszName);
if (RT_FAILURE(rc))
{
rc = PDMDRV_SET_ERROR(pDrvIns, rc,
N_("DrvVD: Configuration error: Querying \"Path\" as string failed"));
break;
}
rc = CFGMR3QueryStringAlloc(pCurNode, "Format", &pszFormat);
if (RT_FAILURE(rc))
{
rc = PDMDRV_SET_ERROR(pDrvIns, rc,
N_("DrvVD: Configuration error: Querying \"Format\" as string failed"));
break;
}
PCFGMNODE pCfg = CFGMR3GetChild(pCurNode, "VDConfig");
rc = VDInterfaceAdd(&pImage->VDIConfig, "DrvVD_Config", VDINTERFACETYPE_CONFIG,
&pThis->VDIConfigCallbacks, pCfg, &pImage->pVDIfsImage);
AssertRC(rc);
/*
* Open the image.
*/
unsigned uOpenFlags;
if (fReadOnly || iLevel != 0)
uOpenFlags = VD_OPEN_FLAGS_READONLY;
else
uOpenFlags = VD_OPEN_FLAGS_NORMAL;
if (fHonorZeroWrites)
uOpenFlags |= VD_OPEN_FLAGS_HONOR_ZEROES;
if (pThis->pDrvMediaAsyncPort)
uOpenFlags |= VD_OPEN_FLAGS_ASYNC_IO;
/* Try to open backend in asyc I/O mode first. */
rc = VDOpen(pThis->pDisk, pszFormat, pszName, uOpenFlags, pImage->pVDIfsImage);
if (rc == VERR_NOT_SUPPORTED)
{
/* Seems async I/O is not supported by the backend, open in normal mode. */
uOpenFlags &= ~VD_OPEN_FLAGS_ASYNC_IO;
rc = VDOpen(pThis->pDisk, pszFormat, pszName, uOpenFlags, pImage->pVDIfsImage);
}
if (RT_SUCCESS(rc))
{
Log(("%s: %d - Opened '%s' in %s mode\n", __FUNCTION__,
iLevel, pszName,
VDIsReadOnly(pThis->pDisk) ? "read-only" : "read-write"));
if ( VDIsReadOnly(pThis->pDisk)
&& !fReadOnly
&& iLevel == 0)
{
rc = PDMDrvHlpVMSetError(pDrvIns, VERR_VD_IMAGE_READ_ONLY, RT_SRC_POS,
N_("Failed to open image '%s' for writing due to wrong "
"permissions"), pszName);
break;
}
}
else
{
rc = PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
N_("Failed to open image '%s' in %s mode rc=%Rrc"), pszName,
(uOpenFlags & VD_OPEN_FLAGS_READONLY) ? "readonly" : "read-write", rc);
break;
}
MMR3HeapFree(pszName);
pszName = NULL;
MMR3HeapFree(pszFormat);
pszFormat = NULL;
/* next */
iLevel--;
pCurNode = CFGMR3GetParent(pCurNode);
}
if (RT_FAILURE(rc))
{
if (VALID_PTR(pThis->pDisk))
{
VDDestroy(pThis->pDisk);
pThis->pDisk = NULL;
}
drvvdFreeImages(pThis);
if (VALID_PTR(pszName))
MMR3HeapFree(pszName);
if (VALID_PTR(pszFormat))
MMR3HeapFree(pszFormat);
return rc;
}
else
{
/*
* Check if every opened image supports async I/O.
* If not we revert to non async I/O.
*/
if (pThis->fAsyncIOSupported)
{
for (unsigned i = 0; i < VDGetCount(pThis->pDisk); i++)
{
VDBACKENDINFO vdBackendInfo;
rc = VDBackendInfoSingle(pThis->pDisk, i, &vdBackendInfo);
AssertRC(rc);
if (vdBackendInfo.uBackendCaps & VD_CAP_ASYNC)
{
/*
* Backend indicates support for at least some files.
* Check if current file is supported with async I/O)
*/
rc = VDImageIsAsyncIOSupported(pThis->pDisk, i, &pThis->fAsyncIOSupported);
AssertRC(rc);
/*
* Check if current image is supported.
* If not we can stop checking because
* at least one does not support it.
*/
if (!pThis->fAsyncIOSupported)
break;
}
else
{
pThis->fAsyncIOSupported = false;
break;
}
}
}
/* Switch to runtime error facility. */
pThis->fErrorUseRuntime = true;
}
LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
return rc;
}
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
static DECLCALLBACK(void) drvvdDestruct(PPDMDRVINS pDrvIns)
{
PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
LogFlow(("%s:\n", __FUNCTION__));
drvvdFreeImages(pThis);
}
/**
* When the VM has been suspended we'll change the image mode to read-only
* so that main and others can read the VDIs. This is important when
* saving state and so forth.
*
* @param pDrvIns The driver instance data.
*/
static DECLCALLBACK(void) drvvdSuspend(PPDMDRVINS pDrvIns)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
if (!VDIsReadOnly(pThis->pDisk))
{
unsigned uOpenFlags;
int rc = VDGetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, &uOpenFlags);
AssertRC(rc);
uOpenFlags |= VD_OPEN_FLAGS_READONLY;
rc = VDSetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, uOpenFlags);
AssertRC(rc);
pThis->fTempReadOnly = true;
}
}
/**
* Before the VM resumes we'll have to undo the read-only mode change
* done in drvvdSuspend.
*
* @param pDrvIns The driver instance data.
*/
static DECLCALLBACK(void) drvvdResume(PPDMDRVINS pDrvIns)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
if (pThis->fTempReadOnly)
{
unsigned uOpenFlags;
int rc = VDGetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, &uOpenFlags);
AssertRC(rc);
uOpenFlags &= ~VD_OPEN_FLAGS_READONLY;
rc = VDSetOpenFlags(pThis->pDisk, VD_LAST_IMAGE, uOpenFlags);
AssertRC(rc);
pThis->fTempReadOnly = false;
}
}
static DECLCALLBACK(void) drvvdPowerOff(PPDMDRVINS pDrvIns)
{
LogFlow(("%s:\n", __FUNCTION__));
PVBOXDISK pThis = PDMINS_2_DATA(pDrvIns, PVBOXDISK);
/*
* We must close the disk here to ensure that
* the backend closes all files before the
* async transport driver is destructed.
*/
int rc = VDCloseAll(pThis->pDisk);
AssertRC(rc);
}
/**
* VBox disk container media driver registration record.
*/
const PDMDRVREG g_DrvVD =
{
/* u32Version */
PDM_DRVREG_VERSION,
/* szDriverName */
"VD",
/* pszDescription */
"Generic VBox disk media driver.",
/* fFlags */
PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
/* fClass. */
PDM_DRVREG_CLASS_MEDIA,
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(VBOXDISK),
/* pfnConstruct */
drvvdConstruct,
/* pfnDestruct */
drvvdDestruct,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
drvvdSuspend,
/* pfnResume */
drvvdResume,
/* pfnDetach */
NULL,
/* pfnPowerOff */
drvvdPowerOff
};
|