VirtualBox

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

Last change on this file since 57358 was 57358, checked in by vboxsync, 10 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 52.7 KB
Line 
1/* $Id: DrvDiskIntegrity.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * VBox storage devices: Disk integrity check.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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/vddbg.h>
25#include <iprt/assert.h>
26#include <iprt/string.h>
27#include <iprt/uuid.h>
28#include <iprt/avl.h>
29#include <iprt/mem.h>
30#include <iprt/message.h>
31#include <iprt/sg.h>
32#include <iprt/time.h>
33#include <iprt/semaphore.h>
34#include <iprt/asm.h>
35
36#include "VBoxDD.h"
37
38
39/*********************************************************************************************************************************
40* Structures and Typedefs *
41*********************************************************************************************************************************/
42
43/**
44 * Transfer direction.
45 */
46typedef enum DRVDISKAIOTXDIR
47{
48 /** Read */
49 DRVDISKAIOTXDIR_READ = 0,
50 /** Write */
51 DRVDISKAIOTXDIR_WRITE,
52 /** Flush */
53 DRVDISKAIOTXDIR_FLUSH,
54 /** Discard */
55 DRVDISKAIOTXDIR_DISCARD,
56 /** Read after write for immediate verification. */
57 DRVDISKAIOTXDIR_READ_AFTER_WRITE
58} DRVDISKAIOTXDIR;
59
60/**
61 * async I/O request.
62 */
63typedef struct DRVDISKAIOREQ
64{
65 /** Transfer direction. */
66 DRVDISKAIOTXDIR enmTxDir;
67 /** Start offset. */
68 uint64_t off;
69 /** Transfer size. */
70 size_t cbTransfer;
71 /** Segment array. */
72 PCRTSGSEG paSeg;
73 /** Number of array entries. */
74 unsigned cSeg;
75 /** User argument */
76 void *pvUser;
77 /** Slot in the array. */
78 unsigned iSlot;
79 /** Start timestamp */
80 uint64_t tsStart;
81 /** Completion timestamp. */
82 uint64_t tsComplete;
83 /** I/O log entry if configured. */
84 VDIOLOGENT hIoLogEntry;
85 /** Ranges to discard. */
86 PCRTRANGE paRanges;
87 /** Number of ranges. */
88 unsigned cRanges;
89} DRVDISKAIOREQ, *PDRVDISKAIOREQ;
90
91/**
92 * I/O log entry.
93 */
94typedef struct IOLOGENT
95{
96 /** Start offset */
97 uint64_t off;
98 /** Write size */
99 size_t cbWrite;
100 /** Number of references to this entry. */
101 unsigned cRefs;
102} IOLOGENT, *PIOLOGENT;
103
104/**
105 * Disk segment.
106 */
107typedef struct DRVDISKSEGMENT
108{
109 /** AVL core. */
110 AVLRFOFFNODECORE Core;
111 /** Size of the segment */
112 size_t cbSeg;
113 /** Data for this segment */
114 uint8_t *pbSeg;
115 /** Number of entries in the I/O array. */
116 unsigned cIoLogEntries;
117 /** Array of I/O log references. */
118 PIOLOGENT apIoLog[1];
119} DRVDISKSEGMENT, *PDRVDISKSEGMENT;
120
121/**
122 * Active requests list entry.
123 */
124typedef struct DRVDISKAIOREQACTIVE
125{
126 /** Pointer to the request. */
127 volatile PDRVDISKAIOREQ pIoReq;
128 /** Start timestamp. */
129 uint64_t tsStart;
130} DRVDISKAIOREQACTIVE, *PDRVDISKAIOREQACTIVE;
131
132/**
133 * Disk integrity driver instance data.
134 *
135 * @implements PDMIMEDIA
136 */
137typedef struct DRVDISKINTEGRITY
138{
139 /** Pointer driver instance. */
140 PPDMDRVINS pDrvIns;
141 /** Pointer to the media driver below us.
142 * This is NULL if the media is not mounted. */
143 PPDMIMEDIA pDrvMedia;
144 /** Our media interface */
145 PDMIMEDIA IMedia;
146
147 /** The media port interface above. */
148 PPDMIMEDIAPORT pDrvMediaPort;
149 /** Media port interface */
150 PDMIMEDIAPORT IMediaPort;
151
152 /** Pointer to the media async driver below us.
153 * This is NULL if the media is not mounted. */
154 PPDMIMEDIAASYNC pDrvMediaAsync;
155 /** Our media async interface */
156 PDMIMEDIAASYNC IMediaAsync;
157
158 /** The async media port interface above. */
159 PPDMIMEDIAASYNCPORT pDrvMediaAsyncPort;
160 /** Our media async port interface */
161 PDMIMEDIAASYNCPORT IMediaAsyncPort;
162
163 /** Flag whether consistency checks are enabled. */
164 bool fCheckConsistency;
165 /** Flag whether the RAM disk was prepopulated. */
166 bool fPrepopulateRamDisk;
167 /** AVL tree containing the disk blocks to check. */
168 PAVLRFOFFTREE pTreeSegments;
169
170 /** Flag whether async request tracing is enabled. */
171 bool fTraceRequests;
172 /** Interval the thread should check for expired requests (milliseconds). */
173 uint32_t uCheckIntervalMs;
174 /** Expire timeout for a request (milliseconds). */
175 uint32_t uExpireIntervalMs;
176 /** Thread which checks for lost requests. */
177 RTTHREAD hThread;
178 /** Event semaphore */
179 RTSEMEVENT SemEvent;
180 /** Flag whether the thread should run. */
181 bool fRunning;
182 /** Array containing active requests. */
183 DRVDISKAIOREQACTIVE apReqActive[128];
184 /** Next free slot in the array */
185 volatile unsigned iNextFreeSlot;
186
187 /** Flag whether we check for requests completing twice. */
188 bool fCheckDoubleCompletion;
189 /** Number of requests we go back. */
190 unsigned cEntries;
191 /** Array of completed but still observed requests. */
192 PDRVDISKAIOREQ *papIoReq;
193 /** Current entry in the array. */
194 unsigned iEntry;
195
196 /** Flag whether to do a immediate read after write for verification. */
197 bool fReadAfterWrite;
198 /** Flag whether to record the data to write before the write completed successfully.
199 * Useful in case the data is modified in place later on (encryption for instance). */
200 bool fRecordWriteBeforeCompletion;
201
202 /** I/O logger to use if enabled. */
203 VDIOLOGGER hIoLogger;
204} DRVDISKINTEGRITY, *PDRVDISKINTEGRITY;
205
206
207/**
208 * Allocate a new I/O request.
209 *
210 * @returns New I/O request.
211 * @param enmTxDir Transfer direction.
212 * @param off Start offset.
213 * @param paSeg Segment array.
214 * @param cSeg Number of segments.
215 * @param cbTransfer Number of bytes to transfer.
216 * @param pvUser User argument.
217 */
218static PDRVDISKAIOREQ drvdiskintIoReqAlloc(DRVDISKAIOTXDIR enmTxDir, uint64_t off, PCRTSGSEG paSeg,
219 unsigned cSeg, size_t cbTransfer, void *pvUser)
220{
221 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)RTMemAlloc(sizeof(DRVDISKAIOREQ));
222
223 if (RT_LIKELY(pIoReq))
224 {
225 pIoReq->enmTxDir = enmTxDir;
226 pIoReq->off = off;
227 pIoReq->cbTransfer = cbTransfer;
228 pIoReq->paSeg = paSeg;
229 pIoReq->cSeg = cSeg;
230 pIoReq->pvUser = pvUser;
231 pIoReq->iSlot = 0;
232 pIoReq->tsStart = RTTimeSystemMilliTS();
233 pIoReq->tsComplete = 0;
234 pIoReq->hIoLogEntry = NULL;
235 }
236
237 return pIoReq;
238}
239
240/**
241 * Free a async I/O request.
242 *
243 * @returns nothing.
244 * @param pThis Disk driver.
245 * @param pIoReq The I/O request to free.
246 */
247static void drvdiskintIoReqFree(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
248{
249 if (pThis->fCheckDoubleCompletion)
250 {
251 /* Search if the I/O request completed already. */
252 for (unsigned i = 0; i < pThis->cEntries; i++)
253 {
254 if (RT_UNLIKELY(pThis->papIoReq[i] == pIoReq))
255 {
256 RTMsgError("Request %#p completed already!\n", pIoReq);
257 RTMsgError("Start timestamp %llu Completion timestamp %llu (completed after %llu ms)\n",
258 pIoReq->tsStart, pIoReq->tsComplete, pIoReq->tsComplete - pIoReq->tsStart);
259 RTAssertDebugBreak();
260 }
261 }
262
263 pIoReq->tsComplete = RTTimeSystemMilliTS();
264 Assert(!pThis->papIoReq[pThis->iEntry]);
265 pThis->papIoReq[pThis->iEntry] = pIoReq;
266
267 pThis->iEntry = (pThis->iEntry+1) % pThis->cEntries;
268 if (pThis->papIoReq[pThis->iEntry])
269 {
270 RTMemFree(pThis->papIoReq[pThis->iEntry]);
271 pThis->papIoReq[pThis->iEntry] = NULL;
272 }
273 }
274 else
275 RTMemFree(pIoReq);
276}
277
278static void drvdiskintIoLogEntryRelease(PIOLOGENT pIoLogEnt)
279{
280 pIoLogEnt->cRefs--;
281 if (!pIoLogEnt->cRefs)
282 RTMemFree(pIoLogEnt);
283}
284
285/**
286 * Record a successful write to the virtual disk.
287 *
288 * @returns VBox status code.
289 * @param pThis Disk integrity driver instance data.
290 * @param paSeg Segment array of the write to record.
291 * @param cSeg Number of segments.
292 * @param off Start offset.
293 * @param cbWrite Number of bytes to record.
294 */
295static int drvdiskintWriteRecord(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
296 uint64_t off, size_t cbWrite)
297{
298 int rc = VINF_SUCCESS;
299
300 LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbWrite=%u\n",
301 pThis, paSeg, cSeg, off, cbWrite));
302
303 /* Update the segments */
304 size_t cbLeft = cbWrite;
305 RTFOFF offCurr = (RTFOFF)off;
306 RTSGBUF SgBuf;
307 PIOLOGENT pIoLogEnt = (PIOLOGENT)RTMemAllocZ(sizeof(IOLOGENT));
308 if (!pIoLogEnt)
309 return VERR_NO_MEMORY;
310
311 pIoLogEnt->off = off;
312 pIoLogEnt->cbWrite = cbWrite;
313 pIoLogEnt->cRefs = 0;
314
315 RTSgBufInit(&SgBuf, paSeg, cSeg);
316
317 while (cbLeft)
318 {
319 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
320 size_t cbRange = 0;
321 bool fSet = false;
322 unsigned offSeg = 0;
323
324 if (!pSeg)
325 {
326 /* Get next segment */
327 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
328 if ( !pSeg
329 || offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
330 cbRange = cbLeft;
331 else
332 cbRange = pSeg->Core.Key - offCurr;
333
334 Assert(cbRange % 512 == 0);
335
336 /* Create new segment */
337 pSeg = (PDRVDISKSEGMENT)RTMemAllocZ(RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbRange / 512]));
338 if (pSeg)
339 {
340 pSeg->Core.Key = offCurr;
341 pSeg->Core.KeyLast = offCurr + (RTFOFF)cbRange - 1;
342 pSeg->cbSeg = cbRange;
343 pSeg->pbSeg = (uint8_t *)RTMemAllocZ(cbRange);
344 pSeg->cIoLogEntries = cbRange / 512;
345 if (!pSeg->pbSeg)
346 RTMemFree(pSeg);
347 else
348 {
349 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
350 AssertMsg(fInserted, ("Bug!\n"));
351 fSet = true;
352 }
353 }
354 }
355 else
356 {
357 fSet = true;
358 offSeg = offCurr - pSeg->Core.Key;
359 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
360 }
361
362 if (fSet)
363 {
364 AssertPtr(pSeg);
365 size_t cbCopied = RTSgBufCopyToBuf(&SgBuf, pSeg->pbSeg + offSeg, cbRange);
366 Assert(cbCopied == cbRange);
367
368 /* Update the I/O log pointers */
369 Assert(offSeg % 512 == 0);
370 Assert(cbRange % 512 == 0);
371 while (offSeg < cbRange)
372 {
373 uint32_t uSector = offSeg / 512;
374 PIOLOGENT pIoLogOld = NULL;
375
376 AssertMsg(uSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
377
378 pIoLogOld = pSeg->apIoLog[uSector];
379 if (pIoLogOld)
380 {
381 pIoLogOld->cRefs--;
382 if (!pIoLogOld->cRefs)
383 RTMemFree(pIoLogOld);
384 }
385
386 pSeg->apIoLog[uSector] = pIoLogEnt;
387 pIoLogEnt->cRefs++;
388
389 offSeg += 512;
390 }
391 }
392 else
393 RTSgBufAdvance(&SgBuf, cbRange);
394
395 offCurr += cbRange;
396 cbLeft -= cbRange;
397 }
398
399 return rc;
400}
401
402/**
403 * Verifies a read request.
404 *
405 * @returns VBox status code.
406 * @param pThis Disk integrity driver instance data.
407 * @param paSeg Segment array of the containing the data buffers to verify.
408 * @param cSeg Number of segments.
409 * @param off Start offset.
410 * @param cbWrite Number of bytes to verify.
411 */
412static int drvdiskintReadVerify(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
413 uint64_t off, size_t cbRead)
414{
415 int rc = VINF_SUCCESS;
416
417 LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbRead=%u\n",
418 pThis, paSeg, cSeg, off, cbRead));
419
420 Assert(off % 512 == 0);
421 Assert(cbRead % 512 == 0);
422
423 /* Compare read data */
424 size_t cbLeft = cbRead;
425 RTFOFF offCurr = (RTFOFF)off;
426 RTSGBUF SgBuf;
427
428 RTSgBufInit(&SgBuf, paSeg, cSeg);
429
430 while (cbLeft)
431 {
432 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
433 size_t cbRange = 0;
434 bool fCmp = false;
435 unsigned offSeg = 0;
436
437 if (!pSeg)
438 {
439 /* Get next segment */
440 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
441 if (!pSeg)
442 {
443 /* No data in the tree for this read. Assume everything is ok. */
444 cbRange = cbLeft;
445 }
446 else if (offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
447 cbRange = cbLeft;
448 else
449 cbRange = pSeg->Core.Key - offCurr;
450
451 if (pThis->fPrepopulateRamDisk)
452 {
453 /* No segment means everything should be 0 for this part. */
454 if (!RTSgBufIsZero(&SgBuf, cbRange))
455 {
456 RTMsgError("Corrupted disk at offset %llu (expected everything to be 0)!\n",
457 offCurr);
458 RTAssertDebugBreak();
459 }
460 }
461 }
462 else
463 {
464 fCmp = true;
465 offSeg = offCurr - pSeg->Core.Key;
466 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
467 }
468
469 if (fCmp)
470 {
471 RTSGSEG Seg;
472 RTSGBUF SgBufCmp;
473 size_t cbOff = 0;
474
475 Seg.cbSeg = cbRange;
476 Seg.pvSeg = pSeg->pbSeg + offSeg;
477
478 RTSgBufInit(&SgBufCmp, &Seg, 1);
479 if (RTSgBufCmpEx(&SgBuf, &SgBufCmp, cbRange, &cbOff, true))
480 {
481 /* Corrupted disk, print I/O log entry of the last write which accessed this range. */
482 uint32_t cSector = (offSeg + cbOff) / 512;
483 AssertMsg(cSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
484
485 RTMsgError("Corrupted disk at offset %llu (%u bytes in the current read buffer)!\n",
486 offCurr + cbOff, cbOff);
487 RTMsgError("Last write to this sector started at offset %llu with %u bytes (%u references to this log entry)\n",
488 pSeg->apIoLog[cSector]->off,
489 pSeg->apIoLog[cSector]->cbWrite,
490 pSeg->apIoLog[cSector]->cRefs);
491 RTAssertDebugBreak();
492 }
493 }
494 else
495 RTSgBufAdvance(&SgBuf, cbRange);
496
497 offCurr += cbRange;
498 cbLeft -= cbRange;
499 }
500
501 return rc;
502}
503
504/**
505 * Discards the given ranges from the disk.
506 *
507 * @returns VBox status code.
508 * @param pThis Disk integrity driver instance data.
509 * @param paRanges Array of ranges to discard.
510 * @param cRanges Number of ranges in the array.
511 */
512static int drvdiskintDiscardRecords(PDRVDISKINTEGRITY pThis, PCRTRANGE paRanges, unsigned cRanges)
513{
514 int rc = VINF_SUCCESS;
515
516 LogFlowFunc(("pThis=%#p paRanges=%#p cRanges=%u\n", pThis, paRanges, cRanges));
517
518 for (unsigned i = 0; i < cRanges; i++)
519 {
520 uint64_t offStart = paRanges[i].offStart;
521 size_t cbLeft = paRanges[i].cbRange;
522
523 LogFlowFunc(("Discarding off=%llu cbRange=%zu\n", offStart, cbLeft));
524
525 while (cbLeft)
526 {
527 size_t cbRange;
528 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offStart);
529
530 if (!pSeg)
531 {
532 /* Get next segment */
533 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offStart, true);
534 if ( !pSeg
535 || (RTFOFF)offStart + (RTFOFF)cbLeft <= pSeg->Core.Key)
536 cbRange = cbLeft;
537 else
538 cbRange = pSeg->Core.Key - offStart;
539
540 Assert(!(cbRange % 512));
541 }
542 else
543 {
544 size_t cbPreLeft, cbPostLeft;
545
546 cbRange = RT_MIN(cbLeft, pSeg->Core.KeyLast - offStart + 1);
547 cbPreLeft = offStart - pSeg->Core.Key;
548 cbPostLeft = pSeg->cbSeg - cbRange - cbPreLeft;
549
550 Assert(!(cbRange % 512));
551 Assert(!(cbPreLeft % 512));
552 Assert(!(cbPostLeft % 512));
553
554 LogFlowFunc(("cbRange=%zu cbPreLeft=%zu cbPostLeft=%zu\n",
555 cbRange, cbPreLeft, cbPostLeft));
556
557 RTAvlrFileOffsetRemove(pThis->pTreeSegments, pSeg->Core.Key);
558
559 if (!cbPreLeft && !cbPostLeft)
560 {
561 /* Just free the whole segment. */
562 LogFlowFunc(("Freeing whole segment pSeg=%#p\n", pSeg));
563 RTMemFree(pSeg->pbSeg);
564 for (unsigned idx = 0; idx < pSeg->cIoLogEntries; idx++)
565 drvdiskintIoLogEntryRelease(pSeg->apIoLog[idx]);
566 RTMemFree(pSeg);
567 }
568 else if (cbPreLeft && !cbPostLeft)
569 {
570 /* Realloc to new size and insert. */
571 LogFlowFunc(("Realloc segment pSeg=%#p\n", pSeg));
572 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPreLeft);
573 for (unsigned idx = cbPreLeft / 512; idx < pSeg->cIoLogEntries; idx++)
574 drvdiskintIoLogEntryRelease(pSeg->apIoLog[idx]);
575 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbPreLeft / 512]));
576 pSeg->Core.KeyLast = pSeg->Core.Key + cbPreLeft - 1;
577 pSeg->cbSeg = cbPreLeft;
578 pSeg->cIoLogEntries = cbPreLeft / 512;
579 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
580 Assert(fInserted);
581 }
582 else if (!cbPreLeft && cbPostLeft)
583 {
584 /* Move data to the front and realloc. */
585 LogFlowFunc(("Move data and realloc segment pSeg=%#p\n", pSeg));
586 memmove(pSeg->pbSeg, pSeg->pbSeg + cbRange, cbPostLeft);
587 for (unsigned idx = 0; idx < cbRange / 512; idx++)
588 drvdiskintIoLogEntryRelease(pSeg->apIoLog[idx]);
589 for (unsigned idx = 0; idx < cbPostLeft /512; idx++)
590 pSeg->apIoLog[idx] = pSeg->apIoLog[(cbRange / 512) + idx];
591 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbPostLeft / 512]));
592 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPostLeft);
593 pSeg->Core.Key += cbRange;
594 pSeg->cbSeg = cbPostLeft;
595 pSeg->cIoLogEntries = cbPostLeft / 512;
596 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
597 Assert(fInserted);
598 }
599 else
600 {
601 /* Split the segment into 2 new segments. */
602 LogFlowFunc(("Split segment pSeg=%#p\n", pSeg));
603 PDRVDISKSEGMENT pSegPost = (PDRVDISKSEGMENT)RTMemAllocZ(RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbPostLeft / 512]));
604 if (pSegPost)
605 {
606 pSegPost->Core.Key = pSeg->Core.Key + cbPreLeft + cbRange;
607 pSegPost->Core.KeyLast = pSeg->Core.KeyLast;
608 pSegPost->cbSeg = cbPostLeft;
609 pSegPost->pbSeg = (uint8_t *)RTMemAllocZ(cbPostLeft);
610 pSegPost->cIoLogEntries = cbPostLeft / 512;
611 if (!pSegPost->pbSeg)
612 RTMemFree(pSegPost);
613 else
614 {
615 memcpy(pSegPost->pbSeg, pSeg->pbSeg + cbPreLeft + cbRange, cbPostLeft);
616 for (unsigned idx = 0; idx < cbPostLeft / 512; idx++)
617 pSegPost->apIoLog[idx] = pSeg->apIoLog[((cbPreLeft + cbRange) / 512) + idx];
618
619 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSegPost->Core);
620 Assert(fInserted);
621 }
622 }
623
624 /* Shrink the current segment. */
625 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPreLeft);
626 for (unsigned idx = cbPreLeft / 512; idx < (cbPreLeft + cbRange) / 512; idx++)
627 drvdiskintIoLogEntryRelease(pSeg->apIoLog[idx]);
628 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbPreLeft / 512]));
629 pSeg->Core.KeyLast = pSeg->Core.Key + cbPreLeft - 1;
630 pSeg->cbSeg = cbPreLeft;
631 pSeg->cIoLogEntries = cbPreLeft / 512;
632 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
633 Assert(fInserted);
634 } /* if (cbPreLeft && cbPostLeft) */
635 }
636
637 offStart += cbRange;
638 cbLeft -= cbRange;
639 }
640 }
641
642 LogFlowFunc(("returns rc=%Rrc\n", rc));
643 return rc;
644}
645
646/**
647 * Adds a request to the active list.
648 *
649 * @returns nothing.
650 * @param pThis The driver instance data.
651 * @param pIoReq The request to add.
652 */
653static void drvdiskintIoReqAdd(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
654{
655 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[pThis->iNextFreeSlot];
656
657 Assert(!pReqActive->pIoReq);
658 pReqActive->tsStart = pIoReq->tsStart;
659 pReqActive->pIoReq = pIoReq;
660 pIoReq->iSlot = pThis->iNextFreeSlot;
661
662 /* Search for the next one. */
663 while (pThis->apReqActive[pThis->iNextFreeSlot].pIoReq)
664 pThis->iNextFreeSlot = (pThis->iNextFreeSlot+1) % RT_ELEMENTS(pThis->apReqActive);
665}
666
667/**
668 * Removes a request from the active list.
669 *
670 * @returns nothing.
671 * @param pThis The driver instance data.
672 * @param pIoReq The request to remove.
673 */
674static void drvdiskintIoReqRemove(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
675{
676 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[pIoReq->iSlot];
677
678 Assert(pReqActive->pIoReq == pIoReq);
679
680 ASMAtomicWriteNullPtr(&pReqActive->pIoReq);
681}
682
683/**
684 * Thread checking for expired requests.
685 *
686 * @returns IPRT status code.
687 * @param pThread Thread handle.
688 * @param pvUser Opaque user data.
689 */
690static int drvdiskIntIoReqExpiredCheck(RTTHREAD pThread, void *pvUser)
691{
692 PDRVDISKINTEGRITY pThis = (PDRVDISKINTEGRITY)pvUser;
693
694 while (pThis->fRunning)
695 {
696 int rc = RTSemEventWait(pThis->SemEvent, pThis->uCheckIntervalMs);
697
698 if (!pThis->fRunning)
699 break;
700
701 Assert(rc == VERR_TIMEOUT);
702
703 /* Get current timestamp for comparison. */
704 uint64_t tsCurr = RTTimeSystemMilliTS();
705
706 /* Go through the array and check for expired requests. */
707 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apReqActive); i++)
708 {
709 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[i];
710 PDRVDISKAIOREQ pIoReq = ASMAtomicReadPtrT(&pReqActive->pIoReq, PDRVDISKAIOREQ);
711
712 if ( pIoReq
713 && (tsCurr > pReqActive->tsStart)
714 && (tsCurr - pReqActive->tsStart) >= pThis->uExpireIntervalMs)
715 {
716 RTMsgError("Request %#p expired (active for %llu ms already)\n",
717 pIoReq, tsCurr - pReqActive->tsStart);
718 RTAssertDebugBreak();
719 }
720 }
721 }
722
723 return VINF_SUCCESS;
724}
725
726/**
727 * Verify a completed read after write request.
728 *
729 * @returns VBox status code.
730 * @param pThis The driver instance data.
731 * @param pIoReq The request to be verified.
732 */
733static int drvdiskintReadAfterWriteVerify(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
734{
735 int rc = VINF_SUCCESS;
736
737 if (pThis->fCheckConsistency)
738 rc = drvdiskintReadVerify(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
739 else /** @todo: Implement read after write verification without a memory based image of the disk. */
740 AssertMsgFailed(("TODO\n"));
741
742 return rc;
743}
744
745/* -=-=-=-=- IMedia -=-=-=-=- */
746
747/** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIA. */
748#define PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMedia)) )
749/** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIAASYNC. */
750#define PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaAsync)) )
751
752
753/*********************************************************************************************************************************
754* Media interface methods *
755*********************************************************************************************************************************/
756
757/** @copydoc PDMIMEDIA::pfnRead */
758static DECLCALLBACK(int) drvdiskintRead(PPDMIMEDIA pInterface,
759 uint64_t off, void *pvBuf, size_t cbRead)
760{
761 int rc = VINF_SUCCESS;
762 VDIOLOGENT hIoLogEntry;
763 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
764
765 if (pThis->hIoLogger)
766 {
767 rc = VDDbgIoLogStart(pThis->hIoLogger, false, VDDBGIOLOGREQ_READ, off,
768 cbRead, NULL, &hIoLogEntry);
769 AssertRC(rc);
770 }
771
772 rc = pThis->pDrvMedia->pfnRead(pThis->pDrvMedia, off, pvBuf, cbRead);
773
774 if (pThis->hIoLogger)
775 {
776 RTSGSEG Seg;
777 RTSGBUF SgBuf;
778
779 Seg.pvSeg = pvBuf;
780 Seg.cbSeg = cbRead;
781 RTSgBufInit(&SgBuf, &Seg, 1);
782
783 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, hIoLogEntry, rc, &SgBuf);
784 AssertRC(rc2);
785 }
786
787 if (RT_FAILURE(rc))
788 return rc;
789
790 if (pThis->fCheckConsistency)
791 {
792 /* Verify the read. */
793 RTSGSEG Seg;
794 Seg.cbSeg = cbRead;
795 Seg.pvSeg = pvBuf;
796 rc = drvdiskintReadVerify(pThis, &Seg, 1, off, cbRead);
797 }
798
799 return rc;
800}
801
802/** @copydoc PDMIMEDIA::pfnWrite */
803static DECLCALLBACK(int) drvdiskintWrite(PPDMIMEDIA pInterface,
804 uint64_t off, const void *pvBuf,
805 size_t cbWrite)
806{
807 int rc = VINF_SUCCESS;
808 VDIOLOGENT hIoLogEntry;
809 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
810
811 if (pThis->hIoLogger)
812 {
813 RTSGSEG Seg;
814 RTSGBUF SgBuf;
815
816 Seg.pvSeg = (void *)pvBuf;
817 Seg.cbSeg = cbWrite;
818 RTSgBufInit(&SgBuf, &Seg, 1);
819
820 rc = VDDbgIoLogStart(pThis->hIoLogger, false, VDDBGIOLOGREQ_WRITE, off,
821 cbWrite, &SgBuf, &hIoLogEntry);
822 AssertRC(rc);
823 }
824
825 if (pThis->fRecordWriteBeforeCompletion)
826 {
827 RTSGSEG Seg;
828 Seg.cbSeg = cbWrite;
829 Seg.pvSeg = (void *)pvBuf;
830
831 rc = drvdiskintWriteRecord(pThis, &Seg, 1, off, cbWrite);
832 if (RT_FAILURE(rc))
833 return rc;
834 }
835
836 rc = pThis->pDrvMedia->pfnWrite(pThis->pDrvMedia, off, pvBuf, cbWrite);
837
838 if (pThis->hIoLogger)
839 {
840 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, hIoLogEntry, rc, NULL);
841 AssertRC(rc2);
842 }
843
844 if (RT_FAILURE(rc))
845 return rc;
846
847 if ( pThis->fCheckConsistency
848 && !pThis->fRecordWriteBeforeCompletion)
849 {
850 /* Record the write. */
851 RTSGSEG Seg;
852 Seg.cbSeg = cbWrite;
853 Seg.pvSeg = (void *)pvBuf;
854 rc = drvdiskintWriteRecord(pThis, &Seg, 1, off, cbWrite);
855 }
856
857 return rc;
858}
859
860static DECLCALLBACK(int) drvdiskintStartRead(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
861 PCRTSGSEG paSeg, unsigned cSeg,
862 size_t cbRead, void *pvUser)
863{
864 LogFlow(("%s: uOffset=%llu paSeg=%#p cSeg=%u cbRead=%d pvUser=%#p\n", __FUNCTION__,
865 uOffset, paSeg, cSeg, cbRead, pvUser));
866 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
867 PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(DRVDISKAIOTXDIR_READ, uOffset, paSeg, cSeg, cbRead, pvUser);
868 AssertPtr(pIoReq);
869
870 if (pThis->fTraceRequests)
871 drvdiskintIoReqAdd(pThis, pIoReq);
872
873 if (pThis->hIoLogger)
874 {
875 int rc2 = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_READ, uOffset,
876 cbRead, NULL, &pIoReq->hIoLogEntry);
877 AssertRC(rc2);
878 }
879
880 int rc = pThis->pDrvMediaAsync->pfnStartRead(pThis->pDrvMediaAsync, uOffset, paSeg, cSeg,
881 cbRead, pIoReq);
882 if (rc == VINF_VD_ASYNC_IO_FINISHED)
883 {
884 /* Verify the read now. */
885 if (pThis->fCheckConsistency)
886 {
887 int rc2 = drvdiskintReadVerify(pThis, paSeg, cSeg, uOffset, cbRead);
888 AssertRC(rc2);
889 }
890
891 if (pThis->hIoLogger)
892 {
893 RTSGBUF SgBuf;
894
895 RTSgBufInit(&SgBuf, paSeg, cSeg);
896
897 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, &SgBuf);
898 AssertRC(rc2);
899 }
900
901 if (pThis->fTraceRequests)
902 drvdiskintIoReqRemove(pThis, pIoReq);
903 RTMemFree(pIoReq);
904 }
905 else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
906 RTMemFree(pIoReq);
907
908 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
909 return rc;
910}
911
912static DECLCALLBACK(int) drvdiskintStartWrite(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
913 PCRTSGSEG paSeg, unsigned cSeg,
914 size_t cbWrite, void *pvUser)
915{
916 LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbWrite=%d pvUser=%#p\n", __FUNCTION__,
917 uOffset, paSeg, cSeg, cbWrite, pvUser));
918 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
919 PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(DRVDISKAIOTXDIR_WRITE, uOffset, paSeg, cSeg, cbWrite, pvUser);
920 AssertPtr(pIoReq);
921
922 if (pThis->fTraceRequests)
923 drvdiskintIoReqAdd(pThis, pIoReq);
924
925 if (pThis->hIoLogger)
926 {
927 RTSGBUF SgBuf;
928
929 RTSgBufInit(&SgBuf, paSeg, cSeg);
930
931 int rc2 = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_WRITE, uOffset,
932 cbWrite, &SgBuf, &pIoReq->hIoLogEntry);
933 AssertRC(rc2);
934 }
935
936 if (pThis->fRecordWriteBeforeCompletion)
937 {
938 int rc2 = drvdiskintWriteRecord(pThis, paSeg, cSeg, uOffset, cbWrite);
939 AssertRC(rc2);
940 }
941
942 int rc = pThis->pDrvMediaAsync->pfnStartWrite(pThis->pDrvMediaAsync, uOffset, paSeg, cSeg,
943 cbWrite, pIoReq);
944 if (rc == VINF_VD_ASYNC_IO_FINISHED)
945 {
946 /* Record the write. */
947 if ( pThis->fCheckConsistency
948 && !pThis->fRecordWriteBeforeCompletion)
949 {
950 int rc2 = drvdiskintWriteRecord(pThis, paSeg, cSeg, uOffset, cbWrite);
951 AssertRC(rc2);
952 }
953
954 if (pThis->hIoLogger)
955 {
956 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, NULL);
957 AssertRC(rc2);
958 }
959
960 if (pThis->fTraceRequests)
961 drvdiskintIoReqRemove(pThis, pIoReq);
962
963 RTMemFree(pIoReq);
964 }
965 else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
966 RTMemFree(pIoReq);
967
968 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
969 return rc;
970}
971
972/** @copydoc PDMIMEDIAASYNC::pfnStartFlush */
973static DECLCALLBACK(int) drvdiskintStartFlush(PPDMIMEDIAASYNC pInterface, void *pvUser)
974{
975 int rc = VINF_SUCCESS;
976 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
977 PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(DRVDISKAIOTXDIR_FLUSH, 0, NULL, 0, 0, pvUser);
978 AssertPtr(pIoReq);
979
980 if (pThis->fTraceRequests)
981 drvdiskintIoReqAdd(pThis, pIoReq);
982
983 if (pThis->hIoLogger)
984 {
985 rc = VDDbgIoLogStart(pThis->hIoLogger, true, VDDBGIOLOGREQ_FLUSH, 0,
986 0, NULL, &pIoReq->hIoLogEntry);
987 AssertRC(rc);
988 }
989
990 rc = pThis->pDrvMediaAsync->pfnStartFlush(pThis->pDrvMediaAsync, pIoReq);
991
992 if (rc == VINF_VD_ASYNC_IO_FINISHED)
993 {
994 if (pThis->hIoLogger)
995 {
996 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, NULL);
997 AssertRC(rc2);
998 }
999
1000 RTMemFree(pIoReq);
1001 }
1002 else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
1003 RTMemFree(pIoReq);
1004
1005 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
1006 return rc;
1007}
1008
1009/** @copydoc PDMIMEDIAASYNC::pfnStartDiscard */
1010static DECLCALLBACK(int) drvdiskintStartDiscard(PPDMIMEDIAASYNC pInterface, PCRTRANGE paRanges, unsigned cRanges, void *pvUser)
1011{
1012 int rc = VINF_SUCCESS;
1013 VDIOLOGENT hIoLogEntry;
1014 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1015 PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(DRVDISKAIOTXDIR_DISCARD, 0, NULL, 0, 0, pvUser);
1016 AssertPtr(pIoReq);
1017
1018 pIoReq->paRanges = paRanges;
1019 pIoReq->cRanges = cRanges;
1020
1021 if (pThis->hIoLogger)
1022 {
1023 rc = VDDbgIoLogStartDiscard(pThis->hIoLogger, true, paRanges, cRanges, &hIoLogEntry);
1024 AssertRC(rc);
1025 }
1026
1027 rc = pThis->pDrvMediaAsync->pfnStartDiscard(pThis->pDrvMediaAsync, paRanges, cRanges, pIoReq);
1028
1029 if (rc == VINF_VD_ASYNC_IO_FINISHED)
1030 {
1031 if (pThis->hIoLogger)
1032 {
1033 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, VINF_SUCCESS, NULL);
1034 AssertRC(rc2);
1035 }
1036
1037 RTMemFree(pIoReq);
1038 }
1039 else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
1040 RTMemFree(pIoReq);
1041
1042 return rc;
1043}
1044
1045/** @copydoc PDMIMEDIA::pfnFlush */
1046static DECLCALLBACK(int) drvdiskintFlush(PPDMIMEDIA pInterface)
1047{
1048 int rc = VINF_SUCCESS;
1049 VDIOLOGENT hIoLogEntry;
1050 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1051
1052 if (pThis->hIoLogger)
1053 {
1054 rc = VDDbgIoLogStart(pThis->hIoLogger, false, VDDBGIOLOGREQ_FLUSH, 0,
1055 0, NULL, &hIoLogEntry);
1056 AssertRC(rc);
1057 }
1058
1059 rc = pThis->pDrvMedia->pfnFlush(pThis->pDrvMedia);
1060
1061 if (pThis->hIoLogger)
1062 {
1063 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, hIoLogEntry, rc, NULL);
1064 AssertRC(rc2);
1065 }
1066
1067 return rc;
1068}
1069
1070/** @copydoc PDMIMEDIA::pfnGetSize */
1071static DECLCALLBACK(uint64_t) drvdiskintGetSize(PPDMIMEDIA pInterface)
1072{
1073 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1074 return pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
1075}
1076
1077/** @copydoc PDMIMEDIA::pfnIsReadOnly */
1078static DECLCALLBACK(bool) drvdiskintIsReadOnly(PPDMIMEDIA pInterface)
1079{
1080 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1081 return pThis->pDrvMedia->pfnIsReadOnly(pThis->pDrvMedia);
1082}
1083
1084/** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
1085static DECLCALLBACK(int) drvdiskintBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
1086 PPDMMEDIAGEOMETRY pPCHSGeometry)
1087{
1088 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1089 return pThis->pDrvMedia->pfnBiosGetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
1090}
1091
1092/** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
1093static DECLCALLBACK(int) drvdiskintBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
1094 PCPDMMEDIAGEOMETRY pPCHSGeometry)
1095{
1096 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1097 return pThis->pDrvMedia->pfnBiosSetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
1098}
1099
1100/** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
1101static DECLCALLBACK(int) drvdiskintBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
1102 PPDMMEDIAGEOMETRY pLCHSGeometry)
1103{
1104 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1105 return pThis->pDrvMedia->pfnBiosGetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
1106}
1107
1108/** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
1109static DECLCALLBACK(int) drvdiskintBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
1110 PCPDMMEDIAGEOMETRY pLCHSGeometry)
1111{
1112 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1113 return pThis->pDrvMedia->pfnBiosSetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
1114}
1115
1116/** @copydoc PDMIMEDIA::pfnGetUuid */
1117static DECLCALLBACK(int) drvdiskintGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
1118{
1119 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1120 return pThis->pDrvMedia->pfnGetUuid(pThis->pDrvMedia, pUuid);
1121}
1122
1123/** @copydoc PDMIMEDIA::pfnGetSectorSize */
1124static DECLCALLBACK(uint32_t) drvdiskintGetSectorSize(PPDMIMEDIA pInterface)
1125{
1126 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1127 return pThis->pDrvMedia->pfnGetSectorSize(pThis->pDrvMedia);
1128}
1129
1130/** @copydoc PDMIMEDIA::pfnDiscard */
1131static DECLCALLBACK(int) drvdiskintDiscard(PPDMIMEDIA pInterface, PCRTRANGE paRanges, unsigned cRanges)
1132{
1133 int rc = VINF_SUCCESS;
1134 VDIOLOGENT hIoLogEntry;
1135 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
1136
1137 if (pThis->hIoLogger)
1138 {
1139 rc = VDDbgIoLogStartDiscard(pThis->hIoLogger, false, paRanges, cRanges, &hIoLogEntry);
1140 AssertRC(rc);
1141 }
1142
1143 rc = pThis->pDrvMedia->pfnDiscard(pThis->pDrvMedia, paRanges, cRanges);
1144
1145 if (pThis->hIoLogger)
1146 {
1147 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, hIoLogEntry, rc, NULL);
1148 AssertRC(rc2);
1149 }
1150
1151 if (pThis->fCheckConsistency)
1152 rc = drvdiskintDiscardRecords(pThis, paRanges, cRanges);
1153
1154 return rc;
1155}
1156
1157/* -=-=-=-=- IMediaAsyncPort -=-=-=-=- */
1158
1159/** Makes a PDRVBLOCKASYNC out of a PPDMIMEDIAASYNCPORT. */
1160#define PDMIMEDIAASYNCPORT_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaAsyncPort))) )
1161
1162static DECLCALLBACK(int) drvdiskintAsyncTransferCompleteNotify(PPDMIMEDIAASYNCPORT pInterface, void *pvUser, int rcReq)
1163{
1164 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNCPORT_2_DRVDISKINTEGRITY(pInterface);
1165 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvUser;
1166 int rc = VINF_SUCCESS;
1167
1168 LogFlowFunc(("pIoReq=%#p\n", pIoReq));
1169
1170 /* Remove from the active list. */
1171 if (pThis->fTraceRequests)
1172 drvdiskintIoReqRemove(pThis, pIoReq);
1173
1174 if (RT_SUCCESS(rcReq) && pThis->fCheckConsistency)
1175 {
1176 if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ)
1177 rc = drvdiskintReadVerify(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
1178 else if ( pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE
1179 && !pThis->fRecordWriteBeforeCompletion)
1180 rc = drvdiskintWriteRecord(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
1181 else if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_DISCARD)
1182 rc = drvdiskintDiscardRecords(pThis, pIoReq->paRanges, pIoReq->cRanges);
1183 else if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ_AFTER_WRITE)
1184 rc = drvdiskintReadAfterWriteVerify(pThis, pIoReq);
1185 else
1186 AssertMsg( pIoReq->enmTxDir == DRVDISKAIOTXDIR_FLUSH
1187 || ( pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE
1188 && pThis->fRecordWriteBeforeCompletion), ("Huh?\n"));
1189
1190 AssertRC(rc);
1191 }
1192
1193 if (pThis->hIoLogger)
1194 {
1195 RTSGBUF SgBuf;
1196
1197 if (pIoReq->enmTxDir == DRVDISKAIOTXDIR_READ)
1198 RTSgBufInit(&SgBuf, pIoReq->paSeg, pIoReq->cSeg);
1199
1200 int rc2 = VDDbgIoLogComplete(pThis->hIoLogger, pIoReq->hIoLogEntry, rc, &SgBuf);
1201 AssertRC(rc2);
1202 }
1203
1204 if ( pThis->fReadAfterWrite
1205 && pIoReq->enmTxDir == DRVDISKAIOTXDIR_WRITE)
1206 {
1207 pIoReq->enmTxDir = DRVDISKAIOTXDIR_READ_AFTER_WRITE;
1208
1209 /* Readd because it was rmeoved above. */
1210 if (pThis->fTraceRequests)
1211 drvdiskintIoReqAdd(pThis, pIoReq);
1212
1213 rc = pThis->pDrvMediaAsync->pfnStartRead(pThis->pDrvMediaAsync, pIoReq->off, pIoReq->paSeg, pIoReq->cSeg,
1214 pIoReq->cbTransfer, pIoReq);
1215 if (rc == VINF_VD_ASYNC_IO_FINISHED)
1216 {
1217 rc = drvdiskintReadAfterWriteVerify(pThis, pIoReq);
1218
1219 if (pThis->fTraceRequests)
1220 drvdiskintIoReqRemove(pThis, pIoReq);
1221 RTMemFree(pIoReq);
1222 }
1223 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1224 rc = VINF_SUCCESS;
1225 else if (RT_FAILURE(rc))
1226 RTMemFree(pIoReq);
1227 }
1228 else
1229 {
1230 void *pvUserComplete = pIoReq->pvUser;
1231 drvdiskintIoReqFree(pThis, pIoReq);
1232
1233 rc = pThis->pDrvMediaAsyncPort->pfnTransferCompleteNotify(pThis->pDrvMediaAsyncPort, pvUserComplete, rcReq);
1234 }
1235
1236 return rc;
1237}
1238
1239/* -=-=-=-=- IMediaPort -=-=-=-=- */
1240
1241/** Makes a PDRVBLOCK out of a PPDMIMEDIAPORT. */
1242#define PDMIMEDIAPORT_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaPort))) )
1243
1244/**
1245 * @interface_method_impl{PDMIMEDIAPORT,pfnQueryDeviceLocation}
1246 */
1247static DECLCALLBACK(int) drvdiskintQueryDeviceLocation(PPDMIMEDIAPORT pInterface, const char **ppcszController,
1248 uint32_t *piInstance, uint32_t *piLUN)
1249{
1250 PDRVDISKINTEGRITY pThis = PDMIMEDIAPORT_2_DRVDISKINTEGRITY(pInterface);
1251
1252 return pThis->pDrvMediaPort->pfnQueryDeviceLocation(pThis->pDrvMediaPort, ppcszController,
1253 piInstance, piLUN);
1254}
1255
1256/* -=-=-=-=- IBase -=-=-=-=- */
1257
1258/**
1259 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1260 */
1261static DECLCALLBACK(void *) drvdiskintQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1262{
1263 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1264 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1265
1266 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1267 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
1268 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAASYNC, pThis->pDrvMediaAsync ? &pThis->IMediaAsync : NULL);
1269 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAASYNCPORT, &pThis->IMediaAsyncPort);
1270 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAPORT, &pThis->IMediaPort);
1271 return NULL;
1272}
1273
1274
1275/* -=-=-=-=- driver interface -=-=-=-=- */
1276
1277static int drvdiskintTreeDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
1278{
1279 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)pNode;
1280
1281 RTMemFree(pSeg->pbSeg);
1282 RTMemFree(pSeg);
1283 return VINF_SUCCESS;
1284}
1285
1286/**
1287 * @copydoc FNPDMDRVDESTRUCT
1288 */
1289static DECLCALLBACK(void) drvdiskintDestruct(PPDMDRVINS pDrvIns)
1290{
1291 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1292
1293 if (pThis->pTreeSegments)
1294 {
1295 RTAvlrFileOffsetDestroy(pThis->pTreeSegments, drvdiskintTreeDestroy, NULL);
1296 RTMemFree(pThis->pTreeSegments);
1297 }
1298
1299 if (pThis->fTraceRequests)
1300 {
1301 pThis->fRunning = false;
1302 RTSemEventSignal(pThis->SemEvent);
1303 RTSemEventDestroy(pThis->SemEvent);
1304 }
1305
1306 if (pThis->fCheckDoubleCompletion)
1307 {
1308 /* Free all requests */
1309 while (pThis->papIoReq[pThis->iEntry])
1310 {
1311 RTMemFree(pThis->papIoReq[pThis->iEntry]);
1312 pThis->papIoReq[pThis->iEntry] = NULL;
1313 pThis->iEntry = (pThis->iEntry+1) % pThis->cEntries;
1314 }
1315 }
1316
1317 if (pThis->hIoLogger)
1318 VDDbgIoLogDestroy(pThis->hIoLogger);
1319}
1320
1321/**
1322 * Construct a disk integrity driver instance.
1323 *
1324 * @copydoc FNPDMDRVCONSTRUCT
1325 */
1326static DECLCALLBACK(int) drvdiskintConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1327{
1328 int rc = VINF_SUCCESS;
1329 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
1330 LogFlow(("drvdiskintConstruct: iInstance=%d\n", pDrvIns->iInstance));
1331 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1332
1333 /*
1334 * Validate configuration.
1335 */
1336 if (!CFGMR3AreValuesValid(pCfg, "CheckConsistency\0"
1337 "TraceRequests\0"
1338 "CheckIntervalMs\0"
1339 "ExpireIntervalMs\0"
1340 "CheckDoubleCompletions\0"
1341 "HistorySize\0"
1342 "IoLog\0"
1343 "PrepopulateRamDisk\0"
1344 "ReadAfterWrite\0"
1345 "RecordWriteBeforeCompletion\0"))
1346 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1347
1348 rc = CFGMR3QueryBoolDef(pCfg, "CheckConsistency", &pThis->fCheckConsistency, false);
1349 AssertRC(rc);
1350 rc = CFGMR3QueryBoolDef(pCfg, "TraceRequests", &pThis->fTraceRequests, false);
1351 AssertRC(rc);
1352 rc = CFGMR3QueryU32Def(pCfg, "CheckIntervalMs", &pThis->uCheckIntervalMs, 5000); /* 5 seconds */
1353 AssertRC(rc);
1354 rc = CFGMR3QueryU32Def(pCfg, "ExpireIntervalMs", &pThis->uExpireIntervalMs, 20000); /* 20 seconds */
1355 AssertRC(rc);
1356 rc = CFGMR3QueryBoolDef(pCfg, "CheckDoubleCompletions", &pThis->fCheckDoubleCompletion, false);
1357 AssertRC(rc);
1358 rc = CFGMR3QueryU32Def(pCfg, "HistorySize", &pThis->cEntries, 512);
1359 AssertRC(rc);
1360 rc = CFGMR3QueryBoolDef(pCfg, "PrepopulateRamDisk", &pThis->fPrepopulateRamDisk, false);
1361 AssertRC(rc);
1362 rc = CFGMR3QueryBoolDef(pCfg, "ReadAfterWrite", &pThis->fReadAfterWrite, false);
1363 AssertRC(rc);
1364 rc = CFGMR3QueryBoolDef(pCfg, "RecordWriteBeforeCompletion", &pThis->fRecordWriteBeforeCompletion, false);
1365 AssertRC(rc);
1366
1367 char *pszIoLogFilename = NULL;
1368 rc = CFGMR3QueryStringAlloc(pCfg, "IoLog", &pszIoLogFilename);
1369 Assert(RT_SUCCESS(rc) || rc == VERR_CFGM_VALUE_NOT_FOUND);
1370
1371 /*
1372 * Initialize most of the data members.
1373 */
1374 pThis->pDrvIns = pDrvIns;
1375
1376 /* IBase. */
1377 pDrvIns->IBase.pfnQueryInterface = drvdiskintQueryInterface;
1378
1379 /* IMedia */
1380 pThis->IMedia.pfnRead = drvdiskintRead;
1381 pThis->IMedia.pfnWrite = drvdiskintWrite;
1382 pThis->IMedia.pfnFlush = drvdiskintFlush;
1383 pThis->IMedia.pfnGetSize = drvdiskintGetSize;
1384 pThis->IMedia.pfnIsReadOnly = drvdiskintIsReadOnly;
1385 pThis->IMedia.pfnBiosGetPCHSGeometry = drvdiskintBiosGetPCHSGeometry;
1386 pThis->IMedia.pfnBiosSetPCHSGeometry = drvdiskintBiosSetPCHSGeometry;
1387 pThis->IMedia.pfnBiosGetLCHSGeometry = drvdiskintBiosGetLCHSGeometry;
1388 pThis->IMedia.pfnBiosSetLCHSGeometry = drvdiskintBiosSetLCHSGeometry;
1389 pThis->IMedia.pfnGetUuid = drvdiskintGetUuid;
1390 pThis->IMedia.pfnGetSectorSize = drvdiskintGetSectorSize;
1391
1392 /* IMediaAsync */
1393 pThis->IMediaAsync.pfnStartRead = drvdiskintStartRead;
1394 pThis->IMediaAsync.pfnStartWrite = drvdiskintStartWrite;
1395 pThis->IMediaAsync.pfnStartFlush = drvdiskintStartFlush;
1396
1397 /* IMediaAsyncPort. */
1398 pThis->IMediaAsyncPort.pfnTransferCompleteNotify = drvdiskintAsyncTransferCompleteNotify;
1399
1400 /* IMediaPort. */
1401 pThis->IMediaPort.pfnQueryDeviceLocation = drvdiskintQueryDeviceLocation;
1402
1403 /* Query the media port interface above us. */
1404 pThis->pDrvMediaPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAPORT);
1405 if (!pThis->pDrvMediaPort)
1406 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
1407 N_("No media port inrerface above"));
1408
1409 /* Try to attach async media port interface above.*/
1410 pThis->pDrvMediaAsyncPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAASYNCPORT);
1411
1412 /*
1413 * Try attach driver below and query it's media interface.
1414 */
1415 PPDMIBASE pBase;
1416 rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
1417 if (RT_FAILURE(rc))
1418 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1419 N_("Failed to attach driver below us! %Rrc"), rc);
1420
1421 pThis->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
1422 if (!pThis->pDrvMedia)
1423 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
1424 N_("No media or async media interface below"));
1425
1426 pThis->pDrvMediaAsync = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIAASYNC);
1427
1428 if (pThis->pDrvMedia->pfnDiscard)
1429 pThis->IMedia.pfnDiscard = drvdiskintDiscard;
1430 if ( pThis->pDrvMediaAsync
1431 && pThis->pDrvMediaAsync->pfnStartDiscard)
1432 pThis->IMediaAsync.pfnStartDiscard = drvdiskintStartDiscard;
1433
1434 if (pThis->fCheckConsistency)
1435 {
1436 /* Create the AVL tree. */
1437 pThis->pTreeSegments = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
1438 if (!pThis->pTreeSegments)
1439 rc = VERR_NO_MEMORY;
1440 }
1441
1442 if (pThis->fTraceRequests)
1443 {
1444 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apReqActive); i++)
1445 {
1446 pThis->apReqActive[i].pIoReq = NULL;
1447 pThis->apReqActive[i].tsStart = 0;
1448 }
1449
1450 pThis->iNextFreeSlot = 0;
1451
1452 /* Init event semaphore. */
1453 rc = RTSemEventCreate(&pThis->SemEvent);
1454 AssertRC(rc);
1455 pThis->fRunning = true;
1456 rc = RTThreadCreate(&pThis->hThread, drvdiskIntIoReqExpiredCheck, pThis,
1457 0, RTTHREADTYPE_INFREQUENT_POLLER, 0, "DiskIntegrity");
1458 AssertRC(rc);
1459 }
1460
1461 if (pThis->fCheckDoubleCompletion)
1462 {
1463 pThis->iEntry = 0;
1464 pThis->papIoReq = (PDRVDISKAIOREQ *)RTMemAllocZ(pThis->cEntries * sizeof(PDRVDISKAIOREQ));
1465 AssertPtr(pThis->papIoReq);
1466 }
1467
1468 if (pszIoLogFilename)
1469 {
1470 rc = VDDbgIoLogCreate(&pThis->hIoLogger, pszIoLogFilename, VDDBG_IOLOG_LOG_DATA);
1471 MMR3HeapFree(pszIoLogFilename);
1472 }
1473
1474 /* Read in all data before the start if requested. */
1475 if (pThis->fPrepopulateRamDisk)
1476 {
1477 uint64_t cbDisk = 0;
1478
1479 LogRel(("DiskIntegrity: Prepopulating RAM disk, this will take some time...\n"));
1480
1481 cbDisk = pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
1482 if (cbDisk)
1483 {
1484 uint64_t off = 0;
1485 uint8_t abBuffer[_64K];
1486 RTSGSEG Seg;
1487
1488 Seg.pvSeg = abBuffer;
1489
1490 while (cbDisk)
1491 {
1492 size_t cbThisRead = RT_MIN(cbDisk, sizeof(abBuffer));
1493
1494 rc = pThis->pDrvMedia->pfnRead(pThis->pDrvMedia, off, abBuffer, cbThisRead);
1495 if (RT_FAILURE(rc))
1496 break;
1497
1498 if (ASMBitFirstSet(abBuffer, sizeof(abBuffer) * 8) != -1)
1499 {
1500 Seg.cbSeg = cbThisRead;
1501 rc = drvdiskintWriteRecord(pThis, &Seg, 1,
1502 off, cbThisRead);
1503 if (RT_FAILURE(rc))
1504 break;
1505 }
1506
1507 cbDisk -= cbThisRead;
1508 off += cbThisRead;
1509 }
1510
1511 LogRel(("DiskIntegrity: Prepopulating RAM disk finished with %Rrc\n", rc));
1512 }
1513 else
1514 return PDMDRV_SET_ERROR(pDrvIns, VERR_INTERNAL_ERROR,
1515 N_("DiskIntegrity: Error querying the media size below"));
1516 }
1517
1518 return rc;
1519}
1520
1521
1522/**
1523 * Block driver registration record.
1524 */
1525const PDMDRVREG g_DrvDiskIntegrity =
1526{
1527 /* u32Version */
1528 PDM_DRVREG_VERSION,
1529 /* szName */
1530 "DiskIntegrity",
1531 /* szRCMod */
1532 "",
1533 /* szR0Mod */
1534 "",
1535 /* pszDescription */
1536 "Disk integrity driver.",
1537 /* fFlags */
1538 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1539 /* fClass. */
1540 PDM_DRVREG_CLASS_BLOCK,
1541 /* cMaxInstances */
1542 ~0U,
1543 /* cbInstance */
1544 sizeof(DRVDISKINTEGRITY),
1545 /* pfnConstruct */
1546 drvdiskintConstruct,
1547 /* pfnDestruct */
1548 drvdiskintDestruct,
1549 /* pfnRelocate */
1550 NULL,
1551 /* pfnIOCtl */
1552 NULL,
1553 /* pfnPowerOn */
1554 NULL,
1555 /* pfnReset */
1556 NULL,
1557 /* pfnSuspend */
1558 NULL,
1559 /* pfnResume */
1560 NULL,
1561 /* pfnAttach */
1562 NULL,
1563 /* pfnDetach */
1564 NULL,
1565 /* pfnPowerOff */
1566 NULL,
1567 /* pfnSoftReset */
1568 NULL,
1569 /* u32EndVersion */
1570 PDM_DRVREG_VERSION
1571};
1572
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