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