VirtualBox

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

Last change on this file since 28144 was 28144, checked in by vboxsync, 15 years ago

DrvDiskIntegrity: Bug fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.0 KB
Line 
1/* $Id: DrvDiskIntegrity.cpp 28144 2010-04-09 13:53:43Z vboxsync $ */
2/** @file
3 * VBox storage devices: Disk integrity check.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_DRV_DISK_INTEGRITY
27#include <VBox/pdmdrv.h>
28#include <iprt/assert.h>
29#include <iprt/string.h>
30#include <iprt/uuid.h>
31#include <iprt/avl.h>
32#include <iprt/mem.h>
33#include <iprt/message.h>
34#include <iprt/sg.h>
35
36#include "Builtins.h"
37
38
39/*******************************************************************************
40* Structures and Typedefs *
41*******************************************************************************/
42
43/**
44 * async I/O request.
45 */
46typedef struct DRVDISKAIOREQ
47{
48 /** Flag whether this is a read or write request. */
49 bool fRead;
50 /** Start offset. */
51 uint64_t off;
52 /** Transfer size. */
53 size_t cbTransfer;
54 /** Segment array. */
55 PCRTSGSEG paSeg;
56 /** Number of array entries. */
57 unsigned cSeg;
58 /** User argument */
59 void *pvUser;
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 * Disk integrity driver instance data.
94 *
95 * @implements PDMIMEDIA
96 */
97typedef struct DRVDISKINTEGRITY
98{
99 /** Pointer driver instance. */
100 PPDMDRVINS pDrvIns;
101 /** Pointer to the media driver below us.
102 * This is NULL if the media is not mounted. */
103 PPDMIMEDIA pDrvMedia;
104 /** Our media interface */
105 PDMIMEDIA IMedia;
106
107 /** Pointer to the media async driver below us.
108 * This is NULL if the media is not mounted. */
109 PPDMIMEDIAASYNC pDrvMediaAsync;
110 /** Our media async interface */
111 PDMIMEDIAASYNC IMediaAsync;
112
113 /** The async media port interface above. */
114 PPDMIMEDIAASYNCPORT pDrvMediaAsyncPort;
115 /** Our media async port interface */
116 PDMIMEDIAASYNCPORT IMediaAsyncPort;
117
118 /** AVL tree containing the disk blocks to check. */
119 PAVLRFOFFTREE pTreeSegments;
120} DRVDISKINTEGRITY, *PDRVDISKINTEGRITY;
121
122
123/**
124 * Allocate a new I/O request.
125 *
126 * @returns New I/O request.
127 * @param fRead Flag whether this is a read or a write.
128 * @param off Start offset.
129 * @param paSeg Segment array.
130 * @param cSeg Number of segments.
131 * @param cbTransfer Number of bytes to transfer.
132 * @param pvUser User argument.
133 */
134static PDRVDISKAIOREQ drvdiskintIoReqAlloc(bool fRead, uint64_t off, PCRTSGSEG paSeg,
135 unsigned cSeg, size_t cbTransfer, void *pvUser)
136{
137 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)RTMemAlloc(sizeof(DRVDISKAIOREQ));
138
139 if (RT_LIKELY(pIoReq))
140 {
141 pIoReq->fRead = fRead;
142 pIoReq->off = off;
143 pIoReq->cbTransfer = cbTransfer;
144 pIoReq->paSeg = paSeg;
145 pIoReq->cSeg = cSeg;
146 pIoReq->pvUser = pvUser;
147 }
148
149 return pIoReq;
150}
151
152/**
153 * Record a successful write to the virtual disk.
154 *
155 * @returns VBox status code.
156 * @param pThis Disk integrity driver instance data.
157 * @param paSeg Segment array of the write to record.
158 * @param cSeg Number of segments.
159 * @param off Start offset.
160 * @param cbWrite Number of bytes to record.
161 */
162static int drvdiskintWriteRecord(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
163 uint64_t off, size_t cbWrite)
164{
165 int rc = VINF_SUCCESS;
166
167 LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbWrite=%u\n",
168 pThis, paSeg, cSeg, off, cbWrite));
169
170 /* Update the segments */
171 size_t cbLeft = cbWrite;
172 RTFOFF offCurr = (RTFOFF)off;
173 RTSGBUF SgBuf;
174 PIOLOGENT pIoLogEnt = (PIOLOGENT)RTMemAllocZ(sizeof(IOLOGENT));
175 if (!pIoLogEnt)
176 return VERR_NO_MEMORY;
177
178 pIoLogEnt->off = off;
179 pIoLogEnt->cbWrite = cbWrite;
180 pIoLogEnt->cRefs = 0;
181
182 RTSgBufInit(&SgBuf, paSeg, cSeg);
183
184 while (cbLeft)
185 {
186 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
187 size_t cbRange = 0;
188 bool fSet = false;
189 unsigned offSeg = 0;
190
191 if (!pSeg)
192 {
193 /* Get next segment */
194 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
195 if ( !pSeg
196 || offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
197 cbRange = cbLeft;
198 else
199 cbRange = pSeg->Core.Key - offCurr;
200
201 Assert(cbRange % 512 == 0);
202
203 /* Create new segment */
204 pSeg = (PDRVDISKSEGMENT)RTMemAllocZ(RT_OFFSETOF(DRVDISKSEGMENT, apIoLog[cbRange / 512]));
205 if (pSeg)
206 {
207 pSeg->Core.Key = offCurr;
208 pSeg->Core.KeyLast = offCurr + (RTFOFF)cbRange - 1;
209 pSeg->cbSeg = cbRange;
210 pSeg->pbSeg = (uint8_t *)RTMemAllocZ(cbRange);
211 pSeg->cIoLogEntries = cbRange / 512;
212 if (!pSeg->pbSeg)
213 RTMemFree(pSeg);
214 else
215 {
216 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
217 AssertMsg(fInserted, ("Bug!\n"));
218 fSet = true;
219 }
220 }
221 }
222 else
223 {
224 fSet = true;
225 offSeg = offCurr - pSeg->Core.Key;
226 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
227 }
228
229 if (fSet)
230 {
231 AssertPtr(pSeg);
232 size_t cbCopied = RTSgBufCopyToBuf(&SgBuf, pSeg->pbSeg + offSeg, cbRange);
233 Assert(cbCopied == cbRange);
234
235 /* Update the I/O log pointers */
236 Assert(offSeg % 512 == 0);
237 Assert(cbRange % 512 == 0);
238 while (offSeg < cbRange)
239 {
240 uint32_t uSector = offSeg / 512;
241 PIOLOGENT pIoLogOld = NULL;
242
243 AssertMsg(uSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
244
245 pIoLogOld = pSeg->apIoLog[uSector];
246 if (pIoLogOld)
247 {
248 pIoLogOld->cRefs--;
249 if (!pIoLogOld->cRefs)
250 RTMemFree(pIoLogOld);
251 }
252
253 pSeg->apIoLog[uSector] = pIoLogEnt;
254 pIoLogEnt->cRefs++;
255
256 offSeg += 512;
257 }
258 }
259 else
260 RTSgBufAdvance(&SgBuf, cbRange);
261
262 offCurr += cbRange;
263 cbLeft -= cbRange;
264 }
265
266 return rc;
267}
268
269/**
270 * Verifies a read request.
271 *
272 * @returns VBox status code.
273 * @param pThis Disk integrity driver instance data.
274 * @param paSeg Segment array of the containing the data buffers to verify.
275 * @param cSeg Number of segments.
276 * @param off Start offset.
277 * @param cbWrite Number of bytes to verify.
278 */
279static int drvdiskintReadVerify(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
280 uint64_t off, size_t cbRead)
281{
282 int rc = VINF_SUCCESS;
283
284 LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbRead=%u\n",
285 pThis, paSeg, cSeg, off, cbRead));
286
287 Assert(off % 512 == 0);
288 Assert(cbRead % 512 == 0);
289
290 /* Compare read data */
291 size_t cbLeft = cbRead;
292 RTFOFF offCurr = (RTFOFF)off;
293 RTSGBUF SgBuf;
294
295 RTSgBufInit(&SgBuf, paSeg, cSeg);
296
297 while (cbLeft)
298 {
299 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
300 size_t cbRange = 0;
301 bool fCmp = false;
302 unsigned offSeg = 0;
303
304 if (!pSeg)
305 {
306 /* Get next segment */
307 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
308 if (!pSeg)
309 {
310 /* No data in the tree for this read. Assume everything is ok. */
311 cbRange = cbLeft;
312 }
313 else if (offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
314 cbRange = cbLeft;
315 else
316 cbRange = pSeg->Core.Key - offCurr;
317 }
318 else
319 {
320 fCmp = true;
321 offSeg = offCurr - pSeg->Core.Key;
322 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
323 }
324
325 if (fCmp)
326 {
327 RTSGSEG Seg;
328 RTSGBUF SgBufCmp;
329 size_t cbOff = 0;
330
331 Seg.cbSeg = cbRange;
332 Seg.pvSeg = pSeg->pbSeg + offSeg;
333
334 RTSgBufInit(&SgBufCmp, &Seg, 1);
335 if (RTSgBufCmpEx(&SgBuf, &SgBufCmp, cbRange, &cbOff, true))
336 {
337 /* Corrupted disk, print I/O log entry of the last write which accessed this range. */
338 uint32_t cSector = (offSeg + cbOff) / 512;
339 AssertMsg(cSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
340
341 RTMsgError("Corrupted disk at offset %llu (%u bytes in the current read buffer)!\n",
342 offCurr + cbOff, cbOff);
343 RTMsgError("Last write to this sector started at offset %llu with %u bytes (%u references to this log entry)\n",
344 pSeg->apIoLog[cSector]->off,
345 pSeg->apIoLog[cSector]->cbWrite,
346 pSeg->apIoLog[cSector]->cRefs);
347 RTAssertDebugBreak();
348 }
349 }
350 else
351 RTSgBufAdvance(&SgBuf, cbRange);
352
353 offCurr += cbRange;
354 cbLeft -= cbRange;
355 }
356
357 return rc;
358}
359
360/* -=-=-=-=- IMedia -=-=-=-=- */
361
362/** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIA. */
363#define PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMedia)) )
364/** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIAASYNC. */
365#define PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaAsync)) )
366
367/*******************************************************************************
368* Media interface methods *
369*******************************************************************************/
370
371/** @copydoc PDMIMEDIA::pfnRead */
372static DECLCALLBACK(int) drvdiskintRead(PPDMIMEDIA pInterface,
373 uint64_t off, void *pvBuf, size_t cbRead)
374{
375 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
376 int rc = pThis->pDrvMedia->pfnRead(pThis->pDrvMedia, off, pvBuf, cbRead);
377 if (RT_FAILURE(rc))
378 return rc;
379
380 /* Verify the read. */
381 RTSGSEG Seg;
382 Seg.cbSeg = cbRead;
383 Seg.pvSeg = pvBuf;
384 return drvdiskintReadVerify(pThis, &Seg, 1, off, cbRead);
385}
386
387/** @copydoc PDMIMEDIA::pfnWrite */
388static DECLCALLBACK(int) drvdiskintWrite(PPDMIMEDIA pInterface,
389 uint64_t off, const void *pvBuf,
390 size_t cbWrite)
391{
392 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
393 int rc = pThis->pDrvMedia->pfnWrite(pThis->pDrvMedia, off, pvBuf, cbWrite);
394 if (RT_FAILURE(rc))
395 return rc;
396
397 /* Record the write. */
398 RTSGSEG Seg;
399 Seg.cbSeg = cbWrite;
400 Seg.pvSeg = (void *)pvBuf;
401 return drvdiskintWriteRecord(pThis, &Seg, 1, off, cbWrite);
402}
403
404static DECLCALLBACK(int) drvdiskintStartRead(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
405 PCRTSGSEG paSeg, unsigned cSeg,
406 size_t cbRead, void *pvUser)
407{
408 LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbRead=%d\n pvUser=%#p", __FUNCTION__,
409 uOffset, paSeg, cSeg, cbRead, pvUser));
410 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
411 PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(true, uOffset, paSeg, cSeg, cbRead, pvUser);
412 AssertPtr(pIoReq);
413
414 int rc = pThis->pDrvMediaAsync->pfnStartRead(pThis->pDrvMediaAsync, uOffset, paSeg, cSeg,
415 cbRead, pIoReq);
416 if (rc == VINF_VD_ASYNC_IO_FINISHED)
417 {
418 /* Verify the read now. */
419 int rc2 = drvdiskintReadVerify(pThis, paSeg, cSeg, uOffset, cbRead);
420 AssertRC(rc2);
421 RTMemFree(pIoReq);
422 }
423 else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
424 RTMemFree(pIoReq);
425
426 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
427 return rc;
428}
429
430static DECLCALLBACK(int) drvdiskintStartWrite(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
431 PCRTSGSEG paSeg, unsigned cSeg,
432 size_t cbWrite, void *pvUser)
433{
434 LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbWrite=%d\n pvUser=%#p", __FUNCTION__,
435 uOffset, paSeg, cSeg, cbWrite, pvUser));
436 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
437 PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(false, uOffset, paSeg, cSeg, cbWrite, pvUser);
438 AssertPtr(pIoReq);
439
440 int rc = pThis->pDrvMediaAsync->pfnStartWrite(pThis->pDrvMediaAsync, uOffset, paSeg, cSeg,
441 cbWrite, pIoReq);
442 if (rc == VINF_VD_ASYNC_IO_FINISHED)
443 {
444 /* Verify the read now. */
445 int rc2 = drvdiskintWriteRecord(pThis, paSeg, cSeg, uOffset, cbWrite);
446 AssertRC(rc2);
447 RTMemFree(pIoReq);
448 }
449 else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
450 RTMemFree(pIoReq);
451
452 LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
453 return rc;
454}
455
456/** @copydoc PDMIMEDIA::pfnFlush */
457static DECLCALLBACK(int) drvdiskintFlush(PPDMIMEDIA pInterface)
458{
459 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
460 return pThis->pDrvMedia->pfnFlush(pThis->pDrvMedia);
461}
462
463/** @copydoc PDMIMEDIA::pfnGetSize */
464static DECLCALLBACK(uint64_t) drvdiskintGetSize(PPDMIMEDIA pInterface)
465{
466 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
467 return pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
468}
469
470/** @copydoc PDMIMEDIA::pfnIsReadOnly */
471static DECLCALLBACK(bool) drvdiskintIsReadOnly(PPDMIMEDIA pInterface)
472{
473 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
474 return pThis->pDrvMedia->pfnIsReadOnly(pThis->pDrvMedia);
475}
476
477/** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
478static DECLCALLBACK(int) drvdiskintBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
479 PPDMMEDIAGEOMETRY pPCHSGeometry)
480{
481 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
482 return pThis->pDrvMedia->pfnBiosGetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
483}
484
485/** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
486static DECLCALLBACK(int) drvdiskintBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
487 PCPDMMEDIAGEOMETRY pPCHSGeometry)
488{
489 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
490 return pThis->pDrvMedia->pfnBiosSetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
491}
492
493/** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
494static DECLCALLBACK(int) drvdiskintBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
495 PPDMMEDIAGEOMETRY pLCHSGeometry)
496{
497 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
498 return pThis->pDrvMedia->pfnBiosGetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
499}
500
501/** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
502static DECLCALLBACK(int) drvdiskintBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
503 PCPDMMEDIAGEOMETRY pLCHSGeometry)
504{
505 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
506 return pThis->pDrvMedia->pfnBiosSetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
507}
508
509/** @copydoc PDMIMEDIA::pfnGetUuid */
510static DECLCALLBACK(int) drvdiskintGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
511{
512 PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
513 return pThis->pDrvMedia->pfnGetUuid(pThis->pDrvMedia, pUuid);
514}
515
516/* -=-=-=-=- IMediaAsyncPort -=-=-=-=- */
517
518/** Makes a PDRVBLOCKASYNC out of a PPDMIMEDIAASYNCPORT. */
519#define PDMIMEDIAASYNCPORT_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaAsyncPort))) )
520
521static DECLCALLBACK(int) drvdiskintAsyncTransferCompleteNotify(PPDMIMEDIAASYNCPORT pInterface, void *pvUser)
522{
523 PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNCPORT_2_DRVDISKINTEGRITY(pInterface);
524 PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvUser;
525 int rc = VINF_SUCCESS;
526
527 LogFlowFunc(("pIoReq=%#p\n", pIoReq));
528
529 if (pIoReq->fRead)
530 rc = drvdiskintReadVerify(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
531 else
532 rc = drvdiskintWriteRecord(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
533
534 AssertRC(rc);
535
536 rc = pThis->pDrvMediaAsyncPort->pfnTransferCompleteNotify(pThis->pDrvMediaAsyncPort, pIoReq->pvUser);
537 RTMemFree(pIoReq);
538
539 return rc;
540}
541
542/* -=-=-=-=- IBase -=-=-=-=- */
543
544/**
545 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
546 */
547static DECLCALLBACK(void *) drvdiskintQueryInterface(PPDMIBASE pInterface, const char *pszIID)
548{
549 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
550 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
551
552 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
553 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
554 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAASYNC, pThis->pDrvMediaAsync ? &pThis->IMediaAsync : NULL);
555 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAASYNCPORT, &pThis->IMediaAsyncPort);
556 return NULL;
557}
558
559
560/* -=-=-=-=- driver interface -=-=-=-=- */
561
562static int drvdiskintTreeDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
563{
564 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)pNode;
565
566 RTMemFree(pSeg->pbSeg);
567 RTMemFree(pSeg);
568 return VINF_SUCCESS;
569}
570
571/**
572 * @copydoc FNPDMDRVDESTRUCT
573 */
574static DECLCALLBACK(void) drvdiskintDestruct(PPDMDRVINS pDrvIns)
575{
576 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
577
578 if (pThis->pTreeSegments)
579 {
580 RTAvlrFileOffsetDestroy(pThis->pTreeSegments, drvdiskintTreeDestroy, NULL);
581 RTMemFree(pThis->pTreeSegments);
582 }
583}
584
585/**
586 * Construct a disk integrity driver instance.
587 *
588 * @copydoc FNPDMDRVCONSTRUCT
589 */
590static DECLCALLBACK(int) drvdiskintConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
591{
592 PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
593 LogFlow(("drvdiskintConstruct: iInstance=%d\n", pDrvIns->iInstance));
594 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
595
596 /*
597 * Validate configuration.
598 */
599 if (!CFGMR3AreValuesValid(pCfg, ""))
600 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
601
602 /*
603 * Initialize most of the data members.
604 */
605 pThis->pDrvIns = pDrvIns;
606
607 /* IBase. */
608 pDrvIns->IBase.pfnQueryInterface = drvdiskintQueryInterface;
609
610 /* IMedia */
611 pThis->IMedia.pfnRead = drvdiskintRead;
612 pThis->IMedia.pfnWrite = drvdiskintWrite;
613 pThis->IMedia.pfnFlush = drvdiskintFlush;
614 pThis->IMedia.pfnGetSize = drvdiskintGetSize;
615 pThis->IMedia.pfnIsReadOnly = drvdiskintIsReadOnly;
616 pThis->IMedia.pfnBiosGetPCHSGeometry = drvdiskintBiosGetPCHSGeometry;
617 pThis->IMedia.pfnBiosSetPCHSGeometry = drvdiskintBiosSetPCHSGeometry;
618 pThis->IMedia.pfnBiosGetLCHSGeometry = drvdiskintBiosGetLCHSGeometry;
619 pThis->IMedia.pfnBiosSetLCHSGeometry = drvdiskintBiosSetLCHSGeometry;
620 pThis->IMedia.pfnGetUuid = drvdiskintGetUuid;
621
622 /* IMediaAsync */
623 pThis->IMediaAsync.pfnStartRead = drvdiskintStartRead;
624 pThis->IMediaAsync.pfnStartWrite = drvdiskintStartWrite;
625
626 /* IMediaAsyncPort. */
627 pThis->IMediaAsyncPort.pfnTransferCompleteNotify = drvdiskintAsyncTransferCompleteNotify;
628
629 /*
630 * Try attach driver below and query it's media interface.
631 */
632 PPDMIBASE pBase;
633 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
634 if (RT_FAILURE(rc))
635 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
636 N_("Failed to attach driver below us! %Rrf"), rc);
637
638 pThis->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
639 if (!pThis->pDrvMedia)
640 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
641 N_("No media or async media interface below"));
642
643 pThis->pDrvMediaAsync = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIAASYNC);
644
645 /* Try to attach async media port interface above.*/
646 pThis->pDrvMediaAsyncPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAASYNCPORT);
647
648 /* Create the AVL tree. */
649 pThis->pTreeSegments = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
650 if (!pThis->pTreeSegments)
651 rc = VERR_NO_MEMORY;
652
653 return rc;
654}
655
656
657/**
658 * Block driver registration record.
659 */
660const PDMDRVREG g_DrvDiskIntegrity =
661{
662 /* u32Version */
663 PDM_DRVREG_VERSION,
664 /* szName */
665 "DiskIntegrity",
666 /* szRCMod */
667 "",
668 /* szR0Mod */
669 "",
670 /* pszDescription */
671 "Disk integrity driver.",
672 /* fFlags */
673 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
674 /* fClass. */
675 PDM_DRVREG_CLASS_BLOCK,
676 /* cMaxInstances */
677 ~0,
678 /* cbInstance */
679 sizeof(DRVDISKINTEGRITY),
680 /* pfnConstruct */
681 drvdiskintConstruct,
682 /* pfnDestruct */
683 drvdiskintDestruct,
684 /* pfnRelocate */
685 NULL,
686 /* pfnIOCtl */
687 NULL,
688 /* pfnPowerOn */
689 NULL,
690 /* pfnReset */
691 NULL,
692 /* pfnSuspend */
693 NULL,
694 /* pfnResume */
695 NULL,
696 /* pfnAttach */
697 NULL,
698 /* pfnDetach */
699 NULL,
700 /* pfnPowerOff */
701 NULL,
702 /* pfnSoftReset */
703 NULL,
704 /* u32EndVersion */
705 PDM_DRVREG_VERSION
706};
707
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