VirtualBox

source: vbox/trunk/src/VBox/Storage/testcase/tstVDIo.cpp@ 68314

Last change on this file since 68314 was 66666, checked in by vboxsync, 8 years ago

Storage/tstVDIo: Plugged another small memory leak

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 97.0 KB
Line 
1/* $Id: tstVDIo.cpp 66666 2017-04-24 18:50:43Z vboxsync $ */
2/** @file
3 *
4 * VBox HDD container test utility - I/O replay.
5 */
6
7/*
8 * Copyright (C) 2011-2016 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18#define LOGGROUP LOGGROUP_DEFAULT
19#include <VBox/vd.h>
20#include <VBox/err.h>
21#include <VBox/log.h>
22#include <iprt/asm.h>
23#include <iprt/string.h>
24#include <iprt/stream.h>
25#include <iprt/mem.h>
26#include <iprt/initterm.h>
27#include <iprt/getopt.h>
28#include <iprt/list.h>
29#include <iprt/ctype.h>
30#include <iprt/semaphore.h>
31#include <iprt/thread.h>
32#include <iprt/rand.h>
33#include <iprt/critsect.h>
34#include <iprt/test.h>
35#include <iprt/system.h>
36
37#ifdef VBOX_TSTVDIO_WITH_LOG_REPLAY
38# include <VBox/vddbg.h>
39#endif
40
41#include "VDMemDisk.h"
42#include "VDIoBackend.h"
43#include "VDIoRnd.h"
44
45#include "VDScript.h"
46#include "BuiltinTests.h"
47
48/** forward declaration for the global test data pointer. */
49typedef struct VDTESTGLOB *PVDTESTGLOB;
50
51/**
52 * A virtual file backed by memory.
53 */
54typedef struct VDFILE
55{
56 /** Pointer to the next file. */
57 RTLISTNODE Node;
58 /** Name of the file. */
59 char *pszName;
60 /** Storage backing the file. */
61 PVDIOSTORAGE pIoStorage;
62 /** Flag whether the file is read locked. */
63 bool fReadLock;
64 /** Flag whether the file is write locked. */
65 bool fWriteLock;
66 /** Statistics: Number of reads. */
67 unsigned cReads;
68 /** Statistics: Number of writes. */
69 unsigned cWrites;
70 /** Statistics: Number of flushes. */
71 unsigned cFlushes;
72 /** Statistics: Number of async reads. */
73 unsigned cAsyncReads;
74 /** Statistics: Number of async writes. */
75 unsigned cAsyncWrites;
76 /** Statistics: Number of async flushes. */
77 unsigned cAsyncFlushes;
78} VDFILE, *PVDFILE;
79
80/**
81 * VD storage object.
82 */
83typedef struct VDSTORAGE
84{
85 /** Pointer to the file. */
86 PVDFILE pFile;
87 /** Completion callback of the VD layer. */
88 PFNVDCOMPLETED pfnComplete;
89} VDSTORAGE, *PVDSTORAGE;
90
91/**
92 * A virtual disk.
93 */
94typedef struct VDDISK
95{
96 /** List node. */
97 RTLISTNODE ListNode;
98 /** Name of the disk handle for identification. */
99 char *pszName;
100 /** HDD handle to operate on. */
101 PVDISK pVD;
102 /** Memory disk used for data verification. */
103 PVDMEMDISK pMemDiskVerify;
104 /** Critical section to serialize access to the memory disk. */
105 RTCRITSECT CritSectVerify;
106 /** Physical CHS Geometry. */
107 VDGEOMETRY PhysGeom;
108 /** Logical CHS geometry. */
109 VDGEOMETRY LogicalGeom;
110 /** Global test data. */
111 PVDTESTGLOB pTestGlob;
112} VDDISK, *PVDDISK;
113
114/**
115 * A data buffer with a pattern.
116 */
117typedef struct VDPATTERN
118{
119 /** List node. */
120 RTLISTNODE ListNode;
121 /** Name of the pattern. */
122 char *pszName;
123 /** Size of the pattern. */
124 size_t cbPattern;
125 /** Pointer to the buffer containing the pattern. */
126 void *pvPattern;
127} VDPATTERN, *PVDPATTERN;
128
129/**
130 * Global VD test state.
131 */
132typedef struct VDTESTGLOB
133{
134 /** List of active virtual disks. */
135 RTLISTNODE ListDisks;
136 /** Head of the active file list. */
137 RTLISTNODE ListFiles;
138 /** Head of the pattern list. */
139 RTLISTNODE ListPatterns;
140 /** I/O backend, common data. */
141 PVDIOBACKEND pIoBackend;
142 /** Error interface. */
143 VDINTERFACEERROR VDIfError;
144 /** Pointer to the per disk interface list. */
145 PVDINTERFACE pInterfacesDisk;
146 /** I/O interface. */
147 VDINTERFACEIO VDIfIo;
148 /** Pointer to the per image interface list. */
149 PVDINTERFACE pInterfacesImages;
150 /** I/O RNG handle. */
151 PVDIORND pIoRnd;
152 /** Current storage backend to use. */
153 char *pszIoBackend;
154 /** Testcase handle. */
155 RTTEST hTest;
156} VDTESTGLOB;
157
158/**
159 * Transfer direction.
160 */
161typedef enum TSTVDIOREQTXDIR
162{
163 TSTVDIOREQTXDIR_READ = 0,
164 TSTVDIOREQTXDIR_WRITE,
165 TSTVDIOREQTXDIR_FLUSH,
166 TSTVDIOREQTXDIR_DISCARD
167} TSTVDIOREQTXDIR;
168
169/**
170 * I/O request.
171 */
172typedef struct TSTVDIOREQ
173{
174 /** Transfer type. */
175 TSTVDIOREQTXDIR enmTxDir;
176 /** slot index. */
177 unsigned idx;
178 /** Start offset. */
179 uint64_t off;
180 /** Size to transfer. */
181 size_t cbReq;
182 /** S/G Buffer */
183 RTSGBUF SgBuf;
184 /** Flag whether the request is outstanding or not. */
185 volatile bool fOutstanding;
186 /** Buffer to use for reads. */
187 void *pvBufRead;
188 /** Contiguous buffer pointer backing the segments. */
189 void *pvBuf;
190 /** Opaque user data. */
191 void *pvUser;
192 /** Number of segments used for the data buffer. */
193 uint32_t cSegs;
194 /** Array of data segments. */
195 RTSGSEG aSegs[10];
196} TSTVDIOREQ, *PTSTVDIOREQ;
197
198/**
199 * I/O test data.
200 */
201typedef struct VDIOTEST
202{
203 /** Start offset. */
204 uint64_t offStart;
205 /** End offset. */
206 uint64_t offEnd;
207 /** Flag whether random or sequential access is wanted */
208 bool fRandomAccess;
209 /** Block size. */
210 size_t cbBlkIo;
211 /** Number of bytes to transfer. */
212 uint64_t cbIo;
213 /** Chance in percent to get a write. */
214 unsigned uWriteChance;
215 /** Maximum number of segments to create for one request. */
216 uint32_t cSegsMax;
217 /** Pointer to the I/O data generator. */
218 PVDIORND pIoRnd;
219 /** Pointer to the data pattern to use. */
220 PVDPATTERN pPattern;
221 /** Data dependent on the I/O mode (sequential or random). */
222 union
223 {
224 /** Next offset for sequential access. */
225 uint64_t offNext;
226 /** Data for random acess. */
227 struct
228 {
229 /** Number of valid entries in the bitmap. */
230 uint32_t cBlocks;
231 /** Pointer to the bitmap marking accessed blocks. */
232 uint8_t *pbMapAccessed;
233 /** Number of unaccessed blocks. */
234 uint32_t cBlocksLeft;
235 } Rnd;
236 } u;
237} VDIOTEST, *PVDIOTEST;
238
239static DECLCALLBACK(int) vdScriptHandlerCreate(PVDSCRIPTARG paScriptArgs, void *pvUser);
240static DECLCALLBACK(int) vdScriptHandlerOpen(PVDSCRIPTARG paScriptArgs, void *pvUser);
241static DECLCALLBACK(int) vdScriptHandlerIo(PVDSCRIPTARG paScriptArgs, void *pvUser);
242static DECLCALLBACK(int) vdScriptHandlerFlush(PVDSCRIPTARG paScriptArgs, void *pvUser);
243static DECLCALLBACK(int) vdScriptHandlerMerge(PVDSCRIPTARG paScriptArgs, void *pvUser);
244static DECLCALLBACK(int) vdScriptHandlerCompact(PVDSCRIPTARG paScriptArgs, void *pvUser);
245static DECLCALLBACK(int) vdScriptHandlerDiscard(PVDSCRIPTARG paScriptArgs, void *pvUser);
246static DECLCALLBACK(int) vdScriptHandlerCopy(PVDSCRIPTARG paScriptArgs, void *pvUser);
247static DECLCALLBACK(int) vdScriptHandlerClose(PVDSCRIPTARG paScriptArgs, void *pvUser);
248static DECLCALLBACK(int) vdScriptHandlerPrintFileSize(PVDSCRIPTARG paScriptArgs, void *pvUser);
249static DECLCALLBACK(int) vdScriptHandlerIoRngCreate(PVDSCRIPTARG paScriptArgs, void *pvUser);
250static DECLCALLBACK(int) vdScriptHandlerIoRngDestroy(PVDSCRIPTARG paScriptArgs, void *pvUser);
251static DECLCALLBACK(int) vdScriptHandlerIoPatternCreateFromNumber(PVDSCRIPTARG paScriptArgs, void *pvUser);
252static DECLCALLBACK(int) vdScriptHandlerIoPatternCreateFromFile(PVDSCRIPTARG paScriptArgs, void *pvUser);
253static DECLCALLBACK(int) vdScriptHandlerIoPatternDestroy(PVDSCRIPTARG paScriptArgs, void *pvUser);
254static DECLCALLBACK(int) vdScriptHandlerSleep(PVDSCRIPTARG paScriptArgs, void *pvUser);
255static DECLCALLBACK(int) vdScriptHandlerDumpFile(PVDSCRIPTARG paScriptArgs, void *pvUser);
256static DECLCALLBACK(int) vdScriptHandlerCreateDisk(PVDSCRIPTARG paScriptArgs, void *pvUser);
257static DECLCALLBACK(int) vdScriptHandlerDestroyDisk(PVDSCRIPTARG paScriptArgs, void *pvUser);
258static DECLCALLBACK(int) vdScriptHandlerCompareDisks(PVDSCRIPTARG paScriptArgs, void *pvUser);
259static DECLCALLBACK(int) vdScriptHandlerDumpDiskInfo(PVDSCRIPTARG paScriptArgs, void *pvUser);
260static DECLCALLBACK(int) vdScriptHandlerPrintMsg(PVDSCRIPTARG paScriptArgs, void *pvUser);
261static DECLCALLBACK(int) vdScriptHandlerShowStatistics(PVDSCRIPTARG paScriptArgs, void *pvUser);
262static DECLCALLBACK(int) vdScriptHandlerResetStatistics(PVDSCRIPTARG paScriptArgs, void *pvUser);
263static DECLCALLBACK(int) vdScriptHandlerResize(PVDSCRIPTARG paScriptArgs, void *pvUser);
264static DECLCALLBACK(int) vdScriptHandlerSetFileBackend(PVDSCRIPTARG paScriptArgs, void *pvUser);
265
266#ifdef VBOX_TSTVDIO_WITH_LOG_REPLAY
267static DECLCALLBACK(int) vdScriptHandlerIoLogReplay(PVDSCRIPTARG paScriptArgs, void *pvUser);
268#endif
269
270/* create action */
271const VDSCRIPTTYPE g_aArgCreate[] =
272{
273 VDSCRIPTTYPE_STRING,
274 VDSCRIPTTYPE_STRING,
275 VDSCRIPTTYPE_STRING,
276 VDSCRIPTTYPE_STRING,
277 VDSCRIPTTYPE_STRING,
278 VDSCRIPTTYPE_UINT64,
279 VDSCRIPTTYPE_BOOL,
280 VDSCRIPTTYPE_BOOL
281};
282
283/* open action */
284const VDSCRIPTTYPE g_aArgOpen[] =
285{
286 VDSCRIPTTYPE_STRING, /* disk */
287 VDSCRIPTTYPE_STRING, /* name */
288 VDSCRIPTTYPE_STRING, /* backend */
289 VDSCRIPTTYPE_BOOL, /* async */
290 VDSCRIPTTYPE_BOOL, /* shareable */
291 VDSCRIPTTYPE_BOOL, /* readonly */
292 VDSCRIPTTYPE_BOOL, /* discard */
293 VDSCRIPTTYPE_BOOL, /* ignoreflush */
294 VDSCRIPTTYPE_BOOL, /* honorsame */
295};
296
297/* I/O action */
298const VDSCRIPTTYPE g_aArgIo[] =
299{
300 VDSCRIPTTYPE_STRING, /* disk */
301 VDSCRIPTTYPE_BOOL, /* async */
302 VDSCRIPTTYPE_UINT32, /* max-reqs */
303 VDSCRIPTTYPE_STRING, /* mode */
304 VDSCRIPTTYPE_UINT64, /* size */
305 VDSCRIPTTYPE_UINT64, /* blocksize */
306 VDSCRIPTTYPE_UINT64, /* offStart */
307 VDSCRIPTTYPE_UINT64, /* offEnd */
308 VDSCRIPTTYPE_UINT32, /* writes */
309 VDSCRIPTTYPE_STRING /* pattern */
310};
311
312/* flush action */
313const VDSCRIPTTYPE g_aArgFlush[] =
314{
315 VDSCRIPTTYPE_STRING, /* disk */
316 VDSCRIPTTYPE_BOOL /* async */
317};
318
319/* merge action */
320const VDSCRIPTTYPE g_aArgMerge[] =
321{
322 VDSCRIPTTYPE_STRING, /* disk */
323 VDSCRIPTTYPE_UINT32, /* from */
324 VDSCRIPTTYPE_UINT32 /* to */
325};
326
327/* Compact a disk */
328const VDSCRIPTTYPE g_aArgCompact[] =
329{
330 VDSCRIPTTYPE_STRING, /* disk */
331 VDSCRIPTTYPE_UINT32 /* image */
332};
333
334/* Discard a part of a disk */
335const VDSCRIPTTYPE g_aArgDiscard[] =
336{
337 VDSCRIPTTYPE_STRING, /* disk */
338 VDSCRIPTTYPE_BOOL, /* async */
339 VDSCRIPTTYPE_STRING /* ranges */
340};
341
342/* Compact a disk */
343const VDSCRIPTTYPE g_aArgCopy[] =
344{
345 VDSCRIPTTYPE_STRING, /* diskfrom */
346 VDSCRIPTTYPE_STRING, /* diskto */
347 VDSCRIPTTYPE_UINT32, /* imagefrom */
348 VDSCRIPTTYPE_STRING, /* backend */
349 VDSCRIPTTYPE_STRING, /* filename */
350 VDSCRIPTTYPE_BOOL, /* movebyrename */
351 VDSCRIPTTYPE_UINT64, /* size */
352 VDSCRIPTTYPE_UINT32, /* fromsame */
353 VDSCRIPTTYPE_UINT32 /* tosame */
354};
355
356/* close action */
357const VDSCRIPTTYPE g_aArgClose[] =
358{
359 VDSCRIPTTYPE_STRING, /* disk */
360 VDSCRIPTTYPE_STRING, /* mode */
361 VDSCRIPTTYPE_BOOL /* delete */
362};
363
364/* print file size action */
365const VDSCRIPTTYPE g_aArgPrintFileSize[] =
366{
367 VDSCRIPTTYPE_STRING, /* disk */
368 VDSCRIPTTYPE_UINT32 /* image */
369};
370
371#ifdef VBOX_TSTVDIO_WITH_LOG_REPLAY
372/* print file size action */
373const VDSCRIPTTYPE g_aArgIoLogReplay[] =
374{
375 VDSCRIPTTYPE_STRING, /* disk */
376 VDSCRIPTTYPE_STRING /* iolog */
377};
378#endif
379
380/* I/O RNG create action */
381const VDSCRIPTTYPE g_aArgIoRngCreate[] =
382{
383 VDSCRIPTTYPE_UINT32, /* size */
384 VDSCRIPTTYPE_STRING, /* mode */
385 VDSCRIPTTYPE_UINT32, /* seed */
386};
387
388/* I/O pattern create action */
389const VDSCRIPTTYPE g_aArgIoPatternCreateFromNumber[] =
390{
391 VDSCRIPTTYPE_STRING, /* name */
392 VDSCRIPTTYPE_UINT32, /* size */
393 VDSCRIPTTYPE_UINT32 /* pattern */
394};
395
396/* I/O pattern create action */
397const VDSCRIPTTYPE g_aArgIoPatternCreateFromFile[] =
398{
399 VDSCRIPTTYPE_STRING, /* name */
400 VDSCRIPTTYPE_STRING /* file */
401};
402
403/* I/O pattern destroy action */
404const VDSCRIPTTYPE g_aArgIoPatternDestroy[] =
405{
406 VDSCRIPTTYPE_STRING /* name */
407};
408
409/* Sleep */
410const VDSCRIPTTYPE g_aArgSleep[] =
411{
412 VDSCRIPTTYPE_UINT32 /* time */
413};
414
415/* Dump memory file */
416const VDSCRIPTTYPE g_aArgDumpFile[] =
417{
418 VDSCRIPTTYPE_STRING, /* file */
419 VDSCRIPTTYPE_STRING /* path */
420};
421
422/* Create virtual disk handle */
423const VDSCRIPTTYPE g_aArgCreateDisk[] =
424{
425 VDSCRIPTTYPE_STRING, /* name */
426 VDSCRIPTTYPE_BOOL /* verify */
427};
428
429/* Create virtual disk handle */
430const VDSCRIPTTYPE g_aArgDestroyDisk[] =
431{
432 VDSCRIPTTYPE_STRING /* name */
433};
434
435/* Compare virtual disks */
436const VDSCRIPTTYPE g_aArgCompareDisks[] =
437{
438 VDSCRIPTTYPE_STRING, /* disk1 */
439 VDSCRIPTTYPE_STRING /* disk2 */
440};
441
442/* Dump disk info */
443const VDSCRIPTTYPE g_aArgDumpDiskInfo[] =
444{
445 VDSCRIPTTYPE_STRING /* disk */
446};
447
448/* Print message */
449const VDSCRIPTTYPE g_aArgPrintMsg[] =
450{
451 VDSCRIPTTYPE_STRING /* msg */
452};
453
454/* Show statistics */
455const VDSCRIPTTYPE g_aArgShowStatistics[] =
456{
457 VDSCRIPTTYPE_STRING /* file */
458};
459
460/* Reset statistics */
461const VDSCRIPTTYPE g_aArgResetStatistics[] =
462{
463 VDSCRIPTTYPE_STRING /* file */
464};
465
466/* Resize disk. */
467const VDSCRIPTTYPE g_aArgResize[] =
468{
469 VDSCRIPTTYPE_STRING, /* disk */
470 VDSCRIPTTYPE_UINT64 /* size */
471};
472
473/* Set file backend. */
474const VDSCRIPTTYPE g_aArgSetFileBackend[] =
475{
476 VDSCRIPTTYPE_STRING /* new file backend */
477};
478
479const VDSCRIPTCALLBACK g_aScriptActions[] =
480{
481 /* pcszFnName enmTypeReturn paArgDesc cArgDescs pfnHandler */
482 {"create", VDSCRIPTTYPE_VOID, g_aArgCreate, RT_ELEMENTS(g_aArgCreate), vdScriptHandlerCreate},
483 {"open", VDSCRIPTTYPE_VOID, g_aArgOpen, RT_ELEMENTS(g_aArgOpen), vdScriptHandlerOpen},
484 {"io", VDSCRIPTTYPE_VOID, g_aArgIo, RT_ELEMENTS(g_aArgIo), vdScriptHandlerIo},
485 {"flush", VDSCRIPTTYPE_VOID, g_aArgFlush, RT_ELEMENTS(g_aArgFlush), vdScriptHandlerFlush},
486 {"close", VDSCRIPTTYPE_VOID, g_aArgClose, RT_ELEMENTS(g_aArgClose), vdScriptHandlerClose},
487 {"printfilesize", VDSCRIPTTYPE_VOID, g_aArgPrintFileSize, RT_ELEMENTS(g_aArgPrintFileSize), vdScriptHandlerPrintFileSize},
488#ifdef VBOX_TSTVDIO_WITH_LOG_REPLAY
489 {"ioreplay", VDSCRIPTTYPE_VOID, g_aArgIoLogReplay, RT_ELEMENTS(g_aArgIoLogReplay), vdScriptHandlerIoLogReplay},
490#endif
491 {"merge", VDSCRIPTTYPE_VOID, g_aArgMerge, RT_ELEMENTS(g_aArgMerge), vdScriptHandlerMerge},
492 {"compact", VDSCRIPTTYPE_VOID, g_aArgCompact, RT_ELEMENTS(g_aArgCompact), vdScriptHandlerCompact},
493 {"discard", VDSCRIPTTYPE_VOID, g_aArgDiscard, RT_ELEMENTS(g_aArgDiscard), vdScriptHandlerDiscard},
494 {"copy", VDSCRIPTTYPE_VOID, g_aArgCopy, RT_ELEMENTS(g_aArgCopy), vdScriptHandlerCopy},
495 {"iorngcreate", VDSCRIPTTYPE_VOID, g_aArgIoRngCreate, RT_ELEMENTS(g_aArgIoRngCreate), vdScriptHandlerIoRngCreate},
496 {"iorngdestroy", VDSCRIPTTYPE_VOID, NULL, 0, vdScriptHandlerIoRngDestroy},
497 {"iopatterncreatefromnumber", VDSCRIPTTYPE_VOID, g_aArgIoPatternCreateFromNumber, RT_ELEMENTS(g_aArgIoPatternCreateFromNumber), vdScriptHandlerIoPatternCreateFromNumber},
498 {"iopatterncreatefromfile", VDSCRIPTTYPE_VOID, g_aArgIoPatternCreateFromFile, RT_ELEMENTS(g_aArgIoPatternCreateFromFile), vdScriptHandlerIoPatternCreateFromFile},
499 {"iopatterndestroy", VDSCRIPTTYPE_VOID, g_aArgIoPatternDestroy, RT_ELEMENTS(g_aArgIoPatternDestroy), vdScriptHandlerIoPatternDestroy},
500 {"sleep", VDSCRIPTTYPE_VOID, g_aArgSleep, RT_ELEMENTS(g_aArgSleep), vdScriptHandlerSleep},
501 {"dumpfile", VDSCRIPTTYPE_VOID, g_aArgDumpFile, RT_ELEMENTS(g_aArgDumpFile), vdScriptHandlerDumpFile},
502 {"createdisk", VDSCRIPTTYPE_VOID, g_aArgCreateDisk, RT_ELEMENTS(g_aArgCreateDisk), vdScriptHandlerCreateDisk},
503 {"destroydisk", VDSCRIPTTYPE_VOID, g_aArgDestroyDisk, RT_ELEMENTS(g_aArgDestroyDisk), vdScriptHandlerDestroyDisk},
504 {"comparedisks", VDSCRIPTTYPE_VOID, g_aArgCompareDisks, RT_ELEMENTS(g_aArgCompareDisks), vdScriptHandlerCompareDisks},
505 {"dumpdiskinfo", VDSCRIPTTYPE_VOID, g_aArgDumpDiskInfo, RT_ELEMENTS(g_aArgDumpDiskInfo), vdScriptHandlerDumpDiskInfo},
506 {"print", VDSCRIPTTYPE_VOID, g_aArgPrintMsg, RT_ELEMENTS(g_aArgPrintMsg), vdScriptHandlerPrintMsg},
507 {"showstatistics", VDSCRIPTTYPE_VOID, g_aArgShowStatistics, RT_ELEMENTS(g_aArgShowStatistics), vdScriptHandlerShowStatistics},
508 {"resetstatistics", VDSCRIPTTYPE_VOID, g_aArgResetStatistics, RT_ELEMENTS(g_aArgResetStatistics), vdScriptHandlerResetStatistics},
509 {"resize", VDSCRIPTTYPE_VOID, g_aArgResize, RT_ELEMENTS(g_aArgResize), vdScriptHandlerResize},
510 {"setfilebackend", VDSCRIPTTYPE_VOID, g_aArgSetFileBackend, RT_ELEMENTS(g_aArgSetFileBackend), vdScriptHandlerSetFileBackend},
511};
512
513const unsigned g_cScriptActions = RT_ELEMENTS(g_aScriptActions);
514
515#if 0 /* unused */
516static DECLCALLBACK(int) vdScriptCallbackPrint(PVDSCRIPTARG paScriptArgs, void *pvUser)
517{
518 NOREF(pvUser);
519 RTPrintf(paScriptArgs[0].psz);
520 return VINF_SUCCESS;
521}
522#endif /* unused */
523
524static DECLCALLBACK(void) tstVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
525{
526 NOREF(pvUser);
527 RTPrintf("tstVDIo: Error %Rrc at %s:%u (%s): ", rc, RT_SRC_POS_ARGS);
528 RTPrintfV(pszFormat, va);
529 RTPrintf("\n");
530}
531
532static DECLCALLBACK(int) tstVDMessage(void *pvUser, const char *pszFormat, va_list va)
533{
534 NOREF(pvUser);
535 RTPrintf("tstVDIo: ");
536 RTPrintfV(pszFormat, va);
537 return VINF_SUCCESS;
538}
539
540static int tstVDIoTestInit(PVDIOTEST pIoTest, PVDTESTGLOB pGlob, bool fRandomAcc, uint32_t cSegsMax,
541 uint64_t cbIo, size_t cbBlkSize, uint64_t offStart, uint64_t offEnd,
542 unsigned uWriteChance, PVDPATTERN pPattern);
543static bool tstVDIoTestRunning(PVDIOTEST pIoTest);
544static void tstVDIoTestDestroy(PVDIOTEST pIoTest);
545static bool tstVDIoTestReqOutstanding(PTSTVDIOREQ pIoReq);
546static int tstVDIoTestReqInit(PVDIOTEST pIoTest, PTSTVDIOREQ pIoReq, void *pvUser);
547static DECLCALLBACK(void) tstVDIoTestReqComplete(void *pvUser1, void *pvUser2, int rcReq);
548
549static PVDDISK tstVDIoGetDiskByName(PVDTESTGLOB pGlob, const char *pcszDisk);
550static PVDPATTERN tstVDIoGetPatternByName(PVDTESTGLOB pGlob, const char *pcszName);
551static PVDPATTERN tstVDIoPatternCreate(const char *pcszName, size_t cbPattern);
552static int tstVDIoPatternGetBuffer(PVDPATTERN pPattern, void **ppv, size_t cb);
553
554static DECLCALLBACK(int) vdScriptHandlerCreate(PVDSCRIPTARG paScriptArgs, void *pvUser)
555{
556 int rc = VINF_SUCCESS;
557 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
558 PVDDISK pDisk = NULL;
559 bool fBase = false;
560 bool fDynamic = true;
561
562 const char *pcszDisk = paScriptArgs[0].psz;
563 if (!RTStrICmp(paScriptArgs[1].psz, "base"))
564 fBase = true;
565 else if (!RTStrICmp(paScriptArgs[1].psz, "diff"))
566 fBase = false;
567 else
568 {
569 RTPrintf("Invalid image mode '%s' given\n", paScriptArgs[1].psz);
570 rc = VERR_INVALID_PARAMETER;
571 }
572 const char *pcszImage = paScriptArgs[2].psz;
573 if (!RTStrICmp(paScriptArgs[3].psz, "fixed"))
574 fDynamic = false;
575 else if (!RTStrICmp(paScriptArgs[3].psz, "dynamic"))
576 fDynamic = true;
577 else
578 {
579 RTPrintf("Invalid image type '%s' given\n", paScriptArgs[3].psz);
580 rc = VERR_INVALID_PARAMETER;
581 }
582 const char *pcszBackend = paScriptArgs[4].psz;
583 uint64_t cbSize = paScriptArgs[5].u64;
584 bool fIgnoreFlush = paScriptArgs[6].f;
585 bool fHonorSame = paScriptArgs[7].f;
586
587 if (RT_SUCCESS(rc))
588 {
589 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
590 if (pDisk)
591 {
592 unsigned fOpenFlags = VD_OPEN_FLAGS_ASYNC_IO;
593 unsigned fImageFlags = VD_IMAGE_FLAGS_NONE;
594
595 if (!fDynamic)
596 fImageFlags |= VD_IMAGE_FLAGS_FIXED;
597
598 if (fIgnoreFlush)
599 fOpenFlags |= VD_OPEN_FLAGS_IGNORE_FLUSH;
600
601 if (fHonorSame)
602 fOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
603
604 if (fBase)
605 rc = VDCreateBase(pDisk->pVD, pcszBackend, pcszImage, cbSize, fImageFlags, NULL,
606 &pDisk->PhysGeom, &pDisk->LogicalGeom,
607 NULL, fOpenFlags, pGlob->pInterfacesImages, NULL);
608 else
609 rc = VDCreateDiff(pDisk->pVD, pcszBackend, pcszImage, fImageFlags, NULL, NULL, NULL,
610 fOpenFlags, pGlob->pInterfacesImages, NULL);
611 }
612 else
613 rc = VERR_NOT_FOUND;
614 }
615
616 return rc;
617}
618
619static DECLCALLBACK(int) vdScriptHandlerOpen(PVDSCRIPTARG paScriptArgs, void *pvUser)
620{
621 int rc = VINF_SUCCESS;
622 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
623 PVDDISK pDisk = NULL;
624
625 const char *pcszDisk = paScriptArgs[0].psz;
626 const char *pcszImage = paScriptArgs[1].psz;
627 const char *pcszBackend = paScriptArgs[2].psz;
628 bool fShareable = paScriptArgs[3].f;
629 bool fReadonly = paScriptArgs[4].f;
630 bool fAsyncIo = paScriptArgs[5].f;
631 bool fDiscard = paScriptArgs[6].f;
632 bool fIgnoreFlush = paScriptArgs[7].f;
633 bool fHonorSame = paScriptArgs[8].f;
634
635 if (RT_SUCCESS(rc))
636 {
637 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
638 if (pDisk)
639 {
640 unsigned fOpenFlags = 0;
641
642 if (fAsyncIo)
643 fOpenFlags |= VD_OPEN_FLAGS_ASYNC_IO;
644 if (fShareable)
645 fOpenFlags |= VD_OPEN_FLAGS_SHAREABLE;
646 if (fReadonly)
647 fOpenFlags |= VD_OPEN_FLAGS_READONLY;
648 if (fDiscard)
649 fOpenFlags |= VD_OPEN_FLAGS_DISCARD;
650 if (fIgnoreFlush)
651 fOpenFlags |= VD_OPEN_FLAGS_IGNORE_FLUSH;
652 if (fHonorSame)
653 fOpenFlags |= VD_OPEN_FLAGS_HONOR_SAME;
654
655 rc = VDOpen(pDisk->pVD, pcszBackend, pcszImage, fOpenFlags, pGlob->pInterfacesImages);
656 }
657 else
658 rc = VERR_NOT_FOUND;
659 }
660
661 return rc;
662}
663
664/**
665 * Returns the speed in KB/s from the amount of and the time in nanoseconds it
666 * took to complete the test.
667 *
668 * @returns Speed in KB/s
669 * @param cbIo Size of the I/O test
670 * @param tsNano Time in nanoseconds it took to complete the test.
671 */
672static uint64_t tstVDIoGetSpeedKBs(uint64_t cbIo, uint64_t tsNano)
673{
674 /* Seen on one of the testboxes, avoid division by 0 below. */
675 if (tsNano == 0)
676 return 0;
677
678 /*
679 * Blow up the value until we can do the calculation without getting 0 as
680 * a result.
681 */
682 uint64_t cbIoTemp = cbIo;
683 unsigned cRounds = 0;
684 while (cbIoTemp < tsNano)
685 {
686 cbIoTemp *= 1000;
687 cRounds++;
688 }
689
690 uint64_t uSpeedKBs = ((cbIoTemp / tsNano) * RT_NS_1SEC) / 1024;
691
692 while (cRounds-- > 0)
693 uSpeedKBs /= 1000;
694
695 return uSpeedKBs;
696}
697
698static DECLCALLBACK(int) vdScriptHandlerIo(PVDSCRIPTARG paScriptArgs, void *pvUser)
699{
700 int rc = VINF_SUCCESS;
701 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
702 bool fRandomAcc = false;
703 PVDDISK pDisk = NULL;
704 PVDPATTERN pPattern = NULL;
705
706 const char *pcszDisk = paScriptArgs[0].psz;
707 bool fAsync = paScriptArgs[1].f;
708 unsigned cMaxReqs = (unsigned)paScriptArgs[2].u64;
709 if (!RTStrICmp(paScriptArgs[3].psz, "seq"))
710 fRandomAcc = false;
711 else if (!RTStrICmp(paScriptArgs[3].psz, "rnd"))
712 fRandomAcc = true;
713 else
714 {
715 RTPrintf("Invalid access mode '%s'\n", paScriptArgs[3].psz);
716 rc = VERR_INVALID_PARAMETER;
717 }
718 uint64_t cbBlkSize = paScriptArgs[4].u64;
719 uint64_t offStart = paScriptArgs[5].u64;
720 uint64_t offEnd = paScriptArgs[6].u64;
721 uint64_t cbIo = paScriptArgs[7].u64;
722 uint8_t uWriteChance = (uint8_t)paScriptArgs[8].u64;
723 const char *pcszPattern = paScriptArgs[9].psz;
724
725 if ( RT_SUCCESS(rc)
726 && fAsync
727 && !cMaxReqs)
728 rc = VERR_INVALID_PARAMETER;
729
730 if (RT_SUCCESS(rc))
731 {
732 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
733 if (!pDisk)
734 rc = VERR_NOT_FOUND;
735 }
736
737 if (RT_SUCCESS(rc))
738 {
739 /* Set defaults if not set by the user. */
740 if (offStart == 0 && offEnd == 0)
741 {
742 offEnd = VDGetSize(pDisk->pVD, VD_LAST_IMAGE);
743 if (offEnd == 0)
744 return VERR_INVALID_STATE;
745 }
746
747 if (!cbIo)
748 cbIo = offEnd;
749 }
750
751 if ( RT_SUCCESS(rc)
752 && RTStrCmp(pcszPattern, "none"))
753 {
754 pPattern = tstVDIoGetPatternByName(pGlob, pcszPattern);
755 if (!pPattern)
756 rc = VERR_NOT_FOUND;
757 }
758
759 if (RT_SUCCESS(rc))
760 {
761 VDIOTEST IoTest;
762
763 RTTestSub(pGlob->hTest, "Basic I/O");
764 rc = tstVDIoTestInit(&IoTest, pGlob, fRandomAcc, 5, cbIo, cbBlkSize, offStart, offEnd, uWriteChance, pPattern);
765 if (RT_SUCCESS(rc))
766 {
767 PTSTVDIOREQ paIoReq = NULL;
768 unsigned cMaxTasksOutstanding = fAsync ? cMaxReqs : 1;
769 RTSEMEVENT EventSem;
770
771 rc = RTSemEventCreate(&EventSem);
772 paIoReq = (PTSTVDIOREQ)RTMemAllocZ(cMaxTasksOutstanding * sizeof(TSTVDIOREQ));
773 if (paIoReq && RT_SUCCESS(rc))
774 {
775 uint64_t NanoTS = RTTimeNanoTS();
776
777 /* Init requests. */
778 for (unsigned i = 0; i < cMaxTasksOutstanding; i++)
779 {
780 paIoReq[i].idx = i;
781 paIoReq[i].pvBufRead = RTMemAlloc(cbBlkSize);
782 if (!paIoReq[i].pvBufRead)
783 {
784 rc = VERR_NO_MEMORY;
785 break;
786 }
787 }
788
789 while ( tstVDIoTestRunning(&IoTest)
790 && RT_SUCCESS(rc))
791 {
792 bool fTasksOutstanding = false;
793 unsigned idx = 0;
794
795 /* Submit all idling requests. */
796 while ( idx < cMaxTasksOutstanding
797 && tstVDIoTestRunning(&IoTest))
798 {
799 if (!tstVDIoTestReqOutstanding(&paIoReq[idx]))
800 {
801 rc = tstVDIoTestReqInit(&IoTest, &paIoReq[idx], pDisk);
802 AssertRC(rc);
803
804 if (RT_SUCCESS(rc))
805 {
806 if (!fAsync)
807 {
808 switch (paIoReq[idx].enmTxDir)
809 {
810 case TSTVDIOREQTXDIR_READ:
811 {
812 rc = VDRead(pDisk->pVD, paIoReq[idx].off, paIoReq[idx].aSegs[0].pvSeg, paIoReq[idx].cbReq);
813
814 if (RT_SUCCESS(rc)
815 && pDisk->pMemDiskVerify)
816 {
817 RTSGBUF SgBuf;
818 RTSgBufInit(&SgBuf, &paIoReq[idx].aSegs[0], paIoReq[idx].cSegs);
819
820 if (VDMemDiskCmp(pDisk->pMemDiskVerify, paIoReq[idx].off, paIoReq[idx].cbReq, &SgBuf))
821 {
822 RTTestFailed(pGlob->hTest, "Corrupted disk at offset %llu!\n", paIoReq[idx].off);
823 rc = VERR_INVALID_STATE;
824 }
825 }
826 break;
827 }
828 case TSTVDIOREQTXDIR_WRITE:
829 {
830 rc = VDWrite(pDisk->pVD, paIoReq[idx].off, paIoReq[idx].aSegs[0].pvSeg, paIoReq[idx].cbReq);
831
832 if (RT_SUCCESS(rc)
833 && pDisk->pMemDiskVerify)
834 {
835 RTSGBUF SgBuf;
836 RTSgBufInit(&SgBuf, &paIoReq[idx].aSegs[0], paIoReq[idx].cSegs);
837 rc = VDMemDiskWrite(pDisk->pMemDiskVerify, paIoReq[idx].off, paIoReq[idx].cbReq, &SgBuf);
838 }
839 break;
840 }
841 case TSTVDIOREQTXDIR_FLUSH:
842 {
843 rc = VDFlush(pDisk->pVD);
844 break;
845 }
846 case TSTVDIOREQTXDIR_DISCARD:
847 AssertMsgFailed(("Invalid\n"));
848 }
849
850 ASMAtomicXchgBool(&paIoReq[idx].fOutstanding, false);
851 if (RT_SUCCESS(rc))
852 idx++;
853 }
854 else
855 {
856 LogFlow(("Queuing request %d\n", idx));
857 switch (paIoReq[idx].enmTxDir)
858 {
859 case TSTVDIOREQTXDIR_READ:
860 {
861 rc = VDAsyncRead(pDisk->pVD, paIoReq[idx].off, paIoReq[idx].cbReq, &paIoReq[idx].SgBuf,
862 tstVDIoTestReqComplete, &paIoReq[idx], EventSem);
863 break;
864 }
865 case TSTVDIOREQTXDIR_WRITE:
866 {
867 rc = VDAsyncWrite(pDisk->pVD, paIoReq[idx].off, paIoReq[idx].cbReq, &paIoReq[idx].SgBuf,
868 tstVDIoTestReqComplete, &paIoReq[idx], EventSem);
869 break;
870 }
871 case TSTVDIOREQTXDIR_FLUSH:
872 {
873 rc = VDAsyncFlush(pDisk->pVD, tstVDIoTestReqComplete, &paIoReq[idx], EventSem);
874 break;
875 }
876 case TSTVDIOREQTXDIR_DISCARD:
877 AssertMsgFailed(("Invalid\n"));
878 }
879
880 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
881 {
882 idx++;
883 fTasksOutstanding = true;
884 rc = VINF_SUCCESS;
885 }
886 else if (rc == VINF_VD_ASYNC_IO_FINISHED)
887 {
888 LogFlow(("Request %d completed\n", idx));
889 switch (paIoReq[idx].enmTxDir)
890 {
891 case TSTVDIOREQTXDIR_READ:
892 {
893 if (pDisk->pMemDiskVerify)
894 {
895 RTCritSectEnter(&pDisk->CritSectVerify);
896 RTSgBufReset(&paIoReq[idx].SgBuf);
897
898 if (VDMemDiskCmp(pDisk->pMemDiskVerify, paIoReq[idx].off, paIoReq[idx].cbReq,
899 &paIoReq[idx].SgBuf))
900 {
901 RTTestFailed(pGlob->hTest, "Corrupted disk at offset %llu!\n", paIoReq[idx].off);
902 rc = VERR_INVALID_STATE;
903 }
904 RTCritSectLeave(&pDisk->CritSectVerify);
905 }
906 break;
907 }
908 case TSTVDIOREQTXDIR_WRITE:
909 {
910 if (pDisk->pMemDiskVerify)
911 {
912 RTCritSectEnter(&pDisk->CritSectVerify);
913 RTSgBufReset(&paIoReq[idx].SgBuf);
914
915 rc = VDMemDiskWrite(pDisk->pMemDiskVerify, paIoReq[idx].off, paIoReq[idx].cbReq,
916 &paIoReq[idx].SgBuf);
917 RTCritSectLeave(&pDisk->CritSectVerify);
918 }
919 break;
920 }
921 case TSTVDIOREQTXDIR_FLUSH:
922 break;
923 case TSTVDIOREQTXDIR_DISCARD:
924 AssertMsgFailed(("Invalid\n"));
925 }
926
927 ASMAtomicXchgBool(&paIoReq[idx].fOutstanding, false);
928 if (rc != VERR_INVALID_STATE)
929 rc = VINF_SUCCESS;
930 }
931 }
932
933 if (RT_FAILURE(rc))
934 RTPrintf("Error submitting task %u rc=%Rrc\n", paIoReq[idx].idx, rc);
935 }
936 }
937 }
938
939 /* Wait for a request to complete. */
940 if ( fAsync
941 && fTasksOutstanding)
942 {
943 rc = RTSemEventWait(EventSem, RT_INDEFINITE_WAIT);
944 AssertRC(rc);
945 }
946 }
947
948 /* Cleanup, wait for all tasks to complete. */
949 while (fAsync)
950 {
951 unsigned idx = 0;
952 bool fAllIdle = true;
953
954 while (idx < cMaxTasksOutstanding)
955 {
956 if (tstVDIoTestReqOutstanding(&paIoReq[idx]))
957 {
958 fAllIdle = false;
959 break;
960 }
961 idx++;
962 }
963
964 if (!fAllIdle)
965 {
966 rc = RTSemEventWait(EventSem, 100);
967 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
968 }
969 else
970 break;
971 }
972
973 NanoTS = RTTimeNanoTS() - NanoTS;
974 uint64_t SpeedKBs = tstVDIoGetSpeedKBs(cbIo, NanoTS);
975 RTTestValue(pGlob->hTest, "Throughput", SpeedKBs, RTTESTUNIT_KILOBYTES_PER_SEC);
976
977 for (unsigned i = 0; i < cMaxTasksOutstanding; i++)
978 {
979 if (paIoReq[i].pvBufRead)
980 RTMemFree(paIoReq[i].pvBufRead);
981 }
982
983 RTSemEventDestroy(EventSem);
984 RTMemFree(paIoReq);
985 }
986 else
987 {
988 if (paIoReq)
989 RTMemFree(paIoReq);
990 if (RT_SUCCESS(rc))
991 RTSemEventDestroy(EventSem);
992 rc = VERR_NO_MEMORY;
993 }
994
995 tstVDIoTestDestroy(&IoTest);
996 }
997 RTTestSubDone(pGlob->hTest);
998 }
999
1000 return rc;
1001}
1002
1003static DECLCALLBACK(int) vdScriptHandlerFlush(PVDSCRIPTARG paScriptArgs, void *pvUser)
1004{
1005 int rc = VINF_SUCCESS;
1006 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1007 PVDDISK pDisk = NULL;
1008 const char *pcszDisk = paScriptArgs[0].psz;
1009 bool fAsync = paScriptArgs[1].f;
1010
1011 if (RT_SUCCESS(rc))
1012 {
1013 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1014 if (!pDisk)
1015 rc = VERR_NOT_FOUND;
1016 else if (fAsync)
1017 {
1018 TSTVDIOREQ IoReq;
1019 RTSEMEVENT EventSem;
1020
1021 rc = RTSemEventCreate(&EventSem);
1022 if (RT_SUCCESS(rc))
1023 {
1024 memset(&IoReq, 0, sizeof(TSTVDIOREQ));
1025 IoReq.enmTxDir = TSTVDIOREQTXDIR_FLUSH;
1026 IoReq.pvUser = pDisk;
1027 IoReq.idx = 0;
1028 rc = VDAsyncFlush(pDisk->pVD, tstVDIoTestReqComplete, &IoReq, EventSem);
1029 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1030 {
1031 rc = RTSemEventWait(EventSem, RT_INDEFINITE_WAIT);
1032 AssertRC(rc);
1033 }
1034 else if (rc == VINF_VD_ASYNC_IO_FINISHED)
1035 rc = VINF_SUCCESS;
1036
1037 RTSemEventDestroy(EventSem);
1038 }
1039 }
1040 else
1041 rc = VDFlush(pDisk->pVD);
1042 }
1043
1044 return rc;
1045}
1046
1047static DECLCALLBACK(int) vdScriptHandlerMerge(PVDSCRIPTARG paScriptArgs, void *pvUser)
1048{
1049 int rc = VINF_SUCCESS;
1050 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1051 PVDDISK pDisk = NULL;
1052 const char *pcszDisk = paScriptArgs[0].psz;
1053 unsigned nImageFrom = paScriptArgs[1].u32;
1054 unsigned nImageTo = paScriptArgs[2].u32;
1055
1056 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1057 if (!pDisk)
1058 rc = VERR_NOT_FOUND;
1059 else
1060 {
1061 /** @todo Provide progress interface to test that cancelation
1062 * doesn't corrupt the data.
1063 */
1064 rc = VDMerge(pDisk->pVD, nImageFrom, nImageTo, NULL);
1065 }
1066
1067 return rc;
1068}
1069
1070static DECLCALLBACK(int) vdScriptHandlerCompact(PVDSCRIPTARG paScriptArgs, void *pvUser)
1071{
1072 int rc = VINF_SUCCESS;
1073 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1074 PVDDISK pDisk = NULL;
1075 const char *pcszDisk = paScriptArgs[0].psz;
1076 unsigned nImage = paScriptArgs[1].u32;
1077
1078 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1079 if (!pDisk)
1080 rc = VERR_NOT_FOUND;
1081 else
1082 {
1083 /** @todo Provide progress interface to test that cancelation
1084 * doesn't corrupt the data.
1085 */
1086 rc = VDCompact(pDisk->pVD, nImage, NULL);
1087 }
1088
1089 return rc;
1090}
1091
1092static DECLCALLBACK(int) vdScriptHandlerDiscard(PVDSCRIPTARG paScriptArgs, void *pvUser)
1093{
1094 int rc = VINF_SUCCESS;
1095 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1096 PVDDISK pDisk = NULL;
1097 const char *pcszDisk = paScriptArgs[0].psz;
1098 bool fAsync = paScriptArgs[1].f;
1099 const char *pcszRanges = paScriptArgs[2].psz;
1100
1101 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1102 if (!pDisk)
1103 rc = VERR_NOT_FOUND;
1104 else
1105 {
1106 unsigned cRanges = 0;
1107 PRTRANGE paRanges = NULL;
1108
1109 /*
1110 * Parse the range string which should look like this:
1111 * n,off1,cb1,off2,cb2,...
1112 *
1113 * <n> gives the number of ranges in the string and every off<i>,cb<i>
1114 * pair afterwards is a start offset + number of bytes to discard entry.
1115 */
1116 do
1117 {
1118 rc = RTStrToUInt32Ex(pcszRanges, (char **)&pcszRanges, 10, &cRanges);
1119 if (RT_FAILURE(rc) && (rc != VWRN_TRAILING_CHARS))
1120 break;
1121
1122 if (!cRanges)
1123 {
1124 rc = VERR_INVALID_PARAMETER;
1125 break;
1126 }
1127
1128 paRanges = (PRTRANGE)RTMemAllocZ(cRanges * sizeof(RTRANGE));
1129 if (!paRanges)
1130 {
1131 rc = VERR_NO_MEMORY;
1132 break;
1133 }
1134
1135 if (*pcszRanges != ',')
1136 {
1137 rc = VERR_INVALID_PARAMETER;
1138 break;
1139 }
1140
1141 pcszRanges++;
1142
1143 /* Retrieve each pair from the string. */
1144 for (unsigned i = 0; i < cRanges; i++)
1145 {
1146 uint64_t off;
1147 uint32_t cb;
1148
1149 rc = RTStrToUInt64Ex(pcszRanges, (char **)&pcszRanges, 10, &off);
1150 if (RT_FAILURE(rc) && (rc != VWRN_TRAILING_CHARS))
1151 break;
1152
1153 if (*pcszRanges != ',')
1154 {
1155 switch (*pcszRanges)
1156 {
1157 case 'k':
1158 case 'K':
1159 {
1160 off *= _1K;
1161 break;
1162 }
1163 case 'm':
1164 case 'M':
1165 {
1166 off *= _1M;
1167 break;
1168 }
1169 case 'g':
1170 case 'G':
1171 {
1172 off *= _1G;
1173 break;
1174 }
1175 default:
1176 {
1177 RTPrintf("Invalid size suffix '%s'\n", pcszRanges);
1178 rc = VERR_INVALID_PARAMETER;
1179 }
1180 }
1181 if (RT_SUCCESS(rc))
1182 pcszRanges++;
1183 }
1184
1185 if (*pcszRanges != ',')
1186 {
1187 rc = VERR_INVALID_PARAMETER;
1188 break;
1189 }
1190
1191 pcszRanges++;
1192
1193 rc = RTStrToUInt32Ex(pcszRanges, (char **)&pcszRanges, 10, &cb);
1194 if (RT_FAILURE(rc) && (rc != VWRN_TRAILING_CHARS))
1195 break;
1196
1197 if (*pcszRanges != ',')
1198 {
1199 switch (*pcszRanges)
1200 {
1201 case 'k':
1202 case 'K':
1203 {
1204 cb *= _1K;
1205 break;
1206 }
1207 case 'm':
1208 case 'M':
1209 {
1210 cb *= _1M;
1211 break;
1212 }
1213 case 'g':
1214 case 'G':
1215 {
1216 cb *= _1G;
1217 break;
1218 }
1219 default:
1220 {
1221 RTPrintf("Invalid size suffix '%s'\n", pcszRanges);
1222 rc = VERR_INVALID_PARAMETER;
1223 }
1224 }
1225 if (RT_SUCCESS(rc))
1226 pcszRanges++;
1227 }
1228
1229 if ( *pcszRanges != ','
1230 && !(i == cRanges - 1 && *pcszRanges == '\0'))
1231 {
1232 rc = VERR_INVALID_PARAMETER;
1233 break;
1234 }
1235
1236 pcszRanges++;
1237
1238 paRanges[i].offStart = off;
1239 paRanges[i].cbRange = cb;
1240 }
1241 } while (0);
1242
1243 if (RT_SUCCESS(rc))
1244 {
1245 if (!fAsync)
1246 rc = VDDiscardRanges(pDisk->pVD, paRanges, cRanges);
1247 else
1248 {
1249 TSTVDIOREQ IoReq;
1250 RTSEMEVENT EventSem;
1251
1252 rc = RTSemEventCreate(&EventSem);
1253 if (RT_SUCCESS(rc))
1254 {
1255 memset(&IoReq, 0, sizeof(TSTVDIOREQ));
1256 IoReq.enmTxDir = TSTVDIOREQTXDIR_FLUSH;
1257 IoReq.pvUser = pDisk;
1258 IoReq.idx = 0;
1259 rc = VDAsyncDiscardRanges(pDisk->pVD, paRanges, cRanges, tstVDIoTestReqComplete, &IoReq, EventSem);
1260 if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
1261 {
1262 rc = RTSemEventWait(EventSem, RT_INDEFINITE_WAIT);
1263 AssertRC(rc);
1264 }
1265 else if (rc == VINF_VD_ASYNC_IO_FINISHED)
1266 rc = VINF_SUCCESS;
1267
1268 RTSemEventDestroy(EventSem);
1269 }
1270 }
1271
1272 if ( RT_SUCCESS(rc)
1273 && pDisk->pMemDiskVerify)
1274 {
1275 for (unsigned i = 0; i < cRanges; i++)
1276 {
1277 void *pv = RTMemAllocZ(paRanges[i].cbRange);
1278 if (pv)
1279 {
1280 RTSGSEG SgSeg;
1281 RTSGBUF SgBuf;
1282
1283 SgSeg.pvSeg = pv;
1284 SgSeg.cbSeg = paRanges[i].cbRange;
1285 RTSgBufInit(&SgBuf, &SgSeg, 1);
1286 rc = VDMemDiskWrite(pDisk->pMemDiskVerify, paRanges[i].offStart, paRanges[i].cbRange, &SgBuf);
1287 RTMemFree(pv);
1288 }
1289 else
1290 {
1291 rc = VERR_NO_MEMORY;
1292 break;
1293 }
1294 }
1295 }
1296 }
1297
1298 if (paRanges)
1299 RTMemFree(paRanges);
1300 }
1301
1302 return rc;
1303}
1304
1305static DECLCALLBACK(int) vdScriptHandlerCopy(PVDSCRIPTARG paScriptArgs, void *pvUser)
1306{
1307 int rc = VINF_SUCCESS;
1308 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1309 PVDDISK pDiskFrom = NULL;
1310 PVDDISK pDiskTo = NULL;
1311 const char *pcszDiskFrom = paScriptArgs[0].psz;
1312 const char *pcszDiskTo = paScriptArgs[1].psz;
1313 unsigned nImageFrom = paScriptArgs[2].u32;
1314 const char *pcszBackend = paScriptArgs[3].psz;
1315 const char *pcszFilename = paScriptArgs[4].psz;
1316 bool fMoveByRename = paScriptArgs[5].f;
1317 uint64_t cbSize = paScriptArgs[6].u64;
1318 unsigned nImageFromSame = paScriptArgs[7].u32;
1319 unsigned nImageToSame = paScriptArgs[8].u32;
1320
1321 pDiskFrom = tstVDIoGetDiskByName(pGlob, pcszDiskFrom);
1322 pDiskTo = tstVDIoGetDiskByName(pGlob, pcszDiskTo);
1323 if (!pDiskFrom || !pDiskTo)
1324 rc = VERR_NOT_FOUND;
1325 else
1326 {
1327 /** @todo Provide progress interface to test that cancelation
1328 * works as intended.
1329 */
1330 rc = VDCopyEx(pDiskFrom->pVD, nImageFrom, pDiskTo->pVD, pcszBackend, pcszFilename,
1331 fMoveByRename, cbSize, nImageFromSame, nImageToSame,
1332 VD_IMAGE_FLAGS_NONE, NULL, VD_OPEN_FLAGS_ASYNC_IO,
1333 NULL, pGlob->pInterfacesImages, NULL);
1334 }
1335
1336 return rc;
1337}
1338
1339static DECLCALLBACK(int) vdScriptHandlerClose(PVDSCRIPTARG paScriptArgs, void *pvUser)
1340{
1341 int rc = VINF_SUCCESS;
1342 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1343 bool fAll = false;
1344 bool fDelete = false;
1345 const char *pcszDisk = NULL;
1346 PVDDISK pDisk = NULL;
1347
1348 pcszDisk = paScriptArgs[0].psz;
1349 if (!RTStrICmp(paScriptArgs[1].psz, "all"))
1350 fAll = true;
1351 else if (!RTStrICmp(paScriptArgs[1].psz, "single"))
1352 fAll = false;
1353 else
1354 {
1355 RTPrintf("Invalid mode '%s' given\n", paScriptArgs[1].psz);
1356 rc = VERR_INVALID_PARAMETER;
1357 }
1358 fDelete = paScriptArgs[2].f;
1359
1360 if ( fAll
1361 && fDelete)
1362 {
1363 RTPrintf("mode=all doesn't work with delete=yes\n");
1364 rc = VERR_INVALID_PARAMETER;
1365 }
1366
1367 if (RT_SUCCESS(rc))
1368 {
1369 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1370 if (pDisk)
1371 {
1372 if (fAll)
1373 rc = VDCloseAll(pDisk->pVD);
1374 else
1375 rc = VDClose(pDisk->pVD, fDelete);
1376 }
1377 else
1378 rc = VERR_NOT_FOUND;
1379 }
1380 return rc;
1381}
1382
1383
1384static DECLCALLBACK(int) vdScriptHandlerPrintFileSize(PVDSCRIPTARG paScriptArgs, void *pvUser)
1385{
1386 int rc = VINF_SUCCESS;
1387 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1388 PVDDISK pDisk = NULL;
1389 const char *pcszDisk = paScriptArgs[0].psz;
1390 uint32_t nImage = paScriptArgs[1].u32;
1391
1392 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1393 if (pDisk)
1394 RTPrintf("%s: size of image %u is %llu\n", pcszDisk, nImage, VDGetFileSize(pDisk->pVD, nImage));
1395 else
1396 rc = VERR_NOT_FOUND;
1397
1398 return rc;
1399}
1400
1401
1402#ifdef VBOX_TSTVDIO_WITH_LOG_REPLAY
1403static DECLCALLBACK(int) vdScriptHandlerIoLogReplay(PVDSCRIPTARG paScriptArgs, void *pvUser)
1404{
1405 int rc = VINF_SUCCESS;
1406 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1407 PVDDISK pDisk = NULL;
1408 const char *pcszDisk = paScriptArgs[0].psz;
1409 const char *pcszIoLog = paScriptArgs[1].psz;
1410
1411 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1412 if (pDisk)
1413 {
1414 VDIOLOGGER hIoLogger;
1415
1416 rc = VDDbgIoLogOpen(&hIoLogger, pcszIoLog);
1417 if (RT_SUCCESS(rc))
1418 {
1419 uint32_t fIoLogFlags;
1420 VDIOLOGEVENT enmEvent;
1421 void *pvBuf = NULL;
1422 size_t cbBuf = 0;
1423
1424 fIoLogFlags = VDDbgIoLogGetFlags(hIoLogger);
1425
1426 /* Loop through events. */
1427 rc = VDDbgIoLogEventTypeGetNext(hIoLogger, &enmEvent);
1428 while ( RT_SUCCESS(rc)
1429 && enmEvent != VDIOLOGEVENT_END)
1430 {
1431 VDDBGIOLOGREQ enmReq = VDDBGIOLOGREQ_INVALID;
1432 uint64_t idEvent = 0;
1433 bool fAsync = false;
1434 uint64_t off = 0;
1435 size_t cbIo = 0;
1436 Assert(enmEvent == VDIOLOGEVENT_START);
1437
1438 rc = VDDbgIoLogReqTypeGetNext(hIoLogger, &enmReq);
1439 if (RT_FAILURE(rc))
1440 break;
1441
1442 switch (enmReq)
1443 {
1444 case VDDBGIOLOGREQ_READ:
1445 {
1446 rc = VDDbgIoLogEventGetStart(hIoLogger, &idEvent, &fAsync,
1447 &off, &cbIo, 0, NULL);
1448 if ( RT_SUCCESS(rc)
1449 && cbIo > cbBuf)
1450 {
1451 pvBuf = RTMemRealloc(pvBuf, cbIo);
1452 if (pvBuf)
1453 cbBuf = cbIo;
1454 else
1455 rc = VERR_NO_MEMORY;
1456 }
1457
1458 if ( RT_SUCCESS(rc)
1459 && !fAsync)
1460 rc = VDRead(pDisk->pVD, off, pvBuf, cbIo);
1461 else if (RT_SUCCESS(rc))
1462 rc = VERR_NOT_SUPPORTED;
1463 break;
1464 }
1465 case VDDBGIOLOGREQ_WRITE:
1466 {
1467 rc = VDDbgIoLogEventGetStart(hIoLogger, &idEvent, &fAsync,
1468 &off, &cbIo, cbBuf, pvBuf);
1469 if (rc == VERR_BUFFER_OVERFLOW)
1470 {
1471 pvBuf = RTMemRealloc(pvBuf, cbIo);
1472 if (pvBuf)
1473 {
1474 cbBuf = cbIo;
1475 rc = VDDbgIoLogEventGetStart(hIoLogger, &idEvent, &fAsync,
1476 &off, &cbIo, cbBuf, pvBuf);
1477 }
1478 else
1479 rc = VERR_NO_MEMORY;
1480 }
1481
1482 if ( RT_SUCCESS(rc)
1483 && !fAsync)
1484 rc = VDWrite(pDisk->pVD, off, pvBuf, cbIo);
1485 else if (RT_SUCCESS(rc))
1486 rc = VERR_NOT_SUPPORTED;
1487 break;
1488 }
1489 case VDDBGIOLOGREQ_FLUSH:
1490 {
1491 rc = VDDbgIoLogEventGetStart(hIoLogger, &idEvent, &fAsync,
1492 &off, &cbIo, 0, NULL);
1493 if ( RT_SUCCESS(rc)
1494 && !fAsync)
1495 rc = VDFlush(pDisk->pVD);
1496 else if (RT_SUCCESS(rc))
1497 rc = VERR_NOT_SUPPORTED;
1498 break;
1499 }
1500 case VDDBGIOLOGREQ_DISCARD:
1501 {
1502 PRTRANGE paRanges = NULL;
1503 unsigned cRanges = 0;
1504
1505 rc = VDDbgIoLogEventGetStartDiscard(hIoLogger, &idEvent, &fAsync,
1506 &paRanges, &cRanges);
1507 if ( RT_SUCCESS(rc)
1508 && !fAsync)
1509 {
1510 rc = VDDiscardRanges(pDisk->pVD, paRanges, cRanges);
1511 RTMemFree(paRanges);
1512 }
1513 else if (RT_SUCCESS(rc))
1514 rc = VERR_NOT_SUPPORTED;
1515 break;
1516 }
1517 default:
1518 AssertMsgFailed(("Invalid request type %d\n", enmReq));
1519 }
1520
1521 if (RT_SUCCESS(rc))
1522 {
1523 /* Get matching complete event. */
1524 rc = VDDbgIoLogEventTypeGetNext(hIoLogger, &enmEvent);
1525 if (RT_SUCCESS(rc))
1526 {
1527 uint64_t idEvtComplete;
1528 int rcReq;
1529 uint64_t msDuration;
1530
1531 Assert(enmEvent == VDIOLOGEVENT_COMPLETE);
1532 rc = VDDbgIoLogEventGetComplete(hIoLogger, &idEvtComplete, &rcReq,
1533 &msDuration, &cbIo, cbBuf, pvBuf);
1534 Assert(RT_FAILURE(rc) || idEvtComplete == idEvent);
1535 }
1536 }
1537
1538 if (RT_SUCCESS(rc))
1539 rc = VDDbgIoLogEventTypeGetNext(hIoLogger, &enmEvent);
1540 }
1541
1542 VDDbgIoLogDestroy(hIoLogger);
1543 }
1544 }
1545 else
1546 rc = VERR_NOT_FOUND;
1547
1548 return rc;
1549}
1550#endif /* VBOX_TSTVDIO_WITH_LOG_REPLAY */
1551
1552
1553static DECLCALLBACK(int) vdScriptHandlerIoRngCreate(PVDSCRIPTARG paScriptArgs, void *pvUser)
1554{
1555 int rc = VINF_SUCCESS;
1556 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1557 size_t cbPattern = (size_t)paScriptArgs[0].u64;
1558 const char *pcszSeeder = paScriptArgs[1].psz;
1559 uint64_t uSeed = paScriptArgs[2].u64;
1560
1561 if (pGlob->pIoRnd)
1562 {
1563 RTPrintf("I/O RNG already exists\n");
1564 rc = VERR_INVALID_STATE;
1565 }
1566 else
1567 {
1568 uint64_t uSeedToUse = 0;
1569
1570 if (!RTStrICmp(pcszSeeder, "manual"))
1571 uSeedToUse = uSeed;
1572 else if (!RTStrICmp(pcszSeeder, "time"))
1573 uSeedToUse = RTTimeSystemMilliTS();
1574 else if (!RTStrICmp(pcszSeeder, "system"))
1575 {
1576 RTRAND hRand;
1577 rc = RTRandAdvCreateSystemTruer(&hRand);
1578 if (RT_SUCCESS(rc))
1579 {
1580 RTRandAdvBytes(hRand, &uSeedToUse, sizeof(uSeedToUse));
1581 RTRandAdvDestroy(hRand);
1582 }
1583 }
1584
1585 if (RT_SUCCESS(rc))
1586 rc = VDIoRndCreate(&pGlob->pIoRnd, cbPattern, uSeed);
1587 }
1588
1589 return rc;
1590}
1591
1592static DECLCALLBACK(int) vdScriptHandlerIoRngDestroy(PVDSCRIPTARG paScriptArgs, void *pvUser)
1593{
1594 RT_NOREF1(paScriptArgs);
1595 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1596
1597 if (pGlob->pIoRnd)
1598 {
1599 VDIoRndDestroy(pGlob->pIoRnd);
1600 pGlob->pIoRnd = NULL;
1601 }
1602 else
1603 RTPrintf("WARNING: No I/O RNG active, faulty script. Continuing\n");
1604
1605 return VINF_SUCCESS;
1606}
1607
1608static DECLCALLBACK(int) vdScriptHandlerIoPatternCreateFromNumber(PVDSCRIPTARG paScriptArgs, void *pvUser)
1609{
1610 int rc = VINF_SUCCESS;
1611 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1612 const char *pcszName = paScriptArgs[0].psz;
1613 size_t cbPattern = (size_t)paScriptArgs[1].u64;
1614 uint64_t u64Pattern = paScriptArgs[2].u64;
1615
1616 PVDPATTERN pPattern = tstVDIoGetPatternByName(pGlob, pcszName);
1617 if (!pPattern)
1618 {
1619 pPattern = tstVDIoPatternCreate(pcszName, RT_ALIGN_Z(cbPattern, sizeof(uint64_t)));
1620 if (pPattern)
1621 {
1622 /* Fill the buffer. */
1623 void *pv = pPattern->pvPattern;
1624
1625 while (pPattern->cbPattern > 0)
1626 {
1627 *((uint64_t*)pv) = u64Pattern;
1628 pPattern->cbPattern -= sizeof(uint64_t);
1629 pv = (uint64_t *)pv + 1;
1630 }
1631 pPattern->cbPattern = cbPattern; /* Set to the desired size. (could be unaligned) */
1632
1633 RTListAppend(&pGlob->ListPatterns, &pPattern->ListNode);
1634 }
1635 else
1636 rc = VERR_NO_MEMORY;
1637 }
1638 else
1639 rc = VERR_ALREADY_EXISTS;
1640
1641 return rc;
1642}
1643
1644static DECLCALLBACK(int) vdScriptHandlerIoPatternCreateFromFile(PVDSCRIPTARG paScriptArgs, void *pvUser)
1645{
1646 int rc = VINF_SUCCESS;
1647 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1648 const char *pcszName = paScriptArgs[0].psz;
1649 const char *pcszFile = paScriptArgs[1].psz;
1650
1651 PVDPATTERN pPattern = tstVDIoGetPatternByName(pGlob, pcszName);
1652 if (!pPattern)
1653 {
1654 RTFILE hFile;
1655 uint64_t cbPattern = 0;
1656
1657 rc = RTFileOpen(&hFile, pcszFile, RTFILE_O_DENY_NONE | RTFILE_O_OPEN | RTFILE_O_READ);
1658 if (RT_SUCCESS(rc))
1659 {
1660 rc = RTFileGetSize(hFile, &cbPattern);
1661 if (RT_SUCCESS(rc))
1662 {
1663 pPattern = tstVDIoPatternCreate(pcszName, (size_t)cbPattern);
1664 if (pPattern)
1665 {
1666 rc = RTFileRead(hFile, pPattern->pvPattern, (size_t)cbPattern, NULL);
1667 if (RT_SUCCESS(rc))
1668 RTListAppend(&pGlob->ListPatterns, &pPattern->ListNode);
1669 else
1670 {
1671 RTMemFree(pPattern->pvPattern);
1672 RTStrFree(pPattern->pszName);
1673 RTMemFree(pPattern);
1674 }
1675 }
1676 else
1677 rc = VERR_NO_MEMORY;
1678 }
1679 RTFileClose(hFile);
1680 }
1681 }
1682 else
1683 rc = VERR_ALREADY_EXISTS;
1684
1685 return rc;
1686}
1687
1688static DECLCALLBACK(int) vdScriptHandlerIoPatternDestroy(PVDSCRIPTARG paScriptArgs, void *pvUser)
1689{
1690 int rc = VINF_SUCCESS;
1691 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1692 const char *pcszName = paScriptArgs[0].psz;
1693
1694 PVDPATTERN pPattern = tstVDIoGetPatternByName(pGlob, pcszName);
1695 if (pPattern)
1696 {
1697 RTListNodeRemove(&pPattern->ListNode);
1698 RTMemFree(pPattern->pvPattern);
1699 RTStrFree(pPattern->pszName);
1700 RTMemFree(pPattern);
1701 }
1702 else
1703 rc = VERR_NOT_FOUND;
1704
1705 return rc;
1706}
1707
1708static DECLCALLBACK(int) vdScriptHandlerSleep(PVDSCRIPTARG paScriptArgs, void *pvUser)
1709{
1710 RT_NOREF1(pvUser);
1711 uint64_t cMillies = paScriptArgs[0].u64;
1712
1713 int rc = RTThreadSleep(cMillies);
1714 return rc;
1715}
1716
1717static DECLCALLBACK(int) vdScriptHandlerDumpFile(PVDSCRIPTARG paScriptArgs, void *pvUser)
1718{
1719 int rc = VINF_SUCCESS;
1720 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1721 const char *pcszFile = paScriptArgs[0].psz;
1722 const char *pcszPathToDump = paScriptArgs[1].psz;
1723
1724 /* Check for the file. */
1725 bool fFound = false;
1726 PVDFILE pIt;
1727 RTListForEach(&pGlob->ListFiles, pIt, VDFILE, Node)
1728 {
1729 if (!RTStrCmp(pIt->pszName, pcszFile))
1730 {
1731 fFound = true;
1732 break;
1733 }
1734 }
1735
1736 if (fFound)
1737 {
1738 RTPrintf("Dumping memory file %s to %s, this might take some time\n", pcszFile, pcszPathToDump);
1739 rc = VDIoBackendDumpToFile(pIt->pIoStorage, pcszPathToDump);
1740 rc = VERR_NOT_IMPLEMENTED;
1741 }
1742 else
1743 rc = VERR_FILE_NOT_FOUND;
1744
1745 return rc;
1746}
1747
1748static DECLCALLBACK(int) vdScriptHandlerCreateDisk(PVDSCRIPTARG paScriptArgs, void *pvUser)
1749{
1750 int rc = VINF_SUCCESS;
1751 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1752 const char *pcszDisk = NULL;
1753 PVDDISK pDisk = NULL;
1754 bool fVerify = false;
1755
1756 pcszDisk = paScriptArgs[0].psz;
1757 fVerify = paScriptArgs[1].f;
1758
1759 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1760 if (pDisk)
1761 rc = VERR_ALREADY_EXISTS;
1762 else
1763 {
1764 pDisk = (PVDDISK)RTMemAllocZ(sizeof(VDDISK));
1765 if (pDisk)
1766 {
1767 pDisk->pTestGlob = pGlob;
1768 pDisk->pszName = RTStrDup(pcszDisk);
1769 if (pDisk->pszName)
1770 {
1771 rc = VINF_SUCCESS;
1772
1773 if (fVerify)
1774 {
1775 rc = VDMemDiskCreate(&pDisk->pMemDiskVerify, 0 /* Growing */);
1776 if (RT_SUCCESS(rc))
1777 {
1778 rc = RTCritSectInit(&pDisk->CritSectVerify);
1779 if (RT_FAILURE(rc))
1780 VDMemDiskDestroy(pDisk->pMemDiskVerify);
1781 }
1782 }
1783
1784 if (RT_SUCCESS(rc))
1785 {
1786 rc = VDCreate(pGlob->pInterfacesDisk, VDTYPE_HDD, &pDisk->pVD);
1787
1788 if (RT_SUCCESS(rc))
1789 RTListAppend(&pGlob->ListDisks, &pDisk->ListNode);
1790 else
1791 {
1792 if (fVerify)
1793 {
1794 RTCritSectDelete(&pDisk->CritSectVerify);
1795 VDMemDiskDestroy(pDisk->pMemDiskVerify);
1796 }
1797 RTStrFree(pDisk->pszName);
1798 }
1799 }
1800 }
1801 else
1802 rc = VERR_NO_MEMORY;
1803
1804 if (RT_FAILURE(rc))
1805 RTMemFree(pDisk);
1806 }
1807 else
1808 rc = VERR_NO_MEMORY;
1809 }
1810 return rc;
1811}
1812
1813static DECLCALLBACK(int) vdScriptHandlerDestroyDisk(PVDSCRIPTARG paScriptArgs, void *pvUser)
1814{
1815 int rc = VINF_SUCCESS;
1816 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1817 const char *pcszDisk = NULL;
1818 PVDDISK pDisk = NULL;
1819
1820 pcszDisk = paScriptArgs[0].psz;
1821
1822 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1823 if (pDisk)
1824 {
1825 RTListNodeRemove(&pDisk->ListNode);
1826 VDDestroy(pDisk->pVD);
1827 if (pDisk->pMemDiskVerify)
1828 {
1829 VDMemDiskDestroy(pDisk->pMemDiskVerify);
1830 RTCritSectDelete(&pDisk->CritSectVerify);
1831 }
1832 RTStrFree(pDisk->pszName);
1833 RTMemFree(pDisk);
1834 }
1835 else
1836 rc = VERR_NOT_FOUND;
1837
1838 return rc;
1839}
1840
1841static DECLCALLBACK(int) vdScriptHandlerCompareDisks(PVDSCRIPTARG paScriptArgs, void *pvUser)
1842{
1843 int rc = VINF_SUCCESS;
1844 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1845 const char *pcszDisk1 = NULL;
1846 PVDDISK pDisk1 = NULL;
1847 const char *pcszDisk2 = NULL;
1848 PVDDISK pDisk2 = NULL;
1849
1850 pcszDisk1 = paScriptArgs[0].psz;
1851 pcszDisk2 = paScriptArgs[1].psz;
1852
1853 pDisk1 = tstVDIoGetDiskByName(pGlob, pcszDisk1);
1854 pDisk2 = tstVDIoGetDiskByName(pGlob, pcszDisk2);
1855
1856 if (pDisk1 && pDisk2)
1857 {
1858 uint8_t *pbBuf1 = (uint8_t *)RTMemAllocZ(16 * _1M);
1859 uint8_t *pbBuf2 = (uint8_t *)RTMemAllocZ(16 * _1M);
1860 if (pbBuf1 && pbBuf2)
1861 {
1862 uint64_t cbDisk1, cbDisk2;
1863 uint64_t uOffCur = 0;
1864
1865 cbDisk1 = VDGetSize(pDisk1->pVD, VD_LAST_IMAGE);
1866 cbDisk2 = VDGetSize(pDisk2->pVD, VD_LAST_IMAGE);
1867
1868 RTTestSub(pGlob->hTest, "Comparing two disks for equal content");
1869 if (cbDisk1 != cbDisk2)
1870 RTTestFailed(pGlob->hTest, "Disks differ in size %llu vs %llu\n", cbDisk1, cbDisk2);
1871 else
1872 {
1873 while (uOffCur < cbDisk1)
1874 {
1875 size_t cbRead = RT_MIN(cbDisk1, 16 * _1M);
1876
1877 rc = VDRead(pDisk1->pVD, uOffCur, pbBuf1, cbRead);
1878 if (RT_SUCCESS(rc))
1879 rc = VDRead(pDisk2->pVD, uOffCur, pbBuf2, cbRead);
1880
1881 if (RT_SUCCESS(rc))
1882 {
1883 if (memcmp(pbBuf1, pbBuf2, cbRead))
1884 {
1885 RTTestFailed(pGlob->hTest, "Disks differ at offset %llu\n", uOffCur);
1886 rc = VERR_DEV_IO_ERROR;
1887 break;
1888 }
1889 }
1890 else
1891 {
1892 RTTestFailed(pGlob->hTest, "Reading one disk at offset %llu failed\n", uOffCur);
1893 break;
1894 }
1895
1896 uOffCur += cbRead;
1897 cbDisk1 -= cbRead;
1898 }
1899 }
1900 RTMemFree(pbBuf1);
1901 RTMemFree(pbBuf2);
1902 }
1903 else
1904 {
1905 if (pbBuf1)
1906 RTMemFree(pbBuf1);
1907 if (pbBuf2)
1908 RTMemFree(pbBuf2);
1909 rc = VERR_NO_MEMORY;
1910 }
1911 }
1912 else
1913 rc = VERR_NOT_FOUND;
1914
1915 return rc;
1916}
1917
1918static DECLCALLBACK(int) vdScriptHandlerDumpDiskInfo(PVDSCRIPTARG paScriptArgs, void *pvUser)
1919{
1920 int rc = VINF_SUCCESS;
1921 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1922 const char *pcszDisk = NULL;
1923 PVDDISK pDisk = NULL;
1924
1925 pcszDisk = paScriptArgs[0].psz;
1926
1927 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
1928
1929 if (pDisk)
1930 VDDumpImages(pDisk->pVD);
1931 else
1932 rc = VERR_NOT_FOUND;
1933
1934 return rc;
1935}
1936
1937static DECLCALLBACK(int) vdScriptHandlerPrintMsg(PVDSCRIPTARG paScriptArgs, void *pvUser)
1938{
1939 RT_NOREF1(pvUser);
1940 RTPrintf("%s\n", paScriptArgs[0].psz);
1941 return VINF_SUCCESS;
1942}
1943
1944static DECLCALLBACK(int) vdScriptHandlerShowStatistics(PVDSCRIPTARG paScriptArgs, void *pvUser)
1945{
1946 int rc = VINF_SUCCESS;
1947 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1948 const char *pcszFile = paScriptArgs[0].psz;
1949
1950 /* Check for the file. */
1951 bool fFound = false;
1952 PVDFILE pIt;
1953 RTListForEach(&pGlob->ListFiles, pIt, VDFILE, Node)
1954 {
1955 if (!RTStrCmp(pIt->pszName, pcszFile))
1956 {
1957 fFound = true;
1958 break;
1959 }
1960 }
1961
1962 if (fFound)
1963 {
1964 RTPrintf("Statistics %s: \n"
1965 " sync reads=%u writes=%u flushes=%u\n"
1966 " async reads=%u writes=%u flushes=%u\n",
1967 pcszFile,
1968 pIt->cReads, pIt->cWrites, pIt->cFlushes,
1969 pIt->cAsyncReads, pIt->cAsyncWrites, pIt->cAsyncFlushes);
1970 }
1971 else
1972 rc = VERR_FILE_NOT_FOUND;
1973
1974 return rc;
1975}
1976
1977static DECLCALLBACK(int) vdScriptHandlerResetStatistics(PVDSCRIPTARG paScriptArgs, void *pvUser)
1978{
1979 int rc = VINF_SUCCESS;
1980 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
1981 const char *pcszFile = paScriptArgs[0].psz;
1982
1983 /* Check for the file. */
1984 bool fFound = false;
1985 PVDFILE pIt;
1986 RTListForEach(&pGlob->ListFiles, pIt, VDFILE, Node)
1987 {
1988 if (!RTStrCmp(pIt->pszName, pcszFile))
1989 {
1990 fFound = true;
1991 break;
1992 }
1993 }
1994
1995 if (fFound)
1996 {
1997 pIt->cReads = 0;
1998 pIt->cWrites = 0;
1999 pIt->cFlushes = 0;
2000
2001 pIt->cAsyncReads = 0;
2002 pIt->cAsyncWrites = 0;
2003 pIt->cAsyncFlushes = 0;
2004 }
2005 else
2006 rc = VERR_FILE_NOT_FOUND;
2007
2008 return rc;
2009}
2010
2011static DECLCALLBACK(int) vdScriptHandlerResize(PVDSCRIPTARG paScriptArgs, void *pvUser)
2012{
2013 int rc = VINF_SUCCESS;
2014 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
2015 const char *pcszDisk = paScriptArgs[0].psz;
2016 uint64_t cbDiskNew = paScriptArgs[1].u64;
2017 PVDDISK pDisk = NULL;
2018
2019 pDisk = tstVDIoGetDiskByName(pGlob, pcszDisk);
2020 if (pDisk)
2021 {
2022 rc = VDResize(pDisk->pVD, cbDiskNew, &pDisk->PhysGeom, &pDisk->LogicalGeom, NULL);
2023 }
2024 else
2025 rc = VERR_NOT_FOUND;
2026
2027 return rc;
2028}
2029
2030static DECLCALLBACK(int) vdScriptHandlerSetFileBackend(PVDSCRIPTARG paScriptArgs, void *pvUser)
2031{
2032 int rc = VINF_SUCCESS;
2033 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
2034 const char *pcszBackend = paScriptArgs[0].psz;
2035
2036 RTStrFree(pGlob->pszIoBackend);
2037 pGlob->pszIoBackend = RTStrDup(pcszBackend);
2038 if (!pGlob->pszIoBackend)
2039 rc = VERR_NO_MEMORY;
2040
2041 return rc;
2042}
2043
2044static DECLCALLBACK(int) tstVDIoFileOpen(void *pvUser, const char *pszLocation,
2045 uint32_t fOpen,
2046 PFNVDCOMPLETED pfnCompleted,
2047 void **ppStorage)
2048{
2049 int rc = VINF_SUCCESS;
2050 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
2051 bool fFound = false;
2052
2053 /*
2054 * Some backends use ./ for paths, strip it.
2055 * @todo: Implement proper directory support for the
2056 * memory filesystem.
2057 */
2058 if ( strlen(pszLocation) >= 2
2059 && *pszLocation == '.'
2060 && ( pszLocation[1] == '/'
2061 || pszLocation[1] == '\\'))
2062 pszLocation += 2;
2063
2064 /* Check if the file exists. */
2065 PVDFILE pIt;
2066 RTListForEach(&pGlob->ListFiles, pIt, VDFILE, Node)
2067 {
2068 if (!RTStrCmp(pIt->pszName, pszLocation))
2069 {
2070 fFound = true;
2071 break;
2072 }
2073 }
2074
2075 if ((fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE)
2076 {
2077 /* If the file exists delete the memory disk. */
2078 if (fFound)
2079 rc = VDIoBackendStorageSetSize(pIt->pIoStorage, 0);
2080 else
2081 {
2082 /* Create completey new. */
2083 pIt = (PVDFILE)RTMemAllocZ(sizeof(VDFILE));
2084 if (pIt)
2085 {
2086 pIt->pszName = RTStrDup(pszLocation);
2087
2088 if (pIt->pszName)
2089 {
2090 rc = VDIoBackendStorageCreate(pGlob->pIoBackend, pGlob->pszIoBackend,
2091 pszLocation, pfnCompleted, &pIt->pIoStorage);
2092 }
2093 else
2094 rc = VERR_NO_MEMORY;
2095
2096 if (RT_FAILURE(rc))
2097 {
2098 if (pIt->pszName)
2099 RTStrFree(pIt->pszName);
2100 RTMemFree(pIt);
2101 }
2102 else
2103 RTListAppend(&pGlob->ListFiles, &pIt->Node);
2104 }
2105 else
2106 rc = VERR_NO_MEMORY;
2107 }
2108 }
2109 else if ((fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_OPEN)
2110 {
2111 if (!fFound)
2112 rc = VERR_FILE_NOT_FOUND;
2113 }
2114 else
2115 rc = VERR_INVALID_PARAMETER;
2116
2117 if (RT_SUCCESS(rc))
2118 {
2119 AssertPtr(pIt);
2120 PVDSTORAGE pStorage = (PVDSTORAGE)RTMemAllocZ(sizeof(VDSTORAGE));
2121 if (pStorage)
2122 {
2123 pStorage->pFile = pIt;
2124 pStorage->pfnComplete = pfnCompleted;
2125 *ppStorage = pStorage;
2126 }
2127 else
2128 rc = VERR_NO_MEMORY;
2129 }
2130
2131 return rc;
2132}
2133
2134static DECLCALLBACK(int) tstVDIoFileClose(void *pvUser, void *pStorage)
2135{
2136 RT_NOREF1(pvUser);
2137 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2138
2139 RTMemFree(pIoStorage);
2140 return VINF_SUCCESS;
2141}
2142
2143static DECLCALLBACK(int) tstVDIoFileDelete(void *pvUser, const char *pcszFilename)
2144{
2145 int rc = VINF_SUCCESS;
2146 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
2147 bool fFound = false;
2148
2149 /*
2150 * Some backends use ./ for paths, strip it.
2151 * @todo: Implement proper directory support for the
2152 * memory filesystem.
2153 */
2154 if ( strlen(pcszFilename) >= 2
2155 && *pcszFilename == '.'
2156 && pcszFilename[1] == '/')
2157 pcszFilename += 2;
2158
2159 /* Check if the file exists. */
2160 PVDFILE pIt;
2161 RTListForEach(&pGlob->ListFiles, pIt, VDFILE, Node)
2162 {
2163 if (!RTStrCmp(pIt->pszName, pcszFilename))
2164 {
2165 fFound = true;
2166 break;
2167 }
2168 }
2169
2170 if (fFound)
2171 {
2172 RTListNodeRemove(&pIt->Node);
2173 VDIoBackendStorageDestroy(pIt->pIoStorage);
2174 RTStrFree(pIt->pszName);
2175 RTMemFree(pIt);
2176 }
2177 else
2178 rc = VERR_FILE_NOT_FOUND;
2179
2180 return rc;
2181}
2182
2183static DECLCALLBACK(int) tstVDIoFileMove(void *pvUser, const char *pcszSrc, const char *pcszDst, unsigned fMove)
2184{
2185 RT_NOREF1(fMove);
2186 int rc = VINF_SUCCESS;
2187 PVDTESTGLOB pGlob = (PVDTESTGLOB)pvUser;
2188 bool fFound = false;
2189
2190 /* Check if the file exists. */
2191 PVDFILE pIt;
2192 RTListForEach(&pGlob->ListFiles, pIt, VDFILE, Node)
2193 {
2194 if (!RTStrCmp(pIt->pszName, pcszSrc))
2195 {
2196 fFound = true;
2197 break;
2198 }
2199 }
2200
2201 if (fFound)
2202 {
2203 char *pszNew = RTStrDup(pcszDst);
2204 if (pszNew)
2205 {
2206 RTStrFree(pIt->pszName);
2207 pIt->pszName = pszNew;
2208 }
2209 else
2210 rc = VERR_NO_MEMORY;
2211 }
2212 else
2213 rc = VERR_FILE_NOT_FOUND;
2214
2215 return rc;
2216}
2217
2218static DECLCALLBACK(int) tstVDIoFileGetFreeSpace(void *pvUser, const char *pcszFilename, int64_t *pcbFreeSpace)
2219{
2220 RT_NOREF2(pvUser, pcszFilename);
2221 AssertPtrReturn(pcbFreeSpace, VERR_INVALID_POINTER);
2222
2223 *pcbFreeSpace = ~0ULL; /** @todo Implement */
2224 return VINF_SUCCESS;
2225}
2226
2227static DECLCALLBACK(int) tstVDIoFileGetModificationTime(void *pvUser, const char *pcszFilename, PRTTIMESPEC pModificationTime)
2228{
2229 RT_NOREF2(pvUser, pcszFilename);
2230 AssertPtrReturn(pModificationTime, VERR_INVALID_POINTER);
2231
2232 /** @todo Implement */
2233 return VINF_SUCCESS;
2234}
2235
2236static DECLCALLBACK(int) tstVDIoFileGetSize(void *pvUser, void *pStorage, uint64_t *pcbSize)
2237{
2238 RT_NOREF1(pvUser);
2239 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2240
2241 return VDIoBackendStorageGetSize(pIoStorage->pFile->pIoStorage, pcbSize);
2242}
2243
2244static DECLCALLBACK(int) tstVDIoFileSetSize(void *pvUser, void *pStorage, uint64_t cbSize)
2245{
2246 RT_NOREF1(pvUser);
2247 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2248
2249 return VDIoBackendStorageSetSize(pIoStorage->pFile->pIoStorage, cbSize);
2250}
2251
2252static DECLCALLBACK(int) tstVDIoFileSetAllocationSize(void *pvUser, void *pStorage, uint64_t cbSize, uint32_t fFlags)
2253{
2254 RT_NOREF4(pvUser, pStorage, cbSize, fFlags);
2255 return VERR_NOT_SUPPORTED;
2256}
2257
2258static DECLCALLBACK(int) tstVDIoFileWriteSync(void *pvUser, void *pStorage, uint64_t uOffset,
2259 const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten)
2260{
2261 RT_NOREF1(pvUser);
2262 int rc = VINF_SUCCESS;
2263 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2264
2265 RTSGBUF SgBuf;
2266 RTSGSEG Seg;
2267
2268 Seg.pvSeg = (void *)pvBuffer;
2269 Seg.cbSeg = cbBuffer;
2270 RTSgBufInit(&SgBuf, &Seg, 1);
2271 rc = VDIoBackendTransfer(pIoStorage->pFile->pIoStorage, VDIOTXDIR_WRITE, uOffset,
2272 cbBuffer, &SgBuf, NULL, true /* fSync */);
2273 if (RT_SUCCESS(rc))
2274 {
2275 pIoStorage->pFile->cWrites++;
2276 if (pcbWritten)
2277 *pcbWritten = cbBuffer;
2278 }
2279
2280 return rc;
2281}
2282
2283static DECLCALLBACK(int) tstVDIoFileReadSync(void *pvUser, void *pStorage, uint64_t uOffset,
2284 void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
2285{
2286 RT_NOREF1(pvUser);
2287 int rc = VINF_SUCCESS;
2288 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2289
2290 RTSGBUF SgBuf;
2291 RTSGSEG Seg;
2292
2293 Seg.pvSeg = pvBuffer;
2294 Seg.cbSeg = cbBuffer;
2295 RTSgBufInit(&SgBuf, &Seg, 1);
2296 rc = VDIoBackendTransfer(pIoStorage->pFile->pIoStorage, VDIOTXDIR_READ, uOffset,
2297 cbBuffer, &SgBuf, NULL, true /* fSync */);
2298 if (RT_SUCCESS(rc))
2299 {
2300 pIoStorage->pFile->cReads++;
2301 if (pcbRead)
2302 *pcbRead = cbBuffer;
2303 }
2304
2305 return rc;
2306}
2307
2308static DECLCALLBACK(int) tstVDIoFileFlushSync(void *pvUser, void *pStorage)
2309{
2310 RT_NOREF1(pvUser);
2311 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2312 int rc = VDIoBackendTransfer(pIoStorage->pFile->pIoStorage, VDIOTXDIR_FLUSH, 0,
2313 0, NULL, NULL, true /* fSync */);
2314 pIoStorage->pFile->cFlushes++;
2315 return rc;
2316}
2317
2318static DECLCALLBACK(int) tstVDIoFileReadAsync(void *pvUser, void *pStorage, uint64_t uOffset,
2319 PCRTSGSEG paSegments, size_t cSegments,
2320 size_t cbRead, void *pvCompletion,
2321 void **ppTask)
2322{
2323 RT_NOREF2(pvUser, ppTask);
2324 int rc = VINF_SUCCESS;
2325 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2326 RTSGBUF SgBuf;
2327
2328 RTSgBufInit(&SgBuf, paSegments, cSegments);
2329 rc = VDIoBackendTransfer(pIoStorage->pFile->pIoStorage, VDIOTXDIR_READ, uOffset,
2330 cbRead, &SgBuf, pvCompletion, false /* fSync */);
2331 if (RT_SUCCESS(rc))
2332 {
2333 pIoStorage->pFile->cAsyncReads++;
2334 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2335 }
2336
2337 return rc;
2338}
2339
2340static DECLCALLBACK(int) tstVDIoFileWriteAsync(void *pvUser, void *pStorage, uint64_t uOffset,
2341 PCRTSGSEG paSegments, size_t cSegments,
2342 size_t cbWrite, void *pvCompletion,
2343 void **ppTask)
2344{
2345 RT_NOREF2(pvUser, ppTask);
2346 int rc = VINF_SUCCESS;
2347 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2348 RTSGBUF SgBuf;
2349
2350 RTSgBufInit(&SgBuf, paSegments, cSegments);
2351 rc = VDIoBackendTransfer(pIoStorage->pFile->pIoStorage, VDIOTXDIR_WRITE, uOffset,
2352 cbWrite, &SgBuf, pvCompletion, false /* fSync */);
2353 if (RT_SUCCESS(rc))
2354 {
2355 pIoStorage->pFile->cAsyncWrites++;
2356 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2357 }
2358
2359 return rc;
2360}
2361
2362static DECLCALLBACK(int) tstVDIoFileFlushAsync(void *pvUser, void *pStorage, void *pvCompletion,
2363 void **ppTask)
2364{
2365 RT_NOREF2(pvUser, ppTask);
2366 int rc = VINF_SUCCESS;
2367 PVDSTORAGE pIoStorage = (PVDSTORAGE)pStorage;
2368
2369 rc = VDIoBackendTransfer(pIoStorage->pFile->pIoStorage, VDIOTXDIR_FLUSH, 0,
2370 0, NULL, pvCompletion, false /* fSync */);
2371 if (RT_SUCCESS(rc))
2372 {
2373 pIoStorage->pFile->cAsyncFlushes++;
2374 rc = VERR_VD_ASYNC_IO_IN_PROGRESS;
2375 }
2376
2377 return rc;
2378}
2379
2380static int tstVDIoTestInit(PVDIOTEST pIoTest, PVDTESTGLOB pGlob, bool fRandomAcc, uint32_t cSegsMax,
2381 uint64_t cbIo, size_t cbBlkSize, uint64_t offStart, uint64_t offEnd,
2382 unsigned uWriteChance, PVDPATTERN pPattern)
2383{
2384 int rc = VINF_SUCCESS;
2385
2386 RT_ZERO(*pIoTest);
2387 pIoTest->fRandomAccess = fRandomAcc;
2388 pIoTest->cbIo = cbIo;
2389 pIoTest->cbBlkIo = cbBlkSize;
2390 pIoTest->offStart = offStart;
2391 pIoTest->offEnd = offEnd;
2392 pIoTest->uWriteChance = uWriteChance;
2393 pIoTest->cSegsMax = cSegsMax;
2394 pIoTest->pIoRnd = pGlob->pIoRnd;
2395 pIoTest->pPattern = pPattern;
2396
2397 if (fRandomAcc)
2398 {
2399 uint64_t cbRange = pIoTest->offEnd < pIoTest->offStart
2400 ? pIoTest->offStart - pIoTest->offEnd
2401 : pIoTest->offEnd - pIoTest->offStart;
2402
2403 pIoTest->u.Rnd.cBlocks = (uint32_t)(cbRange / cbBlkSize + (cbRange % cbBlkSize ? 1 : 0));
2404 pIoTest->u.Rnd.cBlocksLeft = pIoTest->u.Rnd.cBlocks;
2405 pIoTest->u.Rnd.pbMapAccessed = (uint8_t *)RTMemAllocZ(pIoTest->u.Rnd.cBlocks / 8
2406 + ((pIoTest->u.Rnd.cBlocks % 8)
2407 ? 1
2408 : 0));
2409 if (!pIoTest->u.Rnd.pbMapAccessed)
2410 rc = VERR_NO_MEMORY;
2411 }
2412 else
2413 pIoTest->u.offNext = pIoTest->offEnd < pIoTest->offStart ? pIoTest->offStart - cbBlkSize : offStart;
2414
2415 return rc;
2416}
2417
2418static void tstVDIoTestDestroy(PVDIOTEST pIoTest)
2419{
2420 if (pIoTest->fRandomAccess)
2421 RTMemFree(pIoTest->u.Rnd.pbMapAccessed);
2422}
2423
2424static bool tstVDIoTestRunning(PVDIOTEST pIoTest)
2425{
2426 return pIoTest->cbIo > 0;
2427}
2428
2429static bool tstVDIoTestReqOutstanding(PTSTVDIOREQ pIoReq)
2430{
2431 return pIoReq->fOutstanding;
2432}
2433
2434static uint32_t tstVDIoTestReqInitSegments(PVDIOTEST pIoTest, PRTSGSEG paSegs, uint32_t cSegs, void *pvBuf, size_t cbBuf)
2435{
2436 uint8_t *pbBuf = (uint8_t *)pvBuf;
2437 size_t cSectorsLeft = cbBuf / 512;
2438 uint32_t iSeg = 0;
2439
2440 /* Init all but the last segment which needs to take the rest. */
2441 while ( iSeg < cSegs - 1
2442 && cSectorsLeft)
2443 {
2444 uint32_t cThisSectors = VDIoRndGetU32Ex(pIoTest->pIoRnd, 1, (uint32_t)cSectorsLeft / 2);
2445 size_t cbThisBuf = cThisSectors * 512;
2446
2447 paSegs[iSeg].pvSeg = pbBuf;
2448 paSegs[iSeg].cbSeg = cbThisBuf;
2449 pbBuf += cbThisBuf;
2450 cSectorsLeft -= cThisSectors;
2451 iSeg++;
2452 }
2453
2454 if (cSectorsLeft)
2455 {
2456 paSegs[iSeg].pvSeg = pbBuf;
2457 paSegs[iSeg].cbSeg = cSectorsLeft * 512;
2458 iSeg++;
2459 }
2460
2461 return iSeg;
2462}
2463
2464/**
2465 * Returns true with the given chance in percent.
2466 *
2467 * @returns true or false
2468 * @param iPercentage The percentage of the chance to return true.
2469 */
2470static bool tstVDIoTestIsTrue(PVDIOTEST pIoTest, int iPercentage)
2471{
2472 int uRnd = VDIoRndGetU32Ex(pIoTest->pIoRnd, 0, 100);
2473
2474 return (uRnd < iPercentage); /* This should be enough for our purpose */
2475}
2476
2477static int tstVDIoTestReqInit(PVDIOTEST pIoTest, PTSTVDIOREQ pIoReq, void *pvUser)
2478{
2479 int rc = VINF_SUCCESS;
2480
2481 if (pIoTest->cbIo)
2482 {
2483 /* Read or Write? */
2484 pIoReq->enmTxDir = tstVDIoTestIsTrue(pIoTest, pIoTest->uWriteChance) ? TSTVDIOREQTXDIR_WRITE : TSTVDIOREQTXDIR_READ;
2485 pIoReq->cbReq = RT_MIN(pIoTest->cbBlkIo, pIoTest->cbIo);
2486 pIoTest->cbIo -= pIoReq->cbReq;
2487
2488 void *pvBuf = NULL;
2489
2490 if (pIoReq->enmTxDir == TSTVDIOREQTXDIR_WRITE)
2491 {
2492 if (pIoTest->pPattern)
2493 rc = tstVDIoPatternGetBuffer(pIoTest->pPattern, &pvBuf, pIoReq->cbReq);
2494 else
2495 rc = VDIoRndGetBuffer(pIoTest->pIoRnd, &pvBuf, pIoReq->cbReq);
2496 AssertRC(rc);
2497 }
2498 else
2499 {
2500 /* Read */
2501 pvBuf = pIoReq->pvBufRead;
2502 }
2503
2504 if (RT_SUCCESS(rc))
2505 {
2506 pIoReq->pvBuf = pvBuf;
2507 uint32_t cSegsMax = VDIoRndGetU32Ex(pIoTest->pIoRnd, 1, RT_MIN(pIoTest->cSegsMax, RT_ELEMENTS(pIoReq->aSegs)));
2508 pIoReq->cSegs = tstVDIoTestReqInitSegments(pIoTest, &pIoReq->aSegs[0], cSegsMax, pvBuf, pIoReq->cbReq);
2509 RTSgBufInit(&pIoReq->SgBuf, &pIoReq->aSegs[0], pIoReq->cSegs);
2510
2511 if (pIoTest->fRandomAccess)
2512 {
2513 int idx = -1;
2514
2515 idx = ASMBitFirstClear(pIoTest->u.Rnd.pbMapAccessed, pIoTest->u.Rnd.cBlocks);
2516
2517 /* In case this is the last request we don't need to search further. */
2518 if (pIoTest->u.Rnd.cBlocksLeft > 1)
2519 {
2520 int idxIo;
2521 idxIo = VDIoRndGetU32Ex(pIoTest->pIoRnd, idx, pIoTest->u.Rnd.cBlocks - 1);
2522
2523 /*
2524 * If the bit is marked free use it, otherwise search for the next free bit
2525 * and if that doesn't work use the first free bit.
2526 */
2527 if (ASMBitTest(pIoTest->u.Rnd.pbMapAccessed, idxIo))
2528 {
2529 idxIo = ASMBitNextClear(pIoTest->u.Rnd.pbMapAccessed, pIoTest->u.Rnd.cBlocks, idxIo);
2530 if (idxIo != -1)
2531 idx = idxIo;
2532 }
2533 else
2534 idx = idxIo;
2535 }
2536
2537 Assert(idx != -1);
2538 pIoReq->off = (uint64_t)idx * pIoTest->cbBlkIo;
2539 pIoTest->u.Rnd.cBlocksLeft--;
2540 if (!pIoTest->u.Rnd.cBlocksLeft)
2541 {
2542 /* New round, clear everything. */
2543 ASMBitClearRange(pIoTest->u.Rnd.pbMapAccessed, 0, pIoTest->u.Rnd.cBlocks);
2544 pIoTest->u.Rnd.cBlocksLeft = pIoTest->u.Rnd.cBlocks;
2545 }
2546 else
2547 ASMBitSet(pIoTest->u.Rnd.pbMapAccessed, idx);
2548 }
2549 else
2550 {
2551 pIoReq->off = pIoTest->u.offNext;
2552 if (pIoTest->offEnd < pIoTest->offStart)
2553 {
2554 pIoTest->u.offNext = pIoTest->u.offNext == 0
2555 ? pIoTest->offEnd - pIoTest->cbBlkIo
2556 : RT_MAX(pIoTest->offEnd, pIoTest->u.offNext - pIoTest->cbBlkIo);
2557 }
2558 else
2559 {
2560 pIoTest->u.offNext = pIoTest->u.offNext + pIoTest->cbBlkIo >= pIoTest->offEnd
2561 ? 0
2562 : RT_MIN(pIoTest->offEnd, pIoTest->u.offNext + pIoTest->cbBlkIo);
2563 }
2564 }
2565 pIoReq->pvUser = pvUser;
2566 pIoReq->fOutstanding = true;
2567 }
2568 }
2569 else
2570 rc = VERR_ACCESS_DENIED;
2571
2572 return rc;
2573}
2574
2575static DECLCALLBACK(void) tstVDIoTestReqComplete(void *pvUser1, void *pvUser2, int rcReq)
2576{
2577 RT_NOREF1(rcReq);
2578 PTSTVDIOREQ pIoReq = (PTSTVDIOREQ)pvUser1;
2579 RTSEMEVENT hEventSem = (RTSEMEVENT)pvUser2;
2580 PVDDISK pDisk = (PVDDISK)pIoReq->pvUser;
2581
2582 LogFlow(("Request %d completed\n", pIoReq->idx));
2583
2584 if (pDisk->pMemDiskVerify)
2585 {
2586 switch (pIoReq->enmTxDir)
2587 {
2588 case TSTVDIOREQTXDIR_READ:
2589 {
2590 RTCritSectEnter(&pDisk->CritSectVerify);
2591
2592 RTSGBUF SgBufCmp;
2593 RTSGSEG SegCmp;
2594 SegCmp.pvSeg = pIoReq->pvBuf;
2595 SegCmp.cbSeg = pIoReq->cbReq;
2596 RTSgBufInit(&SgBufCmp, &SegCmp, 1);
2597
2598 if (VDMemDiskCmp(pDisk->pMemDiskVerify, pIoReq->off, pIoReq->cbReq,
2599 &SgBufCmp))
2600 RTTestFailed(pDisk->pTestGlob->hTest, "Corrupted disk at offset %llu!\n", pIoReq->off);
2601 RTCritSectLeave(&pDisk->CritSectVerify);
2602 break;
2603 }
2604 case TSTVDIOREQTXDIR_WRITE:
2605 {
2606 RTCritSectEnter(&pDisk->CritSectVerify);
2607
2608 RTSGBUF SgBuf;
2609 RTSGSEG Seg;
2610 Seg.pvSeg = pIoReq->pvBuf;
2611 Seg.cbSeg = pIoReq->cbReq;
2612 RTSgBufInit(&SgBuf, &Seg, 1);
2613
2614 int rc = VDMemDiskWrite(pDisk->pMemDiskVerify, pIoReq->off, pIoReq->cbReq,
2615 &SgBuf);
2616 AssertRC(rc);
2617 RTCritSectLeave(&pDisk->CritSectVerify);
2618 break;
2619 }
2620 case TSTVDIOREQTXDIR_FLUSH:
2621 case TSTVDIOREQTXDIR_DISCARD:
2622 break;
2623 }
2624 }
2625
2626 ASMAtomicXchgBool(&pIoReq->fOutstanding, false);
2627 RTSemEventSignal(hEventSem);
2628 return;
2629}
2630
2631/**
2632 * Returns the disk handle by name or NULL if not found
2633 *
2634 * @returns Disk handle or NULL if the disk could not be found.
2635 *
2636 * @param pGlob Global test state.
2637 * @param pcszDisk Name of the disk to get.
2638 */
2639static PVDDISK tstVDIoGetDiskByName(PVDTESTGLOB pGlob, const char *pcszDisk)
2640{
2641 bool fFound = false;
2642
2643 LogFlowFunc(("pGlob=%#p pcszDisk=%s\n", pGlob, pcszDisk));
2644
2645 PVDDISK pIt;
2646 RTListForEach(&pGlob->ListDisks, pIt, VDDISK, ListNode)
2647 {
2648 if (!RTStrCmp(pIt->pszName, pcszDisk))
2649 {
2650 fFound = true;
2651 break;
2652 }
2653 }
2654
2655 LogFlowFunc(("return %#p\n", fFound ? pIt : NULL));
2656 return fFound ? pIt : NULL;
2657}
2658
2659/**
2660 * Returns the I/O pattern handle by name of NULL if not found.
2661 *
2662 * @returns I/O pattern handle or NULL if the pattern could not be found.
2663 *
2664 * @param pGlob Global test state.
2665 * @param pcszName Name of the pattern.
2666 */
2667static PVDPATTERN tstVDIoGetPatternByName(PVDTESTGLOB pGlob, const char *pcszName)
2668{
2669 bool fFound = false;
2670
2671 LogFlowFunc(("pGlob=%#p pcszName=%s\n", pGlob, pcszName));
2672
2673 PVDPATTERN pIt;
2674 RTListForEach(&pGlob->ListPatterns, pIt, VDPATTERN, ListNode)
2675 {
2676 if (!RTStrCmp(pIt->pszName, pcszName))
2677 {
2678 fFound = true;
2679 break;
2680 }
2681 }
2682
2683 LogFlowFunc(("return %#p\n", fFound ? pIt : NULL));
2684 return fFound ? pIt : NULL;
2685}
2686
2687/**
2688 * Creates a new pattern with the given name and an
2689 * allocated pattern buffer.
2690 *
2691 * @returns Pointer to a new pattern buffer or NULL on failure.
2692 * @param pcszName Name of the pattern.
2693 * @param cbPattern Size of the pattern buffer.
2694 */
2695static PVDPATTERN tstVDIoPatternCreate(const char *pcszName, size_t cbPattern)
2696{
2697 PVDPATTERN pPattern = (PVDPATTERN)RTMemAllocZ(sizeof(VDPATTERN));
2698 char *pszName = RTStrDup(pcszName);
2699 void *pvPattern = RTMemAllocZ(cbPattern);
2700
2701 if (pPattern && pszName && pvPattern)
2702 {
2703 pPattern->pszName = pszName;
2704 pPattern->pvPattern = pvPattern;
2705 pPattern->cbPattern = cbPattern;
2706 }
2707 else
2708 {
2709 if (pPattern)
2710 RTMemFree(pPattern);
2711 if (pszName)
2712 RTStrFree(pszName);
2713 if (pvPattern)
2714 RTMemFree(pvPattern);
2715
2716 pPattern = NULL;
2717 pszName = NULL;
2718 pvPattern = NULL;
2719 }
2720
2721 return pPattern;
2722}
2723
2724static int tstVDIoPatternGetBuffer(PVDPATTERN pPattern, void **ppv, size_t cb)
2725{
2726 AssertPtrReturn(pPattern, VERR_INVALID_POINTER);
2727 AssertPtrReturn(ppv, VERR_INVALID_POINTER);
2728 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
2729
2730 if (cb > pPattern->cbPattern)
2731 return VERR_INVALID_PARAMETER;
2732
2733 *ppv = pPattern->pvPattern;
2734 return VINF_SUCCESS;
2735}
2736
2737/**
2738 * Executes the given script.
2739 *
2740 * @returns nothing.
2741 * @param pszName The script name.
2742 * @param pszScript The script to execute.
2743 */
2744static void tstVDIoScriptExec(const char *pszName, const char *pszScript)
2745{
2746 int rc = VINF_SUCCESS;
2747 VDTESTGLOB GlobTest; /**< Global test data. */
2748
2749 memset(&GlobTest, 0, sizeof(VDTESTGLOB));
2750 RTListInit(&GlobTest.ListFiles);
2751 RTListInit(&GlobTest.ListDisks);
2752 RTListInit(&GlobTest.ListPatterns);
2753 GlobTest.pszIoBackend = RTStrDup("memory");
2754 if (!GlobTest.pszIoBackend)
2755 {
2756 RTPrintf("Out of memory allocating I/O backend string\n");
2757 return;
2758 }
2759
2760 /* Init global test data. */
2761 GlobTest.VDIfError.pfnError = tstVDError;
2762 GlobTest.VDIfError.pfnMessage = tstVDMessage;
2763
2764 rc = VDInterfaceAdd(&GlobTest.VDIfError.Core, "tstVDIo_VDIError", VDINTERFACETYPE_ERROR,
2765 NULL, sizeof(VDINTERFACEERROR), &GlobTest.pInterfacesDisk);
2766 AssertRC(rc);
2767
2768 GlobTest.VDIfIo.pfnOpen = tstVDIoFileOpen;
2769 GlobTest.VDIfIo.pfnClose = tstVDIoFileClose;
2770 GlobTest.VDIfIo.pfnDelete = tstVDIoFileDelete;
2771 GlobTest.VDIfIo.pfnMove = tstVDIoFileMove;
2772 GlobTest.VDIfIo.pfnGetFreeSpace = tstVDIoFileGetFreeSpace;
2773 GlobTest.VDIfIo.pfnGetModificationTime = tstVDIoFileGetModificationTime;
2774 GlobTest.VDIfIo.pfnGetSize = tstVDIoFileGetSize;
2775 GlobTest.VDIfIo.pfnSetSize = tstVDIoFileSetSize;
2776 GlobTest.VDIfIo.pfnSetAllocationSize = tstVDIoFileSetAllocationSize;
2777 GlobTest.VDIfIo.pfnWriteSync = tstVDIoFileWriteSync;
2778 GlobTest.VDIfIo.pfnReadSync = tstVDIoFileReadSync;
2779 GlobTest.VDIfIo.pfnFlushSync = tstVDIoFileFlushSync;
2780 GlobTest.VDIfIo.pfnReadAsync = tstVDIoFileReadAsync;
2781 GlobTest.VDIfIo.pfnWriteAsync = tstVDIoFileWriteAsync;
2782 GlobTest.VDIfIo.pfnFlushAsync = tstVDIoFileFlushAsync;
2783
2784 rc = VDInterfaceAdd(&GlobTest.VDIfIo.Core, "tstVDIo_VDIIo", VDINTERFACETYPE_IO,
2785 &GlobTest, sizeof(VDINTERFACEIO), &GlobTest.pInterfacesImages);
2786 AssertRC(rc);
2787
2788 rc = RTTestCreate(pszName, &GlobTest.hTest);
2789 if (RT_SUCCESS(rc))
2790 {
2791 /* Init I/O backend. */
2792 rc = VDIoBackendCreate(&GlobTest.pIoBackend);
2793 if (RT_SUCCESS(rc))
2794 {
2795 VDSCRIPTCTX hScriptCtx = NULL;
2796 rc = VDScriptCtxCreate(&hScriptCtx);
2797 if (RT_SUCCESS(rc))
2798 {
2799 RTTEST_CHECK_RC_OK(GlobTest.hTest,
2800 VDScriptCtxCallbacksRegister(hScriptCtx, g_aScriptActions, g_cScriptActions, &GlobTest));
2801
2802 RTTestBanner(GlobTest.hTest);
2803 rc = VDScriptCtxLoadScript(hScriptCtx, pszScript);
2804 if (RT_FAILURE(rc))
2805 {
2806 RTPrintf("Loading the script failed rc=%Rrc\n", rc);
2807 }
2808 else
2809 rc = VDScriptCtxCallFn(hScriptCtx, "main", NULL, 0);
2810 VDScriptCtxDestroy(hScriptCtx);
2811 }
2812
2813 /* Clean up all leftover resources. */
2814 PVDPATTERN pPatternIt, pPatternItNext;
2815 RTListForEachSafe(&GlobTest.ListPatterns, pPatternIt, pPatternItNext, VDPATTERN, ListNode)
2816 {
2817 RTPrintf("Cleanup: Leftover pattern \"%s\", deleting...\n", pPatternIt->pszName);
2818 RTListNodeRemove(&pPatternIt->ListNode);
2819 RTMemFree(pPatternIt->pvPattern);
2820 RTStrFree(pPatternIt->pszName);
2821 RTMemFree(pPatternIt);
2822 }
2823
2824 PVDDISK pDiskIt, pDiskItNext;
2825 RTListForEachSafe(&GlobTest.ListDisks, pDiskIt, pDiskItNext, VDDISK, ListNode)
2826 {
2827 RTPrintf("Cleanup: Leftover disk \"%s\", deleting...\n", pDiskIt->pszName);
2828 RTListNodeRemove(&pDiskIt->ListNode);
2829 VDDestroy(pDiskIt->pVD);
2830 if (pDiskIt->pMemDiskVerify)
2831 {
2832 VDMemDiskDestroy(pDiskIt->pMemDiskVerify);
2833 RTCritSectDelete(&pDiskIt->CritSectVerify);
2834 }
2835 RTStrFree(pDiskIt->pszName);
2836 RTMemFree(pDiskIt);
2837 }
2838
2839 PVDFILE pFileIt, pFileItNext;
2840 RTListForEachSafe(&GlobTest.ListFiles, pFileIt, pFileItNext, VDFILE, Node)
2841 {
2842 RTPrintf("Cleanup: Leftover file \"%s\", deleting...\n", pFileIt->pszName);
2843 RTListNodeRemove(&pFileIt->Node);
2844 VDIoBackendStorageDestroy(pFileIt->pIoStorage);
2845 RTStrFree(pFileIt->pszName);
2846 RTMemFree(pFileIt);
2847 }
2848
2849 VDIoBackendDestroy(GlobTest.pIoBackend);
2850 }
2851 else
2852 RTPrintf("Creating the I/O backend failed rc=%Rrc\n", rc);
2853
2854 RTTestSummaryAndDestroy(GlobTest.hTest);
2855 }
2856 else
2857 RTStrmPrintf(g_pStdErr, "tstVDIo: fatal error: RTTestCreate failed with rc=%Rrc\n", rc);
2858
2859 RTStrFree(GlobTest.pszIoBackend);
2860}
2861
2862/**
2863 * Executes the given I/O script using the new scripting engine.
2864 *
2865 * @returns nothing.
2866 *
2867 * @param pcszFilename The script to execute.
2868 */
2869static void tstVDIoScriptRun(const char *pcszFilename)
2870{
2871 int rc = VINF_SUCCESS;
2872 void *pvFile = NULL;
2873 size_t cbFile = 0;
2874
2875 rc = RTFileReadAll(pcszFilename, &pvFile, &cbFile);
2876 if (RT_SUCCESS(rc))
2877 {
2878 char *pszScript = RTStrDupN((char *)pvFile, cbFile);
2879 RTFileReadAllFree(pvFile, cbFile);
2880
2881 AssertPtr(pszScript);
2882 tstVDIoScriptExec(pcszFilename, pszScript);
2883 RTStrFree(pszScript);
2884 }
2885 else
2886 RTPrintf("Opening the script failed: %Rrc\n", rc);
2887
2888}
2889
2890/**
2891 * Run builtin tests.
2892 *
2893 * @returns nothing.
2894 */
2895static void tstVDIoRunBuiltinTests(void)
2896{
2897 /* 32bit hosts are excluded because of the 4GB address space. */
2898#if HC_ARCH_BITS == 32
2899 RTStrmPrintf(g_pStdErr, "tstVDIo: Running on a 32bit host is not supported for the builtin tests, skipping\n");
2900 return;
2901#else
2902 /*
2903 * We need quite a bit of RAM for the builtin tests. Skip it if there
2904 * is not enough free RAM available.
2905 */
2906 uint64_t cbFree = 0;
2907 int rc = RTSystemQueryAvailableRam(&cbFree);
2908 if ( RT_FAILURE(rc)
2909 || cbFree < (UINT64_C(6) * _1G))
2910 {
2911 RTStrmPrintf(g_pStdErr, "tstVDIo: fatal error: Failed to query available RAM or not enough available, skipping (rc=%Rrc cbFree=%llu)\n",
2912 rc, cbFree);
2913 return;
2914 }
2915
2916 for (unsigned i = 0; i < g_cVDIoTests; i++)
2917 {
2918 char *pszScript = RTStrDupN((const char *)g_aVDIoTests[i].pch, g_aVDIoTests[i].cb);
2919
2920 AssertPtr(pszScript);
2921 tstVDIoScriptExec(g_aVDIoTests[i].pszName, pszScript);
2922 RTStrFree(pszScript);
2923 }
2924#endif
2925}
2926
2927/**
2928 * Shows help message.
2929 */
2930static void printUsage(void)
2931{
2932 RTPrintf("Usage:\n"
2933 "--script <filename> Script to execute\n");
2934}
2935
2936static const RTGETOPTDEF g_aOptions[] =
2937{
2938 { "--script", 's', RTGETOPT_REQ_STRING },
2939 { "--help", 'h', RTGETOPT_REQ_NOTHING }
2940};
2941
2942int main(int argc, char *argv[])
2943{
2944 RTR3InitExe(argc, &argv, 0);
2945 int rc;
2946 RTGETOPTUNION ValueUnion;
2947 RTGETOPTSTATE GetState;
2948 char c;
2949
2950 rc = VDInit();
2951 if (RT_FAILURE(rc))
2952 return RTEXITCODE_FAILURE;
2953
2954 if (argc == 1)
2955 {
2956 tstVDIoRunBuiltinTests();
2957 return RTEXITCODE_SUCCESS;
2958 }
2959
2960 RTGetOptInit(&GetState, argc, argv, g_aOptions,
2961 RT_ELEMENTS(g_aOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
2962
2963 while ( RT_SUCCESS(rc)
2964 && (c = RTGetOpt(&GetState, &ValueUnion)))
2965 {
2966 switch (c)
2967 {
2968 case 's':
2969 tstVDIoScriptRun(ValueUnion.psz);
2970 break;
2971 case 'h':
2972 printUsage();
2973 break;
2974 default: /* Default is to run built in tests if no arguments are given (automated testing). */
2975 tstVDIoRunBuiltinTests();
2976 }
2977 }
2978
2979 rc = VDShutdown();
2980 if (RT_FAILURE(rc))
2981 RTPrintf("tstVDIo: unloading backends failed! rc=%Rrc\n", rc);
2982
2983 return RTEXITCODE_SUCCESS;
2984}
2985
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