VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 7 weeks ago

Copyright year updates by scm.

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