VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvRamDisk.cpp@ 78272

Last change on this file since 78272 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 64.9 KB
Line 
1/* $Id: DrvRamDisk.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * VBox storage devices: RAM disk driver.
4 */
5
6/*
7 * Copyright (C) 2016-2019 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/vmm/pdmstorageifs.h>
25#include <iprt/assert.h>
26#include <iprt/string.h>
27#include <iprt/uuid.h>
28#include <iprt/avl.h>
29#include <iprt/list.h>
30#include <iprt/mem.h>
31#include <iprt/memcache.h>
32#include <iprt/message.h>
33#include <iprt/sg.h>
34#include <iprt/time.h>
35#include <iprt/semaphore.h>
36#include <iprt/asm.h>
37#include <iprt/req.h>
38#include <iprt/thread.h>
39
40#include "VBoxDD.h"
41#include "IOBufMgmt.h"
42
43
44/*********************************************************************************************************************************
45* Structures and Typedefs *
46*********************************************************************************************************************************/
47
48/** Pointer to a ramdisk driver instance. */
49typedef struct DRVRAMDISK *PDRVRAMDISK;
50
51/**
52 * Disk segment.
53 */
54typedef struct DRVDISKSEGMENT
55{
56 /** AVL core. */
57 AVLRFOFFNODECORE Core;
58 /** Size of the segment */
59 size_t cbSeg;
60 /** Data for this segment */
61 uint8_t *pbSeg;
62} DRVDISKSEGMENT, *PDRVDISKSEGMENT;
63
64/**
65 * VD I/O request state.
66 */
67typedef enum VDIOREQSTATE
68{
69 /** Invalid. */
70 VDIOREQSTATE_INVALID = 0,
71 /** The request is not in use and resides on the free list. */
72 VDIOREQSTATE_FREE,
73 /** The request was just allocated and is not active. */
74 VDIOREQSTATE_ALLOCATED,
75 /** The request was allocated and is in use. */
76 VDIOREQSTATE_ACTIVE,
77 /** The request was suspended and is not actively processed. */
78 VDIOREQSTATE_SUSPENDED,
79 /** The request is in the last step of completion and syncs memory. */
80 VDIOREQSTATE_COMPLETING,
81 /** The request completed. */
82 VDIOREQSTATE_COMPLETED,
83 /** The request was aborted but wasn't returned as complete from the storage
84 * layer below us. */
85 VDIOREQSTATE_CANCELED,
86 /** 32bit hack. */
87 VDIOREQSTATE_32BIT_HACK = 0x7fffffff
88} VDIOREQSTATE;
89
90/**
91 * VD I/O Request.
92 */
93typedef struct PDMMEDIAEXIOREQINT
94{
95 /** List node for the list of allocated requests. */
96 RTLISTNODE NdAllocatedList;
97 /** List for requests waiting for I/O memory or on the redo list. */
98 RTLISTNODE NdLstWait;
99 /** I/O request type. */
100 PDMMEDIAEXIOREQTYPE enmType;
101 /** Request state. */
102 volatile VDIOREQSTATE enmState;
103 /** I/O request ID. */
104 PDMMEDIAEXIOREQID uIoReqId;
105 /** Pointer to the disk container. */
106 PDRVRAMDISK pDisk;
107 /** Flags. */
108 uint32_t fFlags;
109 /** Timestamp when the request was submitted. */
110 uint64_t tsSubmit;
111 /** Type dependent data. */
112 union
113 {
114 /** Read/Write request sepcific data. */
115 struct
116 {
117 /** Start offset of the request. */
118 uint64_t offStart;
119 /** Size of the request. */
120 size_t cbReq;
121 /** Size left for this request. */
122 size_t cbReqLeft;
123 /** Size of the allocated I/O buffer. */
124 size_t cbIoBuf;
125 /** I/O buffer descriptor. */
126 IOBUFDESC IoBuf;
127 } ReadWrite;
128 /** Discard specific data. */
129 struct
130 {
131 /** Pointer to array of ranges to discard. */
132 PRTRANGE paRanges;
133 /** Number of ranges to discard. */
134 unsigned cRanges;
135 } Discard;
136 };
137 /** Allocator specific memory - variable size. */
138 uint8_t abAlloc[1];
139} PDMMEDIAEXIOREQINT;
140/** Pointer to a VD I/O request. */
141typedef PDMMEDIAEXIOREQINT *PPDMMEDIAEXIOREQINT;
142
143/**
144 * Structure for holding a list of allocated requests.
145 */
146typedef struct VDLSTIOREQALLOC
147{
148 /** Mutex protecting the table of allocated requests. */
149 RTSEMFASTMUTEX hMtxLstIoReqAlloc;
150 /** List anchor. */
151 RTLISTANCHOR LstIoReqAlloc;
152} VDLSTIOREQALLOC;
153typedef VDLSTIOREQALLOC *PVDLSTIOREQALLOC;
154
155/** Number of bins for allocated requests. */
156#define DRVVD_VDIOREQ_ALLOC_BINS 8
157
158/**
159 * Disk integrity driver instance data.
160 *
161 * @implements PDMIMEDIA
162 */
163typedef struct DRVRAMDISK
164{
165 /** Pointer driver instance. */
166 PPDMDRVINS pDrvIns;
167 /** Pointer to the media driver below us.
168 * This is NULL if the media is not mounted. */
169 PPDMIMEDIA pDrvMedia;
170 /** Our media interface */
171 PDMIMEDIA IMedia;
172
173 /** The media port interface above. */
174 PPDMIMEDIAPORT pDrvMediaPort;
175 /** Media port interface */
176 PDMIMEDIAPORT IMediaPort;
177
178 /** Flag whether the RAM disk was pre allocated. */
179 bool fPreallocRamDisk;
180 /** Flag whether to report a non totating medium. */
181 bool fNonRotational;
182 /** AVL tree containing the disk blocks to check. */
183 PAVLRFOFFTREE pTreeSegments;
184 /** Size of the disk. */
185 uint64_t cbDisk;
186 /** Size of one sector. */
187 uint32_t cbSector;
188
189 /** Worker request queue. */
190 RTREQQUEUE hReqQ;
191 /** Worker thread for async requests. */
192 RTTHREAD hThrdWrk;
193
194 /** @name IMEDIAEX interface support specific members.
195 * @{ */
196 /** Pointer to the IMEDIAEXPORT interface above us. */
197 PPDMIMEDIAEXPORT pDrvMediaExPort;
198 /** Our extended media interface. */
199 PDMIMEDIAEX IMediaEx;
200 /** Memory cache for the I/O requests. */
201 RTMEMCACHE hIoReqCache;
202 /** I/O buffer manager. */
203 IOBUFMGR hIoBufMgr;
204 /** Active request counter. */
205 volatile uint32_t cIoReqsActive;
206 /** Bins for allocated requests. */
207 VDLSTIOREQALLOC aIoReqAllocBins[DRVVD_VDIOREQ_ALLOC_BINS];
208 /** List of requests for I/O memory to be available - VDIOREQ::NdLstWait. */
209 RTLISTANCHOR LstIoReqIoBufWait;
210 /** Critical section protecting the list of requests waiting for I/O memory. */
211 RTCRITSECT CritSectIoReqsIoBufWait;
212 /** Number of requests waiting for a I/O buffer. */
213 volatile uint32_t cIoReqsWaiting;
214 /** Flag whether we have to resubmit requests on resume because the
215 * VM was suspended due to a recoverable I/O error.
216 */
217 volatile bool fRedo;
218 /** List of requests we have to redo. */
219 RTLISTANCHOR LstIoReqRedo;
220 /** Criticial section protecting the list of waiting requests. */
221 RTCRITSECT CritSectIoReqRedo;
222 /** Number of errors logged so far. */
223 unsigned cErrors;
224 /** @} */
225
226} DRVRAMDISK;
227
228
229static void drvramdiskMediaExIoReqComplete(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq,
230 int rcReq);
231
232/**
233 * Record a successful write to the virtual disk.
234 *
235 * @returns VBox status code.
236 * @param pThis Disk integrity driver instance data.
237 * @param pSgBuf The S/G buffer holding the data to write.
238 * @param off Start offset.
239 * @param cbWrite Number of bytes to record.
240 */
241static int drvramdiskWriteWorker(PDRVRAMDISK pThis, PRTSGBUF pSgBuf,
242 uint64_t off, size_t cbWrite)
243{
244 int rc = VINF_SUCCESS;
245
246 LogFlowFunc(("pThis=%#p pSgBuf=%#p off=%llx cbWrite=%u\n",
247 pThis, pSgBuf, off, cbWrite));
248
249 /* Update the segments */
250 size_t cbLeft = cbWrite;
251 RTFOFF offCurr = (RTFOFF)off;
252
253 while (cbLeft)
254 {
255 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
256 size_t cbRange = 0;
257 bool fSet = false;
258 unsigned offSeg = 0;
259
260 if (!pSeg)
261 {
262 /* Get next segment */
263 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
264 if ( !pSeg
265 || offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
266 cbRange = cbLeft;
267 else
268 cbRange = pSeg->Core.Key - offCurr;
269
270 Assert(cbRange % 512 == 0);
271
272 /* Create new segment */
273 pSeg = (PDRVDISKSEGMENT)RTMemAllocZ(sizeof(DRVDISKSEGMENT));
274 if (pSeg)
275 {
276 pSeg->Core.Key = offCurr;
277 pSeg->Core.KeyLast = offCurr + (RTFOFF)cbRange - 1;
278 pSeg->cbSeg = cbRange;
279 pSeg->pbSeg = (uint8_t *)RTMemAllocZ(cbRange);
280 if (!pSeg->pbSeg)
281 RTMemFree(pSeg);
282 else
283 {
284 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
285 AssertMsg(fInserted, ("Bug!\n")); RT_NOREF(fInserted);
286 fSet = true;
287 }
288 }
289 }
290 else
291 {
292 fSet = true;
293 offSeg = offCurr - pSeg->Core.Key;
294 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
295 }
296
297 if (fSet)
298 {
299 AssertPtr(pSeg);
300 size_t cbCopied = RTSgBufCopyToBuf(pSgBuf, pSeg->pbSeg + offSeg, cbRange);
301 Assert(cbCopied == cbRange); RT_NOREF(cbCopied);
302 }
303 else
304 RTSgBufAdvance(pSgBuf, cbRange);
305
306 offCurr += cbRange;
307 cbLeft -= cbRange;
308 }
309
310 return rc;
311}
312
313/**
314 * Read data from the ram disk.
315 *
316 * @returns VBox status code.
317 * @param pThis RAM disk driver instance data.
318 * @param pSgBuf The S/G buffer to store the data.
319 * @param off Start offset.
320 * @param cbRead Number of bytes to read.
321 */
322static int drvramdiskReadWorker(PDRVRAMDISK pThis, PRTSGBUF pSgBuf,
323 uint64_t off, size_t cbRead)
324{
325 int rc = VINF_SUCCESS;
326
327 LogFlowFunc(("pThis=%#p pSgBuf=%#p off=%llx cbRead=%u\n",
328 pThis, pSgBuf, off, cbRead));
329
330 Assert(off % 512 == 0);
331 Assert(cbRead % 512 == 0);
332
333 /* Compare read data */
334 size_t cbLeft = cbRead;
335 RTFOFF offCurr = (RTFOFF)off;
336
337 while (cbLeft)
338 {
339 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offCurr);
340 size_t cbRange = 0;
341 bool fCmp = false;
342 unsigned offSeg = 0;
343
344 if (!pSeg)
345 {
346 /* Get next segment */
347 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offCurr, true);
348 if ( !pSeg
349 || offCurr + (RTFOFF)cbLeft <= pSeg->Core.Key)
350 cbRange = cbLeft;
351 else
352 cbRange = pSeg->Core.Key - offCurr;
353
354 /* No segment means everything should be 0 for this part. */
355 RTSgBufSet(pSgBuf, 0, cbRange);
356 }
357 else
358 {
359 fCmp = true;
360 offSeg = offCurr - pSeg->Core.Key;
361 cbRange = RT_MIN(cbLeft, (size_t)(pSeg->Core.KeyLast + 1 - offCurr));
362
363 RTSGSEG Seg;
364 RTSGBUF SgBufSrc;
365
366 Seg.cbSeg = cbRange;
367 Seg.pvSeg = pSeg->pbSeg + offSeg;
368
369 RTSgBufInit(&SgBufSrc, &Seg, 1);
370 RTSgBufCopy(pSgBuf, &SgBufSrc, cbRange);
371 }
372
373 offCurr += cbRange;
374 cbLeft -= cbRange;
375 }
376
377 return rc;
378}
379
380/**
381 * Discards the given ranges from the disk.
382 *
383 * @returns VBox status code.
384 * @param pThis Disk integrity driver instance data.
385 * @param paRanges Array of ranges to discard.
386 * @param cRanges Number of ranges in the array.
387 */
388static int drvramdiskDiscardRecords(PDRVRAMDISK pThis, PCRTRANGE paRanges, unsigned cRanges)
389{
390 int rc = VINF_SUCCESS;
391
392 LogFlowFunc(("pThis=%#p paRanges=%#p cRanges=%u\n", pThis, paRanges, cRanges));
393
394 for (unsigned i = 0; i < cRanges; i++)
395 {
396 uint64_t offStart = paRanges[i].offStart;
397 size_t cbLeft = paRanges[i].cbRange;
398
399 LogFlowFunc(("Discarding off=%llu cbRange=%zu\n", offStart, cbLeft));
400
401 while (cbLeft)
402 {
403 size_t cbRange;
404 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetRangeGet(pThis->pTreeSegments, offStart);
405
406 if (!pSeg)
407 {
408 /* Get next segment */
409 pSeg = (PDRVDISKSEGMENT)RTAvlrFileOffsetGetBestFit(pThis->pTreeSegments, offStart, true);
410 if ( !pSeg
411 || (RTFOFF)offStart + (RTFOFF)cbLeft <= pSeg->Core.Key)
412 cbRange = cbLeft;
413 else
414 cbRange = pSeg->Core.Key - offStart;
415
416 Assert(!(cbRange % 512));
417 }
418 else
419 {
420 size_t cbPreLeft, cbPostLeft;
421
422 cbRange = RT_MIN(cbLeft, pSeg->Core.KeyLast - offStart + 1);
423 cbPreLeft = offStart - pSeg->Core.Key;
424 cbPostLeft = pSeg->cbSeg - cbRange - cbPreLeft;
425
426 Assert(!(cbRange % 512));
427 Assert(!(cbPreLeft % 512));
428 Assert(!(cbPostLeft % 512));
429
430 LogFlowFunc(("cbRange=%zu cbPreLeft=%zu cbPostLeft=%zu\n",
431 cbRange, cbPreLeft, cbPostLeft));
432
433 RTAvlrFileOffsetRemove(pThis->pTreeSegments, pSeg->Core.Key);
434
435 if (!cbPreLeft && !cbPostLeft)
436 {
437 /* Just free the whole segment. */
438 LogFlowFunc(("Freeing whole segment pSeg=%#p\n", pSeg));
439 RTMemFree(pSeg->pbSeg);
440 RTMemFree(pSeg);
441 }
442 else if (cbPreLeft && !cbPostLeft)
443 {
444 /* Realloc to new size and insert. */
445 LogFlowFunc(("Realloc segment pSeg=%#p\n", pSeg));
446 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPreLeft);
447 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, sizeof(DRVDISKSEGMENT));
448 pSeg->Core.KeyLast = pSeg->Core.Key + cbPreLeft - 1;
449 pSeg->cbSeg = cbPreLeft;
450 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
451 Assert(fInserted); RT_NOREF(fInserted);
452 }
453 else if (!cbPreLeft && cbPostLeft)
454 {
455 /* Move data to the front and realloc. */
456 LogFlowFunc(("Move data and realloc segment pSeg=%#p\n", pSeg));
457 memmove(pSeg->pbSeg, pSeg->pbSeg + cbRange, cbPostLeft);
458 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, sizeof(DRVDISKSEGMENT));
459 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPostLeft);
460 pSeg->Core.Key += cbRange;
461 pSeg->cbSeg = cbPostLeft;
462 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
463 Assert(fInserted); RT_NOREF(fInserted);
464 }
465 else
466 {
467 /* Split the segment into 2 new segments. */
468 LogFlowFunc(("Split segment pSeg=%#p\n", pSeg));
469 PDRVDISKSEGMENT pSegPost = (PDRVDISKSEGMENT)RTMemAllocZ(sizeof(DRVDISKSEGMENT));
470 if (pSegPost)
471 {
472 pSegPost->Core.Key = pSeg->Core.Key + cbPreLeft + cbRange;
473 pSegPost->Core.KeyLast = pSeg->Core.KeyLast;
474 pSegPost->cbSeg = cbPostLeft;
475 pSegPost->pbSeg = (uint8_t *)RTMemAllocZ(cbPostLeft);
476 if (!pSegPost->pbSeg)
477 RTMemFree(pSegPost);
478 else
479 {
480 memcpy(pSegPost->pbSeg, pSeg->pbSeg + cbPreLeft + cbRange, cbPostLeft);
481 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSegPost->Core);
482 Assert(fInserted); RT_NOREF(fInserted);
483 }
484 }
485
486 /* Shrink the current segment. */
487 pSeg->pbSeg = (uint8_t *)RTMemRealloc(pSeg->pbSeg, cbPreLeft);
488 pSeg = (PDRVDISKSEGMENT)RTMemRealloc(pSeg, sizeof(DRVDISKSEGMENT));
489 pSeg->Core.KeyLast = pSeg->Core.Key + cbPreLeft - 1;
490 pSeg->cbSeg = cbPreLeft;
491 bool fInserted = RTAvlrFileOffsetInsert(pThis->pTreeSegments, &pSeg->Core);
492 Assert(fInserted); RT_NOREF(fInserted);
493 } /* if (cbPreLeft && cbPostLeft) */
494 }
495
496 offStart += cbRange;
497 cbLeft -= cbRange;
498 }
499 }
500
501 LogFlowFunc(("returns rc=%Rrc\n", rc));
502 return rc;
503}
504
505/* -=-=-=-=- IMedia -=-=-=-=- */
506
507
508/*********************************************************************************************************************************
509* Media interface methods *
510*********************************************************************************************************************************/
511
512/** @copydoc PDMIMEDIA::pfnRead */
513static DECLCALLBACK(int) drvramdiskRead(PPDMIMEDIA pInterface,
514 uint64_t off, void *pvBuf, size_t cbRead)
515{
516 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMedia);
517 RTSGSEG Seg;
518 RTSGBUF SgBuf;
519
520 Seg.cbSeg = cbRead;
521 Seg.pvSeg = pvBuf;
522 RTSgBufInit(&SgBuf, &Seg, 1);
523 return drvramdiskReadWorker(pThis, &SgBuf, off, cbRead);
524}
525
526/** @copydoc PDMIMEDIA::pfnWrite */
527static DECLCALLBACK(int) drvramdiskWrite(PPDMIMEDIA pInterface,
528 uint64_t off, const void *pvBuf,
529 size_t cbWrite)
530{
531 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMedia);
532 RTSGSEG Seg;
533 RTSGBUF SgBuf;
534
535 Seg.cbSeg = cbWrite;
536 Seg.pvSeg = (void *)pvBuf;
537 RTSgBufInit(&SgBuf, &Seg, 1);
538 return drvramdiskWriteWorker(pThis, &SgBuf, off, cbWrite);
539}
540
541/** @copydoc PDMIMEDIA::pfnFlush */
542static DECLCALLBACK(int) drvramdiskFlush(PPDMIMEDIA pInterface)
543{
544 RT_NOREF1(pInterface);
545 /* Nothing to do here. */
546 return VINF_SUCCESS;
547}
548
549/** @copydoc PDMIMEDIA::pfnGetSize */
550static DECLCALLBACK(uint64_t) drvramdiskGetSize(PPDMIMEDIA pInterface)
551{
552 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMedia);
553 return pThis->cbDisk;
554}
555
556/** @interface_method_impl{PDMIMEDIA,pfnBiosIsVisible} */
557static DECLCALLBACK(bool) drvramdiskBiosIsVisible(PPDMIMEDIA pInterface)
558{
559 RT_NOREF1(pInterface);
560 return false;
561}
562
563/** @copydoc PDMIMEDIA::pfnGetType */
564static DECLCALLBACK(PDMMEDIATYPE) drvramdiskGetType(PPDMIMEDIA pInterface)
565{
566 RT_NOREF1(pInterface);
567 return PDMMEDIATYPE_HARD_DISK;
568}
569
570/** @copydoc PDMIMEDIA::pfnIsReadOnly */
571static DECLCALLBACK(bool) drvramdiskIsReadOnly(PPDMIMEDIA pInterface)
572{
573 RT_NOREF1(pInterface);
574 return false; /** @todo */
575}
576
577/** @copydoc PDMIMEDIA::pfnBiosGetPCHSGeometry */
578static DECLCALLBACK(int) drvramdiskBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
579 PPDMMEDIAGEOMETRY pPCHSGeometry)
580{
581 RT_NOREF2(pInterface, pPCHSGeometry);
582 return VERR_NOT_IMPLEMENTED;
583}
584
585/** @copydoc PDMIMEDIA::pfnBiosSetPCHSGeometry */
586static DECLCALLBACK(int) drvramdiskBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
587 PCPDMMEDIAGEOMETRY pPCHSGeometry)
588{
589 RT_NOREF2(pInterface, pPCHSGeometry);
590 return VERR_NOT_IMPLEMENTED;
591}
592
593/** @copydoc PDMIMEDIA::pfnBiosGetLCHSGeometry */
594static DECLCALLBACK(int) drvramdiskBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
595 PPDMMEDIAGEOMETRY pLCHSGeometry)
596{
597 RT_NOREF2(pInterface, pLCHSGeometry);
598 return VERR_NOT_IMPLEMENTED;
599}
600
601/** @copydoc PDMIMEDIA::pfnBiosSetLCHSGeometry */
602static DECLCALLBACK(int) drvramdiskBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
603 PCPDMMEDIAGEOMETRY pLCHSGeometry)
604{
605 RT_NOREF2(pInterface, pLCHSGeometry);
606 return VERR_NOT_IMPLEMENTED;
607}
608
609/** @copydoc PDMIMEDIA::pfnGetUuid */
610static DECLCALLBACK(int) drvramdiskGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
611{
612 RT_NOREF1(pInterface);
613 return RTUuidClear(pUuid);
614}
615
616/** @copydoc PDMIMEDIA::pfnGetSectorSize */
617static DECLCALLBACK(uint32_t) drvramdiskGetSectorSize(PPDMIMEDIA pInterface)
618{
619 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMedia);
620 return pThis->cbSector;
621}
622
623/** @copydoc PDMIMEDIA::pfnDiscard */
624static DECLCALLBACK(int) drvramdiskDiscard(PPDMIMEDIA pInterface, PCRTRANGE paRanges, unsigned cRanges)
625{
626 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMedia);
627 return drvramdiskDiscardRecords(pThis, paRanges, cRanges);
628}
629
630/** @copydoc PDMIMEDIA::pfnReadPcBios */
631static DECLCALLBACK(int) drvramdiskReadPcBios(PPDMIMEDIA pInterface,
632 uint64_t off, void *pvBuf, size_t cbRead)
633{
634 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMedia);
635 RTSGSEG Seg;
636 RTSGBUF SgBuf;
637
638 Seg.cbSeg = cbRead;
639 Seg.pvSeg = pvBuf;
640 RTSgBufInit(&SgBuf, &Seg, 1);
641 return drvramdiskReadWorker(pThis, &SgBuf, off, cbRead);
642}
643
644/** @interface_method_impl{PDMIMEDIA,pfnIsNonRotational} */
645static DECLCALLBACK(bool) drvramdiskIsNonRotational(PPDMIMEDIA pInterface)
646{
647 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMedia);
648 return pThis->fNonRotational;
649}
650
651
652/*********************************************************************************************************************************
653* Extended media interface methods *
654*********************************************************************************************************************************/
655
656static void drvramdiskMediaExIoReqWarningOutOfMemory(PPDMDRVINS pDrvIns)
657{
658 int rc;
659 LogRel(("RamDisk#%u: Out of memory\n", pDrvIns->iInstance));
660 rc = PDMDrvHlpVMSetRuntimeError(pDrvIns, VMSETRTERR_FLAGS_SUSPEND | VMSETRTERR_FLAGS_NO_WAIT, "DrvRamDisk_OOM",
661 N_("There is not enough free memory for the ramdisk"));
662 AssertRC(rc);
663}
664
665/**
666 * Checks whether a given status code indicates a recoverable error
667 * suspending the VM if it is.
668 *
669 * @returns Flag indicating whether the status code is a recoverable error
670 * (full disk, broken network connection).
671 * @param pThis VBox disk container instance data.
672 * @param rc Status code to check.
673 */
674bool drvramdiskMediaExIoReqIsRedoSetWarning(PDRVRAMDISK pThis, int rc)
675{
676 if (rc == VERR_NO_MEMORY)
677 {
678 if (ASMAtomicCmpXchgBool(&pThis->fRedo, true, false))
679 drvramdiskMediaExIoReqWarningOutOfMemory(pThis->pDrvIns);
680 return true;
681 }
682
683 return false;
684}
685
686/**
687 * Syncs the memory buffers between the I/O request allocator and the internal buffer.
688 *
689 * @returns VBox status code.
690 * @param pThis VBox disk container instance data.
691 * @param pIoReq I/O request to sync.
692 * @param fToIoBuf Flag indicating the sync direction.
693 * true to copy data from the allocators buffer to our internal buffer.
694 * false for the other direction.
695 */
696DECLINLINE(int) drvramdiskMediaExIoReqBufSync(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq, bool fToIoBuf)
697{
698 int rc = VINF_SUCCESS;
699
700 Assert(pIoReq->enmType == PDMMEDIAEXIOREQTYPE_READ || pIoReq->enmType == PDMMEDIAEXIOREQTYPE_WRITE);
701
702 /* Make sure the buffer is reset. */
703 RTSgBufReset(&pIoReq->ReadWrite.IoBuf.SgBuf);
704
705 if (fToIoBuf)
706 rc = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, pIoReq, &pIoReq->abAlloc[0],
707 (uint32_t)(pIoReq->ReadWrite.cbReq - pIoReq->ReadWrite.cbReqLeft),
708 &pIoReq->ReadWrite.IoBuf.SgBuf,
709 RT_MIN(pIoReq->ReadWrite.cbIoBuf, pIoReq->ReadWrite.cbReqLeft));
710 else
711 rc = pThis->pDrvMediaExPort->pfnIoReqCopyFromBuf(pThis->pDrvMediaExPort, pIoReq, &pIoReq->abAlloc[0],
712 (uint32_t)(pIoReq->ReadWrite.cbReq - pIoReq->ReadWrite.cbReqLeft),
713 &pIoReq->ReadWrite.IoBuf.SgBuf,
714 RT_MIN(pIoReq->ReadWrite.cbIoBuf, pIoReq->ReadWrite.cbReqLeft));
715
716 RTSgBufReset(&pIoReq->ReadWrite.IoBuf.SgBuf);
717 return rc;
718}
719
720/**
721 * Hashes the I/O request ID to an index for the allocated I/O request bin.
722 */
723DECLINLINE(unsigned) drvramdiskMediaExIoReqIdHash(PDMMEDIAEXIOREQID uIoReqId)
724{
725 return uIoReqId % DRVVD_VDIOREQ_ALLOC_BINS; /** @todo Find something better? */
726}
727
728/**
729 * Inserts the given I/O request in to the list of allocated I/O requests.
730 *
731 * @returns VBox status code.
732 * @param pThis VBox disk container instance data.
733 * @param pIoReq I/O request to insert.
734 */
735static int drvramdiskMediaExIoReqInsert(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq)
736{
737 int rc = VINF_SUCCESS;
738 unsigned idxBin = drvramdiskMediaExIoReqIdHash(pIoReq->uIoReqId);
739
740 rc = RTSemFastMutexRequest(pThis->aIoReqAllocBins[idxBin].hMtxLstIoReqAlloc);
741 if (RT_SUCCESS(rc))
742 {
743 /* Search for conflicting I/O request ID. */
744 PPDMMEDIAEXIOREQINT pIt;
745 RTListForEach(&pThis->aIoReqAllocBins[idxBin].LstIoReqAlloc, pIt, PDMMEDIAEXIOREQINT, NdAllocatedList)
746 {
747 if (RT_UNLIKELY(pIt->uIoReqId == pIoReq->uIoReqId))
748 {
749 rc = VERR_PDM_MEDIAEX_IOREQID_CONFLICT;
750 break;
751 }
752 }
753 if (RT_SUCCESS(rc))
754 RTListAppend(&pThis->aIoReqAllocBins[idxBin].LstIoReqAlloc, &pIoReq->NdAllocatedList);
755 RTSemFastMutexRelease(pThis->aIoReqAllocBins[idxBin].hMtxLstIoReqAlloc);
756 }
757
758 return rc;
759}
760
761/**
762 * Removes the given I/O request from the list of allocated I/O requests.
763 *
764 * @returns VBox status code.
765 * @param pThis VBox disk container instance data.
766 * @param pIoReq I/O request to insert.
767 */
768static int drvramdiskMediaExIoReqRemove(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq)
769{
770 int rc = VINF_SUCCESS;
771 unsigned idxBin = drvramdiskMediaExIoReqIdHash(pIoReq->uIoReqId);
772
773 rc = RTSemFastMutexRequest(pThis->aIoReqAllocBins[idxBin].hMtxLstIoReqAlloc);
774 if (RT_SUCCESS(rc))
775 {
776 RTListNodeRemove(&pIoReq->NdAllocatedList);
777 RTSemFastMutexRelease(pThis->aIoReqAllocBins[idxBin].hMtxLstIoReqAlloc);
778 }
779
780 return rc;
781}
782
783/**
784 * I/O request completion worker.
785 *
786 * @returns VBox status code.
787 * @param pThis VBox disk container instance data.
788 * @param pIoReq I/O request to complete.
789 * @param rcReq The status code the request completed with.
790 * @param fUpNotify Flag whether to notify the driver/device above us about the completion.
791 */
792static int drvramdiskMediaExIoReqCompleteWorker(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq, int rcReq, bool fUpNotify)
793{
794 int rc;
795 bool fXchg = ASMAtomicCmpXchgU32((volatile uint32_t *)&pIoReq->enmState, VDIOREQSTATE_COMPLETING, VDIOREQSTATE_ACTIVE);
796 if (fXchg)
797 ASMAtomicDecU32(&pThis->cIoReqsActive);
798 else
799 {
800 Assert(pIoReq->enmState == VDIOREQSTATE_CANCELED);
801 rcReq = VERR_PDM_MEDIAEX_IOREQ_CANCELED;
802 }
803
804 ASMAtomicXchgU32((volatile uint32_t *)&pIoReq->enmState, VDIOREQSTATE_COMPLETED);
805
806 /*
807 * Leave a release log entry if the request was active for more than 25 seconds
808 * (30 seconds is the timeout of the guest).
809 */
810 uint64_t tsNow = RTTimeMilliTS();
811 if (tsNow - pIoReq->tsSubmit >= 25 * 1000)
812 {
813 const char *pcszReq = NULL;
814
815 switch (pIoReq->enmType)
816 {
817 case PDMMEDIAEXIOREQTYPE_READ:
818 pcszReq = "Read";
819 break;
820 case PDMMEDIAEXIOREQTYPE_WRITE:
821 pcszReq = "Write";
822 break;
823 case PDMMEDIAEXIOREQTYPE_FLUSH:
824 pcszReq = "Flush";
825 break;
826 case PDMMEDIAEXIOREQTYPE_DISCARD:
827 pcszReq = "Discard";
828 break;
829 default:
830 pcszReq = "<Invalid>";
831 }
832
833 LogRel(("RamDisk#%u: %s request was active for %llu seconds\n",
834 pThis->pDrvIns->iInstance, pcszReq, (tsNow - pIoReq->tsSubmit) / 1000));
835 }
836
837 if (RT_FAILURE(rcReq))
838 {
839 /* Log the error. */
840 if (pThis->cErrors++ < 100)
841 {
842 if (rcReq == VERR_PDM_MEDIAEX_IOREQ_CANCELED)
843 {
844 if (pIoReq->enmType == PDMMEDIAEXIOREQTYPE_FLUSH)
845 LogRel(("RamDisk#%u: Aborted flush returned rc=%Rrc\n",
846 pThis->pDrvIns->iInstance, rcReq));
847 else
848 LogRel(("RamDisk#%u: Aborted %s (%u bytes left) returned rc=%Rrc\n",
849 pThis->pDrvIns->iInstance,
850 pIoReq->enmType == PDMMEDIAEXIOREQTYPE_READ
851 ? "read"
852 : "write",
853 pIoReq->ReadWrite.cbReqLeft, rcReq));
854 }
855 else
856 {
857 if (pIoReq->enmType == PDMMEDIAEXIOREQTYPE_FLUSH)
858 LogRel(("RamDisk#%u: Flush returned rc=%Rrc\n",
859 pThis->pDrvIns->iInstance, rcReq));
860 else
861 LogRel(("RamDisk#%u: %s (%u bytes left) returned rc=%Rrc\n",
862 pThis->pDrvIns->iInstance,
863 pIoReq->enmType == PDMMEDIAEXIOREQTYPE_READ
864 ? "Read"
865 : "Write",
866 pIoReq->ReadWrite.cbReqLeft, rcReq));
867 }
868 }
869 }
870
871 if (fUpNotify)
872 {
873 rc = pThis->pDrvMediaExPort->pfnIoReqCompleteNotify(pThis->pDrvMediaExPort,
874 pIoReq, &pIoReq->abAlloc[0], rcReq);
875 AssertRC(rc);
876 }
877
878 return rcReq;
879}
880
881/**
882 * Allocates a memory buffer suitable for I/O for the given request.
883 *
884 * @returns VBox status code.
885 * @retval VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS if there is no I/O memory available to allocate and
886 * the request was placed on a waiting list.
887 * @param pThis VBox disk container instance data.
888 * @param pIoReq I/O request to allocate memory for.
889 * @param cb Size of the buffer.
890 */
891DECLINLINE(int) drvramdiskMediaExIoReqBufAlloc(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq, size_t cb)
892{
893 int rc = IOBUFMgrAllocBuf(pThis->hIoBufMgr, &pIoReq->ReadWrite.IoBuf, cb, &pIoReq->ReadWrite.cbIoBuf);
894 if (rc == VERR_NO_MEMORY)
895 {
896 RTCritSectEnter(&pThis->CritSectIoReqsIoBufWait);
897 RTListAppend(&pThis->LstIoReqIoBufWait, &pIoReq->NdLstWait);
898 RTCritSectLeave(&pThis->CritSectIoReqsIoBufWait);
899 ASMAtomicIncU32(&pThis->cIoReqsWaiting);
900 rc = VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS;
901 }
902
903 return rc;
904}
905
906/**
907 * Worker for a read request.
908 *
909 * @returns VBox status code.
910 * @param pThis RAM disk container instance data.
911 * @param pIoReq The read request.
912 */
913static DECLCALLBACK(int) drvramdiskIoReqReadWorker(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq)
914{
915 size_t cbReqIo = RT_MIN(pIoReq->ReadWrite.cbReqLeft, pIoReq->ReadWrite.cbIoBuf);
916 int rc = drvramdiskReadWorker(pThis, &pIoReq->ReadWrite.IoBuf.SgBuf, pIoReq->ReadWrite.offStart,
917 cbReqIo);
918 drvramdiskMediaExIoReqComplete(pThis, pIoReq, rc);
919 return VINF_SUCCESS;
920}
921
922/**
923 * Worker for a read request.
924 *
925 * @returns VBox status code.
926 * @param pThis RAM disk container instance data.
927 * @param pIoReq The read request.
928 */
929static DECLCALLBACK(int) drvramdiskIoReqWriteWorker(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq)
930{
931 size_t cbReqIo = RT_MIN(pIoReq->ReadWrite.cbReqLeft, pIoReq->ReadWrite.cbIoBuf);
932 int rc = drvramdiskWriteWorker(pThis, &pIoReq->ReadWrite.IoBuf.SgBuf, pIoReq->ReadWrite.offStart,
933 cbReqIo);
934 drvramdiskMediaExIoReqComplete(pThis, pIoReq, rc);
935 return VINF_SUCCESS;
936}
937
938/**
939 * Processes a read/write request.
940 *
941 * @returns VBox status code.
942 * @param pThis VBox disk container instance data.
943 * @param pIoReq I/O request to process.
944 * @param fUpNotify Flag whether to notify the driver/device above us about the completion.
945 */
946static int drvramdiskMediaExIoReqReadWriteProcess(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq, bool fUpNotify)
947{
948 int rc = VINF_SUCCESS;
949
950 Assert(pIoReq->enmType == PDMMEDIAEXIOREQTYPE_READ || pIoReq->enmType == PDMMEDIAEXIOREQTYPE_WRITE);
951
952 while ( pIoReq->ReadWrite.cbReqLeft
953 && rc == VINF_SUCCESS)
954 {
955 if (pIoReq->enmType == PDMMEDIAEXIOREQTYPE_READ)
956 rc = RTReqQueueCallEx(pThis->hReqQ, NULL, 0, RTREQFLAGS_NO_WAIT,
957 (PFNRT)drvramdiskIoReqReadWorker, 2, pThis, pIoReq);
958 else
959 {
960 /* Sync memory buffer from the request initiator. */
961 rc = drvramdiskMediaExIoReqBufSync(pThis, pIoReq, true /* fToIoBuf */);
962 if (RT_SUCCESS(rc))
963 rc = RTReqQueueCallEx(pThis->hReqQ, NULL, 0, RTREQFLAGS_NO_WAIT,
964 (PFNRT)drvramdiskIoReqWriteWorker, 2, pThis, pIoReq);
965 }
966
967 if (rc == VINF_SUCCESS)
968 rc = VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS;
969 }
970
971 if (rc != VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS)
972 {
973 Assert(!pIoReq->ReadWrite.cbReqLeft || RT_FAILURE(rc));
974 rc = drvramdiskMediaExIoReqCompleteWorker(pThis, pIoReq, rc, fUpNotify);
975 }
976
977 return rc;
978}
979
980
981/**
982 * Frees a I/O memory buffer allocated previously.
983 *
984 * @returns nothing.
985 * @param pThis VBox disk container instance data.
986 * @param pIoReq I/O request for which to free memory.
987 */
988DECLINLINE(void) drvramdiskMediaExIoReqBufFree(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq)
989{
990 if ( pIoReq->enmType == PDMMEDIAEXIOREQTYPE_READ
991 || pIoReq->enmType == PDMMEDIAEXIOREQTYPE_WRITE)
992 {
993 IOBUFMgrFreeBuf(&pIoReq->ReadWrite.IoBuf);
994
995 if (ASMAtomicReadU32(&pThis->cIoReqsWaiting) > 0)
996 {
997 /* Try to process as many requests as possible. */
998 RTCritSectEnter(&pThis->CritSectIoReqsIoBufWait);
999 PPDMMEDIAEXIOREQINT pIoReqCur, pIoReqNext;
1000
1001 RTListForEachSafe(&pThis->LstIoReqIoBufWait, pIoReqCur, pIoReqNext, PDMMEDIAEXIOREQINT, NdLstWait)
1002 {
1003 /* Allocate a suitable I/O buffer for this request. */
1004 int rc = IOBUFMgrAllocBuf(pThis->hIoBufMgr, &pIoReqCur->ReadWrite.IoBuf, pIoReqCur->ReadWrite.cbReq,
1005 &pIoReqCur->ReadWrite.cbIoBuf);
1006 if (rc == VINF_SUCCESS)
1007 {
1008 ASMAtomicDecU32(&pThis->cIoReqsWaiting);
1009 RTListNodeRemove(&pIoReqCur->NdLstWait);
1010
1011 bool fXchg = ASMAtomicCmpXchgU32((volatile uint32_t *)&pIoReqCur->enmState, VDIOREQSTATE_ACTIVE, VDIOREQSTATE_ALLOCATED);
1012 if (RT_UNLIKELY(!fXchg))
1013 {
1014 /* Must have been canceled inbetween. */
1015 Assert(pIoReq->enmState == VDIOREQSTATE_CANCELED);
1016 drvramdiskMediaExIoReqCompleteWorker(pThis, pIoReqCur, VERR_PDM_MEDIAEX_IOREQ_CANCELED, true /* fUpNotify */);
1017 }
1018 ASMAtomicIncU32(&pThis->cIoReqsActive);
1019 rc = drvramdiskMediaExIoReqReadWriteProcess(pThis, pIoReqCur, true /* fUpNotify */);
1020 }
1021 else
1022 {
1023 Assert(rc == VERR_NO_MEMORY);
1024 break;
1025 }
1026 }
1027 RTCritSectLeave(&pThis->CritSectIoReqsIoBufWait);
1028 }
1029 }
1030}
1031
1032
1033/**
1034 * Returns whether the VM is in a running state.
1035 *
1036 * @returns Flag indicating whether the VM is currently in a running state.
1037 * @param pThis VBox disk container instance data.
1038 */
1039DECLINLINE(bool) drvramdiskMediaExIoReqIsVmRunning(PDRVRAMDISK pThis)
1040{
1041 VMSTATE enmVmState = PDMDrvHlpVMState(pThis->pDrvIns);
1042 if ( enmVmState == VMSTATE_RESUMING
1043 || enmVmState == VMSTATE_RUNNING
1044 || enmVmState == VMSTATE_RUNNING_LS
1045 || enmVmState == VMSTATE_RUNNING_FT
1046 || enmVmState == VMSTATE_RESETTING
1047 || enmVmState == VMSTATE_RESETTING_LS
1048 || enmVmState == VMSTATE_SOFT_RESETTING
1049 || enmVmState == VMSTATE_SOFT_RESETTING_LS
1050 || enmVmState == VMSTATE_SUSPENDING
1051 || enmVmState == VMSTATE_SUSPENDING_LS
1052 || enmVmState == VMSTATE_SUSPENDING_EXT_LS)
1053 return true;
1054
1055 return false;
1056}
1057
1058/**
1059 * @copydoc FNVDASYNCTRANSFERCOMPLETE
1060 */
1061static void drvramdiskMediaExIoReqComplete(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq,
1062 int rcReq)
1063{
1064 /*
1065 * For a read we need to sync the memory before continuing to process
1066 * the request further.
1067 */
1068 if ( RT_SUCCESS(rcReq)
1069 && pIoReq->enmType == PDMMEDIAEXIOREQTYPE_READ)
1070 rcReq = drvramdiskMediaExIoReqBufSync(pThis, pIoReq, false /* fToIoBuf */);
1071
1072 /*
1073 * When the request owner instructs us to handle recoverable errors like full disks
1074 * do it. Mark the request as suspended, notify the owner and put the request on the
1075 * redo list.
1076 */
1077 if ( RT_FAILURE(rcReq)
1078 && (pIoReq->fFlags & PDMIMEDIAEX_F_SUSPEND_ON_RECOVERABLE_ERR)
1079 && drvramdiskMediaExIoReqIsRedoSetWarning(pThis, rcReq))
1080 {
1081 bool fXchg = ASMAtomicCmpXchgU32((volatile uint32_t *)&pIoReq->enmState, VDIOREQSTATE_SUSPENDED, VDIOREQSTATE_ACTIVE);
1082 if (fXchg)
1083 {
1084 /* Put on redo list and adjust active request counter. */
1085 RTCritSectEnter(&pThis->CritSectIoReqRedo);
1086 RTListAppend(&pThis->LstIoReqRedo, &pIoReq->NdLstWait);
1087 RTCritSectLeave(&pThis->CritSectIoReqRedo);
1088 ASMAtomicDecU32(&pThis->cIoReqsActive);
1089 pThis->pDrvMediaExPort->pfnIoReqStateChanged(pThis->pDrvMediaExPort, pIoReq, &pIoReq->abAlloc[0],
1090 PDMMEDIAEXIOREQSTATE_SUSPENDED);
1091 }
1092 else
1093 {
1094 /* Request was canceled inbetween, so don't care and notify the owner about the completed request. */
1095 Assert(pIoReq->enmState == VDIOREQSTATE_CANCELED);
1096 drvramdiskMediaExIoReqCompleteWorker(pThis, pIoReq, rcReq, true /* fUpNotify */);
1097 }
1098 }
1099 else
1100 {
1101 /* Adjust the remaining amount to transfer. */
1102 size_t cbReqIo = RT_MIN(pIoReq->ReadWrite.cbReqLeft, pIoReq->ReadWrite.cbIoBuf);
1103 pIoReq->ReadWrite.offStart += cbReqIo;
1104 pIoReq->ReadWrite.cbReqLeft -= cbReqIo;
1105
1106 if ( RT_FAILURE(rcReq)
1107 || !pIoReq->ReadWrite.cbReqLeft
1108 || ( pIoReq->enmType != PDMMEDIAEXIOREQTYPE_READ
1109 && pIoReq->enmType != PDMMEDIAEXIOREQTYPE_WRITE))
1110 drvramdiskMediaExIoReqCompleteWorker(pThis, pIoReq, rcReq, true /* fUpNotify */);
1111 else
1112 drvramdiskMediaExIoReqReadWriteProcess(pThis, pIoReq, true /* fUpNotify */);
1113 }
1114}
1115
1116/**
1117 * Worker for a flush request.
1118 *
1119 * @returns VBox status code.
1120 * @param pThis RAM disk container instance data.
1121 * @param pIoReq The flush request.
1122 */
1123static DECLCALLBACK(int) drvramdiskIoReqFlushWorker(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq)
1124{
1125 /* Nothing to do for a ram disk. */
1126 drvramdiskMediaExIoReqComplete(pThis, pIoReq, VINF_SUCCESS);
1127 return VINF_SUCCESS;
1128}
1129
1130/**
1131 * Worker for a discard request.
1132 *
1133 * @returns VBox status code.
1134 * @param pThis RAM disk container instance data.
1135 * @param pIoReq The discard request.
1136 */
1137static DECLCALLBACK(int) drvramdiskIoReqDiscardWorker(PDRVRAMDISK pThis, PPDMMEDIAEXIOREQINT pIoReq)
1138{
1139 int rc = drvramdiskDiscardRecords(pThis, pIoReq->Discard.paRanges, pIoReq->Discard.cRanges);
1140 drvramdiskMediaExIoReqComplete(pThis, pIoReq, rc);
1141 return VINF_SUCCESS;
1142}
1143
1144/**
1145 * @interface_method_impl{PDMIMEDIAEX,pfnQueryFeatures}
1146 */
1147static DECLCALLBACK(int) drvramdiskQueryFeatures(PPDMIMEDIAEX pInterface, uint32_t *pfFeatures)
1148{
1149 RT_NOREF1(pInterface);
1150 *pfFeatures = PDMIMEDIAEX_FEATURE_F_ASYNC | PDMIMEDIAEX_FEATURE_F_DISCARD;
1151 return VINF_SUCCESS;
1152}
1153
1154
1155/**
1156 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqAllocSizeSet}
1157 */
1158static DECLCALLBACK(int) drvramdiskIoReqAllocSizeSet(PPDMIMEDIAEX pInterface, size_t cbIoReqAlloc)
1159{
1160 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1161
1162 if (RT_UNLIKELY(pThis->hIoReqCache != NIL_RTMEMCACHE))
1163 return VERR_INVALID_STATE;
1164
1165 return RTMemCacheCreate(&pThis->hIoReqCache, sizeof(PDMMEDIAEXIOREQINT) + cbIoReqAlloc, 0, UINT32_MAX,
1166 NULL, NULL, NULL, 0);
1167}
1168
1169/**
1170 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqAlloc}
1171 */
1172static DECLCALLBACK(int) drvramdiskIoReqAlloc(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc,
1173 PDMMEDIAEXIOREQID uIoReqId, uint32_t fFlags)
1174{
1175 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1176
1177 AssertReturn(!(fFlags & ~PDMIMEDIAEX_F_VALID), VERR_INVALID_PARAMETER);
1178
1179 PPDMMEDIAEXIOREQINT pIoReq = (PPDMMEDIAEXIOREQINT)RTMemCacheAlloc(pThis->hIoReqCache);
1180
1181 if (RT_UNLIKELY(!pIoReq))
1182 return VERR_NO_MEMORY;
1183
1184 pIoReq->uIoReqId = uIoReqId;
1185 pIoReq->fFlags = fFlags;
1186 pIoReq->pDisk = pThis;
1187 pIoReq->enmState = VDIOREQSTATE_ALLOCATED;
1188 pIoReq->enmType = PDMMEDIAEXIOREQTYPE_INVALID;
1189
1190 int rc = drvramdiskMediaExIoReqInsert(pThis, pIoReq);
1191 if (RT_SUCCESS(rc))
1192 {
1193 *phIoReq = pIoReq;
1194 *ppvIoReqAlloc = &pIoReq->abAlloc[0];
1195 }
1196 else
1197 RTMemCacheFree(pThis->hIoReqCache, pIoReq);
1198
1199 return rc;
1200}
1201
1202/**
1203 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqFree}
1204 */
1205static DECLCALLBACK(int) drvramdiskIoReqFree(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
1206{
1207 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1208 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1209
1210 if ( pIoReq->enmState != VDIOREQSTATE_COMPLETED
1211 && pIoReq->enmState != VDIOREQSTATE_ALLOCATED)
1212 return VERR_PDM_MEDIAEX_IOREQ_INVALID_STATE;
1213
1214 /* Remove from allocated list. */
1215 int rc = drvramdiskMediaExIoReqRemove(pThis, pIoReq);
1216 if (RT_FAILURE(rc))
1217 return rc;
1218
1219 /* Free any associated I/O memory. */
1220 drvramdiskMediaExIoReqBufFree(pThis, pIoReq);
1221
1222 /* For discard request discard the range array. */
1223 if ( pIoReq->enmType == PDMMEDIAEXIOREQTYPE_DISCARD
1224 && pIoReq->Discard.paRanges)
1225 {
1226 RTMemFree(pIoReq->Discard.paRanges);
1227 pIoReq->Discard.paRanges = NULL;
1228 }
1229
1230 pIoReq->enmState = VDIOREQSTATE_FREE;
1231 RTMemCacheFree(pThis->hIoReqCache, pIoReq);
1232 return VINF_SUCCESS;
1233}
1234
1235/**
1236 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryResidual}
1237 */
1238static DECLCALLBACK(int) drvramdiskIoReqQueryResidual(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbResidual)
1239{
1240 RT_NOREF2(pInterface, hIoReq);
1241
1242 *pcbResidual = 0;
1243 return VINF_SUCCESS;
1244}
1245
1246/**
1247 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryXferSize}
1248 */
1249static DECLCALLBACK(int) drvramdiskIoReqQueryXferSize(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbXfer)
1250{
1251 RT_NOREF1(pInterface);
1252 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1253
1254 if ( pIoReq->enmType == PDMMEDIAEXIOREQTYPE_READ
1255 || pIoReq->enmType == PDMMEDIAEXIOREQTYPE_WRITE)
1256 *pcbXfer = pIoReq->ReadWrite.cbReq;
1257 else
1258 *pcbXfer = 0;
1259
1260 return VINF_SUCCESS;
1261}
1262
1263/**
1264 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancelAll}
1265 */
1266static DECLCALLBACK(int) drvramdiskIoReqCancelAll(PPDMIMEDIAEX pInterface)
1267{
1268 RT_NOREF1(pInterface);
1269 return VINF_SUCCESS; /** @todo */
1270}
1271
1272/**
1273 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancel}
1274 */
1275static DECLCALLBACK(int) drvramdiskIoReqCancel(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQID uIoReqId)
1276{
1277 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1278 unsigned idxBin = drvramdiskMediaExIoReqIdHash(uIoReqId);
1279
1280 int rc = RTSemFastMutexRequest(pThis->aIoReqAllocBins[idxBin].hMtxLstIoReqAlloc);
1281 if (RT_SUCCESS(rc))
1282 {
1283 /* Search for I/O request with ID. */
1284 PPDMMEDIAEXIOREQINT pIt;
1285 rc = VERR_PDM_MEDIAEX_IOREQID_NOT_FOUND;
1286
1287 RTListForEach(&pThis->aIoReqAllocBins[idxBin].LstIoReqAlloc, pIt, PDMMEDIAEXIOREQINT, NdAllocatedList)
1288 {
1289 if (pIt->uIoReqId == uIoReqId)
1290 {
1291 bool fXchg = true;
1292 VDIOREQSTATE enmStateOld = (VDIOREQSTATE)ASMAtomicReadU32((volatile uint32_t *)&pIt->enmState);
1293
1294 /*
1295 * We might have to try canceling the request multiple times if it transitioned from
1296 * ALLOCATED to ACTIVE or to SUSPENDED between reading the state and trying to change it.
1297 */
1298 while ( ( enmStateOld == VDIOREQSTATE_ALLOCATED
1299 || enmStateOld == VDIOREQSTATE_ACTIVE
1300 || enmStateOld == VDIOREQSTATE_SUSPENDED)
1301 && !fXchg)
1302 {
1303 fXchg = ASMAtomicCmpXchgU32((volatile uint32_t *)&pIt->enmState, VDIOREQSTATE_CANCELED, enmStateOld);
1304 if (!fXchg)
1305 enmStateOld = (VDIOREQSTATE)ASMAtomicReadU32((volatile uint32_t *)&pIt->enmState);
1306 }
1307
1308 if (fXchg)
1309 {
1310 ASMAtomicDecU32(&pThis->cIoReqsActive);
1311 rc = VINF_SUCCESS;
1312 }
1313 break;
1314 }
1315 }
1316 RTSemFastMutexRelease(pThis->aIoReqAllocBins[idxBin].hMtxLstIoReqAlloc);
1317 }
1318
1319 return rc;
1320}
1321
1322/**
1323 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqRead}
1324 */
1325static DECLCALLBACK(int) drvramdiskIoReqRead(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbRead)
1326{
1327 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1328 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1329 VDIOREQSTATE enmState = (VDIOREQSTATE)ASMAtomicReadU32((volatile uint32_t *)&pIoReq->enmState);
1330
1331 if (RT_UNLIKELY(enmState == VDIOREQSTATE_CANCELED))
1332 return VERR_PDM_MEDIAEX_IOREQ_CANCELED;
1333
1334 if (RT_UNLIKELY(enmState != VDIOREQSTATE_ALLOCATED))
1335 return VERR_PDM_MEDIAEX_IOREQ_INVALID_STATE;
1336
1337 pIoReq->enmType = PDMMEDIAEXIOREQTYPE_READ;
1338 pIoReq->tsSubmit = RTTimeMilliTS();
1339 pIoReq->ReadWrite.offStart = off;
1340 pIoReq->ReadWrite.cbReq = cbRead;
1341 pIoReq->ReadWrite.cbReqLeft = cbRead;
1342 /* Allocate a suitable I/O buffer for this request. */
1343 int rc = drvramdiskMediaExIoReqBufAlloc(pThis, pIoReq, cbRead);
1344 if (rc == VINF_SUCCESS)
1345 {
1346 bool fXchg = ASMAtomicCmpXchgU32((volatile uint32_t *)&pIoReq->enmState, VDIOREQSTATE_ACTIVE, VDIOREQSTATE_ALLOCATED);
1347 if (RT_UNLIKELY(!fXchg))
1348 {
1349 /* Must have been canceled inbetween. */
1350 Assert(pIoReq->enmState == VDIOREQSTATE_CANCELED);
1351 return VERR_PDM_MEDIAEX_IOREQ_CANCELED;
1352 }
1353 ASMAtomicIncU32(&pThis->cIoReqsActive);
1354
1355 rc = drvramdiskMediaExIoReqReadWriteProcess(pThis, pIoReq, false /* fUpNotify */);
1356 }
1357
1358 return rc;
1359}
1360
1361/**
1362 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqWrite}
1363 */
1364static DECLCALLBACK(int) drvramdiskIoReqWrite(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbWrite)
1365{
1366 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1367 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1368 VDIOREQSTATE enmState = (VDIOREQSTATE)ASMAtomicReadU32((volatile uint32_t *)&pIoReq->enmState);
1369
1370 if (RT_UNLIKELY(enmState == VDIOREQSTATE_CANCELED))
1371 return VERR_PDM_MEDIAEX_IOREQ_CANCELED;
1372
1373 if (RT_UNLIKELY(enmState != VDIOREQSTATE_ALLOCATED))
1374 return VERR_PDM_MEDIAEX_IOREQ_INVALID_STATE;
1375
1376 pIoReq->enmType = PDMMEDIAEXIOREQTYPE_WRITE;
1377 pIoReq->tsSubmit = RTTimeMilliTS();
1378 pIoReq->ReadWrite.offStart = off;
1379 pIoReq->ReadWrite.cbReq = cbWrite;
1380 pIoReq->ReadWrite.cbReqLeft = cbWrite;
1381 /* Allocate a suitable I/O buffer for this request. */
1382 int rc = drvramdiskMediaExIoReqBufAlloc(pThis, pIoReq, cbWrite);
1383 if (rc == VINF_SUCCESS)
1384 {
1385 bool fXchg = ASMAtomicCmpXchgU32((volatile uint32_t *)&pIoReq->enmState, VDIOREQSTATE_ACTIVE, VDIOREQSTATE_ALLOCATED);
1386 if (RT_UNLIKELY(!fXchg))
1387 {
1388 /* Must have been canceled inbetween. */
1389 Assert(pIoReq->enmState == VDIOREQSTATE_CANCELED);
1390 return VERR_PDM_MEDIAEX_IOREQ_CANCELED;
1391 }
1392 ASMAtomicIncU32(&pThis->cIoReqsActive);
1393
1394 rc = drvramdiskMediaExIoReqReadWriteProcess(pThis, pIoReq, false /* fUpNotify */);
1395 }
1396
1397 return rc;
1398}
1399
1400/**
1401 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqFlush}
1402 */
1403static DECLCALLBACK(int) drvramdiskIoReqFlush(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
1404{
1405 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1406 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1407 VDIOREQSTATE enmState = (VDIOREQSTATE)ASMAtomicReadU32((volatile uint32_t *)&pIoReq->enmState);
1408
1409 if (RT_UNLIKELY(enmState == VDIOREQSTATE_CANCELED))
1410 return VERR_PDM_MEDIAEX_IOREQ_CANCELED;
1411
1412 if (RT_UNLIKELY(enmState != VDIOREQSTATE_ALLOCATED))
1413 return VERR_PDM_MEDIAEX_IOREQ_INVALID_STATE;
1414
1415 pIoReq->enmType = PDMMEDIAEXIOREQTYPE_FLUSH;
1416 pIoReq->tsSubmit = RTTimeMilliTS();
1417 bool fXchg = ASMAtomicCmpXchgU32((volatile uint32_t *)&pIoReq->enmState, VDIOREQSTATE_ACTIVE, VDIOREQSTATE_ALLOCATED);
1418 if (RT_UNLIKELY(!fXchg))
1419 {
1420 /* Must have been canceled inbetween. */
1421 Assert(pIoReq->enmState == VDIOREQSTATE_CANCELED);
1422 return VERR_PDM_MEDIAEX_IOREQ_CANCELED;
1423 }
1424
1425 ASMAtomicIncU32(&pThis->cIoReqsActive);
1426 return RTReqQueueCallEx(pThis->hReqQ, NULL, 0, RTREQFLAGS_NO_WAIT,
1427 (PFNRT)drvramdiskIoReqFlushWorker, 2, pThis, pIoReq);
1428}
1429
1430/**
1431 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqDiscard}
1432 */
1433static DECLCALLBACK(int) drvramdiskIoReqDiscard(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, unsigned cRangesMax)
1434{
1435 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1436 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1437 VDIOREQSTATE enmState = (VDIOREQSTATE)ASMAtomicReadU32((volatile uint32_t *)&pIoReq->enmState);
1438
1439 if (RT_UNLIKELY(enmState == VDIOREQSTATE_CANCELED))
1440 return VERR_PDM_MEDIAEX_IOREQ_CANCELED;
1441
1442 if (RT_UNLIKELY(enmState != VDIOREQSTATE_ALLOCATED))
1443 return VERR_PDM_MEDIAEX_IOREQ_INVALID_STATE;
1444
1445 /* Copy the ranges over now, this can be optimized in the future. */
1446 pIoReq->Discard.paRanges = (PRTRANGE)RTMemAllocZ(cRangesMax * sizeof(RTRANGE));
1447 if (RT_UNLIKELY(!pIoReq->Discard.paRanges))
1448 return VERR_NO_MEMORY;
1449
1450 int rc = pThis->pDrvMediaExPort->pfnIoReqQueryDiscardRanges(pThis->pDrvMediaExPort, pIoReq, &pIoReq->abAlloc[0],
1451 0, cRangesMax, pIoReq->Discard.paRanges,
1452 &pIoReq->Discard.cRanges);
1453 if (RT_SUCCESS(rc))
1454 {
1455 pIoReq->enmType = PDMMEDIAEXIOREQTYPE_DISCARD;
1456 pIoReq->tsSubmit = RTTimeMilliTS();
1457
1458 bool fXchg = ASMAtomicCmpXchgU32((volatile uint32_t *)&pIoReq->enmState, VDIOREQSTATE_ACTIVE, VDIOREQSTATE_ALLOCATED);
1459 if (RT_UNLIKELY(!fXchg))
1460 {
1461 /* Must have been canceled inbetween. */
1462 Assert(pIoReq->enmState == VDIOREQSTATE_CANCELED);
1463 return VERR_PDM_MEDIAEX_IOREQ_CANCELED;
1464 }
1465
1466 ASMAtomicIncU32(&pThis->cIoReqsActive);
1467
1468 rc = RTReqQueueCallEx(pThis->hReqQ, NULL, 0, RTREQFLAGS_NO_WAIT,
1469 (PFNRT)drvramdiskIoReqDiscardWorker, 2, pThis, pIoReq);
1470 }
1471
1472 return rc;
1473}
1474
1475/**
1476 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetActiveCount}
1477 */
1478static DECLCALLBACK(uint32_t) drvramdiskIoReqGetActiveCount(PPDMIMEDIAEX pInterface)
1479{
1480 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1481 return ASMAtomicReadU32(&pThis->cIoReqsActive);
1482}
1483
1484/**
1485 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetSuspendedCount}
1486 */
1487static DECLCALLBACK(uint32_t) drvramdiskIoReqGetSuspendedCount(PPDMIMEDIAEX pInterface)
1488{
1489 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1490
1491 AssertReturn(!drvramdiskMediaExIoReqIsVmRunning(pThis), 0);
1492
1493 uint32_t cIoReqSuspended = 0;
1494 PPDMMEDIAEXIOREQINT pIoReq;
1495 RTCritSectEnter(&pThis->CritSectIoReqRedo);
1496 RTListForEach(&pThis->LstIoReqRedo, pIoReq, PDMMEDIAEXIOREQINT, NdLstWait)
1497 {
1498 cIoReqSuspended++;
1499 }
1500 RTCritSectLeave(&pThis->CritSectIoReqRedo);
1501
1502 return cIoReqSuspended;
1503}
1504
1505/**
1506 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedStart}
1507 */
1508static DECLCALLBACK(int) drvramdiskIoReqQuerySuspendedStart(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq,
1509 void **ppvIoReqAlloc)
1510{
1511 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1512
1513 AssertReturn(!drvramdiskMediaExIoReqIsVmRunning(pThis), VERR_INVALID_STATE);
1514 AssertReturn(!RTListIsEmpty(&pThis->LstIoReqRedo), VERR_NOT_FOUND);
1515
1516 RTCritSectEnter(&pThis->CritSectIoReqRedo);
1517 PPDMMEDIAEXIOREQINT pIoReq = RTListGetFirst(&pThis->LstIoReqRedo, PDMMEDIAEXIOREQINT, NdLstWait);
1518 *phIoReq = pIoReq;
1519 *ppvIoReqAlloc = &pIoReq->abAlloc[0];
1520 RTCritSectLeave(&pThis->CritSectIoReqRedo);
1521
1522 return VINF_SUCCESS;
1523}
1524
1525/**
1526 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedNext}
1527 */
1528static DECLCALLBACK(int) drvramdiskIoReqQuerySuspendedNext(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq,
1529 PPDMMEDIAEXIOREQ phIoReqNext, void **ppvIoReqAllocNext)
1530{
1531 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1532 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1533
1534 AssertReturn(!drvramdiskMediaExIoReqIsVmRunning(pThis), VERR_INVALID_STATE);
1535 AssertPtrReturn(pIoReq, VERR_INVALID_HANDLE);
1536 AssertReturn(!RTListNodeIsLast(&pThis->LstIoReqRedo, &pIoReq->NdLstWait), VERR_NOT_FOUND);
1537
1538 RTCritSectEnter(&pThis->CritSectIoReqRedo);
1539 PPDMMEDIAEXIOREQINT pIoReqNext = RTListNodeGetNext(&pIoReq->NdLstWait, PDMMEDIAEXIOREQINT, NdLstWait);
1540 *phIoReqNext = pIoReqNext;
1541 *ppvIoReqAllocNext = &pIoReqNext->abAlloc[0];
1542 RTCritSectLeave(&pThis->CritSectIoReqRedo);
1543
1544 return VINF_SUCCESS;
1545}
1546
1547/**
1548 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedSave}
1549 */
1550static DECLCALLBACK(int) drvramdiskIoReqSuspendedSave(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
1551{
1552 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1553 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1554
1555 RT_NOREF1(pSSM);
1556
1557 AssertReturn(!drvramdiskMediaExIoReqIsVmRunning(pThis), VERR_INVALID_STATE);
1558 AssertPtrReturn(pIoReq, VERR_INVALID_HANDLE);
1559 AssertReturn(pIoReq->enmState == VDIOREQSTATE_SUSPENDED, VERR_INVALID_STATE);
1560
1561 return VERR_NOT_IMPLEMENTED;
1562}
1563
1564/**
1565 * @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedLoad}
1566 */
1567static DECLCALLBACK(int) drvramdiskIoReqSuspendedLoad(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
1568{
1569 PDRVRAMDISK pThis = RT_FROM_MEMBER(pInterface, DRVRAMDISK, IMediaEx);
1570 PPDMMEDIAEXIOREQINT pIoReq = hIoReq;
1571
1572 RT_NOREF1(pSSM);
1573
1574 AssertReturn(!drvramdiskMediaExIoReqIsVmRunning(pThis), VERR_INVALID_STATE);
1575 AssertPtrReturn(pIoReq, VERR_INVALID_HANDLE);
1576 AssertReturn(pIoReq->enmState == VDIOREQSTATE_ALLOCATED, VERR_INVALID_STATE);
1577
1578 return VERR_NOT_IMPLEMENTED;
1579}
1580
1581static DECLCALLBACK(int) drvramdiskIoReqWorker(RTTHREAD hThrdSelf, void *pvUser)
1582{
1583 int rc = VINF_SUCCESS;
1584 PDRVRAMDISK pThis = (PDRVRAMDISK)pvUser;
1585
1586 RT_NOREF1(hThrdSelf);
1587
1588 do
1589 {
1590 rc = RTReqQueueProcess(pThis->hReqQ, RT_INDEFINITE_WAIT);
1591 } while (RT_SUCCESS(rc));
1592
1593 return VINF_SUCCESS;
1594}
1595
1596/* -=-=-=-=- IBase -=-=-=-=- */
1597
1598/**
1599 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1600 */
1601static DECLCALLBACK(void *) drvramdiskQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1602{
1603 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1604 PDRVRAMDISK pThis = PDMINS_2_DATA(pDrvIns, PDRVRAMDISK);
1605
1606 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1607 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
1608 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEX, &pThis->IMediaEx);
1609
1610 return NULL;
1611}
1612
1613
1614/* -=-=-=-=- driver interface -=-=-=-=- */
1615
1616static DECLCALLBACK(int) drvramdiskTreeDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
1617{
1618 PDRVDISKSEGMENT pSeg = (PDRVDISKSEGMENT)pNode;
1619
1620 RT_NOREF1(pvUser);
1621
1622 RTMemFree(pSeg->pbSeg);
1623 RTMemFree(pSeg);
1624 return VINF_SUCCESS;
1625}
1626
1627/**
1628 * @copydoc FNPDMDRVDESTRUCT
1629 */
1630static DECLCALLBACK(void) drvramdiskDestruct(PPDMDRVINS pDrvIns)
1631{
1632 PDRVRAMDISK pThis = PDMINS_2_DATA(pDrvIns, PDRVRAMDISK);
1633
1634 if (pThis->pTreeSegments)
1635 {
1636 RTAvlrFileOffsetDestroy(pThis->pTreeSegments, drvramdiskTreeDestroy, NULL);
1637 RTMemFree(pThis->pTreeSegments);
1638 }
1639 RTReqQueueDestroy(pThis->hReqQ);
1640}
1641
1642/**
1643 * Construct a disk integrity driver instance.
1644 *
1645 * @copydoc FNPDMDRVCONSTRUCT
1646 */
1647static DECLCALLBACK(int) drvramdiskConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1648{
1649 RT_NOREF1(fFlags);
1650 int rc = VINF_SUCCESS;
1651 uint32_t cbIoBufMax;
1652 PDRVRAMDISK pThis = PDMINS_2_DATA(pDrvIns, PDRVRAMDISK);
1653 LogFlow(("drvdiskintConstruct: iInstance=%d\n", pDrvIns->iInstance));
1654 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1655
1656 /*
1657 * Validate configuration.
1658 */
1659 if (!CFGMR3AreValuesValid(pCfg, "Size\0"
1660 "PreAlloc\0"
1661 "IoBufMax\0"
1662 "SectorSize\0"
1663 "NonRotational\0"))
1664 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1665
1666 rc = CFGMR3QueryU64(pCfg, "Size", &pThis->cbDisk);
1667 if (RT_FAILURE(rc))
1668 return PDMDRV_SET_ERROR(pDrvIns, rc,
1669 N_("RamDisk: Error querying the media size"));
1670 rc = CFGMR3QueryBoolDef(pCfg, "PreAlloc", &pThis->fPreallocRamDisk, false);
1671 if (RT_FAILURE(rc))
1672 return PDMDRV_SET_ERROR(pDrvIns, rc,
1673 N_("RamDisk: Error querying \"PreAlloc\""));
1674 rc = CFGMR3QueryBoolDef(pCfg, "NonRotational", &pThis->fNonRotational, true);
1675 if (RT_FAILURE(rc))
1676 return PDMDRV_SET_ERROR(pDrvIns, rc,
1677 N_("RamDisk: Error querying \"NonRotational\""));
1678 rc = CFGMR3QueryU32Def(pCfg, "IoBufMax", &cbIoBufMax, 5 * _1M);
1679 if (RT_FAILURE(rc))
1680 return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Failed to query \"IoBufMax\" from the config"));
1681 rc = CFGMR3QueryU32Def(pCfg, "SectorSize", &pThis->cbSector, 512);
1682 if (RT_FAILURE(rc))
1683 return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Failed to query \"SectorSize\" from the config"));
1684
1685 /*
1686 * Initialize most of the data members.
1687 */
1688 pThis->pDrvIns = pDrvIns;
1689
1690 /* IBase. */
1691 pDrvIns->IBase.pfnQueryInterface = drvramdiskQueryInterface;
1692
1693 /* IMedia */
1694 pThis->IMedia.pfnRead = drvramdiskRead;
1695 pThis->IMedia.pfnWrite = drvramdiskWrite;
1696 pThis->IMedia.pfnFlush = drvramdiskFlush;
1697 pThis->IMedia.pfnGetSize = drvramdiskGetSize;
1698 pThis->IMedia.pfnBiosIsVisible = drvramdiskBiosIsVisible;
1699 pThis->IMedia.pfnGetType = drvramdiskGetType;
1700 pThis->IMedia.pfnIsReadOnly = drvramdiskIsReadOnly;
1701 pThis->IMedia.pfnBiosGetPCHSGeometry = drvramdiskBiosGetPCHSGeometry;
1702 pThis->IMedia.pfnBiosSetPCHSGeometry = drvramdiskBiosSetPCHSGeometry;
1703 pThis->IMedia.pfnBiosGetLCHSGeometry = drvramdiskBiosGetLCHSGeometry;
1704 pThis->IMedia.pfnBiosSetLCHSGeometry = drvramdiskBiosSetLCHSGeometry;
1705 pThis->IMedia.pfnGetUuid = drvramdiskGetUuid;
1706 pThis->IMedia.pfnGetSectorSize = drvramdiskGetSectorSize;
1707 pThis->IMedia.pfnReadPcBios = drvramdiskReadPcBios;
1708 pThis->IMedia.pfnDiscard = drvramdiskDiscard;
1709 pThis->IMedia.pfnIsNonRotational = drvramdiskIsNonRotational;
1710
1711 /* IMediaEx */
1712 pThis->IMediaEx.pfnQueryFeatures = drvramdiskQueryFeatures;
1713 pThis->IMediaEx.pfnIoReqAllocSizeSet = drvramdiskIoReqAllocSizeSet;
1714 pThis->IMediaEx.pfnIoReqAlloc = drvramdiskIoReqAlloc;
1715 pThis->IMediaEx.pfnIoReqFree = drvramdiskIoReqFree;
1716 pThis->IMediaEx.pfnIoReqQueryResidual = drvramdiskIoReqQueryResidual;
1717 pThis->IMediaEx.pfnIoReqQueryXferSize = drvramdiskIoReqQueryXferSize;
1718 pThis->IMediaEx.pfnIoReqCancelAll = drvramdiskIoReqCancelAll;
1719 pThis->IMediaEx.pfnIoReqCancel = drvramdiskIoReqCancel;
1720 pThis->IMediaEx.pfnIoReqRead = drvramdiskIoReqRead;
1721 pThis->IMediaEx.pfnIoReqWrite = drvramdiskIoReqWrite;
1722 pThis->IMediaEx.pfnIoReqFlush = drvramdiskIoReqFlush;
1723 pThis->IMediaEx.pfnIoReqDiscard = drvramdiskIoReqDiscard;
1724 pThis->IMediaEx.pfnIoReqGetActiveCount = drvramdiskIoReqGetActiveCount;
1725 pThis->IMediaEx.pfnIoReqGetSuspendedCount = drvramdiskIoReqGetSuspendedCount;
1726 pThis->IMediaEx.pfnIoReqQuerySuspendedStart = drvramdiskIoReqQuerySuspendedStart;
1727 pThis->IMediaEx.pfnIoReqQuerySuspendedNext = drvramdiskIoReqQuerySuspendedNext;
1728 pThis->IMediaEx.pfnIoReqSuspendedSave = drvramdiskIoReqSuspendedSave;
1729 pThis->IMediaEx.pfnIoReqSuspendedLoad = drvramdiskIoReqSuspendedLoad;
1730
1731 /* Query the media port interface above us. */
1732 pThis->pDrvMediaPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAPORT);
1733 if (!pThis->pDrvMediaPort)
1734 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
1735 N_("No media port interface above"));
1736
1737 /* Try to attach extended media port interface above.*/
1738 pThis->pDrvMediaExPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAEXPORT);
1739 if (pThis->pDrvMediaExPort)
1740 {
1741 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aIoReqAllocBins); i++)
1742 {
1743 rc = RTSemFastMutexCreate(&pThis->aIoReqAllocBins[i].hMtxLstIoReqAlloc);
1744 if (RT_FAILURE(rc))
1745 break;
1746 RTListInit(&pThis->aIoReqAllocBins[i].LstIoReqAlloc);
1747 }
1748
1749 if (RT_SUCCESS(rc))
1750 rc = RTCritSectInit(&pThis->CritSectIoReqsIoBufWait);
1751
1752 if (RT_SUCCESS(rc))
1753 rc = RTCritSectInit(&pThis->CritSectIoReqRedo);
1754
1755 if (RT_FAILURE(rc))
1756 return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Creating Mutex failed"));
1757
1758 RTListInit(&pThis->LstIoReqIoBufWait);
1759 RTListInit(&pThis->LstIoReqRedo);
1760 }
1761
1762 /* Create the AVL tree. */
1763 pThis->pTreeSegments = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
1764 if (!pThis->pTreeSegments)
1765 rc = VERR_NO_MEMORY;
1766
1767 if (pThis->pDrvMediaExPort)
1768 {
1769 rc = RTReqQueueCreate(&pThis->hReqQ);
1770 if (RT_SUCCESS(rc))
1771 {
1772 /* Spin up the worker thread. */
1773 rc = RTThreadCreate(&pThis->hThrdWrk, drvramdiskIoReqWorker, pThis, 0,
1774 RTTHREADTYPE_IO, 0, "RAMDSK");
1775 }
1776 }
1777
1778 if (pThis->pDrvMediaExPort)
1779 rc = IOBUFMgrCreate(&pThis->hIoBufMgr, cbIoBufMax, IOBUFMGR_F_DEFAULT);
1780
1781 /* Read in all data before the start if requested. */
1782 if ( RT_SUCCESS(rc)
1783 && pThis->fPreallocRamDisk)
1784 {
1785 LogRel(("RamDisk: Preallocating RAM disk...\n"));
1786 return VERR_NOT_IMPLEMENTED;
1787 }
1788
1789 return rc;
1790}
1791
1792
1793/**
1794 * Block driver registration record.
1795 */
1796const PDMDRVREG g_DrvRamDisk =
1797{
1798 /* u32Version */
1799 PDM_DRVREG_VERSION,
1800 /* szName */
1801 "RamDisk",
1802 /* szRCMod */
1803 "",
1804 /* szR0Mod */
1805 "",
1806 /* pszDescription */
1807 "RAM disk driver.",
1808 /* fFlags */
1809 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1810 /* fClass. */
1811 PDM_DRVREG_CLASS_BLOCK,
1812 /* cMaxInstances */
1813 ~0U,
1814 /* cbInstance */
1815 sizeof(DRVRAMDISK),
1816 /* pfnConstruct */
1817 drvramdiskConstruct,
1818 /* pfnDestruct */
1819 drvramdiskDestruct,
1820 /* pfnRelocate */
1821 NULL,
1822 /* pfnIOCtl */
1823 NULL,
1824 /* pfnPowerOn */
1825 NULL,
1826 /* pfnReset */
1827 NULL,
1828 /* pfnSuspend */
1829 NULL,
1830 /* pfnResume */
1831 NULL,
1832 /* pfnAttach */
1833 NULL,
1834 /* pfnDetach */
1835 NULL,
1836 /* pfnPowerOff */
1837 NULL,
1838 /* pfnSoftReset */
1839 NULL,
1840 /* u32EndVersion */
1841 PDM_DRVREG_VERSION
1842};
1843
Note: See TracBrowser for help on using the repository browser.

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