1 /* 2 ** 2008 October 7 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 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. 16 */ 17 #include "sqliteInt.h" 18 19 /* Forward references to internal structures */ 20 typedef struct MemJournal MemJournal; 21 typedef struct FilePoint FilePoint; 22 typedef struct FileChunk FileChunk; 23 24 /* 25 ** The rollback journal is composed of a linked list of these structures. 26 ** 27 ** The zChunk array is always at least 8 bytes in size - usually much more. 28 ** Its actual size is stored in the MemJournal.nChunkSize variable. 29 */ 30 struct FileChunk { 31 FileChunk *pNext; /* Next chunk in the journal */ 32 u8 zChunk[8]; /* Content of this chunk */ 33 }; 34 35 /* 36 ** By default, allocate this many bytes of memory for each FileChunk object. 37 */ 38 #define MEMJOURNAL_DFLT_FILECHUNKSIZE 1024 39 40 /* 41 ** For chunk size nChunkSize, return the number of bytes that should 42 ** be allocated for each FileChunk structure. 43 */ 44 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8)) 45 46 /* 47 ** An instance of this object serves as a cursor into the rollback journal. 48 ** The cursor can be either for reading or writing. 49 */ 50 struct FilePoint { 51 sqlite3_int64 iOffset; /* Offset from the beginning of the file */ 52 FileChunk *pChunk; /* Specific chunk into which cursor points */ 53 }; 54 55 /* 56 ** This structure is a subclass of sqlite3_file. Each open memory-journal 57 ** is an instance of this class. 58 */ 59 struct MemJournal { 60 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */ 61 int nChunkSize; /* In-memory chunk-size */ 62 63 int nSpill; /* Bytes of data before flushing */ 64 int nSize; /* Bytes of data currently in memory */ 65 FileChunk *pFirst; /* Head of in-memory chunk-list */ 66 FilePoint endpoint; /* Pointer to the end of the file */ 67 FilePoint readpoint; /* Pointer to the end of the last xRead() */ 68 69 int flags; /* xOpen flags */ 70 sqlite3_vfs *pVfs; /* The "real" underlying VFS */ 71 const char *zJournal; /* Name of the journal file */ 72 }; 73 74 /* 75 ** Read data from the in-memory journal file. This is the implementation 76 ** of the sqlite3_vfs.xRead method. 77 */ 78 static int memjrnlRead( 79 sqlite3_file *pJfd, /* The journal file from which to read */ 80 void *zBuf, /* Put the results here */ 81 int iAmt, /* Number of bytes to read */ 82 sqlite_int64 iOfst /* Begin reading at this offset */ 83 ){ 84 MemJournal *p = (MemJournal *)pJfd; 85 u8 *zOut = zBuf; 86 int nRead = iAmt; 87 int iChunkOffset; 88 FileChunk *pChunk; 89 90 #ifdef SQLITE_ENABLE_ATOMIC_WRITE 91 if( (iAmt+iOfst)>p->endpoint.iOffset ){ 92 return SQLITE_IOERR_SHORT_READ; 93 } 94 #endif 95 96 assert( (iAmt+iOfst)<=p->endpoint.iOffset ); 97 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){ 98 sqlite3_int64 iOff = 0; 99 for(pChunk=p->pFirst; 100 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst; 101 pChunk=pChunk->pNext 102 ){ 103 iOff += p->nChunkSize; 104 } 105 }else{ 106 pChunk = p->readpoint.pChunk; 107 } 108 109 iChunkOffset = (int)(iOfst%p->nChunkSize); 110 do { 111 int iSpace = p->nChunkSize - iChunkOffset; 112 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset)); 113 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy); 114 zOut += nCopy; 115 nRead -= iSpace; 116 iChunkOffset = 0; 117 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 ); 118 p->readpoint.iOffset = iOfst+iAmt; 119 p->readpoint.pChunk = pChunk; 120 121 return SQLITE_OK; 122 } 123 124 /* 125 ** Free the list of FileChunk structures headed at MemJournal.pFirst. 126 */ 127 static void memjrnlFreeChunks(MemJournal *p){ 128 FileChunk *pIter; 129 FileChunk *pNext; 130 for(pIter=p->pFirst; pIter; pIter=pNext){ 131 pNext = pIter->pNext; 132 sqlite3_free(pIter); 133 } 134 p->pFirst = 0; 135 } 136 137 /* 138 ** Flush the contents of memory to a real file on disk. 139 */ 140 static int memjrnlCreateFile(MemJournal *p){ 141 int rc; 142 sqlite3_file *pReal = (sqlite3_file*)p; 143 MemJournal copy = *p; 144 145 memset(p, 0, sizeof(MemJournal)); 146 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0); 147 if( rc==SQLITE_OK ){ 148 int nChunk = copy.nChunkSize; 149 i64 iOff = 0; 150 FileChunk *pIter; 151 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){ 152 if( iOff + nChunk > copy.endpoint.iOffset ){ 153 nChunk = copy.endpoint.iOffset - iOff; 154 } 155 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff); 156 if( rc ) break; 157 iOff += nChunk; 158 } 159 if( rc==SQLITE_OK ){ 160 /* No error has occurred. Free the in-memory buffers. */ 161 memjrnlFreeChunks(©); 162 } 163 } 164 if( rc!=SQLITE_OK ){ 165 /* If an error occurred while creating or writing to the file, restore 166 ** the original before returning. This way, SQLite uses the in-memory 167 ** journal data to roll back changes made to the internal page-cache 168 ** before this function was called. */ 169 sqlite3OsClose(pReal); 170 *p = copy; 171 } 172 return rc; 173 } 174 175 176 /* 177 ** Write data to the file. 178 */ 179 static int memjrnlWrite( 180 sqlite3_file *pJfd, /* The journal file into which to write */ 181 const void *zBuf, /* Take data to be written from here */ 182 int iAmt, /* Number of bytes to write */ 183 sqlite_int64 iOfst /* Begin writing at this offset into the file */ 184 ){ 185 MemJournal *p = (MemJournal *)pJfd; 186 int nWrite = iAmt; 187 u8 *zWrite = (u8 *)zBuf; 188 189 /* If the file should be created now, create it and write the new data 190 ** into the file on disk. */ 191 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){ 192 int rc = memjrnlCreateFile(p); 193 if( rc==SQLITE_OK ){ 194 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst); 195 } 196 return rc; 197 } 198 199 /* If the contents of this write should be stored in memory */ 200 else{ 201 /* An in-memory journal file should only ever be appended to. Random 202 ** access writes are not required. The only exception to this is when 203 ** the in-memory journal is being used by a connection using the 204 ** atomic-write optimization. In this case the first 28 bytes of the 205 ** journal file may be written as part of committing the transaction. */ 206 assert( iOfst==p->endpoint.iOffset || iOfst==0 ); 207 #ifdef SQLITE_ENABLE_ATOMIC_WRITE 208 if( iOfst==0 && p->pFirst ){ 209 assert( p->nChunkSize>iAmt ); 210 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt); 211 }else 212 #else 213 assert( iOfst>0 || p->pFirst==0 ); 214 #endif 215 { 216 while( nWrite>0 ){ 217 FileChunk *pChunk = p->endpoint.pChunk; 218 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize); 219 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset); 220 221 if( iChunkOffset==0 ){ 222 /* New chunk is required to extend the file. */ 223 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)); 224 if( !pNew ){ 225 return SQLITE_IOERR_NOMEM_BKPT; 226 } 227 pNew->pNext = 0; 228 if( pChunk ){ 229 assert( p->pFirst ); 230 pChunk->pNext = pNew; 231 }else{ 232 assert( !p->pFirst ); 233 p->pFirst = pNew; 234 } 235 p->endpoint.pChunk = pNew; 236 } 237 238 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace); 239 zWrite += iSpace; 240 nWrite -= iSpace; 241 p->endpoint.iOffset += iSpace; 242 } 243 p->nSize = iAmt + iOfst; 244 } 245 } 246 247 return SQLITE_OK; 248 } 249 250 /* 251 ** Truncate the file. 252 ** 253 ** If the journal file is already on disk, truncate it there. Or, if it 254 ** is still in main memory but is being truncated to zero bytes in size, 255 ** ignore 256 */ 257 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){ 258 MemJournal *p = (MemJournal *)pJfd; 259 if( ALWAYS(size==0) ){ 260 memjrnlFreeChunks(p); 261 p->nSize = 0; 262 p->endpoint.pChunk = 0; 263 p->endpoint.iOffset = 0; 264 p->readpoint.pChunk = 0; 265 p->readpoint.iOffset = 0; 266 } 267 return SQLITE_OK; 268 } 269 270 /* 271 ** Close the file. 272 */ 273 static int memjrnlClose(sqlite3_file *pJfd){ 274 MemJournal *p = (MemJournal *)pJfd; 275 memjrnlFreeChunks(p); 276 return SQLITE_OK; 277 } 278 279 /* 280 ** Sync the file. 281 ** 282 ** If the real file has been created, call its xSync method. Otherwise, 283 ** syncing an in-memory journal is a no-op. 284 */ 285 static int memjrnlSync(sqlite3_file *pJfd, int flags){ 286 UNUSED_PARAMETER2(pJfd, flags); 287 return SQLITE_OK; 288 } 289 290 /* 291 ** Query the size of the file in bytes. 292 */ 293 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){ 294 MemJournal *p = (MemJournal *)pJfd; 295 *pSize = (sqlite_int64) p->endpoint.iOffset; 296 return SQLITE_OK; 297 } 298 299 /* 300 ** Table of methods for MemJournal sqlite3_file object. 301 */ 302 static const struct sqlite3_io_methods MemJournalMethods = { 303 1, /* iVersion */ 304 memjrnlClose, /* xClose */ 305 memjrnlRead, /* xRead */ 306 memjrnlWrite, /* xWrite */ 307 memjrnlTruncate, /* xTruncate */ 308 memjrnlSync, /* xSync */ 309 memjrnlFileSize, /* xFileSize */ 310 0, /* xLock */ 311 0, /* xUnlock */ 312 0, /* xCheckReservedLock */ 313 0, /* xFileControl */ 314 0, /* xSectorSize */ 315 0, /* xDeviceCharacteristics */ 316 0, /* xShmMap */ 317 0, /* xShmLock */ 318 0, /* xShmBarrier */ 319 0, /* xShmUnmap */ 320 0, /* xFetch */ 321 0 /* xUnfetch */ 322 }; 323 324 /* 325 ** Open a journal file. 326 ** 327 ** The behaviour of the journal file depends on the value of parameter 328 ** nSpill. If nSpill is 0, then the journal file is always create and 329 ** accessed using the underlying VFS. If nSpill is less than zero, then 330 ** all content is always stored in main-memory. Finally, if nSpill is a 331 ** positive value, then the journal file is initially created in-memory 332 ** but may be flushed to disk later on. In this case the journal file is 333 ** flushed to disk either when it grows larger than nSpill bytes in size, 334 ** or when sqlite3JournalCreate() is called. 335 */ 336 int sqlite3JournalOpen( 337 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */ 338 const char *zName, /* Name of the journal file */ 339 sqlite3_file *pJfd, /* Preallocated, blank file handle */ 340 int flags, /* Opening flags */ 341 int nSpill /* Bytes buffered before opening the file */ 342 ){ 343 MemJournal *p = (MemJournal*)pJfd; 344 345 /* Zero the file-handle object. If nSpill was passed zero, initialize 346 ** it using the sqlite3OsOpen() function of the underlying VFS. In this 347 ** case none of the code in this module is executed as a result of calls 348 ** made on the journal file-handle. */ 349 memset(p, 0, sizeof(MemJournal)); 350 if( nSpill==0 ){ 351 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0); 352 } 353 354 if( nSpill>0 ){ 355 p->nChunkSize = nSpill; 356 }else{ 357 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk); 358 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) ); 359 } 360 361 p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods; 362 p->nSpill = nSpill; 363 p->flags = flags; 364 p->zJournal = zName; 365 p->pVfs = pVfs; 366 return SQLITE_OK; 367 } 368 369 /* 370 ** Open an in-memory journal file. 371 */ 372 void sqlite3MemJournalOpen(sqlite3_file *pJfd){ 373 sqlite3JournalOpen(0, 0, pJfd, 0, -1); 374 } 375 376 #ifdef SQLITE_ENABLE_ATOMIC_WRITE 377 /* 378 ** If the argument p points to a MemJournal structure that is not an 379 ** in-memory-only journal file (i.e. is one that was opened with a +ve 380 ** nSpill parameter), and the underlying file has not yet been created, 381 ** create it now. 382 */ 383 int sqlite3JournalCreate(sqlite3_file *p){ 384 int rc = SQLITE_OK; 385 if( p->pMethods==&MemJournalMethods && ((MemJournal*)p)->nSpill>0 ){ 386 rc = memjrnlCreateFile((MemJournal*)p); 387 } 388 return rc; 389 } 390 #endif 391 392 /* 393 ** The file-handle passed as the only argument is open on a journal file. 394 ** Return true if this "journal file" is currently stored in heap memory, 395 ** or false otherwise. 396 */ 397 int sqlite3JournalIsInMemory(sqlite3_file *p){ 398 return p->pMethods==&MemJournalMethods; 399 } 400 401 /* 402 ** Return the number of bytes required to store a JournalFile that uses vfs 403 ** pVfs to create the underlying on-disk files. 404 */ 405 int sqlite3JournalSize(sqlite3_vfs *pVfs){ 406 return MAX(pVfs->szOsFile, sizeof(MemJournal)); 407 } 408