1 /*
2 ** 2005 December 14
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 ** $Id: sqlite3async.c,v 1.7 2009/07/18 11:52:04 danielk1977 Exp $
14 **
15 ** This file contains the implementation of an asynchronous IO backend
16 ** for SQLite.
17 */
18 
19 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ASYNCIO)
20 
21 #include "sqlite3async.h"
22 #include "sqlite3.h"
23 #include <stdarg.h>
24 #include <string.h>
25 #include <assert.h>
26 
27 /* Useful macros used in several places */
28 #define MIN(x,y) ((x)<(y)?(x):(y))
29 #define MAX(x,y) ((x)>(y)?(x):(y))
30 
31 #ifndef SQLITE_AMALGAMATION
32 /* Macro to mark parameters as unused and silence compiler warnings. */
33 #define UNUSED_PARAMETER(x) (void)(x)
34 #endif
35 
36 /* Forward references */
37 typedef struct AsyncWrite AsyncWrite;
38 typedef struct AsyncFile AsyncFile;
39 typedef struct AsyncFileData AsyncFileData;
40 typedef struct AsyncFileLock AsyncFileLock;
41 typedef struct AsyncLock AsyncLock;
42 
43 /* Enable for debugging */
44 #ifndef NDEBUG
45 #include <stdio.h>
46 static int sqlite3async_trace = 0;
47 # define ASYNC_TRACE(X) if( sqlite3async_trace ) asyncTrace X
asyncTrace(const char * zFormat,...)48 static void asyncTrace(const char *zFormat, ...){
49   char *z;
50   va_list ap;
51   va_start(ap, zFormat);
52   z = sqlite3_vmprintf(zFormat, ap);
53   va_end(ap);
54   fprintf(stderr, "[%d] %s", 0 /* (int)pthread_self() */, z);
55   sqlite3_free(z);
56 }
57 #else
58 # define ASYNC_TRACE(X)
59 #endif
60 
61 /*
62 ** THREAD SAFETY NOTES
63 **
64 ** Basic rules:
65 **
66 **     * Both read and write access to the global write-op queue must be
67 **       protected by the async.queueMutex. As are the async.ioError and
68 **       async.nFile variables.
69 **
70 **     * The async.pLock list and all AsyncLock and AsyncFileLock
71 **       structures must be protected by the async.lockMutex mutex.
72 **
73 **     * The file handles from the underlying system are not assumed to
74 **       be thread safe.
75 **
76 **     * See the last two paragraphs under "The Writer Thread" for
77 **       an assumption to do with file-handle synchronization by the Os.
78 **
79 ** Deadlock prevention:
80 **
81 **     There are three mutex used by the system: the "writer" mutex,
82 **     the "queue" mutex and the "lock" mutex. Rules are:
83 **
84 **     * It is illegal to block on the writer mutex when any other mutex
85 **       are held, and
86 **
87 **     * It is illegal to block on the queue mutex when the lock mutex
88 **       is held.
89 **
90 **     i.e. mutex's must be grabbed in the order "writer", "queue", "lock".
91 **
92 ** File system operations (invoked by SQLite thread):
93 **
94 **     xOpen
95 **     xDelete
96 **     xFileExists
97 **
98 ** File handle operations (invoked by SQLite thread):
99 **
100 **         asyncWrite, asyncClose, asyncTruncate, asyncSync
101 **
102 **     The operations above add an entry to the global write-op list. They
103 **     prepare the entry, acquire the async.queueMutex momentarily while
104 **     list pointers are  manipulated to insert the new entry, then release
105 **     the mutex and signal the writer thread to wake up in case it happens
106 **     to be asleep.
107 **
108 **
109 **         asyncRead, asyncFileSize.
110 **
111 **     Read operations. Both of these read from both the underlying file
112 **     first then adjust their result based on pending writes in the
113 **     write-op queue.   So async.queueMutex is held for the duration
114 **     of these operations to prevent other threads from changing the
115 **     queue in mid operation.
116 **
117 **
118 **         asyncLock, asyncUnlock, asyncCheckReservedLock
119 **
120 **     These primitives implement in-process locking using a hash table
121 **     on the file name.  Files are locked correctly for connections coming
122 **     from the same process.  But other processes cannot see these locks
123 **     and will therefore not honor them.
124 **
125 **
126 ** The writer thread:
127 **
128 **     The async.writerMutex is used to make sure only there is only
129 **     a single writer thread running at a time.
130 **
131 **     Inside the writer thread is a loop that works like this:
132 **
133 **         WHILE (write-op list is not empty)
134 **             Do IO operation at head of write-op list
135 **             Remove entry from head of write-op list
136 **         END WHILE
137 **
138 **     The async.queueMutex is always held during the <write-op list is
139 **     not empty> test, and when the entry is removed from the head
140 **     of the write-op list. Sometimes it is held for the interim
141 **     period (while the IO is performed), and sometimes it is
142 **     relinquished. It is relinquished if (a) the IO op is an
143 **     ASYNC_CLOSE or (b) when the file handle was opened, two of
144 **     the underlying systems handles were opened on the same
145 **     file-system entry.
146 **
147 **     If condition (b) above is true, then one file-handle
148 **     (AsyncFile.pBaseRead) is used exclusively by sqlite threads to read the
149 **     file, the other (AsyncFile.pBaseWrite) by sqlite3_async_flush()
150 **     threads to perform write() operations. This means that read
151 **     operations are not blocked by asynchronous writes (although
152 **     asynchronous writes may still be blocked by reads).
153 **
154 **     This assumes that the OS keeps two handles open on the same file
155 **     properly in sync. That is, any read operation that starts after a
156 **     write operation on the same file system entry has completed returns
157 **     data consistent with the write. We also assume that if one thread
158 **     reads a file while another is writing it all bytes other than the
159 **     ones actually being written contain valid data.
160 **
161 **     If the above assumptions are not true, set the preprocessor symbol
162 **     SQLITE_ASYNC_TWO_FILEHANDLES to 0.
163 */
164 
165 
166 #ifndef NDEBUG
167 # define TESTONLY( X ) X
168 #else
169 # define TESTONLY( X )
170 #endif
171 
172 /*
173 ** PORTING FUNCTIONS
174 **
175 ** There are two definitions of the following functions. One for pthreads
176 ** compatible systems and one for Win32. These functions isolate the OS
177 ** specific code required by each platform.
178 **
179 ** The system uses three mutexes and a single condition variable. To
180 ** block on a mutex, async_mutex_enter() is called. The parameter passed
181 ** to async_mutex_enter(), which must be one of ASYNC_MUTEX_LOCK,
182 ** ASYNC_MUTEX_QUEUE or ASYNC_MUTEX_WRITER, identifies which of the three
183 ** mutexes to lock. Similarly, to unlock a mutex, async_mutex_leave() is
184 ** called with a parameter identifying the mutex being unlocked. Mutexes
185 ** are not recursive - it is an error to call async_mutex_enter() to
186 ** lock a mutex that is already locked, or to call async_mutex_leave()
187 ** to unlock a mutex that is not currently locked.
188 **
189 ** The async_cond_wait() and async_cond_signal() functions are modelled
190 ** on the pthreads functions with similar names. The first parameter to
191 ** both functions is always ASYNC_COND_QUEUE. When async_cond_wait()
192 ** is called the mutex identified by the second parameter must be held.
193 ** The mutex is unlocked, and the calling thread simultaneously begins
194 ** waiting for the condition variable to be signalled by another thread.
195 ** After another thread signals the condition variable, the calling
196 ** thread stops waiting, locks mutex eMutex and returns. The
197 ** async_cond_signal() function is used to signal the condition variable.
198 ** It is assumed that the mutex used by the thread calling async_cond_wait()
199 ** is held by the caller of async_cond_signal() (otherwise there would be
200 ** a race condition).
201 **
202 ** It is guaranteed that no other thread will call async_cond_wait() when
203 ** there is already a thread waiting on the condition variable.
204 **
205 ** The async_sched_yield() function is called to suggest to the operating
206 ** system that it would be a good time to shift the current thread off the
207 ** CPU. The system will still work if this function is not implemented
208 ** (it is not currently implemented for win32), but it might be marginally
209 ** more efficient if it is.
210 */
211 static void async_mutex_enter(int eMutex);
212 static void async_mutex_leave(int eMutex);
213 static void async_cond_wait(int eCond, int eMutex);
214 static void async_cond_signal(int eCond);
215 static void async_sched_yield(void);
216 
217 /*
218 ** There are also two definitions of the following. async_os_initialize()
219 ** is called when the asynchronous VFS is first installed, and os_shutdown()
220 ** is called when it is uninstalled (from within sqlite3async_shutdown()).
221 **
222 ** For pthreads builds, both of these functions are no-ops. For win32,
223 ** they provide an opportunity to initialize and finalize the required
224 ** mutex and condition variables.
225 **
226 ** If async_os_initialize() returns other than zero, then the initialization
227 ** fails and SQLITE_ERROR is returned to the user.
228 */
229 static int async_os_initialize(void);
230 static void async_os_shutdown(void);
231 
232 /* Values for use as the 'eMutex' argument of the above functions. The
233 ** integer values assigned to these constants are important for assert()
234 ** statements that verify that mutexes are locked in the correct order.
235 ** Specifically, it is unsafe to try to lock mutex N while holding a lock
236 ** on mutex M if (M<=N).
237 */
238 #define ASYNC_MUTEX_LOCK    0
239 #define ASYNC_MUTEX_QUEUE   1
240 #define ASYNC_MUTEX_WRITER  2
241 
242 /* Values for use as the 'eCond' argument of the above functions. */
243 #define ASYNC_COND_QUEUE    0
244 
245 /*************************************************************************
246 ** Start of OS specific code.
247 */
248 #if SQLITE_OS_WIN || defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
249 
250 #include <windows.h>
251 
252 /* The following block contains the win32 specific code. */
253 
254 #define mutex_held(X) (GetCurrentThreadId()==primitives.aHolder[X])
255 
256 static struct AsyncPrimitives {
257   int isInit;
258   DWORD aHolder[3];
259   CRITICAL_SECTION aMutex[3];
260   HANDLE aCond[1];
261 } primitives = { 0 };
262 
async_os_initialize(void)263 static int async_os_initialize(void){
264   if( !primitives.isInit ){
265     primitives.aCond[0] = CreateEvent(NULL, TRUE, FALSE, 0);
266     if( primitives.aCond[0]==NULL ){
267       return 1;
268     }
269     InitializeCriticalSection(&primitives.aMutex[0]);
270     InitializeCriticalSection(&primitives.aMutex[1]);
271     InitializeCriticalSection(&primitives.aMutex[2]);
272     primitives.isInit = 1;
273   }
274   return 0;
275 }
async_os_shutdown(void)276 static void async_os_shutdown(void){
277   if( primitives.isInit ){
278     DeleteCriticalSection(&primitives.aMutex[0]);
279     DeleteCriticalSection(&primitives.aMutex[1]);
280     DeleteCriticalSection(&primitives.aMutex[2]);
281     CloseHandle(primitives.aCond[0]);
282     primitives.isInit = 0;
283   }
284 }
285 
286 /* The following block contains the Win32 specific code. */
async_mutex_enter(int eMutex)287 static void async_mutex_enter(int eMutex){
288   assert( eMutex==0 || eMutex==1 || eMutex==2 );
289   assert( eMutex!=2 || (!mutex_held(0) && !mutex_held(1) && !mutex_held(2)) );
290   assert( eMutex!=1 || (!mutex_held(0) && !mutex_held(1)) );
291   assert( eMutex!=0 || (!mutex_held(0)) );
292   EnterCriticalSection(&primitives.aMutex[eMutex]);
293   TESTONLY( primitives.aHolder[eMutex] = GetCurrentThreadId(); )
294 }
async_mutex_leave(int eMutex)295 static void async_mutex_leave(int eMutex){
296   assert( eMutex==0 || eMutex==1 || eMutex==2 );
297   assert( mutex_held(eMutex) );
298   TESTONLY( primitives.aHolder[eMutex] = 0; )
299   LeaveCriticalSection(&primitives.aMutex[eMutex]);
300 }
async_cond_wait(int eCond,int eMutex)301 static void async_cond_wait(int eCond, int eMutex){
302   ResetEvent(primitives.aCond[eCond]);
303   async_mutex_leave(eMutex);
304   WaitForSingleObject(primitives.aCond[eCond], INFINITE);
305   async_mutex_enter(eMutex);
306 }
async_cond_signal(int eCond)307 static void async_cond_signal(int eCond){
308   assert( mutex_held(ASYNC_MUTEX_QUEUE) );
309   SetEvent(primitives.aCond[eCond]);
310 }
async_sched_yield(void)311 static void async_sched_yield(void){
312   Sleep(0);
313 }
314 #else
315 
316 /* The following block contains the pthreads specific code. */
317 #include <pthread.h>
318 #include <sched.h>
319 
320 #define mutex_held(X) pthread_equal(primitives.aHolder[X], pthread_self())
321 
async_os_initialize(void)322 static int  async_os_initialize(void) {return 0;}
async_os_shutdown(void)323 static void async_os_shutdown(void) {}
324 
325 static struct AsyncPrimitives {
326   pthread_mutex_t aMutex[3];
327   pthread_cond_t aCond[1];
328   pthread_t aHolder[3];
329 } primitives = {
330   { PTHREAD_MUTEX_INITIALIZER,
331     PTHREAD_MUTEX_INITIALIZER,
332     PTHREAD_MUTEX_INITIALIZER
333   } , {
334     PTHREAD_COND_INITIALIZER
335   } , { 0, 0, 0 }
336 };
337 
async_mutex_enter(int eMutex)338 static void async_mutex_enter(int eMutex){
339   assert( eMutex==0 || eMutex==1 || eMutex==2 );
340   assert( eMutex!=2 || (!mutex_held(0) && !mutex_held(1) && !mutex_held(2)) );
341   assert( eMutex!=1 || (!mutex_held(0) && !mutex_held(1)) );
342   assert( eMutex!=0 || (!mutex_held(0)) );
343   pthread_mutex_lock(&primitives.aMutex[eMutex]);
344   TESTONLY( primitives.aHolder[eMutex] = pthread_self(); )
345 }
async_mutex_leave(int eMutex)346 static void async_mutex_leave(int eMutex){
347   assert( eMutex==0 || eMutex==1 || eMutex==2 );
348   assert( mutex_held(eMutex) );
349   TESTONLY( primitives.aHolder[eMutex] = 0; )
350   pthread_mutex_unlock(&primitives.aMutex[eMutex]);
351 }
async_cond_wait(int eCond,int eMutex)352 static void async_cond_wait(int eCond, int eMutex){
353   assert( eMutex==0 || eMutex==1 || eMutex==2 );
354   assert( mutex_held(eMutex) );
355   TESTONLY( primitives.aHolder[eMutex] = 0; )
356   pthread_cond_wait(&primitives.aCond[eCond], &primitives.aMutex[eMutex]);
357   TESTONLY( primitives.aHolder[eMutex] = pthread_self(); )
358 }
async_cond_signal(int eCond)359 static void async_cond_signal(int eCond){
360   assert( mutex_held(ASYNC_MUTEX_QUEUE) );
361   pthread_cond_signal(&primitives.aCond[eCond]);
362 }
async_sched_yield(void)363 static void async_sched_yield(void){
364   sched_yield();
365 }
366 #endif
367 /*
368 ** End of OS specific code.
369 *************************************************************************/
370 
371 #define assert_mutex_is_held(X) assert( mutex_held(X) )
372 
373 
374 #ifndef SQLITE_ASYNC_TWO_FILEHANDLES
375 /* #define SQLITE_ASYNC_TWO_FILEHANDLES 0 */
376 #define SQLITE_ASYNC_TWO_FILEHANDLES 1
377 #endif
378 
379 /*
380 ** State information is held in the static variable "async" defined
381 ** as the following structure.
382 **
383 ** Both async.ioError and async.nFile are protected by async.queueMutex.
384 */
385 static struct TestAsyncStaticData {
386   AsyncWrite *pQueueFirst;     /* Next write operation to be processed */
387   AsyncWrite *pQueueLast;      /* Last write operation on the list */
388   AsyncLock *pLock;            /* Linked list of all AsyncLock structures */
389   volatile int ioDelay;        /* Extra delay between write operations */
390   volatile int eHalt;          /* One of the SQLITEASYNC_HALT_XXX values */
391   volatile int bLockFiles;     /* Current value of "lockfiles" parameter */
392   int ioError;                 /* True if an IO error has occurred */
393   int nFile;                   /* Number of open files (from sqlite pov) */
394 } async = { 0,0,0,0,0,1,0,0 };
395 
396 /* Possible values of AsyncWrite.op */
397 #define ASYNC_NOOP          0
398 #define ASYNC_WRITE         1
399 #define ASYNC_SYNC          2
400 #define ASYNC_TRUNCATE      3
401 #define ASYNC_CLOSE         4
402 #define ASYNC_DELETE        5
403 #define ASYNC_OPENEXCLUSIVE 6
404 #define ASYNC_UNLOCK        7
405 
406 /* Names of opcodes.  Used for debugging only.
407 ** Make sure these stay in sync with the macros above!
408 */
409 static const char *azOpcodeName[] = {
410   "NOOP", "WRITE", "SYNC", "TRUNCATE", "CLOSE", "DELETE", "OPENEX", "UNLOCK"
411 };
412 
413 /*
414 ** Entries on the write-op queue are instances of the AsyncWrite
415 ** structure, defined here.
416 **
417 ** The interpretation of the iOffset and nByte variables varies depending
418 ** on the value of AsyncWrite.op:
419 **
420 ** ASYNC_NOOP:
421 **     No values used.
422 **
423 ** ASYNC_WRITE:
424 **     iOffset -> Offset in file to write to.
425 **     nByte   -> Number of bytes of data to write (pointed to by zBuf).
426 **
427 ** ASYNC_SYNC:
428 **     nByte   -> flags to pass to sqlite3OsSync().
429 **
430 ** ASYNC_TRUNCATE:
431 **     iOffset -> Size to truncate file to.
432 **     nByte   -> Unused.
433 **
434 ** ASYNC_CLOSE:
435 **     iOffset -> Unused.
436 **     nByte   -> Unused.
437 **
438 ** ASYNC_DELETE:
439 **     iOffset -> Contains the "syncDir" flag.
440 **     nByte   -> Number of bytes of zBuf points to (file name).
441 **
442 ** ASYNC_OPENEXCLUSIVE:
443 **     iOffset -> Value of "delflag".
444 **     nByte   -> Number of bytes of zBuf points to (file name).
445 **
446 ** ASYNC_UNLOCK:
447 **     nByte   -> Argument to sqlite3OsUnlock().
448 **
449 **
450 ** For an ASYNC_WRITE operation, zBuf points to the data to write to the file.
451 ** This space is sqlite3_malloc()d along with the AsyncWrite structure in a
452 ** single blob, so is deleted when sqlite3_free() is called on the parent
453 ** structure.
454 */
455 struct AsyncWrite {
456   AsyncFileData *pFileData;    /* File to write data to or sync */
457   int op;                      /* One of ASYNC_xxx etc. */
458   sqlite_int64 iOffset;        /* See above */
459   int nByte;          /* See above */
460   char *zBuf;         /* Data to write to file (or NULL if op!=ASYNC_WRITE) */
461   AsyncWrite *pNext;  /* Next write operation (to any file) */
462 };
463 
464 /*
465 ** An instance of this structure is created for each distinct open file
466 ** (i.e. if two handles are opened on the one file, only one of these
467 ** structures is allocated) and stored in the async.aLock hash table. The
468 ** keys for async.aLock are the full pathnames of the opened files.
469 **
470 ** AsyncLock.pList points to the head of a linked list of AsyncFileLock
471 ** structures, one for each handle currently open on the file.
472 **
473 ** If the opened file is not a main-database (the SQLITE_OPEN_MAIN_DB is
474 ** not passed to the sqlite3OsOpen() call), or if async.bLockFiles is
475 ** false, variables AsyncLock.pFile and AsyncLock.eLock are never used.
476 ** Otherwise, pFile is a file handle opened on the file in question and
477 ** used to obtain the file-system locks required by database connections
478 ** within this process.
479 **
480 ** See comments above the asyncLock() function for more details on
481 ** the implementation of database locking used by this backend.
482 */
483 struct AsyncLock {
484   char *zFile;
485   int nFile;
486   sqlite3_file *pFile;
487   int eLock;
488   AsyncFileLock *pList;
489   AsyncLock *pNext;           /* Next in linked list headed by async.pLock */
490 };
491 
492 /*
493 ** An instance of the following structure is allocated along with each
494 ** AsyncFileData structure (see AsyncFileData.lock), but is only used if the
495 ** file was opened with the SQLITE_OPEN_MAIN_DB.
496 */
497 struct AsyncFileLock {
498   int eLock;                /* Internally visible lock state (sqlite pov) */
499   int eAsyncLock;           /* Lock-state with write-queue unlock */
500   AsyncFileLock *pNext;
501 };
502 
503 /*
504 ** The AsyncFile structure is a subclass of sqlite3_file used for
505 ** asynchronous IO.
506 **
507 ** All of the actual data for the structure is stored in the structure
508 ** pointed to by AsyncFile.pData, which is allocated as part of the
509 ** sqlite3OsOpen() using sqlite3_malloc(). The reason for this is that the
510 ** lifetime of the AsyncFile structure is ended by the caller after OsClose()
511 ** is called, but the data in AsyncFileData may be required by the
512 ** writer thread after that point.
513 */
514 struct AsyncFile {
515   sqlite3_io_methods *pMethod;
516   AsyncFileData *pData;
517 };
518 struct AsyncFileData {
519   char *zName;               /* Underlying OS filename - used for debugging */
520   int nName;                 /* Number of characters in zName */
521   sqlite3_file *pBaseRead;   /* Read handle to the underlying Os file */
522   sqlite3_file *pBaseWrite;  /* Write handle to the underlying Os file */
523   AsyncFileLock lock;        /* Lock state for this handle */
524   AsyncLock *pLock;          /* AsyncLock object for this file system entry */
525   AsyncWrite closeOp;        /* Preallocated close operation */
526 };
527 
528 /*
529 ** Add an entry to the end of the global write-op list. pWrite should point
530 ** to an AsyncWrite structure allocated using sqlite3_malloc().  The writer
531 ** thread will call sqlite3_free() to free the structure after the specified
532 ** operation has been completed.
533 **
534 ** Once an AsyncWrite structure has been added to the list, it becomes the
535 ** property of the writer thread and must not be read or modified by the
536 ** caller.
537 */
addAsyncWrite(AsyncWrite * pWrite)538 static void addAsyncWrite(AsyncWrite *pWrite){
539   /* We must hold the queue mutex in order to modify the queue pointers */
540   if( pWrite->op!=ASYNC_UNLOCK ){
541     async_mutex_enter(ASYNC_MUTEX_QUEUE);
542   }
543 
544   /* Add the record to the end of the write-op queue */
545   assert( !pWrite->pNext );
546   if( async.pQueueLast ){
547     assert( async.pQueueFirst );
548     async.pQueueLast->pNext = pWrite;
549   }else{
550     async.pQueueFirst = pWrite;
551   }
552   async.pQueueLast = pWrite;
553   ASYNC_TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op],
554          pWrite->pFileData ? pWrite->pFileData->zName : "-", pWrite->iOffset));
555 
556   if( pWrite->op==ASYNC_CLOSE ){
557     async.nFile--;
558   }
559 
560   /* The writer thread might have been idle because there was nothing
561   ** on the write-op queue for it to do.  So wake it up. */
562   async_cond_signal(ASYNC_COND_QUEUE);
563 
564   /* Drop the queue mutex */
565   if( pWrite->op!=ASYNC_UNLOCK ){
566     async_mutex_leave(ASYNC_MUTEX_QUEUE);
567   }
568 }
569 
570 /*
571 ** Increment async.nFile in a thread-safe manner.
572 */
incrOpenFileCount(void)573 static void incrOpenFileCount(void){
574   /* We must hold the queue mutex in order to modify async.nFile */
575   async_mutex_enter(ASYNC_MUTEX_QUEUE);
576   if( async.nFile==0 ){
577     async.ioError = SQLITE_OK;
578   }
579   async.nFile++;
580   async_mutex_leave(ASYNC_MUTEX_QUEUE);
581 }
582 
583 /*
584 ** This is a utility function to allocate and populate a new AsyncWrite
585 ** structure and insert it (via addAsyncWrite() ) into the global list.
586 */
addNewAsyncWrite(AsyncFileData * pFileData,int op,sqlite3_int64 iOffset,int nByte,const char * zByte)587 static int addNewAsyncWrite(
588   AsyncFileData *pFileData,
589   int op,
590   sqlite3_int64 iOffset,
591   int nByte,
592   const char *zByte
593 ){
594   AsyncWrite *p;
595   if( op!=ASYNC_CLOSE && async.ioError ){
596     return async.ioError;
597   }
598   p = sqlite3_malloc(sizeof(AsyncWrite) + (zByte?nByte:0));
599   if( !p ){
600     /* The upper layer does not expect operations like OsWrite() to
601     ** return SQLITE_NOMEM. This is partly because under normal conditions
602     ** SQLite is required to do rollback without calling malloc(). So
603     ** if malloc() fails here, treat it as an I/O error. The above
604     ** layer knows how to handle that.
605     */
606     return SQLITE_IOERR;
607   }
608   p->op = op;
609   p->iOffset = iOffset;
610   p->nByte = nByte;
611   p->pFileData = pFileData;
612   p->pNext = 0;
613   if( zByte ){
614     p->zBuf = (char *)&p[1];
615     memcpy(p->zBuf, zByte, nByte);
616   }else{
617     p->zBuf = 0;
618   }
619   addAsyncWrite(p);
620   return SQLITE_OK;
621 }
622 
623 /*
624 ** Close the file. This just adds an entry to the write-op list, the file is
625 ** not actually closed.
626 */
asyncClose(sqlite3_file * pFile)627 static int asyncClose(sqlite3_file *pFile){
628   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
629 
630   /* Unlock the file, if it is locked */
631   async_mutex_enter(ASYNC_MUTEX_LOCK);
632   p->lock.eLock = 0;
633   async_mutex_leave(ASYNC_MUTEX_LOCK);
634 
635   addAsyncWrite(&p->closeOp);
636   return SQLITE_OK;
637 }
638 
639 /*
640 ** Implementation of sqlite3OsWrite() for asynchronous files. Instead of
641 ** writing to the underlying file, this function adds an entry to the end of
642 ** the global AsyncWrite list. Either SQLITE_OK or SQLITE_NOMEM may be
643 ** returned.
644 */
asyncWrite(sqlite3_file * pFile,const void * pBuf,int amt,sqlite3_int64 iOff)645 static int asyncWrite(
646   sqlite3_file *pFile,
647   const void *pBuf,
648   int amt,
649   sqlite3_int64 iOff
650 ){
651   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
652   return addNewAsyncWrite(p, ASYNC_WRITE, iOff, amt, pBuf);
653 }
654 
655 /*
656 ** Read data from the file. First we read from the filesystem, then adjust
657 ** the contents of the buffer based on ASYNC_WRITE operations in the
658 ** write-op queue.
659 **
660 ** This method holds the mutex from start to finish.
661 */
asyncRead(sqlite3_file * pFile,void * zOut,int iAmt,sqlite3_int64 iOffset)662 static int asyncRead(
663   sqlite3_file *pFile,
664   void *zOut,
665   int iAmt,
666   sqlite3_int64 iOffset
667 ){
668   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
669   int rc = SQLITE_OK;
670   sqlite3_int64 filesize = 0;
671   sqlite3_file *pBase = p->pBaseRead;
672   sqlite3_int64 iAmt64 = (sqlite3_int64)iAmt;
673 
674   /* Grab the write queue mutex for the duration of the call */
675   async_mutex_enter(ASYNC_MUTEX_QUEUE);
676 
677   /* If an I/O error has previously occurred in this virtual file
678   ** system, then all subsequent operations fail.
679   */
680   if( async.ioError!=SQLITE_OK ){
681     rc = async.ioError;
682     goto asyncread_out;
683   }
684 
685   if( pBase->pMethods ){
686     sqlite3_int64 nRead;
687     rc = pBase->pMethods->xFileSize(pBase, &filesize);
688     if( rc!=SQLITE_OK ){
689       goto asyncread_out;
690     }
691     nRead = MIN(filesize - iOffset, iAmt64);
692     if( nRead>0 ){
693       rc = pBase->pMethods->xRead(pBase, zOut, (int)nRead, iOffset);
694       ASYNC_TRACE(("READ %s %d bytes at %d\n", p->zName, nRead, iOffset));
695     }
696   }
697 
698   if( rc==SQLITE_OK ){
699     AsyncWrite *pWrite;
700     char *zName = p->zName;
701 
702     for(pWrite=async.pQueueFirst; pWrite; pWrite = pWrite->pNext){
703       if( pWrite->op==ASYNC_WRITE && (
704         (pWrite->pFileData==p) ||
705         (zName && pWrite->pFileData->zName==zName)
706       )){
707         sqlite3_int64 nCopy;
708         sqlite3_int64 nByte64 = (sqlite3_int64)pWrite->nByte;
709 
710         /* Set variable iBeginIn to the offset in buffer pWrite->zBuf[] from
711         ** which data should be copied. Set iBeginOut to the offset within
712         ** the output buffer to which data should be copied. If either of
713         ** these offsets is a negative number, set them to 0.
714         */
715         sqlite3_int64 iBeginOut = (pWrite->iOffset-iOffset);
716         sqlite3_int64 iBeginIn = -iBeginOut;
717         if( iBeginIn<0 ) iBeginIn = 0;
718         if( iBeginOut<0 ) iBeginOut = 0;
719 
720         filesize = MAX(filesize, pWrite->iOffset+nByte64);
721 
722         nCopy = MIN(nByte64-iBeginIn, iAmt64-iBeginOut);
723         if( nCopy>0 ){
724           memcpy(&((char *)zOut)[iBeginOut], &pWrite->zBuf[iBeginIn], (size_t)nCopy);
725           ASYNC_TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset));
726         }
727       }
728     }
729   }
730 
731 asyncread_out:
732   async_mutex_leave(ASYNC_MUTEX_QUEUE);
733   if( rc==SQLITE_OK && filesize<(iOffset+iAmt) ){
734     rc = SQLITE_IOERR_SHORT_READ;
735   }
736   return rc;
737 }
738 
739 /*
740 ** Truncate the file to nByte bytes in length. This just adds an entry to
741 ** the write-op list, no IO actually takes place.
742 */
asyncTruncate(sqlite3_file * pFile,sqlite3_int64 nByte)743 static int asyncTruncate(sqlite3_file *pFile, sqlite3_int64 nByte){
744   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
745   return addNewAsyncWrite(p, ASYNC_TRUNCATE, nByte, 0, 0);
746 }
747 
748 /*
749 ** Sync the file. This just adds an entry to the write-op list, the
750 ** sync() is done later by sqlite3_async_flush().
751 */
asyncSync(sqlite3_file * pFile,int flags)752 static int asyncSync(sqlite3_file *pFile, int flags){
753   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
754   return addNewAsyncWrite(p, ASYNC_SYNC, 0, flags, 0);
755 }
756 
757 /*
758 ** Read the size of the file. First we read the size of the file system
759 ** entry, then adjust for any ASYNC_WRITE or ASYNC_TRUNCATE operations
760 ** currently in the write-op list.
761 **
762 ** This method holds the mutex from start to finish.
763 */
asyncFileSize(sqlite3_file * pFile,sqlite3_int64 * piSize)764 int asyncFileSize(sqlite3_file *pFile, sqlite3_int64 *piSize){
765   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
766   int rc = SQLITE_OK;
767   sqlite3_int64 s = 0;
768   sqlite3_file *pBase;
769 
770   async_mutex_enter(ASYNC_MUTEX_QUEUE);
771 
772   /* Read the filesystem size from the base file. If pMethods is NULL, this
773   ** means the file hasn't been opened yet. In this case all relevant data
774   ** must be in the write-op queue anyway, so we can omit reading from the
775   ** file-system.
776   */
777   pBase = p->pBaseRead;
778   if( pBase->pMethods ){
779     rc = pBase->pMethods->xFileSize(pBase, &s);
780   }
781 
782   if( rc==SQLITE_OK ){
783     AsyncWrite *pWrite;
784     for(pWrite=async.pQueueFirst; pWrite; pWrite = pWrite->pNext){
785       if( pWrite->op==ASYNC_DELETE
786        && p->zName
787        && strcmp(p->zName, pWrite->zBuf)==0
788       ){
789         s = 0;
790       }else if( pWrite->pFileData && (
791           (pWrite->pFileData==p)
792        || (p->zName && pWrite->pFileData->zName==p->zName)
793       )){
794         switch( pWrite->op ){
795           case ASYNC_WRITE:
796             s = MAX(pWrite->iOffset + (sqlite3_int64)(pWrite->nByte), s);
797             break;
798           case ASYNC_TRUNCATE:
799             s = MIN(s, pWrite->iOffset);
800             break;
801         }
802       }
803     }
804     *piSize = s;
805   }
806   async_mutex_leave(ASYNC_MUTEX_QUEUE);
807   return rc;
808 }
809 
810 /*
811 ** Lock or unlock the actual file-system entry.
812 */
getFileLock(AsyncLock * pLock)813 static int getFileLock(AsyncLock *pLock){
814   int rc = SQLITE_OK;
815   AsyncFileLock *pIter;
816   int eRequired = 0;
817 
818   if( pLock->pFile ){
819     for(pIter=pLock->pList; pIter; pIter=pIter->pNext){
820       assert(pIter->eAsyncLock>=pIter->eLock);
821       if( pIter->eAsyncLock>eRequired ){
822         eRequired = pIter->eAsyncLock;
823         assert(eRequired>=0 && eRequired<=SQLITE_LOCK_EXCLUSIVE);
824       }
825     }
826 
827     if( eRequired>pLock->eLock ){
828       rc = pLock->pFile->pMethods->xLock(pLock->pFile, eRequired);
829       if( rc==SQLITE_OK ){
830         pLock->eLock = eRequired;
831       }
832     }
833     else if( eRequired<pLock->eLock && eRequired<=SQLITE_LOCK_SHARED ){
834       rc = pLock->pFile->pMethods->xUnlock(pLock->pFile, eRequired);
835       if( rc==SQLITE_OK ){
836         pLock->eLock = eRequired;
837       }
838     }
839   }
840 
841   return rc;
842 }
843 
844 /*
845 ** Return the AsyncLock structure from the global async.pLock list
846 ** associated with the file-system entry identified by path zName
847 ** (a string of nName bytes). If no such structure exists, return 0.
848 */
findLock(const char * zName,int nName)849 static AsyncLock *findLock(const char *zName, int nName){
850   AsyncLock *p = async.pLock;
851   while( p && (p->nFile!=nName || memcmp(p->zFile, zName, nName)) ){
852     p = p->pNext;
853   }
854   return p;
855 }
856 
857 /*
858 ** The following two methods - asyncLock() and asyncUnlock() - are used
859 ** to obtain and release locks on database files opened with the
860 ** asynchronous backend.
861 */
asyncLock(sqlite3_file * pFile,int eLock)862 static int asyncLock(sqlite3_file *pFile, int eLock){
863   int rc = SQLITE_OK;
864   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
865 
866   if( p->zName ){
867     async_mutex_enter(ASYNC_MUTEX_LOCK);
868     if( p->lock.eLock<eLock ){
869       AsyncLock *pLock = p->pLock;
870       AsyncFileLock *pIter;
871       assert(pLock && pLock->pList);
872       for(pIter=pLock->pList; pIter; pIter=pIter->pNext){
873         if( pIter!=&p->lock && (
874           (eLock==SQLITE_LOCK_EXCLUSIVE && pIter->eLock>=SQLITE_LOCK_SHARED) ||
875           (eLock==SQLITE_LOCK_PENDING && pIter->eLock>=SQLITE_LOCK_RESERVED) ||
876           (eLock==SQLITE_LOCK_RESERVED && pIter->eLock>=SQLITE_LOCK_RESERVED) ||
877           (eLock==SQLITE_LOCK_SHARED && pIter->eLock>=SQLITE_LOCK_PENDING)
878         )){
879           rc = SQLITE_BUSY;
880         }
881       }
882       if( rc==SQLITE_OK ){
883         p->lock.eLock = eLock;
884         p->lock.eAsyncLock = MAX(p->lock.eAsyncLock, eLock);
885       }
886       assert(p->lock.eAsyncLock>=p->lock.eLock);
887       if( rc==SQLITE_OK ){
888         rc = getFileLock(pLock);
889       }
890     }
891     async_mutex_leave(ASYNC_MUTEX_LOCK);
892   }
893 
894   ASYNC_TRACE(("LOCK %d (%s) rc=%d\n", eLock, p->zName, rc));
895   return rc;
896 }
asyncUnlock(sqlite3_file * pFile,int eLock)897 static int asyncUnlock(sqlite3_file *pFile, int eLock){
898   int rc = SQLITE_OK;
899   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
900   if( p->zName ){
901     AsyncFileLock *pLock = &p->lock;
902     async_mutex_enter(ASYNC_MUTEX_QUEUE);
903     async_mutex_enter(ASYNC_MUTEX_LOCK);
904     pLock->eLock = MIN(pLock->eLock, eLock);
905     rc = addNewAsyncWrite(p, ASYNC_UNLOCK, 0, eLock, 0);
906     async_mutex_leave(ASYNC_MUTEX_LOCK);
907     async_mutex_leave(ASYNC_MUTEX_QUEUE);
908   }
909   return rc;
910 }
911 
912 /*
913 ** This function is called when the pager layer first opens a database file
914 ** and is checking for a hot-journal.
915 */
asyncCheckReservedLock(sqlite3_file * pFile,int * pResOut)916 static int asyncCheckReservedLock(sqlite3_file *pFile, int *pResOut){
917   int ret = 0;
918   AsyncFileLock *pIter;
919   AsyncFileData *p = ((AsyncFile *)pFile)->pData;
920 
921   async_mutex_enter(ASYNC_MUTEX_LOCK);
922   for(pIter=p->pLock->pList; pIter; pIter=pIter->pNext){
923     if( pIter->eLock>=SQLITE_LOCK_RESERVED ){
924       ret = 1;
925       break;
926     }
927   }
928   async_mutex_leave(ASYNC_MUTEX_LOCK);
929 
930   ASYNC_TRACE(("CHECK-LOCK %d (%s)\n", ret, p->zName));
931   *pResOut = ret;
932   return SQLITE_OK;
933 }
934 
935 /*
936 ** sqlite3_file_control() implementation.
937 */
asyncFileControl(sqlite3_file * id,int op,void * pArg)938 static int asyncFileControl(sqlite3_file *id, int op, void *pArg){
939   switch( op ){
940     case SQLITE_FCNTL_LOCKSTATE: {
941       async_mutex_enter(ASYNC_MUTEX_LOCK);
942       *(int*)pArg = ((AsyncFile*)id)->pData->lock.eLock;
943       async_mutex_leave(ASYNC_MUTEX_LOCK);
944       return SQLITE_OK;
945     }
946   }
947   return SQLITE_NOTFOUND;
948 }
949 
950 /*
951 ** Return the device characteristics and sector-size of the device. It
952 ** is tricky to implement these correctly, as this backend might
953 ** not have an open file handle at this point.
954 */
asyncSectorSize(sqlite3_file * pFile)955 static int asyncSectorSize(sqlite3_file *pFile){
956   UNUSED_PARAMETER(pFile);
957   return 512;
958 }
asyncDeviceCharacteristics(sqlite3_file * pFile)959 static int asyncDeviceCharacteristics(sqlite3_file *pFile){
960   UNUSED_PARAMETER(pFile);
961   return 0;
962 }
963 
unlinkAsyncFile(AsyncFileData * pData)964 static int unlinkAsyncFile(AsyncFileData *pData){
965   AsyncFileLock **ppIter;
966   int rc = SQLITE_OK;
967 
968   if( pData->zName ){
969     AsyncLock *pLock = pData->pLock;
970     for(ppIter=&pLock->pList; *ppIter; ppIter=&((*ppIter)->pNext)){
971       if( (*ppIter)==&pData->lock ){
972         *ppIter = pData->lock.pNext;
973         break;
974       }
975     }
976     if( !pLock->pList ){
977       AsyncLock **pp;
978       if( pLock->pFile ){
979         pLock->pFile->pMethods->xClose(pLock->pFile);
980       }
981       for(pp=&async.pLock; *pp!=pLock; pp=&((*pp)->pNext));
982       *pp = pLock->pNext;
983       sqlite3_free(pLock);
984     }else{
985       rc = getFileLock(pLock);
986     }
987   }
988 
989   return rc;
990 }
991 
992 /*
993 ** The parameter passed to this function is a copy of a 'flags' parameter
994 ** passed to this modules xOpen() method. This function returns true
995 ** if the file should be opened asynchronously, or false if it should
996 ** be opened immediately.
997 **
998 ** If the file is to be opened asynchronously, then asyncOpen() will add
999 ** an entry to the event queue and the file will not actually be opened
1000 ** until the event is processed. Otherwise, the file is opened directly
1001 ** by the caller.
1002 */
doAsynchronousOpen(int flags)1003 static int doAsynchronousOpen(int flags){
1004   return (flags&SQLITE_OPEN_CREATE) && (
1005       (flags&SQLITE_OPEN_MAIN_JOURNAL) ||
1006       (flags&SQLITE_OPEN_TEMP_JOURNAL) ||
1007       (flags&SQLITE_OPEN_DELETEONCLOSE)
1008   );
1009 }
1010 
1011 /*
1012 ** Open a file.
1013 */
asyncOpen(sqlite3_vfs * pAsyncVfs,const char * zName,sqlite3_file * pFile,int flags,int * pOutFlags)1014 static int asyncOpen(
1015   sqlite3_vfs *pAsyncVfs,
1016   const char *zName,
1017   sqlite3_file *pFile,
1018   int flags,
1019   int *pOutFlags
1020 ){
1021   static sqlite3_io_methods async_methods = {
1022     1,                               /* iVersion */
1023     asyncClose,                      /* xClose */
1024     asyncRead,                       /* xRead */
1025     asyncWrite,                      /* xWrite */
1026     asyncTruncate,                   /* xTruncate */
1027     asyncSync,                       /* xSync */
1028     asyncFileSize,                   /* xFileSize */
1029     asyncLock,                       /* xLock */
1030     asyncUnlock,                     /* xUnlock */
1031     asyncCheckReservedLock,          /* xCheckReservedLock */
1032     asyncFileControl,                /* xFileControl */
1033     asyncSectorSize,                 /* xSectorSize */
1034     asyncDeviceCharacteristics       /* xDeviceCharacteristics */
1035   };
1036 
1037   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1038   AsyncFile *p = (AsyncFile *)pFile;
1039   int nName = 0;
1040   int rc = SQLITE_OK;
1041   int nByte;
1042   AsyncFileData *pData;
1043   AsyncLock *pLock = 0;
1044   char *z;
1045   int isAsyncOpen = doAsynchronousOpen(flags);
1046 
1047   /* If zName is NULL, then the upper layer is requesting an anonymous file.
1048   ** Otherwise, allocate enough space to make a copy of the file name (along
1049   ** with the second nul-terminator byte required by xOpen).
1050   */
1051   if( zName ){
1052     nName = (int)strlen(zName);
1053   }
1054 
1055   nByte = (
1056     sizeof(AsyncFileData) +        /* AsyncFileData structure */
1057     2 * pVfs->szOsFile +           /* AsyncFileData.pBaseRead and pBaseWrite */
1058     nName + 2                      /* AsyncFileData.zName */
1059   );
1060   z = sqlite3_malloc(nByte);
1061   if( !z ){
1062     return SQLITE_NOMEM;
1063   }
1064   memset(z, 0, nByte);
1065   pData = (AsyncFileData*)z;
1066   z += sizeof(pData[0]);
1067   pData->pBaseRead = (sqlite3_file*)z;
1068   z += pVfs->szOsFile;
1069   pData->pBaseWrite = (sqlite3_file*)z;
1070   pData->closeOp.pFileData = pData;
1071   pData->closeOp.op = ASYNC_CLOSE;
1072 
1073   if( zName ){
1074     z += pVfs->szOsFile;
1075     pData->zName = z;
1076     pData->nName = nName;
1077     memcpy(pData->zName, zName, nName);
1078   }
1079 
1080   if( !isAsyncOpen ){
1081     int flagsout;
1082     rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseRead, flags, &flagsout);
1083     if( rc==SQLITE_OK
1084      && (flagsout&SQLITE_OPEN_READWRITE)
1085      && (flags&SQLITE_OPEN_EXCLUSIVE)==0
1086     ){
1087       rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseWrite, flags, 0);
1088     }
1089     if( pOutFlags ){
1090       *pOutFlags = flagsout;
1091     }
1092   }
1093 
1094   async_mutex_enter(ASYNC_MUTEX_LOCK);
1095 
1096   if( zName && rc==SQLITE_OK ){
1097     pLock = findLock(pData->zName, pData->nName);
1098     if( !pLock ){
1099       int nByte = pVfs->szOsFile + sizeof(AsyncLock) + pData->nName + 1;
1100       pLock = (AsyncLock *)sqlite3_malloc(nByte);
1101       if( pLock ){
1102         memset(pLock, 0, nByte);
1103         if( async.bLockFiles && (flags&SQLITE_OPEN_MAIN_DB) ){
1104           pLock->pFile = (sqlite3_file *)&pLock[1];
1105           rc = pVfs->xOpen(pVfs, pData->zName, pLock->pFile, flags, 0);
1106           if( rc!=SQLITE_OK ){
1107             sqlite3_free(pLock);
1108             pLock = 0;
1109           }
1110         }
1111         if( pLock ){
1112           pLock->nFile = pData->nName;
1113           pLock->zFile = &((char *)(&pLock[1]))[pVfs->szOsFile];
1114           memcpy(pLock->zFile, pData->zName, pLock->nFile);
1115           pLock->pNext = async.pLock;
1116           async.pLock = pLock;
1117         }
1118       }else{
1119         rc = SQLITE_NOMEM;
1120       }
1121     }
1122   }
1123 
1124   if( rc==SQLITE_OK ){
1125     p->pMethod = &async_methods;
1126     p->pData = pData;
1127 
1128     /* Link AsyncFileData.lock into the linked list of
1129     ** AsyncFileLock structures for this file.
1130     */
1131     if( zName ){
1132       pData->lock.pNext = pLock->pList;
1133       pLock->pList = &pData->lock;
1134       pData->zName = pLock->zFile;
1135     }
1136   }else{
1137     if( pData->pBaseRead->pMethods ){
1138       pData->pBaseRead->pMethods->xClose(pData->pBaseRead);
1139     }
1140     if( pData->pBaseWrite->pMethods ){
1141       pData->pBaseWrite->pMethods->xClose(pData->pBaseWrite);
1142     }
1143     sqlite3_free(pData);
1144   }
1145 
1146   async_mutex_leave(ASYNC_MUTEX_LOCK);
1147 
1148   if( rc==SQLITE_OK ){
1149     pData->pLock = pLock;
1150   }
1151 
1152   if( rc==SQLITE_OK && isAsyncOpen ){
1153     rc = addNewAsyncWrite(pData, ASYNC_OPENEXCLUSIVE, (sqlite3_int64)flags,0,0);
1154     if( rc==SQLITE_OK ){
1155       if( pOutFlags ) *pOutFlags = flags;
1156     }else{
1157       async_mutex_enter(ASYNC_MUTEX_LOCK);
1158       unlinkAsyncFile(pData);
1159       async_mutex_leave(ASYNC_MUTEX_LOCK);
1160       sqlite3_free(pData);
1161     }
1162   }
1163   if( rc!=SQLITE_OK ){
1164     p->pMethod = 0;
1165   }else{
1166     incrOpenFileCount();
1167   }
1168 
1169   return rc;
1170 }
1171 
1172 /*
1173 ** Implementation of sqlite3OsDelete. Add an entry to the end of the
1174 ** write-op queue to perform the delete.
1175 */
asyncDelete(sqlite3_vfs * pAsyncVfs,const char * z,int syncDir)1176 static int asyncDelete(sqlite3_vfs *pAsyncVfs, const char *z, int syncDir){
1177   UNUSED_PARAMETER(pAsyncVfs);
1178   return addNewAsyncWrite(0, ASYNC_DELETE, syncDir, (int)strlen(z)+1, z);
1179 }
1180 
1181 /*
1182 ** Implementation of sqlite3OsAccess. This method holds the mutex from
1183 ** start to finish.
1184 */
asyncAccess(sqlite3_vfs * pAsyncVfs,const char * zName,int flags,int * pResOut)1185 static int asyncAccess(
1186   sqlite3_vfs *pAsyncVfs,
1187   const char *zName,
1188   int flags,
1189   int *pResOut
1190 ){
1191   int rc;
1192   int ret;
1193   AsyncWrite *p;
1194   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1195 
1196   assert(flags==SQLITE_ACCESS_READWRITE
1197       || flags==SQLITE_ACCESS_READ
1198       || flags==SQLITE_ACCESS_EXISTS
1199   );
1200 
1201   async_mutex_enter(ASYNC_MUTEX_QUEUE);
1202   rc = pVfs->xAccess(pVfs, zName, flags, &ret);
1203   if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
1204     for(p=async.pQueueFirst; p; p = p->pNext){
1205       if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, zName) ){
1206         ret = 0;
1207       }else if( p->op==ASYNC_OPENEXCLUSIVE
1208              && p->pFileData->zName
1209              && 0==strcmp(p->pFileData->zName, zName)
1210       ){
1211         ret = 1;
1212       }
1213     }
1214   }
1215   ASYNC_TRACE(("ACCESS(%s): %s = %d\n",
1216     flags==SQLITE_ACCESS_READWRITE?"read-write":
1217     flags==SQLITE_ACCESS_READ?"read":"exists"
1218     , zName, ret)
1219   );
1220   async_mutex_leave(ASYNC_MUTEX_QUEUE);
1221   *pResOut = ret;
1222   return rc;
1223 }
1224 
1225 /*
1226 ** Fill in zPathOut with the full path to the file identified by zPath.
1227 */
asyncFullPathname(sqlite3_vfs * pAsyncVfs,const char * zPath,int nPathOut,char * zPathOut)1228 static int asyncFullPathname(
1229   sqlite3_vfs *pAsyncVfs,
1230   const char *zPath,
1231   int nPathOut,
1232   char *zPathOut
1233 ){
1234   int rc;
1235   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1236   rc = pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
1237 
1238   /* Because of the way intra-process file locking works, this backend
1239   ** needs to return a canonical path. The following block assumes the
1240   ** file-system uses unix style paths.
1241   */
1242   if( rc==SQLITE_OK ){
1243     int i, j;
1244     char *z = zPathOut;
1245     int n = (int)strlen(z);
1246     while( n>1 && z[n-1]=='/' ){ n--; }
1247     for(i=j=0; i<n; i++){
1248       if( z[i]=='/' ){
1249         if( z[i+1]=='/' ) continue;
1250         if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
1251           i += 1;
1252           continue;
1253         }
1254         if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
1255           while( j>0 && z[j-1]!='/' ){ j--; }
1256           if( j>0 ){ j--; }
1257           i += 2;
1258           continue;
1259         }
1260       }
1261       z[j++] = z[i];
1262     }
1263     z[j] = 0;
1264   }
1265 
1266   return rc;
1267 }
asyncDlOpen(sqlite3_vfs * pAsyncVfs,const char * zPath)1268 static void *asyncDlOpen(sqlite3_vfs *pAsyncVfs, const char *zPath){
1269   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1270   return pVfs->xDlOpen(pVfs, zPath);
1271 }
asyncDlError(sqlite3_vfs * pAsyncVfs,int nByte,char * zErrMsg)1272 static void asyncDlError(sqlite3_vfs *pAsyncVfs, int nByte, char *zErrMsg){
1273   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1274   pVfs->xDlError(pVfs, nByte, zErrMsg);
1275 }
asyncDlSym(sqlite3_vfs * pAsyncVfs,void * pHandle,const char * zSymbol)1276 static void (*asyncDlSym(
1277   sqlite3_vfs *pAsyncVfs,
1278   void *pHandle,
1279   const char *zSymbol
1280 ))(void){
1281   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1282   return pVfs->xDlSym(pVfs, pHandle, zSymbol);
1283 }
asyncDlClose(sqlite3_vfs * pAsyncVfs,void * pHandle)1284 static void asyncDlClose(sqlite3_vfs *pAsyncVfs, void *pHandle){
1285   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1286   pVfs->xDlClose(pVfs, pHandle);
1287 }
asyncRandomness(sqlite3_vfs * pAsyncVfs,int nByte,char * zBufOut)1288 static int asyncRandomness(sqlite3_vfs *pAsyncVfs, int nByte, char *zBufOut){
1289   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1290   return pVfs->xRandomness(pVfs, nByte, zBufOut);
1291 }
asyncSleep(sqlite3_vfs * pAsyncVfs,int nMicro)1292 static int asyncSleep(sqlite3_vfs *pAsyncVfs, int nMicro){
1293   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1294   return pVfs->xSleep(pVfs, nMicro);
1295 }
asyncCurrentTime(sqlite3_vfs * pAsyncVfs,double * pTimeOut)1296 static int asyncCurrentTime(sqlite3_vfs *pAsyncVfs, double *pTimeOut){
1297   sqlite3_vfs *pVfs = (sqlite3_vfs *)pAsyncVfs->pAppData;
1298   return pVfs->xCurrentTime(pVfs, pTimeOut);
1299 }
1300 
1301 static sqlite3_vfs async_vfs = {
1302   1,                    /* iVersion */
1303   sizeof(AsyncFile),    /* szOsFile */
1304   0,                    /* mxPathname */
1305   0,                    /* pNext */
1306   SQLITEASYNC_VFSNAME,  /* zName */
1307   0,                    /* pAppData */
1308   asyncOpen,            /* xOpen */
1309   asyncDelete,          /* xDelete */
1310   asyncAccess,          /* xAccess */
1311   asyncFullPathname,    /* xFullPathname */
1312   asyncDlOpen,          /* xDlOpen */
1313   asyncDlError,         /* xDlError */
1314   asyncDlSym,           /* xDlSym */
1315   asyncDlClose,         /* xDlClose */
1316   asyncRandomness,      /* xDlError */
1317   asyncSleep,           /* xDlSym */
1318   asyncCurrentTime      /* xDlClose */
1319 };
1320 
1321 /*
1322 ** This procedure runs in a separate thread, reading messages off of the
1323 ** write queue and processing them one by one.
1324 **
1325 ** If async.writerHaltNow is true, then this procedure exits
1326 ** after processing a single message.
1327 **
1328 ** If async.writerHaltWhenIdle is true, then this procedure exits when
1329 ** the write queue is empty.
1330 **
1331 ** If both of the above variables are false, this procedure runs
1332 ** indefinately, waiting for operations to be added to the write queue
1333 ** and processing them in the order in which they arrive.
1334 **
1335 ** An artifical delay of async.ioDelay milliseconds is inserted before
1336 ** each write operation in order to simulate the effect of a slow disk.
1337 **
1338 ** Only one instance of this procedure may be running at a time.
1339 */
asyncWriterThread(void)1340 static void asyncWriterThread(void){
1341   sqlite3_vfs *pVfs = (sqlite3_vfs *)(async_vfs.pAppData);
1342   AsyncWrite *p = 0;
1343   int rc = SQLITE_OK;
1344   int holdingMutex = 0;
1345 
1346   async_mutex_enter(ASYNC_MUTEX_WRITER);
1347 
1348   while( async.eHalt!=SQLITEASYNC_HALT_NOW ){
1349     int doNotFree = 0;
1350     sqlite3_file *pBase = 0;
1351 
1352     if( !holdingMutex ){
1353       async_mutex_enter(ASYNC_MUTEX_QUEUE);
1354     }
1355     while( (p = async.pQueueFirst)==0 ){
1356       if( async.eHalt!=SQLITEASYNC_HALT_NEVER ){
1357         async_mutex_leave(ASYNC_MUTEX_QUEUE);
1358         break;
1359       }else{
1360         ASYNC_TRACE(("IDLE\n"));
1361         async_cond_wait(ASYNC_COND_QUEUE, ASYNC_MUTEX_QUEUE);
1362         ASYNC_TRACE(("WAKEUP\n"));
1363       }
1364     }
1365     if( p==0 ) break;
1366     holdingMutex = 1;
1367 
1368     /* Right now this thread is holding the mutex on the write-op queue.
1369     ** Variable 'p' points to the first entry in the write-op queue. In
1370     ** the general case, we hold on to the mutex for the entire body of
1371     ** the loop.
1372     **
1373     ** However in the cases enumerated below, we relinquish the mutex,
1374     ** perform the IO, and then re-request the mutex before removing 'p' from
1375     ** the head of the write-op queue. The idea is to increase concurrency with
1376     ** sqlite threads.
1377     **
1378     **     * An ASYNC_CLOSE operation.
1379     **     * An ASYNC_OPENEXCLUSIVE operation. For this one, we relinquish
1380     **       the mutex, call the underlying xOpenExclusive() function, then
1381     **       re-aquire the mutex before seting the AsyncFile.pBaseRead
1382     **       variable.
1383     **     * ASYNC_SYNC and ASYNC_WRITE operations, if
1384     **       SQLITE_ASYNC_TWO_FILEHANDLES was set at compile time and two
1385     **       file-handles are open for the particular file being "synced".
1386     */
1387     if( async.ioError!=SQLITE_OK && p->op!=ASYNC_CLOSE ){
1388       p->op = ASYNC_NOOP;
1389     }
1390     if( p->pFileData ){
1391       pBase = p->pFileData->pBaseWrite;
1392       if(
1393         p->op==ASYNC_CLOSE ||
1394         p->op==ASYNC_OPENEXCLUSIVE ||
1395         (pBase->pMethods && (p->op==ASYNC_SYNC || p->op==ASYNC_WRITE) )
1396       ){
1397         async_mutex_leave(ASYNC_MUTEX_QUEUE);
1398         holdingMutex = 0;
1399       }
1400       if( !pBase->pMethods ){
1401         pBase = p->pFileData->pBaseRead;
1402       }
1403     }
1404 
1405     switch( p->op ){
1406       case ASYNC_NOOP:
1407         break;
1408 
1409       case ASYNC_WRITE:
1410         assert( pBase );
1411         ASYNC_TRACE(("WRITE %s %d bytes at %d\n",
1412                 p->pFileData->zName, p->nByte, p->iOffset));
1413         rc = pBase->pMethods->xWrite(pBase, (void *)(p->zBuf), p->nByte, p->iOffset);
1414         break;
1415 
1416       case ASYNC_SYNC:
1417         assert( pBase );
1418         ASYNC_TRACE(("SYNC %s\n", p->pFileData->zName));
1419         rc = pBase->pMethods->xSync(pBase, p->nByte);
1420         break;
1421 
1422       case ASYNC_TRUNCATE:
1423         assert( pBase );
1424         ASYNC_TRACE(("TRUNCATE %s to %d bytes\n",
1425                 p->pFileData->zName, p->iOffset));
1426         rc = pBase->pMethods->xTruncate(pBase, p->iOffset);
1427         break;
1428 
1429       case ASYNC_CLOSE: {
1430         AsyncFileData *pData = p->pFileData;
1431         ASYNC_TRACE(("CLOSE %s\n", p->pFileData->zName));
1432         if( pData->pBaseWrite->pMethods ){
1433           pData->pBaseWrite->pMethods->xClose(pData->pBaseWrite);
1434         }
1435         if( pData->pBaseRead->pMethods ){
1436           pData->pBaseRead->pMethods->xClose(pData->pBaseRead);
1437         }
1438 
1439         /* Unlink AsyncFileData.lock from the linked list of AsyncFileLock
1440         ** structures for this file. Obtain the async.lockMutex mutex
1441         ** before doing so.
1442         */
1443         async_mutex_enter(ASYNC_MUTEX_LOCK);
1444         rc = unlinkAsyncFile(pData);
1445         async_mutex_leave(ASYNC_MUTEX_LOCK);
1446 
1447         if( !holdingMutex ){
1448           async_mutex_enter(ASYNC_MUTEX_QUEUE);
1449           holdingMutex = 1;
1450         }
1451         assert_mutex_is_held(ASYNC_MUTEX_QUEUE);
1452         async.pQueueFirst = p->pNext;
1453         sqlite3_free(pData);
1454         doNotFree = 1;
1455         break;
1456       }
1457 
1458       case ASYNC_UNLOCK: {
1459         AsyncWrite *pIter;
1460         AsyncFileData *pData = p->pFileData;
1461         int eLock = p->nByte;
1462 
1463         /* When a file is locked by SQLite using the async backend, it is
1464         ** locked within the 'real' file-system synchronously. When it is
1465         ** unlocked, an ASYNC_UNLOCK event is added to the write-queue to
1466         ** unlock the file asynchronously. The design of the async backend
1467         ** requires that the 'real' file-system file be locked from the
1468         ** time that SQLite first locks it (and probably reads from it)
1469         ** until all asynchronous write events that were scheduled before
1470         ** SQLite unlocked the file have been processed.
1471         **
1472         ** This is more complex if SQLite locks and unlocks the file multiple
1473         ** times in quick succession. For example, if SQLite does:
1474         **
1475         **   lock, write, unlock, lock, write, unlock
1476         **
1477         ** Each "lock" operation locks the file immediately. Each "write"
1478         ** and "unlock" operation adds an event to the event queue. If the
1479         ** second "lock" operation is performed before the first "unlock"
1480         ** operation has been processed asynchronously, then the first
1481         ** "unlock" cannot be safely processed as is, since this would mean
1482         ** the file was unlocked when the second "write" operation is
1483         ** processed. To work around this, when processing an ASYNC_UNLOCK
1484         ** operation, SQLite:
1485         **
1486         **   1) Unlocks the file to the minimum of the argument passed to
1487         **      the xUnlock() call and the current lock from SQLite's point
1488         **      of view, and
1489         **
1490         **   2) Only unlocks the file at all if this event is the last
1491         **      ASYNC_UNLOCK event on this file in the write-queue.
1492         */
1493         assert( holdingMutex==1 );
1494         assert( async.pQueueFirst==p );
1495         for(pIter=async.pQueueFirst->pNext; pIter; pIter=pIter->pNext){
1496           if( pIter->pFileData==pData && pIter->op==ASYNC_UNLOCK ) break;
1497         }
1498         if( !pIter ){
1499           async_mutex_enter(ASYNC_MUTEX_LOCK);
1500           pData->lock.eAsyncLock = MIN(
1501               pData->lock.eAsyncLock, MAX(pData->lock.eLock, eLock)
1502           );
1503           assert(pData->lock.eAsyncLock>=pData->lock.eLock);
1504           rc = getFileLock(pData->pLock);
1505           async_mutex_leave(ASYNC_MUTEX_LOCK);
1506         }
1507         break;
1508       }
1509 
1510       case ASYNC_DELETE:
1511         ASYNC_TRACE(("DELETE %s\n", p->zBuf));
1512         rc = pVfs->xDelete(pVfs, p->zBuf, (int)p->iOffset);
1513         if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
1514         break;
1515 
1516       case ASYNC_OPENEXCLUSIVE: {
1517         int flags = (int)p->iOffset;
1518         AsyncFileData *pData = p->pFileData;
1519         ASYNC_TRACE(("OPEN %s flags=%d\n", p->zBuf, (int)p->iOffset));
1520         assert(pData->pBaseRead->pMethods==0 && pData->pBaseWrite->pMethods==0);
1521         rc = pVfs->xOpen(pVfs, pData->zName, pData->pBaseRead, flags, 0);
1522         assert( holdingMutex==0 );
1523         async_mutex_enter(ASYNC_MUTEX_QUEUE);
1524         holdingMutex = 1;
1525         break;
1526       }
1527 
1528       default: assert(!"Illegal value for AsyncWrite.op");
1529     }
1530 
1531     /* If we didn't hang on to the mutex during the IO op, obtain it now
1532     ** so that the AsyncWrite structure can be safely removed from the
1533     ** global write-op queue.
1534     */
1535     if( !holdingMutex ){
1536       async_mutex_enter(ASYNC_MUTEX_QUEUE);
1537       holdingMutex = 1;
1538     }
1539     /* ASYNC_TRACE(("UNLINK %p\n", p)); */
1540     if( p==async.pQueueLast ){
1541       async.pQueueLast = 0;
1542     }
1543     if( !doNotFree ){
1544       assert_mutex_is_held(ASYNC_MUTEX_QUEUE);
1545       async.pQueueFirst = p->pNext;
1546       sqlite3_free(p);
1547     }
1548     assert( holdingMutex );
1549 
1550     /* An IO error has occurred. We cannot report the error back to the
1551     ** connection that requested the I/O since the error happened
1552     ** asynchronously.  The connection has already moved on.  There
1553     ** really is nobody to report the error to.
1554     **
1555     ** The file for which the error occurred may have been a database or
1556     ** journal file. Regardless, none of the currently queued operations
1557     ** associated with the same database should now be performed. Nor should
1558     ** any subsequently requested IO on either a database or journal file
1559     ** handle for the same database be accepted until the main database
1560     ** file handle has been closed and reopened.
1561     **
1562     ** Furthermore, no further IO should be queued or performed on any file
1563     ** handle associated with a database that may have been part of a
1564     ** multi-file transaction that included the database associated with
1565     ** the IO error (i.e. a database ATTACHed to the same handle at some
1566     ** point in time).
1567     */
1568     if( rc!=SQLITE_OK ){
1569       async.ioError = rc;
1570     }
1571 
1572     if( async.ioError && !async.pQueueFirst ){
1573       async_mutex_enter(ASYNC_MUTEX_LOCK);
1574       if( 0==async.pLock ){
1575         async.ioError = SQLITE_OK;
1576       }
1577       async_mutex_leave(ASYNC_MUTEX_LOCK);
1578     }
1579 
1580     /* Drop the queue mutex before continuing to the next write operation
1581     ** in order to give other threads a chance to work with the write queue.
1582     */
1583     if( !async.pQueueFirst || !async.ioError ){
1584       async_mutex_leave(ASYNC_MUTEX_QUEUE);
1585       holdingMutex = 0;
1586       if( async.ioDelay>0 ){
1587         pVfs->xSleep(pVfs, async.ioDelay*1000);
1588       }else{
1589         async_sched_yield();
1590       }
1591     }
1592   }
1593 
1594   async_mutex_leave(ASYNC_MUTEX_WRITER);
1595   return;
1596 }
1597 
1598 /*
1599 ** Install the asynchronous VFS.
1600 */
sqlite3async_initialize(const char * zParent,int isDefault)1601 int sqlite3async_initialize(const char *zParent, int isDefault){
1602   int rc = SQLITE_OK;
1603   if( async_vfs.pAppData==0 ){
1604     sqlite3_vfs *pParent = sqlite3_vfs_find(zParent);
1605     if( !pParent || async_os_initialize() ){
1606       rc = SQLITE_ERROR;
1607     }else if( SQLITE_OK!=(rc = sqlite3_vfs_register(&async_vfs, isDefault)) ){
1608       async_os_shutdown();
1609     }else{
1610       async_vfs.pAppData = (void *)pParent;
1611       async_vfs.mxPathname = ((sqlite3_vfs *)async_vfs.pAppData)->mxPathname;
1612     }
1613   }
1614   return rc;
1615 }
1616 
1617 /*
1618 ** Uninstall the asynchronous VFS.
1619 */
sqlite3async_shutdown(void)1620 void sqlite3async_shutdown(void){
1621   if( async_vfs.pAppData ){
1622     async_os_shutdown();
1623     sqlite3_vfs_unregister((sqlite3_vfs *)&async_vfs);
1624     async_vfs.pAppData = 0;
1625   }
1626 }
1627 
1628 /*
1629 ** Process events on the write-queue.
1630 */
sqlite3async_run(void)1631 void sqlite3async_run(void){
1632   asyncWriterThread();
1633 }
1634 
1635 /*
1636 ** Control/configure the asynchronous IO system.
1637 */
sqlite3async_control(int op,...)1638 int sqlite3async_control(int op, ...){
1639   int rc = SQLITE_OK;
1640   va_list ap;
1641   va_start(ap, op);
1642   switch( op ){
1643     case SQLITEASYNC_HALT: {
1644       int eWhen = va_arg(ap, int);
1645       if( eWhen!=SQLITEASYNC_HALT_NEVER
1646        && eWhen!=SQLITEASYNC_HALT_NOW
1647        && eWhen!=SQLITEASYNC_HALT_IDLE
1648       ){
1649         rc = SQLITE_MISUSE;
1650         break;
1651       }
1652       async.eHalt = eWhen;
1653       async_mutex_enter(ASYNC_MUTEX_QUEUE);
1654       async_cond_signal(ASYNC_COND_QUEUE);
1655       async_mutex_leave(ASYNC_MUTEX_QUEUE);
1656       break;
1657     }
1658 
1659     case SQLITEASYNC_DELAY: {
1660       int iDelay = va_arg(ap, int);
1661       if( iDelay<0 ){
1662         rc = SQLITE_MISUSE;
1663         break;
1664       }
1665       async.ioDelay = iDelay;
1666       break;
1667     }
1668 
1669     case SQLITEASYNC_LOCKFILES: {
1670       int bLock = va_arg(ap, int);
1671       async_mutex_enter(ASYNC_MUTEX_QUEUE);
1672       if( async.nFile || async.pQueueFirst ){
1673         async_mutex_leave(ASYNC_MUTEX_QUEUE);
1674         rc = SQLITE_MISUSE;
1675         break;
1676       }
1677       async.bLockFiles = bLock;
1678       async_mutex_leave(ASYNC_MUTEX_QUEUE);
1679       break;
1680     }
1681 
1682     case SQLITEASYNC_GET_HALT: {
1683       int *peWhen = va_arg(ap, int *);
1684       *peWhen = async.eHalt;
1685       break;
1686     }
1687     case SQLITEASYNC_GET_DELAY: {
1688       int *piDelay = va_arg(ap, int *);
1689       *piDelay = async.ioDelay;
1690       break;
1691     }
1692     case SQLITEASYNC_GET_LOCKFILES: {
1693       int *piDelay = va_arg(ap, int *);
1694       *piDelay = async.bLockFiles;
1695       break;
1696     }
1697 
1698     default:
1699       rc = SQLITE_ERROR;
1700       break;
1701   }
1702   va_end(ap);
1703   return rc;
1704 }
1705 
1706 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ASYNCIO) */
1707 
1708