1 | /* $Id: DrvDiskIntegrity.cpp 28118 2010-04-08 21:39:01Z 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.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 | * 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 | */
|
---|
46 | typedef 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 | */
|
---|
65 | typedef 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 | */
|
---|
78 | typedef 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 | */
|
---|
97 | typedef 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 | */
|
---|
134 | static 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 | */
|
---|
162 | static 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 | RTSgBufCopyToBuf(&SgBuf, pSeg->pbSeg + offSeg, cbRange);
|
---|
233 |
|
---|
234 | /* Update the I/O log pointers */
|
---|
235 | Assert(offSeg % 512 == 0);
|
---|
236 | Assert(cbRange % 512 == 0);
|
---|
237 | while (offSeg < cbRange)
|
---|
238 | {
|
---|
239 | uint32_t uSector = offSeg / 512;
|
---|
240 | PIOLOGENT pIoLogOld = NULL;
|
---|
241 |
|
---|
242 | AssertMsg(uSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
|
---|
243 |
|
---|
244 | pIoLogOld = pSeg->apIoLog[uSector];
|
---|
245 | if (pIoLogOld)
|
---|
246 | {
|
---|
247 | pIoLogOld->cRefs--;
|
---|
248 | if (!pIoLogOld->cRefs)
|
---|
249 | RTMemFree(pIoLogOld);
|
---|
250 | }
|
---|
251 |
|
---|
252 | pSeg->apIoLog[uSector] = pIoLogEnt;
|
---|
253 | pIoLogEnt->cRefs++;
|
---|
254 |
|
---|
255 | offSeg += 512;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | else
|
---|
259 | RTSgBufAdvance(&SgBuf, cbRange);
|
---|
260 |
|
---|
261 | offCurr += cbRange;
|
---|
262 | cbLeft -= cbRange;
|
---|
263 | }
|
---|
264 |
|
---|
265 | return rc;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Verifies a read request.
|
---|
270 | *
|
---|
271 | * @returns VBox status code.
|
---|
272 | * @param pThis Disk integrity driver instance data.
|
---|
273 | * @param paSeg Segment array of the containing the data buffers to verify.
|
---|
274 | * @param cSeg Number of segments.
|
---|
275 | * @param off Start offset.
|
---|
276 | * @param cbWrite Number of bytes to verify.
|
---|
277 | */
|
---|
278 | static int drvdiskintReadVerify(PDRVDISKINTEGRITY pThis, PCRTSGSEG paSeg, unsigned cSeg,
|
---|
279 | uint64_t off, size_t cbRead)
|
---|
280 | {
|
---|
281 | int rc = VINF_SUCCESS;
|
---|
282 |
|
---|
283 | LogFlowFunc(("pThis=%#p paSeg=%#p cSeg=%u off=%llx cbRead=%u\n",
|
---|
284 | pThis, paSeg, cSeg, off, cbRead));
|
---|
285 |
|
---|
286 | Assert(off % 512 == 0);
|
---|
287 | Assert(cbRead % 512 == 0);
|
---|
288 |
|
---|
289 | /* Compare read data */
|
---|
290 | size_t cbLeft = cbRead;
|
---|
291 | RTFOFF offCurr = (RTFOFF)off;
|
---|
292 | RTSGBUF SgBuf;
|
---|
293 |
|
---|
294 | RTSgBufInit(&SgBuf, paSeg, cSeg);
|
---|
295 |
|
---|
296 | while (cbLeft)
|
---|
297 | {
|
---|
298 | PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
|
---|
299 | size_t cbRange = 0;
|
---|
300 | bool fCmp = false;
|
---|
301 | unsigned offSeg = 0;
|
---|
302 |
|
---|
303 | if (!pSeg)
|
---|
304 | {
|
---|
305 | /* Get next segment */
|
---|
306 | pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
|
---|
307 | if (!pSeg)
|
---|
308 | {
|
---|
309 | /* No data in the tree for this read. Assume everything is ok. */
|
---|
310 | cbRange = cbLeft;
|
---|
311 | }
|
---|
312 | else if (offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
|
---|
313 | cbRange = cbLeft;
|
---|
314 | else
|
---|
315 | cbRange = pSeg->Core.Key - offCurr;
|
---|
316 | }
|
---|
317 | else
|
---|
318 | {
|
---|
319 | fCmp = true;
|
---|
320 | offSeg = offCurr - pSeg->Core.Key;
|
---|
321 | cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (fCmp)
|
---|
325 | {
|
---|
326 | RTSGSEG Seg;
|
---|
327 | RTSGBUF SgBufCmp;
|
---|
328 | size_t cbOff = 0;
|
---|
329 |
|
---|
330 | Seg.cbSeg = cbRange;
|
---|
331 | Seg.pvSeg = pSeg->pbSeg + offSeg;
|
---|
332 |
|
---|
333 | RTSgBufInit(&SgBufCmp, &Seg, 1);
|
---|
334 | if (RTSgBufCmpEx(&SgBuf, &SgBufCmp, cbRange, &cbOff, true))
|
---|
335 | {
|
---|
336 | /* Corrupted disk, print I/O log entry of the last write which accessed this range. */
|
---|
337 | uint32_t cSector = (offSeg + cbOff) / 512;
|
---|
338 | AssertMsg(cSector < pSeg->cIoLogEntries, ("Internal bug!\n"));
|
---|
339 |
|
---|
340 | RTMsgError("Corrupted disk at offset %llu (%u bytes in the current read buffer)!\n",
|
---|
341 | offCurr + cbOff, cbOff);
|
---|
342 | RTMsgError("Last write to this sector started at offset %llu with %u bytes (%u references to this log entry)\n",
|
---|
343 | pSeg->apIoLog[cSector]->off,
|
---|
344 | pSeg->apIoLog[cSector]->cbWrite,
|
---|
345 | pSeg->apIoLog[cSector]->cRefs);
|
---|
346 | RTAssertDebugBreak();
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | offCurr += cbRange;
|
---|
351 | cbLeft -= cbRange;
|
---|
352 | }
|
---|
353 |
|
---|
354 | return rc;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /* -=-=-=-=- IMedia -=-=-=-=- */
|
---|
358 |
|
---|
359 | /** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIA. */
|
---|
360 | #define PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMedia)) )
|
---|
361 | /** Makes a PDRVDISKINTEGRITY out of a PPDMIMEDIAASYNC. */
|
---|
362 | #define PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY)((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaAsync)) )
|
---|
363 |
|
---|
364 | /*******************************************************************************
|
---|
365 | * Media interface methods *
|
---|
366 | *******************************************************************************/
|
---|
367 |
|
---|
368 | /** @copydoc PDMIMEDIA::pfnRead */
|
---|
369 | static DECLCALLBACK(int) drvdiskintRead(PPDMIMEDIA pInterface,
|
---|
370 | uint64_t off, void *pvBuf, size_t cbRead)
|
---|
371 | {
|
---|
372 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
373 | int rc = pThis->pDrvMedia->pfnRead(pThis->pDrvMedia, off, pvBuf, cbRead);
|
---|
374 | if (RT_FAILURE(rc))
|
---|
375 | return rc;
|
---|
376 |
|
---|
377 | /* Verify the read. */
|
---|
378 | RTSGSEG Seg;
|
---|
379 | Seg.cbSeg = cbRead;
|
---|
380 | Seg.pvSeg = pvBuf;
|
---|
381 | return drvdiskintReadVerify(pThis, &Seg, 1, off, cbRead);
|
---|
382 | }
|
---|
383 |
|
---|
384 | /** @copydoc PDMIMEDIA::pfnWrite */
|
---|
385 | static DECLCALLBACK(int) drvdiskintWrite(PPDMIMEDIA pInterface,
|
---|
386 | uint64_t off, const void *pvBuf,
|
---|
387 | size_t cbWrite)
|
---|
388 | {
|
---|
389 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
390 | int rc = pThis->pDrvMedia->pfnWrite(pThis->pDrvMedia, off, pvBuf, cbWrite);
|
---|
391 | if (RT_FAILURE(rc))
|
---|
392 | return rc;
|
---|
393 |
|
---|
394 | /* Record the write. */
|
---|
395 | RTSGSEG Seg;
|
---|
396 | Seg.cbSeg = cbWrite;
|
---|
397 | Seg.pvSeg = (void *)pvBuf;
|
---|
398 | return drvdiskintWriteRecord(pThis, &Seg, 1, off, cbWrite);
|
---|
399 | }
|
---|
400 |
|
---|
401 | static DECLCALLBACK(int) drvdiskintStartRead(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
|
---|
402 | PCRTSGSEG paSeg, unsigned cSeg,
|
---|
403 | size_t cbRead, void *pvUser)
|
---|
404 | {
|
---|
405 | LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbRead=%d\n pvUser=%#p", __FUNCTION__,
|
---|
406 | uOffset, paSeg, cSeg, cbRead, pvUser));
|
---|
407 | PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
|
---|
408 | PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(true, uOffset, paSeg, cSeg, cbRead, pvUser);
|
---|
409 | AssertPtr(pIoReq);
|
---|
410 |
|
---|
411 | int rc = pThis->pDrvMediaAsync->pfnStartRead(pThis->pDrvMediaAsync, uOffset, paSeg, cSeg,
|
---|
412 | cbRead, pIoReq);
|
---|
413 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
414 | {
|
---|
415 | /* Verify the read now. */
|
---|
416 | int rc2 = drvdiskintReadVerify(pThis, paSeg, cSeg, uOffset, cbRead);
|
---|
417 | AssertRC(rc2);
|
---|
418 | RTMemFree(pIoReq);
|
---|
419 | }
|
---|
420 | else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
421 | RTMemFree(pIoReq);
|
---|
422 |
|
---|
423 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
424 | return rc;
|
---|
425 | }
|
---|
426 |
|
---|
427 | static DECLCALLBACK(int) drvdiskintStartWrite(PPDMIMEDIAASYNC pInterface, uint64_t uOffset,
|
---|
428 | PCRTSGSEG paSeg, unsigned cSeg,
|
---|
429 | size_t cbWrite, void *pvUser)
|
---|
430 | {
|
---|
431 | LogFlow(("%s: uOffset=%#llx paSeg=%#p cSeg=%u cbWrite=%d\n pvUser=%#p", __FUNCTION__,
|
---|
432 | uOffset, paSeg, cSeg, cbWrite, pvUser));
|
---|
433 | PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNC_2_DRVDISKINTEGRITY(pInterface);
|
---|
434 | PDRVDISKAIOREQ pIoReq = drvdiskintIoReqAlloc(false, uOffset, paSeg, cSeg, cbWrite, pvUser);
|
---|
435 | AssertPtr(pIoReq);
|
---|
436 |
|
---|
437 | int rc = pThis->pDrvMediaAsync->pfnStartWrite(pThis->pDrvMediaAsync, uOffset, paSeg, cSeg,
|
---|
438 | cbWrite, pIoReq);
|
---|
439 | if (rc == VINF_VD_ASYNC_IO_FINISHED)
|
---|
440 | {
|
---|
441 | /* Verify the read now. */
|
---|
442 | int rc2 = drvdiskintWriteRecord(pThis, paSeg, cSeg, uOffset, cbWrite);
|
---|
443 | AssertRC(rc2);
|
---|
444 | RTMemFree(pIoReq);
|
---|
445 | }
|
---|
446 | else if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
|
---|
447 | RTMemFree(pIoReq);
|
---|
448 |
|
---|
449 | LogFlow(("%s: returns %Rrc\n", __FUNCTION__, rc));
|
---|
450 | return rc;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /** @copydoc PDMIMEDIA::pfnFlush */
|
---|
454 | static DECLCALLBACK(int) drvdiskintFlush(PPDMIMEDIA pInterface)
|
---|
455 | {
|
---|
456 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
457 | return pThis->pDrvMedia->pfnFlush(pThis->pDrvMedia);
|
---|
458 | }
|
---|
459 |
|
---|
460 | /** @copydoc PDMIMEDIA::pfnGetSize */
|
---|
461 | static DECLCALLBACK(uint64_t) drvdiskintGetSize(PPDMIMEDIA pInterface)
|
---|
462 | {
|
---|
463 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
464 | return pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
|
---|
465 | }
|
---|
466 |
|
---|
467 | /** @copydoc PDMIMEDIA::pfnIsReadOnly */
|
---|
468 | static DECLCALLBACK(bool) drvdiskintIsReadOnly(PPDMIMEDIA pInterface)
|
---|
469 | {
|
---|
470 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
471 | return pThis->pDrvMedia->pfnIsReadOnly(pThis->pDrvMedia);
|
---|
472 | }
|
---|
473 |
|
---|
474 | /** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
|
---|
475 | static DECLCALLBACK(int) drvdiskintBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
|
---|
476 | PPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
477 | {
|
---|
478 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
479 | return pThis->pDrvMedia->pfnBiosGetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
|
---|
480 | }
|
---|
481 |
|
---|
482 | /** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
|
---|
483 | static DECLCALLBACK(int) drvdiskintBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
|
---|
484 | PCPDMMEDIAGEOMETRY pPCHSGeometry)
|
---|
485 | {
|
---|
486 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
487 | return pThis->pDrvMedia->pfnBiosSetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
|
---|
488 | }
|
---|
489 |
|
---|
490 | /** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
|
---|
491 | static DECLCALLBACK(int) drvdiskintBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
|
---|
492 | PPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
493 | {
|
---|
494 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
495 | return pThis->pDrvMedia->pfnBiosGetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
|
---|
496 | }
|
---|
497 |
|
---|
498 | /** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
|
---|
499 | static DECLCALLBACK(int) drvdiskintBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
|
---|
500 | PCPDMMEDIAGEOMETRY pLCHSGeometry)
|
---|
501 | {
|
---|
502 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
503 | return pThis->pDrvMedia->pfnBiosSetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
|
---|
504 | }
|
---|
505 |
|
---|
506 | /** @copydoc PDMIMEDIA::pfnGetUuid */
|
---|
507 | static DECLCALLBACK(int) drvdiskintGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
|
---|
508 | {
|
---|
509 | PDRVDISKINTEGRITY pThis = PDMIMEDIA_2_DRVDISKINTEGRITY(pInterface);
|
---|
510 | return pThis->pDrvMedia->pfnGetUuid(pThis->pDrvMedia, pUuid);
|
---|
511 | }
|
---|
512 |
|
---|
513 | /* -=-=-=-=- IMediaAsyncPort -=-=-=-=- */
|
---|
514 |
|
---|
515 | /** Makes a PDRVBLOCKASYNC out of a PPDMIMEDIAASYNCPORT. */
|
---|
516 | #define PDMIMEDIAASYNCPORT_2_DRVDISKINTEGRITY(pInterface) ( (PDRVDISKINTEGRITY((uintptr_t)pInterface - RT_OFFSETOF(DRVDISKINTEGRITY, IMediaAsyncPort))) )
|
---|
517 |
|
---|
518 | static DECLCALLBACK(int) drvdiskintAsyncTransferCompleteNotify(PPDMIMEDIAASYNCPORT pInterface, void *pvUser)
|
---|
519 | {
|
---|
520 | PDRVDISKINTEGRITY pThis = PDMIMEDIAASYNCPORT_2_DRVDISKINTEGRITY(pInterface);
|
---|
521 | PDRVDISKAIOREQ pIoReq = (PDRVDISKAIOREQ)pvUser;
|
---|
522 | int rc = VINF_SUCCESS;
|
---|
523 |
|
---|
524 | LogFlowFunc(("pIoReq=%#p\n", pIoReq));
|
---|
525 |
|
---|
526 | if (pIoReq->fRead)
|
---|
527 | rc = drvdiskintReadVerify(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
|
---|
528 | else
|
---|
529 | rc = drvdiskintWriteRecord(pThis, pIoReq->paSeg, pIoReq->cSeg, pIoReq->off, pIoReq->cbTransfer);
|
---|
530 |
|
---|
531 | AssertRC(rc);
|
---|
532 |
|
---|
533 | rc = pThis->pDrvMediaAsyncPort->pfnTransferCompleteNotify(pThis->pDrvMediaAsyncPort, pIoReq->pvUser);
|
---|
534 | RTMemFree(pIoReq);
|
---|
535 |
|
---|
536 | return rc;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /* -=-=-=-=- IBase -=-=-=-=- */
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
543 | */
|
---|
544 | static DECLCALLBACK(void *) drvdiskintQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
545 | {
|
---|
546 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
547 | PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
|
---|
548 |
|
---|
549 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
550 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
|
---|
551 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAASYNC, pThis->pDrvMediaAsync ? &pThis->IMediaAsync : NULL);
|
---|
552 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAASYNCPORT, &pThis->IMediaAsyncPort);
|
---|
553 | return NULL;
|
---|
554 | }
|
---|
555 |
|
---|
556 |
|
---|
557 | /* -=-=-=-=- driver interface -=-=-=-=- */
|
---|
558 |
|
---|
559 | static int drvdiskintTreeDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
|
---|
560 | {
|
---|
561 | PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)pNode;
|
---|
562 |
|
---|
563 | RTMemFree(pSeg->pbSeg);
|
---|
564 | RTMemFree(pSeg);
|
---|
565 | return VINF_SUCCESS;
|
---|
566 | }
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * @copydoc FNPDMDRVDESTRUCT
|
---|
570 | */
|
---|
571 | static DECLCALLBACK(void) drvdiskintDestruct(PPDMDRVINS pDrvIns)
|
---|
572 | {
|
---|
573 | PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
|
---|
574 |
|
---|
575 | if (pThis->pTreeSegments)
|
---|
576 | {
|
---|
577 | RTAvlrFileOffsetDestroy(pThis->pTreeSegments, drvdiskintTreeDestroy, NULL);
|
---|
578 | RTMemFree(pThis->pTreeSegments);
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * Construct a disk integrity driver instance.
|
---|
584 | *
|
---|
585 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
586 | */
|
---|
587 | static DECLCALLBACK(int) drvdiskintConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
588 | {
|
---|
589 | PDRVDISKINTEGRITY pThis = PDMINS_2_DATA(pDrvIns, PDRVDISKINTEGRITY);
|
---|
590 | LogFlow(("drvdiskintConstruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
591 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
592 |
|
---|
593 | /*
|
---|
594 | * Validate configuration.
|
---|
595 | */
|
---|
596 | if (!CFGMR3AreValuesValid(pCfg, ""))
|
---|
597 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
598 |
|
---|
599 | /*
|
---|
600 | * Initialize most of the data members.
|
---|
601 | */
|
---|
602 | pThis->pDrvIns = pDrvIns;
|
---|
603 |
|
---|
604 | /* IBase. */
|
---|
605 | pDrvIns->IBase.pfnQueryInterface = drvdiskintQueryInterface;
|
---|
606 |
|
---|
607 | /* IMedia */
|
---|
608 | pThis->IMedia.pfnRead = drvdiskintRead;
|
---|
609 | pThis->IMedia.pfnWrite = drvdiskintWrite;
|
---|
610 | pThis->IMedia.pfnFlush = drvdiskintFlush;
|
---|
611 | pThis->IMedia.pfnGetSize = drvdiskintGetSize;
|
---|
612 | pThis->IMedia.pfnIsReadOnly = drvdiskintIsReadOnly;
|
---|
613 | pThis->IMedia.pfnBiosGetPCHSGeometry = drvdiskintBiosGetPCHSGeometry;
|
---|
614 | pThis->IMedia.pfnBiosSetPCHSGeometry = drvdiskintBiosSetPCHSGeometry;
|
---|
615 | pThis->IMedia.pfnBiosGetLCHSGeometry = drvdiskintBiosGetLCHSGeometry;
|
---|
616 | pThis->IMedia.pfnBiosSetLCHSGeometry = drvdiskintBiosSetLCHSGeometry;
|
---|
617 | pThis->IMedia.pfnGetUuid = drvdiskintGetUuid;
|
---|
618 |
|
---|
619 | /* IMediaAsync */
|
---|
620 | pThis->IMediaAsync.pfnStartRead = drvdiskintStartRead;
|
---|
621 | pThis->IMediaAsync.pfnStartWrite = drvdiskintStartWrite;
|
---|
622 |
|
---|
623 | /* IMediaAsyncPort. */
|
---|
624 | pThis->IMediaAsyncPort.pfnTransferCompleteNotify = drvdiskintAsyncTransferCompleteNotify;
|
---|
625 |
|
---|
626 | /*
|
---|
627 | * Try attach driver below and query it's media interface.
|
---|
628 | */
|
---|
629 | PPDMIBASE pBase;
|
---|
630 | int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
|
---|
631 | if (RT_FAILURE(rc))
|
---|
632 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
633 | N_("Failed to attach driver below us! %Rrf"), rc);
|
---|
634 |
|
---|
635 | pThis->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIA);
|
---|
636 | if (!pThis->pDrvMedia)
|
---|
637 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
|
---|
638 | N_("No media or async media interface below"));
|
---|
639 |
|
---|
640 | pThis->pDrvMediaAsync = PDMIBASE_QUERY_INTERFACE(pBase, PDMIMEDIAASYNC);
|
---|
641 |
|
---|
642 | /* Try to attach async media port interface above.*/
|
---|
643 | pThis->pDrvMediaAsyncPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAASYNCPORT);
|
---|
644 |
|
---|
645 | /* Create the AVL tree. */
|
---|
646 | pThis->pTreeSegments = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
|
---|
647 | if (!pThis->pTreeSegments)
|
---|
648 | rc = VERR_NO_MEMORY;
|
---|
649 |
|
---|
650 | return rc;
|
---|
651 | }
|
---|
652 |
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Block driver registration record.
|
---|
656 | */
|
---|
657 | const PDMDRVREG g_DrvDiskIntegrity =
|
---|
658 | {
|
---|
659 | /* u32Version */
|
---|
660 | PDM_DRVREG_VERSION,
|
---|
661 | /* szName */
|
---|
662 | "DiskIntegrity",
|
---|
663 | /* szRCMod */
|
---|
664 | "",
|
---|
665 | /* szR0Mod */
|
---|
666 | "",
|
---|
667 | /* pszDescription */
|
---|
668 | "Disk integrity driver.",
|
---|
669 | /* fFlags */
|
---|
670 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
671 | /* fClass. */
|
---|
672 | PDM_DRVREG_CLASS_BLOCK,
|
---|
673 | /* cMaxInstances */
|
---|
674 | ~0,
|
---|
675 | /* cbInstance */
|
---|
676 | sizeof(DRVDISKINTEGRITY),
|
---|
677 | /* pfnConstruct */
|
---|
678 | drvdiskintConstruct,
|
---|
679 | /* pfnDestruct */
|
---|
680 | drvdiskintDestruct,
|
---|
681 | /* pfnRelocate */
|
---|
682 | NULL,
|
---|
683 | /* pfnIOCtl */
|
---|
684 | NULL,
|
---|
685 | /* pfnPowerOn */
|
---|
686 | NULL,
|
---|
687 | /* pfnReset */
|
---|
688 | NULL,
|
---|
689 | /* pfnSuspend */
|
---|
690 | NULL,
|
---|
691 | /* pfnResume */
|
---|
692 | NULL,
|
---|
693 | /* pfnAttach */
|
---|
694 | NULL,
|
---|
695 | /* pfnDetach */
|
---|
696 | NULL,
|
---|
697 | /* pfnPowerOff */
|
---|
698 | NULL,
|
---|
699 | /* pfnSoftReset */
|
---|
700 | NULL,
|
---|
701 | /* u32EndVersion */
|
---|
702 | PDM_DRVREG_VERSION
|
---|
703 | };
|
---|
704 |
|
---|