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 | ** |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 13 | ** This file contains the implementation of a write-ahead log (WAL) used in |
| 14 | ** "journal_mode=WAL" mode. |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 15 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 16 | ** WRITE-AHEAD LOG (WAL) FILE FORMAT |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 17 | ** |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 18 | ** A WAL file consists of a header followed by zero or more "frames". |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 19 | ** Each frame records the revised content of a single page from the |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 20 | ** database file. All changes to the database are recorded by writing |
| 21 | ** frames into the WAL. Transactions commit when a frame is written that |
| 22 | ** contains a commit marker. A single WAL can and usually does record |
| 23 | ** multiple transactions. Periodically, the content of the WAL is |
| 24 | ** transferred back into the database file in an operation called a |
| 25 | ** "checkpoint". |
| 26 | ** |
| 27 | ** A single WAL file can be used multiple times. In other words, the |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 28 | ** WAL can fill up with frames and then be checkpointed and then new |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 29 | ** frames can overwrite the old ones. A WAL always grows from beginning |
| 30 | ** toward the end. Checksums and counters attached to each frame are |
| 31 | ** used to determine which frames within the WAL are valid and which |
| 32 | ** are leftovers from prior checkpoints. |
| 33 | ** |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 34 | ** The WAL header is 24 bytes in size and consists of the following six |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 35 | ** big-endian 32-bit unsigned integer values: |
| 36 | ** |
drh | 1b78eaf | 2010-05-25 13:40:03 +0000 | [diff] [blame] | 37 | ** 0: Magic number. 0x377f0682 or 0x377f0683 |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 38 | ** 4: File format version. Currently 3007000 |
| 39 | ** 8: Database page size. Example: 1024 |
| 40 | ** 12: Checkpoint sequence number |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 41 | ** 16: Salt-1, random integer incremented with each checkpoint |
| 42 | ** 20: Salt-2, a different random integer changing with each ckpt |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 43 | ** |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 44 | ** Immediately following the wal-header are zero or more frames. Each |
| 45 | ** frame consists of a 24-byte frame-header followed by a <page-size> bytes |
| 46 | ** of page data. The frame-header is broken into 6 big-endian 32-bit unsigned |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 47 | ** integer values, as follows: |
| 48 | ** |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 49 | ** 0: Page number. |
| 50 | ** 4: For commit records, the size of the database image in pages |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 51 | ** after the commit. For all other records, zero. |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 52 | ** 8: Salt-1 (copied from the header) |
| 53 | ** 12: Salt-2 (copied from the header) |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 54 | ** 16: Checksum-1. |
| 55 | ** 20: Checksum-2. |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 56 | ** |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 57 | ** A frame is considered valid if and only if the following conditions are |
| 58 | ** true: |
| 59 | ** |
| 60 | ** (1) The salt-1 and salt-2 values in the frame-header match |
| 61 | ** salt values in the wal-header |
| 62 | ** |
| 63 | ** (2) The checksum values in the final 8 bytes of the frame-header |
drh | 1b78eaf | 2010-05-25 13:40:03 +0000 | [diff] [blame] | 64 | ** exactly match the checksum computed consecutively on the |
| 65 | ** WAL header and the first 8 bytes and the content of all frames |
| 66 | ** up to and including the current frame. |
| 67 | ** |
| 68 | ** The checksum is computed using 32-bit big-endian integers if the |
| 69 | ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it |
| 70 | ** is computed using little-endian if the magic number is 0x377f0682. |
drh | 51b21b1 | 2010-05-25 15:53:31 +0000 | [diff] [blame] | 71 | ** The checksum values are always stored in the frame header in a |
| 72 | ** big-endian format regardless of which byte order is used to compute |
| 73 | ** the checksum. The checksum is computed by interpreting the input as |
| 74 | ** an even number of unsigned 32-bit integers: x[0] through x[N]. The |
| 75 | ** |
| 76 | ** for i from 0 to n-1 step 2: |
| 77 | ** s0 += x[i] + s1; |
| 78 | ** s1 += x[i+1] + s0; |
| 79 | ** endfor |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 80 | ** |
| 81 | ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the |
| 82 | ** WAL is transferred into the database, then the database is VFS.xSync-ed. |
| 83 | ** The VFS.xSync operations server as write barriers - all writes launched |
| 84 | ** before the xSync must complete before any write that launches after the |
| 85 | ** xSync begins. |
| 86 | ** |
| 87 | ** After each checkpoint, the salt-1 value is incremented and the salt-2 |
| 88 | ** value is randomized. This prevents old and new frames in the WAL from |
| 89 | ** being considered valid at the same time and being checkpointing together |
| 90 | ** following a crash. |
| 91 | ** |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 92 | ** READER ALGORITHM |
| 93 | ** |
| 94 | ** To read a page from the database (call it page number P), a reader |
| 95 | ** first checks the WAL to see if it contains page P. If so, then the |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 96 | ** last valid instance of page P that is a followed by a commit frame |
| 97 | ** or is a commit frame itself becomes the value read. If the WAL |
| 98 | ** contains no copies of page P that are valid and which are a commit |
| 99 | ** frame or are followed by a commit frame, then page P is read from |
| 100 | ** the database file. |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 101 | ** |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 102 | ** To start a read transaction, the reader records the index of the last |
| 103 | ** valid frame in the WAL. The reader uses this recorded "mxFrame" value |
| 104 | ** for all subsequent read operations. New transactions can be appended |
| 105 | ** to the WAL, but as long as the reader uses its original mxFrame value |
| 106 | ** and ignores the newly appended content, it will see a consistent snapshot |
| 107 | ** of the database from a single point in time. This technique allows |
| 108 | ** multiple concurrent readers to view different versions of the database |
| 109 | ** content simultaneously. |
| 110 | ** |
| 111 | ** The reader algorithm in the previous paragraphs works correctly, but |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 112 | ** because frames for page P can appear anywhere within the WAL, the |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 113 | ** reader has to scan the entire WAL looking for page P frames. If the |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 114 | ** WAL is large (multiple megabytes is typical) that scan can be slow, |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 115 | ** and read performance suffers. To overcome this problem, a separate |
| 116 | ** data structure called the wal-index is maintained to expedite the |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 117 | ** search for frames of a particular page. |
| 118 | ** |
| 119 | ** WAL-INDEX FORMAT |
| 120 | ** |
| 121 | ** Conceptually, the wal-index is shared memory, though VFS implementations |
| 122 | ** might choose to implement the wal-index using a mmapped file. Because |
| 123 | ** the wal-index is shared memory, SQLite does not support journal_mode=WAL |
| 124 | ** on a network filesystem. All users of the database must be able to |
| 125 | ** share memory. |
| 126 | ** |
| 127 | ** The wal-index is transient. After a crash, the wal-index can (and should |
| 128 | ** be) reconstructed from the original WAL file. In fact, the VFS is required |
| 129 | ** to either truncate or zero the header of the wal-index when the last |
| 130 | ** connection to it closes. Because the wal-index is transient, it can |
| 131 | ** use an architecture-specific format; it does not have to be cross-platform. |
| 132 | ** Hence, unlike the database and WAL file formats which store all values |
| 133 | ** as big endian, the wal-index can store multi-byte values in the native |
| 134 | ** byte order of the host computer. |
| 135 | ** |
| 136 | ** The purpose of the wal-index is to answer this question quickly: Given |
| 137 | ** a page number P, return the index of the last frame for page P in the WAL, |
| 138 | ** or return NULL if there are no frames for page P in the WAL. |
| 139 | ** |
| 140 | ** The wal-index consists of a header region, followed by an one or |
| 141 | ** more index blocks. |
| 142 | ** |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 143 | ** The wal-index header contains the total number of frames within the WAL |
| 144 | ** in the the mxFrame field. Each index block contains information on |
| 145 | ** HASHTABLE_NPAGE frames. Each index block contains two sections, a |
| 146 | ** mapping which is a database page number for each frame, and a hash |
| 147 | ** table used to look up frames by page number. The mapping section is |
| 148 | ** an array of HASHTABLE_NPAGE 32-bit page numbers. The first entry on the |
| 149 | ** array is the page number for the first frame; the second entry is the |
| 150 | ** page number for the second frame; and so forth. The last index block |
| 151 | ** holds a total of (mxFrame%HASHTABLE_NPAGE) page numbers. All index |
| 152 | ** blocks other than the last are completely full with HASHTABLE_NPAGE |
| 153 | ** page numbers. All index blocks are the same size; the mapping section |
| 154 | ** of the last index block merely contains unused entries if mxFrame is |
| 155 | ** not an even multiple of HASHTABLE_NPAGE. |
| 156 | ** |
| 157 | ** Even without using the hash table, the last frame for page P |
| 158 | ** can be found by scanning the mapping sections of each index block |
| 159 | ** starting with the last index block and moving toward the first, and |
| 160 | ** within each index block, starting at the end and moving toward the |
| 161 | ** beginning. The first entry that equals P corresponds to the frame |
| 162 | ** holding the content for that page. |
| 163 | ** |
| 164 | ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers. |
| 165 | ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the |
| 166 | ** hash table for each page number in the mapping section, so the hash |
| 167 | ** table is never more than half full. The expected number of collisions |
| 168 | ** prior to finding a match is 1. Each entry of the hash table is an |
| 169 | ** 1-based index of an entry in the mapping section of the same |
| 170 | ** index block. Let K be the 1-based index of the largest entry in |
| 171 | ** the mapping section. (For index blocks other than the last, K will |
| 172 | ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block |
| 173 | ** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 174 | ** contain a value of 0. |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 175 | ** |
| 176 | ** To look for page P in the hash table, first compute a hash iKey on |
| 177 | ** P as follows: |
| 178 | ** |
| 179 | ** iKey = (P * 383) % HASHTABLE_NSLOT |
| 180 | ** |
| 181 | ** Then start scanning entries of the hash table, starting with iKey |
| 182 | ** (wrapping around to the beginning when the end of the hash table is |
| 183 | ** reached) until an unused hash slot is found. Let the first unused slot |
| 184 | ** be at index iUnused. (iUnused might be less than iKey if there was |
| 185 | ** wrap-around.) Because the hash table is never more than half full, |
| 186 | ** the search is guaranteed to eventually hit an unused entry. Let |
| 187 | ** iMax be the value between iKey and iUnused, closest to iUnused, |
| 188 | ** where aHash[iMax]==P. If there is no iMax entry (if there exists |
| 189 | ** no hash slot such that aHash[i]==p) then page P is not in the |
| 190 | ** current index block. Otherwise the iMax-th mapping entry of the |
| 191 | ** current index block corresponds to the last entry that references |
| 192 | ** page P. |
| 193 | ** |
| 194 | ** A hash search begins with the last index block and moves toward the |
| 195 | ** first index block, looking for entries corresponding to page P. On |
| 196 | ** average, only two or three slots in each index block need to be |
| 197 | ** examined in order to either find the last entry for page P, or to |
| 198 | ** establish that no such entry exists in the block. Each index block |
| 199 | ** holds over 4000 entries. So two or three index blocks are sufficient |
| 200 | ** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10 |
| 201 | ** comparisons (on average) suffice to either locate a frame in the |
| 202 | ** WAL or to establish that the frame does not exist in the WAL. This |
| 203 | ** is much faster than scanning the entire 10MB WAL. |
| 204 | ** |
| 205 | ** Note that entries are added in order of increasing K. Hence, one |
| 206 | ** reader might be using some value K0 and a second reader that started |
| 207 | ** at a later time (after additional transactions were added to the WAL |
| 208 | ** and to the wal-index) might be using a different value K1, where K1>K0. |
| 209 | ** Both readers can use the same hash table and mapping section to get |
| 210 | ** the correct result. There may be entries in the hash table with |
| 211 | ** K>K0 but to the first reader, those entries will appear to be unused |
| 212 | ** slots in the hash table and so the first reader will get an answer as |
| 213 | ** if no values greater than K0 had ever been inserted into the hash table |
| 214 | ** in the first place - which is what reader one wants. Meanwhile, the |
| 215 | ** second reader using K1 will see additional values that were inserted |
| 216 | ** later, which is exactly what reader two wants. |
| 217 | ** |
dan | 6f15014 | 2010-05-21 15:31:56 +0000 | [diff] [blame] | 218 | ** When a rollback occurs, the value of K is decreased. Hash table entries |
| 219 | ** that correspond to frames greater than the new K value are removed |
| 220 | ** from the hash table at this point. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 221 | */ |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 222 | #ifndef SQLITE_OMIT_WAL |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 223 | |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 224 | #include "wal.h" |
| 225 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 226 | /* |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 227 | ** Trace output macros |
| 228 | */ |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 229 | #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 230 | int sqlite3WalTrace = 0; |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 231 | # define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X |
| 232 | #else |
| 233 | # define WALTRACE(X) |
| 234 | #endif |
| 235 | |
| 236 | |
| 237 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 238 | ** Indices of various locking bytes. WAL_NREADER is the number |
| 239 | ** of available reader locks and should be at least 3. |
| 240 | */ |
| 241 | #define WAL_WRITE_LOCK 0 |
| 242 | #define WAL_ALL_BUT_WRITE 1 |
| 243 | #define WAL_CKPT_LOCK 1 |
| 244 | #define WAL_RECOVER_LOCK 2 |
| 245 | #define WAL_READ_LOCK(I) (3+(I)) |
| 246 | #define WAL_NREADER (SQLITE_SHM_NLOCK-3) |
| 247 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 248 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 249 | /* Object declarations */ |
| 250 | typedef struct WalIndexHdr WalIndexHdr; |
| 251 | typedef struct WalIterator WalIterator; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 252 | typedef struct WalCkptInfo WalCkptInfo; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 253 | |
| 254 | |
| 255 | /* |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 256 | ** The following object holds a copy of the wal-index header content. |
| 257 | ** |
| 258 | ** The actual header in the wal-index consists of two copies of this |
| 259 | ** object. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 260 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 261 | struct WalIndexHdr { |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 262 | u32 iChange; /* Counter incremented each transaction */ |
drh | 4b82c38 | 2010-05-31 18:24:19 +0000 | [diff] [blame] | 263 | u8 isInit; /* 1 when initialized */ |
| 264 | u8 bigEndCksum; /* True if checksums in WAL are big-endian */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 265 | u16 szPage; /* Database page size in bytes */ |
dan | d0aa342 | 2010-05-31 16:41:53 +0000 | [diff] [blame] | 266 | u32 mxFrame; /* Index of last valid frame in the WAL */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 267 | u32 nPage; /* Size of database in pages */ |
| 268 | u32 aFrameCksum[2]; /* Checksum of last frame in log */ |
| 269 | u32 aSalt[2]; /* Two salt values copied from WAL header */ |
| 270 | u32 aCksum[2]; /* Checksum over all prior fields */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 273 | /* |
| 274 | ** A copy of the following object occurs in the wal-index immediately |
| 275 | ** following the second copy of the WalIndexHdr. This object stores |
| 276 | ** information used by checkpoint. |
| 277 | ** |
| 278 | ** nBackfill is the number of frames in the WAL that have been written |
| 279 | ** back into the database. (We call the act of moving content from WAL to |
| 280 | ** database "backfilling".) The nBackfill number is never greater than |
| 281 | ** WalIndexHdr.mxFrame. nBackfill can only be increased by threads |
| 282 | ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread). |
| 283 | ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from |
| 284 | ** mxFrame back to zero when the WAL is reset. |
| 285 | ** |
| 286 | ** There is one entry in aReadMark[] for each reader lock. If a reader |
| 287 | ** holds read-lock K, then the value in aReadMark[K] is no greater than |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 288 | ** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff) |
| 289 | ** for any aReadMark[] means that entry is unused. aReadMark[0] is |
| 290 | ** a special case; its value is never used and it exists as a place-holder |
| 291 | ** to avoid having to offset aReadMark[] indexs by one. Readers holding |
| 292 | ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content |
| 293 | ** directly from the database. |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 294 | ** |
| 295 | ** The value of aReadMark[K] may only be changed by a thread that |
| 296 | ** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of |
| 297 | ** aReadMark[K] cannot changed while there is a reader is using that mark |
| 298 | ** since the reader will be holding a shared lock on WAL_READ_LOCK(K). |
| 299 | ** |
| 300 | ** The checkpointer may only transfer frames from WAL to database where |
| 301 | ** the frame numbers are less than or equal to every aReadMark[] that is |
| 302 | ** in use (that is, every aReadMark[j] for which there is a corresponding |
| 303 | ** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the |
| 304 | ** largest value and will increase an unused aReadMark[] to mxFrame if there |
| 305 | ** is not already an aReadMark[] equal to mxFrame. The exception to the |
| 306 | ** previous sentence is when nBackfill equals mxFrame (meaning that everything |
| 307 | ** in the WAL has been backfilled into the database) then new readers |
| 308 | ** will choose aReadMark[0] which has value 0 and hence such reader will |
| 309 | ** get all their all content directly from the database file and ignore |
| 310 | ** the WAL. |
| 311 | ** |
| 312 | ** Writers normally append new frames to the end of the WAL. However, |
| 313 | ** if nBackfill equals mxFrame (meaning that all WAL content has been |
| 314 | ** written back into the database) and if no readers are using the WAL |
| 315 | ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then |
| 316 | ** the writer will first "reset" the WAL back to the beginning and start |
| 317 | ** writing new content beginning at frame 1. |
| 318 | ** |
| 319 | ** We assume that 32-bit loads are atomic and so no locks are needed in |
| 320 | ** order to read from any aReadMark[] entries. |
| 321 | */ |
| 322 | struct WalCkptInfo { |
| 323 | u32 nBackfill; /* Number of WAL frames backfilled into DB */ |
| 324 | u32 aReadMark[WAL_NREADER]; /* Reader marks */ |
| 325 | }; |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 326 | #define READMARK_NOT_USED 0xffffffff |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 327 | |
| 328 | |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 329 | /* A block of WALINDEX_LOCK_RESERVED bytes beginning at |
| 330 | ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems |
| 331 | ** only support mandatory file-locks, we do not read or write data |
| 332 | ** from the region of the file on which locks are applied. |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 333 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 334 | #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo)) |
| 335 | #define WALINDEX_LOCK_RESERVED 16 |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 336 | #define WALINDEX_HDR_SIZE (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 337 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 338 | /* Size of header before each frame in wal */ |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 339 | #define WAL_FRAME_HDRSIZE 24 |
dan | ff20701 | 2010-04-24 04:49:15 +0000 | [diff] [blame] | 340 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 341 | /* Size of write ahead log header */ |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 342 | #define WAL_HDRSIZE 24 |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 343 | |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 344 | /* WAL magic value. Either this value, or the same value with the least |
| 345 | ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit |
| 346 | ** big-endian format in the first 4 bytes of a WAL file. |
| 347 | ** |
| 348 | ** If the LSB is set, then the checksums for each frame within the WAL |
| 349 | ** file are calculated by treating all data as an array of 32-bit |
| 350 | ** big-endian words. Otherwise, they are calculated by interpreting |
| 351 | ** all data as 32-bit little-endian words. |
| 352 | */ |
| 353 | #define WAL_MAGIC 0x377f0682 |
| 354 | |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 355 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 356 | ** Return the offset of frame iFrame in the write-ahead log file, |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 357 | ** assuming a database page size of szPage bytes. The offset returned |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 358 | ** is to the start of the write-ahead log frame-header. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 359 | */ |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 360 | #define walFrameOffset(iFrame, szPage) ( \ |
| 361 | WAL_HDRSIZE + ((iFrame)-1)*((szPage)+WAL_FRAME_HDRSIZE) \ |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 362 | ) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 363 | |
| 364 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 365 | ** An open write-ahead log file is represented by an instance of the |
| 366 | ** following object. |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 367 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 368 | struct Wal { |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 369 | sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 370 | sqlite3_file *pDbFd; /* File handle for the database file */ |
| 371 | sqlite3_file *pWalFd; /* File handle for WAL file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 372 | u32 iCallback; /* Value to pass to log callback (or 0) */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 373 | int nWiData; /* Size of array apWiData */ |
| 374 | volatile u32 **apWiData; /* Pointer to wal-index content in memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 375 | u16 szPage; /* Database page size */ |
| 376 | i16 readLock; /* Which read lock is being held. -1 for none */ |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 377 | u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 378 | u8 isWIndexOpen; /* True if ShmOpen() called on pDbFd */ |
| 379 | u8 writeLock; /* True if in a write transaction */ |
| 380 | u8 ckptLock; /* True if holding a checkpoint lock */ |
| 381 | WalIndexHdr hdr; /* Wal-index header for current transaction */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 382 | char *zWalName; /* Name of WAL file */ |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 383 | u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 384 | #ifdef SQLITE_DEBUG |
| 385 | u8 lockError; /* True if a locking error has occurred */ |
| 386 | #endif |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 389 | /* |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 390 | ** Each page of the wal-index mapping contains a hash-table made up of |
| 391 | ** an array of HASHTABLE_NSLOT elements of the following type. |
| 392 | */ |
| 393 | typedef u16 ht_slot; |
| 394 | |
| 395 | /* |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 396 | ** Define the parameters of the hash tables in the wal-index file. There |
| 397 | ** is a hash-table following every HASHTABLE_NPAGE page numbers in the |
| 398 | ** wal-index. |
| 399 | ** |
| 400 | ** Changing any of these constants will alter the wal-index format and |
| 401 | ** create incompatibilities. |
| 402 | */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 403 | #define HASHTABLE_NPAGE 4096 /* Must be power of 2 */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 404 | #define HASHTABLE_HASH_1 383 /* Should be prime */ |
| 405 | #define HASHTABLE_NSLOT (HASHTABLE_NPAGE*2) /* Must be a power of 2 */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 406 | |
| 407 | /* The block of page numbers associated with the first hash-table in a |
| 408 | ** wal-index is smaller than usual. This is so that there is a complete |
| 409 | ** hash-table on each aligned 32KB page of the wal-index. |
| 410 | */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 411 | #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32))) |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 412 | |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 413 | /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */ |
| 414 | #define WALINDEX_PGSZ ( \ |
| 415 | sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \ |
| 416 | ) |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 420 | ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 421 | ** numbered from zero. |
| 422 | ** |
| 423 | ** If this call is successful, *ppPage is set to point to the wal-index |
| 424 | ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs, |
| 425 | ** then an SQLite error code is returned and *ppPage is set to 0. |
| 426 | */ |
| 427 | static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){ |
| 428 | int rc = SQLITE_OK; |
| 429 | |
| 430 | /* Enlarge the pWal->apWiData[] array if required */ |
| 431 | if( pWal->nWiData<=iPage ){ |
| 432 | int nByte = sizeof(u32 *)*(iPage+1); |
| 433 | volatile u32 **apNew; |
| 434 | apNew = (volatile u32 **)sqlite3_realloc(pWal->apWiData, nByte); |
| 435 | if( !apNew ){ |
| 436 | *ppPage = 0; |
| 437 | return SQLITE_NOMEM; |
| 438 | } |
| 439 | memset(&apNew[pWal->nWiData], 0, sizeof(u32 *)*(iPage+1-pWal->nWiData)); |
| 440 | pWal->apWiData = apNew; |
| 441 | pWal->nWiData = iPage+1; |
| 442 | } |
| 443 | |
| 444 | /* Request a pointer to the required page from the VFS */ |
| 445 | if( pWal->apWiData[iPage]==0 ){ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 446 | rc = sqlite3OsShmPage(pWal->pDbFd, iPage, WALINDEX_PGSZ, |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 447 | pWal->writeLock, (void volatile **)&pWal->apWiData[iPage] |
| 448 | ); |
| 449 | } |
| 450 | |
| 451 | *ppPage = pWal->apWiData[iPage]; |
| 452 | assert( iPage==0 || *ppPage || rc!=SQLITE_OK ); |
| 453 | return rc; |
| 454 | } |
| 455 | |
| 456 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 457 | ** Return a pointer to the WalCkptInfo structure in the wal-index. |
| 458 | */ |
| 459 | static volatile WalCkptInfo *walCkptInfo(Wal *pWal){ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 460 | assert( pWal->nWiData>0 && pWal->apWiData[0] ); |
| 461 | return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]); |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | ** Return a pointer to the WalIndexHdr structure in the wal-index. |
| 466 | */ |
| 467 | static volatile WalIndexHdr *walIndexHdr(Wal *pWal){ |
| 468 | assert( pWal->nWiData>0 && pWal->apWiData[0] ); |
| 469 | return (volatile WalIndexHdr*)pWal->apWiData[0]; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 470 | } |
| 471 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 472 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 473 | ** This structure is used to implement an iterator that loops through |
| 474 | ** all frames in the WAL in database page order. Where two or more frames |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 475 | ** correspond to the same database page, the iterator visits only the |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 476 | ** frame most recently written to the WAL (in other words, the frame with |
| 477 | ** the largest index). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 478 | ** |
| 479 | ** The internals of this structure are only accessed by: |
| 480 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 481 | ** walIteratorInit() - Create a new iterator, |
| 482 | ** walIteratorNext() - Step an iterator, |
| 483 | ** walIteratorFree() - Free an iterator. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 484 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 485 | ** This functionality is used by the checkpoint code (see walCheckpoint()). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 486 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 487 | struct WalIterator { |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 488 | int iPrior; /* Last result returned from the iterator */ |
| 489 | int nSegment; /* Size of the aSegment[] array */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 490 | struct WalSegment { |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 491 | int iNext; /* Next slot in aIndex[] not yet returned */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 492 | ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 493 | u32 *aPgno; /* Array of page numbers. */ |
| 494 | int nEntry; /* Max size of aPgno[] and aIndex[] arrays */ |
| 495 | int iZero; /* Frame number associated with aPgno[0] */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 496 | } aSegment[1]; /* One for every 32KB page in the WAL */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 497 | }; |
| 498 | |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 499 | /* |
| 500 | ** The argument to this macro must be of type u32. On a little-endian |
| 501 | ** architecture, it returns the u32 value that results from interpreting |
| 502 | ** the 4 bytes as a big-endian value. On a big-endian architecture, it |
| 503 | ** returns the value that would be produced by intepreting the 4 bytes |
| 504 | ** of the input value as a little-endian integer. |
| 505 | */ |
| 506 | #define BYTESWAP32(x) ( \ |
| 507 | (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \ |
| 508 | + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \ |
| 509 | ) |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 510 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 511 | /* |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 512 | ** Generate or extend an 8 byte checksum based on the data in |
| 513 | ** array aByte[] and the initial values of aIn[0] and aIn[1] (or |
| 514 | ** initial values of 0 and 0 if aIn==NULL). |
| 515 | ** |
| 516 | ** The checksum is written back into aOut[] before returning. |
| 517 | ** |
| 518 | ** nByte must be a positive multiple of 8. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 519 | */ |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 520 | static void walChecksumBytes( |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 521 | int nativeCksum, /* True for native byte-order, false for non-native */ |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 522 | u8 *a, /* Content to be checksummed */ |
| 523 | int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */ |
| 524 | const u32 *aIn, /* Initial checksum value input */ |
| 525 | u32 *aOut /* OUT: Final checksum value output */ |
| 526 | ){ |
| 527 | u32 s1, s2; |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 528 | u32 *aData = (u32 *)a; |
| 529 | u32 *aEnd = (u32 *)&a[nByte]; |
| 530 | |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 531 | if( aIn ){ |
| 532 | s1 = aIn[0]; |
| 533 | s2 = aIn[1]; |
| 534 | }else{ |
| 535 | s1 = s2 = 0; |
| 536 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 537 | |
drh | 584c754 | 2010-05-19 18:08:10 +0000 | [diff] [blame] | 538 | assert( nByte>=8 ); |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 539 | assert( (nByte&0x00000007)==0 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 540 | |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 541 | if( nativeCksum ){ |
| 542 | do { |
| 543 | s1 += *aData++ + s2; |
| 544 | s2 += *aData++ + s1; |
| 545 | }while( aData<aEnd ); |
| 546 | }else{ |
| 547 | do { |
| 548 | s1 += BYTESWAP32(aData[0]) + s2; |
| 549 | s2 += BYTESWAP32(aData[1]) + s1; |
| 550 | aData += 2; |
| 551 | }while( aData<aEnd ); |
| 552 | } |
| 553 | |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 554 | aOut[0] = s1; |
| 555 | aOut[1] = s2; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 559 | ** Write the header information in pWal->hdr into the wal-index. |
| 560 | ** |
| 561 | ** The checksum on pWal->hdr is updated before it is written. |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 562 | */ |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 563 | static void walIndexWriteHdr(Wal *pWal){ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 564 | volatile WalIndexHdr *aHdr = walIndexHdr(pWal); |
| 565 | const int nCksum = offsetof(WalIndexHdr, aCksum); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 566 | |
| 567 | assert( pWal->writeLock ); |
drh | 4b82c38 | 2010-05-31 18:24:19 +0000 | [diff] [blame] | 568 | pWal->hdr.isInit = 1; |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 569 | walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); |
| 570 | memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr)); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 571 | sqlite3OsShmBarrier(pWal->pDbFd); |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 572 | memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* |
| 576 | ** This function encodes a single frame header and writes it to a buffer |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 577 | ** 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] | 578 | ** 4-byte big-endian integers, as follows: |
| 579 | ** |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 580 | ** 0: Page number. |
| 581 | ** 4: For commit records, the size of the database image in pages |
| 582 | ** after the commit. For all other records, zero. |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 583 | ** 8: Salt-1 (copied from the wal-header) |
| 584 | ** 12: Salt-2 (copied from the wal-header) |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 585 | ** 16: Checksum-1. |
| 586 | ** 20: Checksum-2. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 587 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 588 | static void walEncodeFrame( |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 589 | Wal *pWal, /* The write-ahead log */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 590 | u32 iPage, /* Database page number for frame */ |
| 591 | u32 nTruncate, /* New db size (or 0 for non-commit frames) */ |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 592 | u8 *aData, /* Pointer to page data */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 593 | u8 *aFrame /* OUT: Write encoded frame here */ |
| 594 | ){ |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 595 | int nativeCksum; /* True for native byte-order checksums */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 596 | u32 *aCksum = pWal->hdr.aFrameCksum; |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 597 | assert( WAL_FRAME_HDRSIZE==24 ); |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 598 | sqlite3Put4byte(&aFrame[0], iPage); |
| 599 | sqlite3Put4byte(&aFrame[4], nTruncate); |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 600 | memcpy(&aFrame[8], pWal->hdr.aSalt, 8); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 601 | |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 602 | nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN); |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 603 | walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum); |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 604 | walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 605 | |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 606 | sqlite3Put4byte(&aFrame[16], aCksum[0]); |
| 607 | sqlite3Put4byte(&aFrame[20], aCksum[1]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 611 | ** Check to see if the frame with header in aFrame[] and content |
| 612 | ** in aData[] is valid. If it is a valid frame, fill *piPage and |
| 613 | ** *pnTruncate and return true. Return if the frame is not valid. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 614 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 615 | static int walDecodeFrame( |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 616 | Wal *pWal, /* The write-ahead log */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 617 | u32 *piPage, /* OUT: Database page number for frame */ |
| 618 | u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 619 | u8 *aData, /* Pointer to page data (for checksum) */ |
| 620 | u8 *aFrame /* Frame data */ |
| 621 | ){ |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 622 | int nativeCksum; /* True for native byte-order checksums */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 623 | u32 *aCksum = pWal->hdr.aFrameCksum; |
drh | c817915 | 2010-05-24 13:28:36 +0000 | [diff] [blame] | 624 | u32 pgno; /* Page number of the frame */ |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 625 | assert( WAL_FRAME_HDRSIZE==24 ); |
| 626 | |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 627 | /* A frame is only valid if the salt values in the frame-header |
| 628 | ** match the salt values in the wal-header. |
| 629 | */ |
| 630 | if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){ |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 631 | return 0; |
| 632 | } |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 633 | |
drh | c817915 | 2010-05-24 13:28:36 +0000 | [diff] [blame] | 634 | /* A frame is only valid if the page number is creater than zero. |
| 635 | */ |
| 636 | pgno = sqlite3Get4byte(&aFrame[0]); |
| 637 | if( pgno==0 ){ |
| 638 | return 0; |
| 639 | } |
| 640 | |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 641 | /* A frame is only valid if a checksum of the first 16 bytes |
| 642 | ** of the frame-header, and the frame-data matches |
| 643 | ** the checksum in the last 8 bytes of the frame-header. |
| 644 | */ |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 645 | nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN); |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 646 | walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum); |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 647 | walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum); |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 648 | if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) |
| 649 | || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 650 | ){ |
| 651 | /* Checksum failed. */ |
| 652 | return 0; |
| 653 | } |
| 654 | |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 655 | /* If we reach this point, the frame is valid. Return the page number |
| 656 | ** and the new database size. |
| 657 | */ |
drh | c817915 | 2010-05-24 13:28:36 +0000 | [diff] [blame] | 658 | *piPage = pgno; |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 659 | *pnTruncate = sqlite3Get4byte(&aFrame[4]); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 660 | return 1; |
| 661 | } |
| 662 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 663 | |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 664 | #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 665 | /* |
drh | 181e091 | 2010-06-01 01:08:08 +0000 | [diff] [blame] | 666 | ** Names of locks. This routine is used to provide debugging output and is not |
| 667 | ** a part of an ordinary build. |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 668 | */ |
| 669 | static const char *walLockName(int lockIdx){ |
| 670 | if( lockIdx==WAL_WRITE_LOCK ){ |
| 671 | return "WRITE-LOCK"; |
| 672 | }else if( lockIdx==WAL_CKPT_LOCK ){ |
| 673 | return "CKPT-LOCK"; |
| 674 | }else if( lockIdx==WAL_RECOVER_LOCK ){ |
| 675 | return "RECOVER-LOCK"; |
| 676 | }else{ |
| 677 | static char zName[15]; |
| 678 | sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]", |
| 679 | lockIdx-WAL_READ_LOCK(0)); |
| 680 | return zName; |
| 681 | } |
| 682 | } |
| 683 | #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */ |
| 684 | |
| 685 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 686 | /* |
drh | 181e091 | 2010-06-01 01:08:08 +0000 | [diff] [blame] | 687 | ** Set or release locks on the WAL. Locks are either shared or exclusive. |
| 688 | ** A lock cannot be moved directly between shared and exclusive - it must go |
| 689 | ** through the unlocked state first. |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 690 | ** |
| 691 | ** In locking_mode=EXCLUSIVE, all of these routines become no-ops. |
| 692 | */ |
| 693 | static int walLockShared(Wal *pWal, int lockIdx){ |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 694 | int rc; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 695 | if( pWal->exclusiveMode ) return SQLITE_OK; |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 696 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| 697 | SQLITE_SHM_LOCK | SQLITE_SHM_SHARED); |
| 698 | WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal, |
| 699 | walLockName(lockIdx), rc ? "failed" : "ok")); |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 700 | VVA_ONLY( pWal->lockError = (rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 701 | return rc; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 702 | } |
| 703 | static void walUnlockShared(Wal *pWal, int lockIdx){ |
| 704 | if( pWal->exclusiveMode ) return; |
| 705 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1, |
| 706 | SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED); |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 707 | WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx))); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 708 | } |
| 709 | static int walLockExclusive(Wal *pWal, int lockIdx, int n){ |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 710 | int rc; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 711 | if( pWal->exclusiveMode ) return SQLITE_OK; |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 712 | rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| 713 | SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE); |
| 714 | WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal, |
| 715 | walLockName(lockIdx), n, rc ? "failed" : "ok")); |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 716 | VVA_ONLY( pWal->lockError = (rc!=SQLITE_OK && rc!=SQLITE_BUSY); ) |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 717 | return rc; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 718 | } |
| 719 | static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){ |
| 720 | if( pWal->exclusiveMode ) return; |
| 721 | (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n, |
| 722 | SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE); |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 723 | WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal, |
| 724 | walLockName(lockIdx), n)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 728 | ** Compute a hash on a page number. The resulting hash value must land |
drh | 181e091 | 2010-06-01 01:08:08 +0000 | [diff] [blame] | 729 | ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances |
| 730 | ** the hash to the next value in the event of a collision. |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 731 | */ |
| 732 | static int walHash(u32 iPage){ |
| 733 | assert( iPage>0 ); |
| 734 | assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 ); |
| 735 | return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1); |
| 736 | } |
| 737 | static int walNextHash(int iPriorHash){ |
| 738 | return (iPriorHash+1)&(HASHTABLE_NSLOT-1); |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 739 | } |
| 740 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 741 | /* |
| 742 | ** Return pointers to the hash table and page number array stored on |
| 743 | ** page iHash of the wal-index. The wal-index is broken into 32KB pages |
| 744 | ** numbered starting from 0. |
| 745 | ** |
| 746 | ** Set output variable *paHash to point to the start of the hash table |
| 747 | ** in the wal-index file. Set *piZero to one less than the frame |
| 748 | ** number of the first frame indexed by this hash table. If a |
| 749 | ** slot in the hash table is set to N, it refers to frame number |
| 750 | ** (*piZero+N) in the log. |
| 751 | ** |
| 752 | ** Finally, set *paPgno such that for all frames F between (*piZero+1) and |
| 753 | ** (*piZero+HASHTABLE_NPAGE), (*paPgno)[F] is the database page number |
| 754 | ** associated with frame F. |
| 755 | */ |
| 756 | static int walHashGet( |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 757 | Wal *pWal, /* WAL handle */ |
| 758 | int iHash, /* Find the iHash'th table */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 759 | volatile ht_slot **paHash, /* OUT: Pointer to hash index */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 760 | volatile u32 **paPgno, /* OUT: Pointer to page number array */ |
| 761 | u32 *piZero /* OUT: Frame associated with *paPgno[0] */ |
| 762 | ){ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 763 | int rc; /* Return code */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 764 | volatile u32 *aPgno; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 765 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 766 | rc = walIndexPage(pWal, iHash, &aPgno); |
| 767 | assert( rc==SQLITE_OK || iHash>0 ); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 768 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 769 | if( rc==SQLITE_OK ){ |
| 770 | u32 iZero; |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 771 | volatile ht_slot *aHash; |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 772 | |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 773 | aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE]; |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 774 | if( iHash==0 ){ |
| 775 | aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)-1]; |
| 776 | iZero = 0; |
| 777 | }else{ |
| 778 | iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE; |
| 779 | aPgno = &aPgno[-1*iZero-1]; |
| 780 | } |
| 781 | |
| 782 | *paPgno = aPgno; |
| 783 | *paHash = aHash; |
| 784 | *piZero = iZero; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 785 | } |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 786 | return rc; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 787 | } |
| 788 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 789 | /* |
| 790 | ** Return the number of the wal-index page that contains the hash-table |
| 791 | ** and page-number array that contain entries corresponding to WAL frame |
| 792 | ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages |
| 793 | ** are numbered starting from 0. |
| 794 | */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 795 | static int walFramePage(u32 iFrame){ |
| 796 | int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE; |
| 797 | assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE) |
| 798 | && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE) |
| 799 | && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)) |
| 800 | && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE) |
| 801 | && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE)) |
| 802 | ); |
| 803 | return iHash; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | ** Return the page number associated with frame iFrame in this WAL. |
| 808 | */ |
| 809 | static u32 walFramePgno(Wal *pWal, u32 iFrame){ |
| 810 | int iHash = walFramePage(iFrame); |
| 811 | if( iHash==0 ){ |
| 812 | return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1]; |
| 813 | } |
| 814 | return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE]; |
| 815 | } |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 816 | |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 817 | /* |
| 818 | ** Remove entries from the hash table that point to WAL slots greater |
| 819 | ** than pWal->hdr.mxFrame. |
| 820 | ** |
| 821 | ** This function is called whenever pWal->hdr.mxFrame is decreased due |
| 822 | ** to a rollback or savepoint. |
| 823 | ** |
drh | 181e091 | 2010-06-01 01:08:08 +0000 | [diff] [blame] | 824 | ** At most only the hash table containing pWal->hdr.mxFrame needs to be |
| 825 | ** updated. Any later hash tables will be automatically cleared when |
| 826 | ** pWal->hdr.mxFrame advances to the point where those hash tables are |
| 827 | ** actually needed. |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 828 | */ |
| 829 | static void walCleanupHash(Wal *pWal){ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 830 | volatile ht_slot *aHash; /* Pointer to hash table to clear */ |
| 831 | volatile u32 *aPgno; /* Page number array for hash table */ |
| 832 | u32 iZero; /* frame == (aHash[x]+iZero) */ |
| 833 | int iLimit = 0; /* Zero values greater than this */ |
| 834 | int nByte; /* Number of bytes to zero in aPgno[] */ |
| 835 | int i; /* Used to iterate through aHash[] */ |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 836 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 837 | assert( pWal->writeLock ); |
drh | 9c15647 | 2010-06-01 12:58:41 +0000 | [diff] [blame] | 838 | testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE-1 ); |
| 839 | testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE ); |
| 840 | testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE+1 ); |
drh | 9c15647 | 2010-06-01 12:58:41 +0000 | [diff] [blame] | 841 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 842 | if( pWal->hdr.mxFrame==0 ) return; |
| 843 | |
| 844 | /* Obtain pointers to the hash-table and page-number array containing |
| 845 | ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed |
| 846 | ** that the page said hash-table and array reside on is already mapped. |
| 847 | */ |
| 848 | assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) ); |
| 849 | assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] ); |
| 850 | walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero); |
| 851 | |
| 852 | /* Zero all hash-table entries that correspond to frame numbers greater |
| 853 | ** than pWal->hdr.mxFrame. |
| 854 | */ |
| 855 | iLimit = pWal->hdr.mxFrame - iZero; |
| 856 | assert( iLimit>0 ); |
| 857 | for(i=0; i<HASHTABLE_NSLOT; i++){ |
| 858 | if( aHash[i]>iLimit ){ |
| 859 | aHash[i] = 0; |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 860 | } |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 861 | } |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 862 | |
| 863 | /* Zero the entries in the aPgno array that correspond to frames with |
| 864 | ** frame numbers greater than pWal->hdr.mxFrame. |
| 865 | */ |
| 866 | nByte = ((char *)aHash - (char *)&aPgno[pWal->hdr.mxFrame+1]); |
| 867 | memset((void *)&aPgno[pWal->hdr.mxFrame+1], 0, nByte); |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 868 | |
| 869 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 870 | /* Verify that the every entry in the mapping region is still reachable |
| 871 | ** via the hash table even after the cleanup. |
| 872 | */ |
drh | f77bbd9 | 2010-06-01 13:17:44 +0000 | [diff] [blame] | 873 | if( iLimit ){ |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 874 | int i; /* Loop counter */ |
| 875 | int iKey; /* Hash key */ |
| 876 | for(i=1; i<=iLimit; i++){ |
| 877 | for(iKey=walHash(aPgno[i+iZero]); aHash[iKey]; iKey=walNextHash(iKey)){ |
| 878 | if( aHash[iKey]==i ) break; |
| 879 | } |
| 880 | assert( aHash[iKey]==i ); |
| 881 | } |
| 882 | } |
| 883 | #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ |
| 884 | } |
| 885 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 886 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 887 | /* |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 888 | ** Set an entry in the wal-index that will map database page number |
| 889 | ** pPage into WAL frame iFrame. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 890 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 891 | static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 892 | int rc; /* Return code */ |
| 893 | u32 iZero; /* One less than frame number of aPgno[1] */ |
| 894 | volatile u32 *aPgno; /* Page number array */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 895 | volatile ht_slot *aHash; /* Hash table */ |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 896 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 897 | rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero); |
| 898 | |
| 899 | /* Assuming the wal-index file was successfully mapped, populate the |
| 900 | ** page number array and hash table entry. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 901 | */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 902 | if( rc==SQLITE_OK ){ |
| 903 | int iKey; /* Hash table key */ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 904 | int idx; /* Value to write to hash-table slot */ |
| 905 | TESTONLY( int nCollide = 0; /* Number of hash collisions */ ) |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 906 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 907 | idx = iFrame - iZero; |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 908 | assert( idx <= HASHTABLE_NSLOT/2 + 1 ); |
| 909 | |
| 910 | /* If this is the first entry to be added to this hash-table, zero the |
| 911 | ** entire hash table and aPgno[] array before proceding. |
| 912 | */ |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 913 | if( idx==1 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 914 | int nByte = (u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1+iZero]; |
| 915 | memset((void*)&aPgno[1+iZero], 0, nByte); |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 916 | } |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 917 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 918 | /* If the entry in aPgno[] is already set, then the previous writer |
| 919 | ** must have exited unexpectedly in the middle of a transaction (after |
| 920 | ** writing one or more dirty pages to the WAL to free up memory). |
| 921 | ** Remove the remnants of that writers uncommitted transaction from |
| 922 | ** the hash-table before writing any new entries. |
| 923 | */ |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 924 | if( aPgno[iFrame] ){ |
dan | ca6b5ba | 2010-05-25 10:50:56 +0000 | [diff] [blame] | 925 | walCleanupHash(pWal); |
| 926 | assert( !aPgno[iFrame] ); |
| 927 | } |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 928 | |
| 929 | /* Write the aPgno[] array entry and the hash-table slot. */ |
dan | 6f15014 | 2010-05-21 15:31:56 +0000 | [diff] [blame] | 930 | for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){ |
drh | 29d4dbe | 2010-05-18 23:29:52 +0000 | [diff] [blame] | 931 | assert( nCollide++ < idx ); |
| 932 | } |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 933 | aPgno[iFrame] = iPage; |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 934 | aHash[iKey] = idx; |
drh | 4fa95bf | 2010-05-22 00:55:39 +0000 | [diff] [blame] | 935 | |
| 936 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 937 | /* Verify that the number of entries in the hash table exactly equals |
| 938 | ** the number of entries in the mapping region. |
| 939 | */ |
| 940 | { |
| 941 | int i; /* Loop counter */ |
| 942 | int nEntry = 0; /* Number of entries in the hash table */ |
| 943 | for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; } |
| 944 | assert( nEntry==idx ); |
| 945 | } |
| 946 | |
| 947 | /* Verify that the every entry in the mapping region is reachable |
| 948 | ** via the hash table. This turns out to be a really, really expensive |
| 949 | ** thing to check, so only do this occasionally - not on every |
| 950 | ** iteration. |
| 951 | */ |
| 952 | if( (idx&0x3ff)==0 ){ |
| 953 | int i; /* Loop counter */ |
| 954 | for(i=1; i<=idx; i++){ |
| 955 | for(iKey=walHash(aPgno[i+iZero]); aHash[iKey]; iKey=walNextHash(iKey)){ |
| 956 | if( aHash[iKey]==i ) break; |
| 957 | } |
| 958 | assert( aHash[iKey]==i ); |
| 959 | } |
| 960 | } |
| 961 | #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 962 | } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 963 | |
drh | 4fa95bf | 2010-05-22 00:55:39 +0000 | [diff] [blame] | 964 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 965 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | |
| 969 | /* |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 970 | ** Recover the wal-index by reading the write-ahead log file. |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 971 | ** |
| 972 | ** This routine first tries to establish an exclusive lock on the |
| 973 | ** wal-index to prevent other threads/processes from doing anything |
| 974 | ** with the WAL or wal-index while recovery is running. The |
| 975 | ** WAL_RECOVER_LOCK is also held so that other threads will know |
| 976 | ** that this thread is running recovery. If unable to establish |
| 977 | ** the necessary locks, this routine returns SQLITE_BUSY. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 978 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 979 | static int walIndexRecover(Wal *pWal){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 980 | int rc; /* Return Code */ |
| 981 | i64 nSize; /* Size of log file */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 982 | u32 aFrameCksum[2] = {0, 0}; |
dan | d0aa342 | 2010-05-31 16:41:53 +0000 | [diff] [blame] | 983 | int iLock; /* Lock offset to lock for checkpoint */ |
| 984 | int nLock; /* Number of locks to hold */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 985 | |
dan | d0aa342 | 2010-05-31 16:41:53 +0000 | [diff] [blame] | 986 | /* Obtain an exclusive lock on all byte in the locking range not already |
| 987 | ** locked by the caller. The caller is guaranteed to have locked the |
| 988 | ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte. |
| 989 | ** If successful, the same bytes that are locked here are unlocked before |
| 990 | ** this function returns. |
| 991 | */ |
| 992 | assert( pWal->ckptLock==1 || pWal->ckptLock==0 ); |
| 993 | assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 ); |
| 994 | assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE ); |
| 995 | assert( pWal->writeLock ); |
| 996 | iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock; |
| 997 | nLock = SQLITE_SHM_NLOCK - iLock; |
| 998 | rc = walLockExclusive(pWal, iLock, nLock); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 999 | if( rc ){ |
| 1000 | return rc; |
| 1001 | } |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 1002 | WALTRACE(("WAL%p: recovery begin...\n", pWal)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1003 | |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 1004 | memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1005 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1006 | rc = sqlite3OsFileSize(pWal->pWalFd, &nSize); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1007 | if( rc!=SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1008 | goto recovery_error; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1011 | if( nSize>WAL_HDRSIZE ){ |
| 1012 | u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1013 | u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ |
drh | 584c754 | 2010-05-19 18:08:10 +0000 | [diff] [blame] | 1014 | int szFrame; /* Number of bytes in buffer aFrame[] */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1015 | u8 *aData; /* Pointer to data part of aFrame buffer */ |
| 1016 | int iFrame; /* Index of last frame read */ |
| 1017 | i64 iOffset; /* Next offset to read from log file */ |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 1018 | int szPage; /* Page size according to the log */ |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1019 | u32 magic; /* Magic value read from WAL header */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1020 | |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1021 | /* Read in the WAL header. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1022 | rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1023 | if( rc!=SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1024 | goto recovery_error; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* If the database page size is not a power of two, or is greater than |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1028 | ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid |
| 1029 | ** data. Similarly, if the 'magic' value is invalid, ignore the whole |
| 1030 | ** WAL file. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1031 | */ |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1032 | magic = sqlite3Get4byte(&aBuf[0]); |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 1033 | szPage = sqlite3Get4byte(&aBuf[8]); |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1034 | if( (magic&0xFFFFFFFE)!=WAL_MAGIC |
| 1035 | || szPage&(szPage-1) |
| 1036 | || szPage>SQLITE_MAX_PAGE_SIZE |
| 1037 | || szPage<512 |
| 1038 | ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1039 | goto finished; |
| 1040 | } |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 1041 | pWal->hdr.bigEndCksum = (magic&0x00000001); |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1042 | pWal->szPage = szPage; |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 1043 | pWal->nCkpt = sqlite3Get4byte(&aBuf[12]); |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1044 | memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 1045 | walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, |
| 1046 | aBuf, WAL_HDRSIZE, 0, pWal->hdr.aFrameCksum |
| 1047 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1048 | |
| 1049 | /* Malloc a buffer to read frames into. */ |
drh | 584c754 | 2010-05-19 18:08:10 +0000 | [diff] [blame] | 1050 | szFrame = szPage + WAL_FRAME_HDRSIZE; |
| 1051 | aFrame = (u8 *)sqlite3_malloc(szFrame); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1052 | if( !aFrame ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1053 | rc = SQLITE_NOMEM; |
| 1054 | goto recovery_error; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1055 | } |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1056 | aData = &aFrame[WAL_FRAME_HDRSIZE]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1057 | |
| 1058 | /* Read all frames from the log file. */ |
| 1059 | iFrame = 0; |
drh | 584c754 | 2010-05-19 18:08:10 +0000 | [diff] [blame] | 1060 | for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1061 | u32 pgno; /* Database page number for frame */ |
| 1062 | u32 nTruncate; /* dbsize field from frame header */ |
| 1063 | int isValid; /* True if this frame is valid */ |
| 1064 | |
| 1065 | /* Read and decode the next log frame. */ |
drh | 584c754 | 2010-05-19 18:08:10 +0000 | [diff] [blame] | 1066 | rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1067 | if( rc!=SQLITE_OK ) break; |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1068 | isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1069 | if( !isValid ) break; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1070 | rc = walIndexAppend(pWal, ++iFrame, pgno); |
| 1071 | if( rc!=SQLITE_OK ) break; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1072 | |
| 1073 | /* If nTruncate is non-zero, this is a commit record. */ |
| 1074 | if( nTruncate ){ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 1075 | pWal->hdr.mxFrame = iFrame; |
| 1076 | pWal->hdr.nPage = nTruncate; |
| 1077 | pWal->hdr.szPage = szPage; |
| 1078 | aFrameCksum[0] = pWal->hdr.aFrameCksum[0]; |
| 1079 | aFrameCksum[1] = pWal->hdr.aFrameCksum[1]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | sqlite3_free(aFrame); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | finished: |
dan | 576bc32 | 2010-05-06 18:04:50 +0000 | [diff] [blame] | 1087 | if( rc==SQLITE_OK ){ |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1088 | volatile WalCkptInfo *pInfo; |
| 1089 | int i; |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 1090 | pWal->hdr.aFrameCksum[0] = aFrameCksum[0]; |
| 1091 | pWal->hdr.aFrameCksum[1] = aFrameCksum[1]; |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1092 | walIndexWriteHdr(pWal); |
dan | 3dee6da | 2010-05-31 16:17:54 +0000 | [diff] [blame] | 1093 | |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1094 | /* Reset the checkpoint-header. This is safe because this thread is |
dan | 3dee6da | 2010-05-31 16:17:54 +0000 | [diff] [blame] | 1095 | ** currently holding locks that exclude all other readers, writers and |
| 1096 | ** checkpointers. |
| 1097 | */ |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1098 | pInfo = walCkptInfo(pWal); |
| 1099 | pInfo->nBackfill = 0; |
| 1100 | pInfo->aReadMark[0] = 0; |
| 1101 | for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
dan | 576bc32 | 2010-05-06 18:04:50 +0000 | [diff] [blame] | 1102 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1103 | |
| 1104 | recovery_error: |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 1105 | WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok")); |
dan | d0aa342 | 2010-05-31 16:41:53 +0000 | [diff] [blame] | 1106 | walUnlockExclusive(pWal, iLock, nLock); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1107 | return rc; |
| 1108 | } |
| 1109 | |
drh | a8e654e | 2010-05-04 17:38:42 +0000 | [diff] [blame] | 1110 | /* |
dan | 1018e90 | 2010-05-05 15:33:05 +0000 | [diff] [blame] | 1111 | ** Close an open wal-index. |
drh | a8e654e | 2010-05-04 17:38:42 +0000 | [diff] [blame] | 1112 | */ |
dan | 1018e90 | 2010-05-05 15:33:05 +0000 | [diff] [blame] | 1113 | static void walIndexClose(Wal *pWal, int isDelete){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1114 | if( pWal->isWIndexOpen ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1115 | sqlite3OsShmClose(pWal->pDbFd, isDelete); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1116 | pWal->isWIndexOpen = 0; |
drh | a8e654e | 2010-05-04 17:38:42 +0000 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1120 | /* |
drh | 181e091 | 2010-06-01 01:08:08 +0000 | [diff] [blame] | 1121 | ** Open a connection to the WAL file associated with database zDbName. |
| 1122 | ** The database file must already be opened on connection pDbFd. |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 1123 | ** |
| 1124 | ** A SHARED lock should be held on the database file when this function |
| 1125 | ** is called. The purpose of this SHARED lock is to prevent any other |
drh | 181e091 | 2010-06-01 01:08:08 +0000 | [diff] [blame] | 1126 | ** client from unlinking the WAL or wal-index file. If another process |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 1127 | ** were to do this just after this client opened one of these files, the |
| 1128 | ** system would be badly broken. |
dan | ef37802 | 2010-05-04 11:06:03 +0000 | [diff] [blame] | 1129 | ** |
| 1130 | ** If the log file is successfully opened, SQLITE_OK is returned and |
| 1131 | ** *ppWal is set to point to a new WAL handle. If an error occurs, |
| 1132 | ** an SQLite error code is returned and *ppWal is left unmodified. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1133 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1134 | int sqlite3WalOpen( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1135 | sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1136 | sqlite3_file *pDbFd, /* The open database file */ |
| 1137 | const char *zDbName, /* Name of the database file */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1138 | Wal **ppWal /* OUT: Allocated Wal handle */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1139 | ){ |
dan | ef37802 | 2010-05-04 11:06:03 +0000 | [diff] [blame] | 1140 | int rc; /* Return Code */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1141 | Wal *pRet; /* Object to allocate and return */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1142 | int flags; /* Flags passed to OsOpen() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1143 | char *zWal; /* Name of write-ahead log file */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1144 | int nWal; /* Length of zWal in bytes */ |
| 1145 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1146 | assert( zDbName && zDbName[0] ); |
| 1147 | assert( pDbFd ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1148 | |
drh | 1b78eaf | 2010-05-25 13:40:03 +0000 | [diff] [blame] | 1149 | /* In the amalgamation, the os_unix.c and os_win.c source files come before |
| 1150 | ** this source file. Verify that the #defines of the locking byte offsets |
| 1151 | ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value. |
| 1152 | */ |
| 1153 | #ifdef WIN_SHM_BASE |
| 1154 | assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET ); |
| 1155 | #endif |
| 1156 | #ifdef UNIX_SHM_BASE |
| 1157 | assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET ); |
| 1158 | #endif |
| 1159 | |
| 1160 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1161 | /* Allocate an instance of struct Wal to return. */ |
| 1162 | *ppWal = 0; |
drh | 686138f | 2010-05-12 18:10:52 +0000 | [diff] [blame] | 1163 | nWal = sqlite3Strlen30(zDbName) + 5; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1164 | pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal); |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 1165 | if( !pRet ){ |
| 1166 | return SQLITE_NOMEM; |
| 1167 | } |
| 1168 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1169 | pRet->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1170 | pRet->pWalFd = (sqlite3_file *)&pRet[1]; |
| 1171 | pRet->pDbFd = pDbFd; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1172 | pRet->readLock = -1; |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1173 | sqlite3_randomness(8, &pRet->hdr.aSalt); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1174 | pRet->zWalName = zWal = pVfs->szOsFile + (char*)pRet->pWalFd; |
| 1175 | sqlite3_snprintf(nWal, zWal, "%s-wal", zDbName); |
| 1176 | rc = sqlite3OsShmOpen(pDbFd); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1177 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1178 | /* Open file handle on the write-ahead log file. */ |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 1179 | if( rc==SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1180 | pRet->isWIndexOpen = 1; |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 1181 | flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1182 | rc = sqlite3OsOpen(pVfs, zWal, pRet->pWalFd, flags, &flags); |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 1183 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1184 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1185 | if( rc!=SQLITE_OK ){ |
dan | 1018e90 | 2010-05-05 15:33:05 +0000 | [diff] [blame] | 1186 | walIndexClose(pRet, 0); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1187 | sqlite3OsClose(pRet->pWalFd); |
dan | ef37802 | 2010-05-04 11:06:03 +0000 | [diff] [blame] | 1188 | sqlite3_free(pRet); |
| 1189 | }else{ |
| 1190 | *ppWal = pRet; |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 1191 | WALTRACE(("WAL%d: opened\n", pRet)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1192 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1193 | return rc; |
| 1194 | } |
| 1195 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1196 | /* |
| 1197 | ** Find the smallest page number out of all pages held in the WAL that |
| 1198 | ** has not been returned by any prior invocation of this method on the |
| 1199 | ** same WalIterator object. Write into *piFrame the frame index where |
| 1200 | ** that page was last written into the WAL. Write into *piPage the page |
| 1201 | ** number. |
| 1202 | ** |
| 1203 | ** Return 0 on success. If there are no pages in the WAL with a page |
| 1204 | ** number larger than *piPage, then return 1. |
| 1205 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1206 | static int walIteratorNext( |
| 1207 | WalIterator *p, /* Iterator */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1208 | u32 *piPage, /* OUT: The page number of the next page */ |
| 1209 | u32 *piFrame /* OUT: Wal frame index of next page */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1210 | ){ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1211 | u32 iMin; /* Result pgno must be greater than iMin */ |
| 1212 | u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */ |
| 1213 | int i; /* For looping through segments */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1214 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1215 | iMin = p->iPrior; |
| 1216 | assert( iMin<0xffffffff ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1217 | for(i=p->nSegment-1; i>=0; i--){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1218 | struct WalSegment *pSegment = &p->aSegment[i]; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1219 | while( pSegment->iNext<pSegment->nEntry ){ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1220 | u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1221 | if( iPg>iMin ){ |
| 1222 | if( iPg<iRet ){ |
| 1223 | iRet = iPg; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1224 | *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext]; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1225 | } |
| 1226 | break; |
| 1227 | } |
| 1228 | pSegment->iNext++; |
| 1229 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1232 | *piPage = p->iPrior = iRet; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1233 | return (iRet==0xFFFFFFFF); |
| 1234 | } |
| 1235 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1236 | |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1237 | static void walMergesort( |
| 1238 | u32 *aContent, /* Pages in wal */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 1239 | ht_slot *aBuffer, /* Buffer of at least *pnList items to use */ |
| 1240 | ht_slot *aList, /* IN/OUT: List to sort */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1241 | int *pnList /* IN/OUT: Number of elements in aList[] */ |
| 1242 | ){ |
| 1243 | int nList = *pnList; |
| 1244 | if( nList>1 ){ |
| 1245 | int nLeft = nList / 2; /* Elements in left list */ |
| 1246 | int nRight = nList - nLeft; /* Elements in right list */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1247 | int iLeft = 0; /* Current index in aLeft */ |
| 1248 | int iRight = 0; /* Current index in aright */ |
| 1249 | int iOut = 0; /* Current index in output buffer */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 1250 | ht_slot *aLeft = aList; /* Left list */ |
| 1251 | ht_slot *aRight = aList+nLeft;/* Right list */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1252 | |
| 1253 | /* TODO: Change to non-recursive version. */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1254 | walMergesort(aContent, aBuffer, aLeft, &nLeft); |
| 1255 | walMergesort(aContent, aBuffer, aRight, &nRight); |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1256 | |
| 1257 | while( iRight<nRight || iLeft<nLeft ){ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 1258 | ht_slot logpage; |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1259 | Pgno dbpage; |
| 1260 | |
| 1261 | if( (iLeft<nLeft) |
| 1262 | && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]]) |
| 1263 | ){ |
| 1264 | logpage = aLeft[iLeft++]; |
| 1265 | }else{ |
| 1266 | logpage = aRight[iRight++]; |
| 1267 | } |
| 1268 | dbpage = aContent[logpage]; |
| 1269 | |
| 1270 | aBuffer[iOut++] = logpage; |
| 1271 | if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++; |
| 1272 | |
| 1273 | assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage ); |
| 1274 | assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage ); |
| 1275 | } |
| 1276 | memcpy(aList, aBuffer, sizeof(aList[0])*iOut); |
| 1277 | *pnList = iOut; |
| 1278 | } |
| 1279 | |
| 1280 | #ifdef SQLITE_DEBUG |
| 1281 | { |
| 1282 | int i; |
| 1283 | for(i=1; i<*pnList; i++){ |
| 1284 | assert( aContent[aList[i]] > aContent[aList[i-1]] ); |
| 1285 | } |
| 1286 | } |
| 1287 | #endif |
| 1288 | } |
| 1289 | |
dan | 5d65685 | 2010-06-14 07:53:26 +0000 | [diff] [blame] | 1290 | /* |
| 1291 | ** Free an iterator allocated by walIteratorInit(). |
| 1292 | */ |
| 1293 | static void walIteratorFree(WalIterator *p){ |
| 1294 | sqlite3_free(p); |
| 1295 | } |
| 1296 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1297 | /* |
| 1298 | ** Map the wal-index into memory owned by this thread, if it is not |
| 1299 | ** mapped already. Then construct a WalInterator object that can be |
| 1300 | ** used to loop over all pages in the WAL in ascending order. |
| 1301 | ** |
| 1302 | ** On success, make *pp point to the newly allocated WalInterator object |
| 1303 | ** return SQLITE_OK. Otherwise, leave *pp unchanged and return an error |
| 1304 | ** code. |
| 1305 | ** |
| 1306 | ** The calling routine should invoke walIteratorFree() to destroy the |
| 1307 | ** WalIterator object when it has finished with it. The caller must |
| 1308 | ** also unmap the wal-index. But the wal-index must not be unmapped |
| 1309 | ** prior to the WalIterator object being destroyed. |
| 1310 | */ |
| 1311 | static int walIteratorInit(Wal *pWal, WalIterator **pp){ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 1312 | WalIterator *p; /* Return value */ |
| 1313 | int nSegment; /* Number of segments to merge */ |
| 1314 | u32 iLast; /* Last frame in log */ |
| 1315 | int nByte; /* Number of bytes to allocate */ |
| 1316 | int i; /* Iterator variable */ |
| 1317 | ht_slot *aTmp; /* Temp space used by merge-sort */ |
| 1318 | ht_slot *aSpace; /* Space at the end of the allocation */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1319 | |
| 1320 | /* This routine only runs while holding SQLITE_SHM_CHECKPOINT. No other |
| 1321 | ** thread is able to write to shared memory while this routine is |
| 1322 | ** running (or, indeed, while the WalIterator object exists). Hence, |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1323 | ** we can cast off the volatile qualification from shared memory |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1324 | */ |
dan | 1beb939 | 2010-05-31 12:02:30 +0000 | [diff] [blame] | 1325 | assert( pWal->ckptLock ); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1326 | iLast = pWal->hdr.mxFrame; |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* Allocate space for the WalIterator object */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1329 | nSegment = walFramePage(iLast) + 1; |
| 1330 | nByte = sizeof(WalIterator) |
| 1331 | + nSegment*(sizeof(struct WalSegment)) |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 1332 | + (nSegment+1)*(HASHTABLE_NPAGE * sizeof(ht_slot)); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1333 | p = (WalIterator *)sqlite3_malloc(nByte); |
dan | 8f6097c | 2010-05-06 07:43:58 +0000 | [diff] [blame] | 1334 | if( !p ){ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1335 | return SQLITE_NOMEM; |
| 1336 | } |
| 1337 | memset(p, 0, nByte); |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 1338 | |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1339 | /* Allocate space for the WalIterator object */ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1340 | p->nSegment = nSegment; |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 1341 | aSpace = (ht_slot *)&p->aSegment[nSegment]; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1342 | aTmp = &aSpace[HASHTABLE_NPAGE*nSegment]; |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1343 | for(i=0; i<nSegment; i++){ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 1344 | volatile ht_slot *aHash; |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1345 | int j; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1346 | u32 iZero; |
| 1347 | int nEntry; |
| 1348 | volatile u32 *aPgno; |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1349 | int rc; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1350 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1351 | rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero); |
| 1352 | if( rc!=SQLITE_OK ){ |
dan | 5d65685 | 2010-06-14 07:53:26 +0000 | [diff] [blame] | 1353 | walIteratorFree(p); |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1354 | return rc; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1355 | } |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1356 | nEntry = ((i+1)==nSegment)?iLast-iZero:(u32 *)aHash-(u32 *)&aPgno[iZero+1]; |
| 1357 | |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1358 | iZero++; |
| 1359 | aPgno += iZero; |
| 1360 | |
| 1361 | for(j=0; j<nEntry; j++){ |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1362 | aSpace[j] = j; |
dan | 76ed3bc | 2010-05-03 17:18:24 +0000 | [diff] [blame] | 1363 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1364 | walMergesort((u32 *)aPgno, aTmp, aSpace, &nEntry); |
| 1365 | p->aSegment[i].iZero = iZero; |
| 1366 | p->aSegment[i].nEntry = nEntry; |
| 1367 | p->aSegment[i].aIndex = aSpace; |
| 1368 | p->aSegment[i].aPgno = (u32 *)aPgno; |
| 1369 | aSpace += HASHTABLE_NPAGE; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1370 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1371 | assert( aSpace==aTmp ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1372 | |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1373 | /* Return the fully initialized WalIterator object */ |
dan | 8f6097c | 2010-05-06 07:43:58 +0000 | [diff] [blame] | 1374 | *pp = p; |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1375 | return SQLITE_OK ; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1378 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1379 | ** Copy as much content as we can from the WAL back into the database file |
| 1380 | ** in response to an sqlite3_wal_checkpoint() request or the equivalent. |
| 1381 | ** |
| 1382 | ** The amount of information copies from WAL to database might be limited |
| 1383 | ** by active readers. This routine will never overwrite a database page |
| 1384 | ** that a concurrent reader might be using. |
| 1385 | ** |
| 1386 | ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when |
| 1387 | ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if |
| 1388 | ** checkpoints are always run by a background thread or background |
| 1389 | ** process, foreground threads will never block on a lengthy fsync call. |
| 1390 | ** |
| 1391 | ** Fsync is called on the WAL before writing content out of the WAL and |
| 1392 | ** into the database. This ensures that if the new content is persistent |
| 1393 | ** in the WAL and can be recovered following a power-loss or hard reset. |
| 1394 | ** |
| 1395 | ** Fsync is also called on the database file if (and only if) the entire |
| 1396 | ** WAL content is copied into the database file. This second fsync makes |
| 1397 | ** it safe to delete the WAL since the new content will persist in the |
| 1398 | ** database file. |
| 1399 | ** |
| 1400 | ** This routine uses and updates the nBackfill field of the wal-index header. |
| 1401 | ** This is the only routine tha will increase the value of nBackfill. |
| 1402 | ** (A WAL reset or recovery will revert nBackfill to zero, but not increase |
| 1403 | ** its value.) |
| 1404 | ** |
| 1405 | ** The caller must be holding sufficient locks to ensure that no other |
| 1406 | ** checkpoint is running (in any other thread or process) at the same |
| 1407 | ** time. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1408 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1409 | static int walCheckpoint( |
| 1410 | Wal *pWal, /* Wal connection */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1411 | int sync_flags, /* Flags for OsSync() (or 0) */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1412 | int nBuf, /* Size of zBuf in bytes */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1413 | u8 *zBuf /* Temporary buffer to use */ |
| 1414 | ){ |
| 1415 | int rc; /* Return code */ |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 1416 | int szPage = pWal->hdr.szPage; /* Database page-size */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1417 | WalIterator *pIter = 0; /* Wal iterator context */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1418 | u32 iDbpage = 0; /* Next database page to write */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1419 | u32 iFrame = 0; /* Wal frame containing data for iDbpage */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1420 | u32 mxSafeFrame; /* Max frame that can be backfilled */ |
| 1421 | int i; /* Loop counter */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1422 | volatile WalCkptInfo *pInfo; /* The checkpoint status information */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* Allocate the iterator */ |
dan | 8f6097c | 2010-05-06 07:43:58 +0000 | [diff] [blame] | 1425 | rc = walIteratorInit(pWal, &pIter); |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 1426 | if( rc!=SQLITE_OK || pWal->hdr.mxFrame==0 ){ |
dan | 83f42d1 | 2010-06-04 10:37:05 +0000 | [diff] [blame] | 1427 | goto walcheckpoint_out; |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1430 | /*** TODO: Move this test out to the caller. Make it an assert() here ***/ |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 1431 | if( pWal->hdr.szPage!=nBuf ){ |
dan | 83f42d1 | 2010-06-04 10:37:05 +0000 | [diff] [blame] | 1432 | rc = SQLITE_CORRUPT_BKPT; |
| 1433 | goto walcheckpoint_out; |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1436 | /* Compute in mxSafeFrame the index of the last frame of the WAL that is |
| 1437 | ** safe to write into the database. Frames beyond mxSafeFrame might |
| 1438 | ** overwrite database pages that are in use by active readers and thus |
| 1439 | ** cannot be backfilled from the WAL. |
| 1440 | */ |
dan | d54ff60 | 2010-05-31 11:16:30 +0000 | [diff] [blame] | 1441 | mxSafeFrame = pWal->hdr.mxFrame; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1442 | pInfo = walCkptInfo(pWal); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1443 | for(i=1; i<WAL_NREADER; i++){ |
| 1444 | u32 y = pInfo->aReadMark[i]; |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1445 | if( mxSafeFrame>=y ){ |
dan | 83f42d1 | 2010-06-04 10:37:05 +0000 | [diff] [blame] | 1446 | assert( y<=pWal->hdr.mxFrame ); |
| 1447 | rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 1448 | if( rc==SQLITE_OK ){ |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1449 | pInfo->aReadMark[i] = READMARK_NOT_USED; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1450 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
drh | 2d37e1c | 2010-06-02 20:38:20 +0000 | [diff] [blame] | 1451 | }else if( rc==SQLITE_BUSY ){ |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1452 | mxSafeFrame = y; |
drh | 2d37e1c | 2010-06-02 20:38:20 +0000 | [diff] [blame] | 1453 | }else{ |
dan | 83f42d1 | 2010-06-04 10:37:05 +0000 | [diff] [blame] | 1454 | goto walcheckpoint_out; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1455 | } |
| 1456 | } |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1457 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1458 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1459 | if( pInfo->nBackfill<mxSafeFrame |
| 1460 | && (rc = walLockExclusive(pWal, WAL_READ_LOCK(0), 1))==SQLITE_OK |
| 1461 | ){ |
| 1462 | u32 nBackfill = pInfo->nBackfill; |
| 1463 | |
| 1464 | /* Sync the WAL to disk */ |
| 1465 | if( sync_flags ){ |
| 1466 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
| 1467 | } |
| 1468 | |
| 1469 | /* Iterate through the contents of the WAL, copying data to the db file. */ |
| 1470 | while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1471 | assert( walFramePgno(pWal, iFrame)==iDbpage ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1472 | if( iFrame<=nBackfill || iFrame>mxSafeFrame ) continue; |
| 1473 | rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, |
| 1474 | walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE |
| 1475 | ); |
| 1476 | if( rc!=SQLITE_OK ) break; |
| 1477 | rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, (iDbpage-1)*szPage); |
| 1478 | if( rc!=SQLITE_OK ) break; |
| 1479 | } |
| 1480 | |
| 1481 | /* If work was actually accomplished... */ |
dan | d764c7d | 2010-06-04 11:56:22 +0000 | [diff] [blame] | 1482 | if( rc==SQLITE_OK ){ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1483 | if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1484 | rc = sqlite3OsTruncate(pWal->pDbFd, ((i64)pWal->hdr.nPage*(i64)szPage)); |
| 1485 | if( rc==SQLITE_OK && sync_flags ){ |
| 1486 | rc = sqlite3OsSync(pWal->pDbFd, sync_flags); |
| 1487 | } |
| 1488 | } |
dan | d764c7d | 2010-06-04 11:56:22 +0000 | [diff] [blame] | 1489 | if( rc==SQLITE_OK ){ |
| 1490 | pInfo->nBackfill = mxSafeFrame; |
| 1491 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /* Release the reader lock held while backfilling */ |
| 1495 | walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1); |
drh | 2d37e1c | 2010-06-02 20:38:20 +0000 | [diff] [blame] | 1496 | }else if( rc==SQLITE_BUSY ){ |
drh | 34116ea | 2010-05-31 12:30:52 +0000 | [diff] [blame] | 1497 | /* Reset the return code so as not to report a checkpoint failure |
| 1498 | ** just because active readers prevent any backfill. |
| 1499 | */ |
| 1500 | rc = SQLITE_OK; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
dan | 83f42d1 | 2010-06-04 10:37:05 +0000 | [diff] [blame] | 1503 | walcheckpoint_out: |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1504 | walIteratorFree(pIter); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1505 | return rc; |
| 1506 | } |
| 1507 | |
| 1508 | /* |
| 1509 | ** Close a connection to a log file. |
| 1510 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 1511 | int sqlite3WalClose( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1512 | Wal *pWal, /* Wal to close */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 1513 | int sync_flags, /* Flags to pass to OsSync() (or 0) */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1514 | int nBuf, |
| 1515 | u8 *zBuf /* Buffer of at least nBuf bytes */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1516 | ){ |
| 1517 | int rc = SQLITE_OK; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1518 | if( pWal ){ |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1519 | int isDelete = 0; /* True to unlink wal and wal-index files */ |
| 1520 | |
| 1521 | /* If an EXCLUSIVE lock can be obtained on the database file (using the |
| 1522 | ** ordinary, rollback-mode locking methods, this guarantees that the |
| 1523 | ** connection associated with this log file is the only connection to |
| 1524 | ** the database. In this case checkpoint the database and unlink both |
| 1525 | ** the wal and wal-index files. |
| 1526 | ** |
| 1527 | ** The EXCLUSIVE lock is not released before returning. |
| 1528 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1529 | rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1530 | if( rc==SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1531 | pWal->exclusiveMode = 1; |
dan | 1beb939 | 2010-05-31 12:02:30 +0000 | [diff] [blame] | 1532 | rc = sqlite3WalCheckpoint(pWal, sync_flags, nBuf, zBuf); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1533 | if( rc==SQLITE_OK ){ |
| 1534 | isDelete = 1; |
| 1535 | } |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
dan | 1018e90 | 2010-05-05 15:33:05 +0000 | [diff] [blame] | 1538 | walIndexClose(pWal, isDelete); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1539 | sqlite3OsClose(pWal->pWalFd); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1540 | if( isDelete ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1541 | sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0); |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 1542 | } |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 1543 | WALTRACE(("WAL%p: closed\n", pWal)); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1544 | sqlite3_free(pWal->apWiData); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1545 | sqlite3_free(pWal); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1546 | } |
| 1547 | return rc; |
| 1548 | } |
| 1549 | |
| 1550 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1551 | ** Try to read the wal-index header. Return 0 on success and 1 if |
| 1552 | ** there is a problem. |
| 1553 | ** |
| 1554 | ** The wal-index is in shared memory. Another thread or process might |
| 1555 | ** be writing the header at the same time this procedure is trying to |
| 1556 | ** read it, which might result in inconsistency. A dirty read is detected |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1557 | ** by verifying that both copies of the header are the same and also by |
| 1558 | ** a checksum on the header. |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1559 | ** |
| 1560 | ** If and only if the read is consistent and the header is different from |
| 1561 | ** pWal->hdr, then pWal->hdr is updated to the content of the new header |
| 1562 | ** and *pChanged is set to 1. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1563 | ** |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1564 | ** If the checksum cannot be verified return non-zero. If the header |
| 1565 | ** is read successfully and the checksum verified, return zero. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1566 | */ |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1567 | int walIndexTryHdr(Wal *pWal, int *pChanged){ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1568 | u32 aCksum[2]; /* Checksum on the header content */ |
| 1569 | WalIndexHdr h1, h2; /* Two copies of the header content */ |
| 1570 | WalIndexHdr volatile *aHdr; /* Header in shared memory */ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1571 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1572 | /* The first page of the wal-index must be mapped at this point. */ |
| 1573 | assert( pWal->nWiData>0 && pWal->apWiData[0] ); |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 1574 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1575 | /* Read the header. This might happen currently with a write to the |
| 1576 | ** same area of shared memory on a different CPU in a SMP, |
| 1577 | ** meaning it is possible that an inconsistent snapshot is read |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1578 | ** from the file. If this happens, return non-zero. |
drh | f0b20f8 | 2010-05-21 13:16:18 +0000 | [diff] [blame] | 1579 | ** |
| 1580 | ** There are two copies of the header at the beginning of the wal-index. |
| 1581 | ** When reading, read [0] first then [1]. Writes are in the reverse order. |
| 1582 | ** Memory barriers are used to prevent the compiler or the hardware from |
| 1583 | ** reordering the reads and writes. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1584 | */ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1585 | aHdr = walIndexHdr(pWal); |
| 1586 | memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 1587 | sqlite3OsShmBarrier(pWal->pDbFd); |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1588 | memcpy(&h2, (void *)&aHdr[1], sizeof(h2)); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 1589 | |
drh | f0b20f8 | 2010-05-21 13:16:18 +0000 | [diff] [blame] | 1590 | if( memcmp(&h1, &h2, sizeof(h1))!=0 ){ |
| 1591 | return 1; /* Dirty read */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 1592 | } |
drh | 4b82c38 | 2010-05-31 18:24:19 +0000 | [diff] [blame] | 1593 | if( h1.isInit==0 ){ |
drh | f0b20f8 | 2010-05-21 13:16:18 +0000 | [diff] [blame] | 1594 | return 1; /* Malformed header - probably all zeros */ |
| 1595 | } |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1596 | walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum); |
drh | f0b20f8 | 2010-05-21 13:16:18 +0000 | [diff] [blame] | 1597 | if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){ |
| 1598 | return 1; /* Checksum does not match */ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
drh | f0b20f8 | 2010-05-21 13:16:18 +0000 | [diff] [blame] | 1601 | if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){ |
dan | a861469 | 2010-05-06 14:42:34 +0000 | [diff] [blame] | 1602 | *pChanged = 1; |
drh | f0b20f8 | 2010-05-21 13:16:18 +0000 | [diff] [blame] | 1603 | memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr)); |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1604 | pWal->szPage = pWal->hdr.szPage; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1605 | } |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1606 | |
| 1607 | /* The header was successfully read. Return zero. */ |
| 1608 | return 0; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /* |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 1612 | ** Read the wal-index header from the wal-index and into pWal->hdr. |
| 1613 | ** If the wal-header appears to be corrupt, try to recover the log |
| 1614 | ** before returning. |
| 1615 | ** |
| 1616 | ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is |
| 1617 | ** changed by this opertion. If pWal->hdr is unchanged, set *pChanged |
| 1618 | ** to 0. |
| 1619 | ** |
| 1620 | ** This routine also maps the wal-index content into memory and assigns |
| 1621 | ** ownership of that mapping to the current thread. In some implementations, |
| 1622 | ** only one thread at a time can hold a mapping of the wal-index. Hence, |
| 1623 | ** the caller should strive to invoke walIndexUnmap() as soon as possible |
| 1624 | ** after this routine returns. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1625 | ** |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1626 | ** If the wal-index header is successfully read, return SQLITE_OK. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1627 | ** Otherwise an SQLite error code. |
| 1628 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1629 | static int walIndexReadHdr(Wal *pWal, int *pChanged){ |
dan | 8467050 | 2010-05-07 05:46:23 +0000 | [diff] [blame] | 1630 | int rc; /* Return code */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1631 | int badHdr; /* True if a header read failed */ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1632 | volatile u32 *page0; |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1633 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1634 | /* Ensure that page 0 of the wal-index (the page that contains the |
| 1635 | ** wal-index header) is mapped. Return early if an error occurs here. |
| 1636 | */ |
dan | a861469 | 2010-05-06 14:42:34 +0000 | [diff] [blame] | 1637 | assert( pChanged ); |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1638 | rc = walIndexPage(pWal, 0, &page0); |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 1639 | if( rc!=SQLITE_OK ){ |
| 1640 | return rc; |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1641 | }; |
| 1642 | assert( page0 || pWal->writeLock==0 ); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1643 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1644 | /* If the first page of the wal-index has been mapped, try to read the |
| 1645 | ** wal-index header immediately, without holding any lock. This usually |
| 1646 | ** works, but may fail if the wal-index header is corrupt or currently |
| 1647 | ** being modified by another user. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1648 | */ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1649 | badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1); |
drh | bab7b91 | 2010-05-26 17:31:58 +0000 | [diff] [blame] | 1650 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1651 | /* If the first attempt failed, it might have been due to a race |
| 1652 | ** with a writer. So get a WRITE lock and try again. |
| 1653 | */ |
dan | d54ff60 | 2010-05-31 11:16:30 +0000 | [diff] [blame] | 1654 | assert( badHdr==0 || pWal->writeLock==0 ); |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1655 | if( badHdr && SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){ |
| 1656 | pWal->writeLock = 1; |
| 1657 | if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1658 | badHdr = walIndexTryHdr(pWal, pChanged); |
| 1659 | if( badHdr ){ |
| 1660 | /* If the wal-index header is still malformed even while holding |
| 1661 | ** a WRITE lock, it can only mean that the header is corrupted and |
| 1662 | ** needs to be reconstructed. So run recovery to do exactly that. |
| 1663 | */ |
drh | bab7b91 | 2010-05-26 17:31:58 +0000 | [diff] [blame] | 1664 | rc = walIndexRecover(pWal); |
dan | 3dee6da | 2010-05-31 16:17:54 +0000 | [diff] [blame] | 1665 | *pChanged = 1; |
drh | bab7b91 | 2010-05-26 17:31:58 +0000 | [diff] [blame] | 1666 | } |
drh | bab7b91 | 2010-05-26 17:31:58 +0000 | [diff] [blame] | 1667 | } |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1668 | pWal->writeLock = 0; |
| 1669 | walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 1672 | return rc; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1676 | ** This is the value that walTryBeginRead returns when it needs to |
| 1677 | ** be retried. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1678 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1679 | #define WAL_RETRY (-1) |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1680 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1681 | /* |
| 1682 | ** Attempt to start a read transaction. This might fail due to a race or |
| 1683 | ** other transient condition. When that happens, it returns WAL_RETRY to |
| 1684 | ** indicate to the caller that it is safe to retry immediately. |
| 1685 | ** |
| 1686 | ** On success return SQLITE_OK. On a permantent failure (such an |
| 1687 | ** I/O error or an SQLITE_BUSY because another process is running |
| 1688 | ** recovery) return a positive error code. |
| 1689 | ** |
| 1690 | ** On success, this routine obtains a read lock on |
| 1691 | ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is |
| 1692 | ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1) |
| 1693 | ** that means the Wal does not hold any read lock. The reader must not |
| 1694 | ** access any database page that is modified by a WAL frame up to and |
| 1695 | ** including frame number aReadMark[pWal->readLock]. The reader will |
| 1696 | ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0 |
| 1697 | ** Or if pWal->readLock==0, then the reader will ignore the WAL |
| 1698 | ** completely and get all content directly from the database file. |
| 1699 | ** When the read transaction is completed, the caller must release the |
| 1700 | ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1. |
| 1701 | ** |
| 1702 | ** This routine uses the nBackfill and aReadMark[] fields of the header |
| 1703 | ** to select a particular WAL_READ_LOCK() that strives to let the |
| 1704 | ** checkpoint process do as much work as possible. This routine might |
| 1705 | ** update values of the aReadMark[] array in the header, but if it does |
| 1706 | ** so it takes care to hold an exclusive lock on the corresponding |
| 1707 | ** WAL_READ_LOCK() while changing values. |
| 1708 | */ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 1709 | static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1710 | volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ |
| 1711 | u32 mxReadMark; /* Largest aReadMark[] value */ |
| 1712 | int mxI; /* Index of largest aReadMark[] value */ |
| 1713 | int i; /* Loop counter */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1714 | int rc = SQLITE_OK; /* Return code */ |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1715 | |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 1716 | assert( pWal->readLock<0 ); /* Not currently locked */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1717 | |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 1718 | /* Take steps to avoid spinning forever if there is a protocol error. */ |
| 1719 | if( cnt>5 ){ |
| 1720 | if( cnt>100 ) return SQLITE_PROTOCOL; |
| 1721 | sqlite3OsSleep(pWal->pVfs, 1); |
| 1722 | } |
| 1723 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1724 | if( !useWal ){ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1725 | rc = walIndexReadHdr(pWal, pChanged); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1726 | if( rc==SQLITE_BUSY ){ |
| 1727 | /* If there is not a recovery running in another thread or process |
| 1728 | ** then convert BUSY errors to WAL_RETRY. If recovery is known to |
| 1729 | ** be running, convert BUSY to BUSY_RECOVERY. There is a race here |
| 1730 | ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY |
| 1731 | ** would be technically correct. But the race is benign since with |
| 1732 | ** WAL_RETRY this routine will be called again and will probably be |
| 1733 | ** right on the second iteration. |
| 1734 | */ |
| 1735 | rc = walLockShared(pWal, WAL_RECOVER_LOCK); |
| 1736 | if( rc==SQLITE_OK ){ |
| 1737 | walUnlockShared(pWal, WAL_RECOVER_LOCK); |
| 1738 | rc = WAL_RETRY; |
| 1739 | }else if( rc==SQLITE_BUSY ){ |
| 1740 | rc = SQLITE_BUSY_RECOVERY; |
| 1741 | } |
| 1742 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1743 | } |
| 1744 | if( rc!=SQLITE_OK ){ |
| 1745 | return rc; |
| 1746 | } |
| 1747 | |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1748 | pInfo = walCkptInfo(pWal); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1749 | if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){ |
| 1750 | /* The WAL has been completely backfilled (or it is empty). |
| 1751 | ** and can be safely ignored. |
| 1752 | */ |
| 1753 | rc = walLockShared(pWal, WAL_READ_LOCK(0)); |
dan | eb8cb3a | 2010-06-05 18:34:26 +0000 | [diff] [blame] | 1754 | sqlite3OsShmBarrier(pWal->pDbFd); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1755 | if( rc==SQLITE_OK ){ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1756 | if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ |
dan | 493cc59 | 2010-06-05 18:12:23 +0000 | [diff] [blame] | 1757 | /* It is not safe to allow the reader to continue here if frames |
| 1758 | ** may have been appended to the log before READ_LOCK(0) was obtained. |
| 1759 | ** When holding READ_LOCK(0), the reader ignores the entire log file, |
| 1760 | ** which implies that the database file contains a trustworthy |
| 1761 | ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from |
| 1762 | ** happening, this is usually correct. |
| 1763 | ** |
| 1764 | ** However, if frames have been appended to the log (or if the log |
| 1765 | ** is wrapped and written for that matter) before the READ_LOCK(0) |
| 1766 | ** is obtained, that is not necessarily true. A checkpointer may |
| 1767 | ** have started to backfill the appended frames but crashed before |
| 1768 | ** it finished. Leaving a corrupt image in the database file. |
| 1769 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1770 | walUnlockShared(pWal, WAL_READ_LOCK(0)); |
| 1771 | return WAL_RETRY; |
| 1772 | } |
| 1773 | pWal->readLock = 0; |
| 1774 | return SQLITE_OK; |
| 1775 | }else if( rc!=SQLITE_BUSY ){ |
| 1776 | return rc; |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 1777 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1778 | } |
dan | ba51590 | 2010-04-30 09:32:06 +0000 | [diff] [blame] | 1779 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1780 | /* If we get this far, it means that the reader will want to use |
| 1781 | ** the WAL to get at content from recent commits. The job now is |
| 1782 | ** to select one of the aReadMark[] entries that is closest to |
| 1783 | ** but not exceeding pWal->hdr.mxFrame and lock that entry. |
| 1784 | */ |
| 1785 | mxReadMark = 0; |
| 1786 | mxI = 0; |
| 1787 | for(i=1; i<WAL_NREADER; i++){ |
| 1788 | u32 thisMark = pInfo->aReadMark[i]; |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1789 | if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){ |
| 1790 | assert( thisMark!=READMARK_NOT_USED ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1791 | mxReadMark = thisMark; |
| 1792 | mxI = i; |
| 1793 | } |
| 1794 | } |
| 1795 | if( mxI==0 ){ |
| 1796 | /* If we get here, it means that all of the aReadMark[] entries between |
| 1797 | ** 1 and WAL_NREADER-1 are zero. Try to initialize aReadMark[1] to |
| 1798 | ** be mxFrame, then retry. |
| 1799 | */ |
| 1800 | rc = walLockExclusive(pWal, WAL_READ_LOCK(1), 1); |
| 1801 | if( rc==SQLITE_OK ){ |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1802 | pInfo->aReadMark[1] = pWal->hdr.mxFrame; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1803 | walUnlockExclusive(pWal, WAL_READ_LOCK(1), 1); |
drh | 38933f2 | 2010-06-02 15:43:18 +0000 | [diff] [blame] | 1804 | rc = WAL_RETRY; |
| 1805 | }else if( rc==SQLITE_BUSY ){ |
| 1806 | rc = WAL_RETRY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1807 | } |
drh | 38933f2 | 2010-06-02 15:43:18 +0000 | [diff] [blame] | 1808 | return rc; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1809 | }else{ |
| 1810 | if( mxReadMark < pWal->hdr.mxFrame ){ |
dan | d54ff60 | 2010-05-31 11:16:30 +0000 | [diff] [blame] | 1811 | for(i=1; i<WAL_NREADER; i++){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1812 | rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 1813 | if( rc==SQLITE_OK ){ |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1814 | mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1815 | mxI = i; |
| 1816 | walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); |
| 1817 | break; |
drh | 38933f2 | 2010-06-02 15:43:18 +0000 | [diff] [blame] | 1818 | }else if( rc!=SQLITE_BUSY ){ |
| 1819 | return rc; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); |
| 1825 | if( rc ){ |
| 1826 | return rc==SQLITE_BUSY ? WAL_RETRY : rc; |
| 1827 | } |
dan | eb8cb3a | 2010-06-05 18:34:26 +0000 | [diff] [blame] | 1828 | /* Now that the read-lock has been obtained, check that neither the |
| 1829 | ** value in the aReadMark[] array or the contents of the wal-index |
| 1830 | ** header have changed. |
| 1831 | ** |
| 1832 | ** It is necessary to check that the wal-index header did not change |
| 1833 | ** between the time it was read and when the shared-lock was obtained |
| 1834 | ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility |
| 1835 | ** that the log file may have been wrapped by a writer, or that frames |
| 1836 | ** that occur later in the log than pWal->hdr.mxFrame may have been |
| 1837 | ** copied into the database by a checkpointer. If either of these things |
| 1838 | ** happened, then reading the database with the current value of |
| 1839 | ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry |
| 1840 | ** instead. |
| 1841 | ** |
dan | 640aac4 | 2010-06-05 19:18:59 +0000 | [diff] [blame] | 1842 | ** This does not guarantee that the copy of the wal-index header is up to |
| 1843 | ** date before proceeding. That would not be possible without somehow |
| 1844 | ** blocking writers. It only guarantees that a dangerous checkpoint or |
dan | eb8cb3a | 2010-06-05 18:34:26 +0000 | [diff] [blame] | 1845 | ** log-wrap (either of which would require an exclusive lock on |
| 1846 | ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid. |
| 1847 | */ |
| 1848 | sqlite3OsShmBarrier(pWal->pDbFd); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1849 | if( pInfo->aReadMark[mxI]!=mxReadMark |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1850 | || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1851 | ){ |
| 1852 | walUnlockShared(pWal, WAL_READ_LOCK(mxI)); |
| 1853 | return WAL_RETRY; |
| 1854 | }else{ |
drh | db7f647 | 2010-06-09 14:45:12 +0000 | [diff] [blame] | 1855 | assert( mxReadMark<=pWal->hdr.mxFrame ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1856 | pWal->readLock = mxI; |
| 1857 | } |
| 1858 | } |
| 1859 | return rc; |
| 1860 | } |
| 1861 | |
| 1862 | /* |
| 1863 | ** Begin a read transaction on the database. |
| 1864 | ** |
| 1865 | ** This routine used to be called sqlite3OpenSnapshot() and with good reason: |
| 1866 | ** it takes a snapshot of the state of the WAL and wal-index for the current |
| 1867 | ** instant in time. The current thread will continue to use this snapshot. |
| 1868 | ** Other threads might append new content to the WAL and wal-index but |
| 1869 | ** that extra content is ignored by the current thread. |
| 1870 | ** |
| 1871 | ** If the database contents have changes since the previous read |
| 1872 | ** transaction, then *pChanged is set to 1 before returning. The |
| 1873 | ** Pager layer will use this to know that is cache is stale and |
| 1874 | ** needs to be flushed. |
| 1875 | */ |
| 1876 | int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ |
| 1877 | int rc; /* Return code */ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 1878 | int cnt = 0; /* Number of TryBeginRead attempts */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1879 | |
| 1880 | do{ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 1881 | rc = walTryBeginRead(pWal, pChanged, 0, ++cnt); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1882 | }while( rc==WAL_RETRY ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1883 | return rc; |
| 1884 | } |
| 1885 | |
| 1886 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1887 | ** Finish with a read transaction. All this does is release the |
| 1888 | ** read-lock. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1889 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1890 | void sqlite3WalEndReadTransaction(Wal *pWal){ |
| 1891 | if( pWal->readLock>=0 ){ |
| 1892 | walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); |
| 1893 | pWal->readLock = -1; |
| 1894 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 1897 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1898 | ** Read a page from the WAL, if it is present in the WAL and if the |
| 1899 | ** current read transaction is configured to use the WAL. |
| 1900 | ** |
| 1901 | ** The *pInWal is set to 1 if the requested page is in the WAL and |
| 1902 | ** has been loaded. Or *pInWal is set to 0 if the page was not in |
| 1903 | ** the WAL and needs to be read out of the database. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1904 | */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1905 | int sqlite3WalRead( |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1906 | Wal *pWal, /* WAL handle */ |
| 1907 | Pgno pgno, /* Database page number to read data for */ |
| 1908 | int *pInWal, /* OUT: True if data is read from WAL */ |
| 1909 | int nOut, /* Size of buffer pOut in bytes */ |
| 1910 | u8 *pOut /* Buffer to write page data to */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1911 | ){ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1912 | u32 iRead = 0; /* If !=0, WAL frame to return data from */ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 1913 | u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1914 | int iHash; /* Used to loop through N hash tables */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1915 | |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 1916 | /* This routine is only be called from within a read transaction. */ |
| 1917 | assert( pWal->readLock>=0 || pWal->lockError ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1918 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1919 | /* If the "last page" field of the wal-index header snapshot is 0, then |
| 1920 | ** no data will be read from the wal under any circumstances. Return early |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1921 | ** in this case to avoid the walIndexMap/Unmap overhead. Likewise, if |
| 1922 | ** pWal->readLock==0, then the WAL is ignored by the reader so |
| 1923 | ** return early, as if the WAL were empty. |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1924 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 1925 | if( iLast==0 || pWal->readLock==0 ){ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1926 | *pInWal = 0; |
| 1927 | return SQLITE_OK; |
| 1928 | } |
| 1929 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1930 | /* Search the hash table or tables for an entry matching page number |
| 1931 | ** pgno. Each iteration of the following for() loop searches one |
| 1932 | ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames). |
| 1933 | ** |
| 1934 | ** This code may run concurrently to the code in walIndexAppend() |
| 1935 | ** that adds entries to the wal-index (and possibly to this hash |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 1936 | ** table). This means the value just read from the hash |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1937 | ** slot (aHash[iKey]) may have been added before or after the |
| 1938 | ** current read transaction was opened. Values added after the |
| 1939 | ** read transaction was opened may have been written incorrectly - |
| 1940 | ** i.e. these slots may contain garbage data. However, we assume |
| 1941 | ** that any slots written before the current read transaction was |
| 1942 | ** opened remain unmodified. |
| 1943 | ** |
| 1944 | ** For the reasons above, the if(...) condition featured in the inner |
| 1945 | ** loop of the following block is more stringent that would be required |
| 1946 | ** if we had exclusive access to the hash-table: |
| 1947 | ** |
| 1948 | ** (aPgno[iFrame]==pgno): |
| 1949 | ** This condition filters out normal hash-table collisions. |
| 1950 | ** |
| 1951 | ** (iFrame<=iLast): |
| 1952 | ** This condition filters out entries that were added to the hash |
| 1953 | ** table after the current read-transaction had started. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1954 | */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1955 | for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 1956 | volatile ht_slot *aHash; /* Pointer to hash table */ |
| 1957 | volatile u32 *aPgno; /* Pointer to array of page numbers */ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1958 | u32 iZero; /* Frame number corresponding to aPgno[0] */ |
| 1959 | int iKey; /* Hash slot index */ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1960 | int rc; |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1961 | |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 1962 | rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero); |
| 1963 | if( rc!=SQLITE_OK ){ |
| 1964 | return rc; |
| 1965 | } |
dan | 6f15014 | 2010-05-21 15:31:56 +0000 | [diff] [blame] | 1966 | for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1967 | u32 iFrame = aHash[iKey] + iZero; |
dan | 493cc59 | 2010-06-05 18:12:23 +0000 | [diff] [blame] | 1968 | if( iFrame<=iLast && aPgno[iFrame]==pgno ){ |
| 1969 | assert( iFrame>iRead ); |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1970 | iRead = iFrame; |
| 1971 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1974 | |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1975 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 1976 | /* If expensive assert() statements are available, do a linear search |
| 1977 | ** of the wal-index file content. Make sure the results agree with the |
| 1978 | ** result obtained using the hash indexes above. */ |
| 1979 | { |
| 1980 | u32 iRead2 = 0; |
| 1981 | u32 iTest; |
| 1982 | for(iTest=iLast; iTest>0; iTest--){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 1983 | if( walFramePgno(pWal, iTest)==pgno ){ |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1984 | iRead2 = iTest; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1985 | break; |
| 1986 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1987 | } |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1988 | assert( iRead==iRead2 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1989 | } |
dan | bb23aff | 2010-05-10 14:46:09 +0000 | [diff] [blame] | 1990 | #endif |
dan | cd11fb2 | 2010-04-26 10:40:52 +0000 | [diff] [blame] | 1991 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1992 | /* If iRead is non-zero, then it is the log frame number that contains the |
| 1993 | ** required page. Read and return data from the log file. |
| 1994 | */ |
| 1995 | if( iRead ){ |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 1996 | i64 iOffset = walFrameOffset(iRead, pWal->hdr.szPage) + WAL_FRAME_HDRSIZE; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1997 | *pInWal = 1; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 1998 | return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2001 | *pInWal = 0; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2002 | return SQLITE_OK; |
| 2003 | } |
| 2004 | |
| 2005 | |
| 2006 | /* |
| 2007 | ** Set *pPgno to the size of the database file (or zero, if unknown). |
| 2008 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2009 | void sqlite3WalDbsize(Wal *pWal, Pgno *pPgno){ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 2010 | assert( pWal->readLock>=0 || pWal->lockError ); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2011 | *pPgno = pWal->hdr.nPage; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
dan | 30c8629 | 2010-04-30 16:24:46 +0000 | [diff] [blame] | 2014 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2015 | /* |
| 2016 | ** This function starts a write transaction on the WAL. |
| 2017 | ** |
| 2018 | ** A read transaction must have already been started by a prior call |
| 2019 | ** to sqlite3WalBeginReadTransaction(). |
| 2020 | ** |
| 2021 | ** If another thread or process has written into the database since |
| 2022 | ** the read transaction was started, then it is not possible for this |
| 2023 | ** thread to write as doing so would cause a fork. So this routine |
| 2024 | ** returns SQLITE_BUSY in that case and no write transaction is started. |
| 2025 | ** |
| 2026 | ** There can only be a single writer active at a time. |
| 2027 | */ |
| 2028 | int sqlite3WalBeginWriteTransaction(Wal *pWal){ |
| 2029 | int rc; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2030 | |
| 2031 | /* Cannot start a write transaction without first holding a read |
| 2032 | ** transaction. */ |
| 2033 | assert( pWal->readLock>=0 ); |
| 2034 | |
| 2035 | /* Only one writer allowed at a time. Get the write lock. Return |
| 2036 | ** SQLITE_BUSY if unable. |
| 2037 | */ |
| 2038 | rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1); |
| 2039 | if( rc ){ |
| 2040 | return rc; |
| 2041 | } |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 2042 | pWal->writeLock = 1; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2043 | |
| 2044 | /* If another connection has written to the database file since the |
| 2045 | ** time the read transaction on this connection was started, then |
| 2046 | ** the write is disallowed. |
| 2047 | */ |
dan | 4280eb3 | 2010-06-12 12:02:35 +0000 | [diff] [blame] | 2048 | if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2049 | walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 2050 | pWal->writeLock = 0; |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2051 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2054 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 2057 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2058 | ** End a write transaction. The commit has already been done. This |
| 2059 | ** routine merely releases the lock. |
| 2060 | */ |
| 2061 | int sqlite3WalEndWriteTransaction(Wal *pWal){ |
| 2062 | walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); |
dan | d54ff60 | 2010-05-31 11:16:30 +0000 | [diff] [blame] | 2063 | pWal->writeLock = 0; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2064 | return SQLITE_OK; |
| 2065 | } |
| 2066 | |
| 2067 | /* |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 2068 | ** If any data has been written (but not committed) to the log file, this |
| 2069 | ** function moves the write-pointer back to the start of the transaction. |
| 2070 | ** |
| 2071 | ** Additionally, the callback function is invoked for each frame written |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2072 | ** to the WAL since the start of the transaction. If the callback returns |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 2073 | ** other than SQLITE_OK, it is not invoked again and the error code is |
| 2074 | ** returned to the caller. |
| 2075 | ** |
| 2076 | ** Otherwise, if the callback function does not return an error, this |
| 2077 | ** function returns SQLITE_OK. |
| 2078 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2079 | int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 2080 | int rc = SQLITE_OK; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2081 | if( pWal->writeLock ){ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 2082 | Pgno iMax = pWal->hdr.mxFrame; |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 2083 | Pgno iFrame; |
| 2084 | |
dan | 5d65685 | 2010-06-14 07:53:26 +0000 | [diff] [blame] | 2085 | /* Restore the clients cache of the wal-index header to the state it |
| 2086 | ** was in before the client began writing to the database. |
| 2087 | */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame^] | 2088 | memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr)); |
dan | 5d65685 | 2010-06-14 07:53:26 +0000 | [diff] [blame] | 2089 | |
| 2090 | for(iFrame=pWal->hdr.mxFrame+1; |
| 2091 | ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; |
| 2092 | iFrame++ |
| 2093 | ){ |
| 2094 | /* This call cannot fail. Unless the page for which the page number |
| 2095 | ** is passed as the second argument is (a) in the cache and |
| 2096 | ** (b) has an outstanding reference, then xUndo is either a no-op |
| 2097 | ** (if (a) is false) or simply expels the page from the cache (if (b) |
| 2098 | ** is false). |
| 2099 | ** |
| 2100 | ** If the upper layer is doing a rollback, it is guaranteed that there |
| 2101 | ** are no outstanding references to any page other than page 1. And |
| 2102 | ** page 1 is never written to the log until the transaction is |
| 2103 | ** committed. As a result, the call to xUndo may not fail. |
| 2104 | */ |
dan | 5d65685 | 2010-06-14 07:53:26 +0000 | [diff] [blame] | 2105 | assert( walFramePgno(pWal, iFrame)!=1 ); |
| 2106 | rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame)); |
dan | 6f15014 | 2010-05-21 15:31:56 +0000 | [diff] [blame] | 2107 | } |
dan | 5d65685 | 2010-06-14 07:53:26 +0000 | [diff] [blame] | 2108 | walCleanupHash(pWal); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 2109 | } |
dan | 5d65685 | 2010-06-14 07:53:26 +0000 | [diff] [blame] | 2110 | assert( rc==SQLITE_OK ); |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 2111 | return rc; |
| 2112 | } |
| 2113 | |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 2114 | /* |
| 2115 | ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 |
| 2116 | ** values. This function populates the array with values required to |
| 2117 | ** "rollback" the write position of the WAL handle back to the current |
| 2118 | ** point in the event of a savepoint rollback (via WalSavepointUndo()). |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2119 | */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 2120 | void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2121 | assert( pWal->writeLock ); |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 2122 | aWalData[0] = pWal->hdr.mxFrame; |
| 2123 | aWalData[1] = pWal->hdr.aFrameCksum[0]; |
| 2124 | aWalData[2] = pWal->hdr.aFrameCksum[1]; |
dan | 6e6bd56 | 2010-06-02 18:59:03 +0000 | [diff] [blame] | 2125 | aWalData[3] = pWal->nCkpt; |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 2128 | /* |
| 2129 | ** Move the write position of the WAL back to the point identified by |
| 2130 | ** the values in the aWalData[] array. aWalData must point to an array |
| 2131 | ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated |
| 2132 | ** by a call to WalSavepoint(). |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2133 | */ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 2134 | int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){ |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 2135 | int rc = SQLITE_OK; |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 2136 | |
dan | 6e6bd56 | 2010-06-02 18:59:03 +0000 | [diff] [blame] | 2137 | assert( pWal->writeLock ); |
| 2138 | assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame ); |
| 2139 | |
| 2140 | if( aWalData[3]!=pWal->nCkpt ){ |
| 2141 | /* This savepoint was opened immediately after the write-transaction |
| 2142 | ** was started. Right after that, the writer decided to wrap around |
| 2143 | ** to the start of the log. Update the savepoint values to match. |
| 2144 | */ |
| 2145 | aWalData[0] = 0; |
| 2146 | aWalData[3] = pWal->nCkpt; |
| 2147 | } |
| 2148 | |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 2149 | if( aWalData[0]<pWal->hdr.mxFrame ){ |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 2150 | pWal->hdr.mxFrame = aWalData[0]; |
| 2151 | pWal->hdr.aFrameCksum[0] = aWalData[1]; |
| 2152 | pWal->hdr.aFrameCksum[1] = aWalData[2]; |
dan | 5d65685 | 2010-06-14 07:53:26 +0000 | [diff] [blame] | 2153 | walCleanupHash(pWal); |
dan | 6f15014 | 2010-05-21 15:31:56 +0000 | [diff] [blame] | 2154 | } |
dan | 6e6bd56 | 2010-06-02 18:59:03 +0000 | [diff] [blame] | 2155 | |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 2156 | return rc; |
| 2157 | } |
| 2158 | |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2159 | /* |
| 2160 | ** This function is called just before writing a set of frames to the log |
| 2161 | ** file (see sqlite3WalFrames()). It checks to see if, instead of appending |
| 2162 | ** to the current log file, it is possible to overwrite the start of the |
| 2163 | ** existing log file with the new frames (i.e. "reset" the log). If so, |
| 2164 | ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left |
| 2165 | ** unchanged. |
| 2166 | ** |
| 2167 | ** SQLITE_OK is returned if no error is encountered (regardless of whether |
| 2168 | ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned |
| 2169 | ** if some error |
| 2170 | */ |
| 2171 | static int walRestartLog(Wal *pWal){ |
| 2172 | int rc = SQLITE_OK; |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 2173 | int cnt; |
| 2174 | |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 2175 | if( pWal->readLock==0 ){ |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2176 | volatile WalCkptInfo *pInfo = walCkptInfo(pWal); |
| 2177 | assert( pInfo->nBackfill==pWal->hdr.mxFrame ); |
| 2178 | if( pInfo->nBackfill>0 ){ |
| 2179 | rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); |
| 2180 | if( rc==SQLITE_OK ){ |
| 2181 | /* If all readers are using WAL_READ_LOCK(0) (in other words if no |
| 2182 | ** readers are currently using the WAL), then the transactions |
| 2183 | ** frames will overwrite the start of the existing log. Update the |
| 2184 | ** wal-index header to reflect this. |
| 2185 | ** |
| 2186 | ** In theory it would be Ok to update the cache of the header only |
| 2187 | ** at this point. But updating the actual wal-index header is also |
| 2188 | ** safe and means there is no special case for sqlite3WalUndo() |
| 2189 | ** to handle if this transaction is rolled back. |
| 2190 | */ |
dan | 199100e | 2010-06-09 16:58:49 +0000 | [diff] [blame] | 2191 | int i; /* Loop counter */ |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2192 | u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */ |
| 2193 | pWal->nCkpt++; |
| 2194 | pWal->hdr.mxFrame = 0; |
| 2195 | sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); |
| 2196 | sqlite3_randomness(4, &aSalt[1]); |
| 2197 | walIndexWriteHdr(pWal); |
dan | 199100e | 2010-06-09 16:58:49 +0000 | [diff] [blame] | 2198 | pInfo->nBackfill = 0; |
| 2199 | for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED; |
| 2200 | assert( pInfo->aReadMark[0]==0 ); |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2201 | walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); |
| 2202 | } |
| 2203 | } |
| 2204 | walUnlockShared(pWal, WAL_READ_LOCK(0)); |
| 2205 | pWal->readLock = -1; |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 2206 | cnt = 0; |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2207 | do{ |
| 2208 | int notUsed; |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 2209 | rc = walTryBeginRead(pWal, ¬Used, 1, ++cnt); |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2210 | }while( rc==WAL_RETRY ); |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2211 | } |
| 2212 | return rc; |
| 2213 | } |
| 2214 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2215 | /* |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 2216 | ** Write a set of frames to the log. The caller must hold the write-lock |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2217 | ** on the log file (obtained using sqlite3WalBeginWriteTransaction()). |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2218 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 2219 | int sqlite3WalFrames( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2220 | Wal *pWal, /* Wal handle to write to */ |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 2221 | int szPage, /* Database page-size in bytes */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2222 | PgHdr *pList, /* List of dirty pages to write */ |
| 2223 | Pgno nTruncate, /* Database size after this commit */ |
| 2224 | int isCommit, /* True if this is a commit */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 2225 | int sync_flags /* Flags to pass to OsSync() (or 0) */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2226 | ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2227 | int rc; /* Used to catch return codes */ |
| 2228 | u32 iFrame; /* Next frame address */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2229 | u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2230 | PgHdr *p; /* Iterator to run through pList with. */ |
drh | e874d9e | 2010-05-07 20:02:23 +0000 | [diff] [blame] | 2231 | PgHdr *pLast = 0; /* Last frame in list */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2232 | int nLast = 0; /* Number of extra copies of last page */ |
| 2233 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2234 | assert( pList ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2235 | assert( pWal->writeLock ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2236 | |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 2237 | #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 2238 | { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){} |
| 2239 | WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n", |
| 2240 | pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill")); |
| 2241 | } |
| 2242 | #endif |
| 2243 | |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2244 | /* See if it is possible to write these frames into the start of the |
| 2245 | ** log file, instead of appending to it at pWal->hdr.mxFrame. |
| 2246 | */ |
| 2247 | if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){ |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2248 | return rc; |
| 2249 | } |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2250 | |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 2251 | /* If this is the first frame written into the log, write the WAL |
| 2252 | ** header to the start of the WAL file. See comments at the top of |
| 2253 | ** this source file for a description of the WAL header format. |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 2254 | */ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 2255 | iFrame = pWal->hdr.mxFrame; |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 2256 | if( iFrame==0 ){ |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 2257 | u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assembly wal-header in */ |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 2258 | sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN)); |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 2259 | sqlite3Put4byte(&aWalHdr[4], 3007000); |
| 2260 | sqlite3Put4byte(&aWalHdr[8], szPage); |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 2261 | pWal->szPage = szPage; |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 2262 | pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN; |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 2263 | sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt); |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 2264 | memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8); |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 2265 | rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0); |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 2266 | WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok")); |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 2267 | if( rc!=SQLITE_OK ){ |
| 2268 | return rc; |
| 2269 | } |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 2270 | walChecksumBytes(1, aWalHdr, sizeof(aWalHdr), 0, pWal->hdr.aFrameCksum); |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 2271 | } |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 2272 | assert( pWal->szPage==szPage ); |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 2273 | |
dan | 9971e71 | 2010-06-01 15:44:57 +0000 | [diff] [blame] | 2274 | /* Write the log file. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2275 | for(p=pList; p; p=p->pDirty){ |
| 2276 | u32 nDbsize; /* Db-size field for frame header */ |
| 2277 | i64 iOffset; /* Write offset in log file */ |
| 2278 | |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 2279 | iOffset = walFrameOffset(++iFrame, szPage); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2280 | |
| 2281 | /* Populate and write the frame header */ |
| 2282 | nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0; |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 2283 | walEncodeFrame(pWal, p->pgno, nDbsize, p->pData, aFrame); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 2284 | rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2285 | if( rc!=SQLITE_OK ){ |
| 2286 | return rc; |
| 2287 | } |
| 2288 | |
| 2289 | /* Write the page data */ |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 2290 | rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOffset+sizeof(aFrame)); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2291 | if( rc!=SQLITE_OK ){ |
| 2292 | return rc; |
| 2293 | } |
| 2294 | pLast = p; |
| 2295 | } |
| 2296 | |
| 2297 | /* Sync the log file if the 'isSync' flag was specified. */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 2298 | if( sync_flags ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 2299 | i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd); |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 2300 | i64 iOffset = walFrameOffset(iFrame+1, szPage); |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 2301 | |
| 2302 | assert( isCommit ); |
drh | 69c4696 | 2010-05-17 20:16:50 +0000 | [diff] [blame] | 2303 | assert( iSegment>0 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2304 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2305 | iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment); |
| 2306 | while( iOffset<iSegment ){ |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 2307 | walEncodeFrame(pWal, pLast->pgno, nTruncate, pLast->pData, aFrame); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 2308 | rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2309 | if( rc!=SQLITE_OK ){ |
| 2310 | return rc; |
| 2311 | } |
| 2312 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2313 | iOffset += WAL_FRAME_HDRSIZE; |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 2314 | rc = sqlite3OsWrite(pWal->pWalFd, pLast->pData, szPage, iOffset); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2315 | if( rc!=SQLITE_OK ){ |
| 2316 | return rc; |
| 2317 | } |
| 2318 | nLast++; |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 2319 | iOffset += szPage; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2320 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2321 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 2322 | rc = sqlite3OsSync(pWal->pWalFd, sync_flags); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
drh | e730fec | 2010-05-18 12:56:50 +0000 | [diff] [blame] | 2325 | /* Append data to the wal-index. It is not necessary to lock the |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 2326 | ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2327 | ** guarantees that there are no other writers, and no data that may |
| 2328 | ** be in use by existing readers is being overwritten. |
| 2329 | */ |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 2330 | iFrame = pWal->hdr.mxFrame; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2331 | for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2332 | iFrame++; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2333 | rc = walIndexAppend(pWal, iFrame, p->pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2334 | } |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2335 | while( nLast>0 && rc==SQLITE_OK ){ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2336 | iFrame++; |
| 2337 | nLast--; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2338 | rc = walIndexAppend(pWal, iFrame, pLast->pgno); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2341 | if( rc==SQLITE_OK ){ |
| 2342 | /* Update the private copy of the header. */ |
drh | 6e81096 | 2010-05-19 17:49:50 +0000 | [diff] [blame] | 2343 | pWal->hdr.szPage = szPage; |
drh | 027a128 | 2010-05-19 01:53:53 +0000 | [diff] [blame] | 2344 | pWal->hdr.mxFrame = iFrame; |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2345 | if( isCommit ){ |
| 2346 | pWal->hdr.iChange++; |
| 2347 | pWal->hdr.nPage = nTruncate; |
| 2348 | } |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2349 | /* If this is a commit, update the wal-index header too. */ |
| 2350 | if( isCommit ){ |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 2351 | walIndexWriteHdr(pWal); |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2352 | pWal->iCallback = iFrame; |
| 2353 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2354 | } |
dan | c7991bd | 2010-05-05 19:04:59 +0000 | [diff] [blame] | 2355 | |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 2356 | WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok")); |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 2357 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2361 | ** This routine is called to implement sqlite3_wal_checkpoint() and |
| 2362 | ** related interfaces. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 2363 | ** |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2364 | ** Obtain a CHECKPOINT lock and then backfill as much information as |
| 2365 | ** we can from WAL into the database. |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2366 | */ |
drh | c438efd | 2010-04-26 00:19:45 +0000 | [diff] [blame] | 2367 | int sqlite3WalCheckpoint( |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2368 | Wal *pWal, /* Wal connection */ |
dan | c511878 | 2010-04-17 17:34:41 +0000 | [diff] [blame] | 2369 | int sync_flags, /* Flags to sync db file with (or 0) */ |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 2370 | int nBuf, /* Size of temporary buffer */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2371 | u8 *zBuf /* Temporary buffer to use */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2372 | ){ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 2373 | int rc; /* Return code */ |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 2374 | int isChanged = 0; /* True if a new wal-index header is loaded */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2375 | |
dan | d54ff60 | 2010-05-31 11:16:30 +0000 | [diff] [blame] | 2376 | assert( pWal->ckptLock==0 ); |
dan | 39c79f5 | 2010-04-15 10:58:51 +0000 | [diff] [blame] | 2377 | |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 2378 | WALTRACE(("WAL%p: checkpoint begins\n", pWal)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2379 | rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); |
| 2380 | if( rc ){ |
| 2381 | /* Usually this is SQLITE_BUSY meaning that another thread or process |
| 2382 | ** is already running a checkpoint, or maybe a recovery. But it might |
| 2383 | ** also be SQLITE_IOERR. */ |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 2384 | return rc; |
| 2385 | } |
dan | d54ff60 | 2010-05-31 11:16:30 +0000 | [diff] [blame] | 2386 | pWal->ckptLock = 1; |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 2387 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 2388 | /* Copy data from the log to the database file. */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2389 | rc = walIndexReadHdr(pWal, &isChanged); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 2390 | if( rc==SQLITE_OK ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 2391 | rc = walCheckpoint(pWal, sync_flags, nBuf, zBuf); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 2392 | } |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 2393 | if( isChanged ){ |
| 2394 | /* If a new wal-index header was loaded before the checkpoint was |
drh | a2a4201 | 2010-05-18 18:01:08 +0000 | [diff] [blame] | 2395 | ** performed, then the pager-cache associated with pWal is now |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 2396 | ** out of date. So zero the cached wal-index header to ensure that |
| 2397 | ** next time the pager opens a snapshot on this database it knows that |
| 2398 | ** the cache needs to be reset. |
| 2399 | */ |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2400 | memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 2401 | } |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 2402 | |
| 2403 | /* Release the locks. */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 2404 | walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1); |
dan | d54ff60 | 2010-05-31 11:16:30 +0000 | [diff] [blame] | 2405 | pWal->ckptLock = 0; |
drh | c74c333 | 2010-05-31 12:15:19 +0000 | [diff] [blame] | 2406 | WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok")); |
dan | 64d039e | 2010-04-13 19:27:31 +0000 | [diff] [blame] | 2407 | return rc; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2410 | /* Return the value to pass to a sqlite3_wal_hook callback, the |
| 2411 | ** number of frames in the WAL at the point of the last commit since |
| 2412 | ** sqlite3WalCallback() was called. If no commits have occurred since |
| 2413 | ** the last call, then return 0. |
| 2414 | */ |
| 2415 | int sqlite3WalCallback(Wal *pWal){ |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 2416 | u32 ret = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2417 | if( pWal ){ |
| 2418 | ret = pWal->iCallback; |
| 2419 | pWal->iCallback = 0; |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 2420 | } |
| 2421 | return (int)ret; |
| 2422 | } |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 2423 | |
| 2424 | /* |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2425 | ** This function is called to change the WAL subsystem into or out |
| 2426 | ** of locking_mode=EXCLUSIVE. |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 2427 | ** |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2428 | ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE |
| 2429 | ** into locking_mode=NORMAL. This means that we must acquire a lock |
| 2430 | ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL |
| 2431 | ** or if the acquisition of the lock fails, then return 0. If the |
| 2432 | ** transition out of exclusive-mode is successful, return 1. This |
| 2433 | ** operation must occur while the pager is still holding the exclusive |
| 2434 | ** lock on the main database file. |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 2435 | ** |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2436 | ** If op is one, then change from locking_mode=NORMAL into |
| 2437 | ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must |
| 2438 | ** be released. Return 1 if the transition is made and 0 if the |
| 2439 | ** WAL is already in exclusive-locking mode - meaning that this |
| 2440 | ** routine is a no-op. The pager must already hold the exclusive lock |
| 2441 | ** on the main database file before invoking this operation. |
| 2442 | ** |
| 2443 | ** If op is negative, then do a dry-run of the op==1 case but do |
| 2444 | ** not actually change anything. The pager uses this to see if it |
| 2445 | ** should acquire the database exclusive lock prior to invoking |
| 2446 | ** the op==1 case. |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 2447 | */ |
| 2448 | int sqlite3WalExclusiveMode(Wal *pWal, int op){ |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2449 | int rc; |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 2450 | assert( pWal->writeLock==0 ); |
dan | 3cac5dc | 2010-06-04 18:37:59 +0000 | [diff] [blame] | 2451 | |
| 2452 | /* pWal->readLock is usually set, but might be -1 if there was a |
| 2453 | ** prior error while attempting to acquire are read-lock. This cannot |
| 2454 | ** happen if the connection is actually in exclusive mode (as no xShmLock |
| 2455 | ** locks are taken in this case). Nor should the pager attempt to |
| 2456 | ** upgrade to exclusive-mode following such an error. |
| 2457 | */ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 2458 | assert( pWal->readLock>=0 || pWal->lockError ); |
dan | 3cac5dc | 2010-06-04 18:37:59 +0000 | [diff] [blame] | 2459 | assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) ); |
| 2460 | |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2461 | if( op==0 ){ |
| 2462 | if( pWal->exclusiveMode ){ |
| 2463 | pWal->exclusiveMode = 0; |
dan | 3cac5dc | 2010-06-04 18:37:59 +0000 | [diff] [blame] | 2464 | if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){ |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2465 | pWal->exclusiveMode = 1; |
| 2466 | } |
| 2467 | rc = pWal->exclusiveMode==0; |
| 2468 | }else{ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 2469 | /* Already in locking_mode=NORMAL */ |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2470 | rc = 0; |
| 2471 | } |
| 2472 | }else if( op>0 ){ |
| 2473 | assert( pWal->exclusiveMode==0 ); |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 2474 | assert( pWal->readLock>=0 ); |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2475 | walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); |
| 2476 | pWal->exclusiveMode = 1; |
| 2477 | rc = 1; |
| 2478 | }else{ |
| 2479 | rc = pWal->exclusiveMode==0; |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 2480 | } |
drh | 61e4ace | 2010-05-31 20:28:37 +0000 | [diff] [blame] | 2481 | return rc; |
dan | 5543759 | 2010-05-11 12:19:26 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 2484 | #endif /* #ifndef SQLITE_OMIT_WAL */ |