dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2 | ** 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 |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 14 | ** "journal_mode=wal" mode. |
| 15 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 16 | #include "wal.h" |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 17 | |
dan | 4b64c1e | 2010-04-27 18:49:54 +0000 | [diff] [blame] | 18 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 19 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 20 | ** WRITE-AHEAD LOG (WAL) FILE FORMAT |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 21 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 22 | ** A wal file consists of a header followed by zero or more "frames". |
| 23 | ** The header is 12 bytes in size and consists of the following three |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 24 | ** big-endian 32-bit unsigned integer values: |
| 25 | ** |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 26 | ** 0: Database page size, |
| 27 | ** 4: Randomly selected salt value 1, |
| 28 | ** 8: Randomly selected salt value 2. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 29 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 30 | ** Immediately following the header are zero or more frames. Each |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 31 | ** frame itself consists of a 16-byte header followed by a <page-size> bytes |
| 32 | ** of page data. The header is broken into 4 big-endian 32-bit unsigned |
| 33 | ** integer values, as follows: |
| 34 | ** |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 35 | ** 0: Page number. |
| 36 | ** 4: For commit records, the size of the database image in pages |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 37 | ** after the commit. For all other records, zero. |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 38 | ** 8: Checksum value 1. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 39 | ** 12: Checksum value 2. |
| 40 | */ |
| 41 | |
| 42 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 43 | ** WAL-INDEX FILE FORMAT |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 44 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 45 | ** The wal-index file consists of a 32-byte header region, followed by an |
| 46 | ** 8-byte region that contains no useful data (used to apply byte-range locks |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 47 | ** to), followed by the data region. |
| 48 | ** |
| 49 | ** The contents of both the header and data region are specified in terms |
| 50 | ** of 1, 2 and 4 byte unsigned integers. All integers are stored in |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 51 | ** machine-endian order. The wal-index is not a persistent file and |
| 52 | ** so it does not need to be portable across archtectures. |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 53 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 54 | ** A wal-index file is essentially a shadow-pager map. It contains a |
| 55 | ** mapping from database page number to the set of locations in the wal |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 56 | ** file that contain versions of the database page. When a database |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 57 | ** client needs to read a page of data, it first queries the wal-index |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 58 | ** file to determine if the required version of the page is stored in |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 59 | ** the wal. If so, the page is read from the wal. If not, the page is |
| 60 | ** read from the database file. |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 61 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 62 | ** Whenever a transaction is appended to the wal or a checkpoint transfers |
| 63 | ** data from the wal into the database file, the wal-index is |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 64 | ** updated accordingly. |
| 65 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 66 | ** The fields in the wal-index file header are described in the comment |
| 67 | ** directly above the definition of struct WalIndexHdr (see below). |
| 68 | ** Immediately following the fields in the WalIndexHdr structure is |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 69 | ** an 8 byte checksum based on the contents of the header. This field is |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 70 | ** not the same as the iCheck1 and iCheck2 fields of the WalIndexHdr. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 71 | */ |
| 72 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 73 | /* Object declarations */ |
| 74 | typedef struct WalIndexHdr WalIndexHdr; |
| 75 | typedef struct WalIterator WalIterator; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 79 | ** The following object stores a copy of the wal-index header. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 80 | ** |
| 81 | ** Member variables iCheck1 and iCheck2 contain the checksum for the |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 82 | ** last frame written to the wal, or 2 and 3 respectively if the log |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 83 | ** is currently empty. |
| 84 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 85 | struct WalIndexHdr { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 86 | u32 iChange; /* Counter incremented each transaction */ |
| 87 | u32 pgsz; /* Database page size in bytes */ |
| 88 | u32 iLastPg; /* Address of last valid frame in log */ |
| 89 | u32 nPage; /* Size of database in pages */ |
| 90 | u32 iCheck1; /* Checkpoint value 1 */ |
| 91 | u32 iCheck2; /* Checkpoint value 2 */ |
| 92 | }; |
| 93 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 94 | /* Size of serialized WalIndexHdr object. */ |
| 95 | #define WALINDEX_HDR_NFIELD (sizeof(WalIndexHdr) / sizeof(u32)) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 96 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 97 | /* A block of 16 bytes beginning at WALINDEX_LOCK_OFFSET is reserved |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 98 | ** for locks. Since some systems only feature mandatory file-locks, we |
| 99 | ** do not read or write data from the region of the file on which locks |
| 100 | ** are applied. |
| 101 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 102 | #define WALINDEX_LOCK_OFFSET ((sizeof(WalIndexHdr))+2*sizeof(u32)) |
| 103 | #define WALINDEX_LOCK_RESERVED 8 |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 104 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 105 | /* Size of header before each frame in wal */ |
| 106 | #define WAL_FRAME_HDRSIZE 16 |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 107 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 108 | /* Size of write ahead log header */ |
| 109 | #define WAL_HDRSIZE 12 |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 112 | ** Return the offset of frame iFrame in the write-ahead log file, |
| 113 | ** assuming a database page size of pgsz bytes. The offset returned |
| 114 | ** is to the start of the write-ahead log frame-header. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 115 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 116 | #define walFrameOffset(iFrame, pgsz) ( \ |
| 117 | WAL_HDRSIZE + ((iFrame)-1)*((pgsz)+WAL_FRAME_HDRSIZE) \ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 118 | ) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 121 | ** An open write-ahead log file is represented by an instance of the |
| 122 | ** following object. |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 123 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 124 | struct Wal { |
| 125 | sqlite3_vfs *pVfs; /* The VFS used to create pFd */ |
| 126 | sqlite3_file *pFd; /* File handle for WAL file */ |
| 127 | u32 iCallback; /* Value to pass to log callback (or 0) */ |
| 128 | sqlite3_shm *pWIndex; /* The open wal-index file */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 129 | int szWIndex; /* Size of the wal-index that is mapped in mem */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 130 | u32 *pWiData; /* Pointer to wal-index content in memory */ |
| 131 | u8 lockState; /* SQLITE_SHM_xxxx constant showing lock state */ |
| 132 | u8 readerType; /* SQLITE_SHM_READ or SQLITE_SHM_READ_FULL */ |
| 133 | WalIndexHdr hdr; /* Wal-index for current snapshot */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 136 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 137 | /* |
| 138 | ** This structure is used to implement an iterator that iterates through |
| 139 | ** all frames in the log in database page order. Where two or more frames |
| 140 | ** correspond to the same database page, the iterator visits only the |
| 141 | ** frame most recently written to the log. |
| 142 | ** |
| 143 | ** The internals of this structure are only accessed by: |
| 144 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 145 | ** walIteratorInit() - Create a new iterator, |
| 146 | ** walIteratorNext() - Step an iterator, |
| 147 | ** walIteratorFree() - Free an iterator. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 148 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 149 | ** This functionality is used by the checkpoint code (see walCheckpoint()). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 150 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 151 | struct WalIterator { |
| 152 | int nSegment; /* Size of WalIterator.aSegment[] array */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 153 | int nFinal; /* Elements in segment nSegment-1 */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 154 | struct WalSegment { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 155 | int iNext; /* Next aIndex index */ |
| 156 | u8 *aIndex; /* Pointer to index array */ |
| 157 | u32 *aDbPage; /* Pointer to db page array */ |
| 158 | } aSegment[1]; |
| 159 | }; |
| 160 | |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 161 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 162 | /* |
| 163 | ** Generate an 8 byte checksum based on the data in array aByte[] and the |
| 164 | ** initial values of aCksum[0] and aCksum[1]. The checksum is written into |
| 165 | ** aCksum[] before returning. |
dan | 56d9591 | 2010-04-24 19:07:29 +0000 | [diff] [blame] | 166 | ** |
| 167 | ** The range of bytes to checksum is treated as an array of 32-bit |
| 168 | ** little-endian unsigned integers. For each integer X in the array, from |
| 169 | ** start to finish, do the following: |
| 170 | ** |
| 171 | ** aCksum[0] += X; |
| 172 | ** aCksum[1] += aCksum[0]; |
| 173 | ** |
| 174 | ** For the calculation above, use 64-bit unsigned accumulators. Before |
| 175 | ** returning, truncate the values to 32-bits as follows: |
| 176 | ** |
| 177 | ** aCksum[0] = (u32)(aCksum[0] + (aCksum[0]>>24)); |
| 178 | ** aCksum[1] = (u32)(aCksum[1] + (aCksum[1]>>24)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 179 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 180 | static void walChecksumBytes(u8 *aByte, int nByte, u32 *aCksum){ |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 181 | u64 sum1 = aCksum[0]; |
| 182 | u64 sum2 = aCksum[1]; |
| 183 | u32 *a32 = (u32 *)aByte; |
| 184 | u32 *aEnd = (u32 *)&aByte[nByte]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 185 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 186 | assert( (nByte&0x00000003)==0 ); |
| 187 | |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 188 | if( SQLITE_LITTLEENDIAN ){ |
| 189 | #ifdef SQLITE_DEBUG |
| 190 | u8 *a = (u8 *)a32; |
| 191 | assert( *a32==(a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24)) ); |
| 192 | #endif |
| 193 | do { |
| 194 | sum1 += *a32; |
| 195 | sum2 += sum1; |
| 196 | } while( ++a32<aEnd ); |
| 197 | }else{ |
| 198 | do { |
| 199 | u8 *a = (u8*)a32; |
| 200 | sum1 += a[0] + (a[1]<<8) + (a[2]<<16) + (a[3]<<24); |
| 201 | sum2 += sum1; |
| 202 | } while( ++a32<aEnd ); |
| 203 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 204 | |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 205 | aCksum[0] = sum1 + (sum1>>24); |
| 206 | aCksum[1] = sum2 + (sum2>>24); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 210 | ** Attempt to change the lock status. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 211 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 212 | ** When changing the lock status to SQLITE_SHM_READ, store the |
| 213 | ** type of reader lock (either SQLITE_SHM_READ or SQLITE_SHM_READ_FULL) |
| 214 | ** in pWal->readerType. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 215 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 216 | static int walSetLock(Wal *pWal, int desiredStatus){ |
| 217 | int rc, got; |
| 218 | if( pWal->lockState==desiredStatus ) return SQLITE_OK; |
| 219 | rc = pWal->pVfs->xShmLock(pWal->pWIndex, desiredStatus, &got); |
drh | 49156b2 | 2010-04-30 16:12:04 +0000 | [diff] [blame] | 220 | pWal->lockState = got; |
| 221 | if( got==SQLITE_SHM_READ_FULL || got==SQLITE_SHM_READ ){ |
| 222 | pWal->readerType = got; |
| 223 | pWal->lockState = SQLITE_SHM_READ; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 224 | } |
| 225 | return rc; |
| 226 | } |
| 227 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 228 | /* |
| 229 | ** Update the header of the wal-index file. |
| 230 | */ |
| 231 | static void walIndexWriteHdr(Wal *pWal, WalIndexHdr *pHdr){ |
| 232 | u32 *aHdr = pWal->pWiData; /* Write header here */ |
| 233 | u32 *aCksum = &aHdr[WALINDEX_HDR_NFIELD]; /* Write header cksum here */ |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 234 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 235 | assert( WALINDEX_HDR_NFIELD==sizeof(WalIndexHdr)/4 ); |
| 236 | assert( aHdr!=0 ); |
| 237 | memcpy(aHdr, pHdr, sizeof(WalIndexHdr)); |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 238 | aCksum[0] = aCksum[1] = 1; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 239 | walChecksumBytes((u8 *)aHdr, sizeof(WalIndexHdr), aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | ** This function encodes a single frame header and writes it to a buffer |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 244 | ** supplied by the caller. A frame-header is made up of a series of |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 245 | ** 4-byte big-endian integers, as follows: |
| 246 | ** |
| 247 | ** 0: Database page size in bytes. |
| 248 | ** 4: Page number. |
| 249 | ** 8: New database size (for commit frames, otherwise zero). |
| 250 | ** 12: Frame checksum 1. |
| 251 | ** 16: Frame checksum 2. |
| 252 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 253 | static void walEncodeFrame( |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 254 | u32 *aCksum, /* IN/OUT: Checksum values */ |
| 255 | u32 iPage, /* Database page number for frame */ |
| 256 | u32 nTruncate, /* New db size (or 0 for non-commit frames) */ |
| 257 | int nData, /* Database page size (size of aData[]) */ |
| 258 | u8 *aData, /* Pointer to page data (for checksum) */ |
| 259 | u8 *aFrame /* OUT: Write encoded frame here */ |
| 260 | ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 261 | assert( WAL_FRAME_HDRSIZE==16 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 262 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 263 | sqlite3Put4byte(&aFrame[0], iPage); |
| 264 | sqlite3Put4byte(&aFrame[4], nTruncate); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 265 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 266 | walChecksumBytes(aFrame, 8, aCksum); |
| 267 | walChecksumBytes(aData, nData, aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 268 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 269 | sqlite3Put4byte(&aFrame[8], aCksum[0]); |
| 270 | sqlite3Put4byte(&aFrame[12], aCksum[1]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | ** Return 1 and populate *piPage, *pnTruncate and aCksum if the |
| 275 | ** frame checksum looks Ok. Otherwise return 0. |
| 276 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 277 | static int walDecodeFrame( |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 278 | u32 *aCksum, /* IN/OUT: Checksum values */ |
| 279 | u32 *piPage, /* OUT: Database page number for frame */ |
| 280 | u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */ |
| 281 | int nData, /* Database page size (size of aData[]) */ |
| 282 | u8 *aData, /* Pointer to page data (for checksum) */ |
| 283 | u8 *aFrame /* Frame data */ |
| 284 | ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 285 | assert( WAL_FRAME_HDRSIZE==16 ); |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 286 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 287 | walChecksumBytes(aFrame, 8, aCksum); |
| 288 | walChecksumBytes(aData, nData, aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 289 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 290 | if( aCksum[0]!=sqlite3Get4byte(&aFrame[8]) |
| 291 | || aCksum[1]!=sqlite3Get4byte(&aFrame[12]) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 292 | ){ |
| 293 | /* Checksum failed. */ |
| 294 | return 0; |
| 295 | } |
| 296 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 297 | *piPage = sqlite3Get4byte(&aFrame[0]); |
| 298 | *pnTruncate = sqlite3Get4byte(&aFrame[4]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 299 | return 1; |
| 300 | } |
| 301 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 302 | static void walMergesort8( |
| 303 | Pgno *aContent, /* Pages in wal */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 304 | u8 *aBuffer, /* Buffer of at least *pnList items to use */ |
| 305 | u8 *aList, /* IN/OUT: List to sort */ |
| 306 | int *pnList /* IN/OUT: Number of elements in aList[] */ |
| 307 | ){ |
| 308 | int nList = *pnList; |
| 309 | if( nList>1 ){ |
| 310 | int nLeft = nList / 2; /* Elements in left list */ |
| 311 | int nRight = nList - nLeft; /* Elements in right list */ |
| 312 | u8 *aLeft = aList; /* Left list */ |
| 313 | u8 *aRight = &aList[nLeft]; /* Right list */ |
| 314 | int iLeft = 0; /* Current index in aLeft */ |
| 315 | int iRight = 0; /* Current index in aright */ |
| 316 | int iOut = 0; /* Current index in output buffer */ |
| 317 | |
| 318 | /* TODO: Change to non-recursive version. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 319 | walMergesort8(aContent, aBuffer, aLeft, &nLeft); |
| 320 | walMergesort8(aContent, aBuffer, aRight, &nRight); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 321 | |
| 322 | while( iRight<nRight || iLeft<nLeft ){ |
| 323 | u8 logpage; |
| 324 | Pgno dbpage; |
| 325 | |
| 326 | if( (iLeft<nLeft) |
| 327 | && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]]) |
| 328 | ){ |
| 329 | logpage = aLeft[iLeft++]; |
| 330 | }else{ |
| 331 | logpage = aRight[iRight++]; |
| 332 | } |
| 333 | dbpage = aContent[logpage]; |
| 334 | |
| 335 | aBuffer[iOut++] = logpage; |
| 336 | if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++; |
| 337 | |
| 338 | assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage ); |
| 339 | assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage ); |
| 340 | } |
| 341 | memcpy(aList, aBuffer, sizeof(aList[0])*iOut); |
| 342 | *pnList = iOut; |
| 343 | } |
| 344 | |
| 345 | #ifdef SQLITE_DEBUG |
| 346 | { |
| 347 | int i; |
| 348 | for(i=1; i<*pnList; i++){ |
| 349 | assert( aContent[aList[i]] > aContent[aList[i-1]] ); |
| 350 | } |
| 351 | } |
| 352 | #endif |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 357 | ** Return the index in the WalIndex.aData array that corresponds to |
| 358 | ** frame iFrame. The wal-index file consists of a header, followed by |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 359 | ** alternating "map" and "index" blocks. |
| 360 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 361 | static int walIndexEntry(u32 iFrame){ |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 362 | return ( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 363 | (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)/sizeof(u32) |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 364 | + (((iFrame-1)>>8)<<6) /* Indexes that occur before iFrame */ |
| 365 | + iFrame-1 /* Db page numbers that occur before iFrame */ |
| 366 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 367 | } |
| 368 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 369 | /* |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 370 | ** Release our reference to the wal-index memory map, if we are holding |
| 371 | ** it. |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 372 | */ |
| 373 | static void walIndexUnmap(Wal *pWal){ |
| 374 | if( pWal->pWiData ){ |
| 375 | pWal->pVfs->xShmRelease(pWal->pWIndex); |
| 376 | pWal->pWiData = 0; |
| 377 | } |
| 378 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 379 | |
| 380 | /* |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 381 | ** Map the wal-index file into memory if it isn't already. |
| 382 | ** |
| 383 | ** The reqSize parameter is the minimum required size of the mapping. |
| 384 | ** A value of -1 means "don't care". The reqSize parameter is ignored |
| 385 | ** if the mapping is already held. |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 386 | */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 387 | static int walIndexMap(Wal *pWal, int reqSize){ |
| 388 | int rc = SQLITE_OK; |
| 389 | if( pWal->pWiData==0 ){ |
| 390 | rc = pWal->pVfs->xShmGet(pWal->pWIndex, reqSize, &pWal->szWIndex, |
| 391 | (void**)(char*)&pWal->pWiData); |
| 392 | if( rc==SQLITE_OK && pWal->pWiData==0 ){ |
| 393 | /* Make sure pWal->pWiData is not NULL while we are holding the |
| 394 | ** lock on the mapping. */ |
| 395 | assert( pWal->szWIndex==0 ); |
| 396 | pWal->pWiData = &pWal->iCallback; |
| 397 | } |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 398 | } |
| 399 | return rc; |
| 400 | } |
| 401 | |
| 402 | /* |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 403 | ** Remap the wal-index so that the mapping covers the full size |
| 404 | ** of the underlying file. |
| 405 | ** |
| 406 | ** If enlargeTo is non-negative, then increase the size of the underlying |
| 407 | ** storage to be at least as big as enlargeTo before remapping. |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 408 | */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 409 | static int walIndexRemap(Wal *pWal, int enlargeTo){ |
| 410 | int rc; |
| 411 | int sz; |
| 412 | rc = pWal->pVfs->xShmSize(pWal->pWIndex, enlargeTo, &sz); |
| 413 | if( rc==SQLITE_OK && sz>pWal->szWIndex ){ |
| 414 | walIndexUnmap(pWal); |
| 415 | rc = walIndexMap(pWal, sz); |
| 416 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 417 | return rc; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | ** Increment by which to increase the wal-index file size. |
| 422 | */ |
| 423 | #define WALINDEX_MMAP_INCREMENT (64*1024) |
| 424 | |
| 425 | /* |
| 426 | ** Set an entry in the wal-index map to map log frame iFrame to db |
| 427 | ** page iPage. Values are always appended to the wal-index (i.e. the |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 428 | ** value of iFrame is always exactly one more than the value passed to |
| 429 | ** the previous call), but that restriction is not enforced or asserted |
| 430 | ** here. |
| 431 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 432 | static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ |
| 433 | u32 iSlot = walIndexEntry(iFrame); |
| 434 | |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 435 | walIndexMap(pWal, -1); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 436 | while( (iSlot+128)>=pWal->szWIndex ){ |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 437 | int rc; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 438 | int nByte = pWal->szWIndex*4 + WALINDEX_MMAP_INCREMENT; |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 439 | |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 440 | /* Enlarge the storage, then remap it. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 441 | rc = walIndexRemap(pWal, nByte); |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 442 | if( rc!=SQLITE_OK ){ |
| 443 | return rc; |
| 444 | } |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 445 | } |
| 446 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 447 | /* Set the wal-index entry itself */ |
| 448 | pWal->pWiData[iSlot] = iPage; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 449 | |
| 450 | /* If the frame number is a multiple of 256 (frames are numbered starting |
| 451 | ** at 1), build an index of the most recently added 256 frames. |
| 452 | */ |
| 453 | if( (iFrame&0x000000FF)==0 ){ |
| 454 | int i; /* Iterator used while initializing aIndex */ |
| 455 | u32 *aFrame; /* Pointer to array of 256 frames */ |
| 456 | int nIndex; /* Number of entries in index */ |
| 457 | u8 *aIndex; /* 256 bytes to build index in */ |
| 458 | u8 *aTmp; /* Scratch space to use while sorting */ |
| 459 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 460 | aFrame = &pWal->pWiData[iSlot-255]; |
| 461 | aIndex = (u8 *)&pWal->pWiData[iSlot+1]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 462 | aTmp = &aIndex[256]; |
| 463 | |
| 464 | nIndex = 256; |
| 465 | for(i=0; i<256; i++) aIndex[i] = (u8)i; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 466 | walMergesort8(aFrame, aTmp, aIndex, &nIndex); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 467 | memset(&aIndex[nIndex], aIndex[nIndex-1], 256-nIndex); |
| 468 | } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 469 | |
| 470 | return SQLITE_OK; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | |
| 474 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 475 | ** Recover the wal-index by reading the write-ahead log file. |
| 476 | ** The caller must hold RECOVER lock on the wal-index file. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 477 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 478 | static int walIndexRecover(Wal *pWal){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 479 | int rc; /* Return Code */ |
| 480 | i64 nSize; /* Size of log file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 481 | WalIndexHdr hdr; /* Recovered wal-index header */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 482 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 483 | assert( pWal->lockState==SQLITE_SHM_RECOVER ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 484 | memset(&hdr, 0, sizeof(hdr)); |
| 485 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 486 | rc = sqlite3OsFileSize(pWal->pFd, &nSize); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 487 | if( rc!=SQLITE_OK ){ |
| 488 | return rc; |
| 489 | } |
| 490 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 491 | if( nSize>WAL_FRAME_HDRSIZE ){ |
| 492 | u8 aBuf[WAL_FRAME_HDRSIZE]; /* Buffer to load first frame header into */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 493 | u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ |
| 494 | int nFrame; /* Number of bytes at aFrame */ |
| 495 | u8 *aData; /* Pointer to data part of aFrame buffer */ |
| 496 | int iFrame; /* Index of last frame read */ |
| 497 | i64 iOffset; /* Next offset to read from log file */ |
| 498 | int nPgsz; /* Page size according to the log */ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 499 | u32 aCksum[2]; /* Running checksum */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 500 | |
| 501 | /* Read in the first frame header in the file (to determine the |
| 502 | ** database page size). |
| 503 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 504 | rc = sqlite3OsRead(pWal->pFd, aBuf, WAL_HDRSIZE, 0); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 505 | if( rc!=SQLITE_OK ){ |
| 506 | return rc; |
| 507 | } |
| 508 | |
| 509 | /* If the database page size is not a power of two, or is greater than |
| 510 | ** SQLITE_MAX_PAGE_SIZE, conclude that the log file contains no valid data. |
| 511 | */ |
| 512 | nPgsz = sqlite3Get4byte(&aBuf[0]); |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 513 | if( nPgsz&(nPgsz-1) || nPgsz>SQLITE_MAX_PAGE_SIZE || nPgsz<512 ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 514 | goto finished; |
| 515 | } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 516 | aCksum[0] = sqlite3Get4byte(&aBuf[4]); |
| 517 | aCksum[1] = sqlite3Get4byte(&aBuf[8]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 518 | |
| 519 | /* Malloc a buffer to read frames into. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 520 | nFrame = nPgsz + WAL_FRAME_HDRSIZE; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 521 | aFrame = (u8 *)sqlite3_malloc(nFrame); |
| 522 | if( !aFrame ){ |
| 523 | return SQLITE_NOMEM; |
| 524 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 525 | aData = &aFrame[WAL_FRAME_HDRSIZE]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 526 | |
| 527 | /* Read all frames from the log file. */ |
| 528 | iFrame = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 529 | for(iOffset=WAL_HDRSIZE; (iOffset+nFrame)<=nSize; iOffset+=nFrame){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 530 | u32 pgno; /* Database page number for frame */ |
| 531 | u32 nTruncate; /* dbsize field from frame header */ |
| 532 | int isValid; /* True if this frame is valid */ |
| 533 | |
| 534 | /* Read and decode the next log frame. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 535 | rc = sqlite3OsRead(pWal->pFd, aFrame, nFrame, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 536 | if( rc!=SQLITE_OK ) break; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 537 | isValid = walDecodeFrame(aCksum, &pgno, &nTruncate, nPgsz, aData, aFrame); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 538 | if( !isValid ) break; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 539 | walIndexAppend(pWal, ++iFrame, pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 540 | |
| 541 | /* If nTruncate is non-zero, this is a commit record. */ |
| 542 | if( nTruncate ){ |
| 543 | hdr.iCheck1 = aCksum[0]; |
| 544 | hdr.iCheck2 = aCksum[1]; |
| 545 | hdr.iLastPg = iFrame; |
| 546 | hdr.nPage = nTruncate; |
| 547 | hdr.pgsz = nPgsz; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | sqlite3_free(aFrame); |
| 552 | }else{ |
| 553 | hdr.iCheck1 = 2; |
| 554 | hdr.iCheck2 = 3; |
| 555 | } |
| 556 | |
| 557 | finished: |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 558 | walIndexWriteHdr(pWal, &hdr); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 559 | return rc; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | ** Open a connection to the log file associated with database zDb. The |
| 564 | ** database file does not actually have to exist. zDb is used only to |
| 565 | ** figure out the name of the log file to open. If the log file does not |
| 566 | ** exist it is created by this call. |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 567 | ** |
| 568 | ** A SHARED lock should be held on the database file when this function |
| 569 | ** is called. The purpose of this SHARED lock is to prevent any other |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 570 | ** client from unlinking the log or wal-index file. If another process |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 571 | ** were to do this just after this client opened one of these files, the |
| 572 | ** system would be badly broken. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 573 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 574 | int sqlite3WalOpen( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 575 | sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 576 | const char *zDb, /* Name of database file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 577 | Wal **ppWal /* OUT: Allocated Wal handle */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 578 | ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 579 | int rc = SQLITE_OK; /* Return Code */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 580 | Wal *pRet; /* Object to allocate and return */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 581 | int flags; /* Flags passed to OsOpen() */ |
| 582 | char *zWal = 0; /* Path to WAL file */ |
| 583 | int nWal; /* Length of zWal in bytes */ |
| 584 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 585 | assert( zDb ); |
dan | 87bfb51 | 2010-04-30 11:43:28 +0000 | [diff] [blame] | 586 | if( pVfs->xShmOpen==0 ) return SQLITE_CANTOPEN_BKPT; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 587 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 588 | /* Allocate an instance of struct Wal to return. */ |
| 589 | *ppWal = 0; |
| 590 | nWal = strlen(zDb); |
| 591 | pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal+11); |
| 592 | if( !pRet ) goto wal_open_out; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 593 | pRet->pVfs = pVfs; |
| 594 | pRet->pFd = (sqlite3_file *)&pRet[1]; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 595 | zWal = pVfs->szOsFile + (char*)pRet->pFd; |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 596 | sqlite3_snprintf(nWal+11, zWal, "%s-wal-index", zDb); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 597 | rc = pVfs->xShmOpen(pVfs, zWal, &pRet->pWIndex); |
| 598 | if( rc ) goto wal_open_out; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 599 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 600 | /* Open file handle on the write-ahead log file. */ |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 601 | zWal[nWal+4] = 0; |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 602 | flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 603 | rc = sqlite3OsOpen(pVfs, zWal, pRet->pFd, flags, &flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 604 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 605 | wal_open_out: |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 606 | if( rc!=SQLITE_OK ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 607 | if( pRet ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 608 | pVfs->xShmClose(pRet->pWIndex); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 609 | sqlite3OsClose(pRet->pFd); |
| 610 | sqlite3_free(pRet); |
| 611 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 612 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 613 | *ppWal = pRet; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 614 | return rc; |
| 615 | } |
| 616 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 617 | static int walIteratorNext( |
| 618 | WalIterator *p, /* Iterator */ |
| 619 | u32 *piPage, /* OUT: Next db page to write */ |
| 620 | u32 *piFrame /* OUT: Wal frame to read from */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 621 | ){ |
| 622 | u32 iMin = *piPage; |
| 623 | u32 iRet = 0xFFFFFFFF; |
| 624 | int i; |
| 625 | int nBlock = p->nFinal; |
| 626 | |
| 627 | for(i=p->nSegment-1; i>=0; i--){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 628 | struct WalSegment *pSegment = &p->aSegment[i]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 629 | while( pSegment->iNext<nBlock ){ |
| 630 | u32 iPg = pSegment->aDbPage[pSegment->aIndex[pSegment->iNext]]; |
| 631 | if( iPg>iMin ){ |
| 632 | if( iPg<iRet ){ |
| 633 | iRet = iPg; |
| 634 | *piFrame = i*256 + 1 + pSegment->aIndex[pSegment->iNext]; |
| 635 | } |
| 636 | break; |
| 637 | } |
| 638 | pSegment->iNext++; |
| 639 | } |
| 640 | |
| 641 | nBlock = 256; |
| 642 | } |
| 643 | |
| 644 | *piPage = iRet; |
| 645 | return (iRet==0xFFFFFFFF); |
| 646 | } |
| 647 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 648 | static WalIterator *walIteratorInit(Wal *pWal){ |
| 649 | u32 *aData; /* Content of the wal-index file */ |
| 650 | WalIterator *p; /* Return value */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 651 | int nSegment; /* Number of segments to merge */ |
| 652 | u32 iLast; /* Last frame in log */ |
| 653 | int nByte; /* Number of bytes to allocate */ |
| 654 | int i; /* Iterator variable */ |
| 655 | int nFinal; /* Number of unindexed entries */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 656 | struct WalSegment *pFinal; /* Final (unindexed) segment */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 657 | u8 *aTmp; /* Temp space used by merge-sort */ |
| 658 | |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 659 | walIndexMap(pWal, -1); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 660 | aData = pWal->pWiData; |
| 661 | iLast = pWal->hdr.iLastPg; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 662 | nSegment = (iLast >> 8) + 1; |
| 663 | nFinal = (iLast & 0x000000FF); |
| 664 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 665 | nByte = sizeof(WalIterator) + (nSegment-1)*sizeof(struct WalSegment) + 512; |
| 666 | p = (WalIterator *)sqlite3_malloc(nByte); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 667 | if( p ){ |
| 668 | memset(p, 0, nByte); |
| 669 | p->nSegment = nSegment; |
| 670 | p->nFinal = nFinal; |
| 671 | } |
| 672 | |
| 673 | for(i=0; i<nSegment-1; i++){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 674 | p->aSegment[i].aDbPage = &aData[walIndexEntry(i*256+1)]; |
| 675 | p->aSegment[i].aIndex = (u8 *)&aData[walIndexEntry(i*256+1)+256]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 676 | } |
| 677 | pFinal = &p->aSegment[nSegment-1]; |
| 678 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 679 | pFinal->aDbPage = &aData[walIndexEntry((nSegment-1)*256+1)]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 680 | pFinal->aIndex = (u8 *)&pFinal[1]; |
| 681 | aTmp = &pFinal->aIndex[256]; |
| 682 | for(i=0; i<nFinal; i++){ |
| 683 | pFinal->aIndex[i] = i; |
| 684 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 685 | walMergesort8(pFinal->aDbPage, aTmp, pFinal->aIndex, &nFinal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 686 | p->nFinal = nFinal; |
| 687 | |
| 688 | return p; |
| 689 | } |
| 690 | |
| 691 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 692 | ** Free a log iterator allocated by walIteratorInit(). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 693 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 694 | static void walIteratorFree(WalIterator *p){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 695 | sqlite3_free(p); |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | ** Checkpoint the contents of the log file. |
| 700 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 701 | static int walCheckpoint( |
| 702 | Wal *pWal, /* Wal connection */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 703 | sqlite3_file *pFd, /* File descriptor open on db file */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 704 | int sync_flags, /* Flags for OsSync() (or 0) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 705 | u8 *zBuf /* Temporary buffer to use */ |
| 706 | ){ |
| 707 | int rc; /* Return code */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 708 | int pgsz = pWal->hdr.pgsz; /* Database page-size */ |
| 709 | WalIterator *pIter = 0; /* Wal iterator context */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 710 | u32 iDbpage = 0; /* Next database page to write */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 711 | u32 iFrame = 0; /* Wal frame containing data for iDbpage */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 712 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 713 | if( pWal->hdr.iLastPg==0 ){ |
dan | bb2e9c9 | 2010-04-15 13:33:18 +0000 | [diff] [blame] | 714 | return SQLITE_OK; |
| 715 | } |
| 716 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 717 | /* Allocate the iterator */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 718 | pIter = walIteratorInit(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 719 | if( !pIter ) return SQLITE_NOMEM; |
| 720 | |
| 721 | /* Sync the log file to disk */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 722 | if( sync_flags ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 723 | rc = sqlite3OsSync(pWal->pFd, sync_flags); |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 724 | if( rc!=SQLITE_OK ) goto out; |
| 725 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 726 | |
| 727 | /* Iterate through the contents of the log, copying data to the db file. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 728 | while( 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ |
| 729 | rc = sqlite3OsRead(pWal->pFd, zBuf, pgsz, |
| 730 | walFrameOffset(iFrame, pgsz) + WAL_FRAME_HDRSIZE |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 731 | ); |
| 732 | if( rc!=SQLITE_OK ) goto out; |
| 733 | rc = sqlite3OsWrite(pFd, zBuf, pgsz, (iDbpage-1)*pgsz); |
| 734 | if( rc!=SQLITE_OK ) goto out; |
| 735 | } |
| 736 | |
| 737 | /* Truncate the database file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 738 | rc = sqlite3OsTruncate(pFd, ((i64)pWal->hdr.nPage*(i64)pgsz)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 739 | if( rc!=SQLITE_OK ) goto out; |
| 740 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 741 | /* Sync the database file. If successful, update the wal-index. */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 742 | if( sync_flags ){ |
| 743 | rc = sqlite3OsSync(pFd, sync_flags); |
| 744 | if( rc!=SQLITE_OK ) goto out; |
| 745 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 746 | pWal->hdr.iLastPg = 0; |
| 747 | pWal->hdr.iCheck1 = 2; |
| 748 | pWal->hdr.iCheck2 = 3; |
| 749 | walIndexWriteHdr(pWal, &pWal->hdr); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 750 | |
| 751 | /* TODO: If a crash occurs and the current log is copied into the |
| 752 | ** database there is no problem. However, if a crash occurs while |
| 753 | ** writing the next transaction into the start of the log, such that: |
| 754 | ** |
| 755 | ** * The first transaction currently in the log is left intact, but |
| 756 | ** * The second (or subsequent) transaction is damaged, |
| 757 | ** |
| 758 | ** then the database could become corrupt. |
| 759 | ** |
| 760 | ** The easiest thing to do would be to write and sync a dummy header |
| 761 | ** into the log at this point. Unfortunately, that turns out to be |
| 762 | ** an unwelcome performance hit. Alternatives are... |
| 763 | */ |
| 764 | #if 0 |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 765 | memset(zBuf, 0, WAL_FRAME_HDRSIZE); |
| 766 | rc = sqlite3OsWrite(pWal->pFd, zBuf, WAL_FRAME_HDRSIZE, 0); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 767 | if( rc!=SQLITE_OK ) goto out; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 768 | rc = sqlite3OsSync(pWal->pFd, pWal->sync_flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 769 | #endif |
| 770 | |
| 771 | out: |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 772 | walIteratorFree(pIter); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 773 | return rc; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | ** Close a connection to a log file. |
| 778 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 779 | int sqlite3WalClose( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 780 | Wal *pWal, /* Wal to close */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 781 | sqlite3_file *pFd, /* Database file */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 782 | int sync_flags, /* Flags to pass to OsSync() (or 0) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 783 | u8 *zBuf /* Buffer of at least page-size bytes */ |
| 784 | ){ |
| 785 | int rc = SQLITE_OK; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 786 | if( pWal ){ |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 787 | int isDelete = 0; /* True to unlink wal and wal-index files */ |
| 788 | |
| 789 | /* If an EXCLUSIVE lock can be obtained on the database file (using the |
| 790 | ** ordinary, rollback-mode locking methods, this guarantees that the |
| 791 | ** connection associated with this log file is the only connection to |
| 792 | ** the database. In this case checkpoint the database and unlink both |
| 793 | ** the wal and wal-index files. |
| 794 | ** |
| 795 | ** The EXCLUSIVE lock is not released before returning. |
| 796 | */ |
| 797 | rc = sqlite3OsLock(pFd, SQLITE_LOCK_EXCLUSIVE); |
| 798 | if( rc==SQLITE_OK ){ |
| 799 | rc = walCheckpoint(pWal, pFd, sync_flags, zBuf); |
| 800 | if( rc==SQLITE_OK ){ |
| 801 | isDelete = 1; |
| 802 | } |
| 803 | walIndexUnmap(pWal); |
| 804 | } |
| 805 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 806 | pWal->pVfs->xShmClose(pWal->pWIndex); |
| 807 | sqlite3OsClose(pWal->pFd); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 808 | if( isDelete ){ |
| 809 | int nWal; |
| 810 | char *zWal = &((char *)pWal->pFd)[pWal->pVfs->szOsFile]; |
| 811 | sqlite3OsDelete(pWal->pVfs, zWal, 0); |
| 812 | nWal = sqlite3Strlen30(zWal); |
| 813 | memcpy(&zWal[nWal], "-index", 7); |
| 814 | pWal->pVfs->xShmDelete(pWal->pVfs, zWal); |
| 815 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 816 | sqlite3_free(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 817 | } |
| 818 | return rc; |
| 819 | } |
| 820 | |
| 821 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 822 | ** Try to read the wal-index header. Attempt to verify the header |
| 823 | ** checksum. If the checksum can be verified, copy the wal-index |
| 824 | ** header into structure pWal->hdr. If the contents of pWal->hdr are |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 825 | ** modified by this and pChanged is not NULL, set *pChanged to 1. |
| 826 | ** Otherwise leave *pChanged unmodified. |
| 827 | ** |
| 828 | ** If the checksum cannot be verified return SQLITE_ERROR. |
| 829 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 830 | int walIndexTryHdr(Wal *pWal, int *pChanged){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 831 | u32 aCksum[2] = {1, 1}; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 832 | u32 aHdr[WALINDEX_HDR_NFIELD+2]; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 833 | |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 834 | if( pWal->szWIndex==0 ){ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 835 | int rc; |
| 836 | rc = walIndexRemap(pWal, WALINDEX_MMAP_INCREMENT); |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 837 | if( rc ) return rc; |
| 838 | } |
| 839 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 840 | /* Read the header. The caller may or may not have locked the wal-index |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 841 | ** file, meaning it is possible that an inconsistent snapshot is read |
| 842 | ** from the file. If this happens, return SQLITE_ERROR. The caller will |
| 843 | ** retry. Or, if the caller has already locked the file and the header |
| 844 | ** still looks inconsistent, it will run recovery. |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 845 | ** |
| 846 | ** FIX-ME: It is no longer possible to have not locked the wal-index. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 847 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 848 | memcpy(aHdr, pWal->pWiData, sizeof(aHdr)); |
| 849 | walChecksumBytes((u8*)aHdr, sizeof(u32)*WALINDEX_HDR_NFIELD, aCksum); |
| 850 | if( aCksum[0]!=aHdr[WALINDEX_HDR_NFIELD] |
| 851 | || aCksum[1]!=aHdr[WALINDEX_HDR_NFIELD+1] |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 852 | ){ |
| 853 | return SQLITE_ERROR; |
| 854 | } |
| 855 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 856 | if( memcmp(&pWal->hdr, aHdr, sizeof(WalIndexHdr)) ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 857 | if( pChanged ){ |
| 858 | *pChanged = 1; |
| 859 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 860 | memcpy(&pWal->hdr, aHdr, sizeof(WalIndexHdr)); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 861 | } |
| 862 | return SQLITE_OK; |
| 863 | } |
| 864 | |
| 865 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 866 | ** Read the wal-index header from the wal-index file into structure |
| 867 | ** pWal->hdr. If attempting to verify the header checksum fails, try |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 868 | ** to recover the log before returning. |
| 869 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 870 | ** If the wal-index header is successfully read, return SQLITE_OK. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 871 | ** Otherwise an SQLite error code. |
| 872 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 873 | static int walIndexReadHdr(Wal *pWal, int *pChanged){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 874 | int rc; |
| 875 | |
dan | 4c97b53 | 2010-04-30 09:52:17 +0000 | [diff] [blame] | 876 | assert( pWal->lockState>=SQLITE_SHM_READ ); |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 877 | walIndexMap(pWal, -1); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 878 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 879 | /* First try to read the header without a lock. Verify the checksum |
| 880 | ** before returning. This will almost always work. |
| 881 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 882 | if( SQLITE_OK==walIndexTryHdr(pWal, pChanged) ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 883 | return SQLITE_OK; |
| 884 | } |
| 885 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 886 | /* If the first attempt to read the header failed, lock the wal-index |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 887 | ** file and try again. If the header checksum verification fails this |
| 888 | ** time as well, run log recovery. |
| 889 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 890 | if( SQLITE_OK==(rc = walSetLock(pWal, SQLITE_SHM_RECOVER)) ){ |
| 891 | if( SQLITE_OK!=walIndexTryHdr(pWal, pChanged) ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 892 | if( pChanged ){ |
| 893 | *pChanged = 1; |
| 894 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 895 | rc = walIndexRecover(pWal); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 896 | if( rc==SQLITE_OK ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 897 | rc = walIndexTryHdr(pWal, 0); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 900 | walSetLock(pWal, SQLITE_SHM_READ); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | return rc; |
| 904 | } |
| 905 | |
| 906 | /* |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 907 | ** Lock a snapshot. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 908 | ** |
| 909 | ** If this call obtains a new read-lock and the database contents have been |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 910 | ** modified since the most recent call to WalCloseSnapshot() on this Wal |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 911 | ** connection, then *pChanged is set to 1 before returning. Otherwise, it |
| 912 | ** is left unmodified. This is used by the pager layer to determine whether |
| 913 | ** or not any cached pages may be safely reused. |
| 914 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 915 | int sqlite3WalOpenSnapshot(Wal *pWal, int *pChanged){ |
| 916 | int rc; |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 917 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 918 | rc = walSetLock(pWal, SQLITE_SHM_READ); |
| 919 | if( rc==SQLITE_OK ){ |
| 920 | pWal->lockState = SQLITE_SHM_READ; |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 921 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 922 | rc = walIndexReadHdr(pWal, pChanged); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 923 | if( rc!=SQLITE_OK ){ |
| 924 | /* An error occured while attempting log recovery. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 925 | sqlite3WalCloseSnapshot(pWal); |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 926 | }else{ |
| 927 | /* Check if the mapping needs to grow. */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 928 | if( pWal->hdr.iLastPg |
| 929 | && walIndexEntry(pWal->hdr.iLastPg)>=pWal->szWIndex |
| 930 | ){ |
| 931 | walIndexRemap(pWal, -1); |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 932 | } |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 933 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 934 | } |
dan | ba51590 | 2010-04-30 09:32:06 +0000 | [diff] [blame] | 935 | |
| 936 | walIndexUnmap(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 937 | return rc; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | ** Unlock the current snapshot. |
| 942 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 943 | void sqlite3WalCloseSnapshot(Wal *pWal){ |
| 944 | if( pWal->lockState!=SQLITE_SHM_UNLOCK ){ |
| 945 | assert( pWal->lockState==SQLITE_SHM_READ ); |
| 946 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 947 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 948 | } |
| 949 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 950 | /* |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 951 | ** Read a page from the log, if it is present. |
| 952 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 953 | int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, u8 *pOut){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 954 | u32 iRead = 0; |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 955 | u32 *aData; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 956 | int iFrame = (pWal->hdr.iLastPg & 0xFFFFFF00); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 957 | |
dan | 1bc6171 | 2010-04-30 10:24:54 +0000 | [diff] [blame] | 958 | assert( pWal->lockState==SQLITE_SHM_READ||pWal->lockState==SQLITE_SHM_WRITE ); |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 959 | walIndexMap(pWal, -1); |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 960 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 961 | /* Do a linear search of the unindexed block of page-numbers (if any) |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 962 | ** at the end of the wal-index. An alternative to this would be to |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 963 | ** build an index in private memory each time a read transaction is |
| 964 | ** opened on a new snapshot. |
| 965 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 966 | aData = pWal->pWiData; |
| 967 | if( pWal->hdr.iLastPg ){ |
| 968 | u32 *pi = &aData[walIndexEntry(pWal->hdr.iLastPg)]; |
| 969 | u32 *piStop = pi - (pWal->hdr.iLastPg & 0xFF); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 970 | while( *pi!=pgno && pi!=piStop ) pi--; |
| 971 | if( pi!=piStop ){ |
| 972 | iRead = (pi-piStop) + iFrame; |
| 973 | } |
| 974 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 975 | assert( iRead==0 || aData[walIndexEntry(iRead)]==pgno ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 976 | |
| 977 | while( iRead==0 && iFrame>0 ){ |
| 978 | int iLow = 0; |
| 979 | int iHigh = 255; |
| 980 | u32 *aFrame; |
| 981 | u8 *aIndex; |
| 982 | |
| 983 | iFrame -= 256; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 984 | aFrame = &aData[walIndexEntry(iFrame+1)]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 985 | aIndex = (u8 *)&aFrame[256]; |
| 986 | |
| 987 | while( iLow<=iHigh ){ |
| 988 | int iTest = (iLow+iHigh)>>1; |
| 989 | u32 iPg = aFrame[aIndex[iTest]]; |
| 990 | |
| 991 | if( iPg==pgno ){ |
| 992 | iRead = iFrame + 1 + aIndex[iTest]; |
| 993 | break; |
| 994 | } |
| 995 | else if( iPg<pgno ){ |
| 996 | iLow = iTest+1; |
| 997 | }else{ |
| 998 | iHigh = iTest-1; |
| 999 | } |
| 1000 | } |
| 1001 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1002 | assert( iRead==0 || aData[walIndexEntry(iRead)]==pgno ); |
| 1003 | walIndexUnmap(pWal); |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 1004 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1005 | /* If iRead is non-zero, then it is the log frame number that contains the |
| 1006 | ** required page. Read and return data from the log file. |
| 1007 | */ |
| 1008 | if( iRead ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1009 | i64 iOffset = walFrameOffset(iRead, pWal->hdr.pgsz) + WAL_FRAME_HDRSIZE; |
| 1010 | *pInWal = 1; |
| 1011 | return sqlite3OsRead(pWal->pFd, pOut, pWal->hdr.pgsz, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1014 | *pInWal = 0; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1015 | return SQLITE_OK; |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | /* |
| 1020 | ** Set *pPgno to the size of the database file (or zero, if unknown). |
| 1021 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1022 | void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){ |
| 1023 | assert( pWal->lockState==SQLITE_SHM_READ |
| 1024 | || pWal->lockState==SQLITE_SHM_WRITE ); |
| 1025 | *pPgno = pWal->hdr.nPage; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1029 | ** This function returns SQLITE_OK if the caller may write to the database. |
| 1030 | ** Otherwise, if the caller is operating on a snapshot that has already |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 1031 | ** been overwritten by another writer, SQLITE_BUSY is returned. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1032 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1033 | int sqlite3WalWriteLock(Wal *pWal, int op){ |
| 1034 | int rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1035 | if( op ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1036 | assert( pWal->lockState == SQLITE_SHM_READ ); |
| 1037 | rc = walSetLock(pWal, SQLITE_SHM_WRITE); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1038 | |
| 1039 | /* If this connection is not reading the most recent database snapshot, |
| 1040 | ** it is not possible to write to the database. In this case release |
| 1041 | ** the write locks and return SQLITE_BUSY. |
| 1042 | */ |
| 1043 | if( rc==SQLITE_OK ){ |
| 1044 | rc = walIndexMap(pWal, -1); |
| 1045 | if( rc==SQLITE_OK |
| 1046 | && memcmp(&pWal->hdr, pWal->pWiData, sizeof(WalIndexHdr)) |
| 1047 | ){ |
| 1048 | rc = SQLITE_BUSY; |
| 1049 | } |
| 1050 | walIndexUnmap(pWal); |
| 1051 | if( rc!=SQLITE_OK ){ |
| 1052 | walSetLock(pWal, SQLITE_SHM_READ); |
| 1053 | } |
| 1054 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1055 | }else if( pWal->lockState==SQLITE_SHM_WRITE ){ |
| 1056 | rc = walSetLock(pWal, SQLITE_SHM_READ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1057 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1058 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1061 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1062 | ** The Wal object passed to this function must be holding the write-lock. |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1063 | ** |
| 1064 | ** If any data has been written (but not committed) to the log file, this |
| 1065 | ** function moves the write-pointer back to the start of the transaction. |
| 1066 | ** |
| 1067 | ** Additionally, the callback function is invoked for each frame written |
| 1068 | ** to the log since the start of the transaction. If the callback returns |
| 1069 | ** other than SQLITE_OK, it is not invoked again and the error code is |
| 1070 | ** returned to the caller. |
| 1071 | ** |
| 1072 | ** Otherwise, if the callback function does not return an error, this |
| 1073 | ** function returns SQLITE_OK. |
| 1074 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1075 | int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1076 | int rc = SQLITE_OK; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1077 | Pgno iMax = pWal->hdr.iLastPg; |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1078 | Pgno iFrame; |
| 1079 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1080 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
| 1081 | walIndexReadHdr(pWal, 0); |
| 1082 | for(iFrame=pWal->hdr.iLastPg+1; iFrame<=iMax && rc==SQLITE_OK; iFrame++){ |
| 1083 | rc = xUndo(pUndoCtx, pWal->pWiData[walIndexEntry(iFrame)]); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1084 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1085 | walIndexUnmap(pWal); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 1086 | return rc; |
| 1087 | } |
| 1088 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1089 | /* Return an integer that records the current (uncommitted) write |
| 1090 | ** position in the WAL |
| 1091 | */ |
| 1092 | u32 sqlite3WalSavepoint(Wal *pWal){ |
| 1093 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
| 1094 | return pWal->hdr.iLastPg; |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1097 | /* Move the write position of the WAL back to iFrame. Called in |
| 1098 | ** response to a ROLLBACK TO command. |
| 1099 | */ |
| 1100 | int sqlite3WalSavepointUndo(Wal *pWal, u32 iFrame){ |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1101 | int rc = SQLITE_OK; |
| 1102 | u8 aCksum[8]; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1103 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1104 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1105 | pWal->hdr.iLastPg = iFrame; |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1106 | if( iFrame>0 ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1107 | i64 iOffset = walFrameOffset(iFrame, pWal->hdr.pgsz) + sizeof(u32)*2; |
| 1108 | rc = sqlite3OsRead(pWal->pFd, aCksum, sizeof(aCksum), iOffset); |
| 1109 | pWal->hdr.iCheck1 = sqlite3Get4byte(&aCksum[0]); |
| 1110 | pWal->hdr.iCheck2 = sqlite3Get4byte(&aCksum[4]); |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | return rc; |
| 1114 | } |
| 1115 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1116 | /* |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 1117 | ** Write a set of frames to the log. The caller must hold the write-lock |
| 1118 | ** on the log file (obtained using sqlite3WalWriteLock()). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1119 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1120 | int sqlite3WalFrames( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1121 | Wal *pWal, /* Wal handle to write to */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1122 | int nPgsz, /* Database page-size in bytes */ |
| 1123 | PgHdr *pList, /* List of dirty pages to write */ |
| 1124 | Pgno nTruncate, /* Database size after this commit */ |
| 1125 | int isCommit, /* True if this is a commit */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1126 | int sync_flags /* Flags to pass to OsSync() (or 0) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1127 | ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1128 | int rc; /* Used to catch return codes */ |
| 1129 | u32 iFrame; /* Next frame address */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1130 | u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1131 | PgHdr *p; /* Iterator to run through pList with. */ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1132 | u32 aCksum[2]; /* Checksums */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1133 | PgHdr *pLast; /* Last frame in list */ |
| 1134 | int nLast = 0; /* Number of extra copies of last page */ |
| 1135 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1136 | assert( WAL_FRAME_HDRSIZE==(4 * 2 + 2*sizeof(u32)) ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1137 | assert( pList ); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1138 | assert( pWal->lockState==SQLITE_SHM_WRITE ); |
dan | ba51590 | 2010-04-30 09:32:06 +0000 | [diff] [blame] | 1139 | assert( pWal->pWiData==0 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1140 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1141 | /* If this is the first frame written into the log, write the log |
| 1142 | ** header to the start of the log file. See comments at the top of |
| 1143 | ** this file for a description of the log-header format. |
| 1144 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1145 | assert( WAL_FRAME_HDRSIZE>=WAL_HDRSIZE ); |
| 1146 | iFrame = pWal->hdr.iLastPg; |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1147 | if( iFrame==0 ){ |
| 1148 | sqlite3Put4byte(aFrame, nPgsz); |
| 1149 | sqlite3_randomness(8, &aFrame[4]); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1150 | pWal->hdr.iCheck1 = sqlite3Get4byte(&aFrame[4]); |
| 1151 | pWal->hdr.iCheck2 = sqlite3Get4byte(&aFrame[8]); |
| 1152 | rc = sqlite3OsWrite(pWal->pFd, aFrame, WAL_HDRSIZE, 0); |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 1153 | if( rc!=SQLITE_OK ){ |
| 1154 | return rc; |
| 1155 | } |
| 1156 | } |
| 1157 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1158 | aCksum[0] = pWal->hdr.iCheck1; |
| 1159 | aCksum[1] = pWal->hdr.iCheck2; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* Write the log file. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1162 | for(p=pList; p; p=p->pDirty){ |
| 1163 | u32 nDbsize; /* Db-size field for frame header */ |
| 1164 | i64 iOffset; /* Write offset in log file */ |
| 1165 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1166 | iOffset = walFrameOffset(++iFrame, nPgsz); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* Populate and write the frame header */ |
| 1169 | nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1170 | walEncodeFrame(aCksum, p->pgno, nDbsize, nPgsz, p->pData, aFrame); |
| 1171 | rc = sqlite3OsWrite(pWal->pFd, aFrame, sizeof(aFrame), iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1172 | if( rc!=SQLITE_OK ){ |
| 1173 | return rc; |
| 1174 | } |
| 1175 | |
| 1176 | /* Write the page data */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1177 | rc = sqlite3OsWrite(pWal->pFd, p->pData, nPgsz, iOffset + sizeof(aFrame)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1178 | if( rc!=SQLITE_OK ){ |
| 1179 | return rc; |
| 1180 | } |
| 1181 | pLast = p; |
| 1182 | } |
| 1183 | |
| 1184 | /* Sync the log file if the 'isSync' flag was specified. */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1185 | if( sync_flags ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1186 | i64 iSegment = sqlite3OsSectorSize(pWal->pFd); |
| 1187 | i64 iOffset = walFrameOffset(iFrame+1, nPgsz); |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 1188 | |
| 1189 | assert( isCommit ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1190 | |
| 1191 | if( iSegment<SQLITE_DEFAULT_SECTOR_SIZE ){ |
| 1192 | iSegment = SQLITE_DEFAULT_SECTOR_SIZE; |
| 1193 | } |
| 1194 | iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment); |
| 1195 | while( iOffset<iSegment ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1196 | walEncodeFrame(aCksum,pLast->pgno,nTruncate,nPgsz,pLast->pData,aFrame); |
| 1197 | rc = sqlite3OsWrite(pWal->pFd, aFrame, sizeof(aFrame), iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1198 | if( rc!=SQLITE_OK ){ |
| 1199 | return rc; |
| 1200 | } |
| 1201 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1202 | iOffset += WAL_FRAME_HDRSIZE; |
| 1203 | rc = sqlite3OsWrite(pWal->pFd, pLast->pData, nPgsz, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1204 | if( rc!=SQLITE_OK ){ |
| 1205 | return rc; |
| 1206 | } |
| 1207 | nLast++; |
| 1208 | iOffset += nPgsz; |
| 1209 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1210 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1211 | rc = sqlite3OsSync(pWal->pFd, sync_flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1212 | if( rc!=SQLITE_OK ){ |
| 1213 | return rc; |
| 1214 | } |
| 1215 | } |
dan | ba51590 | 2010-04-30 09:32:06 +0000 | [diff] [blame] | 1216 | assert( pWal->pWiData==0 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1217 | |
| 1218 | /* Append data to the log summary. It is not necessary to lock the |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1219 | ** wal-index to do this as the RESERVED lock held on the db file |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1220 | ** guarantees that there are no other writers, and no data that may |
| 1221 | ** be in use by existing readers is being overwritten. |
| 1222 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1223 | iFrame = pWal->hdr.iLastPg; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1224 | for(p=pList; p; p=p->pDirty){ |
| 1225 | iFrame++; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1226 | walIndexAppend(pWal, iFrame, p->pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1227 | } |
| 1228 | while( nLast>0 ){ |
| 1229 | iFrame++; |
| 1230 | nLast--; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1231 | walIndexAppend(pWal, iFrame, pLast->pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /* Update the private copy of the header. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1235 | pWal->hdr.pgsz = nPgsz; |
| 1236 | pWal->hdr.iLastPg = iFrame; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1237 | if( isCommit ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1238 | pWal->hdr.iChange++; |
| 1239 | pWal->hdr.nPage = nTruncate; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1240 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1241 | pWal->hdr.iCheck1 = aCksum[0]; |
| 1242 | pWal->hdr.iCheck2 = aCksum[1]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1243 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1244 | /* If this is a commit, update the wal-index header too. */ |
| 1245 | if( isCommit ){ |
| 1246 | walIndexWriteHdr(pWal, &pWal->hdr); |
| 1247 | pWal->iCallback = iFrame; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1248 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1249 | walIndexUnmap(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1250 | |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1251 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /* |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1255 | ** Checkpoint the database: |
| 1256 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1257 | ** 1. Acquire a CHECKPOINT lock |
| 1258 | ** 2. Copy the contents of the log into the database file. |
| 1259 | ** 3. Zero the wal-index header (so new readers will ignore the log). |
| 1260 | ** 4. Drop the CHECKPOINT lock. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1261 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1262 | int sqlite3WalCheckpoint( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1263 | Wal *pWal, /* Wal connection */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1264 | sqlite3_file *pFd, /* File descriptor open on db file */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1265 | int sync_flags, /* Flags to sync db file with (or 0) */ |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1266 | u8 *zBuf, /* Temporary buffer to use */ |
| 1267 | int (*xBusyHandler)(void *), /* Pointer to busy-handler function */ |
| 1268 | void *pBusyHandlerArg /* Argument to pass to xBusyHandler */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1269 | ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1270 | int rc; /* Return code */ |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1271 | int isChanged = 0; /* True if a new wal-index header is loaded */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1272 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1273 | assert( pWal->lockState==SQLITE_SHM_UNLOCK ); |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 1274 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1275 | /* Get the CHECKPOINT lock */ |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1276 | do { |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1277 | rc = walSetLock(pWal, SQLITE_SHM_CHECKPOINT); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1278 | }while( rc==SQLITE_BUSY && xBusyHandler(pBusyHandlerArg) ); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1279 | if( rc!=SQLITE_OK ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1280 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1281 | return rc; |
| 1282 | } |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1283 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1284 | /* Copy data from the log to the database file. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1285 | rc = walIndexReadHdr(pWal, &isChanged); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1286 | if( rc==SQLITE_OK ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1287 | rc = walCheckpoint(pWal, pFd, sync_flags, zBuf); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1288 | } |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1289 | if( isChanged ){ |
| 1290 | /* If a new wal-index header was loaded before the checkpoint was |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1291 | ** performed, then the pager-cache associated with log pWal is now |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1292 | ** out of date. So zero the cached wal-index header to ensure that |
| 1293 | ** next time the pager opens a snapshot on this database it knows that |
| 1294 | ** the cache needs to be reset. |
| 1295 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1296 | memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 1297 | } |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1298 | |
| 1299 | /* Release the locks. */ |
dan | 87bfb51 | 2010-04-30 11:43:28 +0000 | [diff] [blame] | 1300 | walIndexUnmap(pWal); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1301 | walSetLock(pWal, SQLITE_SHM_UNLOCK); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1302 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1305 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 1306 | ** number of frames in the WAL at the point of the last commit since |
| 1307 | ** sqlite3WalCallback() was called. If no commits have occurred since |
| 1308 | ** the last call, then return 0. |
| 1309 | */ |
| 1310 | int sqlite3WalCallback(Wal *pWal){ |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1311 | u32 ret = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1312 | if( pWal ){ |
| 1313 | ret = pWal->iCallback; |
| 1314 | pWal->iCallback = 0; |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1315 | } |
| 1316 | return (int)ret; |
| 1317 | } |