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