VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvDiskIntegrity.cpp@ 65282

Last change on this file since 65282 was 64724, checked in by vboxsync, 8 years ago

Storage/DrvDiskIntegrity: Fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.4 KB
Line 
1/* $Id: DrvDiskIntegrity.cpp 64724 2016-11-20 22:21:35Z vboxsync $ */
2/** @file
3 * VBox storage devices: Disk integrity check.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_DISK_INTEGRITY
23#include <VBox/vmm/pdmdrv.h>
24#include <VBox/vmm/pdmstorageifs.h>
25#include <VBox/vddbg.h>
26#include <iprt/assert.h>
27#include <iprt/string.h>
28#include <iprt/uuid.h>
29#include <iprt/avl.h>
30#include <iprt/mem.h>
31#include <iprt/message.h>
32#include <iprt/sg.h>
33#include <iprt/time.h>
34#include <iprt/semaphore.h>
35#include <iprt/asm.h>
36
37#include "VBoxDD.h"
38
39
40/*********************************************************************************************************************************
41* Structures and Typedefs *
42*********************************************************************************************************************************/
43
44/**
45 * Transfer direction.
46 */
47typedef enum DRVDISKAIOTXDIR
48{
49 /** Invalid. */
50 DRVDISKAIOTXDIR_INVALID = 0,
51 /** Read */
52 DRVDISKAIOTXDIR_READ,
53 /** Write */
54 DRVDISKAIOTXDIR_WRITE,
55 /** Flush */
56 DRVDISKAIOTXDIR_FLUSH,
57 /** Discard */
58 DRVDISKAIOTXDIR_DISCARD,
59 /** Read after write for immediate verification. */
60 DRVDISKAIOTXDIR_READ_AFTER_WRITE
61} DRVDISKAIOTXDIR;
62
63/**
64 * async I/O request.
65 */
66typedef struct DRVDISKAIOREQ
67{
68 /** Transfer direction. */
69 DRVDISKAIOTXDIR enmTxDir;
70 /** Start offset. */
71 uint64_t off;
72 /** Transfer size. */
73 size_t cbTransfer;
74 /** Segment array. */
75 PCRTSGSEG paSeg;
76 /** Number of array entries. */
77 unsigned cSeg;
78 /** User argument */
79 void *pvUser;
80 /** Slot in the array. */
81 unsigned iSlot;
82 /** Start timestamp */
83 uint64_t tsStart;
84 /** Completion timestamp. */
85 uint64_t tsComplete;
86 /** I/O log entry if configured. */
87 VDIOLOGENT hIoLogEntry;
88 /** Ranges to discard. */
89 PCRTRANGE paRanges;
90 /** Number of ranges. */
91 unsigned cRanges;
92 /** I/O segment for the extended media interface
93 * to hold the data. */
94 RTSGSEG IoSeg;
95} DRVDISKAIOREQ, *PDRVDISKAIOREQ;
96
97/**
98 * I/O log entry.
99 */
100typedef struct IOLOGENT
101{
102 /** Start offset */
103 uint64_t off;
104 /** Write size */
105 size_t cbWrite;
106 /** Number of references to this entry. */
107 unsigned cRefs;
108} IOLOGENT, *PIOLOGENT;
109
110/**
111 * Disk segment.
112 */
113typedef struct DRVDISKSEGMENT
114{
115 /** AVL core. */
116 AVLRFOFFNODECORE Core;
117 /** Size of the segment */
118 size_t cbSeg;
119 /** Data for this segment */
120 uint8_t *pbSeg;
121 /** Number of entries in the I/O array. */
122 unsigned cIoLogEntries;
123 /** Array of I/O log references. */
124 PIOLOGENT apIoLog[1];
125} DRVDISKSEGMENT, *PDRVDISKSEGMENT;
126
127/**
128 * Active requests list entry.
129 */
130typedef struct DRVDISKAIOREQACTIVE
131{
132 /** Pointer to the request. */
133 volatile PDRVDISKAIOREQ pIoReq;
134 /** Start timestamp. */
135 uint64_t tsStart;
136} DRVDISKAIOREQACTIVE, *PDRVDISKAIOREQACTIVE;
137
138/**
139 * Disk integrity driver instance data.
140 *
141 * @implements PDMIMEDIA
142 */
143typedef struct DRVDISKINTEGRITY
144{
145 /** Pointer driver instance. */
146 PPDMDRVINS pDrvIns;
147 /** Pointer to the media driver below us.
148 * This is NULL if the media is not mounted. */
149 PPDMIMEDIA pDrvMedia;
150 /** Our media interface */
151 PDMIMEDIA IMedia;
152
153 /** The media port interface above. */
154 PPDMIMEDIAPORT pDrvMediaPort;
155 /** Media port interface */
156 PDMIMEDIAPORT IMediaPort;
157
158 /** The extended media port interface above. */
159 PPDMIMEDIAEXPORT pDrvMediaExPort;
160 /** Our extended media port interface */
161 PDMIMEDIAEXPORT IMediaExPort;
162
163 /** The extended media interface below. */
164 PPDMIMEDIAEX pDrvMediaEx;
165 /** Our extended media interface */
166 PDMIMEDIAEX IMediaEx;
167
168 /** Flag whether consistency checks are enabled. */
169 bool fCheckConsistency;
170 /** Flag whether the RAM disk was prepopulated. */
171 bool fPrepopulateRamDisk;
172 /** AVL tree containing the disk blocks to check. */
173 PAVLRFOFFTREE pTreeSegments;
174
175 /** Flag whether async request tracing is enabled. */
176 bool fTraceRequests;
177 /** Interval the thread should check for expired requests (milliseconds). */
178 uint32_t uCheckIntervalMs;
179 /** Expire timeout for a request (milliseconds). */
180 uint32_t uExpireIntervalMs;
181 /** Thread which checks for lost requests. */
182 RTTHREAD hThread;
183 /** Event semaphore */
184 RTSEMEVENT SemEvent;
185 /** Flag whether the thread should run. */
186 bool fRunning;
187 /** Array containing active requests. */
188 DRVDISKAIOREQACTIVE apReqActive[128];
189 /** Next free slot in the array */
190 volatile unsigned iNextFreeSlot;
191
192 /** Flag whether we check for requests completing twice. */
193 bool fCheckDoubleCompletion;
194 /** Number of requests we go back. */
195 unsigned cEntries;
196 /** Array of completed but still observed requests. */
197 PDRVDISKAIOREQ *papIoReq;
198 /** Current entry in the array. */
199 unsigned iEntry;
200
201 /** Flag whether to do a immediate read after write for verification. */
202 bool fReadAfterWrite;
203 /** Flag whether to record the data to write before the write completed successfully.
204 * Useful in case the data is modified in place later on (encryption for instance). */
205 bool fRecordWriteBeforeCompletion;
206 /** Flag whether to validate memory buffers when the extended media interface is used. */
207 bool fValidateMemBufs;
208
209 /** I/O logger to use if enabled. */
210 VDIOLOGGER hIoLogger;
211 /** Size of the opaque handle until our tracking structure starts in bytes. */
212 size_t cbIoReqOpaque;
213} DRVDISKINTEGRITY, *PDRVDISKINTEGRITY;
214
215
216static void drvdiskintIoReqCheckForDoubleCompletion(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq,
217 bool fMediaEx)
218{
219 /* Search if the I/O request completed already. */
220 for (unsigned i = 0; i < pThis->cEntries; i++)
221 {
222 if (RT_UNLIKELY(pThis->papIoReq[i] == pIoReq))
223 {
224 RTMsgError("Request %#p completed already!\n", pIoReq);
225 if (!fMediaEx)
226 RTMsgError("Start timestamp %llu Completion timestamp %llu (completed after %llu ms)\n",
227 pIoReq->tsStart, pIoReq->tsComplete, pIoReq->tsComplete - pIoReq->tsStart);
228 RTAssertDebugBreak();
229 }
230 }
231
232 pIoReq->tsComplete = RTTimeSystemMilliTS();
233 Assert(!pThis->papIoReq[pThis->iEntry]);
234 pThis->papIoReq[pThis->iEntry] = pIoReq;
235
236 pThis->iEntry = (pThis->iEntry+1) % pThis->cEntries;
237 if (pThis->papIoReq[pThis->iEntry])
238 {
239 if (!fMediaEx)
240 RTMemFree(pThis->papIoReq[pThis->iEntry]);
241 pThis->papIoReq[pThis->iEntry] = NULL;
242 }
243}
244
245static void drvdiskintIoLogEntryRelease(PIOLOGENT pIoLogEnt)
246{
247 pIoLogEnt->cRefs--;
248 if (!pIoLogEnt->cRefs)
249 RTMemFree(pIoLogEnt);
250}
251
252/**
253 * Record a successful write to the virtual disk.
254 *
255 * @returns VBox status code.
256 * @param pThis Disk integrity driver instance data.
257 * @param paSeg Segment array of the write to record.
258 * @param cSeg Number of segments.
259 * @param off Start offset.
260 * @param cbWrite Number of bytes to record.
261 */
262static int drvdiskintWriteRecord(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
263 uint64_t off, size_t cbWrite)
264{
265 int rc = VINF_SUCCESS;
266
267 LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbWrite=%u\n",
268 pThis, paSeg, cSeg, off, cbWrite));
269
270 /* Update the segments */
271 size_t cbLeft = cbWrite;
272 RTFOFF offCurr = (RTFOFF)off;
273 RTSGBUF SgBuf;
274 PIOLOGENT pIoLogEnt = (PIOLOGENT)RTMemAllocZ(sizeof(IOLOGENT));
275 if (!pIoLogEnt)
276 return VERR_NO_MEMORY;
277
278 pIoLogEnt->off = off;
279 pIoLogEnt->cbWrite = cbWrite;
280 pIoLogEnt->cRefs = 0;
281
282 RTSgBufInit(&SgBuf, paSeg, cSeg);
283
284 while (cbLeft)
285 {
286 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
287 size_t cbRange = 0;
288 bool fSet = false;
289 unsigned offSeg = 0;
290
291 if (!pSeg)
292 {
293 /* Get next segment */
294 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
295 if ( !pSeg
296 || offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
297 cbRange = cbLeft;
298 else
299 cbRange = pSeg->Core.Key - offCurr;
300
301 Assert(cbRange % 512 == 0);
302
303 /* Create new segment */
304 pSeg = (PDRVDISKSEGMENT)RTMemAllocZ(RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbRange / 512]));
305 if (pSeg)
306 {
307 pSeg->Core.Key = offCurr;
308 pSeg->Core.KeyLast = offCurr + (RTFOFF)cbRange - 1;
309 pSeg->cbSeg = cbRange;
310 pSeg->pbSeg = (uint8_t *)RTMemAllocZ(cbRange);
311 pSeg->cIoLogEntries = cbRange / 512;
312 if (!pSeg->pbSeg)
313 RTMemFree(pSeg);
314 else
315 {
316 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
317 AssertMsg(fInserted, ("Bug!\n")); RT_NOREF(fInserted);
318 fSet = true;
319 }
320 }
321 }
322 else
323 {
324 fSet = true;
325 offSeg = offCurr - pSeg->Core.Key;
326 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
327 }
328
329 if (fSet)
330 {
331 AssertPtr(pSeg);
332 size_t cbCopied = RTSgBufCopyToBuf(&SgBuf, pSeg->pbSeg + offSeg, cbRange);
333 Assert(cbCopied == cbRange); RT_NOREF(cbCopied);
334
335 /* Update the I/O log pointers */
336 Assert(offSeg % 512 == 0);
337 Assert(cbRange % 512 == 0);
338 while (offSeg < cbRange)
339 {
340 uint32_t uSector = offSeg / 512;
341 PIOLOGENT pIoLogOld = NULL;
342
343 AssertMsg(uSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
344
345 pIoLogOld = pSeg->apIoLog[uSector];
346 if (pIoLogOld)
347 {
348 pIoLogOld->cRefs--;
349 if (!pIoLogOld->cRefs)
350 RTMemFree(pIoLogOld);
351 }
352
353 pSeg->apIoLog[uSector] = pIoLogEnt;
354 pIoLogEnt->cRefs++;
355
356 offSeg += 512;
357 }
358 }
359 else
360 RTSgBufAdvance(&SgBuf, cbRange);
361
362 offCurr += cbRange;
363 cbLeft -= cbRange;
364 }
365
366 return rc;
367}
368
369/**
370 * Verifies a read request.
371 *
372 * @returns VBox status code.
373 * @param pThis Disk integrity driver instance data.
374 * @param paSeg Segment array of the containing the data buffers to verify.
375 * @param cSeg Number of segments.
376 * @param off Start offset.
377 * @param cbRead Number of bytes to verify.
378 */
379static int drvdiskintReadVerify(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
380 uint64_t off, size_t cbRead)
381{
382 int rc = VINF_SUCCESS;
383
384 LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbRead=%u\n",
385 pThis, paSeg, cSeg, off, cbRead));
386
387 Assert(off % 512 == 0);
388 Assert(cbRead % 512 == 0);
389
390 /* Compare read data */
391 size_t cbLeft = cbRead;
392 RTFOFF offCurr = (RTFOFF)off;
393 RTSGBUF SgBuf;
394
395 RTSgBufInit(&SgBuf, paSeg, cSeg);
396
397 while (cbLeft)
398 {
399 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
400 size_t cbRange = 0;
401 bool fCmp = false;
402 unsigned offSeg = 0;
403
404 if (!pSeg)
405 {
406 /* Get next segment */
407 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
408 if (!pSeg)
409 {
410 /* No data in the tree for this read. Assume everything is ok. */
411 cbRange = cbLeft;
412 }
413 else if (offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
414 cbRange = cbLeft;
415 else
416 cbRange = pSeg->Core.Key - offCurr;
417
418 if (pThis->fPrepopulateRamDisk)
419 {
420 /* No segment means everything should be 0 for this part. */
421 if (!RTSgBufIsZero(&SgBuf, cbRange))
422 {
423 RTMsgError("Corrupted disk at offset %llu (expected everything to be 0)!\n",
424 offCurr);
425 RTAssertDebugBreak();
426 }
427 }
428 }
429 else
430 {
431 fCmp = true;
432 offSeg = offCurr - pSeg->Core.Key;
433 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
434 }
435
436 if (fCmp)
437 {
438 RTSGSEG Seg;
439 RTSGBUF SgBufCmp;
440 size_t cbOff = 0;
441
442 Seg.cbSeg = cbRange;
443 Seg.pvSeg = pSeg->pbSeg + offSeg;
444
445 RTSgBufInit(&SgBufCmp, &Seg, 1);
446 if (RTSgBufCmpEx(&SgBuf, &SgBufCmp, cbRange, &cbOff, true))
447 {
448 /* Corrupted disk, print I/O log entry of the last write which accessed this range. */
449 uint32_t cSector = (offSeg + cbOff) / 512;
450 AssertMsg(cSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
451
452 RTMsgError("Corrupted disk at offset %llu (%u bytes in the current read buffer)!\n",
453 offCurr + cbOff, cbOff);
454 RTMsgError("Last write to this sector started at offset %llu with %u bytes (%u references to this log entry)\n",
455 pSeg->apIoLog[cSector]->off,
456 pSeg->apIoLog[cSector]->cbWrite,
457 pSeg->apIoLog[cSector]->cRefs);
458 RTAssertDebugBreak();
459 }
460 }
461 else
462 RTSgBufAdvance(&SgBuf, cbRange);
463
464 offCurr += cbRange;
465 cbLeft -= cbRange;
466 }
467
468 return rc;
469}
470
471/**
472 * Discards the given ranges from the disk.
473 *
474 * @returns VBox status code.
475 * @param pThis Disk integrity driver instance data.
476 * @param paRanges Array of ranges to discard.
477 * @param cRanges Number of ranges in the array.
478 */
479static int drvdiskintDiscardRecords(PDRVDISKINTEGRITY pThis, PCRTRANGE paRanges, unsigned cRanges)
480{
481 int rc = VINF_SUCCESS;
482
483 LogFlowFunc(("pThis=%#p paRanges=%#p cRanges=%u\n", pThis, paRanges, cRanges));
484
485 for (unsigned i = 0; i < cRanges; i++)
486 {
487 uint64_t offStart = paRanges[i].offStart;
488 size_t cbLeft = paRanges[i].cbRange;
489
490 LogFlowFunc(("Discarding off=%llu cbRange=%zu\n", offStart, cbLeft));
491
492 while (cbLeft)
493 {
494 size_t cbRange;
495 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offStart);
496
497 if (!pSeg)
498 {
499 /* Get next segment */
500 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offStart, true);
501 if ( !pSeg
502 || (RTFOFF)offStart + (RTFOFF)cbLeft <= pSeg->Core.Key)
503 cbRange = cbLeft;
504 else
505 cbRange = pSeg->Core.Key - offStart;
506
507 Assert(!(cbRange % 512));
508 }
509 else
510 {
511 size_t cbPreLeft, cbPostLeft;
512
513 cbRange = RT_MIN(cbLeft, pSeg->Core.KeyLast - offStart + 1);
514 cbPreLeft = offStart - pSeg->Core.Key;
515 cbPostLeft = pSeg->cbSeg - cbRange - cbPreLeft;
516
517 Assert(!(cbRange % 512));
518 Assert(!(cbPreLeft % 512));
519 Assert(!(cbPostLeft % 512));
520
521 LogFlowFunc(("cbRange=%zu cbPreLeft=%zu cbPostLeft=%zu\n",
522 cbRange, cbPreLeft, cbPostLeft));
523
524 RTAvlrFileOffsetRemove(pThis->pTreeSegments, pSeg->Core.Key);
525
526 if (!cbPreLeft && !cbPostLeft)
527 {
528 /* Just free the whole segment. */
529 LogFlowFunc(("Freeing whole segment pSeg=%#p\n", pSeg));
530 RTMemFree(pSeg->pbSeg);
531 for (unsigned idx = 0; idx < pSeg->cIoLogEntries; idx++)
532 drvdiskintIoLogEntryRelease(pSeg->apIoLog[idx]);
533 RTMemFree(pSeg);
534 }
535 else if (cbPreLeft && !cbPostLeft)
536 {
537 /* Realloc to new size and insert. */
538 LogFlowFunc(("Realloc segment pSeg=%#p\n", pSeg));
539 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPreLeft);
540 for (unsigned idx = cbPreLeft / 512; idx < pSeg->cIoLogEntries; idx++)
541 drvdiskintIoLogEntryRelease(pSeg->apIoLog[idx]);
542 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbPreLeft / 512]));
543 pSeg->Core.KeyLast = pSeg->Core.Key + cbPreLeft - 1;
544 pSeg->cbSeg = cbPreLeft;
545 pSeg->cIoLogEntries = cbPreLeft / 512;
546 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
547 Assert(fInserted); RT_NOREF(fInserted);
548 }
549 else if (!cbPreLeft && cbPostLeft)
550 {
551 /* Move data to the front and realloc. */
552 LogFlowFunc(("Move data and realloc segment pSeg=%#p\n", pSeg));
553 memmove(pSeg->pbSeg, pSeg->pbSeg + cbRange, cbPostLeft);
554 for (unsigned idx = 0; idx < cbRange / 512; idx++)
555 drvdiskintIoLogEntryRelease(pSeg->apIoLog[idx]);
556 for (unsigned idx = 0; idx < cbPostLeft /512; idx++)
557 pSeg->apIoLog[idx] = pSeg->apIoLog[(cbRange / 512) + idx];
558 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbPostLeft / 512]));
559 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPostLeft);
560 pSeg->Core.Key += cbRange;
561 pSeg->cbSeg = cbPostLeft;
562 pSeg->cIoLogEntries = cbPostLeft / 512;
563 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
564 Assert(fInserted); RT_NOREF(fInserted);
565 }
566 else
567 {
568 /* Split the segment into 2 new segments. */
569 LogFlowFunc(("Split segment pSeg=%#p\n", pSeg));
570 PDRVDISKSEGMENT pSegPost = (PDRVDISKSEGMENT)RTMemAllocZ(RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbPostLeft / 512]));
571 if (pSegPost)
572 {
573 pSegPost->Core.Key = pSeg->Core.Key + cbPreLeft + cbRange;
574 pSegPost->Core.KeyLast = pSeg->Core.KeyLast;
575 pSegPost->cbSeg = cbPostLeft;
576 pSegPost->pbSeg = (uint8_t *)RTMemAllocZ(cbPostLeft);
577 pSegPost->cIoLogEntries = cbPostLeft / 512;
578 if (!pSegPost->pbSeg)
579 RTMemFree(pSegPost);
580 else
581 {
582 memcpy(pSegPost->pbSeg, pSeg->pbSeg + cbPreLeft + cbRange, cbPostLeft);
583 for (unsigned idx = 0; idx < cbPostLeft / 512; idx++)
584 pSegPost->apIoLog[idx] = pSeg->apIoLog[((cbPreLeft + cbRange) / 512) + idx];
585
586 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSegPost->Core);
587 Assert(fInserted); RT_NOREF(fInserted);
588 }
589 }
590
591 /* Shrink the current segment. */
592 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPreLeft);
593 for (unsigned idx = cbPreLeft / 512; idx < (cbPreLeft + cbRange) / 512; idx++)
594 drvdiskintIoLogEntryRelease(pSeg->apIoLog[idx]);
595 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbPreLeft / 512]));
596 pSeg->Core.KeyLast = pSeg->Core.Key + cbPreLeft - 1;
597 pSeg->cbSeg = cbPreLeft;
598 pSeg->cIoLogEntries = cbPreLeft / 512;
599 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
600 Assert(fInserted); RT_NOREF(fInserted);
601 } /* if (cbPreLeft && cbPostLeft) */
602 }
603
604 offStart += cbRange;
605 cbLeft -= cbRange;
606 }
607 }
608
609 LogFlowFunc(("returns rc=%Rrc\n", rc));
610 return rc;
611}
612
613/**
614 * Adds a request to the active list.
615 *
616 * @returns nothing.
617 * @param pThis The driver instance data.
618 * @param pIoReq The request to add.
619 */
620static void drvdiskintIoReqAdd(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
621{
622 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[pThis->iNextFreeSlot];
623
624 Assert(!pReqActive->pIoReq);
625 pReqActive->tsStart = pIoReq->tsStart;
626 pReqActive->pIoReq = pIoReq;
627 pIoReq->iSlot = pThis->iNextFreeSlot;
628
629 /* Search for the next one. */
630 while (pThis->apReqActive[pThis->iNextFreeSlot].pIoReq)
631 pThis->iNextFreeSlot = (pThis->iNextFreeSlot+1) % RT_ELEMENTS(pThis->apReqActive);
632}
633
634/**
635 * Removes a request from the active list.
636 *
637 * @returns nothing.
638 * @param pThis The driver instance data.
639 * @param pIoReq The request to remove.
640 */
641static void drvdiskintIoReqRemove(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
642{
643 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[pIoReq->iSlot];
644
645 Assert(pReqActive->pIoReq == pIoReq);
646
647 ASMAtomicWriteNullPtr(&pReqActive->pIoReq);
648}
649
650/**
651 * Thread checking for expired requests.
652 *
653 * @returns IPRT status code.
654 * @param pThread Thread handle.
655 * @param pvUser Opaque user data.
656 */
657static DECLCALLBACK(int) drvdiskIntIoReqExpiredCheck(RTTHREAD pThread, void *pvUser)
658{
659 PDRVDISKINTEGRITY pThis = (PDRVDISKINTEGRITY)pvUser;
660
661 RT_NOREF(pThread);
662
663 while (pThis->fRunning)
664 {
665 int rc = RTSemEventWait(pThis->SemEvent, pThis->uCheckIntervalMs);
666
667 if (!pThis->fRunning)
668 break;
669
670 Assert(rc == VERR_TIMEOUT); RT_NOREF(rc);
671
672 /* Get current timestamp for comparison. */
673 uint64_t tsCurr = RTTimeSystemMilliTS();
674
675 /* Go through the array and check for expired requests. */
676 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apReqActive); i++)
677 {
678 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[i];
679 PDRVDISKAIOREQ pIoReq = ASMAtomicReadPtrT(&pReqActive->pIoReq, PDRVDISKAIOREQ);
680
681 if ( pIoReq
682 && (tsCurr > pReqActive->tsStart)
683 && (tsCurr - pReqActive->tsStart) >= pThis->uExpireIntervalMs)
684 {
685 RTMsgError("Request %#p expired (active for %llu ms already)\n",
686 pIoReq, tsCurr - pReqActive->tsStart);
687 RTAssertDebugBreak();
688 }
689 }
690 }
691
692 return VINF_SUCCESS;
693}
694
695/**
696 * Verify a completed read after write request.
697 *
698 * @returns VBox status code.
699 * @param pThis The driver instance data.
700 * @param pIoReq The request to be verified.
701 */
702static int drvdiskintReadAfterWriteVerify(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
703{
704 int rc = VINF_SUCCESS;
705
706 if (pThis->fCheckConsistency)
707 rc = drvdiskintReadVerify(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
708 else /** @todo Implement read after write verification without a memory based image of the disk. */
709 AssertMsgFailed(("TODO\n"));
710
711 return rc;
712}
713
714/* -=-=-=-=- IMedia -=-=-=-=- */
715
716/** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIA. */
717#define PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMedia)) )
718
719
720/*********************************************************************************************************************************
721* Media interface methods *
722*********************************************************************************************************************************/
723
724/** @interface_method_impl{PDMIMEDIA,pfnRead} */
725static DECLCALLBACK(int) drvdiskintRead(PPDMIMEDIA pInterface,
726 uint64_t off, void *pvBuf, size_t cbRead)
727{
728 int rc = VINF_SUCCESS;
729 VDIOLOGENT hIoLogEntry;
730 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
731
732 if (pThis->hIoLogger)
733 {
734 rc = VDDbgIoLogStart(pThis->hIoLogger, false, VDDBGIOLOGREQ_READ, off,
735 cbRead, NULL, &hIoLogEntry);
736 AssertRC(rc);
737 }
738
739 rc = pThis->pDrvMedia->pfnRead(pThis->pDrvMedia, off, pvBuf, cbRead);
740
741 if (pThis->hIoLogger)
742 {
743 RTSGSEG Seg;
744 RTSGBUF SgBuf;
745
746 Seg.pvSeg = pvBuf;
747 Seg.cbSeg = cbRead;
748 RTSgBufInit(&SgBuf, &Seg, 1);
749
750 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, hIoLogEntry, rc, &SgBuf);
751 AssertRC(rc2);
752 }
753
754 if (RT_FAILURE(rc))
755 return rc;
756
757 if (pThis->fCheckConsistency)
758 {
759 /* Verify the read. */
760 RTSGSEG Seg;
761 Seg.cbSeg = cbRead;
762 Seg.pvSeg = pvBuf;
763 rc = drvdiskintReadVerify(pThis, &Seg, 1, off, cbRead);
764 }
765
766 return rc;
767}
768
769/** @interface_method_impl{PDMIMEDIA,pfnWrite} */
770static DECLCALLBACK(int) drvdiskintWrite(PPDMIMEDIA pInterface,
771 uint64_t off, const void *pvBuf,
772 size_t cbWrite)
773{
774 int rc = VINF_SUCCESS;
775 VDIOLOGENT hIoLogEntry;
776 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
777
778 if (pThis->hIoLogger)
779 {
780 RTSGSEG Seg;
781 RTSGBUF SgBuf;
782
783 Seg.pvSeg = (void *)pvBuf;
784 Seg.cbSeg = cbWrite;
785 RTSgBufInit(&SgBuf, &Seg, 1);
786
787 rc = VDDbgIoLogStart(pThis->hIoLogger, false, VDDBGIOLOGREQ_WRITE, off,
788 cbWrite, &SgBuf, &hIoLogEntry);
789 AssertRC(rc);
790 }
791
792 if (pThis->fRecordWriteBeforeCompletion)
793 {
794 RTSGSEG Seg;
795 Seg.cbSeg = cbWrite;
796 Seg.pvSeg = (void *)pvBuf;
797
798 rc = drvdiskintWriteRecord(pThis, &Seg, 1, off, cbWrite);
799 if (RT_FAILURE(rc))
800 return rc;
801 }
802
803 rc = pThis->pDrvMedia->pfnWrite(pThis->pDrvMedia, off, pvBuf, cbWrite);
804
805 if (pThis->hIoLogger)
806 {
807 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, hIoLogEntry, rc, NULL);
808 AssertRC(rc2);
809 }
810
811 if (RT_FAILURE(rc))
812 return rc;
813
814 if ( pThis->fCheckConsistency
815 && !pThis->fRecordWriteBeforeCompletion)
816 {
817 /* Record the write. */
818 RTSGSEG Seg;
819 Seg.cbSeg = cbWrite;
820 Seg.pvSeg = (void *)pvBuf;
821 rc = drvdiskintWriteRecord(pThis, &Seg, 1, off, cbWrite);
822 }
823
824 return rc;
825}
826
827/** @interface_method_impl{PDMIMEDIA,pfnFlush} */
828static DECLCALLBACK(int) drvdiskintFlush(PPDMIMEDIA pInterface)
829{
830 int rc = VINF_SUCCESS;
831 VDIOLOGENT hIoLogEntry;
832 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
833
834 if (pThis->hIoLogger)
835 {
836 rc = VDDbgIoLogStart(pThis->hIoLogger, false, VDDBGIOLOGREQ_FLUSH, 0,
837 0, NULL, &hIoLogEntry);
838 AssertRC(rc);
839 }
840
841 rc = pThis->pDrvMedia->pfnFlush(pThis->pDrvMedia);
842
843 if (pThis->hIoLogger)
844 {
845 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, hIoLogEntry, rc, NULL);
846 AssertRC(rc2);
847 }
848
849 return rc;
850}
851
852/** @interface_method_impl{PDMIMEDIA,pfnGetSize} */
853static DECLCALLBACK(uint64_t) drvdiskintGetSize(PPDMIMEDIA pInterface)
854{
855 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
856 return pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
857}
858
859/** @interface_method_impl{PDMIMEDIA,pfnIsReadOnly} */
860static DECLCALLBACK(bool) drvdiskintIsReadOnly(PPDMIMEDIA pInterface)
861{
862 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
863 return pThis->pDrvMedia->pfnIsReadOnly(pThis->pDrvMedia);
864}
865
866/** @interface_method_impl{PDMIMEDIA,pfnBiosIsVisible} */
867static DECLCALLBACK(bool) drvdiskintBiosIsVisible(PPDMIMEDIA pInterface)
868{
869 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
870 return pThis->pDrvMedia->pfnBiosIsVisible(pInterface);
871}
872
873/** @interface_method_impl{PDMIMEDIA,pfnGetType} */
874static DECLCALLBACK(PDMMEDIATYPE) drvdiskintGetType(PPDMIMEDIA pInterface)
875{
876 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
877 return pThis->pDrvMedia->pfnGetType(pThis->pDrvMedia);
878}
879
880/** @interface_method_impl{PDMIMEDIA,pfnBiosGetPCHSGeometry} */
881static DECLCALLBACK(int) drvdiskintBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
882 PPDMMEDIAGEOMETRY pPCHSGeometry)
883{
884 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
885 return pThis->pDrvMedia->pfnBiosGetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
886}
887
888/** @interface_method_impl{PDMIMEDIA,pfnBiosSetPCHSGeometry} */
889static DECLCALLBACK(int) drvdiskintBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
890 PCPDMMEDIAGEOMETRY pPCHSGeometry)
891{
892 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
893 return pThis->pDrvMedia->pfnBiosSetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
894}
895
896/** @interface_method_impl{PDMIMEDIA,pfnBiosGetLCHSGeometry} */
897static DECLCALLBACK(int) drvdiskintBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
898 PPDMMEDIAGEOMETRY pLCHSGeometry)
899{
900 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
901 return pThis->pDrvMedia->pfnBiosGetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
902}
903
904/** @interface_method_impl{PDMIMEDIA,pfnBiosSetLCHSGeometry} */
905static DECLCALLBACK(int) drvdiskintBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
906 PCPDMMEDIAGEOMETRY pLCHSGeometry)
907{
908 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
909 return pThis->pDrvMedia->pfnBiosSetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
910}
911
912/** @interface_method_impl{PDMIMEDIA,pfnGetUuid} */
913static DECLCALLBACK(int) drvdiskintGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
914{
915 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
916 return pThis->pDrvMedia->pfnGetUuid(pThis->pDrvMedia, pUuid);
917}
918
919/** @interface_method_impl{PDMIMEDIA,pfnGetSectorSize} */
920static DECLCALLBACK(uint32_t) drvdiskintGetSectorSize(PPDMIMEDIA pInterface)
921{
922 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
923 return pThis->pDrvMedia->pfnGetSectorSize(pThis->pDrvMedia);
924}
925
926/** @interface_method_impl{PDMIMEDIA,pfnDiscard} */
927static DECLCALLBACK(int) drvdiskintDiscard(PPDMIMEDIA pInterface, PCRTRANGE paRanges, unsigned cRanges)
928{
929 int rc = VINF_SUCCESS;
930 VDIOLOGENT hIoLogEntry;
931 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
932
933 if (pThis->hIoLogger)
934 {
935 rc = VDDbgIoLogStartDiscard(pThis->hIoLogger, false, paRanges, cRanges, &hIoLogEntry);
936 AssertRC(rc);
937 }
938
939 rc = pThis->pDrvMedia->pfnDiscard(pThis->pDrvMedia, paRanges, cRanges);
940
941 if (pThis->hIoLogger)
942 {
943 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, hIoLogEntry, rc, NULL);
944 AssertRC(rc2);
945 }
946
947 if (pThis->fCheckConsistency)
948 rc = drvdiskintDiscardRecords(pThis, paRanges, cRanges);
949
950 return rc;
951}
952
953/** @interface_method_impl{PDMIMEDIA,pfnReadPcBios} */
954static DECLCALLBACK(int) drvdiskintReadPcBios(PPDMIMEDIA pInterface,
955 uint64_t off, void *pvBuf, size_t cbRead)
956{
957 LogFlowFunc(("\n"));
958 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
959
960 return pThis->pDrvMedia->pfnReadPcBios(pThis->pDrvMedia, off, pvBuf, cbRead);
961}
962
963/** @interface_method_impl{PDMIMEDIA,pfnIsNonRotational} */
964static DECLCALLBACK(bool) drvdiskintIsNonRotational(PPDMIMEDIA pInterface)
965{
966 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
967 return pThis->pDrvMedia->pfnIsNonRotational(pThis->pDrvMedia);
968}
969
970/* -=-=-=-=- IMediaPort -=-=-=-=- */
971
972/** Makes a PDRVBLOCK out of a PPDMIMEDIAPORT. */
973#define PDMIMEDIAPORT_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaPort))) )
974
975/**
976 * @interface_method_impl{PDMIMEDIAPORT,pfnQueryDeviceLocation}
977 */
978static DECLCALLBACK(int) drvdiskintQueryDeviceLocation(PPDMIMEDIAPORT pInterface, const char **ppcszController,
979 uint32_t *piInstance, uint32_t *piLUN)
980{
981 PDRVDISKINTEGRITY pThis = PDMIMEDIAPORT_2_DRVDISKINTEGRITY(pInterface);
982
983 return pThis->pDrvMediaPort->pfnQueryDeviceLocation(pThis->pDrvMediaPort, ppcszController,
984 piInstance, piLUN);
985}
986
987/* -=-=-=-=- IMediaExPort -=-=-=-=- */
988
989/**
990 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCompleteNotify}
991 */
992static DECLCALLBACK(int) drvdiskintIoReqCompleteNotify(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
993 void *pvIoReqAlloc, int rcReq)
994{
995 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
996 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
997 int rc = VINF_SUCCESS;
998
999 LogFlowFunc(("pIoReq=%#p\n", pIoReq));
1000
1001 /* Remove from the active list. */
1002 if (pThis->fTraceRequests)
1003 drvdiskintIoReqRemove(pThis, pIoReq);
1004
1005 if (RT_SUCCESS(rcReq) && pThis->fCheckConsistency)
1006 {
1007 if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ)
1008 rc = drvdiskintReadVerify(pThis, &pIoReq->IoSeg, 1, pIoReq->off, pIoReq->cbTransfer);
1009 else if ( pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE
1010 && !pThis->fRecordWriteBeforeCompletion)
1011 rc = drvdiskintWriteRecord(pThis, &pIoReq->IoSeg, 1, pIoReq->off, pIoReq->cbTransfer);
1012 else if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_DISCARD)
1013 rc = drvdiskintDiscardRecords(pThis, pIoReq->paRanges, pIoReq->cRanges);
1014 else if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ_AFTER_WRITE)
1015 rc = drvdiskintReadAfterWriteVerify(pThis, pIoReq);
1016 else
1017 AssertMsg( pIoReq->enmTxDir == DRVDISKAIOTXDIR_FLUSH
1018 || ( pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE
1019 && pThis->fRecordWriteBeforeCompletion), ("Huh?\n"));
1020
1021 AssertRC(rc);
1022 }
1023
1024 if ( RT_SUCCESS(rcReq)
1025 && pThis->fValidateMemBufs
1026 && pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ)
1027 {
1028 /* Check that the guest memory buffer matches what was written. */
1029 RTSGSEG SegCmp;
1030 SegCmp.pvSeg = RTMemAlloc(pIoReq->cbTransfer);
1031 SegCmp.cbSeg = pIoReq->cbTransfer;
1032
1033 RTSGBUF SgBufCmp;
1034 RTSgBufInit(&SgBufCmp, &SegCmp, 1);
1035 rc = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1,
1036 0, &SgBufCmp, pIoReq->cbTransfer);
1037 AssertRC(rc);
1038
1039 RTSGBUF SgBuf;
1040 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1041 if (RTSgBufCmp(&SgBuf, &SgBufCmp, pIoReq->cbTransfer))
1042 {
1043 RTMsgError("Corrupted memory buffer at offset %llu!\n", 0);
1044 RTAssertDebugBreak();
1045 }
1046
1047 RTMemFree(SegCmp.pvSeg);
1048 }
1049
1050 if (pThis->hIoLogger)
1051 {
1052 RTSGBUF SgBuf;
1053
1054 if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ)
1055 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1056
1057 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, rc, &SgBuf);
1058 AssertRC(rc2);
1059 }
1060
1061 if ( pThis->fReadAfterWrite
1062 && pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE)
1063 {
1064#if 0 /** @todo */
1065 pIoReq->enmTxDir = DRVDISKAIOTXDIR_READ_AFTER_WRITE;
1066
1067 /* Add again because it was removed above. */
1068 if (pThis->fTraceRequests)
1069 drvdiskintIoReqAdd(pThis, pIoReq);
1070
1071 rc = pThis->pDrvMediaAsync->pfnStartRead(pThis->pDrvMediaAsync, pIoReq->off, pIoReq->paSeg, pIoReq->cSeg,
1072 pIoReq->cbTransfer, pIoReq);
1073 if (rc == VINF_VD_ASYNC_IO_FINISHED)
1074 {
1075 rc = drvdiskintReadAfterWriteVerify(pThis, pIoReq);
1076
1077 if (pThis->fTraceRequests)
1078 drvdiskintIoReqRemove(pThis, pIoReq);
1079 RTMemFree(pIoReq);
1080 }
1081 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1082 rc = VINF_SUCCESS;
1083 else if (RT_FAILURE(rc))
1084 RTMemFree(pIoReq);
1085#endif
1086 }
1087 else
1088 {
1089 rc = pThis->pDrvMediaExPort->pfnIoReqCompleteNotify(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, rcReq);
1090 /* Put on the watch list. */
1091 if (pThis->fCheckDoubleCompletion)
1092 drvdiskintIoReqCheckForDoubleCompletion(pThis, pIoReq, true /* fMediaEx */);
1093 }
1094
1095 return rc;
1096}
1097
1098/**
1099 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyFromBuf}
1100 */
1101static DECLCALLBACK(int) drvdiskintIoReqCopyFromBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1102 void *pvIoReqAlloc, uint32_t offDst, PRTSGBUF pSgBuf,
1103 size_t cbCopy)
1104{
1105 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1106 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1107 RTSGBUF SgBuf;
1108
1109 RTSgBufClone(&SgBuf, pSgBuf);
1110
1111 int rc = pThis->pDrvMediaExPort->pfnIoReqCopyFromBuf(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, offDst,
1112 pSgBuf, cbCopy);
1113 if ( RT_SUCCESS(rc)
1114 && pIoReq->IoSeg.pvSeg)
1115 {
1116 /* Update our copy. */
1117 RTSgBufCopyToBuf(&SgBuf, (uint8_t *)pIoReq->IoSeg.pvSeg + offDst, cbCopy);
1118 }
1119
1120 return rc;
1121}
1122
1123/**
1124 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyToBuf}
1125 */
1126static DECLCALLBACK(int) drvdiskintIoReqCopyToBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1127 void *pvIoReqAlloc, uint32_t offSrc, PRTSGBUF pSgBuf,
1128 size_t cbCopy)
1129{
1130 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1131 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1132 RTSGBUF SgBuf;
1133
1134 RTSgBufClone(&SgBuf, pSgBuf);
1135
1136 int rc = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, offSrc,
1137 pSgBuf, cbCopy);
1138 if ( RT_SUCCESS(rc)
1139 && pIoReq->IoSeg.pvSeg)
1140 {
1141 if (pThis->fValidateMemBufs)
1142 {
1143 /* Make sure what the caller requested matches what we got earlier. */
1144 RTSGBUF SgBufCmp;
1145 RTSgBufInit(&SgBufCmp, &pIoReq->IoSeg, 1);
1146 RTSgBufAdvance(&SgBufCmp, offSrc);
1147
1148 if (RTSgBufCmp(&SgBuf, &SgBufCmp, cbCopy))
1149 {
1150 RTMsgError("Corrupted memory buffer at offset %llu!\n", offSrc);
1151 RTAssertDebugBreak();
1152 }
1153 }
1154 else
1155 {
1156 /* Update our copy. */
1157 RTSgBufCopyToBuf(&SgBuf, (uint8_t *)pIoReq->IoSeg.pvSeg + offSrc, cbCopy);
1158 }
1159 }
1160
1161 return rc;
1162}
1163
1164/**
1165 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqQueryDiscardRanges}
1166 */
1167static DECLCALLBACK(int) drvdiskintIoReqQueryDiscardRanges(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1168 void *pvIoReqAlloc, uint32_t idxRangeStart,
1169 uint32_t cRanges, PRTRANGE paRanges,
1170 uint32_t *pcRanges)
1171{
1172 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1173 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1174
1175 return pThis->pDrvMediaExPort->pfnIoReqQueryDiscardRanges(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, idxRangeStart,
1176 cRanges, paRanges, pcRanges);
1177}
1178
1179/**
1180 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqStateChanged}
1181 */
1182static DECLCALLBACK(void) drvdiskintIoReqStateChanged(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1183 void *pvIoReqAlloc, PDMMEDIAEXIOREQSTATE enmState)
1184{
1185 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1186 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1187
1188 pThis->pDrvMediaExPort->pfnIoReqStateChanged(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, enmState);
1189}
1190
1191/* -=-=-=-=- IMediaEx -=-=-=-=- */
1192
1193/**
1194 * @interface_method_impl{PDMIMEDIAEX,pfnQueryFeatures}
1195 */
1196static DECLCALLBACK(int) drvdiskintQueryFeatures(PPDMIMEDIAEX pInterface, uint32_t *pfFeatures)
1197{
1198 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1199 return pThis->pDrvMediaEx->pfnQueryFeatures(pThis->pDrvMediaEx, pfFeatures);
1200}
1201
1202/**
1203 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqAllocSizeSet}
1204 */
1205static DECLCALLBACK(int) drvdiskintIoReqAllocSizeSet(PPDMIMEDIAEX pInterface, size_t cbIoReqAlloc)
1206{
1207 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1208
1209 /* Increase the amount by our private tracking structure. */
1210 cbIoReqAlloc += sizeof(DRVDISKAIOREQ);
1211
1212 pThis->fCheckDoubleCompletion = false;
1213
1214 return pThis->pDrvMediaEx->pfnIoReqAllocSizeSet(pThis->pDrvMediaEx, cbIoReqAlloc);
1215}
1216
1217/**
1218 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqAlloc}
1219 */
1220static DECLCALLBACK(int) drvdiskintIoReqAlloc(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc,
1221 PDMMEDIAEXIOREQID uIoReqId, uint32_t fFlags)
1222{
1223 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1224 PDRVDISKAIOREQ pIoReq = NULL;
1225
1226 int rc = pThis->pDrvMediaEx->pfnIoReqAlloc(pThis->pDrvMediaEx, phIoReq, (void **)&pIoReq, uIoReqId, fFlags);
1227 if RT_SUCCESS(rc)
1228 {
1229 pIoReq->enmTxDir = DRVDISKAIOTXDIR_INVALID;
1230 pIoReq->off = 0;
1231 pIoReq->cbTransfer = 0;
1232 pIoReq->paSeg = NULL;
1233 pIoReq->cSeg = 0;
1234 pIoReq->pvUser = NULL;
1235 pIoReq->iSlot = 0;
1236 pIoReq->tsStart = 0;
1237 pIoReq->tsComplete = 0;
1238 pIoReq->hIoLogEntry = NULL;
1239 pIoReq->IoSeg.pvSeg = NULL;
1240 pIoReq->IoSeg.cbSeg = 0;
1241
1242 /*
1243 * Store the size off the start of our tracking structure because it is
1244 * required to access it for the read/write callbacks.
1245 *
1246 * ASSUMPTION that the offset is constant.
1247 */
1248 if (!pThis->cbIoReqOpaque)
1249 pThis->cbIoReqOpaque = (uintptr_t)pIoReq - (uintptr_t)*phIoReq;
1250 else
1251 Assert(pThis->cbIoReqOpaque == (uintptr_t)pIoReq - (uintptr_t)*phIoReq);
1252
1253 *ppvIoReqAlloc = pIoReq + 1;
1254 }
1255
1256 return rc;
1257}
1258
1259/**
1260 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqFree}
1261 */
1262static DECLCALLBACK(int) drvdiskintIoReqFree(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
1263{
1264 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1265 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)((uintptr_t)hIoReq + pThis->cbIoReqOpaque);
1266
1267 if (pIoReq->IoSeg.pvSeg)
1268 RTMemFree(pIoReq->IoSeg.pvSeg);
1269
1270 return pThis->pDrvMediaEx->pfnIoReqFree(pThis->pDrvMediaEx, hIoReq);
1271}
1272
1273/**
1274 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryResidual}
1275 */
1276static DECLCALLBACK(int) drvdiskintIoReqQueryResidual(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbResidual)
1277{
1278 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1279 return pThis->pDrvMediaEx->pfnIoReqQueryResidual(pThis->pDrvMediaEx, hIoReq, pcbResidual);
1280}
1281
1282/**
1283 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryXferSize}
1284 */
1285static DECLCALLBACK(int) drvdiskintIoReqQueryXferSize(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbXfer)
1286{
1287 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1288 return pThis->pDrvMediaEx->pfnIoReqQueryXferSize(pThis->pDrvMediaEx, hIoReq, pcbXfer);
1289}
1290
1291/**
1292 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancelAll}
1293 */
1294static DECLCALLBACK(int) drvdiskintIoReqCancelAll(PPDMIMEDIAEX pInterface)
1295{
1296 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1297 return pThis->pDrvMediaEx->pfnIoReqCancelAll(pThis->pDrvMediaEx);
1298}
1299
1300/**
1301 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancel}
1302 */
1303static DECLCALLBACK(int) drvdiskintIoReqCancel(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQID uIoReqId)
1304{
1305 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1306 return pThis->pDrvMediaEx->pfnIoReqCancel(pThis->pDrvMediaEx, uIoReqId);
1307}
1308
1309/**
1310 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqRead}
1311 */
1312static DECLCALLBACK(int) drvdiskintIoReqRead(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbRead)
1313{
1314 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1315 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)((uintptr_t)hIoReq + pThis->cbIoReqOpaque);
1316
1317 pIoReq->enmTxDir = DRVDISKAIOTXDIR_READ;
1318 pIoReq->off = off;
1319 pIoReq->cbTransfer = cbRead;
1320
1321 /* Allocate a I/O buffer if the I/O is verified.*/
1322 if (pThis->fCheckConsistency)
1323 {
1324 pIoReq->IoSeg.pvSeg = RTMemAlloc(cbRead);
1325 pIoReq->IoSeg.cbSeg = cbRead;
1326 }
1327
1328 if (pThis->fTraceRequests)
1329 drvdiskintIoReqAdd(pThis, pIoReq);
1330
1331 if (pThis->hIoLogger)
1332 {
1333 int rc2 = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_READ, off,
1334 cbRead, NULL, &pIoReq->hIoLogEntry);
1335 AssertRC(rc2);
1336 }
1337
1338 int rc = pThis->pDrvMediaEx->pfnIoReqRead(pThis->pDrvMediaEx, hIoReq, off, cbRead);
1339 if (rc == VINF_SUCCESS)
1340 {
1341 /* Verify the read now. */
1342 if (pThis->fCheckConsistency)
1343 {
1344 int rc2 = drvdiskintReadVerify(pThis, &pIoReq->IoSeg, 1, off, cbRead);
1345 AssertRC(rc2);
1346 }
1347
1348 if (pThis->hIoLogger)
1349 {
1350 RTSGBUF SgBuf;
1351
1352 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1353
1354 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, &SgBuf);
1355 AssertRC(rc2);
1356 }
1357
1358 if (pThis->fTraceRequests)
1359 drvdiskintIoReqRemove(pThis, pIoReq);
1360 }
1361
1362 LogFlowFunc(("returns %Rrc\n", rc));
1363 return rc;
1364}
1365
1366/**
1367 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqWrite}
1368 */
1369static DECLCALLBACK(int) drvdiskintIoReqWrite(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbWrite)
1370{
1371 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1372 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)((uintptr_t)hIoReq + pThis->cbIoReqOpaque);
1373
1374 pIoReq->enmTxDir = DRVDISKAIOTXDIR_WRITE;
1375 pIoReq->off = off;
1376 pIoReq->cbTransfer = cbWrite;
1377
1378 /* Allocate a I/O buffer if the I/O is verified.*/
1379 if ( pThis->fCheckConsistency
1380 || pThis->fValidateMemBufs
1381 || pThis->hIoLogger
1382 || pThis->fRecordWriteBeforeCompletion)
1383 {
1384 pIoReq->IoSeg.pvSeg = RTMemAlloc(cbWrite);
1385 pIoReq->IoSeg.cbSeg = cbWrite;
1386
1387 /* Sync the memory buffer over if we should validate it. */
1388 if ( pThis->fValidateMemBufs
1389 || pThis->hIoLogger
1390 || pThis->fRecordWriteBeforeCompletion)
1391 {
1392 RTSGBUF SgBuf;
1393
1394 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1395 int rc2 = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, 0,
1396 &SgBuf, cbWrite);
1397 AssertRC(rc2);
1398 }
1399 }
1400
1401 if (pThis->fTraceRequests)
1402 drvdiskintIoReqAdd(pThis, pIoReq);
1403
1404 if (pThis->hIoLogger)
1405 {
1406 RTSGBUF SgBuf;
1407
1408 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1409 int rc2 = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_WRITE, off,
1410 cbWrite, &SgBuf, &pIoReq->hIoLogEntry);
1411 AssertRC(rc2);
1412 }
1413
1414 if (pThis->fRecordWriteBeforeCompletion)
1415 {
1416
1417 int rc2 = drvdiskintWriteRecord(pThis, &pIoReq->IoSeg, 1, off, cbWrite);
1418 AssertRC(rc2);
1419 }
1420
1421 int rc = pThis->pDrvMediaEx->pfnIoReqWrite(pThis->pDrvMediaEx, hIoReq, off, cbWrite);
1422 if (rc == VINF_SUCCESS)
1423 {
1424 /* Record the write. */
1425 if ( pThis->fCheckConsistency
1426 && !pThis->fRecordWriteBeforeCompletion)
1427 {
1428 int rc2 = drvdiskintWriteRecord(pThis, &pIoReq->IoSeg, 1, off, cbWrite);
1429 AssertRC(rc2);
1430 }
1431
1432 if (pThis->hIoLogger)
1433 {
1434 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, NULL);
1435 AssertRC(rc2);
1436 }
1437
1438 if (pThis->fTraceRequests)
1439 drvdiskintIoReqRemove(pThis, pIoReq);
1440 }
1441
1442 LogFlowFunc(("returns %Rrc\n", rc));
1443 return rc;
1444}
1445
1446/**
1447 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqFlush}
1448 */
1449static DECLCALLBACK(int) drvdiskintIoReqFlush(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
1450{
1451 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1452 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)((uintptr_t)hIoReq + pThis->cbIoReqOpaque);
1453
1454 pIoReq->enmTxDir = DRVDISKAIOTXDIR_FLUSH;
1455 pIoReq->off = 0;
1456 pIoReq->cbTransfer = 0;
1457
1458 if (pThis->fTraceRequests)
1459 drvdiskintIoReqAdd(pThis, pIoReq);
1460
1461 if (pThis->hIoLogger)
1462 {
1463 int rc2 = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_FLUSH, 0,
1464 0, NULL, &pIoReq->hIoLogEntry);
1465 AssertRC(rc2);
1466 }
1467
1468 int rc = pThis->pDrvMediaEx->pfnIoReqFlush(pThis->pDrvMediaEx, hIoReq);
1469 if (rc == VINF_SUCCESS)
1470 {
1471 if (pThis->hIoLogger)
1472 {
1473 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, NULL);
1474 AssertRC(rc2);
1475 }
1476 }
1477
1478 LogFlowFunc(("returns %Rrc\n", rc));
1479 return rc;
1480}
1481
1482/**
1483 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqDiscard}
1484 */
1485static DECLCALLBACK(int) drvdiskintIoReqDiscard(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, unsigned cRangesMax)
1486{
1487 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1488 return pThis->pDrvMediaEx->pfnIoReqDiscard(pThis->pDrvMediaEx, hIoReq, cRangesMax);
1489}
1490
1491/**
1492 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetActiveCount}
1493 */
1494static DECLCALLBACK(uint32_t) drvdiskintIoReqGetActiveCount(PPDMIMEDIAEX pInterface)
1495{
1496 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1497 return pThis->pDrvMediaEx->pfnIoReqGetActiveCount(pThis->pDrvMediaEx);
1498}
1499
1500/**
1501 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetSuspendedCount}
1502 */
1503static DECLCALLBACK(uint32_t) drvdiskintIoReqGetSuspendedCount(PPDMIMEDIAEX pInterface)
1504{
1505 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1506 return pThis->pDrvMediaEx->pfnIoReqGetSuspendedCount(pThis->pDrvMediaEx);
1507}
1508
1509/**
1510 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedStart}
1511 */
1512static DECLCALLBACK(int) drvdiskintIoReqQuerySuspendedStart(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc)
1513{
1514 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1515 return pThis->pDrvMediaEx->pfnIoReqQuerySuspendedStart(pThis->pDrvMediaEx, phIoReq, ppvIoReqAlloc);
1516}
1517
1518/**
1519 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedNext}
1520 */
1521static DECLCALLBACK(int) drvdiskintIoReqQuerySuspendedNext(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq,
1522 PPDMMEDIAEXIOREQ phIoReqNext, void **ppvIoReqAllocNext)
1523{
1524 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1525 return pThis->pDrvMediaEx->pfnIoReqQuerySuspendedNext(pThis->pDrvMediaEx, hIoReq, phIoReqNext, ppvIoReqAllocNext);
1526}
1527
1528/**
1529 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedSave}
1530 */
1531static DECLCALLBACK(int) drvdiskintIoReqSuspendedSave(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
1532{
1533 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1534 return pThis->pDrvMediaEx->pfnIoReqSuspendedSave(pThis->pDrvMediaEx, pSSM, hIoReq);
1535}
1536
1537/**
1538 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedLoad}
1539 */
1540static DECLCALLBACK(int) drvdiskintIoReqSuspendedLoad(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
1541{
1542 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1543 return pThis->pDrvMediaEx->pfnIoReqSuspendedLoad(pThis->pDrvMediaEx, pSSM, hIoReq);
1544}
1545
1546/* -=-=-=-=- IBase -=-=-=-=- */
1547
1548/**
1549 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1550 */
1551static DECLCALLBACK(void *) drvdiskintQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1552{
1553 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1554 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1555
1556 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1557 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
1558 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAPORT, &pThis->IMediaPort);
1559 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEXPORT, &pThis->IMediaExPort);
1560 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEX, pThis->pDrvMediaEx ? &pThis->IMediaEx : NULL);
1561 return NULL;
1562}
1563
1564
1565/* -=-=-=-=- driver interface -=-=-=-=- */
1566
1567static DECLCALLBACK(int) drvdiskintTreeDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
1568{
1569 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)pNode;
1570
1571 RT_NOREF(pvUser);
1572
1573 RTMemFree(pSeg->pbSeg);
1574 RTMemFree(pSeg);
1575 return VINF_SUCCESS;
1576}
1577
1578/**
1579 * @copydoc FNPDMDRVDESTRUCT
1580 */
1581static DECLCALLBACK(void) drvdiskintDestruct(PPDMDRVINS pDrvIns)
1582{
1583 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1584
1585 if (pThis->pTreeSegments)
1586 {
1587 RTAvlrFileOffsetDestroy(pThis->pTreeSegments, drvdiskintTreeDestroy, NULL);
1588 RTMemFree(pThis->pTreeSegments);
1589 }
1590
1591 if (pThis->fTraceRequests)
1592 {
1593 pThis->fRunning = false;
1594 RTSemEventSignal(pThis->SemEvent);
1595 RTSemEventDestroy(pThis->SemEvent);
1596 }
1597
1598 if (pThis->fCheckDoubleCompletion)
1599 {
1600 /* Free all requests */
1601 while (pThis->papIoReq[pThis->iEntry])
1602 {
1603 RTMemFree(pThis->papIoReq[pThis->iEntry]);
1604 pThis->papIoReq[pThis->iEntry] = NULL;
1605 pThis->iEntry = (pThis->iEntry+1) % pThis->cEntries;
1606 }
1607 }
1608
1609 if (pThis->hIoLogger)
1610 VDDbgIoLogDestroy(pThis->hIoLogger);
1611}
1612
1613/**
1614 * Construct a disk integrity driver instance.
1615 *
1616 * @copydoc FNPDMDRVCONSTRUCT
1617 */
1618static DECLCALLBACK(int) drvdiskintConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1619{
1620 int rc = VINF_SUCCESS;
1621 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1622 LogFlow(("drvdiskintConstruct: iInstance=%d\n", pDrvIns->iInstance));
1623 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1624
1625 /*
1626 * Validate configuration.
1627 */
1628 if (!CFGMR3AreValuesValid(pCfg, "CheckConsistency\0"
1629 "TraceRequests\0"
1630 "CheckIntervalMs\0"
1631 "ExpireIntervalMs\0"
1632 "CheckDoubleCompletions\0"
1633 "HistorySize\0"
1634 "IoLog\0"
1635 "PrepopulateRamDisk\0"
1636 "ReadAfterWrite\0"
1637 "RecordWriteBeforeCompletion\0"
1638 "ValidateMemoryBuffers\0"))
1639 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1640
1641 rc = CFGMR3QueryBoolDef(pCfg, "CheckConsistency", &pThis->fCheckConsistency, false);
1642 AssertRC(rc);
1643 rc = CFGMR3QueryBoolDef(pCfg, "TraceRequests", &pThis->fTraceRequests, false);
1644 AssertRC(rc);
1645 rc = CFGMR3QueryU32Def(pCfg, "CheckIntervalMs", &pThis->uCheckIntervalMs, 5000); /* 5 seconds */
1646 AssertRC(rc);
1647 rc = CFGMR3QueryU32Def(pCfg, "ExpireIntervalMs", &pThis->uExpireIntervalMs, 20000); /* 20 seconds */
1648 AssertRC(rc);
1649 rc = CFGMR3QueryBoolDef(pCfg, "CheckDoubleCompletions", &pThis->fCheckDoubleCompletion, false);
1650 AssertRC(rc);
1651 rc = CFGMR3QueryU32Def(pCfg, "HistorySize", &pThis->cEntries, 512);
1652 AssertRC(rc);
1653 rc = CFGMR3QueryBoolDef(pCfg, "PrepopulateRamDisk", &pThis->fPrepopulateRamDisk, false);
1654 AssertRC(rc);
1655 rc = CFGMR3QueryBoolDef(pCfg, "ReadAfterWrite", &pThis->fReadAfterWrite, false);
1656 AssertRC(rc);
1657 rc = CFGMR3QueryBoolDef(pCfg, "RecordWriteBeforeCompletion", &pThis->fRecordWriteBeforeCompletion, false);
1658 AssertRC(rc);
1659 rc = CFGMR3QueryBoolDef(pCfg, "ValidateMemoryBuffers", &pThis->fValidateMemBufs, true);
1660 AssertRC(rc);
1661
1662 char *pszIoLogFilename = NULL;
1663 rc = CFGMR3QueryStringAlloc(pCfg, "IoLog", &pszIoLogFilename);
1664 Assert(RT_SUCCESS(rc) || rc == VERR_CFGM_VALUE_NOT_FOUND);
1665
1666 /*
1667 * Initialize most of the data members.
1668 */
1669 pThis->pDrvIns = pDrvIns;
1670
1671 /* IBase. */
1672 pDrvIns->IBase.pfnQueryInterface = drvdiskintQueryInterface;
1673
1674 /* IMedia */
1675 pThis->IMedia.pfnRead = drvdiskintRead;
1676 pThis->IMedia.pfnWrite = drvdiskintWrite;
1677 pThis->IMedia.pfnFlush = drvdiskintFlush;
1678 pThis->IMedia.pfnGetSize = drvdiskintGetSize;
1679 pThis->IMedia.pfnIsReadOnly = drvdiskintIsReadOnly;
1680 pThis->IMedia.pfnBiosIsVisible = drvdiskintBiosIsVisible;
1681 pThis->IMedia.pfnBiosGetPCHSGeometry = drvdiskintBiosGetPCHSGeometry;
1682 pThis->IMedia.pfnBiosSetPCHSGeometry = drvdiskintBiosSetPCHSGeometry;
1683 pThis->IMedia.pfnBiosGetLCHSGeometry = drvdiskintBiosGetLCHSGeometry;
1684 pThis->IMedia.pfnBiosSetLCHSGeometry = drvdiskintBiosSetLCHSGeometry;
1685 pThis->IMedia.pfnGetUuid = drvdiskintGetUuid;
1686 pThis->IMedia.pfnGetSectorSize = drvdiskintGetSectorSize;
1687 pThis->IMedia.pfnGetType = drvdiskintGetType;
1688 pThis->IMedia.pfnReadPcBios = drvdiskintReadPcBios;
1689 pThis->IMedia.pfnIsNonRotational = drvdiskintIsNonRotational;
1690
1691 /* IMediaEx. */
1692 pThis->IMediaEx.pfnQueryFeatures = drvdiskintQueryFeatures;
1693 pThis->IMediaEx.pfnIoReqAllocSizeSet = drvdiskintIoReqAllocSizeSet;
1694 pThis->IMediaEx.pfnIoReqAlloc = drvdiskintIoReqAlloc;
1695 pThis->IMediaEx.pfnIoReqFree = drvdiskintIoReqFree;
1696 pThis->IMediaEx.pfnIoReqQueryResidual = drvdiskintIoReqQueryResidual;
1697 pThis->IMediaEx.pfnIoReqQueryXferSize = drvdiskintIoReqQueryXferSize;
1698 pThis->IMediaEx.pfnIoReqCancelAll = drvdiskintIoReqCancelAll;
1699 pThis->IMediaEx.pfnIoReqCancel = drvdiskintIoReqCancel;
1700 pThis->IMediaEx.pfnIoReqRead = drvdiskintIoReqRead;
1701 pThis->IMediaEx.pfnIoReqWrite = drvdiskintIoReqWrite;
1702 pThis->IMediaEx.pfnIoReqFlush = drvdiskintIoReqFlush;
1703 pThis->IMediaEx.pfnIoReqDiscard = drvdiskintIoReqDiscard;
1704 pThis->IMediaEx.pfnIoReqGetActiveCount = drvdiskintIoReqGetActiveCount;
1705 pThis->IMediaEx.pfnIoReqGetSuspendedCount = drvdiskintIoReqGetSuspendedCount;
1706 pThis->IMediaEx.pfnIoReqQuerySuspendedStart = drvdiskintIoReqQuerySuspendedStart;
1707 pThis->IMediaEx.pfnIoReqQuerySuspendedNext = drvdiskintIoReqQuerySuspendedNext;
1708 pThis->IMediaEx.pfnIoReqSuspendedSave = drvdiskintIoReqSuspendedSave;
1709 pThis->IMediaEx.pfnIoReqSuspendedLoad = drvdiskintIoReqSuspendedLoad;
1710
1711 /* IMediaPort. */
1712 pThis->IMediaPort.pfnQueryDeviceLocation = drvdiskintQueryDeviceLocation;
1713
1714 /* IMediaExPort. */
1715 pThis->IMediaExPort.pfnIoReqCompleteNotify = drvdiskintIoReqCompleteNotify;
1716 pThis->IMediaExPort.pfnIoReqCopyFromBuf = drvdiskintIoReqCopyFromBuf;
1717 pThis->IMediaExPort.pfnIoReqCopyToBuf = drvdiskintIoReqCopyToBuf;
1718 pThis->IMediaExPort.pfnIoReqQueryDiscardRanges = drvdiskintIoReqQueryDiscardRanges;
1719 pThis->IMediaExPort.pfnIoReqStateChanged = drvdiskintIoReqStateChanged;
1720
1721 /* Query the media port interface above us. */
1722 pThis->pDrvMediaPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAPORT);
1723 if (!pThis->pDrvMediaPort)
1724 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
1725 N_("No media port inrerface above"));
1726
1727 /* Try to attach extended media port interface above.*/
1728 pThis->pDrvMediaExPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAEXPORT);
1729
1730 /*
1731 * Try attach driver below and query it's media interface.
1732 */
1733 PPDMIBASE pBase;
1734 rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
1735 if (RT_FAILURE(rc))
1736 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1737 N_("Failed to attach driver below us! %Rrc"), rc);
1738
1739 pThis->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
1740 if (!pThis->pDrvMedia)
1741 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
1742 N_("No media or async media interface below"));
1743
1744 pThis->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIAEX);
1745
1746 if (pThis->pDrvMedia->pfnDiscard)
1747 pThis->IMedia.pfnDiscard = drvdiskintDiscard;
1748
1749 if (pThis->fCheckConsistency)
1750 {
1751 /* Create the AVL tree. */
1752 pThis->pTreeSegments = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
1753 if (!pThis->pTreeSegments)
1754 rc = VERR_NO_MEMORY;
1755 }
1756
1757 if (pThis->fTraceRequests)
1758 {
1759 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apReqActive); i++)
1760 {
1761 pThis->apReqActive[i].pIoReq = NULL;
1762 pThis->apReqActive[i].tsStart = 0;
1763 }
1764
1765 pThis->iNextFreeSlot = 0;
1766
1767 /* Init event semaphore. */
1768 rc = RTSemEventCreate(&pThis->SemEvent);
1769 AssertRC(rc);
1770 pThis->fRunning = true;
1771 rc = RTThreadCreate(&pThis->hThread, drvdiskIntIoReqExpiredCheck, pThis,
1772 0, RTTHREADTYPE_INFREQUENT_POLLER, 0, "DiskIntegrity");
1773 AssertRC(rc);
1774 }
1775
1776 if (pThis->fCheckDoubleCompletion)
1777 {
1778 pThis->iEntry = 0;
1779 pThis->papIoReq = (PDRVDISKAIOREQ *)RTMemAllocZ(pThis->cEntries * sizeof(PDRVDISKAIOREQ));
1780 AssertPtr(pThis->papIoReq);
1781 }
1782
1783 if (pszIoLogFilename)
1784 {
1785 rc = VDDbgIoLogCreate(&pThis->hIoLogger, pszIoLogFilename, VDDBG_IOLOG_LOG_DATA);
1786 MMR3HeapFree(pszIoLogFilename);
1787 }
1788
1789 /* Read in all data before the start if requested. */
1790 if (pThis->fPrepopulateRamDisk)
1791 {
1792 uint64_t cbDisk = 0;
1793
1794 LogRel(("DiskIntegrity: Prepopulating RAM disk, this will take some time...\n"));
1795
1796 cbDisk = pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
1797 if (cbDisk)
1798 {
1799 uint64_t off = 0;
1800 uint8_t abBuffer[_64K];
1801 RTSGSEG Seg;
1802
1803 Seg.pvSeg = abBuffer;
1804
1805 while (cbDisk)
1806 {
1807 size_t cbThisRead = RT_MIN(cbDisk, sizeof(abBuffer));
1808
1809 rc = pThis->pDrvMedia->pfnRead(pThis->pDrvMedia, off, abBuffer, cbThisRead);
1810 if (RT_FAILURE(rc))
1811 break;
1812
1813 if (ASMBitFirstSet(abBuffer, sizeof(abBuffer) * 8) != -1)
1814 {
1815 Seg.cbSeg = cbThisRead;
1816 rc = drvdiskintWriteRecord(pThis, &Seg, 1,
1817 off, cbThisRead);
1818 if (RT_FAILURE(rc))
1819 break;
1820 }
1821
1822 cbDisk -= cbThisRead;
1823 off += cbThisRead;
1824 }
1825
1826 LogRel(("DiskIntegrity: Prepopulating RAM disk finished with %Rrc\n", rc));
1827 }
1828 else
1829 return PDMDRV_SET_ERROR(pDrvIns, VERR_INTERNAL_ERROR,
1830 N_("DiskIntegrity: Error querying the media size below"));
1831 }
1832
1833 return rc;
1834}
1835
1836
1837/**
1838 * Block driver registration record.
1839 */
1840const PDMDRVREG g_DrvDiskIntegrity =
1841{
1842 /* u32Version */
1843 PDM_DRVREG_VERSION,
1844 /* szName */
1845 "DiskIntegrity",
1846 /* szRCMod */
1847 "",
1848 /* szR0Mod */
1849 "",
1850 /* pszDescription */
1851 "Disk integrity driver.",
1852 /* fFlags */
1853 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1854 /* fClass. */
1855 PDM_DRVREG_CLASS_BLOCK,
1856 /* cMaxInstances */
1857 ~0U,
1858 /* cbInstance */
1859 sizeof(DRVDISKINTEGRITY),
1860 /* pfnConstruct */
1861 drvdiskintConstruct,
1862 /* pfnDestruct */
1863 drvdiskintDestruct,
1864 /* pfnRelocate */
1865 NULL,
1866 /* pfnIOCtl */
1867 NULL,
1868 /* pfnPowerOn */
1869 NULL,
1870 /* pfnReset */
1871 NULL,
1872 /* pfnSuspend */
1873 NULL,
1874 /* pfnResume */
1875 NULL,
1876 /* pfnAttach */
1877 NULL,
1878 /* pfnDetach */
1879 NULL,
1880 /* pfnPowerOff */
1881 NULL,
1882 /* pfnSoftReset */
1883 NULL,
1884 /* u32EndVersion */
1885 PDM_DRVREG_VERSION
1886};
1887
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette