blob: 598d5cc026beafcec4afc782c944814429fef5a7 [file] [log] [blame]
danielk197739281b42008-10-17 19:13:04 +00001/*
drh27c3bd72008-10-28 18:12:36 +00002** 2008 October 7
danielk197739281b42008-10-17 19:13:04 +00003**
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**
drh27c3bd72008-10-28 18:12:36 +000013** This file contains code use to implement an in-memory rollback journal.
14** The in-memory rollback journal is used to journal transactions for
15** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
drh70b8d6b2016-04-12 11:58:18 +000016**
17** Update: The in-memory journal is also used to temporarily cache
18** smaller journals that are not critical for power-loss recovery.
19** For example, statement journals that are not too big will be held
20** entirely in memory, thus reducing the number of file I/O calls, and
21** more importantly, reducing temporary file creation events. If these
22** journals become too large for memory, they are spilled to disk. But
23** in the common case, they are usually small and no file I/O needs to
24** occur.
danielk197739281b42008-10-17 19:13:04 +000025*/
danielk197739281b42008-10-17 19:13:04 +000026#include "sqliteInt.h"
27
drh27c3bd72008-10-28 18:12:36 +000028/* Forward references to internal structures */
danielk197739281b42008-10-17 19:13:04 +000029typedef struct MemJournal MemJournal;
30typedef struct FilePoint FilePoint;
31typedef struct FileChunk FileChunk;
32
drh27c3bd72008-10-28 18:12:36 +000033/*
34** The rollback journal is composed of a linked list of these structures.
dan2491de22016-02-27 20:14:55 +000035**
36** The zChunk array is always at least 8 bytes in size - usually much more.
37** Its actual size is stored in the MemJournal.nChunkSize variable.
drh27c3bd72008-10-28 18:12:36 +000038*/
danielk197739281b42008-10-17 19:13:04 +000039struct FileChunk {
drh27c3bd72008-10-28 18:12:36 +000040 FileChunk *pNext; /* Next chunk in the journal */
dan2491de22016-02-27 20:14:55 +000041 u8 zChunk[8]; /* Content of this chunk */
danielk197739281b42008-10-17 19:13:04 +000042};
43
drh27c3bd72008-10-28 18:12:36 +000044/*
dan2491de22016-02-27 20:14:55 +000045** By default, allocate this many bytes of memory for each FileChunk object.
46*/
47#define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024
48
49/*
50** For chunk size nChunkSize, return the number of bytes that should
51** be allocated for each FileChunk structure.
52*/
53#define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
54
55/*
drh27c3bd72008-10-28 18:12:36 +000056** An instance of this object serves as a cursor into the rollback journal.
57** The cursor can be either for reading or writing.
58*/
danielk197739281b42008-10-17 19:13:04 +000059struct FilePoint {
drh27c3bd72008-10-28 18:12:36 +000060 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
61 FileChunk *pChunk; /* Specific chunk into which cursor points */
danielk197739281b42008-10-17 19:13:04 +000062};
63
drh27c3bd72008-10-28 18:12:36 +000064/*
dan2491de22016-02-27 20:14:55 +000065** This structure is a subclass of sqlite3_file. Each open memory-journal
drh27c3bd72008-10-28 18:12:36 +000066** is an instance of this class.
67*/
danielk197739281b42008-10-17 19:13:04 +000068struct MemJournal {
dan2491de22016-02-27 20:14:55 +000069 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
70 int nChunkSize; /* In-memory chunk-size */
71
drhc2f18ad2016-03-05 15:35:09 +000072 int nSpill; /* Bytes of data before flushing */
danielk197739281b42008-10-17 19:13:04 +000073 FileChunk *pFirst; /* Head of in-memory chunk-list */
74 FilePoint endpoint; /* Pointer to the end of the file */
75 FilePoint readpoint; /* Pointer to the end of the last xRead() */
dan2491de22016-02-27 20:14:55 +000076
77 int flags; /* xOpen flags */
78 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
79 const char *zJournal; /* Name of the journal file */
danielk197739281b42008-10-17 19:13:04 +000080};
81
82/*
drh2206a2b2009-04-01 23:09:43 +000083** Read data from the in-memory journal file. This is the implementation
84** of the sqlite3_vfs.xRead method.
danielk197739281b42008-10-17 19:13:04 +000085*/
86static int memjrnlRead(
87 sqlite3_file *pJfd, /* The journal file from which to read */
88 void *zBuf, /* Put the results here */
89 int iAmt, /* Number of bytes to read */
90 sqlite_int64 iOfst /* Begin reading at this offset */
91){
92 MemJournal *p = (MemJournal *)pJfd;
drhd93b2b82016-03-09 04:17:17 +000093 u8 *zOut = zBuf;
94 int nRead = iAmt;
95 int iChunkOffset;
96 FileChunk *pChunk;
97
dan7ed40202016-03-08 17:44:08 +000098 if( (iAmt+iOfst)>p->endpoint.iOffset ){
dan2491de22016-02-27 20:14:55 +000099 return SQLITE_IOERR_SHORT_READ;
dan2491de22016-02-27 20:14:55 +0000100 }
drh38b3dde2016-04-07 18:42:23 +0000101 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
drhd93b2b82016-03-09 04:17:17 +0000102 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
103 sqlite3_int64 iOff = 0;
104 for(pChunk=p->pFirst;
105 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
106 pChunk=pChunk->pNext
107 ){
108 iOff += p->nChunkSize;
109 }
110 }else{
111 pChunk = p->readpoint.pChunk;
drh38b3dde2016-04-07 18:42:23 +0000112 assert( pChunk!=0 );
drhd93b2b82016-03-09 04:17:17 +0000113 }
114
115 iChunkOffset = (int)(iOfst%p->nChunkSize);
116 do {
117 int iSpace = p->nChunkSize - iChunkOffset;
118 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
119 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
120 zOut += nCopy;
121 nRead -= iSpace;
122 iChunkOffset = 0;
123 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
drh38b3dde2016-04-07 18:42:23 +0000124 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
drhd93b2b82016-03-09 04:17:17 +0000125 p->readpoint.pChunk = pChunk;
danielk197739281b42008-10-17 19:13:04 +0000126
127 return SQLITE_OK;
128}
129
130/*
dan2491de22016-02-27 20:14:55 +0000131** Free the list of FileChunk structures headed at MemJournal.pFirst.
132*/
danf43fef22021-02-22 15:44:45 +0000133static void memjrnlFreeChunks(FileChunk *pFirst){
dan2491de22016-02-27 20:14:55 +0000134 FileChunk *pIter;
135 FileChunk *pNext;
danf43fef22021-02-22 15:44:45 +0000136 for(pIter=pFirst; pIter; pIter=pNext){
dan2491de22016-02-27 20:14:55 +0000137 pNext = pIter->pNext;
138 sqlite3_free(pIter);
139 }
dan2491de22016-02-27 20:14:55 +0000140}
141
142/*
143** Flush the contents of memory to a real file on disk.
144*/
drhc2f18ad2016-03-05 15:35:09 +0000145static int memjrnlCreateFile(MemJournal *p){
dan7ed40202016-03-08 17:44:08 +0000146 int rc;
147 sqlite3_file *pReal = (sqlite3_file*)p;
148 MemJournal copy = *p;
149
150 memset(p, 0, sizeof(MemJournal));
151 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
152 if( rc==SQLITE_OK ){
153 int nChunk = copy.nChunkSize;
154 i64 iOff = 0;
155 FileChunk *pIter;
drh769b4c92016-03-09 03:44:32 +0000156 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
drhd93b2b82016-03-09 04:17:17 +0000157 if( iOff + nChunk > copy.endpoint.iOffset ){
158 nChunk = copy.endpoint.iOffset - iOff;
dan2491de22016-02-27 20:14:55 +0000159 }
drhd93b2b82016-03-09 04:17:17 +0000160 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
drh769b4c92016-03-09 03:44:32 +0000161 if( rc ) break;
drhd93b2b82016-03-09 04:17:17 +0000162 iOff += nChunk;
dan2491de22016-02-27 20:14:55 +0000163 }
dan7ed40202016-03-08 17:44:08 +0000164 if( rc==SQLITE_OK ){
165 /* No error has occurred. Free the in-memory buffers. */
danf43fef22021-02-22 15:44:45 +0000166 memjrnlFreeChunks(copy.pFirst);
dan7ed40202016-03-08 17:44:08 +0000167 }
168 }
169 if( rc!=SQLITE_OK ){
170 /* If an error occurred while creating or writing to the file, restore
171 ** the original before returning. This way, SQLite uses the in-memory
172 ** journal data to roll back changes made to the internal page-cache
173 ** before this function was called. */
174 sqlite3OsClose(pReal);
175 *p = copy;
dan2491de22016-02-27 20:14:55 +0000176 }
177 return rc;
178}
179
180
181/*
danielk197739281b42008-10-17 19:13:04 +0000182** Write data to the file.
183*/
184static int memjrnlWrite(
185 sqlite3_file *pJfd, /* The journal file into which to write */
186 const void *zBuf, /* Take data to be written from here */
187 int iAmt, /* Number of bytes to write */
188 sqlite_int64 iOfst /* Begin writing at this offset into the file */
189){
190 MemJournal *p = (MemJournal *)pJfd;
191 int nWrite = iAmt;
192 u8 *zWrite = (u8 *)zBuf;
193
dan7ed40202016-03-08 17:44:08 +0000194 /* If the file should be created now, create it and write the new data
195 ** into the file on disk. */
196 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
drhc2f18ad2016-03-05 15:35:09 +0000197 int rc = memjrnlCreateFile(p);
dan2491de22016-02-27 20:14:55 +0000198 if( rc==SQLITE_OK ){
dan7ed40202016-03-08 17:44:08 +0000199 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
danielk197739281b42008-10-17 19:13:04 +0000200 }
dan2491de22016-02-27 20:14:55 +0000201 return rc;
202 }
danielk197739281b42008-10-17 19:13:04 +0000203
dan2491de22016-02-27 20:14:55 +0000204 /* If the contents of this write should be stored in memory */
205 else{
206 /* An in-memory journal file should only ever be appended to. Random
207 ** access writes are not required. The only exception to this is when
208 ** the in-memory journal is being used by a connection using the
209 ** atomic-write optimization. In this case the first 28 bytes of the
210 ** journal file may be written as part of committing the transaction. */
211 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
drh94e32ac2017-07-20 21:01:53 +0000212#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
dand67a9772017-07-20 21:00:03 +0000213 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
dan2491de22016-02-27 20:14:55 +0000214 if( iOfst==0 && p->pFirst ){
215 assert( p->nChunkSize>iAmt );
drh65a7e762016-03-05 15:03:31 +0000216 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
drh273021d2016-03-09 02:03:03 +0000217 }else
218#else
219 assert( iOfst>0 || p->pFirst==0 );
220#endif
221 {
dan2491de22016-02-27 20:14:55 +0000222 while( nWrite>0 ){
223 FileChunk *pChunk = p->endpoint.pChunk;
224 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
225 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
226
227 if( iChunkOffset==0 ){
228 /* New chunk is required to extend the file. */
229 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
230 if( !pNew ){
231 return SQLITE_IOERR_NOMEM_BKPT;
232 }
233 pNew->pNext = 0;
234 if( pChunk ){
235 assert( p->pFirst );
236 pChunk->pNext = pNew;
237 }else{
238 assert( !p->pFirst );
239 p->pFirst = pNew;
240 }
241 p->endpoint.pChunk = pNew;
242 }
243
drh65a7e762016-03-05 15:03:31 +0000244 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
dan2491de22016-02-27 20:14:55 +0000245 zWrite += iSpace;
246 nWrite -= iSpace;
247 p->endpoint.iOffset += iSpace;
248 }
dan2491de22016-02-27 20:14:55 +0000249 }
danielk197739281b42008-10-17 19:13:04 +0000250 }
251
252 return SQLITE_OK;
253}
254
255/*
danf43fef22021-02-22 15:44:45 +0000256** Truncate the in-memory file.
danielk197739281b42008-10-17 19:13:04 +0000257*/
258static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
259 MemJournal *p = (MemJournal *)pJfd;
danc00727a2021-05-24 14:35:19 +0000260 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
261 if( size<p->endpoint.iOffset ){
262 FileChunk *pIter = 0;
263 if( size==0 ){
264 memjrnlFreeChunks(p->pFirst);
265 p->pFirst = 0;
266 }else{
267 i64 iOff = p->nChunkSize;
268 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<=size; pIter=pIter->pNext){
269 iOff += p->nChunkSize;
270 }
271 if( ALWAYS(pIter) ){
272 memjrnlFreeChunks(pIter->pNext);
273 pIter->pNext = 0;
274 }
275 }
danf43fef22021-02-22 15:44:45 +0000276
danc00727a2021-05-24 14:35:19 +0000277 p->endpoint.pChunk = pIter;
278 p->endpoint.iOffset = size;
279 p->readpoint.pChunk = 0;
280 p->readpoint.iOffset = 0;
danielk197739281b42008-10-17 19:13:04 +0000281 }
danielk197739281b42008-10-17 19:13:04 +0000282 return SQLITE_OK;
283}
284
285/*
286** Close the file.
287*/
288static int memjrnlClose(sqlite3_file *pJfd){
dan2491de22016-02-27 20:14:55 +0000289 MemJournal *p = (MemJournal *)pJfd;
danf43fef22021-02-22 15:44:45 +0000290 memjrnlFreeChunks(p->pFirst);
danielk197739281b42008-10-17 19:13:04 +0000291 return SQLITE_OK;
292}
293
danielk197739281b42008-10-17 19:13:04 +0000294/*
295** Sync the file.
drh2206a2b2009-04-01 23:09:43 +0000296**
dan2491de22016-02-27 20:14:55 +0000297** If the real file has been created, call its xSync method. Otherwise,
298** syncing an in-memory journal is a no-op.
danielk197739281b42008-10-17 19:13:04 +0000299*/
dan2491de22016-02-27 20:14:55 +0000300static int memjrnlSync(sqlite3_file *pJfd, int flags){
dan7ed40202016-03-08 17:44:08 +0000301 UNUSED_PARAMETER2(pJfd, flags);
drh09c0f6d2010-04-12 19:44:22 +0000302 return SQLITE_OK;
303}
danielk197739281b42008-10-17 19:13:04 +0000304
305/*
306** Query the size of the file in bytes.
307*/
308static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
309 MemJournal *p = (MemJournal *)pJfd;
310 *pSize = (sqlite_int64) p->endpoint.iOffset;
311 return SQLITE_OK;
312}
313
314/*
315** Table of methods for MemJournal sqlite3_file object.
316*/
drhf83dc1e2010-06-03 12:09:52 +0000317static const struct sqlite3_io_methods MemJournalMethods = {
danielk197739281b42008-10-17 19:13:04 +0000318 1, /* iVersion */
319 memjrnlClose, /* xClose */
320 memjrnlRead, /* xRead */
321 memjrnlWrite, /* xWrite */
322 memjrnlTruncate, /* xTruncate */
323 memjrnlSync, /* xSync */
324 memjrnlFileSize, /* xFileSize */
325 0, /* xLock */
326 0, /* xUnlock */
327 0, /* xCheckReservedLock */
328 0, /* xFileControl */
329 0, /* xSectorSize */
drhff828942010-06-26 21:34:06 +0000330 0, /* xDeviceCharacteristics */
drhff828942010-06-26 21:34:06 +0000331 0, /* xShmMap */
drh6e1f4822010-07-13 23:41:40 +0000332 0, /* xShmLock */
drhff828942010-06-26 21:34:06 +0000333 0, /* xShmBarrier */
drhda8caa02013-04-22 23:38:50 +0000334 0, /* xShmUnmap */
335 0, /* xFetch */
336 0 /* xUnfetch */
danielk197739281b42008-10-17 19:13:04 +0000337};
338
339/*
dan2491de22016-02-27 20:14:55 +0000340** Open a journal file.
341**
342** The behaviour of the journal file depends on the value of parameter
drhc2f18ad2016-03-05 15:35:09 +0000343** nSpill. If nSpill is 0, then the journal file is always create and
344** accessed using the underlying VFS. If nSpill is less than zero, then
345** all content is always stored in main-memory. Finally, if nSpill is a
dan2491de22016-02-27 20:14:55 +0000346** positive value, then the journal file is initially created in-memory
347** but may be flushed to disk later on. In this case the journal file is
drhc2f18ad2016-03-05 15:35:09 +0000348** flushed to disk either when it grows larger than nSpill bytes in size,
dan2491de22016-02-27 20:14:55 +0000349** or when sqlite3JournalCreate() is called.
danielk197739281b42008-10-17 19:13:04 +0000350*/
dan2491de22016-02-27 20:14:55 +0000351int sqlite3JournalOpen(
352 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
353 const char *zName, /* Name of the journal file */
354 sqlite3_file *pJfd, /* Preallocated, blank file handle */
355 int flags, /* Opening flags */
drhc2f18ad2016-03-05 15:35:09 +0000356 int nSpill /* Bytes buffered before opening the file */
dan2491de22016-02-27 20:14:55 +0000357){
358 MemJournal *p = (MemJournal*)pJfd;
359
drhc2f18ad2016-03-05 15:35:09 +0000360 /* Zero the file-handle object. If nSpill was passed zero, initialize
dan2491de22016-02-27 20:14:55 +0000361 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
362 ** case none of the code in this module is executed as a result of calls
363 ** made on the journal file-handle. */
dan7ed40202016-03-08 17:44:08 +0000364 memset(p, 0, sizeof(MemJournal));
drhc2f18ad2016-03-05 15:35:09 +0000365 if( nSpill==0 ){
dan2491de22016-02-27 20:14:55 +0000366 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
367 }
368
drhc2f18ad2016-03-05 15:35:09 +0000369 if( nSpill>0 ){
370 p->nChunkSize = nSpill;
dan2491de22016-02-27 20:14:55 +0000371 }else{
372 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
373 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
374 }
375
drhd9059bd2020-07-24 09:14:44 +0000376 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
drhc2f18ad2016-03-05 15:35:09 +0000377 p->nSpill = nSpill;
dan2491de22016-02-27 20:14:55 +0000378 p->flags = flags;
379 p->zJournal = zName;
380 p->pVfs = pVfs;
381 return SQLITE_OK;
danielk197739281b42008-10-17 19:13:04 +0000382}
383
384/*
dan2491de22016-02-27 20:14:55 +0000385** Open an in-memory journal file.
386*/
387void sqlite3MemJournalOpen(sqlite3_file *pJfd){
388 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
389}
390
dand67a9772017-07-20 21:00:03 +0000391#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
392 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
dan2491de22016-02-27 20:14:55 +0000393/*
dan2491de22016-02-27 20:14:55 +0000394** If the argument p points to a MemJournal structure that is not an
dan5f37ed52016-02-29 20:00:13 +0000395** in-memory-only journal file (i.e. is one that was opened with a +ve
danefe16972017-07-20 19:49:14 +0000396** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
397** file has not yet been created, create it now.
dan2491de22016-02-27 20:14:55 +0000398*/
danefe16972017-07-20 19:49:14 +0000399int sqlite3JournalCreate(sqlite3_file *pJfd){
dan2491de22016-02-27 20:14:55 +0000400 int rc = SQLITE_OK;
danefe16972017-07-20 19:49:14 +0000401 MemJournal *p = (MemJournal*)pJfd;
drhd9059bd2020-07-24 09:14:44 +0000402 if( pJfd->pMethods==&MemJournalMethods && (
drh9e611042017-07-28 18:16:14 +0000403#ifdef SQLITE_ENABLE_ATOMIC_WRITE
404 p->nSpill>0
405#else
406 /* While this appears to not be possible without ATOMIC_WRITE, the
407 ** paths are complex, so it seems prudent to leave the test in as
408 ** a NEVER(), in case our analysis is subtly flawed. */
409 NEVER(p->nSpill>0)
410#endif
dand67a9772017-07-20 21:00:03 +0000411#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
412 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
413#endif
414 )){
danefe16972017-07-20 19:49:14 +0000415 rc = memjrnlCreateFile(p);
dan2491de22016-02-27 20:14:55 +0000416 }
417 return rc;
418}
drhff6b8262016-03-04 00:13:29 +0000419#endif
dan2491de22016-02-27 20:14:55 +0000420
421/*
dan5f37ed52016-02-29 20:00:13 +0000422** The file-handle passed as the only argument is open on a journal file.
423** Return true if this "journal file" is currently stored in heap memory,
dan2491de22016-02-27 20:14:55 +0000424** or false otherwise.
425*/
426int sqlite3JournalIsInMemory(sqlite3_file *p){
dan7ed40202016-03-08 17:44:08 +0000427 return p->pMethods==&MemJournalMethods;
danielk197739281b42008-10-17 19:13:04 +0000428}
429
430/*
dan2491de22016-02-27 20:14:55 +0000431** Return the number of bytes required to store a JournalFile that uses vfs
432** pVfs to create the underlying on-disk files.
danielk197739281b42008-10-17 19:13:04 +0000433*/
dan2491de22016-02-27 20:14:55 +0000434int sqlite3JournalSize(sqlite3_vfs *pVfs){
drh13969f52016-03-21 22:28:51 +0000435 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
danielk197739281b42008-10-17 19:13:04 +0000436}