VirtualBox

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

最後變更 在這個檔案從28902是 28902,由 vboxsync 提交於 15 年 前

DiskIntegrity: Add possibility to check for hanging requests

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 28.6 KB
 
1/* $Id: DrvDiskIntegrity.cpp 28902 2010-04-29 14:54:08Z vboxsync $ */
2/** @file
3 * VBox storage devices: Disk integrity check.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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/pdmdrv.h>
24#include <iprt/assert.h>
25#include <iprt/string.h>
26#include <iprt/uuid.h>
27#include <iprt/avl.h>
28#include <iprt/mem.h>
29#include <iprt/message.h>
30#include <iprt/sg.h>
31#include <iprt/time.h>
32#include <iprt/semaphore.h>
33
34#include "Builtins.h"
35
36
37/*******************************************************************************
38* Structures and Typedefs *
39*******************************************************************************/
40
41/**
42 * async I/O request.
43 */
44typedef struct DRVDISKAIOREQ
45{
46 /** Flag whether this is a read or write request. */
47 bool fRead;
48 /** Start offset. */
49 uint64_t off;
50 /** Transfer size. */
51 size_t cbTransfer;
52 /** Segment array. */
53 PCRTSGSEG paSeg;
54 /** Number of array entries. */
55 unsigned cSeg;
56 /** User argument */
57 void *pvUser;
58 /** Slot in the array. */
59 unsigned iSlot;
60} DRVDISKAIOREQ, *PDRVDISKAIOREQ;
61
62/**
63 * I/O log entry.
64 */
65typedef struct IOLOGENT
66{
67 /** Start offset */
68 uint64_t off;
69 /** Write size */
70 size_t cbWrite;
71 /** Number of references to this entry. */
72 unsigned cRefs;
73} IOLOGENT, *PIOLOGENT;
74
75/**
76 * Disk segment.
77 */
78typedef struct DRVDISKSEGMENT
79{
80 /** AVL core. */
81 AVLRFOFFNODECORE Core;
82 /** Size of the segment */
83 size_t cbSeg;
84 /** Data for this segment */
85 uint8_t *pbSeg;
86 /** Numbner of entries in the I/O array. */
87 unsigned cIoLogEntries;
88 /** Array of I/O log references. */
89 PIOLOGENT apIoLog[1];
90} DRVDISKSEGMENT, *PDRVDISKSEGMENT;
91
92/**
93 * Active requests list entry.
94 */
95typedef struct DRVDISKAIOREQACTIVE
96{
97 /** Pointer to the request. */
98 volatile PDRVDISKAIOREQ pIoReq;
99 /** Start timestamp. */
100 uint64_t tsStart;
101} DRVDISKAIOREQACTIVE, *PDRVDISKAIOREQACTIVE;
102
103/**
104 * Disk integrity driver instance data.
105 *
106 * @implements PDMIMEDIA
107 */
108typedef struct DRVDISKINTEGRITY
109{
110 /** Pointer driver instance. */
111 PPDMDRVINS pDrvIns;
112 /** Pointer to the media driver below us.
113 * This is NULL if the media is not mounted. */
114 PPDMIMEDIA pDrvMedia;
115 /** Our media interface */
116 PDMIMEDIA IMedia;
117
118 /** Pointer to the media async driver below us.
119 * This is NULL if the media is not mounted. */
120 PPDMIMEDIAASYNC pDrvMediaAsync;
121 /** Our media async interface */
122 PDMIMEDIAASYNC IMediaAsync;
123
124 /** The async media port interface above. */
125 PPDMIMEDIAASYNCPORT pDrvMediaAsyncPort;
126 /** Our media async port interface */
127 PDMIMEDIAASYNCPORT IMediaAsyncPort;
128
129 /** Flag whether consistency checks are enabled. */
130 bool fCheckConsistency;
131 /** AVL tree containing the disk blocks to check. */
132 PAVLRFOFFTREE pTreeSegments;
133
134 /** Flag whether async request tracing is enabled. */
135 bool fTraceRequests;
136 /** Interval the thread should check for expired requests (milliseconds). */
137 uint32_t uCheckIntervalMs;
138 /** Expire timeout for a request (milliseconds). */
139 uint32_t uExpireIntervalMs;
140 /** Thread which checks for lost requests. */
141 RTTHREAD hThread;
142 /** Event semaphore */
143 RTSEMEVENT SemEvent;
144 /** Flag whether the thread should run. */
145 bool fRunning;
146 /** Array containing active requests. */
147 DRVDISKAIOREQACTIVE apReqActive[128];
148 /** Next free slot in the array */
149 volatile unsigned iNextFreeSlot;
150} DRVDISKINTEGRITY, *PDRVDISKINTEGRITY;
151
152
153/**
154 * Allocate a new I/O request.
155 *
156 * @returns New I/O request.
157 * @param fRead Flag whether this is a read or a write.
158 * @param off Start offset.
159 * @param paSeg Segment array.
160 * @param cSeg Number of segments.
161 * @param cbTransfer Number of bytes to transfer.
162 * @param pvUser User argument.
163 */
164static PDRVDISKAIOREQ drvdiskintIoReqAlloc(bool fRead, uint64_t off, PCRTSGSEG paSeg,
165 unsigned cSeg, size_t cbTransfer, void *pvUser)
166{
167 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)RTMemAlloc(sizeof(DRVDISKAIOREQ));
168
169 if (RT_LIKELY(pIoReq))
170 {
171 pIoReq->fRead = fRead;
172 pIoReq->off = off;
173 pIoReq->cbTransfer = cbTransfer;
174 pIoReq->paSeg = paSeg;
175 pIoReq->cSeg = cSeg;
176 pIoReq->pvUser = pvUser;
177 }
178
179 return pIoReq;
180}
181
182/**
183 * Record a successful write to the virtual disk.
184 *
185 * @returns VBox status code.
186 * @param pThis Disk integrity driver instance data.
187 * @param paSeg Segment array of the write to record.
188 * @param cSeg Number of segments.
189 * @param off Start offset.
190 * @param cbWrite Number of bytes to record.
191 */
192static int drvdiskintWriteRecord(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
193 uint64_t off, size_t cbWrite)
194{
195 int rc = VINF_SUCCESS;
196
197 LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbWrite=%u\n",
198 pThis, paSeg, cSeg, off, cbWrite));
199
200 /* Update the segments */
201 size_t cbLeft = cbWrite;
202 RTFOFF offCurr = (RTFOFF)off;
203 RTSGBUF SgBuf;
204 PIOLOGENT pIoLogEnt = (PIOLOGENT)RTMemAllocZ(sizeof(IOLOGENT));
205 if (!pIoLogEnt)
206 return VERR_NO_MEMORY;
207
208 pIoLogEnt->off = off;
209 pIoLogEnt->cbWrite = cbWrite;
210 pIoLogEnt->cRefs = 0;
211
212 RTSgBufInit(&SgBuf, paSeg, cSeg);
213
214 while (cbLeft)
215 {
216 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
217 size_t cbRange = 0;
218 bool fSet = false;
219 unsigned offSeg = 0;
220
221 if (!pSeg)
222 {
223 /* Get next segment */
224 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
225 if ( !pSeg
226 || offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
227 cbRange = cbLeft;
228 else
229 cbRange = pSeg->Core.Key - offCurr;
230
231 Assert(cbRange % 512 == 0);
232
233 /* Create new segment */
234 pSeg = (PDRVDISKSEGMENT)RTMemAllocZ(RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbRange / 512]));
235 if (pSeg)
236 {
237 pSeg->Core.Key = offCurr;
238 pSeg->Core.KeyLast = offCurr + (RTFOFF)cbRange - 1;
239 pSeg->cbSeg = cbRange;
240 pSeg->pbSeg = (uint8_t *)RTMemAllocZ(cbRange);
241 pSeg->cIoLogEntries = cbRange / 512;
242 if (!pSeg->pbSeg)
243 RTMemFree(pSeg);
244 else
245 {
246 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
247 AssertMsg(fInserted, ("Bug!\n"));
248 fSet = true;
249 }
250 }
251 }
252 else
253 {
254 fSet = true;
255 offSeg = offCurr - pSeg->Core.Key;
256 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
257 }
258
259 if (fSet)
260 {
261 AssertPtr(pSeg);
262 size_t cbCopied = RTSgBufCopyToBuf(&SgBuf, pSeg->pbSeg + offSeg, cbRange);
263 Assert(cbCopied == cbRange);
264
265 /* Update the I/O log pointers */
266 Assert(offSeg % 512 == 0);
267 Assert(cbRange % 512 == 0);
268 while (offSeg < cbRange)
269 {
270 uint32_t uSector = offSeg / 512;
271 PIOLOGENT pIoLogOld = NULL;
272
273 AssertMsg(uSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
274
275 pIoLogOld = pSeg->apIoLog[uSector];
276 if (pIoLogOld)
277 {
278 pIoLogOld->cRefs--;
279 if (!pIoLogOld->cRefs)
280 RTMemFree(pIoLogOld);
281 }
282
283 pSeg->apIoLog[uSector] = pIoLogEnt;
284 pIoLogEnt->cRefs++;
285
286 offSeg += 512;
287 }
288 }
289 else
290 RTSgBufAdvance(&SgBuf, cbRange);
291
292 offCurr += cbRange;
293 cbLeft -= cbRange;
294 }
295
296 return rc;
297}
298
299/**
300 * Verifies a read request.
301 *
302 * @returns VBox status code.
303 * @param pThis Disk integrity driver instance data.
304 * @param paSeg Segment array of the containing the data buffers to verify.
305 * @param cSeg Number of segments.
306 * @param off Start offset.
307 * @param cbWrite Number of bytes to verify.
308 */
309static int drvdiskintReadVerify(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
310 uint64_t off, size_t cbRead)
311{
312 int rc = VINF_SUCCESS;
313
314 LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbRead=%u\n",
315 pThis, paSeg, cSeg, off, cbRead));
316
317 Assert(off % 512 == 0);
318 Assert(cbRead % 512 == 0);
319
320 /* Compare read data */
321 size_t cbLeft = cbRead;
322 RTFOFF offCurr = (RTFOFF)off;
323 RTSGBUF SgBuf;
324
325 RTSgBufInit(&SgBuf, paSeg, cSeg);
326
327 while (cbLeft)
328 {
329 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
330 size_t cbRange = 0;
331 bool fCmp = false;
332 unsigned offSeg = 0;
333
334 if (!pSeg)
335 {
336 /* Get next segment */
337 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
338 if (!pSeg)
339 {
340 /* No data in the tree for this read. Assume everything is ok. */
341 cbRange = cbLeft;
342 }
343 else if (offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
344 cbRange = cbLeft;
345 else
346 cbRange = pSeg->Core.Key - offCurr;
347 }
348 else
349 {
350 fCmp = true;
351 offSeg = offCurr - pSeg->Core.Key;
352 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
353 }
354
355 if (fCmp)
356 {
357 RTSGSEG Seg;
358 RTSGBUF SgBufCmp;
359 size_t cbOff = 0;
360
361 Seg.cbSeg = cbRange;
362 Seg.pvSeg = pSeg->pbSeg + offSeg;
363
364 RTSgBufInit(&SgBufCmp, &Seg, 1);
365 if (RTSgBufCmpEx(&SgBuf, &SgBufCmp, cbRange, &cbOff, true))
366 {
367 /* Corrupted disk, print I/O log entry of the last write which accessed this range. */
368 uint32_t cSector = (offSeg + cbOff) / 512;
369 AssertMsg(cSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
370
371 RTMsgError("Corrupted disk at offset %llu (%u bytes in the current read buffer)!\n",
372 offCurr + cbOff, cbOff);
373 RTMsgError("Last write to this sector started at offset %llu with %u bytes (%u references to this log entry)\n",
374 pSeg->apIoLog[cSector]->off,
375 pSeg->apIoLog[cSector]->cbWrite,
376 pSeg->apIoLog[cSector]->cRefs);
377 RTAssertDebugBreak();
378 }
379 }
380 else
381 RTSgBufAdvance(&SgBuf, cbRange);
382
383 offCurr += cbRange;
384 cbLeft -= cbRange;
385 }
386
387 return rc;
388}
389
390/**
391 * Adds a request to the active list.
392 *
393 * @returns nothing.
394 * @param pThis The driver instance data.
395 * @param pIoReq The request to add.
396 */
397static void drvdiskintIoReqAdd(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
398{
399 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[pThis->iNextFreeSlot];
400
401 Assert(!pReqActive->pIoReq);
402 pReqActive->tsStart = RTTimeSystemMilliTS();
403 pReqActive->pIoReq = pIoReq;
404 pIoReq->iSlot = pThis->iNextFreeSlot;
405
406 /* Search for the next one. */
407 pThis->iNextFreeSlot++;
408 while (pThis->apReqActive[pThis->iNextFreeSlot].pIoReq)
409 pThis->iNextFreeSlot = pThis->iNextFreeSlot++ % RT_ELEMENTS(pThis->apReqActive);
410}
411
412/**
413 * Removes a request from the active list.
414 *
415 * @returns nothing.
416 * @param pThis The driver instance data.
417 * @param pIoReq The request to remove.
418 */
419static void drvdiskintIoReqRemove(PDRVDISKINTEGRITY pThis, PDRVDISKAIOREQ pIoReq)
420{
421 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[pIoReq->iSlot];
422
423 Assert(pReqActive->pIoReq == pIoReq);
424
425 ASMAtomicXchgPtr((void * volatile *)&pReqActive->pIoReq, NULL);
426}
427
428/**
429 * Thread checking for expired requests.
430 *
431 * @returns IPRT status code.
432 * @param pThread Thread handle.
433 * @param pvUser Opaque user data.
434 */
435static int drvdiskIntIoReqExpiredCheck(RTTHREAD pThread, void *pvUser)
436{
437 PDRVDISKINTEGRITY pThis = (PDRVDISKINTEGRITY)pvUser;
438
439 while (pThis->fRunning)
440 {
441 int rc = RTSemEventWait(pThis->SemEvent, pThis->uCheckIntervalMs);
442
443 if (!pThis->fRunning)
444 break;
445
446 Assert(rc == VERR_TIMEOUT);
447
448 /* Get current timestamp for comparison. */
449 uint64_t tsCurr = RTTimeSystemMilliTS();
450
451 /* Go through the array and check for expired requests. */
452 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apReqActive); i++)
453 {
454 PDRVDISKAIOREQACTIVE pReqActive = &pThis->apReqActive[i];
455 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)ASMAtomicReadPtr((void * volatile *)&pReqActive->pIoReq);
456
457 if ( pIoReq
458 && (tsCurr - pReqActive->tsStart) >= pThis->uExpireIntervalMs)
459 {
460 RTMsgError("Request %#p expired (active for %llu ms already)\n",
461 pIoReq, tsCurr - pReqActive->tsStart);
462 RTAssertDebugBreak();
463 }
464 }
465 }
466
467 return VINF_SUCCESS;
468}
469
470/* -=-=-=-=- IMedia -=-=-=-=- */
471
472/** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIA. */
473#define PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMedia)) )
474/** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIAASYNC. */
475#define PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaAsync)) )
476
477/*******************************************************************************
478* Media interface methods *
479*******************************************************************************/
480
481/** @copydoc PDMIMEDIA::pfnRead */
482static DECLCALLBACK(int) drvdiskintRead(PPDMIMEDIA pInterface,
483 uint64_t off, void *pvBuf, size_t cbRead)
484{
485 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
486 int rc = pThis->pDrvMedia->pfnRead(pThis->pDrvMedia, off, pvBuf, cbRead);
487 if (RT_FAILURE(rc))
488 return rc;
489
490 if (pThis->fCheckConsistency)
491 {
492 /* Verify the read. */
493 RTSGSEG Seg;
494 Seg.cbSeg = cbRead;
495 Seg.pvSeg = pvBuf;
496 rc = drvdiskintReadVerify(pThis, &Seg, 1, off, cbRead);
497 }
498
499 return rc;
500}
501
502/** @copydoc PDMIMEDIA::pfnWrite */
503static DECLCALLBACK(int) drvdiskintWrite(PPDMIMEDIA pInterface,
504 uint64_t off, const void *pvBuf,
505 size_t cbWrite)
506{
507 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
508 int rc = pThis->pDrvMedia->pfnWrite(pThis->pDrvMedia, off, pvBuf, cbWrite);
509 if (RT_FAILURE(rc))
510 return rc;
511
512 if (pThis->fCheckConsistency)
513 {
514 /* Record the write. */
515 RTSGSEG Seg;
516 Seg.cbSeg = cbWrite;
517 Seg.pvSeg = (void *)pvBuf;
518 rc = drvdiskintWriteRecord(pThis, &Seg, 1, off, cbWrite);
519 }
520
521 return rc;
522}
523
524static DECLCALLBACK(int) drvdiskintStartRead(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
525 PCRTSGSEG paSeg, unsigned cSeg,
526 size_t cbRead, void *pvUser)
527{
528 LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbRead=%d\n pvUser=%#p", __FUNCTION__,
529 uOffset, paSeg, cSeg, cbRead, pvUser));
530 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
531 PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(true, uOffset, paSeg, cSeg, cbRead, pvUser);
532 AssertPtr(pIoReq);
533
534 if (pThis->fTraceRequests)
535 drvdiskintIoReqAdd(pThis, pIoReq);
536
537 int rc = pThis->pDrvMediaAsync->pfnStartRead(pThis->pDrvMediaAsync, uOffset, paSeg, cSeg,
538 cbRead, pIoReq);
539 if (rc == VINF_VD_ASYNC_IO_FINISHED)
540 {
541 /* Verify the read now. */
542 if (pThis->fCheckConsistency)
543 {
544 int rc2 = drvdiskintReadVerify(pThis, paSeg, cSeg, uOffset, cbRead);
545 AssertRC(rc2);
546 }
547
548 if (pThis->fTraceRequests)
549 drvdiskintIoReqRemove(pThis, pIoReq);
550 RTMemFree(pIoReq);
551 }
552 else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
553 RTMemFree(pIoReq);
554
555 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
556 return rc;
557}
558
559static DECLCALLBACK(int) drvdiskintStartWrite(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
560 PCRTSGSEG paSeg, unsigned cSeg,
561 size_t cbWrite, void *pvUser)
562{
563 LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbWrite=%d\n pvUser=%#p", __FUNCTION__,
564 uOffset, paSeg, cSeg, cbWrite, pvUser));
565 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
566 PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(false, uOffset, paSeg, cSeg, cbWrite, pvUser);
567 AssertPtr(pIoReq);
568
569 if (pThis->fTraceRequests)
570 drvdiskintIoReqAdd(pThis, pIoReq);
571
572 int rc = pThis->pDrvMediaAsync->pfnStartWrite(pThis->pDrvMediaAsync, uOffset, paSeg, cSeg,
573 cbWrite, pIoReq);
574 if (rc == VINF_VD_ASYNC_IO_FINISHED)
575 {
576 /* Verify the read now. */
577 if (pThis->fCheckConsistency)
578 {
579 int rc2 = drvdiskintWriteRecord(pThis, paSeg, cSeg, uOffset, cbWrite);
580 AssertRC(rc2);
581 }
582
583 if (pThis->fTraceRequests)
584 drvdiskintIoReqRemove(pThis, pIoReq);
585
586 RTMemFree(pIoReq);
587 }
588 else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
589 RTMemFree(pIoReq);
590
591 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
592 return rc;
593}
594
595/** @copydoc PDMIMEDIA::pfnFlush */
596static DECLCALLBACK(int) drvdiskintFlush(PPDMIMEDIA pInterface)
597{
598 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
599 return pThis->pDrvMedia->pfnFlush(pThis->pDrvMedia);
600}
601
602/** @copydoc PDMIMEDIA::pfnGetSize */
603static DECLCALLBACK(uint64_t) drvdiskintGetSize(PPDMIMEDIA pInterface)
604{
605 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
606 return pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
607}
608
609/** @copydoc PDMIMEDIA::pfnIsReadOnly */
610static DECLCALLBACK(bool) drvdiskintIsReadOnly(PPDMIMEDIA pInterface)
611{
612 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
613 return pThis->pDrvMedia->pfnIsReadOnly(pThis->pDrvMedia);
614}
615
616/** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
617static DECLCALLBACK(int) drvdiskintBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
618 PPDMMEDIAGEOMETRY pPCHSGeometry)
619{
620 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
621 return pThis->pDrvMedia->pfnBiosGetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
622}
623
624/** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
625static DECLCALLBACK(int) drvdiskintBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
626 PCPDMMEDIAGEOMETRY pPCHSGeometry)
627{
628 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
629 return pThis->pDrvMedia->pfnBiosSetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
630}
631
632/** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
633static DECLCALLBACK(int) drvdiskintBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
634 PPDMMEDIAGEOMETRY pLCHSGeometry)
635{
636 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
637 return pThis->pDrvMedia->pfnBiosGetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
638}
639
640/** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
641static DECLCALLBACK(int) drvdiskintBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
642 PCPDMMEDIAGEOMETRY pLCHSGeometry)
643{
644 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
645 return pThis->pDrvMedia->pfnBiosSetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
646}
647
648/** @copydoc PDMIMEDIA::pfnGetUuid */
649static DECLCALLBACK(int) drvdiskintGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
650{
651 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
652 return pThis->pDrvMedia->pfnGetUuid(pThis->pDrvMedia, pUuid);
653}
654
655/* -=-=-=-=- IMediaAsyncPort -=-=-=-=- */
656
657/** Makes a PDRVBLOCKASYNC out of a PPDMIMEDIAASYNCPORT. */
658#define PDMIMEDIAASYNCPORT_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaAsyncPort))) )
659
660static DECLCALLBACK(int) drvdiskintAsyncTransferCompleteNotify(PPDMIMEDIAASYNCPORT pInterface, void *pvUser, int rcReq)
661{
662 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNCPORT_2_DRVDISKINTEGRITY(pInterface);
663 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvUser;
664 int rc = VINF_SUCCESS;
665
666 LogFlowFunc(("pIoReq=%#p\n", pIoReq));
667
668 /* Remove from the active list. */
669 if (pThis->fTraceRequests)
670 drvdiskintIoReqRemove(pThis, pIoReq);
671
672 if (RT_SUCCESS(rcReq) && pThis->fCheckConsistency)
673 {
674 if (pIoReq->fRead)
675 rc = drvdiskintReadVerify(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
676 else
677 rc = drvdiskintWriteRecord(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
678
679 AssertRC(rc);
680 }
681
682 rc = pThis->pDrvMediaAsyncPort->pfnTransferCompleteNotify(pThis->pDrvMediaAsyncPort, pIoReq->pvUser, rcReq);
683 RTMemFree(pIoReq);
684
685 return rc;
686}
687
688/* -=-=-=-=- IBase -=-=-=-=- */
689
690/**
691 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
692 */
693static DECLCALLBACK(void *) drvdiskintQueryInterface(PPDMIBASE pInterface, const char *pszIID)
694{
695 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
696 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
697
698 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
699 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
700 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAASYNC, pThis->pDrvMediaAsync ? &pThis->IMediaAsync : NULL);
701 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAASYNCPORT, &pThis->IMediaAsyncPort);
702 return NULL;
703}
704
705
706/* -=-=-=-=- driver interface -=-=-=-=- */
707
708static int drvdiskintTreeDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
709{
710 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)pNode;
711
712 RTMemFree(pSeg->pbSeg);
713 RTMemFree(pSeg);
714 return VINF_SUCCESS;
715}
716
717/**
718 * @copydoc FNPDMDRVDESTRUCT
719 */
720static DECLCALLBACK(void) drvdiskintDestruct(PPDMDRVINS pDrvIns)
721{
722 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
723
724 if (pThis->pTreeSegments)
725 {
726 RTAvlrFileOffsetDestroy(pThis->pTreeSegments, drvdiskintTreeDestroy, NULL);
727 RTMemFree(pThis->pTreeSegments);
728 }
729
730 if (pThis->fTraceRequests)
731 {
732 pThis->fRunning = false;
733 RTSemEventSignal(pThis->SemEvent);
734 RTSemEventDestroy(pThis->SemEvent);
735 }
736}
737
738/**
739 * Construct a disk integrity driver instance.
740 *
741 * @copydoc FNPDMDRVCONSTRUCT
742 */
743static DECLCALLBACK(int) drvdiskintConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
744{
745 int rc = VINF_SUCCESS;
746 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
747 LogFlow(("drvdiskintConstruct: iInstance=%d\n", pDrvIns->iInstance));
748 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
749
750 /*
751 * Validate configuration.
752 */
753 if (!CFGMR3AreValuesValid(pCfg, "CheckConsistency\0"
754 "TraceRequests\0"
755 "CheckIntervalMs\0"
756 "ExpireIntervalMs\0"))
757 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
758
759 rc = CFGMR3QueryBoolDef(pCfg, "CheckConsistency", &pThis->fCheckConsistency, false);
760 AssertRC(rc);
761 rc = CFGMR3QueryBoolDef(pCfg, "TraceRequests", &pThis->fTraceRequests, false);
762 AssertRC(rc);
763 rc = CFGMR3QueryU32Def(pCfg, "CheckIntervalMs", &pThis->uCheckIntervalMs, 5000); /* 5 seconds */
764 AssertRC(rc);
765 rc = CFGMR3QueryU32Def(pCfg, "ExpireIntervalMs", &pThis->uExpireIntervalMs, 20000); /* 20 seconds */
766 AssertRC(rc);
767
768 /*
769 * Initialize most of the data members.
770 */
771 pThis->pDrvIns = pDrvIns;
772
773 /* IBase. */
774 pDrvIns->IBase.pfnQueryInterface = drvdiskintQueryInterface;
775
776 /* IMedia */
777 pThis->IMedia.pfnRead = drvdiskintRead;
778 pThis->IMedia.pfnWrite = drvdiskintWrite;
779 pThis->IMedia.pfnFlush = drvdiskintFlush;
780 pThis->IMedia.pfnGetSize = drvdiskintGetSize;
781 pThis->IMedia.pfnIsReadOnly = drvdiskintIsReadOnly;
782 pThis->IMedia.pfnBiosGetPCHSGeometry = drvdiskintBiosGetPCHSGeometry;
783 pThis->IMedia.pfnBiosSetPCHSGeometry = drvdiskintBiosSetPCHSGeometry;
784 pThis->IMedia.pfnBiosGetLCHSGeometry = drvdiskintBiosGetLCHSGeometry;
785 pThis->IMedia.pfnBiosSetLCHSGeometry = drvdiskintBiosSetLCHSGeometry;
786 pThis->IMedia.pfnGetUuid = drvdiskintGetUuid;
787
788 /* IMediaAsync */
789 pThis->IMediaAsync.pfnStartRead = drvdiskintStartRead;
790 pThis->IMediaAsync.pfnStartWrite = drvdiskintStartWrite;
791
792 /* IMediaAsyncPort. */
793 pThis->IMediaAsyncPort.pfnTransferCompleteNotify = drvdiskintAsyncTransferCompleteNotify;
794
795 /*
796 * Try attach driver below and query it's media interface.
797 */
798 PPDMIBASE pBase;
799 rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
800 if (RT_FAILURE(rc))
801 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
802 N_("Failed to attach driver below us! %Rrc"), rc);
803
804 pThis->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
805 if (!pThis->pDrvMedia)
806 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
807 N_("No media or async media interface below"));
808
809 pThis->pDrvMediaAsync = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIAASYNC);
810
811 /* Try to attach async media port interface above.*/
812 pThis->pDrvMediaAsyncPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAASYNCPORT);
813
814 if (pThis->fCheckConsistency)
815 {
816 /* Create the AVL tree. */
817 pThis->pTreeSegments = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
818 if (!pThis->pTreeSegments)
819 rc = VERR_NO_MEMORY;
820 }
821
822 if (pThis->fTraceRequests)
823 {
824 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apReqActive); i++)
825 {
826 pThis->apReqActive[i].pIoReq = NULL;
827 pThis->apReqActive[i].tsStart = 0;
828 }
829
830 pThis->iNextFreeSlot = 0;
831
832 /* Init event semaphore. */
833 rc = RTSemEventCreate(&pThis->SemEvent);
834 AssertRC(rc);
835 pThis->fRunning = true;
836 rc = RTThreadCreate(&pThis->hThread, drvdiskIntIoReqExpiredCheck, pThis,
837 0, RTTHREADTYPE_INFREQUENT_POLLER, 0, "DiskIntegrity");
838 AssertRC(rc);
839 }
840
841 return rc;
842}
843
844
845/**
846 * Block driver registration record.
847 */
848const PDMDRVREG g_DrvDiskIntegrity =
849{
850 /* u32Version */
851 PDM_DRVREG_VERSION,
852 /* szName */
853 "DiskIntegrity",
854 /* szRCMod */
855 "",
856 /* szR0Mod */
857 "",
858 /* pszDescription */
859 "Disk integrity driver.",
860 /* fFlags */
861 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
862 /* fClass. */
863 PDM_DRVREG_CLASS_BLOCK,
864 /* cMaxInstances */
865 ~0,
866 /* cbInstance */
867 sizeof(DRVDISKINTEGRITY),
868 /* pfnConstruct */
869 drvdiskintConstruct,
870 /* pfnDestruct */
871 drvdiskintDestruct,
872 /* pfnRelocate */
873 NULL,
874 /* pfnIOCtl */
875 NULL,
876 /* pfnPowerOn */
877 NULL,
878 /* pfnReset */
879 NULL,
880 /* pfnSuspend */
881 NULL,
882 /* pfnResume */
883 NULL,
884 /* pfnAttach */
885 NULL,
886 /* pfnDetach */
887 NULL,
888 /* pfnPowerOff */
889 NULL,
890 /* pfnSoftReset */
891 NULL,
892 /* u32EndVersion */
893 PDM_DRVREG_VERSION
894};
895
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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