blob: 0a3657c3b5984a2f0334fb7c8a7949a615f63d9c [file] [log] [blame]
dan7c246102010-04-12 19:00:29 +00001/*
drh7ed91f22010-04-29 22:34:07 +00002** 2010 February 1
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 the implementation of a write-ahead log file used in
dan7c246102010-04-12 19:00:29 +000014** "journal_mode=wal" mode.
15*/
dan5cf53532010-05-01 16:40:20 +000016#ifndef SQLITE_OMIT_WAL
17
drh7ed91f22010-04-29 22:34:07 +000018#include "wal.h"
dan7c246102010-04-12 19:00:29 +000019
dan4b64c1e2010-04-27 18:49:54 +000020
dan97a31352010-04-16 13:59:31 +000021/*
drh7ed91f22010-04-29 22:34:07 +000022** WRITE-AHEAD LOG (WAL) FILE FORMAT
dan97a31352010-04-16 13:59:31 +000023**
drh7ed91f22010-04-29 22:34:07 +000024** A wal file consists of a header followed by zero or more "frames".
25** The header is 12 bytes in size and consists of the following three
dan97a31352010-04-16 13:59:31 +000026** big-endian 32-bit unsigned integer values:
27**
dan3de777f2010-04-17 12:31:37 +000028** 0: Database page size,
29** 4: Randomly selected salt value 1,
30** 8: Randomly selected salt value 2.
dan97a31352010-04-16 13:59:31 +000031**
drh7ed91f22010-04-29 22:34:07 +000032** Immediately following the header are zero or more frames. Each
dan97a31352010-04-16 13:59:31 +000033** frame itself consists of a 16-byte header followed by a <page-size> bytes
34** of page data. The header is broken into 4 big-endian 32-bit unsigned
35** integer values, as follows:
36**
dan3de777f2010-04-17 12:31:37 +000037** 0: Page number.
38** 4: For commit records, the size of the database image in pages
dan97a31352010-04-16 13:59:31 +000039** after the commit. For all other records, zero.
dan3de777f2010-04-17 12:31:37 +000040** 8: Checksum value 1.
dan97a31352010-04-16 13:59:31 +000041** 12: Checksum value 2.
42*/
43
44/*
drh7ed91f22010-04-29 22:34:07 +000045** WAL-INDEX FILE FORMAT
dan97a31352010-04-16 13:59:31 +000046**
drh7ed91f22010-04-29 22:34:07 +000047** The wal-index file consists of a 32-byte header region, followed by an
48** 8-byte region that contains no useful data (used to apply byte-range locks
danff207012010-04-24 04:49:15 +000049** to), followed by the data region.
50**
51** The contents of both the header and data region are specified in terms
52** of 1, 2 and 4 byte unsigned integers. All integers are stored in
drh7ed91f22010-04-29 22:34:07 +000053** machine-endian order. The wal-index is not a persistent file and
54** so it does not need to be portable across archtectures.
danff207012010-04-24 04:49:15 +000055**
drh7ed91f22010-04-29 22:34:07 +000056** A wal-index file is essentially a shadow-pager map. It contains a
57** mapping from database page number to the set of locations in the wal
danff207012010-04-24 04:49:15 +000058** file that contain versions of the database page. When a database
drh7ed91f22010-04-29 22:34:07 +000059** client needs to read a page of data, it first queries the wal-index
danff207012010-04-24 04:49:15 +000060** file to determine if the required version of the page is stored in
drh7ed91f22010-04-29 22:34:07 +000061** the wal. If so, the page is read from the wal. If not, the page is
62** read from the database file.
danff207012010-04-24 04:49:15 +000063**
drh7ed91f22010-04-29 22:34:07 +000064** Whenever a transaction is appended to the wal or a checkpoint transfers
65** data from the wal into the database file, the wal-index is
danff207012010-04-24 04:49:15 +000066** updated accordingly.
67**
drh7ed91f22010-04-29 22:34:07 +000068** The fields in the wal-index file header are described in the comment
69** directly above the definition of struct WalIndexHdr (see below).
70** Immediately following the fields in the WalIndexHdr structure is
danff207012010-04-24 04:49:15 +000071** an 8 byte checksum based on the contents of the header. This field is
drh7ed91f22010-04-29 22:34:07 +000072** not the same as the iCheck1 and iCheck2 fields of the WalIndexHdr.
dan97a31352010-04-16 13:59:31 +000073*/
74
drh7ed91f22010-04-29 22:34:07 +000075/* Object declarations */
76typedef struct WalIndexHdr WalIndexHdr;
77typedef struct WalIterator WalIterator;
dan7c246102010-04-12 19:00:29 +000078
79
80/*
drh7ed91f22010-04-29 22:34:07 +000081** The following object stores a copy of the wal-index header.
dan7c246102010-04-12 19:00:29 +000082**
83** Member variables iCheck1 and iCheck2 contain the checksum for the
drh7ed91f22010-04-29 22:34:07 +000084** last frame written to the wal, or 2 and 3 respectively if the log
dan7c246102010-04-12 19:00:29 +000085** is currently empty.
86*/
drh7ed91f22010-04-29 22:34:07 +000087struct WalIndexHdr {
dan7c246102010-04-12 19:00:29 +000088 u32 iChange; /* Counter incremented each transaction */
89 u32 pgsz; /* Database page size in bytes */
90 u32 iLastPg; /* Address of last valid frame in log */
91 u32 nPage; /* Size of database in pages */
92 u32 iCheck1; /* Checkpoint value 1 */
93 u32 iCheck2; /* Checkpoint value 2 */
94};
95
drh7ed91f22010-04-29 22:34:07 +000096/* Size of serialized WalIndexHdr object. */
97#define WALINDEX_HDR_NFIELD (sizeof(WalIndexHdr) / sizeof(u32))
dan7c246102010-04-12 19:00:29 +000098
drh7ed91f22010-04-29 22:34:07 +000099/* A block of 16 bytes beginning at WALINDEX_LOCK_OFFSET is reserved
danff207012010-04-24 04:49:15 +0000100** for locks. Since some systems only feature mandatory file-locks, we
101** do not read or write data from the region of the file on which locks
102** are applied.
103*/
drh7ed91f22010-04-29 22:34:07 +0000104#define WALINDEX_LOCK_OFFSET ((sizeof(WalIndexHdr))+2*sizeof(u32))
105#define WALINDEX_LOCK_RESERVED 8
dan7c246102010-04-12 19:00:29 +0000106
drh7ed91f22010-04-29 22:34:07 +0000107/* Size of header before each frame in wal */
108#define WAL_FRAME_HDRSIZE 16
danff207012010-04-24 04:49:15 +0000109
drh7ed91f22010-04-29 22:34:07 +0000110/* Size of write ahead log header */
111#define WAL_HDRSIZE 12
dan97a31352010-04-16 13:59:31 +0000112
113/*
drh7ed91f22010-04-29 22:34:07 +0000114** Return the offset of frame iFrame in the write-ahead log file,
115** assuming a database page size of pgsz bytes. The offset returned
116** is to the start of the write-ahead log frame-header.
dan97a31352010-04-16 13:59:31 +0000117*/
drh7ed91f22010-04-29 22:34:07 +0000118#define walFrameOffset(iFrame, pgsz) ( \
119 WAL_HDRSIZE + ((iFrame)-1)*((pgsz)+WAL_FRAME_HDRSIZE) \
dan97a31352010-04-16 13:59:31 +0000120)
dan7c246102010-04-12 19:00:29 +0000121
122/*
drh7ed91f22010-04-29 22:34:07 +0000123** An open write-ahead log file is represented by an instance of the
124** following object.
dance4f05f2010-04-22 19:14:13 +0000125*/
drh7ed91f22010-04-29 22:34:07 +0000126struct Wal {
127 sqlite3_vfs *pVfs; /* The VFS used to create pFd */
128 sqlite3_file *pFd; /* File handle for WAL file */
129 u32 iCallback; /* Value to pass to log callback (or 0) */
130 sqlite3_shm *pWIndex; /* The open wal-index file */
drh5530b762010-04-30 14:39:50 +0000131 int szWIndex; /* Size of the wal-index that is mapped in mem */
drh7ed91f22010-04-29 22:34:07 +0000132 u32 *pWiData; /* Pointer to wal-index content in memory */
133 u8 lockState; /* SQLITE_SHM_xxxx constant showing lock state */
134 u8 readerType; /* SQLITE_SHM_READ or SQLITE_SHM_READ_FULL */
135 WalIndexHdr hdr; /* Wal-index for current snapshot */
drh2d536e12010-05-01 20:17:30 +0000136 char *zName; /* Name of underlying storage */
dan7c246102010-04-12 19:00:29 +0000137};
138
dan64d039e2010-04-13 19:27:31 +0000139
dan7c246102010-04-12 19:00:29 +0000140/*
141** This structure is used to implement an iterator that iterates through
142** all frames in the log in database page order. Where two or more frames
143** correspond to the same database page, the iterator visits only the
144** frame most recently written to the log.
145**
146** The internals of this structure are only accessed by:
147**
drh7ed91f22010-04-29 22:34:07 +0000148** walIteratorInit() - Create a new iterator,
149** walIteratorNext() - Step an iterator,
150** walIteratorFree() - Free an iterator.
dan7c246102010-04-12 19:00:29 +0000151**
drh7ed91f22010-04-29 22:34:07 +0000152** This functionality is used by the checkpoint code (see walCheckpoint()).
dan7c246102010-04-12 19:00:29 +0000153*/
drh7ed91f22010-04-29 22:34:07 +0000154struct WalIterator {
155 int nSegment; /* Size of WalIterator.aSegment[] array */
dan7c246102010-04-12 19:00:29 +0000156 int nFinal; /* Elements in segment nSegment-1 */
drh7ed91f22010-04-29 22:34:07 +0000157 struct WalSegment {
dan7c246102010-04-12 19:00:29 +0000158 int iNext; /* Next aIndex index */
159 u8 *aIndex; /* Pointer to index array */
160 u32 *aDbPage; /* Pointer to db page array */
161 } aSegment[1];
162};
163
dan64d039e2010-04-13 19:27:31 +0000164
dan7c246102010-04-12 19:00:29 +0000165/*
166** Generate an 8 byte checksum based on the data in array aByte[] and the
167** initial values of aCksum[0] and aCksum[1]. The checksum is written into
168** aCksum[] before returning.
dan56d95912010-04-24 19:07:29 +0000169**
170** The range of bytes to checksum is treated as an array of 32-bit
171** little-endian unsigned integers. For each integer X in the array, from
172** start to finish, do the following:
173**
174** aCksum[0] += X;
175** aCksum[1] += aCksum[0];
176**
177** For the calculation above, use 64-bit unsigned accumulators. Before
178** returning, truncate the values to 32-bits as follows:
179**
180** aCksum[0] = (u32)(aCksum[0] + (aCksum[0]>>24));
181** aCksum[1] = (u32)(aCksum[1] + (aCksum[1]>>24));
dan7c246102010-04-12 19:00:29 +0000182*/
drh7ed91f22010-04-29 22:34:07 +0000183static void walChecksumBytes(u8 *aByte, int nByte, u32 *aCksum){
dan39c79f52010-04-15 10:58:51 +0000184 u64 sum1 = aCksum[0];
185 u64 sum2 = aCksum[1];
186 u32 *a32 = (u32 *)aByte;
187 u32 *aEnd = (u32 *)&aByte[nByte];
dan7c246102010-04-12 19:00:29 +0000188
dan7c246102010-04-12 19:00:29 +0000189 assert( (nByte&0x00000003)==0 );
190
dance4f05f2010-04-22 19:14:13 +0000191 if( SQLITE_LITTLEENDIAN ){
192#ifdef SQLITE_DEBUG
193 u8 *a = (u8 *)a32;
194 assert( *a32==(a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24)) );
195#endif
196 do {
197 sum1 += *a32;
198 sum2 += sum1;
199 } while( ++a32<aEnd );
200 }else{
201 do {
202 u8 *a = (u8*)a32;
203 sum1 += a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24);
204 sum2 += sum1;
205 } while( ++a32<aEnd );
206 }
dan7c246102010-04-12 19:00:29 +0000207
dan39c79f52010-04-15 10:58:51 +0000208 aCksum[0] = sum1 + (sum1>>24);
209 aCksum[1] = sum2 + (sum2>>24);
dan7c246102010-04-12 19:00:29 +0000210}
211
212/*
drh7ed91f22010-04-29 22:34:07 +0000213** Attempt to change the lock status.
dan7c246102010-04-12 19:00:29 +0000214**
drh7ed91f22010-04-29 22:34:07 +0000215** When changing the lock status to SQLITE_SHM_READ, store the
216** type of reader lock (either SQLITE_SHM_READ or SQLITE_SHM_READ_FULL)
217** in pWal->readerType.
dan7c246102010-04-12 19:00:29 +0000218*/
drh7ed91f22010-04-29 22:34:07 +0000219static int walSetLock(Wal *pWal, int desiredStatus){
220 int rc, got;
221 if( pWal->lockState==desiredStatus ) return SQLITE_OK;
drh1fbe0f22010-05-03 16:30:27 +0000222 rc = pWal->pVfs->xShmLock(pWal->pVfs, pWal->pWIndex, desiredStatus, &got);
drh49156b22010-04-30 16:12:04 +0000223 pWal->lockState = got;
224 if( got==SQLITE_SHM_READ_FULL || got==SQLITE_SHM_READ ){
225 pWal->readerType = got;
226 pWal->lockState = SQLITE_SHM_READ;
dan7c246102010-04-12 19:00:29 +0000227 }
228 return rc;
229}
230
drh7ed91f22010-04-29 22:34:07 +0000231/*
232** Update the header of the wal-index file.
233*/
234static void walIndexWriteHdr(Wal *pWal, WalIndexHdr *pHdr){
235 u32 *aHdr = pWal->pWiData; /* Write header here */
236 u32 *aCksum = &aHdr[WALINDEX_HDR_NFIELD]; /* Write header cksum here */
danff207012010-04-24 04:49:15 +0000237
drh7ed91f22010-04-29 22:34:07 +0000238 assert( WALINDEX_HDR_NFIELD==sizeof(WalIndexHdr)/4 );
239 assert( aHdr!=0 );
240 memcpy(aHdr, pHdr, sizeof(WalIndexHdr));
danff207012010-04-24 04:49:15 +0000241 aCksum[0] = aCksum[1] = 1;
drh7ed91f22010-04-29 22:34:07 +0000242 walChecksumBytes((u8 *)aHdr, sizeof(WalIndexHdr), aCksum);
dan7c246102010-04-12 19:00:29 +0000243}
244
245/*
246** This function encodes a single frame header and writes it to a buffer
drh7ed91f22010-04-29 22:34:07 +0000247** supplied by the caller. A frame-header is made up of a series of
dan7c246102010-04-12 19:00:29 +0000248** 4-byte big-endian integers, as follows:
249**
250** 0: Database page size in bytes.
251** 4: Page number.
252** 8: New database size (for commit frames, otherwise zero).
253** 12: Frame checksum 1.
254** 16: Frame checksum 2.
255*/
drh7ed91f22010-04-29 22:34:07 +0000256static void walEncodeFrame(
dan7c246102010-04-12 19:00:29 +0000257 u32 *aCksum, /* IN/OUT: Checksum values */
258 u32 iPage, /* Database page number for frame */
259 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
260 int nData, /* Database page size (size of aData[]) */
261 u8 *aData, /* Pointer to page data (for checksum) */
262 u8 *aFrame /* OUT: Write encoded frame here */
263){
drh7ed91f22010-04-29 22:34:07 +0000264 assert( WAL_FRAME_HDRSIZE==16 );
dan7c246102010-04-12 19:00:29 +0000265
dan97a31352010-04-16 13:59:31 +0000266 sqlite3Put4byte(&aFrame[0], iPage);
267 sqlite3Put4byte(&aFrame[4], nTruncate);
dan7c246102010-04-12 19:00:29 +0000268
drh7ed91f22010-04-29 22:34:07 +0000269 walChecksumBytes(aFrame, 8, aCksum);
270 walChecksumBytes(aData, nData, aCksum);
dan7c246102010-04-12 19:00:29 +0000271
dan97a31352010-04-16 13:59:31 +0000272 sqlite3Put4byte(&aFrame[8], aCksum[0]);
273 sqlite3Put4byte(&aFrame[12], aCksum[1]);
dan7c246102010-04-12 19:00:29 +0000274}
275
276/*
277** Return 1 and populate *piPage, *pnTruncate and aCksum if the
278** frame checksum looks Ok. Otherwise return 0.
279*/
drh7ed91f22010-04-29 22:34:07 +0000280static int walDecodeFrame(
dan7c246102010-04-12 19:00:29 +0000281 u32 *aCksum, /* IN/OUT: Checksum values */
282 u32 *piPage, /* OUT: Database page number for frame */
283 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
284 int nData, /* Database page size (size of aData[]) */
285 u8 *aData, /* Pointer to page data (for checksum) */
286 u8 *aFrame /* Frame data */
287){
drh7ed91f22010-04-29 22:34:07 +0000288 assert( WAL_FRAME_HDRSIZE==16 );
dan4a4b01d2010-04-16 11:30:18 +0000289
drh7ed91f22010-04-29 22:34:07 +0000290 walChecksumBytes(aFrame, 8, aCksum);
291 walChecksumBytes(aData, nData, aCksum);
dan7c246102010-04-12 19:00:29 +0000292
dan97a31352010-04-16 13:59:31 +0000293 if( aCksum[0]!=sqlite3Get4byte(&aFrame[8])
294 || aCksum[1]!=sqlite3Get4byte(&aFrame[12])
dan7c246102010-04-12 19:00:29 +0000295 ){
296 /* Checksum failed. */
297 return 0;
298 }
299
dan97a31352010-04-16 13:59:31 +0000300 *piPage = sqlite3Get4byte(&aFrame[0]);
301 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
dan7c246102010-04-12 19:00:29 +0000302 return 1;
303}
304
drh7ed91f22010-04-29 22:34:07 +0000305static void walMergesort8(
306 Pgno *aContent, /* Pages in wal */
dan7c246102010-04-12 19:00:29 +0000307 u8 *aBuffer, /* Buffer of at least *pnList items to use */
308 u8 *aList, /* IN/OUT: List to sort */
309 int *pnList /* IN/OUT: Number of elements in aList[] */
310){
311 int nList = *pnList;
312 if( nList>1 ){
313 int nLeft = nList / 2; /* Elements in left list */
314 int nRight = nList - nLeft; /* Elements in right list */
315 u8 *aLeft = aList; /* Left list */
316 u8 *aRight = &aList[nLeft]; /* Right list */
317 int iLeft = 0; /* Current index in aLeft */
318 int iRight = 0; /* Current index in aright */
319 int iOut = 0; /* Current index in output buffer */
320
321 /* TODO: Change to non-recursive version. */
drh7ed91f22010-04-29 22:34:07 +0000322 walMergesort8(aContent, aBuffer, aLeft, &nLeft);
323 walMergesort8(aContent, aBuffer, aRight, &nRight);
dan7c246102010-04-12 19:00:29 +0000324
325 while( iRight<nRight || iLeft<nLeft ){
326 u8 logpage;
327 Pgno dbpage;
328
329 if( (iLeft<nLeft)
330 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
331 ){
332 logpage = aLeft[iLeft++];
333 }else{
334 logpage = aRight[iRight++];
335 }
336 dbpage = aContent[logpage];
337
338 aBuffer[iOut++] = logpage;
339 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
340
341 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
342 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
343 }
344 memcpy(aList, aBuffer, sizeof(aList[0])*iOut);
345 *pnList = iOut;
346 }
347
348#ifdef SQLITE_DEBUG
349 {
350 int i;
351 for(i=1; i<*pnList; i++){
352 assert( aContent[aList[i]] > aContent[aList[i-1]] );
353 }
354 }
355#endif
356}
357
358
359/*
drh7ed91f22010-04-29 22:34:07 +0000360** Return the index in the WalIndex.aData array that corresponds to
361** frame iFrame. The wal-index file consists of a header, followed by
dan7c246102010-04-12 19:00:29 +0000362** alternating "map" and "index" blocks.
363*/
drh7ed91f22010-04-29 22:34:07 +0000364static int walIndexEntry(u32 iFrame){
danff207012010-04-24 04:49:15 +0000365 return (
drh7ed91f22010-04-29 22:34:07 +0000366 (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)/sizeof(u32)
danff207012010-04-24 04:49:15 +0000367 + (((iFrame-1)>>8)<<6) /* Indexes that occur before iFrame */
368 + iFrame-1 /* Db page numbers that occur before iFrame */
369 );
dan7c246102010-04-12 19:00:29 +0000370}
371
drh7ed91f22010-04-29 22:34:07 +0000372/*
drh5530b762010-04-30 14:39:50 +0000373** Release our reference to the wal-index memory map, if we are holding
374** it.
drh7ed91f22010-04-29 22:34:07 +0000375*/
376static void walIndexUnmap(Wal *pWal){
377 if( pWal->pWiData ){
drh1fbe0f22010-05-03 16:30:27 +0000378 pWal->pVfs->xShmRelease(pWal->pVfs, pWal->pWIndex);
drh7ed91f22010-04-29 22:34:07 +0000379 pWal->pWiData = 0;
380 }
381}
dan7c246102010-04-12 19:00:29 +0000382
383/*
drh5530b762010-04-30 14:39:50 +0000384** Map the wal-index file into memory if it isn't already.
385**
386** The reqSize parameter is the minimum required size of the mapping.
387** A value of -1 means "don't care". The reqSize parameter is ignored
388** if the mapping is already held.
drh7ed91f22010-04-29 22:34:07 +0000389*/
drh5530b762010-04-30 14:39:50 +0000390static int walIndexMap(Wal *pWal, int reqSize){
391 int rc = SQLITE_OK;
392 if( pWal->pWiData==0 ){
drh1fbe0f22010-05-03 16:30:27 +0000393 rc = pWal->pVfs->xShmGet(pWal->pVfs, pWal->pWIndex, reqSize,
394 &pWal->szWIndex, (void**)(char*)&pWal->pWiData);
drh5530b762010-04-30 14:39:50 +0000395 if( rc==SQLITE_OK && pWal->pWiData==0 ){
396 /* Make sure pWal->pWiData is not NULL while we are holding the
397 ** lock on the mapping. */
398 assert( pWal->szWIndex==0 );
399 pWal->pWiData = &pWal->iCallback;
400 }
drh79e6c782010-04-30 02:13:26 +0000401 }
402 return rc;
403}
404
405/*
drh5530b762010-04-30 14:39:50 +0000406** Remap the wal-index so that the mapping covers the full size
407** of the underlying file.
408**
409** If enlargeTo is non-negative, then increase the size of the underlying
410** storage to be at least as big as enlargeTo before remapping.
drh79e6c782010-04-30 02:13:26 +0000411*/
drh5530b762010-04-30 14:39:50 +0000412static int walIndexRemap(Wal *pWal, int enlargeTo){
413 int rc;
414 int sz;
drh1fbe0f22010-05-03 16:30:27 +0000415 rc = pWal->pVfs->xShmSize(pWal->pVfs, pWal->pWIndex, enlargeTo, &sz);
drh5530b762010-04-30 14:39:50 +0000416 if( rc==SQLITE_OK && sz>pWal->szWIndex ){
417 walIndexUnmap(pWal);
418 rc = walIndexMap(pWal, sz);
419 }
drh7ed91f22010-04-29 22:34:07 +0000420 return rc;
421}
422
423/*
424** Increment by which to increase the wal-index file size.
425*/
426#define WALINDEX_MMAP_INCREMENT (64*1024)
427
428/*
429** Set an entry in the wal-index map to map log frame iFrame to db
430** page iPage. Values are always appended to the wal-index (i.e. the
dan7c246102010-04-12 19:00:29 +0000431** value of iFrame is always exactly one more than the value passed to
432** the previous call), but that restriction is not enforced or asserted
433** here.
434*/
drh7ed91f22010-04-29 22:34:07 +0000435static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
danc7991bd2010-05-05 19:04:59 +0000436 int rc;
drh7ed91f22010-04-29 22:34:07 +0000437 u32 iSlot = walIndexEntry(iFrame);
438
danc7991bd2010-05-05 19:04:59 +0000439 rc = walIndexMap(pWal, -1);
440 if( rc!=SQLITE_OK ){
441 return rc;
442 }
danc9d53db2010-04-30 16:50:00 +0000443 while( ((iSlot+128)*sizeof(u32))>=pWal->szWIndex ){
danc9d53db2010-04-30 16:50:00 +0000444 int nByte = pWal->szWIndex + WALINDEX_MMAP_INCREMENT;
dance4f05f2010-04-22 19:14:13 +0000445
drh5530b762010-04-30 14:39:50 +0000446 /* Enlarge the storage, then remap it. */
drh7ed91f22010-04-29 22:34:07 +0000447 rc = walIndexRemap(pWal, nByte);
dan31f98fc2010-04-27 05:42:32 +0000448 if( rc!=SQLITE_OK ){
449 return rc;
450 }
dance4f05f2010-04-22 19:14:13 +0000451 }
452
drh7ed91f22010-04-29 22:34:07 +0000453 /* Set the wal-index entry itself */
454 pWal->pWiData[iSlot] = iPage;
dan7c246102010-04-12 19:00:29 +0000455
456 /* If the frame number is a multiple of 256 (frames are numbered starting
457 ** at 1), build an index of the most recently added 256 frames.
458 */
459 if( (iFrame&0x000000FF)==0 ){
460 int i; /* Iterator used while initializing aIndex */
461 u32 *aFrame; /* Pointer to array of 256 frames */
462 int nIndex; /* Number of entries in index */
463 u8 *aIndex; /* 256 bytes to build index in */
464 u8 *aTmp; /* Scratch space to use while sorting */
465
drh7ed91f22010-04-29 22:34:07 +0000466 aFrame = &pWal->pWiData[iSlot-255];
467 aIndex = (u8 *)&pWal->pWiData[iSlot+1];
dan7c246102010-04-12 19:00:29 +0000468 aTmp = &aIndex[256];
469
470 nIndex = 256;
471 for(i=0; i<256; i++) aIndex[i] = (u8)i;
drh7ed91f22010-04-29 22:34:07 +0000472 walMergesort8(aFrame, aTmp, aIndex, &nIndex);
dan7c246102010-04-12 19:00:29 +0000473 memset(&aIndex[nIndex], aIndex[nIndex-1], 256-nIndex);
474 }
dan31f98fc2010-04-27 05:42:32 +0000475
476 return SQLITE_OK;
dan7c246102010-04-12 19:00:29 +0000477}
478
479
480/*
drh7ed91f22010-04-29 22:34:07 +0000481** Recover the wal-index by reading the write-ahead log file.
482** The caller must hold RECOVER lock on the wal-index file.
dan7c246102010-04-12 19:00:29 +0000483*/
drh7ed91f22010-04-29 22:34:07 +0000484static int walIndexRecover(Wal *pWal){
dan7c246102010-04-12 19:00:29 +0000485 int rc; /* Return Code */
486 i64 nSize; /* Size of log file */
drh7ed91f22010-04-29 22:34:07 +0000487 WalIndexHdr hdr; /* Recovered wal-index header */
dan7c246102010-04-12 19:00:29 +0000488
drh7ed91f22010-04-29 22:34:07 +0000489 assert( pWal->lockState==SQLITE_SHM_RECOVER );
dan7c246102010-04-12 19:00:29 +0000490 memset(&hdr, 0, sizeof(hdr));
491
drh7ed91f22010-04-29 22:34:07 +0000492 rc = sqlite3OsFileSize(pWal->pFd, &nSize);
dan7c246102010-04-12 19:00:29 +0000493 if( rc!=SQLITE_OK ){
494 return rc;
495 }
496
drh7ed91f22010-04-29 22:34:07 +0000497 if( nSize>WAL_FRAME_HDRSIZE ){
498 u8 aBuf[WAL_FRAME_HDRSIZE]; /* Buffer to load first frame header into */
dan7c246102010-04-12 19:00:29 +0000499 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
500 int nFrame; /* Number of bytes at aFrame */
501 u8 *aData; /* Pointer to data part of aFrame buffer */
502 int iFrame; /* Index of last frame read */
503 i64 iOffset; /* Next offset to read from log file */
504 int nPgsz; /* Page size according to the log */
dan97a31352010-04-16 13:59:31 +0000505 u32 aCksum[2]; /* Running checksum */
dan7c246102010-04-12 19:00:29 +0000506
507 /* Read in the first frame header in the file (to determine the
508 ** database page size).
509 */
drh7ed91f22010-04-29 22:34:07 +0000510 rc = sqlite3OsRead(pWal->pFd, aBuf, WAL_HDRSIZE, 0);
dan7c246102010-04-12 19:00:29 +0000511 if( rc!=SQLITE_OK ){
512 return rc;
513 }
514
515 /* If the database page size is not a power of two, or is greater than
516 ** SQLITE_MAX_PAGE_SIZE, conclude that the log file contains no valid data.
517 */
518 nPgsz = sqlite3Get4byte(&aBuf[0]);
dance4f05f2010-04-22 19:14:13 +0000519 if( nPgsz&(nPgsz-1) || nPgsz>SQLITE_MAX_PAGE_SIZE || nPgsz<512 ){
dan7c246102010-04-12 19:00:29 +0000520 goto finished;
521 }
dan97a31352010-04-16 13:59:31 +0000522 aCksum[0] = sqlite3Get4byte(&aBuf[4]);
523 aCksum[1] = sqlite3Get4byte(&aBuf[8]);
dan7c246102010-04-12 19:00:29 +0000524
525 /* Malloc a buffer to read frames into. */
drh7ed91f22010-04-29 22:34:07 +0000526 nFrame = nPgsz + WAL_FRAME_HDRSIZE;
dan7c246102010-04-12 19:00:29 +0000527 aFrame = (u8 *)sqlite3_malloc(nFrame);
528 if( !aFrame ){
529 return SQLITE_NOMEM;
530 }
drh7ed91f22010-04-29 22:34:07 +0000531 aData = &aFrame[WAL_FRAME_HDRSIZE];
dan7c246102010-04-12 19:00:29 +0000532
533 /* Read all frames from the log file. */
534 iFrame = 0;
drh7ed91f22010-04-29 22:34:07 +0000535 for(iOffset=WAL_HDRSIZE; (iOffset+nFrame)<=nSize; iOffset+=nFrame){
dan7c246102010-04-12 19:00:29 +0000536 u32 pgno; /* Database page number for frame */
537 u32 nTruncate; /* dbsize field from frame header */
538 int isValid; /* True if this frame is valid */
539
540 /* Read and decode the next log frame. */
drh7ed91f22010-04-29 22:34:07 +0000541 rc = sqlite3OsRead(pWal->pFd, aFrame, nFrame, iOffset);
dan7c246102010-04-12 19:00:29 +0000542 if( rc!=SQLITE_OK ) break;
drh7ed91f22010-04-29 22:34:07 +0000543 isValid = walDecodeFrame(aCksum, &pgno, &nTruncate, nPgsz, aData, aFrame);
dan7c246102010-04-12 19:00:29 +0000544 if( !isValid ) break;
danc7991bd2010-05-05 19:04:59 +0000545 rc = walIndexAppend(pWal, ++iFrame, pgno);
546 if( rc!=SQLITE_OK ) break;
dan7c246102010-04-12 19:00:29 +0000547
548 /* If nTruncate is non-zero, this is a commit record. */
549 if( nTruncate ){
550 hdr.iCheck1 = aCksum[0];
551 hdr.iCheck2 = aCksum[1];
552 hdr.iLastPg = iFrame;
553 hdr.nPage = nTruncate;
554 hdr.pgsz = nPgsz;
555 }
556 }
557
558 sqlite3_free(aFrame);
559 }else{
560 hdr.iCheck1 = 2;
561 hdr.iCheck2 = 3;
562 }
563
564finished:
drh7ed91f22010-04-29 22:34:07 +0000565 walIndexWriteHdr(pWal, &hdr);
dan7c246102010-04-12 19:00:29 +0000566 return rc;
567}
568
drha8e654e2010-05-04 17:38:42 +0000569/*
dan1018e902010-05-05 15:33:05 +0000570** Close an open wal-index.
drha8e654e2010-05-04 17:38:42 +0000571*/
dan1018e902010-05-05 15:33:05 +0000572static void walIndexClose(Wal *pWal, int isDelete){
drha8e654e2010-05-04 17:38:42 +0000573 sqlite3_shm *pWIndex = pWal->pWIndex;
574 if( pWIndex ){
575 sqlite3_vfs *pVfs = pWal->pVfs;
576 int notUsed;
577 pVfs->xShmLock(pVfs, pWIndex, SQLITE_SHM_UNLOCK, &notUsed);
dan1018e902010-05-05 15:33:05 +0000578 pVfs->xShmClose(pVfs, pWIndex, isDelete);
drha8e654e2010-05-04 17:38:42 +0000579 }
580}
581
dan7c246102010-04-12 19:00:29 +0000582/*
583** Open a connection to the log file associated with database zDb. The
584** database file does not actually have to exist. zDb is used only to
585** figure out the name of the log file to open. If the log file does not
586** exist it is created by this call.
dan3de777f2010-04-17 12:31:37 +0000587**
588** A SHARED lock should be held on the database file when this function
589** is called. The purpose of this SHARED lock is to prevent any other
drh7ed91f22010-04-29 22:34:07 +0000590** client from unlinking the log or wal-index file. If another process
dan3de777f2010-04-17 12:31:37 +0000591** were to do this just after this client opened one of these files, the
592** system would be badly broken.
danef378022010-05-04 11:06:03 +0000593**
594** If the log file is successfully opened, SQLITE_OK is returned and
595** *ppWal is set to point to a new WAL handle. If an error occurs,
596** an SQLite error code is returned and *ppWal is left unmodified.
dan7c246102010-04-12 19:00:29 +0000597*/
drhc438efd2010-04-26 00:19:45 +0000598int sqlite3WalOpen(
drh7ed91f22010-04-29 22:34:07 +0000599 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
dan7c246102010-04-12 19:00:29 +0000600 const char *zDb, /* Name of database file */
drh7ed91f22010-04-29 22:34:07 +0000601 Wal **ppWal /* OUT: Allocated Wal handle */
dan7c246102010-04-12 19:00:29 +0000602){
danef378022010-05-04 11:06:03 +0000603 int rc; /* Return Code */
drh7ed91f22010-04-29 22:34:07 +0000604 Wal *pRet; /* Object to allocate and return */
dan7c246102010-04-12 19:00:29 +0000605 int flags; /* Flags passed to OsOpen() */
danef378022010-05-04 11:06:03 +0000606 char *zWal; /* Path to WAL file */
dan7c246102010-04-12 19:00:29 +0000607 int nWal; /* Length of zWal in bytes */
608
dan7c246102010-04-12 19:00:29 +0000609 assert( zDb );
dan87bfb512010-04-30 11:43:28 +0000610 if( pVfs->xShmOpen==0 ) return SQLITE_CANTOPEN_BKPT;
dan7c246102010-04-12 19:00:29 +0000611
drh7ed91f22010-04-29 22:34:07 +0000612 /* Allocate an instance of struct Wal to return. */
613 *ppWal = 0;
614 nWal = strlen(zDb);
drh2d536e12010-05-01 20:17:30 +0000615 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal+5);
dan76ed3bc2010-05-03 17:18:24 +0000616 if( !pRet ){
617 return SQLITE_NOMEM;
618 }
619
dan7c246102010-04-12 19:00:29 +0000620 pRet->pVfs = pVfs;
621 pRet->pFd = (sqlite3_file *)&pRet[1];
drh2d536e12010-05-01 20:17:30 +0000622 pRet->zName = zWal = pVfs->szOsFile + (char*)pRet->pFd;
623 sqlite3_snprintf(nWal+5, zWal, "%s-wal", zDb);
drh7ed91f22010-04-29 22:34:07 +0000624 rc = pVfs->xShmOpen(pVfs, zWal, &pRet->pWIndex);
dan7c246102010-04-12 19:00:29 +0000625
drh7ed91f22010-04-29 22:34:07 +0000626 /* Open file handle on the write-ahead log file. */
dan76ed3bc2010-05-03 17:18:24 +0000627 if( rc==SQLITE_OK ){
628 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL);
629 rc = sqlite3OsOpen(pVfs, zWal, pRet->pFd, flags, &flags);
630 }
dan7c246102010-04-12 19:00:29 +0000631
dan7c246102010-04-12 19:00:29 +0000632 if( rc!=SQLITE_OK ){
dan1018e902010-05-05 15:33:05 +0000633 walIndexClose(pRet, 0);
danef378022010-05-04 11:06:03 +0000634 sqlite3OsClose(pRet->pFd);
635 sqlite3_free(pRet);
636 }else{
637 *ppWal = pRet;
dan7c246102010-04-12 19:00:29 +0000638 }
dan7c246102010-04-12 19:00:29 +0000639 return rc;
640}
641
drh7ed91f22010-04-29 22:34:07 +0000642static int walIteratorNext(
643 WalIterator *p, /* Iterator */
644 u32 *piPage, /* OUT: Next db page to write */
645 u32 *piFrame /* OUT: Wal frame to read from */
dan7c246102010-04-12 19:00:29 +0000646){
647 u32 iMin = *piPage;
648 u32 iRet = 0xFFFFFFFF;
649 int i;
650 int nBlock = p->nFinal;
651
652 for(i=p->nSegment-1; i>=0; i--){
drh7ed91f22010-04-29 22:34:07 +0000653 struct WalSegment *pSegment = &p->aSegment[i];
dan7c246102010-04-12 19:00:29 +0000654 while( pSegment->iNext<nBlock ){
655 u32 iPg = pSegment->aDbPage[pSegment->aIndex[pSegment->iNext]];
656 if( iPg>iMin ){
657 if( iPg<iRet ){
658 iRet = iPg;
659 *piFrame = i*256 + 1 + pSegment->aIndex[pSegment->iNext];
660 }
661 break;
662 }
663 pSegment->iNext++;
664 }
665
666 nBlock = 256;
667 }
668
669 *piPage = iRet;
670 return (iRet==0xFFFFFFFF);
671}
672
dan8f6097c2010-05-06 07:43:58 +0000673static int walIteratorInit(Wal *pWal, WalIterator **pp){
drh7ed91f22010-04-29 22:34:07 +0000674 u32 *aData; /* Content of the wal-index file */
675 WalIterator *p; /* Return value */
dan7c246102010-04-12 19:00:29 +0000676 int nSegment; /* Number of segments to merge */
677 u32 iLast; /* Last frame in log */
678 int nByte; /* Number of bytes to allocate */
679 int i; /* Iterator variable */
680 int nFinal; /* Number of unindexed entries */
drh7ed91f22010-04-29 22:34:07 +0000681 struct WalSegment *pFinal; /* Final (unindexed) segment */
dan7c246102010-04-12 19:00:29 +0000682 u8 *aTmp; /* Temp space used by merge-sort */
dan8f6097c2010-05-06 07:43:58 +0000683 int rc; /* Return code of walIndexMap() */
dan7c246102010-04-12 19:00:29 +0000684
dan8f6097c2010-05-06 07:43:58 +0000685 rc = walIndexMap(pWal, -1);
686 if( rc!=SQLITE_OK ){
687 return rc;
688 }
drh7ed91f22010-04-29 22:34:07 +0000689 aData = pWal->pWiData;
690 iLast = pWal->hdr.iLastPg;
dan7c246102010-04-12 19:00:29 +0000691 nSegment = (iLast >> 8) + 1;
692 nFinal = (iLast & 0x000000FF);
693
drh7ed91f22010-04-29 22:34:07 +0000694 nByte = sizeof(WalIterator) + (nSegment-1)*sizeof(struct WalSegment) + 512;
695 p = (WalIterator *)sqlite3_malloc(nByte);
dan8f6097c2010-05-06 07:43:58 +0000696 if( !p ){
697 return SQLITE_NOMEM;
698 }
dan76ed3bc2010-05-03 17:18:24 +0000699
dan7c246102010-04-12 19:00:29 +0000700 if( p ){
701 memset(p, 0, nByte);
702 p->nSegment = nSegment;
dan76ed3bc2010-05-03 17:18:24 +0000703
704 for(i=0; i<nSegment-1; i++){
705 p->aSegment[i].aDbPage = &aData[walIndexEntry(i*256+1)];
706 p->aSegment[i].aIndex = (u8 *)&aData[walIndexEntry(i*256+1)+256];
707 }
708 pFinal = &p->aSegment[nSegment-1];
709
710 pFinal->aDbPage = &aData[walIndexEntry((nSegment-1)*256+1)];
711 pFinal->aIndex = (u8 *)&pFinal[1];
712 aTmp = &pFinal->aIndex[256];
713 for(i=0; i<nFinal; i++){
714 pFinal->aIndex[i] = i;
715 }
716 walMergesort8(pFinal->aDbPage, aTmp, pFinal->aIndex, &nFinal);
dan7c246102010-04-12 19:00:29 +0000717 p->nFinal = nFinal;
718 }
719
dan8f6097c2010-05-06 07:43:58 +0000720 *pp = p;
721 return SQLITE_OK;
dan7c246102010-04-12 19:00:29 +0000722}
723
724/*
drh7ed91f22010-04-29 22:34:07 +0000725** Free a log iterator allocated by walIteratorInit().
dan7c246102010-04-12 19:00:29 +0000726*/
drh7ed91f22010-04-29 22:34:07 +0000727static void walIteratorFree(WalIterator *p){
dan7c246102010-04-12 19:00:29 +0000728 sqlite3_free(p);
729}
730
731/*
732** Checkpoint the contents of the log file.
733*/
drh7ed91f22010-04-29 22:34:07 +0000734static int walCheckpoint(
735 Wal *pWal, /* Wal connection */
dan7c246102010-04-12 19:00:29 +0000736 sqlite3_file *pFd, /* File descriptor open on db file */
danc5118782010-04-17 17:34:41 +0000737 int sync_flags, /* Flags for OsSync() (or 0) */
danb6e099a2010-05-04 14:47:39 +0000738 int nBuf, /* Size of zBuf in bytes */
dan7c246102010-04-12 19:00:29 +0000739 u8 *zBuf /* Temporary buffer to use */
740){
741 int rc; /* Return code */
drh7ed91f22010-04-29 22:34:07 +0000742 int pgsz = pWal->hdr.pgsz; /* Database page-size */
743 WalIterator *pIter = 0; /* Wal iterator context */
dan7c246102010-04-12 19:00:29 +0000744 u32 iDbpage = 0; /* Next database page to write */
drh7ed91f22010-04-29 22:34:07 +0000745 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
dan7c246102010-04-12 19:00:29 +0000746
747 /* Allocate the iterator */
dan8f6097c2010-05-06 07:43:58 +0000748 rc = walIteratorInit(pWal, &pIter);
749 if( rc!=SQLITE_OK || pWal->hdr.iLastPg==0 ){
danb6e099a2010-05-04 14:47:39 +0000750 goto out;
751 }
752
753 if( pWal->hdr.pgsz!=nBuf ){
754 rc = SQLITE_CORRUPT_BKPT;
755 goto out;
756 }
757
dan7c246102010-04-12 19:00:29 +0000758 /* Sync the log file to disk */
danc5118782010-04-17 17:34:41 +0000759 if( sync_flags ){
drh7ed91f22010-04-29 22:34:07 +0000760 rc = sqlite3OsSync(pWal->pFd, sync_flags);
danc5118782010-04-17 17:34:41 +0000761 if( rc!=SQLITE_OK ) goto out;
762 }
dan7c246102010-04-12 19:00:29 +0000763
764 /* Iterate through the contents of the log, copying data to the db file. */
drh7ed91f22010-04-29 22:34:07 +0000765 while( 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
766 rc = sqlite3OsRead(pWal->pFd, zBuf, pgsz,
767 walFrameOffset(iFrame, pgsz) + WAL_FRAME_HDRSIZE
dan7c246102010-04-12 19:00:29 +0000768 );
769 if( rc!=SQLITE_OK ) goto out;
770 rc = sqlite3OsWrite(pFd, zBuf, pgsz, (iDbpage-1)*pgsz);
771 if( rc!=SQLITE_OK ) goto out;
772 }
773
774 /* Truncate the database file */
drh7ed91f22010-04-29 22:34:07 +0000775 rc = sqlite3OsTruncate(pFd, ((i64)pWal->hdr.nPage*(i64)pgsz));
dan7c246102010-04-12 19:00:29 +0000776 if( rc!=SQLITE_OK ) goto out;
777
drh7ed91f22010-04-29 22:34:07 +0000778 /* Sync the database file. If successful, update the wal-index. */
danc5118782010-04-17 17:34:41 +0000779 if( sync_flags ){
780 rc = sqlite3OsSync(pFd, sync_flags);
781 if( rc!=SQLITE_OK ) goto out;
782 }
drh7ed91f22010-04-29 22:34:07 +0000783 pWal->hdr.iLastPg = 0;
784 pWal->hdr.iCheck1 = 2;
785 pWal->hdr.iCheck2 = 3;
786 walIndexWriteHdr(pWal, &pWal->hdr);
dan7c246102010-04-12 19:00:29 +0000787
788 /* TODO: If a crash occurs and the current log is copied into the
789 ** database there is no problem. However, if a crash occurs while
790 ** writing the next transaction into the start of the log, such that:
791 **
792 ** * The first transaction currently in the log is left intact, but
793 ** * The second (or subsequent) transaction is damaged,
794 **
795 ** then the database could become corrupt.
796 **
797 ** The easiest thing to do would be to write and sync a dummy header
798 ** into the log at this point. Unfortunately, that turns out to be
799 ** an unwelcome performance hit. Alternatives are...
800 */
801#if 0
drh7ed91f22010-04-29 22:34:07 +0000802 memset(zBuf, 0, WAL_FRAME_HDRSIZE);
803 rc = sqlite3OsWrite(pWal->pFd, zBuf, WAL_FRAME_HDRSIZE, 0);
dan7c246102010-04-12 19:00:29 +0000804 if( rc!=SQLITE_OK ) goto out;
drh7ed91f22010-04-29 22:34:07 +0000805 rc = sqlite3OsSync(pWal->pFd, pWal->sync_flags);
dan7c246102010-04-12 19:00:29 +0000806#endif
807
808 out:
drh7ed91f22010-04-29 22:34:07 +0000809 walIteratorFree(pIter);
dan7c246102010-04-12 19:00:29 +0000810 return rc;
811}
812
813/*
814** Close a connection to a log file.
815*/
drhc438efd2010-04-26 00:19:45 +0000816int sqlite3WalClose(
drh7ed91f22010-04-29 22:34:07 +0000817 Wal *pWal, /* Wal to close */
dan7c246102010-04-12 19:00:29 +0000818 sqlite3_file *pFd, /* Database file */
danc5118782010-04-17 17:34:41 +0000819 int sync_flags, /* Flags to pass to OsSync() (or 0) */
danb6e099a2010-05-04 14:47:39 +0000820 int nBuf,
821 u8 *zBuf /* Buffer of at least nBuf bytes */
dan7c246102010-04-12 19:00:29 +0000822){
823 int rc = SQLITE_OK;
drh7ed91f22010-04-29 22:34:07 +0000824 if( pWal ){
dan30c86292010-04-30 16:24:46 +0000825 int isDelete = 0; /* True to unlink wal and wal-index files */
826
827 /* If an EXCLUSIVE lock can be obtained on the database file (using the
828 ** ordinary, rollback-mode locking methods, this guarantees that the
829 ** connection associated with this log file is the only connection to
830 ** the database. In this case checkpoint the database and unlink both
831 ** the wal and wal-index files.
832 **
833 ** The EXCLUSIVE lock is not released before returning.
834 */
835 rc = sqlite3OsLock(pFd, SQLITE_LOCK_EXCLUSIVE);
836 if( rc==SQLITE_OK ){
danb6e099a2010-05-04 14:47:39 +0000837 rc = walCheckpoint(pWal, pFd, sync_flags, nBuf, zBuf);
dan30c86292010-04-30 16:24:46 +0000838 if( rc==SQLITE_OK ){
839 isDelete = 1;
840 }
841 walIndexUnmap(pWal);
842 }
843
dan1018e902010-05-05 15:33:05 +0000844 walIndexClose(pWal, isDelete);
drh7ed91f22010-04-29 22:34:07 +0000845 sqlite3OsClose(pWal->pFd);
dan30c86292010-04-30 16:24:46 +0000846 if( isDelete ){
drh2d536e12010-05-01 20:17:30 +0000847 sqlite3OsDelete(pWal->pVfs, pWal->zName, 0);
dan30c86292010-04-30 16:24:46 +0000848 }
drh7ed91f22010-04-29 22:34:07 +0000849 sqlite3_free(pWal);
dan7c246102010-04-12 19:00:29 +0000850 }
851 return rc;
852}
853
854/*
drh7ed91f22010-04-29 22:34:07 +0000855** Try to read the wal-index header. Attempt to verify the header
856** checksum. If the checksum can be verified, copy the wal-index
857** header into structure pWal->hdr. If the contents of pWal->hdr are
danb9bf16b2010-04-14 11:23:30 +0000858** modified by this and pChanged is not NULL, set *pChanged to 1.
859** Otherwise leave *pChanged unmodified.
860**
861** If the checksum cannot be verified return SQLITE_ERROR.
862*/
danc7991bd2010-05-05 19:04:59 +0000863int walIndexTryHdr(Wal *pWal, int *pisValid, int *pChanged){
danb9bf16b2010-04-14 11:23:30 +0000864 u32 aCksum[2] = {1, 1};
drh7ed91f22010-04-29 22:34:07 +0000865 u32 aHdr[WALINDEX_HDR_NFIELD+2];
danb9bf16b2010-04-14 11:23:30 +0000866
drh79e6c782010-04-30 02:13:26 +0000867 if( pWal->szWIndex==0 ){
drh5530b762010-04-30 14:39:50 +0000868 int rc;
869 rc = walIndexRemap(pWal, WALINDEX_MMAP_INCREMENT);
drh79e6c782010-04-30 02:13:26 +0000870 if( rc ) return rc;
871 }
872
drh7ed91f22010-04-29 22:34:07 +0000873 /* Read the header. The caller may or may not have locked the wal-index
dancd11fb22010-04-26 10:40:52 +0000874 ** file, meaning it is possible that an inconsistent snapshot is read
875 ** from the file. If this happens, return SQLITE_ERROR. The caller will
876 ** retry. Or, if the caller has already locked the file and the header
877 ** still looks inconsistent, it will run recovery.
drh79e6c782010-04-30 02:13:26 +0000878 **
879 ** FIX-ME: It is no longer possible to have not locked the wal-index.
danb9bf16b2010-04-14 11:23:30 +0000880 */
drh7ed91f22010-04-29 22:34:07 +0000881 memcpy(aHdr, pWal->pWiData, sizeof(aHdr));
882 walChecksumBytes((u8*)aHdr, sizeof(u32)*WALINDEX_HDR_NFIELD, aCksum);
883 if( aCksum[0]!=aHdr[WALINDEX_HDR_NFIELD]
884 || aCksum[1]!=aHdr[WALINDEX_HDR_NFIELD+1]
danb9bf16b2010-04-14 11:23:30 +0000885 ){
danc7991bd2010-05-05 19:04:59 +0000886 return SQLITE_OK;
danb9bf16b2010-04-14 11:23:30 +0000887 }
danc7991bd2010-05-05 19:04:59 +0000888 *pisValid = 1;
danb9bf16b2010-04-14 11:23:30 +0000889
drh7ed91f22010-04-29 22:34:07 +0000890 if( memcmp(&pWal->hdr, aHdr, sizeof(WalIndexHdr)) ){
danb9bf16b2010-04-14 11:23:30 +0000891 if( pChanged ){
892 *pChanged = 1;
893 }
drh7ed91f22010-04-29 22:34:07 +0000894 memcpy(&pWal->hdr, aHdr, sizeof(WalIndexHdr));
danb9bf16b2010-04-14 11:23:30 +0000895 }
896 return SQLITE_OK;
897}
898
899/*
drh7ed91f22010-04-29 22:34:07 +0000900** Read the wal-index header from the wal-index file into structure
901** pWal->hdr. If attempting to verify the header checksum fails, try
danb9bf16b2010-04-14 11:23:30 +0000902** to recover the log before returning.
903**
drh7ed91f22010-04-29 22:34:07 +0000904** If the wal-index header is successfully read, return SQLITE_OK.
danb9bf16b2010-04-14 11:23:30 +0000905** Otherwise an SQLite error code.
906*/
drh7ed91f22010-04-29 22:34:07 +0000907static int walIndexReadHdr(Wal *pWal, int *pChanged){
danb9bf16b2010-04-14 11:23:30 +0000908 int rc;
danc7991bd2010-05-05 19:04:59 +0000909 int isValid = 0;
danb9bf16b2010-04-14 11:23:30 +0000910
dan4c97b532010-04-30 09:52:17 +0000911 assert( pWal->lockState>=SQLITE_SHM_READ );
danc7991bd2010-05-05 19:04:59 +0000912 rc = walIndexMap(pWal, -1);
913 if( rc!=SQLITE_OK ){
914 return rc;
915 }
drh7ed91f22010-04-29 22:34:07 +0000916
danb9bf16b2010-04-14 11:23:30 +0000917 /* First try to read the header without a lock. Verify the checksum
918 ** before returning. This will almost always work.
919 */
danc7991bd2010-05-05 19:04:59 +0000920 rc = walIndexTryHdr(pWal, &isValid, pChanged);
921 if( isValid || rc!=SQLITE_OK ){
922 return rc;
danb9bf16b2010-04-14 11:23:30 +0000923 }
924
drh7ed91f22010-04-29 22:34:07 +0000925 /* If the first attempt to read the header failed, lock the wal-index
danb9bf16b2010-04-14 11:23:30 +0000926 ** file and try again. If the header checksum verification fails this
927 ** time as well, run log recovery.
928 */
drh7ed91f22010-04-29 22:34:07 +0000929 if( SQLITE_OK==(rc = walSetLock(pWal, SQLITE_SHM_RECOVER)) ){
danc7991bd2010-05-05 19:04:59 +0000930 rc = walIndexTryHdr(pWal, &isValid, pChanged);
931 if( rc==SQLITE_OK && isValid==0 ){
danb9bf16b2010-04-14 11:23:30 +0000932 if( pChanged ){
933 *pChanged = 1;
934 }
drh7ed91f22010-04-29 22:34:07 +0000935 rc = walIndexRecover(pWal);
danb9bf16b2010-04-14 11:23:30 +0000936 if( rc==SQLITE_OK ){
danc7991bd2010-05-05 19:04:59 +0000937 rc = walIndexTryHdr(pWal, &isValid, 0);
danb9bf16b2010-04-14 11:23:30 +0000938 }
939 }
drh7ed91f22010-04-29 22:34:07 +0000940 walSetLock(pWal, SQLITE_SHM_READ);
danb9bf16b2010-04-14 11:23:30 +0000941 }
942
danc7991bd2010-05-05 19:04:59 +0000943 if( rc==SQLITE_OK && isValid==0 ){
944 rc = SQLITE_ERROR;
945 }
danb9bf16b2010-04-14 11:23:30 +0000946 return rc;
947}
948
949/*
dan64d039e2010-04-13 19:27:31 +0000950** Lock a snapshot.
dan7c246102010-04-12 19:00:29 +0000951**
952** If this call obtains a new read-lock and the database contents have been
drh7ed91f22010-04-29 22:34:07 +0000953** modified since the most recent call to WalCloseSnapshot() on this Wal
dan7c246102010-04-12 19:00:29 +0000954** connection, then *pChanged is set to 1 before returning. Otherwise, it
955** is left unmodified. This is used by the pager layer to determine whether
956** or not any cached pages may be safely reused.
957*/
drh7ed91f22010-04-29 22:34:07 +0000958int sqlite3WalOpenSnapshot(Wal *pWal, int *pChanged){
dan8d6ad1c2010-05-04 10:36:20 +0000959 int rc; /* Return code */
dan64d039e2010-04-13 19:27:31 +0000960
drh7ed91f22010-04-29 22:34:07 +0000961 rc = walSetLock(pWal, SQLITE_SHM_READ);
dan8d6ad1c2010-05-04 10:36:20 +0000962 assert( rc!=SQLITE_OK || pWal->lockState==SQLITE_SHM_READ );
dan64d039e2010-04-13 19:27:31 +0000963
dan8d6ad1c2010-05-04 10:36:20 +0000964 if( rc==SQLITE_OK ){
drh7ed91f22010-04-29 22:34:07 +0000965 rc = walIndexReadHdr(pWal, pChanged);
dan64d039e2010-04-13 19:27:31 +0000966 if( rc!=SQLITE_OK ){
967 /* An error occured while attempting log recovery. */
drh7ed91f22010-04-29 22:34:07 +0000968 sqlite3WalCloseSnapshot(pWal);
dan31f98fc2010-04-27 05:42:32 +0000969 }else{
970 /* Check if the mapping needs to grow. */
drh5530b762010-04-30 14:39:50 +0000971 if( pWal->hdr.iLastPg
danfe05aa12010-04-30 17:05:23 +0000972 && walIndexEntry(pWal->hdr.iLastPg)*sizeof(u32)>=pWal->szWIndex
drh5530b762010-04-30 14:39:50 +0000973 ){
974 walIndexRemap(pWal, -1);
dan31f98fc2010-04-27 05:42:32 +0000975 }
dan64d039e2010-04-13 19:27:31 +0000976 }
dan7c246102010-04-12 19:00:29 +0000977 }
danba515902010-04-30 09:32:06 +0000978
979 walIndexUnmap(pWal);
dan7c246102010-04-12 19:00:29 +0000980 return rc;
981}
982
983/*
984** Unlock the current snapshot.
985*/
drh7ed91f22010-04-29 22:34:07 +0000986void sqlite3WalCloseSnapshot(Wal *pWal){
dan8d6ad1c2010-05-04 10:36:20 +0000987 assert( pWal->lockState==SQLITE_SHM_READ
988 || pWal->lockState==SQLITE_SHM_UNLOCK
989 );
990 walSetLock(pWal, SQLITE_SHM_UNLOCK);
dan7c246102010-04-12 19:00:29 +0000991}
992
dan5e0ce872010-04-28 17:48:44 +0000993/*
dan7c246102010-04-12 19:00:29 +0000994** Read a page from the log, if it is present.
995*/
danb6e099a2010-05-04 14:47:39 +0000996int sqlite3WalRead(
997 Wal *pWal,
998 Pgno pgno,
999 int *pInWal,
1000 int nOut,
1001 u8 *pOut
1002){
danc7991bd2010-05-05 19:04:59 +00001003 int rc; /* Return code */
dan7c246102010-04-12 19:00:29 +00001004 u32 iRead = 0;
dancd11fb22010-04-26 10:40:52 +00001005 u32 *aData;
drh7ed91f22010-04-29 22:34:07 +00001006 int iFrame = (pWal->hdr.iLastPg & 0xFFFFFF00);
dan7c246102010-04-12 19:00:29 +00001007
dan1bc61712010-04-30 10:24:54 +00001008 assert( pWal->lockState==SQLITE_SHM_READ||pWal->lockState==SQLITE_SHM_WRITE );
danc7991bd2010-05-05 19:04:59 +00001009 rc = walIndexMap(pWal, -1);
1010 if( rc!=SQLITE_OK ){
1011 return rc;
1012 }
dancd11fb22010-04-26 10:40:52 +00001013
dan7c246102010-04-12 19:00:29 +00001014 /* Do a linear search of the unindexed block of page-numbers (if any)
drh7ed91f22010-04-29 22:34:07 +00001015 ** at the end of the wal-index. An alternative to this would be to
dan7c246102010-04-12 19:00:29 +00001016 ** build an index in private memory each time a read transaction is
1017 ** opened on a new snapshot.
1018 */
drh7ed91f22010-04-29 22:34:07 +00001019 aData = pWal->pWiData;
1020 if( pWal->hdr.iLastPg ){
1021 u32 *pi = &aData[walIndexEntry(pWal->hdr.iLastPg)];
1022 u32 *piStop = pi - (pWal->hdr.iLastPg & 0xFF);
dan7c246102010-04-12 19:00:29 +00001023 while( *pi!=pgno && pi!=piStop ) pi--;
1024 if( pi!=piStop ){
1025 iRead = (pi-piStop) + iFrame;
1026 }
1027 }
drh7ed91f22010-04-29 22:34:07 +00001028 assert( iRead==0 || aData[walIndexEntry(iRead)]==pgno );
dan7c246102010-04-12 19:00:29 +00001029
1030 while( iRead==0 && iFrame>0 ){
1031 int iLow = 0;
1032 int iHigh = 255;
1033 u32 *aFrame;
1034 u8 *aIndex;
1035
1036 iFrame -= 256;
drh7ed91f22010-04-29 22:34:07 +00001037 aFrame = &aData[walIndexEntry(iFrame+1)];
dan7c246102010-04-12 19:00:29 +00001038 aIndex = (u8 *)&aFrame[256];
1039
1040 while( iLow<=iHigh ){
1041 int iTest = (iLow+iHigh)>>1;
1042 u32 iPg = aFrame[aIndex[iTest]];
1043
1044 if( iPg==pgno ){
1045 iRead = iFrame + 1 + aIndex[iTest];
1046 break;
1047 }
1048 else if( iPg<pgno ){
1049 iLow = iTest+1;
1050 }else{
1051 iHigh = iTest-1;
1052 }
1053 }
1054 }
drh7ed91f22010-04-29 22:34:07 +00001055 assert( iRead==0 || aData[walIndexEntry(iRead)]==pgno );
1056 walIndexUnmap(pWal);
dancd11fb22010-04-26 10:40:52 +00001057
dan7c246102010-04-12 19:00:29 +00001058 /* If iRead is non-zero, then it is the log frame number that contains the
1059 ** required page. Read and return data from the log file.
1060 */
1061 if( iRead ){
drh7ed91f22010-04-29 22:34:07 +00001062 i64 iOffset = walFrameOffset(iRead, pWal->hdr.pgsz) + WAL_FRAME_HDRSIZE;
1063 *pInWal = 1;
danb6e099a2010-05-04 14:47:39 +00001064 return sqlite3OsRead(pWal->pFd, pOut, nOut, iOffset);
dan7c246102010-04-12 19:00:29 +00001065 }
1066
drh7ed91f22010-04-29 22:34:07 +00001067 *pInWal = 0;
dan7c246102010-04-12 19:00:29 +00001068 return SQLITE_OK;
1069}
1070
1071
1072/*
1073** Set *pPgno to the size of the database file (or zero, if unknown).
1074*/
drh7ed91f22010-04-29 22:34:07 +00001075void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){
1076 assert( pWal->lockState==SQLITE_SHM_READ
1077 || pWal->lockState==SQLITE_SHM_WRITE );
1078 *pPgno = pWal->hdr.nPage;
dan7c246102010-04-12 19:00:29 +00001079}
1080
1081/*
dan7c246102010-04-12 19:00:29 +00001082** This function returns SQLITE_OK if the caller may write to the database.
1083** Otherwise, if the caller is operating on a snapshot that has already
dan49320f82010-04-14 18:50:08 +00001084** been overwritten by another writer, SQLITE_BUSY is returned.
dan7c246102010-04-12 19:00:29 +00001085*/
drh7ed91f22010-04-29 22:34:07 +00001086int sqlite3WalWriteLock(Wal *pWal, int op){
1087 int rc;
dan7c246102010-04-12 19:00:29 +00001088 if( op ){
drh7ed91f22010-04-29 22:34:07 +00001089 assert( pWal->lockState == SQLITE_SHM_READ );
1090 rc = walSetLock(pWal, SQLITE_SHM_WRITE);
dan30c86292010-04-30 16:24:46 +00001091
1092 /* If this connection is not reading the most recent database snapshot,
1093 ** it is not possible to write to the database. In this case release
1094 ** the write locks and return SQLITE_BUSY.
1095 */
1096 if( rc==SQLITE_OK ){
1097 rc = walIndexMap(pWal, -1);
1098 if( rc==SQLITE_OK
1099 && memcmp(&pWal->hdr, pWal->pWiData, sizeof(WalIndexHdr))
1100 ){
1101 rc = SQLITE_BUSY;
1102 }
1103 walIndexUnmap(pWal);
1104 if( rc!=SQLITE_OK ){
1105 walSetLock(pWal, SQLITE_SHM_READ);
1106 }
1107 }
drh7ed91f22010-04-29 22:34:07 +00001108 }else if( pWal->lockState==SQLITE_SHM_WRITE ){
1109 rc = walSetLock(pWal, SQLITE_SHM_READ);
dan7c246102010-04-12 19:00:29 +00001110 }
drh7ed91f22010-04-29 22:34:07 +00001111 return rc;
dan7c246102010-04-12 19:00:29 +00001112}
1113
dan74d6cd82010-04-24 18:44:05 +00001114/*
dan74d6cd82010-04-24 18:44:05 +00001115** If any data has been written (but not committed) to the log file, this
1116** function moves the write-pointer back to the start of the transaction.
1117**
1118** Additionally, the callback function is invoked for each frame written
1119** to the log since the start of the transaction. If the callback returns
1120** other than SQLITE_OK, it is not invoked again and the error code is
1121** returned to the caller.
1122**
1123** Otherwise, if the callback function does not return an error, this
1124** function returns SQLITE_OK.
1125*/
drh7ed91f22010-04-29 22:34:07 +00001126int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
dan74d6cd82010-04-24 18:44:05 +00001127 int rc = SQLITE_OK;
drh7ed91f22010-04-29 22:34:07 +00001128 Pgno iMax = pWal->hdr.iLastPg;
dan74d6cd82010-04-24 18:44:05 +00001129 Pgno iFrame;
1130
danc7991bd2010-05-05 19:04:59 +00001131 rc = walIndexReadHdr(pWal, 0);
drh7ed91f22010-04-29 22:34:07 +00001132 for(iFrame=pWal->hdr.iLastPg+1; iFrame<=iMax && rc==SQLITE_OK; iFrame++){
drhcd058ec2010-05-04 17:20:09 +00001133 assert( pWal->lockState==SQLITE_SHM_WRITE );
drh7ed91f22010-04-29 22:34:07 +00001134 rc = xUndo(pUndoCtx, pWal->pWiData[walIndexEntry(iFrame)]);
dan74d6cd82010-04-24 18:44:05 +00001135 }
drh7ed91f22010-04-29 22:34:07 +00001136 walIndexUnmap(pWal);
dan74d6cd82010-04-24 18:44:05 +00001137 return rc;
1138}
1139
drh7ed91f22010-04-29 22:34:07 +00001140/* Return an integer that records the current (uncommitted) write
1141** position in the WAL
1142*/
1143u32 sqlite3WalSavepoint(Wal *pWal){
1144 assert( pWal->lockState==SQLITE_SHM_WRITE );
1145 return pWal->hdr.iLastPg;
dan4cd78b42010-04-26 16:57:10 +00001146}
1147
drh7ed91f22010-04-29 22:34:07 +00001148/* Move the write position of the WAL back to iFrame. Called in
1149** response to a ROLLBACK TO command.
1150*/
1151int sqlite3WalSavepointUndo(Wal *pWal, u32 iFrame){
dan4cd78b42010-04-26 16:57:10 +00001152 int rc = SQLITE_OK;
1153 u8 aCksum[8];
drh7ed91f22010-04-29 22:34:07 +00001154 assert( pWal->lockState==SQLITE_SHM_WRITE );
dan4cd78b42010-04-26 16:57:10 +00001155
drh7ed91f22010-04-29 22:34:07 +00001156 pWal->hdr.iLastPg = iFrame;
dan4cd78b42010-04-26 16:57:10 +00001157 if( iFrame>0 ){
drh7ed91f22010-04-29 22:34:07 +00001158 i64 iOffset = walFrameOffset(iFrame, pWal->hdr.pgsz) + sizeof(u32)*2;
1159 rc = sqlite3OsRead(pWal->pFd, aCksum, sizeof(aCksum), iOffset);
1160 pWal->hdr.iCheck1 = sqlite3Get4byte(&aCksum[0]);
1161 pWal->hdr.iCheck2 = sqlite3Get4byte(&aCksum[4]);
dan4cd78b42010-04-26 16:57:10 +00001162 }
1163
1164 return rc;
1165}
1166
dan7c246102010-04-12 19:00:29 +00001167/*
dan4cd78b42010-04-26 16:57:10 +00001168** Write a set of frames to the log. The caller must hold the write-lock
1169** on the log file (obtained using sqlite3WalWriteLock()).
dan7c246102010-04-12 19:00:29 +00001170*/
drhc438efd2010-04-26 00:19:45 +00001171int sqlite3WalFrames(
drh7ed91f22010-04-29 22:34:07 +00001172 Wal *pWal, /* Wal handle to write to */
dan7c246102010-04-12 19:00:29 +00001173 int nPgsz, /* Database page-size in bytes */
1174 PgHdr *pList, /* List of dirty pages to write */
1175 Pgno nTruncate, /* Database size after this commit */
1176 int isCommit, /* True if this is a commit */
danc5118782010-04-17 17:34:41 +00001177 int sync_flags /* Flags to pass to OsSync() (or 0) */
dan7c246102010-04-12 19:00:29 +00001178){
dan7c246102010-04-12 19:00:29 +00001179 int rc; /* Used to catch return codes */
1180 u32 iFrame; /* Next frame address */
drh7ed91f22010-04-29 22:34:07 +00001181 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
dan7c246102010-04-12 19:00:29 +00001182 PgHdr *p; /* Iterator to run through pList with. */
dan97a31352010-04-16 13:59:31 +00001183 u32 aCksum[2]; /* Checksums */
dan7c246102010-04-12 19:00:29 +00001184 PgHdr *pLast; /* Last frame in list */
1185 int nLast = 0; /* Number of extra copies of last page */
1186
drh7ed91f22010-04-29 22:34:07 +00001187 assert( WAL_FRAME_HDRSIZE==(4 * 2 + 2*sizeof(u32)) );
dan7c246102010-04-12 19:00:29 +00001188 assert( pList );
drh7ed91f22010-04-29 22:34:07 +00001189 assert( pWal->lockState==SQLITE_SHM_WRITE );
danba515902010-04-30 09:32:06 +00001190 assert( pWal->pWiData==0 );
dan7c246102010-04-12 19:00:29 +00001191
dan97a31352010-04-16 13:59:31 +00001192 /* If this is the first frame written into the log, write the log
1193 ** header to the start of the log file. See comments at the top of
1194 ** this file for a description of the log-header format.
1195 */
drh7ed91f22010-04-29 22:34:07 +00001196 assert( WAL_FRAME_HDRSIZE>=WAL_HDRSIZE );
1197 iFrame = pWal->hdr.iLastPg;
dan97a31352010-04-16 13:59:31 +00001198 if( iFrame==0 ){
1199 sqlite3Put4byte(aFrame, nPgsz);
1200 sqlite3_randomness(8, &aFrame[4]);
drh7ed91f22010-04-29 22:34:07 +00001201 pWal->hdr.iCheck1 = sqlite3Get4byte(&aFrame[4]);
1202 pWal->hdr.iCheck2 = sqlite3Get4byte(&aFrame[8]);
1203 rc = sqlite3OsWrite(pWal->pFd, aFrame, WAL_HDRSIZE, 0);
dan97a31352010-04-16 13:59:31 +00001204 if( rc!=SQLITE_OK ){
1205 return rc;
1206 }
1207 }
1208
drh7ed91f22010-04-29 22:34:07 +00001209 aCksum[0] = pWal->hdr.iCheck1;
1210 aCksum[1] = pWal->hdr.iCheck2;
dan7c246102010-04-12 19:00:29 +00001211
1212 /* Write the log file. */
dan7c246102010-04-12 19:00:29 +00001213 for(p=pList; p; p=p->pDirty){
1214 u32 nDbsize; /* Db-size field for frame header */
1215 i64 iOffset; /* Write offset in log file */
1216
drh7ed91f22010-04-29 22:34:07 +00001217 iOffset = walFrameOffset(++iFrame, nPgsz);
dan7c246102010-04-12 19:00:29 +00001218
1219 /* Populate and write the frame header */
1220 nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
drh7ed91f22010-04-29 22:34:07 +00001221 walEncodeFrame(aCksum, p->pgno, nDbsize, nPgsz, p->pData, aFrame);
1222 rc = sqlite3OsWrite(pWal->pFd, aFrame, sizeof(aFrame), iOffset);
dan7c246102010-04-12 19:00:29 +00001223 if( rc!=SQLITE_OK ){
1224 return rc;
1225 }
1226
1227 /* Write the page data */
drh7ed91f22010-04-29 22:34:07 +00001228 rc = sqlite3OsWrite(pWal->pFd, p->pData, nPgsz, iOffset + sizeof(aFrame));
dan7c246102010-04-12 19:00:29 +00001229 if( rc!=SQLITE_OK ){
1230 return rc;
1231 }
1232 pLast = p;
1233 }
1234
1235 /* Sync the log file if the 'isSync' flag was specified. */
danc5118782010-04-17 17:34:41 +00001236 if( sync_flags ){
drh7ed91f22010-04-29 22:34:07 +00001237 i64 iSegment = sqlite3OsSectorSize(pWal->pFd);
1238 i64 iOffset = walFrameOffset(iFrame+1, nPgsz);
dan67032392010-04-17 15:42:43 +00001239
1240 assert( isCommit );
dan7c246102010-04-12 19:00:29 +00001241
1242 if( iSegment<SQLITE_DEFAULT_SECTOR_SIZE ){
1243 iSegment = SQLITE_DEFAULT_SECTOR_SIZE;
1244 }
1245 iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
1246 while( iOffset<iSegment ){
drh7ed91f22010-04-29 22:34:07 +00001247 walEncodeFrame(aCksum,pLast->pgno,nTruncate,nPgsz,pLast->pData,aFrame);
1248 rc = sqlite3OsWrite(pWal->pFd, aFrame, sizeof(aFrame), iOffset);
dan7c246102010-04-12 19:00:29 +00001249 if( rc!=SQLITE_OK ){
1250 return rc;
1251 }
1252
drh7ed91f22010-04-29 22:34:07 +00001253 iOffset += WAL_FRAME_HDRSIZE;
1254 rc = sqlite3OsWrite(pWal->pFd, pLast->pData, nPgsz, iOffset);
dan7c246102010-04-12 19:00:29 +00001255 if( rc!=SQLITE_OK ){
1256 return rc;
1257 }
1258 nLast++;
1259 iOffset += nPgsz;
1260 }
dan7c246102010-04-12 19:00:29 +00001261
drh7ed91f22010-04-29 22:34:07 +00001262 rc = sqlite3OsSync(pWal->pFd, sync_flags);
dan7c246102010-04-12 19:00:29 +00001263 }
danba515902010-04-30 09:32:06 +00001264 assert( pWal->pWiData==0 );
dan7c246102010-04-12 19:00:29 +00001265
1266 /* Append data to the log summary. It is not necessary to lock the
drh7ed91f22010-04-29 22:34:07 +00001267 ** wal-index to do this as the RESERVED lock held on the db file
dan7c246102010-04-12 19:00:29 +00001268 ** guarantees that there are no other writers, and no data that may
1269 ** be in use by existing readers is being overwritten.
1270 */
drh7ed91f22010-04-29 22:34:07 +00001271 iFrame = pWal->hdr.iLastPg;
danc7991bd2010-05-05 19:04:59 +00001272 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
dan7c246102010-04-12 19:00:29 +00001273 iFrame++;
danc7991bd2010-05-05 19:04:59 +00001274 rc = walIndexAppend(pWal, iFrame, p->pgno);
dan7c246102010-04-12 19:00:29 +00001275 }
danc7991bd2010-05-05 19:04:59 +00001276 while( nLast>0 && rc==SQLITE_OK ){
dan7c246102010-04-12 19:00:29 +00001277 iFrame++;
1278 nLast--;
danc7991bd2010-05-05 19:04:59 +00001279 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
dan7c246102010-04-12 19:00:29 +00001280 }
1281
danc7991bd2010-05-05 19:04:59 +00001282 if( rc==SQLITE_OK ){
1283 /* Update the private copy of the header. */
1284 pWal->hdr.pgsz = nPgsz;
1285 pWal->hdr.iLastPg = iFrame;
1286 if( isCommit ){
1287 pWal->hdr.iChange++;
1288 pWal->hdr.nPage = nTruncate;
1289 }
1290 pWal->hdr.iCheck1 = aCksum[0];
1291 pWal->hdr.iCheck2 = aCksum[1];
dan7c246102010-04-12 19:00:29 +00001292
danc7991bd2010-05-05 19:04:59 +00001293 /* If this is a commit, update the wal-index header too. */
1294 if( isCommit ){
1295 walIndexWriteHdr(pWal, &pWal->hdr);
1296 pWal->iCallback = iFrame;
1297 }
dan7c246102010-04-12 19:00:29 +00001298 }
danc7991bd2010-05-05 19:04:59 +00001299
drh7ed91f22010-04-29 22:34:07 +00001300 walIndexUnmap(pWal);
dan8d22a172010-04-19 18:03:51 +00001301 return rc;
dan7c246102010-04-12 19:00:29 +00001302}
1303
1304/*
danb9bf16b2010-04-14 11:23:30 +00001305** Checkpoint the database:
1306**
drh7ed91f22010-04-29 22:34:07 +00001307** 1. Acquire a CHECKPOINT lock
1308** 2. Copy the contents of the log into the database file.
1309** 3. Zero the wal-index header (so new readers will ignore the log).
1310** 4. Drop the CHECKPOINT lock.
dan7c246102010-04-12 19:00:29 +00001311*/
drhc438efd2010-04-26 00:19:45 +00001312int sqlite3WalCheckpoint(
drh7ed91f22010-04-29 22:34:07 +00001313 Wal *pWal, /* Wal connection */
dan7c246102010-04-12 19:00:29 +00001314 sqlite3_file *pFd, /* File descriptor open on db file */
danc5118782010-04-17 17:34:41 +00001315 int sync_flags, /* Flags to sync db file with (or 0) */
danb6e099a2010-05-04 14:47:39 +00001316 int nBuf, /* Size of temporary buffer */
dan64d039e2010-04-13 19:27:31 +00001317 u8 *zBuf, /* Temporary buffer to use */
1318 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
1319 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
dan7c246102010-04-12 19:00:29 +00001320){
danb9bf16b2010-04-14 11:23:30 +00001321 int rc; /* Return code */
dan31c03902010-04-29 14:51:33 +00001322 int isChanged = 0; /* True if a new wal-index header is loaded */
dan7c246102010-04-12 19:00:29 +00001323
drh7ed91f22010-04-29 22:34:07 +00001324 assert( pWal->lockState==SQLITE_SHM_UNLOCK );
dan5cf53532010-05-01 16:40:20 +00001325 assert( pWal->pWiData==0 );
dan39c79f52010-04-15 10:58:51 +00001326
drh7ed91f22010-04-29 22:34:07 +00001327 /* Get the CHECKPOINT lock */
dan64d039e2010-04-13 19:27:31 +00001328 do {
drh7ed91f22010-04-29 22:34:07 +00001329 rc = walSetLock(pWal, SQLITE_SHM_CHECKPOINT);
dan64d039e2010-04-13 19:27:31 +00001330 }while( rc==SQLITE_BUSY && xBusyHandler(pBusyHandlerArg) );
danb9bf16b2010-04-14 11:23:30 +00001331 if( rc!=SQLITE_OK ){
drh7ed91f22010-04-29 22:34:07 +00001332 walSetLock(pWal, SQLITE_SHM_UNLOCK);
danb9bf16b2010-04-14 11:23:30 +00001333 return rc;
1334 }
dan64d039e2010-04-13 19:27:31 +00001335
danb9bf16b2010-04-14 11:23:30 +00001336 /* Copy data from the log to the database file. */
drh7ed91f22010-04-29 22:34:07 +00001337 rc = walIndexReadHdr(pWal, &isChanged);
danb9bf16b2010-04-14 11:23:30 +00001338 if( rc==SQLITE_OK ){
danb6e099a2010-05-04 14:47:39 +00001339 rc = walCheckpoint(pWal, pFd, sync_flags, nBuf, zBuf);
danb9bf16b2010-04-14 11:23:30 +00001340 }
dan31c03902010-04-29 14:51:33 +00001341 if( isChanged ){
1342 /* If a new wal-index header was loaded before the checkpoint was
drh7ed91f22010-04-29 22:34:07 +00001343 ** performed, then the pager-cache associated with log pWal is now
dan31c03902010-04-29 14:51:33 +00001344 ** out of date. So zero the cached wal-index header to ensure that
1345 ** next time the pager opens a snapshot on this database it knows that
1346 ** the cache needs to be reset.
1347 */
drh7ed91f22010-04-29 22:34:07 +00001348 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
dan31c03902010-04-29 14:51:33 +00001349 }
danb9bf16b2010-04-14 11:23:30 +00001350
1351 /* Release the locks. */
dan87bfb512010-04-30 11:43:28 +00001352 walIndexUnmap(pWal);
drh7ed91f22010-04-29 22:34:07 +00001353 walSetLock(pWal, SQLITE_SHM_UNLOCK);
dan64d039e2010-04-13 19:27:31 +00001354 return rc;
dan7c246102010-04-12 19:00:29 +00001355}
1356
drh7ed91f22010-04-29 22:34:07 +00001357/* Return the value to pass to a sqlite3_wal_hook callback, the
1358** number of frames in the WAL at the point of the last commit since
1359** sqlite3WalCallback() was called. If no commits have occurred since
1360** the last call, then return 0.
1361*/
1362int sqlite3WalCallback(Wal *pWal){
dan8d22a172010-04-19 18:03:51 +00001363 u32 ret = 0;
drh7ed91f22010-04-29 22:34:07 +00001364 if( pWal ){
1365 ret = pWal->iCallback;
1366 pWal->iCallback = 0;
dan8d22a172010-04-19 18:03:51 +00001367 }
1368 return (int)ret;
1369}
dan5cf53532010-05-01 16:40:20 +00001370#endif /* #ifndef SQLITE_OMIT_WAL */