VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

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