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