blob: 6747fb613d6e31317371c868c553d7cfa257591c [file] [log] [blame]
drh9c06c952005-11-26 00:25:00 +00001/*
2** 2004 May 22
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This file contains code that modified the OS layer in order to simulate
14** the effect on the database file of an OS crash or power failure. This
15** is used to test the ability of SQLite to recover from those situations.
16*/
danielk1977f55b8992007-08-24 08:15:53 +000017#if SQLITE_TEST /* This file is used for testing only */
drh9c06c952005-11-26 00:25:00 +000018#include "sqliteInt.h"
drh9c06c952005-11-26 00:25:00 +000019#include "tcl.h"
20
drh198bf392006-01-06 21:52:49 +000021#ifndef SQLITE_OMIT_DISKIO /* This file is a no-op if disk I/O is disabled */
22
danielk1977f8940ae2007-08-23 11:07:10 +000023/* #define TRACE_CRASHTEST */
24
danielk197762079062007-08-15 17:08:46 +000025typedef struct CrashFile CrashFile;
26typedef struct CrashGlobal CrashGlobal;
danielk19770d24e6b2007-08-14 17:42:05 +000027typedef struct WriteBuffer WriteBuffer;
28
29/*
30** Method:
31**
32** This layer is implemented as a wrapper around the "real"
33** sqlite3_file object for the host system. Each time data is
34** written to the file object, instead of being written to the
35** underlying file, the write operation is stored in an in-memory
36** structure (type WriteBuffer). This structure is placed at the
37** end of a global ordered list (the write-list).
38**
39** When data is read from a file object, the requested region is
40** first retrieved from the real file. The write-list is then
41** traversed and data copied from any overlapping WriteBuffer
42** structures to the output buffer. i.e. a read() operation following
43** one or more write() operations works as expected, even if no
44** data has actually been written out to the real file.
45**
46** When a fsync() operation is performed, an operating system crash
47** may be simulated, in which case exit(-1) is called (the call to
48** xSync() never returns). Whether or not a crash is simulated,
49** the data associated with a subset of the WriteBuffer structures
50** stored in the write-list is written to the real underlying files
51** and the entries removed from the write-list. If a crash is simulated,
52** a subset of the buffers may be corrupted before the data is written.
53**
54** The exact subset of the write-list written and/or corrupted is
55** determined by the simulated device characteristics and sector-size.
56**
57** "Normal" mode:
58**
59** Normal mode is used when the simulated device has none of the
60** SQLITE_IOCAP_XXX flags set.
61**
62** In normal mode, if the fsync() is not a simulated crash, the
63** write-list is traversed from beginning to end. Each WriteBuffer
64** structure associated with the file handle used to call xSync()
65** is written to the real file and removed from the write-list.
66**
67** If a crash is simulated, one of the following takes place for
68** each WriteBuffer in the write-list, regardless of which
69** file-handle it is associated with:
70**
71** 1. The buffer is correctly written to the file, just as if
72** a crash were not being simulated.
73**
74** 2. Nothing is done.
75**
76** 3. Garbage data is written to all sectors of the file that
77** overlap the region specified by the WriteBuffer. Or garbage
78** data is written to some contiguous section within the
79** overlapped sectors.
80**
81** Device Characteristic flag handling:
82**
83** If the IOCAP_ATOMIC flag is set, then option (3) above is
84** never selected.
85**
86** If the IOCAP_ATOMIC512 flag is set, and the WriteBuffer represents
87** an aligned write() of an integer number of 512 byte regions, then
88** option (3) above is never selected. Instead, each 512 byte region
89** is either correctly written or left completely untouched. Similar
90** logic governs the behaviour if any of the other ATOMICXXX flags
91** is set.
92**
93** If either the IOCAP_SAFEAPPEND or IOCAP_SEQUENTIAL flags are set
94** and a crash is being simulated, then an entry of the write-list is
95** selected at random. Everything in the list after the selected entry
96** is discarded before processing begins.
97**
98** If IOCAP_SEQUENTIAL is set and a crash is being simulated, option
99** (1) is selected for all write-list entries except the last. If a
100** crash is not being simulated, then all entries in the write-list
101** that occur before at least one write() on the file-handle specified
102** as part of the xSync() are written to their associated real files.
103**
104** If IOCAP_SAFEAPPEND is set and the first byte written by the write()
105** operation is one byte past the current end of the file, then option
106** (1) is always selected.
107*/
danielk197762079062007-08-15 17:08:46 +0000108
109/*
110** Each write operation in the write-list is represented by an instance
111** of the following structure.
112**
113** If zBuf is 0, then this structure represents a call to xTruncate(),
114** not xWrite(). In that case, iOffset is the size that the file is
115** truncated to.
116*/
danielk19770d24e6b2007-08-14 17:42:05 +0000117struct WriteBuffer {
danielk197762079062007-08-15 17:08:46 +0000118 i64 iOffset; /* Byte offset of the start of this write() */
119 int nBuf; /* Number of bytes written */
120 u8 *zBuf; /* Pointer to copy of written data */
121 CrashFile *pFile; /* File this write() applies to */
122
123 WriteBuffer *pNext; /* Next in CrashGlobal.pWriteList */
danielk19770d24e6b2007-08-14 17:42:05 +0000124};
125
danielk197762079062007-08-15 17:08:46 +0000126struct CrashFile {
127 const sqlite3_io_methods *pMethod; /* Must be first */
128 sqlite3_file *pRealFile; /* Underlying "real" file handle */
danielk19771e536952007-08-16 10:09:01 +0000129 char *zName;
danielk1977967a4a12007-08-20 14:23:44 +0000130
danielk1977f1da17a2007-08-21 13:07:46 +0000131 /* Cache of the entire file. This is used to speed up OsRead() and
132 ** OsFileSize() calls. Although both could be done by traversing the
133 ** write-list, in practice this is impractically slow.
134 */
danielk1977967a4a12007-08-20 14:23:44 +0000135 int iSize; /* Size of file in bytes */
136 int nData; /* Size of buffer allocated at zData */
137 u8 *zData; /* Buffer containing file contents */
drh9c06c952005-11-26 00:25:00 +0000138};
139
danielk197762079062007-08-15 17:08:46 +0000140struct CrashGlobal {
141 WriteBuffer *pWriteList; /* Head of write-list */
danielk1977967a4a12007-08-20 14:23:44 +0000142 WriteBuffer *pWriteListEnd; /* End of write-list */
drh9c06c952005-11-26 00:25:00 +0000143
danielk197762079062007-08-15 17:08:46 +0000144 int iSectorSize; /* Value of simulated sector size */
145 int iDeviceCharacteristics; /* Value of simulated device characteristics */
drh9c06c952005-11-26 00:25:00 +0000146
danielk197762079062007-08-15 17:08:46 +0000147 int iCrash; /* Crash on the iCrash'th call to xSync() */
148 char zCrashFile[500]; /* Crash during an xSync() on this file */
149};
drh9c06c952005-11-26 00:25:00 +0000150
danielk1977967a4a12007-08-20 14:23:44 +0000151static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0};
drh9c06c952005-11-26 00:25:00 +0000152
153/*
drh66560ad2006-01-06 14:32:19 +0000154** Set this global variable to 1 to enable crash testing.
155*/
danielk1977967a4a12007-08-20 14:23:44 +0000156static int sqlite3CrashTestEnable = 0;
drh66560ad2006-01-06 14:32:19 +0000157
158/*
danielk197762079062007-08-15 17:08:46 +0000159** Flush the write-list as if xSync() had been called on file handle
160** pFile. If isCrash is true, simulate a crash.
drh9c06c952005-11-26 00:25:00 +0000161*/
danielk197762079062007-08-15 17:08:46 +0000162static int writeListSync(CrashFile *pFile, int isCrash){
163 int rc = SQLITE_OK;
164 int iDc = g.iDeviceCharacteristics;
drh66560ad2006-01-06 14:32:19 +0000165
danielk197762079062007-08-15 17:08:46 +0000166 WriteBuffer *pWrite;
167 WriteBuffer **ppPtr;
drh66560ad2006-01-06 14:32:19 +0000168
danielk1977f55b8992007-08-24 08:15:53 +0000169 /* If this is not a crash simulation, set pFinal to point to the
170 ** last element of the write-list that is associated with file handle
171 ** pFile.
172 **
173 ** If this is a crash simulation, set pFinal to an arbitrarily selected
174 ** element of the write-list.
danielk197762079062007-08-15 17:08:46 +0000175 */
176 WriteBuffer *pFinal = 0;
177 if( !isCrash ){
178 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){
179 if( pWrite->pFile==pFile ){
180 pFinal = pWrite;
181 }
182 }
danielk1977f55b8992007-08-24 08:15:53 +0000183 }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){
184 int nWrite = 0;
185 int iFinal;
186 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++;
187 sqlite3Randomness(sizeof(int), &iFinal);
188 iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite;
189 for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--;
190 pFinal = pWrite;
drh1a235932005-11-29 18:37:15 +0000191 }
drh66560ad2006-01-06 14:32:19 +0000192
danielk1977f8940ae2007-08-23 11:07:10 +0000193#ifdef TRACE_CRASHTEST
194 printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a"));
195#endif
196
danielk197762079062007-08-15 17:08:46 +0000197 ppPtr = &g.pWriteList;
198 for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){
danielk1977967a4a12007-08-20 14:23:44 +0000199 sqlite3_file *pRealFile = pWrite->pFile->pRealFile;
danielk197762079062007-08-15 17:08:46 +0000200
201 /* (eAction==1) -> write block out normally,
202 ** (eAction==2) -> do nothing,
203 ** (eAction==3) -> trash sectors.
204 */
205 int eAction = 0;
206 if( !isCrash ){
207 eAction = 2;
208 if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){
209 eAction = 1;
210 }
211 }else{
212 char random;
213 sqlite3Randomness(1, &random);
214
danielk1977f55b8992007-08-24 08:15:53 +0000215 /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag
216 ** is set or this is an OsTruncate(), not an Oswrite().
217 */
218 if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){
danielk197762079062007-08-15 17:08:46 +0000219 random &= 0x01;
220 }
221
danielk1977f55b8992007-08-24 08:15:53 +0000222 /* If IOCAP_SEQUENTIAL is set and this is not the final entry
223 ** in the truncated write-list, always select option 1 (write
224 ** out correctly).
225 */
226 if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){
227 random = 0;
228 }
229
230 /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is
231 ** an append (first byte of the written region is 1 byte past the
232 ** current EOF), always select option 1 (write out correctly).
233 */
234 if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){
235 i64 iSize;
236 sqlite3OsFileSize(pRealFile, &iSize);
237 if( iSize==pWrite->iOffset ){
238 random = 0;
239 }
240 }
241
danielk197762079062007-08-15 17:08:46 +0000242 if( (random&0x06)==0x06 ){
243 eAction = 3;
244 }else{
245 eAction = ((random&0x01)?2:1);
246 }
247 }
248
249 switch( eAction ){
250 case 1: { /* Write out correctly */
251 if( pWrite->zBuf ){
252 rc = sqlite3OsWrite(
danielk1977967a4a12007-08-20 14:23:44 +0000253 pRealFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset
danielk197762079062007-08-15 17:08:46 +0000254 );
255 }else{
danielk1977967a4a12007-08-20 14:23:44 +0000256 rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset);
danielk197762079062007-08-15 17:08:46 +0000257 }
258 *ppPtr = pWrite->pNext;
danielk1977f8940ae2007-08-23 11:07:10 +0000259#ifdef TRACE_CRASHTEST
260 if( isCrash ){
danielk1977f55b8992007-08-24 08:15:53 +0000261 printf("Writing %d bytes @ %d (%s)\n",
262 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName
263 );
danielk1977f8940ae2007-08-23 11:07:10 +0000264 }
265#endif
drh17435752007-08-16 04:30:38 +0000266 sqlite3_free(pWrite);
danielk197762079062007-08-15 17:08:46 +0000267 break;
268 }
269 case 2: { /* Do nothing */
270 ppPtr = &pWrite->pNext;
danielk1977f8940ae2007-08-23 11:07:10 +0000271#ifdef TRACE_CRASHTEST
272 if( isCrash ){
danielk1977f55b8992007-08-24 08:15:53 +0000273 printf("Omiting %d bytes @ %d (%s)\n",
274 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName
275 );
danielk1977f8940ae2007-08-23 11:07:10 +0000276 }
277#endif
danielk197762079062007-08-15 17:08:46 +0000278 break;
279 }
280 case 3: { /* Trash sectors */
281 u8 *zGarbage;
danielk1977967a4a12007-08-20 14:23:44 +0000282 int iFirst = (pWrite->iOffset/g.iSectorSize);
283 int iLast = (pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize;
284
285 assert(pWrite->zBuf);
danielk197762079062007-08-15 17:08:46 +0000286
danielk1977f8940ae2007-08-23 11:07:10 +0000287#ifdef TRACE_CRASHTEST
danielk1977f55b8992007-08-24 08:15:53 +0000288 printf("Trashing %d sectors @ sector %d (%s)\n",
289 1+iLast-iFirst, iFirst, pWrite->pFile->zName
290 );
danielk1977f8940ae2007-08-23 11:07:10 +0000291#endif
292
drh17435752007-08-16 04:30:38 +0000293 zGarbage = sqlite3_malloc(g.iSectorSize);
danielk197762079062007-08-15 17:08:46 +0000294 if( zGarbage ){
295 sqlite3_int64 i;
296 for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){
297 sqlite3Randomness(g.iSectorSize, zGarbage);
298 rc = sqlite3OsWrite(
danielk1977967a4a12007-08-20 14:23:44 +0000299 pRealFile, zGarbage, g.iSectorSize, i*g.iSectorSize
danielk197762079062007-08-15 17:08:46 +0000300 );
301 }
drh17435752007-08-16 04:30:38 +0000302 sqlite3_free(zGarbage);
danielk197762079062007-08-15 17:08:46 +0000303 }else{
304 rc = SQLITE_NOMEM;
305 }
306
307 ppPtr = &pWrite->pNext;
308 break;
309 }
310
311 default:
312 assert(!"Cannot happen");
313 }
314
315 if( pWrite==pFinal ) break;
drh1a235932005-11-29 18:37:15 +0000316 }
danielk197762079062007-08-15 17:08:46 +0000317
318 if( rc==SQLITE_OK && isCrash ){
319 exit(-1);
320 }
321
danielk1977967a4a12007-08-20 14:23:44 +0000322 for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext);
323 g.pWriteListEnd = pWrite;
324
drh1a235932005-11-29 18:37:15 +0000325 return rc;
drh9c06c952005-11-26 00:25:00 +0000326}
327
328/*
danielk197762079062007-08-15 17:08:46 +0000329** Add an entry to the end of the write-list.
drh18839212005-11-26 03:43:23 +0000330*/
danielk197762079062007-08-15 17:08:46 +0000331static int writeListAppend(
332 sqlite3_file *pFile,
333 sqlite3_int64 iOffset,
334 const u8 *zBuf,
335 int nBuf
336){
337 WriteBuffer *pNew;
338
339 assert((zBuf && nBuf) || (!nBuf && !zBuf));
340
danielk1977b4b47412007-08-17 15:53:36 +0000341 pNew = (WriteBuffer *)sqlite3MallocZero(sizeof(WriteBuffer) + nBuf);
danielk197762079062007-08-15 17:08:46 +0000342 pNew->iOffset = iOffset;
343 pNew->nBuf = nBuf;
344 pNew->pFile = (CrashFile *)pFile;
345 if( zBuf ){
346 pNew->zBuf = (u8 *)&pNew[1];
347 memcpy(pNew->zBuf, zBuf, nBuf);
348 }
349
350 if( g.pWriteList ){
danielk1977967a4a12007-08-20 14:23:44 +0000351 assert(g.pWriteListEnd);
352 g.pWriteListEnd->pNext = pNew;
danielk197762079062007-08-15 17:08:46 +0000353 }else{
354 g.pWriteList = pNew;
355 }
danielk1977967a4a12007-08-20 14:23:44 +0000356 g.pWriteListEnd = pNew;
danielk197762079062007-08-15 17:08:46 +0000357
drh1a235932005-11-29 18:37:15 +0000358 return SQLITE_OK;
359}
360
361/*
danielk197762079062007-08-15 17:08:46 +0000362** Close a crash-file.
drh1a235932005-11-29 18:37:15 +0000363*/
danielk19771e536952007-08-16 10:09:01 +0000364static int cfClose(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000365 CrashFile *pCrash = (CrashFile *)pFile;
366 writeListSync(pCrash, 0);
danielk1977f1da17a2007-08-21 13:07:46 +0000367 sqlite3OsClose(pCrash->pRealFile);
danielk197762079062007-08-15 17:08:46 +0000368 return SQLITE_OK;
drh1a235932005-11-29 18:37:15 +0000369}
370
371/*
danielk197762079062007-08-15 17:08:46 +0000372** Read data from a crash-file.
drh1a235932005-11-29 18:37:15 +0000373*/
danielk19771e536952007-08-16 10:09:01 +0000374static int cfRead(
375 sqlite3_file *pFile,
376 void *zBuf,
377 int iAmt,
378 sqlite_int64 iOfst
379){
danielk197762079062007-08-15 17:08:46 +0000380 CrashFile *pCrash = (CrashFile *)pFile;
danielk197762079062007-08-15 17:08:46 +0000381
382 /* Check the file-size to see if this is a short-read */
danielk1977967a4a12007-08-20 14:23:44 +0000383 if( pCrash->iSize<(iOfst+iAmt) ){
danielk197762079062007-08-15 17:08:46 +0000384 return SQLITE_IOERR_SHORT_READ;
385 }
386
danielk1977967a4a12007-08-20 14:23:44 +0000387 memcpy(zBuf, &pCrash->zData[iOfst], iAmt);
388 return SQLITE_OK;
drh18839212005-11-26 03:43:23 +0000389}
390
391/*
danielk197762079062007-08-15 17:08:46 +0000392** Write data to a crash-file.
danielk1977b4721172007-03-19 05:54:48 +0000393*/
danielk19771e536952007-08-16 10:09:01 +0000394static int cfWrite(
395 sqlite3_file *pFile,
396 const void *zBuf,
397 int iAmt,
398 sqlite_int64 iOfst
399){
danielk1977967a4a12007-08-20 14:23:44 +0000400 CrashFile *pCrash = (CrashFile *)pFile;
401 if( iAmt+iOfst>pCrash->iSize ){
402 pCrash->iSize = iAmt+iOfst;
403 }
404 while( pCrash->iSize>pCrash->nData ){
drh4a50aac2007-08-23 02:47:53 +0000405 u8 *zNew;
danielk1977967a4a12007-08-20 14:23:44 +0000406 int nNew = (pCrash->nData*2) + 4096;
drh4a50aac2007-08-23 02:47:53 +0000407 zNew = sqlite3_realloc(pCrash->zData, nNew);
danielk1977967a4a12007-08-20 14:23:44 +0000408 if( !zNew ){
409 return SQLITE_NOMEM;
410 }
411 memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData);
412 pCrash->nData = nNew;
413 pCrash->zData = zNew;
414 }
415 memcpy(&pCrash->zData[iOfst], zBuf, iAmt);
danielk197762079062007-08-15 17:08:46 +0000416 return writeListAppend(pFile, iOfst, zBuf, iAmt);
danielk1977b4721172007-03-19 05:54:48 +0000417}
418
419/*
danielk197762079062007-08-15 17:08:46 +0000420** Truncate a crash-file.
drh054889e2005-11-30 03:20:31 +0000421*/
danielk19771e536952007-08-16 10:09:01 +0000422static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){
danielk1977967a4a12007-08-20 14:23:44 +0000423 CrashFile *pCrash = (CrashFile *)pFile;
424 assert(size>=0);
425 if( pCrash->iSize>size ){
426 pCrash->iSize = size;
427 }
danielk197762079062007-08-15 17:08:46 +0000428 return writeListAppend(pFile, size, 0, 0);
429}
430
431/*
432** Sync a crash-file.
433*/
danielk19771e536952007-08-16 10:09:01 +0000434static int cfSync(sqlite3_file *pFile, int flags){
danielk197762079062007-08-15 17:08:46 +0000435 CrashFile *pCrash = (CrashFile *)pFile;
436 int isCrash = 0;
437
danielk1977967a4a12007-08-20 14:23:44 +0000438 const char *zName = pCrash->zName;
439 const char *zCrashFile = g.zCrashFile;
440 int nName = strlen(zName);
441 int nCrashFile = strlen(zCrashFile);
442
443 if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){
444 nCrashFile--;
445 if( nName>nCrashFile ) nName = nCrashFile;
446 }
447
448 if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){
449 if( (--g.iCrash)==0 ) isCrash = 1;
danielk197762079062007-08-15 17:08:46 +0000450 }
451
452 return writeListSync(pCrash, isCrash);
453}
454
455/*
456** Return the current file-size of the crash-file.
457*/
danielk19771e536952007-08-16 10:09:01 +0000458static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
danielk197762079062007-08-15 17:08:46 +0000459 CrashFile *pCrash = (CrashFile *)pFile;
danielk1977967a4a12007-08-20 14:23:44 +0000460 *pSize = (i64)pCrash->iSize;
danielk197762079062007-08-15 17:08:46 +0000461 return SQLITE_OK;
462}
463
464/*
465** Calls related to file-locks are passed on to the real file handle.
466*/
danielk19771e536952007-08-16 10:09:01 +0000467static int cfLock(sqlite3_file *pFile, int eLock){
danielk197762079062007-08-15 17:08:46 +0000468 return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock);
469}
danielk19771e536952007-08-16 10:09:01 +0000470static int cfUnlock(sqlite3_file *pFile, int eLock){
danielk197762079062007-08-15 17:08:46 +0000471 return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock);
472}
danielk19771e536952007-08-16 10:09:01 +0000473static int cfCheckReservedLock(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000474 return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile);
475}
danielk1977967a4a12007-08-20 14:23:44 +0000476static int cfLockState(sqlite3_file *pFile){
477 return sqlite3OsLockState(((CrashFile *)pFile)->pRealFile);
478}
danielk19771e536952007-08-16 10:09:01 +0000479static int cfBreakLock(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000480 return sqlite3OsBreakLock(((CrashFile *)pFile)->pRealFile);
481}
482
483/*
484** The xSectorSize() and xDeviceCharacteristics() functions return
485** the global values configured by the [sqlite_crashparams] tcl
486* interface.
487*/
danielk19771e536952007-08-16 10:09:01 +0000488static int cfSectorSize(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000489 return g.iSectorSize;
490}
danielk19771e536952007-08-16 10:09:01 +0000491static int cfDeviceCharacteristics(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000492 return g.iDeviceCharacteristics;
493}
494
495static const sqlite3_io_methods CrashFileVtab = {
496 1, /* iVersion */
497 cfClose, /* xClose */
498 cfRead, /* xRead */
499 cfWrite, /* xWrite */
500 cfTruncate, /* xTruncate */
501 cfSync, /* xSync */
502 cfFileSize, /* xFileSize */
503 cfLock, /* xLock */
504 cfUnlock, /* xUnlock */
505 cfCheckReservedLock, /* xCheckReservedLock */
506 cfBreakLock, /* xBreakLock */
danielk1977967a4a12007-08-20 14:23:44 +0000507 cfLockState, /* xLockState */
danielk197762079062007-08-15 17:08:46 +0000508 cfSectorSize, /* xSectorSize */
509 cfDeviceCharacteristics /* xDeviceCharacteristics */
drh054889e2005-11-30 03:20:31 +0000510};
511
drh054889e2005-11-30 03:20:31 +0000512/*
drhd677b3d2007-08-20 22:48:41 +0000513** Application data for the crash VFS
514*/
515struct crashAppData {
danielk1977f1da17a2007-08-21 13:07:46 +0000516 sqlite3_vfs *pOrig; /* Wrapped vfs structure */
drhd677b3d2007-08-20 22:48:41 +0000517};
518
519/*
danielk19770e87b702007-08-25 12:29:30 +0000520** Open a crash-file file handle.
danielk1977967a4a12007-08-20 14:23:44 +0000521**
522** The caller will have allocated pVfs->szOsFile bytes of space
523** at pFile. This file uses this space for the CrashFile structure
524** and allocates space for the "real" file structure using
525** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is
526** equal or greater than sizeof(CrashFile).
drh054889e2005-11-30 03:20:31 +0000527*/
danielk1977f1da17a2007-08-21 13:07:46 +0000528static int cfOpen(
danielk1977f55b8992007-08-24 08:15:53 +0000529 sqlite3_vfs *pCfVfs,
danielk197762079062007-08-15 17:08:46 +0000530 const char *zName,
531 sqlite3_file *pFile,
532 int flags,
533 int *pOutFlags
534){
danielk1977f55b8992007-08-24 08:15:53 +0000535 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977967a4a12007-08-20 14:23:44 +0000536 int rc;
danielk1977f1da17a2007-08-21 13:07:46 +0000537 CrashFile *pWrapper = (CrashFile *)pFile;
drh4a50aac2007-08-23 02:47:53 +0000538 sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1];
danielk1977f1da17a2007-08-21 13:07:46 +0000539
540 memset(pWrapper, 0, sizeof(CrashFile));
541 rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags);
542
543 if( rc==SQLITE_OK ){
544 i64 iSize;
545 pWrapper->pMethod = &CrashFileVtab;
546 pWrapper->zName = (char *)zName;
547 pWrapper->pRealFile = pReal;
548 rc = sqlite3OsFileSize(pReal, &iSize);
549 pWrapper->iSize = (int)iSize;
550 }
551 if( rc==SQLITE_OK ){
552 pWrapper->nData = (4096 + pWrapper->iSize);
drh4a50aac2007-08-23 02:47:53 +0000553 pWrapper->zData = sqlite3_malloc(pWrapper->nData);
danielk1977f1da17a2007-08-21 13:07:46 +0000554 if( pWrapper->zData ){
555 memset(pWrapper->zData, 0, pWrapper->nData);
556 rc = sqlite3OsRead(pReal, pWrapper->zData, pWrapper->iSize, 0);
557 }else{
558 rc = SQLITE_NOMEM;
danielk197762079062007-08-15 17:08:46 +0000559 }
danielk1977f1da17a2007-08-21 13:07:46 +0000560 }
561 if( rc!=SQLITE_OK && pWrapper->pMethod ){
562 sqlite3OsClose(pFile);
danielk197762079062007-08-15 17:08:46 +0000563 }
564 return rc;
565}
566
danielk1977f55b8992007-08-24 08:15:53 +0000567static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){
568 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
569 return pVfs->xDelete(pVfs, zPath, dirSync);
danielk1977f1da17a2007-08-21 13:07:46 +0000570}
danielk1977f55b8992007-08-24 08:15:53 +0000571static int cfAccess(sqlite3_vfs *pCfVfs, const char *zPath, int flags){
572 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
573 return pVfs->xAccess(pVfs, zPath, flags);
danielk1977f1da17a2007-08-21 13:07:46 +0000574}
danielk1977f55b8992007-08-24 08:15:53 +0000575static int cfGetTempName(sqlite3_vfs *pCfVfs, char *zBufOut){
576 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
577 return pVfs->xGetTempName(pVfs, zBufOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000578}
danielk1977f55b8992007-08-24 08:15:53 +0000579static int cfFullPathname(sqlite3_vfs *pCfVfs, const char *zPath, char *zPathOut){
580 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
581 return pVfs->xFullPathname(pVfs, zPath, zPathOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000582}
danielk1977f55b8992007-08-24 08:15:53 +0000583static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){
584 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
585 return pVfs->xDlOpen(pVfs, zPath);
danielk1977f1da17a2007-08-21 13:07:46 +0000586}
danielk19770e87b702007-08-25 12:29:30 +0000587static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){
588 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
589 pVfs->xDlError(pVfs, nByte, zErrMsg);
590}
591static void *cfDlSym(sqlite3_vfs *pCfVfs, void *pHandle, const char *zSymbol){
592 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
593 return pVfs->xDlSym(pVfs, pHandle, zSymbol);
594}
595static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){
596 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
597 pVfs->xDlClose(pVfs, pHandle);
598}
danielk1977f55b8992007-08-24 08:15:53 +0000599static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){
600 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
601 return pVfs->xRandomness(pVfs, nByte, zBufOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000602}
danielk1977f55b8992007-08-24 08:15:53 +0000603static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){
604 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
605 return pVfs->xSleep(pVfs, nMicro);
danielk1977f1da17a2007-08-21 13:07:46 +0000606}
danielk1977f55b8992007-08-24 08:15:53 +0000607static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){
608 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
609 return pVfs->xCurrentTime(pVfs, pTimeOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000610}
611
danielk19772ca0f862007-08-23 08:06:44 +0000612static int processDevSymArgs(
613 Tcl_Interp *interp,
614 int objc,
615 Tcl_Obj *CONST objv[],
616 int *piDeviceChar,
617 int *piSectorSize
618){
619 struct DeviceFlag {
620 char *zName;
621 int iValue;
622 } aFlag[] = {
623 { "atomic", SQLITE_IOCAP_ATOMIC },
624 { "atomic512", SQLITE_IOCAP_ATOMIC512 },
625 { "atomic1k", SQLITE_IOCAP_ATOMIC1K },
626 { "atomic2k", SQLITE_IOCAP_ATOMIC2K },
627 { "atomic4k", SQLITE_IOCAP_ATOMIC4K },
628 { "atomic8k", SQLITE_IOCAP_ATOMIC8K },
629 { "atomic16k", SQLITE_IOCAP_ATOMIC16K },
630 { "atomic32k", SQLITE_IOCAP_ATOMIC32K },
631 { "atomic64k", SQLITE_IOCAP_ATOMIC64K },
632 { "sequential", SQLITE_IOCAP_SEQUENTIAL },
633 { "safe_append", SQLITE_IOCAP_SAFE_APPEND },
634 { 0, 0 }
635 };
636
637 int i;
638 int iDc = 0;
639 int iSectorSize = 0;
640 int setSectorsize = 0;
641 int setDeviceChar = 0;
642
643 for(i=0; i<objc; i+=2){
644 int nOpt;
645 char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt);
646
647 if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt))
648 && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt))
649 ){
650 Tcl_AppendResult(interp,
651 "Bad option: \"", zOpt,
652 "\" - must be \"-characteristics\" or \"-sectorsize\"", 0
653 );
654 return TCL_ERROR;
655 }
656 if( i==objc-1 ){
657 Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0);
658 return TCL_ERROR;
659 }
660
661 if( zOpt[1]=='s' ){
662 if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){
663 return TCL_ERROR;
664 }
665 setSectorsize = 1;
666 }else{
667 int j;
668 Tcl_Obj **apObj;
669 int nObj;
670 if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){
671 return TCL_ERROR;
672 }
673 for(j=0; j<nObj; j++){
674 int rc;
675 int iChoice;
676 Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]);
677 Tcl_IncrRefCount(pFlag);
678 Tcl_UtfToLower(Tcl_GetString(pFlag));
679
680 rc = Tcl_GetIndexFromObjStruct(
681 interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice
682 );
683 Tcl_DecrRefCount(pFlag);
684 if( rc ){
685 return TCL_ERROR;
686 }
687
688 iDc |= aFlag[iChoice].iValue;
689 }
690 setDeviceChar = 1;
691 }
692 }
693
694 if( setDeviceChar ){
695 *piDeviceChar = iDc;
696 }
697 if( setSectorsize ){
698 *piSectorSize = iSectorSize;
699 }
700
701 return TCL_OK;
702}
703
drh054889e2005-11-30 03:20:31 +0000704/*
danielk197762079062007-08-15 17:08:46 +0000705** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE
drh9c06c952005-11-26 00:25:00 +0000706**
707** This procedure implements a TCL command that enables crash testing
708** in testfixture. Once enabled, crash testing cannot be disabled.
danielk197762079062007-08-15 17:08:46 +0000709**
710** Available options are "-characteristics" and "-sectorsize". Both require
711** an argument. For -sectorsize, this is the simulated sector size in
712** bytes. For -characteristics, the argument must be a list of io-capability
713** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K",
714** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K",
715** "atomic64K", "sequential" and "safe_append".
716**
717** Example:
718**
719** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1
720**
drh9c06c952005-11-26 00:25:00 +0000721*/
722static int crashParamsObjCmd(
723 void * clientData,
724 Tcl_Interp *interp,
725 int objc,
726 Tcl_Obj *CONST objv[]
727){
danielk197762079062007-08-15 17:08:46 +0000728 int iDelay;
729 const char *zCrashFile;
drh153c62c2007-08-24 03:51:33 +0000730 int nCrashFile, iDc, iSectorSize;
drhd677b3d2007-08-20 22:48:41 +0000731
danielk1977f1da17a2007-08-21 13:07:46 +0000732 static sqlite3_vfs crashVfs = {
733 1, /* iVersion */
734 0, /* szOsFile */
735 0, /* mxPathname */
danielk1977f1da17a2007-08-21 13:07:46 +0000736 0, /* pNext */
737 "crash", /* zName */
738 0, /* pAppData */
739
740 cfOpen, /* xOpen */
741 cfDelete, /* xDelete */
742 cfAccess, /* xAccess */
743 cfGetTempName, /* xGetTempName */
744 cfFullPathname, /* xFullPathname */
745 cfDlOpen, /* xDlOpen */
danielk19770e87b702007-08-25 12:29:30 +0000746 cfDlError, /* xDlError */
747 cfDlSym, /* xDlSym */
748 cfDlClose, /* xDlClose */
danielk1977f1da17a2007-08-21 13:07:46 +0000749 cfRandomness, /* xRandomness */
750 cfSleep, /* xSleep */
751 cfCurrentTime /* xCurrentTime */
752 };
753
drh153c62c2007-08-24 03:51:33 +0000754
danielk1977f1da17a2007-08-21 13:07:46 +0000755 if( crashVfs.pAppData==0 ){
756 sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0);
danielk1977f1da17a2007-08-21 13:07:46 +0000757 crashVfs.mxPathname = pOriginalVfs->mxPathname;
758 crashVfs.pAppData = (void *)pOriginalVfs;
759 crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile;
danielk1977f1da17a2007-08-21 13:07:46 +0000760 /* sqlite3_vfs_unregister(pOriginalVfs); */
drhd677b3d2007-08-20 22:48:41 +0000761 sqlite3_vfs_register(&crashVfs, 1);
762 }
danielk197762079062007-08-15 17:08:46 +0000763
drh153c62c2007-08-24 03:51:33 +0000764 iDc = -1;
765 iSectorSize = -1;
danielk197762079062007-08-15 17:08:46 +0000766
danielk197762079062007-08-15 17:08:46 +0000767 if( objc<3 ){
768 Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE");
danielk1977b4b47412007-08-17 15:53:36 +0000769 goto error;
drh9c06c952005-11-26 00:25:00 +0000770 }
danielk197762079062007-08-15 17:08:46 +0000771
danielk1977967a4a12007-08-20 14:23:44 +0000772 zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile);
danielk197762079062007-08-15 17:08:46 +0000773 if( nCrashFile>=sizeof(g.zCrashFile) ){
774 Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0);
danielk1977b4b47412007-08-17 15:53:36 +0000775 goto error;
drh9c06c952005-11-26 00:25:00 +0000776 }
danielk197762079062007-08-15 17:08:46 +0000777 if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){
danielk1977b4b47412007-08-17 15:53:36 +0000778 goto error;
danielk197762079062007-08-15 17:08:46 +0000779 }
780
danielk19772ca0f862007-08-23 08:06:44 +0000781 if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){
782 return TCL_ERROR;
danielk197759a33f92007-03-17 10:26:59 +0000783 }
danielk197762079062007-08-15 17:08:46 +0000784
danielk19772ca0f862007-08-23 08:06:44 +0000785 if( iDc>=0 ){
danielk197762079062007-08-15 17:08:46 +0000786 g.iDeviceCharacteristics = iDc;
787 }
danielk19772ca0f862007-08-23 08:06:44 +0000788 if( iSectorSize>=0 ){
danielk197762079062007-08-15 17:08:46 +0000789 g.iSectorSize = iSectorSize;
790 }
danielk19772ca0f862007-08-23 08:06:44 +0000791
danielk197762079062007-08-15 17:08:46 +0000792 g.iCrash = iDelay;
793 memcpy(g.zCrashFile, zCrashFile, nCrashFile+1);
drh66560ad2006-01-06 14:32:19 +0000794 sqlite3CrashTestEnable = 1;
drh9c06c952005-11-26 00:25:00 +0000795 return TCL_OK;
danielk1977b4b47412007-08-17 15:53:36 +0000796
797error:
danielk1977b4b47412007-08-17 15:53:36 +0000798 return TCL_ERROR;
drh9c06c952005-11-26 00:25:00 +0000799}
800
danielk19772ca0f862007-08-23 08:06:44 +0000801static int devSymObjCmd(
802 void * clientData,
803 Tcl_Interp *interp,
804 int objc,
805 Tcl_Obj *CONST objv[]
806){
807
808 extern int sqlite3_test_device_characteristics;
809 extern int sqlite3_test_sector_size;
810
811 int iDc = -1;
812 int iSectorSize = -1;
813 if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){
814 return TCL_ERROR;
815 }
816
817 if( iDc>=0 ){
818 sqlite3_test_device_characteristics = iDc;
819 }
820 if( iSectorSize>=0 ){
821 sqlite3_test_sector_size = iSectorSize;
822 }
823
824 return TCL_OK;
825}
826
drh198bf392006-01-06 21:52:49 +0000827#endif /* SQLITE_OMIT_DISKIO */
828
drh9c06c952005-11-26 00:25:00 +0000829/*
830** This procedure registers the TCL procedures defined in this file.
831*/
832int Sqlitetest6_Init(Tcl_Interp *interp){
drh198bf392006-01-06 21:52:49 +0000833#ifndef SQLITE_OMIT_DISKIO
drh9c06c952005-11-26 00:25:00 +0000834 Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0);
danielk19772ca0f862007-08-23 08:06:44 +0000835 Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0);
drh198bf392006-01-06 21:52:49 +0000836#endif
drh9c06c952005-11-26 00:25:00 +0000837 return TCL_OK;
838}
839
840#endif /* SQLITE_TEST */