VirtualBox

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

Last change on this file since 64009 was 64002, checked in by vboxsync, 9 years ago

Devices/Storage: Throw out PDMIMEDIAASYNC which is superseded by PDMIMEDIAEX now

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 64.1 KB
Line 
1/* $Id: DrvDiskIntegrity.cpp 64002 2016-09-26 11:40:57Z 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 cbWrite 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,pfnIoBufAlloc} */
954static DECLCALLBACK(int) drvdiskintIoBufAlloc(PPDMIMEDIA pInterface, size_t cb, void **ppvNew)
955{
956 LogFlowFunc(("\n"));
957 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
958
959 return pThis->pDrvMedia->pfnIoBufAlloc(pThis->pDrvMedia, cb, ppvNew);
960}
961
962/** @interface_method_impl{PDMIMEDIA,pfnIoBufFree} */
963static DECLCALLBACK(int) drvdiskintIoBufFree(PPDMIMEDIA pInterface, void *pv, size_t cb)
964{
965 LogFlowFunc(("\n"));
966 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
967
968 return pThis->pDrvMedia->pfnIoBufFree(pThis->pDrvMedia, pv, cb);
969}
970
971/** @interface_method_impl{PDMIMEDIA,pfnReadPcBios} */
972static DECLCALLBACK(int) drvdiskintReadPcBios(PPDMIMEDIA pInterface,
973 uint64_t off, void *pvBuf, size_t cbRead)
974{
975 LogFlowFunc(("\n"));
976 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
977
978 return pThis->pDrvMedia->pfnReadPcBios(pThis->pDrvMedia, off, pvBuf, cbRead);
979}
980
981/* -=-=-=-=- IMediaPort -=-=-=-=- */
982
983/** Makes a PDRVBLOCK out of a PPDMIMEDIAPORT. */
984#define PDMIMEDIAPORT_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaPort))) )
985
986/**
987 * @interface_method_impl{PDMIMEDIAPORT,pfnQueryDeviceLocation}
988 */
989static DECLCALLBACK(int) drvdiskintQueryDeviceLocation(PPDMIMEDIAPORT pInterface, const char **ppcszController,
990 uint32_t *piInstance, uint32_t *piLUN)
991{
992 PDRVDISKINTEGRITY pThis = PDMIMEDIAPORT_2_DRVDISKINTEGRITY(pInterface);
993
994 return pThis->pDrvMediaPort->pfnQueryDeviceLocation(pThis->pDrvMediaPort, ppcszController,
995 piInstance, piLUN);
996}
997
998/* -=-=-=-=- IMediaExPort -=-=-=-=- */
999
1000/**
1001 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCompleteNotify}
1002 */
1003static DECLCALLBACK(int) drvdiskintIoReqCompleteNotify(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1004 void *pvIoReqAlloc, int rcReq)
1005{
1006 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1007 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1008 int rc = VINF_SUCCESS;
1009
1010 LogFlowFunc(("pIoReq=%#p\n", pIoReq));
1011
1012 /* Remove from the active list. */
1013 if (pThis->fTraceRequests)
1014 drvdiskintIoReqRemove(pThis, pIoReq);
1015
1016 if (RT_SUCCESS(rcReq) && pThis->fCheckConsistency)
1017 {
1018 if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ)
1019 rc = drvdiskintReadVerify(pThis, &pIoReq->IoSeg, 1, pIoReq->off, pIoReq->cbTransfer);
1020 else if ( pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE
1021 && !pThis->fRecordWriteBeforeCompletion)
1022 rc = drvdiskintWriteRecord(pThis, &pIoReq->IoSeg, 1, pIoReq->off, pIoReq->cbTransfer);
1023 else if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_DISCARD)
1024 rc = drvdiskintDiscardRecords(pThis, pIoReq->paRanges, pIoReq->cRanges);
1025 else if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ_AFTER_WRITE)
1026 rc = drvdiskintReadAfterWriteVerify(pThis, pIoReq);
1027 else
1028 AssertMsg( pIoReq->enmTxDir == DRVDISKAIOTXDIR_FLUSH
1029 || ( pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE
1030 && pThis->fRecordWriteBeforeCompletion), ("Huh?\n"));
1031
1032 AssertRC(rc);
1033 }
1034
1035 if ( RT_SUCCESS(rcReq)
1036 && pThis->fValidateMemBufs
1037 && pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ)
1038 {
1039 /* Check that the guest memory buffer matches what was written. */
1040 RTSGSEG SegCmp;
1041 SegCmp.pvSeg = RTMemAlloc(pIoReq->cbTransfer);
1042 SegCmp.cbSeg = pIoReq->cbTransfer;
1043
1044 RTSGBUF SgBufCmp;
1045 RTSgBufInit(&SgBufCmp, &SegCmp, 1);
1046 rc = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1,
1047 0, &SgBufCmp, pIoReq->cbTransfer);
1048 AssertRC(rc);
1049
1050 RTSGBUF SgBuf;
1051 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1052 if (RTSgBufCmp(&SgBuf, &SgBufCmp, pIoReq->cbTransfer))
1053 {
1054 RTMsgError("Corrupted memory buffer at offset %llu!\n", 0);
1055 RTAssertDebugBreak();
1056 }
1057
1058 RTMemFree(SegCmp.pvSeg);
1059 }
1060
1061 if (pThis->hIoLogger)
1062 {
1063 RTSGBUF SgBuf;
1064
1065 if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ)
1066 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1067
1068 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, rc, &SgBuf);
1069 AssertRC(rc2);
1070 }
1071
1072 if ( pThis->fReadAfterWrite
1073 && pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE)
1074 {
1075#if 0 /** @todo */
1076 pIoReq->enmTxDir = DRVDISKAIOTXDIR_READ_AFTER_WRITE;
1077
1078 /* Add again because it was removed above. */
1079 if (pThis->fTraceRequests)
1080 drvdiskintIoReqAdd(pThis, pIoReq);
1081
1082 rc = pThis->pDrvMediaAsync->pfnStartRead(pThis->pDrvMediaAsync, pIoReq->off, pIoReq->paSeg, pIoReq->cSeg,
1083 pIoReq->cbTransfer, pIoReq);
1084 if (rc == VINF_VD_ASYNC_IO_FINISHED)
1085 {
1086 rc = drvdiskintReadAfterWriteVerify(pThis, pIoReq);
1087
1088 if (pThis->fTraceRequests)
1089 drvdiskintIoReqRemove(pThis, pIoReq);
1090 RTMemFree(pIoReq);
1091 }
1092 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1093 rc = VINF_SUCCESS;
1094 else if (RT_FAILURE(rc))
1095 RTMemFree(pIoReq);
1096#endif
1097 }
1098 else
1099 {
1100 rc = pThis->pDrvMediaExPort->pfnIoReqCompleteNotify(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, rcReq);
1101 /* Put on the watch list. */
1102 if (pThis->fCheckDoubleCompletion)
1103 drvdiskintIoReqCheckForDoubleCompletion(pThis, pIoReq, true /* fMediaEx */);
1104 }
1105
1106 return rc;
1107}
1108
1109/**
1110 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyFromBuf}
1111 */
1112static DECLCALLBACK(int) drvdiskintIoReqCopyFromBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1113 void *pvIoReqAlloc, uint32_t offDst, PRTSGBUF pSgBuf,
1114 size_t cbCopy)
1115{
1116 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1117 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1118 RTSGBUF SgBuf;
1119
1120 RTSgBufClone(&SgBuf, pSgBuf);
1121
1122 int rc = pThis->pDrvMediaExPort->pfnIoReqCopyFromBuf(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, offDst,
1123 pSgBuf, cbCopy);
1124 if ( RT_SUCCESS(rc)
1125 && pIoReq->IoSeg.pvSeg)
1126 {
1127 /* Update our copy. */
1128 RTSgBufCopyToBuf(&SgBuf, (uint8_t *)pIoReq->IoSeg.pvSeg + offDst, cbCopy);
1129 }
1130
1131 return rc;
1132}
1133
1134/**
1135 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyToBuf}
1136 */
1137static DECLCALLBACK(int) drvdiskintIoReqCopyToBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1138 void *pvIoReqAlloc, uint32_t offSrc, PRTSGBUF pSgBuf,
1139 size_t cbCopy)
1140{
1141 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1142 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1143 RTSGBUF SgBuf;
1144
1145 RTSgBufClone(&SgBuf, pSgBuf);
1146
1147 int rc = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, offSrc,
1148 pSgBuf, cbCopy);
1149 if ( RT_SUCCESS(rc)
1150 && pIoReq->IoSeg.pvSeg)
1151 {
1152 if (pThis->fValidateMemBufs)
1153 {
1154 /* Make sure what the caller requested matches what we got earlier. */
1155 RTSGBUF SgBufCmp;
1156 RTSgBufInit(&SgBufCmp, &pIoReq->IoSeg, 1);
1157 RTSgBufAdvance(&SgBufCmp, offSrc);
1158
1159 if (RTSgBufCmp(&SgBuf, &SgBufCmp, cbCopy))
1160 {
1161 RTMsgError("Corrupted memory buffer at offset %llu!\n", offSrc);
1162 RTAssertDebugBreak();
1163 }
1164 }
1165 else
1166 {
1167 /* Update our copy. */
1168 RTSgBufCopyToBuf(&SgBuf, (uint8_t *)pIoReq->IoSeg.pvSeg + offSrc, cbCopy);
1169 }
1170 }
1171
1172 return rc;
1173}
1174
1175/**
1176 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqQueryDiscardRanges}
1177 */
1178static DECLCALLBACK(int) drvdiskintIoReqQueryDiscardRanges(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1179 void *pvIoReqAlloc, uint32_t idxRangeStart,
1180 uint32_t cRanges, PRTRANGE paRanges,
1181 uint32_t *pcRanges)
1182{
1183 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1184 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1185
1186 return pThis->pDrvMediaExPort->pfnIoReqQueryDiscardRanges(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, idxRangeStart,
1187 cRanges, paRanges, pcRanges);
1188}
1189
1190/**
1191 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqStateChanged}
1192 */
1193static DECLCALLBACK(void) drvdiskintIoReqStateChanged(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1194 void *pvIoReqAlloc, PDMMEDIAEXIOREQSTATE enmState)
1195{
1196 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaExPort);
1197 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvIoReqAlloc;
1198
1199 pThis->pDrvMediaExPort->pfnIoReqStateChanged(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, enmState);
1200}
1201
1202/* -=-=-=-=- IMediaEx -=-=-=-=- */
1203
1204/**
1205 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqAllocSizeSet}
1206 */
1207static DECLCALLBACK(int) drvdiskintIoReqAllocSizeSet(PPDMIMEDIAEX pInterface, size_t cbIoReqAlloc)
1208{
1209 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1210
1211 /* Increase the amount by our private tracking structure. */
1212 cbIoReqAlloc += sizeof(DRVDISKAIOREQ);
1213
1214 pThis->fCheckDoubleCompletion = false;
1215
1216 return pThis->pDrvMediaEx->pfnIoReqAllocSizeSet(pThis->pDrvMediaEx, cbIoReqAlloc);
1217}
1218
1219/**
1220 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqAlloc}
1221 */
1222static DECLCALLBACK(int) drvdiskintIoReqAlloc(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc,
1223 PDMMEDIAEXIOREQID uIoReqId, uint32_t fFlags)
1224{
1225 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1226 PDRVDISKAIOREQ pIoReq = NULL;
1227
1228 int rc = pThis->pDrvMediaEx->pfnIoReqAlloc(pThis->pDrvMediaEx, phIoReq, (void **)&pIoReq, uIoReqId, fFlags);
1229 if RT_SUCCESS(rc)
1230 {
1231 pIoReq->enmTxDir = DRVDISKAIOTXDIR_INVALID;
1232 pIoReq->off = 0;
1233 pIoReq->cbTransfer = 0;
1234 pIoReq->paSeg = NULL;
1235 pIoReq->cSeg = 0;
1236 pIoReq->pvUser = NULL;
1237 pIoReq->iSlot = 0;
1238 pIoReq->tsStart = 0;
1239 pIoReq->tsComplete = 0;
1240 pIoReq->hIoLogEntry = NULL;
1241 pIoReq->IoSeg.pvSeg = NULL;
1242 pIoReq->IoSeg.cbSeg = 0;
1243
1244 /*
1245 * Store the size off the start of our tracking structure because it is
1246 * required to access it for the read/write callbacks.
1247 *
1248 * ASSUMPTION that the offset is constant.
1249 */
1250 if (!pThis->cbIoReqOpaque)
1251 pThis->cbIoReqOpaque = (uintptr_t)pIoReq - (uintptr_t)*phIoReq;
1252 else
1253 Assert(pThis->cbIoReqOpaque == (uintptr_t)pIoReq - (uintptr_t)*phIoReq);
1254
1255 *ppvIoReqAlloc = pIoReq + 1;
1256 }
1257
1258 return rc;
1259}
1260
1261/**
1262 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqFree}
1263 */
1264static DECLCALLBACK(int) drvdiskintIoReqFree(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
1265{
1266 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1267 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)((uintptr_t)hIoReq + pThis->cbIoReqOpaque);
1268
1269 if (pIoReq->IoSeg.pvSeg)
1270 RTMemFree(pIoReq->IoSeg.pvSeg);
1271
1272 return pThis->pDrvMediaEx->pfnIoReqFree(pThis->pDrvMediaEx, hIoReq);
1273}
1274
1275/**
1276 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancel}
1277 */
1278static DECLCALLBACK(int) drvdiskintIoReqCancel(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQID uIoReqId)
1279{
1280 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1281 return pThis->pDrvMediaEx->pfnIoReqCancel(pThis->pDrvMediaEx, uIoReqId);
1282}
1283
1284/**
1285 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqRead}
1286 */
1287static DECLCALLBACK(int) drvdiskintIoReqRead(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbRead)
1288{
1289 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1290 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)((uintptr_t)hIoReq + pThis->cbIoReqOpaque);
1291
1292 pIoReq->enmTxDir = DRVDISKAIOTXDIR_READ;
1293 pIoReq->off = off;
1294 pIoReq->cbTransfer = cbRead;
1295
1296 /* Allocate a I/O buffer if the I/O is verified.*/
1297 if (pThis->fCheckConsistency)
1298 {
1299 pIoReq->IoSeg.pvSeg = RTMemAlloc(cbRead);
1300 pIoReq->IoSeg.cbSeg = cbRead;
1301 }
1302
1303 if (pThis->fTraceRequests)
1304 drvdiskintIoReqAdd(pThis, pIoReq);
1305
1306 if (pThis->hIoLogger)
1307 {
1308 int rc2 = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_READ, off,
1309 cbRead, NULL, &pIoReq->hIoLogEntry);
1310 AssertRC(rc2);
1311 }
1312
1313 int rc = pThis->pDrvMediaEx->pfnIoReqRead(pThis->pDrvMediaEx, hIoReq, off, cbRead);
1314 if (rc == VINF_SUCCESS)
1315 {
1316 /* Verify the read now. */
1317 if (pThis->fCheckConsistency)
1318 {
1319 int rc2 = drvdiskintReadVerify(pThis, &pIoReq->IoSeg, 1, off, cbRead);
1320 AssertRC(rc2);
1321 }
1322
1323 if (pThis->hIoLogger)
1324 {
1325 RTSGBUF SgBuf;
1326
1327 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1328
1329 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, &SgBuf);
1330 AssertRC(rc2);
1331 }
1332
1333 if (pThis->fTraceRequests)
1334 drvdiskintIoReqRemove(pThis, pIoReq);
1335 }
1336
1337 LogFlowFunc(("returns %Rrc\n", rc));
1338 return rc;
1339}
1340
1341/**
1342 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqWrite}
1343 */
1344static DECLCALLBACK(int) drvdiskintIoReqWrite(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbWrite)
1345{
1346 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1347 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)((uintptr_t)hIoReq + pThis->cbIoReqOpaque);
1348
1349 pIoReq->enmTxDir = DRVDISKAIOTXDIR_WRITE;
1350 pIoReq->off = off;
1351 pIoReq->cbTransfer = cbWrite;
1352
1353 /* Allocate a I/O buffer if the I/O is verified.*/
1354 if ( pThis->fCheckConsistency
1355 || pThis->fValidateMemBufs
1356 || pThis->hIoLogger
1357 || pThis->fRecordWriteBeforeCompletion)
1358 {
1359 pIoReq->IoSeg.pvSeg = RTMemAlloc(cbWrite);
1360 pIoReq->IoSeg.cbSeg = cbWrite;
1361
1362 /* Sync the memory buffer over if we should validate it. */
1363 if ( pThis->fValidateMemBufs
1364 || pThis->hIoLogger
1365 || pThis->fRecordWriteBeforeCompletion)
1366 {
1367 RTSGBUF SgBuf;
1368
1369 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1370 int rc2 = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, hIoReq, pIoReq + 1, 0,
1371 &SgBuf, cbWrite);
1372 AssertRC(rc2);
1373 }
1374 }
1375
1376 if (pThis->fTraceRequests)
1377 drvdiskintIoReqAdd(pThis, pIoReq);
1378
1379 if (pThis->hIoLogger)
1380 {
1381 RTSGBUF SgBuf;
1382
1383 RTSgBufInit(&SgBuf, &pIoReq->IoSeg, 1);
1384 int rc2 = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_WRITE, off,
1385 cbWrite, &SgBuf, &pIoReq->hIoLogEntry);
1386 AssertRC(rc2);
1387 }
1388
1389 if (pThis->fRecordWriteBeforeCompletion)
1390 {
1391
1392 int rc2 = drvdiskintWriteRecord(pThis, &pIoReq->IoSeg, 1, off, cbWrite);
1393 AssertRC(rc2);
1394 }
1395
1396 int rc = pThis->pDrvMediaEx->pfnIoReqWrite(pThis->pDrvMediaEx, hIoReq, off, cbWrite);
1397 if (rc == VINF_SUCCESS)
1398 {
1399 /* Record the write. */
1400 if ( pThis->fCheckConsistency
1401 && !pThis->fRecordWriteBeforeCompletion)
1402 {
1403 int rc2 = drvdiskintWriteRecord(pThis, &pIoReq->IoSeg, 1, off, cbWrite);
1404 AssertRC(rc2);
1405 }
1406
1407 if (pThis->hIoLogger)
1408 {
1409 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, NULL);
1410 AssertRC(rc2);
1411 }
1412
1413 if (pThis->fTraceRequests)
1414 drvdiskintIoReqRemove(pThis, pIoReq);
1415 }
1416
1417 LogFlowFunc(("returns %Rrc\n", rc));
1418 return rc;
1419}
1420
1421/**
1422 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqFlush}
1423 */
1424static DECLCALLBACK(int) drvdiskintIoReqFlush(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
1425{
1426 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1427 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)((uintptr_t)hIoReq + pThis->cbIoReqOpaque);
1428
1429 pIoReq->enmTxDir = DRVDISKAIOTXDIR_FLUSH;
1430 pIoReq->off = 0;
1431 pIoReq->cbTransfer = 0;
1432
1433 if (pThis->fTraceRequests)
1434 drvdiskintIoReqAdd(pThis, pIoReq);
1435
1436 if (pThis->hIoLogger)
1437 {
1438 int rc2 = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_FLUSH, 0,
1439 0, NULL, &pIoReq->hIoLogEntry);
1440 AssertRC(rc2);
1441 }
1442
1443 int rc = pThis->pDrvMediaEx->pfnIoReqFlush(pThis->pDrvMediaEx, hIoReq);
1444 if (rc == VINF_SUCCESS)
1445 {
1446 if (pThis->hIoLogger)
1447 {
1448 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, NULL);
1449 AssertRC(rc2);
1450 }
1451 }
1452
1453 LogFlowFunc(("returns %Rrc\n", rc));
1454 return rc;
1455}
1456
1457/**
1458 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqDiscard}
1459 */
1460static DECLCALLBACK(int) drvdiskintIoReqDiscard(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, unsigned cRangesMax)
1461{
1462 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1463 return pThis->pDrvMediaEx->pfnIoReqDiscard(pThis->pDrvMediaEx, hIoReq, cRangesMax);
1464}
1465
1466/**
1467 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetActiveCount}
1468 */
1469static DECLCALLBACK(uint32_t) drvdiskintIoReqGetActiveCount(PPDMIMEDIAEX pInterface)
1470{
1471 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1472 return pThis->pDrvMediaEx->pfnIoReqGetActiveCount(pThis->pDrvMediaEx);
1473}
1474
1475/**
1476 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetSuspendedCount}
1477 */
1478static DECLCALLBACK(uint32_t) drvdiskintIoReqGetSuspendedCount(PPDMIMEDIAEX pInterface)
1479{
1480 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1481 return pThis->pDrvMediaEx->pfnIoReqGetSuspendedCount(pThis->pDrvMediaEx);
1482}
1483
1484/**
1485 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedStart}
1486 */
1487static DECLCALLBACK(int) drvdiskintIoReqQuerySuspendedStart(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc)
1488{
1489 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1490 return pThis->pDrvMediaEx->pfnIoReqQuerySuspendedStart(pThis->pDrvMediaEx, phIoReq, ppvIoReqAlloc);
1491}
1492
1493/**
1494 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedNext}
1495 */
1496static DECLCALLBACK(int) drvdiskintIoReqQuerySuspendedNext(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq,
1497 PPDMMEDIAEXIOREQ phIoReqNext, void **ppvIoReqAllocNext)
1498{
1499 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1500 return pThis->pDrvMediaEx->pfnIoReqQuerySuspendedNext(pThis->pDrvMediaEx, hIoReq, phIoReqNext, ppvIoReqAllocNext);
1501}
1502
1503/**
1504 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedSave}
1505 */
1506static DECLCALLBACK(int) drvdiskintIoReqSuspendedSave(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
1507{
1508 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1509 return pThis->pDrvMediaEx->pfnIoReqSuspendedSave(pThis->pDrvMediaEx, pSSM, hIoReq);
1510}
1511
1512/**
1513 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedLoad}
1514 */
1515static DECLCALLBACK(int) drvdiskintIoReqSuspendedLoad(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
1516{
1517 PDRVDISKINTEGRITY pThis = RT_FROM_MEMBER(pInterface, DRVDISKINTEGRITY, IMediaEx);
1518 return pThis->pDrvMediaEx->pfnIoReqSuspendedLoad(pThis->pDrvMediaEx, pSSM, hIoReq);
1519}
1520
1521/* -=-=-=-=- IBase -=-=-=-=- */
1522
1523/**
1524 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1525 */
1526static DECLCALLBACK(void *) drvdiskintQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1527{
1528 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1529 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1530
1531 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1532 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
1533 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAPORT, &pThis->IMediaPort);
1534 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEXPORT, &pThis->IMediaExPort);
1535 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEX, pThis->pDrvMediaEx ? &pThis->IMediaEx : NULL);
1536 return NULL;
1537}
1538
1539
1540/* -=-=-=-=- driver interface -=-=-=-=- */
1541
1542static DECLCALLBACK(int) drvdiskintTreeDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
1543{
1544 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)pNode;
1545
1546 RT_NOREF(pvUser);
1547
1548 RTMemFree(pSeg->pbSeg);
1549 RTMemFree(pSeg);
1550 return VINF_SUCCESS;
1551}
1552
1553/**
1554 * @copydoc FNPDMDRVDESTRUCT
1555 */
1556static DECLCALLBACK(void) drvdiskintDestruct(PPDMDRVINS pDrvIns)
1557{
1558 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1559
1560 if (pThis->pTreeSegments)
1561 {
1562 RTAvlrFileOffsetDestroy(pThis->pTreeSegments, drvdiskintTreeDestroy, NULL);
1563 RTMemFree(pThis->pTreeSegments);
1564 }
1565
1566 if (pThis->fTraceRequests)
1567 {
1568 pThis->fRunning = false;
1569 RTSemEventSignal(pThis->SemEvent);
1570 RTSemEventDestroy(pThis->SemEvent);
1571 }
1572
1573 if (pThis->fCheckDoubleCompletion)
1574 {
1575 /* Free all requests */
1576 while (pThis->papIoReq[pThis->iEntry])
1577 {
1578 RTMemFree(pThis->papIoReq[pThis->iEntry]);
1579 pThis->papIoReq[pThis->iEntry] = NULL;
1580 pThis->iEntry = (pThis->iEntry+1) % pThis->cEntries;
1581 }
1582 }
1583
1584 if (pThis->hIoLogger)
1585 VDDbgIoLogDestroy(pThis->hIoLogger);
1586}
1587
1588/**
1589 * Construct a disk integrity driver instance.
1590 *
1591 * @copydoc FNPDMDRVCONSTRUCT
1592 */
1593static DECLCALLBACK(int) drvdiskintConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1594{
1595 int rc = VINF_SUCCESS;
1596 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1597 LogFlow(("drvdiskintConstruct: iInstance=%d\n", pDrvIns->iInstance));
1598 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1599
1600 /*
1601 * Validate configuration.
1602 */
1603 if (!CFGMR3AreValuesValid(pCfg, "CheckConsistency\0"
1604 "TraceRequests\0"
1605 "CheckIntervalMs\0"
1606 "ExpireIntervalMs\0"
1607 "CheckDoubleCompletions\0"
1608 "HistorySize\0"
1609 "IoLog\0"
1610 "PrepopulateRamDisk\0"
1611 "ReadAfterWrite\0"
1612 "RecordWriteBeforeCompletion\0"
1613 "ValidateMemoryBuffers\0"))
1614 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1615
1616 rc = CFGMR3QueryBoolDef(pCfg, "CheckConsistency", &pThis->fCheckConsistency, false);
1617 AssertRC(rc);
1618 rc = CFGMR3QueryBoolDef(pCfg, "TraceRequests", &pThis->fTraceRequests, false);
1619 AssertRC(rc);
1620 rc = CFGMR3QueryU32Def(pCfg, "CheckIntervalMs", &pThis->uCheckIntervalMs, 5000); /* 5 seconds */
1621 AssertRC(rc);
1622 rc = CFGMR3QueryU32Def(pCfg, "ExpireIntervalMs", &pThis->uExpireIntervalMs, 20000); /* 20 seconds */
1623 AssertRC(rc);
1624 rc = CFGMR3QueryBoolDef(pCfg, "CheckDoubleCompletions", &pThis->fCheckDoubleCompletion, false);
1625 AssertRC(rc);
1626 rc = CFGMR3QueryU32Def(pCfg, "HistorySize", &pThis->cEntries, 512);
1627 AssertRC(rc);
1628 rc = CFGMR3QueryBoolDef(pCfg, "PrepopulateRamDisk", &pThis->fPrepopulateRamDisk, false);
1629 AssertRC(rc);
1630 rc = CFGMR3QueryBoolDef(pCfg, "ReadAfterWrite", &pThis->fReadAfterWrite, false);
1631 AssertRC(rc);
1632 rc = CFGMR3QueryBoolDef(pCfg, "RecordWriteBeforeCompletion", &pThis->fRecordWriteBeforeCompletion, false);
1633 AssertRC(rc);
1634 rc = CFGMR3QueryBoolDef(pCfg, "ValidateMemoryBuffers", &pThis->fValidateMemBufs, true);
1635 AssertRC(rc);
1636
1637 char *pszIoLogFilename = NULL;
1638 rc = CFGMR3QueryStringAlloc(pCfg, "IoLog", &pszIoLogFilename);
1639 Assert(RT_SUCCESS(rc) || rc == VERR_CFGM_VALUE_NOT_FOUND);
1640
1641 /*
1642 * Initialize most of the data members.
1643 */
1644 pThis->pDrvIns = pDrvIns;
1645
1646 /* IBase. */
1647 pDrvIns->IBase.pfnQueryInterface = drvdiskintQueryInterface;
1648
1649 /* IMedia */
1650 pThis->IMedia.pfnRead = drvdiskintRead;
1651 pThis->IMedia.pfnWrite = drvdiskintWrite;
1652 pThis->IMedia.pfnFlush = drvdiskintFlush;
1653 pThis->IMedia.pfnGetSize = drvdiskintGetSize;
1654 pThis->IMedia.pfnIsReadOnly = drvdiskintIsReadOnly;
1655 pThis->IMedia.pfnBiosIsVisible = drvdiskintBiosIsVisible;
1656 pThis->IMedia.pfnBiosGetPCHSGeometry = drvdiskintBiosGetPCHSGeometry;
1657 pThis->IMedia.pfnBiosSetPCHSGeometry = drvdiskintBiosSetPCHSGeometry;
1658 pThis->IMedia.pfnBiosGetLCHSGeometry = drvdiskintBiosGetLCHSGeometry;
1659 pThis->IMedia.pfnBiosSetLCHSGeometry = drvdiskintBiosSetLCHSGeometry;
1660 pThis->IMedia.pfnGetUuid = drvdiskintGetUuid;
1661 pThis->IMedia.pfnGetSectorSize = drvdiskintGetSectorSize;
1662 pThis->IMedia.pfnGetType = drvdiskintGetType;
1663 pThis->IMedia.pfnIoBufAlloc = drvdiskintIoBufAlloc;
1664 pThis->IMedia.pfnIoBufFree = drvdiskintIoBufFree;
1665 pThis->IMedia.pfnReadPcBios = drvdiskintReadPcBios;
1666
1667 /* IMediaEx. */
1668 pThis->IMediaEx.pfnIoReqAllocSizeSet = drvdiskintIoReqAllocSizeSet;
1669 pThis->IMediaEx.pfnIoReqAlloc = drvdiskintIoReqAlloc;
1670 pThis->IMediaEx.pfnIoReqFree = drvdiskintIoReqFree;
1671 pThis->IMediaEx.pfnIoReqCancel = drvdiskintIoReqCancel;
1672 pThis->IMediaEx.pfnIoReqRead = drvdiskintIoReqRead;
1673 pThis->IMediaEx.pfnIoReqWrite = drvdiskintIoReqWrite;
1674 pThis->IMediaEx.pfnIoReqFlush = drvdiskintIoReqFlush;
1675 pThis->IMediaEx.pfnIoReqDiscard = drvdiskintIoReqDiscard;
1676 pThis->IMediaEx.pfnIoReqGetActiveCount = drvdiskintIoReqGetActiveCount;
1677 pThis->IMediaEx.pfnIoReqGetSuspendedCount = drvdiskintIoReqGetSuspendedCount;
1678 pThis->IMediaEx.pfnIoReqQuerySuspendedStart = drvdiskintIoReqQuerySuspendedStart;
1679 pThis->IMediaEx.pfnIoReqQuerySuspendedNext = drvdiskintIoReqQuerySuspendedNext;
1680 pThis->IMediaEx.pfnIoReqSuspendedSave = drvdiskintIoReqSuspendedSave;
1681 pThis->IMediaEx.pfnIoReqSuspendedLoad = drvdiskintIoReqSuspendedLoad;
1682
1683 /* IMediaPort. */
1684 pThis->IMediaPort.pfnQueryDeviceLocation = drvdiskintQueryDeviceLocation;
1685
1686 /* IMediaExPort. */
1687 pThis->IMediaExPort.pfnIoReqCompleteNotify = drvdiskintIoReqCompleteNotify;
1688 pThis->IMediaExPort.pfnIoReqCopyFromBuf = drvdiskintIoReqCopyFromBuf;
1689 pThis->IMediaExPort.pfnIoReqCopyToBuf = drvdiskintIoReqCopyToBuf;
1690 pThis->IMediaExPort.pfnIoReqQueryDiscardRanges = drvdiskintIoReqQueryDiscardRanges;
1691 pThis->IMediaExPort.pfnIoReqStateChanged = drvdiskintIoReqStateChanged;
1692
1693 /* Query the media port interface above us. */
1694 pThis->pDrvMediaPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAPORT);
1695 if (!pThis->pDrvMediaPort)
1696 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
1697 N_("No media port inrerface above"));
1698
1699 /* Try to attach extended media port interface above.*/
1700 pThis->pDrvMediaExPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAEXPORT);
1701
1702 /*
1703 * Try attach driver below and query it's media interface.
1704 */
1705 PPDMIBASE pBase;
1706 rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
1707 if (RT_FAILURE(rc))
1708 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1709 N_("Failed to attach driver below us! %Rrc"), rc);
1710
1711 pThis->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
1712 if (!pThis->pDrvMedia)
1713 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
1714 N_("No media or async media interface below"));
1715
1716 pThis->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIAEX);
1717
1718 if (pThis->pDrvMedia->pfnDiscard)
1719 pThis->IMedia.pfnDiscard = drvdiskintDiscard;
1720
1721 if (pThis->fCheckConsistency)
1722 {
1723 /* Create the AVL tree. */
1724 pThis->pTreeSegments = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
1725 if (!pThis->pTreeSegments)
1726 rc = VERR_NO_MEMORY;
1727 }
1728
1729 if (pThis->fTraceRequests)
1730 {
1731 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apReqActive); i++)
1732 {
1733 pThis->apReqActive[i].pIoReq = NULL;
1734 pThis->apReqActive[i].tsStart = 0;
1735 }
1736
1737 pThis->iNextFreeSlot = 0;
1738
1739 /* Init event semaphore. */
1740 rc = RTSemEventCreate(&pThis->SemEvent);
1741 AssertRC(rc);
1742 pThis->fRunning = true;
1743 rc = RTThreadCreate(&pThis->hThread, drvdiskIntIoReqExpiredCheck, pThis,
1744 0, RTTHREADTYPE_INFREQUENT_POLLER, 0, "DiskIntegrity");
1745 AssertRC(rc);
1746 }
1747
1748 if (pThis->fCheckDoubleCompletion)
1749 {
1750 pThis->iEntry = 0;
1751 pThis->papIoReq = (PDRVDISKAIOREQ *)RTMemAllocZ(pThis->cEntries * sizeof(PDRVDISKAIOREQ));
1752 AssertPtr(pThis->papIoReq);
1753 }
1754
1755 if (pszIoLogFilename)
1756 {
1757 rc = VDDbgIoLogCreate(&pThis->hIoLogger, pszIoLogFilename, VDDBG_IOLOG_LOG_DATA);
1758 MMR3HeapFree(pszIoLogFilename);
1759 }
1760
1761 /* Read in all data before the start if requested. */
1762 if (pThis->fPrepopulateRamDisk)
1763 {
1764 uint64_t cbDisk = 0;
1765
1766 LogRel(("DiskIntegrity: Prepopulating RAM disk, this will take some time...\n"));
1767
1768 cbDisk = pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
1769 if (cbDisk)
1770 {
1771 uint64_t off = 0;
1772 uint8_t abBuffer[_64K];
1773 RTSGSEG Seg;
1774
1775 Seg.pvSeg = abBuffer;
1776
1777 while (cbDisk)
1778 {
1779 size_t cbThisRead = RT_MIN(cbDisk, sizeof(abBuffer));
1780
1781 rc = pThis->pDrvMedia->pfnRead(pThis->pDrvMedia, off, abBuffer, cbThisRead);
1782 if (RT_FAILURE(rc))
1783 break;
1784
1785 if (ASMBitFirstSet(abBuffer, sizeof(abBuffer) * 8) != -1)
1786 {
1787 Seg.cbSeg = cbThisRead;
1788 rc = drvdiskintWriteRecord(pThis, &Seg, 1,
1789 off, cbThisRead);
1790 if (RT_FAILURE(rc))
1791 break;
1792 }
1793
1794 cbDisk -= cbThisRead;
1795 off += cbThisRead;
1796 }
1797
1798 LogRel(("DiskIntegrity: Prepopulating RAM disk finished with %Rrc\n", rc));
1799 }
1800 else
1801 return PDMDRV_SET_ERROR(pDrvIns, VERR_INTERNAL_ERROR,
1802 N_("DiskIntegrity: Error querying the media size below"));
1803 }
1804
1805 return rc;
1806}
1807
1808
1809/**
1810 * Block driver registration record.
1811 */
1812const PDMDRVREG g_DrvDiskIntegrity =
1813{
1814 /* u32Version */
1815 PDM_DRVREG_VERSION,
1816 /* szName */
1817 "DiskIntegrity",
1818 /* szRCMod */
1819 "",
1820 /* szR0Mod */
1821 "",
1822 /* pszDescription */
1823 "Disk integrity driver.",
1824 /* fFlags */
1825 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1826 /* fClass. */
1827 PDM_DRVREG_CLASS_BLOCK,
1828 /* cMaxInstances */
1829 ~0U,
1830 /* cbInstance */
1831 sizeof(DRVDISKINTEGRITY),
1832 /* pfnConstruct */
1833 drvdiskintConstruct,
1834 /* pfnDestruct */
1835 drvdiskintDestruct,
1836 /* pfnRelocate */
1837 NULL,
1838 /* pfnIOCtl */
1839 NULL,
1840 /* pfnPowerOn */
1841 NULL,
1842 /* pfnReset */
1843 NULL,
1844 /* pfnSuspend */
1845 NULL,
1846 /* pfnResume */
1847 NULL,
1848 /* pfnAttach */
1849 NULL,
1850 /* pfnDetach */
1851 NULL,
1852 /* pfnPowerOff */
1853 NULL,
1854 /* pfnSoftReset */
1855 NULL,
1856 /* u32EndVersion */
1857 PDM_DRVREG_VERSION
1858};
1859
Note: See TracBrowser for help on using the repository browser.

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