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